forked from rkfg/Talho
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jabberbot.py
370 lines (310 loc) · 12.2 KB
/
jabberbot.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
# -*- coding: utf-8 -*-
###############################################
# Based on JabberBot by Thomas Perl <[email protected]>
# (http://thpinfo.com/2007/python-jabberbot/)
###############################################
import signal
import xmpp
import logging
import os
import re
import imp
import datetime
import time
import misc
import radio
import random
import traceback
import sys
import gettext
from xml.sax.saxutils import escape, unescape
from misc import _
import socket
class JabberBot:
reconnectTime = 30
def __init__(self, user, rooms, usersjid, usersnick, logfile): #{{{
gettext.textdomain('talho')
signal.signal(signal.SIGTERM, sigTermCB)
signal.signal(signal.SIGHUP, sigHupCB)
self.jid = xmpp.JID(user[0])
self.password = user[1]
self.res = user[2]
self.rooms = rooms
self.usersnick = usersnick
self.usersjid = usersjid
self.conn = None
self.__finished = False
self.iq = True
self.info = ''
self.last = datetime.datetime(1, 1, 1)
self.plaintext_dispatchers = {}
socket.setdefaulttimeout(15)
logging.basicConfig(level=logging.DEBUG, filename=logfile, format='[%(asctime)s] [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
#}}}
###############################################
def load(self, bot=None, args=None): #{{{
'''load\nLoad all modules.\nSee also: modprobe, rmmod, lsmod'''
if args: return
self.commands = {'load': (0, self.load), 'modprobe': (0, self.modprobe), 'rmmod': (0, self.rmmod)}
for file in os.listdir('modules'):
if file.endswith('.py'):
self.modprobe(self, [file[:-3]])
return 'done'
#}}}
def modprobe(self, bot, args): #{{{
'''modprobe <module>\nLoads module.\nSee also: load, rmmod, lsmod'''
if len(args) != 1: return
try:
file, pathname, description = imp.find_module("modules/" + args[0])
name = "modules/" + args[0]
except:
error = _('MODULE: %s not found') % args[0]
logging.error(error)
return error
try:
method = imp.load_module(name, file, pathname, description).info
except:
error = _('MODULE: can\'t load %s') % args[0]
logging.error(error)
return error
else:
info = method(bot)
if info[0] == '':
self.plaintext_dispatchers[args[0]] = info[1:]
else:
for command in info[0]:
self.commands[command] = info[1:]
info = _('MODULE: %s loaded') % args[0]
logging.info(info)
return info
#}}}
def rmmod(self, bot, args): #{{{
'''rmmod <module>\nRemove module.\nSee also: load, modprobe, lsmod'''
if len(args) != 1: return
if args[0] == 'load' or args[0] == 'modprobe' or args[0] == 'rmmod':
return _('MODULE: can\'t remove %s') % args[0]
if self.commands.has_key(args[0]):
del self.commands[args[0]]
else:
if self.plaintext_dispatchers.has_key(args[0]):
del self.plaintext_dispatchers[args[0]]
else:
return _('MODULE: %s not loaded') % args[0]
info = _('MODULE: %s removed') % args[0]
logging.info(info)
return info
#}}}
###############################################
def connect(self): #{{{
if not self.conn:
#conn = xmpp.Client(self.jid.getDomain(), debug = ['always', 'nodebuilder'])
conn = xmpp.Client(self.jid.getDomain(), debug = [])
if not conn.connect():
logging.error('CONNECTION: unable to connect to server')
return
if not conn.auth(self.jid.getNode(), self.password, self.res):
logging.error('CONNECTION: unable to authorize with server')
return
for room, data in self.rooms.iteritems():
self._join_presence(conn, room, data[0])
conn.RegisterHandler('message', self.messageCB)
conn.RegisterHandler('presence', self.presenceCB)
conn.RegisterHandler('iq', self.iqCB, typ='result', ns=xmpp.NS_TIME)
conn.sendInitPresence()
self.conn = conn
return True
#}}}
def exit(self, msg = 'exit'): #{{{
if self.conn:
for room, data in self.rooms.iteritems():
self.leave(room)
self.conn = None
self.__finished = True
logging.info(msg)
#}}}
def _validate(self, text): #{{{
return escape(unescape(text, {'"': '\'', ''': '\'', ',': ',', '·': u'·'}))
#}}}
def send(self, user, type, text, extra = ''): #{{{
self.conn.send(u'<message to="%s" type="%s"><body>%s</body>%s</message>' %(user, type, self._validate(text), extra))
#}}}
def _join_presence(self, conn, room, password=None): #{{{
if password:
conn.send('<presence to=\'%s/%s\'><x xmlns=\'http://jabber.org/protocol/muc\'><password>%s</password></x></presence>'
%(room, self.res, password))
else:
conn.send(xmpp.Presence(to='%s/%s' %(room, self.res)))
#}}}
def join(self, room): #{{{
if not room[0] in self.rooms:
self._join_presence(self.conn, room[0], room[1])
self.rooms[room[0]] = (room[1], room[2])
return True
#}}}
def leave(self, room): #{{{
if room[0] in self.rooms:
self.conn.send(xmpp.Presence(to='%s/%s' %(room[0], self.res), typ='unavailable', status='offline'))
del self.rooms[room[0]]
return True
#}}}
def _inRoom(self, user): #{{{
return user in self.rooms
#}}}
def messageCB(self, conn, mess): #{{{
# Just a history
if mess.getTimestamp():
return
type = mess.getType()
mfrm = mess.getFrom()
user = mfrm.getStripped()
prefix = mfrm.getResource()
if type != 'groupchat':
#logging.info(misc.force_unicode(user) + " " + misc.force_unicode(prefix) + " " + misc.force_unicode(mess.getBody()))
return
if (not prefix) or (type == 'groupchat' and prefix == self.res):
return
text = misc.force_unicode(mess.getBody())
level = 10
if self._inRoom(user):
if prefix in self.settings['ignors']:
return
level = self.rooms[user][1] # room-level
if prefix in self.usersnick: # Msg from room
level = self.usersnick[prefix]
else:
if user in self.usersjid: # Msg from jid
level = self.usersjid[user]
# Checking command
if text:
if text[0] == '%':
text = text[1:]
elif type == 'groupchat':
for dispatcher in self.plaintext_dispatchers:
result = self.plaintext_dispatchers[dispatcher][0] >= level and self.plaintext_dispatchers[dispatcher][1](self, text)
if result:
self.send(user, type, misc.force_unicode(prefix) + u", " + misc.force_unicode(result))
return
if not text.startswith('%show') and re.search('http://[^ ]+', text):
link = re.findall('(http://[^ ]+)', text)[0]
title = misc.getTitle(link, self)
if title:
if re.search('http://(www\.)?youtube.com/', link) and re.search('[rR][iI][cC][kK]', title):
self.send(user, type, u'%s — там рикролл!!!' %link)
else:
tiny = ''
if len(link) > 70: tiny = ' ( %s )' %misc.makeTiny(link, self)
self.send(user, type, u'Title: %s%s' %(title, tiny))
return
else:
return
else:
return
# Parsing command
spl = text.split()
if spl:
cmd, args = spl[0].lower(), spl[1:]
else:
return
if type == 'groupchat':
if '>' in args:
index = args.index('>') # Redirect (>)
prefix = ''
redir = ' '.join(args[index+1:])
if redir:
prefix = redir + ', '
args = args[:index]
else:
prefix += ', ' # Groupchat => prefix
else:
user, prefix = mfrm, '' # Chat => no prefix
# Executing command
error = None
if self.commands.has_key(cmd):
if self.commands[cmd][0] >= level:
try:
result = self.commands[cmd][1](self, args)
except:
error = True
else:
result = 'доступ запрещён. Вам нужен минимум уровень %d, но у вас %d' % (self.commands[cmd][0], level)
else:
return #result = 'command not found: %s' %(cmd)
if self.__finished: return
if error:
error = u'Исключение в модуле %s. Трейсбэк: %s' % (cmd, str("".join(traceback.format_exception(*sys.exc_info()))))
logging.error(error)
msg, extra = error, ''
else:
if result:
if isinstance(result, tuple):
msg, extra = result
else:
msg, extra = result, ''
else:
msg, extra = 'синтаксическая ошибка.', ''
# Replying
if msg:
self.send(user, type, prefix + misc.force_unicode(msg), extra)
#}}}
def presenceCB(self, conn, pres): #{{{
if pres.getType() == 'subscribe' and pres.getFrom().getStripped() in self.usersjid:
self.conn.send(xmpp.Presence(to=pres.getFrom(), typ='subscribed'))
if pres.getFrom().getResource() == self.res and pres.getType() == 'unavailable' and pres.getStatus() == 'Replaced by new connection':
user = pres.getFrom().getStripped()
for room, data in self.rooms.iteritems():
if user == room:
self._join_presence(self.conn, room, data[0])
#}}}
def iqCB(self, conn, iq_node): #{{{
self.iq = iq_node
#}}}
def process(self): #{{{
while not self.__finished:
try:
self.checkReconnect()
if self.conn:
if self.conn.Process(1) == 0:
self.connect()
else:
if self.connect():
logging.info('CONNECTION: bot connected')
else:
time.sleep(self.reconnectTime)
except xmpp.protocol.XMLNotWellFormed:
logging.error('CONNECTION: reconnect (detected not valid XML)')
self.conn = None
except KeyboardInterrupt:
self.exit('EXIT: interrupted by keyboard')
except SystemExit:
self.exit('EXIT: interrupted by SIGTERM')
except ReloadData:
logging.info('RELOAD: by SIGHUP')
self.load()
except:
pass
#}}}
def checkReconnect(self): #{{{
if self.conn:
now = datetime.datetime.now()
if (now - self.last).seconds > self.reconnectTime:
if self.iq:
self.iq = None
self.last = now
self.conn.send(xmpp.protocol.Iq(to='jabber.ru', typ='get', queryNS=xmpp.NS_TIME))
for room in self.rooms:
self._join_presence(self.conn, room)
else:
logging.warning('CONNECTION: reconnect (iq reply timeout)')
self.conn = None
self.iq = True
#}}}
###############################################
def sigTermCB(signum, frame): #{{{
raise SystemExit()
#}}}
class ReloadData(Exception): #{{{
pass
#}}}
def sigHupCB(signum, frame): #{{{
raise ReloadData()
#}}}