01: /*
02: * Copyright 2001-2005 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package examples.nntp;
18:
19: import java.io.IOException;
20: import java.io.PrintWriter;
21: import java.net.SocketException;
22:
23: import org.apache.commons.net.nntp.Article;
24: import org.apache.commons.net.nntp.NNTPClient;
25: import org.apache.commons.net.nntp.NewsgroupInfo;
26: import org.apache.commons.net.nntp.Threader;
27:
28: import examples.PrintCommandListener;
29:
30: public class MessageThreading {
31: public MessageThreading() {
32: }
33:
34: public static void main(String[] args) throws SocketException,
35: IOException {
36:
37: if (args.length != 3)
38: usage();
39:
40: String hostname = args[0];
41: String user = args[1];
42: String password = args[2];
43:
44: NNTPClient client = new NNTPClient();
45: client.addProtocolCommandListener(new PrintCommandListener(
46: new PrintWriter(System.out)));
47: client.connect(hostname);
48:
49: if (!client.authenticate(user, password)) {
50: System.out.println("Authentication failed for user " + user
51: + "!");
52: System.exit(1);
53: }
54:
55: NewsgroupInfo group = new NewsgroupInfo();
56: client.selectNewsgroup("comp.lang.lisp", group);
57:
58: int lowArticleNumber = group.getFirstArticle();
59: int highArticleNumber = lowArticleNumber + 100;
60:
61: System.out.println("Retrieving articles between ["
62: + lowArticleNumber + "] and [" + highArticleNumber
63: + "]");
64: Article[] articles = NNTPUtils.getArticleInfo(client,
65: lowArticleNumber, highArticleNumber);
66:
67: System.out.println("Building message thread tree...");
68: Threader threader = new Threader();
69: Article root = (Article) threader.thread(articles);
70:
71: Article.printThread(root, 0);
72:
73: }
74:
75: public static void usage() {
76: System.out
77: .println("Usage: MessageThreading <hostname> <user> <password>");
78: System.exit(0);
79: }
80: }
|