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