class Mib: """ Main class which handles most of the core functionality. """ def __init__(self): """ Initialize variables and read config. """ sys.path.append("plugins") self.loaded_plugins = {} # plugin name : module self.cmd_callbacks = {} # command : set(function) self.privmsg_cmd_callbacks = {} # command : set(function) self.command_masks = {} # command : list(regexp) self.plugins = set(config.LOAD_PLUGINS) self.cmd_prefixes = set(config.CMD_PREFIXES) self.nick = config.NICK self.username = config.USERNAME self.realname = config.REALNAME self.server, self.port = config.SERVER self.channels = config.CHANNELS self.socket = IrcSocket(self.server, self.port, self.nick, self.username, self.realname) self.socket.register_readline_cb(self.parse_line) for channel in self.channels: self.socket.join(channel) for plugin in self.plugins: print self.load_plugin(plugin)[1] def run(self): """ Start socket's main loop. """ self.socket.run() def clean(self): for plugin in self.loaded_plugins.itervalues(): plugin.clean() def parse_line(self, line): """ Parse line and call callbacks registered for command. """ print line parsed = parse(line) if not parsed: print 'Unable to parse line: "%s"' % (line) return # call registered functions for function in self.cmd_callbacks.get(parsed.cmd, ()): try: function(parsed) except Exception, e: print "Error from function", repr(function), ":", e # call registered privmsg functions with pre-parsed line if parsed.cmd == "PRIVMSG": cmd_prefix = parsed.postfix.split(" ", 1)[0] postfix = parsed.postfix[len(cmd_prefix) :].lstrip() if cmd_prefix in self.cmd_prefixes: print "Found command prefix", cmd_prefix cmd = postfix.lstrip().split(" ", 1)[0] postfix = postfix[len(cmd) :].lstrip() stripped_parsed = IRCMsg(parsed.prefix, parsed.cmd, parsed.params, postfix) print "stripped_parsed = ", stripped_parsed print "Searching for command", cmd for function in self.privmsg_cmd_callbacks.get(cmd, ()): run = False if cmd not in self.command_masks: run = True else: print "There are limitations for this command" for regexp in self.command_masks[cmd]: print "Matching %s to %s" % (parsed.prefix, regexp.pattern) if regexp.match(parsed.prefix): run = True break if run: try: print "Executing command %s" % cmd function(stripped_parsed) except Exception, e: print "Error from function", repr(function), ":", e