X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Femail%2FEmailProvider.java;h=644c46218709f6fc8d28e6551c093d1fceae12ec;hb=78c6b04860c66d34be008b6766b83c2f594b9909;hp=b5ac8d5ca88f1b58322e018dd1fb15ab54d0b718;hpb=634b7f75c8fc2ed8799bad74731278fb59198c48;p=gigi.git diff --git a/src/org/cacert/gigi/email/EmailProvider.java b/src/org/cacert/gigi/email/EmailProvider.java index b5ac8d5c..644c4621 100644 --- a/src/org/cacert/gigi/email/EmailProvider.java +++ b/src/org/cacert/gigi/email/EmailProvider.java @@ -1,7 +1,17 @@ package org.cacert.gigi.email; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.Socket; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.LinkedList; import java.util.Properties; +import java.util.regex.Pattern; + +import org.cacert.gigi.database.DatabaseConnection; public abstract class EmailProvider { public abstract void sendmail(String to, String subject, String message, @@ -20,4 +30,111 @@ public abstract class EmailProvider { e.printStackTrace(); } } + + public static final String OK = "OK"; + public static final String FAIL = "FAIL"; + private static final Pattern MAIL = Pattern + .compile("^([a-zA-Z0-9])+([a-zA-Z0-9\\+\\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\\._-]+)+$"); + + public String checkEmailServer(int forUid, String address) + throws IOException { + if (MAIL.matcher(address).matches()) { + String[] parts = address.split("@", 2); + String domain = parts[1]; + + LinkedList mxhosts = getMxHosts(domain); + + for (String host : mxhosts) { + try (Socket s = new Socket(host, 25); + BufferedReader br = new BufferedReader( + new InputStreamReader(s.getInputStream())); + PrintWriter pw = new PrintWriter(s.getOutputStream())) { + String line; + while ((line = br.readLine()) != null + && line.startsWith("220-")) { + } + if (line == null || !line.startsWith("220")) { + continue; + } + + pw.print("HELO www.cacert.org\r\n"); + pw.flush(); + + while ((line = br.readLine()) != null + && line.startsWith("220")) { + } + + if (line == null || !line.startsWith("250")) { + continue; + } + pw.print("MAIL FROM: \r\n"); + pw.flush(); + + line = br.readLine(); + + if (line == null || !line.startsWith("250")) { + continue; + } + pw.print("RCPT TO: <" + address + ">\r\n"); + pw.flush(); + + line = br.readLine(); + pw.print("QUIT\r\n"); + pw.flush(); + + try { + PreparedStatement statmt = DatabaseConnection + .getInstance() + .prepare( + "insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?"); + statmt.setString(1, address); + statmt.setString(2, line); + statmt.setInt(3, forUid); + statmt.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } + + if (line == null || !line.startsWith("250")) { + return line; + } else { + return OK; + } + } + + } + } + try { + PreparedStatement statmt = DatabaseConnection + .getInstance() + .prepare( + "insert into `pinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?"); + statmt.setString(1, address); + statmt.setString(2, + "Failed to make a connection to the mail server"); + statmt.setInt(3, forUid); + statmt.execute(); + } catch (SQLException e) { + e.printStackTrace(); + } + return FAIL; + } + private static LinkedList getMxHosts(String domain) + throws IOException { + LinkedList mxhosts = new LinkedList(); + Process dig = Runtime.getRuntime().exec( + new String[]{"dig", "+short", "MX", domain}); + try (BufferedReader br = new BufferedReader(new InputStreamReader( + dig.getInputStream()))) { + String line; + while ((line = br.readLine()) != null) { + String[] mxparts = line.split(" ", 2); + if (mxparts.length != 2) { + continue; + } + mxhosts.add(mxparts[1].substring(0, mxparts[1].length() - 1)); + } + } + return mxhosts; + } }