kom.news
Class NewsClient

java.lang.Object
  |
  +--kom.tools.ipc.IPCClient
        |
        +--kom.news.NewsClient

public class NewsClient
extends IPCClient

This is a subclass of IPCClient, used to communicate with NEWS servers using the NNTP protocol.


Inner Class Summary
 class NewsClient.Status
          Status structure used to record command/server status
 
Field Summary
protected static ServerConfig config
          Configuration instance
protected  IPCConnection connection
          IP connection
(package private)  NewsGroup currentNewsGroup
           
protected  java.lang.String host
           
protected  java.lang.String[] overviewFormat
           
protected  java.lang.String pass
          User password for AUTHINFO
protected  int port
           
(package private)  boolean posting
          True when a posting has commenced and not finished
protected  NewsClient.Status status
           
protected  java.io.File tmpFile
           
protected  java.lang.String user
          User name for AUTHINFO
 
Fields inherited from class kom.tools.ipc.IPCClient
connection
 
Constructor Summary
NewsClient(java.lang.String host, int port, ServerConfig config, java.lang.String user, java.lang.String pass)
          Class constructor
NewsClient(java.lang.String host, ServerConfig config, java.lang.String user, java.lang.String pass)
          Class constructor using default NEWS port
 
Method Summary
 java.lang.StringBuffer article(int nnn)
          Gets text of an article
 java.lang.StringBuffer article(java.lang.String msgid)
          Gets text of an article
 boolean authenticate(java.lang.String user, java.lang.String pass)
          Sends authentication info to the server
 int[] available(java.lang.String interval)
          Gets an array of available article numbers in the current newsgroup.
 java.lang.StringBuffer body(int nnn)
          Gets body of an article
 java.lang.StringBuffer body(java.lang.String msgid)
          Gets body of an article
static java.lang.String canonicalize(java.lang.String s)
          Normalizes the argument to the form "host:port".
 boolean checkActive()
          Checks if the server is active or can be made active
 void close()
          Closes connection to remote host.
 java.lang.StringBuffer getArticle(java.lang.String groupname, java.lang.String art, java.lang.String mid)
          Gets the full text of a news article as a StringBuffer
 java.lang.String getError()
          Gets the current error message String
 int getFirst()
          Gets first available article number in current newsgroup
 java.lang.String getGroupName()
          Gets the current group name, or null
 java.lang.StringBuffer getHelp()
          Gets the standard help text from the server
 int getLast()
          Gets last available article number in current newsgroup
private  java.util.Vector getList()
          Gets a list of lines (up to a "." line) from the server
static NewsClient getNewsClient(NewsClient nc, java.lang.String nn, ServerConfig conf, java.lang.String user, java.lang.String pass)
          Retrieves or creates the current NewsClient instance
 java.lang.String getNntp()
          Gets nntphost:port
private  java.lang.String[] getOverviewFormat()
          Gets the format of overview data
protected  NewsClient.Status getStatus()
          Gets current status info
 void group(java.lang.String g)
          Defines current group
 java.lang.StringBuffer head(int nnn)
          Gets headers of an article
 java.lang.StringBuffer head(java.lang.String msgid)
          Gets headers of an article
 void init()
          (Re)initializes this NewsClient with current info
 void init(java.lang.String host, int port, ServerConfig config, java.lang.String user, java.lang.String pass)
          Initializes this NewsClient
 void init(java.lang.String nntp, ServerConfig config, java.lang.String user, java.lang.String pass)
          Initializes this NewsClient
 boolean isReady()
          Returns true if status indicates that the server is ready to accept commandd
 int last()
          Goes back one article in newsgroup, if valid
 java.util.Vector list()
          Gets a list of newsgroups, one per line.
 Overview[] loadOverviews(java.lang.String ng, java.lang.String load, KomData rc)
          Makes an Overview array from a newsgroup.
 java.util.Vector newgroups(java.lang.String datetime)
          Gets a list of new newsgroups, one per line.
 int next()
          Goes forward one article in newsgroup, if valid
private  int nextorlast(java.lang.String cmd)
          Goes back or forward one article in newsgroup, if valid
 java.lang.StringBuffer particle(java.lang.String choice, java.lang.String nnn)
          Gets text of full ARTICLE, HEAD, OR BODY, or STAT results for an article
 boolean postfinish()
          Finishes the current posting by sending a final dot + eol.
 java.io.OutputStream postprepare()
          Gets an OutputStream for sending the text of a POST
 void quit()
          Sends a QUIT command to the server, breaking the connection
 java.lang.StringBuffer readText(java.lang.StringBuffer sb)
          Receives data from server
 int request(java.lang.String line)
          Sends a request to the NEWS server
 boolean requestOk()
          Returns true if request status is ok
 void resetError()
          Resets status.requestOK to true
 int send(java.lang.String line)
          Sends a line to the NEWS server
 void setAuth(java.lang.String user, java.lang.String pass)
          Sets parameters for possible authorization
 void setgroup(java.lang.String name)
          Defines current group
 java.lang.StringBuffer status(int nnn)
          Gets status of an article
 java.lang.StringBuffer status(java.lang.String msgid)
          Gets status of an article
 Overview[] xover(int n)
          Gets overview data for an article
 Overview[] xover(int m, int n)
          Gets overview data for a range of articles
 Overview[] xover(java.lang.String interval)
          Gets an array of article overview data for the current newsgroup.
 
Methods inherited from class kom.tools.ipc.IPCClient
connect
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

config

protected static ServerConfig config
Configuration instance

connection

protected IPCConnection connection
IP connection

host

protected java.lang.String host

port

protected int port

tmpFile

protected java.io.File tmpFile

overviewFormat

protected java.lang.String[] overviewFormat

status

protected NewsClient.Status status

currentNewsGroup

NewsGroup currentNewsGroup

user

protected java.lang.String user
User name for AUTHINFO

pass

protected java.lang.String pass
User password for AUTHINFO

posting

boolean posting
True when a posting has commenced and not finished
Constructor Detail

NewsClient

public NewsClient(java.lang.String host,
                  int port,
                  ServerConfig config,
                  java.lang.String user,
                  java.lang.String pass)
Class constructor
Parameters:
host - Remote server address
port - Port on remote server
config - Configuration for this client

NewsClient

public NewsClient(java.lang.String host,
                  ServerConfig config,
                  java.lang.String user,
                  java.lang.String pass)
Class constructor using default NEWS port
Parameters:
host - Remote server address
config - Configuration for this client
Method Detail

getArticle

public java.lang.StringBuffer getArticle(java.lang.String groupname,
                                         java.lang.String art,
                                         java.lang.String mid)
Gets the full text of a news article as a StringBuffer
Parameters:
groupname - Newsgroup name
art - Article number in newsgroup
mid - Message id of article. Used if article number fails
Returns:
A Stringbuffer containing the full article with headers, unless an error is indicated by status.

setAuth

public void setAuth(java.lang.String user,
                    java.lang.String pass)
Sets parameters for possible authorization
Parameters:
user - User name for authorization
pass - Password for authorization

getNntp

public java.lang.String getNntp()
Gets nntphost:port
Returns:
Hostname:portnumber, e g "news.kth.se:119"

getFirst

public int getFirst()
Gets first available article number in current newsgroup
Returns:
First valid article number in current newsgroup, or 0. May be inaccurate (too low).

getLast

public int getLast()
Gets last available article number in current newsgroup
Returns:
Last valid article in current newsgroup, or 0. May be too low.

getError

public java.lang.String getError()
Gets the current error message String
Returns:
Response for last "stat" command or null if there is no status info, the empty string if status is ok.

resetError

public void resetError()
Resets status.requestOK to true

canonicalize

public static java.lang.String canonicalize(java.lang.String s)
Normalizes the argument to the form "host:port".
Parameters:
s - News url or just a host domain, possibly also ":port"
Returns:
hostdomain:port (default port == 119)

getNewsClient

public static NewsClient getNewsClient(NewsClient nc,
                                       java.lang.String nn,
                                       ServerConfig conf,
                                       java.lang.String user,
                                       java.lang.String pass)
                                throws NewsClientException
Retrieves or creates the current NewsClient instance
Parameters:
nc - Existing NewsClient reference, or null
nn - NNTP reference (host:port) corresponding to nc
conf - KOM server configuration instance
user - Authinfo user data, or null
pass - Authinfo password data, or null
Returns:
The nc NewsClient instance or a new instance

init

public void init(java.lang.String nntp,
                 ServerConfig config,
                 java.lang.String user,
                 java.lang.String pass)
Initializes this NewsClient
Parameters:
nntp - Host:port, or just host, maybe with "news://" as prefix
config - KOM server configuration data
user - Authenticating user name
pass - Authenticating password

init

public void init(java.lang.String host,
                 int port,
                 ServerConfig config,
                 java.lang.String user,
                 java.lang.String pass)
Initializes this NewsClient
Parameters:
host - Host domain
port - NNTP port
config - KOM server configuration data
user - Authenticating user name
pass - Authenticating password

init

public void init()
(Re)initializes this NewsClient with current info

getOverviewFormat

private java.lang.String[] getOverviewFormat()
Gets the format of overview data

list

public java.util.Vector list()
Gets a list of newsgroups, one per line. Line format: group last first p where group is group name, last and first are last and first available article numbers, p is y or n indicating if posting is allowed.
Returns:
Vector of lines

newgroups

public java.util.Vector newgroups(java.lang.String datetime)
Gets a list of new newsgroups, one per line. Line format: group last first p where group is group name, last and first are last and first available article numbers, p is y or n indicating if posting is allowed.
Parameters:
datetime - YYMMDD HHMMSS [GMT] []
Returns:
Vector of lines

getList

private java.util.Vector getList()
Gets a list of lines (up to a "." line) from the server
Returns:
a Vector of lines, or null if there is no data

getGroupName

public java.lang.String getGroupName()
Gets the current group name, or null

group

public void group(java.lang.String g)
Defines current group
Parameters:
g - Group name

setgroup

public void setgroup(java.lang.String name)
Defines current group
Parameters:
name - Group name

article

public java.lang.StringBuffer article(int nnn)
Gets text of an article
Parameters:
nnn - Article number
Returns:
StringBuffer with article text

article

public java.lang.StringBuffer article(java.lang.String msgid)
Gets text of an article
Parameters:
msgid - Article MessageID
Returns:
StringBuffer with article text

head

public java.lang.StringBuffer head(int nnn)
Gets headers of an article
Parameters:
nnn - Article number
Returns:
StringBuffer with headers text

head

public java.lang.StringBuffer head(java.lang.String msgid)
Gets headers of an article
Parameters:
msgid - Article MessageID
Returns:
StringBuffer with article text

body

public java.lang.StringBuffer body(int nnn)
Gets body of an article
Parameters:
nnn - Article number
Returns:
StringBuffer with article body text

body

public java.lang.StringBuffer body(java.lang.String msgid)
Gets body of an article
Parameters:
msgid - Article MessageID
Returns:
StringBuffer with article body

status

public java.lang.StringBuffer status(int nnn)
Gets status of an article
Parameters:
nnn - Article number
Returns:
StringBuffer with article status

status

public java.lang.StringBuffer status(java.lang.String msgid)
Gets status of an article
Parameters:
msgid - Article MessageID
Returns:
StringBuffer with article status

particle

public java.lang.StringBuffer particle(java.lang.String choice,
                                       java.lang.String nnn)
Gets text of full ARTICLE, HEAD, OR BODY, or STAT results for an article
Parameters:
choice - One of "article", "head", "body" or "stat"
nnn - Optional article number or
Returns:
Requested text, unless an error condition is indicated by status

authenticate

public boolean authenticate(java.lang.String user,
                            java.lang.String pass)
Sends authentication info to the server
Parameters:
user - User name
pass - Password
Returns:
true if authentication was accepted

postprepare

public java.io.OutputStream postprepare()
Gets an OutputStream for sending the text of a POST
Returns:
OutputStream for the connection, or null if posting is not possible

postfinish

public boolean postfinish()
Finishes the current posting by sending a final dot + eol.
Returns:
true if the posting succeeded

send

public final int send(java.lang.String line)
Sends a line to the NEWS server
Parameters:
line - Line w/o eol

request

public final int request(java.lang.String line)
Sends a request to the NEWS server
Parameters:
line - Line w/o eol

getStatus

protected NewsClient.Status getStatus()
Gets current status info
Returns:
Status instance

getHelp

public java.lang.StringBuffer getHelp()
Gets the standard help text from the server

last

public int last()
Goes back one article in newsgroup, if valid
Returns:
article pointer, or -1

next

public int next()
Goes forward one article in newsgroup, if valid
Returns:
article pointer, or -1

nextorlast

private int nextorlast(java.lang.String cmd)
Goes back or forward one article in newsgroup, if valid
Parameters:
cmd - "next" or "last"
Returns:
article pointer, or -1

xover

public Overview[] xover(int n)
Gets overview data for an article
Parameters:
n - Article number in current newsgroup
Returns:
Overview data for the article

xover

public Overview[] xover(int m,
                        int n)
Gets overview data for a range of articles
Parameters:
m - First article number
n - Last article number
Returns:
Overview data array for the articles in the range

loadOverviews

public Overview[] loadOverviews(java.lang.String ng,
                                java.lang.String load,
                                KomData rc)
Makes an Overview array from a newsgroup.
Parameters:
ng - Newsgroup name
load - Max new entries, or "all"
rc - If non-null, shows entries already loaded

quit

public void quit()
Sends a QUIT command to the server, breaking the connection

readText

public java.lang.StringBuffer readText(java.lang.StringBuffer sb)
Receives data from server
Parameters:
sb - Existing StringBuffer, or null
Returns:
the old StringBuffer, or a new StringBuffer, containing the (additional) data received.

close

public void close()
Closes connection to remote host. Sets serverReady status to false

checkActive

public boolean checkActive()
Checks if the server is active or can be made active
Returns:
true if the server is ready, possibly after reinitialization

isReady

public boolean isReady()
Returns true if status indicates that the server is ready to accept commandd

requestOk

public boolean requestOk()
Returns true if request status is ok

xover

public Overview[] xover(java.lang.String interval)
Gets an array of article overview data for the current newsgroup.
Parameters:
interval - 1) empty: current article 2) an article number 3) a range: "mm-nn" 4) an open range: "mm-" (mm to last)
Returns:
Overview array - header data for available articles

available

public int[] available(java.lang.String interval)
Gets an array of available article numbers in the current newsgroup.
Parameters:
interval - 1) empty: current article 2) an article number 3) a range: "mm-nn" 4) an open range: "mm-" (mm to last)
Returns:
Array of article numbers