3f2817ff107beb8abca811eb587bb7a849a8fd53
[org.ibex.mail.git] / src / org / ibex / mail / protocol / NNTP.java
1 package org.ibex.mail.protocol;
2 import org.ibex.util.*;
3 import org.ibex.io.*;
4 import org.ibex.mail.*;
5 import org.ibex.mail.target.*;
6 import org.ibex.jinetd.*;
7 import java.io.*;
8 import java.net.*;
9 import java.util.*;
10 import java.text.*;
11
12 /** NNTP send/recieve */
13 public class NNTP {
14
15     public static final DateFormat dateFormat = new SimpleDateFormat("YYYYMMDDhhmmss");
16
17     public static class No  extends RuntimeException { int code = 400; }    // 4xx response codes
18     public static class Bad extends RuntimeException { int code = 500; public Bad(String s) { super(s); } }    // 5xx response codes
19
20     public static class Group {
21         public Group(String n, boolean p, int f, int l, int c) { this.name=n;this.post=p;this.first=f;this.last=l;this.count=c;}
22         public final String  name;    // case insensitive
23         public final boolean post;
24         public final int     first;
25         public final int     last;
26         public final int     count;   // an approximation; must be >= actual number
27     }
28
29     public static class Article {
30         public Article(int num, Message message) { this.message = message; this.num = num;}
31         public final int     num;
32         public final Message message;
33     }
34
35     public static interface Server {
36         public Group    group(String s);
37         public boolean  ihave(String messageid);
38         public Article  next();
39         public Article  last();
40         public boolean  post(Message m);
41         public Article  article(String messageid,  boolean head, boolean body);
42         public Article  article(int    messagenum, boolean head, boolean body);
43         public Group[]  list();
44         public Group[]  newgroups(Date d, String[] distributions);
45         public String[] newnews(String[] groups, Date d, String[] distributions);
46     }
47
48     public static class MailboxWrapper implements Server {
49         private final Mailbox root;
50         private Mailbox current;
51         private int ptr = 0;
52         public MailboxWrapper(Mailbox root)     { this.root = root; }
53         public Group    group(String s)         { ptr = 0; setgroup(s); return getgroup(s); }
54
55         public boolean  ihave(String messageid) { /* FEATURE */ return false; }
56         public boolean  post(Message m)         { /* FEATURE */ return false; }
57
58         public Article  next()                  { return article(ptr++, false, false); }
59         public Article  last()                  { return article(ptr--, false, false); }
60         public Article  article(String i, boolean h, boolean b) { return article(Query.header("message-id",i),h,b); }
61         public Article  article(int    n, boolean h, boolean b) { ptr = n; return article(Query.messagenum(n,n),h,b); }
62         private Article article(Query q,  boolean head, boolean body) {
63             Mailbox.Iterator it = current.iterator(q);
64             if (!it.next()) return null;
65             //Message m = body ? it.cur() : it.head();
66             Message m = it.cur(); // FIXME
67             return new Article(m.messageid, it.num(), m);
68         }
69         public Group[]  list() { return list(root, ""); }
70         private Group[] list(Mailbox who, String prefix) {
71             Vec v = new Vec();
72             if (who == null) who = root;
73             String[] s = who.children();
74             for(int i=0; i<s.length; i++) {
75                 v.addElement(new Group(prefix + s[i], true, 0, 0, 0)); // FIXME numbers
76                 Group[] g2 = list(who.slash(s[i], false), prefix + s[i] + ".");
77                 for(int j=0; j<g2.length; j++) v.addElement(g2[j]);
78             }
79             Group[] ret = new Group[v.size()];
80             v.copyInto(ret);
81             return ret;
82         }
83
84         private void setgroup(String s) {
85             current = root;
86             for(StringTokenizer st = new StringTokenizer(s, ".");
87                 st.hasMoreTokens();
88                 current = current.slash(st.nextToken(), false));
89         }
90         private Group getgroup(String s) {
91             Mailbox box = root;
92             for(StringTokenizer st = new StringTokenizer(s, "."); st.hasMoreTokens(); box = box.slash(st.nextToken(), false));
93             return new Group(s, true, 1, box.count(Query.all()), box.count(Query.all()));
94         }
95
96         public Group[]  newgroups(Date d, String[] distributions) { /* FEATURE */ return new Group[] { }; }
97         public String[] newnews(String[] groups, Date d, String[] distributions) { /* FIXME */  return null; }
98     }
99
100     public static class Listener implements Worker {
101         private Server api = null;
102         private Login login;
103         private Connection conn;
104         public Listener(Login l) { this.login = l; }
105
106         private void println(String s) { Log.warn("[nntp-write]", s); conn.println(s); }
107         private void println() { Log.warn("[nntp-write]", ""); conn.println(""); }
108         private void print(String s) { Log.warn("[nntp-write]", s); conn.print(s); }
109
110         private void article(String numOrMessageId, boolean head, boolean body) {
111             String s = numOrMessageId.trim();
112             Article a;
113             if (s.startsWith("<")) a = api.article(s.substring(0, s.length() - 1), head, body);
114             else                   a = api.article(Integer.parseInt(s), head, body);
115             if (a == null) {
116                 println("423 No such article.");
117                 return;
118             }
119             int code = (head && body) ? 220 : head ? 221 : body ? 222 : 223;
120             println(code + " " + a.num + " <" + a.message.messageid + "> get ready for some stuff...");
121             if (head) println(a.message.headers.raw);
122             if (head && body) println();
123             if (body) {
124                 Stream stream = new Stream(a.message.body);
125                 while(true) {
126                     s = stream.readln();
127                     if (s == null) break;
128                     if (s.startsWith(".")) print(".");
129                     println(s);
130                 }
131             }
132             println(".");
133         }
134         public void handleRequest(Connection conn) {
135             this.conn = conn;
136             conn.setTimeout(30 * 60 * 1000);
137             conn.setNewline("\r\n");
138             println("200 " + conn.vhost + " [" + NNTP.class.getName() + "]");
139             String user = null;
140             String pass = null;
141             Account account = login.anonymous();
142             this.api = account == null ? null : new MailboxWrapper(account.getMailbox(NNTP.class));
143             for(String line = conn.readln(); line != null; line = conn.readln()) try {
144                 Log.warn("[nntp-read]", line);
145                 StringTokenizer st = new StringTokenizer(line, " ");
146                 String command = st.nextToken().toUpperCase();
147                 if (command.equals("AUTHINFO")) {
148                     // FIXME technically the RFC says we need to use this info to generate a SEnder: header...
149                     String uop = st.nextToken().toUpperCase();
150                     if (uop.equals("USER")) user = st.nextToken();
151                     else if (uop.equals("PASS")) pass = st.nextToken();
152                     // FIXME error here
153                 }
154                 if (this.api == null) {
155                     if (user == null) { println("480 Authentication required"); continue; }
156                     if (pass == null) { println("381 Password required"); continue; }
157                     account = login.login(user, pass);
158                     if (account == null) { println("502 Invalid"); continue; }
159                     this.api = new MailboxWrapper(account.getMailbox(NNTP.class));
160                     println("281 Good to go");
161                     continue;
162                 }
163                 if        (command.equals("ARTICLE"))   { article(st.hasMoreTokens() ? st.nextToken() : null, true,  true); 
164                 } else if (command.equals("HEAD"))      { article(st.hasMoreTokens() ? st.nextToken() : null, true,  false); 
165                 } else if (command.equals("DATE"))      {
166                     // FIXME must be GMT
167                     println("111 " + dateFormat.format(new Date()));
168                 } else if (command.equals("MODE"))      {
169                     if (st.hasMoreTokens()) {
170                         String arg = st.nextToken();
171                         if (arg.equalsIgnoreCase("STREAM"));
172                         streaming = true;
173                         println("203 Streaming permitted");
174                     } else {
175                         println("201 Hello, you can post.");
176                     }
177                 } else if (command.equals("BODY"))      { article(st.hasMoreTokens() ? st.nextToken() : null, false, true); 
178                 } else if (command.equals("STAT"))      { article(st.hasMoreTokens() ? st.nextToken() : null, false, false); 
179                 } else if (command.equals("HELP"))      { println("100 you are beyond help."); println(".");
180                 } else if (command.equals("SLAVE"))     { println("220 I don't care");
181                 } else if (command.equals("XOVER"))     {
182                     println("224 Overview information follows");
183                     MailboxWrapper api = (MailboxWrapper)this.api;
184                     String range = st.hasMoreTokens() ? st.nextToken() : (api.ptr+"-"+api.ptr);
185                     int start = Integer.parseInt(range.substring(0, range.indexOf('-')));
186                     int end   = Integer.parseInt(range.substring(range.indexOf('-') + 1));
187                     Mailbox.Iterator it = api.current.iterator(Query.messagenum(start, end));
188                     while(it.next()) {
189                         Message m = it.cur();
190                         println(it.num()+"\t"+m.subject+"\t"+m.from+"\t"+m.date+"\t"+m.messageid+"\t"+
191                                 m.headers.gets("references") + "\t" + m.size() + "\t" + m.lines);
192                     }
193                     println(".");
194                 } else if (command.equals("LAST"))      { Article a = api.last(); println("223 "+a.num+" "+a.message.messageid+" ok");
195                 } else if (command.equals("NEXT"))      { Article a = api.next(); println("223 "+a.num+" "+a.message.messageid+" ok");
196                 } else if (command.equals("QUIT"))      { println("205 Bye."); conn.close(); return; 
197                 } else if (command.equals("GROUP"))     {
198                     Group g = api.group(st.nextToken().toLowerCase());
199                     println("211 " + g.count + " " + g.first + " " + g.last + " " + g.name);
200                 } else if (command.equals("NEWGROUPS") || command.equals("NEWNEWS")) { 
201                     // FIXME: * and ! unsupported
202                     // NEWNEWS is often not supported
203                     String groups = command.equals("NEWNEWS") ? st.nextToken() : null;
204                     String datetime = st.nextToken() + " " + st.nextToken();
205                     String gmt = st.nextToken();
206                     String distributions = gmt.equals("GMT") ? (st.hasMoreTokens() ? st.nextToken() : null) : gmt;
207                     while(st.hasMoreTokens()) distributions += " " + st.nextToken();
208                     // FIXME deal with GMT
209                     Date d = new Date();
210                     try {
211                         d = new SimpleDateFormat("YYMMDD HHMMSS").parse(gmt);
212                     } catch (ParseException pe) {
213                         Log.warn(this, pe);
214                     }
215                     distributions = distributions.trim();
216                     if (distributions.startsWith("<")) distributions = distributions.substring(1, distributions.length() - 1);
217
218                     st = new StringTokenizer(distributions, ",");
219                     String[] dists = new String[st.countTokens()];
220                     for(int i=0; st.hasMoreTokens(); i++) dists[i] = st.nextToken();
221
222                     if (command.equals("NEWGROUPS")) {
223                         Group[] g = api.newgroups(d, dists);
224                         println("231 list of groups follows");
225                         for(int i=0; i<g.length; i++)
226                             println(g[i].name + " " + g[i].last + " " + g[i].first + " " + (g[i].post ? "y" : "n"));
227                         println(".");
228                     } else {
229                         st = new StringTokenizer(groups, ",");
230                         String[] g = new String[st.countTokens()];
231                         for(int i=0; st.hasMoreTokens(); i++) g[i] = st.nextToken();
232                         String[] a = api.newnews(g, d, dists);
233                         println("230 list of article messageids follows");
234                         for(int i=0; i<a.length; i++) println(a[i]);
235                         println(".");
236                     }
237
238                 } else if (command.equals("POST"))      { 
239                     // add NNTP-Posting-Host header
240                     // FIXME
241                     // required headers: Newsgroups, Subject, Message-ID, Path, From, Date.  No wildcars in newsgroups list
242                     // Path header: prepend <myname>, (any punctuation separates the list)
243                     // Expires header: the date when expiration happens (??) should we ignore this?
244                     // Control header: body is the command.  Inteprert posts to all.all.ctl as control messages, use Subject line if no Cntrol line
245                     // "Approved" line is used for moderaion
246                     // Xref: drop this header if you see it
247
248                     // Control messages
249                     //   cancel <Message-ID>      (do not forward if I am unable to cancel locally)
250                     //   ihave/sendme:            do not support
251                     //   newgroup <groupname> [moderated] -- body of message is a description of the group
252                     //   rmgroup  <groupname>
253
254                     /*
255                     boolean postok = api.post();
256                     if (!postok) {
257                         println("440 no posting allowed");
258                     } else {
259                     */
260                         println("340 send the article");
261                         // FIME read the article here
262                         println("240 article posted ok");
263                         //}
264
265                 } else if (command.equals("LIST"))      { 
266                     Group[] g = api.list();
267                     println("215 list of groups follows");
268                     for(int i=0; i<g.length; i++)
269                         println(g[i].name + " " + g[i].last + " " + g[i].first + " " + (g[i].post ? "y" : "n"));
270                     println(".");
271                     // 412 if no group selected and numeric form used
272                     // 430 if <mid> and not found
273                     // 420 if no messages in range
274                 } else if (command.equals("XPAT"))      { 
275                     // just like XHDR, but a pattern follows the last argument (may contain whitespace)
276                     println("221 yep");
277                     // print 
278                     println(".");
279                 } else if (command.equals("LIST"))      { 
280                     if (st.hasMoreTokens()) {
281                         String argument = st.nextToken().toUppercase();
282                         if (argument.equalsIgnoreCase("EXTENSIONS")) {
283                             println("202 Extensions supported:");
284                             println("STREAMING");
285                             println("");
286                             println(".");
287                         } else if (argument.equals("ACTIVE")) {
288                             String wildmat = st.hasMoreTokens() ? st.nextToken() : null;
289                             // FIXME: deal with wildmat
290                             // just like list, but only show active groups
291                         } else if (argument.equals("SUBSCRIPTIONS")) {
292                             // FIXME: show 215, default subscription list for new users, period
293                         } else if (argument.equals("OVERVIEW.FMT")) {
294                             println("215 Overview format:");
295                             println("Subject:");
296                             println("From:");
297                             println("Date:");
298                             println("Message-ID:");
299                             println("References:");
300                             println("Bytes:");
301                             println("Lines:");
302                             //println("Xref:full");
303                             println(".");
304                         } else if (argument.equals("NEWSGROUPS")) {
305                             String wildmat = st.hasMoreTokens() ? st.nextToken() : null;
306                             // respond 215, print each newsgroup, a space, and the description; end with lone period
307                         } else {
308                             // barf here
309                         }
310                     } else {
311                         Group[] g = api.list();
312                         println("215 list of groups follows");
313                         for(int i=0; i<g.length; i++)
314                             println(g[i].name + " " + g[i].last + " " + g[i].first + " " + (g[i].post ? "y" : "n"));
315                         println(".");
316                     }
317
318                 } else if (command.equals("LISTGROUP"))     {
319                     String groupname = st.hasMoreTokens() ? st.nextToken() : null;
320                     // 211, all article numbers in group, period.  Set article ptr to first item in group
321
322                 } else if (command.equals("XGTITLE"))     {
323                     String wildmat = st.hasMoreTokens() ? st.nextToken() : null;
324                     // 282, then identical to LIST NEWSGROUP
325
326                 } else if (command.equals("CHECK"))     {
327                     // FIXME: may be pipelined; must spawn threads
328                     String mid = st.nextToken();
329                     boolean want = api.ihave(mid);
330                     if (!want) {
331                         println("438 "+ mid+" No thanks");
332                     } else {
333                         println("238 "+mid+" Yes, I'd like that");
334                     }
335
336                 } else if (command.equals("TAKETHIS"))     {
337                     // FIXME: may be pipelined
338                     String mid = st.nextToken();
339                     // MUST read message here
340                     if (!want) {
341                         println("439 "+ mid+" Transfer failed");
342                     } else {
343                         println("239 "+mid+" Rock on.");
344                     }
345
346                 } else if (command.equals("IHAVE"))     {
347                     boolean want = api.ihave(st.nextToken());
348                     if (!want) {
349                         println("435 No thanks");
350                     } else {
351                         println("335 Proceed");
352                         // FIXME read article here
353                         println("235 Got it");
354                     }
355                 } else {
356                     throw new Bad("wtf are you talking about?");
357                 }
358             } catch (No n)  { println(n.code + " " + n.getMessage());
359             } catch (Bad b) { println(b.code + " " + b.getMessage()); Log.warn(this, b); }
360             conn.close();
361         }
362     }
363 }