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 public final class PostMessage {
40
41 public static void main(final String[] args) {
42 final String from;
43 final String subject;
44 String newsgroup;
45 final String fileName;
46 final String server;
47 final String organization;
48 final String references;
49 final BufferedReader stdin;
50 FileReader fileReader = null;
51 final SimpleNNTPHeader header;
52 final NNTPClient client;
53
54 if (args.length < 1) {
55 System.err.println("Usage: post newsserver");
56 System.exit(1);
57 }
58
59 server = args[0];
60
61 stdin = new BufferedReader(new InputStreamReader(System.in));
62
63 try {
64 System.out.print("From: ");
65 System.out.flush();
66
67 from = stdin.readLine();
68
69 System.out.print("Subject: ");
70 System.out.flush();
71
72 subject = stdin.readLine();
73
74 header = new SimpleNNTPHeader(from, subject);
75
76 System.out.print("Newsgroup: ");
77 System.out.flush();
78
79 newsgroup = stdin.readLine();
80 header.addNewsgroup(newsgroup);
81
82 while (true) {
83 System.out.print("Additional Newsgroup <Hit enter to end>: ");
84 System.out.flush();
85
86 newsgroup = stdin.readLine();
87 if (newsgroup == null) {
88 break;
89 }
90
91 newsgroup = newsgroup.trim();
92
93 if (newsgroup.isEmpty()) {
94 break;
95 }
96
97 header.addNewsgroup(newsgroup);
98 }
99
100 System.out.print("Organization: ");
101 System.out.flush();
102
103 organization = stdin.readLine();
104
105 System.out.print("References: ");
106 System.out.flush();
107
108 references = stdin.readLine();
109
110 if (organization != null && !organization.isEmpty()) {
111 header.addHeaderField("Organization", organization);
112 }
113
114 if (references != null && !references.isEmpty()) {
115 header.addHeaderField("References", references);
116 }
117
118 header.addHeaderField("X-Newsreader", "NetComponents");
119
120 System.out.print("Filename: ");
121 System.out.flush();
122
123 fileName = stdin.readLine();
124
125 try {
126 fileReader = new FileReader(fileName);
127 } catch (final FileNotFoundException e) {
128 System.err.println("File not found. " + e.getMessage());
129 System.exit(1);
130 }
131
132 client = new NNTPClient();
133 client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
134
135 client.connect(server);
136
137 if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
138 client.disconnect();
139 System.err.println("NNTP server refused connection.");
140 System.exit(1);
141 }
142
143 if (client.isAllowedToPost()) {
144 final Writer writer = client.postArticle();
145
146 if (writer != null) {
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 } catch (final IOException e) {
160 e.printStackTrace();
161 System.exit(1);
162 }
163 }
164 }