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 jid
= self
.request
.get('from')
24 L
= utils
.MemLock('delete_user')
27 u
= lilytalk
.get_user_by_jid(jid
)
29 if u
.jid
== config
.root
:
30 xmpp
.send_message(jid
, u
'root 用户:离开前请确定你已做好善后工作!')
31 lilytalk
.log_onoff(u
, lilytalk
.LEAVE
)
32 lilytalk
.send_to_all(u
'%s 已经离开' % u
.nick
)
34 logging
.info(u
'%s 已经离开' % jid
)
38 class XMPPMsg(webapp
.RequestHandler
):
41 message
= xmpp
.Message(self
.request
.POST
)
42 lilytalk
.handle_message(message
)
43 except xmpp
.InvalidMessageError
:
44 logging
.info('InvalidMessageError: %r' % self
.request
.POST
)
46 class XMPPAvail(webapp
.RequestHandler
):
48 '''show 可以是 away、dnd(忙碌)或空(在线)'''
49 jid
, resource
= self
.request
.get('from').split('/', 1)
50 status
= self
.request
.get('status')
51 show
= self
.request
.get('show')
52 logging
.debug(u
'%s 的状态: %s (%s)' % (jid
, status
, show
))
54 show
= lilytalk
.STATUS_CODE
[show
]
56 logging
.error('%s has sent an incorrect show code %s' % (jid
, show
))
59 xmpp
.send_presence(self
.request
.get('from'),
60 status
=lilytalk
.notice
)
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英文版或者其它客户端。')
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 xmpp
.send_presence(self
.request
.get('from'),
103 status
=lilytalk
.notice
)
105 logging
.error('Error while sending presence to %s' % jid
)
108 class XMPPDummy(webapp
.RequestHandler
):
112 application
= webapp
.WSGIApplication(
114 ('/_ah/xmpp/subscription/subscribed/', XMPPSub
),
115 ('/_ah/xmpp/subscription/unsubscribed/', XMPPUnsub
),
116 ('/_ah/xmpp/message/chat/', XMPPMsg
),
117 ('/_ah/xmpp/presence/available/', XMPPAvail
),
118 ('/_ah/xmpp/presence/unavailable/', XMPPUnavail
),
119 ('/_ah/xmpp/presence/probe/', XMPPProbe
),
120 ('/_ah/xmpp/subscription/subscribe/', XMPPDummy
),
121 ('/_ah/xmpp/subscription/unsubscribe/', XMPPDummy
),
126 run_wsgi_app(application
)
128 if __name__
== "__main__":