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
= lilytalk
.get_user_by_jid(jid
)
19 lilytalk
.try_add_user(jid
)
21 class XMPPUnsub(webapp
.RequestHandler
):
23 # 注意:由于 gtalk 客户端的错误处理,提供了一个使用命令离开的方式
24 jid
= self
.request
.get('from')
25 L
= utils
.MemLock('delete_user')
28 u
= lilytalk
.get_user_by_jid(jid
)
30 if u
.jid
== config
.root
:
31 xmpp
.send_message(jid
, u
'root 用户:离开前请确定你已做好善后工作!')
32 lilytalk
.log_onoff(u
, lilytalk
.LEAVE
)
33 lilytalk
.send_to_all(u
'%s 已经离开' % u
.nick
)
35 logging
.info(u
'%s 已经离开' % jid
)
39 class XMPPMsg(webapp
.RequestHandler
):
42 message
= xmpp
.Message(self
.request
.POST
)
43 lilytalk
.handle_message(message
)
44 except xmpp
.InvalidMessageError
:
45 logging
.info('InvalidMessageError: %r' % self
.request
.POST
)
47 class XMPPAvail(webapp
.RequestHandler
):
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
))
55 show
= lilytalk
.STATUS_CODE
[show
]
57 logging
.error('%s has sent an incorrect show code %s' % (jid
, show
))
60 lilytalk
.send_status(self
.request
.get('from'))
62 logging
.error('Error while sending presence to %s' % jid
)
64 u
= lilytalk
.get_user_by_jid(jid
)
67 if resource
not in u
.resources
:
68 u
.resources
.append(resource
)
71 if u
.avail
== lilytalk
.OFFLINE
:
72 u
.last_online_date
= datetime
.datetime
.now()
76 lilytalk
.log_onoff(u
, show
, resource
)
78 if config
.warnGtalk105
and resource
.startswith('Talk.v105'):
79 xmpp
.send_message(jid
, u
'您的客户端使用明文传输数据,为了大家的安全,请使用Gtalk英文版或者其它使用SSL加密的客户端。')
81 lilytalk
.try_add_user(jid
, show
, resource
)
83 class XMPPUnavail(webapp
.RequestHandler
):
85 jid
, resource
= self
.request
.get('from').split('/', 1)
86 status
= self
.request
.get('status')
87 logging
.info(u
'%s 下线了' % jid
)
88 u
= lilytalk
.get_user_by_jid(jid
)
90 if resource
in u
.resources
:
91 u
.resources
.remove(resource
)
93 u
.avail
= lilytalk
.OFFLINE
94 u
.last_offline_date
= datetime
.datetime
.now()
96 lilytalk
.log_onoff(u
, lilytalk
.OFFLINE
, resource
)
98 class XMPPProbe(webapp
.RequestHandler
):
100 fulljid
= self
.request
.get('from')
102 lilytalk
.send_status(self
.request
.get('from'))
104 logging
.error('Error while sending presence to %s' % jid
)
107 class XMPPDummy(webapp
.RequestHandler
):
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
),
125 run_wsgi_app(application
)
127 if __name__
== "__main__":