001: /*
002: * Copyright 2001-2005 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package examples.nntp;
017:
018: import java.io.BufferedReader;
019: import java.io.FileNotFoundException;
020: import java.io.FileReader;
021: import java.io.IOException;
022: import java.io.InputStreamReader;
023: import java.io.PrintWriter;
024: import java.io.Writer;
025: import org.apache.commons.net.io.Util;
026: import org.apache.commons.net.nntp.NNTPClient;
027: import org.apache.commons.net.nntp.NNTPReply;
028: import org.apache.commons.net.nntp.SimpleNNTPHeader;
029:
030: import examples.PrintCommandListener;
031:
032: /***
033: * This is an example program using the NNTP package to post an article
034: * to the specified newsgroup(s). It prompts you for header information and
035: * a filename to post.
036: * <p>
037: ***/
038:
039: public final class post {
040:
041: public final static void main(String[] args) {
042: String from, subject, newsgroup, filename, server, organization;
043: String references;
044: BufferedReader stdin;
045: FileReader fileReader = null;
046: SimpleNNTPHeader header;
047: NNTPClient client;
048:
049: if (args.length < 1) {
050: System.err.println("Usage: post newsserver");
051: System.exit(1);
052: }
053:
054: server = args[0];
055:
056: stdin = new BufferedReader(new InputStreamReader(System.in));
057:
058: try {
059: System.out.print("From: ");
060: System.out.flush();
061:
062: from = stdin.readLine();
063:
064: System.out.print("Subject: ");
065: System.out.flush();
066:
067: subject = stdin.readLine();
068:
069: header = new SimpleNNTPHeader(from, subject);
070:
071: System.out.print("Newsgroup: ");
072: System.out.flush();
073:
074: newsgroup = stdin.readLine();
075: header.addNewsgroup(newsgroup);
076:
077: while (true) {
078: System.out
079: .print("Additional Newsgroup <Hit enter to end>: ");
080: System.out.flush();
081:
082: // Of course you don't want to do this because readLine() may be null
083: newsgroup = stdin.readLine().trim();
084:
085: if (newsgroup.length() == 0)
086: break;
087:
088: header.addNewsgroup(newsgroup);
089: }
090:
091: System.out.print("Organization: ");
092: System.out.flush();
093:
094: organization = stdin.readLine();
095:
096: System.out.print("References: ");
097: System.out.flush();
098:
099: references = stdin.readLine();
100:
101: if (organization != null && organization.length() > 0)
102: header.addHeaderField("Organization", organization);
103:
104: if (references != null && organization.length() > 0)
105: header.addHeaderField("References", references);
106:
107: header.addHeaderField("X-Newsreader", "NetComponents");
108:
109: System.out.print("Filename: ");
110: System.out.flush();
111:
112: filename = stdin.readLine();
113:
114: try {
115: fileReader = new FileReader(filename);
116: } catch (FileNotFoundException e) {
117: System.err.println("File not found. " + e.getMessage());
118: System.exit(1);
119: }
120:
121: client = new NNTPClient();
122: client.addProtocolCommandListener(new PrintCommandListener(
123: new PrintWriter(System.out)));
124:
125: client.connect(server);
126:
127: if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
128: client.disconnect();
129: System.err.println("NNTP server refused connection.");
130: System.exit(1);
131: }
132:
133: if (client.isAllowedToPost()) {
134: Writer writer = client.postArticle();
135:
136: if (writer != null) {
137: writer.write(header.toString());
138: Util.copyReader(fileReader, writer);
139: writer.close();
140: client.completePendingCommand();
141: }
142: }
143:
144: fileReader.close();
145:
146: client.logout();
147:
148: client.disconnect();
149: } catch (IOException e) {
150: e.printStackTrace();
151: System.exit(1);
152: }
153: }
154: }
|