-
Notifications
You must be signed in to change notification settings - Fork 0
/
botbase.py
executable file
·855 lines (727 loc) · 31.8 KB
/
botbase.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import sys
import thread
import time
import inspect
import logging
import traceback
import atexit
import json
import collections
import cgi
import gettext
import linecache
import datetime
try:
import xmpp
except ImportError:
print >> sys.stderr, """
You need to install xmpppy from http://xmpppy.sf.net/.
On Debian-based systems, install the python-xmpp package.
"""
sys.exit(-1)
class BotBase(object):
# Show types for presence
AVAILABLE, AWAY, CHAT = None, 'away', 'chat'
DND, XA, OFFLINE = 'dnd', 'xa', 'unavailable'
PING_FREQUENCY = 30 # Set to the number of seconds, e.g. 60.
PING_TIMEOUT = 2 # Seconds to wait for a response.
########## Constructor ##########
def __init__(self, username, password, nickname=None, timezone='UTC', text_color=None, localization=None, res=None, debug=False, privatedomain=False, acceptownmsgs=False, handlers=None):
# TODO sort this initialisation thematically
self.__debug = debug
self.log = logging.getLogger(__name__)
self.__username = username
self.__password = password
self.nickname = nickname
self.timezone = timezone
self.jid = xmpp.JID(self.__username)
self.res = (res or self.__class__.__name__)
self.conn = None
self.readyTs = 0
self.__finished = False
self.__show = None
self.__status = None
self.__seen = {}
self.__threads = {}
self.__lastping = time.time()
self.__privatedomain = privatedomain
self.__acceptownmsgs = acceptownmsgs
self.text_color = text_color
self.currentTopic = ""
self.defaultLocalization = "en"
self.localizations = ["en", "de"]
self.muc_users = {}
self.roster = None
self.myroster = {}
self.muc_channels = []
self.handlers = (handlers or [('message', self.callback_message), ('presence', self.callback_presence)])
# Collect commands from source
self.commands = {}
for name, value in inspect.getmembers(self, inspect.ismethod):
if getattr(value, '_jabberbot_command', False):
name = getattr(value, '_jabberbot_command_name')
self.log.debug('Registered command: %s' % name)
self.commands[name] = value
if not localization:
localization = self.defaultLocalization
self.loadLocalization(localization)
self.MSG_AUTHORIZE_ME = _('Hey there. You are not yet on my roster. Authorize my request and I will do the same.')
self.MSG_NOT_AUTHORIZED = _('You did not authorize my subscription request. Access denied.')
self.MSG_HELP_TAIL = _('Type help *command name* to get more info about that specific command.')
self.MSG_HELP_UNDEFINED_COMMAND = _('That command is not defined.')
self.stats = self.loadJSON('save_stats.dat', { 'messageCount': 0, 'usersSeenComing': 0, 'usersSeenGoing': 0, 'messagesSeen': 0, 'starts': 0 })
atexit.register(self.saveJSON, 'save_stats.dat', self.stats)
self.muted = self.loadJSON('save_muted.dat', False)
atexit.register(self.saveJSON, 'save_muted.dat', self.muted)
########## Save / Load Functions ##########
def saveJSON(self, filename, content):
"""Saves the given content to the given filename as JSON"""
dstfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'db', self.nickname.lower() + '_' + filename)
try:
if isinstance(content, (bool)) or len(content) > 0 or filename == 'save_afk.dat':
file = open(dstfile, 'w')
if isinstance(content, (bool)):
print "writing content %s" % content
file.write(json.dumps(content))
file.close()
self.log.info("Saving to %s" % (dstfile))
else:
self.log.warning("Did not save to log %s because content was empty!" % (dstfile))
except IOError:
self.log.warning("Could not safe data to file %s!" % (dstfile))
def loadJSON(self, filename, default):
"""Loads content from the given filename as JSON. If no file could be read, it returns the default."""
dstfile = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'db', self.nickname.lower() + '_' + filename)
try:
file = open(dstfile, 'r')
# self.cron_list = self.utils.convert_from_unicode(json.loads(file.read()))
values = json.loads(file.read())
file.close()
self.log.debug("Loading data from %s" % (dstfile))
return values
except IOError:
return default
except ValueError:
return default
########## Class Methods ##########
def _send_status(self):
"""Send status to everyone"""
self.conn.send(xmpp.dispatcher.Presence(show=self.__show,
status=self.__status))
def __set_status(self, value):
"""Set status message.
If value remains constant, no presence stanza will be send"""
if self.__status != value:
self.__status = value
self._send_status()
def __get_status(self):
"""Get current status message"""
return self.__status
def __set_show(self, value):
"""Set show (status type like AWAY, DND etc.).
If value remains constant, no presence stanza will be send"""
if self.__show != value:
self.__show = value
self._send_status()
def __get_show(self):
"""Get current show (status type like AWAY, DND etc.)."""
return self.__show
status_message = property(fget=__get_status, fset=__set_status)
status_type = property(fget=__get_show, fset=__set_show)
########## Instance Methods ##########
def get_sender_username(self, mess):
"""Extract the sender's user name from a message"""
type = mess.getType()
jid = mess.getFrom()
if type == "groupchat":
username = jid.getResource()
elif type == "chat":
username = jid.getNode()
else:
username = ""
return username
def get_full_jids(self, jid):
"""Returns all full jids, which belong to a bare jid
Example: A bare jid is [email protected], with two clients connected, which
have the full jids [email protected]/home and [email protected]/work."""
for res in self.roster.getResources(jid):
full_jid = "%s/%s" % (jid,res)
yield full_jid
########## Client Methods ##########
def connect(self):
if not self.conn:
#TODO improve debug
if self.__debug:
conn = xmpp.Client(self.jid.getDomain())
else:
conn = xmpp.Client(self.jid.getDomain(), debug=[])
#connection attempt
conres = conn.connect()
if not conres:
self.log.error('Unable to connect to server %s.' % self.jid.getDomain())
return None
if conres != 'tls':
self.log.warning('Unable to establish secure connection '\
'- TLS failed!')
authres = conn.auth(self.jid.getNode(), self.__password, self.res)
if not authres:
self.log.error('Unable to authorize with server.')
return None
if authres != 'sasl':
self.log.warning("unable to perform SASL auth on %s. "\
"Old authentication method used!" % self.jid.getDomain())
# Connection established - save connection
self.conn = conn
# Send initial presence stanza (say hello to everyone)
self.conn.sendInitPresence(requestRoster=1)
# Save roster and log Items
self.roster = self.conn.getRoster()
# self.roster = self.conn.Roster.getRoster()
self.log.debug('*** roster ***')
for contact in self.roster.getItems():
self.log.debug(' %s' % contact)
self.log.debug('*** roster ***')
my_roster = self.conn.getRoster()
for i in my_roster.getItems():
self.myroster[i] = my_roster.getStatus(i)
if len(self.myroster) > 0:
self.myroster = self.convert_from_unicode(self.myroster)
# Register given handlers (TODO move to own function)
for (handler, callback) in self.handlers:
self.conn.RegisterHandler(handler, callback)
self.log.debug('Registered handler: %s' % handler)
self.stats['starts'] += 1
self.log.info("Connection to %s established" % self.jid.getDomain())
return self.conn
def quitBot(self):
self.__finished = True
def join_room(self, room, username=None, password=None):
"""Join the specified multi-user chat room
If username is NOT provided fallback to node part of JID"""
# TODO fix namespacestrings and history settings
NS_MUC = 'http://jabber.org/protocol/muc'
username = self.nickname
if username is None:
# TODO use xmpppy function getNode
username = self.__username.split('@')[0]
my_room_JID = '/'.join((room, username))
self.log.debug("Room JID: %s" % my_room_JID)
pres = xmpp.Presence(to=my_room_JID)
pres.setShow(None)
pres.setStatus("At your service.")
if password is not None:
pres.setTag('x',namespace=NS_MUC).setTagData('password',password)
try:
self.connect().send(pres)
self.readyTs = time.time()
self.muc_channels.append(room)
self.log.info("Room %s joned as %s" % (room, username))
except (AttributeError, ValueError) as e:
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
self.log.error('Unable to join room %s as %s: %s' % (room, username, '({}:{} "{}: {}")'.format(filename, lineno, line.strip(), exc_obj)))
self.quitBot()
def send_message(self, mess):
"""Send an XMPP message"""
self.stats['messageCount'] += 1
self.connect().send(mess)
def send(self, user, text, in_reply_to=None, message_type='chat'):
"""Sends a simple message to the specified user."""
mess = self.build_message(text)
mess.setTo(user)
if in_reply_to:
mess.setThread(in_reply_to.getThread())
mess.setType(in_reply_to.getType())
else:
mess.setThread(self.__threads.get(user, None))
mess.setType(message_type)
self.send_message(mess)
def send_simple_reply(self, mess, text, private=False):
"""Send a simple response to a message"""
if not self.muted:
self.send_message(self.build_reply(mess, text, private))
else:
self.send_message(self.build_reply(mess, text, True))
def build_reply(self, mess, text=None, private=False):
"""Build a message for responding to another message.
Message is NOT sent"""
response = self.build_message(text)
if private:
response.setTo(mess.getFrom())
response.setType('chat')
else:
response.setTo(mess.getFrom().getStripped())
response.setType(mess.getType())
response.setThread(mess.getThread())
return response
def encode_message(self, content):
"""Encodes the text as needed"""
# cgi.escape(unicode(str(line), "utf-8")).encode('ascii', 'xmlcharrefreplace') #nohtml
if isinstance(content, str):
self.log.debug('Content is string: %s' % (content))
unicode_content = content.decode('utf-8')
elif isinstance(content, unicode):
self.log.debug('Content is unicode: %s' % (content))
unicode_content = content
else:
self.log.warning('Content is no string: %s' % (content))
unicode_content = content
return unicode_content #.encode('ascii', 'replace')
def build_message(self, text):
"""Builds an xhtml message without attributes.
If input is not valid xhtml-im fallback to normal."""
plain_message = text
if isinstance(text, list):
plain_message = '\n'.join(text)
plain_message = re.sub(r'<[^>]+>', '', plain_message)
message = xmpp.protocol.Message(body=plain_message)
html = xmpp.Node('html', {'xmlns': 'http://jabber.org/protocol/xhtml-im'})
html_message = ""
if isinstance(text, list):
html_message = '<br />'.join(text)
else:
html_message = text
try:
html_message = "<span style='color: #%s'>" % self.text_color + html_message + "</span>"
html_message = self.encode_message("<body xmlns='http://www.w3.org/1999/xhtml'>" + html_message + "</body>")
html_message = html_message.encode('utf-8')
html.addChild(node=xmpp.simplexml.XML2Node(html_message))
message.addChild(node=html)
except Exception, e:
self.log.warning('Error while building XHTML message with %s: %s' % (html_message, e))
return message
def broadcast(self, message, only_available=False):
"""Broadcast a message to all users 'seen' by this bot.
If the parameter 'only_available' is True, the broadcast
will not go to users whose status is not 'Available'."""
for jid, (show, status) in self.__seen.items():
if not only_available or show is self.AVAILABLE:
self.send(jid, message)
########## Calbacks ##########
def callback_presence(self, conn, presence):
jid, type_, show, status = presence.getFrom(), \
presence.getType(), presence.getShow(), \
presence.getStatus()
try:
who = str(presence.getFrom())
except UnicodeEncodeError as e:
self.log.warning("Catched UNICODE ERROR! type: %s" % type(presence.getFrom()))
who = self.convert_from_unicode(presence.getFrom())
if self.jid.bareMatch(jid):
# update internal status
if type_ != self.OFFLINE:
self.__status = status
self.__show = show
else:
self.__status = ""
self.__show = self.OFFLINE
if not self.__acceptownmsgs:
# Ignore our own presence messages
return
if type_ is None:
# Keep track of status message and type changes
old_show, old_status = self.__seen.get(jid, (self.OFFLINE, None))
if old_show != show:
self.status_type_changed(jid, show)
if old_status != status:
self.status_message_changed(jid, status)
self.__seen[jid] = (show, status)
elif type_ == self.OFFLINE and jid in self.__seen:
# Notify of user offline status change
del self.__seen[jid]
self.status_type_changed(jid, self.OFFLINE)
else:
self.status_type_changed(jid, show)
try:
subscription = self.roster.getSubscription(unicode(jid.__str__()))
except KeyError, e:
# User not on our roster
subscription = None
except AttributeError, e:
# Recieved presence update before roster built
return
if type_ == 'error':
self.log.error(presence.getError())
self.log.debug('Got presence: %s (type: %s, show: %s, status: %s, '\
'subscription: %s)' % (jid, type_, show, status, subscription))
# If subscription is private,
# disregard anything not from the private domain
if self.__privatedomain and type_ in ('subscribe', 'subscribed', \
'unsubscribe'):
if self.__privatedomain == True:
# Use the bot's domain
domain = self.jid.getDomain()
else:
# Use the specified domain
domain = self.__privatedomain
# Check if the sender is in the private domain
user_domain = jid.getDomain()
if domain != user_domain:
self.log.info('Ignoring subscribe request: %s does not '\
'match private domain (%s)' % (user_domain, domain))
return
if type_ == 'subscribe':
# Incoming presence subscription request
if subscription in ('to', 'both', 'from'):
self.roster.Authorize(jid)
self._send_status()
if subscription not in ('to', 'both'):
self.roster.Subscribe(jid)
if subscription in (None, 'none'):
self.send(jid, self.MSG_AUTHORIZE_ME)
elif type_ == 'subscribed':
# Authorize any pending requests for that JID
self.roster.Authorize(jid)
elif type_ == 'unsubscribed':
# Authorization was not granted
self.send(jid, self.MSG_NOT_AUTHORIZED)
self.roster.Unauthorize(jid)
elif type_ == 'unavailable':
self.log.info("Remove online user: %s" % (who))
try:
del self.muc_users[who]
except Exception as e:
self.log.info("Remove online user error: %s" % (e))
else:
if presence.getJid():
# print "presence.getJid(): %s" % presence.getJid()
# print "self.jid: %s" % self.jid
myself = False
for room in self.muc_channels:
if who == "%s/%s" % (room, self.nickname):
myself = True
if not myself:
# try:
# srcJid = presence.getJid().split('/')[0]
# except KeyError:
# srcJid = False
# if srcJid:
# if srcJid != self.jid:
try:
# status = self.roster.getShow(presence.getJid())
status = presence.getShow()
# print "%s -> %s" % (who, status)
except KeyError:
status = False
if status in [None, 'chat']:
self.log.info("User now available (online, chat): %s" % (who))
self.muc_users[who] = presence.getJid()
elif status in ['xa', 'away', 'dnd']:
self.log.info("User now unavailable (offline, afk, dnd): %s" % (who))
try:
del self.muc_users[who]
except Exception as e:
self.log.info("Remove online user error: %s" % (e))
else:
self.log.debug("Ignoring myself")
self.log.debug("Users online: %s" % (' '.join(self.muc_users)))
def callback_message(self, conn, mess):
"""Messages sent to the bot will arrive here.
Command handling + routing is done in this function."""
# Prepare to handle either private chats or group chats
type = mess.getType()
jid = mess.getFrom()
props = mess.getProperties()
text = mess.getBody()
username = self.get_sender_username(mess)
if type not in ("groupchat", "chat"):
self.log.debug("unhandled message type: %s" % type)
return
# Ignore messages from before we joined
if xmpp.NS_DELAY in props: return
# Ignore messages from myself
if self.jid.bareMatch(jid): return
self.log.debug("*** props = %s" % props)
self.log.debug("*** jid = %s" % jid)
self.log.debug("*** username = %s" % username)
self.log.debug("*** type = %s" % type)
self.log.debug("*** text = %s" % text)
# If a message format is not supported (eg. encrypted),
# txt will be None
if not text: return
# Ignore messages from users not seen by this bot
if jid not in self.__seen:
self.log.debug('Ignoring message from unseen guest: %s' % jid)
self.log.debug("I've seen: %s" %
["%s" % x for x in self.__seen.keys()])
return
# Remember the last-talked-in message thread for replies
# FIXME i am not threadsafe
self.__threads[jid] = mess.getThread()
try:
jid_string = '{0}'.format(jid).lower()
except UnicodeEncodeError:
jid_string = '{0}'.encode('ascii', 'ignore').format(jid).lower()
channel = jid_string.split('/')[0]
botname1 = '{0}/{1}'.format(channel, self.__username).lower()
botname2 = '{0}/{1}'.format(channel, self.__username.split('@')[0]).lower()
if (jid_string != botname1) and (jid_string != botname2):
self.stats['messagesSeen'] += 1
if text[0] == '!':
text = self.nickname + ' ' + text[1:]
if text[0] in ['@', '!']:
text = text[1:]
if text[0:len(self.nickname)].lower() == self.nickname.lower():
text_without_nickname = text[(len(self.nickname) + 1):]
if ' ' in text_without_nickname:
command, args = text_without_nickname.split(' ', 1)
else:
command, args = text_without_nickname, ''
args = args.encode('utf-8')
cmd = command.lower()
if self.commands.has_key(cmd):
def execute_and_send():
try:
reply = self.commands[cmd](mess, args)
if reply:
self.send_simple_reply(mess, reply)
except Exception, e:
self.log.exception('An error happened while processing '\
'a message ("%s") from %s: %s"' %
(text, jid, traceback.format_exc(e)))
execute_and_send()
else:
self.on_not_a_command(mess)
########## Bot Thread Methods ##########
def idle_proc(self):
"""This function will be called in the main loop."""
self._idle_ping()
def _idle_ping(self):
"""Pings the server, calls on_ping_timeout() on no response.
To enable set self.PING_FREQUENCY to a value higher than zero.
"""
if self.PING_FREQUENCY \
and time.time() - self.__lastping > self.PING_FREQUENCY:
self.__lastping = time.time()
#logging.debug('Pinging the server.')
ping = xmpp.Protocol('iq', typ='get', \
payload=[xmpp.Node('ping', attrs={'xmlns':'urn:xmpp:ping'})])
try:
res = self.conn.SendAndWaitForResponse(ping, self.PING_TIMEOUT)
#logging.debug('Got response: ' + str(res))
if res is None:
self.on_ping_timeout()
except IOError, e:
logging.error('Error pinging the server: %s, '\
'treating as ping timeout.' % e)
self.on_ping_timeout()
def on_ping_timeout(self):
logging.info('Terminating due to PING timeout.')
self.quitBot()
def shutdown(self):
"""This function will be called when we're done serving
Override this method in derived class if you
want to do anything special at shutdown.
"""
pass
def serve_forever(self, connect_callback=None, disconnect_callback=None):
"""Connects to the server and handles messages."""
conn = self.connect()
if conn:
self.log.info('Bot connected. serving forever.')
else:
self.log.warn('Could not connect to server - aborting.')
return
if connect_callback:
connect_callback()
self.__lastping = time.time()
while not self.__finished:
try:
conn.Process(1)
self.idle_proc()
except KeyboardInterrupt:
self.log.info('Bot stopped by user request. Shutting down.')
break
self.shutdown()
if disconnect_callback:
disconnect_callback()
########## Events ##########
def do_kick(self, room, nick, reason=None):
"""Kicks user from muc
Works only with sufficient rights."""
NS_MUCADMIN = 'http://jabber.org/protocol/muc#admin'
item = xmpp.simplexml.Node('item')
item.setAttr('nick', nick)
item.setAttr('role', 'none')
iq = xmpp.Iq(typ='set',queryNS=NS_MUCADMIN,xmlns=None,to=room,payload=set([item]))
if reason is not None:
item.setTagData('reason',reason)
self.connect().send(iq)
def do_topic(self, room):
newTopic = [self.userTopic]
count = 0
for (ts, topic) in sorted(self.countTopic):
count += 1
if count > 4:
break
newTopic.append(topic)
asciiTopic = []
for topic in newTopic:
try:
asciiTopic.append(topic.decode('utf8', 'ignore'))
except UnicodeEncodeError as e:
asciiTopic.append(topic)
newTmpTopic = ' | '.join(asciiTopic)
if newTmpTopic != self.currentTopic:
self.currentTopic = newTmpTopic
subject = xmpp.simplexml.Node('subject', payload=set([newTmpTopic]))
mess = xmpp.Message(to=room, xmlns=None, typ='groupchat', payload=set([subject]))
self.connect().send(mess)
def do_invite(self, room, jid, reason=None):
"""Invites user to muc.
Works only if user has permission to invite to muc"""
NS_MUCUSER = 'http://jabber.org/protocol/muc#user'
invite = xmpp.simplexml.Node('invite')
invite.setAttr('to',jid)
if reason is not None:
invite.setTagData('reason',reason)
mess = xmpp.Message(to=room)
mess.setTag('x',namespace=NS_MUCUSER).addChild(node=invite)
self.log.error(mess)
self.connect().send(mess)
#FIXME rename!
def status_type_changed(self, jid, new_status_type):
"""Callback for tracking status types (dnd, away, offline, ...)"""
self.log.debug('user %s changed status to %s' % (jid, new_status_type))
# status = "{0}".format(new_status_type)
if new_status_type in [None, 'chat']:
self.on_came_online(jid)
elif new_status_type in ['xa', 'away', 'dnd', 'unavailable']:
self.on_gone_offline(jid)
else:
if str(jid).split('/')[1] != self.nickname:
try:
self.suggestion(None, 'User %s changed to unknown status: %s' % (jid, new_status_type))
except:
pass
#FIXME rename!
def status_message_changed(self, jid, new_status_message):
"""Callback for tracking status messages (the free-form status text)"""
self.log.debug('user %s updated text to %s' %
(jid, new_status_message))
def on_not_a_command(self, mess):
"""plix overwrite me"""
self.log.debug("on not a command called")
def on_came_online(self, jid):
"""plix overwrite me"""
self.log.debug("{0} came online".format(jid))
def on_gone_offline(self, jid):
"""plix overwrite me"""
self.log.debug("{0} gone offline".format(jid))
def get_my_username(self):
return self.nickname
###### Helper methods (mostly taken from JamesII https://github.com/oxivanisher/JamesII/blob/master/src/james/jamesutils.py) ########
def loadLocalization(self, localization):
trans = gettext.translation("rmk.gabi", "locale", [localization])
trans.install()
self.log.info("Loaded language {0}".format(localization))
def getAge(self, timestamp):
age = int(time.time() - timestamp)
if age < 0:
age = age * -1
if age == 0:
return ''
elif age == 1:
return _('1 second')
elif age < 60:
return _('{0} seconds').format(age)
elif age == 60:
return _('1 minute')
elif age > 60 and age < 3600:
return _('{0} minutes').format(int(age / 60))
elif age >= 3600 and age < 7200:
return _('1 hour')
elif age >= 7200 and age < 86400:
return _('{0} hours').format(int(age / 3600))
elif age >= 86400 and age < 172300:
return _('1 day')
elif age >= 172300 and age < 604800:
return _('{0} days').format(int(age / 86400))
elif age >= 604800 and age < 1209600:
return _('1 week')
elif age >= 1209600 and age < 31449600:
return _('{0} weeks').format(int(age / 604800))
elif age >= 31449600 and age < 62899200:
return _('1 year')
else:
return _('{0} years').format(int(age / 31449600))
def convert_from_unicode(self, data):
if isinstance(data, unicode):
return str(data)
elif isinstance(data, collections.Mapping):
return dict(map(self.convert_from_unicode, data.iteritems()))
elif isinstance(data, collections.Iterable):
return type(data)(map(self.convert_from_unicode, data))
else:
return data
def list_unicode_cleanup(self, data):
args = [s.encode('utf-8', errors='ignore').strip() for s in data]
args = filter(lambda s: s != '', args)
return args
def time_string2seconds(self, arg):
# converts 12:22 and 12:22:33 into seconds
seconds = 0
minutes = 0
hours = 0
try:
if arg.count(':') == 2:
data = arg.split(':')
seconds = int(data[2])
minutes = int(data[1])
hours = int(data[0])
elif arg.count(':') == 1:
data = arg.split(':')
minutes = int(data[1])
hours = int(data[0])
except Exception as e:
pass
return (hours * 3600 + minutes * 60 + int(seconds))
def date_string2values(self, arg):
# converts dd.mm.yyyy into [yyyy, mm, dd]
try:
data = arg.split('.')
if int(data[0]) > 0 and int(data[0]) < 32:
if int(data[1]) > 0 and int(data[1]) < 13:
if int(data[2]) > 1969:
return [int(data[2]), int(data[1]), int(data[0])]
except Exception as e:
return False
def get_long_duration(self, age):
intervals = (
('y', 220752000), # 60 * 60 * 24 * 7 * 365
('w', 604800), # 60 * 60 * 24 * 7
('d', 86400), # 60 * 60 * 24
('h', 3600), # 60 * 60
('m', 60),
('s', 1),
)
result = []
for name, count in intervals:
value = age // count
if value:
age -= value * count
result.append("%s%s" % (int(value), name))
return ' '.join(result)
def timestampToString(self, ts):
return datetime.datetime.fromtimestamp(int(ts)).strftime('%d.%m.%Y %H:%M:%S')
########## Decorator for Bot Command Functions ##########
def botcmd(*args, **kwargs):
"""Decorator for bot command functions"""
def decorate(func, hidden=False, name=None, thread=False):
setattr(func, '_jabberbot_command', True)
setattr(func, '_jabberbot_command_hidden', hidden)
setattr(func, '_jabberbot_command_name', name or func.__name__)
setattr(func, '_jabberbot_command_thread', thread) # Experimental!
return func
if len(args):
return decorate(args[0], **kwargs)
else:
return lambda func: decorate(func, **kwargs)