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