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    
042        public final static void main(String[] args)
043        {
044            String from, subject, newsgroup, filename, server, organization;
045            String references;
046            BufferedReader stdin;
047            FileReader fileReader = null;
048            SimpleNNTPHeader header;
049            NNTPClient client;
050    
051            if (args.length < 1)
052            {
053                System.err.println("Usage: post newsserver");
054                System.exit(1);
055            }
056    
057            server = args[0];
058    
059            stdin = new BufferedReader(new InputStreamReader(System.in));
060    
061            try
062            {
063                System.out.print("From: ");
064                System.out.flush();
065    
066                from = stdin.readLine();
067    
068                System.out.print("Subject: ");
069                System.out.flush();
070    
071                subject = stdin.readLine();
072    
073                header = new SimpleNNTPHeader(from, subject);
074    
075                System.out.print("Newsgroup: ");
076                System.out.flush();
077    
078                newsgroup = stdin.readLine();
079                header.addNewsgroup(newsgroup);
080    
081                while (true)
082                {
083                    System.out.print("Additional Newsgroup <Hit enter to end>: ");
084                    System.out.flush();
085    
086                    // Of course you don't want to do this because readLine() may be null
087                    newsgroup = stdin.readLine().trim();
088    
089                    if (newsgroup.length() == 0)
090                        break;
091    
092                    header.addNewsgroup(newsgroup);
093                }
094    
095                System.out.print("Organization: ");
096                System.out.flush();
097    
098                organization = stdin.readLine();
099    
100                System.out.print("References: ");
101                System.out.flush();
102    
103                references = stdin.readLine();
104    
105                if (organization != null && organization.length() > 0)
106                    header.addHeaderField("Organization", organization);
107    
108                if (references != null && organization.length() > 0)
109                    header.addHeaderField("References", references);
110    
111                header.addHeaderField("X-Newsreader", "NetComponents");
112    
113                System.out.print("Filename: ");
114                System.out.flush();
115    
116                filename = stdin.readLine();
117    
118                try
119                {
120                    fileReader = new FileReader(filename);
121                }
122                catch (FileNotFoundException e)
123                {
124                    System.err.println("File not found. " + e.getMessage());
125                    System.exit(1);
126                }
127    
128                client = new NNTPClient();
129                client.addProtocolCommandListener(new PrintCommandListener(
130                                                      new PrintWriter(System.out)));
131    
132                client.connect(server);
133    
134                if (!NNTPReply.isPositiveCompletion(client.getReplyCode()))
135                {
136                    client.disconnect();
137                    System.err.println("NNTP server refused connection.");
138                    System.exit(1);
139                }
140    
141                if (client.isAllowedToPost())
142                {
143                    Writer writer = client.postArticle();
144    
145                    if (writer != null)
146                    {
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            }
160            catch (IOException e)
161            {
162                e.printStackTrace();
163                System.exit(1);
164            }
165        }
166    }
167    
168