forked from dequis/wakarimasen
-
Notifications
You must be signed in to change notification settings - Fork 2
/
misc.py
399 lines (325 loc) · 12.4 KB
/
misc.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# misc.py: Temporary place for new functions
import os
import re
import time
import crypt
import struct
from subprocess import Popen, PIPE
import util
import crypto # part of wakarimasen
import config, config_defaults
import str_format
import urllib
from util import local
def dot_to_dec(ip):
try:
parts = [int(x) for x in ip.split(".")]
return struct.unpack('>L', struct.pack('>4B', *parts))[0]
except ValueError:
return ip
def dec_to_dot(numip):
try:
# Parse 64-bit integer for compatibility with database written
# with Perl's 'N' packing format.
parts = struct.unpack('>8B', struct.pack('>Q', long(numip)))
return '.'.join([str(x) for x in parts[4:8]])
except ValueError:
return numip
def validate_ip(ip):
try:
if isinstance(dot_to_dec(ip), int):
return True
except:
pass
return False
def is_whitelisted(numip):
return False
TRIP_RE = '^(.*?)((?<!&)#|%s)(.*)$'
SECURE_TRIP_RE = '(?:%s)(?<!&#)(?:%s)*(.*)$'
SALT_CLEAN_RE = re.compile('[^\.-z]')
def process_tripcode(name, tripkey='!'):
match = re.match(TRIP_RE % re.escape(tripkey), name)
if not match:
return (str_format.clean_string(str_format.decode_string(name)), '')
trip = ''
namepart, marker, trippart = match.groups()
namepart = str_format.clean_string(str_format.decode_string(namepart))
# do we want secure trips, and is there one?
if config.SECRET:
regexp = re.compile(SECURE_TRIP_RE.replace("%s", re.escape(marker)))
smatch = regexp.match(trippart)
if smatch:
trippart = regexp.sub('', trippart)
maxlen = 255 - len(config.SECRET)
string = smatch.group(1)[:maxlen]
trip = tripkey * 2 + hide_data(smatch.group(1), 6, "trip",
config.SECRET, True)
if not trippart: # return directly if there's no normal tripcode
return (namepart, trip)
# 2ch trips are processed as Shift_JIS whenever possible
trippart = trippart.encode("shiftjis", "xmlcharrefreplace")
trippar = str_format.clean_string(trippart)
salt = (trippart + "H..")[1:3]
salt = SALT_CLEAN_RE.sub('.', salt)
for old, new in map(None, ':;<=>?@[\\]^_`', 'ABCDEFGabcdef'):
salt = salt.replace(old, new)
trip = tripkey + crypt.crypt(trippart, salt)[-10:] + trip
return (namepart, trip)
def make_key(key, secret, length):
return crypto.rc4('\0' * length, key + secret)
def hide_data(data, bytes, key, secret, base64=False):
ret = crypto.rc4('\0' * bytes, make_key(key, secret, 32) + str(data))
if base64:
return ret.encode("base64").rstrip('\n')
return ret
def hide_critical_data(string, key):
rc6 = crypto.RC6(key)
ret = ''
i = 0
while i < len(string):
ret += rc6.encrypt(string[i:i+15]).encode("base64")[:-1]
i += 15
return ret
def compile_spam_checker(spam_files):
# TODO caching this by timestamps would be nice
regexps = []
for file in spam_files:
for line in open(file).readlines():
line = re.sub("(^|\s+)#.*", "", line).strip()
if not line:
continue
match = re.match("^/(.*)/([xism]*)$", line)
if match:
pattern, modifiers = match.groups()
flags = sum([getattr(re, x.upper()) for x in modifiers])
else:
pattern = re.escape(line)
flags = re.I
regexps.append(re.compile(pattern, flags))
def spam_checker(string):
for regexp in regexps:
if regexp.search(string) is not None:
return True
return False
return spam_checker
def spam_engine(trap_fields, spam_files):
def spam_screen():
raise util.SpamError()
request = local.request
for field in trap_fields:
if request.values.get('request', None) is not None:
spam_screen()
spam_checker = compile_spam_checker(spam_files)
fields = request.values.keys()
fulltext = '\n'.join([str_format.decode_string(request.values[x])
for x in fields])
if spam_checker(fulltext):
spam_screen()
def is_trusted(trip):
# needed only when captcha is enabled?
pass
def check_captcha(*args):
# broken in wakaba+desuchan?
pass
def make_date(timestamp, style='futaba'):
'''Generate a date string from a passed timestamp based on a requested
style. The string formatting power of the time module's strftime()
method is used here. The optional locale array from Wakaba is dropped
in favor of using the local day and month abbreviations provided by the
module. The format used can also be inputted directly into the style
parameter.'''
localtime = time.localtime(timestamp + config.TIME_OFFSET)
gmt = time.gmtime(timestamp)
time_str = ''
if style.lower() == '2ch':
time_str = time.strftime('%Y-%m-%d %H:%M', localtime)
elif style.lower() == '2ch-gmt':
time_str = time.strftime('%Y-%m-%d %H:%M GMT', gmt)
elif style.lower() == 'futaba' or style == 0:
time_str = time.strftime('%y/%m/%d(%a)%H:%M', localtime)
elif style.lower() == 'futaba-gmt':
time_str = time.strftime('%y/%m/%d(%a)%H:%M GMT', gmt)
elif style.lower() == 'tiny':
time_str = time.strftime('%m/%d %H:%M', localtime)
elif style.lower() == 'cookie':
time_str = time.strftime('%a, %d-%b-%Y %H:%M:%S GMT', gmt)
elif style.lower() == 'http':
time_str = time.strftime('%a, %d %b %Y %H:%M:%S GMT', gmt)
elif style.lower() == 'month':
time_str = time.strftime('%b %Y', gmt)
elif style.lower() == 'us-en':
time_str = time.strftime('%A, %B %d, %Y @ %h:%M %p', localtime)
elif style.lower() == 'uk-en':
time_str = time.strftime('%A, %d %B %Y @ %h:%M %p', localtime)
elif style.lower() == 'ctime' or style.lower() == 'c':
time_str = time.asctime(localtime)
elif style.lower() == 'localtime':
time_str = str(timestamp)
elif style.lower() == '2ch-sep93': # Damn AOLers! Get offa mah lawn!
# September 1, 1993 as a timestamp.
SEP_1_1993 = 746884800L
seconds_past = long(timestamp) - SEP_1_1993
days_past = seconds_past / 86400L
time_str = '1993-09-%u' % (days_past + 1)
time_str = ' '.join([time_str, time.strftime('%H:%M', localtime)])
else:
# Let the style parameter default to a format string.
time_str = time.strftime(style, timestamp)
return time_str
def make_cookies(**kwargs):
expires = kwargs.pop('expires', time.time() + 14 * 24 * 3600)
path = kwargs.pop('path', '/')
httponly = kwargs.pop('httponly', False)
expire_date = make_date(expires, "cookie")
environ = local.environ
cookies = environ['waka.cookies']
for key, value in kwargs.iteritems():
cookies[key] = urllib.quote(value.encode('unicode-escape'))
cookies[key]['expires'] = expire_date
cookies[key]['path'] = path
if httponly:
cookies[key]['httponly'] = True
def get_script_name():
return local.environ['SCRIPT_NAME']
def get_secure_script_name():
script_name = get_script_name()
if config.USE_SECURE_ADMIN:
return 'https://' + local.environ['SERVER_NAME'] + script_name
return script_name
def make_script_url(**kwargs):
'''Builds an url pointing to the cgi script
Optional params:
- _secure: set to False to avoid enforcing https
- _amp: set to True to html escape ampersands
Everything else is included as url parameters'''
secure = kwargs.pop('_secure', True)
amp = kwargs.pop('_amp', False)
base = get_secure_script_name() if secure else get_script_name()
url = '%s?%s' % (base, urllib.urlencode(kwargs))
if amp:
url = url.replace('&', '&')
return url
def get_filestorage_size(filestorage):
filestorage.stream.seek(0, 2)
size = filestorage.stream.tell()
filestorage.stream.seek(0, 0)
return size
def analyze_image(file, name):
types = [("jpg", analyze_jpeg), ("png", analyze_png), ("gif", analyze_gif)]
for ext, analyze in types:
res = analyze(file)
if res:
return (ext, res[0], res[1])
# find file extension for unknown files
ext = ''
if name.find(".") != -1:
ext = name.split(".")[-1].lower()
return (ext, 0, 0)
def analyze_jpeg(file):
# TODO: requires testing
try:
buffer = file.read(2)
if buffer != '\xff\xd8':
return
while True:
while True:
buffer = file.read(1)
if not buffer:
return
if buffer == '\xff':
break
mark, size = struct.unpack(">BH", file.read(3))
if mark in (0xda, 0xd9): # SOS/EOI
break
if size < 2:
# MS GDI+ JPEG exploit uses short chunks
raise util.WakaError("Possible virus in image")
if mark >= 0xc0 and mark <= 0xc2: # SOF0..SOF2 - what the hell are the rest?
bits, height, width = struct.unpack(">BHH", file.read(5))
return (width, height)
file.seek(size - 2, 1)
except struct.error:
return
finally:
file.seek(0)
PNG_MAGIC = '\x89PNG\r\n\x1a\n'
PNG_IHDR = 'IHDR'
def analyze_png(file):
buffer = file.read(24)
file.seek(0)
if len(buffer) != 24:
return
magic, length, ihdr, width, height = struct.unpack(">8sL4sLL", buffer)
if magic != PNG_MAGIC and ihdr != PNG_IHDR:
return
return (width, height)
GIF_MAGICS = ('GIF87a', 'GIF89a')
def analyze_gif(file):
buffer = file.read(10)
file.seek(0)
if len(buffer) != 10:
return
magic, width, height = struct.unpack('<6sHH', buffer)
if magic not in GIF_MAGICS:
return
return (width, height)
def make_thumbnail(filename, thumbnail, width, height, quality, convert):
is_animated = False
magickname = filename
convert = convert or 'convert' # lol
popen_array = [convert, '-resize', '%sx%s!' % (width, height),
'-quality', str(quality)]
if magickname.endswith(".gif"):
identify = config.IDENTIFY_COMMAND
gif_check = Popen([identify, '-format', '%n', magickname],
stdout=PIPE).communicate()[0]
try:
if int(gif_check) > 1:
magickname += '[0]'
is_animated = True
except ValueError:
pass
if is_animated:
popen_array.extend([magickname, '-background', config.BG_ANIM_COLOR,
'-gravity', 'Center',
'-fill', config.FG_ANIM_COLOR,
'-size', '100x15',
'label:Animated', '-append', thumbnail])
height += 15
else:
popen_array.extend([magickname, thumbnail])
process = Popen(popen_array)
if process.wait() == 0 and os.path.exists(thumbnail) and \
os.path.getsize(thumbnail) != 0:
return width, height
elif os.path.exists(thumbnail):
os.unlink(thumbnail)
return 0, 0
# other methods supported by the original wakaba
# but not by wakaba+desuchan aren't included here
# because they suck.
def get_cookie_from_request(request, key):
return urllib.unquote(request.cookies.get(key, '')).decode('unicode-escape')
def kwargs_from_params(request, params_arg=None, **params):
'''Associate function to convert CGI request data with dictionary
of parameter keys to a dictionary of keyword arguments for passing
to an application function.
Dictonary format: {'cookies': ['cookie_keys'],
'form': ['html_form_input_names'],
'file': ['file_keys']}
Not all keys are necessary. Invalid keys are ignored.'''
if params_arg:
params = params_arg
kwargs = {}
if 'admin' in params.keys():
kwargs['cookie'] = get_cookie_from_request(request, 'wakaadmin')
if 'cookies' in params.keys():
for param in params['cookies']:
kwargs[param] = get_cookie_from_request(request, param)
if 'form' in params.keys():
for param in params['form']:
kwargs[param] = request.values.get(param, '')
if 'file' in params.keys():
for param in params['file']:
kwargs[param] = request.files.get(param, None)
return kwargs