/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package examples.nntp;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.nntp.Article;
import org.apache.commons.net.nntp.NNTPClient;
import org.apache.commons.net.nntp.NewsgroupInfo;


/**
 * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
 * 
 * @author Rory Winston <rwinston@apache.org>
 */
public class ExtendedNNTPOps {

    
    NNTPClient client;

    public ExtendedNNTPOps() {
        client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    }


    public void demo(String host, String user, String password) {
        try {
            client.connect(host);

            // AUTHINFO USER/AUTHINFO PASS
            boolean success = client.authenticate(user, password);
            if (success) {
                System.out.println("Authentication succeeded");
            } else {
                System.out.println("Authentication failed, error =" + client.getReplyString());
            }

            // XOVER
            NewsgroupInfo testGroup = new NewsgroupInfo();
            client.selectNewsgroup("alt.test", testGroup);
            long lowArticleNumber = testGroup.getFirstArticle();
            long  highArticleNumber = lowArticleNumber + 100;
            List<Article> articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);

            for (Article article : articles) {
                System.out.println(article.getSubject());
            }

            // LIST ACTIVE
            NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
            for (int i = 0; i < fanGroups.length; ++i) {
                System.out.println(fanGroups[i].getNewsgroup());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ExtendedNNTPOps ops;

        if (args.length != 3) {
            System.err.println("usage: ExtendedNNTPOps nntpserver username password");
            System.exit(1);
        }

        ops = new ExtendedNNTPOps();
        ops.demo(args[0], args[1], args[2]);
    }

}

/* Emacs configuration
 * Local variables:        **
 * mode:             java  **
 * c-basic-offset:   4     **
 * indent-tabs-mode: nil   **
 * End:                    **
 */

@@ -880,31 +880,8 @@
 ter;
-%0Aimport java.util.List;
 %0A%0Aim
@@ -1450,21 +1450,22 @@
 %0A%0A%0A    p
-ublic
+rivate
  void de
@@ -1604,16 +1604,72 @@
 FO PASS%0A
+            if (user != null && password != null) %7B%0A    
         
@@ -1735,24 +1735,28 @@
             
+ 
+   
 if (success)
@@ -1754,24 +1754,28 @@
 (success) %7B%0A
+    
             
@@ -1822,24 +1822,28 @@
 ucceeded%22);%0A
+    
             
@@ -1859,32 +1859,36 @@
                 
+    
 System.out.print
@@ -1947,24 +1947,42 @@
 yString());%0A
+                %7D%0A
             
@@ -2262,20 +2262,24 @@
         
-List
+Iterable
 %3CArticle
@@ -2295,21 +2295,22 @@
 s = 
-NNTPUtils.get
+client.iterate
 Arti
@@ -2321,16 +2321,8 @@
 nfo(
-client, 
 lowA
@@ -2352,24 +2352,64 @@
 leNumber);%0A%0A
+            if (articles != null) %7B%0A    
             
@@ -2439,24 +2439,28 @@
 articles) %7B%0A
+    
             
@@ -2501,24 +2501,42 @@
 Subject());%0A
+                %7D%0A
             
@@ -2529,33 +2529,32 @@
 %7D%0A            %7D%0A
-%0A
             // L
@@ -2945,19 +2945,26 @@
        i
-f (
+nt argc = 
 args.len
@@ -2970,13 +2970,30 @@
 ngth
- != 3
+;%0A        if (argc %3C 1
 ) %7B%0A
@@ -3058,16 +3058,17 @@
 pserver 
+%5B
 username
@@ -3076,16 +3076,17 @@
 password
+%5D
 %22);%0A    
@@ -3190,21 +3190,57 @@
  arg
-s%5B1%5D, args%5B2%5D
+c %3E=3 ? args%5B1%5D : null, argc %3E=3 ? args%5B2%5D : null
 );%0A 

Rework to use iterateArticleInfo instead of creating unnecessary List