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, String from, String replyto,
18 String toname, String fromname, String errorsto, boolean extra) throws IOException;
20 private static EmailProvider instance;
22 public static EmailProvider getInstance() {
26 public static void init(Properties conf) {
28 Class<?> c = Class.forName(conf.getProperty("emailProvider"));
29 instance = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
30 } catch (ReflectiveOperationException e) {
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\\._-]+)+$");
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];
45 LinkedList<String> mxhosts = getMxHosts(domain);
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())) {
52 while ((line = br.readLine()) != null && line.startsWith("220-")) {
54 if (line == null || !line.startsWith("220")) {
58 pw.print("HELO www.cacert.org\r\n");
61 while ((line = br.readLine()) != null && line.startsWith("220")) {
64 if (line == null || !line.startsWith("250")) {
67 pw.print("MAIL FROM: <returns@cacert.org>\r\n");
72 if (line == null || !line.startsWith("250")) {
75 pw.print("RCPT TO: <" + address + ">\r\n");
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);
89 } catch (SQLException e) {
93 if (line == null || !line.startsWith("250")) {
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);
109 } catch (SQLException e) {
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()))) {
120 while ((line = br.readLine()) != null) {
121 String[] mxparts = line.split(" ", 2);
122 if (mxparts.length != 2) {
125 mxhosts.add(mxparts[1].substring(0, mxparts[1].length() - 1));