synchronize Whitelist methods and whitelist all three "from" addresses
authoradam <adam@megacz.com>
Thu, 5 Jul 2007 01:33:16 +0000 (01:33 +0000)
committeradam <adam@megacz.com>
Thu, 5 Jul 2007 01:33:16 +0000 (01:33 +0000)
darcs-hash:20070705013316-5007d-121a44181c4aaf9027ed2c89e17f925ac2d5fda1.gz

src/org/ibex/mail/Whitelist.java

index b2b520c..934963a 100644 (file)
@@ -25,7 +25,7 @@ public class Whitelist extends SqliteTable {
               "date");
     }
 
-    public synchronized boolean handleRequest(org.ibex.net.Connection c) {
+    public boolean handleRequest(org.ibex.net.Connection c) {
         try {
             Socket sock = c.getSocket();
             BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
@@ -87,16 +87,41 @@ public class Whitelist extends SqliteTable {
             ResultSet rs = query.executeQuery();
             if (!rs.next())
                 throw new RuntimeException("could not find messageid \""+messageid+"\"");
-            do {
-                addWhitelist(Address.parse(rs.getString(1)));
-                Message m = Message.newMessage(new Fountain.StringFountain(rs.getString(2)));
+            HashSet<Message> hsm = new HashSet<Message>();
+            synchronized(this) {
+                do {
+                    addWhitelist(Address.parse(rs.getString(1)));
+                    Message m = Message.newMessage(new Fountain.StringFountain(rs.getString(2)));
+                    Address a = m.headers.get("reply-to")==null ? null : Address.parse(m.headers.get("reply-to"));
+                    if (a!=null) addWhitelist(a);
+                    a = m.from;
+                    if (a!=null) addWhitelist(a);
+                    a = m.envelopeFrom;
+                    if (a!=null) addWhitelist(a);
+                    hsm.add(m);
+                } while (rs.next());
+            }
+            for(Message m : hsm)
                 Target.root.accept(m);
-            } while (rs.next());
         } catch (SQLException e) { throw new RuntimeException(e); }
     }
 
-    public synchronized void challenge(Message m) {
+    public void challenge(Message m) {
         try {
+            // FIXME: don't challenge emails with binaries in them;
+            // reject them outright and have the sender send an
+            // initial message w/o a binary.
+
+            // FIXME: use Auto here!!!
+            // The challenge should refer to the message-id of the mail being challenged. 
+
+            // FIXME: watch outgoing MessageID's: if something comes
+            // back with an In-Reply-To mentioning a MessageID from
+            // the last few days, auto-whitelist them.
+
+            // FIXME: important that "From" on the challenge matches
+            // RCPT TO on the original message.
+
             Log.warn(Whitelist.class, "challenging message: " + m.summary());
 
             Address to = m.headers.get("reply-to")==null ? null : Address.parse(m.headers.get("reply-to"));
@@ -171,22 +196,30 @@ public class Whitelist extends SqliteTable {
 
             Message challenge = Message.newMessage(new Fountain.StringFountain(message));
 
-            PreparedStatement query = conn.prepareStatement("select email from pending where email=?");
-            query.setString(1, to.toString(false));
-            ResultSet rs = query.executeQuery();
-            if (rs.next()) {
-                Log.warn(this, "already challenged " + to.toString(false) + "; not challenging again.");
-            } else {
+            boolean send = false;
+            synchronized(this) {
+                PreparedStatement query = conn.prepareStatement("select email from pending where email=?");
+                query.setString(1, to.toString(false));
+                ResultSet rs = query.executeQuery();
+                if (rs.next()) {
+                    Log.warn(this, "already challenged " + to.toString(false) + "; not challenging again.");
+                } else {
+                    send = true;
+                }
+            }
+
+            if (send)
                 if (!SMTP.Outgoing.attempt(challenge))
                     throw new RuntimeException("attempted to send challenge but could not: " + m.summary());
-            }
 
-            PreparedStatement add = conn.prepareStatement("insert into pending values(?,?,?,?)");
-            add.setString(1, messageid);
-            add.setString(2, to.toString(false));
-            add.setString(3, streamToString(m.getStream()));
-            add.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
-            add.executeUpdate();
+            synchronized(this) {
+                PreparedStatement add = conn.prepareStatement("insert into pending values(?,?,?,?)");
+                add.setString(1, messageid);
+                add.setString(2, to.toString(false));
+                add.setString(3, streamToString(m.getStream()));
+                add.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
+                add.executeUpdate();
+            }
         } catch (Exception e) { throw new RuntimeException(e); }
     }