From 2dc2b6a8a224728b57d33b7699899a7d97ec4f6c Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Fri, 19 Jun 2015 21:50:35 -0500 Subject: [PATCH] remove old IRC bot entirely the porting is complete, everything i care about has been moved to the django-based codebase, and this old junk can finally go IT IS A NEW ERA, one of maintainability and flexible changes. after years of procrastinating, i have finally done this. the future is now --- EXTERNALS | 4 - README | 2 +- ircbot/DrBotIRC.py | 821 ----------- ircbot/Module.py | 328 ----- ircbot/dr.botzo.cfg.example | 23 - ircbot/dr.botzo.py | 129 -- ircbot/extlib/__init__.py | 0 ircbot/extlib/irclib.py | 1711 ---------------------- ircbot/logging.cfg | 58 - scripts/factfile-to-facts.py | 61 - scripts/import-file-into-markov_chain.py | 58 - 11 files changed, 1 insertion(+), 3194 deletions(-) delete mode 100644 EXTERNALS delete mode 100644 ircbot/DrBotIRC.py delete mode 100644 ircbot/Module.py delete mode 100644 ircbot/dr.botzo.cfg.example delete mode 100644 ircbot/dr.botzo.py delete mode 100644 ircbot/extlib/__init__.py delete mode 100644 ircbot/extlib/irclib.py delete mode 100644 ircbot/logging.cfg delete mode 100644 scripts/factfile-to-facts.py delete mode 100644 scripts/import-file-into-markov_chain.py diff --git a/EXTERNALS b/EXTERNALS deleted file mode 100644 index 8c186c7..0000000 --- a/EXTERNALS +++ /dev/null @@ -1,4 +0,0 @@ -Python IRC library (python-irclib) -* http://python-irclib.sourceforge.net/ -* 0.6.4 -* LGPLv2 diff --git a/README b/README index b22dfa5..5d85521 100644 --- a/README +++ b/README @@ -17,4 +17,4 @@ DEVELOPMENT The bot is currently written to be fairly modular. No real documentation on this yet, but those interested in developing more features should take a - look at the classes in dr.botzo.py. + look at any of the ircplugin.py files. diff --git a/ircbot/DrBotIRC.py b/ircbot/DrBotIRC.py deleted file mode 100644 index b7d1abb..0000000 --- a/ircbot/DrBotIRC.py +++ /dev/null @@ -1,821 +0,0 @@ -""" -DrBotIRC - customizations of irclib, for dr.botzo -Copyright (C) 2011 Brian S. Stephan - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -""" - -import bisect -import copy -from ConfigParser import NoOptionError, NoSectionError -import logging -import re -import signal -from SimpleXMLRPCServer import SimpleXMLRPCServer -import socket -import sys -import thread - -from extlib import irclib - - -log = logging.getLogger('drbotzo') - - -class DrBotzoMethods: - - """Methods to expose to the XML-RPC server.""" - - def __init__(self, irc): - """Store the same stuff the core module would, since we'll probably - need it. - - Args: - irc the irc instance to save a reference to - - """ - - self.irc = irc - - def say(self, target, message): - """Say a message in a channel/to a nick. - - Args: - target the nick/channel to privmsg - message the message to send - - Returns: - "OK", since there's no other feedback to give. - - """ - - self.irc.server.privmsg(target, message) - return "OK" - - def execute_module_method(self, modname, method, args): - """Execute the method (with arguments) of the specified module. - - Args: - modname the loaded module to retrieve - method the method to call from that module - args the arguments to provide to the method as a tuple - - Returns: - An error string, or the outcome of the method call. - - """ - - for module in self.irc.modlist: - if modname == module.__class__.__name__: - try: - func = getattr(module, method) - except AttributeError: - return ("couldn't find '{0:s}' in found module " - "'{1:s}'".format(method, modname)) - - if hasattr(func, '__call__'): - return func(*args) - else: - return ("'{0:s}' in found module '{1:s}' is not " - "callable".format(method, modname)) - - return "couldn't find module '{0:s}'".format(modname) - -class DrBotServerConnection(irclib.ServerConnection): - - """Subclass irclib's ServerConnection, in order to expand privmsg.""" - - nickmask = None - - def __init__(self, irclibobj, nickname=None, username=None): - """Instantiate the server connection. - - Also start guessing at the nickmask and get ready to do on_welcome - stuff. - - Args: - irclibobj the irclib instance to connect with - nickname the nickname to use in nickmask guess - username the username to use in nickmask guess - - """ - - irclib.ServerConnection.__init__(self, irclibobj) - - # temporary. hopefully on_welcome() will set this, but this should be - # a pretty good guess if not - nick = nickname - user = username if username is not None else nick - host = socket.getfqdn() - self.nickmask = "{0:s}!~{1:s}@{2:s}".format(nick, user, host) - log.debug("guessing at nickmask '{0:s}'".format(self.nickmask)) - - self.add_global_handler('welcome', self.on_welcome, 1) - - def on_welcome(self, connection, event): - """Set the nickmask that the ircd tells us is us. - - Args: - connection source connection - event incoming event - - """ - - what = event.arguments()[0] - - log.debug("welcome: {0:s}".format(what)) - - match = re.search('(\S+!\S+@\S+)', what) - if match: - self.nickmask = match.group(1) - log.debug("setting nickmask: {0:s}".format(self.nickmask)) - - def privmsg(self, target, text): - """Send a PRIVMSG command. - - Args: - target the destination nick/channel - text the message to send - - """ - - log.debug("OUTGOING PRIVMSG: t[{0:s}] m[{1:s}]".format(target, text)) - - splitter = "..." - - # split messages that are too long. Max length is 512. - # TODO: this does not properly handle when the nickmask has been - # masked by the ircd - # is the above still the case? - space = 512 - len('\r\n') - len(' PRIVMSG ') - len(target) - len(' :') - len(self.nickmask) - len(' :') - splitspace = space - (len(splitter) + 1) - - if len(text) > space: - times = 1 - - while len(text) > splitspace: - splitpos = text.rfind(' ', 0, splitspace) - splittext = text[0:splitpos] + ' ' + splitter - text = splitter + ' ' + text[splitpos+1:] - self.send_raw("PRIVMSG {0:s} :{1:s}".format(target, splittext)) - - times = times + 1 - if times >= 4: - # this is stupidly long, abort - return - - # done splitting - self.send_raw("PRIVMSG {0:s} :{1:s}".format(target, text)) - else: - self.send_raw("PRIVMSG {0:s} :{1:s}".format(target, text)) - -class DrBotIRC(irclib.IRC): - - """Implement a customized irclib IRC.""" - - modlist = [] - config = None - server = None - - def __init__(self, config): - """Initialize XML-RPC interface and save references. - - Args: - config the config structure to load stuff from - - """ - irclib.IRC.__init__(self) - - self.config = config - self.xmlrpc = None - - self.regex_handlers = dict() - - # handle SIGINT - signal.signal(signal.SIGINT, self.sigint_handler) - - # load XML-RPC server - try: - if self.config.has_section('XMLRPC'): - host = self.config.get('XMLRPC', 'host') - port = self.config.getint('XMLRPC', 'port') - if host and port: - self.funcs = DrBotzoMethods(self) - - self.xmlrpc = SimpleXMLRPCServer((host, port), logRequests=False) - self.xmlrpc.register_introspection_functions() - self.xmlrpc.register_instance(self.funcs) - - thread.start_new_thread(self._xmlrpc_listen, ()) - except NoOptionError: pass - - def server(self): - """Create a DrBotServerConnection. - - Returns: - The newly created DrBotServerConnection. - - """ - - # get the nick and user name so we can provide it to - # DrBotServerConnection as a hint for the nickmask - user = None - nick = None - try: - if self.config.has_section('dr.botzo'): - user = self.config.get('dr.botzo', 'user') - nick = self.config.get('dr.botzo', 'nick') - except NoOptionError: pass - - self.server = DrBotServerConnection(self, user, nick) - - # though we only handle one connection, the underlying library supports - # multiple. append the new one though we intend no others - self.connections.append(self.server) - - return self.server - - def add_global_regex_handler(self, event, regex, handler, priority=0): - """Adds a global handler function for a specific event type and regex. - - The handler function is called whenever the specified event is - triggered in any of the connections and the regex matches. - See documentation for the Event class. - - The handler functions are called in priority order (lowest - number is highest priority). If a handler function returns - "NO MORE", no more handlers will be called. - - This is basically an extension of add_global_handler(), either may - work, though it turns out most modules probably want this one. - - The provided method should take as arguments: - * nick the nick creating the event, or None - * userhost the userhost creating the event, or None - * event list of the raw IRC events, which may be None - * from_admin whether or not the event came from an admin - * groups list of match.groups(), from re.search() - - Args: - event event type (a string), as in numeric_events - regex regex string to match before doing callback invocation - handler callback function to invoke - priority integer, the lower number, the higher priority - - """ - - if type(event) != list: - event = [event] - - for ev in event: - if not ev in self.regex_handlers: - self.regex_handlers[ev] = [] - bisect.insort(self.regex_handlers[ev], ((priority, regex, handler))) - - def remove_global_regex_handler(self, event, regex, handler): - """Removes a global regex handler function. - - Args: - event list of event type (a string), as in numeric_events - regex regex string that was used in matching - handler callback function to remove - - Returns: - 1 on success, otherwise 0. - - """ - - if type(event) != list: - event = [event] - - for ev in event: - if not ev in self.regex_handlers: - continue - - for h in self.regex_handlers[ev]: - if regex == h[1] and handler == h[2]: - self.regex_handlers[ev].remove(h) - - def _handle_event(self, connection, event): - """Override event handler to do recursion and regex checking. - - Args: - connection source connection - event incoming event - - """ - - log.debug("EVENT: e[{0:s}] s[{1:s}] t[{2:s}] " - "a[{3:s}]".format(event.eventtype(), event.source(), - event.target(), event.arguments())) - - try: - nick = irclib.nm_to_n(event.source()) - except (IndexError, AttributeError): - nick = '' - - try: - if self.config.has_section('Ignore'): - alias = self.config.get('Ignore', nick.lower()) - if alias: - log.debug("ignoring {0:s} as per config file".format(nick)) - return - except NoOptionError: pass - - self.try_alias_cmds(connection, event) - self.try_recursion(connection, event) - self.try_alias(connection, event) - - nick = None - userhost = None - admin = False - - if event.source() is not None: - nick = irclib.nm_to_n(event.source()) - try: - userhost = irclib.nm_to_uh(event.source()) - except IndexError: pass - - try: - if userhost == self.config.get('dr.botzo', 'admin_userhost'): - admin = True - except NoOptionError: pass - - # try regex handlers first, since they're more specific - rh = self.regex_handlers - trh = sorted(rh.get('all_events', []) + rh.get(event.eventtype(), [])) - for handler in trh: - try: - prio, regex, method = handler - for line in event.arguments(): - match = re.search(regex, line) - if match: - log.debug("pattern matched, calling " - "{0:s}".format(method)) - # pattern matched, call method with pattern groups - ret = method(nick, userhost, event, admin, - match.groups()) - if ret == 'NO MORE': - return - except Exception as e: - log.error("exception floated up to DrBotIrc!") - log.exception(e) - - h = self.handlers - th = sorted(h.get('all_events', []) + h.get(event.eventtype(), [])) - for handler in th: - try: - prio, method = handler - ret = method(connection, event) - if ret == 'NO MORE': - return - except Exception as e: - log.error("exception floated up to DrBotIrc!") - log.exception(e) - - def xmlrpc_register_function(self, func, name): - """Add a method to the XML-RPC interface. - - Args: - func the method to register - name the name to expose the method as - - """ - - if func and self.xmlrpc: - if hasattr(func, '__call__'): - self.xmlrpc.register_function(func, name) - - def _xmlrpc_listen(self): - """Begin listening. - - Hopefully this was called in a new thread. - - """ - - self.xmlrpc.serve_forever() - - def try_to_replace_event_text_with_module_text(self, connection, event): - """Do something very similar to _handle_event, but for recursion. - - The intent here is that we replace [text] with whatever a module - provides to us. - - Args: - connection source connection - event incoming event - - """ - - replies = [] - - nick = None - userhost = None - admin = False - - if event.source() is not None: - nick = irclib.nm_to_n(event.source()) - try: - userhost = irclib.nm_to_uh(event.source()) - except IndexError: pass - - try: - if userhost == self.config.get('dr.botzo', 'admin_userhost'): - admin = True - except NoOptionError: pass - - # try regex handlers first, since they're more specific - rh = self.regex_handlers - trh = sorted(rh.get('all_events', []) + rh.get(event.eventtype(), [])) - for handler in trh: - try: - prio, regex, method = handler - for line in event.arguments(): - match = re.search(regex, line) - if match: - log.debug("pattern matched, calling " - "{0:s}".format(method)) - # pattern matched, call method with pattern groups - ret = method(nick, userhost, event, admin, - match.groups()) - if ret: - replies.append(ret) - except Exception as e: - log.error("exception floated up to DrBotIrc!") - log.exception(e) - - h = self.handlers - th = sorted(h.get('all_events', []) + h.get(event.eventtype(), [])) - for handler in th: - try: - prio, method = handler - ret = method(connection, event) - if ret: - replies.append(ret) - except Exception as e: - log.error("exception floated up to DrBotIrc!") - log.exception(e) - - if len(replies): - event.arguments()[0] = '\n'.join(replies) - - def try_alias_cmds(self, connection, event): - """See if there is an alias ("!command") in the text, and if so - do alias manipulation before any other recursion or aliasing. - - Args: - connection source connection - event incoming event - - Returns: - The outcome of the alias command, if the text had one. - - """ - - try: - nick = irclib.nm_to_n(event.source()) - except (IndexError, AttributeError): - nick = '' - try: - userhost = irclib.nm_to_uh(event.source()) - except (IndexError, AttributeError): - userhost = '' - replypath = event.target() - try: - what = event.arguments()[0] - except (IndexError, AttributeError): - what = '' - admin_unlocked = False - - # privmsg - if replypath == connection.get_nickname(): - replypath = nick - - try: - if userhost == self.config.get('dr.botzo', 'admin_userhost'): - admin_unlocked = True - except NoOptionError: pass - - # first see if the aliases are being directly manipulated via add/remove - whats = what.split(' ') - - if len(whats) <= 1: - return - - try: - if whats[0] == '!alias' and whats[1] == 'add' and len(whats) >= 4: - if not self.config.has_section('Alias'): - self.config.add_section('Alias') - - self.config.set('Alias', whats[2], ' '.join(whats[3:])) - replystr = "Added alias '{0:s}'.".format(whats[2]) - return self.reply(event, replystr) - if whats[0] == '!alias' and whats[1] == 'remove' and len(whats) >= 3: - if not self.config.has_section('Alias'): - self.config.add_section('Alias') - - if self.config.remove_option('Alias', whats[2]): - replystr = "Removed alias '{0:s}'.".format(whats[2]) - return self.reply(event, replystr) - elif whats[0] == '!alias' and whats[1] == 'list': - try: - if len(whats) > 2: - alias = self.config.get('Alias', whats[2]) - return self.reply(event, alias) - else: - alist = self.config.options('Alias') - alist.remove('debug') - alist.sort() - liststr = ', '.join(alist) - return self.reply(event, liststr) - except NoSectionError: pass - except NoOptionError: pass - except NoSectionError: pass - - def try_alias(self, connection, event): - """Try turning aliases into commands. - - Args: - connection source connection - event incoming event - - Returns: - The de-aliased event object. - - """ - - try: - what = event.arguments()[0] - alias_list = self.config.options('Alias') - - for alias in alias_list: - alias_re = re.compile(alias, re.IGNORECASE) - if alias_re.search(what) and alias != 'debug': - # we found an alias for our given string, doing a replace - command = re.sub(alias, self.config.get('Alias', alias), what, flags=re.IGNORECASE) - event.arguments()[0] = command - - # now we have to check it for recursions again - self.try_recursion(connection, event) - - # i guess someone could have an alias of an alias... try again - return self.try_alias(connection, event) - except NoOptionError: pass - except NoSectionError: pass - except IndexError: pass - - # if we got here, there are no matching aliases, so return what we got - return event - - def try_recursion(self, connection, event): - """Scan message for subcommands to execute and use as part of this command. - - Upon seeing a line intended for this module, see if there are subcommands - that we should do what is basically a text replacement on. The intent is to - allow things like the following: - - command arg1 [anothercommand arg1 arg2] - - where the output of anothercommand is command's arg2..n. - - Args: - connection source connection - event incoming event - - """ - - try: - # begin recursion search - attempt = event.arguments()[0] - - start_idx = attempt.find('[') - subcmd = attempt[start_idx+1:] - end_idx = subcmd.rfind(']') - subcmd = subcmd[:end_idx] - - if start_idx != -1 and end_idx != -1 and len(subcmd) > 0: - # found recursion candidate - # copy the event and see if IT has recursion to do - newevent = copy.deepcopy(event) - newevent.arguments()[0] = subcmd - newevent._recursing = True - - self.try_recursion(connection, newevent) - - # recursion over, check for aliases - self.try_alias(connection, newevent) - - # now that we have a string that has been de-aliased and - # recursed all the way deeper into its text, see if any - # modules can do something with it. this calls the same - # event handlers in the same way as if this were a native - # event. - self.try_to_replace_event_text_with_module_text(connection, newevent) - - # we have done all we can do with the sub-event. whatever - # the text of that event now is, we should replace the parent - # event's [] section with it. - oldtext = event.arguments()[0] - newtext = oldtext.replace('['+subcmd+']', newevent.arguments()[0]) - event.arguments()[0] = newtext - - # we have now resolved the []. recursion will unfold, replacing - # it further and further, until we eventually get back to the - # original irc event in _handle_event, which will do one - # last search on the text. - except IndexError: pass - - def quit_irc(self, connection, msg): - """Quit IRC, disconnect, shut everything down. - - Args: - connection the connection to quit - msg the message to send while quitting - """ - - for module in self.modlist: - module.save() - module.shutdown() - - connection.quit(msg) - log.info(self.save_config()) - self._xmlrpc_shutdown() - log.info("Bot shutting down.") - sys.exit() - - def reply(self, event, replystr, stop=False): - """Reply over IRC to replypath or return a string with the reply. - - The primary utility for this is to properly handle recursion. The - recursion code in DrBotIRC will set up a couple hints that this method - picks up on and will appropriately send an IRC event or return a - string. - - Unless you know what you are doing, the modules you write should use - this method rather than send a privmsg reply, as failing to call this - method will certainly have recursion do odd things with your module. - - Args: - event incoming event - replystr the message to reply with - stop whether or not to let other handlers see this - - Returns: - The replystr if the event is inside recursion, or, potentially, - "NO MORE" to stop other event handlers from acting. - - """ - - replypath = event.target() - - # if this is a privmsg, reply to the sender - if replypath == self.server.get_nickname(): - replypath = irclib.nm_to_n(event.source()) - - if replystr is not None: - if event._recursing: - return replystr - else: - replies = replystr.split('\n') - for reply in replies: - self.server.privmsg(replypath, reply) - if stop: - return "NO MORE" - - def save_modules(self): - """Call each module's save(), in case they have something to do.""" - - for module in self.modlist: - module.save() - - def _xmlrpc_shutdown(self): - """Shut down the XML-RPC server.""" - - if self.xmlrpc is not None: - self.xmlrpc.shutdown() - self.xmlrpc.server_close() - - def save_config(self): - """Write the config file to disk. - - Returns: - Short string indicating success. - - """ - - with open('dr.botzo.cfg', 'w') as cfg: - self.config.write(cfg) - - return "Saved config." - - def load_module(self, modname): - """Load a module (in both the python and dr.botzo sense) if not - already loaded. - - Args: - modname the module to attempt to load - - Returns: - String describing the outcome of the load attempt. - - """ - - for module in self.modlist: - if modname == module.__class__.__name__: - return "Module '{0:s}' is already loaded.".format(modname) - - # not loaded, let's get to work - try: - modstr = 'modules.'+modname - __import__(modstr) - module = sys.modules[modstr] - botmod = eval('module.' + modname + '(self, self.config)') - self.modlist.append(botmod) - botmod.register_handlers() - - # might as well add it to the list - modset = set(self.config.get('dr.botzo', 'module_list').split(',')) - modset.add(modname) - self.config.set('dr.botzo', 'module_list', ','.join(modset)) - - return "Module '{0:s}' loaded.".format(modname) - except ImportError as e: - log.error("Error loading '{0:s}'".format(modname)) - log.exception(e) - return "Module '{0:s}' could not be loaded.".format(modname) - - def unload_module(self, modname): - """Attempt to unload and del a module if it's loaded. - - Args: - modname the module to attempt to unload - - Returns: - String describing the outcome of the unload attempt. - - """ - - modstr = 'modules.'+modname - for module in self.modlist: - if modname == module.__class__.__name__: - # do anything the module needs to do to clean up - module.save() - module.shutdown() - - # remove module references - self.modlist.remove(module) - module.unregister_handlers() - - # del it - del(sys.modules[modstr]) - del(module) - - # might as well remove it from the list - modset = set(self.config.get('dr.botzo', 'module_list').split(',')) - modset.remove(modname) - self.config.set('dr.botzo', 'module_list', ','.join(modset)) - - return "Module '{0:s}' unloaded.".format(modname) - - # guess it was never loaded - return "Module '{0:s}' is not loaded.".format(modname) - - def list_modules(self): - """List loaded modules. - - Returns: - A list of the loaded modules' names. - - """ - - modnames = [] - for module in self.modlist: - modnames.append(module.__class__.__name__) - - return modnames - - def sigint_handler(self, signal, frame): - """Cleanly shutdown on SIGINT.""" - - for module in self.modlist: - module.save() - module.shutdown() - - log.info(self.save_config()) - self._xmlrpc_shutdown() - sys.exit() - -# vi:tabstop=4:expandtab:autoindent -# kate: indent-mode python;indent-width 4;replace-tabs on; diff --git a/ircbot/Module.py b/ircbot/Module.py deleted file mode 100644 index 39e158a..0000000 --- a/ircbot/Module.py +++ /dev/null @@ -1,328 +0,0 @@ -""" -Module - dr.botzo modular functionality base class -Copyright (C) 2010 Brian S. Stephan - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -""" - -from ConfigParser import NoSectionError, NoOptionError - -import logging - -import MySQLdb as mdb - -from extlib import irclib - -class Module(object): - - """Provide module base class with convenience functionality/bootstrap.""" - - def priority(self): - return 50 - - def __init__(self, irc, config): - """Construct a feature module. - - Inheritors can do special things here, but should be sure to call - Module.__init__. - - Args: - irc DrBotIRC object for the running bot - config ConfigParser object, the entire config file - - """ - - self.irc = irc - self.config = config - - # reload logging config every time - logging.config.fileConfig('logging.cfg') - self.log = logging.getLogger('drbotzo.'+self.__class__.__name__.lower()) - - self.is_shutdown = False - - # set up database for this module - self.db_init() - - # set up XML-RPC for this module - self.xmlrpc_init() - - self.log.info("Loaded " + self.__class__.__name__) - - def register_handlers(self): - """Hook handler functions into the IRC library. - - This is called when the module is loaded. Classes with special stuff - to do could implement this and set up the appropriate handlers, e.g.: - - self.irc.server.add_global_handler('welcome', self.on_connect) - - By default, a module attaches to pubmsg/privmsg, which sets up some - common variables and then calls do(). You are free to implement do() - (see below), or override this and do whatever you want. - - Modules interested in doing so might also register XML-RPC stuff here. - - """ - - self.irc.server.add_global_handler('pubmsg', self.on_pub_or_privmsg, - self.priority()) - self.irc.server.add_global_handler('privmsg', self.on_pub_or_privmsg, - self.priority()) - - def unregister_handlers(self): - """Unhook handler functions from the IRC library. - - Inverse of the above. This is called by unload, to remove the - soon-to-be old object from the server global handlers (or whatever has - been added via register_handlers). Classes inheriting from Module - could reimplement this, e.g.: - - self.irc.server.remove_global_handler('welcome', self.on_connect) - - """ - - self.irc.server.remove_global_handler('pubmsg', self.on_pub_or_privmsg) - self.irc.server.remove_global_handler('privmsg', self.on_pub_or_privmsg) - - def on_pub_or_privmsg(self, connection, event): - """Do a default thing on a pubmsg or privmsg. - - Sets up a couple variables and then calls do(), which by default we - expect implementers to implement. - - Args: - connection the source connection for this event - event the event to handle - - Returns: - The results of handling the event. - - """ - - nick = irclib.nm_to_n(event.source()) - userhost = irclib.nm_to_uh(event.source()) - what = event.arguments()[0] - admin_unlocked = False - - try: - if userhost == self.config.get('dr.botzo', 'admin_userhost'): - admin_unlocked = True - except NoOptionError: pass - - return self.do(connection, event, nick, userhost, what, admin_unlocked) - - def sendmsg(self, target, msg): - """Send a privmsg over IRC to target. - - Args: - target destination on the network - msg the message to send - - """ - - if msg is not None: - if target is not None: - msgs = msg.split('\n') - for line in msgs: - self.irc.server.privmsg(target, line) - - def save(self): - """Save whatever the module may need to save. Sync files, etc. - - Implement this if you need it. - - """ - - pass - - def shutdown(self): - """Do pre-deletion type cleanup. - - Implement this to close databases, write to disk, etc. Note that - DrBotIRC will call save() before this, so implement appropriately. - - """ - - self.is_shutdown = True - self.log.info("Unloading " + self.__class__.__name__) - - def remove_metaoptions(self, list_): - """Remove metaoptions from provided list, which was probably from a - config file. - - DEPRECATED - - This is a convenience method that can go away once we get module stuff - out of the config file and into the database. - - Args: - list_ the list to prune known keywords from - - """ - - list_.remove('debug') - - def retransmit_event(self, event): - """Pretend that some event the bot has generated is rather an incoming - IRC event. - - DEPRECATED - - Why one would do this is unclear, but I wrote it and then realized - I didn't need it. - - Args: - event the event to replay - - """ - - self.irc.server._handle_event(event) - - def get_db(self): - """Get a database connection to mdb. - - Once grabbed, it should be closed when work is done. Modules that - need a database connection should test for and create (or, eventually, - alter) required table structure in their __init__ IF that structure - does not already exist. Well-behaved modules should use a prefix in - their table names (eg "karma_log" rather than "log") - - See also db_module_registered, below. - - """ - - dbhost = self.config.get('dr.botzo', 'dbhost') - dbuser = self.config.get('dr.botzo', 'dbuser') - dbpass = self.config.get('dr.botzo', 'dbpass') - dbname = self.config.get('dr.botzo', 'dbname') - db = mdb.connect(dbhost, dbuser, dbpass, dbname, charset='utf8', - use_unicode=True) - - return db - - def db_module_registered(self, modulename): - """Ask the database for a version number for a module. - - Return that version number if the module has registered, or None if - not. - - Args: - modulename the name of the module to check - - Returns: - The version number stored for the module in the database, as a int - - """ - - db = self.get_db() - version = None - try: - cur = db.cursor() - cur.execute("SELECT version FROM drbotzo_modules WHERE module = %s", - (modulename,)) - version = cur.fetchone() - if (version != None): - version = version[0] - except mdb.Error as e: - self.log.error("database error during module registered check") - self.log.exception(e) - raise - finally: cur.close() - - return version - - def db_register_module_version(self, modulename, version): - """Enter the given module name and version number into the database. - - The primary want for this is to record what the current revision the - tables are, to do upgrades. - - Args: - modulename the name of the module to update - version the version number to set as a int - - """ - - db = self.get_db() - try: - cur = db.cursor(mdb.cursors.DictCursor) - cur.execute('INSERT IGNORE INTO drbotzo_modules (version, module) VALUES (%s, %s)', - (version, modulename)) - db.commit() - except mdb.Error as e: - db.rollback() - self.log.error("database error during register module version") - self.log.exception(e) - raise - finally: cur.close() - - def db_init(self): - """Set up the database tables and so on. - - Modules interested in this should implement table setup here. - - """ - - pass - - def xmlrpc_init(self): - """Set up XML-RPC handlers. - - Modules interested in this should implement registration here. - - """ - - pass - - def do(self, connection, event, nick, userhost, what, admin): - """Do the primary thing this module was intended to do, in most cases. - - Implement this method in your subclass to have a fairly-automatic hook - into IRC functionality. This is called by DrBotIRC during pubmsg and - privmsg events. - - Args: - connection the connection instance to handle - event the event to handle - nick the nick of the originator of the event - userhost the userhost of the originator of the event - what the message body of the event - admin if the event was created by an admin - - """ - - pass - - def _unencode_xml(self, text): - """Convert <, >, & to their real entities. - - Convenience method for modules, I've been thinking about moving this. - - Args: - text the text to clean up - - Returns: - The provided text string, with the known entities replaced. - - """ - - text = text.replace('<', '<') - text = text.replace('>', '>') - text = text.replace('&', '&') - return text - -# vi:tabstop=4:expandtab:autoindent -# kate: indent-mode python;indent-width 4;replace-tabs on; diff --git a/ircbot/dr.botzo.cfg.example b/ircbot/dr.botzo.cfg.example deleted file mode 100644 index 8953f36..0000000 --- a/ircbot/dr.botzo.cfg.example +++ /dev/null @@ -1,23 +0,0 @@ -[dr.botzo] -server = irc.foonetic.net -port = 6667 -nick = dr_devzo -name = dr. devzo -usermode = -x -debug = true -admin_userhost = bss@ayu.incorporeal.org -module_list = IrcAdmin -dbhost = localhost -dbuser = dr_botzo -dbpass = password -dbname = dr_botzo -ssl = no -ipv6 = yes - -[IrcAdmin] -autojoin = #bss -sleep = 30 -automsg = nickserv identify foo - -[Karma] -meta.pubmsg_needs_bot_prefix = false diff --git a/ircbot/dr.botzo.py b/ircbot/dr.botzo.py deleted file mode 100644 index d4d2c63..0000000 --- a/ircbot/dr.botzo.py +++ /dev/null @@ -1,129 +0,0 @@ -""" -dr.botzo - a pluggable IRC bot written in Python -Copyright (C) 2010 Brian S. Stephan - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . - -""" - -import django -from django.conf import settings - -from ConfigParser import ConfigParser, NoSectionError, NoOptionError -import logging -import logging.config -import os -import sys - -import MySQLdb as mdb - -import DrBotIRC - -# gross hack to get to django -sys.path.insert(0, '../dr_botzo') -django.setup() - -config_file = 'dr.botzo.cfg' -# check argv -if len(sys.argv) == 2: - config_file = sys.argv[1] - -# read config file -config = ConfigParser({'debug': 'false'}) -config.read(os.path.expanduser(config_file)) - -logging.config.fileConfig('logging.cfg') -log = logging.getLogger('drbotzo') - -try: - dbhost = config.get('dr.botzo', 'dbhost') - dbuser = config.get('dr.botzo', 'dbuser') - dbpass = config.get('dr.botzo', 'dbpass') - dbname = config.get('dr.botzo', 'dbname') - db = mdb.connect(dbhost, dbuser, dbpass, dbname, charset='utf8', - use_unicode=True) - try: - cur = db.cursor() - # need to create the drbotzo_modules table if it doesn't exist - query = """ - SELECT COUNT(*) FROM information_schema.tables - WHERE table_schema = %s - AND table_name = %s - """ - cur.execute(query, (dbname, 'drbotzo_modules')) - row = cur.fetchone() - if row[0] == 0: - query = """ - CREATE TABLE IF NOT EXISTS drbotzo_modules ( - module VARCHAR(64) PRIMARY KEY, - version INTEGER - ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin - """ - cur.execute(query) - db.commit() - finally: cur.close() -except NoOptionError as e: - sys.exit("Aborted due to error with necessary configuration: " - "{0:s}".format(str(e))) - -# get some optional parameters -use_ssl = False -try: - use_ssl = config.getboolean('dr.botzo', 'ssl') -except NoOptionError: - pass -if use_ssl: - log.info("SSL support enabled") -else: - log.debug("SSL not requested") - -use_ipv6 = False -try: - use_ipv6 = config.getboolean('dr.botzo', 'ipv6') -except NoOptionError: - pass -if use_ipv6: - log.info("IPv6 support enabled") -else: - log.debug("IPv6 not requested") - -# start up the IRC bot - -# create IRC and server objects and connect -irc = DrBotIRC.DrBotIRC(config) -server = irc.server().connect(settings.IRC_SERVER, - settings.IRC_PORT, - settings.IRC_NICK, - settings.IRC_PASS, - settings.IRC_USER, - settings.IRC_NAME, - ssl=use_ssl, - ipv6=use_ipv6) - -# load features -try: - cfgmodlist = config.get('dr.botzo', 'module_list') - - mods = cfgmodlist.split(',') - for mod in mods: - irc.load_module(mod) -except NoOptionError as e: - log.warning("You seem to be missing a module_list config option, which " - "you probably wanted.") - -# loop forever -irc.process_forever() - -# vi:tabstop=4:expandtab:autoindent -# kate: indent-mode python;indent-width 4;replace-tabs on; diff --git a/ircbot/extlib/__init__.py b/ircbot/extlib/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/ircbot/extlib/irclib.py b/ircbot/extlib/irclib.py deleted file mode 100644 index ae23766..0000000 --- a/ircbot/extlib/irclib.py +++ /dev/null @@ -1,1711 +0,0 @@ -# -*- coding: utf-8 -*- - -# Copyright (C) 1999-2002 Joel Rosdahl -# Portions Copyright © 2011 Jason R. Coombs -# -# This library is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 2.1 of the License, or (at your option) any later version. -# -# This library is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with this library; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -# -# keltus - -""" -irclib -- Internet Relay Chat (IRC) protocol client library. - -This library is intended to encapsulate the IRC protocol at a quite -low level. It provides an event-driven IRC client framework. It has -a fairly thorough support for the basic IRC protocol, CTCP, DCC chat, -but DCC file transfers is not yet supported. - -In order to understand how to make an IRC client, I'm afraid you more -or less must understand the IRC specifications. They are available -here: [IRC specifications]. - -The main features of the IRC client framework are: - - * Abstraction of the IRC protocol. - * Handles multiple simultaneous IRC server connections. - * Handles server PONGing transparently. - * Messages to the IRC server are done by calling methods on an IRC - connection object. - * Messages from an IRC server triggers events, which can be caught - by event handlers. - * Reading from and writing to IRC server sockets are normally done - by an internal select() loop, but the select()ing may be done by - an external main loop. - * Functions can be registered to execute at specified times by the - event-loop. - * Decodes CTCP tagging correctly (hopefully); I haven't seen any - other IRC client implementation that handles the CTCP - specification subtilties. - * A kind of simple, single-server, object-oriented IRC client class - that dispatches events to instance methods is included. - -Current limitations: - - * The IRC protocol shines through the abstraction a bit too much. - * Data is not written asynchronously to the server, i.e. the write() - may block if the TCP buffers are stuffed. - * There are no support for DCC file transfers. - * The author haven't even read RFC 2810, 2811, 2812 and 2813. - * Like most projects, documentation is lacking... - -.. [IRC specifications] http://www.irchelp.org/irchelp/rfc/ -""" - -import bisect -import logging -import re -import select -import socket -import string -import time -import types -import ssl as ssl_mod -import datetime - -try: - import pkg_resources - _pkg = pkg_resources.require('python-irclib')[0] - VERSION = tuple(int(res) for res in re.findall('\d+', _pkg.version)) -except Exception: - VERSION = () - -log = logging.getLogger('irclib') - -# TODO -# ---- -# (maybe) thread safety -# (maybe) color parser convenience functions -# documentation (including all event types) -# (maybe) add awareness of different types of ircds -# send data asynchronously to the server (and DCC connections) -# (maybe) automatically close unused, passive DCC connections after a while - -# NOTES -# ----- -# connection.quit() only sends QUIT to the server. -# ERROR from the server triggers the error event and the disconnect event. -# dropping of the connection triggers the disconnect event. - -class IRCError(Exception): - """Represents an IRC exception.""" - pass - - -class IRC(object): - """Class that handles one or several IRC server connections. - - When an IRC object has been instantiated, it can be used to create - Connection objects that represent the IRC connections. The - responsibility of the IRC object is to provide an event-driven - framework for the connections and to keep the connections alive. - It runs a select loop to poll each connection's TCP socket and - hands over the sockets with incoming data for processing by the - corresponding connection. - - The methods of most interest for an IRC client writer are server, - add_global_handler, remove_global_handler, execute_at, - execute_delayed, process_once and process_forever. - - Here is an example: - - irc = irclib.IRC() - server = irc.server() - server.connect("irc.some.where", 6667, "my_nickname") - server.privmsg("a_nickname", "Hi there!") - irc.process_forever() - - This will connect to the IRC server irc.some.where on port 6667 - using the nickname my_nickname and send the message "Hi there!" - to the nickname a_nickname. - """ - - def __init__(self, fn_to_add_socket=None, - fn_to_remove_socket=None, - fn_to_add_timeout=None): - """Constructor for IRC objects. - - Optional arguments are fn_to_add_socket, fn_to_remove_socket - and fn_to_add_timeout. The first two specify functions that - will be called with a socket object as argument when the IRC - object wants to be notified (or stop being notified) of data - coming on a new socket. When new data arrives, the method - process_data should be called. Similarly, fn_to_add_timeout - is called with a number of seconds (a floating point number) - as first argument when the IRC object wants to receive a - notification (by calling the process_timeout method). So, if - e.g. the argument is 42.17, the object wants the - process_timeout method to be called after 42 seconds and 170 - milliseconds. - - The three arguments mainly exist to be able to use an external - main loop (for example Tkinter's or PyGTK's main app loop) - instead of calling the process_forever method. - - An alternative is to just call ServerConnection.process_once() - once in a while. - """ - - if fn_to_add_socket and fn_to_remove_socket: - self.fn_to_add_socket = fn_to_add_socket - self.fn_to_remove_socket = fn_to_remove_socket - else: - self.fn_to_add_socket = None - self.fn_to_remove_socket = None - - self.fn_to_add_timeout = fn_to_add_timeout - self.connections = [] - self.handlers = {} - self.delayed_commands = [] # list of DelayedCommands - - self.add_global_handler("ping", _ping_ponger, -42) - - def server(self): - """Creates and returns a ServerConnection object.""" - - c = ServerConnection(self) - self.connections.append(c) - return c - - def process_data(self, sockets): - """Called when there is more data to read on connection sockets. - - Arguments: - - sockets -- A list of socket objects. - - See documentation for IRC.__init__. - """ - for s in sockets: - for c in self.connections: - if s == c._get_socket(): - c.process_data() - - def process_timeout(self): - """Called when a timeout notification is due. - - See documentation for IRC.__init__. - """ - while self.delayed_commands: - command = self.delayed_commands[0] - if not command.due(): - break - command.function(*command.arguments) - if isinstance(command, PeriodicCommand): - self._schedule_command(command.next()) - del self.delayed_commands[0] - - def process_once(self, timeout=0): - """Process data from connections once. - - Arguments: - - timeout -- How long the select() call should wait if no - data is available. - - This method should be called periodically to check and process - incoming data, if there are any. If that seems boring, look - at the process_forever method. - """ - sockets = map(lambda x: x._get_socket(), self.connections) - sockets = filter(lambda x: x != None, sockets) - if sockets: - (i, o, e) = select.select(sockets, [], [], timeout) - self.process_data(i) - else: - time.sleep(timeout) - self.process_timeout() - - def process_forever(self, timeout=0.2): - """Run an infinite loop, processing data from connections. - - This method repeatedly calls process_once. - - Arguments: - - timeout -- Parameter to pass to process_once. - """ - while 1: - self.process_once(timeout) - - def disconnect_all(self, message=""): - """Disconnects all connections.""" - for c in self.connections: - c.disconnect(message) - - def add_global_handler(self, event, handler, priority=0): - """Adds a global handler function for a specific event type. - - Arguments: - - event -- Event type (a string). Check the values of the - numeric_events dictionary in irclib.py for possible event - types. - - handler -- Callback function. - - priority -- A number (the lower number, the higher priority). - - The handler function is called whenever the specified event is - triggered in any of the connections. See documentation for - the Event class. - - The handler functions are called in priority order (lowest - number is highest priority). If a handler function returns - "NO MORE", no more handlers will be called. - """ - if not event in self.handlers: - self.handlers[event] = [] - bisect.insort(self.handlers[event], ((priority, handler))) - - def remove_global_handler(self, event, handler): - """Removes a global handler function. - - Arguments: - - event -- Event type (a string). - handler -- Callback function. - - Returns 1 on success, otherwise 0. - """ - if not event in self.handlers: - return 0 - for h in self.handlers[event]: - if handler == h[1]: - self.handlers[event].remove(h) - return 1 - - def execute_at(self, at, function, arguments=()): - """Execute a function at a specified time. - - Arguments: - - at -- Execute at this time (standard "time_t" time). - function -- Function to call. - arguments -- Arguments to give the function. - """ - command = DelayedCommand.at_time(at, function, arguments) - self._schedule_command(command) - - def execute_delayed(self, delay, function, arguments=()): - """ - Execute a function after a specified time. - - delay -- How many seconds to wait. - function -- Function to call. - arguments -- Arguments to give the function. - """ - command = DelayedCommand(delay, function, arguments) - self._schedule_command(command) - - def execute_every(self, period, function, arguments=()): - """ - Execute a function every 'period' seconds. - - period -- How often to run (always waits this long for first). - function -- Function to call. - arguments -- Arguments to give the function. - """ - command = PeriodicCommand(period, function, arguments) - self._schedule_command(command) - - def _schedule_command(self, command): - bisect.insort(self.delayed_commands, command) - if self.fn_to_add_timeout: - self.fn_to_add_timeout(total_seconds(command.delay)) - - def dcc(self, dcctype="chat"): - """Creates and returns a DCCConnection object. - - Arguments: - - dcctype -- "chat" for DCC CHAT connections or "raw" for - DCC SEND (or other DCC types). If "chat", - incoming data will be split in newline-separated - chunks. If "raw", incoming data is not touched. - """ - c = DCCConnection(self, dcctype) - self.connections.append(c) - return c - - def _handle_event(self, connection, event): - """[Internal]""" - h = self.handlers - th = sorted(h.get("all_events", []) + h.get(event.eventtype(), [])) - for handler in th: - if handler[1](connection, event) == "NO MORE": - return - - def _remove_connection(self, connection): - """[Internal]""" - self.connections.remove(connection) - if self.fn_to_remove_socket: - self.fn_to_remove_socket(connection._get_socket()) - -class DelayedCommand(datetime.datetime): - """ - A command to be executed after some delay (seconds or timedelta). - """ - def __new__(cls, delay, function, arguments): - if not isinstance(delay, datetime.timedelta): - delay = datetime.timedelta(seconds=delay) - at = datetime.datetime.utcnow() + delay - cmd = datetime.datetime.__new__(DelayedCommand, at.year, - at.month, at.day, at.hour, at.minute, at.second, - at.microsecond, at.tzinfo) - cmd.delay = delay - cmd.function = function - cmd.arguments = arguments - return cmd - - def at_time(cls, at, function, arguments): - """ - Construct a DelayedCommand to come due at `at`, where `at` may be - a datetime or timestamp. - """ - if isinstance(at, int): - at = datetime.datetime.utcfromtimestamp(at) - delay = at - datetime.datetime.utcnow() - return cls(delay, function, arguments) - at_time = classmethod(at_time) - - def due(self): - return datetime.datetime.utcnow() >= self - -class PeriodicCommand(DelayedCommand): - """ - Like a deferred command, but expect this command to run every delay - seconds. - """ - def next(self): - return PeriodicCommand(self.delay, self.function, - self.arguments) - -_rfc_1459_command_regexp = re.compile("^(:(?P[^ ]+) +)?(?P[^ ]+)( *(?P .+))?") - -class Connection(object): - """Base class for IRC connections. - - Must be overridden. - """ - def __init__(self, irclibobj): - self.irclibobj = irclibobj - - def _get_socket(): - raise IRCError("Not overridden") - - ############################## - ### Convenience wrappers. - - def execute_at(self, at, function, arguments=()): - self.irclibobj.execute_at(at, function, arguments) - - def execute_delayed(self, delay, function, arguments=()): - self.irclibobj.execute_delayed(delay, function, arguments) - - def execute_every(self, period, function, arguments=()): - self.irclibobj.execute_every(period, function, arguments) - -class ServerConnectionError(IRCError): - pass - -class ServerNotConnectedError(ServerConnectionError): - pass - - -# Huh!? Crrrrazy EFNet doesn't follow the RFC: their ircd seems to -# use \n as message separator! :P -_linesep_regexp = re.compile("\r?\n") - -class ServerConnection(Connection): - """This class represents an IRC server connection. - - ServerConnection objects are instantiated by calling the server - method on an IRC object. - """ - - def __init__(self, irclibobj): - super(ServerConnection, self).__init__(irclibobj) - self.connected = 0 # Not connected yet. - self.socket = None - self.ssl = None - - def connect(self, server, port, nickname, password=None, username=None, - ircname=None, localaddress="", localport=0, ssl=False, ipv6=False): - """Connect/reconnect to a server. - - Arguments: - - server -- Server name. - - port -- Port number. - - nickname -- The nickname. - - password -- Password (if any). - - username -- The username. - - ircname -- The IRC name ("realname"). - - localaddress -- Bind the connection to a specific local IP address. - - localport -- Bind the connection to a specific local port. - - ssl -- Enable support for ssl. - - ipv6 -- Enable support for ipv6. - - This function can be called to reconnect a closed connection. - - Returns the ServerConnection object. - """ - if self.connected: - self.disconnect("Changing servers") - - self.previous_buffer = "" - self.handlers = {} - self.real_server_name = "" - self.real_nickname = nickname - self.server = server - self.port = port - self.nickname = nickname - self.username = username or nickname - self.ircname = ircname or nickname - self.password = password - self.localaddress = localaddress - self.localport = localport - self.localhost = socket.gethostname() - if ipv6: - self.socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) - else: - self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - try: - self.socket.bind((self.localaddress, self.localport)) - self.socket.connect((self.server, self.port)) - if ssl: - self.ssl = ssl_mod.wrap_socket(self.socket) - except socket.error, x: - self.socket.close() - self.socket = None - raise ServerConnectionError("Couldn't connect to socket: %s" % x) - self.connected = 1 - if self.irclibobj.fn_to_add_socket: - self.irclibobj.fn_to_add_socket(self.socket) - - # Log on... - if self.password: - self.pass_(self.password) - self.nick(self.nickname) - self.user(self.username, self.ircname) - return self - - def close(self): - """Close the connection. - - This method closes the connection permanently; after it has - been called, the object is unusable. - """ - - self.disconnect("Closing object") - self.irclibobj._remove_connection(self) - - def _get_socket(self): - """[Internal]""" - return self.socket - - def get_server_name(self): - """Get the (real) server name. - - This method returns the (real) server name, or, more - specifically, what the server calls itself. - """ - - if self.real_server_name: - return self.real_server_name - else: - return "" - - def get_nickname(self): - """Get the (real) nick name. - - This method returns the (real) nickname. The library keeps - track of nick changes, so it might not be the nick name that - was passed to the connect() method. """ - - return self.real_nickname - - def process_data(self): - """[Internal]""" - - try: - if self.ssl: - new_data = self.ssl.read(2 ** 14) - else: - new_data = self.socket.recv(2 ** 14) - except socket.error: - # The server hung up. - self.disconnect("Connection reset by peer") - return - if not new_data: - # Read nothing: connection must be down. - self.disconnect("Connection reset by peer") - return - - lines = _linesep_regexp.split(self.previous_buffer + new_data) - - # Save the last, unfinished line. - self.previous_buffer = lines.pop() - - for line in lines: - log.debug("FROM SERVER:" + line) - - if not line: - continue - - prefix = None - command = None - arguments = None - self._handle_event(Event("all_raw_messages", - self.get_server_name(), - None, - [line])) - - m = _rfc_1459_command_regexp.match(line) - if m.group("prefix"): - prefix = m.group("prefix") - if not self.real_server_name: - self.real_server_name = prefix - - if m.group("command"): - command = m.group("command").lower() - - if m.group("argument"): - a = m.group("argument").split(" :", 1) - arguments = a[0].split() - if len(a) == 2: - arguments.append(a[1]) - - # Translate numerics into more readable strings. - if command in numeric_events: - command = numeric_events[command] - - if command == "nick": - if nm_to_n(prefix) == self.real_nickname: - self.real_nickname = arguments[0] - elif command == "welcome": - # Record the nickname in case the client changed nick - # in a nicknameinuse callback. - self.real_nickname = arguments[0] - - if command in ["privmsg", "notice"]: - target, message = arguments[0], arguments[1] - messages = _ctcp_dequote(message) - - if command == "privmsg": - if is_channel(target): - command = "pubmsg" - else: - if is_channel(target): - command = "pubnotice" - else: - command = "privnotice" - - for m in messages: - if type(m) is types.TupleType: - if command in ["privmsg", "pubmsg"]: - command = "ctcp" - else: - command = "ctcpreply" - - m = list(m) - log.debug("command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, m)) - self._handle_event(Event(command, prefix, target, m)) - if command == "ctcp" and m[0] == "ACTION": - self._handle_event(Event("action", prefix, target, m[1:])) - else: - log.debug("command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, [m])) - self._handle_event(Event(command, prefix, target, [m])) - else: - target = None - - if command == "quit": - arguments = [arguments[0]] - elif command == "ping": - target = arguments[0] - else: - target = arguments[0] - arguments = arguments[1:] - - if command == "mode": - if not is_channel(target): - command = "umode" - - log.debug("command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, arguments)) - self._handle_event(Event(command, prefix, target, arguments)) - - def _handle_event(self, event): - """[Internal]""" - self.irclibobj._handle_event(self, event) - if event.eventtype() in self.handlers: - for fn in self.handlers[event.eventtype()]: - fn(self, event) - - def is_connected(self): - """Return connection status. - - Returns true if connected, otherwise false. - """ - return self.connected - - def add_global_handler(self, *args): - """Add global handler. - - See documentation for IRC.add_global_handler. - """ - self.irclibobj.add_global_handler(*args) - - def remove_global_handler(self, *args): - """Remove global handler. - - See documentation for IRC.remove_global_handler. - """ - self.irclibobj.remove_global_handler(*args) - - def action(self, target, action): - """Send a CTCP ACTION command.""" - self.ctcp("ACTION", target, action) - - def admin(self, server=""): - """Send an ADMIN command.""" - self.send_raw(" ".join(["ADMIN", server]).strip()) - - def ctcp(self, ctcptype, target, parameter=""): - """Send a CTCP command.""" - ctcptype = ctcptype.upper() - self.privmsg(target, "\001%s%s\001" % (ctcptype, parameter and (" " + parameter) or "")) - - def ctcp_reply(self, target, parameter): - """Send a CTCP REPLY command.""" - self.notice(target, "\001%s\001" % parameter) - - def disconnect(self, message=""): - """Hang up the connection. - - Arguments: - - message -- Quit message. - """ - if not self.connected: - return - - self.connected = 0 - - self.quit(message) - - try: - self.socket.close() - except socket.error: - pass - self.socket = None - self._handle_event(Event("disconnect", self.server, "", [message])) - - def globops(self, text): - """Send a GLOBOPS command.""" - self.send_raw("GLOBOPS :" + text) - - def info(self, server=""): - """Send an INFO command.""" - self.send_raw(" ".join(["INFO", server]).strip()) - - def invite(self, nick, channel): - """Send an INVITE command.""" - self.send_raw(" ".join(["INVITE", nick, channel]).strip()) - - def ison(self, nicks): - """Send an ISON command. - - Arguments: - - nicks -- List of nicks. - """ - self.send_raw("ISON " + " ".join(nicks)) - - def join(self, channel, key=""): - """Send a JOIN command.""" - self.send_raw("JOIN %s%s" % (channel, (key and (" " + key)))) - - def kick(self, channel, nick, comment=""): - """Send a KICK command.""" - self.send_raw("KICK %s %s%s" % (channel, nick, (comment and (" :" + comment)))) - - def links(self, remote_server="", server_mask=""): - """Send a LINKS command.""" - command = "LINKS" - if remote_server: - command = command + " " + remote_server - if server_mask: - command = command + " " + server_mask - self.send_raw(command) - - def list(self, channels=None, server=""): - """Send a LIST command.""" - command = "LIST" - if channels: - command = command + " " + ",".join(channels) - if server: - command = command + " " + server - self.send_raw(command) - - def lusers(self, server=""): - """Send a LUSERS command.""" - self.send_raw("LUSERS" + (server and (" " + server))) - - def mode(self, target, command): - """Send a MODE command.""" - self.send_raw("MODE %s %s" % (target, command)) - - def motd(self, server=""): - """Send an MOTD command.""" - self.send_raw("MOTD" + (server and (" " + server))) - - def names(self, channels=None): - """Send a NAMES command.""" - self.send_raw("NAMES" + (channels and (" " + ",".join(channels)) or "")) - - def nick(self, newnick): - """Send a NICK command.""" - self.send_raw("NICK " + newnick) - - def notice(self, target, text): - """Send a NOTICE command.""" - # Should limit len(text) here! - self.send_raw("NOTICE %s :%s" % (target, text)) - - def oper(self, nick, password): - """Send an OPER command.""" - self.send_raw("OPER %s %s" % (nick, password)) - - def part(self, channels, message=""): - """Send a PART command.""" - channels = always_iterable(channels) - cmd_parts = [ - 'PART', - ','.join(channels), - ] - if message: cmd_parts.append(message) - self.send_raw(' '.join(cmd_parts)) - - def pass_(self, password): - """Send a PASS command.""" - self.send_raw("PASS " + password) - - def ping(self, target, target2=""): - """Send a PING command.""" - self.send_raw("PING %s%s" % (target, target2 and (" " + target2))) - - def pong(self, target, target2=""): - """Send a PONG command.""" - self.send_raw("PONG %s%s" % (target, target2 and (" " + target2))) - - def privmsg(self, target, text): - """Send a PRIVMSG command.""" - # Should limit len(text) here! - self.send_raw("PRIVMSG %s :%s" % (target, text)) - - def privmsg_many(self, targets, text): - """Send a PRIVMSG command to multiple targets.""" - # Should limit len(text) here! - self.send_raw("PRIVMSG %s :%s" % (",".join(targets), text)) - - def quit(self, message=""): - """Send a QUIT command.""" - # Note that many IRC servers don't use your QUIT message - # unless you've been connected for at least 5 minutes! - self.send_raw("QUIT" + (message and (" :" + message))) - - def send_raw(self, string): - """Send raw string to the server. - - The string will be padded with appropriate CR LF. - """ - if self.socket is None: - raise ServerNotConnectedError("Not connected.") - try: - if self.ssl: - self.ssl.write(string + "\r\n") - else: - self.socket.send(string + "\r\n") - log.debug("TO SERVER:" + string) - except socket.error: - # Ouch! - self.disconnect("Connection reset by peer.") - - def squit(self, server, comment=""): - """Send an SQUIT command.""" - self.send_raw("SQUIT %s%s" % (server, comment and (" :" + comment))) - - def stats(self, statstype, server=""): - """Send a STATS command.""" - self.send_raw("STATS %s%s" % (statstype, server and (" " + server))) - - def time(self, server=""): - """Send a TIME command.""" - self.send_raw("TIME" + (server and (" " + server))) - - def topic(self, channel, new_topic=None): - """Send a TOPIC command.""" - if new_topic is None: - self.send_raw("TOPIC " + channel) - else: - self.send_raw("TOPIC %s :%s" % (channel, new_topic)) - - def trace(self, target=""): - """Send a TRACE command.""" - self.send_raw("TRACE" + (target and (" " + target))) - - def user(self, username, realname): - """Send a USER command.""" - self.send_raw("USER %s 0 * :%s" % (username, realname)) - - def userhost(self, nicks): - """Send a USERHOST command.""" - self.send_raw("USERHOST " + ",".join(nicks)) - - def users(self, server=""): - """Send a USERS command.""" - self.send_raw("USERS" + (server and (" " + server))) - - def version(self, server=""): - """Send a VERSION command.""" - self.send_raw("VERSION" + (server and (" " + server))) - - def wallops(self, text): - """Send a WALLOPS command.""" - self.send_raw("WALLOPS :" + text) - - def who(self, target="", op=""): - """Send a WHO command.""" - self.send_raw("WHO%s%s" % (target and (" " + target), op and (" o"))) - - def whois(self, targets): - """Send a WHOIS command.""" - self.send_raw("WHOIS " + ",".join(targets)) - - def whowas(self, nick, max="", server=""): - """Send a WHOWAS command.""" - self.send_raw("WHOWAS %s%s%s" % (nick, - max and (" " + max), - server and (" " + server))) - -class DCCConnectionError(IRCError): - pass - - -class DCCConnection(Connection): - """This class represents a DCC connection. - - DCCConnection objects are instantiated by calling the dcc - method on an IRC object. - """ - def __init__(self, irclibobj, dcctype): - super(DCCConnection, self).__init__(irclibobj) - self.connected = 0 - self.passive = 0 - self.dcctype = dcctype - self.peeraddress = None - self.peerport = None - - def connect(self, address, port): - """Connect/reconnect to a DCC peer. - - Arguments: - address -- Host/IP address of the peer. - - port -- The port number to connect to. - - Returns the DCCConnection object. - """ - self.peeraddress = socket.gethostbyname(address) - self.peerport = port - self.socket = None - self.previous_buffer = "" - self.handlers = {} - self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.passive = 0 - try: - self.socket.connect((self.peeraddress, self.peerport)) - except socket.error, x: - raise DCCConnectionError("Couldn't connect to socket: %s" % x) - self.connected = 1 - if self.irclibobj.fn_to_add_socket: - self.irclibobj.fn_to_add_socket(self.socket) - return self - - def listen(self): - """Wait for a connection/reconnection from a DCC peer. - - Returns the DCCConnection object. - - The local IP address and port are available as - self.localaddress and self.localport. After connection from a - peer, the peer address and port are available as - self.peeraddress and self.peerport. - """ - self.previous_buffer = "" - self.handlers = {} - self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.passive = 1 - try: - self.socket.bind((socket.gethostbyname(socket.gethostname()), 0)) - self.localaddress, self.localport = self.socket.getsockname() - self.socket.listen(10) - except socket.error, x: - raise DCCConnectionError("Couldn't bind socket: %s" % x) - return self - - def disconnect(self, message=""): - """Hang up the connection and close the object. - - Arguments: - - message -- Quit message. - """ - if not self.connected: - return - - self.connected = 0 - try: - self.socket.close() - except socket.error: - pass - self.socket = None - self.irclibobj._handle_event( - self, - Event("dcc_disconnect", self.peeraddress, "", [message])) - self.irclibobj._remove_connection(self) - - def process_data(self): - """[Internal]""" - - if self.passive and not self.connected: - conn, (self.peeraddress, self.peerport) = self.socket.accept() - self.socket.close() - self.socket = conn - self.connected = 1 - log.debug("DCC connection from %s:%d" % ( - self.peeraddress, self.peerport)) - self.irclibobj._handle_event( - self, - Event("dcc_connect", self.peeraddress, None, None)) - return - - try: - new_data = self.socket.recv(2 ** 14) - except socket.error: - # The server hung up. - self.disconnect("Connection reset by peer") - return - if not new_data: - # Read nothing: connection must be down. - self.disconnect("Connection reset by peer") - return - - if self.dcctype == "chat": - # The specification says lines are terminated with LF, but - # it seems safer to handle CR LF terminations too. - chunks = _linesep_regexp.split(self.previous_buffer + new_data) - - # Save the last, unfinished line. - self.previous_buffer = chunks[-1] - if len(self.previous_buffer) > 2 ** 14: - # Bad peer! Naughty peer! - self.disconnect() - return - chunks = chunks[:-1] - else: - chunks = [new_data] - - command = "dccmsg" - prefix = self.peeraddress - target = None - for chunk in chunks: - log.debug("FROM PEER:" + chunk) - arguments = [chunk] - log.debug("command: %s, source: %s, target: %s, arguments: %s" % ( - command, prefix, target, arguments)) - self.irclibobj._handle_event( - self, - Event(command, prefix, target, arguments)) - - def _get_socket(self): - """[Internal]""" - return self.socket - - def privmsg(self, string): - """Send data to DCC peer. - - The string will be padded with appropriate LF if it's a DCC - CHAT session. - """ - try: - self.socket.send(string) - if self.dcctype == "chat": - self.socket.send("\n") - log.debug("TO PEER: %s\n" % string) - except socket.error: - # Ouch! - self.disconnect("Connection reset by peer.") - -class SimpleIRCClient(object): - """A simple single-server IRC client class. - - This is an example of an object-oriented wrapper of the IRC - framework. A real IRC client can be made by subclassing this - class and adding appropriate methods. - - The method on_join will be called when a "join" event is created - (which is done when the server sends a JOIN messsage/command), - on_privmsg will be called for "privmsg" events, and so on. The - handler methods get two arguments: the connection object (same as - self.connection) and the event object. - - Instance attributes that can be used by sub classes: - - ircobj -- The IRC instance. - - connection -- The ServerConnection instance. - - dcc_connections -- A list of DCCConnection instances. - """ - def __init__(self): - self.ircobj = IRC() - self.connection = self.ircobj.server() - self.dcc_connections = [] - self.ircobj.add_global_handler("all_events", self._dispatcher, -10) - self.ircobj.add_global_handler("dcc_disconnect", self._dcc_disconnect, -10) - - def _dispatcher(self, c, e): - """[Internal]""" - log.debug("irclib.py:_dispatcher:%s" % e.eventtype()) - - m = "on_" + e.eventtype() - if hasattr(self, m): - getattr(self, m)(c, e) - - def _dcc_disconnect(self, c, e): - self.dcc_connections.remove(c) - - def connect(self, server, port, nickname, password=None, username=None, - ircname=None, localaddress="", localport=0, ssl=False, ipv6=False): - """Connect/reconnect to a server. - - Arguments: - - server -- Server name. - - port -- Port number. - - nickname -- The nickname. - - password -- Password (if any). - - username -- The username. - - ircname -- The IRC name. - - localaddress -- Bind the connection to a specific local IP address. - - localport -- Bind the connection to a specific local port. - - ssl -- Enable support for ssl. - - ipv6 -- Enable support for ipv6. - - This function can be called to reconnect a closed connection. - """ - self.connection.connect(server, port, nickname, - password, username, ircname, - localaddress, localport, ssl, ipv6) - - def dcc_connect(self, address, port, dcctype="chat"): - """Connect to a DCC peer. - - Arguments: - - address -- IP address of the peer. - - port -- Port to connect to. - - Returns a DCCConnection instance. - """ - dcc = self.ircobj.dcc(dcctype) - self.dcc_connections.append(dcc) - dcc.connect(address, port) - return dcc - - def dcc_listen(self, dcctype="chat"): - """Listen for connections from a DCC peer. - - Returns a DCCConnection instance. - """ - dcc = self.ircobj.dcc(dcctype) - self.dcc_connections.append(dcc) - dcc.listen() - return dcc - - def start(self): - """Start the IRC client.""" - self.ircobj.process_forever() - - -class Event(object): - """Class representing an IRC event.""" - def __init__(self, eventtype, source, target, arguments=None): - """Constructor of Event objects. - - Arguments: - - eventtype -- A string describing the event. - - source -- The originator of the event (a nick mask or a server). - - target -- The target of the event (a nick or a channel). - - arguments -- Any event specific arguments. - """ - self._eventtype = eventtype - self._source = source - self._target = target - self._recursing = False - if arguments: - self._arguments = arguments - else: - self._arguments = [] - - def eventtype(self): - """Get the event type.""" - return self._eventtype - - def source(self): - """Get the event source.""" - return self._source - - def target(self): - """Get the event target.""" - return self._target - - def arguments(self): - """Get the event arguments.""" - return self._arguments - -_LOW_LEVEL_QUOTE = "\020" -_CTCP_LEVEL_QUOTE = "\134" -_CTCP_DELIMITER = "\001" - -_low_level_mapping = { - "0": "\000", - "n": "\n", - "r": "\r", - _LOW_LEVEL_QUOTE: _LOW_LEVEL_QUOTE -} - -_low_level_regexp = re.compile(_LOW_LEVEL_QUOTE + "(.)") - -def mask_matches(nick, mask): - """Check if a nick matches a mask. - - Returns true if the nick matches, otherwise false. - """ - nick = irc_lower(nick) - mask = irc_lower(mask) - mask = mask.replace("\\", "\\\\") - for ch in ".$|[](){}+": - mask = mask.replace(ch, "\\" + ch) - mask = mask.replace("?", ".") - mask = mask.replace("*", ".*") - r = re.compile(mask, re.IGNORECASE) - return r.match(nick) - -_special = "-[]\\`^{}" -nick_characters = string.ascii_letters + string.digits + _special - -def _ctcp_dequote(message): - """[Internal] Dequote a message according to CTCP specifications. - - The function returns a list where each element can be either a - string (normal message) or a tuple of one or two strings (tagged - messages). If a tuple has only one element (ie is a singleton), - that element is the tag; otherwise the tuple has two elements: the - tag and the data. - - Arguments: - - message -- The message to be decoded. - """ - - def _low_level_replace(match_obj): - ch = match_obj.group(1) - - # If low_level_mapping doesn't have the character as key, we - # should just return the character. - return _low_level_mapping.get(ch, ch) - - if _LOW_LEVEL_QUOTE in message: - # Yup, there was a quote. Release the dequoter, man! - message = _low_level_regexp.sub(_low_level_replace, message) - - if _CTCP_DELIMITER not in message: - return [message] - else: - # Split it into parts. (Does any IRC client actually *use* - # CTCP stacking like this?) - chunks = message.split(_CTCP_DELIMITER) - - messages = [] - i = 0 - while i < len(chunks) - 1: - # Add message if it's non-empty. - if len(chunks[i]) > 0: - messages.append(chunks[i]) - - if i < len(chunks) - 2: - # Aye! CTCP tagged data ahead! - messages.append(tuple(chunks[i + 1].split(" ", 1))) - - i = i + 2 - - if len(chunks) % 2 == 0: - # Hey, a lonely _CTCP_DELIMITER at the end! This means - # that the last chunk, including the delimiter, is a - # normal message! (This is according to the CTCP - # specification.) - messages.append(_CTCP_DELIMITER + chunks[-1]) - - return messages - -def is_channel(string): - """Check if a string is a channel name. - - Returns true if the argument is a channel name, otherwise false. - """ - return string and string[0] in "#&+!" - -def ip_numstr_to_quad(num): - """Convert an IP number as an integer given in ASCII - representation (e.g. '3232235521') to an IP address string - (e.g. '192.168.0.1').""" - n = long(num) - p = map(str, map(int, [n >> 24 & 0xFF, n >> 16 & 0xFF, - n >> 8 & 0xFF, n & 0xFF])) - return ".".join(p) - -def ip_quad_to_numstr(quad): - """Convert an IP address string (e.g. '192.168.0.1') to an IP - number as an integer given in ASCII representation - (e.g. '3232235521').""" - p = map(long, quad.split(".")) - s = str((p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]) - if s[-1] == "L": - s = s[:-1] - return s - -def nm_to_n(s): - """Get the nick part of a nickmask. - - (The source of an Event is a nickmask.) - """ - return s.split("!")[0] - -def nm_to_uh(s): - """Get the userhost part of a nickmask. - - (The source of an Event is a nickmask.) - """ - return s.split("!")[1] - -def nm_to_h(s): - """Get the host part of a nickmask. - - (The source of an Event is a nickmask.) - """ - return s.split("@")[1] - -def nm_to_u(s): - """Get the user part of a nickmask. - - (The source of an Event is a nickmask.) - """ - s = s.split("!")[1] - return s.split("@")[0] - -def parse_nick_modes(mode_string): - """Parse a nick mode string. - - The function returns a list of lists with three members: sign, - mode and argument. The sign is "+" or "-". The argument is - always None. - - Example: - - >>> parse_nick_modes("+ab-c") - [['+', 'a', None], ['+', 'b', None], ['-', 'c', None]] - """ - - return _parse_modes(mode_string, "") - -def parse_channel_modes(mode_string): - """Parse a channel mode string. - - The function returns a list of lists with three members: sign, - mode and argument. The sign is "+" or "-". The argument is - None if mode isn't one of "b", "k", "l", "v" or "o". - - Example: - - >>> parse_channel_modes("+ab-c foo") - [['+', 'a', None], ['+', 'b', 'foo'], ['-', 'c', None]] - """ - - return _parse_modes(mode_string, "bklvo") - -def _parse_modes(mode_string, unary_modes=""): - """[Internal]""" - modes = [] - arg_count = 0 - - # State variable. - sign = "" - - a = mode_string.split() - if len(a) == 0: - return [] - else: - mode_part, args = a[0], a[1:] - - if mode_part[0] not in "+-": - return [] - for ch in mode_part: - if ch in "+-": - sign = ch - elif ch == " ": - pass - elif ch in unary_modes: - if len(args) >= arg_count + 1: - modes.append([sign, ch, args[arg_count]]) - arg_count = arg_count + 1 - else: - modes.append([sign, ch, None]) - else: - modes.append([sign, ch, None]) - return modes - -def _ping_ponger(connection, event): - """[Internal]""" - connection.pong(event.target()) - -# Numeric table mostly stolen from the Perl IRC module (Net::IRC). -numeric_events = { - "001": "welcome", - "002": "yourhost", - "003": "created", - "004": "myinfo", - "005": "featurelist", # XXX - "200": "tracelink", - "201": "traceconnecting", - "202": "tracehandshake", - "203": "traceunknown", - "204": "traceoperator", - "205": "traceuser", - "206": "traceserver", - "207": "traceservice", - "208": "tracenewtype", - "209": "traceclass", - "210": "tracereconnect", - "211": "statslinkinfo", - "212": "statscommands", - "213": "statscline", - "214": "statsnline", - "215": "statsiline", - "216": "statskline", - "217": "statsqline", - "218": "statsyline", - "219": "endofstats", - "221": "umodeis", - "231": "serviceinfo", - "232": "endofservices", - "233": "service", - "234": "servlist", - "235": "servlistend", - "241": "statslline", - "242": "statsuptime", - "243": "statsoline", - "244": "statshline", - "250": "luserconns", - "251": "luserclient", - "252": "luserop", - "253": "luserunknown", - "254": "luserchannels", - "255": "luserme", - "256": "adminme", - "257": "adminloc1", - "258": "adminloc2", - "259": "adminemail", - "261": "tracelog", - "262": "endoftrace", - "263": "tryagain", - "265": "n_local", - "266": "n_global", - "300": "none", - "301": "away", - "302": "userhost", - "303": "ison", - "305": "unaway", - "306": "nowaway", - "311": "whoisuser", - "312": "whoisserver", - "313": "whoisoperator", - "314": "whowasuser", - "315": "endofwho", - "316": "whoischanop", - "317": "whoisidle", - "318": "endofwhois", - "319": "whoischannels", - "321": "liststart", - "322": "list", - "323": "listend", - "324": "channelmodeis", - "329": "channelcreate", - "331": "notopic", - "332": "currenttopic", - "333": "topicinfo", - "341": "inviting", - "342": "summoning", - "346": "invitelist", - "347": "endofinvitelist", - "348": "exceptlist", - "349": "endofexceptlist", - "351": "version", - "352": "whoreply", - "353": "namreply", - "361": "killdone", - "362": "closing", - "363": "closeend", - "364": "links", - "365": "endoflinks", - "366": "endofnames", - "367": "banlist", - "368": "endofbanlist", - "369": "endofwhowas", - "371": "info", - "372": "motd", - "373": "infostart", - "374": "endofinfo", - "375": "motdstart", - "376": "endofmotd", - "377": "motd2", # 1997-10-16 -- tkil - "381": "youreoper", - "382": "rehashing", - "384": "myportis", - "391": "time", - "392": "usersstart", - "393": "users", - "394": "endofusers", - "395": "nousers", - "401": "nosuchnick", - "402": "nosuchserver", - "403": "nosuchchannel", - "404": "cannotsendtochan", - "405": "toomanychannels", - "406": "wasnosuchnick", - "407": "toomanytargets", - "409": "noorigin", - "411": "norecipient", - "412": "notexttosend", - "413": "notoplevel", - "414": "wildtoplevel", - "421": "unknowncommand", - "422": "nomotd", - "423": "noadmininfo", - "424": "fileerror", - "431": "nonicknamegiven", - "432": "erroneusnickname", # Thiss iz how its speld in thee RFC. - "433": "nicknameinuse", - "436": "nickcollision", - "437": "unavailresource", # "Nick temporally unavailable" - "441": "usernotinchannel", - "442": "notonchannel", - "443": "useronchannel", - "444": "nologin", - "445": "summondisabled", - "446": "usersdisabled", - "451": "notregistered", - "461": "needmoreparams", - "462": "alreadyregistered", - "463": "nopermforhost", - "464": "passwdmismatch", - "465": "yourebannedcreep", # I love this one... - "466": "youwillbebanned", - "467": "keyset", - "471": "channelisfull", - "472": "unknownmode", - "473": "inviteonlychan", - "474": "bannedfromchan", - "475": "badchannelkey", - "476": "badchanmask", - "477": "nochanmodes", # "Channel doesn't support modes" - "478": "banlistfull", - "481": "noprivileges", - "482": "chanoprivsneeded", - "483": "cantkillserver", - "484": "restricted", # Connection is restricted - "485": "uniqopprivsneeded", - "491": "nooperhost", - "492": "noservicehost", - "501": "umodeunknownflag", - "502": "usersdontmatch", -} - -generated_events = [ - # Generated events - "dcc_connect", - "dcc_disconnect", - "dccmsg", - "disconnect", - "ctcp", - "ctcpreply", -] - -protocol_events = [ - # IRC protocol events - "error", - "join", - "kick", - "mode", - "part", - "ping", - "privmsg", - "privnotice", - "pubmsg", - "pubnotice", - "quit", - "invite", - "pong", -] - -all_events = generated_events + protocol_events + numeric_events.values() - -# from jaraco.util.itertools -def always_iterable(item): - """ - Given an object, always return an iterable. If the item is not - already iterable, return a tuple containing only the item. - - >>> always_iterable([1,2,3]) - [1, 2, 3] - >>> always_iterable('foo') - ('foo',) - >>> always_iterable(None) - (None,) - >>> always_iterable(xrange(10)) - xrange(10) - """ - if isinstance(item, basestring) or not hasattr(item, '__iter__'): - item = item, - return item - -# from jaraco.util.string -class FoldedCase(str): - """ - A case insensitive string class; behaves just like str - except compares equal when the only variation is case. - >>> s = FoldedCase('hello world') - - >>> s == 'Hello World' - True - - >>> 'Hello World' == s - True - - >>> s.index('O') - 4 - - >>> s.split('O') - ['hell', ' w', 'rld'] - - >>> names = map(FoldedCase, ['GAMMA', 'alpha', 'Beta']) - >>> names.sort() - >>> names - ['alpha', 'Beta', 'GAMMA'] - """ - def __lt__(self, other): - return self.lower() < other.lower() - - def __gt__(self, other): - return self.lower() > other.lower() - - def __eq__(self, other): - return self.lower() == other.lower() - - def __hash__(self): - return hash(self.lower()) - - # cache lower since it's likely to be called frequently. - def lower(self): - self._lower = super(FoldedCase, self).lower() - self.lower = lambda: self._lower - return self._lower - - def index(self, sub): - return self.lower().index(sub.lower()) - - def split(self, splitter=' ', maxsplit=0): - pattern = re.compile(re.escape(splitter), re.I) - return pattern.split(self, maxsplit) - -class IRCFoldedCase(FoldedCase): - """ - A version of FoldedCase that honors the IRC specification for lowercased - strings (RFC 1459). - - >>> IRCFoldedCase('Foo^').lower() - 'foo~' - >>> IRCFoldedCase('[this]') == IRCFoldedCase('{THIS}') - True - """ - translation = string.maketrans( - string.ascii_uppercase + r"[]\^", - string.ascii_lowercase + r"{}|~", - ) - - def lower(self): - return self.translate(self.translation) - -# for compatibility -def irc_lower(str): - return IRCFoldedCase(str).lower() - -def total_seconds(td): - """ - Python 2.7 adds a total_seconds method to timedelta objects. - See http://docs.python.org/library/datetime.html#datetime.timedelta.total_seconds - """ - try: - result = td.total_seconds() - except AttributeError: - result = (td.microseconds + (td.seconds + td.days * 24 * 3600) * 10**6) / 10**6 - return result diff --git a/ircbot/logging.cfg b/ircbot/logging.cfg deleted file mode 100644 index 1e6db8d..0000000 --- a/ircbot/logging.cfg +++ /dev/null @@ -1,58 +0,0 @@ -[loggers] -keys = root,drbotzo,irclib,dr_botzo.markov,drbotzo.weather,drbotzo.dispatch - -[handlers] -keys = stdout,logfile - -[formatters] -keys = verbose - -[logger_root] -level = INFO -handlers = stdout,logfile - -[logger_irclib] -level = INFO -handlers = stdout,logfile -propagate = 0 -qualname = irclib - -[logger_drbotzo] -level = INFO -handlers = stdout,logfile -propagate = 0 -qualname = drbotzo - -[logger_dr_botzo.markov] -level = DEBUG -handlers = stdout,logfile -propagate = 0 -qualname = dr_botzo.markov - -[logger_drbotzo.dispatch] -level = DEBUG -handlers = stdout,logfile -propagate = 0 -qualname = drbotzo.dispatch - -[logger_drbotzo.weather] -level = DEBUG -handlers = stdout,logfile -propagate = 0 -qualname = drbotzo.weather - -[handler_stdout] -class = StreamHandler -level = DEBUG -formatter = verbose -args = '' - -[handler_logfile] -class = FileHandler -level = DEBUG -formatter = verbose -args = ('dr.botzo.irc.log', 'a') - -[formatter_verbose] -format = [%(levelname)-8s %(asctime)s %(name)s] %(message)s -datefmt = diff --git a/scripts/factfile-to-facts.py b/scripts/factfile-to-facts.py deleted file mode 100644 index 3f0ad7c..0000000 --- a/scripts/factfile-to-facts.py +++ /dev/null @@ -1,61 +0,0 @@ -""" -factfiles-to-facts - insert facts from flat file to database -Copyright (C) 2010 Brian S. Stephan - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -""" - -import argparse -import getpass -import os -import sqlite3 -import socket -import sys - -if __name__ == '__main__': - parser = argparse.ArgumentParser() - parser.add_argument('-f', '--factfile') - parser.add_argument('-c', '--category') - args = parser.parse_args() - - if args.factfile == None or args.category == None: - print('script needs both -c (category) and -f (filename)') - sys.exit(2) - - # database - dbfile = 'dr.botzo.data' - db = sqlite3.connect(dbfile) - - # open file - if os.path.isfile(args.factfile): - with open(args.factfile, 'r') as file: - lines = file.readlines() - for line in lines: - try: - line = line.rstrip() - print('inserting \'' + line + '\'') - db.execute('''INSERT INTO facts_facts (category, fact, who, userhost) - VALUES (?,?,?,?)''', (args.category, line.decode('utf-8', 'ignore'), getpass.getuser(), getpass.getuser() + '@' + socket.getfqdn())) - except sqlite3.Error as e: - db.rollback() - print("sqlite error: " + str(e)) - sys.exit(2) - - db.commit() - else: - print('filename \'' + whats[0] + '\' doesn\'t exist') - - -# vi:tabstop=4:expandtab:autoindent -# kate: indent-mode python;indent-width 4;replace-tabs on; diff --git a/scripts/import-file-into-markov_chain.py b/scripts/import-file-into-markov_chain.py deleted file mode 100644 index e55e048..0000000 --- a/scripts/import-file-into-markov_chain.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -import-file-into-markov_chain.py -Copyright (C) 2011 Brian S. Stephan - -This program is free software: you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation, either version 3 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. - -You should have received a copy of the GNU General Public License -along with this program. If not, see . -""" - -import argparse -import os -import sqlite3 -import sys - -parser = argparse.ArgumentParser(description='Import lines into the specified context_id.') -parser.add_argument('context_id', metavar='CONTEXT', type=int, nargs=1) -args = parser.parse_args() -print(args.context_id[0]) - -db = sqlite3.connect('dr.botzo.data') -db.row_factory = sqlite3.Row - -cur = db.cursor() -statement = 'INSERT INTO markov_chain (k1, k2, v, context_id) VALUES (?, ?, ?, ?)' -for line in sys.stdin: - # set up the head of the chain - w1 = '__start1' - w2 = '__start2' - - # for each word pair, add the next word to the dictionary - for word in line.split(): - try: - cur.execute(statement, (w1.decode('utf-8', 'replace'), w2.decode('utf-8', 'replace'), word.decode('utf-8', 'replace'), args.context_id[0])) - except sqlite3.Error as e: - db.rollback() - print("sqlite error: " + str(e)) - raise - - w1, w2 = w2, word - - try: - cur.execute(statement, (w1.decode('utf-8', 'replace'), w2.decode('utf-8', 'replace'), '__stop', args.context_id[0])) - db.commit() - except sqlite3.Error as e: - db.rollback() - print("sqlite error: " + str(e)) - raise - -# vi:tabstop=4:expandtab:autoindent