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.15 by ajm, Mon Mar 5 12:45:51 2001 UTC vs.
Revision 1.16 by tdb, Mon Mar 5 23:12:43 2001 UTC

# Line 40 | 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 60 | 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() == 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 186 | 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 197 | 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 236 | 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 306 | 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 349 | 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 400 | 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+",")
459 <                    || getMsg(line).startsWith(_nickname+":")
460 <                    || 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                  // this needs to go here if it contains the same words as the lastAlertCommand
492 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"))!=-1) {
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");
# Line 424 | Line 499 | public class IRC__Alerter implements PluginAlerter {
499                          sendMsg(getMsgSender(line)+", I've never sent an alert!");
500                      }
501                  }
502 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"))!=-1) {
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);
# Line 434 | Line 509 | public class IRC__Alerter implements PluginAlerter {
509                      }
510                      
511                  }
512 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.joinCommand"))!=-1) {
513 <                    String joinCmd = cp.getProperty(_name, "Alerter.IRC.joinCommand");
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 447 | 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.nickChangeCommand"))!=-1) {
526 <                    String nickChangeCmd = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand");
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) {
# Line 459 | Line 534 | public class IRC__Alerter implements PluginAlerter {
534                      _socketOut.println("NICK "+newNick);
535                      _nickname = newNick;
536                  }
537 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.versionCommand"))!=-1) {
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(cp.getProperty(_name, "Alerter.IRC.helpCommand"))!=-1) {
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"));
546 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.joinCommand"));
547 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"));
548 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.statCommand"));
549 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.uptimeCommand"));
550 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"));
551 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.helpCommand"));
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(cp.getProperty(_name, "Alerter.IRC.statCommand"))!=-1) {
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(cp.getProperty(_name, "Alerter.IRC.uptimeCommand"))!=-1) {
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);
# Line 488 | Line 563 | public class IRC__Alerter implements PluginAlerter {
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