更新 paste 地址
[gaetalk.git] / gaetalk.py
blobe63f58d73dfadf3cb0ac78bb2dc397063ee66244
1 #!/usr/bin/env python2
2 # vim:fileencoding=utf-8
4 import re
5 import logging
6 import datetime
8 from google.appengine.ext import db
9 from google.appengine.api import xmpp
11 import utils
12 import config
14 helpre = re.compile(r'^\W{0,2}help$')
16 #用户所有资源离线时,会加上“完全”二字
17 OFFLINE = u'离线'
18 AWAY = u'离开'
19 XAWAY = u'离开'
20 BUSY = u'忙碌'
21 ONLINE = u'在线'
22 CHAT = u'和我说话吧'
24 NEW = u'加入'
25 LEAVE = u'退出'
26 NICK = u'昵称更改 (%s -> %s)'
27 SNOOZE = u'snooze %ds'
28 BLACK = u'禁言 %s %ds'
29 BLACK_AUTO = u'被禁言 %ds'
30 KICK = u'删除 %s (%s)'
31 ADMIN = u'%s 成为管理员 (by %s)'
32 UNADMIN = u'%s 不再是管理员 (by %s)'
33 NOTICE = u'通告:%s'
34 BLOCK = u'封禁 %s,原因:%s'
35 UNBLOCK = u'解封 %s'
37 STATUS_CODE = {
38 '': ONLINE,
39 'away': AWAY,
40 'dnd': BUSY,
41 'xa': XAWAY,
42 'chat': CHAT,
45 #状态的排序顺序
46 STATUS_LIST = [CHAT, ONLINE, AWAY, XAWAY, BUSY, OFFLINE]
48 timezone = datetime.timedelta(hours=config.timezoneoffset)
50 class User(db.Model):
51 jid = db.StringProperty(required=True, indexed=True)
52 nick = db.StringProperty(required=True, indexed=True)
54 add_date = db.DateTimeProperty(auto_now_add=True)
55 last_online_date = db.DateTimeProperty()
56 last_offline_date = db.DateTimeProperty()
57 last_speak_date = db.DateTimeProperty()
59 msg_count = db.IntegerProperty(required=True, default=0)
60 msg_chars = db.IntegerProperty(required=True, default=0)
61 credit = db.IntegerProperty(required=True, default=0)
63 black_before = db.DateTimeProperty(auto_now_add=True)
64 snooze_before = db.DateTimeProperty()
65 flooding_point = db.IntegerProperty(default=0)
67 avail = db.StringProperty(required=True)
68 is_admin = db.BooleanProperty(required=True, default=False)
69 resources = db.StringListProperty(required=True)
70 #允许他人私信?
71 reject_pm = db.BooleanProperty(default=False)
73 prefix = db.StringProperty(required=True, default=config.default_prefix)
74 nick_pattern = db.StringProperty(required=True, default='[%s]')
75 nick_changed = db.BooleanProperty(required=True, default=False)
76 intro = db.StringProperty()
78 class Log(db.Model):
79 time = db.DateTimeProperty(auto_now_add=True, indexed=True)
80 msg = db.StringProperty(required=True, multiline=True)
81 jid = db.StringProperty()
82 nick = db.StringProperty()
83 type = db.StringProperty(required=True, indexed=True,
84 choices=set(['chat', 'member', 'admin', 'misc']))
86 class Group(db.Model):
87 time = db.DateTimeProperty(auto_now_add=True)
88 topic = db.StringProperty(multiline=True)
89 status = db.StringProperty()
91 class BlockedUser(db.Model):
92 jid = db.StringProperty(required=True, indexed=True)
93 add_date = db.DateTimeProperty(auto_now_add=True)
94 reason = db.StringProperty()
96 def log_msg(sender, msg):
97 l = Log(jid=sender.jid, nick=sender.nick,
98 type='chat', msg=msg)
99 l.put()
101 def log_onoff(sender, action, resource=''):
102 if resource:
103 if action == OFFLINE and not sender.resources:
104 msg = u'完全%s (%s)' % (action, resource)
105 else:
106 msg = '%s (%s)' % (action, resource)
107 else:
108 msg = action
109 l = Log(jid=sender.jid, nick=sender.nick,
110 type='member', msg=msg)
111 l.put()
113 def log_admin(sender, action):
114 l = Log(jid=sender.jid, nick=sender.nick,
115 type='admin', msg=action)
116 l.put()
118 def get_user_by_jid(jid):
119 return User.gql('where jid = :1', jid.lower()).get()
121 def get_user_by_nick(nick):
122 return User.gql('where nick = :1', nick).get()
124 def get_group_info():
125 return Group.all().get()
127 def get_blocked_user(jid):
128 return BlockedUser.gql('where jid = :1', jid.lower()).get()
130 def get_member_list():
131 now = datetime.datetime.now()
132 #一个查询中最多只能有一个不等比较
133 l = User.gql('where avail != :1', OFFLINE)
134 return [unicode(x.jid) for x in l \
135 if x.snooze_before is None or x.snooze_before < now]
137 def send_to_all_except(jid, message):
138 if isinstance(jid, str):
139 jids = [x for x in get_member_list() if x != jid]
140 else:
141 jids = [x for x in get_member_list() if x not in jid]
142 logging.debug(jid)
143 logging.debug(jids)
144 try:
145 xmpp.send_message(jids, message)
146 except xmpp.InvalidJidError:
147 pass
149 def send_to_all(message):
150 jids = get_member_list()
151 xmpp.send_message(jids, message)
153 def send_status(jid):
154 if get_blocked_user(jid):
155 xmpp.send_presence(jid, status=u'您已经被本群封禁')
156 return
158 grp = get_group_info()
159 if grp is None or not grp.status:
160 xmpp.send_presence(jid)
161 else:
162 xmpp.send_presence(jid, status=grp.status)
164 def handle_message(msg):
165 jid = msg.sender.split('/')[0]
167 sender = get_blocked_user(jid)
168 if sender is not None:
169 msg.reply(u'您已经被本群封禁,原因为 %s。' % sender.reason)
170 return
172 sender = get_user_by_jid(jid)
173 if sender is None:
174 msg.reply('很抱歉,出错了,请尝试更改状态或者重新添加好友。')
175 return
177 if msg.body.startswith('?OTR:'):
178 msg.reply('不支持 OTR 加密!')
179 return
181 if msg.body in config.blocked_away_messages:
182 msg.reply('系统认为您的客户端正在自动发送离开消息。如果您认为这并不正确,请向管理员反馈。')
183 return
185 if len(msg.body) > 500 or msg.body.count('\n') > 5:
186 msgbody = utils.post_code(msg.body)
187 if msgbody:
188 msg.reply(u'内容过长,已贴至 %s' % msgbody)
189 else:
190 logging.warn(u'贴代码失败,代码长度 %d' % len(msg.body))
191 msg.reply('由于技术限制,每条消息最长为 500 字。大段文本请贴 paste 网站。\n'
192 '如 http://paste.ubuntu.org.cn/ http://slexy.org/\n'
193 'PS: 自动转贴失败!')
194 return
195 ch = None
196 else:
197 msgbody = msg.body
198 if sender.is_admin or sender.jid == config.root:
199 ch = AdminCommand(msg, sender)
200 else:
201 ch = BasicCommand(msg, sender)
203 if not ch or not ch.handled:
204 now = datetime.datetime.now()
205 if sender.black_before is not None \
206 and sender.black_before > now:
207 if (datetime.datetime.today()+timezone).date() == \
208 (sender.black_before+timezone).date():
209 format = '%H时%M分%S秒'
210 else:
211 format = '%m月%d日 %H时%M分%S秒'
212 msg.reply('你已被禁言至 ' \
213 + (sender.black_before+timezone).strftime(format))
214 return
216 if sender.last_speak_date is not None:
217 d = now - sender.last_speak_date
218 t = d.seconds
219 if d.days > 0 or t > 60:
220 sender.flooding_point = 0
221 else:
222 k = 1000 / (t * t + 1)
223 if k > 0:
224 sender.flooding_point += k
225 else:
226 sender.flooding_point = 0
228 k = sender.flooding_point / 1500
229 if k > 0:
230 msg.reply('刷屏啊?禁言 %d 分钟!' % k)
231 send_to_all_except(sender.jid,
232 (u'%s 已因刷屏而被禁言 %d 分钟。' % (sender.nick, k)) \
233 .encode('utf-8'))
234 log_onoff(sender, BLACK_AUTO % (60 * k))
235 sender.black_before = now + datetime.timedelta(seconds=60*k)
236 sender.put()
237 return
239 sender.last_speak_date = now
240 sender.snooze_before = None
241 try:
242 sender.msg_count += 1
243 sender.msg_chars += len(msgbody)
244 except TypeError:
245 sender.msg_count = 1
246 sender.msg_chars = len(msgbody)
247 sender.put()
248 body = utils.removelinks(msgbody)
249 for u in User.gql('where avail != :1', OFFLINE):
250 if u.snooze_before is not None and u.snooze_before >= now:
251 continue
252 if u.jid == sender.jid:
253 continue
254 try:
255 message = '%s %s' % (
256 u.nick_pattern % sender.nick,
257 body
259 xmpp.send_message(u.jid, message)
260 except xmpp.InvalidJidError:
261 pass
262 log_msg(sender, msgbody)
264 def try_add_user(jid, show=OFFLINE, resource=''):
265 '''使用 memcache 作为锁添加用户'''
266 u = get_blocked_user(jid)
267 if u is not None:
268 xmpp.send_message(jid, u'您已经被本群封禁,原因为 %s。' % u.reason)
269 return
271 L = utils.MemLock('add_user')
272 L.require()
273 try:
274 u = get_user_by_jid(jid)
275 if u is not None:
276 return
277 u = add_user(jid, show, resource)
278 finally:
279 L.release()
280 if show != OFFLINE:
281 log_onoff(u, show, resource)
282 logging.info(u'%s added', jid)
284 def add_user(jid, show=OFFLINE, resource=''):
285 '''resource 在 presence type 为 available 里使用'''
286 nick = jid.split('@')[0]
287 # same user name with different domains are possible
288 while get_user_by_nick(nick):
289 nick += '_'
290 u = User(jid=jid.lower(), avail=show, nick=nick)
291 if show != OFFLINE:
292 u.last_online_date = datetime.datetime.now()
293 if resource:
294 u.resources.append(resource)
295 u.put()
296 log_onoff(u, NEW)
297 logging.info(u'%s 已经加入' % jid)
298 send_status(jid)
299 xmpp.send_message(jid, u'欢迎 %s 加入!要获取使用帮助,请输入 %shelp,要获知群主题,请输入 %stopic。' % (
300 u.nick, u.prefix, u.prefix))
301 return u
303 def del_user(jid, by_cmd=False):
304 l = User.gql('where jid = :1', jid.lower())
305 for u in l:
306 if u.jid == config.root:
307 xmpp.send_message(jid, u'root 用户:离开前请确定你已做好善后工作!')
308 log_onoff(u, LEAVE)
309 if by_cmd:
310 logging.info(u'%s (%s) 已经离开 (通过使用命令)' % (u.nick, u.jid))
311 else:
312 logging.info(u'%s (%s) 已经离开' % (u.nick, u.jid))
313 u.delete()
315 class BasicCommand:
316 handled = True
317 def __init__(self, msg, sender):
318 self.sender = sender
319 self.msg = msg
321 if helpre.match(msg.body):
322 self.do_help()
323 elif msg.body.startswith(sender.prefix):
324 cmd = msg.body[len(sender.prefix):].split()
325 try:
326 handle = getattr(self, 'do_' + cmd[0])
327 except AttributeError:
328 msg.reply(u'错误:未知命令 %s' % cmd[0])
329 except IndexError:
330 msg.reply(u'错误:无命令')
331 except UnicodeEncodeError:
332 msg.reply(u'错误:命令名解码失败。此问题在 GAE 升级其 Python 到 3.x 后方能解决。')
333 else:
334 handle(cmd[1:])
335 logging.debug('%s did command %s' % (sender.jid, cmd[0]))
336 else:
337 self.handled = False
339 def get_msg_part(self, index):
340 '''返回消息中第 index 个单词及其后的所有字符'''
341 return self.msg.body[len(self.sender.prefix):].split(None, index)[-1]
343 def do_online(self, args):
344 '''在线成员列表。可带一个参数,指定在名字中出现的一个子串。'''
345 r = []
346 pat = args[0] if args else None
347 now = datetime.datetime.now()
348 l = User.gql('where avail != :1', OFFLINE)
349 for u in l:
350 m = u.nick
351 if pat and m.find(pat) == -1:
352 continue
353 status = u.avail
354 if status != ONLINE:
355 m += u' (%s)' % status
356 if u.snooze_before is not None and u.snooze_before > now:
357 m += u' (snoozing)'
358 if u.black_before is not None and u.black_before > now:
359 m += u' (已禁言)'
360 r.append(unicode('* ' + m))
361 r.sort()
362 n = len(r)
363 if pat:
364 r.insert(0, u'在线成员列表(包含子串 %s):' % pat)
365 r.append(u'共 %d 人。' % n)
366 else:
367 r.insert(0, u'在线成员列表:')
368 r.append(u'共 %d 人在线。' % n)
369 self.msg.reply(u'\n'.join(r).encode('utf-8'))
371 def do_lsadmin(self, args):
372 '''管理员列表'''
373 r = []
374 now = datetime.datetime.now()
375 l = User.gql('where is_admin = :1', True)
376 for u in l:
377 m = u.nick
378 status = u.avail
379 if status != ONLINE:
380 m += u' (%s)' % status
381 if u.snooze_before is not None and u.snooze_before > now:
382 m += u' (snoozing)'
383 if u.black_before is not None and u.black_before > now:
384 m += u' (已禁言)'
385 r.append(unicode('* ' + m))
386 r.sort()
387 n = len(r)
388 r.insert(0, u'管理员列表:')
389 r.append(u'共 %d 位管理员。' % n)
390 self.msg.reply(u'\n'.join(r).encode('utf-8'))
392 def do_lsblocked(self, args):
393 '''列出被封禁用户名单'''
394 r = []
395 l = BlockedUser.all()
396 for u in l:
397 r.append(unicode('* %s (%s, %s)' % (u.jid,
398 utils.strftime(u.add_date, timezone),
399 u.reason)
402 r.sort()
403 n = len(r)
404 r.insert(0, u'封禁列表:')
405 r.append(u'共 %d 个 JID 被封禁。' % n)
406 self.msg.reply(u'\n'.join(r).encode('utf-8'))
408 def do_chatty(self, args):
409 '''消息数排行'''
410 r = []
411 for u in User.gql('ORDER BY msg_count ASC'):
412 m = u.nick
413 m = u'* %s:\t%5d条,共 %s' % (
414 u.nick, u.msg_count,
415 utils.filesize(u.msg_chars))
416 r.append(m)
417 n = len(r)
418 r.insert(0, u'消息数量排行:')
419 r.append(u'共 %d 人。' % n)
420 self.msg.reply(u'\n'.join(r).encode('utf-8'))
422 def do_nick(self, args):
423 '''更改昵称,需要一个参数,不能使用大部分标点符号'''
424 if len(args) != 1:
425 self.msg.reply('错误:请给出你想到的昵称(不能包含空格)')
426 return
428 q = get_user_by_nick(args[0])
429 if q is not None:
430 self.msg.reply('错误:该昵称已被使用,请使用其它昵称')
431 elif not utils.checkNick(args[0]):
432 self.msg.reply('错误:非法的昵称')
433 else:
434 if not config.nick_can_change and self.sender.nick_changed:
435 self.msg.reply('乖哦,你已经没机会再改昵称了')
436 return
437 old_nick = self.sender.nick
438 log_onoff(self.sender, NICK % (old_nick, args[0]))
439 self.sender.nick = args[0]
440 self.sender.nick_changed = True
441 self.sender.put()
442 send_to_all_except(self.sender.jid,
443 (u'%s 的昵称改成了 %s' % (old_nick, args[0])).encode('utf-8'))
444 self.msg.reply('昵称更改成功!')
445 do_nick.__doc__ += ',最长 %d 字节' % config.nick_maxlen
447 def do_whois(self, args):
448 '''查看用户信息,参数为用户昵称'''
449 if len(args) != 1:
450 self.msg.reply('错误:你想知道关于谁的信息?')
451 return
453 u = get_user_by_nick(args[0])
454 if u is None:
455 self.msg.reply(u'Sorry,查无此人。')
456 return
458 now = datetime.datetime.now()
459 status = u.avail
460 addtime = (u.add_date + timezone).strftime('%Y年%m月%d日 %H时%M分').decode('utf-8')
461 allowpm = u'否' if u.reject_pm else u'是'
462 if u.snooze_before is not None and u.snooze_before > now:
463 status += u' (snoozing)'
464 if u.black_before is not None and u.black_before > now:
465 status += u' (已禁言)'
466 r = []
467 r.append(u'昵称:\t%s' % u.nick)
468 if self.sender.is_admin:
469 r.append(u'JID:\t%s' % u.jid)
470 r.append(u'状态:\t%s' % status)
471 r.append(u'消息数:\t%d' % u.msg_count)
472 r.append(u'消息总量:\t%s' % utils.filesize(u.msg_chars))
473 r.append(u'加入时间:\t%s' % addtime)
474 r.append(u'接收私信:\t%s' % allowpm)
475 r.append(u'自我介绍:\t%s' % u.intro)
476 self.msg.reply(u'\n'.join(r).encode('utf-8'))
478 def do_help(self, args=()):
479 '''显示本帮助。参数 long 显示详细帮助,也可指定命令名。'''
480 doc = []
481 prefix = self.sender.prefix
483 if len(args) > 1:
484 self.msg.reply('参数错误。')
485 return
486 arg = args[0] if args else None
488 if arg is None or arg == 'long':
489 for b in self.__class__.__bases__ + (self.__class__,):
490 for c, f in b.__dict__.items():
491 if c.startswith('do_'):
492 if arg is None:
493 doc.append(u'%s%s:\t%s' % (prefix, c[3:], f.__doc__.decode('utf-8').\
494 split(u',', 1)[0].split(u'。', 1)[0]))
495 else:
496 doc.append(u'%s%s:\t%s' % (prefix, c[3:], f.__doc__.decode('utf-8')))
497 doc.sort()
498 if arg is None:
499 doc.insert(0, u'** 命令指南 **\n(当前命令前缀 %s,可设置。使用 %shelp long 显示详细帮助)' % (prefix, prefix))
500 else:
501 doc.insert(0, u'** 命令指南 **\n(当前命令前缀 %s,可设置)' % prefix)
502 doc.append(u'要离开,直接删掉好友即可。')
503 doc.append(u'Gtalk 客户端用户要离开请使用 quit 命令。')
504 self.msg.reply(u'\n'.join(doc).encode('utf-8'))
505 else:
506 try:
507 handle = getattr(self, 'do_' + arg)
508 except AttributeError:
509 self.msg.reply(u'错误:未知命令 %s' % arg)
510 except UnicodeEncodeError:
511 self.msg.reply(u'错误:命令名解码失败。此问题在 GAE 升级其 Python 到 3.x 后方能解决。')
512 else:
513 self.msg.reply(u'%s%s:\t%s' % (prefix, arg, handle.__doc__.decode('utf-8')))
515 def do_topic(self, args=()):
516 '''查看群主题'''
517 grp = get_group_info()
518 if grp is None or not grp.topic:
519 self.msg.reply(u'没有设置群主题。')
520 else:
521 self.msg.reply(grp.topic)
523 def do_iam(self, args):
524 '''查看自己的信息'''
525 u = self.sender
526 addtime = (u.add_date + timezone).strftime('%Y年%m月%d日 %H时%M分').decode('utf-8')
527 allowpm = u'否' if u.reject_pm else u'是'
528 r = []
529 r.append(u'昵称:\t%s' % u.nick)
530 r.append(u'JID:\t%s' % u.jid)
531 r.append(u'资源:\t%s' % u' '.join(u.resources))
532 r.append(u'消息数:\t%d' % u.msg_count)
533 r.append(u'消息总量:\t%s' % utils.filesize(u.msg_chars))
534 r.append(u'加入时间:\t%s' % addtime)
535 r.append(u'命令前缀:\t%s' % u.prefix)
536 r.append(u'接收私信:\t%s' % allowpm)
537 r.append(u'自我介绍:\t%s' % u.intro)
538 self.msg.reply(u'\n'.join(r).encode('utf-8'))
540 def do_m(self, args):
541 '''发私信,需要昵称和内容两个参数。私信不会以任何方式被记录。用户可使用 set 命令设置是否接收私信。'''
542 if len(args) < 2:
543 self.msg.reply('请给出昵称和内容。')
544 return
546 target = get_user_by_nick(args[0])
547 if target is None:
548 self.msg.reply('Sorry,查无此人。')
549 return
551 if target.reject_pm:
552 self.msg.reply('很抱歉,对方不接收私信。')
553 return
555 msg = self.get_msg_part(2)
556 msg = u'_私信_ %s %s' % (target.nick_pattern % self.sender.nick, msg)
557 if xmpp.send_message(target.jid, msg) == xmpp.NO_ERROR:
558 self.msg.reply(u'OK')
559 else:
560 self.msg.reply(u'消息发送失败')
562 def do_intro(self, arg):
563 '''设置自我介绍信息'''
564 if not arg:
565 self.msg.reply('请给出自我介绍的内容。')
566 return
568 msg = self.get_msg_part(1)
569 u = self.sender
570 try:
571 u.intro = msg
572 except db.BadValueError:
573 # 过长文本已在 handle_message 中被拦截
574 self.msg.reply('错误:自我介绍内容只能为一行。')
575 return
577 u.put()
578 self.msg.reply(u'设置成功!')
580 def do_snooze(self, args):
581 '''暂停接收消息,参数为时间(默认单位为秒)。再次发送消息时自动清除'''
582 if len(args) != 1:
583 self.msg.reply('你想停止接收消息多久?')
584 return
585 else:
586 try:
587 n = utils.parseTime(args[0])
588 except ValueError:
589 self.msg.reply('Sorry,我无法理解你说的时间。')
590 return
592 try:
593 self.sender.snooze_before = datetime.datetime.now() + datetime.timedelta(seconds=n)
594 except OverflowError:
595 self.msg.reply('Sorry,你不能睡太久。')
596 return
597 self.sender.put()
598 if n == 0:
599 self.msg.reply('你已经醒来。')
600 else:
601 self.msg.reply(u'OK,停止接收消息 %s。' % utils.displayTime(n))
602 log_onoff(self.sender, SNOOZE % n)
604 def do_offline(self, args):
605 '''假装离线,让程序认为你的所有资源已离线。如在你离线时程序仍认为你在线,请使用此命令。'''
606 del self.sender.resources[:]
607 self.sender.avail = OFFLINE
608 self.sender.last_offline_date = datetime.datetime.now()
609 self.sender.put()
610 self.msg.reply('OK,在下次你说你在线之前我都认为你已离线。')
612 def do_fakeresource(self, args):
613 '''假装在线,人工加入一个新的资源,使程序认为你总是在线。使用 off 参数来取消。'''
614 if args and args[0] == 'off':
615 try:
616 self.sender.resources.remove('fakeresouce')
617 self.msg.reply('OK,fakeresouce 已取消。')
618 self.sender.put()
619 except ValueError:
620 self.msg.reply('没有设置 fakeresouce。')
621 else:
622 try:
623 self.sender.resources.index('fakeresouce')
624 self.msg.reply('你已经设置了永远在线。')
625 except ValueError:
626 self.sender.resources.append('fakeresouce')
627 self.msg.reply('OK,你将永远在线。')
628 self.sender.put()
630 def do_old(self, args):
631 '''聊天记录查询,可选一个数字参数。默认为最后20条。特殊参数 OFFLINE (不区分大小写)显示离线消息(最多 100 条)'''
632 s = self.sender
633 q = False
634 if not args:
635 q = Log.gql("WHERE type = 'chat' ORDER BY time DESC LIMIT 20")
636 elif len(args) == 1:
637 try:
638 n = int(args[0])
639 if n > 0:
640 q = Log.gql("WHERE type = 'chat' ORDER BY time DESC LIMIT %d" % n)
641 except ValueError:
642 if args[0].upper() == 'OFFLINE':
643 q = Log.gql("WHERE time < :1 AND time > :2 AND type = 'chat' ORDER BY time DESC LIMIT 100", s.last_online_date, s.last_offline_date)
644 else:
645 pass
646 if q is not False:
647 r = []
648 q = list(q)
649 q.reverse()
650 if q:
651 if datetime.datetime.today() - q[0].time > datetime.timedelta(hours=24):
652 show_date = True
653 else:
654 show_date = False
655 for l in q:
656 message = '%s %s %s' % (
657 utils.strftime(l.time, timezone, show_date),
658 s.nick_pattern % l.nick,
659 l.msg
661 r.append(message)
662 if r:
663 self.msg.reply(u'\n'.join(r).encode('utf-8'))
664 else:
665 self.msg.reply('没有符合的聊天记录。')
666 else:
667 self.msg.reply('Oops, 参数不正确哦。')
669 def do_set(self, args):
670 '''设置参数。参数格式 key=value;不带参数以查看说明。'''
671 #注意:选项名/值中不能包含空格
672 if len(args) != 1:
673 doc = []
674 for c, f in self.__class__.__dict__.items():
675 if c.startswith('set_'):
676 doc.append(u'* %s:\t%s' % (c[4:], f.__doc__.decode('utf-8')))
677 for b in self.__class__.__bases__:
678 for c, f in b.__dict__.items():
679 if c.startswith('set_'):
680 doc.append(u'* %s:\t%s' % (c[4:], f.__doc__.decode('utf-8')))
681 doc.sort()
682 doc.insert(0, u'设置选项:')
683 self.msg.reply(u'\n'.join(doc).encode('utf-8'))
684 else:
685 msg = self.msg
686 cmd = msg.body.split(None, 1)[1].split('=', 1)
687 if len(cmd) == 1:
688 msg.reply(u'错误:请给出选项值')
689 return
690 try:
691 handle = getattr(self, 'set_' + cmd[0])
692 except AttributeError:
693 msg.reply(u'错误:未知选项 %s' % cmd[0])
694 except IndexError:
695 msg.reply(u'错误:无选项')
696 except UnicodeEncodeError:
697 msg.reply(u'错误:选项名解码失败。此问题在 GAE 升级其 Python 到 3.x 后方能解决。')
698 else:
699 handle(cmd[1])
701 def do_quit(self, args):
702 '''删除用户数据。某些自称“不作恶”的公司的客户端会不按协议要求发送删除好友的消息,请 gtalk 官方客户端用户使用此命令退出。参见 http://xmpp.org/rfcs/rfc3921.html#rfc.section.6.3 。'''
703 del_user(self.sender.jid)
704 self.msg.reply(u'OK.')
706 def set_prefix(self, arg):
707 '''设置命令前缀'''
708 self.sender.prefix = arg
709 self.sender.put()
710 self.msg.reply(u'设置成功!')
712 def set_nickpattern(self, arg):
713 '''设置昵称显示格式,用 %s 表示昵称的位置'''
714 try:
715 arg % 'test'
716 except (TypeError, ValueError):
717 self.msg.reply(u'错误:不正确的格式')
718 return
720 self.sender.nick_pattern = arg
721 self.sender.put()
722 self.msg.reply(u'设置成功!')
724 def set_allowpm(self, arg):
725 '''设置是否接收私信,参数为 y(接收)或者 n(拒绝)'''
726 if arg not in 'yn':
727 self.msg.reply(u'错误的参数。')
728 return
730 if arg == 'y':
731 self.sender.reject_pm = False
732 else:
733 self.sender.reject_pm = True
734 self.sender.put()
735 self.msg.reply(u'设置成功!')
737 class AdminCommand(BasicCommand):
738 def do_kick(self, args):
739 '''删除某人。他仍可以重新加入。'''
740 if len(args) != 1:
741 self.msg.reply('请给出昵称。')
742 return
744 target = get_user_by_nick(args[0])
745 if target is None:
746 self.msg.reply('Sorry,查无此人。')
747 return
749 if target.jid == config.root:
750 self.msg.reply('不能删除 root 用户')
751 return
753 targetjid = target.jid
754 targetnick = target.nick
755 target.delete()
756 self.msg.reply((u'OK,删除 %s。' % target.nick).encode('utf-8'))
757 send_to_all_except(self.sender.jid, (u'%s 已被删除。' % target.nick) \
758 .encode('utf-8'))
759 xmpp.send_message(targetjid, u'你已被管理员从此群中删除,请删除该好友。')
760 log_admin(self.sender, KICK % (targetnick, targetjid))
762 def do_quiet(self, args):
763 '''禁言某人,参数为昵称和时间(默认单位秒)'''
764 if len(args) != 2:
765 self.msg.reply('请给出昵称和时间。')
766 return
767 else:
768 try:
769 n = utils.parseTime(args[1])
770 except ValueError:
771 self.msg.reply('Sorry,我无法理解你说的时间。')
772 return
774 target = get_user_by_nick(args[0])
775 if target is None:
776 self.msg.reply('Sorry,查无此人。')
777 return
779 target.black_before = datetime.datetime.now() + datetime.timedelta(seconds=n)
780 target.put()
781 self.msg.reply(u'OK,禁言 %s %s。' % (target.nick, utils.displayTime(n)))
782 send_to_all_except((self.sender.jid, target.jid),
783 (u'%s 已被禁言 %s。' % (target.nick, utils.displayTime(n))) \
784 .encode('utf-8'))
785 xmpp.send_message(target.jid, u'你已被管理员禁言 %s。' % utils.displayTime(n))
786 log_admin(self.sender, BLACK % (target.nick, n))
788 def do_notice(self, arg):
789 '''发送群通告。只会发给在线的人,包括 snoozing 者。'''
790 if not arg:
791 self.msg.reply('请给出群通告的内容。')
792 return
794 msg = self.msg.body[len(self.sender.prefix):].split(None, 1)[-1]
796 l = User.gql('where avail != :1', OFFLINE)
797 log_admin(self.sender, NOTICE % msg)
798 for u in l:
799 try:
800 xmpp.send_message(u.jid, u'通告:' + msg)
801 except xmpp.InvalidJidError:
802 pass
804 def do_topic(self, args=()):
805 '''查看或设置群主题'''
806 grp = get_group_info()
807 if not args:
808 if grp is None or not grp.topic:
809 self.msg.reply(u'没有设置群主题。')
810 else:
811 self.msg.reply(grp.topic)
812 else:
813 grp = get_group_info()
814 if grp is None:
815 grp = Group()
816 grp.topic = self.msg.body[len(self.sender.prefix):].split(None, 1)[-1]
817 grp.put()
818 self.msg.reply(u'群主题已更新。')
820 def do_admin(self, args):
821 '''将某人添加为管理员'''
822 if len(args) != 1:
823 self.msg.reply(u'请给出昵称。')
824 return
826 target = get_user_by_nick(args[0])
827 if target is None:
828 self.msg.reply(u'Sorry,查无此人。')
829 return
831 if target.is_admin:
832 self.msg.reply(u'%s 已经是管理员了。' % target.nick)
833 return
835 target.is_admin = True
836 target.put()
837 send_to_all_except(target.jid,
838 (u'%s 已成为管理员。' % target.nick) \
839 .encode('utf-8'))
840 xmpp.send_message(target.jid, u'你已是本群管理员。')
841 log_admin(self.sender, ADMIN % (target.nick, self.sender.nick))
843 def do_unadmin(self, args):
844 '''取消某人管理员的权限'''
845 if len(args) != 1:
846 self.msg.reply(u'请给出昵称。')
847 return
849 target = get_user_by_nick(args[0])
850 if target is None:
851 self.msg.reply(u'Sorry,查无此人。')
852 return
854 if not target.is_admin:
855 self.msg.reply(u'%s 不是管理员。' % target.nick)
856 return
858 target.is_admin = False
859 target.put()
860 send_to_all_except(target.jid,
861 (u'%s 已不再是管理员。' % target.nick) \
862 .encode('utf-8'))
863 xmpp.send_message(target.jid, u'你已不再是本群管理员。')
864 log_admin(self.sender, UNADMIN % (target.nick, self.sender.nick))
866 def do_block(self, args):
867 '''封禁某个 ID,参数为用户昵称或者 ID(如果不是已经加入的 ID 的话),以及封禁原因'''
868 if len(args) < 2:
869 self.msg.reply(u'请给出要封禁的用户和原因。')
870 return
872 target = get_user_by_nick(args[0])
873 reason = self.msg.body[len(self.sender.prefix):].split(None, 2)[-1]
874 if target is None:
875 jid = args[0]
876 name = jid
877 fullname = name
878 else:
879 jid = target.jid
880 name = target.nick
881 fullname = '%s (%s)' % (name, jid)
882 u = BlockedUser.gql('where jid = :1', jid).get()
883 if u is not None:
884 self.msg.reply(u'此 JID 已经被封禁。')
885 return
887 if jid == config.root:
888 self.msg.reply('不能封禁 root 用户')
889 return
891 if target:
892 target.delete()
893 u = BlockedUser(jid=jid, reason=reason)
894 u.put()
896 send_to_all_except(self.sender.jid,
897 (u'%s 已被本群封禁,理由为 %s。' % (name, reason)) \
898 .encode('utf-8'))
899 self.msg.reply(u'%s 已被本群封禁,理由为 %s。' % (fullname, reason))
900 xmpp.send_message(jid, u'你已被本群封禁,理由为 %s。' % reason)
901 xmpp.send_presence(jid, status=u'您已经被本群封禁')
902 log_admin(self.sender, BLOCK % (fullname, reason))
904 def do_unblock(self, args):
905 '''解封某个 ID'''
906 if len(args) != 1:
907 self.msg.reply(u'请给出要解封用户的 JID。')
908 return
910 target = get_blocked_user(args[0])
911 if target is None:
912 self.msg.reply(u'封禁列表中没有这个 JID。')
913 return
915 target.delete()
916 send_to_all((u'%s 已被解除封禁。' % args[0]) \
917 .encode('utf-8'))
918 log_admin(self.sender, UNBLOCK % args[0])
920 def do_groupstatus(self, arg):
921 '''设置群状态'''
922 grp = get_group_info()
923 if grp is None:
924 grp = Group()
925 grp.status = self.msg.body[len(self.sender.prefix):].split(None, 1)[-1]
926 grp.put()
927 for u in User.all():
928 xmpp.send_presence(u.jid, status=grp.status)
929 self.msg.reply(u'设置成功!')