nntp improvements: posting, authentication
authoradam <adam@megacz.com>
Sun, 21 Jan 2007 23:11:30 +0000 (23:11 +0000)
committeradam <adam@megacz.com>
Sun, 21 Jan 2007 23:11:30 +0000 (23:11 +0000)
darcs-hash:20070121231130-5007d-3fc83a55a5789e64c26d134dc1352b25e864877d.gz

src/org/ibex/mail/protocol/NNTP.java

index b59a15e..44a5ca3 100644 (file)
@@ -65,8 +65,7 @@ public class NNTP {
         public MailboxWrapper(Mailbox root, boolean post) { this.root = root; this.post = post; }
         public boolean  postok() { return post; }
         public void     post(Message m) throws IOException { current.accept(m); }
-
-        public Group    group(String s)         {
+        public Group    group(String s) {
             ptr = 0;
             Group g = getgroup(s);
             if (g==null) return null;
@@ -112,6 +111,8 @@ public class NNTP {
         }
         private Group getgroup(String s) {
             Mailbox box = root;
+            Log.error("", "getgroup " + s);
+            Log.error("", "mailbox " + root);
             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()));
@@ -171,19 +172,24 @@ public class NNTP {
                 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 (uop.equals("USER")) {
+                        user = st.nextToken();
+                        println("381 More authentication required");
+                        continue;
+                    } else if (uop.equals("PASS")) {
+                        pass = st.nextToken();
+                        account = login.login(user, pass);
+                        if (account == null) { println("502 Invalid"); continue; }
+                        Mailbox box = account.getMailbox(NNTP.class);
+                        this.api = new MailboxWrapper(box, true);
+                        println("281 Good to go");
+                        continue;
+                    }
+                    throw new Bad("wtf are you talking about?");
                 }
                 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; }
-                    Mailbox box = account.getMailbox(NNTP.class);
-                    this.api = new MailboxWrapper(box, true);
-                    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); 
@@ -265,9 +271,8 @@ public class NNTP {
                     }
 
                 } 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
+                    // add NNTP-Posting-Host header
                     // 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
@@ -295,11 +300,26 @@ public class NNTP {
                      }
                      String body = buf.toString();
                      try {
-                       api.post(Message.newMessage(new Fountain.StringFountain(body)));
-                       println("240 article posted ok");
+                          Message m = Message.newMessage(new Fountain.StringFountain(body));
+                          if (m.headers.get("newsgroups")==null)
+                              println("441 posted messages must have a Newsgroups header per RFC 977");
+                          else if (m.headers.get("newsgroups").indexOf('*')!=-1)
+                              println("441 Newsgroups header in posted messages may not contain wildcards (*) per RFC 977");
+                          else if (m.headers.get("subject")==null)
+                              println("441 posted messages must have a Subject header per RFC 977");
+                          //                          else if (m.headers.get("path")==null)
+                          //println("441 posted messages must have a Path header per RFC 977");
+                          else if (m.headers.get("from")==null)
+                              println("441 posted messages must have a From header per RFC 977");
+                          else if (m.headers.get("date")==null)
+                              println("441 posted messages must have a Date header per RFC 977");
+                          else {
+                              api.post(m);
+                              println("240 article posted ok");
+                          }
                      } catch (Exception e) {
-                       e.printStackTrace();
-                       println("441 posting failed: " + e);
+                          e.printStackTrace();
+                          println("441 posting failed: " + e);
                      }
                    }