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