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 SqliteTable {
17 public Whitelist(String filename) {
20 "create table if not exists 'whitelist' (email)",
21 "create table if not exists 'pending' (spamid,email,message,date)"
28 public boolean handleRequest(org.ibex.net.Connection c) {
30 Socket sock = c.getSocket();
31 BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
32 String s = br.readLine();
33 String url = s.substring(s.indexOf(' ')+1);
34 url = url.substring(0, url.indexOf(' '));
35 while(s!=null && !s.equals(""))
37 PrintWriter pw = new PrintWriter(new OutputStreamWriter(sock.getOutputStream()));
38 if (!url.startsWith("/whitelist/")) {
39 pw.print("HTTP/1.0 404 Not Found\r\n");
40 pw.print("Content-Type: text/plain\r\n");
42 pw.println("you are lost.");
44 url = url.substring("/whitelist/".length());
45 url = URLDecoder.decode(url);
46 pw.print("HTTP/1.0 200 OK\r\n");
47 pw.print("Content-Type: text/plain\r\n");
50 SMTP.whitelist.response(url);
51 pw.println("Thanks! You've been added to my list of non-spammers and your message");
52 pw.println("has been moved to my inbox.");
53 pw.println("email id " + url);
55 } catch (Exception e) {
56 e.printStackTrace(pw);
61 } catch (Exception e) { throw new RuntimeException(e); }
65 public synchronized boolean isWhitelisted(Address a) {
67 if (a==null) return false;
68 PreparedStatement check = conn.prepareStatement("select * from 'whitelist' where email=?");
69 check.setString(1, a.toString(false).toLowerCase());
70 ResultSet rs = check.executeQuery();
71 return !rs.isAfterLast();
72 } catch (SQLException e) { throw new RuntimeException(e); }
75 public synchronized void addWhitelist(Address a) {
77 PreparedStatement add = conn.prepareStatement("insert or replace into 'whitelist' values(?)");
78 add.setString(1, a.toString(false).toLowerCase());
80 } catch (SQLException e) { throw new RuntimeException(e); }
83 public synchronized void response(String messageid) throws IOException, MailException {
85 PreparedStatement query = conn.prepareStatement("select email,message from pending where spamid=?");
86 query.setString(1, messageid);
87 ResultSet rs = query.executeQuery();
89 throw new RuntimeException("could not find messageid \""+messageid+"\"");
90 HashSet<Message> hsm = new HashSet<Message>();
93 addWhitelist(Address.parse(rs.getString(1)));
94 Message m = Message.newMessage(new Fountain.StringFountain(rs.getString(2)));
95 Address a = m.headers.get("reply-to")==null ? null : Address.parse(m.headers.get("reply-to"));
96 if (a!=null) addWhitelist(a);
98 if (a!=null) addWhitelist(a);
100 if (a!=null) addWhitelist(a);
105 Target.root.accept(m);
106 } catch (SQLException e) { throw new RuntimeException(e); }
109 public void challenge(Message m) {
111 // FIXME: don't challenge emails with binaries in them;
112 // reject them outright and have the sender send an
113 // initial message w/o a binary.
115 // FIXME: use Auto here!!!
116 // The challenge should refer to the message-id of the mail being challenged.
118 // FIXME: watch outgoing MessageID's: if something comes
119 // back with an In-Reply-To mentioning a MessageID from
120 // the last few days, auto-whitelist them.
122 // FIXME: important that "From" on the challenge matches
123 // RCPT TO on the original message.
125 Log.warn(Whitelist.class, "challenging message: " + m.summary());
127 Address to = m.headers.get("reply-to")==null ? null : Address.parse(m.headers.get("reply-to"));
128 if (to==null) to = m.from;
129 if (to==null) to = m.envelopeFrom;
131 if (m.envelopeTo==null || m.envelopeTo.equals("null") || m.envelopeTo.equals("")) {
132 Log.warn(this, "message is missing a to/replyto/envelopeto header; cannot accept");
135 if (m.headers.get("Auto-Submitted") != null &&
136 m.headers.get("Auto-Submitted").toLowerCase().indexOf("auto-replied")!=-1) {
137 Log.warn(this, "refusing to send a challenge to a message "+
138 "with Auto-Submitted=\""+m.headers.get("Auto-Submitted")+"\"");
142 Address from = Address.parse("adam@megacz.com");
144 String messageid = "x" + m.messageid.substring(1);
145 messageid = messageid.substring(0, messageid.length() - 1);
146 messageid = messageid.replace('%','_');
147 Log.warn(Whitelist.class, "got challenge for: " + messageid);
149 String url = "http://www.megacz.com:8025/whitelist/"+URLEncoder.encode(messageid);
151 "Return-Path: <>" + "\r\n" +
152 "Envelope-To: " + to + "\r\n" +
153 "X-Originally-Received: " + m.headers.get("received") + "\r\n" +
154 "To: " + to + "\r\n" +
155 "From: " + from + "\r\n" +
156 "Subject: Re: " + m.subject + "\r\n" +
157 "Message-ID:" + Message.generateFreshMessageId() + "\r\n" +
159 "Hi, I've never sent a message to you before, so my spam filter trapped\n" +
160 "your email. If you're really a human being and not an evil spammer,\n" +
161 "please click the link below or paste it into a web browser; doing so will\n" +
162 "add you to my list of non-spammers (so you won't get this email in the future)\n"+
163 "and it will move your message from my spam folder to my incoming mail folder.\n" +
172 "About this message:\n" +
174 "NOTE: SPAMCOP DOES NOT CONSIDER THIS TO BE SPAM; see this:\n"+
176 " http://www.spamcop.net/fom-serve/cache/369.html\n"+
178 " and examine the \"x-originally-received\" header on this message \n"+
179 " for the required \"chain of custody\" information.\n"+
181 " Only one of these challenge messages is ever generated in response to \n"+
182 " a given inbound SMTP connection; it cannot be used to amplify spam \n"+
183 " attacks, and in fact actually retards them while also stripping the \n"+
184 " advertisement they were meant to convey.\n"+
186 " Only one delivery attempt for this challenge is ever made, and it is made\n"+
187 " DURING the SMTP delivery of the message being challenged (that is, \n"+
188 " between C:DATA and S:250); the deliverer of the possibly-spam message\n"+
189 " must remain SMTP connected to my server during the entire process or else\n"+
190 " the delivery will immediately abort. These challenge messages are NEVER,\n"+
191 " EVER queued for multiple delivery attempts\n"+
193 " For more information, please see:\n"+
195 " http://www.templetons.com/brad/spam/crgood.html\n";
197 Message challenge = Message.newMessage(new Fountain.StringFountain(message));
199 boolean send = false;
201 PreparedStatement query = conn.prepareStatement("select email from pending where email=?");
202 query.setString(1, to.toString(false));
203 ResultSet rs = query.executeQuery();
205 Log.warn(this, "already challenged " + to.toString(false) + "; not challenging again.");
212 if (!SMTP.Outgoing.attempt(challenge))
213 throw new RuntimeException("attempted to send challenge but could not: " + m.summary());
216 PreparedStatement add = conn.prepareStatement("insert into pending values(?,?,?,?)");
217 add.setString(1, messageid);
218 add.setString(2, to.toString(false));
219 add.setString(3, streamToString(m.getStream()));
220 add.setTimestamp(4, new Timestamp(System.currentTimeMillis()));
223 } catch (Exception e) { throw new RuntimeException(e); }
226 private static String streamToString(Stream stream) throws Exception {
227 StringBuffer b = new StringBuffer();
228 for(String s = stream.readln(); s!=null; s=stream.readln())