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 |
> |
import org.jibble.pircbot.*; |
14 |
|
|
15 |
|
/** |
16 |
|
* This Alert sends an IRC message. |
21 |
|
* @author $Author$ |
22 |
|
* @version $Id$ |
23 |
|
*/ |
24 |
< |
public class IRC__Alerter implements PluginAlerter { |
24 |
> |
public class IRC__Alerter extends AlerterSkeleton { |
25 |
|
|
26 |
|
//---FINAL ATTRIBUTES--- |
27 |
|
|
35 |
|
*/ |
36 |
|
public final String DESC = "Sends alerts on an IRC channel"; |
37 |
|
|
38 |
– |
/** |
39 |
– |
* The default reconnect delay in seconds |
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 |
– |
|
38 |
|
//---STATIC METHODS--- |
39 |
|
|
40 |
|
//---CONSTRUCTORS--- |
41 |
< |
|
41 |
> |
|
42 |
|
public IRC__Alerter() { |
43 |
< |
|
44 |
< |
// connect to the IRC server |
43 |
> |
super(); |
44 |
> |
// construct and initialise the bot |
45 |
|
_ircbot = new IRCBot(); |
46 |
< |
_ircbot.start(); |
46 |
> |
//_ircbot.setVerbose(true); |
47 |
> |
Thread ircThread = new Thread(_ircbot); |
48 |
> |
// set it's name and start it |
49 |
> |
ircThread.setName("client.IRC__Alerter$IRCBot"); |
50 |
> |
ircThread.start(); |
51 |
> |
// log our start time |
52 |
|
_startTime = System.currentTimeMillis(); |
57 |
– |
|
53 |
|
_logger.write(toString(), Logger.SYSINIT, "IRC Alerter started"); |
54 |
|
} |
55 |
|
|
56 |
|
//---PUBLIC METHODS--- |
57 |
|
|
58 |
+ |
/** |
59 |
+ |
* Implements the abstract method from the skeleton class. |
60 |
+ |
* This method will attempt to send an alert |
61 |
+ |
* message over the IRC channel. |
62 |
+ |
* |
63 |
+ |
* @param alert the alert to send |
64 |
+ |
*/ |
65 |
|
public void sendAlert(Alert alert) { |
66 |
+ |
// sort out the message |
67 |
+ |
String alertType = Alert.alertLevels[alert.getLevel()]; |
68 |
+ |
String message; |
69 |
+ |
try { |
70 |
+ |
message = _cp.getProperty(_name, "Alerter.IRC.message"); |
71 |
+ |
} catch (PropertyNotFoundException e) { |
72 |
+ |
message = NOT_CONFIGURED; |
73 |
+ |
_logger.write(toString(), Logger.WARNING, "Alerter.IRC.message value unavailable using default of " + message); |
74 |
+ |
} |
75 |
+ |
message = processAlertMessage(message, alert); |
76 |
|
// only send alerts if we're active |
77 |
< |
if(_active) { |
78 |
< |
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
79 |
< |
|
80 |
< |
String levelName; |
81 |
< |
try { |
82 |
< |
levelName = cp.getProperty(_name, "Alerter.IRC.level"); |
83 |
< |
} catch (PropertyNotFoundException e) { |
84 |
< |
levelName = DEFAULT_LEVEL; |
85 |
< |
_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 |
< |
String timeFirstSince = DateUtils.formatTime(System.currentTimeMillis() - alert.getInitialAlertTime(), "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
81 |
< |
String timeFirstOccured = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(alert.getInitialAlertTime())); |
82 |
< |
// sort out the message |
83 |
< |
String message; |
84 |
< |
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 |
< |
} |
77 |
> |
if(_active) { |
78 |
> |
// send the message |
79 |
> |
_logger.write(toString(), Logger.DEBUG, "Sending " + _name + " at "+ alertType + " level"); |
80 |
> |
_ircbot.sendMsg(message); |
81 |
> |
// count sent alerts |
82 |
> |
_alertCount++; |
83 |
> |
} else { |
84 |
> |
// don't send, but keep a count that we ignored it |
85 |
> |
_ignoredCount++; |
86 |
|
} |
87 |
< |
else { |
88 |
< |
_ignoredCount ++; |
89 |
< |
} |
87 |
> |
// we'll always store the "last alert", regardless |
88 |
> |
// of whether we actually display it or not |
89 |
> |
_lastAlert = message; |
90 |
> |
_lastAlertTime = System.currentTimeMillis(); |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
95 |
|
* method to provide clean logging (every class should have this). |
96 |
|
* |
97 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
97 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
98 |
|
* to format the toString() |
99 |
|
* |
100 |
|
* @return the name of this class and its CVS revision |
106 |
|
REVISION); |
107 |
|
} |
108 |
|
|
109 |
< |
/** |
110 |
< |
* return the String representation of what the filter does |
109 |
> |
/** |
110 |
> |
* Return the String representation of what the alerter does |
111 |
> |
* |
112 |
> |
* @return the description |
113 |
|
*/ |
114 |
|
public String getDescription(){ |
115 |
|
return DESC; |
117 |
|
|
118 |
|
//---PRIVATE METHODS--- |
119 |
|
|
139 |
– |
private String getTimeString(long time) { |
140 |
– |
String timeString = null; |
141 |
– |
if (time >= 60) { |
142 |
– |
timeString = (time / 60) + " minute(s)"; |
143 |
– |
} else if (time >= 3600) { |
144 |
– |
timeString = ((time/60) / 60) + " hour(s)"; |
145 |
– |
} else { |
146 |
– |
timeString = time + " second(s)"; |
147 |
– |
} |
148 |
– |
return timeString; |
149 |
– |
} |
150 |
– |
|
120 |
|
//---ACCESSOR/MUTATOR METHODS--- |
121 |
|
|
122 |
+ |
/** |
123 |
+ |
* Returns the "friendly" name of this class. This |
124 |
+ |
* is simply an accessor for _name, required due to |
125 |
+ |
* inheritance issues with extending AlerterSkeleton. |
126 |
+ |
* |
127 |
+ |
* @return the friendly name |
128 |
+ |
*/ |
129 |
+ |
protected String getFName() { |
130 |
+ |
return _name; |
131 |
+ |
} |
132 |
+ |
|
133 |
|
//---ATTRIBUTES--- |
134 |
|
|
135 |
|
/** |
168 |
|
private long _startTime; |
169 |
|
|
170 |
|
/** |
171 |
+ |
* This holds a reference to the |
172 |
+ |
* system logger that is being used. |
173 |
+ |
*/ |
174 |
+ |
protected Logger _logger = ReferenceManager.getInstance().getLogger(); |
175 |
+ |
|
176 |
+ |
/** |
177 |
|
* This is the friendly identifier of the |
178 |
|
* component this class is running in. |
179 |
|
* eg, a Filter may be called "filter1", |
182 |
|
* can be placed here. This name could also |
183 |
|
* be changed to null for utility classes. |
184 |
|
*/ |
185 |
< |
private String _name = "IRC Alert"; |
185 |
> |
private String _name = "IRC"; |
186 |
|
|
201 |
– |
/** |
202 |
– |
* This holds a reference to the |
203 |
– |
* system logger that is being used. |
204 |
– |
*/ |
205 |
– |
private Logger _logger = ReferenceManager.getInstance().getLogger(); |
206 |
– |
|
187 |
|
//---STATIC ATTRIBUTES--- |
188 |
|
|
189 |
|
//---INNER CLASSES--- |
190 |
|
|
191 |
< |
/** |
212 |
< |
* This class provides some basic IRCBot functionality. It connects |
213 |
< |
* to a specified server, and will remain there until told to |
214 |
< |
* leave. Whilst connected it can send a message or a notice to |
215 |
< |
* the server. |
216 |
< |
*/ |
217 |
< |
class IRCBot extends Thread { |
191 |
> |
class IRCBot extends PircBot implements Runnable { |
192 |
|
|
219 |
– |
public static final String DEFAULT_STARTUP_NOTICE = "i-scream ircbot starting..."; |
220 |
– |
|
193 |
|
/** |
194 |
< |
* Main thread loop, this part of the class listens for |
223 |
< |
* messages from the server, and acts accordingly. At the |
224 |
< |
* present moment it only responds to pings. |
194 |
> |
* The default reconnect delay in seconds |
195 |
|
*/ |
196 |
+ |
public final int DEFAULT_RECONNECT_DELAY = 30; |
197 |
+ |
|
198 |
|
public void run() { |
199 |
< |
// so we can stop if required |
228 |
< |
boolean run = true; |
229 |
< |
while(run) { |
230 |
< |
// flag so we can stop the loop |
231 |
< |
boolean doRead = true; |
232 |
< |
// get the startup notice |
233 |
< |
String startupNotice; |
199 |
> |
while(true) { |
200 |
|
try { |
201 |
< |
startupNotice = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice"); |
202 |
< |
} catch (PropertyNotFoundException e) { |
237 |
< |
startupNotice = DEFAULT_STARTUP_NOTICE; |
238 |
< |
_logger.write(this.toString(), Logger.WARNING, "Configuration error: "+e); |
201 |
> |
init(); |
202 |
> |
break; |
203 |
|
} |
204 |
< |
// connect to the IRC server |
205 |
< |
try { |
206 |
< |
connect(); |
243 |
< |
sendNotice(startupNotice); |
244 |
< |
} catch(IOException e) { |
245 |
< |
doRead=false; |
246 |
< |
_logger.write(this.toString(), Logger.ERROR, "Error connecting: "+e); |
204 |
> |
catch (IOException e) { |
205 |
> |
_logger.write(this.toString(), Logger.ERROR, "Error initialising IRCBot: "+e); |
206 |
> |
reconnectSleep(); |
207 |
|
} |
248 |
– |
while(doRead) { |
249 |
– |
try { |
250 |
– |
// read a command |
251 |
– |
String cmd = _socketIn.readLine(); |
252 |
– |
// if we have a null, we've lost contact |
253 |
– |
if(cmd == null) { |
254 |
– |
throw new IOException("End of stream reached"); |
255 |
– |
} |
256 |
– |
// let another method deal with the input |
257 |
– |
handleInput(cmd); |
258 |
– |
} catch (IOException e) { |
259 |
– |
// comms failure, maybe our link is dead. |
260 |
– |
_logger.write(this.toString(), Logger.ERROR, "Communication error: "+e); |
261 |
– |
// stop, and loop round for a reconnect. |
262 |
– |
doRead = false; |
263 |
– |
} |
264 |
– |
} |
265 |
– |
// make sure we're disconnected |
266 |
– |
try { |
267 |
– |
disconnect(); |
268 |
– |
} catch (IOException e) { |
269 |
– |
_logger.write(this.toString(), Logger.ERROR, "Communication error: "+e); |
270 |
– |
} |
271 |
– |
|
272 |
– |
// comms have failed, so wait a while and reconnect |
273 |
– |
int delayTime = 0; |
274 |
– |
try { |
275 |
– |
delayTime = Integer.parseInt(ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.reconnectDelay")); |
276 |
– |
} catch (NumberFormatException e) { |
277 |
– |
delayTime = DEFAULT_RECONNECT_DELAY; |
278 |
– |
_logger.write(this.toString(), Logger.WARNING, "Erronous Alerter.IRC.reconnectDelay value in configuration using default of " + delayTime + " seconds"); |
279 |
– |
} catch (PropertyNotFoundException e) { |
280 |
– |
delayTime = DEFAULT_RECONNECT_DELAY; |
281 |
– |
_logger.write(this.toString(), Logger.WARNING, "Alerter.IRC.reconnectDelay value unavailable using default of " + delayTime + " seconds"); |
282 |
– |
} |
283 |
– |
try { |
284 |
– |
Thread.sleep(delayTime * 1000); |
285 |
– |
} catch (InterruptedException e) {} |
208 |
|
} |
209 |
< |
// maybe disconnect here ? - shutdown method not implemented yet |
288 |
< |
//disconnect(); |
209 |
> |
//System.out.println("falling out!"); |
210 |
|
} |
211 |
|
|
212 |
< |
/** |
213 |
< |
* Sends a message to the channel. |
293 |
< |
* |
294 |
< |
* @param msg The message to send |
295 |
< |
*/ |
296 |
< |
public void sendMsg(String msg) { |
297 |
< |
_socketOut.println("PRIVMSG "+_channel+" :"+msg); |
298 |
< |
// wait a second before returning... |
299 |
< |
// this ensures messages can't be sent too fast |
300 |
< |
try {Thread.sleep(1000);} catch (InterruptedException e) {} |
301 |
< |
} |
302 |
< |
|
303 |
< |
/** |
304 |
< |
* Sends a message to the channel. |
305 |
< |
* |
306 |
< |
* @param user The user to send to |
307 |
< |
* @param msg The message to send |
308 |
< |
*/ |
309 |
< |
public void sendPrivMsg(String user, String msg) { |
310 |
< |
_socketOut.println("PRIVMSG "+user+" :"+msg); |
311 |
< |
// wait a second before returning... |
312 |
< |
// this ensures messages can't be sent too fast |
313 |
< |
try {Thread.sleep(1000);} catch (InterruptedException e) {} |
314 |
< |
} |
315 |
< |
|
316 |
< |
/** |
317 |
< |
* Sends an action to the channel. |
318 |
< |
* |
319 |
< |
* @param msg the action message |
320 |
< |
*/ |
321 |
< |
public void sendAction(String msg) { |
322 |
< |
char esc = 001; |
323 |
< |
sendMsg(esc+"ACTION "+msg+esc); |
324 |
< |
// wait a second before returning... |
325 |
< |
// this ensures messages can't be sent too fast |
326 |
< |
try {Thread.sleep(1000);} catch (InterruptedException e) {} |
327 |
< |
} |
328 |
< |
|
329 |
< |
/** |
330 |
< |
* Sends a notice to the channel. |
331 |
< |
* |
332 |
< |
* @param msg The notice to send |
333 |
< |
*/ |
334 |
< |
public void sendNotice(String msg) { |
335 |
< |
_socketOut.println("NOTICE "+_channel+" :"+msg); |
336 |
< |
// wait a second before returning... |
337 |
< |
// this ensures messages can't be sent too fast |
338 |
< |
try {Thread.sleep(1000);} catch (InterruptedException e) {} |
339 |
< |
} |
340 |
< |
|
341 |
< |
/** |
342 |
< |
* Connect to the IRC server, log in, and join the channel. |
343 |
< |
* |
344 |
< |
* @throws IOException if the connection fails |
345 |
< |
*/ |
346 |
< |
public void connect() throws IOException { |
212 |
> |
public void init() throws IOException { |
213 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Initialising IRCBot..."); |
214 |
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
348 |
– |
// setup the socket, reader and writer |
215 |
|
String server; |
216 |
|
int port; |
217 |
|
try { |
224 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
225 |
|
throw new IOException("Can't get irc server details due to malformed configuration"); |
226 |
|
} |
361 |
– |
_socket = new Socket(server, port); |
362 |
– |
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
363 |
– |
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
364 |
– |
//_socketOut.println("PASS"); |
365 |
– |
// send USER details |
227 |
|
String user, comment; |
228 |
|
try { |
229 |
|
user = cp.getProperty(_name, "Alerter.IRC.user"); |
232 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
233 |
|
throw new IOException("Can't get user details due to configuration error"); |
234 |
|
} |
235 |
< |
_socketOut.println("USER "+user+" 8 * :"+comment); |
236 |
< |
// attempt to get a nick |
235 |
> |
this.setLogin(user); |
236 |
> |
this.setVersion(comment); |
237 |
> |
this.setFinger(comment); // ? |
238 |
|
String nickList; |
239 |
|
try { |
240 |
|
nickList = cp.getProperty(_name, "Alerter.IRC.nickList"); |
244 |
|
} |
245 |
|
StringTokenizer st = new StringTokenizer(nickList, ";"); |
246 |
|
boolean ok = false; |
385 |
– |
// try until we exhaust our list |
247 |
|
while(!ok && st.hasMoreTokens()) { |
248 |
|
String nick = st.nextToken(); |
249 |
< |
_socketOut.println("NICK "+nick); |
250 |
< |
// get a "yes" or "no" response back |
251 |
< |
String response = ""; |
252 |
< |
do { |
253 |
< |
response = _socketIn.readLine(); |
393 |
< |
if(response==null) { |
394 |
< |
throw new IOException("Communication error whilst logging in"); |
395 |
< |
} |
396 |
< |
} while(response.indexOf("001")==-1 && response.indexOf("433")==-1); |
397 |
< |
// see if it was a yes |
398 |
< |
if(response.indexOf("001")!=-1) { |
399 |
< |
// great, we're logged in ! |
400 |
< |
ok = true; |
401 |
< |
// store the name we're using |
249 |
> |
try { |
250 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Trying nick: "+nick); |
251 |
> |
this.setName(nick); |
252 |
> |
this.connect(server, port); |
253 |
> |
// must be ok if we get here |
254 |
|
_nickname = nick; |
255 |
+ |
ok = true; |
256 |
|
} |
257 |
< |
else { |
258 |
< |
// log we couldn't get the name |
259 |
< |
_logger.write(this.toString(), Logger.WARNING, "Nickname in use: "+nick); |
257 |
> |
catch(IOException e) { |
258 |
> |
_logger.write(this.toString(), Logger.ERROR, "IO error when connecting to server: "+e); |
259 |
> |
throw new IOException("IO error when connecting to server"); |
260 |
|
} |
261 |
+ |
catch(IrcException e) { |
262 |
+ |
_logger.write(this.toString(), Logger.ERROR, "IRC error when connecting to server: "+e); |
263 |
+ |
throw new IOException("IRC error when connecting to server"); |
264 |
+ |
} |
265 |
+ |
catch(NickAlreadyInUseException e) { |
266 |
+ |
_logger.write(this.toString(), Logger.ERROR, "Nickname "+nick+" is already in use: "+e); |
267 |
+ |
// just carry on around while loop |
268 |
+ |
} |
269 |
|
} |
270 |
|
if(!ok) { |
271 |
|
// oh dear, we couldn't get on. |
272 |
< |
throw new IOException("All nicknames in use"); |
272 |
> |
_logger.write(this.toString(), Logger.ERROR, "All nicknames already in use"); |
273 |
> |
throw new IOException("All nicknames already in use"); |
274 |
|
} |
413 |
– |
// join the channel |
275 |
|
try { |
276 |
|
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
277 |
+ |
this.joinChannel(_channel); |
278 |
|
} catch (PropertyNotFoundException e) { |
279 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
280 |
|
throw new IOException("Can't get channel name due to configuration error"); |
281 |
|
} |
282 |
< |
_socketOut.println("JOIN "+_channel); |
283 |
< |
// allow alerts |
282 |
> |
|
283 |
> |
String startupNotice; |
284 |
> |
try { |
285 |
> |
startupNotice = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice"); |
286 |
> |
sendNotice(_channel, startupNotice); |
287 |
> |
} catch (PropertyNotFoundException e) { |
288 |
> |
_logger.write(this.toString(), Logger.DEBUG, "Startup notice not defined, so not sending: "+e); |
289 |
> |
} |
290 |
> |
|
291 |
> |
// we should set this when all is ready! |
292 |
|
_active = true; |
293 |
+ |
//System.out.println("leaving init"); |
294 |
|
} |
295 |
|
|
296 |
< |
/** |
297 |
< |
* Disconnect "nicely" from the IRC server. |
298 |
< |
* |
299 |
< |
* @throws IOException if the disconnection fails |
300 |
< |
*/ |
430 |
< |
public void disconnect() throws IOException { |
431 |
< |
// stop alerts |
296 |
> |
public void sendMsg(String msg) { |
297 |
> |
sendMessage(_channel, msg); |
298 |
> |
} |
299 |
> |
|
300 |
> |
public void onDisconnect() { |
301 |
|
_active = false; |
302 |
< |
// send proper QUIT |
303 |
< |
_socketOut.println("QUIT : iscreamBot component shutting down..."); |
304 |
< |
// close the socket |
305 |
< |
_socketOut.close(); |
306 |
< |
_socketIn.close(); |
307 |
< |
_socket.close(); |
302 |
> |
while(true) { |
303 |
> |
reconnectSleep(); |
304 |
> |
try { |
305 |
> |
init(); |
306 |
> |
break; |
307 |
> |
} |
308 |
> |
catch (IOException e) { |
309 |
> |
_logger.write(this.toString(), Logger.ERROR, "Error initialising IRCBot: "+e); |
310 |
> |
} |
311 |
> |
} |
312 |
> |
//System.out.println("falling out of disconnect!"); |
313 |
|
} |
314 |
|
|
315 |
< |
/** |
316 |
< |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
317 |
< |
* method to provide clean logging (every class should have this). |
318 |
< |
* |
319 |
< |
* This uses the uk.ac.ukc.iscream.util.NameFormat class |
320 |
< |
* to format the toString() |
321 |
< |
* |
448 |
< |
* @return the name of this class and its CVS revision |
449 |
< |
*/ |
450 |
< |
public String toString() { |
451 |
< |
return FormatName.getName( |
452 |
< |
_name, |
453 |
< |
getClass().getName(), |
454 |
< |
REVISION); |
315 |
> |
public void onMessage(String channel, String sender, String login, String hostname, String message) { |
316 |
> |
if(isForMe(message)) { |
317 |
> |
String response = handleInput(message); |
318 |
> |
if(response != null) { |
319 |
> |
sendMessage(channel, response); |
320 |
> |
} |
321 |
> |
} |
322 |
|
} |
323 |
|
|
324 |
< |
/** |
325 |
< |
* Deals with incoming lines from the server. |
326 |
< |
* |
327 |
< |
* @param line the line to deal with |
328 |
< |
*/ |
329 |
< |
private void handleInput(String line) { |
324 |
> |
public void onPrivateMessage(String sender, String login, String hostname, String message) { |
325 |
> |
String response = handleInput(message); |
326 |
> |
if(response != null) { |
327 |
> |
sendMessage(sender, response); |
328 |
> |
} |
329 |
> |
} |
330 |
> |
|
331 |
> |
public void onNickChange(String oldNick, String login, String hostname, String newNick) { |
332 |
> |
if(oldNick.equals(_nickname)) { |
333 |
> |
_nickname = newNick; |
334 |
> |
} |
335 |
> |
} |
336 |
> |
|
337 |
> |
private String handleInput(String message) { |
338 |
> |
//return "yup, gotcha!"; |
339 |
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
340 |
< |
// if it's a PING... |
341 |
< |
if(line.startsWith("PING")) { |
342 |
< |
// ...send a PONG |
343 |
< |
_socketOut.println("PONG" + line.substring(4)); |
340 |
> |
// setup some String's |
341 |
> |
String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand; |
342 |
> |
String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand; |
343 |
> |
// get the command set |
344 |
> |
try { |
345 |
> |
stopCommand = cp.getProperty(_name, "Alerter.IRC.stopCommand"); |
346 |
> |
startCommand = cp.getProperty(_name, "Alerter.IRC.startCommand"); |
347 |
> |
timeSinceLastAlertCommand = cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"); |
348 |
> |
lastAlertCommand = cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"); |
349 |
> |
joinCommand = cp.getProperty(_name, "Alerter.IRC.joinCommand"); |
350 |
> |
nickChangeCommand = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"); |
351 |
> |
versionCommand = cp.getProperty(_name, "Alerter.IRC.versionCommand"); |
352 |
> |
helpCommand = cp.getProperty(_name, "Alerter.IRC.helpCommand"); |
353 |
> |
statCommand = cp.getProperty(_name, "Alerter.IRC.statCommand"); |
354 |
> |
uptimeCommand = cp.getProperty(_name, "Alerter.IRC.uptimeCommand"); |
355 |
> |
} catch (PropertyNotFoundException e) { |
356 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
357 |
> |
// lets bail from handling this line... |
358 |
> |
// ...it's gonna be hard without a command set! |
359 |
> |
return null; |
360 |
|
} |
361 |
< |
// see if it's for us |
362 |
< |
else if(getMsg(line).startsWith(_nickname+",") || getMsg(line).startsWith(_nickname+":") || getMsg(line).startsWith(_nickname+" ")) { |
363 |
< |
// setup some String's |
364 |
< |
String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand; |
365 |
< |
String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand; |
366 |
< |
// get the command set |
367 |
< |
try { |
368 |
< |
stopCommand = cp.getProperty(_name, "Alerter.IRC.stopCommand"); |
369 |
< |
startCommand = cp.getProperty(_name, "Alerter.IRC.startCommand"); |
370 |
< |
timeSinceLastAlertCommand = cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"); |
371 |
< |
lastAlertCommand = cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"); |
372 |
< |
joinCommand = cp.getProperty(_name, "Alerter.IRC.joinCommand"); |
373 |
< |
nickChangeCommand = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"); |
374 |
< |
versionCommand = cp.getProperty(_name, "Alerter.IRC.versionCommand"); |
375 |
< |
helpCommand = cp.getProperty(_name, "Alerter.IRC.helpCommand"); |
484 |
< |
statCommand = cp.getProperty(_name, "Alerter.IRC.statCommand"); |
485 |
< |
uptimeCommand = cp.getProperty(_name, "Alerter.IRC.uptimeCommand"); |
486 |
< |
} catch (PropertyNotFoundException e) { |
487 |
< |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
488 |
< |
// lets bail from handling this line... |
489 |
< |
// ...it's gonna be hard without a command set! |
490 |
< |
return; |
361 |
> |
|
362 |
> |
if(message.indexOf(stopCommand)!=-1) { |
363 |
> |
_active = false; |
364 |
> |
return "alerts have been stopped"; |
365 |
> |
} |
366 |
> |
else if(message.indexOf(startCommand)!=-1) { |
367 |
> |
_active = true; |
368 |
> |
return "alerts have been activated"; |
369 |
> |
} |
370 |
> |
// this needs to go here if it contains the same words as the lastAlertCommand |
371 |
> |
else if(message.indexOf(timeSinceLastAlertCommand)!=-1) { |
372 |
> |
if(_lastAlertTime != -1) { |
373 |
> |
long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000; |
374 |
> |
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
375 |
> |
return "I last sent an alert "+uptimeText+ " ago"; |
376 |
|
} |
377 |
< |
|
378 |
< |
// we have a message for us |
494 |
< |
String message = getMsg(line).substring(_nickname.length()); |
495 |
< |
if(message.indexOf(stopCommand)!=-1) { |
496 |
< |
_active = false; |
497 |
< |
sendMsg(getMsgSender(line)+", alerts have been stopped"); |
377 |
> |
else { |
378 |
> |
return "I've never sent an alert!"; |
379 |
|
} |
380 |
< |
else if(message.indexOf(startCommand)!=-1) { |
381 |
< |
_active = true; |
382 |
< |
sendMsg(getMsgSender(line)+", alerts have been activated"); |
380 |
> |
} |
381 |
> |
else if(message.indexOf(lastAlertCommand)!=-1) { |
382 |
> |
if(_lastAlertTime != -1) { |
383 |
> |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime)); |
384 |
> |
return "last alert was at "+date+"; "+_lastAlert; |
385 |
|
} |
386 |
< |
// this needs to go here if it contains the same words as the lastAlertCommand |
387 |
< |
else if(message.indexOf(timeSinceLastAlertCommand)!=-1) { |
505 |
< |
if(_lastAlertTime != -1) { |
506 |
< |
long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000; |
507 |
< |
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
508 |
< |
sendMsg(getMsgSender(line)+", I last sent an alert "+uptimeText+ " ago"); |
509 |
< |
} |
510 |
< |
else { |
511 |
< |
sendMsg(getMsgSender(line)+", I've never sent an alert!"); |
512 |
< |
} |
386 |
> |
else { |
387 |
> |
return "I've never sent an alert!"; |
388 |
|
} |
389 |
< |
else if(message.indexOf(lastAlertCommand)!=-1) { |
390 |
< |
if(_lastAlertTime != -1) { |
391 |
< |
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime)); |
392 |
< |
sendMsg(getMsgSender(line)+", last alert was at "+date+"; "+_lastAlert); |
393 |
< |
} |
394 |
< |
else { |
395 |
< |
sendMsg(getMsgSender(line)+", I've never sent an alert!"); |
396 |
< |
} |
522 |
< |
|
389 |
> |
|
390 |
> |
} |
391 |
> |
else if(message.indexOf(joinCommand)!=-1) { |
392 |
> |
String joinCmd = joinCommand; |
393 |
> |
String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1); |
394 |
> |
int endOfChan = newChan.indexOf(" "); |
395 |
> |
if(endOfChan == -1) { |
396 |
> |
endOfChan = newChan.length(); |
397 |
|
} |
398 |
< |
else if(message.indexOf(joinCommand)!=-1) { |
399 |
< |
String joinCmd = joinCommand; |
400 |
< |
String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1); |
401 |
< |
int endOfChan = newChan.indexOf(" "); |
402 |
< |
if(endOfChan == -1) { |
403 |
< |
endOfChan = newChan.length(); |
530 |
< |
} |
531 |
< |
newChan = newChan.substring(0, endOfChan); |
532 |
< |
sendMsg(getMsgSender(line)+", okay, I'm off to "+newChan); |
533 |
< |
_socketOut.println("PART "+_channel); |
534 |
< |
_socketOut.println("JOIN "+newChan); |
398 |
> |
newChan = newChan.substring(0, endOfChan); |
399 |
> |
if(newChan.equals(_channel)) { |
400 |
> |
return "I'm already on "+newChan+"!"; |
401 |
> |
} else { |
402 |
> |
partChannel(_channel); |
403 |
> |
joinChannel(newChan); |
404 |
|
_channel = newChan; |
405 |
+ |
return null; // ??? |
406 |
|
} |
407 |
< |
else if(message.indexOf(nickChangeCommand)!=-1) { |
408 |
< |
String nickChangeCmd = nickChangeCommand; |
409 |
< |
String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1); |
410 |
< |
int endOfNick = newNick.indexOf(" "); |
411 |
< |
if(endOfNick == -1) { |
412 |
< |
endOfNick = newNick.length(); |
413 |
< |
} |
544 |
< |
newNick = newNick.substring(0, endOfNick); |
545 |
< |
sendMsg(getMsgSender(line)+", okay, changing my nickname to "+newNick); |
546 |
< |
_socketOut.println("NICK "+newNick); |
547 |
< |
_nickname = newNick; |
407 |
> |
} |
408 |
> |
else if(message.indexOf(nickChangeCommand)!=-1) { |
409 |
> |
String nickChangeCmd = nickChangeCommand; |
410 |
> |
String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1); |
411 |
> |
int endOfNick = newNick.indexOf(" "); |
412 |
> |
if(endOfNick == -1) { |
413 |
> |
endOfNick = newNick.length(); |
414 |
|
} |
415 |
< |
else if(message.indexOf(versionCommand)!=-1) { |
416 |
< |
sendMsg(getMsgSender(line)+", I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot"); |
417 |
< |
} |
418 |
< |
else if(message.indexOf(helpCommand)!=-1) { |
419 |
< |
sendPrivMsg(getMsgSender(line), "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2)); |
554 |
< |
sendPrivMsg(getMsgSender(line), "I understand the following commands;"); |
555 |
< |
sendPrivMsg(getMsgSender(line), stopCommand); |
556 |
< |
sendPrivMsg(getMsgSender(line), startCommand); |
557 |
< |
sendPrivMsg(getMsgSender(line), lastAlertCommand); |
558 |
< |
sendPrivMsg(getMsgSender(line), joinCommand); |
559 |
< |
sendPrivMsg(getMsgSender(line), nickChangeCommand); |
560 |
< |
sendPrivMsg(getMsgSender(line), statCommand); |
561 |
< |
sendPrivMsg(getMsgSender(line), uptimeCommand); |
562 |
< |
sendPrivMsg(getMsgSender(line), timeSinceLastAlertCommand); |
563 |
< |
sendPrivMsg(getMsgSender(line), helpCommand); |
564 |
< |
} |
565 |
< |
else if(message.indexOf(statCommand)!=-1) { |
566 |
< |
sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"); |
567 |
< |
} |
568 |
< |
else if(message.indexOf(uptimeCommand)!=-1) { |
569 |
< |
long uptime = (System.currentTimeMillis() - _startTime) / 1000; |
570 |
< |
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
571 |
< |
sendMsg(getMsgSender(line)+", I have been running for "+uptimeText); |
572 |
< |
} |
573 |
< |
else if(message.indexOf("ping")!=-1) { |
574 |
< |
sendMsg("pong"); |
575 |
< |
} |
576 |
< |
else if(message.indexOf("do a jibble dance")!=-1) { |
577 |
< |
// little joke :) |
578 |
< |
sendAction("jives to the funky beat shouting \"ii--screeeaaammm\""); |
579 |
< |
} |
580 |
< |
else { |
581 |
< |
String rejectMessage = NOT_CONFIGURED; |
582 |
< |
try { |
583 |
< |
rejectMessage = cp.getProperty(_name, "Alerter.IRC.rejectMessage"); |
584 |
< |
} catch(PropertyNotFoundException e) { |
585 |
< |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
586 |
< |
} |
587 |
< |
sendMsg(getMsgSender(line)+", "+rejectMessage); |
588 |
< |
} |
415 |
> |
newNick = newNick.substring(0, endOfNick); |
416 |
> |
changeNick(newNick); |
417 |
> |
// should we check this worked? |
418 |
> |
//_nickname = newNick; |
419 |
> |
return null; // ??? |
420 |
|
} |
421 |
< |
else if(line.indexOf(_nickname)!=-1 && line.indexOf(_channel)!=-1 && line.indexOf("KICK")!=-1) { |
422 |
< |
sendPrivMsg(getMsgSender(line), "That wasn't a nice thing to do..."); |
421 |
> |
else if(message.indexOf(versionCommand)!=-1) { |
422 |
> |
return "I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot"; |
423 |
> |
} |
424 |
> |
else if(message.indexOf(helpCommand)!=-1) { |
425 |
> |
return "this will return some help text soon"; |
426 |
> |
/* |
427 |
> |
sendPrivMsg(getMsgSender(line), "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2)); |
428 |
> |
sendPrivMsg(getMsgSender(line), "I understand the following commands;"); |
429 |
> |
sendPrivMsg(getMsgSender(line), stopCommand); |
430 |
> |
sendPrivMsg(getMsgSender(line), startCommand); |
431 |
> |
sendPrivMsg(getMsgSender(line), lastAlertCommand); |
432 |
> |
sendPrivMsg(getMsgSender(line), joinCommand); |
433 |
> |
sendPrivMsg(getMsgSender(line), nickChangeCommand); |
434 |
> |
sendPrivMsg(getMsgSender(line), statCommand); |
435 |
> |
sendPrivMsg(getMsgSender(line), uptimeCommand); |
436 |
> |
sendPrivMsg(getMsgSender(line), timeSinceLastAlertCommand); |
437 |
> |
sendPrivMsg(getMsgSender(line), helpCommand); |
438 |
> |
*/ |
439 |
> |
} |
440 |
> |
else if(message.indexOf(statCommand)!=-1) { |
441 |
> |
return "I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"; |
442 |
> |
} |
443 |
> |
else if(message.indexOf(uptimeCommand)!=-1) { |
444 |
> |
long uptime = (System.currentTimeMillis() - _startTime) / 1000; |
445 |
> |
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
446 |
> |
return "I have been running for "+uptimeText; |
447 |
> |
} |
448 |
> |
else if(message.indexOf("ping")!=-1) { |
449 |
> |
return "pong"; |
450 |
> |
} |
451 |
> |
else if(message.indexOf("do a jibble dance")!=-1) { |
452 |
> |
// little joke :) |
453 |
> |
return "jives to the funky beat shouting \"ii--screeeaaammm\""; |
454 |
> |
} |
455 |
> |
|
456 |
> |
else { |
457 |
> |
String rejectMessage = NOT_CONFIGURED; |
458 |
|
try { |
459 |
< |
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
459 |
> |
rejectMessage = cp.getProperty(_name, "Alerter.IRC.rejectMessage"); |
460 |
|
} catch(PropertyNotFoundException e) { |
461 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
462 |
|
} |
463 |
< |
_socketOut.println("JOIN "+_channel); |
463 |
> |
return rejectMessage; |
464 |
|
} |
465 |
|
} |
466 |
|
|
467 |
< |
/** |
468 |
< |
* Strips the header from a message line |
469 |
< |
* |
470 |
< |
* @param line the line to strip |
471 |
< |
* @return the message from the line |
472 |
< |
*/ |
473 |
< |
private String getMsg(String line) { |
474 |
< |
String result = ""; |
609 |
< |
if(line.indexOf("PRIVMSG")!=-1) { |
610 |
< |
int firstColon = line.indexOf(":"); |
611 |
< |
if(firstColon != -1) { |
612 |
< |
int secondColon = line.indexOf(":", firstColon+1); |
613 |
< |
if(secondColon != -1) { |
614 |
< |
result = line.substring(secondColon+1); |
615 |
< |
} |
616 |
< |
} |
467 |
> |
private boolean isForMe(String message) { |
468 |
> |
// change this! |
469 |
> |
String nick = _nickname.toLowerCase(); |
470 |
> |
String msg = message.toLowerCase(); |
471 |
> |
if(msg.startsWith(nick + ", ") || |
472 |
> |
msg.startsWith(nick + ": ") || |
473 |
> |
msg.startsWith(nick + " ")) { |
474 |
> |
return true; |
475 |
|
} |
476 |
< |
return result; |
476 |
> |
else { |
477 |
> |
return false; |
478 |
> |
} |
479 |
|
} |
480 |
|
|
481 |
< |
/** |
482 |
< |
* Finds out the sender of the message |
483 |
< |
* |
484 |
< |
* @param line the line to look for a sender in |
485 |
< |
* @return the sender |
486 |
< |
*/ |
487 |
< |
private String getMsgSender(String line) { |
488 |
< |
String result = ""; |
489 |
< |
int colon = line.indexOf(":"); |
490 |
< |
int excl = line.indexOf("!"); |
631 |
< |
if(colon!=-1 && excl!=-1) { |
632 |
< |
result = line.substring(colon+1, excl); |
481 |
> |
private void reconnectSleep() { |
482 |
> |
int delayTime = 0; |
483 |
> |
try { |
484 |
> |
delayTime = Integer.parseInt(ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.reconnectDelay")); |
485 |
> |
} catch (NumberFormatException e) { |
486 |
> |
delayTime = DEFAULT_RECONNECT_DELAY; |
487 |
> |
_logger.write(this.toString(), Logger.WARNING, "Erronous Alerter.IRC.reconnectDelay value in configuration using default of " + delayTime + " seconds"); |
488 |
> |
} catch (PropertyNotFoundException e) { |
489 |
> |
delayTime = DEFAULT_RECONNECT_DELAY; |
490 |
> |
_logger.write(this.toString(), Logger.WARNING, "Alerter.IRC.reconnectDelay value unavailable using default of " + delayTime + " seconds"); |
491 |
|
} |
492 |
< |
return result; |
492 |
> |
_logger.write(this.toString(), Logger.ERROR, "Waiting "+delayTime+" seconds for reconnect..."); |
493 |
> |
try { |
494 |
> |
Thread.sleep(delayTime * 1000); |
495 |
> |
} catch (InterruptedException e) {} |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
< |
* The socket connected to the server |
499 |
> |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
500 |
> |
* method to provide clean logging (every class should have this). |
501 |
> |
* |
502 |
> |
* This uses the uk.org.iscream.cms.server.util.NameFormat class |
503 |
> |
* to format the toString() |
504 |
> |
* |
505 |
> |
* @return the name of this class and its CVS revision |
506 |
|
*/ |
507 |
< |
private Socket _socket; |
508 |
< |
|
509 |
< |
/** |
510 |
< |
* The writer |
511 |
< |
*/ |
512 |
< |
private PrintWriter _socketOut; |
646 |
< |
|
647 |
< |
/** |
648 |
< |
* The reader |
649 |
< |
*/ |
650 |
< |
private BufferedReader _socketIn; |
507 |
> |
public String toString() { |
508 |
> |
return FormatName.getName( |
509 |
> |
_name, |
510 |
> |
getClass().getName(), |
511 |
> |
REVISION); |
512 |
> |
} |
513 |
|
|
514 |
|
/** |
515 |
|
* Just a reminder to what channel we're on... |