1 |
+ |
/* |
2 |
+ |
* i-scream central monitoring system |
3 |
+ |
* Copyright (C) 2000-2002 i-scream |
4 |
+ |
* |
5 |
+ |
* This program is free software; you can redistribute it and/or |
6 |
+ |
* modify it under the terms of the GNU General Public License |
7 |
+ |
* as published by the Free Software Foundation; either version 2 |
8 |
+ |
* of the License, or (at your option) any later version. |
9 |
+ |
* |
10 |
+ |
* This program is distributed in the hope that it will be useful, |
11 |
+ |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
+ |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
+ |
* GNU General Public License for more details. |
14 |
+ |
* |
15 |
+ |
* You should have received a copy of the GNU General Public License |
16 |
+ |
* along with this program; if not, write to the Free Software |
17 |
+ |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
+ |
*/ |
19 |
+ |
|
20 |
|
//---PACKAGE DECLARATION--- |
21 |
|
package uk.org.iscream.cms.server.client.alerters; |
22 |
|
|
243 |
|
* |
244 |
|
* @throws IOException if there is any problem initialising |
245 |
|
*/ |
246 |
< |
public void init() throws IOException { |
246 |
> |
private void init() throws IOException { |
247 |
|
_logger.write(this.toString(), Logger.DEBUG, "Initialising IRCBot..."); |
248 |
|
|
249 |
|
// get a hook on the configuration system |
264 |
|
} |
265 |
|
|
266 |
|
// get hold of the user details and nickname list |
267 |
< |
String user, comment, finger, nickList; |
267 |
> |
String user, nickList; |
268 |
|
try { |
269 |
|
user = cp.getProperty(_name, "Alerter.IRC.user"); |
251 |
– |
comment = cp.getProperty(_name, "Alerter.IRC.comment"); |
252 |
– |
finger = cp.getProperty(_name, "Alerter.IRC.finger"); |
270 |
|
nickList = cp.getProperty(_name, "Alerter.IRC.nickList"); |
271 |
|
} catch (PropertyNotFoundException e) { |
272 |
|
_logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e); |
273 |
|
throw new IOException("Can't get user/nickname details due to configuration error"); |
274 |
|
} |
275 |
|
|
276 |
+ |
// get hold of comment and finger information |
277 |
+ |
// -- we're not too fussed if these aren't set |
278 |
+ |
String comment = "Alerter.IRC.comment is undefined"; |
279 |
+ |
String finger = "Alerter.IRC.finger is undefined"; |
280 |
+ |
try { |
281 |
+ |
comment = cp.getProperty(_name, "Alerter.IRC.comment"); |
282 |
+ |
finger = cp.getProperty(_name, "Alerter.IRC.finger"); |
283 |
+ |
} catch (PropertyNotFoundException e) { |
284 |
+ |
_logger.write(this.toString(), Logger.WARNING, "Configuration warning, using default: "+e); |
285 |
+ |
} |
286 |
+ |
|
287 |
|
// put these details into the bot |
288 |
|
this.setLogin(user); |
289 |
< |
this.setVersion(comment); |
289 |
> |
this.setVersion("[" + getRevision() + "] " + comment); |
290 |
|
this.setFinger(finger); |
291 |
|
|
292 |
|
// attempt to connect, trying each nickname |
354 |
|
* |
355 |
|
* @param message The message to send |
356 |
|
*/ |
357 |
< |
public void sendMessage(String message) { |
357 |
> |
private void sendMessage(String message) { |
358 |
|
sendMessage(_channel, message); |
359 |
|
} |
360 |
|
|
392 |
|
* @param message The message sent |
393 |
|
*/ |
394 |
|
public void onMessage(String channel, String sender, String login, String hostname, String message) { |
395 |
< |
if(isForMe(message)) { |
396 |
< |
handleInput(message, channel); |
395 |
> |
String trimmedMessage = isForMe(message); |
396 |
> |
// if trimmedMessage is null, it's not for us |
397 |
> |
if(trimmedMessage != null) { |
398 |
> |
handleInput(trimmedMessage, channel); |
399 |
|
} |
400 |
|
} |
401 |
|
|
429 |
|
public void onNickChange(String oldNick, String login, String hostname, String newNick) { |
430 |
|
if(oldNick.equals(_nickname)) { |
431 |
|
_nickname = newNick; |
432 |
+ |
// tell the underlying pircbot that our nick has changed too :) |
433 |
+ |
setName(newNick); |
434 |
|
} |
435 |
|
} |
436 |
|
|
469 |
|
* This method handles input directed to us, and |
470 |
|
* responds accordingly if required. |
471 |
|
* |
440 |
– |
* nb. matching user input is quite bad right now, |
441 |
– |
* and should be improved one day. |
442 |
– |
* |
472 |
|
* @param message the message from someone |
473 |
|
* @param source where the message came from, so we know where to send the response |
474 |
|
*/ |
497 |
|
return; |
498 |
|
} |
499 |
|
|
500 |
< |
// see if the message matches (loosely!) any |
472 |
< |
// of our known commands |
473 |
< |
if(message.indexOf(stopCommand) != -1) { |
500 |
> |
if(message.equalsIgnoreCase(stopCommand)) { |
501 |
|
_active = false; |
502 |
|
sendMessage(source, "alerts have been stopped"); |
503 |
|
} |
504 |
< |
else if(message.indexOf(startCommand) != -1) { |
504 |
> |
else if(message.equalsIgnoreCase(startCommand)) { |
505 |
|
_active = true; |
506 |
|
sendMessage(source, "alerts have been activated"); |
507 |
|
} |
508 |
|
// this needs to go before lastAlertCommand if it contains |
509 |
|
// the same words as the lastAlertCommand. |
510 |
< |
else if(message.indexOf(timeSinceLastAlertCommand) != -1) { |
510 |
> |
else if(message.equalsIgnoreCase(timeSinceLastAlertCommand)) { |
511 |
|
if(_lastAlertTime != -1) { |
512 |
|
long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000; |
513 |
|
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
517 |
|
sendMessage(source, "I've never sent an alert!"); |
518 |
|
} |
519 |
|
} |
520 |
< |
else if(message.indexOf(lastAlertCommand) != -1) { |
520 |
> |
else if(message.equalsIgnoreCase(lastAlertCommand)) { |
521 |
|
if(_lastAlertTime != -1) { |
522 |
|
String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime)); |
523 |
|
sendMessage(source, "last alert was at "+date+"; "+_lastAlert); |
527 |
|
} |
528 |
|
|
529 |
|
} |
530 |
< |
else if(message.indexOf(joinCommand) != -1) { |
530 |
> |
else if(message.toLowerCase().startsWith(joinCommand.toLowerCase())) { |
531 |
|
String joinCmd = joinCommand; |
532 |
|
String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1); |
533 |
|
int endOfChan = newChan.indexOf(" "); |
543 |
|
_channel = newChan; |
544 |
|
} |
545 |
|
} |
546 |
< |
else if(message.indexOf(nickChangeCommand) != -1) { |
546 |
> |
else if(message.toLowerCase().startsWith(nickChangeCommand.toLowerCase())) { |
547 |
|
String nickChangeCmd = nickChangeCommand; |
548 |
|
String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1); |
549 |
|
int endOfNick = newNick.indexOf(" "); |
553 |
|
newNick = newNick.substring(0, endOfNick); |
554 |
|
changeNick(newNick); |
555 |
|
} |
556 |
< |
else if(message.indexOf(versionCommand) != -1) { |
557 |
< |
sendMessage(source, "I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot"); |
556 |
> |
else if(message.equalsIgnoreCase(versionCommand)) { |
557 |
> |
sendMessage(source, "I am version " + getRevision() + " of the i-scream alerting bot"); |
558 |
|
} |
559 |
< |
else if(message.indexOf(helpCommand) != -1) { |
559 |
> |
else if(message.equalsIgnoreCase(helpCommand)) { |
560 |
|
sendMessage(source, "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2)); |
561 |
|
sendMessage(source, "I understand the following commands;"); |
562 |
|
sendMessage(source, stopCommand); |
569 |
|
sendMessage(source, timeSinceLastAlertCommand); |
570 |
|
sendMessage(source, helpCommand); |
571 |
|
} |
572 |
< |
else if(message.indexOf(statCommand) != -1) { |
572 |
> |
else if(message.equalsIgnoreCase(statCommand)) { |
573 |
|
sendMessage(source, "I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!"); |
574 |
|
} |
575 |
< |
else if(message.indexOf(uptimeCommand) != -1) { |
575 |
> |
else if(message.equalsIgnoreCase(uptimeCommand)) { |
576 |
|
long uptime = (System.currentTimeMillis() - _startTime) / 1000; |
577 |
|
String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs"); |
578 |
|
sendMessage(source, "I have been running for "+uptimeText); |
579 |
|
} |
580 |
< |
else if(message.indexOf("ping") != -1) { |
580 |
> |
else if(message.equalsIgnoreCase("ping")) { |
581 |
|
sendMessage(source, "pong"); |
582 |
|
} |
583 |
< |
else if(message.indexOf("do a jibble dance") != -1) { |
583 |
> |
else if(message.equalsIgnoreCase("do a jibble dance")) { |
584 |
|
// little joke :) |
585 |
|
sendAction(source, "jives to the funky beat shouting \"ii--screeeaaammm\""); |
586 |
|
} |
599 |
|
* Quick method to check if the message appears |
600 |
|
* to be directed at us. We simply check to see |
601 |
|
* if it starts with our nick in some fashion. |
602 |
+ |
* This will return null if the message is not |
603 |
+ |
* for us - thus allowing this method to perform |
604 |
+ |
* two tasks at once. |
605 |
|
* |
606 |
|
* @param message the message to check |
607 |
< |
* @return if the message is for us |
607 |
> |
* @return the message (with our nick removed), or null if it's not for us. |
608 |
|
*/ |
609 |
< |
private boolean isForMe(String message) { |
609 |
> |
private String isForMe(String message) { |
610 |
> |
// change to lower case to remove |
611 |
> |
// case ambiguities |
612 |
|
String nick = _nickname.toLowerCase(); |
613 |
|
String msg = message.toLowerCase(); |
614 |
|
if(msg.startsWith(nick + ", ") || |
615 |
|
msg.startsWith(nick + ": ") || |
616 |
|
msg.startsWith(nick + " ")) { |
617 |
< |
return true; |
617 |
> |
// NOTE that the 1 here is hardcoded to be the length |
618 |
> |
// of the above 'extra' character (eg , or :) |
619 |
> |
return message.substring(nick.length()+1).trim(); |
620 |
|
} |
621 |
|
else { |
622 |
< |
return false; |
622 |
> |
return null; |
623 |
|
} |
624 |
|
} |
625 |
|
|
643 |
|
try { |
644 |
|
Thread.sleep(delayTime * 1000); |
645 |
|
} catch (InterruptedException e) {} |
646 |
+ |
} |
647 |
+ |
|
648 |
+ |
/** |
649 |
+ |
* Returns the revision of the bot. |
650 |
+ |
* |
651 |
+ |
* @return the revision of this bot |
652 |
+ |
*/ |
653 |
+ |
private String getRevision() { |
654 |
+ |
return REVISION.substring(11, REVISION.length()-2); |
655 |
|
} |
656 |
|
|
657 |
|
/** |