2 # vim:fileencoding=utf-8
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
14 class XMPPSub(webapp
.RequestHandler
):
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
):
23 # 注意:由于 gtalk 客户端的错误处理,提供了一个使用命令离开的方式
24 jid
= self
.request
.get('from')
25 L
= utils
.MemLock('delete_user')
32 class XMPPMsg(webapp
.RequestHandler
):
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
):
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
))
48 show
= gaetalk
.STATUS_CODE
[show
]
50 logging
.error('%s has sent an incorrect show code %s' % (jid
, show
))
53 gaetalk
.send_status(self
.request
.get('from'))
55 logging
.error('Error while sending presence to %s' % jid
)
57 u
= gaetalk
.get_user_by_jid(jid
)
60 if resource
not in u
.resources
:
61 u
.resources
.append(resource
)
64 if u
.avail
== gaetalk
.OFFLINE
:
65 u
.last_online_date
= datetime
.datetime
.now()
69 gaetalk
.log_onoff(u
, show
, resource
)
71 if config
.warnGtalk105
and resource
.startswith('Talk.v105'):
72 xmpp
.send_message(jid
, u
'您的客户端使用明文传输数据,为了大家的安全,请使用Gtalk英文版或者其它使用SSL加密的客户端。')
74 gaetalk
.try_add_user(jid
, show
, resource
)
76 class XMPPUnavail(webapp
.RequestHandler
):
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
)
83 if resource
in u
.resources
:
84 u
.resources
.remove(resource
)
86 u
.avail
= gaetalk
.OFFLINE
87 u
.last_offline_date
= datetime
.datetime
.now()
89 gaetalk
.log_onoff(u
, gaetalk
.OFFLINE
, resource
)
91 class XMPPProbe(webapp
.RequestHandler
):
93 fulljid
= self
.request
.get('from')
95 gaetalk
.send_status(self
.request
.get('from'))
97 logging
.error('Error while sending presence to %s' % jid
)
100 class XMPPDummy(webapp
.RequestHandler
):
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
),
118 run_wsgi_app(application
)
120 if __name__
== "__main__":