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.20 by tdb, Wed Mar 14 23:25:29 2001 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.client.alerters;
2 > package uk.org.iscream.client.alerters;
3  
4   //---IMPORTS---
5 < import uk.ac.ukc.iscream.client.*;
6 < import uk.ac.ukc.iscream.core.*;
7 < import uk.ac.ukc.iscream.util.*;
8 < import uk.ac.ukc.iscream.componentmanager.*;
5 > import uk.org.iscream.client.*;
6 > import uk.org.iscream.core.*;
7 > import uk.org.iscream.util.*;
8 > import uk.org.iscream.componentmanager.*;
9  
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 47 | Line 52 | public class IRC__Alerter implements PluginAlerter {
52                          
53          // connect to the IRC server
54          _ircbot = new IRCBot();
55 +        // set it's name and start it
56 +        _ircbot.setName("client.IRC__Alerter$IRCBot");
57          _ircbot.start();
58 +        _startTime = System.currentTimeMillis();
59          
60          _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
61      }
# Line 58 | Line 66 | public class IRC__Alerter implements PluginAlerter {
66          // only send alerts if we're active
67          if(_active) {
68              ConfigurationProxy cp = ConfigurationProxy.getInstance();
69 <            String levelName = cp.getProperty(_name, "Alerter.IRC.level");
69 >            
70 >            String levelName;
71 >            try {
72 >                levelName = cp.getProperty(_name, "Alerter.IRC.level");
73 >            } catch (PropertyNotFoundException e) {
74 >                levelName = DEFAULT_LEVEL;
75 >                _logger.write(toString(), Logger.WARNING, "Alerter.IRC.level value unavailable using default of " + levelName);
76 >            }
77              int level = StringUtils.getStringPos(levelName, Alert.alertLevels);
78              // only send if it's equal (or above) our level
79 <            if(alert.getLevel() >= level) {
79 >            if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) {
80                  String alertType = Alert.alertLevels[alert.getLevel()];
81                  String thresholdType = Alert.thresholdLevels[alert.getThreshold()];
82 <                // sort out the message
83 <                String message = cp.getProperty(_name, "Alerter.IRC.message");
82 >                String timeFirstSince = DateUtils.formatTime(System.currentTimeMillis() - alert.getInitialAlertTime(), "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
83 >                String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime()));
84 >                // sort out the message              
85 >                String message;
86 >                try {
87 >                    message = cp.getProperty(_name, "Alerter.IRC.message");
88 >                } catch (PropertyNotFoundException e) {
89 >                    message = NOT_CONFIGURED;
90 >                    _logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message);
91 >                }
92 >                
93                  message = StringUtils.replaceText(message, "%level%", alertType);
94                  message = StringUtils.replaceText(message, "%threshold%", thresholdType);
95                  message = StringUtils.replaceText(message, "%source%", alert.getSource());
# Line 73 | Line 97 | public class IRC__Alerter implements PluginAlerter {
97                  message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
98                  message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
99                  message = StringUtils.replaceText(message, "%timeTillNextAlert%",  getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
100 +                message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince);
101 +                message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured);
102                  
103                  // send the message
104                  _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
105                  _ircbot.sendMsg(message);
106                  _lastAlert = message;
107 +                _lastAlertTime = System.currentTimeMillis();
108 +                _alertCount ++;
109              }
110          }
111 +        else {
112 +            _ignoredCount ++;
113 +        }
114      }
115  
116      /**
117       * Overrides the {@link java.lang.Object#toString() Object.toString()}
118       * method to provide clean logging (every class should have this).
119       *
120 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
120 >     * This uses the uk.org.iscream.util.NameFormat class
121       * to format the toString()
122       *
123       * @return the name of this class and its CVS revision
# Line 139 | Line 170 | public class IRC__Alerter implements PluginAlerter {
170      private String _lastAlert = "no alerts have been sent";
171      
172      /**
173 +     * The time of the last alert
174 +     */
175 +    private long _lastAlertTime = -1;
176 +    
177 +    /**
178 +     * Number of alerts sent
179 +     */
180 +    private int _alertCount = 0;
181 +    
182 +    /**
183 +     * Number of alerts ignored when in "stopped" mode
184 +     */
185 +    private int _ignoredCount = 0;
186 +    
187 +    /**
188 +     * Time of IRCBot startup
189 +     */
190 +    private long _startTime;
191 +    
192 +    /**
193       * This is the friendly identifier of the
194       * component this class is running in.
195       * eg, a Filter may be called "filter1",
# Line 167 | Line 218 | public class IRC__Alerter implements PluginAlerter {
218       */
219      class IRCBot extends Thread {
220          
221 +        public static final String DEFAULT_STARTUP_NOTICE = "i-scream ircbot starting...";
222 +        
223          /**
224           * Main thread loop, this part of the class listens for
225           * messages from the server, and acts accordingly. At the
# Line 178 | Line 231 | public class IRC__Alerter implements PluginAlerter {
231              while(run) {
232                  // flag so we can stop the loop
233                  boolean doRead = true;
234 +                // get the startup notice
235 +                String startupNotice;
236 +                try {
237 +                    startupNotice = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice");
238 +                } catch (PropertyNotFoundException e) {
239 +                    startupNotice = DEFAULT_STARTUP_NOTICE;
240 +                    _logger.write(this.toString(), Logger.WARNING, "Configuration error: "+e);
241 +                }
242                  // connect to the IRC server
243                  try {
244                      connect();
245 <                    sendNotice(ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice"));
245 >                    sendNotice(startupNotice);
246                  } catch(IOException e) {
247                      doRead=false;
248                      _logger.write(this.toString(), Logger.ERROR, "Error connecting: "+e);
# Line 217 | Line 278 | public class IRC__Alerter implements PluginAlerter {
278                  } catch (NumberFormatException e) {
279                      delayTime = DEFAULT_RECONNECT_DELAY;
280                      _logger.write(this.toString(), Logger.WARNING, "Erronous Alerter.IRC.reconnectDelay value in configuration using default of " + delayTime + " seconds");
281 <                } catch (org.omg.CORBA.MARSHAL e2) {
281 >                } catch (PropertyNotFoundException e) {
282                      delayTime = DEFAULT_RECONNECT_DELAY;
283                      _logger.write(this.toString(), Logger.WARNING, "Alerter.IRC.reconnectDelay value unavailable using default of " + delayTime + " seconds");
284                  }
# Line 236 | Line 297 | public class IRC__Alerter implements PluginAlerter {
297           */
298          public void sendMsg(String msg) {
299              _socketOut.println("PRIVMSG "+_channel+" :"+msg);
300 +            // wait a second before returning...
301 +            // this ensures messages can't be sent too fast
302 +            try {Thread.sleep(1000);} catch (InterruptedException e) {}
303          }
304          
305          /**
# Line 246 | Line 310 | public class IRC__Alerter implements PluginAlerter {
310           */
311          public void sendPrivMsg(String user, String msg) {
312              _socketOut.println("PRIVMSG "+user+" :"+msg);
313 +            // wait a second before returning...
314 +            // this ensures messages can't be sent too fast
315 +            try {Thread.sleep(1000);} catch (InterruptedException e) {}
316          }
317          
318          /**
# Line 256 | Line 323 | public class IRC__Alerter implements PluginAlerter {
323          public void sendAction(String msg) {
324              char esc = 001;
325              sendMsg(esc+"ACTION "+msg+esc);
326 +            // wait a second before returning...
327 +            // this ensures messages can't be sent too fast
328 +            try {Thread.sleep(1000);} catch (InterruptedException e) {}
329          }
330          
331          /**
# Line 265 | Line 335 | public class IRC__Alerter implements PluginAlerter {
335           */
336          public void sendNotice(String msg) {
337              _socketOut.println("NOTICE "+_channel+" :"+msg);
338 +            // wait a second before returning...
339 +            // this ensures messages can't be sent too fast
340 +            try {Thread.sleep(1000);} catch (InterruptedException e) {}
341          }
342          
343          /**
# Line 275 | Line 348 | public class IRC__Alerter implements PluginAlerter {
348          public void connect() throws IOException {
349              ConfigurationProxy cp = ConfigurationProxy.getInstance();
350              // setup the socket, reader and writer
351 <            String server = cp.getProperty(_name, "Alerter.IRC.IRCServer");
352 <            int port = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.IRCPort"));
351 >            String server;
352 >            int port;
353 >            try {
354 >                server = cp.getProperty(_name, "Alerter.IRC.IRCServer");
355 >                port = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.IRCPort"));
356 >            } catch (PropertyNotFoundException e) {
357 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
358 >                throw new IOException("Can't get irc server details due to configuration error");
359 >            } catch (NumberFormatException e) {
360 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
361 >                throw new IOException("Can't get irc server details due to malformed configuration");
362 >            }
363              _socket = new Socket(server, port);
364              _socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
365              _socketOut = new PrintWriter(_socket.getOutputStream(), true);
366              //_socketOut.println("PASS");
367              // send USER details
368 <            String user = cp.getProperty(_name, "Alerter.IRC.user");
369 <            String comment = cp.getProperty(_name, "Alerter.IRC.comment");
368 >            String user, comment;
369 >            try {
370 >                user = cp.getProperty(_name, "Alerter.IRC.user");
371 >                comment = cp.getProperty(_name, "Alerter.IRC.comment");
372 >            } catch (PropertyNotFoundException e) {
373 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
374 >                throw new IOException("Can't get user details due to configuration error");
375 >            }
376              _socketOut.println("USER "+user+" 8 * :"+comment);
377              // attempt to get a nick
378 <            String nickList = cp.getProperty(_name, "Alerter.IRC.nickList");
378 >            String nickList;
379 >            try {
380 >                nickList = cp.getProperty(_name, "Alerter.IRC.nickList");
381 >            } catch (PropertyNotFoundException e) {
382 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
383 >                throw new IOException("Can't get nickname due to configuration error");
384 >            }
385              StringTokenizer st = new StringTokenizer(nickList, ";");
386              boolean ok = false;
387              // try until we exhaust our list
# Line 318 | Line 413 | public class IRC__Alerter implements PluginAlerter {
413                  throw new IOException("All nicknames in use");
414              }
415              // join the channel
416 <            _channel = cp.getProperty(_name, "Alerter.IRC.channel");
416 >            try {
417 >                _channel = cp.getProperty(_name, "Alerter.IRC.channel");
418 >            } catch (PropertyNotFoundException e) {
419 >                _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
420 >                throw new IOException("Can't get channel name due to configuration error");
421 >            }
422              _socketOut.println("JOIN "+_channel);
423              // allow alerts
424              _active = true;
# Line 344 | Line 444 | public class IRC__Alerter implements PluginAlerter {
444           * Overrides the {@link java.lang.Object#toString() Object.toString()}
445           * method to provide clean logging (every class should have this).
446           *
447 <         * This uses the uk.ac.ukc.iscream.util.NameFormat class
447 >         * This uses the uk.org.iscream.util.NameFormat class
448           * to format the toString()
449           *
450           * @return the name of this class and its CVS revision
# Line 369 | Line 469 | public class IRC__Alerter implements PluginAlerter {
469                  _socketOut.println("PONG" + line.substring(4));
470              }
471              // see if it's for us
472 <            else if(getMsg(line).startsWith(_nickname)) {
472 >            else if(getMsg(line).startsWith(_nickname+",") || getMsg(line).startsWith(_nickname+":") || getMsg(line).startsWith(_nickname+" ")) {
473 >                // setup some String's
474 >                String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand;
475 >                String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand;
476 >                // get the command set
477 >                try {
478 >                    stopCommand = cp.getProperty(_name, "Alerter.IRC.stopCommand");
479 >                    startCommand = cp.getProperty(_name, "Alerter.IRC.startCommand");
480 >                    timeSinceLastAlertCommand = cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand");
481 >                    lastAlertCommand = cp.getProperty(_name, "Alerter.IRC.lastAlertCommand");
482 >                    joinCommand = cp.getProperty(_name, "Alerter.IRC.joinCommand");
483 >                    nickChangeCommand = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand");
484 >                    versionCommand = cp.getProperty(_name, "Alerter.IRC.versionCommand");
485 >                    helpCommand = cp.getProperty(_name, "Alerter.IRC.helpCommand");
486 >                    statCommand = cp.getProperty(_name, "Alerter.IRC.statCommand");
487 >                    uptimeCommand = cp.getProperty(_name, "Alerter.IRC.uptimeCommand");
488 >                } catch (PropertyNotFoundException e) {
489 >                    _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
490 >                    // lets bail from handling this line...
491 >                    // ...it's gonna be hard without a command set!
492 >                    return;
493 >                }
494 >                
495                  // we have a message for us
496                  String message = getMsg(line).substring(_nickname.length());
497 <                if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.stopCommand"))!=-1) {
497 >                if(message.indexOf(stopCommand)!=-1) {
498                      _active = false;
499                      sendMsg(getMsgSender(line)+", alerts have been stopped");
500                  }
501 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.startCommand"))!=-1) {
501 >                else if(message.indexOf(startCommand)!=-1) {
502                      _active = true;
503                      sendMsg(getMsgSender(line)+", alerts have been activated");
504                  }
505 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"))!=-1) {
506 <                    sendMsg(getMsgSender(line)+", last alert was: "+_lastAlert);
505 >                // this needs to go here if it contains the same words as the lastAlertCommand
506 >                else if(message.indexOf(timeSinceLastAlertCommand)!=-1) {
507 >                    if(_lastAlertTime != -1) {
508 >                        long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000;
509 >                        String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
510 >                        sendMsg(getMsgSender(line)+", I last sent an alert "+uptimeText+ " ago");
511 >                    }
512 >                    else {
513 >                        sendMsg(getMsgSender(line)+", I've never sent an alert!");
514 >                    }
515                  }
516 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.joinCommand"))!=-1) {
517 <                    String joinCmd = cp.getProperty(_name, "Alerter.IRC.joinCommand");
516 >                else if(message.indexOf(lastAlertCommand)!=-1) {
517 >                    if(_lastAlertTime != -1) {
518 >                        String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime));
519 >                        sendMsg(getMsgSender(line)+", last alert was at "+date+"; "+_lastAlert);
520 >                    }
521 >                    else {
522 >                        sendMsg(getMsgSender(line)+", I've never sent an alert!");
523 >                    }
524 >                    
525 >                }
526 >                else if(message.indexOf(joinCommand)!=-1) {
527 >                    String joinCmd = joinCommand;
528                      String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1);
529                      int endOfChan = newChan.indexOf(" ");
530                      if(endOfChan == -1) {
# Line 396 | Line 536 | public class IRC__Alerter implements PluginAlerter {
536                      _socketOut.println("JOIN "+newChan);
537                      _channel = newChan;
538                  }
539 <                else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.helpCommand"))!=-1) {
540 <                    sendPrivMsg(getMsgSender(line), "I am the i-scream alerting bot revision "+REVISION.substring(11, REVISION.length() -2));
539 >                else if(message.indexOf(nickChangeCommand)!=-1) {
540 >                    String nickChangeCmd = nickChangeCommand;
541 >                    String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1);
542 >                    int endOfNick = newNick.indexOf(" ");
543 >                    if(endOfNick == -1) {
544 >                        endOfNick = newNick.length();
545 >                    }
546 >                    newNick = newNick.substring(0, endOfNick);
547 >                    sendMsg(getMsgSender(line)+", okay, changing my nickname to "+newNick);
548 >                    _socketOut.println("NICK "+newNick);
549 >                    _nickname = newNick;
550 >                }
551 >                else if(message.indexOf(versionCommand)!=-1) {
552 >                    sendMsg(getMsgSender(line)+", I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot");
553 >                }
554 >                else if(message.indexOf(helpCommand)!=-1) {
555 >                    sendPrivMsg(getMsgSender(line), "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2));
556                      sendPrivMsg(getMsgSender(line), "I understand the following commands;");
557 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.stopCommand"));
558 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.startCommand"));
559 <                    sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"));
557 >                    sendPrivMsg(getMsgSender(line), stopCommand);
558 >                    sendPrivMsg(getMsgSender(line), startCommand);
559 >                    sendPrivMsg(getMsgSender(line), lastAlertCommand);
560 >                    sendPrivMsg(getMsgSender(line), joinCommand);
561 >                    sendPrivMsg(getMsgSender(line), nickChangeCommand);
562 >                    sendPrivMsg(getMsgSender(line), statCommand);
563 >                    sendPrivMsg(getMsgSender(line), uptimeCommand);
564 >                    sendPrivMsg(getMsgSender(line), timeSinceLastAlertCommand);
565 >                    sendPrivMsg(getMsgSender(line), helpCommand);
566                  }
567 +                else if(message.indexOf(statCommand)!=-1) {
568 +                    sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!");
569 +                }
570 +                else if(message.indexOf(uptimeCommand)!=-1) {
571 +                    long uptime = (System.currentTimeMillis() - _startTime) / 1000;
572 +                    String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
573 +                    sendMsg(getMsgSender(line)+", I have been running for "+uptimeText);
574 +                }
575 +                else if(message.indexOf("ping")!=-1) {
576 +                    sendMsg("pong");
577 +                }
578                  else if(message.indexOf("do a jibble dance")!=-1) {
579                      // little joke :)
580                      sendAction("jives to the funky beat shouting \"ii--screeeaaammm\"");
581                  }
582                  else {
583 <                    sendMsg(getMsgSender(line)+", "+cp.getProperty(_name, "Alerter.IRC.rejectMessage"));
583 >                    String rejectMessage = NOT_CONFIGURED;
584 >                    try {
585 >                        rejectMessage = cp.getProperty(_name, "Alerter.IRC.rejectMessage");
586 >                    } catch(PropertyNotFoundException e) {
587 >                        _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
588 >                    }
589 >                    sendMsg(getMsgSender(line)+", "+rejectMessage);
590                  }
591              }
592              else if(line.indexOf(_nickname)!=-1 && line.indexOf(_channel)!=-1 && line.indexOf("KICK")!=-1) {
593                  sendPrivMsg(getMsgSender(line), "That wasn't a nice thing to do...");
594 <                _channel = cp.getProperty(_name, "Alerter.IRC.channel");
594 >                try {
595 >                    _channel = cp.getProperty(_name, "Alerter.IRC.channel");
596 >                } catch(PropertyNotFoundException e) {
597 >                    _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
598 >                }
599                  _socketOut.println("JOIN "+_channel);
600              }
601          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines