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--- |
64 |
|
// only send alerts if we're active |
65 |
|
if(_active) { |
66 |
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
67 |
< |
String levelName = cp.getProperty(_name, "Alerter.IRC.level"); |
67 |
> |
|
68 |
> |
String levelName; |
69 |
> |
try { |
70 |
> |
levelName = cp.getProperty(_name, "Alerter.IRC.level"); |
71 |
> |
} catch (PropertyNotFoundException e) { |
72 |
> |
levelName = DEFAULT_LEVEL; |
73 |
> |
_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.getLevel() >= 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 = cp.getProperty(_name, "Alerter.IRC.message"); |
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()); |
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"); |
208 |
|
*/ |
209 |
|
class IRCBot extends Thread { |
210 |
|
|
211 |
+ |
public static final String DEFAULT_STARTUP_NOTICE = "i-scream ircbot starting..."; |
212 |
+ |
|
213 |
|
/** |
214 |
|
* Main thread loop, this part of the class listens for |
215 |
|
* messages from the server, and acts accordingly. At the |
221 |
|
while(run) { |
222 |
|
// flag so we can stop the loop |
223 |
|
boolean doRead = true; |
224 |
+ |
// get the startup notice |
225 |
+ |
String startupNotice; |
226 |
+ |
try { |
227 |
+ |
startupNotice = ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice"); |
228 |
+ |
} catch (PropertyNotFoundException e) { |
229 |
+ |
startupNotice = DEFAULT_STARTUP_NOTICE; |
230 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Configuration error: "+e); |
231 |
+ |
} |
232 |
|
// connect to the IRC server |
233 |
|
try { |
234 |
|
connect(); |
235 |
< |
sendNotice(ConfigurationProxy.getInstance().getProperty(_name, "Alerter.IRC.startupNotice")); |
235 |
> |
sendNotice(startupNotice); |
236 |
|
} catch(IOException e) { |
237 |
|
doRead=false; |
238 |
|
_logger.write(this.toString(), Logger.ERROR, "Error connecting: "+e); |
268 |
|
} catch (NumberFormatException e) { |
269 |
|
delayTime = DEFAULT_RECONNECT_DELAY; |
270 |
|
_logger.write(this.toString(), Logger.WARNING, "Erronous Alerter.IRC.reconnectDelay value in configuration using default of " + delayTime + " seconds"); |
271 |
< |
} catch (org.omg.CORBA.MARSHAL e2) { |
271 |
> |
} catch (PropertyNotFoundException e) { |
272 |
|
delayTime = DEFAULT_RECONNECT_DELAY; |
273 |
|
_logger.write(this.toString(), Logger.WARNING, "Alerter.IRC.reconnectDelay value unavailable using default of " + delayTime + " seconds"); |
274 |
|
} |
338 |
|
public void connect() throws IOException { |
339 |
|
ConfigurationProxy cp = ConfigurationProxy.getInstance(); |
340 |
|
// setup the socket, reader and writer |
341 |
< |
String server = cp.getProperty(_name, "Alerter.IRC.IRCServer"); |
342 |
< |
int port = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.IRCPort")); |
341 |
> |
String server; |
342 |
> |
int port; |
343 |
> |
try { |
344 |
> |
server = cp.getProperty(_name, "Alerter.IRC.IRCServer"); |
345 |
> |
port = Integer.parseInt(cp.getProperty(_name, "Alerter.IRC.IRCPort")); |
346 |
> |
} catch (PropertyNotFoundException e) { |
347 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
348 |
> |
throw new IOException("Can't get irc server details due to configuration error"); |
349 |
> |
} catch (NumberFormatException e) { |
350 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
351 |
> |
throw new IOException("Can't get irc server details due to malformed configuration"); |
352 |
> |
} |
353 |
|
_socket = new Socket(server, port); |
354 |
|
_socketIn = new BufferedReader(new InputStreamReader(_socket.getInputStream())); |
355 |
|
_socketOut = new PrintWriter(_socket.getOutputStream(), true); |
356 |
|
//_socketOut.println("PASS"); |
357 |
|
// send USER details |
358 |
< |
String user = cp.getProperty(_name, "Alerter.IRC.user"); |
359 |
< |
String comment = cp.getProperty(_name, "Alerter.IRC.comment"); |
358 |
> |
String user, comment; |
359 |
> |
try { |
360 |
> |
user = cp.getProperty(_name, "Alerter.IRC.user"); |
361 |
> |
comment = cp.getProperty(_name, "Alerter.IRC.comment"); |
362 |
> |
} catch (PropertyNotFoundException e) { |
363 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
364 |
> |
throw new IOException("Can't get user details due to configuration error"); |
365 |
> |
} |
366 |
|
_socketOut.println("USER "+user+" 8 * :"+comment); |
367 |
|
// attempt to get a nick |
368 |
< |
String nickList = cp.getProperty(_name, "Alerter.IRC.nickList"); |
368 |
> |
String nickList; |
369 |
> |
try { |
370 |
> |
nickList = cp.getProperty(_name, "Alerter.IRC.nickList"); |
371 |
> |
} catch (PropertyNotFoundException e) { |
372 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
373 |
> |
throw new IOException("Can't get nickname due to configuration error"); |
374 |
> |
} |
375 |
|
StringTokenizer st = new StringTokenizer(nickList, ";"); |
376 |
|
boolean ok = false; |
377 |
|
// try until we exhaust our list |
403 |
|
throw new IOException("All nicknames in use"); |
404 |
|
} |
405 |
|
// join the channel |
406 |
< |
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
406 |
> |
try { |
407 |
> |
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
408 |
> |
} catch (PropertyNotFoundException e) { |
409 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
410 |
> |
throw new IOException("Can't get channel name due to configuration error"); |
411 |
> |
} |
412 |
|
_socketOut.println("JOIN "+_channel); |
413 |
|
// allow alerts |
414 |
|
_active = true; |
459 |
|
_socketOut.println("PONG" + line.substring(4)); |
460 |
|
} |
461 |
|
// see if it's for us |
462 |
< |
else if(getMsg(line).startsWith(_nickname+",") |
463 |
< |
|| getMsg(line).startsWith(_nickname+":") |
464 |
< |
|| getMsg(line).startsWith(_nickname+" ")) { |
462 |
> |
else if(getMsg(line).startsWith(_nickname+",") || getMsg(line).startsWith(_nickname+":") || getMsg(line).startsWith(_nickname+" ")) { |
463 |
> |
// setup some String's |
464 |
> |
String stopCommand, startCommand, timeSinceLastAlertCommand, lastAlertCommand, joinCommand; |
465 |
> |
String nickChangeCommand, versionCommand, helpCommand, statCommand, uptimeCommand; |
466 |
> |
// get the command set |
467 |
> |
try { |
468 |
> |
stopCommand = cp.getProperty(_name, "Alerter.IRC.stopCommand"); |
469 |
> |
startCommand = cp.getProperty(_name, "Alerter.IRC.startCommand"); |
470 |
> |
timeSinceLastAlertCommand = cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"); |
471 |
> |
lastAlertCommand = cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"); |
472 |
> |
joinCommand = cp.getProperty(_name, "Alerter.IRC.joinCommand"); |
473 |
> |
nickChangeCommand = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"); |
474 |
> |
versionCommand = cp.getProperty(_name, "Alerter.IRC.versionCommand"); |
475 |
> |
helpCommand = cp.getProperty(_name, "Alerter.IRC.helpCommand"); |
476 |
> |
statCommand = cp.getProperty(_name, "Alerter.IRC.statCommand"); |
477 |
> |
uptimeCommand = cp.getProperty(_name, "Alerter.IRC.uptimeCommand"); |
478 |
> |
} catch (PropertyNotFoundException e) { |
479 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
480 |
> |
// lets bail from handling this line... |
481 |
> |
// ...it's gonna be hard without a command set! |
482 |
> |
return; |
483 |
> |
} |
484 |
> |
|
485 |
|
// we have a message for us |
486 |
|
String message = getMsg(line).substring(_nickname.length()); |
487 |
< |
if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.stopCommand"))!=-1) { |
487 |
> |
if(message.indexOf(stopCommand)!=-1) { |
488 |
|
_active = false; |
489 |
|
sendMsg(getMsgSender(line)+", alerts have been stopped"); |
490 |
|
} |
491 |
< |
else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.startCommand"))!=-1) { |
491 |
> |
else if(message.indexOf(startCommand)!=-1) { |
492 |
|
_active = true; |
493 |
|
sendMsg(getMsgSender(line)+", alerts have been activated"); |
494 |
|
} |
495 |
|
// this needs to go here if it contains the same words as the lastAlertCommand |
496 |
< |
else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand"))!=-1) { |
496 |
> |
else if(message.indexOf(timeSinceLastAlertCommand)!=-1) { |
497 |
|
if(_lastAlertTime != -1) { |
498 |
|
long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000; |
499 |
|
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
503 |
|
sendMsg(getMsgSender(line)+", I've never sent an alert!"); |
504 |
|
} |
505 |
|
} |
506 |
< |
else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.lastAlertCommand"))!=-1) { |
506 |
> |
else if(message.indexOf(lastAlertCommand)!=-1) { |
507 |
|
if(_lastAlertTime != -1) { |
508 |
|
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime)); |
509 |
|
sendMsg(getMsgSender(line)+", last alert was at "+date+"; "+_lastAlert); |
513 |
|
} |
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(joinCommand)!=-1) { |
517 |
> |
String joinCmd = joinCommand; |
518 |
|
String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1); |
519 |
|
int endOfChan = newChan.indexOf(" "); |
520 |
|
if(endOfChan == -1) { |
526 |
|
_socketOut.println("JOIN "+newChan); |
527 |
|
_channel = newChan; |
528 |
|
} |
529 |
< |
else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"))!=-1) { |
530 |
< |
String nickChangeCmd = cp.getProperty(_name, "Alerter.IRC.nickChangeCommand"); |
529 |
> |
else if(message.indexOf(nickChangeCommand)!=-1) { |
530 |
> |
String nickChangeCmd = nickChangeCommand; |
531 |
|
String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1); |
532 |
|
int endOfNick = newNick.indexOf(" "); |
533 |
|
if(endOfNick == -1) { |
538 |
|
_socketOut.println("NICK "+newNick); |
539 |
|
_nickname = newNick; |
540 |
|
} |
541 |
< |
else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.versionCommand"))!=-1) { |
541 |
> |
else if(message.indexOf(versionCommand)!=-1) { |
542 |
|
sendMsg(getMsgSender(line)+", I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot"); |
543 |
|
} |
544 |
< |
else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.helpCommand"))!=-1) { |
544 |
> |
else if(message.indexOf(helpCommand)!=-1) { |
545 |
|
sendPrivMsg(getMsgSender(line), "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2)); |
546 |
|
sendPrivMsg(getMsgSender(line), "I understand the following commands;"); |
547 |
< |
sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.stopCommand")); |
548 |
< |
sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.startCommand")); |
549 |
< |
sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.lastAlertCommand")); |
550 |
< |
sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.joinCommand")); |
551 |
< |
sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.nickChangeCommand")); |
552 |
< |
sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.statCommand")); |
553 |
< |
sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.uptimeCommand")); |
554 |
< |
sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.timeSinceLastAlertCommand")); |
555 |
< |
sendPrivMsg(getMsgSender(line), cp.getProperty(_name, "Alerter.IRC.helpCommand")); |
547 |
> |
sendPrivMsg(getMsgSender(line), stopCommand); |
548 |
> |
sendPrivMsg(getMsgSender(line), startCommand); |
549 |
> |
sendPrivMsg(getMsgSender(line), lastAlertCommand); |
550 |
> |
sendPrivMsg(getMsgSender(line), joinCommand); |
551 |
> |
sendPrivMsg(getMsgSender(line), nickChangeCommand); |
552 |
> |
sendPrivMsg(getMsgSender(line), statCommand); |
553 |
> |
sendPrivMsg(getMsgSender(line), uptimeCommand); |
554 |
> |
sendPrivMsg(getMsgSender(line), timeSinceLastAlertCommand); |
555 |
> |
sendPrivMsg(getMsgSender(line), helpCommand); |
556 |
|
} |
557 |
< |
else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.statCommand"))!=-1) { |
557 |
> |
else if(message.indexOf(statCommand)!=-1) { |
558 |
|
sendMsg(getMsgSender(line)+", I have sent a total of "+_alertCount+" alerts!"); |
559 |
|
} |
560 |
< |
else if(message.indexOf(cp.getProperty(_name, "Alerter.IRC.uptimeCommand"))!=-1) { |
560 |
> |
else if(message.indexOf(uptimeCommand)!=-1) { |
561 |
|
long uptime = (System.currentTimeMillis() - _startTime) / 1000; |
562 |
|
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
563 |
|
sendMsg(getMsgSender(line)+", I have been running for "+uptimeText); |
567 |
|
sendAction("jives to the funky beat shouting \"ii--screeeaaammm\""); |
568 |
|
} |
569 |
|
else { |
570 |
< |
sendMsg(getMsgSender(line)+", "+cp.getProperty(_name, "Alerter.IRC.rejectMessage")); |
570 |
> |
String rejectMessage = NOT_CONFIGURED; |
571 |
> |
try { |
572 |
> |
rejectMessage = cp.getProperty(_name, "Alerter.IRC.rejectMessage"); |
573 |
> |
} catch(PropertyNotFoundException e) { |
574 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
575 |
> |
} |
576 |
> |
sendMsg(getMsgSender(line)+", "+rejectMessage); |
577 |
|
} |
578 |
|
} |
579 |
|
else if(line.indexOf(_nickname)!=-1 && line.indexOf(_channel)!=-1 && line.indexOf("KICK")!=-1) { |
580 |
|
sendPrivMsg(getMsgSender(line), "That wasn't a nice thing to do..."); |
581 |
< |
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
581 |
> |
try { |
582 |
> |
_channel = cp.getProperty(_name, "Alerter.IRC.channel"); |
583 |
> |
} catch(PropertyNotFoundException e) { |
584 |
> |
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
585 |
> |
} |
586 |
|
_socketOut.println("JOIN "+_channel); |
587 |
|
} |
588 |
|
} |