Class IRCParser
- java.lang.Object
-
- org.schwering.irc.lib.IRCParser
-
public class IRCParser extends java.lang.ObjectParses a line sent from the IRC server.Note: Probably this class is unimportant for you. It's used by the
IRCConnectionto parse incoming lines. Nevertheless I declared it aspublicbecause you might want to use it to parse IRC command-shortcuts likeMSGinstead ofPRIVMSGin 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.StringgetCommand()Returns the line's command.java.lang.StringgetHost()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.StringgetLine()Returns the unparsed line.java.lang.StringgetMiddle()Returns the line's middle.java.lang.StringgetNick()Returns the nickname of the person who sent the line or the servername of the server which sent the line.java.lang.StringgetParameter(int i)Get one parameter of the line.intgetParameterCount()Gets count of parameters.java.lang.StringgetParameters()Returns the line's parameters which consists of the middle and the trailing.java.lang.StringgetParametersFrom(int i)Grabs the line's parameters from theith to the last parameter (including theith).java.lang.StringgetParametersTo(int i)Grabs the line's parameters from the first to theith parameters (including theith).java.lang.StringgetPrefix()Returns the line's prefix.java.lang.StringgetServername()Returns the servername of the server which sent the line or the nickname of the person who sent the line.java.lang.StringgetTrailing()Returns the line's trailing.IRCUsergetUser()Returns a newIRCUserobject.java.lang.StringgetUsername()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.StringtoString()Generates aStringwith 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.parseColorsmethod.
-
-
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,nullis returned.
Note: This method is totally equal togetServername!
Note: There is also the methodgetUserwhich returns anIRCUserobject which holds the nickname, username and host. By the way, thegetUseruses thegetNick,getUsernameandgetHostmethods to create this object.- Returns:
- The nickname or the servername of the line. If no prefix is given,
nullis 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,nullis returned.
Note: This method is totally equal togetNick!
Note: There is also the methodgetUserwhich returns anIRCUserobject which holds the nickname, username and host. By the way, thegetUseruses thegetNick,getUsernameandgetHostmethods 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 methodgetUserwhich returns anIRCUserobject which holds the nickname, username and host. By the way, thegetUseruses thegetNick,getUsernameandgetHostmethods 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 methodgetUserwhich returns anIRCUserobject which holds the nickname, username and host. By the way, thegetUseruses thegetNick,getUsernameandgetHostmethods to create this object.- Returns:
- The host of the line;
nullif it's not given. - See Also:
getNick(),getUsername(),getUser()
-
getUser
public IRCUser getUser()
Returns a newIRCUserobject. 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
IRCUserobject with exactly those values which are returned by thegetNick,getUsernameandgetHostmethods. - See Also:
getNick(),getUsername(),getHost()
-
getParameterCount
public int getParameterCount()
Gets count of parameters. Ifparametersisn't initialized yet, it callsinitParametersto do that.- Returns:
- The number of parameters.
-
getParameter
public java.lang.String getParameter(int i)
Get one parameter of the line. Ifparametersisn't initialized yet, it callsinitParametersto 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. Ifiis out of bounds,""is returned.
-
getParametersFrom
public java.lang.String getParametersFrom(int i)
Grabs the line's parameters from theith to the last parameter (including theith). Ifparametersisn't initialized yet, it callsinitParametersto do that.- Parameters:
i- The index of the first parameter you want to get.- Returns:
- All parameters behind another beginning at the
ith. Ifiis out of bounds,""is returned.
-
getParametersTo
public java.lang.String getParametersTo(int i)
Grabs the line's parameters from the first to theith parameters (including theith). Ifparametersisn't initialized yet, it callsinitParametersto 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. Ifiis out of bounds,""is returned.
-
toString
public java.lang.String toString()
Generates aStringwith some information about the instance ofIRCParser.
Its format is:classname[prefix,command,middle,trailing].- Overrides:
toStringin classjava.lang.Object- Returns:
- A
Stringwith information about the instance.
-
-