graylist/whitelist updates
[org.ibex.mail.git] / src / org / ibex / mail / Whitelist.java
1 package org.ibex.mail;
2
3 import org.ibex.io.*;
4 import org.ibex.mail.protocol.*;
5 import org.ibex.util.*;
6 import org.ibex.net.*;
7 import java.sql.*;
8 import java.net.*;
9 import java.io.*;
10 import java.util.*;
11 import java.sql.Timestamp;
12 import java.sql.Connection;
13
14 public class Whitelist extends SqliteTable {
15
16     public Whitelist(String filename) {
17         super(filename,
18               new String[] {
19                   "create table if not exists 'whitelist' (email)",
20                   "create table if not exists 'pending' (spamid,email,message,date)"
21               },
22               "pending",
23               "date");
24     }
25
26     public synchronized boolean handleRequest(org.ibex.io.Connection c) {
27         try {
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(""))
34                 s = br.readLine();
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");
39                 pw.print("\r\n");
40                 pw.println("you are lost.");
41             } else {
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");
46                 pw.print("\r\n");
47                 try {
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);
52                     pw.println("");
53                 } catch (Exception e) {
54                     e.printStackTrace(pw);
55                 }
56             }
57             pw.flush();
58             sock.close();
59         } catch (Exception e) { throw new RuntimeException(e); }
60         return true;
61     }
62
63     public synchronized boolean isWhitelisted(Address a) {
64         try {
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); }
71     }
72
73     public synchronized void addWhitelist(Address a) {
74         try {
75             PreparedStatement add    = conn.prepareStatement("insert or replace into 'whitelist' values(?)");
76             add.setString(1, a.toString(false).toLowerCase());
77             add.executeUpdate();
78         } catch (SQLException e) { throw new RuntimeException(e); }
79     }
80
81     public synchronized void response(String messageid) throws IOException, MailException {
82         try {
83             PreparedStatement query = conn.prepareStatement("select email,message from pending where spamid=?");
84             query.setString(1, messageid);
85             ResultSet rs = query.executeQuery();
86             if (!rs.next())
87                 throw new RuntimeException("could not find messageid \""+messageid+"\"");
88             do {
89                 addWhitelist(Address.parse(rs.getString(1)));
90                 Message m = Message.newMessage(new Fountain.StringFountain(rs.getString(2)));
91                 Target.root.accept(m);
92             } while (rs.next());
93         } catch (SQLException e) { throw new RuntimeException(e); }
94     }
95
96     public synchronized void challenge(Message m) {
97         try {
98             Log.warn(Whitelist.class, "challenging message: " + m.summary());
99
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;
103
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");
106                 return;
107             }
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")+"\"");
112                 return;
113             }
114
115             Address from = Address.parse("adam@megacz.com");
116
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);
121
122             String url = "http://www.megacz.com:8025/whitelist/"+URLEncoder.encode(messageid);
123             String message =
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" +
131                 "\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" +
137                 "\n"                                                                              +
138                 "Thanks!\n"                                                                       +
139                 "\n"                                                                              +
140                 "  - Adam\n"                                                                      +
141                 "\n"                                                                              +
142                 url+"\n" +
143                 "\n"                                                                              +
144                 "\n"                                                                              +
145                 "About this message:\n" +
146                 "\n"                                                                              +
147                 "NOTE: SPAMCOP DOES NOT CONSIDER THIS TO BE SPAM; see this:\n"+
148                 "\n"+
149                 "         http://www.spamcop.net/fom-serve/cache/369.html\n"+
150                 "\n"+
151                 "      and examine the \"x-originally-received\" header on this message \n"+
152                 "      for the required \"chain of custody\" information.\n"+
153                 "\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"+
158                 "\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"+
165                 "      \n"+
166                 "      For more information, please see:\n"+
167                 "      \n"+
168                 "      http://www.templetons.com/brad/spam/crgood.html\n";
169
170             Message challenge = Message.newMessage(new Fountain.StringFountain(message));
171
172             PreparedStatement query = conn.prepareStatement("select email from pending where email=?");
173             query.setString(1, to.toString(false));
174             ResultSet rs = query.executeQuery();
175             if (rs.next()) {
176                 Log.warn(this, "already challenged " + to.toString(false) + "; not challenging again.");
177             } else {
178                 if (!SMTP.Outgoing.attempt(challenge))
179                     throw new RuntimeException("attempted to send challenge but could not: " + m.summary());
180             }
181
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()));
187             add.executeUpdate();
188         } catch (Exception e) { throw new RuntimeException(e); }
189     }
190
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())
194             b.append(s+"\n");
195         return b.toString();
196     }
197 }
198