Main Loop
There are just a few more concepts we must be aware of. First is a “ping”, it’s where the server is making sure our client is still active and not just connected for no reason. We’ll recieve these periodically, but they may appear in any line - so we have to silter them out. If you don’t respond for a while, the server will kick you out.
0081 Command: PING 0082 Parameters: <server1> [<server2>]
And the correct response to a “ping”, is a “pong”:
0083 Command: PONG 0084 Parameters: <daemon> [<daemon2>]
At the same time, we also request to join a channel:
0085 Command: JOIN
0086 Parameters: <channel>{,<channel>} [<key>{,<key>}]
Technically you should only have to join once… But. Because we don’t listen to the “MOTD” (message of the day) end, we don’t know if we were accepted and therefore connect the join request at the correct time. It’s also possible that our bot gets kicked out of the channel. For simplicty, we just send it every time we have to pong back at the server. (All tested servers had no problem with this, so don’t @ me.)
In the RFC the protocol is as follows:
0087 Command: PRIVMSG
0088 Parameters: <receiver>{,<receiver>} <text to be sent>
We are only interested in one case for now, messages in the channel. Therefore we want the format (“direct” messages are a different format):
0089 PRIVMSG <channel> <server> :<msg>
And finally the code for the main loop:
0090 if(socket != null){ // Make sure there is a server
0091 String in = null; // Pre-define a variable we will always use
0092 boolean running = true; // Possibility of stopping the client
0093 while(running){ // Bot should reply to all messages
0094 try{ // We don't want our client to crash if we wrote bad code
0095 in = getLine(); // Get a line from the server
0096 if(in.contains("PING")){ // Did the server ping us?
0097 String[] resp = in.split(" "); // Get parts of ping
0098 if(resp.length >= 2){ // Make sure we heard a valid ping
0099 sendLine("PONG " + resp[1] + "\r"); // Send the ping response
0100 sendLine("JOIN " + channel); // Re-join channel encase of disconnect
0101 }else{
0102 System.err.println("Unable to PONG the IRC server");
0103 }
0104 }else{ // We likely just heard a message
0105 int s = in.indexOf(':');
0106 int e = in.indexOf(':', s + 1);
0107 if(s >= 0 && e >= 0){ // Did we understand the message?
0108 String[] head = in.substring(s + 1, e).split(" "); // Get info
0109 if(
0110 head.length == 3 && // Make sure there were enough parts
0111 head[1].equals("PRIVMSG") && // Make sure it was a message
0112 !head[0].equals(server) // Check it was from the server
0113 ){
0114 String usr = head[0].split("!")[0]; // Who sent it?
0115 if(!usr.equals(user)){ // Don't reply to ourself
0116 String msg = in.substring(e + 1); // Get the actual message
0117 /* TODO: Process the message and response here! */
0118 sendLine("PRIVMSG " + channel + " :" + msg); // Echo message back
0119 }
0120 }
0121 }
0122 }
0123 }catch(Exception e){
0124 System.err.println("Bot tried to die! Line: " + in); // Print offender
0125 e.printStackTrace(); // Information about crash location
0126 }
0127 }
0128 }else{
0129 System.err.println("Unable to run server"); // Say something went wrong
0130 }
That’s all, folks!
Coffee Space 