1 package org.cacert.gigi.email;
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;
14 import org.cacert.gigi.database.DatabaseConnection;
16 public abstract class EmailProvider {
17 public abstract void sendmail(String to, String subject, String message,
18 String from, String replyto, String toname, String fromname,
19 String errorsto, boolean extra) throws IOException;
20 private static EmailProvider instance;
21 public static EmailProvider getInstance() {
24 public static void init(Properties conf) {
26 Class<?> c = Class.forName(conf.getProperty("emailProvider"));
27 instance = (EmailProvider) c.getDeclaredConstructor(
28 Properties.class).newInstance(conf);
29 } catch (ReflectiveOperationException e) {
34 public static final String OK = "OK";
35 public static final String FAIL = "FAIL";
36 private static final Pattern MAIL = Pattern
37 .compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
39 public String checkEmailServer(int forUid, String address)
41 if (MAIL.matcher(address).matches()) {
42 String[] parts = address.split("@", 2);
43 String domain = parts[1];
45 LinkedList<String> mxhosts = getMxHosts(domain);
47 for (String host : mxhosts) {
48 try (Socket s = new Socket(host, 25);
49 BufferedReader br = new BufferedReader(
50 new InputStreamReader(s.getInputStream()));
51 PrintWriter pw = new PrintWriter(s.getOutputStream())) {
53 while ((line = br.readLine()) != null
54 && line.startsWith("220-")) {
56 if (line == null || !line.startsWith("220")) {
60 pw.print("HELO www.cacert.org\r\n");
63 while ((line = br.readLine()) != null
64 && line.startsWith("220")) {
67 if (line == null || !line.startsWith("250")) {
70 pw.print("MAIL FROM: <returns@cacert.org>\r\n");
75 if (line == null || !line.startsWith("250")) {
78 pw.print("RCPT TO: <" + address + ">\r\n");
86 PreparedStatement statmt = DatabaseConnection
89 "insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
90 statmt.setString(1, address);
91 statmt.setString(2, line);
92 statmt.setInt(3, forUid);
94 } catch (SQLException e) {
98 if (line == null || !line.startsWith("250")) {
108 PreparedStatement statmt = DatabaseConnection
111 "insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
112 statmt.setString(1, address);
114 "Failed to make a connection to the mail server");
115 statmt.setInt(3, forUid);
117 } catch (SQLException e) {
122 private static LinkedList<String> getMxHosts(String domain)
124 LinkedList<String> mxhosts = new LinkedList<String>();
125 Process dig = Runtime.getRuntime().exec(
126 new String[]{"dig", "+short", "MX", domain});
127 try (BufferedReader br = new BufferedReader(new InputStreamReader(
128 dig.getInputStream()))) {
130 while ((line = br.readLine()) != null) {
131 String[] mxparts = line.split(" ", 2);
132 if (mxparts.length != 2) {
135 mxhosts.add(mxparts[1].substring(0, mxparts[1].length() - 1));