-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
339 lines (324 loc) · 7.9 KB
/
commands.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import time
import os
import json
import html
import sqlite3
import ezemail
from basic import *
connection = sqlite3.connect(folder()+'data.sql', check_same_thread=False)
connection.cursor().execute('PRAGMA foreign_keys = ON;')
def checkCreds(username,password):
return connection.cursor().execute(
'SELECT password FROM users where username=?',
(username,)
).fetchone() == (password,)
def prefs(username):
return dict( connection.cursor().execute(
'''SELECT preference, value from preferences
WHERE username == ?''',
(username,)
).fetchall() )
def login(contents):
return {'status':0,'message':'<h1>Welcome. Enjoy your premium <a href="content.html#posts">membership.</a></h1>'}
def posts(contents):
start = contents['data']['start']
length = int(prefs(contents['username'])['postnumber'])
return {'posts': [
{
'time':message[0],
'title':message[1],
'author':message[2],
'contents':message[3]
}
for message in connection.cursor().execute(
'''
SELECT * from (
SELECT * from (
SELECT
time,
title,
author,
content
FROM posts
ORDER BY time DESC
LIMIT ?
)
ORDER BY time ASC
LIMIT ?
)
ORDER BY time DESC
;''',
(
start+length,
length
)
).fetchall()
],'start':start}
def post(contents):
post = contents['data']
connection.cursor().execute(
'''
INSERT INTO posts
VALUES (?, ?, ?, ?, ?)
''',(
time.time(),
html.escape(post['title']),
contents['username'],
html.escape(post['contents']),
'[]'
)
)
connection.commit()
return {'status':0}
def comments(contents):
postnum = contents['data']['post']
post = connection.cursor().execute(
'''
SELECT
time,
title,
author,
content,
comments
FROM posts
WHERE time=?
;''',
(postnum,)
).fetchone()
return {
'post':{
'time':post[0],
'title':post[1],
'author':post[2],
'contents':post[3]
},
'comments':json.loads(post[4]),
}
def comment(contents):
posttime = time.time()
commentid=contents['data']['comment']
post = contents['data']['post']
comments = json.loads(connection.cursor().execute(
'''
SELECT (comments) FROM posts
WHERE time=?
;''',(
post,
)
).fetchone()[0])#TODO: prevent race condition, someone else could comment in this time
def findComment(tree,commentid):
if tree['time']==commentid:
return tree
else:
for comment in tree['comments']:
status = findComment(comment,commentid)
if status:
return status
return False
comment = findComment(
{'time':contents['data']['post'],'comments':comments},
commentid
)
comment['comments'].append(
{
'time':posttime,
'author':contents['username'],
'content':contents['data']['content'],
'comments':[]
})
connection.cursor().execute(
'''
UPDATE posts
SET comments = ?
WHERE time=?
''',(
json.dumps(comments),
post
)
)
connection.commit()
return {'message':'Commented!'}
def checkPath(path):
if '..' in path: return {'status':0,'excuse':'You cant have a \'..\' in a filename, got it?'}
return False
def checkFilename(name):
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'
for char in name:
if char not in alphabet:
return False
return True
def files(contents):
subpath = '/'.join(contents['data']['path'])
path = folder()+'user/'+contents['username']+'/files/'+ subpath
if checkPath(path):return checkPath(path)
if os.path.isdir(path):
filetype = 'directory'
return {'type':'directory','files':os.listdir(path),'path':subpath}
else:
actuafile = open(path,'r')
content = actuafile.read()
actuafile.close()
return{'type':'file','content':content,'path':subpath}
def addFile(contents):
path = folder()+'user/'+contents['username']+'/files/'+ '/'.join(contents['data']['path'])
if checkPath(path):return checkPath(path)
if not checkFilename(path.split('/')[-1]): return {'status':1,'excuse':'bad name'}
if contents['data']['type'] == 'file':
open(path,'w').close()
else:
os.mkdir(path)
return {'status':'0'}
def removeFile(contents):
path = folder()+'user/'+contents['username']+'/files/'+ '/'.join(contents['data']['path'])
if checkPath(path):return checkPath(path)
if os.path.isdir(path):
os.rmdir(path)
else:
os.remove(path)
return {'status':'0'}
def renameFile(contents):
path = folder()+'user/'+contents['username']+'/files/'+ '/'.join(contents['data']['path'])
if checkPath(path):return checkPath(path)
if not checkFilename(contents['data']['newname']): return {'status':1,'excuse':'bad name', 'path':path}
newpath = '/'.join(path.split('/')[:-1]+ [contents['data']['newname']])
if checkPath(newpath):return checkPath(newpath)
os.rename(path,newpath)
return {'path':'/'.join(contents['data']['path'][:-1]+ [contents['data']['newname']])}
def editFile(contents):
path = folder()+'user/'+contents['username']+'/files/'+ '/'.join(contents['data']['path'])
if checkPath(path):return checkPath(path)
data = contents['data']['data']
f=open(path,'w')
f.write(data)
f.close()
return {'status':'0'}
def messagers(contents):
def otherUser(pair, user):
return pair[ (pair.index(user)+1) % 2 ]
username = contents['username']
return {'messagers':[
otherUser(userPair,username)
for userPair in connection.cursor().execute(
'''
SELECT user1, user2
FROM messagers
WHERE user1=? or user2=?
;''',
(username,)*2
).fetchall()
]}
def messager(contents):
targetName = contents['data']['name']
ownname = contents['username']
owndir = folder()+'user/'+ownname+'/messages/'
targetDir = folder()+'user/'+targetName+'/messages/'
connection.cursor().execute(
'''
INSERT INTO messagers(user1,user2)
VALUES (?,?)
''',
(min(targetName,ownname),max(targetName,ownname))
)
connection.commit()
#if targetName in os.listdir(owndir):
# return {'status':1,'excuse':'already there, dummy'}
return {'status':'0'}
def messages(contents):
start = contents['data']['end']
if start == None: start=1e100
username = contents['username']
target = contents['data']['user']
#length = prefs(contents['username'])['messagenum']
return {'messages': [
{
'time':message[0],
'author':message[1],
'content':message[2]
}
for message in connection.cursor().execute(
'''
SELECT time, author, content
FROM messages
JOIN messagers
ON messages.usersid=messagers.id
WHERE time<?
AND user1=?
AND user2=?
ORDER BY time DESC
LIMIT 8
;''',
(
start,
min(username,target),
max(username,target)
)
).fetchall()
]}
def updateMessages(contents):
end = contents['data']['start']
username = contents['username']
target = contents['data']['user']
return {'messages': [
{
'time':message[0],
'author':message[1],
'content':message[2]
}
for message in connection.cursor().execute(
'''
SELECT time, author, content
FROM messages
JOIN messagers
ON messages.usersid=messagers.id
WHERE time>?
AND user1=?
AND user2=?
ORDER BY time DESC
LIMIT 8
;''',
(
end,
min(username,target),
max(username,target)
)
).fetchall()
]}
def message(contents):
target = contents['data']['user']
ownname = contents['username']
content = contents['data']['content']
sendtime = time.time()
connection.cursor().execute(
'''
INSERT INTO messages
SELECT id, ?, ?, ?
FROM messagers
WHERE user1=? and user2=?
;''',
(
sendtime,
ownname,
content,
min(ownname,target),
max(ownname,target)
)
)
connection.commit()
def preferences(contents):
return {'prefs':prefs(contents['username'])}
def preference(contents):
connection.cursor().executemany('''
UPDATE preferences
SET value = ?
WHERE username = ? AND preference = ?;
''',[
(value, contents['username'], preference)
for preference, value in contents['data'].items()
]);
connection.commit()
return {'status':'0'}
def cat(contents):
to = contents['data']['to']
catfile = open(folder()+'cats/clients','a')
catfile.write(to+'\n')
return {'status':'0'}