代码预览时跳过空行
[gaetalk.git] / chatmain.py
blob098dc05b3832e0411ee56612e78c257776ccc6f6
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 gaetalk.try_add_user(jid)
20 class XMPPUnsub(webapp.RequestHandler):
21 def post(self):
22 # 注意:由于 gtalk 客户端的错误处理,提供了一个使用命令离开的方式
23 jid = self.request.get('from')
24 L = utils.MemLock('delete_user')
25 L.require()
26 try:
27 gaetalk.del_user(jid)
28 finally:
29 L.release()
31 class XMPPMsg(webapp.RequestHandler):
32 def post(self):
33 try:
34 message = xmpp.Message(self.request.POST)
35 gaetalk.handle_message(message)
36 except xmpp.InvalidMessageError:
37 logging.info('InvalidMessageError: %r' % self.request.POST)
39 class XMPPAvail(webapp.RequestHandler):
40 def post(self):
41 '''show 可以是 away、dnd(忙碌)或空(在线)'''
42 jid, resource = self.request.get('from').split('/', 1)
43 status = self.request.get('status')
44 show = self.request.get('show')
45 logging.debug(u'%s 的状态: %s (%s)' % (jid, status, show))
46 try:
47 show = gaetalk.STATUS_CODE[show]
48 except KeyError:
49 logging.error('%s has sent an incorrect show code %s' % (jid, show))
50 return
51 try:
52 gaetalk.send_status(self.request.get('from'))
53 except xmpp.Error:
54 logging.error('Error while sending presence to %s' % jid)
55 return
56 u = gaetalk.get_user_by_jid(jid)
57 if u is not None:
58 modified = False
59 if resource not in u.resources:
60 u.resources.append(resource)
61 modified = True
62 if u.avail != show:
63 if u.avail == gaetalk.OFFLINE:
64 u.last_online_date = datetime.datetime.now()
65 u.avail = show
66 modified = True
67 if modified:
68 gaetalk.log_onoff(u, show, resource)
69 u.put()
70 if config.warnGtalk105 and resource.startswith('Talk.v105'):
71 xmpp.send_message(jid, u'您的客户端使用明文传输数据,为了大家的安全,请使用Gtalk英文版或者其它使用SSL加密的客户端。')
72 else:
73 gaetalk.try_add_user(jid, show, resource)
75 class XMPPUnavail(webapp.RequestHandler):
76 def post(self):
77 jid, resource = self.request.get('from').split('/', 1)
78 logging.info(u'%s 下线了' % jid)
79 u = gaetalk.get_user_by_jid(jid)
80 if u is not None:
81 if resource in u.resources:
82 u.resources.remove(resource)
83 if not u.resources:
84 u.avail = gaetalk.OFFLINE
85 u.last_offline_date = datetime.datetime.now()
86 u.put()
87 gaetalk.log_onoff(u, gaetalk.OFFLINE, resource)
89 class XMPPProbe(webapp.RequestHandler):
90 def post(self):
91 fulljid = self.request.get('from')
92 try:
93 gaetalk.send_status(fulljid)
94 except xmpp.Error:
95 logging.error('Error while sending presence to %s' % fulljid)
97 class XMPPDummy(webapp.RequestHandler):
98 def post(self):
99 pass
101 application = webapp.WSGIApplication(
103 ('/_ah/xmpp/subscription/subscribed/', XMPPSub),
104 ('/_ah/xmpp/subscription/unsubscribed/', XMPPUnsub),
105 ('/_ah/xmpp/message/chat/', XMPPMsg),
106 ('/_ah/xmpp/presence/available/', XMPPAvail),
107 ('/_ah/xmpp/presence/unavailable/', XMPPUnavail),
108 ('/_ah/xmpp/presence/probe/', XMPPProbe),
109 ('/_ah/xmpp/subscription/subscribe/', XMPPDummy),
110 ('/_ah/xmpp/subscription/unsubscribe/', XMPPDummy),
112 debug=True)
114 def main():
115 run_wsgi_app(application)
117 if __name__ == "__main__":
118 import counter
119 main()