| 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.*; |
| 9 |
< |
|
| 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 |
|
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. |
| 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 |
|
|
| 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 |
|
// only send alerts if we're active |
| 68 |
|
if(_active) { |
| 69 |
< |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
| 70 |
< |
|
| 71 |
< |
String levelName; |
| 69 |
> |
// sort out the message |
| 70 |
> |
String alertType = Alert.alertLevels[alert.getLevel()]; |
| 71 |
> |
String message; |
| 72 |
|
try { |
| 73 |
< |
levelName = cp.getProperty(_name, "Alerter.IRC.level"); |
| 73 |
> |
message = _cp.getProperty(_name, "Alerter.IRC.message"); |
| 74 |
|
} catch (PropertyNotFoundException e) { |
| 75 |
< |
levelName = DEFAULT_LEVEL; |
| 76 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.IRC.level value unavailable using default of " + levelName); |
| 75 |
> |
message = NOT_CONFIGURED; |
| 76 |
> |
_logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message); |
| 77 |
|
} |
| 78 |
< |
int level = StringUtils.getStringPos(levelName, Alert.alertLevels); |
| 79 |
< |
// only send if it's equal (or above) our level |
| 80 |
< |
if(((alert.getLevel() == 0) && (alert.getLastLevel() >= level)) || (alert.getLevel() >= level)) { |
| 81 |
< |
String alertType = Alert.alertLevels[alert.getLevel()]; |
| 82 |
< |
String thresholdType = Alert.thresholdLevels[alert.getThreshold()]; |
| 83 |
< |
String timeFirstSince = DateUtils.formatTime(System.currentTimeMillis() - alert.getInitialAlertTime(), "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
| 84 |
< |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
| 85 |
< |
// sort out the message |
| 86 |
< |
String message; |
| 87 |
< |
try { |
| 85 |
< |
message = cp.getProperty(_name, "Alerter.IRC.message"); |
| 86 |
< |
} catch (PropertyNotFoundException e) { |
| 87 |
< |
message = NOT_CONFIGURED; |
| 88 |
< |
_logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message); |
| 89 |
< |
} |
| 90 |
< |
|
| 91 |
< |
message = StringUtils.replaceText(message, "%level%", alertType); |
| 92 |
< |
message = StringUtils.replaceText(message, "%threshold%", thresholdType); |
| 93 |
< |
message = StringUtils.replaceText(message, "%source%", alert.getSource()); |
| 94 |
< |
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
| 95 |
< |
message = StringUtils.replaceText(message, "%thresholdValue%", alert.getThresholdValue()); |
| 96 |
< |
message = StringUtils.replaceText(message, "%attributeName%", alert.getAttributeName()); |
| 97 |
< |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
| 98 |
< |
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince); |
| 99 |
< |
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
| 100 |
< |
|
| 101 |
< |
// send the message |
| 102 |
< |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
| 103 |
< |
_ircbot.sendMsg(message); |
| 104 |
< |
_lastAlert = message; |
| 105 |
< |
_lastAlertTime = System.currentTimeMillis(); |
| 106 |
< |
_alertCount ++; |
| 107 |
< |
} |
| 78 |
> |
message = processAlertMessage(message, alert); |
| 79 |
> |
|
| 80 |
> |
// send the message |
| 81 |
> |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
| 82 |
> |
_ircbot.sendMsg(message); |
| 83 |
> |
// count sent alerts |
| 84 |
> |
_alertCount++; |
| 85 |
> |
} else { |
| 86 |
> |
// don't send, but keep a count that we ignored it |
| 87 |
> |
_ignoredCount++; |
| 88 |
|
} |
| 89 |
+ |
// we'll always store the "last alert", regardless |
| 90 |
+ |
// of whether we actually display it or not |
| 91 |
+ |
_lastAlert = message; |
| 92 |
+ |
_lastAlertTime = System.currentTimeMillis(); |
| 93 |
|
} |
| 94 |
|
|
| 95 |
|
/** |
| 96 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 97 |
|
* method to provide clean logging (every class should have this). |
| 98 |
|
* |
| 99 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
| 99 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
| 100 |
|
* to format the toString() |
| 101 |
|
* |
| 102 |
|
* @return the name of this class and its CVS revision |
| 108 |
|
REVISION); |
| 109 |
|
} |
| 110 |
|
|
| 111 |
< |
/** |
| 112 |
< |
* return the String representation of what the filter does |
| 111 |
> |
/** |
| 112 |
> |
* Return the String representation of what the alerter does |
| 113 |
> |
* |
| 114 |
> |
* @return the description |
| 115 |
|
*/ |
| 116 |
|
public String getDescription(){ |
| 117 |
|
return DESC; |
| 119 |
|
|
| 120 |
|
//---PRIVATE METHODS--- |
| 121 |
|
|
| 136 |
– |
private String getTimeString(long time) { |
| 137 |
– |
String timeString = null; |
| 138 |
– |
if (time >= 60) { |
| 139 |
– |
timeString = (time / 60) + " minute(s)"; |
| 140 |
– |
} else if (time >= 3600) { |
| 141 |
– |
timeString = ((time/60) / 60) + " hour(s)"; |
| 142 |
– |
} else { |
| 143 |
– |
timeString = time + " second(s)"; |
| 144 |
– |
} |
| 145 |
– |
return timeString; |
| 146 |
– |
} |
| 147 |
– |
|
| 122 |
|
//---ACCESSOR/MUTATOR METHODS--- |
| 123 |
|
|
| 124 |
|
//---ATTRIBUTES--- |
| 146 |
|
/** |
| 147 |
|
* Number of alerts sent |
| 148 |
|
*/ |
| 149 |
< |
private long _alertCount = 0; |
| 149 |
> |
private int _alertCount = 0; |
| 150 |
|
|
| 151 |
|
/** |
| 152 |
+ |
* Number of alerts ignored when in "stopped" mode |
| 153 |
+ |
*/ |
| 154 |
+ |
private int _ignoredCount = 0; |
| 155 |
+ |
|
| 156 |
+ |
/** |
| 157 |
|
* Time of IRCBot startup |
| 158 |
|
*/ |
| 159 |
|
private long _startTime; |
| 167 |
|
* can be placed here. This name could also |
| 168 |
|
* be changed to null for utility classes. |
| 169 |
|
*/ |
| 170 |
< |
private String _name = "IRC Alert"; |
| 170 |
> |
private String _name = "IRC"; |
| 171 |
|
|
| 193 |
– |
/** |
| 194 |
– |
* This holds a reference to the |
| 195 |
– |
* system logger that is being used. |
| 196 |
– |
*/ |
| 197 |
– |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 198 |
– |
|
| 172 |
|
//---STATIC ATTRIBUTES--- |
| 173 |
|
|
| 174 |
|
//---INNER CLASSES--- |
| 407 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 408 |
|
* method to provide clean logging (every class should have this). |
| 409 |
|
* |
| 410 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
| 410 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
| 411 |
|
* to format the toString() |
| 412 |
|
* |
| 413 |
|
* @return the name of this class and its CVS revision |
| 494 |
|
endOfChan = newChan.length(); |
| 495 |
|
} |
| 496 |
|
newChan = newChan.substring(0, endOfChan); |
| 497 |
< |
sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan); |
| 498 |
< |
_socketOut.println("PART "+_channel); |
| 499 |
< |
_socketOut.println("JOIN "+newChan); |
| 500 |
< |
_channel = newChan; |
| 497 |
> |
if(newChan.equals(_channel)) { |
| 498 |
> |
sendMsg(getMsgSender(line)+", I'm already on "+newChan+"!"); |
| 499 |
> |
} else { |
| 500 |
> |
sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan); |
| 501 |
> |
_socketOut.println("PART "+_channel); |
| 502 |
> |
_socketOut.println("JOIN "+newChan); |
| 503 |
> |
_channel = newChan; |
| 504 |
> |
} |
| 505 |
|
} |
| 506 |
|
else if(message.indexOf(nickChangeCommand)!=-1) { |
| 507 |
|
String nickChangeCmd = nickChangeCommand; |
| 532 |
|
sendPrivMsg(getMsgSender(line), helpCommand); |
| 533 |
|
} |
| 534 |
|
else if(message.indexOf(statCommand)!=-1) { |
| 535 |
< |
sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts!"); |
| 535 |
> |
sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"); |
| 536 |
|
} |
| 537 |
|
else if(message.indexOf(uptimeCommand)!=-1) { |
| 538 |
|
long uptime = (System.currentTimeMillis() - _startTime) / 1000; |
| 539 |
|
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
| 540 |
|
sendMsg(getMsgSender(line)+", I have been running for "+uptimeText); |
| 541 |
|
} |
| 542 |
+ |
else if(message.indexOf("ping")!=-1) { |
| 543 |
+ |
sendMsg("pong"); |
| 544 |
+ |
} |
| 545 |
|
else if(message.indexOf("do a jibble dance")!=-1) { |
| 546 |
|
// little joke :) |
| 547 |
|
sendAction("jives to the funky beat shouting \"ii--screeeaaammm\""); |
| 628 |
|
* A reminder of our current nickname... |
| 629 |
|
*/ |
| 630 |
|
private String _nickname; |
| 631 |
+ |
|
| 632 |
+ |
/** |
| 633 |
+ |
* This holds a reference to the |
| 634 |
+ |
* system logger that is being used. |
| 635 |
+ |
*/ |
| 636 |
+ |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
| 637 |
|
|
| 638 |
|
} |
| 639 |
|
|