-
Notifications
You must be signed in to change notification settings - Fork 10
/
paste.py
427 lines (377 loc) · 13.9 KB
/
paste.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tornado.ioloop
import tornado.web
from tornado.httpserver import HTTPServer
from tornado.netutil import bind_unix_socket
from tornado.escape import xhtml_escape as escape
from tornado.options import options, define, parse_command_line
from pygments import highlight
from pygments.lexers import get_lexer_by_name, get_lexer_for_filename, get_all_lexers
from pygments.formatters import HtmlFormatter
from pygments.util import ClassNotFound
from os import mkdir, listdir
from os.path import isfile, dirname, basename, exists
from pickle import dump, load
from random import choice
from re import match
from string import ascii_letters, digits
from subprocess import Popen, PIPE
from sys import argv
import codecs
### Options
title = 'Paste it §'
doc = '(<a href="https://github.com/acieroid/paste-py/">doc</a>|<a href="https://raw.github.com/acieroid/paste-py/master/paste.sh">script</a>)'
filename_path = 'pastes'
filename_length = 4
filename_characters = ascii_letters + digits
mldown_path = '' # if '', disable the mldown option
mldown_args = []
linenos_type = 'table' # 'table', 'inline' or '' (table is copy-paste friendly)
base_url = '/'
production = False
spam = ["buy ", "phentermine", "pill", "cialis", "carisoprodol", "soma ",
" soma", "carisoprodol", " mg", "30mg", "adipex", "business",
"loan", "credit", "cash ", " cash", "viagra", "phentramine",
"advance", "pay day", "payday", "weltmine" "metermine", "tramadol",
"ultram", "duromine", "side effects", "prescription", "vanadom",
"stomach", "tablets", "tadalafil", "drug", "tamoxifen", "synthroid",
"vardenafil", "tadalafil", "vitamin", "accutane", "adderral",
"strattera", "acyclovir", "xanax", "loan"]
def valid_username(username):
legal = not (('/' in username) or (' ' in username))
if not legal:
return False
for kw in spam:
if username.lower().find(kw) != -1:
return False
return True
### Highlight & format
def highlight_code(code, lang, linenos=''):
if lang == '':
lang = 'text'
res = highlight(code, get_lexer_by_name(lang),
HtmlFormatter(linenos=linenos))
return res
def list_languages():
return sorted (
((x[0], x[1][0]) for x in get_all_lexers() if x[1] != ()),
key = lambda x: x[1].lower()
)
def lang_from_ext(ext):
try:
return get_lexer_for_filename('.' + ext).aliases[0]
except (ClassNotFound, IndexError):
return ''
def format_mldown(code):
if mldown_path == '' or not isfile(mldown_path):
return ('mldown disabled, source output<br/><pre>' + code + '</pre>')
pipe = Popen([mldown_path] + mldown_args, stdin=PIPE, stdout=PIPE)
content = pipe.communicate(code)[0]
if pipe.returncode == 0:
return content
else:
return ('mldown failed, source output<br/><pre>' + code + '</pre>')
### Paste form
def checkbox(name, label, checked=False, value='on'):
res = '<label><input type="checkbox" name="' + name + '" value="' + value + '" '
if checked:
res += 'checked="checked"'
res += '/>' + label + '</label>\n'
return res
def option_boxes():
checkboxes = ''
if mldown_path != '':
checkboxes += checkbox('mldown', 'Format with <a href="http://gitorious.org/mldown">mldown</a>')
checkboxes += checkbox('ln', 'Line numbers')
checkboxes += checkbox('raw', 'Raw paste')
checkboxes += '<br/>'
return checkboxes
def language_box():
res = '<select name="hl">'
res += '<option value="">No highlighting</option>'
for (language, lang) in list_languages():
res += '<option value="' + lang + '">' + language + '</option>\n'
res += '</select><br/>'
return res
def name_field():
res = '<label for="user">User (optional):</label><br/>'
res += '<input type="text" name="user" id="user"/><br/>'
return res
def comment_field():
res = '<label for="comment">Comment (optional):</label><br/>'
res += '<input type="text" name="comment" id="comment"/>'
return res
def paste_form():
res = ''
res += '<form method="post" action="/" enctype="multipart/form-data">'
res += '<input type="text" name="content" style="display: none;">'
res += '<textarea name="paste" rows="20" cols="80"></textarea><br/>'
res += language_box()
res += option_boxes()
res += name_field()
res += comment_field()
res += '<input type="submit" value="Paste"/>'
res += '</form>'
return res
### Access to disk (read & write paste)
def user_dir(user):
return '%s/%s' % (filename_path, user)
def random_filename():
res = ''
for i in xrange(filename_length):
res += choice(filename_characters)
return res
def new_path(user):
path = ''
if user is not None:
path = user_dir(user)
else:
path = filename_path
def fn():
return '%s/%s' % (path, random_filename())
filename = fn()
while isfile(filename):
filename = fn()
if user is not None:
if not exists(path):
mkdir(path)
return filename
def dump_paste(content, user):
filename = new_path(user)
with codecs.open(filename, 'w', 'utf-8') as f:
f.write(content)
return filename
## Users
def read_paste(filename):
try:
with codecs.open(filename, 'r', 'utf-8') as f:
return f.read()
except IOError:
raise tornado.web.HTTPError(404)
def pastes_for_user(user):
pastes = []
try:
for filename in listdir(user_dir(user)):
if (not match(r'.*\.meta', filename) and
isfile(user_dir(user) + '/' + filename)):
pastes.append(filename)
except OSError:
pass
return pastes
## Meta informations
def meta_dir(user, paste):
if user:
return ('%s/%s.meta' % (user_dir(user), paste))
else:
return ('%s/%s.meta' % (filename_path, paste))
def dump_meta(user, paste, meta):
filename = meta_dir(user, paste)
with open(filename, 'w') as f:
dump(meta, f)
def read_meta(user, paste):
filename = meta_dir(user, paste)
if not isfile(filename):
return {}
with open(filename, 'r') as f:
return load(f)
## Logic
html_pre = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>''' + title + '''</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="/paste.css" title="Clear"/>
<link rel="stylesheet" type="text/css" href="/paste-margin.css" title="Clear with margin"/>
<link rel="alternate stylesheet" type="text/css" href="/light-center.css" title="Light center design with a bit of css"/>
<link rel="alternate stylesheet" type="text/css" href="/dark.css" title="Dark"/>
<link rel="alternate stylesheet" type="text/css" href="/dark2.css" title="Alternative dark"/>
</head>
<body>'''
html_post = '</body></html>'
html_invalid_user = html_pre + '''
<h1>Invalid Username</h1>
<p>The username you supplied is invalid or considered as spam.<br/>
Please try another username or file a <a href="https://github.com/acieroid/paste-py/issues">bug report</a> if you think your username is not spammy and should be allowed.</p>
<p>Go <a href="''' + base_url + '''">back</a>.</p>
''' + html_post
html_spam = html_pre + '''
<h1>Spam Detected</h1>
<p>You probably are spam, so we'll just ignore you<br/>
If you think that's an error, file a <a href="https://github.com/acieroid/paste-py/issues">bug report</a>.</p>
<p>Go <a href="''' + base_url + '''">back</a>.</p>
''' + html_post
def extract_args(uri):
content = map(lambda s: s.split('='), uri.split('&')[1:])
content = map(lambda v: len(v) != 2 and [v[0], ''] or v, content)
return dict(content)
def view_paste(paste, args, handler):
pre = html_pre
post = ''
paste_content = read_paste(filename_path + '/' + paste)
meta = read_meta(None, paste)
if 'raw' in args:
handler.set_header('Content-Type', 'text/plain; charset=utf-8')
handler.write(paste_content)
return
elif 'mldown' in args:
handler.write(format_mldown(paste_content))
return
elif 'hl' in args or 'hl' in meta:
try:
hl = ('hl' in args and args['hl']) or meta['hl']
if 'ln' in args:
paste_content = highlight_code(paste_content, hl,
linenos_type)
else:
paste_content = highlight_code(paste_content, hl)
except ClassNotFound as e:
paste_content = escape(paste_content)
pre += '<pre>'
post += '</pre>'
else:
if 'ln' in args:
paste_content = highlight_code(paste_content,
'text', linenos_type)
else:
paste_content = escape(paste_content)
pre += '<pre>'
post += '</pre>'
post += html_post
handler.content_type = 'text/html'
handler.write(pre)
handler.write(paste_content)
handler.write(post)
def add_paste(user, content, comment, args, handler):
if not valid_username(user):
handler.write(html_invalid_user)
return
paste = basename(dump_paste(content, user))
options = paste
meta = {'hl': '', 'comment': escape(comment)}
if user:
options = '%s/%s' % (user, options)
if 'raw' in args:
options = 'raw/' + options
else:
if 'ln' in args:
options += '&ln'
if 'mldown' in args:
options += '&mldown'
elif 'hl' in args:
hl = args['hl']
meta['hl'] = hl
elif 'ext' in args:
hl = lang_from_ext(args['ext'])
if hl != '':
# options += '&hl=' + hl
meta['hl'] = hl
dump_meta(user, paste, meta)
if 'script' in args:
handler.set_header('Content-Type', 'text/plain; charset=utf-8')
handler.write(options)
return
handler.redirect(base_url + options);
def view_index(handler):
body = paste_form()
handler.content_type = 'text/html'
handler.write(html_pre)
handler.write('<h1>%s <span style="font-size: 12px">%s</span></h1>' %
(title, doc))
handler.write(body)
handler.write(html_post)
def view_user(user, handler):
user = escape(user)
if not valid_username(user):
raise tornado.web.HTTPError(404)
pastes = pastes_for_user(user)
body = '<h2>Pastes for %s</h2>' % user
if pastes == []:
body += '<p>No paste for this user</p>'
else:
body += '<ul>'
for paste in pastes:
meta = read_meta(user, paste)
body += ('<li><a href="%s%s/%s">%s</a>' %
(base_url, user, paste, paste))
if 'comment' in meta and meta['comment'] != '':
body += ': %s' % meta['comment']
body += '</li>'
body += '</ul>'
handler.content_type = 'text/html'
handler.write(html_pre)
handler.write(body)
handler.write(html_post)
### App
class MainHandler(tornado.web.RequestHandler):
def get(self):
args = extract_args(self.request.uri)
if self.get_argument('id', False):
view_paste(self.get_argument('id'), args, self)
elif self.get_argument('paste', False):
args = {k: v[0]
for k, v in self.request.arguments.items()}
# the field 'content' should be empty, unless filled by bots
if self.get_argument('content', '') != '':
self.set_status(404)
self.write(html_spam)
return
add_paste(self.get_argument('user', ''),
self.get_argument('paste', strip=False),
self.get_argument('comment', ''),
args, self)
elif self.get_argument('user', False):
view_user(self.get_argument('user', ''), self)
else:
view_index(self)
def post(self):
self.get()
class ViewHandler(tornado.web.RequestHandler):
def get(self, name, args, last):
view_paste(name, extract_args(args), self)
class UserViewHandler(tornado.web.RequestHandler):
def get(self, user, name, args, last):
paste = user + '/' + name
view_paste(paste, extract_args(args, self))
class UserHandler(tornado.web.RequestHandler):
def get(self, user):
view_user(user, self)
class RawHandler(tornado.web.RequestHandler):
def get(self, name):
view_paste(name, {'raw': ''}, self)
class UserRawHandler(tornado.web.RequestHandler):
def get(self, user, name):
view_paste(name, {'raw': '', 'user': user}, self)
application = tornado.web.Application([
(r"/", MainHandler),
(r"/user/([^/]+)/?", UserHandler),
(r"/raw/([^&]+)", RawHandler),
(r"/raw/([^/]+)/([^&]+)", UserRawHandler),
(r"/([a-zA-Z0-9\-]+\.css)", tornado.web.StaticFileHandler,
dict(path=dirname(__file__))),
# Those two routes should stay in last position to avoid conflicts
# with the other routes
(r"/([^&]+)((&[^&]+)*)", ViewHandler),
(r"/([^/]+)/([^&]+)((&[^&]+)*)", UserViewHandler),
])
define('addr', group='webserver', default='127.0.0.1',
help='Address on which to listen')
define('port', group='webserver', default=8888,
help='Port on which to listen')
define('socket', group='webserver', default=None,
help='Unix socket file on which to listen')
def run():
parse_command_line()
if options.socket:
print('Listening on {}'.format(options.socket))
server = HTTPServer(application)
socket = bind_unix_socket(options.socket, mode=0o777)
server.add_socket(socket)
else:
print('Listening on {}:{}'.format(options.addr, options.port))
application.listen(options.port, address=options.addr)
application.debug = not production
tornado.ioloop.IOLoop.instance().start()
if __name__ == '__main__':
run()