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.*; |
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 |
|
|
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 |
< |
String timeFirstSince = DateUtils.formatTime(System.currentTimeMillis() - alert.getInitialAlertTime(), "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
82 |
> |
String timeFirstSince = DateUtils.formatTime((System.currentTimeMillis() - alert.getInitialAlertTime())/1000, "%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; |
96 |
|
message = StringUtils.replaceText(message, "%value%", alert.getValue()); |
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()))); |
99 |
> |
message = StringUtils.replaceText(message, "%timeTillNextAlert%", DateUtils.getTimeString(Long.parseLong(alert.getTimeTillNextAlert()))); |
100 |
|
message = StringUtils.replaceText(message, "%timeSinceFirstAlert%", timeFirstSince); |
101 |
|
message = StringUtils.replaceText(message, "%timeOfFirstAlert%", timeFirstOccured); |
102 |
|
|
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 |
138 |
|
|
139 |
|
//---PRIVATE METHODS--- |
140 |
|
|
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 |
– |
|
141 |
|
//---ACCESSOR/MUTATOR METHODS--- |
142 |
|
|
143 |
|
//---ATTRIBUTES--- |
165 |
|
/** |
166 |
|
* Number of alerts sent |
167 |
|
*/ |
168 |
< |
private long _alertCount = 0; |
168 |
> |
private int _alertCount = 0; |
169 |
|
|
170 |
|
/** |
171 |
+ |
* Number of alerts ignored when in "stopped" mode |
172 |
+ |
*/ |
173 |
+ |
private int _ignoredCount = 0; |
174 |
+ |
|
175 |
+ |
/** |
176 |
|
* Time of IRCBot startup |
177 |
|
*/ |
178 |
|
private long _startTime; |
432 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
433 |
|
* method to provide clean logging (every class should have this). |
434 |
|
* |
435 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
435 |
> |
* This uses the uk.org.iscream.util.NameFormat class |
436 |
|
* to format the toString() |
437 |
|
* |
438 |
|
* @return the name of this class and its CVS revision |
519 |
|
endOfChan = newChan.length(); |
520 |
|
} |
521 |
|
newChan = newChan.substring(0, endOfChan); |
522 |
< |
sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan); |
523 |
< |
_socketOut.println("PART "+_channel); |
524 |
< |
_socketOut.println("JOIN "+newChan); |
525 |
< |
_channel = newChan; |
522 |
> |
if(newChan.equals(_channel)) { |
523 |
> |
sendMsg(getMsgSender(line)+", I'm already on "+newChan+"!"); |
524 |
> |
} else { |
525 |
> |
sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan); |
526 |
> |
_socketOut.println("PART "+_channel); |
527 |
> |
_socketOut.println("JOIN "+newChan); |
528 |
> |
_channel = newChan; |
529 |
> |
} |
530 |
|
} |
531 |
|
else if(message.indexOf(nickChangeCommand)!=-1) { |
532 |
|
String nickChangeCmd = nickChangeCommand; |
557 |
|
sendPrivMsg(getMsgSender(line), helpCommand); |
558 |
|
} |
559 |
|
else if(message.indexOf(statCommand)!=-1) { |
560 |
< |
sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts!"); |
560 |
> |
sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"); |
561 |
|
} |
562 |
|
else if(message.indexOf(uptimeCommand)!=-1) { |
563 |
|
long uptime = (System.currentTimeMillis() - _startTime) / 1000; |
564 |
|
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
565 |
|
sendMsg(getMsgSender(line)+", I have been running for "+uptimeText); |
566 |
+ |
} |
567 |
+ |
else if(message.indexOf("ping")!=-1) { |
568 |
+ |
sendMsg("pong"); |
569 |
|
} |
570 |
|
else if(message.indexOf("do a jibble dance")!=-1) { |
571 |
|
// little joke :) |