昵称中允许使用 + 号
[gaetalk.git] / chatmain.py
blob2d82502d95f60fa433f75740fccd4d3ba8f09a69
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 gaetalk
11 import config
12 import utils
14 class XMPPSub(webapp.RequestHandler):
15 '''被人加好友了~可能被触发多次'''
16 def post(self):
17 jid = self.request.get('from')
18 u = gaetalk.get_user_by_jid(jid)
19 gaetalk.try_add_user(jid)
21 class XMPPUnsub(webapp.RequestHandler):
22 def post(self):
23 # 注意:由于 gtalk 客户端的错误处理,提供了一个使用命令离开的方式
24 jid = self.request.get('from')
25 L = utils.MemLock('delete_user')
26 L.require()
27 try:
28 u = gaetalk.get_user_by_jid(jid)
29 if u is not None:
30 if u.jid == config.root:
31 xmpp.send_message(jid, u'root 用户:离开前请确定你已做好善后工作!')
32 gaetalk.log_onoff(u, gaetalk.LEAVE)
33 gaetalk.send_to_all(u'%s 已经离开' % u.nick)
34 u.delete()
35 logging.info(u'%s 已经离开' % jid)
36 finally:
37 L.release()
39 class XMPPMsg(webapp.RequestHandler):
40 def post(self):
41 try:
42 message = xmpp.Message(self.request.POST)
43 gaetalk.handle_message(message)
44 except xmpp.InvalidMessageError:
45 logging.info('InvalidMessageError: %r' % self.request.POST)
47 class XMPPAvail(webapp.RequestHandler):
48 def post(self):
49 '''show 可以是 away、dnd(忙碌)或空(在线)'''
50 jid, resource = self.request.get('from').split('/', 1)
51 status = self.request.get('status')
52 show = self.request.get('show')
53 logging.debug(u'%s 的状态: %s (%s)' % (jid, status, show))
54 try:
55 show = gaetalk.STATUS_CODE[show]
56 except KeyError:
57 logging.error('%s has sent an incorrect show code %s' % (jid, show))
58 return
59 try:
60 gaetalk.send_status(self.request.get('from'))
61 except xmpp.Error:
62 logging.error('Error while sending presence to %s' % jid)
63 return
64 u = gaetalk.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 == gaetalk.OFFLINE:
72 u.last_online_date = datetime.datetime.now()
73 u.avail = show
74 modified = True
75 if modified:
76 gaetalk.log_onoff(u, show, resource)
77 u.put()
78 if config.warnGtalk105 and resource.startswith('Talk.v105'):
79 xmpp.send_message(jid, u'您的客户端使用明文传输数据,为了大家的安全,请使用Gtalk英文版或者其它使用SSL加密的客户端。')
80 else:
81 gaetalk.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 = gaetalk.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 = gaetalk.OFFLINE
94 u.last_offline_date = datetime.datetime.now()
95 u.put()
96 gaetalk.log_onoff(u, gaetalk.OFFLINE, resource)
98 class XMPPProbe(webapp.RequestHandler):
99 def post(self):
100 fulljid = self.request.get('from')
101 try:
102 gaetalk.send_status(self.request.get('from'))
103 except xmpp.Error:
104 logging.error('Error while sending presence to %s' % jid)
105 return
107 class XMPPDummy(webapp.RequestHandler):
108 def post(self):
109 pass
111 application = webapp.WSGIApplication(
113 ('/_ah/xmpp/subscription/subscribed/', XMPPSub),
114 ('/_ah/xmpp/subscription/unsubscribed/', XMPPUnsub),
115 ('/_ah/xmpp/message/chat/', XMPPMsg),
116 ('/_ah/xmpp/presence/available/', XMPPAvail),
117 ('/_ah/xmpp/presence/unavailable/', XMPPUnavail),
118 ('/_ah/xmpp/presence/probe/', XMPPProbe),
119 ('/_ah/xmpp/subscription/subscribe/', XMPPDummy),
120 ('/_ah/xmpp/subscription/unsubscribe/', XMPPDummy),
122 debug=True)
124 def main():
125 run_wsgi_app(application)
127 if __name__ == "__main__":
128 main()