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