X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2FWhitelist.java;h=7355a5c449ff7b2b995f8f0cebf07cec9e3727b6;hb=4b76a31e8f86bc8de673899bbb45252c01e9a0f6;hp=b2b520c4068d799b55d0fac07b5c8e2b3a940c24;hpb=99730a18f75565e684deebb43f17b58d1ac22283;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/Whitelist.java b/src/org/ibex/mail/Whitelist.java index b2b520c..7355a5c 100644 --- a/src/org/ibex/mail/Whitelist.java +++ b/src/org/ibex/mail/Whitelist.java @@ -12,20 +12,19 @@ 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 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())); @@ -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"); @@ -87,16 +87,44 @@ 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 hsm = new HashSet(); + 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); + if (m.cc != null) for(Address aa : m.cc) { + if (aa!= null) addWhitelist(aa); + } + } 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")); @@ -121,7 +149,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" + @@ -171,30 +199,32 @@ 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, 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(); - } }