robustness hacks for NNTP
[org.ibex.mail.git] / src / org / ibex / mail / protocol / NNTP.java
index ef5fc0c..6d441e7 100644 (file)
@@ -1,3 +1,13 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
+// 500 unrec. command
+// 501 syntax error
+// 503 optional subfeature not supported 
+// Xref header
+// LIST EXTENSIONS is probably incomplete
+
 package org.ibex.mail.protocol;
 import org.ibex.util.*;
 import org.ibex.io.*;
@@ -12,23 +22,22 @@ import java.text.*;
 /** NNTP send/recieve */
 public class NNTP {
 
-    // FIXME: command lines limited to 512 chars
+    public static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMDDhhmmss");
 
-    public static class No  extends RuntimeException { int code = 0; }    // 4xx response codes
-    public static class Bad extends RuntimeException { int code = 0; }    // 5xx response codes
+    public static class No  extends RuntimeException { int code = 400; }    // 4xx response codes
+    public static class Bad extends RuntimeException { int code = 500; public Bad(String s) { super(s); } }    // 5xx response codes
 
     public static class Group {
         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;}
-        public final String  name;
+        public final String  name;    // case insensitive
         public final boolean post;
         public final int     first;
         public final int     last;
-        public final int     count;
+        public final int     count;   // an approximation; must be >= actual number
     }
 
     public static class Article {
-        public Article(String messageid, int num, Message message) { this.message=message;this.messageid=messageid;this.num=num;}
-        public final String  messageid;
+        public Article(int num, Message message) { this.message = message; this.num = num;}
         public final int     num;
         public final Message message;
     }
@@ -51,22 +60,33 @@ public class NNTP {
         private Mailbox current;
         private int ptr = 0;
         public MailboxWrapper(Mailbox root)     { this.root = root; }
-        public Group    group(String s)         { ptr = 0; setgroup(s); return getgroup(s); }
+        public Group    group(String s)         {
+            ptr = 0;
+            Group g = getgroup(s);
+            if (g==null) return null;
+            setgroup(s);
+            return g;
+        }
+
         public boolean  ihave(String messageid) { /* FEATURE */ return false; }
         public boolean  post(Message m)         { /* FEATURE */ return false; }
+
         public Article  next()                  { return article(ptr++, false, false); }
         public Article  last()                  { return article(ptr--, false, false); }
         public Article  article(String i, boolean h, boolean b) { return article(Query.header("message-id",i),h,b); }
         public Article  article(int    n, boolean h, boolean b) { ptr = n; return article(Query.messagenum(n,n),h,b); }
-        private Article article(Query q, boolean head, boolean body) {
+        private Article article(Query q,  boolean head, boolean body) {
             Mailbox.Iterator it = current.iterator(q);
             if (!it.next()) return null;
-            Message m = body ? it.cur() : it.head();
-            return new Article(m.messageid, it.num(), m);
+            try {
+                Message m = body ? it.cur() : Message.newMessage(new Fountain.StringFountain(it.head() + "\r\n"));
+                return new Article(it.num(), m);
+            } catch (Exception e) { return null; }
         }
         public Group[]  list() { return list(root, ""); }
         private Group[] list(Mailbox who, String prefix) {
             Vec v = new Vec();
+            if (who == null) who = root;
             String[] s = who.children();
             for(int i=0; i<s.length; i++) {
                 v.addElement(new Group(prefix + s[i], true, 0, 0, 0)); // FIXME numbers
@@ -79,66 +99,139 @@ public class NNTP {
         }
 
         private void setgroup(String s) {
-            current = root;
+            Mailbox ncurrent = root;
             for(StringTokenizer st = new StringTokenizer(s, ".");
-                st.hasMoreTokens();
-                current = current.slash(st.nextToken(), false));
+                ncurrent != null && st.hasMoreTokens();
+                ncurrent = ncurrent.slash(st.nextToken(), false));
+            if (ncurrent!=null) current=ncurrent;
         }
         private Group getgroup(String s) {
             Mailbox box = root;
-            for(StringTokenizer st = new StringTokenizer(s, "."); st.hasMoreTokens(); box = box.slash(st.nextToken(), false));
-            return new Group(s, true, 0, box.count(Query.all()), box.count(Query.all()));
+            for(StringTokenizer st = new StringTokenizer(s, "."); box!=null && st.hasMoreTokens(); box = box.slash(st.nextToken(), false));
+            if (box==null) return null;
+            return new Group(s, true, 1, box.count(Query.all()), box.count(Query.all()));
         }
 
         public Group[]  newgroups(Date d, String[] distributions) { /* FEATURE */ return new Group[] { }; }
         public String[] newnews(String[] groups, Date d, String[] distributions) { /* FIXME */  return null; }
     }
 
-    public static class Listener implements Worker {
-        private Server api;
+    public static class Listener {
+        private Server api = null;
+        private Login login;
         private Connection conn;
+        public Listener(Login l) { this.login = l; }
+
+        private void println(String s) { Log.warn("[nntp-write]", s); conn.println(s); }
+        private void println() { Log.warn("[nntp-write]", ""); conn.println(""); }
+        private void print(String s) { Log.warn("[nntp-write]", s); conn.print(s); }
+
         private void article(String numOrMessageId, boolean head, boolean body) {
             String s = numOrMessageId.trim();
             Article a;
             if (s.startsWith("<")) a = api.article(s.substring(0, s.length() - 1), head, body);
             else                   a = api.article(Integer.parseInt(s), head, body);
+            if (a == null) {
+                println("423 No such article.");
+                return;
+            }
             int code = (head && body) ? 220 : head ? 221 : body ? 222 : 223;
-            conn.println(code + " " + a.num + " <" + a.messageid + "> get ready for some stuff...");
-            if (head) conn.println(a.message.allHeaders);
-            if (head && body) conn.println();
-            if (body) conn.println(a.message.body);
-            conn.println(".");
+            println(code + " " + a.num + " <" + a.message.messageid + "> get ready for some stuff...");
+            if (head) println(a.message.headers.getString());
+            if (head && body) println();
+            if (body) {
+                Stream stream = a.message.getBody().getStream();
+                while(true) {
+                    s = stream.readln();
+                    if (s == null) break;
+                    if (s.startsWith(".")) print(".");
+                    println(s);
+                }
+            }
+            println(".");
         }
         public void handleRequest(Connection conn) {
             this.conn = conn;
-            api = new NNTP.MailboxWrapper(null);  // FIXME
             conn.setTimeout(30 * 60 * 1000);
-            conn.println("200 " + conn.vhost + " [" + NNTP.class.getName() + "]");
+            conn.setNewline("\r\n");
+            println("200 " + conn.vhost + " [" + NNTP.class.getName() + "]");
+            String user = null;
+            String pass = null;
+            Account account = login.anonymous();
+            this.api = account == null ? null : new MailboxWrapper(account.getMailbox(NNTP.class));
             for(String line = conn.readln(); line != null; line = conn.readln()) try {
+                Log.warn("[nntp-read]", line);
                 StringTokenizer st = new StringTokenizer(line, " ");
                 String command = st.nextToken().toUpperCase();
+                if (command.equals("AUTHINFO")) {
+                    // FIXME technically the RFC says we need to use this info to generate a SEnder: header...
+                    String uop = st.nextToken().toUpperCase();
+                    if (uop.equals("USER")) user = st.nextToken();
+                    else if (uop.equals("PASS")) pass = st.nextToken();
+                    // FIXME error here
+                }
+                if (this.api == null) {
+                    if (user == null) { println("480 Authentication required"); continue; }
+                    if (pass == null) { println("381 Password required"); continue; }
+                    account = login.login(user, pass);
+                    if (account == null) { println("502 Invalid"); continue; }
+                    this.api = new MailboxWrapper(account.getMailbox(NNTP.class));
+                    println("281 Good to go");
+                    continue;
+                }
                 if        (command.equals("ARTICLE"))   { article(st.hasMoreTokens() ? st.nextToken() : null, true,  true); 
                 } else if (command.equals("HEAD"))      { article(st.hasMoreTokens() ? st.nextToken() : null, true,  false); 
+                } else if (command.equals("DATE"))      {
+                    // FIXME must be GMT
+                    println("111 " + dateFormat.format(new Date()));
+                } else if (command.equals("MODE"))      {
+                    if (st.hasMoreTokens()) {
+                        String arg = st.nextToken();
+                        if (arg.equalsIgnoreCase("STREAM"));
+                        //streaming = true;
+                        println("203 Streaming permitted");
+                    } else {
+                        println("201 Hello, you can post.");
+                    }
                 } else if (command.equals("BODY"))      { article(st.hasMoreTokens() ? st.nextToken() : null, false, true); 
                 } else if (command.equals("STAT"))      { article(st.hasMoreTokens() ? st.nextToken() : null, false, false); 
-                } else if (command.equals("HELP"))      { conn.println("100 you are beyond help."); conn.println(".");
-                } else if (command.equals("SLAVE"))     { conn.println("220 I don't care");
-                } else if (command.equals("LAST"))      { Article a = api.last(); conn.println("223 "+a.num+" "+a.messageid+" ok");
-                } else if (command.equals("NEXT"))      { Article a = api.next(); conn.println("223 "+a.num+" "+a.messageid+" ok");
-                } else if (command.equals("QUIT"))      { conn.println("205 Bye."); conn.close(); return; 
+                } else if (command.equals("HELP"))      { println("100 you are beyond help."); println(".");
+                } else if (command.equals("SLAVE"))     { println("220 I don't care");
+                } else if (command.equals("XOVER"))     {
+                    println("224 Overview information follows");
+                    MailboxWrapper api = (MailboxWrapper)this.api;
+                    String range = st.hasMoreTokens() ? st.nextToken() : (api.ptr+"-"+api.ptr);
+                    int start = Integer.parseInt(range.substring(0, range.indexOf('-')));
+                    int end   = Integer.parseInt(range.substring(range.indexOf('-') + 1));
+                    Mailbox.Iterator it = api.current.iterator(Query.messagenum(start, end));
+                    while(it.next()) {
+                        try {
+                            Message m = it.cur();
+                            println(it.num()+"\t"+m.subject+"\t"+m.from+"\t"+m.date+"\t"+m.messageid+"\t"+
+                                    m.headers.get("references") + "\t" + m.getLength() + "\t" + m.getNumLines());
+                        } catch (Exception e) { Log.error(this, e); }
+                    }
+                    println(".");
+                } else if (command.equals("LAST"))      { Article a = api.last(); println("223 "+a.num+" "+a.message.messageid+" ok");
+                } else if (command.equals("NEXT"))      { Article a = api.next(); println("223 "+a.num+" "+a.message.messageid+" ok");
+                } else if (command.equals("QUIT"))      { println("205 Bye."); conn.close(); return; 
                 } else if (command.equals("GROUP"))     {
                     Group g = api.group(st.nextToken().toLowerCase());
-                    conn.println("221 " + g.count + " " + g.first + " " + g.last + " " + g.name);
+                    if (g==null) println("411 no such group");
+                    else         println("211 " + g.count + " " + g.first + " " + g.last + " " + g.name);
                 } else if (command.equals("NEWGROUPS") || command.equals("NEWNEWS")) { 
+                    // FIXME: * and ! unsupported
+                    // NEWNEWS is often not supported
                     String groups = command.equals("NEWNEWS") ? st.nextToken() : null;
                     String datetime = st.nextToken() + " " + st.nextToken();
                     String gmt = st.nextToken();
-                    String distributions = gmt.equals("GMT") ? (st.hasMoreTokens() ? st.nextToken() : null) : gmt;
+                    String distributions = gmt.equals("GMT") ? (st.hasMoreTokens() ? st.nextToken() : "") : gmt;
                     while(st.hasMoreTokens()) distributions += " " + st.nextToken();
+
                     // FIXME deal with GMT
                     Date d = new Date();
                     try {
-                        d = new SimpleDateFormat("YYMMDD HHMMSS").parse(gmt);
+                        d = new SimpleDateFormat("yyMMDD HHMMSS").parse(datetime);
                     } catch (ParseException pe) {
                         Log.warn(this, pe);
                     }
@@ -151,52 +244,147 @@ public class NNTP {
 
                     if (command.equals("NEWGROUPS")) {
                         Group[] g = api.newgroups(d, dists);
-                        conn.println("231 list of groups follows");
+                        println("231 list of groups follows");
                         for(int i=0; i<g.length; i++)
-                            conn.println(g[i].name + " " + g[i].last + " " + g[i].first + " " + (g[i].post ? "y" : "n"));
-                        conn.println(".");
+                            println(g[i].name + " " + g[i].last + " " + g[i].first + " " + (g[i].post ? "y" : "n"));
+                        println(".");
                     } else {
                         st = new StringTokenizer(groups, ",");
                         String[] g = new String[st.countTokens()];
                         for(int i=0; st.hasMoreTokens(); i++) g[i] = st.nextToken();
                         String[] a = api.newnews(g, d, dists);
-                        conn.println("230 list of article messageids follows");
-                        for(int i=0; i<a.length; i++) conn.println(a[i]);
-                        conn.println(".");
+                        println("230 list of article messageids follows");
+                        for(int i=0; i<a.length; i++) println(a[i]);
+                        println(".");
                     }
 
                 } else if (command.equals("POST"))      { 
+                    // add NNTP-Posting-Host header
                     // FIXME
+                    // required headers: Newsgroups, Subject, Message-ID, Path, From, Date.  No wildcars in newsgroups list
+                    // Path header: prepend <myname>, (any punctuation separates the list)
+                    // Expires header: the date when expiration happens (??) should we ignore this?
+                    // Control header: body is the command.  Inteprert posts to all.all.ctl as control messages, use Subject line if no Cntrol line
+                    // "Approved" line is used for moderaion
+                    // Xref: drop this header if you see it
+
+                    // Control messages
+                    //   cancel <Message-ID>      (do not forward if I am unable to cancel locally)
+                    //   ihave/sendme:            do not support
+                    //   newgroup <groupname> [moderated] -- body of message is a description of the group
+                    //   rmgroup  <groupname>
+
                     /*
                     boolean postok = api.post();
                     if (!postok) {
-                        conn.println("440 no posting allowed");
+                        println("440 no posting allowed");
                     } else {
                     */
-                        conn.println("340 send the article");
+                        println("340 send the article");
                         // FIME read the article here
-                        conn.println("240 article posted ok");
+                        println("240 article posted ok");
                         //}
 
+                } else if (command.equals("XROVER"))      { 
+                    // equivalent to "XHDR References"
+                } else if (command.equals("XHDR"))      { 
+                    // argument: header name
+                    // argument: 1 | 1- | 1-2 | <mid> | nothing (use current article)
+                    println("221 yep");
+                    // print art#+header for all matching messages
+                    println(".");
+                    // 412 if no group selected and numeric form used
+                    // 430 if <mid> and not found
+                    // 420 if no messages in range
+                } else if (command.equals("XPAT"))      { 
+                    // just like XHDR, but a pattern follows the last argument (may contain whitespace)
+                    println("221 yep");
+                    // print 
+                    println(".");
                 } else if (command.equals("LIST"))      { 
-                    Group[] g = api.list();
-                    conn.println("215 list of groups follows");
-                    for(int i=0; i<g.length; i++)
-                        conn.println(g[i].name + " " + g[i].last + " " + g[i].first + " " + (g[i].post ? "y" : "n"));
-                    conn.println(".");
+                    if (st.hasMoreTokens()) {
+                        String argument = st.nextToken().toUpperCase();
+                        if (argument.equalsIgnoreCase("EXTENSIONS")) {
+                            println("202 Extensions supported:");
+                            println("STREAMING");
+                            println("");
+                            println(".");
+                        } else if (argument.equals("ACTIVE")) {
+                            String wildmat = st.hasMoreTokens() ? st.nextToken() : null;
+                            // FIXME: deal with wildmat
+                            // just like list, but only show active groups
+                            throw new Bad("not implemented yet");
+                        } else if (argument.equals("SUBSCRIPTIONS")) {
+                            // FIXME: show 215, default subscription list for new users, period
+                        } else if (argument.equals("OVERVIEW.FMT")) {
+                            println("215 Overview format:");
+                            println("Subject:");
+                            println("From:");
+                            println("Date:");
+                            println("Message-ID:");
+                            println("References:");
+                            println("Bytes:");
+                            println("Lines:");
+                            //println("Xref:full");
+                            println(".");
+                        } else if (argument.equals("NEWSGROUPS")) {
+                            String wildmat = st.hasMoreTokens() ? st.nextToken() : null;
+                            // respond 215, print each newsgroup, a space, and the description; end with lone period
+                        } else {
+                            // barf here
+                        }
+                    } else {
+                        Group[] g = api.list();
+                        println("215 list of groups follows");
+                        for(int i=0; i<g.length; i++)
+                            println(g[i].name + " " + g[i].last + " " + g[i].first + " " + (g[i].post ? "y" : "n"));
+                        println(".");
+                    }
+
+                } else if (command.equals("LISTGROUP"))     {
+                    String groupname = st.hasMoreTokens() ? st.nextToken() : null;
+                    // 211, all article numbers in group, period.  Set article ptr to first item in group
+
+                } else if (command.equals("XGTITLE"))     {
+                    String wildmat = st.hasMoreTokens() ? st.nextToken() : null;
+                    // 282, then identical to LIST NEWSGROUP
+
+                } else if (command.equals("CHECK"))     {
+                    // FIXME: may be pipelined; must spawn threads
+                    String mid = st.nextToken();
+                    boolean want = api.ihave(mid);
+                    if (!want) {
+                        println("438 "+ mid+" No thanks");
+                    } else {
+                        println("238 "+mid+" Yes, I'd like that");
+                    }
+
+                } else if (command.equals("TAKETHIS"))     {
+                    // FIXME: may be pipelined
+                    String mid = st.nextToken();
+                    // MUST read message here
+                    /*
+                    if (!want) {
+                        println("439 "+ mid+" Transfer failed");
+                    } else {
+                        println("239 "+mid+" Rock on.");
+                    }
+                    */
 
                 } else if (command.equals("IHAVE"))     {
                     boolean want = api.ihave(st.nextToken());
                     if (!want) {
-                        conn.println("435 No thanks");
+                        println("435 No thanks");
                     } else {
-                        conn.println("335 Proceed");
+                        println("335 Proceed");
                         // FIXME read article here
-                        conn.println("235 Got it");
+                        println("235 Got it");
                     }
+                } else {
+                    throw new Bad("wtf are you talking about?");
                 }
-            } catch (No n)  { conn.println(n.code + " " + n.getMessage());
-            } catch (Bad b) { conn.println(b.code + " " + b.getMessage()); Log.warn(this, b); }
+            } catch (No n)  { println(n.code + " " + n.getMessage());
+            } catch (Bad b) { println(b.code + " " + b.getMessage()); Log.warn(this, b); }
             conn.close();
         }
     }