-#+print "This is mod-python.py"
-#+
-#+caller = ''
-#+target = ''
-#+service = ''
-#+
-#+def handle_init():
-#+ print "This is x3init in python"
-#+ return 0
-#+
-#+
-#+def handle_join(channel, nick):
-#+ global caller, target, service
-#+ print "This is handle_join() in python"
-#+ user = svc.get_user(nick)
-#+ svc.send_target_privmsg("x3", channel, "test %s "%(service))
-#+ svc.send_target_privmsg("x3", channel, " %s joined %s: %s"%(nick, channel, user))
-#+ svc.send_target_privmsg("x3", channel, "Welcome to %s %s (*%s)! Your IP is %s. You are now in %d channels!"%(channel, user['nick'], user['account'], user['ip'], len(user['channels']) ))
-#+ chan = svc.get_channel(channel)
-#+ svc.send_target_privmsg("x3", channel, "Channel details: %s"%chan)
-#+ return 0
-#+
-#+def run(command):
-#+ eval(command)
-#+ return 0
-#+
-#+def reply(message):
-#+ global caller, target, service
-#+ print "DEBUG: %s / %s / %s : %s" %(caller, target, service, message);
-#+ if(len(target) > 0):
-#+ svc.send_target_privmsg(service, target, "%s: %s"%(caller, message));
-#+ else:
-#+ svc.send_target_notice(service, caller, message);
-#+ return 0
+ def __init__(self, event, method, filter, data):
+ self.event = event
+ self.method = method
+ self.filter = filter
+ self.data = data
+
+ def event_is(self, event, evdata):
+ if(self.event == event):
+ for i in range(len(self.filter)):
+ if( self.filter[i] != None
+ and self.filter[i] != evdata[i]): # should be case insensitive? or how to compare?
+ #print "DEBUG: rejecting event, %s is not %s"%(self.filter[i], evdata[i])
+ return False
+ return True
+ else:
+ return False
+
+ def trigger(self, irc, args):
+ #print "DEBUG: Triggering %s event. with '%s' arguments."%(self.event, args)
+ self.method(irc, *args)
+
+ def __init__(self, handler):
+ """ Constructor """
+ #print "DEBUG: constructor for plugins initing"
+ self.handler = handler
+
+ def init(self):
+ #print "DEBUG: in plugins.init()"
+ self.load("annoy")
+ self.load("hangman")
+
+ def addhook(self, event, method, filter=[None], data=None):
+ #print "DEBUG: Adding hook for %s."%event
+ self.hooks.append(self.hook(event, method, filter, data))
+
+ def findhooksforevent(self, event, data):
+ ret = []
+ #print "DEBUG: findhooksforevent() looking..."
+ for hook in self.hooks:
+ #print "DEBUG: looking at a %s hook..."%hook.event
+ if(hook.event_is(event, data)):
+ ret.append(hook)
+ return ret
+
+ def callhandler(self, event, irc, filter, args):
+ for hook in self.findhooksforevent(event, filter):
+ if(hook.trigger(irc, args)):
+ return 1
+ return 0
+
+ def load(self, name):
+ """ Loads a plugin by name """
+ mod_name = "plugins.%s"%name
+ need_reload = False
+ if(sys.modules.has_key(mod_name)):
+ need_reload = True
+ #TODO: try to catch compile errors etc.
+
+ if(need_reload == False):
+ __import__(mod_name)
+ module = sys.modules[mod_name]
+ if(need_reload == True):
+ reload(module) # to ensure its read fresh
+ Class = module.Class
+ pluginObj = Class(self.handler, irc())
+ self.loaded_plugins[mod_name] = pluginObj
+ return True
+