-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
146 lines (123 loc) · 4.43 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from google.appengine.api import app_identity
from google.appengine.api import taskqueue
from google.appengine.api import xmpp
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import logging
import os
import sys
appid = app_identity.get_application_id()
version = os.environ['CURRENT_VERSION_ID'].split('.')[0]
cluster = 'appspot.com'
custom_cluster = 'appspotchat.com'
class Friends(db.Model):
bare_jid = db.StringProperty()
my_jid = db.StringProperty()
nickname = db.StringProperty()
class Resources(db.Model):
jid = db.StringProperty()
bare_jid = db.StringProperty()
my_jid = db.StringProperty()
show = db.StringProperty()
status = db.StringProperty()
class MessageHandler(webapp.RequestHandler):
def post(self):
pass
class ResourceFetcher():
def __init__(self, bare_jid, my_jid):
q = Resources.all()
q.filter('bare_jid =', bare_jid)
q.filter('my_jid =', my_jid)
q.order('bare_jid')
self.pairs = {}
for resource in q:
self.pairs['.'.join([resource.show, resource.status])] = 1
def best(self):
SHOW_BY_VALUE = ['', 'away', 'dnd', 'xa']
best_idx = sys.maxint
for pair in self.pairs:
(show, status) = pair.split('.')
idx = SHOW_BY_VALUE.index(show)
if idx < best_idx:
best_idx = idx
best_pair = (show, status)
return best_pair
class MainPage(webapp.RequestHandler):
def get(self):
for friend in Friends.all():
(show, status) = ResourceFetcher(friend.bare_jid, friend.my_jid).best()
self.response.out.write('%s, %s, %s, %s<br>' % (friend.bare_jid,
friend.my_jid,
show,
status))
xmpp.send_presence(friend.bare_jid, from_jid=friend.my_jid,
presence_type='probe')
class SubscribeHandler(webapp.RequestHandler):
def post(self):
sender = self.request.get('from').split('/')[0]
to = self.request.get('to').split('/')[0]
logging.debug('Subscription status: %s, stanza: %s.',
self.request.get('status'), self.request.get('stanza'))
logging.info('Subscription request from %s to %s.', sender, to)
class PresenceHandler(webapp.RequestHandler):
def post(self):
available = self.request.path == '/_ah/xmpp/presence/available/'
sender = self.request.get('from')
to = self.request.get('to').split('/')[0]
status = self.request.get('status')
show = self.request.get('show')
logging.debug('Show: %s, Status: %s, stanza: %s, available: %s',
show, status, self.request.get('stanza'), available)
logging.debug('Presence from %s to %s.', sender, to)
taskqueue.add(url='/updatepresence',
params = {'jid': sender,
'my_jid': to,
'show': show,
'available': available,
'status': status})
class UpdatePresenceHandler(webapp.RequestHandler):
def post(self):
available = self.request.get('available') == 'True'
sender = self.request.get('jid')
to = self.request.get('my_jid')
status = self.request.get('status')
show = self.request.get('show')
q = Friends.all()
q.filter("bare_jid =", sender.split('/')[0])
q.filter("my_jid =", to)
f = q.get()
if not f:
Friends(bare_jid=sender.split('/')[0],
my_jid=to).put()
q = Resources.all()
q.filter("jid =", sender)
q.filter("my_jid =", to)
r = q.get()
if not available:
if r:
r.delete()
elif r:
r.show = show
r.status = status
r.put()
else:
resource = Resources(jid=sender,
bare_jid=sender.split('/')[0],
my_jid=to,
show=show,
status=status)
resource.put()
def get(self):
self.post()
application = webapp.WSGIApplication([
('/', MainPage),
('/updatepresence', UpdatePresenceHandler),
('/_ah/xmpp/message/chat/', MessageHandler),
('/_ah/xmpp/subscription/subscribe/', SubscribeHandler),
('/_ah/xmpp/presence/unavailable/', PresenceHandler),
('/_ah/xmpp/presence/available/', PresenceHandler)], debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()