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 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
public class ExtendedNNTPOps { |
33 |
|
|
34 |
|
|
35 |
|
NNTPClient client; |
36 |
|
|
37 |
0 |
public ExtendedNNTPOps() { |
38 |
0 |
client = new NNTPClient(); |
39 |
0 |
client.addProtocolCommandListener(new PrintCommandListener(class="keyword">new PrintWriter(System.out))); |
40 |
0 |
} |
41 |
|
|
42 |
|
|
43 |
|
public void demo(String host, String user, String password) { |
44 |
|
try { |
45 |
0 |
client.connect(host); |
46 |
|
|
47 |
|
|
48 |
0 |
boolean success = client.authenticate(user, password); |
49 |
0 |
if (success) { |
50 |
0 |
System.out.println("Authentication succeeded"); |
51 |
|
} else { |
52 |
0 |
System.out.println("Authentication failed, error =" + client.getReplyString()); |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
0 |
NewsgroupInfo testGroup = new NewsgroupInfo(); |
57 |
0 |
client.selectNewsgroup("alt.test", testGroup); |
58 |
0 |
int lowArticleNumber = testGroup.getFirstArticle(); |
59 |
0 |
int highArticleNumber = lowArticleNumber + 100; |
60 |
0 |
Article[] articles = NNTPUtils.getArticleInfo(client, lowArticleNumber, highArticleNumber); |
61 |
|
|
62 |
0 |
for (int i = 0; i < articles.length; ++i) { |
63 |
0 |
System.out.println(articles[i].getSubject()); |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
0 |
NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*"); |
68 |
0 |
for (int i = 0; i < fanGroups.length; ++i) { |
69 |
0 |
System.out.println(fanGroups[i].getNewsgroup()); |
70 |
|
} |
71 |
|
|
72 |
0 |
} catch (IOException e) { |
73 |
0 |
e.printStackTrace(); |
74 |
0 |
} |
75 |
0 |
} |
76 |
|
|
77 |
|
public static void main(String[] args) { |
78 |
|
ExtendedNNTPOps ops; |
79 |
|
|
80 |
0 |
if (args.length != 3) { |
81 |
0 |
System.err.println("usage: ExtendedNNTPOps nntpserver username password"); |
82 |
0 |
System.exit(1); |
83 |
|
} |
84 |
|
|
85 |
0 |
ops = new ExtendedNNTPOps(); |
86 |
0 |
ops.demo(args[0], args[1], args[2]); |
87 |
0 |
} |
88 |
|
|
89 |
|
} |
90 |
|
|
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|