Example usage for org.apache.commons.net.nntp NNTPClient NNTPClient

List of usage examples for org.apache.commons.net.nntp NNTPClient NNTPClient

Introduction

In this page you can find the example usage for org.apache.commons.net.nntp NNTPClient NNTPClient.

Prototype

NNTPClient

Source Link

Usage

From source file:examples.nntp.ListNewsgroups.java

public static void main(String[] args) {
    if (args.length < 1) {
        System.err.println("Usage: newsgroups newsserver [pattern]");
        return;//from  www.ja va  2  s.  c o  m
    }

    NNTPClient client = new NNTPClient();
    String pattern = args.length >= 2 ? args[1] : "";

    try {
        client.connect(args[0]);

        int j = 0;
        try {
            for (String s : client.iterateNewsgroupListing(pattern)) {
                j++;
                System.out.println(s);
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        System.out.println(j);

        j = 0;
        for (NewsgroupInfo n : client.iterateNewsgroups(pattern)) {
            j++;
            System.out.println(n.getNewsgroup());
        }
        System.out.println(j);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (client.isConnected()) {
                client.disconnect();
            }
        } catch (IOException e) {
            System.err.println("Error disconnecting from server.");
            e.printStackTrace();
            System.exit(1);
        }
    }

}

From source file:examples.nntp.newsgroups.java

public final static void main(String[] args) {
    NNTPClient client;/*from w  ww .ja  v  a  2s .  com*/
    NewsgroupInfo[] list;

    if (args.length < 1) {
        System.err.println("Usage: newsgroups newsserver");
        System.exit(1);
    }

    client = new NNTPClient();

    try {
        client.connect(args[0]);

        list = client.listNewsgroups();

        if (list != null) {
            for (int i = 0; i < list.length; i++)
                System.out.println(list[i].getNewsgroup());
        } else {
            System.err.println("LIST command failed.");
            System.err.println("Server reply: " + client.getReplyString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (client.isConnected())
                client.disconnect();
        } catch (IOException e) {
            System.err.println("Error disconnecting from server.");
            e.printStackTrace();
            System.exit(1);
        }
    }

}

From source file:examples.nntp.MessageThreading.java

public static void main(String[] args) throws SocketException, IOException {

    if (args.length != 2 && args.length != 4) {
        System.out.println("Usage: MessageThreading <hostname> <groupname> [<user> <password>]");
        return;/*  w w w. j av  a2s.  co  m*/
    }

    String hostname = args[0];
    String newsgroup = args[1];

    NNTPClient client = new NNTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    client.connect(hostname);

    if (args.length == 4) { // Optional auth
        String user = args[2];
        String password = args[3];
        if (!client.authenticate(user, password)) {
            System.out.println("Authentication failed for user " + user + "!");
            System.exit(1);
        }
    }

    String fmt[] = client.listOverviewFmt();
    if (fmt != null) {
        System.out.println("LIST OVERVIEW.FMT:");
        for (String s : fmt) {
            System.out.println(s);
        }
    } else {
        System.out.println("Failed to get OVERVIEW.FMT");
    }
    NewsgroupInfo group = new NewsgroupInfo();
    client.selectNewsgroup(newsgroup, group);

    long lowArticleNumber = group.getFirstArticleLong();
    long highArticleNumber = lowArticleNumber + 5000;

    System.out
            .println("Retrieving articles between [" + lowArticleNumber + "] and [" + highArticleNumber + "]");
    Iterable<Article> articles = client.iterateArticleInfo(lowArticleNumber, highArticleNumber);

    System.out.println("Building message thread tree...");
    Threader threader = new Threader();
    Article root = (Article) threader.thread(articles);

    Article.printThread(root, 0);
}

From source file:examples.nntp.ArticleReader.java

public static void main(String[] args) throws SocketException, IOException {

    if (args.length != 2 && args.length != 3 && args.length != 5) {
        System.out.println(/*from  ww w .j av a2  s . c  o m*/
                "Usage: MessageThreading <hostname> <groupname> [<article specifier> [<user> <password>]]");
        return;
    }

    String hostname = args[0];
    String newsgroup = args[1];
    // Article specifier can be numeric or Id in form <m.n.o.x@host>
    String articleSpec = args.length >= 3 ? args[2] : null;

    NNTPClient client = new NNTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    client.connect(hostname);

    if (args.length == 5) { // Optional auth
        String user = args[3];
        String password = args[4];
        if (!client.authenticate(user, password)) {
            System.out.println("Authentication failed for user " + user + "!");
            System.exit(1);
        }
    }

    NewsgroupInfo group = new NewsgroupInfo();
    client.selectNewsgroup(newsgroup, group);

    BufferedReader br;
    String line;
    if (articleSpec != null) {
        br = (BufferedReader) client.retrieveArticleHeader(articleSpec);
    } else {
        long articleNum = group.getLastArticleLong();
        br = client.retrieveArticleHeader(articleNum);
    }
    if (br != null) {
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
    if (articleSpec != null) {
        br = (BufferedReader) client.retrieveArticleBody(articleSpec);
    } else {
        long articleNum = group.getLastArticleLong();
        br = client.retrieveArticleBody(articleNum);
    }
    if (br != null) {
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
        br.close();
    }
}

From source file:examples.nntp.post.java

public final static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;/*from w w  w .  ja  v  a 2s .  c  o  m*/
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            // Of course you don't want to do this because readLine() may be null
            newsgroup = stdin.readLine().trim();

            if (newsgroup.length() == 0)
                break;

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0)
            header.addHeaderField("Organization", organization);

        if (references != null && organization.length() > 0)
            header.addHeaderField("References", references);

        header.addHeaderField("X-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        filename = stdin.readLine();

        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

        client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        fileReader.close();

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:examples.nntp.PostMessage.java

public static void main(String[] args) {
    String from, subject, newsgroup, filename, server, organization;
    String references;//from w  w w.ja v  a  2  s.c o  m
    BufferedReader stdin;
    FileReader fileReader = null;
    SimpleNNTPHeader header;
    NNTPClient client;

    if (args.length < 1) {
        System.err.println("Usage: post newsserver");
        System.exit(1);
    }

    server = args[0];

    stdin = new BufferedReader(new InputStreamReader(System.in));

    try {
        System.out.print("From: ");
        System.out.flush();

        from = stdin.readLine();

        System.out.print("Subject: ");
        System.out.flush();

        subject = stdin.readLine();

        header = new SimpleNNTPHeader(from, subject);

        System.out.print("Newsgroup: ");
        System.out.flush();

        newsgroup = stdin.readLine();
        header.addNewsgroup(newsgroup);

        while (true) {
            System.out.print("Additional Newsgroup <Hit enter to end>: ");
            System.out.flush();

            // Of course you don't want to do this because readLine() may be null
            newsgroup = stdin.readLine().trim();

            if (newsgroup.length() == 0) {
                break;
            }

            header.addNewsgroup(newsgroup);
        }

        System.out.print("Organization: ");
        System.out.flush();

        organization = stdin.readLine();

        System.out.print("References: ");
        System.out.flush();

        references = stdin.readLine();

        if (organization != null && organization.length() > 0) {
            header.addHeaderField("Organization", organization);
        }

        if (references != null && references.length() > 0) {
            header.addHeaderField("References", references);
        }

        header.addHeaderField("X-Newsreader", "NetComponents");

        System.out.print("Filename: ");
        System.out.flush();

        filename = stdin.readLine();

        try {
            fileReader = new FileReader(filename);
        } catch (FileNotFoundException e) {
            System.err.println("File not found. " + e.getMessage());
            System.exit(1);
        }

        client = new NNTPClient();
        client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));

        client.connect(server);

        if (!NNTPReply.isPositiveCompletion(client.getReplyCode())) {
            client.disconnect();
            System.err.println("NNTP server refused connection.");
            System.exit(1);
        }

        if (client.isAllowedToPost()) {
            Writer writer = client.postArticle();

            if (writer != null) {
                writer.write(header.toString());
                Util.copyReader(fileReader, writer);
                writer.close();
                client.completePendingCommand();
            }
        }

        if (fileReader != null) {
            fileReader.close();
        }

        client.logout();

        client.disconnect();
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:examples.nntp.ExtendedNNTPOps.java

public ExtendedNNTPOps() {
    client = new NNTPClient();
    client.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
}

From source file:io.dimitris.newsgroupwatcher.NewsgroupWatcher.java

protected void watch() throws IOException {

    while (watching) {
        try {/*from   ww  w .  j  a  v a2 s.  c o  m*/
            //System.err.println("Watching ");
            client = new NNTPClient();
            client.connect(server);
            if (isAuthenticationRequired()) {
                client.authenticate(username, password);
            }

            client.selectNewsgroup(newsgroup);

            NewsgroupInfo newsgroupInfo = null;

            for (NewsgroupInfo ni : client.listNewsgroups()) {

                if (ni.getNewsgroup().equals(newsgroup)) {
                    newsgroupInfo = ni;
                    break;
                }
            }

            int newArticleCount = newsgroupInfo.getArticleCount();

            //if (articleCount == -1) {
            //   articleCount = newArticleCount;
            //}
            //else 
            if (articleCount < newArticleCount) {
                Reader articleReader = client.retrieveArticleHeader(newArticleCount);
                notifyNewArticleListeners(new ArticleHeader(articleReader));
                articleCount = newArticleCount;
            } else {
                //System.err.println("Still " + newArticleCount);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            sleep();
        }
    }
}

From source file:me.lehrner.newsgroupsndy.presenter.tasks.UpdateGroupListAsyncTask.java

@Override
protected Boolean doInBackground(Server... servers) {
    mServerId = servers[0].getId();/*from w ww.  j a  v  a 2  s  . c  o m*/

    String hostName = servers[0].getServerUrl();
    int lastVisitInt = servers[0].getLastVisit();

    NewsgroupInfo[] newsgroupInfos;
    ArrayList<String> groupNames = new ArrayList<>();

    try {
        NNTPClient nntpClient = new NNTPClient();
        nntpClient.connect(hostName, 119);

        Log.d(LOG_TAG, "lastVisitInt: " + lastVisitInt);

        if (lastVisitInt != 0) {
            Calendar lastVisit = Calendar.getInstance();
            lastVisit.setTimeInMillis(lastVisitInt * 1000L);

            NewGroupsOrNewsQuery query = new NewGroupsOrNewsQuery(lastVisit, false);
            newsgroupInfos = nntpClient.listNewNewsgroups(query);

            Log.d(LOG_TAG, "listNewNewsgroups");

        } else {
            newsgroupInfos = nntpClient.listNewsgroups();

            Log.d(LOG_TAG, "listNewsgroups");

        }

        nntpClient.disconnect();
    } catch (IOException e) {
        Log.d(LOG_TAG, "IOException");

        GroupPresenter groupPresenter = mGroupPresenterWeakReference.get();

        if (groupPresenter != null) {
            groupPresenter.updateNotSuccessful();
        }
        return false;
    }

    if (newsgroupInfos == null) {
        Log.d(LOG_TAG, "newsgroupInfos is null");
        return false;
    }

    for (NewsgroupInfo newsgroupInfo : newsgroupInfos) {
        groupNames.add(newsgroupInfo.getNewsgroup());
    }

    return mGroupRepository.saveGroups(mServerId, groupNames);
}

From source file:ProtocolRunner.java

public static SocketClient getTCPClientInstance(int clientType) {
   switch(clientType) {
        case 0: { // is chargen
            if(charGenTCPClient == null) {
                charGenTCPClient = new CharGenTCPClient();
            }//from w  w  w  .  ja v  a  2  s .c o m
            return charGenTCPClient;
        }
       case 1: { // is daytime 
           if(daytimeTCPClient == null) {
               daytimeTCPClient = new DaytimeTCPClient();
           }
           return daytimeTCPClient;
       }
       case 2: { // is echo
           if(echoTCPClient == null) {
               echoTCPClient = new EchoTCPClient();
           }
           return echoTCPClient;
       }
       case 3: { // is finger
           if(fingerClient == null) {
               fingerClient = new FingerClient();
           }
           return fingerClient;
       }
       case 4: { // is ftp
           if(ftpClient == null) {
               ftpClient = new FTPClient();
           }
           return ftpClient;
       }
       case 5: { // is nntp
           if(nntpClient == null) {
               nntpClient = new NNTPClient();
           }
           return nntpClient;
       }
       case 6: { // is pop3
           if(pop3Client == null) {
               pop3Client = new POP3Client();
           }
           return pop3Client;
       }
       case 7: { // is smtp
           if(smtpClient == null) {
               smtpClient = new SMTPClient();
           }
           return smtpClient;
       }
       case 8: { // is time
           if(timeTCPClient == null) {
               timeTCPClient = new TimeTCPClient();
           }
           return timeTCPClient;
       }
       case 9: { // is whois
           if(whoisClient == null) {
               whoisClient = new WhoisClient();
           }
           return whoisClient;
       }
    }
    return null;
}