4 import org.ibex.mail.protocol.*;
5 import org.ibex.util.*;
11 import java.sql.Timestamp;
12 import java.sql.Connection;
14 public class Whitelist extends SqliteTable {
16 public Whitelist(String filename) {
19 "create table if not exists 'whitelist' (email)",
20 "create table if not exists 'pending' (spamid,email,message,date)"
26 public synchronized boolean handleRequest(org.ibex.io.Connection c) {
28 Socket sock = c.getSocket();
29 BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
30 String s = br.readLine();
31 String url = s.substring(s.indexOf(' ')+1);
32 url = url.substring(0, url.indexOf(' '));
33 while(s!=null && !s.equals(""))
35 PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
36 if (!url.startsWith("/whitelist/")) {
37 pw.print("HTTP/1.0 404 Not FoundK\r\n");
38 pw.print("Content-Type: text/plain\r\n");
40 pw.println("you are lost.");
42 url = url.substring("/whitelist/".length());
43 url = URLDecoder.decode(url);
44 pw.print("HTTP/1.0 200 OK\r\n");
45 pw.print("Content-Type: text/plain\r\n");
48 SMTP.whitelist.response(url);
49 pw.println("Thanks! You've been added to my list of non-spammers and your message");
50 pw.println("has been moved to my inbox.");
51 pw.println("email id " + url);
53 } catch (Exception e) {
54 e.printStackTrace(pw);
59 } catch (Exception e) { throw new RuntimeException(e); }
63 public synchronized boolean isWhitelisted(Address a) {
65 if (a==null) return false;
66 PreparedStatement check = conn.prepareStatement("select * from 'whitelist' where email=?");
67 check.setString(1, a.toString(false).toLowerCase());
68 ResultSet rs = check.executeQuery();
69 return !rs.isAfterLast();
70 } catch (SQLException e) { throw new RuntimeException(e); }
73 public synchronized void addWhitelist(Address a) {
75 PreparedStatement add = conn.prepareStatement("insert or replace into 'whitelist' values(?)");
76 add.setString(1, a.toString(false).toLowerCase());
78 } catch (SQLException e) { throw new RuntimeException(e); }
81 public synchronized void response(String messageid) throws IOException, MailException {
83 PreparedStatement query = conn.prepareStatement("select email,message from pending where spamid=?");
84 query.setString(1, messageid);
85 ResultSet rs = query.executeQuery();
87 throw new RuntimeException("could not find messageid \""+messageid+"\"");
89 addWhitelist(Address.parse(rs.getString(1)));
90 Message m = Message.newMessage(new Fountain.StringFountain(rs.getString(2)));
91 Target.root.accept(m);
93 } catch (SQLException e) { throw new RuntimeException(e); }
96 public synchronized void challenge(Message m) {
98 Log.warn(Whitelist.class, "challenging message: " + m.summary());
100 Address to = m.headers.get("reply-to")==null ? null : Address.parse(m.headers.get("reply-to"));
101 if (to==null) to = m.from;
102 if (to==null) to = m.envelopeFrom;
104 if (m.envelopeTo==null || m.envelopeTo.equals("null") || m.envelopeTo.equals("")) {
105 Log.warn(this, "message is missing a to/replyto/envelopeto header; cannot accept");
108 if (m.headers.get("Auto-Submitted") != null &&
109 m.headers.get("Auto-Submitted").toLowerCase().indexOf("auto-replied")!=-1) {
110 Log.warn(this, "refusing to send a challenge to a message "+
111 "with Auto-Submitted=\""+m.headers.get("Auto-Submitted")+"\"");
115 Address from = Address.parse("adam@megacz.com");
117 String messageid = "x" + m.messageid.substring(1);
118 messageid = messageid.substring(0, messageid.length() - 1);
119 messageid = messageid.replace('%','_');
120 Log.warn(Whitelist.class, "got challenge for: " + messageid);
122 String url = "http://www.megacz.com:8025/whitelist/"+URLEncoder.encode(messageid);
124 "Return-Path: <>" + "\r\n" +
125 "Envelope-To: " + to + "\r\n" +
126 "X-Originally-Received: " + m.headers.get("received") + "\r\n" +
127 "To: " + to + "\r\n" +
128 "From: " + from + "\r\n" +
129 "Subject: Re: " + m.subject + "\r\n" +
130 "Message-ID:" + Message.generateFreshMessageId() + "\r\n" +
132 "Hi, I've never sent a message to you before, so my spam filter trapped\n" +
133 "your email. If you're really a human being and not an evil spammer,\n" +
134 "please click the link below or paste it into a web browser; doing so will\n" +
135 "add you to my list of non-spammers (so you won't get this email in the future)\n"+
136 "and it will move your message from my spam folder to my incoming mail folder.\n" +
145 "About this message:\n" +
147 "NOTE: SPAMCOP DOES NOT CONSIDER THIS TO BE SPAM; see this:\n"+
149 " http://www.spamcop.net/fom-serve/cache/369.html\n"+
151 " and examine the \"x-originally-received\" header on this message \n"+
152 " for the required \"chain of custody\" information.\n"+
154 " Only one of these challenge messages is ever generated in response to \n"+
155 " a given inbound SMTP connection; it cannot be used to amplify spam \n"+
156 " attacks, and in fact actually retards them while also stripping the \n"+
157 " advertisement they were meant to convey.\n"+
159 " Only one delivery attempt for this challenge is ever made, and it is made\n"+
160 " DURING the SMTP delivery of the message being challenged (that is, \n"+
161 " between C:DATA and S:250); the deliverer of the possibly-spam message\n"+
162 " must remain SMTP connected to my server during the entire process or else\n"+
163 " the delivery will immediately abort. These challenge messages are NEVER,\n"+
164 " EVER queued for multiple delivery attempts\n"+
166 " For more information, please see:\n"+
168 " http://www.templetons.com/brad/spam/crgood.html\n";
170 Message challenge = Message.newMessage(new Fountain.StringFountain(message));
172 PreparedStatement query = conn.prepareStatement("select email from pending where email=?");
173 query.setString(1, to.toString(false));
174 ResultSet rs = query.executeQuery();
176 Log.warn(this, "already challenged " + to.toString(false) + "; not challenging again.");
178 if (!SMTP.Outgoing.attempt(challenge))
179 throw new RuntimeException("attempted to send challenge but could not: " + m.summary());
182 PreparedStatement add = conn.prepareStatement("insert into pending values(?,?,?,?)");
183 add.setString(1, messageid);
184 add.setString(2, to.toString(false));
185 add.setString(3, streamToString(m.getStream()));
186 add.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
188 } catch (Exception e) { throw new RuntimeException(e); }
191 private static String streamToString(Stream stream) throws Exception {
192 StringBuffer b = new StringBuffer();
193 for(String s = stream.readln(); s!=null; s=stream.readln())