/*
 * @(#)NntpNewsgroup.java
 *
 * Copyright (c) 2001-2002 JScape
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JScape. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JScape.
 */
package com.jscape.inet.nntp;

import java.util.StringTokenizer;


/**
 * Implements methods for manipulating NNTP newsgroups. Note:<br>
 * According to RFC 977, server is not required to return exact article count
 * in response to GROUP command. In addition, often NntpNewsgroup is created
 * from response to LIST command in which case article count is not returned
 * at all but can be estimated from FIRST and LAST fields. Thus, the article
 * count provided by this class should not be regarded as exact value.<br>
 * Posting permissions can also be treated with suspicion as GROUP command
 * will not return any information regarding posting and LIST command returns
 * this information based on authentication at the moment of invoking LIST
 * command.
 */
public class NntpNewsgroup {
    private String name;
    private String sfirst;
    private String slast;
    private int count;
    private int first;
    private int last;
    private boolean post;

    /**
     * Creates NntpNewsgroup from server response to LIST command.
     *
     * @param desc name of newsgroup
     *
     * @throws NntpException if an I/O or NNTP related error occurs
     */
    protected NntpNewsgroup(String desc)
            throws NntpException {
        try {
            StringTokenizer tok = new StringTokenizer(desc, " ");

            name = tok.nextToken();
            slast = tok.nextToken();
            sfirst = tok.nextToken();

            String perm = tok.nextToken();

            post = perm.equals("y");
            first = Integer.parseInt(sfirst);
            last = Integer.parseInt(slast);
            count = last - first;
        } catch (Exception e) {
            throw new NntpException("Invalid server response.");
        }
    }

    /**
     * Creates NntpNewsgroup from server response to GROUP command.<br>
     * Posting permission is not available in response to this command and it
     * will be assigned from the second parameter 'post'.
     *
     * @param desc name of newsgroup
     * @param post <CODE>true</CODE> if posting allowed, <CODE>false</CODE>
     *             otherwise
     *
     * @throws NntpException if an I/O or NNTP related error occurs
     */
    protected NntpNewsgroup(String desc, boolean post)
            throws NntpException {
        try {
            StringTokenizer tok = new StringTokenizer(desc, " ");
            String snum = tok.nextToken();

            sfirst = tok.nextToken();
            slast = tok.nextToken();
            name = tok.nextToken();

            // default to yes, but Nntp.postArticle will check anyway
            this.post = post;
            first = Integer.parseInt(sfirst);
            last = Integer.parseInt(slast);
            count = Integer.parseInt(snum);
        } catch (Exception e) {
            throw new NntpException("Invalid server response.");
        }
    }

    /**
     * Gets name of this <code>NntpNewsgroup</code>.
     *
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * Returns the value of FIRST field returned by LIST and GROUP commands.
     *
     * @return value of FIRST field
     */
    public int getFirst() {
        return first;
    }

    /**
     * Returns the value of LAST field returned by LIST and GROUP commands.
     *
     * @return value of LAST field
     */
    public int getLast() {
        return last;
    }

    /**
     * Gets estimated article count for this <code>NntpNewsgroup</code>.
     *
     * @return article count
     */
    public int getArticleCount() {
        return count;
    }

    /**
     * Gets posting permissions for this NntpNewsgroup at time of retrieving
     * newsgroup data.
     *
     * @return <CODE>true</CODE> if posting allowed, <CODE>false</CODE>
     *         otherwise
     */
    public boolean getPostPermission() {
        return post;
    }

    /**
     * Gets string representation of newsgroup in format as expected by server
     * when posting article.
     *
     * @return name of newsgroup
     */
    public String toString() {
        StringBuffer a = new StringBuffer();

        a.append(name);
        a.append(" ");
        a.append(slast);
        a.append(" ");
        a.append(sfirst);
        a.append(" ");

        if (post) {
            a.append("y");
        } else {
            a.append("n");
        }

        return a.toString();
    }
}
