-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
talktalktalk.py
177 lines (146 loc) · 7.26 KB
/
talktalktalk.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# TalkTalkTalk
#
# is an easy-installable small chat room, with chat history.
#
# author: Joseph Ernest (twitter: @JosephErnest)
# url: http://github.com/josephernest/talktalktalk
# license: MIT license
import sys, json, bleach, time, threading, dumbdbm, random, re
import daemon
from bottle import route, run, view, request, post, ServerAdapter, get, static_file
from gevent import pywsgi
from geventwebsocket.handler import WebSocketHandler
from geventwebsocket.exceptions import WebSocketError
from collections import deque
from config import PORT, HOST, ADMINNAME, ADMINHIDDENNAME, ALLOWEDTAGS
idx = 0
def websocket(callback):
def wrapper(*args, **kwargs):
callback(request.environ.get('wsgi.websocket'), *args, **kwargs)
return wrapper
class GeventWebSocketServer(ServerAdapter):
def run(self, handler):
server = pywsgi.WSGIServer((self.host, self.port), handler, handler_class=WebSocketHandler)
server.serve_forever()
def main():
global idx
db = dumbdbm.open('talktalktalk.db', 'c')
idx = len(db)
users = {}
pings = {}
usermessagetimes = {}
def send_userlist():
for u in users.keys():
if not u.closed:
u.send(json.dumps({'type' : 'userlist', 'connected': users.values()}))
def clean_username(usr, ws):
username = bleach.clean(usr, tags=ALLOWEDTAGS, strip=True)
#username = re.sub('[ :]', '', username) # removes " ", ":", and the evil char "" http://unicode-table.com/fr/200D/
username = re.sub(r'\W+', '', username) # because of spam and usage of malicious utf8 characters, let's use alphanumeric usernames only for now
username = username[:16]
if username.lower() == ADMINNAME or username == '':
username = 'user' + str(random.randint(0, 1000))
ws.send(json.dumps({'type' : 'usernameunavailable', 'username' : username}))
elif username.lower() == ADMINHIDDENNAME:
username = ADMINNAME
ws.send(json.dumps({'type' : 'displayeduser', 'username' : username}))
return username
def dbworker(): # when a user disappears during more than 30 seconds (+/- 10), remove him/her from the userlist
while True:
userlistchanged = False
t = time.time()
for ws in users.copy():
if t - pings[ws] > 30:
del users[ws]
del pings[ws]
userlistchanged = True
if userlistchanged:
send_userlist()
time.sleep(10)
dbworkerThread = threading.Thread(target=dbworker)
dbworkerThread.daemon = True
dbworkerThread.start()
@get('/ws', apply=[websocket])
def chat(ws):
global idx
usermessagetimes[ws] = deque(maxlen=10)
while True:
try:
receivedmsg = ws.receive()
if receivedmsg is not None:
receivedmsg = receivedmsg.decode('utf8')
if len(receivedmsg) > 4096: # this user is probably a spammer
ws.send(json.dumps({'type' : 'flood'}))
break
pings[ws] = time.time()
if receivedmsg == 'ping': # ping/pong packet to make sure connection is still alive
ws.send('id' + str(idx-1)) # send the latest message id in return
if ws not in users: # was deleted by dbworker
ws.send(json.dumps({'type' : 'username'}))
else:
usermessagetimes[ws].append(time.time()) # flood control
if len(usermessagetimes[ws]) == usermessagetimes[ws].maxlen:
if usermessagetimes[ws][-1] - usermessagetimes[ws][0] < 5: # if more than 10 messages in 5 seconds (including ping messages)
ws.send(json.dumps({'type' : 'flood'})) # disconnect the spammer
break
msg = json.loads(receivedmsg)
if msg['type'] == 'message':
message = (bleach.clean(msg['message'], tags=ALLOWEDTAGS, strip=True)).strip()
if ws not in users: # is this really mandatory ?
username = clean_username(msg['username'], ws)
users[ws] = username
send_userlist()
if message:
if len(message) > 1000:
message = message[:1000] + '...'
s = json.dumps({'type' : 'message', 'message': message, 'username': users[ws], 'id': idx, 'datetime': int(time.time())})
db[str(idx)] = s # Neither dumbdbm nor shelve module allow integer as key... I'm still looking for a better solution!
idx += 1
for u in users.keys():
u.send(s)
elif msg['type'] == 'messagesbefore':
idbefore = msg['id']
ws.send(json.dumps({'type' : 'messages', 'before': 1, 'messages': [db[str(i)] for i in range(max(0,idbefore - 100),idbefore)]}))
elif msg['type'] == 'messagesafter':
idafter = msg['id']
ws.send(json.dumps({'type' : 'messages', 'before': 0, 'messages': [db[str(i)] for i in range(idafter,idx)]}))
elif msg['type'] == 'username':
username = clean_username(msg['username'], ws)
if ws not in users: # welcome new user
ws.send(json.dumps({'type' : 'messages', 'before': 0, 'messages': [db[str(i)] for i in range(max(0,idx - 100),idx)]}))
users[ws] = username
send_userlist()
else:
break
except (WebSocketError, ValueError, UnicodeDecodeError): # ValueError happens for example when "No JSON object could be decoded", would be interesting to log it
break
if ws in users:
del users[ws]
del pings[ws]
send_userlist()
@route('/')
@route('/index.html')
@view('talktalktalk.html')
def index():
context = {'request': request}
return (context)
@route('/popsound.mp3')
def popsound():
return static_file('popsound.mp3', root='.')
run(host=HOST, port=PORT, debug=True, server=GeventWebSocketServer)
class talktalktalk(daemon.Daemon):
def run(self):
main()
if len(sys.argv) == 1: # command line interactive mode
main()
elif len(sys.argv) == 2: # daemon mode
daemon = talktalktalk(pidfile='_.pid', stdout='log.txt', stderr='log.txt')
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()