1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package examples.nntp;
17
18 import java.io.BufferedReader;
19 import java.io.FileNotFoundException;
20 import java.io.FileReader;
21 import java.io.IOException;
22 import java.io.InputStreamReader;
23 import java.io.PrintWriter;
24 import java.io.Writer;
25 import org.apache.commons.net.io.Util;
26 import org.apache.commons.net.nntp.NNTPClient;
27 import org.apache.commons.net.nntp.NNTPReply;
28 import org.apache.commons.net.nntp.SimpleNNTPHeader;
29
30 import examples.PrintCommandListener;
31
32 /***
33 * This is an example program using the NNTP package to post an article
34 * to the specified newsgroup(s). It prompts you for header information and
35 * a filename to post.
36 * <p>
37 ***/
38
39 public final class post
40 {
41
42 public final static void main(String[] args)
43 {
44 String from, subject, newsgroup, filename, server, organization;
45 String references;
46 BufferedReader stdin;
47 FileReader fileReader = null;
48 SimpleNNTPHeader header;
49 NNTPClient client;
50
51 if (args.length < 1)
52 {
53 System.err.println("Usage: post newsserver");
54 System.exit(1);
55 }
56
57 server = args[0];
58
59 stdin = new BufferedReader(new InputStreamReader(System.in));
60
61 try
62 {
63 System.out.print("From: ");
64 System.out.flush();
65
66 from = stdin.readLine();
67
68 System.out.print("Subject: ");
69 System.out.flush();
70
71 subject = stdin.readLine();
72
73 header = new SimpleNNTPHeader(from, subject);
74
75 System.out.print("Newsgroup: ");
76 System.out.flush();
77
78 newsgroup = stdin.readLine();
79 header.addNewsgroup(newsgroup);
80
81 while (true)
82 {
83 System.out.print("Additional Newsgroup <Hit enter to end>: ");
84 System.out.flush();
85
86
87 newsgroup = stdin.readLine().trim();
88
89 if (newsgroup.length() == 0)
90 break;
91
92 header.addNewsgroup(newsgroup);
93 }
94
95 System.out.print("Organization: ");
96 System.out.flush();
97
98 organization = stdin.readLine();
99
100 System.out.print("References: ");
101 System.out.flush();
102
103 references = stdin.readLine();
104
105 if (organization != null && organization.length() > 0)
106 header.addHeaderField("Organization", organization);
107
108 if (references != null && organization.length() > 0)
109 header.addHeaderField("References", references);
110
111 header.addHeaderField("X-Newsreader", "NetComponents");
112
113 System.out.print("Filename: ");
114 System.out.flush();
115
116 filename = stdin.readLine();
117
118 try
119 {
120 fileReader = new FileReader(filename);
121 }
122 catch (FileNotFoundException e)
123 {
124 System.err.println("File not found. " + e.getMessage());
125 System.exit(1);
126 }
127
128 client = new NNTPClient();
129 client.addProtocolCommandListener(new PrintCommandListener(
130 new PrintWriter(System.out)));
131
132 client.connect(server);
133
134 if (!NNTPReply.isPositiveCompletion(client.getReplyCode()))
135 {
136 client.disconnect();
137 System.err.println("NNTP server refused connection.");
138 System.exit(1);
139 }
140
141 if (client.isAllowedToPost())
142 {
143 Writer writer = client.postArticle();
144
145 if (writer != null)
146 {
147 writer.write(header.toString());
148 Util.copyReader(fileReader, writer);
149 writer.close();
150 client.completePendingCommand();
151 }
152 }
153
154 fileReader.close();
155
156 client.logout();
157
158 client.disconnect();
159 }
160 catch (IOException e)
161 {
162 e.printStackTrace();
163 System.exit(1);
164 }
165 }
166 }
167
168