1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.nntp;
17
18 import java.io.IOException;
19 import java.io.PrintWriter;
20
21 import org.apache.commons.net.nntp.Article;
22 import org.apache.commons.net.nntp.NNTPClient;
23 import org.apache.commons.net.nntp.NewsgroupInfo;
24
25 import examples.PrintCommandListener;
26
27 /**
28 * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
29 *
30 * @author Rory Winston <rwinston@checkfree.com>
31 */
32 public class ExtendedNNTPOps {
33
34
35 NNTPClient client;
36
37 public ExtendedNNTPOps() {
38 client = new NNTPClient();
39 client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
40 }
41
42
43 public void demo(String host, String user, String password) {
44 try {
45 client.connect(host);
46
47
48 boolean success = client.authenticate(user, password);
49 if (success) {
50 System.out.println("Authentication succeeded");
51 } else {
52 System.out.println("Authentication failed, error =" + client.getReplyString());
53 }
54
55
56 NewsgroupInfo testGroup = new NewsgroupInfo();
57 client.selectNewsgroup("alt.test", testGroup);
58 int lowArticleNumber = testGroup.getFirstArticle();
59 int highArticleNumber = lowArticleNumber + 100;
60 Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber);
61
62 for (int i = 0; i < articles.length; ++i) {
63 System.out.println(articles[i].getSubject());
64 }
65
66
67 NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
68 for (int i = 0; i < fanGroups.length; ++i) {
69 System.out.println(fanGroups[i].getNewsgroup());
70 }
71
72 } catch (IOException e) {
73 e.printStackTrace();
74 }
75 }
76
77 public static void main(String[] args) {
78 ExtendedNNTPOps ops;
79
80 if (args.length != 3) {
81 System.err.println("usage: ExtendedNNTPOps nntpserver username password");
82 System.exit(1);
83 }
84
85 ops = new ExtendedNNTPOps();
86 ops.demo(args[0], args[1], args[2]);
87 }
88
89 }
90
91
92
93
94
95
96
97