added graylisting
[org.ibex.mail.git] / src / org / ibex / mail / Graylist.java
diff --git a/src/org/ibex/mail/Graylist.java b/src/org/ibex/mail/Graylist.java
new file mode 100644 (file)
index 0000000..0628062
--- /dev/null
@@ -0,0 +1,70 @@
+package org.ibex.mail;
+// todo: periodically flush out old graylist entries
+
+import java.sql.*;
+import java.net.*;
+import java.io.*;
+import java.util.*;
+import java.sql.Timestamp;
+
+public class Graylist {
+
+    private Connection conn;
+
+    public Graylist(String filename) {
+        try {
+            Class.forName("org.sqlite.JDBC");
+            conn = DriverManager.getConnection("jdbc:sqlite:"+filename);
+            conn.prepareStatement("create table if not exists "+
+                                  "'whitelist' (ip unique)").executeUpdate();
+            conn.prepareStatement("create table if not exists "+
+                                  "'graylist' (ip,fromaddr,toaddr,date, primary key(ip,fromaddr,toaddr))").executeUpdate();
+        }
+        catch (SQLException e) { throw new RuntimeException(e); }
+        catch (ClassNotFoundException e) { throw new RuntimeException(e); }
+    }
+
+    public synchronized void addWhitelist(InetAddress ip) {
+        try {
+            PreparedStatement add    = conn.prepareStatement("insert or replace into 'whitelist' values(?)");
+            add.setString(1, ip.getHostAddress());
+            add.executeUpdate();
+        } catch (SQLException e) { throw new RuntimeException(e); }
+    }
+
+    public synchronized boolean isWhitelisted(InetAddress ip) {
+        try {
+            PreparedStatement check = conn.prepareStatement("select * from 'whitelist' where ip=?");
+            check.setString(1, ip.getHostAddress());
+            ResultSet rs = check.executeQuery();
+            return !rs.isAfterLast();
+        } catch (SQLException e) { throw new RuntimeException(e); }
+    }
+
+    public synchronized long getGrayListTimestamp(InetAddress ip, String from, String to) {
+        try {
+            PreparedStatement check =
+                conn.prepareStatement("select date from graylist where ip=? and fromaddr=? and toaddr=?");
+            check.setString(1, ip.getHostAddress());
+            check.setString(2, from);
+            check.setString(3, to);
+            ResultSet rs = check.executeQuery();
+            if (rs.isAfterLast()) return 0;
+            return rs.getTimestamp(1).getTime();
+        } catch (SQLException e) { throw new RuntimeException(e); }
+    }
+
+    public synchronized void setGrayListTimestamp(InetAddress ip, String from, String to, long date) {
+        try {
+            PreparedStatement check =
+                conn.prepareStatement("insert or replace into graylist (ip,fromaddr,toaddr,date) values(?,?,?,?)");
+            check.setString(1, ip.getHostAddress());
+            check.setString(2, from);
+            check.setString(3, to);
+            check.setTimestamp(4, new Timestamp(date));
+            check.executeUpdate();
+        } catch (SQLException e) { throw new RuntimeException(e); }
+    }
+
+}
+