-
Notifications
You must be signed in to change notification settings - Fork 0
/
libuser.py
141 lines (128 loc) · 4.24 KB
/
libuser.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
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from google.appengine.api import channel
from google.appengine.api import memcache
from django.utils import simplejson
import re
import datetime
import string
import oauth
import urllib
from urlparse import urlparse
import libchat
def getAliveUsers(username = "", token = ""): #optional params to get specific data
data = memcache.get("aliveusers")
if data is not None:
ndata = data
else:
#Memcache for this set is empty, create a new one
ndata = []
sdata = [] #specific data
if username != "":
for user in ndata:
if user['username'] == username:
sdata.append(user)
if token != "":
for user in ndata:
if user['token'] == token:
sdata.append(user)
if len(sdata) > 0:
return sdata
return ndata
def getAliveUsersSpecific(data, username = "", token = ""):
sdata = [] #specific data
if username != "":
for user in data:
if user['username'] == username:
sdata.append(user)
if token != "":
for user in data:
if user['token'] == token:
sdata.append(user)
return sdata
def writeAliveUsers(ndata):
memcache.set(key="aliveusers",value=ndata)
return
def appendAliveUsers(username):
created = datetime.datetime.now()
last_updated = created
client_id = username + str(created)
token = channel.create_channel(client_id)
nuser = {
'username':username,
'token':token,
'created':created,
'last_updated' :last_updated,
'client_id':client_id
}
data = getAliveUsers()
data.append(nuser)
writeAliveUsers(data)
return token
def pingUser(user_token):
users = getAliveUsers()
for user in users:
if user['token'] == user_token:
user['last_updated'] = datetime.datetime.now()
writeAliveUsers(users)
return
def listAliveUsers(users = None):
if users is None:
users = getAliveUsers()
arr_users = []
for user in users:
found = False
tmp_username = user['username']
if tmp_username == "__anonymous":
tmp_username = "(Anonymous)"
for a in arr_users:
if a['usr'] == tmp_username:
a['ch'] += 1
found = True
if found == False:
arr_users.append({'usr':tmp_username, 'ch':1})
output = template.render('userlist.html', {'users' : arr_users})
output_users = {
'return_type' : 'userlist',
'data' : output
}
users_json = simplejson.dumps(output_users)
for user in users:
client_id = user['client_id']# later change this to client id
try:
channel.send_message(client_id, users_json)
except channel.InvalidChannelClientIdError:
pass
def cronUpdateAliveUsers():
#delete zombie channel, update non zombie channel
changes = False
users = getAliveUsers()
for user in users:
diff_sec = (datetime.datetime.now() - user['last_updated']).seconds
if diff_sec > 60 * 1:
changes = True
usr_ch_count = len(getAliveUsersSpecific(users,username=user['username']))
if user['username'] != "__anonymous":
if usr_ch_count == 1:
chat = libchat.ChatData()
chat.usr = ''
chat.msg = user['username'] + " left the chat (request timed out)"
chat.date = user['last_updated']
chat.put()
users.remove(user)
if changes == True:
writeAliveUsers(users) # later, if no changes, don't perform this task
listAliveUsers(users)
return
def logout(username):
users = getAliveUsers()
for user in users:
if user['username'] == username:
users.remove(user)
writeAliveUsers(users)
return
def join(nickname):
token = appendAliveUsers(nickname)
return token