X-Git-Url: http://git.megacz.com/?p=org.ibex.mail.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2FGraylist.java;h=d31ae00be93c757c271f537725c5606f6e150c38;hp=0a0ad538e783d21fc057233cd9f02bd84a65b9c5;hb=4b76a31e8f86bc8de673899bbb45252c01e9a0f6;hpb=24ff72d2529147b2dcbda9bfed28836bb674c6ad diff --git a/src/org/ibex/mail/Graylist.java b/src/org/ibex/mail/Graylist.java index 0a0ad53..d31ae00 100644 --- a/src/org/ibex/mail/Graylist.java +++ b/src/org/ibex/mail/Graylist.java @@ -9,13 +9,11 @@ import java.sql.Timestamp; public class Graylist extends SqliteDB { - public Graylist(String filename) { - super(filename, - new String[] { - "create table if not exists 'whitelist' (ip unique)", - "create table if not exists 'graylist' (ip,fromaddr,toaddr,date, primary key(ip,fromaddr,toaddr))" - }); - getTable("graylist").reap("date"); + public Graylist(String filename) throws SQLException { + super(filename); + SqliteTable whitelist = getTable("whitelist", "(ip unique)"); + SqliteTable graylist = getTable("graylist", "(ip,fromaddr,toaddr,date,PRIMARY KEY(ip,fromaddr,toaddr))"); + graylist.reap("date"); } public synchronized void addWhitelist(InetAddress ip) { @@ -30,8 +28,13 @@ public class Graylist extends SqliteDB { try { PreparedStatement check = conn.prepareStatement("select * from 'whitelist' where ip=?"); check.setString(1, ip.getHostAddress()); - ResultSet rs = check.executeQuery(); - return !rs.isAfterLast(); + ResultSet rs = null; + boolean ret; + try { + rs = check.executeQuery(); + ret = !rs.isAfterLast(); + } finally { close(rs); } + return ret; } catch (SQLException e) { throw new RuntimeException(e); } } @@ -39,12 +42,15 @@ public class Graylist extends SqliteDB { try { PreparedStatement check = conn.prepareStatement("select date from graylist where ip=? and fromaddr=? and toaddr=?"); - check.setString(1, ip.getHostAddress()); + check.setString(1, graylistAddress(ip)); check.setString(2, from); check.setString(3, to); - ResultSet rs = check.executeQuery(); - if (rs.isAfterLast()) return 0; - return rs.getTimestamp(1).getTime(); + ResultSet rs = null; + try { + rs = check.executeQuery(); + if (rs.isAfterLast()) return 0; + return rs.getTimestamp(1).getTime(); + } finally { close(rs); } } catch (SQLException e) { throw new RuntimeException(e); } } @@ -52,7 +58,7 @@ public class Graylist extends SqliteDB { try { PreparedStatement check = conn.prepareStatement("insert or replace into graylist (ip,fromaddr,toaddr,date) values(?,?,?,?)"); - check.setString(1, ip.getHostAddress()); + check.setString(1, graylistAddress(ip)); check.setString(2, from); check.setString(3, to); check.setTimestamp(4, new Timestamp(date)); @@ -60,5 +66,19 @@ public class Graylist extends SqliteDB { } catch (SQLException e) { throw new RuntimeException(e); } } + // + // this is mostly to deal with comcast's pathetic retry policy and + // rotating pool of outbound servers + // see: http://lists.puremagic.com/pipermail/greylist-users/2006-November/001255.html + // + private static String graylistAddress(InetAddress ipa) { + byte[] ip = ipa.getAddress(); + return + (ip[0] & 0xff)+"."+ + (ip[1] & 0xff)+"."+ + (ip[2] & 0xff)+"."+ + ".0"; + } + }