]> jfr.im git - erebus.git/blobdiff - bot.py
track and report uptime in !resources
[erebus.git] / bot.py
diff --git a/bot.py b/bot.py
index e86fdbd5f13fcc4fb4b5a0c2677576cc0b57ecff..1f8c710157444eb7038ac5918caeee6fc14db48f 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -1,4 +1,5 @@
 #!/usr/bin/python
+# vim: fileencoding=utf-8
 
 # Erebus IRC bot - Author: John Runyon
 # "Bot" and "BotConnection" classes (handling a specific "arm")
@@ -10,13 +11,19 @@ MAXLEN = 400 # arbitrary max length of a command generated by Bot.msg functions
 
 if sys.version_info.major < 3:
        timerbase = threading._Timer
+       stringbase = basestring
 else:
        timerbase = threading.Timer
+       stringbase = str
 class MyTimer(timerbase):
        def __init__(self, *args, **kwargs):
                timerbase.__init__(self, *args, **kwargs)
                self.daemon = True
 
+if sys.version_info.major < 3:
+       stringbase = basestring
+else:
+       stringbase = str
 
 #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')}
 class Bot(object):
@@ -280,19 +287,20 @@ class Bot(object):
                if triggerused: msg = msg[len(self.parent.trigger):]
                pieces = msg.split()
 
+               if len(pieces) == 0:
+                       return
+
                if target != self.nick: # message was sent to a channel
                        try:
-                               if msg.startswith('*'): # message may be addressed to bot by "*BOTNICK" trigger?
-                                       if pieces[0][1:].lower() == self.nick.lower():
-                                               pieces.pop(0) # command actually starts with next word
-                                               msg = ' '.join(pieces) # command actually starts with next word
-                                               triggerused = True
+                               if pieces[0][:-1].lower() == self.nick.lower() and (pieces[0][-1] == ":" or pieces[0][-1] == ","):
+                                       pieces.pop(0) # command actually starts with next word
+                                       if len(pieces) == 0: # is there still anything left?
+                                               return
+                                       msg = ' '.join(pieces)
+                                       triggerused = True
                        except IndexError:
                                return # "message" is empty
 
-               if len(pieces) == 0:
-                       return
-
                if len(pieces) > 1:
                        chanword = pieces[1]
                        if chanword.startswith('#'):
@@ -328,6 +336,8 @@ class Bot(object):
                                                cbret = callback(self, user, chan, target, *pieces[1:])
                                                if cbret is NotImplemented:
                                                        raise NotImplementedError
+                                               elif isinstance(cbret, stringbase):
+                                                       self.reply(chan, user, cbret)
                                        except NotImplementedError:
                                                self.msg(user, "Command not implemented.")
                                        except Exception:
@@ -352,6 +362,13 @@ class Bot(object):
 #                      print "%09.3f %s [!] %s" % (time.time() % 100000, self.nick, "!!! NOMSG")
                        __import__('traceback').print_stack()
 
+
+       def reply(self, chan, user, msg):
+               if chan is not None:
+                       self.msg(chan, "%s: %s" % (user, msg))
+               else:
+                       self.msg(user, msg)
+
        def msg(self, target, msg):
                if self.parent.cfg.getboolean('erebus', 'nofakelag'): return self.fastmsg(target, msg)
                cmd = self._formatmsg(target, msg)