def main(): import sys import string from time import time args = sys.argv try: plugin_name = args[1] except IndexError: print "Usage: %s <plugin_name> <message>" % args[0] quit() cfg = Conf('../config.ini', 'LOCAL') cfg.set('debug', True) bridge = FakeBridge() bridge.setConfig(cfg=cfg) try: of = ObjectFactory() plug = of.create(plugin_name, mod='plugins', inst="Plugin", args=[bridge]) except ImportError as ie: print "Couldn't load plugin: %s" % plugin_name print "Args:", ie.args quit() msg = unicode(' '.join(args[2:]), 'utf-8') shout = Shout(1, 4711, 'PluginTestUser', msg, time()) plug.setup() bridge.plugins[plugin_name] = plug bridge.trigger_plugin_event("Message", shout)
def start_shoutbridge(): # Read command line options. (options, args) = get_options() # Read configuration. configfile = options.config or 'config.ini' configsection = options.section or 'LOCAL' cfg = Conf(configfile, configsection) cfg.set_items(vars(options)) if cfg.get_bool('verbose'): print "Shoutbridge started..." # Setup Shoutbox of = ObjectFactory() sbox = of.create(cfg.shoutbox_type, mod='shoutbox', inst="Shoutbox") #sbox.setConfig(cfg) # Setup xmpp bridge bridge = of.create(cfg.bridge_type, mod='bridges', inst="XmppBridge") bridge.setup(sbox, cfg) # Start bridge bridge.listen() if cfg.get_bool('verbose'): print "Shoutbridge ended."
def load_plugins(self, plugs): """ Imports all configured Plugins and adds them to the list of active plugins. """ if not plugs: return if not self.of: self.of = ObjectFactory() pluginlist = plugs.split(',') for p in pluginlist: try: plug = self.of.create(p + "Plugin", mod='plugins', inst='Plugin', args=[self]) except ImportError, ie: self.logprint("Couldn't load plugin:", p, ie) continue plug.setup() self.logprint("Loaded plugin:", plug.name, "\n", plug.description) self.plugins[p] = plug
class Twippy(BridgeClass): #: Conf object with configuration options to use. cfg = None #: List of references to loadd bot Plugins. plugins = dict() #: An ObjectFactory instance to use for loading plugins. of = None #: List of twitter accounts to ignore messages from. ignorelist = [] def __init__(self): # Read command line options. (options, args) = self.get_options() # Read configuration. configfile = options.config or 'config.ini' configsection = options.section or 'LOCAL' self.cfg = Conf(configfile, configsection) self.cfg.set_items(vars(options)) try: for s in self.cfg.ignorelist.split(','): self.ignorelist.append(s.strip()) except AttributeError: pass # Setup database try: debug = self.cfg.get_bool('debug') except AttributeError: debug = False self.db = HaliDb('sqlite:///extras/twippy.db', debug) # Load list of plugins if not self.load_plugins(self.cfg.plugins): print "No plugins loaded. Quitting." sys.exit(1) # setup twitter self.setup_twitter() def get_options(self): parser = OptionParser(version="%prog 1.0") parser.add_option("-c", "--config", dest="config", default="config.ini", help="Read configuration from FILE", metavar="FILE") parser.add_option("-S", "--section", dest="section", default="LOCAL", help="Read configuration from SECTION", metavar="SECTION") parser.add_option("-D", "--debug", action="store_true", dest="debug", default=False, help="Print RAW data sent and received on the stream.") return parser.parse_args() def load_plugins(self, plugs): """ Imports all configured Plugins and adds them to the list of active plugins. """ if not plugs: return if not self.of: self.of = ObjectFactory() pluginlist = plugs.split(',') for p in pluginlist: try: plug = self.of.create(p + "Plugin", mod='plugins', inst='Plugin', args=[self]) except ImportError, ie: self.logprint("Couldn't load plugin:", p, ie) continue plug.setup() self.logprint("Loaded plugin:", plug.name, "\n", plug.description) self.plugins[p] = plug return len(self.plugins)
class XmppBridge(BridgeClass): """ Abstract interface class for implementing a XMPP (Jabber) connection. This interface should be sub-classed to implement a specific XMPP library to do the actual communcation. """ #: Name of the client program, returned as information on version queries. client_name = "Shoutbridge" #: Version of the client running, returned as information on version queries. client_version = "1.0" #: Reference to a Shoutbox object for web chat connectivity. shoutbox = None #: Roster of known users in the chat. roster = dict() #: Conf object with configuration options to use. cfg = None #: A unix timestamp defining the last time the client did something. last_time = 0 #: List of references to loadd bot Plugins. plugins = dict() #: An ObjectFactory instance to use for loading plugins. of = None #: List of JIDs to ignore messages from. ignorelist = [] def __init__(self, sbox=None, cfg=None): """ Instantiate an XMPP bridge using XMPP login details and a shoutbox object. """ if sbox: self.setShoutbox(sbox) #self.shoutbox.setDb(self.db) if cfg: self.setConfig(cfg) self.shoutbox.setConfig(cfg) # Update last active time. self.update_last_time() def setConfig(self, cfg): """ Sets the config object and some default class attributes from config. """ self.cfg = cfg self.login = self.cfg.xmpp_login self.passwd = self.cfg.xmpp_pass self.host = self.cfg.xmpp_host self.port = self.cfg.xmpp_port (self.room, foo, self.resource) = self.cfg.xmpp_room.rpartition('/') self.roomjid = self.cfg.xmpp_room self.current_nick = self.resource try: for s in self.cfg.ignorelist.split(','): self.ignorelist.append(s.strip()) except AttributeError: pass def setShoutbox(self, sbox): """ Sets the shoutbox instance object for shoutbox communication. """ self.shoutbox = sbox def setup(self, sbox=None, cfg=None): """ Initializes the bridge and sets Shoutbox and Configuration object. Also loads plugins and starts up the XMPP connection. """ # Setup database try: debug = cfg.get_bool('debug') except AttributeError: debug = False self.db = HaliDb('sqlite:///extras/halidata.db', debug) if sbox: self.setShoutbox(sbox) self.shoutbox.setDb(self.db) if cfg: self.setConfig(cfg) self.logprint("Config:", cfg) self.shoutbox.setConfig(cfg) # Load list of plugins self.load_plugins(self.cfg.plugins) # Make an XMPP connection self.make_connection() @protectedMethod def load_plugins(self, plugs): """ Imports all configured Plugins and adds them to the list of active plugins. """ if not plugs: return if not self.of: self.of = ObjectFactory() pluginlist = plugs.split(',') for p in pluginlist: try: plug = self.of.create(p + "Plugin", mod='plugins', inst='Plugin', args=[self]) except ImportError: self.logprint("Couldn't load plugin:", p) continue plug.setup() self.logprint("Loaded plugin:", plug.name, "\n", plug.description) self.plugins[p] = plug def trigger_plugin_event(self, event, obj): """ Triggers given event on all loaded plugins with obj as argument. """ if not obj or not event: return for plugin_name, plugin in self.plugins.items(): try: text = obj.text nick = obj.name except AttributeError: text = obj nick = "" try: (cmd, comobj, func) = self.get_plugin_handler(plugin, event, text) except AttributeError, ae: self.logprint("Attribute Error encountered in plugin:", plugin_name, ae) continue except UnicodeDecodeError, ude: self.logprint("UnicodeDecodeError on text:", text, ude) continue try: if func: self.logprint("Calling plugin:", plugin_name, event, cmd) plugin.sender_nick = nick func(obj, cmd, comobj) except Exception as e: self.logprint("Plugin raised exception:", plugin_name, "\n", type(e), e)
class Twippy(BridgeClass): #: Conf object with configuration options to use. cfg = None #: List of references to loadd bot Plugins. plugins = dict() #: An ObjectFactory instance to use for loading plugins. of = None #: List of twitter accounts to ignore messages from. ignorelist = [] def __init__(self): # Read command line options. (options, args) = self.get_options() # Read configuration. configfile = options.config or 'config.ini' configsection = options.section or 'LOCAL' self.cfg = Conf(configfile, configsection) self.cfg.set_items(vars(options)) try: for s in self.cfg.ignorelist.split(','): self.ignorelist.append(s.strip()) except AttributeError: pass # Setup database try: debug = self.cfg.get_bool('debug') except AttributeError: debug = False self.db = HaliDb('sqlite:///extras/twippy.db', debug) # Load list of plugins if not self.load_plugins(self.cfg.plugins): print "No plugins loaded. Quitting." sys.exit(1) # setup twitter self.setup_twitter() def get_options(self): parser = OptionParser(version="%prog 1.0") parser.add_option("-c", "--config", dest="config", default="config.ini", help="Read configuration from FILE", metavar="FILE") parser.add_option("-S", "--section", dest="section", default="LOCAL", help="Read configuration from SECTION", metavar="SECTION") parser.add_option( "-D", "--debug", action="store_true", dest="debug", default=False, help="Print RAW data sent and received on the stream.") return parser.parse_args() def load_plugins(self, plugs): """ Imports all configured Plugins and adds them to the list of active plugins. """ if not plugs: return if not self.of: self.of = ObjectFactory() pluginlist = plugs.split(',') for p in pluginlist: try: plug = self.of.create(p + "Plugin", mod='plugins', inst='Plugin', args=[self]) except ImportError, ie: self.logprint("Couldn't load plugin:", p, ie) continue plug.setup() self.logprint("Loaded plugin:", plug.name, "\n", plug.description) self.plugins[p] = plug return len(self.plugins)