ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java
(Generate patch)

Comparing projects/cms/source/server/uk/org/iscream/cms/server/client/alerters/IRC__Alerter.java (file contents):
Revision 1.30.2.3 by tdb, Tue Feb 5 18:00:15 2002 UTC vs.
Revision 1.31 by tdb, Thu Feb 7 17:15:19 2002 UTC

# Line 224 | Line 224 | public class IRC__Alerter extends AlerterSkeleton {
224           *
225           * @throws IOException if there is any problem initialising
226           */
227 <        public void init() throws IOException {
227 >        private void init() throws IOException {
228              _logger.write(this.toString(), Logger.DEBUG, "Initialising IRCBot...");
229              
230              // get a hook on the configuration system
# Line 245 | Line 245 | public class IRC__Alerter extends AlerterSkeleton {
245              }
246              
247              // get hold of the user details and nickname list
248 <            String user, comment, finger, nickList;
248 >            String user, nickList;
249              try {
250                  user = cp.getProperty(_name, "Alerter.IRC.user");
251                comment = cp.getProperty(_name, "Alerter.IRC.comment");
252                finger = cp.getProperty(_name, "Alerter.IRC.finger");
251                  nickList = cp.getProperty(_name, "Alerter.IRC.nickList");
252              } catch (PropertyNotFoundException e) {
253                  _logger.write(this.toString(), Logger.ERROR, "Configuration error: "+e);
254                  throw new IOException("Can't get user/nickname details due to configuration error");
255              }
256              
257 +            // get hold of comment and finger information
258 +            //  -- we're not too fussed if these aren't set
259 +            String comment = "Alerter.IRC.comment is undefined";
260 +            String finger = "Alerter.IRC.finger is undefined";
261 +            try {
262 +                comment = cp.getProperty(_name, "Alerter.IRC.comment");
263 +                finger = cp.getProperty(_name, "Alerter.IRC.finger");
264 +            } catch (PropertyNotFoundException e) {
265 +                _logger.write(this.toString(), Logger.WARNING, "Configuration warning, using default: "+e);
266 +            }
267 +            
268              // put these details into the bot
269              this.setLogin(user);
270 <            this.setVersion(comment);
270 >            this.setVersion("[" + getRevision() + "] " + comment);
271              this.setFinger(finger);
272              
273              // attempt to connect, trying each nickname
# Line 326 | Line 335 | public class IRC__Alerter extends AlerterSkeleton {
335           *
336           * @param message The message to send
337           */
338 <        public void sendMessage(String message) {
338 >        private void sendMessage(String message) {
339              sendMessage(_channel, message);
340          }
341          
# Line 364 | Line 373 | public class IRC__Alerter extends AlerterSkeleton {
373           * @param message The message sent
374           */
375          public void onMessage(String channel, String sender, String login, String hostname, String message) {
376 <            if(isForMe(message)) {
377 <                handleInput(message, channel);
376 >            String trimmedMessage = isForMe(message);
377 >            // if trimmedMessage is null, it's not for us
378 >            if(trimmedMessage != null) {
379 >                handleInput(trimmedMessage, channel);
380              }
381          }
382          
# Line 399 | Line 410 | public class IRC__Alerter extends AlerterSkeleton {
410          public void onNickChange(String oldNick, String login, String hostname, String newNick) {
411              if(oldNick.equals(_nickname)) {
412                  _nickname = newNick;
413 +                // tell the underlying pircbot that our nick has changed too :)
414 +                setName(newNick);
415              }
416          }
417          
# Line 437 | Line 450 | public class IRC__Alerter extends AlerterSkeleton {
450           * This method handles input directed to us, and
451           * responds accordingly if required.
452           *
440         * nb. matching user input is quite bad right now,
441         *     and should be improved one day.
442         *
453           * @param message the message from someone
454           * @param source where the message came from, so we know where to send the response
455           */
# Line 468 | Line 478 | public class IRC__Alerter extends AlerterSkeleton {
478                  return;
479              }
480              
481 <            // see if the message matches (loosely!) any
472 <            // of our known commands
473 <            if(message.indexOf(stopCommand) != -1) {
481 >            if(message.equalsIgnoreCase(stopCommand)) {
482                  _active = false;
483                  sendMessage(source, "alerts have been stopped");
484              }
485 <            else if(message.indexOf(startCommand) != -1) {
485 >            else if(message.equalsIgnoreCase(startCommand)) {
486                  _active = true;
487                  sendMessage(source, "alerts have been activated");
488              }
489              // this needs to go before lastAlertCommand if it contains
490              // the same words as the lastAlertCommand.
491 <            else if(message.indexOf(timeSinceLastAlertCommand) != -1) {
491 >            else if(message.equalsIgnoreCase(timeSinceLastAlertCommand)) {
492                  if(_lastAlertTime != -1) {
493                      long uptime = (System.currentTimeMillis() - _lastAlertTime) / 1000;
494                      String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
# Line 490 | Line 498 | public class IRC__Alerter extends AlerterSkeleton {
498                      sendMessage(source, "I've never sent an alert!");
499                  }
500              }
501 <            else if(message.indexOf(lastAlertCommand) != -1) {
501 >            else if(message.equalsIgnoreCase(lastAlertCommand)) {
502                  if(_lastAlertTime != -1) {
503                      String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.UK).format(new Date(_lastAlertTime));
504                      sendMessage(source, "last alert was at "+date+"; "+_lastAlert);
# Line 500 | Line 508 | public class IRC__Alerter extends AlerterSkeleton {
508                  }
509                  
510              }
511 <            else if(message.indexOf(joinCommand) != -1) {
511 >            else if(message.toLowerCase().startsWith(joinCommand.toLowerCase())) {
512                  String joinCmd = joinCommand;
513                  String newChan = message.substring(message.indexOf(joinCmd) + joinCmd.length() + 1);
514                  int endOfChan = newChan.indexOf(" ");
# Line 516 | Line 524 | public class IRC__Alerter extends AlerterSkeleton {
524                      _channel = newChan;
525                  }
526              }
527 <            else if(message.indexOf(nickChangeCommand) != -1) {
527 >            else if(message.toLowerCase().startsWith(nickChangeCommand.toLowerCase())) {
528                  String nickChangeCmd = nickChangeCommand;
529                  String newNick = message.substring(message.indexOf(nickChangeCmd) + nickChangeCmd.length() + 1);
530                  int endOfNick = newNick.indexOf(" ");
# Line 526 | Line 534 | public class IRC__Alerter extends AlerterSkeleton {
534                  newNick = newNick.substring(0, endOfNick);
535                  changeNick(newNick);
536              }
537 <            else if(message.indexOf(versionCommand) != -1) {
538 <                sendMessage(source, "I am version "+REVISION.substring(11, REVISION.length() -2)+" of the i-scream alerting bot");
537 >            else if(message.equalsIgnoreCase(versionCommand)) {
538 >                sendMessage(source, "I am version " + getRevision() + " of the i-scream alerting bot");
539              }
540 <            else if(message.indexOf(helpCommand) != -1) {
540 >            else if(message.equalsIgnoreCase(helpCommand)) {
541                  sendMessage(source, "Hello, I am the i-scream alerting bot version "+REVISION.substring(11, REVISION.length() -2));
542                  sendMessage(source, "I understand the following commands;");
543                  sendMessage(source, stopCommand);
# Line 542 | Line 550 | public class IRC__Alerter extends AlerterSkeleton {
550                  sendMessage(source, timeSinceLastAlertCommand);
551                  sendMessage(source, helpCommand);
552              }
553 <            else if(message.indexOf(statCommand) != -1) {
553 >            else if(message.equalsIgnoreCase(statCommand)) {
554                  sendMessage(source, "I have sent a total of "+_alertCount+" alerts, and ignored a total of "+_ignoredCount+"!");
555              }
556 <            else if(message.indexOf(uptimeCommand) != -1) {
556 >            else if(message.equalsIgnoreCase(uptimeCommand)) {
557                  long uptime = (System.currentTimeMillis() - _startTime) / 1000;
558                  String uptimeText = DateUtils.formatTime(uptime, "%DAYS% days, %HOURS% hours, %MINS% mins, and %SECS% secs");
559                  sendMessage(source, "I have been running for "+uptimeText);
560              }
561 <            else if(message.indexOf("ping") != -1) {
561 >            else if(message.equalsIgnoreCase("ping")) {
562                  sendMessage(source, "pong");
563              }
564 <            else if(message.indexOf("do a jibble dance") != -1) {
564 >            else if(message.equalsIgnoreCase("do a jibble dance")) {
565                  // little joke :)
566                  sendAction(source, "jives to the funky beat shouting \"ii--screeeaaammm\"");
567              }
# Line 572 | Line 580 | public class IRC__Alerter extends AlerterSkeleton {
580           * Quick method to check if the message appears
581           * to be directed at us. We simply check to see
582           * if it starts with our nick in some fashion.
583 +         * This will return null if the message is not
584 +         * for us - thus allowing this method to perform
585 +         * two tasks at once.
586           *
587           * @param message the message to check
588 <         * @return if the message is for us
588 >         * @return the message (with our nick removed), or null if it's not for us.
589           */
590 <        private boolean isForMe(String message) {
590 >        private String isForMe(String message) {
591 >            // change to lower case to remove
592 >            // case ambiguities
593              String nick = _nickname.toLowerCase();
594              String msg = message.toLowerCase();
595              if(msg.startsWith(nick + ", ") ||
596                 msg.startsWith(nick + ": ") ||
597                 msg.startsWith(nick + " ")) {
598 <                return true;
598 >                // NOTE that the 1 here is hardcoded to be the length
599 >                // of the above 'extra' character (eg , or :)
600 >                return message.substring(nick.length()+1).trim();
601              }
602              else {
603 <                return false;
603 >                return null;
604              }
605          }
606          
# Line 609 | Line 624 | public class IRC__Alerter extends AlerterSkeleton {
624              try {
625                  Thread.sleep(delayTime * 1000);
626              } catch (InterruptedException e) {}
627 +        }
628 +        
629 +        /**
630 +         * Returns the revision of the bot.
631 +         *
632 +         * @return the revision of this bot
633 +         */
634 +        private String getRevision() {
635 +            return REVISION.substring(11, REVISION.length()-2);
636          }
637          
638          /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines