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