添加 notice 命令
[gaetalk.git] / chatmain.py
blobc8712070f9aba39589f5b75a38d6f29471a29de2
1 #!/usr/bin/env python2
2 # vim:fileencoding=utf-8
4 import logging
5 import datetime
6 from google.appengine.api import xmpp
7 from google.appengine.ext import webapp
8 from google.appengine.ext.webapp.util import run_wsgi_app
10 import lilytalk
11 import config
12 import utils
14 class XMPPSub(webapp.RequestHandler):
15 '''被人加好友了~可能被触发多次'''
16 def post(self):
17 jid = self.request.get('from')
18 u = lilytalk.get_user_by_jid(jid)
19 lilytalk.try_add_user(jid)
21 class XMPPUnsub(webapp.RequestHandler):
22 def post(self):
23 jid = self.request.get('from')
24 L = utils.MemLock('delete_user')
25 L.require()
26 try:
27 u = lilytalk.get_user_by_jid(jid)
28 if u is not None:
29 if u.jid == config.root:
30 xmpp.send_message(jid, u'root 用户:离开前请确定你已做好善后工作!')
31 lilytalk.log_onoff(u, lilytalk.LEAVE)
32 lilytalk.send_to_all(u'%s 已经离开' % u.nick)
33 u.delete()
34 logging.info(u'%s 已经离开' % jid)
35 finally:
36 L.release()
38 class XMPPMsg(webapp.RequestHandler):
39 def post(self):
40 try:
41 message = xmpp.Message(self.request.POST)
42 lilytalk.handle_message(message)
43 except xmpp.InvalidMessageError:
44 logging.info('InvalidMessageError: %r' % self.request.POST)
46 class XMPPAvail(webapp.RequestHandler):
47 def post(self):
48 '''show 可以是 away、dnd(忙碌)或空(在线)'''
49 jid, resource = self.request.get('from').split('/', 1)
50 status = self.request.get('status')
51 show = self.request.get('show')
52 logging.debug(u'%s 的状态: %s (%s)' % (jid, status, show))
53 try:
54 show = lilytalk.STATUS_CODE[show]
55 except KeyError:
56 logging.error('%s has sent an incorrect show code %s' % (jid, show))
57 return
58 try:
59 xmpp.send_presence(self.request.get('from'),
60 status=lilytalk.notice)
61 except xmpp.Error:
62 logging.error('Error while sending presence to %s' % jid)
63 return
64 u = lilytalk.get_user_by_jid(jid)
65 if u is not None:
66 modified = False
67 if resource not in u.resources:
68 u.resources.append(resource)
69 modified = True
70 if u.avail != show:
71 if u.avail == lilytalk.OFFLINE:
72 u.last_online_date = datetime.datetime.now()
73 u.avail = show
74 modified = True
75 if modified:
76 lilytalk.log_onoff(u, show, resource)
77 u.put()
78 if config.warnGtalk105 and resource.startswith('Talk.v105'):
79 xmpp.send_message(jid, u'您的客户端使用明文传输数据,这样不安全,请使用Gtalk英文版或者其它客户端。')
80 else:
81 lilytalk.try_add_user(jid, show, resource)
83 class XMPPUnavail(webapp.RequestHandler):
84 def post(self):
85 jid, resource = self.request.get('from').split('/', 1)
86 status = self.request.get('status')
87 logging.info(u'%s 下线了' % jid)
88 u = lilytalk.get_user_by_jid(jid)
89 if u is not None:
90 if resource in u.resources:
91 u.resources.remove(resource)
92 if not u.resources:
93 u.avail = lilytalk.OFFLINE
94 u.last_offline_date = datetime.datetime.now()
95 u.put()
96 lilytalk.log_onoff(u, lilytalk.OFFLINE, resource)
98 class XMPPProbe(webapp.RequestHandler):
99 def post(self):
100 fulljid = self.request.get('from')
101 try:
102 xmpp.send_presence(self.request.get('from'),
103 status=lilytalk.notice)
104 except xmpp.Error:
105 logging.error('Error while sending presence to %s' % jid)
106 return
108 class XMPPDummy(webapp.RequestHandler):
109 def post(self):
110 pass
112 application = webapp.WSGIApplication(
114 ('/_ah/xmpp/subscription/subscribed/', XMPPSub),
115 ('/_ah/xmpp/subscription/unsubscribed/', XMPPUnsub),
116 ('/_ah/xmpp/message/chat/', XMPPMsg),
117 ('/_ah/xmpp/presence/available/', XMPPAvail),
118 ('/_ah/xmpp/presence/unavailable/', XMPPUnavail),
119 ('/_ah/xmpp/presence/probe/', XMPPProbe),
120 ('/_ah/xmpp/subscription/subscribe/', XMPPDummy),
121 ('/_ah/xmpp/subscription/unsubscribe/', XMPPDummy),
123 debug=True)
125 def main():
126 run_wsgi_app(application)
128 if __name__ == "__main__":
129 main()