]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/email/EmailProvider.java
upd: rename sendmail to sendMail
[gigi.git] / src / org / cacert / gigi / email / EmailProvider.java
index 49a27356061062cc06f2d58f7eaba0dabee0eb5c..ed61aeca2f6cf3ddab0c35f43c7a1e25ebb04031 100644 (file)
@@ -3,6 +3,7 @@ package org.cacert.gigi.email;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.net.Socket;
 import java.security.GeneralSecurityException;
@@ -19,13 +20,12 @@ import javax.naming.NamingException;
 import javax.net.ssl.SSLSocketFactory;
 
 import org.cacert.gigi.crypto.SMIME;
-import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.util.DNSUtil;
 
 public abstract class EmailProvider {
 
-    public abstract void sendmail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException;
+    public abstract void sendMail(String to, String subject, String message, String from, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException;
 
     private static EmailProvider instance;
 
@@ -33,13 +33,19 @@ public abstract class EmailProvider {
 
     private PrivateKey k;
 
-    protected final void init(Certificate c, Key k) {
+    protected void init(Certificate c, Key k) {
         this.c = (X509Certificate) c;
         this.k = (PrivateKey) k;
     }
 
     protected final void sendSigned(String contents, PrintWriter output) throws IOException, GeneralSecurityException {
-        SMIME.smime(contents, k, c, output);
+        if (k == null || c == null) {
+            output.println("Content-Transfer-Encoding: base64");
+            output.println();
+            output.print(contents);
+        } else {
+            SMIME.smime(contents, k, c, output);
+        }
     }
 
     public static EmailProvider getInstance() {
@@ -87,11 +93,13 @@ public abstract class EmailProvider {
                 } else {
                     return "Strange MX records.";
                 }
-                try (Socket s = new Socket(host, 25); BufferedReader br0 = new BufferedReader(new InputStreamReader(s.getInputStream())); PrintWriter pw0 = new PrintWriter(s.getOutputStream())) {
+                try (Socket s = new Socket(host, 25);
+                        BufferedReader br0 = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));//
+                        PrintWriter pw0 = new PrintWriter(new OutputStreamWriter(s.getOutputStream(), "UTF-8"))) {
                     BufferedReader br = br0;
                     PrintWriter pw = pw0;
                     String line;
-                    if ( !Sendmail.readSMTPResponse(br, 220)) {
+                    if ( !SendMail.readSMTPResponse(br, 220)) {
                         continue;
                     }
 
@@ -100,8 +108,9 @@ public abstract class EmailProvider {
                     boolean starttls = false;
                     do {
                         line = br.readLine();
-                        if (line == null)
+                        if (line == null) {
                             break;
+                        }
                         starttls |= line.substring(4).equals("STARTTLS");
                     } while (line.startsWith("250-"));
                     if (line == null || !line.startsWith("250 ")) {
@@ -111,15 +120,15 @@ public abstract class EmailProvider {
                     if (starttls) {
                         pw.print("STARTTLS\r\n");
                         pw.flush();
-                        if ( !Sendmail.readSMTPResponse(br, 220)) {
+                        if ( !SendMail.readSMTPResponse(br, 220)) {
                             continue;
                         }
                         Socket s1 = ((SSLSocketFactory) SSLSocketFactory.getDefault()).createSocket(s, host, 25, true);
-                        br = new BufferedReader(new InputStreamReader(s1.getInputStream()));
-                        pw = new PrintWriter(s1.getOutputStream());
+                        br = new BufferedReader(new InputStreamReader(s1.getInputStream(), "UTF-8"));
+                        pw = new PrintWriter(new OutputStreamWriter(s1.getOutputStream(), "UTF-8"));
                         pw.print("EHLO www.cacert.org\r\n");
                         pw.flush();
-                        if ( !Sendmail.readSMTPResponse(br, 250)) {
+                        if ( !SendMail.readSMTPResponse(br, 250)) {
                             continue;
                         }
                     }
@@ -127,26 +136,28 @@ public abstract class EmailProvider {
                     pw.print("MAIL FROM: <returns@cacert.org>\r\n");
                     pw.flush();
 
-                    if ( !Sendmail.readSMTPResponse(br, 250)) {
+                    if ( !SendMail.readSMTPResponse(br, 250)) {
                         continue;
                     }
                     pw.print("RCPT TO: <" + address + ">\r\n");
                     pw.flush();
 
-                    if ( !Sendmail.readSMTPResponse(br, 250)) {
+                    if ( !SendMail.readSMTPResponse(br, 250)) {
                         continue;
                     }
                     pw.print("QUIT\r\n");
                     pw.flush();
-                    if ( !Sendmail.readSMTPResponse(br, 221)) {
+                    if ( !SendMail.readSMTPResponse(br, 221)) {
                         continue;
                     }
 
-                    GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `emailPinglog` set `when`=NOW(), `email`=?, `result`=?, `uid`=?");
-                    statmt.setString(1, address);
-                    statmt.setString(2, line);
-                    statmt.setInt(3, forUid);
-                    statmt.execute();
+                    try (GigiPreparedStatement statmt = new GigiPreparedStatement("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`=?, `uid`=?, `type`='fast', `status`=?::`pingState`")) {
+                        statmt.setString(1, address);
+                        statmt.setString(2, line);
+                        statmt.setInt(3, forUid);
+                        statmt.setString(4, "success");
+                        statmt.execute();
+                    }
 
                     if (line == null || !line.startsWith("250")) {
                         return line;
@@ -157,11 +168,13 @@ public abstract class EmailProvider {
 
             }
         }
-        GigiPreparedStatement statmt = DatabaseConnection.getInstance().prepare("insert into `emailPinglog` 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();
+        try (GigiPreparedStatement statmt = new GigiPreparedStatement("INSERT INTO `emailPinglog` SET `when`=NOW(), `email`=?, `result`=?, `uid`=?, `type`='fast', `status`=?::`pingState`")) {
+            statmt.setString(1, address);
+            statmt.setString(2, "Failed to make a connection to the mail server");
+            statmt.setInt(3, forUid);
+            statmt.setString(4, "failed");
+            statmt.execute();
+        }
         return FAIL;
     }