From 2d12afba41ae8521af67a4fc64d4b338613077da Mon Sep 17 00:00:00 2001 From: Arthur Furlan Date: Fri, 3 Dec 2010 16:55:57 -0200 Subject: [PATCH] Add a base class for MUCs and a usage example --- examples/muc.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 examples/muc.py diff --git a/examples/muc.py b/examples/muc.py new file mode 100644 index 0000000..3459a29 --- /dev/null +++ b/examples/muc.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +# coding: utf-8 + +# Copyright (C) 2010 Arthur Furlan +# +# 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. +# +# On Debian systems, you can find the full text of the license in +# /usr/share/common-licenses/GPL-3 + + +from jabberbot import JabberBot, botcmd +from datetime import datetime +import re + +class MUCJabberBot(JabberBot): + + ''' Add features in JabberBot to allow it to handle specific + caractheristics of multiple users chatroom (MUC). ''' + + def __init__(self, *args, **kwargs): + ''' Initialize variables. ''' + + # answer only direct messages or not? + self.only_direct = kwargs.get('only_direct', False) + try: + del kwargs['only_direct'] + except KeyError: + pass + + # initialize jabberbot + super(MUCJabberBot, self).__init__(*args, **kwargs) + + # create a regex to check if a message is a direct message + user, domain = str(self.jid).split('@') + self.direct_message_re = re.compile('^%s(@%s)?[^\w]? ' \ + % (user, domain)) + + def callback_message(self, conn, mess): + ''' Changes the behaviour of the JabberBot in order to allow + it to answer direct messages. This is used often when it is + connected in MUCs (multiple users chatroom). ''' + + message = mess.getBody() + if not message: + return + + if self.direct_message_re.match(message): + mess.setBody(' '.join(message.split(' ', 1)[1:])) + return super(MUCJabberBot, self).callback_message(conn, mess) + elif not self.only_direct: + return super(MUCJabberBot, self).callback_message(conn, mess) + + +class Example(MUCJabberBot): + + @botcmd + def date(self, mess, args): + reply = datetime.now().strftime('%Y-%m-%d') + self.send_simple_reply(mess, reply) + + +if __name__ == '__main__': + + username = 'username' + password = 'password' + nickname = 'nickname' + chatroom = 'chatroom' + + mucbot = Example(username, password, only_direct=True) + mucbot.join_room(chatroom, nickname) + mucbot.serve_forever() -- 2.11.4.GIT