org.schwering.irc.lib
Class IRCParser

java.lang.Object
  extended byorg.schwering.irc.lib.IRCParser

public class IRCParser
extends java.lang.Object

Parses a line sent from the IRC server.

Note: Probably this class is unimportant for you. It's used by the IRCConnection to parse incoming lines. Nevertheless I declared it as public because you might want to use it to parse IRC command-shortcuts like MSG instead of PRIVMSG in your client.
The following text goes on with the description of the class and what it does.

According with RFC1459 it divides the line into a prefix, a command and its parameters.

The prefix is only given if a line starts with a : (colon) and is used to indicate from where the line is send.

The next word in the line (if no prefix exists it is the first, else the second word) is the command. The command is eiter a valid IRC command or a three-digit number which represents a numeric reply or a numeric error.

The parameters are divided into a middle and a trailing part. In the middle part one word means one parameter while the trailing part is just one parameter independent from the amount of words in it. If there is a " :" (space+colon) in the line, this point means the beginning of the trailing. If there is no such space+colon, the trailing is just the last word. All words behind the space+colon mean just one parameter. If there is only one parameter given (the parameter is the first, the last and the only one), the parameter is available as trailing (with getTrailing), not as middle!

One line may have up to 15 parameters. Therefore up to 14 are middle and one is the trailing.

The line may have up to 510 characters plus the CR-LF (carriage return - line feed) which trails the incoming line.

The following extract of the RFC1459 shows the message format in BNF:

<message>  ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix>   ::= <servername> | <nick> [ '!' <username> ] [ '@' <host> ]
<command>  ::= <letter> { <letter> } | <number> <number> <number>
<SPACE>    ::= ' ' { ' ' }
<params>   ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
<middle>   ::= <Any *non-empty* sequence of octets not including SPACE or NUL or CR or LF, the first of which may not be ':'>
<trailing> ::= <Any, possibly *empty*, sequence of octets not including NUL or CR or LF>
<crlf>     ::= CR LF

Version:
3.22
Author:
Christoph Schwering <schwering@gmail.com>
See Also:
IRCConnection

Constructor Summary
IRCParser(java.lang.String line)
          Parses the line after erasing all mIRC color codes.
IRCParser(java.lang.String line, boolean colorsEnabled)
          The main constructor.
 
Method Summary
 java.lang.String getCommand()
          Returns the line's command.
 java.lang.String getHost()
          Returns the host of the person who sent the line.
 java.lang.String getLine()
          Returns the unparsed line.
 java.lang.String getMiddle()
          Returns the line's middle.
 java.lang.String getNick()
          Returns the nickname of the person who sent the line or the servername of the server which sent the line.
 java.lang.String getParameter(int i)
          Get one parameter of the line.
 int getParameterCount()
          Gets count of parameters.
 java.lang.String getParameters()
          Returns the line's parameters which consists of the middle and the trailing.
 java.lang.String getParametersFrom(int i)
          Grabs the line's parameters from the ith to the last parameter (including the ith).
 java.lang.String getParametersTo(int i)
          Grabs the line's parameters from the first to the ith parameters (including the ith).
 java.lang.String getPrefix()
          Returns the line's prefix.
 java.lang.String getServername()
          Returns the servername of the server which sent the line or the nickname of the person who sent the line.
 java.lang.String getTrailing()
          Returns the line's trailing.
 IRCUser getUser()
          Returns a new IRCUser object.
 java.lang.String getUsername()
          Returns the username of the person who sent the line.
 java.lang.String toString()
          Generates a String with some information about the instance of IRCParser.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IRCParser

public IRCParser(java.lang.String line)
Parses the line after erasing all mIRC color codes. This constructor is a shorthand for IRCParser(line, false).

Parameters:
line - The line which will be parsed.

IRCParser

public IRCParser(java.lang.String line,
                 boolean colorsEnabled)
The main constructor. Parses prefix, command, middle and trailing.

Parameters:
line - The line which will be parsed.
colorsEnabled - If false, mIRC color codes are parsed out by using IRCUtil.parseColors method.
Method Detail

getPrefix

public java.lang.String getPrefix()
Returns the line's prefix. A prefix is the part which contains information about the sender of the line. If no prefix is set, "" is returned; but in fact there's always a prefix.

Returns:
The line's prefix.

getCommand

public java.lang.String getCommand()
Returns the line's command.

Returns:
The line's command.

getMiddle

public java.lang.String getMiddle()
Returns the line's middle.

Returns:
The line's middle.

getTrailing

public java.lang.String getTrailing()
Returns the line's trailing.

Returns:
The line's trailing.

getLine

public java.lang.String getLine()
Returns the unparsed line. It looks exacttly as the server sent it, but if colors are disabled and therefore already parsed out by IRCUtil.parseColors, the colors are not included in here.

Returns:
The line.

getParameters

public java.lang.String getParameters()
Returns the line's parameters which consists of the middle and the trailing.

Returns:
The line's parameters.

getNick

public java.lang.String getNick()
Returns the nickname of the person who sent the line or the servername of the server which sent the line.
It is found in the prefix which always looks like that:
<servername> | <nick> [ '!' <username> ] [ '@' <host> ]

If no prefix is given in the whole line, null is returned.

Note: This method is totally equal to getServername!
Note: There is also the method getUser which returns an IRCUser object which holds the nickname, username and host. By the way, the getUser uses the getNick, getUsername and getHost methods to create this object.

Returns:
The nickname or the servername of the line. If no prefix is given, null is returned.
See Also:
getServername(), getUsername(), getHost(), getUser()

getServername

public java.lang.String getServername()
Returns the servername of the server which sent the line or the nickname of the person who sent the line.
It is found in the prefix which always looks like that:
<servername> | <nick> [ '!' <username> ] [ '@' <host> ]

If no prefix is given in the whole line, null is returned.

Note: This method is totally equal to getNick!
Note: There is also the method getUser which returns an IRCUser object which holds the nickname, username and host. By the way, the getUser uses the getNick, getUsername and getHost methods to create this object.

Returns:
The servername or the nickname of the line. If no prefix is given, null is returned.
See Also:
getNick(), getUser()

getUsername

public java.lang.String getUsername()
Returns the username of the person who sent the line.
It is found in the prefix which always looks like that:
<servername> | <nick> [ '!' <username> ] [ '@' <host> ]

If the username is not specified, this method returns null.
Note: There is also the method getUser which returns an IRCUser object which holds the nickname, username and host. By the way, the getUser uses the getNick, getUsername and getHost methods to create this object.

Returns:
The username of the line; null if it's not given.
See Also:
getNick(), getHost(), getUser()

getHost

public java.lang.String getHost()
Returns the host of the person who sent the line.
It is found in the prefix which always looks like that:
<servername> | <nick> [ '!' <username> ] [ '@' <host> ]

If the host is not specified, this method returns null.
Note: There is also the method getUser which returns an IRCUser object which holds the nickname, username and host. By the way, the getUser uses the getNick, getUsername and getHost methods to create this object.

Returns:
The host of the line; null if it's not given.
See Also:
getNick(), getUsername(), getUser()

getUser

public IRCUser getUser()
Returns a new IRCUser object. This method is equal to new IRCUser(IRCParser.getNick(), IRCParser.getUsername(), IRCParser.getHost()). See those methods to learn which value they return if they are not set.

Returns:
A new IRCUser object with exactly those values which are returned by the getNick, getUsername and getHost methods.
See Also:
getNick(), getUsername(), getHost()

getParameterCount

public int getParameterCount()
Gets count of parameters. If parameters isn't initialized yet, it calls initParameters to do that.

Returns:
The number of parameters.

getParameter

public java.lang.String getParameter(int i)
Get one parameter of the line. If parameters isn't initialized yet, it calls initParameters to do that.

Parameters:
i - The index of the parameter you want to get. The index starts with 1 and not with 0.
Returns:
The ith parameter. If i is out of bounds, "" is returned.

getParametersFrom

public java.lang.String getParametersFrom(int i)
Grabs the line's parameters from the ith to the last parameter (including the ith). If parameters isn't initialized yet, it calls initParameters to do that.

Parameters:
i - The index of the first parameter you want to get.
Returns:
All parameters behind another beginning at the ith. If i is out of bounds, "" is returned.

getParametersTo

public java.lang.String getParametersTo(int i)
Grabs the line's parameters from the first to the ith parameters (including the ith). If parameters isn't initialized yet, it calls initParameters to do that.

Parameters:
i - The index of the last parameter you want to get.
Returns:
All parameters beginning at the first and ending at the ith. If i is out of bounds, "" is returned.

toString

public java.lang.String toString()
Generates a String with some information about the instance of IRCParser.
Its format is: classname[prefix,command,middle,trailing].

Returns:
A String with information about the instance.