mailing list improvements (and nntp)
[org.ibex.mail.git] / src / org / ibex / mail / protocol / NNTP.java
index 6d441e7..b59a15e 100644 (file)
@@ -47,7 +47,8 @@ public class NNTP {
         public boolean  ihave(String messageid);
         public Article  next();
         public Article  last();
-        public boolean  post(Message m);
+        public boolean  postok();
+        public void     post(Message m) throws IOException;
         public Article  article(String messageid,  boolean head, boolean body);
         public Article  article(int    messagenum, boolean head, boolean body);
         public Group[]  list();
@@ -59,7 +60,12 @@ public class NNTP {
         private final Mailbox root;
         private Mailbox current;
         private int ptr = 0;
-        public MailboxWrapper(Mailbox root)     { this.root = root; }
+        private boolean post;
+        public MailboxWrapper(Mailbox root) { this(root, false); }
+        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)         {
             ptr = 0;
             Group g = getgroup(s);
@@ -69,7 +75,6 @@ public class NNTP {
         }
 
         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); }
@@ -158,7 +163,7 @@ public class NNTP {
             String user = null;
             String pass = null;
             Account account = login.anonymous();
-            this.api = account == null ? null : new MailboxWrapper(account.getMailbox(NNTP.class));
+            this.api = account == null ? null : new MailboxWrapper(account.getMailbox(NNTP.class), true);
             for(String line = conn.readln(); line != null; line = conn.readln()) try {
                 Log.warn("[nntp-read]", line);
                 StringTokenizer st = new StringTokenizer(line, " ");
@@ -175,7 +180,8 @@ public class NNTP {
                     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));
+                    Mailbox box = account.getMailbox(NNTP.class);
+                    this.api = new MailboxWrapper(box, true);
                     println("281 Good to go");
                     continue;
                 }
@@ -274,16 +280,28 @@ public class NNTP {
                     //   newgroup <groupname> [moderated] -- body of message is a description of the group
                     //   rmgroup  <groupname>
 
-                    /*
-                    boolean postok = api.post();
+                    boolean postok = api.postok();
                     if (!postok) {
                         println("440 no posting allowed");
                     } else {
-                    */
-                        println("340 send the article");
-                        // FIME read the article here
-                        println("240 article posted ok");
-                        //}
+                     println("340 send the article");
+                     StringBuffer buf = new StringBuffer();
+                     while(true) {
+                       String s = conn.readln();
+                       if (s == null) throw new RuntimeException("connection closed");
+                       if (s.equals(".")) break;
+                       if (s.startsWith(".")) s = s.substring(1);
+                       buf.append(s + "\r\n");
+                     }
+                     String body = buf.toString();
+                     try {
+                       api.post(Message.newMessage(new Fountain.StringFountain(body)));
+                       println("240 article posted ok");
+                     } catch (Exception e) {
+                       e.printStackTrace();
+                       println("441 posting failed: " + e);
+                     }
+                   }
 
                 } else if (command.equals("XROVER"))      { 
                     // equivalent to "XHDR References"