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 {
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;
20 private static EmailProvider instance;
22 public static EmailProvider getInstance() {
26 protected static void setInstance(EmailProvider instance) {
27 EmailProvider.instance = instance;
30 public static void init(Properties conf) {
32 Class<?> c = Class.forName(conf.getProperty("emailProvider"));
33 instance = (EmailProvider) c.getDeclaredConstructor(Properties.class).newInstance(conf);
34 } catch (ReflectiveOperationException e) {
39 public static final String OK = "OK";
41 public static final String FAIL = "FAIL";
43 public static final Pattern MAIL = Pattern.compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$");
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];
50 LinkedList<String> mxhosts = getMxHosts(domain);
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())) {
55 while ((line = br.readLine()) != null && line.startsWith("220-")) {
57 if (line == null || !line.startsWith("220")) {
61 pw.print("HELO www.cacert.org\r\n");
64 while ((line = br.readLine()) != null && 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.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);
91 } catch (SQLException e) {
95 if (line == null || !line.startsWith("250")) {
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);
110 } catch (SQLException e) {
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
121 try (BufferedReader br = new BufferedReader(new InputStreamReader(dig.getInputStream()))) {
123 while ((line = br.readLine()) != null) {
124 String[] mxparts = line.split(" ", 2);
125 if (mxparts.length != 2) {
128 mxhosts.add(mxparts[1].substring(0, mxparts[1].length() - 1));