成员加入/离开不发送通知
[gaetalk.git] / chatmain.py
blob3b7b51802e01c549d106380c257f52a9fb612044
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 gaetalk.del_user(jid)
29 finally:
30 L.release()
32 class XMPPMsg(webapp.RequestHandler):
33 def post(self):
34 try:
35 message = xmpp.Message(self.request.POST)
36 gaetalk.handle_message(message)
37 except xmpp.InvalidMessageError:
38 logging.info('InvalidMessageError: %r' % self.request.POST)
40 class XMPPAvail(webapp.RequestHandler):
41 def post(self):
42 '''show 可以是 away、dnd(忙碌)或空(在线)'''
43 jid, resource = self.request.get('from').split('/', 1)
44 status = self.request.get('status')
45 show = self.request.get('show')
46 logging.debug(u'%s 的状态: %s (%s)' % (jid, status, show))
47 try:
48 show = gaetalk.STATUS_CODE[show]
49 except KeyError:
50 logging.error('%s has sent an incorrect show code %s' % (jid, show))
51 return
52 try:
53 gaetalk.send_status(self.request.get('from'))
54 except xmpp.Error:
55 logging.error('Error while sending presence to %s' % jid)
56 return
57 u = gaetalk.get_user_by_jid(jid)
58 if u is not None:
59 modified = False
60 if resource not in u.resources:
61 u.resources.append(resource)
62 modified = True
63 if u.avail != show:
64 if u.avail == gaetalk.OFFLINE:
65 u.last_online_date = datetime.datetime.now()
66 u.avail = show
67 modified = True
68 if modified:
69 gaetalk.log_onoff(u, show, resource)
70 u.put()
71 if config.warnGtalk105 and resource.startswith('Talk.v105'):
72 xmpp.send_message(jid, u'您的客户端使用明文传输数据,为了大家的安全,请使用Gtalk英文版或者其它使用SSL加密的客户端。')
73 else:
74 gaetalk.try_add_user(jid, show, resource)
76 class XMPPUnavail(webapp.RequestHandler):
77 def post(self):
78 jid, resource = self.request.get('from').split('/', 1)
79 status = self.request.get('status')
80 logging.info(u'%s 下线了' % jid)
81 u = gaetalk.get_user_by_jid(jid)
82 if u is not None:
83 if resource in u.resources:
84 u.resources.remove(resource)
85 if not u.resources:
86 u.avail = gaetalk.OFFLINE
87 u.last_offline_date = datetime.datetime.now()
88 u.put()
89 gaetalk.log_onoff(u, gaetalk.OFFLINE, resource)
91 class XMPPProbe(webapp.RequestHandler):
92 def post(self):
93 fulljid = self.request.get('from')
94 try:
95 gaetalk.send_status(self.request.get('from'))
96 except xmpp.Error:
97 logging.error('Error while sending presence to %s' % jid)
98 return
100 class XMPPDummy(webapp.RequestHandler):
101 def post(self):
102 pass
104 application = webapp.WSGIApplication(
106 ('/_ah/xmpp/subscription/subscribed/', XMPPSub),
107 ('/_ah/xmpp/subscription/unsubscribed/', XMPPUnsub),
108 ('/_ah/xmpp/message/chat/', XMPPMsg),
109 ('/_ah/xmpp/presence/available/', XMPPAvail),
110 ('/_ah/xmpp/presence/unavailable/', XMPPUnavail),
111 ('/_ah/xmpp/presence/probe/', XMPPProbe),
112 ('/_ah/xmpp/subscription/subscribe/', XMPPDummy),
113 ('/_ah/xmpp/subscription/unsubscribe/', XMPPDummy),
115 debug=True)
117 def main():
118 run_wsgi_app(application)
120 if __name__ == "__main__":
121 main()