5 import org.ibex.mail.protocol.*;
6 import org.ibex.util.*;
12 import java.sql.Timestamp;
13 import java.sql.Connection;
15 public class Whitelist extends SqliteDB {
17 public Whitelist(String filename) throws SQLException {
19 SqliteTable whitelist = getTable("whitelist", "(email)");
20 SqliteTable pending = getTable("pending", "(spamid,email,message,date)");
24 public boolean handleRequest(org.ibex.net.Connection c) {
26 Socket sock = c.getSocket();
27 BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
28 String s = br.readLine();
29 String url = s.substring(s.indexOf(' ')+1);
30 url = url.substring(0, url.indexOf(' '));
31 while(s!=null && !s.equals(""))
33 PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
34 if (!url.startsWith("/whitelist/")) {
35 pw.print("HTTP/1.0 404 Not Found\r\n");
36 pw.print("Content-Type: text/plain\r\n");
38 pw.println("you are lost.");
40 url = url.substring("/whitelist/".length());
41 url = URLDecoder.decode(url);
42 if (url.endsWith(".txt")) url = url.substring(0, url.length()-4);
43 pw.print("HTTP/1.0 200 OK\r\n");
44 pw.print("Content-Type: text/plain\r\n");
47 SMTP.whitelist.response(url);
48 pw.println("Thanks! You've been added to my list of non-spammers and your message");
49 pw.println("has been moved to my inbox.");
50 pw.println("email id " + url);
52 } catch (Exception e) {
53 e.printStackTrace(pw);
58 } catch (Exception e) { throw new RuntimeException(e); }
62 public synchronized boolean isWhitelisted(Address a) {
64 if (a==null) return false;
65 PreparedStatement check = conn.prepareStatement("select * from 'whitelist' where email=?");
66 check.setString(1, a.toString(false).toLowerCase());
67 ResultSet rs = check.executeQuery();
68 return !rs.isAfterLast();
69 } catch (SQLException e) { throw new RuntimeException(e); }
72 public synchronized void addWhitelist(Address a) {
74 PreparedStatement add = conn.prepareStatement("insert or replace into 'whitelist' values(?)");
75 add.setString(1, a.toString(false).toLowerCase());
77 } catch (SQLException e) { throw new RuntimeException(e); }
80 public synchronized void response(String messageid) throws IOException, MailException {
82 PreparedStatement query = conn.prepareStatement("select email,message from pending where spamid=?");
83 query.setString(1, messageid);
84 ResultSet rs = query.executeQuery();
86 throw new RuntimeException("could not find messageid \""+messageid+"\"");
87 HashSet<Message> hsm = new HashSet<Message>();
90 addWhitelist(Address.parse(rs.getString(1)));
91 Message m = Message.newMessage(new Fountain.StringFountain(rs.getString(2)));
92 Address a = m.headers.get("reply-to")==null ? null : Address.parse(m.headers.get("reply-to"));
93 if (a!=null) addWhitelist(a);
95 if (a!=null) addWhitelist(a);
97 if (a!=null) addWhitelist(a);
102 Target.root.accept(m);
103 } catch (SQLException e) { throw new RuntimeException(e); }
106 public void challenge(Message m) {
108 // FIXME: don't challenge emails with binaries in them;
109 // reject them outright and have the sender send an
110 // initial message w/o a binary.
112 // FIXME: use Auto here!!!
113 // The challenge should refer to the message-id of the mail being challenged.
115 // FIXME: watch outgoing MessageID's: if something comes
116 // back with an In-Reply-To mentioning a MessageID from
117 // the last few days, auto-whitelist them.
119 // FIXME: important that "From" on the challenge matches
120 // RCPT TO on the original message.
122 Log.warn(Whitelist.class, "challenging message: " + m.summary());
124 Address to = m.headers.get("reply-to")==null ? null : Address.parse(m.headers.get("reply-to"));
125 if (to==null) to = m.from;
126 if (to==null) to = m.envelopeFrom;
128 if (m.envelopeTo==null || m.envelopeTo.equals("null") || m.envelopeTo.equals("")) {
129 Log.warn(this, "message is missing a to/replyto/envelopeto header; cannot accept");
132 if (m.headers.get("Auto-Submitted") != null &&
133 m.headers.get("Auto-Submitted").toLowerCase().indexOf("auto-replied")!=-1) {
134 Log.warn(this, "refusing to send a challenge to a message "+
135 "with Auto-Submitted=\""+m.headers.get("Auto-Submitted")+"\"");
139 Address from = Address.parse("adam@megacz.com");
141 String messageid = "x" + m.messageid.substring(1);
142 messageid = messageid.substring(0, messageid.length() - 1);
143 messageid = messageid.replace('%','_');
144 Log.warn(Whitelist.class, "got challenge for: " + messageid);
146 String url = "http://www.megacz.com:8025/whitelist/"+URLEncoder.encode(messageid)+".txt";
148 "Return-Path: <>" + "\r\n" +
149 "Envelope-To: " + to + "\r\n" +
150 "X-Originally-Received: " + m.headers.get("received") + "\r\n" +
151 "To: " + to + "\r\n" +
152 "From: " + from + "\r\n" +
153 "Subject: Re: " + m.subject + "\r\n" +
154 "Message-ID:" + Message.generateFreshMessageId() + "\r\n" +
156 "Hi, I've never sent a message to you before, so my spam filter trapped\n" +
157 "your email. If you're really a human being and not an evil spammer,\n" +
158 "please click the link below or paste it into a web browser; doing so will\n" +
159 "add you to my list of non-spammers (so you won't get this email in the future)\n"+
160 "and it will move your message from my spam folder to my incoming mail folder.\n" +
169 "About this message:\n" +
171 "NOTE: SPAMCOP DOES NOT CONSIDER THIS TO BE SPAM; see this:\n"+
173 " http://www.spamcop.net/fom-serve/cache/369.html\n"+
175 " and examine the \"x-originally-received\" header on this message \n"+
176 " for the required \"chain of custody\" information.\n"+
178 " Only one of these challenge messages is ever generated in response to \n"+
179 " a given inbound SMTP connection; it cannot be used to amplify spam \n"+
180 " attacks, and in fact actually retards them while also stripping the \n"+
181 " advertisement they were meant to convey.\n"+
183 " Only one delivery attempt for this challenge is ever made, and it is made\n"+
184 " DURING the SMTP delivery of the message being challenged (that is, \n"+
185 " between C:DATA and S:250); the deliverer of the possibly-spam message\n"+
186 " must remain SMTP connected to my server during the entire process or else\n"+
187 " the delivery will immediately abort. These challenge messages are NEVER,\n"+
188 " EVER queued for multiple delivery attempts\n"+
190 " For more information, please see:\n"+
192 " http://www.templetons.com/brad/spam/crgood.html\n";
194 Message challenge = Message.newMessage(new Fountain.StringFountain(message));
196 boolean send = false;
198 PreparedStatement query = conn.prepareStatement("select email from pending where email=?");
199 query.setString(1, to.toString(false));
200 ResultSet rs = query.executeQuery();
202 Log.warn(this, "already challenged " + to.toString(false) + "; not challenging again.");
209 if (!SMTP.Outgoing.attempt(challenge))
210 throw new RuntimeException("attempted to send challenge but could not: " + m.summary());
213 PreparedStatement add = conn.prepareStatement("insert into pending values(?,?,?,?)");
214 add.setString(1, messageid);
215 add.setString(2, to.toString(false));
216 add.setString(3, streamToString(m.getStream()));
217 add.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
220 } catch (Exception e) { throw new RuntimeException(e); }
223 private static String streamToString(Stream stream) throws Exception {
224 StringBuffer b = new StringBuffer();
225 for(String s = stream.readln(); s!=null; s=stream.readln())