001    /*
002     * Copyright 2001-2005 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package examples.nntp;
018    
019    import java.io.IOException;
020    import java.io.PrintWriter;
021    import java.net.SocketException;
022    
023    import org.apache.commons.net.nntp.Article;
024    import org.apache.commons.net.nntp.NNTPClient;
025    import org.apache.commons.net.nntp.NewsgroupInfo;
026    import org.apache.commons.net.nntp.Threader;
027    
028    import examples.PrintCommandListener;
029    
030    public class MessageThreading {
031            public MessageThreading() {
032            }
033            
034            public static void main(String[] args) throws SocketException, IOException {
035                    
036                    if (args.length != 3)
037                            usage();
038                    
039                    String hostname = args[0];
040                    String user = args[1];
041                    String password = args[2];
042                    
043                    NNTPClient client = new NNTPClient();
044                    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
045                    client.connect(hostname);
046                    
047                    if(!client.authenticate(user, password)) {
048                            System.out.println("Authentication failed for user " + user + "!");
049                            System.exit(1);
050                    }
051                    
052                    NewsgroupInfo group = new NewsgroupInfo();
053                    client.selectNewsgroup("comp.lang.lisp", group);
054                    
055                    int lowArticleNumber = group.getFirstArticle();
056                    int highArticleNumber = lowArticleNumber + 100;
057                    
058                    System.out.println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]");
059                    Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
060                    
061                    System.out.println("Building message thread tree...");
062                    Threader threader = new Threader();
063                    Article root = (Article)threader.thread(articles);
064                    
065                    Article.printThread(root, 0);   
066                    
067            }
068            
069            
070            public static void usage() {
071                    System.out.println("Usage: MessageThreading <hostname> <user> <password>");
072                    System.exit(0);
073            }
074    }