-
Notifications
You must be signed in to change notification settings - Fork 7
/
wakapost.py
322 lines (264 loc) · 10.5 KB
/
wakapost.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
import re
import misc
import strings
import str_format
from util import WakaError, local
class ValidationError(WakaError):
pass
class WakaPost(object):
'''A post object that uses __slots__ for no good reason'''
__slots__ = [
# columns copied directly from model.board
'num', 'parent', 'timestamp', 'lasthit', 'ip', 'date', 'name', 'trip',
'email', 'subject', 'password', 'comment', 'size', 'md5',
'width', 'height', 'thumbnail', 'tn_width', 'tn_height', 'lastedit',
'lastedit_ip', '_admin_post', 'stickied', 'locked',
# extensions
'abbrev', 'nofile', 'req_file', 'filename', 'req_no_format',
'killtrip', 'postfix', 'ninja'
]
def __init__(self, rowproxy=None, **kwargs):
self.date = ''
self.filename = ''
self.thumbnail = ''
self.md5 = ''
self.comment = ''
self.name = ''
self.email = ''
self.subject = ''
self.trip = ''
self.password = ''
self.lastedit = ''
self.postfix = ''
self.lasthit = 0
self.ip = 0
self.lastedit_ip = 0
self.num = 0
self.size = 0
self.width = 0
self.height = 0
self.tn_width = 0
self.tn_height = 0
self.parent = 0
self.timestamp = 0
self.abbrev = 0
# tri-state: True, False, or None for unset
self.stickied = None
self.locked = None
self.nofile = None
self.req_no_format = None
self.ninja = None
self.killtrip = None
self.req_file = None
self.admin_post = False
if rowproxy:
self.update(items=rowproxy.items())
else:
self.update(**kwargs)
def __repr__(self):
parent = ''
if self.parent:
parent = ' in thread %s' % self.parent
return '<Post >>%s%s>' % (self.num, parent)
@property
def noko(self):
return ((self.subject.lower() == 'noko') or
(self.email.lower() == 'noko'))
@property
def db_values(self):
'''Return a kwargs dict of values to set in the db'''
return dict(
parent=self.parent,
timestamp=self.timestamp,
ip=self.ip,
date=self.date,
name=self.name,
trip=self.trip,
email=self.email,
subject=self.subject,
password=self.password,
comment=self.comment,
image=self.filename,
size=self.size,
md5=self.md5,
width=self.width,
height=self.height,
thumbnail=self.thumbnail,
tn_width=self.tn_width,
tn_height=self.tn_height,
admin_post=self.admin_post,
stickied=self.stickied,
locked=self.locked,
lastedit_ip=self.lastedit_ip,
lasthit=self.lasthit,
lastedit=self.lastedit)
@classmethod
def from_request(cls, request):
'''Creates a Post object based on a request
This function does not do ANY kind of validation!'''
self = cls()
# direct fields - assigned to attributes
# same name in the input as the database
for key in ['num', 'parent', 'email', 'subject', 'comment',
'password', 'killtrip', 'postfix', 'ninja', 'nofile']:
setattr(self, key, request.values.get(key, ''))
# modify these a bit here
self.name = request.values.get('field1', '')
self.admin_post = (request.values.get('adminpost', '0') == '1' or
request.values.get('adminedit', '0') == '1')
self.stickied = request.values.get('sticky', '0') == '1'
self.locked = request.values.get('lock', '0') == '1'
self.req_no_format = request.values.get('no_format', '0') == '1'
self.req_file = request.files.get('file', None)
return self
@classmethod
def copy(cls, instance):
'''Copies all the attributes of another instance into a new one'''
return cls().merge(instance)
def merge(self, other, which='all'):
'''Merges only the attributes relevant to edited posts
which: either 'all', 'request', or a list of keys'''
keys = []
if which == 'all':
keys = WakaPost.__slots__
elif which == 'request':
keys = ['num', 'parent', 'email', 'subject', 'comment',
'password', 'killtrip', 'postfix', 'ninja', 'nofile',
'name', 'admin_post', 'stickied', 'locked',
'req_no_format', 'req_file']
elif type(which) == list:
keys = which
for key in keys:
setattr(self, key, getattr(other, key, ''))
return self
def update(self, items=None, **kwargs):
for key, value in (items or kwargs.iteritems()):
setattr(self, key, value)
@property
def admin_post(self):
return self._admin_post
@admin_post.setter
def admin_post(self, value):
# TODO: database migration / unfucking
self._admin_post = (value in (True, 1, 'yes', 'True', '1'))
@property
def image(self):
return self.filename
@image.setter
def image(self, value):
self.filename = value
def set_ip(self, numip, editing=None):
'''Sets the ip or the lastedit ip'''
if editing:
self.lastedit_ip = numip
else:
self.ip = numip
def set_date(self, editing, date_style):
'''Sets the date or lastedit according to the timestamp'''
if not editing:
self.date = misc.make_date(self.timestamp, date_style)
else:
self.date = editing.date
if not self.ninja:
self.lastedit = misc.make_date(self.timestamp, date_style)
def set_tripcode(self, tripkey):
'''Splits the name by the tripcode'''
self.name, temp = misc.process_tripcode(self.name, tripkey)
self.trip = self.trip or temp
def validate(self, editing, admin_mode, options):
'''Validates the post contents, raises ValidationError'''
if not admin_mode:
if self.req_no_format or \
(self.stickied and not self.parent) or self.locked:
# the user is not allowed to do this
raise ValidationError(strings.NOTALLOWED)
file_ = self.req_file
if self.parent:
if file_ and not options['ALLOW_IMAGE_REPLIES']:
raise ValidationError(strings.NOTALLOWED)
if not file_ and not options['ALLOW_TEXT_REPLIES']:
raise ValidationError(strings.NOTALLOWED)
else:
if file_ and not options['ALLOW_IMAGES']:
raise ValidationError(strings.NOTALLOWED)
if not file_ and self.nofile and options['ALLOW_TEXTONLY']:
raise ValidationError(strings.NOTALLOWED)
try:
if len(self.parent) == 0:
self.parent = 0
elif len(self.parent) > 10:
raise ValueError
else:
self.parent = int(self.parent)
except ValueError:
raise ValidationError(strings.UNUSUAL)
# Check for weird characters
has_crlf = lambda x: '\n' in x or '\r' in x
if [True for x in (self.name, self.email, self.subject)
if has_crlf(x)]:
raise ValidationError(strings.UNUSUAL)
# Check for excessive amounts of text
if (len(self.name) > options['MAX_FIELD_LENGTH'] or
len(self.email) > options['MAX_FIELD_LENGTH'] or
len(self.subject) > options['MAX_FIELD_LENGTH'] or
len(self.comment) > options['MAX_COMMENT_LENGTH']):
raise ValidationError(strings.TOOLONG)
# check to make sure the user selected a file, or clicked the checkbox
if (not editing and not self.parent and
not self.req_file and not self.nofile):
raise ValidationError(strings.NOPIC)
# check for empty reply or empty text-only post
if not self.comment.strip() and not self.req_file:
raise ValidationError(strings.NOTEXT)
# get file size, and check for limitations.
if self.req_file:
self.size = misc.get_filestorage_size(self.req_file)
if self.size > (options['MAX_KB'] * 1024):
raise ValidationError(strings.TOOBIG)
if self.size == 0:
raise ValidationError(strings.TOOBIGORNONE)
def clean_fields(self, editing, admin_mode, options):
'''Modifies fields to clean them'''
# kill the name if anonymous posting is being enforced
if options['FORCED_ANON']:
self.name = ''
self.trip = ''
if self.email.lower() == 'sage':
self.email = 'sage'
else:
self.email = ''
# fix up the email/link, if it is not a generic URI already.
if self.email and not re.search(r"(?:^\w+:)|(?:\:\/\/)", self.email):
self.email = "mailto:" + self.email
# clean up the inputs
self.subject = str_format.clean_string(
str_format.decode_string(self.subject))
# format comment
if not self.req_no_format:
self.comment = str_format.format_comment(str_format.clean_string(
str_format.decode_string(self.comment)))
# insert default values for empty fields
if not (self.name or self.trip):
self.name = options['S_ANONAME']
self.subject = self.subject or options['S_ANOTITLE']
self.comment = self.comment or options['S_ANOTEXT']
def process_file(self, board, editing):
'''Wrapper around board.process_file to use wakapost'''
self.filename, self.md5, self.width, self.height, \
self.thumbnail, self.tn_width, self.tn_height = \
board.process_file(self.req_file, self.timestamp,
self.parent, editing is not None)
def make_post_cookies(self, options, url):
'''Sets the name, email and password cookies'''
c_name = self.name
c_email = self.email
c_password = self.password
autopath = options['COOKIE_PATH']
if autopath == 'current':
path = url
elif autopath == 'parent':
path = local.environ['waka.rootpath']
else:
path = '/'
misc.make_cookies(name=c_name, email=c_email, password=c_password,
path=path) # yum !