1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package examples.nntp;
19
20 import java.io.BufferedReader;
21 import java.io.IOException;
22 import java.io.PrintWriter;
23 import java.net.SocketException;
24
25 import org.apache.commons.net.PrintCommandListener;
26 import org.apache.commons.net.nntp.NNTPClient;
27 import org.apache.commons.net.nntp.NewsgroupInfo;
28
29
30
31
32 public class ArticleReader {
33
34 public static void main(String[] args) throws SocketException, IOException {
35
36 if (args.length != 2 && args.length != 3 && args.length != 5) {
37 System.out.println("Usage: MessageThreading <hostname> <groupname> [<article specifier> [<user> <password>]]");
38 return;
39 }
40
41 String hostname = args[0];
42 String newsgroup = args[1];
43
44 String articleSpec = args.length >= 3 ? args[2] : null;
45
46 NNTPClient client = new NNTPClient();
47 client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
48 client.connect(hostname);
49
50 if (args.length == 5) {
51 String user = args[3];
52 String password = args[4];
53 if(!client.authenticate(user, password)) {
54 System.out.println("Authentication failed for user " + user + "!");
55 System.exit(1);
56 }
57 }
58
59 NewsgroupInfo group = new NewsgroupInfo();
60 client.selectNewsgroup(newsgroup, group);
61
62 BufferedReader br;
63 String line;
64 if (articleSpec != null) {
65 br = (BufferedReader) client.retrieveArticleHeader(articleSpec);
66 } else {
67 long articleNum = group.getLastArticleLong();
68 br = client.retrieveArticleHeader(articleNum);
69 }
70 if (br != null) {
71 while((line=br.readLine()) != null) {
72 System.out.println(line);
73 }
74 br.close();
75 }
76 if (articleSpec != null) {
77 br = (BufferedReader) client.retrieveArticleBody(articleSpec);
78 } else {
79 long articleNum = group.getLastArticleLong();
80 br = client.retrieveArticleBody(articleNum);
81 }
82 if (br != null) {
83 while((line=br.readLine()) != null) {
84 System.out.println(line);
85 }
86 br.close();
87 }
88 }
89
90 }