]> WPIA git - gigi.git/blob - src/org/cacert/gigi/email/EmailProvider.java
Format code according do BenBE's formatter.
[gigi.git] / src / org / cacert / gigi / email / EmailProvider.java
1 package org.cacert.gigi.email;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.io.PrintWriter;
7 import java.net.Socket;
8 import java.sql.PreparedStatement;
9 import java.sql.SQLException;
10 import java.util.LinkedList;
11 import java.util.Properties;
12 import java.util.regex.Pattern;
13
14 import org.cacert.gigi.database.DatabaseConnection;
15
16 public abstract class EmailProvider {
17
18     public abstract void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException;
19
20     private static EmailProvider instance;
21
22     public static EmailProvider getInstance() {
23         return instance;
24     }
25
26     protected static void setInstance(EmailProvider instance) {
27         EmailProvider.instance = instance;
28     }
29
30     public static void init(Properties conf) {
31         try {
32             Class<?> c = Class.forName(conf.getProperty("emailProvider"));
33             instance = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
34         } catch (ReflectiveOperationException e) {
35             e.printStackTrace();
36         }
37     }
38
39     public static final String OK = "OK";
40
41     public static final String FAIL = "FAIL";
42
43     public static final Pattern MAIL = Pattern.compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
44
45     public String checkEmailServer(int forUid, String address) throws IOException {
46         if (MAIL.matcher(address).matches()) {
47             String[] parts = address.split("@", 2);
48             String domain = parts[1];
49
50             LinkedList<String> mxhosts = getMxHosts(domain);
51
52             for (String host : mxhosts) {
53                 try (Socket s = new Socket(host, 25); BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw = new PrintWriter(s.getOutputStream())) {
54                     String line;
55                     while ((line = br.readLine()) != null && line.startsWith("220-")) {
56                     }
57                     if (line == null || !line.startsWith("220")) {
58                         continue;
59                     }
60
61                     pw.print("HELO www.cacert.org\r\n");
62                     pw.flush();
63
64                     while ((line = br.readLine()) != null && line.startsWith("220")) {
65                     }
66
67                     if (line == null || !line.startsWith("250")) {
68                         continue;
69                     }
70                     pw.print("MAIL FROM: <returns@cacert.org>\r\n");
71                     pw.flush();
72
73                     line = br.readLine();
74
75                     if (line == null || !line.startsWith("250")) {
76                         continue;
77                     }
78                     pw.print("RCPT TO: <" + address + ">\r\n");
79                     pw.flush();
80
81                     line = br.readLine();
82                     pw.print("QUIT\r\n");
83                     pw.flush();
84
85                     try {
86                         PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
87                         statmt.setString(1, address);
88                         statmt.setString(2, line);
89                         statmt.setInt(3, forUid);
90                         statmt.execute();
91                     } catch (SQLException e) {
92                         e.printStackTrace();
93                     }
94
95                     if (line == null || !line.startsWith("250")) {
96                         return line;
97                     } else {
98                         return OK;
99                     }
100                 }
101
102             }
103         }
104         try {
105             PreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
106             statmt.setString(1, address);
107             statmt.setString(2, "Failed to make a connection to the mail server");
108             statmt.setInt(3, forUid);
109             statmt.execute();
110         } catch (SQLException e) {
111             e.printStackTrace();
112         }
113         return FAIL;
114     }
115
116     private static LinkedList<String> getMxHosts(String domain) throws IOException {
117         LinkedList<String> mxhosts = new LinkedList<String>();
118         Process dig = Runtime.getRuntime().exec(new String[] {
119                 "dig", "+short", "MX", domain
120         });
121         try (BufferedReader br = new BufferedReader(new InputStreamReader(dig.getInputStream()))) {
122             String line;
123             while ((line = br.readLine()) != null) {
124                 String[] mxparts = line.split(" ", 2);
125                 if (mxparts.length != 2) {
126                     continue;
127                 }
128                 mxhosts.add(mxparts[1].substring(0, mxparts[1].length() - 1));
129             }
130         }
131         return mxhosts;
132     }
133 }