self.log.debug(self.presence)
if pres['type'] == 'available':
while self.inbox.get(nick, []):
- self.send(self.inbox.get(nick).pop())
+ self.send(self.bot.room,
+ self.inbox.get(nick).pop(),
+ mtype='groupchat')
self.inbox.pop(nick)
@botcmd
!tell queue : messages in queue
!tell <nick> <msg> : append <msg> to <nick> in queue"""
if not len(args):
- self.send('Missing arguments:\n{}'.format(self.tell.__doc__))
+ msg = 'Missing arguments:\n{}'.format(self.tell.__doc__)
+ self.reply(message, msg)
return
if len(args) == 1:
if args[0] == 'queue':
- self.send('\n'.join(['{0}:\n\t{1}'.format(k, '\n'.join(v))
- for k, v in self.inbox.items()]))
+ msg = '\n'.join(['{0}:\n\t{1}'.format(k, '\n'.join(v))
+ for k, v in self.inbox.items()])
+ self.reply(message, msg)
return
if args[0] == 'purge':
sender = message['from'].resource
self.online = set()
return
if len(args) < 2:
- self.send('Please provide a message:\n{}'.format(self.tell.__doc__))
+ msg = 'Please provide a message:\n{}'.format(self.tell.__doc__)
+ self.reply(message, msg)
return
+ self._handle_msg(message)
+
+ def _handle_msg(self, message):
sender = message['from'].resource
recipient = message['body'].split()[1]
tell_msg = ' '.join(message['body'].split()[2:])
self.log.debug('{0}: {1}'.format(recipient, tell_msg))
- letter = '{0}, {1} told me to tell you: {2}'.format(recipient, sender, tell_msg)
+ letter = '{0}, {1} wanted you to know: {2}'.format(recipient, sender, tell_msg)
if (self.presence.get(recipient) and
self.presence[recipient][1] == 'available'):
return
# Updating self.seen
self.seen[feed_id] = entries
if len(text) > 1:
- self.plugin.send({'mhtml':'<br />'.join(xhtml), 'mbody':'\n'.join(text)})
+ self.plugin.send(self.plugin.bot.room,
+ {'mhtml':'<br />'.join(xhtml), 'mbody':'\n'.join(text)},
+ mtype='groupchat')
def run(self):
while not self.thread_killed:
self.th_mon.thread_killed = True
@botcmd
- def feeds(self, message, args):
+ def feeds(self, rcv, args):
"""feeds monitors debian project related feeds.
!feeds : registred feeds list
!feeds last : last check time"""
if 'last' in args:
- self.send('Last feeds check: %s' % self.th_mon.last_check)
+ self.reply(rcv, 'Last feeds check: %s' % self.th_mon.last_check)
return
html = ['<a href="{0}">{1}</a>'.format(html_escape(u),
html_escape(u[7:])
'mbody': 'Feeds:\n' + '\n'.join(Feeds.FEEDS),
'mhtml': 'Feeds:<br />' + '<br />'.join(html),
}
- self.send(msg)
+ self.reply(rcv, msg)
Plugin.__init__(self, bot)
@botcmd
- def ping(self, message, args):
+ def ping(self, rcv, args):
"""ping's answering a pong showing the bot is alive.
!ping : You'll get back "pong"!
"""
'mhtml':'!<a href="{0.PongURL}">pong</a>'.format(self),
'mbody':'!pong',
}
- self.send(msg)
+ self.reply(rcv, msg)
# VIM MODLINE
# vim: ai ts=4 sw=4 sts=4 expandtab
from .sid import botcmd
-class Plugin(object):
+class Plugin:
+ """Simple Plugin object to derive from:
+ Exposes the bot object and its logger
+ Provides some send helpers
+ """
def __init__(self, bot):
self.bot = bot
self.log = bot.log
- def send(self, msg):
- """
- Send msg to the current groupchat defined in self.bot.room
+ def send(self, dest, msg, mtype='chat'):
+ """Send msg to dest
msg = {
mbody: 'text',
mhtml: '<b>text</b>, # optional'
if isinstance(msg, str):
msg = {'mbody':msg}
msg.setdefault('mhtml', None)
- self.bot.send_message(mto=self.bot.room,
- mtype='groupchat',
+ self.bot.send_message(mto=dest,
+ mtype=mtype,
**msg)
+ def reply(self, rcv, msg):
+ """Smart reply to message received.
+ Replies <msg> in private or on the muc depending on <rcv>
+ """
+ to = rcv['from']
+ if rcv['type'] == 'groupchat':
+ to = rcv['mucroom']
+ self.send(to, msg, mtype=rcv['type'])
+
def shutdown(self):
pass
self.room = room
self.nick = nick
self.__set_logger(log_file, log_level)
+ self.__seen = dict()
self.register_plugin('xep_0030') # Service Discovery
self.register_plugin('xep_0045') # Multi-User Chat
self.register_plugin('xep_0199') # self Ping
# and the XML streams are ready for use. We want to
# listen for this event so that we we can initialize
# our roster.
- self.add_event_handler("session_start", self.start)
+ self.add_event_handler('session_start', self.start)
# Handles MUC message and dispatch
- self.add_event_handler("groupchat_message", self.muc_message)
+ self.add_event_handler('message', self.message)
+ self.add_event_handler('got_online', self._view)
# Discover bot internal command (ie. help)
for name, value in inspect.getmembers(self):
self.log.setLevel(log_level)
self.log.debug('set logger, log level : %s' % log_level)
- def muc_message(self, msg):
- # ignore message from self
- body = msg['body'].strip()
- mucfrom = msg['mucnic']
+ def message(self, msg):
+ if msg['type'] not in ('groupchat', 'chat'):
+ self.log.warning('Unhandled message')
+ return
if msg['mucnick'] == self.nick:
return
+ body = msg['body'].strip()
if not body.startswith(MUCBot.prefix):
return
+ if msg['from'] not in self.__seen:
+ self.log.warning('Will not handle message '
+ 'from unseen jid: %s' % msg['from'])
+ #return
args = body[1:].split()
cmd = args.pop(0)
if cmd not in self.commands:
if self.log.level < 10 and reply:
self.send_message(mto=msg['from'].bare, mbody=reply, mtype='groupchat')
+ def _view(self, pres):
+ nick = pres['from']
+ status = (pres['type'], pres['status'])
+ self.__seen.update({nick: status})
+
def start(self, event):
"""
Process the session_start event.
text = self.commands[args[0]].__doc__.strip() or 'undocumented'
else:
text = 'That command is not defined.'
- self.send_message(mto=message['from'].bare, mbody=text, mtype='groupchat')
+ if message['type'] == 'groupchat':
+ to = message['from'].bare
+ else:
+ to = message['from']
+ self.send_message(mto=to, mbody=text, mtype=message['type'])