--- projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java 2002/01/17 17:58:12 1.30 +++ projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java 2002/02/04 00:14:13 1.30.2.1 @@ -10,6 +10,7 @@ import java.io.*; import java.net.*; import java.util.*; import java.text.DateFormat; +import org.jibble.pircbot.*; /** * This Alert sends an IRC message. @@ -18,7 +19,7 @@ import java.text.DateFormat; * IRCBot inner class. * * @author $Author: tdb $ - * @version $Id: IRC__Alerter.java,v 1.30 2002/01/17 17:58:12 tdb Exp $ + * @version $Id: IRC__Alerter.java,v 1.30.2.1 2002/02/04 00:14:13 tdb Exp $ */ public class IRC__Alerter extends AlerterSkeleton { @@ -27,29 +28,27 @@ public class IRC__Alerter extends AlerterSkeleton { /** * The current CVS revision of this class */ - public final String REVISION = "$Revision: 1.30 $"; + public final String REVISION = "$Revision: 1.30.2.1 $"; /** * A description of this alerter */ public final String DESC = "Sends alerts on an IRC channel"; - /** - * The default reconnect delay in seconds - */ - public final int DEFAULT_RECONNECT_DELAY = 30; - //---STATIC METHODS--- //---CONSTRUCTORS--- - + public IRC__Alerter() { super(); - // connect to the IRC server + // construct and initialise the bot _ircbot = new IRCBot(); + //_ircbot.setVerbose(true); + Thread ircThread = new Thread(_ircbot); // set it's name and start it - _ircbot.setName("client.IRC__Alerter$IRCBot"); - _ircbot.start(); + ircThread.setName("client.IRC__Alerter$IRCBot"); + ircThread.start(); + // log our start time _startTime = System.currentTimeMillis(); _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); } @@ -169,6 +168,12 @@ public class IRC__Alerter extends AlerterSkeleton { private long _startTime; /** + * This holds a reference to the + * system logger that is being used. + */ + protected Logger _logger = ReferenceManager.getInstance().getLogger(); + + /** * This is the friendly identifier of the * component this class is running in. * eg, a Filter may be called "filter1", @@ -183,144 +188,30 @@ public class IRC__Alerter extends AlerterSkeleton { //---INNER CLASSES--- - /** - * This class provides some basic IRCBot functionality. It connects - * to a specified server, and will remain there until told to - * leave. Whilst connected it can send a message or a notice to - * the server. - */ - class IRCBot extends Thread { + class IRCBot extends PircBot implements Runnable { - public static final String DEFAULT_STARTUP_NOTICE = "i-scream ircbot starting..."; - /** - * Main thread loop, this part of the class listens for - * messages from the server, and acts accordingly. At the - * present moment it only responds to pings. + * The default reconnect delay in seconds */ + public final int DEFAULT_RECONNECT_DELAY = 30; + public void run() { - // so we can stop if required - boolean run = true; - while(run) { - // flag so we can stop the loop - boolean doRead = true; - // get the startup notice - String startupNotice; + while(true) { try { - startupNotice = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice"); - } catch (PropertyNotFoundException e) { - startupNotice = DEFAULT_STARTUP_NOTICE; - _logger.write(this.toString(), Logger.WARNING, "Configuration error: "+e); + init(); + break; } - // connect to the IRC server - try { - connect(); - sendNotice(startupNotice); - } catch(IOException e) { - doRead=false; - _logger.write(this.toString(), Logger.ERROR, "Error connecting: "+e); + catch (IOException e) { + _logger.write(this.toString(), Logger.ERROR, "Error initialising IRCBot: "+e); + reconnectSleep(); } - while(doRead) { - try { - // read a command - String cmd = _socketIn.readLine(); - // if we have a null, we've lost contact - if(cmd == null) { - throw new IOException("End of stream reached"); - } - // let another method deal with the input - handleInput(cmd); - } catch (IOException e) { - // comms failure, maybe our link is dead. - _logger.write(this.toString(), Logger.ERROR, "Communication error: "+e); - // stop, and loop round for a reconnect. - doRead = false; - } - } - // make sure we're disconnected - try { - disconnect(); - } catch (IOException e) { - _logger.write(this.toString(), Logger.ERROR, "Communication error: "+e); - } - - // comms have failed, so wait a while and reconnect - int delayTime = 0; - try { - delayTime = Integer.parseInt(ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.reconnectDelay")); - } catch (NumberFormatException e) { - delayTime = DEFAULT_RECONNECT_DELAY; - _logger.write(this.toString(), Logger.WARNING, "Erronous Alerter.IRC.reconnectDelay value in configuration using default of " + delayTime + " seconds"); - } catch (PropertyNotFoundException e) { - delayTime = DEFAULT_RECONNECT_DELAY; - _logger.write(this.toString(), Logger.WARNING, "Alerter.IRC.reconnectDelay value unavailable using default of " + delayTime + " seconds"); - } - try { - Thread.sleep(delayTime * 1000); - } catch (InterruptedException e) {} } - // maybe disconnect here ? - shutdown method not implemented yet - //disconnect(); + //System.out.println("falling out!"); } - /** - * Sends a message to the channel. - * - * @param msg The message to send - */ - public void sendMsg(String msg) { - _socketOut.println("PRIVMSG "+_channel+" :"+msg); - // wait a second before returning... - // this ensures messages can't be sent too fast - try {Thread.sleep(1000);} catch (InterruptedException e) {} - } - - /** - * Sends a message to the channel. - * - * @param user The user to send to - * @param msg The message to send - */ - public void sendPrivMsg(String user, String msg) { - _socketOut.println("PRIVMSG "+user+" :"+msg); - // wait a second before returning... - // this ensures messages can't be sent too fast - try {Thread.sleep(1000);} catch (InterruptedException e) {} - } - - /** - * Sends an action to the channel. - * - * @param msg the action message - */ - public void sendAction(String msg) { - char esc = 001; - sendMsg(esc+"ACTION "+msg+esc); - // wait a second before returning... - // this ensures messages can't be sent too fast - try {Thread.sleep(1000);} catch (InterruptedException e) {} - } - - /** - * Sends a notice to the channel. - * - * @param msg The notice to send - */ - public void sendNotice(String msg) { - _socketOut.println("NOTICE "+_channel+" :"+msg); - // wait a second before returning... - // this ensures messages can't be sent too fast - try {Thread.sleep(1000);} catch (InterruptedException e) {} - } - - /** - * Connect to the IRC server, log in, and join the channel. - * - * @throws IOException if the connection fails - */ - public void connect() throws IOException { + public void init() throws IOException { + _logger.write(this.toString(), Logger.DEBUG, "Initialising IRCBot..."); ConfigurationProxy cp = ConfigurationProxy.getInstance(); - // setup the socket, reader and writer String server; int port; try { @@ -333,11 +224,6 @@ public class IRC__Alerter extends AlerterSkeleton { _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); throw new IOException("Can't get irc server details due to malformed configuration"); } - _socket = new Socket(server, port); - _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); - _socketOut = new PrintWriter(_socket.getOutputStream(), true); - //_socketOut.println("PASS"); - // send USER details String user, comment; try { user = cp.getProperty(_name, "Alerter.IRC.user"); @@ -346,8 +232,9 @@ public class IRC__Alerter extends AlerterSkeleton { _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); throw new IOException("Can't get user details due to configuration error"); } - _socketOut.println("USER "+user+" 8 * :"+comment); - // attempt to get a nick + this.setLogin(user); + this.setVersion(comment); + this.setFinger(comment); // ? String nickList; try { nickList = cp.getProperty(_name, "Alerter.IRC.nickList"); @@ -357,280 +244,274 @@ public class IRC__Alerter extends AlerterSkeleton { } StringTokenizer st = new StringTokenizer(nickList, ";"); boolean ok = false; - // try until we exhaust our list while(!ok && st.hasMoreTokens()) { String nick = st.nextToken(); - _socketOut.println("NICK "+nick); - // get a "yes" or "no" response back - String response = ""; - do { - response = _socketIn.readLine(); - if(response==null) { - throw new IOException("Communication error whilst logging in"); - } - } while(response.indexOf("001")==-1 && response.indexOf("433")==-1); - // see if it was a yes - if(response.indexOf("001")!=-1) { - // great, we're logged in ! - ok = true; - // store the name we're using + try { + _logger.write(this.toString(), Logger.DEBUG, "Trying nick: "+nick); + this.setName(nick); + this.connect(server, port); + // must be ok if we get here _nickname = nick; + ok = true; } - else { - // log we couldn't get the name - _logger.write(this.toString(), Logger.WARNING, "Nickname in use: "+nick); + catch(IOException e) { + _logger.write(this.toString(), Logger.ERROR, "IO error when connecting to server: "+e); + throw new IOException("IO error when connecting to server"); } + catch(IrcException e) { + _logger.write(this.toString(), Logger.ERROR, "IRC error when connecting to server: "+e); + throw new IOException("IRC error when connecting to server"); + } + catch(NickAlreadyInUseException e) { + _logger.write(this.toString(), Logger.ERROR, "Nickname "+nick+" is already in use: "+e); + // just carry on around while loop + } } if(!ok) { // oh dear, we couldn't get on. - throw new IOException("All nicknames in use"); + _logger.write(this.toString(), Logger.ERROR, "All nicknames already in use"); + throw new IOException("All nicknames already in use"); } - // join the channel try { _channel = cp.getProperty(_name, "Alerter.IRC.channel"); + this.joinChannel(_channel); } catch (PropertyNotFoundException e) { _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); throw new IOException("Can't get channel name due to configuration error"); } - _socketOut.println("JOIN "+_channel); - // allow alerts + + String startupNotice; + try { + startupNotice = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice"); + sendNotice(_channel, startupNotice); + } catch (PropertyNotFoundException e) { + _logger.write(this.toString(), Logger.DEBUG, "Startup notice not defined, so not sending: "+e); + } + + // we should set this when all is ready! _active = true; + //System.out.println("leaving init"); } - /** - * Disconnect "nicely" from the IRC server. - * - * @throws IOException if the disconnection fails - */ - public void disconnect() throws IOException { - // stop alerts + public void sendMsg(String msg) { + sendMessage(_channel, msg); + } + + public void onDisconnect() { _active = false; - // send proper QUIT - _socketOut.println("QUIT : iscreamBot component shutting down..."); - // close the socket - _socketOut.close(); - _socketIn.close(); - _socket.close(); + while(true) { + reconnectSleep(); + try { + init(); + break; + } + catch (IOException e) { + _logger.write(this.toString(), Logger.ERROR, "Error initialising IRCBot: "+e); + } + } + //System.out.println("falling out of disconnect!"); } - /** - * Overrides the {@link java.lang.Object#toString() Object.toString()} - * method to provide clean logging (every class should have this). - * - * This uses the uk.org.iscream.cms.server.util.NameFormat class - * to format the toString() - * - * @return the name of this class and its CVS revision - */ - public String toString() { - return FormatName.getName( - _name, - getClass().getName(), - REVISION); + public void onMessage(String channel, String sender, String login, String hostname, String message) { + if(isForMe(message)) { + String response = handleInput(message); + if(response != null) { + sendMessage(channel, response); + } + } } - /** - * Deals with incoming lines from the server. - * - * @param line the line to deal with - */ - private void handleInput(String line) { + public void onPrivateMessage(String sender, String login, String hostname, String message) { + String response = handleInput(message); + if(response != null) { + sendMessage(sender, response); + } + } + + public void onNickChange(String oldNick, String login, String hostname, String newNick) { + if(oldNick.equals(_nickname)) { + _nickname = newNick; + } + } + + private String handleInput(String message) { + //return "yup, gotcha!"; ConfigurationProxy cp = ConfigurationProxy.getInstance(); - // if it's a PING... - if(line.startsWith("PING")) { - // ...send a PONG - _socketOut.println("PONG" + line.substring(4)); + // setup some String's + String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand; + String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand; + // get the command set + try { + stopCommand = cp.getProperty(_name, "Alerter.IRC.stopCommand"); + startCommand = cp.getProperty(_name, "Alerter.IRC.startCommand"); + timeSinceLastAlertCommand = cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"); + lastAlertCommand = cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"); + joinCommand = cp.getProperty(_name, "Alerter.IRC.joinCommand"); + nickChangeCommand = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"); + versionCommand = cp.getProperty(_name, "Alerter.IRC.versionCommand"); + helpCommand = cp.getProperty(_name, "Alerter.IRC.helpCommand"); + statCommand = cp.getProperty(_name, "Alerter.IRC.statCommand"); + uptimeCommand = cp.getProperty(_name, "Alerter.IRC.uptimeCommand"); + } catch (PropertyNotFoundException e) { + _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); + // lets bail from handling this line... + // ...it's gonna be hard without a command set! + return null; } - // see if it's for us - else if(getMsg(line).toLowerCase().startsWith(_nickname.toLowerCase()+",") || - getMsg(line).toLowerCase().startsWith(_nickname.toLowerCase()+":") || - getMsg(line).toLowerCase().startsWith(_nickname.toLowerCase()+" ")) { - // setup some String's - String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand; - String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand; - // get the command set - try { - stopCommand = cp.getProperty(_name, "Alerter.IRC.stopCommand"); - startCommand = cp.getProperty(_name, "Alerter.IRC.startCommand"); - timeSinceLastAlertCommand = cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"); - lastAlertCommand = cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"); - joinCommand = cp.getProperty(_name, "Alerter.IRC.joinCommand"); - nickChangeCommand = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"); - versionCommand = cp.getProperty(_name, "Alerter.IRC.versionCommand"); - helpCommand = cp.getProperty(_name, "Alerter.IRC.helpCommand"); - statCommand = cp.getProperty(_name, "Alerter.IRC.statCommand"); - uptimeCommand = cp.getProperty(_name, "Alerter.IRC.uptimeCommand"); - } catch (PropertyNotFoundException e) { - _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); - // lets bail from handling this line... - // ...it's gonna be hard without a command set! - return; + + if(message.indexOf(stopCommand)!=-1) { + _active = false; + return "alerts have been stopped"; + } + else if(message.indexOf(startCommand)!=-1) { + _active = true; + return "alerts have been activated"; + } + // this needs to go here if it contains the same words as the lastAlertCommand + else if(message.indexOf(timeSinceLastAlertCommand)!=-1) { + if(_lastAlertTime != -1) { + long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000; + String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); + return "I last sent an alert "+uptimeText+ " ago"; } - - // we have a message for us - String message = getMsg(line).substring(_nickname.length()); - if(message.indexOf(stopCommand)!=-1) { - _active = false; - sendMsg(getMsgSender(line)+", alerts have been stopped"); + else { + return "I've never sent an alert!"; } - else if(message.indexOf(startCommand)!=-1) { - _active = true; - sendMsg(getMsgSender(line)+", alerts have been activated"); + } + else if(message.indexOf(lastAlertCommand)!=-1) { + if(_lastAlertTime != -1) { + String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime)); + return "last alert was at "+date+"; "+_lastAlert; } - // this needs to go here if it contains the same words as the lastAlertCommand - else if(message.indexOf(timeSinceLastAlertCommand)!=-1) { - if(_lastAlertTime != -1) { - long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000; - String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); - sendMsg(getMsgSender(line)+", I last sent an alert "+uptimeText+ " ago"); - } - else { - sendMsg(getMsgSender(line)+", I've never sent an alert!"); - } + else { + return "I've never sent an alert!"; } - else if(message.indexOf(lastAlertCommand)!=-1) { - if(_lastAlertTime != -1) { - String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime)); - sendMsg(getMsgSender(line)+", last alert was at "+date+"; "+_lastAlert); - } - else { - sendMsg(getMsgSender(line)+", I've never sent an alert!"); - } - + + } + else if(message.indexOf(joinCommand)!=-1) { + String joinCmd = joinCommand; + String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1); + int endOfChan = newChan.indexOf(" "); + if(endOfChan == -1) { + endOfChan = newChan.length(); } - else if(message.indexOf(joinCommand)!=-1) { - String joinCmd = joinCommand; - String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1); - int endOfChan = newChan.indexOf(" "); - if(endOfChan == -1) { - endOfChan = newChan.length(); - } - newChan = newChan.substring(0, endOfChan); - if(newChan.equals(_channel)) { - sendMsg(getMsgSender(line)+", I'm already on "+newChan+"!"); - } else { - sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan); - _socketOut.println("PART "+_channel); - _socketOut.println("JOIN "+newChan); - _channel = newChan; - } + newChan = newChan.substring(0, endOfChan); + if(newChan.equals(_channel)) { + return "I'm already on "+newChan+"!"; + } else { + partChannel(_channel); + joinChannel(newChan); + _channel = newChan; + return null; // ??? } - else if(message.indexOf(nickChangeCommand)!=-1) { - String nickChangeCmd = nickChangeCommand; - String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1); - int endOfNick = newNick.indexOf(" "); - if(endOfNick == -1) { - endOfNick = newNick.length(); - } - newNick = newNick.substring(0, endOfNick); - sendMsg(getMsgSender(line)+", okay, changing my nickname to "+newNick); - _socketOut.println("NICK "+newNick); - _nickname = newNick; + } + else if(message.indexOf(nickChangeCommand)!=-1) { + String nickChangeCmd = nickChangeCommand; + String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1); + int endOfNick = newNick.indexOf(" "); + if(endOfNick == -1) { + endOfNick = newNick.length(); } - else if(message.indexOf(versionCommand)!=-1) { - sendMsg(getMsgSender(line)+", I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot"); - } - else if(message.indexOf(helpCommand)!=-1) { - sendPrivMsg(getMsgSender(line), "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2)); - sendPrivMsg(getMsgSender(line), "I understand the following commands;"); - sendPrivMsg(getMsgSender(line), stopCommand); - sendPrivMsg(getMsgSender(line), startCommand); - sendPrivMsg(getMsgSender(line), lastAlertCommand); - sendPrivMsg(getMsgSender(line), joinCommand); - sendPrivMsg(getMsgSender(line), nickChangeCommand); - sendPrivMsg(getMsgSender(line), statCommand); - sendPrivMsg(getMsgSender(line), uptimeCommand); - sendPrivMsg(getMsgSender(line), timeSinceLastAlertCommand); - sendPrivMsg(getMsgSender(line), helpCommand); - } - else if(message.indexOf(statCommand)!=-1) { - sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"); - } - else if(message.indexOf(uptimeCommand)!=-1) { - long uptime = (System.currentTimeMillis() - _startTime) / 1000; - String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); - sendMsg(getMsgSender(line)+", I have been running for "+uptimeText); - } - else if(message.indexOf("ping")!=-1) { - sendMsg("pong"); - } - else if(message.indexOf("do a jibble dance")!=-1) { - // little joke :) - sendAction("jives to the funky beat shouting \"ii--screeeaaammm\""); - } - else { - String rejectMessage = NOT_CONFIGURED; - try { - rejectMessage = cp.getProperty(_name, "Alerter.IRC.rejectMessage"); - } catch(PropertyNotFoundException e) { - _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); - } - sendMsg(getMsgSender(line)+", "+rejectMessage); - } + newNick = newNick.substring(0, endOfNick); + changeNick(newNick); + // should we check this worked? + //_nickname = newNick; + return null; // ??? } - else if(line.indexOf(_nickname)!=-1 && line.indexOf(_channel)!=-1 && line.indexOf("KICK")!=-1) { - sendPrivMsg(getMsgSender(line), "That wasn't a nice thing to do..."); + else if(message.indexOf(versionCommand)!=-1) { + return "I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot"; + } + else if(message.indexOf(helpCommand)!=-1) { + return "this will return some help text soon"; + /* + sendPrivMsg(getMsgSender(line), "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2)); + sendPrivMsg(getMsgSender(line), "I understand the following commands;"); + sendPrivMsg(getMsgSender(line), stopCommand); + sendPrivMsg(getMsgSender(line), startCommand); + sendPrivMsg(getMsgSender(line), lastAlertCommand); + sendPrivMsg(getMsgSender(line), joinCommand); + sendPrivMsg(getMsgSender(line), nickChangeCommand); + sendPrivMsg(getMsgSender(line), statCommand); + sendPrivMsg(getMsgSender(line), uptimeCommand); + sendPrivMsg(getMsgSender(line), timeSinceLastAlertCommand); + sendPrivMsg(getMsgSender(line), helpCommand); + */ + } + else if(message.indexOf(statCommand)!=-1) { + return "I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"; + } + else if(message.indexOf(uptimeCommand)!=-1) { + long uptime = (System.currentTimeMillis() - _startTime) / 1000; + String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); + return "I have been running for "+uptimeText; + } + else if(message.indexOf("ping")!=-1) { + return "pong"; + } + else if(message.indexOf("do a jibble dance")!=-1) { + // little joke :) + return "jives to the funky beat shouting \"ii--screeeaaammm\""; + } + + else { + String rejectMessage = NOT_CONFIGURED; try { - _channel = cp.getProperty(_name, "Alerter.IRC.channel"); + rejectMessage = cp.getProperty(_name, "Alerter.IRC.rejectMessage"); } catch(PropertyNotFoundException e) { _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); } - _socketOut.println("JOIN "+_channel); + return rejectMessage; } } - /** - * Strips the header from a message line - * - * @param line the line to strip - * @return the message from the line - */ - private String getMsg(String line) { - String result = ""; - if(line.indexOf("PRIVMSG")!=-1) { - int firstColon = line.indexOf(":"); - if(firstColon != -1) { - int secondColon = line.indexOf(":", firstColon+1); - if(secondColon != -1) { - result = line.substring(secondColon+1); - } - } + private boolean isForMe(String message) { + // change this! + String nick = _nickname.toLowerCase(); + String msg = message.toLowerCase(); + if(msg.startsWith(nick + ", ") || + msg.startsWith(nick + ": ") || + msg.startsWith(nick + " ")) { + return true; } - return result; + else { + return false; + } } - /** - * Finds out the sender of the message - * - * @param line the line to look for a sender in - * @return the sender - */ - private String getMsgSender(String line) { - String result = ""; - int colon = line.indexOf(":"); - int excl = line.indexOf("!"); - if(colon!=-1 && excl!=-1) { - result = line.substring(colon+1, excl); + private void reconnectSleep() { + int delayTime = 0; + try { + delayTime = Integer.parseInt(ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.reconnectDelay")); + } catch (NumberFormatException e) { + delayTime = DEFAULT_RECONNECT_DELAY; + _logger.write(this.toString(), Logger.WARNING, "Erronous Alerter.IRC.reconnectDelay value in configuration using default of " + delayTime + " seconds"); + } catch (PropertyNotFoundException e) { + delayTime = DEFAULT_RECONNECT_DELAY; + _logger.write(this.toString(), Logger.WARNING, "Alerter.IRC.reconnectDelay value unavailable using default of " + delayTime + " seconds"); } - return result; + _logger.write(this.toString(), Logger.ERROR, "Waiting "+delayTime+" seconds for reconnect..."); + try { + Thread.sleep(delayTime * 1000); + } catch (InterruptedException e) {} } /** - * The socket connected to the server + * Overrides the {@link java.lang.Object#toString() Object.toString()} + * method to provide clean logging (every class should have this). + * + * This uses the uk.org.iscream.cms.server.util.NameFormat class + * to format the toString() + * + * @return the name of this class and its CVS revision */ - private Socket _socket; + public String toString() { + return FormatName.getName( + _name, + getClass().getName(), + REVISION); + } /** - * The writer - */ - private PrintWriter _socketOut; - - /** - * The reader - */ - private BufferedReader _socketIn; - - /** * Just a reminder to what channel we're on... * this can't be dynamic :) */ @@ -640,12 +521,6 @@ public class IRC__Alerter extends AlerterSkeleton { * A reminder of our current nickname... */ private String _nickname; - - /** - * This holds a reference to the - * system logger that is being used. - */ - protected Logger _logger = ReferenceManager.getInstance().getLogger(); }