-
Notifications
You must be signed in to change notification settings - Fork 0
/
mucjabberbot.py
45 lines (33 loc) · 1.44 KB
/
mucjabberbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import re
from .jabberbot import JabberBot
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 _process_message(self, mess):
pass
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
self._process_message(mess)
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)