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    
018    package examples.nntp;
019    
020    import java.io.BufferedReader;
021    import java.io.FileNotFoundException;
022    import java.io.FileReader;
023    import java.io.IOException;
024    import java.io.InputStreamReader;
025    import java.io.PrintWriter;
026    import java.io.Writer;
027    
028    import org.apache.commons.net.PrintCommandListener;
029    import org.apache.commons.net.io.Util;
030    import org.apache.commons.net.nntp.NNTPClient;
031    import org.apache.commons.net.nntp.NNTPReply;
032    import 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     * <p>
040     ***/
041    
042    public final class post
043    {
044    
045        public final static void main(String[] args)
046        {
047            String from, subject, newsgroup, filename, server, organization;
048            String references;
049            BufferedReader stdin;
050            FileReader fileReader = null;
051            SimpleNNTPHeader header;
052            NNTPClient client;
053    
054            if (args.length < 1)
055            {
056                System.err.println("Usage: post newsserver");
057                System.exit(1);
058            }
059    
060            server = args[0];
061    
062            stdin = new BufferedReader(new InputStreamReader(System.in));
063    
064            try
065            {
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                {
086                    System.out.print("Additional Newsgroup <Hit enter to end>: ");
087                    System.out.flush();
088    
089                    // Of course you don't want to do this because readLine() may be null
090                    newsgroup = stdin.readLine().trim();
091    
092                    if (newsgroup.length() == 0)
093                        break;
094    
095                    header.addNewsgroup(newsgroup);
096                }
097    
098                System.out.print("Organization: ");
099                System.out.flush();
100    
101                organization = stdin.readLine();
102    
103                System.out.print("References: ");
104                System.out.flush();
105    
106                references = stdin.readLine();
107    
108                if (organization != null && organization.length() > 0)
109                    header.addHeaderField("Organization", organization);
110    
111                if (references != null && organization.length() > 0)
112                    header.addHeaderField("References", references);
113    
114                header.addHeaderField("X-Newsreader", "NetComponents");
115    
116                System.out.print("Filename: ");
117                System.out.flush();
118    
119                filename = stdin.readLine();
120    
121                try
122                {
123                    fileReader = new FileReader(filename);
124                }
125                catch (FileNotFoundException e)
126                {
127                    System.err.println("File not found. " + e.getMessage());
128                    System.exit(1);
129                }
130    
131                client = new NNTPClient();
132                client.addProtocolCommandListener(new PrintCommandListener(
133                                                      new PrintWriter(System.out)));
134    
135                client.connect(server);
136    
137                if (!NNTPReply.isPositiveCompletion(client.getReplyCode()))
138                {
139                    client.disconnect();
140                    System.err.println("NNTP server refused connection.");
141                    System.exit(1);
142                }
143    
144                if (client.isAllowedToPost())
145                {
146                    Writer writer = client.postArticle();
147    
148                    if (writer != null)
149                    {
150                        writer.write(header.toString());
151                        Util.copyReader(fileReader, writer);
152                        writer.close();
153                        client.completePendingCommand();
154                    }
155                }
156    
157                fileReader.close();
158    
159                client.logout();
160    
161                client.disconnect();
162            }
163            catch (IOException e)
164            {
165                e.printStackTrace();
166                System.exit(1);
167            }
168        }
169    }
170    
171