added graylisting
[org.ibex.mail.git] / src / org / ibex / mail / Graylist.java
1 package org.ibex.mail;
2 // todo: periodically flush out old graylist entries
3
4 import java.sql.*;
5 import java.net.*;
6 import java.io.*;
7 import java.util.*;
8 import java.sql.Timestamp;
9
10 public class Graylist {
11
12     private Connection conn;
13
14     public Graylist(String filename) {
15         try {
16             Class.forName("org.sqlite.JDBC");
17             conn = DriverManager.getConnection("jdbc:sqlite:"+filename);
18             conn.prepareStatement("create table if not exists "+
19                                   "'whitelist' (ip unique)").executeUpdate();
20             conn.prepareStatement("create table if not exists "+
21                                   "'graylist' (ip,fromaddr,toaddr,date, primary key(ip,fromaddr,toaddr))").executeUpdate();
22         }
23         catch (SQLException e) { throw new RuntimeException(e); }
24         catch (ClassNotFoundException e) { throw new RuntimeException(e); }
25     }
26
27     public synchronized void addWhitelist(InetAddress ip) {
28         try {
29             PreparedStatement add    = conn.prepareStatement("insert or replace into 'whitelist' values(?)");
30             add.setString(1, ip.getHostAddress());
31             add.executeUpdate();
32         } catch (SQLException e) { throw new RuntimeException(e); }
33     }
34
35     public synchronized boolean isWhitelisted(InetAddress ip) {
36         try {
37             PreparedStatement check = conn.prepareStatement("select * from 'whitelist' where ip=?");
38             check.setString(1, ip.getHostAddress());
39             ResultSet rs = check.executeQuery();
40             return !rs.isAfterLast();
41         } catch (SQLException e) { throw new RuntimeException(e); }
42     }
43
44     public synchronized long getGrayListTimestamp(InetAddress ip, String from, String to) {
45         try {
46             PreparedStatement check =
47                 conn.prepareStatement("select date from graylist where ip=? and fromaddr=? and toaddr=?");
48             check.setString(1, ip.getHostAddress());
49             check.setString(2, from);
50             check.setString(3, to);
51             ResultSet rs = check.executeQuery();
52             if (rs.isAfterLast()) return 0;
53             return rs.getTimestamp(1).getTime();
54         } catch (SQLException e) { throw new RuntimeException(e); }
55     }
56
57     public synchronized void setGrayListTimestamp(InetAddress ip, String from, String to, long date) {
58         try {
59             PreparedStatement check =
60                 conn.prepareStatement("insert or replace into graylist (ip,fromaddr,toaddr,date) values(?,?,?,?)");
61             check.setString(1, ip.getHostAddress());
62             check.setString(2, from);
63             check.setString(3, to);
64             check.setTimestamp(4, new Timestamp(date));
65             check.executeUpdate();
66         } catch (SQLException e) { throw new RuntimeException(e); }
67     }
68
69 }
70