formatting
[org.ibex.mail.git] / src / org / ibex / mail / Whitelist.java
index 934963a..95fb162 100644 (file)
@@ -12,17 +12,16 @@ import java.util.*;
 import java.sql.Timestamp;
 import java.sql.Connection;
 
-public class Whitelist extends SqliteTable {
-
-    public Whitelist(String filename) {
-        super(filename,
-              new String[] {
-                  "create table if not exists 'whitelist' (email)",
-                  "create table if not exists 'pending' (spamid,email,message,date)"
-              },
-              true,
-              "pending",
-              "date");
+public class Whitelist extends SqliteDB {
+
+    public Whitelist(String filename) throws SQLException {
+        super(filename);
+        SqliteTable whitelist = getTable("whitelist", "(email)");
+        whitelist.createIndex("email");
+        SqliteTable pending   = getTable("pending",   "(spamid,email,message,date)");
+        pending.reap("date");
+        pending.createIndex("spamid");
+        pending.createIndex("email");
     }
 
     public boolean handleRequest(org.ibex.net.Connection c) {
@@ -43,6 +42,7 @@ public class Whitelist extends SqliteTable {
             } else {
                 url = url.substring("/whitelist/".length());
                 url = URLDecoder.decode(url);
+                if (url.endsWith(".txt")) url = url.substring(0, url.length()-4);
                 pw.print("HTTP/1.0 200 OK\r\n");
                 pw.print("Content-Type: text/plain\r\n");
                 pw.print("\r\n");
@@ -99,6 +99,9 @@ public class Whitelist extends SqliteTable {
                     a = m.envelopeFrom;
                     if (a!=null) addWhitelist(a);
                     hsm.add(m);
+                    if (m.cc != null) for(Address aa : m.cc) {
+                        if (aa!= null) addWhitelist(aa);
+                    }
                 } while (rs.next());
             }
             for(Message m : hsm)
@@ -138,6 +141,10 @@ public class Whitelist extends SqliteTable {
                          "with Auto-Submitted=\""+m.headers.get("Auto-Submitted")+"\"");
                 return;
             }
+            if (m.headers.get("List-Id") != null || m.headers.get("List-Post") != null) {
+                Log.warn(this, "refusing to send a challenge to a message with a List-Id or List-Post header");
+                return;
+            }
 
             Address from = Address.parse("adam@megacz.com");
 
@@ -146,7 +153,7 @@ public class Whitelist extends SqliteTable {
             messageid = messageid.replace('%','_');
             Log.warn(Whitelist.class, "got challenge for: " + messageid);
 
-            String url = "http://www.megacz.com:8025/whitelist/"+URLEncoder.encode(messageid);
+            String url = "http://www.megacz.com:8025/whitelist/"+URLEncoder.encode(messageid)+".txt";
             String message =
                 "Return-Path: <>"                                     + "\r\n" +
                 "Envelope-To: "           + to                        + "\r\n" +
@@ -168,7 +175,8 @@ public class Whitelist extends SqliteTable {
                 "\n"                                                                              +
                 url+"\n" +
                 "\n"                                                                              +
-                "\n"                                                                              +
+                "\n"                                                                              
+                /*
                 "About this message:\n" +
                 "\n"                                                                              +
                 "NOTE: SPAMCOP DOES NOT CONSIDER THIS TO BE SPAM; see this:\n"+
@@ -192,8 +200,9 @@ public class Whitelist extends SqliteTable {
                 "      \n"+
                 "      For more information, please see:\n"+
                 "      \n"+
-                "      http://www.templetons.com/brad/spam/crgood.html\n";
-
+                "      http://www.templetons.com/brad/spam/crgood.html\n"
+                */
+                ;
             Message challenge = Message.newMessage(new Fountain.StringFountain(message));
 
             boolean send = false;
@@ -216,18 +225,12 @@ public class Whitelist extends SqliteTable {
                 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.setString(3, SqliteDB.streamToString(m.getStream()));
                 add.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
                 add.executeUpdate();
             }
         } catch (Exception e) { throw new RuntimeException(e); }
     }
 
-    private static String streamToString(Stream stream) throws Exception {
-        StringBuffer b = new StringBuffer();
-        for(String s = stream.readln(); s!=null; s=stream.readln())
-            b.append(s+"\n");
-        return b.toString();
-    }
 }