1 package org.ibex.mail.protocol;
2 import org.ibex.util.*;
4 import org.ibex.mail.*;
5 import org.ibex.mail.target.*;
6 import org.ibex.jinetd.*;
12 /** NNTP send/recieve */
15 public static final DateFormat dateFormat = new SimpleDateFormat("YYYYMMDDhhmmss");
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
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
29 public static class Article {
30 public Article(int num, Message message) { this.message = message; this.num = num;}
32 public final Message message;
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);
48 public static class MailboxWrapper implements Server {
49 private final Mailbox root;
50 private Mailbox current;
52 public MailboxWrapper(Mailbox root) { this.root = root; }
53 public Group group(String s) { ptr = 0; setgroup(s); return getgroup(s); }
55 public boolean ihave(String messageid) { /* FEATURE */ return false; }
56 public boolean post(Message m) { /* FEATURE */ return false; }
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);
69 public Group[] list() { return list(root, ""); }
70 private Group[] list(Mailbox who, String prefix) {
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]);
79 Group[] ret = new Group[v.size()];
84 private void setgroup(String s) {
86 for(StringTokenizer st = new StringTokenizer(s, ".");
88 current = current.slash(st.nextToken(), false));
90 private Group getgroup(String s) {
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()));
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; }
100 public static class Listener implements Worker {
101 private Server api = null;
103 private Connection conn;
104 public Listener(Login l) { this.login = l; }
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); }
110 private void article(String numOrMessageId, boolean head, boolean body) {
111 String s = numOrMessageId.trim();
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);
116 println("423 No such article.");
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();
124 Stream stream = new Stream(a.message.body);
127 if (s == null) break;
128 if (s.startsWith(".")) print(".");
134 public void handleRequest(Connection conn) {
136 conn.setTimeout(30 * 60 * 1000);
137 conn.setNewline("\r\n");
138 println("200 " + conn.vhost + " [" + NNTP.class.getName() + "]");
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();
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");
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")) {
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"));
173 println("203 Streaming permitted");
175 println("201 Hello, you can post.");
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));
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);
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
211 d = new SimpleDateFormat("YYMMDD HHMMSS").parse(gmt);
212 } catch (ParseException pe) {
215 distributions = distributions.trim();
216 if (distributions.startsWith("<")) distributions = distributions.substring(1, distributions.length() - 1);
218 st = new StringTokenizer(distributions, ",");
219 String[] dists = new String[st.countTokens()];
220 for(int i=0; st.hasMoreTokens(); i++) dists[i] = st.nextToken();
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"));
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]);
238 } else if (command.equals("POST")) {
239 // add NNTP-Posting-Host header
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
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>
255 boolean postok = api.post();
257 println("440 no posting allowed");
260 println("340 send the article");
261 // FIME read the article here
262 println("240 article posted ok");
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"));
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)
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");
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:");
298 println("Message-ID:");
299 println("References:");
302 //println("Xref:full");
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
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"));
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
322 } else if (command.equals("XGTITLE")) {
323 String wildmat = st.hasMoreTokens() ? st.nextToken() : null;
324 // 282, then identical to LIST NEWSGROUP
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);
331 println("438 "+ mid+" No thanks");
333 println("238 "+mid+" Yes, I'd like that");
336 } else if (command.equals("TAKETHIS")) {
337 // FIXME: may be pipelined
338 String mid = st.nextToken();
339 // MUST read message here
341 println("439 "+ mid+" Transfer failed");
343 println("239 "+mid+" Rock on.");
346 } else if (command.equals("IHAVE")) {
347 boolean want = api.ihave(st.nextToken());
349 println("435 No thanks");
351 println("335 Proceed");
352 // FIXME read article here
353 println("235 Got it");
356 throw new Bad("wtf are you talking about?");
358 } catch (No n) { println(n.code + " " + n.getMessage());
359 } catch (Bad b) { println(b.code + " " + b.getMessage()); Log.warn(this, b); }