-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
201 lines (157 loc) · 5.61 KB
/
chat.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import socketio
from colorsys import hsv_to_rgb
import firebase_admin
from firebase_admin import credentials, auth, db
import time
import os
import re
import uuid
import hashlib
import aiohttp
import base64
from chat_fb import userinfo_ref
from dotenv import load_dotenv
load_dotenv()
# region PCLOUD SCRAPER
pc_endpoint = 'https://api.pcloud.com'
pc_auth = {'username': os.environ['STORAGE_EMAIL'], 'password': os.environ['STORAGE_PASSWORD']}
async def delete_file(fileid):
async with aiohttp.ClientSession() as s:
async with s.get(pc_endpoint+'/deletefile', params={**pc_auth, 'fileid': fileid}) as r:
pass
async def get_publink(fileid):
async with aiohttp.ClientSession() as s:
async with s.get(pc_endpoint+'/getfilepublink', params={**pc_auth, 'fileid': fileid}) as r:
res = await r.json()
return res['link']
# endregion PCLOUD SCRAPER
# region SOCKET.IO
origins = [
'https://kamiak.org',
'https://beta.kamiak.org',
'https://kamiak.herokuapp.com',
'https://kamiakhs.github.io',
'http://localhost',
'http://localhost:8000',
'http://192.168.1.9',
'http://10.1.10.249',
]
files = {
'/': 'public/'
}
socket = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins='*', max_http_buffer_size=100*1024*1024+1000)
app = socketio.ASGIApp(socket, static_files=files, socketio_path='/chat/socket.io')
# endregion SOCKET.IO
users = {}
uid_count = {} # uid: count
salt = uuid.uuid4().hex
def random_hue(seed):
hash = 7
for c in seed:
hash = (ord(c)+(hash<<5)-(hash>>2))%47160
return hash%360
def random_color(seed):
rgb = hsv_to_rgb(random_hue(seed)/360, 1, .8)
return '#'+''.join('%02x'%round(i*255) for i in rgb)
def timestamp():
return time.time_ns()//1000000
def jslen(string):
return int(len(string.encode(encoding='utf_16_le'))/2)
def verify_nickname(nickname):
return 2 <= jslen(nickname) <= 24
def format_nickname(nickname):
return re.sub(r'\s+', ' ', nickname.strip())
def generate_nickname(token):
names = token['name'].split(' ')
return ' '.join(names[:-1]) if token['email'][:-20].isdigit() else names[-1]
def generate_userinfo(userinfo, token):
uid = token['uid']
if uid not in userinfo: userinfo[uid] = {}
userinfo[uid] = {
'nickname': generate_nickname(token),
**userinfo[uid],
'picture': token['picture'],
'name': token['name'],
'email': token['email'],
}
def generate_fileid():
return uuid.uuid4().hex
def generate_filekey(fileid):
return hashlib.sha1((fileid+salt).encode('utf-8')).hexdigest()
@socket.event
async def connect(sid, environ, auth_key):
print(sid, 'connected')
try:
token = auth.verify_id_token(auth_key['token'])
# assert token['email'].endswith('@mukilteo.wednet.edu')
except:
print("COULDN'T VERIFY")
await socket.disconnect(sid)
return
uid = token['uid']
userinfo = userinfo_ref.get()
generate_userinfo(userinfo, token)
userinfo_ref.child(uid).set(userinfo[uid])
users[sid] = {
'sid': sid,
'uid': token['uid'],
'color': '', # random_color(nickname+queries['seed'][0]),
}
await socket.emit('login', {'sid': sid, 'users': users, 'sync': userinfo}, to=sid)
await socket.emit('add user', {'sid': sid, 'user': users[sid], 'timestamp': timestamp()}, skip_sid=sid)
if uid in uid_count:
uid_count[uid] += 1
else:
uid_count[uid] = 1
await socket.emit('add uid', {'uid': uid, 'info': userinfo[uid]})
@socket.event
async def disconnect(sid):
print(sid, 'disconnected')
if sid in users:
await socket.emit('remove user', {'sid': sid, 'timestamp': timestamp()}, skip_sid=sid)
uid = users[sid]['uid']
del users[sid]
uid_count[uid] -= 1
if uid_count[uid] == 0:
del uid_count[uid]
await socket.emit('remove uid', {'uid': uid})
@socket.on('send message')
async def send_message(sid, data):
if len(data['files']) > 0: print('RECEIVED', data['files'][0]['name'])
message = data['message'].strip()
files = [{**file, 'id': generate_fileid()} for file in data['files']]
if len(message) == len(files) == 0: return
await socket.emit('new message', {'sid': sid, 'message': message, 'files': files, 'timestamp': timestamp()}, skip_sid=sid)
return {'files': [{**file, 'key': generate_filekey(file['id'])} for file in files]}
@socket.on('start typing')
async def start_typing(sid):
await socket.emit('start typing', {'sid': sid}, skip_sid=sid)
@socket.on('stop typing')
async def stop_typing(sid):
await socket.emit('stop typing', {'sid': sid}, skip_sid=sid)
@socket.on('set nickname')
async def set_nickname(sid, data):
nickname = format_nickname(data['nickname'])
print('set nickname '+nickname)
uid = users[sid]['uid']
if not verify_nickname(nickname):
await socket.emit('set nickname', {'uid': uid, 'nickname': userinfo_ref.get()[uid]}, to=sid)
return
userinfo_ref.child(uid).set({'nickname': nickname})
await socket.emit('set nickname', {'uid': uid, 'nickname': nickname}, skip_sid=sid)
print('done')
@socket.on('ping')
async def ping(sid):
return
@socket.on('update file')
async def update_file(sid, data):
if data['file']['key'] != generate_filekey(data['file']['id']): return
publink = await get_publink(data['file']['fileid'])
await socket.emit('update file', {'file': {'id': data['file']['id'], 'name': data['file']['name']}, 'publink': publink})
@socket.on('upload progress')
async def upload_progress(sid, data):
if data['file']['key'] != generate_filekey(data['file']['id']): return
await socket.emit('upload progress', {'file': {'id': data['file']['id']}, 'progress': data['progress']})
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host='localhost', port=80, ws='websockets', ws_max_size=100*1024*1024+1000)