#!/usr/bin/python # -*- coding: utf-8 -*- # Copyright 2009 Martin Borho <*****@*****.**> # GPL - see License.txt for details import ConfigParser from waveapi import events from waveapi import robot from waveapi import appengine_robot_runner from baas.core.plugins import PluginLoader from baas.core.helpers import * config = ConfigParser.ConfigParser() config.read("../../baas.cfg") pluginHnd = PluginLoader(config=config, format="wave") pluginHnd.load_plugins() pluginHnd.load_map() pluginHnd.load_help() commands= pluginHnd.commands def OnRobotAdded(event, wavelet): """Invoked when the robot has been added.""" reply = "type 'buddy:help' for available commands" wavelet.reply(reply) def OnBlipSubmitted(event, wavelet): blip = event.blip text = blip.text.strip() reply = '' if text and text == 'buddy:help': help = "\n\n%s" % pluginHnd.help
class Bot(object): def __init__(self, config, ): self.config = config self.debug = self.config.getint('app','debug') self._load_plugins() me = jid.JID(self.config.get('bot','jid')) self.factory = client.basicClientFactory(me, self.config.get('bot','pwd')) self.factory.addBootstrap('//event/stream/authd',self.authd) self.factory.addBootstrap(client.BasicAuthenticator.AUTH_FAILED_EVENT, self.authfailedEvent) reactor.connectTCP(self.config.get('bot','server'), self.config.getint('bot','port'), self.factory) reactor.run() self.reactor = reactor def _load_plugins(self): self.pluginHnd = PluginLoader(config=self.config) self.pluginHnd.load_plugins() self.pluginHnd.load_map() self.pluginHnd.load_help() self.commands= self.pluginHnd.commands def gotMessage(self, message): if self.debug: print message.toXml() text = None user = message["from"] # sorry for the __str__(), makes unicode happy for e in message.elements(): if e.name == "body": text = unicode(e.__str__()) break reply = "type 'help' for available commands" try: if text and text.find(':')+1: cmd,args=text.split(':',1) commando_func = self.commands.get(cmd) if commando_func: result_msg = commando_func(args) reply = result_msg else: reply = 'Uups, commando not known\n' elif text and text == 'help': reply += "\n\n%s" % self.pluginHnd.help except: reply = "Error occured" response = domish.Element((None, 'message')) response['to'] = user response["type"] = 'chat' response.addElement('body', content=reply) self.xmlstream.send(response) def authfailedEvent(self, xmlstream): global reactor try: self.reactor.stop() except: pass sys.exit('Auth failed!') def authd(self, xmlstream): # need to send presence so clients know we're # actually online. presence = domish.Element(('jabber:client', 'presence')) presence.addElement('status').addContent('Online') xmlstream.send(presence) # add a callback for the messages xmlstream.addObserver('/message', self.gotMessage) self.xmlstream = xmlstream
def _load_plugins(self): self.pluginHnd = PluginLoader(config=self.config) self.pluginHnd.load_plugins() self.pluginHnd.load_map() self.pluginHnd.load_help() self.commands= self.pluginHnd.commands
# -*- coding: utf-8 -*- # Copyright 2009 Martin Borho <*****@*****.**> # GPL - see License.txt for details from nose.tools import * from baas.core.plugins import PluginLoader pluginXmmp = PluginLoader() pluginXmmp.load_plugins() pluginXmmp.load_map() pluginXmmp.load_help() pluginXmmp.load_limits() pluginWave = PluginLoader(format="wave") pluginWave.load_plugins() pluginWave.load_map() pluginWave.load_help() pluginWave.load_limits() pluginRaw = PluginLoader(format="raw") pluginRaw.load_plugins() pluginRaw.load_map() pluginRaw.load_help() pluginRaw.load_limits() def test_xmmp_handler(): assert_true(len(pluginXmmp.plugins) > 1, 'no plugins loaded, xmpp (default) mode') def test_wave_handler(): assert_true(len(pluginWave.plugins) > 1, 'no plugins loaded, wave mode') def test_raw_handler():