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 gaetalk
.try_add_user(jid
)
20 class XMPPUnsub(webapp
.RequestHandler
):
22 # 注意:由于 gtalk 客户端的错误处理,提供了一个使用命令离开的方式
23 jid
= self
.request
.get('from')
24 L
= utils
.MemLock('delete_user')
31 class XMPPMsg(webapp
.RequestHandler
):
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
):
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
))
47 show
= gaetalk
.STATUS_CODE
[show
]
49 logging
.error('%s has sent an incorrect show code %s' % (jid
, show
))
52 gaetalk
.send_status(self
.request
.get('from'))
54 logging
.error('Error while sending presence to %s' % jid
)
56 u
= gaetalk
.get_user_by_jid(jid
)
59 if resource
not in u
.resources
:
60 u
.resources
.append(resource
)
63 if u
.avail
== gaetalk
.OFFLINE
:
64 u
.last_online_date
= datetime
.datetime
.now()
68 gaetalk
.log_onoff(u
, show
, resource
)
70 if config
.warnGtalk105
and resource
.startswith('Talk.v105'):
71 xmpp
.send_message(jid
, u
'您的客户端使用明文传输数据,为了大家的安全,请使用Gtalk英文版或者其它使用SSL加密的客户端。')
73 gaetalk
.try_add_user(jid
, show
, resource
)
75 class XMPPUnavail(webapp
.RequestHandler
):
77 jid
, resource
= self
.request
.get('from').split('/', 1)
78 logging
.info(u
'%s 下线了' % jid
)
79 u
= gaetalk
.get_user_by_jid(jid
)
81 if resource
in u
.resources
:
82 u
.resources
.remove(resource
)
84 u
.avail
= gaetalk
.OFFLINE
85 u
.last_offline_date
= datetime
.datetime
.now()
87 gaetalk
.log_onoff(u
, gaetalk
.OFFLINE
, resource
)
89 class XMPPProbe(webapp
.RequestHandler
):
91 fulljid
= self
.request
.get('from')
93 gaetalk
.send_status(fulljid
)
95 logging
.error('Error while sending presence to %s' % fulljid
)
97 class XMPPDummy(webapp
.RequestHandler
):
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
),
115 run_wsgi_app(application
)
117 if __name__
== "__main__":