SqliteTable: fixups to schema handling
[org.ibex.mail.git] / src / org / ibex / mail / SMTP.java
index 0ae8890..a426987 100644 (file)
@@ -14,6 +14,8 @@ import java.text.*;
 import javax.naming.*;
 import javax.naming.directory.*;
 
+// FIXME: inbound throttling/ratelimiting
+
 // RFC's implemented
 // RFC2554: SMTP Service Extension for Authentication
 //     - did not implement section 5, though
@@ -44,17 +46,22 @@ public class SMTP {
     public static final int GRAYLIST_MINWAIT =  1000 * 60 * 60;           // one hour
     public static final int GRAYLIST_MAXWAIT =  1000 * 60 * 60 * 24 * 5;  // five days
 
-    public static final Graylist graylist =
-        new Graylist(Mailbox.STORAGE_ROOT+"/db/graylist.sqlite");
-
-    public static final Whitelist whitelist =
-        new Whitelist(Mailbox.STORAGE_ROOT+"/db/whitelist.sqlite");
+    public static final Graylist graylist;
+    public static final Whitelist whitelist;
+    static {
+        try {
+            graylist =  new Graylist(Mailbox.STORAGE_ROOT+"/db/graylist.sqlite");
+            whitelist = new Whitelist(Mailbox.STORAGE_ROOT+"/db/whitelist.sqlite");
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
 
     public static final int MAX_MESSAGE_SIZE =
         Integer.parseInt(System.getProperty("org.ibex.mail.smtp.maxMessageSize", "-1"));
 
     private static final Mailbox spool =
-        FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT,false).slash("spool",true).slash("smtp",true);
+        FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT,false).slash("spool",true).slash("smtp",true).getMailbox();
 
     static {
         for(int i=0; i<numOutgoingThreads; i++)
@@ -174,6 +181,7 @@ public class SMTP {
                     command = command.substring(10).trim();
                     from = command.equals("<>") ? null : new Address(command);
                     conn.println("250 " + from + " is syntactically correct");
+                    // FEATURE: perform SMTP validation on the address, reject if invalid
                 } else if (c.startsWith("RCPT TO:")) {
                     // some clients are broken and put RCPT first; we will tolerate this
                     command = command.substring(8).trim();
@@ -249,7 +257,7 @@ public class SMTP {
                             if (s.equals(".")) break;
                             if (s.startsWith(".")) s = s.substring(1);
                             buf.append(s + "\r\n");
-                            if (MAX_MESSAGE_SIZE != -1 && buf.length() > MAX_MESSAGE_SIZE) {
+                            if (MAX_MESSAGE_SIZE != -1 && buf.length() > MAX_MESSAGE_SIZE && (from+"").indexOf("paperless")==-1) {
                                 Log.error("**"+conn.getRemoteAddress()+"**",
                                           "sorry, this mail server only accepts messages of less than " +
                                           ByteSize.toString(MAX_MESSAGE_SIZE));
@@ -257,12 +265,10 @@ public class SMTP {
                                                                   ByteSize.toString(MAX_MESSAGE_SIZE));
                             }
                         }
-                        String body = buf.toString();
+                        String message = buf.toString();
                         Message m = null;
-                        for(int i=0; i<to.size(); i++) {
-                           m = Message.newMessage(Fountain.Util.create(body), from, (Address)to.elementAt(i));
-                            enqueue(m);
-                       }
+                        for(int i=0; i<to.size(); i++)
+                            enqueue(m = Message.newMessage(Fountain.Util.create(message)).withEnvelope(from, (Address)to.elementAt(i)));
                         if (m != null) Log.info(SMTP.class, "accepted message: " + m.summary());
                         conn.println("250 message accepted");
                         conn.flush();
@@ -331,7 +337,7 @@ public class SMTP {
             }
             for(int i=0; i<mx.length; i++) {
                 //if (deadHosts.contains(mx[i])) continue;
-                if (attempt(m, mx[i])) { return true; }
+                if (attempt(m, mx[i])) return true;
             }
             return false;
         }
@@ -391,6 +397,8 @@ public class SMTP {
                 Log.warn(SMTP.Outgoing.class, "    unable to send; error=" + e);
                 Log.warn(SMTP.Outgoing.class, "      message: " + m.summary());
                 Log.warn(SMTP.Outgoing.class, e);
+                /*
+                  // FIXME: we should not be bouncing here!
                 if (e.code >= 500 && e.code <= 599) {
                     try {
                         attempt(m.bounce("unable to deliver: " + e), true);
@@ -400,13 +408,14 @@ public class SMTP {
                     }
                     return true;
                 }
+                */
                 return false;
             } catch (Exception e) {
                 if (accepted) return true;
                 Log.warn(SMTP.Outgoing.class, "    unable to send; error=" + e);
                 Log.warn(SMTP.Outgoing.class, "      message: " + m.summary());
                 Log.warn(SMTP.Outgoing.class, e);
-                if (conn != null) Log.warn(SMTP.Outgoing.class, conn.dumpLog());
+                //if (conn != null) Log.warn(SMTP.Outgoing.class, conn.dumpLog());
                 return false;
             } finally {
                 if (conn != null) conn.close();