Replying to a PING message in an IRC Bot.
I am making an IRC Bot. The thing is, IRC randomly sends PING messages. I need to filter these and reply to these PING messages with a PONG message.
An usual log of 1 minute is:
Code:
:coyote.furnet.org NOTICE AUTH :*** Looking up your hostname...
:coyote.furnet.org NOTICE AUTH :*** Checking ident...
:coyote.furnet.org NOTICE AUTH :*** Found your hostname
:coyote.furnet.org NOTICE AUTH :*** No ident response; username prefixed with ~
PING :BF005A21
:coyote.furnet.org 451 SnowMurrTestUser :You have not registered
(Yes, I am a furry. Deal with it.)
To successfully stay on the server you need to reply to the PING :BF005A21 message with a
PONG :BF005A21 message. The letters and numbers after the : is randomly generated. I figure i need too split the string.
However, i need the other messages to stay intact.
How will i do this?
Re: Replying to a PING message in an IRC Bot.
How are you reading the messages?
If line by line as they come in (which is what that looks like to me) then seeing if it's a PING shouldn't be a problem?
Re: Replying to a PING message in an IRC Bot.
I am reading them one-by-one as they drop in. I have no idea how I will identify if it is an PING message however.
EDIT: I now split them, but for some reason it does not acnowledge that the server sends the PING-command.
My Java Code:
Code:
while (true) {
//out.println(userInput);
line = in.readLine();
if (line != null) {
linearray = line.split(":");
System.out.println("PING :" + linearray[1]); //Debug to compare the lines
if (linearray[0] == "PING ") {
out.println("PONG :" + linearray[1]);
System.out.println("Recieved Ping with ID " + linearray[1] + ". Sent PONG with ID " + linearray[1] + "!");
}
else {
System.out.println(line);
}
}
}
The Log:
Code:
Opening connection to irc.furnet.org at port 6667...
PING :coyote.furnet.org NOTICE AUTH
:coyote.furnet.org NOTICE AUTH :*** Looking up your hostname...
PING :coyote.furnet.org NOTICE AUTH
:coyote.furnet.org NOTICE AUTH :*** Found your hostname (cached)
PING :coyote.furnet.org NOTICE AUTH
:coyote.furnet.org NOTICE AUTH :*** Checking ident...
PING :coyote.furnet.org NOTICE AUTH
:coyote.furnet.org NOTICE AUTH :*** No ident response; username prefixed with ~
PING :8C99AFE2 <----------- Same, that would trigger the
PING :8C99AFE2 <----------- action and send "PONG :8C99AFE2"
PING :coyote.furnet.org 451 SnowMurrTestUser
:coyote.furnet.org 451 SnowMurrTestUser :You have not registered
Re: Replying to a PING message in an IRC Bot.
Code:
linearray[0] == "PING "
Never use '==' to compare objects. ALways (unless you really need to know if they are the same reference) use 'equals()'.
You could save a bit of processing on the JVMs behald by simply comparing the first 4 characters (using substring) before doing the split, as you are currently splitting lines which don't need to be split.
Re: Replying to a PING message in an IRC Bot.
Quote:
Originally Posted by
Tolls
Code:
linearray[0] == "PING "
Never use '==' to compare objects. ALways (unless you really need to know if they are the same reference) use 'equals()'.
You could save a bit of processing on the JVMs behald by simply comparing the first 4 characters (using substring) before doing the split, as you are currently splitting lines which don't need to be split.
Okay, done. I am still splitting the lines by the ":" because they are also used before messages (regular messages) recieved.
However, I keep getting an error here on line 5 (ArrayOutOfBoundsException):
Code:
public static String CreateCompleteMessage(String[] Array) {
Integer control = Array.length - 1;
Integer beginningoffmessage = 2; //We guess it is 2. Bad practice, I know.
Integer shuffling = beginningoffmessage + 1;
String returnmessage = Array[beginningoffmessage];
PrintMessage(control.toString());
if (control.equals(beginningoffmessage) == false) {
while (shuffling.equals(control) == false) {
returnmessage = returnmessage.concat(":");
returnmessage = returnmessage.concat(Array[shuffling]);
shuffling = shuffling + 1;
}
}
return returnmessage;
}
The line being read when it is thrown is:
Quote:
:foxtaur.furnet.org 003 SnowMurrTestUser :This server was created Sun Jan 31 2010 at 21:57:37 EST
In order to debug: Split the string and feed it to the function.
Expected Output: "This server was created Sun Jan 31 2010 at 21:57:37 EST"
Help please :P
Re: Replying to a PING message in an IRC Bot.
Use some printlns to see what the data is.
What is the value of 'control'.
What is the contents of 'Array' (which is a bad name, should be 'array' or preferably a more meaningful description based on what the code is operating on)?
Print that out (useing a for loop).
Re: Replying to a PING message in an IRC Bot.
Heh, got an idea before i saw you replied and remade the function!
Instead of trying to put together strings in a array i got the lenght of the strings i wanted gone and removed them from the string in the end!
Code:
public static String CreateCompleteMessage(String[] stringarray, String OriginalMessage) {
Integer letterno0 = stringarray[0].length();
Integer letterno1 = stringarray[1].length();
Integer letterstoremove = letterno0 + letterno1 + 1; // The extra 1 is to justify the ":" in the beginning of every message.
String returnmessage = OriginalMessage.substring(letterstoremove);
return returnmessage;
}
No more problems right now, so I consider this solved!