ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java (file contents):
Revision 1.10 by ajm, Sun Mar 4 04:43:29 2001 UTC vs.
Revision 1.16 by tdb, Mon Mar 5 23:12:43 2001 UTC

# Line 10 | Line 10 | import uk.ac.ukc.iscream.componentmanager.*;
10   import java.io.*;
11   import java.net.*;
12   import java.util.*;
13 + import java.text.*;
14  
15   /**
16   * This Alert sends an IRC message.
# Line 39 | Line 40 | public class IRC__Alerter implements PluginAlerter {
40       */
41      public final int DEFAULT_RECONNECT_DELAY = 30;
42      
43 +    public final String DEFAULT_LEVEL = Alert.alertLevels[0];
44 +    
45 +    public final String NOT_CONFIGURED = "<NOT CONFIGURED>";
46 +    
47   //---STATIC METHODS---
48  
49   //---CONSTRUCTORS---
# Line 48 | Line 53 | public class IRC__Alerter implements PluginAlerter {
53          // connect to the IRC server
54          _ircbot = new IRCBot();
55          _ircbot.start();
56 +        _startTime = System.currentTimeMillis();
57          
58          _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
59      }
# Line 58 | Line 64 | public class IRC__Alerter implements PluginAlerter {
64          // only send alerts if we're active
65          if(_active) {
66              ConfigurationProxy cp = ConfigurationProxy.getInstance();
67 <            String levelName = cp.getProperty(_name, "Alerter.IRC.level");
67 >            
68 >            String levelName;
69 >            try {
70 >                levelName = cp.getProperty(_name, "Alerter.IRC.level");
71 >            } catch (PropertyNotFoundException e) {
72 >                levelName = DEFAULT_LEVEL;
73 >                _logger.write(toString(), Logger.WARNING, "Alerter.IRC.level value unavailable using default of " + levelName);
74 >            }
75              int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
76              // only send if it's equal (or above) our level
77 <            if(alert.getLevel() >= level) {
77 >            if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
78                  String alertType = Alert.alertLevels[alert.getLevel()];
79                  String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
80 <                // sort out the message
81 <                String message = cp.getProperty(_name, "Alerter.IRC.message");
80 >                // sort out the message              
81 >                String message;
82 >                try {
83 >                    message = cp.getProperty(_name, "Alerter.IRC.message");
84 >                } catch (PropertyNotFoundException e) {
85 >                    message = NOT_CONFIGURED;
86 >                    _logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message);
87 >                }
88 >                
89                  message = StringUtils.replaceText(message, "%level%", alertType);
90                  message = StringUtils.replaceText(message, "%threshold%", thresholdType);
91                  message = StringUtils.replaceText(message, "%source%", alert.getSource());
# Line 78 | Line 98 | public class IRC__Alerter implements PluginAlerter {
98                  _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
99                  _ircbot.sendMsg(message);
100                  _lastAlert = message;
101 +                _lastAlertTime = System.currentTimeMillis();
102 +                _alertCount ++;
103              }
104          }
105      }
# Line 139 | Line 161 | public class IRC__Alerter implements PluginAlerter {
161      private String _lastAlert = "no alerts have been sent";
162      
163      /**
164 +     * The time of the last alert
165 +     */
166 +    private long _lastAlertTime = -1;
167 +    
168 +    /**
169 +     * Number of alerts sent
170 +     */
171 +    private long _alertCount = 0;
172 +    
173 +    /**
174 +     * Time of IRCBot startup
175 +     */
176 +    private long _startTime;
177 +    
178 +    /**
179       * This is the friendly identifier of the
180       * component this class is running in.
181       * eg, a Filter may be called "filter1",
# Line 167 | Line 204 | public class IRC__Alerter implements PluginAlerter {
204       */
205      class IRCBot extends Thread {
206          
207 +        public static final String DEFAULT_STARTUP_NOTICE = "i-scream ircbot starting...";
208 +        
209          /**
210           * Main thread loop, this part of the class listens for
211           * messages from the server, and acts accordingly. At the
# Line 178 | Line 217 | public class IRC__Alerter implements PluginAlerter {
217              while(run) {
218                  // flag so we can stop the loop
219                  boolean doRead = true;
220 +                // get the startup notice
221 +                String startupNotice;
222 +                try {
223 +                    startupNotice = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice");
224 +                } catch (PropertyNotFoundException e) {
225 +                    startupNotice = DEFAULT_STARTUP_NOTICE;
226 +                    _logger.write(this.toString(), Logger.WARNING, "Configuration error: "+e);
227 +                }
228                  // connect to the IRC server
229                  try {
230                      connect();
231 <                    sendNotice(ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice"));
231 >                    sendNotice(startupNotice);
232                  } catch(IOException e) {
233                      doRead=false;
234                      _logger.write(this.toString(), Logger.ERROR, "Error connecting: "+e);
# Line 217 | Line 264 | public class IRC__Alerter implements PluginAlerter {
264                  } catch (NumberFormatException e) {
265                      delayTime = DEFAULT_RECONNECT_DELAY;
266                      _logger.write(this.toString(), Logger.WARNING, "Erronous Alerter.IRC.reconnectDelay value in configuration using default of " + delayTime + " seconds");
267 <                } catch (org.omg.CORBA.MARSHAL e2) {
267 >                } catch (PropertyNotFoundException e) {
268                      delayTime = DEFAULT_RECONNECT_DELAY;
269                      _logger.write(this.toString(), Logger.WARNING, "Alerter.IRC.reconnectDelay value unavailable using default of " + delayTime + " seconds");
270                  }
# Line 236 | Line 283 | public class IRC__Alerter implements PluginAlerter {
283           */
284          public void sendMsg(String msg) {
285              _socketOut.println("PRIVMSG "+_channel+" :"+msg);
286 +            // wait a second before returning...
287 +            // this ensures messages can't be sent too fast
288 +            try {Thread.sleep(1000);} catch (InterruptedException e) {}
289          }
290          
291          /**
# Line 246 | Line 296 | public class IRC__Alerter implements PluginAlerter {
296           */
297          public void sendPrivMsg(String user, String msg) {
298              _socketOut.println("PRIVMSG "+user+" :"+msg);
299 +            // wait a second before returning...
300 +            // this ensures messages can't be sent too fast
301 +            try {Thread.sleep(1000);} catch (InterruptedException e) {}
302          }
303          
304          /**
# Line 256 | Line 309 | public class IRC__Alerter implements PluginAlerter {
309          public void sendAction(String msg) {
310              char esc = 001;
311              sendMsg(esc+"ACTION "+msg+esc);
312 +            // wait a second before returning...
313 +            // this ensures messages can't be sent too fast
314 +            try {Thread.sleep(1000);} catch (InterruptedException e) {}
315          }
316          
317          /**
# Line 265 | Line 321 | public class IRC__Alerter implements PluginAlerter {
321           */
322          public void sendNotice(String msg) {
323              _socketOut.println("NOTICE "+_channel+" :"+msg);
324 +            // wait a second before returning...
325 +            // this ensures messages can't be sent too fast
326 +            try {Thread.sleep(1000);} catch (InterruptedException e) {}
327          }
328          
329          /**
# Line 275 | Line 334 | public class IRC__Alerter implements PluginAlerter {
334          public void connect() throws IOException {
335              ConfigurationProxy cp = ConfigurationProxy.getInstance();
336              // setup the socket, reader and writer
337 <            String server = cp.getProperty(_name, "Alerter.IRC.IRCServer");
338 <            int port = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.IRCPort"));
337 >            String server;
338 >            int port;
339 >            try {
340 >                server = cp.getProperty(_name, "Alerter.IRC.IRCServer");
341 >                port = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.IRCPort"));
342 >            } catch (PropertyNotFoundException e) {
343 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
344 >                throw new IOException("Can't get irc server details due to configuration error");
345 >            } catch (NumberFormatException e) {
346 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
347 >                throw new IOException("Can't get irc server details due to malformed configuration");
348 >            }
349              _socket = new Socket(server, port);
350              _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
351              _socketOut = new PrintWriter(_socket.getOutputStream(), true);
352              //_socketOut.println("PASS");
353              // send USER details
354 <            String user = cp.getProperty(_name, "Alerter.IRC.user");
355 <            String comment = cp.getProperty(_name, "Alerter.IRC.comment");
354 >            String user, comment;
355 >            try {
356 >                user = cp.getProperty(_name, "Alerter.IRC.user");
357 >                comment = cp.getProperty(_name, "Alerter.IRC.comment");
358 >            } catch (PropertyNotFoundException e) {
359 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
360 >                throw new IOException("Can't get user details due to configuration error");
361 >            }
362              _socketOut.println("USER "+user+" 8 * :"+comment);
363              // attempt to get a nick
364 <            String nickList = cp.getProperty(_name, "Alerter.IRC.nickList");
364 >            String nickList;
365 >            try {
366 >                nickList = cp.getProperty(_name, "Alerter.IRC.nickList");
367 >            } catch (PropertyNotFoundException e) {
368 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
369 >                throw new IOException("Can't get nickname due to configuration error");
370 >            }
371              StringTokenizer st = new StringTokenizer(nickList, ";");
372              boolean ok = false;
373              // try until we exhaust our list
# Line 318 | Line 399 | public class IRC__Alerter implements PluginAlerter {
399                  throw new IOException("All nicknames in use");
400              }
401              // join the channel
402 <            _channel = cp.getProperty(_name, "Alerter.IRC.channel");
402 >            try {
403 >                _channel = cp.getProperty(_name, "Alerter.IRC.channel");
404 >            } catch (PropertyNotFoundException e) {
405 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
406 >                throw new IOException("Can't get channel name due to configuration error");
407 >            }
408              _socketOut.println("JOIN "+_channel);
409              // allow alerts
410              _active = true;
# Line 369 | Line 455 | public class IRC__Alerter implements PluginAlerter {
455                  _socketOut.println("PONG" + line.substring(4));
456              }
457              // see if it's for us
458 <            else if(getMsg(line).startsWith(_nickname)) {
458 >            else if(getMsg(line).startsWith(_nickname+",") || getMsg(line).startsWith(_nickname+":") || getMsg(line).startsWith(_nickname+" ")) {
459 >                // setup some String's
460 >                String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand;
461 >                String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand;
462 >                // get the command set
463 >                try {
464 >                    stopCommand = cp.getProperty(_name, "Alerter.IRC.stopCommand");
465 >                    startCommand = cp.getProperty(_name, "Alerter.IRC.startCommand");
466 >                    timeSinceLastAlertCommand = cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand");
467 >                    lastAlertCommand = cp.getProperty(_name, "Alerter.IRC.lastAlertCommand");
468 >                    joinCommand = cp.getProperty(_name, "Alerter.IRC.joinCommand");
469 >                    nickChangeCommand = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand");
470 >                    versionCommand = cp.getProperty(_name, "Alerter.IRC.versionCommand");
471 >                    helpCommand = cp.getProperty(_name, "Alerter.IRC.helpCommand");
472 >                    statCommand = cp.getProperty(_name, "Alerter.IRC.statCommand");
473 >                    uptimeCommand = cp.getProperty(_name, "Alerter.IRC.uptimeCommand");
474 >                } catch (PropertyNotFoundException e) {
475 >                    _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
476 >                    // lets bail from handling this line...
477 >                    // ...it's gonna be hard without a command set!
478 >                    return;
479 >                }
480 >                
481                  // we have a message for us
482                  String message = getMsg(line).substring(_nickname.length());
483 <                if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.stopCommand"))!=-1) {
483 >                if(message.indexOf(stopCommand)!=-1) {
484                      _active = false;
485                      sendMsg(getMsgSender(line)+", alerts have been stopped");
486                  }
487 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.startCommand"))!=-1) {
487 >                else if(message.indexOf(startCommand)!=-1) {
488                      _active = true;
489                      sendMsg(getMsgSender(line)+", alerts have been activated");
490                  }
491 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"))!=-1) {
492 <                    sendMsg(getMsgSender(line)+", last alert was: "+_lastAlert);
491 >                // this needs to go here if it contains the same words as the lastAlertCommand
492 >                else if(message.indexOf(timeSinceLastAlertCommand)!=-1) {
493 >                    if(_lastAlertTime != -1) {
494 >                        long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000;
495 >                        String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
496 >                        sendMsg(getMsgSender(line)+", I last sent an alert "+uptimeText+ " ago");
497 >                    }
498 >                    else {
499 >                        sendMsg(getMsgSender(line)+", I've never sent an alert!");
500 >                    }
501                  }
502 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.joinCommand"))!=-1) {
503 <                    String joinCmd = cp.getProperty(_name, "Alerter.IRC.joinCommand");
502 >                else if(message.indexOf(lastAlertCommand)!=-1) {
503 >                    if(_lastAlertTime != -1) {
504 >                        String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime));
505 >                        sendMsg(getMsgSender(line)+", last alert was at "+date+"; "+_lastAlert);
506 >                    }
507 >                    else {
508 >                        sendMsg(getMsgSender(line)+", I've never sent an alert!");
509 >                    }
510 >                    
511 >                }
512 >                else if(message.indexOf(joinCommand)!=-1) {
513 >                    String joinCmd = joinCommand;
514                      String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1);
515                      int endOfChan = newChan.indexOf(" ");
516                      if(endOfChan == -1) {
# Line 396 | Line 522 | public class IRC__Alerter implements PluginAlerter {
522                      _socketOut.println("JOIN "+newChan);
523                      _channel = newChan;
524                  }
525 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.helpCommand"))!=-1) {
526 <                    sendPrivMsg(getMsgSender(line), "I am the i-scream alerting bot revision "+REVISION.substring(11, REVISION.length() -2));
525 >                else if(message.indexOf(nickChangeCommand)!=-1) {
526 >                    String nickChangeCmd = nickChangeCommand;
527 >                    String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1);
528 >                    int endOfNick = newNick.indexOf(" ");
529 >                    if(endOfNick == -1) {
530 >                        endOfNick = newNick.length();
531 >                    }
532 >                    newNick = newNick.substring(0, endOfNick);
533 >                    sendMsg(getMsgSender(line)+", okay, changing my nickname to "+newNick);
534 >                    _socketOut.println("NICK "+newNick);
535 >                    _nickname = newNick;
536 >                }
537 >                else if(message.indexOf(versionCommand)!=-1) {
538 >                    sendMsg(getMsgSender(line)+", I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot");
539 >                }
540 >                else if(message.indexOf(helpCommand)!=-1) {
541 >                    sendPrivMsg(getMsgSender(line), "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2));
542                      sendPrivMsg(getMsgSender(line), "I understand the following commands;");
543 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.stopCommand"));
544 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.startCommand"));
545 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"));
543 >                    sendPrivMsg(getMsgSender(line), stopCommand);
544 >                    sendPrivMsg(getMsgSender(line), startCommand);
545 >                    sendPrivMsg(getMsgSender(line), lastAlertCommand);
546 >                    sendPrivMsg(getMsgSender(line), joinCommand);
547 >                    sendPrivMsg(getMsgSender(line), nickChangeCommand);
548 >                    sendPrivMsg(getMsgSender(line), statCommand);
549 >                    sendPrivMsg(getMsgSender(line), uptimeCommand);
550 >                    sendPrivMsg(getMsgSender(line), timeSinceLastAlertCommand);
551 >                    sendPrivMsg(getMsgSender(line), helpCommand);
552                  }
553 +                else if(message.indexOf(statCommand)!=-1) {
554 +                    sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts!");
555 +                }
556 +                else if(message.indexOf(uptimeCommand)!=-1) {
557 +                    long uptime = (System.currentTimeMillis() - _startTime) / 1000;
558 +                    String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
559 +                    sendMsg(getMsgSender(line)+", I have been running for "+uptimeText);
560 +                }
561                  else if(message.indexOf("do a jibble dance")!=-1) {
562                      // little joke :)
563                      sendAction("jives to the funky beat shouting \"ii--screeeaaammm\"");
564                  }
565                  else {
566 <                    sendMsg(getMsgSender(line)+", "+cp.getProperty(_name, "Alerter.IRC.rejectMessage"));
566 >                    String rejectMessage = NOT_CONFIGURED;
567 >                    try {
568 >                        rejectMessage = cp.getProperty(_name, "Alerter.IRC.rejectMessage");
569 >                    } catch(PropertyNotFoundException e) {
570 >                        _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
571 >                    }
572 >                    sendMsg(getMsgSender(line)+", "+rejectMessage);
573                  }
574              }
575              else if(line.indexOf(_nickname)!=-1 && line.indexOf(_channel)!=-1 && line.indexOf("KICK")!=-1) {
576                  sendPrivMsg(getMsgSender(line), "That wasn't a nice thing to do...");
577 <                _channel = cp.getProperty(_name, "Alerter.IRC.channel");
577 >                try {
578 >                    _channel = cp.getProperty(_name, "Alerter.IRC.channel");
579 >                } catch(PropertyNotFoundException e) {
580 >                    _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
581 >                }
582                  _socketOut.println("JOIN "+_channel);
583              }
584          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines