Class IRCParser
- java.lang.Object
-
- org.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 aspublic
because you might want to use it to parse IRC command-shortcuts likeMSG
instead ofPRIVMSG
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 (withgetTrailing
), 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
- See Also:
IRCConnection
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
getCommand()
Returns the line's command.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 returnsnull
.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 thei
th to the last parameter (including thei
th).java.lang.String
getParametersTo(int i)
Grabs the line's parameters from the first to thei
th parameters (including thei
th).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 newIRCUser
object.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 returnsnull
.java.lang.String
toString()
Generates aString
with some information about the instance ofIRCParser
.
Its format is:classname[prefix,command,middle,trailing]
.
-
-
-
Constructor Detail
-
IRCParser
public IRCParser(java.lang.String line)
Parses the line after erasing all mIRC color codes. This constructor is a shorthand forIRCParser(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
- Iffalse
, mIRC color codes are parsed out by usingIRCUtil.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 togetServername
!
Note: There is also the methodgetUser
which returns anIRCUser
object which holds the nickname, username and host. By the way, thegetUser
uses thegetNick
,getUsername
andgetHost
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 togetNick
!
Note: There is also the methodgetUser
which returns anIRCUser
object which holds the nickname, username and host. By the way, thegetUser
uses thegetNick
,getUsername
andgetHost
methods to create this object.
-
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 returnsnull
.
Note: There is also the methodgetUser
which returns anIRCUser
object which holds the nickname, username and host. By the way, thegetUser
uses thegetNick
,getUsername
andgetHost
methods to create this object.
-
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 returnsnull
.
Note: There is also the methodgetUser
which returns anIRCUser
object which holds the nickname, username and host. By the way, thegetUser
uses thegetNick
,getUsername
andgetHost
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 newIRCUser
object. This method is equal tonew 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 thegetNick
,getUsername
andgetHost
methods. - See Also:
getNick()
,getUsername()
,getHost()
-
getParameterCount
public int getParameterCount()
Gets count of parameters. Ifparameters
isn't initialized yet, it callsinitParameters
to do that.- Returns:
- The number of parameters.
-
getParameter
public java.lang.String getParameter(int i)
Get one parameter of the line. Ifparameters
isn't initialized yet, it callsinitParameters
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
i
th parameter. Ifi
is out of bounds,""
is returned.
-
getParametersFrom
public java.lang.String getParametersFrom(int i)
Grabs the line's parameters from thei
th to the last parameter (including thei
th). Ifparameters
isn't initialized yet, it callsinitParameters
to do that.- Parameters:
i
- The index of the first parameter you want to get.- Returns:
- All parameters behind another beginning at the
i
th. Ifi
is out of bounds,""
is returned.
-
getParametersTo
public java.lang.String getParametersTo(int i)
Grabs the line's parameters from the first to thei
th parameters (including thei
th). Ifparameters
isn't initialized yet, it callsinitParameters
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
i
th. Ifi
is out of bounds,""
is returned.
-
toString
public java.lang.String toString()
Generates aString
with some information about the instance ofIRCParser
.
Its format is:classname[prefix,command,middle,trailing]
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- A
String
with information about the instance.
-
-