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.16 by tdb, Mon Mar 5 23:12:43 2001 UTC vs.
Revision 1.30 by tdb, Thu Jan 17 17:58:12 2002 UTC

# Line 1 | Line 1
1   //---PACKAGE DECLARATION---
2 < package uk.ac.ukc.iscream.client.alerters;
2 > package uk.org.iscream.cms.server.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.*;
9 <
5 > import uk.org.iscream.cms.server.client.*;
6 > import uk.org.iscream.cms.server.core.*;
7 > import uk.org.iscream.cms.server.util.*;
8 > import uk.org.iscream.cms.server.componentmanager.*;
9   import java.io.*;
10   import java.net.*;
11   import java.util.*;
12 < import java.text.*;
12 > import java.text.DateFormat;
13  
14   /**
15   * This Alert sends an IRC message.
# Line 21 | Line 20 | import java.text.*;
20   * @author  $Author$
21   * @version $Id$
22   */
23 < public class IRC__Alerter implements PluginAlerter {
23 > public class IRC__Alerter extends AlerterSkeleton {
24  
25   //---FINAL ATTRIBUTES---
26  
# Line 40 | Line 39 | public class IRC__Alerter implements PluginAlerter {
39       */
40      public final int DEFAULT_RECONNECT_DELAY = 30;
41      
43    public final String DEFAULT_LEVEL = Alert.alertLevels[0];
44    
45    public final String NOT_CONFIGURED = "<NOT CONFIGURED>";
46    
42   //---STATIC METHODS---
43  
44   //---CONSTRUCTORS---
45  
46      public IRC__Alerter() {
47 <                        
47 >        super();
48          // connect to the IRC server
49          _ircbot = new IRCBot();
50 +        // set it's name and start it
51 +        _ircbot.setName("client.IRC__Alerter$IRCBot");
52          _ircbot.start();
53          _startTime = System.currentTimeMillis();
57        
54          _logger.write(toString(), Logger.SYSINIT, "IRC Alerter started");
55      }
56  
57   //---PUBLIC METHODS---
58  
59 +    /**
60 +     * Implements the abstract method from the skeleton class.
61 +     * This method will attempt to send an alert
62 +     * message over the IRC channel.
63 +     *
64 +     * @param alert the alert to send
65 +     */
66      public void sendAlert(Alert alert) {
67 +        // sort out the message      
68 +        String alertType = Alert.alertLevels[alert.getLevel()];        
69 +        String message;
70 +        try {
71 +            message = _cp.getProperty(_name, "Alerter.IRC.message");
72 +        } catch (PropertyNotFoundException e) {
73 +            message = NOT_CONFIGURED;
74 +            _logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message);
75 +        }
76 +        message = processAlertMessage(message, alert);        
77          // only send alerts if we're active
78 <        if(_active) {
79 <            ConfigurationProxy cp = ConfigurationProxy.getInstance();
80 <            
81 <            String levelName;
82 <            try {
83 <                levelName = cp.getProperty(_name, "Alerter.IRC.level");
84 <            } catch (PropertyNotFoundException e) {
85 <                levelName = DEFAULT_LEVEL;
86 <                _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;
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());
92 <                message = StringUtils.replaceText(message, "%value%", alert.getValue());
93 <                message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue());
94 <                message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName());
95 <                message = StringUtils.replaceText(message, "%timeTillNextAlert%",  getTimeString(Long.parseLong(alert.getTimeTillNextAlert())));
96 <                
97 <                // send the message
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 <            }
78 >        if(_active) {          
79 >            // send the message
80 >            _logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level");
81 >            _ircbot.sendMsg(message);
82 >            // count sent alerts
83 >            _alertCount++;
84 >        } else {
85 >            // don't send, but keep a count that we ignored it
86 >            _ignoredCount++;
87          }
88 +        // we'll always store the "last alert", regardless
89 +        // of whether we actually display it or not
90 +        _lastAlert = message;
91 +        _lastAlertTime = System.currentTimeMillis();
92      }
93  
94      /**
95       * Overrides the {@link java.lang.Object#toString() Object.toString()}
96       * method to provide clean logging (every class should have this).
97       *
98 <     * This uses the uk.ac.ukc.iscream.util.NameFormat class
98 >     * This uses the uk.org.iscream.cms.server.util.NameFormat class
99       * to format the toString()
100       *
101       * @return the name of this class and its CVS revision
# Line 120 | Line 107 | public class IRC__Alerter implements PluginAlerter {
107              REVISION);
108      }
109  
110 <    /**
111 <     * return the String representation of what the filter does
110 >    /**
111 >     * Return the String representation of what the alerter does
112 >     *
113 >     * @return the description
114       */
115      public String getDescription(){
116          return DESC;
# Line 129 | Line 118 | public class IRC__Alerter implements PluginAlerter {
118  
119   //---PRIVATE METHODS---
120  
132    private String getTimeString(long time) {
133        String timeString = null;
134        if (time >= 60) {
135            timeString = (time / 60) + " minute(s)";
136        } else if (time >= 3600) {
137            timeString = ((time/60) / 60) + " hour(s)";
138        } else {
139            timeString = time + " second(s)";
140        }
141        return timeString;
142    }
143
121   //---ACCESSOR/MUTATOR METHODS---
122  
123 +    /**
124 +     * Returns the "friendly" name of this class. This
125 +     * is simply an accessor for _name, required due to
126 +     * inheritance issues with extending AlerterSkeleton.
127 +     *
128 +     * @return the friendly name
129 +     */
130 +    protected String getFName() {
131 +        return _name;
132 +    }
133 +
134   //---ATTRIBUTES---
135      
136      /**
# Line 168 | Line 156 | public class IRC__Alerter implements PluginAlerter {
156      /**
157       * Number of alerts sent
158       */
159 <    private long _alertCount = 0;
159 >    private int _alertCount = 0;
160      
161      /**
162 +     * Number of alerts ignored when in "stopped" mode
163 +     */
164 +    private int _ignoredCount = 0;
165 +    
166 +    /**
167       * Time of IRCBot startup
168       */
169      private long _startTime;
# Line 184 | Line 177 | public class IRC__Alerter implements PluginAlerter {
177       * can be placed here.  This name could also
178       * be changed to null for utility classes.
179       */
180 <    private String _name = "IRC Alert";
180 >    private String _name = "IRC";
181  
189    /**
190     * This holds a reference to the
191     * system logger that is being used.
192     */
193    private Logger _logger = ReferenceManager.getInstance().getLogger();
194
182   //---STATIC ATTRIBUTES---
183  
184   //---INNER CLASSES---
# Line 430 | Line 417 | public class IRC__Alerter implements PluginAlerter {
417           * Overrides the {@link java.lang.Object#toString() Object.toString()}
418           * method to provide clean logging (every class should have this).
419           *
420 <         * This uses the uk.ac.ukc.iscream.util.NameFormat class
420 >         * This uses the uk.org.iscream.cms.server.util.NameFormat class
421           * to format the toString()
422           *
423           * @return the name of this class and its CVS revision
# Line 455 | Line 442 | public class IRC__Alerter implements PluginAlerter {
442                  _socketOut.println("PONG" + line.substring(4));
443              }
444              // see if it's for us
445 <            else if(getMsg(line).startsWith(_nickname+",") || getMsg(line).startsWith(_nickname+":") || getMsg(line).startsWith(_nickname+" ")) {
445 >            else if(getMsg(line).toLowerCase().startsWith(_nickname.toLowerCase()+",") ||
446 >                    getMsg(line).toLowerCase().startsWith(_nickname.toLowerCase()+":") ||
447 >                    getMsg(line).toLowerCase().startsWith(_nickname.toLowerCase()+" ")) {
448                  // setup some String's
449                  String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand;
450                  String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand;
# Line 517 | Line 506 | public class IRC__Alerter implements PluginAlerter {
506                          endOfChan = newChan.length();
507                      }
508                      newChan = newChan.substring(0, endOfChan);
509 <                    sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan);
510 <                    _socketOut.println("PART "+_channel);
511 <                    _socketOut.println("JOIN "+newChan);
512 <                    _channel = newChan;
509 >                    if(newChan.equals(_channel)) {
510 >                        sendMsg(getMsgSender(line)+", I'm already on "+newChan+"!");
511 >                    } else {
512 >                        sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan);
513 >                        _socketOut.println("PART "+_channel);
514 >                        _socketOut.println("JOIN "+newChan);
515 >                        _channel = newChan;
516 >                    }
517                  }
518                  else if(message.indexOf(nickChangeCommand)!=-1) {
519                      String nickChangeCmd = nickChangeCommand;
# Line 551 | Line 544 | public class IRC__Alerter implements PluginAlerter {
544                      sendPrivMsg(getMsgSender(line), helpCommand);
545                  }
546                  else if(message.indexOf(statCommand)!=-1) {
547 <                    sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts!");
547 >                    sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!");
548                  }
549                  else if(message.indexOf(uptimeCommand)!=-1) {
550                      long uptime = (System.currentTimeMillis() - _startTime) / 1000;
551                      String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
552                      sendMsg(getMsgSender(line)+", I have been running for "+uptimeText);
553                  }
554 +                else if(message.indexOf("ping")!=-1) {
555 +                    sendMsg("pong");
556 +                }
557                  else if(message.indexOf("do a jibble dance")!=-1) {
558                      // little joke :)
559                      sendAction("jives to the funky beat shouting \"ii--screeeaaammm\"");
# Line 644 | Line 640 | public class IRC__Alerter implements PluginAlerter {
640           * A reminder of our current nickname...
641           */
642          private String _nickname;
643 +        
644 +        /**
645 +         * This holds a reference to the
646 +         * system logger that is being used.
647 +         */
648 +        protected Logger _logger = ReferenceManager.getInstance().getLogger();
649          
650      }
651  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines