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.IOException; 023import java.io.InputStreamReader; 024import java.io.Reader; 025import java.io.Writer; 026import java.nio.charset.Charset; 027import java.nio.file.Files; 028import java.nio.file.Paths; 029 030import org.apache.commons.net.PrintCommandListener; 031import org.apache.commons.net.io.Util; 032import org.apache.commons.net.nntp.NNTPClient; 033import org.apache.commons.net.nntp.NNTPReply; 034import org.apache.commons.net.nntp.SimpleNNTPHeader; 035 036/** 037 * 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 038 * post. 039 */ 040 041public final class PostMessage { 042 043 public static void main(final String[] args) { 044 final String from; 045 final String subject; 046 String newsgroup; 047 final String fileName; 048 final String server; 049 final String organization; 050 final String references; 051 final BufferedReader stdin; 052 Reader fileReader = null; 053 final SimpleNNTPHeader header; 054 final NNTPClient client; 055 056 if (args.length < 1) { 057 System.err.println("Usage: post newsserver"); 058 System.exit(1); 059 } 060 061 server = args[0]; 062 063 stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset())); 064 065 try { 066 System.out.print("From: "); 067 System.out.flush(); 068 069 from = stdin.readLine(); 070 071 System.out.print("Subject: "); 072 System.out.flush(); 073 074 subject = stdin.readLine(); 075 076 header = new SimpleNNTPHeader(from, subject); 077 078 System.out.print("Newsgroup: "); 079 System.out.flush(); 080 081 newsgroup = stdin.readLine(); 082 header.addNewsgroup(newsgroup); 083 084 while (true) { 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.isEmpty()) { 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.isEmpty()) { 113 header.addHeaderField("Organization", organization); 114 } 115 116 if (references != null && !references.isEmpty()) { 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 fileReader = Files.newBufferedReader(Paths.get(fileName), Charset.defaultCharset()); 129 } catch (final FileNotFoundException e) { 130 System.err.println("File not found. " + e.getMessage()); 131 System.exit(1); 132 } 133 134 client = new NNTPClient(); 135 client.addProtocolCommandListener(new PrintCommandListener(Util.newPrintWriter(System.out), true)); 136 137 client.connect(server); 138 139 if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) { 140 client.disconnect(); 141 System.err.println("NNTP server refused connection."); 142 System.exit(1); 143 } 144 145 if (client.isAllowedToPost()) { 146 final Writer writer = client.postArticle(); 147 148 if (writer != null) { 149 writer.write(header.toString()); 150 Util.copyReader(fileReader, writer); 151 writer.close(); 152 client.completePendingCommand(); 153 } 154 } 155 156 fileReader.close(); 157 158 client.logout(); 159 160 client.disconnect(); 161 } catch (final IOException e) { 162 e.printStackTrace(); 163 System.exit(1); 164 } 165 } 166}