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 org.apache.commons.net.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 * This is an example program using the NNTP package to post an article to the specified newsgroup(s). It prompts you for header information and a file name to 036 * post. 037 */ 038 039public final class PostMessage { 040 041 public static void main(final String[] args) { 042 final String from; 043 final String subject; 044 String newsgroup; 045 final String fileName; 046 final String server; 047 final String organization; 048 final String references; 049 final BufferedReader stdin; 050 FileReader fileReader = null; 051 final SimpleNNTPHeader header; 052 final NNTPClient client; 053 054 if (args.length < 1) { 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 System.out.print("From: "); 065 System.out.flush(); 066 067 from = stdin.readLine(); 068 069 System.out.print("Subject: "); 070 System.out.flush(); 071 072 subject = stdin.readLine(); 073 074 header = new SimpleNNTPHeader(from, subject); 075 076 System.out.print("Newsgroup: "); 077 System.out.flush(); 078 079 newsgroup = stdin.readLine(); 080 header.addNewsgroup(newsgroup); 081 082 while (true) { 083 System.out.print("Additional Newsgroup <Hit enter to end>: "); 084 System.out.flush(); 085 086 newsgroup = stdin.readLine(); 087 if (newsgroup == null) { 088 break; 089 } 090 091 newsgroup = newsgroup.trim(); 092 093 if (newsgroup.isEmpty()) { 094 break; 095 } 096 097 header.addNewsgroup(newsgroup); 098 } 099 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}