Results 1 to 7 of 7
  1. #1
    steamruler is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Question 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:
    Java 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?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default 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?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    steamruler is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default 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:
    Java 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:
    Java 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
    Last edited by steamruler; 03-05-2012 at 08:48 PM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: Replying to a PING message in an IRC Bot.

    Java 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    steamruler is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default Re: Replying to a PING message in an IRC Bot.

    Quote Originally Posted by Tolls View Post
    Java 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):
    Java 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:
    :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
    Last edited by steamruler; 03-06-2012 at 05:56 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default 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).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    steamruler is offline Member
    Join Date
    Feb 2012
    Posts
    9
    Rep Power
    0

    Default 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!

    Java 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!

Similar Threads

  1. [Error] Reading mails and replying at the same time
    By ravjot28 in forum Networking
    Replies: 23
    Last Post: 09-24-2010, 08:12 AM
  2. Replies: 6
    Last Post: 06-04-2010, 08:43 PM
  3. Cannot perform a PING
    By mnementh64 in forum Networking
    Replies: 1
    Last Post: 09-17-2009, 04:39 PM
  4. ping
    By Arunmathi in forum JCreator
    Replies: 0
    Last Post: 08-28-2009, 08:24 PM
  5. ping cmd
    By kishi in forum Advanced Java
    Replies: 2
    Last Post: 11-14-2007, 03:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •