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.IOException;
021import java.io.PrintWriter;
022
023import org.apache.commons.net.PrintCommandListener;
024import org.apache.commons.net.nntp.Article;
025import org.apache.commons.net.nntp.NNTPClient;
026import org.apache.commons.net.nntp.NewsgroupInfo;
027
028/**
029 * Simple class showing some of the extended commands (AUTH, XOVER, LIST ACTIVE)
030 */
031public class ExtendedNNTPOps {
032
033    public static void main(final String[] args) {
034        final ExtendedNNTPOps ops;
035
036        final int argc = args.length;
037        if (argc < 1) {
038            System.err.println("usage: ExtendedNNTPOps nntpserver [username password]");
039            System.exit(1);
040        }
041
042        ops = new ExtendedNNTPOps();
043        ops.demo(args[0], argc >= 3 ? args[1] : null, argc >= 3 ? args[2] : null);
044    }
045
046    private final NNTPClient client;
047
048    public ExtendedNNTPOps() {
049        client = new NNTPClient();
050        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
051    }
052
053    private void demo(final String host, final String user, final String password) {
054        try {
055            client.connect(host);
056
057            // AUTHINFO USER/AUTHINFO PASS
058            if (user != null && password != null) {
059                final boolean success = client.authenticate(user, password);
060                if (success) {
061                    System.out.println("Authentication succeeded");
062                } else {
063                    System.out.println("Authentication failed, error =" + client.getReplyString());
064                }
065            }
066
067            // XOVER
068            final NewsgroupInfo testGroup = new NewsgroupInfo();
069            client.selectNewsgroup("alt.test", testGroup);
070            final long lowArticleNumber = testGroup.getFirstArticleLong();
071            final long highArticleNumber = lowArticleNumber + 100;
072            final Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);
073
074            for (final Article article : articles) {
075                if (article.isDummy()) { // Subject will contain raw response
076                    System.out.println("Could not parse: " + article.getSubject());
077                } else {
078                    System.out.println(article.getSubject());
079                }
080            }
081
082            // LIST ACTIVE
083            final NewsgroupInfo[] fanGroups = client.listNewsgroups("alt.fan.*");
084            for (final NewsgroupInfo fanGroup : fanGroups) {
085                System.out.println(fanGroup.getNewsgroup());
086            }
087
088        } catch (final IOException e) {
089            e.printStackTrace();
090        }
091    }
092
093}