forked from argomaintainer/argon
/
mail.py
450 lines (391 loc) · 14.1 KB
/
mail.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/bin/python2
# -*- coding: utf-8 -*-
import sys
sys.path.append('../')
import logging
logger = logging.getLogger('@mail')
from chaofeng import ascii as ac
from chaofeng.g import mark
from edit import BaseEditFrame, handler_edit
from chaofeng.ui import NullValueError, FinitePagedTable, TableLoadNoDataError
from libframe import BaseAuthedFrame, gen_quote_mail
from view import BaseTextBoxFrame
from model import manager
import config
import random
@mark('mail_menu')
class MailMenuFrame(BaseAuthedFrame):
def initialize(self):
self.goto('menu', 'mail')
class BaseMailListFrame(BaseAuthedFrame):
_TABEL_START_LINE = 4
_TABEL_HEIGHT = 20
def get(self, char):
if char == ac.k_finish:
self.fetch_mail('next_frame')
self.do_command(self.shortcuts.get(char))
self._table.do_command(self.shortcuts_table.get(char))
self.fetch_mail(self.shortcuts_fetch.get(char))
self.fetch_mail_update(self.shortcuts_update.get(char))
def _wrapper_li(self, d):
return self.render_str('mail-li', **d)
def catch_nodata(self):
self.cls()
self.pause_back(u'你没有信笺哟!')
def _init_screen(self):
self.cls()
self.top_bar()
self.push('\r\n')
self.push(config.str['MAILLIST_QUICK_HELP'])
self.push('\r\n')
self.push(config.str['MAILLIST_THEAD'])
self.bottom_bar()
self._table.restore_screen()
def setup(self, dataloader, counter, default=0):
try:
self._table = self.load(FinitePagedTable, dataloader,
self._wrapper_li, counter, default,
start_line=self._TABEL_START_LINE,
height=self._TABEL_HEIGHT)
except NullValueError:
self.catch_nodata()
raise NullValueError
self._init_screen()
def fetch_mail(self, cmd):
if cmd is None:
return
mail = self._table.fetch()
getattr(self, cmd)(mail)
def fetch_mail_update(self, cmd):
if cmd is None:
return
mail = self._table.fetch()
mail = getattr(self, cmd)(mail)
if mail is not None:
self._table.set_hover_data(mail)
def reload(self):
try:
self._table.reload()
except TableLoadNoDataError:
self.goto_back()
self._table.restore_screen()
def restore(self):
self._init_screen()
@mark('get_mail')
class ReadMailFrame(BaseMailListFrame):
shortcuts = {
ac.k_ctrl_p:"send_mail",
ac.k_left:"goto_back",
'p':'same_topic_mode',
ac.k_ctrl_s:'same_topic_mode',
}
shortcuts_table = {
ac.k_up:"move_up",
ac.k_down:"move_down",
ac.k_page_up:"page_up",
ac.k_page_down:"page_down",
" ":"page_down",
"k":"move_up", "p":"move_up",
"j":"movewn", "n":"movewn",
"P":"page_up", "N":"pagewn",
"$":"goto_last",
ac.k_home:"goto_first",
ac.k_end:"goto_last",
}
shortcuts_fetch = {
ac.k_right : "next_frame",
'r':'reply',
'R':'reply',
'd':'remove_mail',
'D':'remove_mail_range',
ac.k_ctrl_c:'repost',
}
shortcuts_update = {
'm':'set_m_mark',
}
NORMAL_MODE = 0
TOPICE_MODE = 1
MODE_LOADER = [
manager.mail.get_mail_loader,
manager.mail.get_topic_mail_loader,
]
MODE_COUNTER = [
manager.mail.get_mail_counter,
manager.mail.get_mail_counter,
]
MODE_RANKER = [
manager.mail.get_mid_rank,
manager.mail.get_topic_mid_rank,
]
def get_mid_rank(self, mid):
return self.MODE_RANKER[self.mode](self.userid, mid)
def initialize(self, mode=0):
manager.status.set_status(self.seid,
manager.status.MAIL)
manager.notify.clear_mail_notify(self.userid)
self.uid = self.session.user['uid']
self.mode = mode
dataloader, counter = self._get_loader(mode)
self.setup(dataloader, counter)
def _get_loader(self, mode):
return (self.MODE_LOADER[mode](self.userid),
self.MODE_COUNTER[mode](self.userid))
def reset_mode(self, mode):
dataloader, counter = self._get_loader(mode)
try:
self._table.reset_load(dataloader, counter, 0)
except NullValueError:
return
self.get_mid_rank = self.MODE_RANKER[mode]
self._init_screen()
def change_mode(self):
self.mode = 1 - self.mode
self.reset_mode(self.mode)
def next_frame(self, mail):
self.suspend('_view_mail_o', mail=mail)
def send_mail(self):
self.suspend('send_mail')
def reply(self, mail):
self.suspend('_reply_mail_o', mail=mail)
def set_m_mark(self, mail):
return manager.mail.set_m_mark(mail)
def remove_mail(self, mail):
if self.bottom_do(self.confirm, prompt=u'删除你的邮件?[y/n] ') :
manager.mail.remove_mail(mail['mid'])
self.reload()
def remove_mail_range(self):
start = self.readline(prompt=u'首篇文章编号: ')
if start.isdigit():
end = self.readline(prompt=u'末篇文章编号:')
if end.isdigit() :
start_mid = manager.mail.rank2mid(self.session.user.uid,
int(start) - 1)
end_mid = manager.mail.rank2mid(self.session.user.uid,
int(end) - 1)
if start_mid >= end_mid :
return
manager.mail.remove_mail_range(self.session.user.uid,
self.userid,
start_mid, end_mid)
self.reload()
def restore(self):
if self.session['lastmid'] and\
self.session['lastmid'] != self._table.fetch()['mid']:
default = self.get_mid_rank(self.session['lastmid'])
elif not self.session:
default = 0
else:
default = self._table.fetch_num()
try:
try:
self._table.setup(default)
except NullValueError:
self._table.setup(0)
except NullValueError:
self.catch_nodata()
raise ValueError
self._init_screen()
@mark('send_mail')
class SendMailFrame(BaseAuthedFrame):
shortcuts = {
ac.k_ctrl_w : "finish",
}
shortcuts_ui = config.shortcuts['edit_ui']
PROMPT = u'[25;1H[K[1;32m0[m~[1;32m%s[m/[1;32mx[m 选择/随机签名档 [1;32mt[m标题,[1;32mq[m取消:'
def update_attr(self, attrs):
self.render('edit_head_email', **attrs)
def read_attrs(self, touserid, sign_num):
attrs = {
"touserid":touserid,
"usesign":0,
"title":u"[正在设定标题]",
}
if sign_num :
attrs["usersign"] = random.randint(1, sign_num)
self.update_attr(attrs)
attrs['title'] = self.safe_readline(prompt=u'标题:', buf_size=40)
if not attrs['title']:
return
prompt = self.PROMPT % sign_num
while True:
self.update_attr(attrs)
op = self.safe_readline(buf_size=4, prompt=prompt)
if op == '':
break
elif op is False or op=='q':
return
elif op == 't':
attrs['title'] = self.safe_readline(prompt=u'\r\x1b[K标题:',
buf_size=40)
if not attrs['title']:
return
elif op == 'x' and sign_num:
attrs['usesign'] = random.randint(1, sign_num)
elif op.isdigit():
n = int(op)
if n <= sign_num:
attrs['usesign'] = n
return attrs
def initialize(self, touserid=None):
manager.status.set_status(self.seid,
manager.status.SMAIL)
self.cls()
if touserid is None:
touserid = self.safe_readline(prompt=u'收信人:')
if touserid is False :
self.writeln(u'取消写信!')
self.pause()
self.goto_back()
touser = manager.userinfo.get_user(touserid)
if not touser :
self.writeln(u'无法找到该收信人!')
self.pause()
self.goto_back()
touserid = touser.userid
self.touserid = touserid
sign_num = manager.usersign.get_sign_num(self.userid)
self.attrs = self.read_attrs(touserid, sign_num)
if not self.attrs:
self.goto_back()
self.suspend('edit_text', filename='mail')
@handler_edit
def restore(self):
pass
def handler_mail(self, text):
if self.attrs['usesign'] :
signtext = manager.usersign.get_sign(self.userid,
self.attrs['usesign']-1)
else:
signtext = ''
mid = manager.action.send_mail(
fromuserid=self.userid,
touserid=self.touserid,
content=text,
title=self.attrs['title'],
fromaddr=self.session.ip,
signature=signtext)
self.goto_back()
@mark("_reply_mail_o")
class ReplyMailFrame(BaseEditFrame):
PROMPT = u'[25;1H[K[1;32m0[m~[1;32m%s[m/[1;32mx[m 选择/随机签名档 [1;32mt[m标题,[1;32mq[m取消:'
def update_attr(self, attrs):
self.render('edit_head_email', **attrs)
def read_attrs(self, touserid, sign_num, title):
attrs = {
"touserid":touserid,
"usesign":0,
"title":title
}
if sign_num :
attrs["usersign"] = random.randint(1, sign_num)
prompt = self.PROMPT % sign_num
while True:
self.update_attr(attrs)
op = self.safe_readline(buf_size=4, prompt=prompt)
if op == '':
break
elif op is False or op=='q':
return
elif op == 't':
attrs['title'] = self.safe_readline(prompt=u'\r\x1b[K标题:',
prefix=u'Re: ',
buf_size=40)
if not attrs['title']:
return
elif op == 'x' and sign_num:
attrs['usesign'] = random.randint(1, sign_num)
elif op.isdigit():
n = int(op)
if n <= sign_num:
attrs['usesign'] = n
return attrs
def initialize(self, mail):
manager.status.set_status(self.seid,
manager.status.RMAIL)
self.replymail = mail
self.cls()
self.touserid = mail['fromuserid']
if not manager.userinfo.get_user(self.touserid):
self.writeln(u'无法找到该收信人!')
self.pause()
self.goto_back()
sign_num = manager.usersign.get_sign_num(self.userid)
title = mail['title'] if mail['title'].startswith('Re:')\
else u'Re: %s' % mail['title']
self.attrs = self.read_attrs(self.touserid, sign_num, title=title)
if not self.attrs :
self.goto_back()
text = gen_quote_mail(mail)
self.suspend('edit_text', filename='mail', text=text)
@handler_edit
def restore(self):
self.goto_back()
def handler_mail(self, text):
if self.attrs['usesign'] :
signtext = manager.usersign.get_sign(self.userid,
self.attrs['usesign']-1)
else:
signtext = ''
manager.action.reply_mail(self.userid,
self.replymail,
content=text,
title=self.attrs['title'],
fromaddr=self.session.ip,
signature=signtext)
@mark('_view_mail_o')
class ReadMailFrame(BaseTextBoxFrame):
shortcuts = {
"r":"reply",
"R":"reply",
ac.k_ctrl_x:"change_mode",
ac.k_left:"back_to_maillist",
}
MODE_LOADER = [
manager.mail.get_mail_loader_signle,
manager.mail.get_topic_loader_signle,
]
MODE_BOTTOM = [
'bottom_view',
'bottom_view_topic',
]
def wrapper_mail(self, mail):
return self.render_str('mail-t', **mail)
def initialize(self, mail, mode=0):
manager.status.set_status(self.seid,
manager.status.RMAIL)
manager.mail.set_read(self.session.user['uid'], mail['mid'])
self.mail = mail
self.mode = mode
self._set_mode(mode)
self.setup(self.wrapper_mail(mail))
def _set_mode(self, mode):
self.next_loader, self.prev_loader = self.MODE_LOADER[mode](
self.userid
)
self.bottom_tpl = self.MODE_BOTTOM[mode]
def change_mode(self):
self.mode = 1 - self.mode
self._set_mode(self.mode)
self.restore_screen()
def bottom_bar(self, s, l, message=''):
self.push(ac.move2(24, 1))
self.render(self.bottom_tpl, s=s, l=l, message=message)
def finish(self, e=None):
logger.debug('finish [%s]', e)
if e is None:
self.session['lastmid'] = self.mail['mid']
self.goto_back()
if e is True:
mail = self.next_loader(self.mail['mid'])
else:
mail = self.prev_loader(self.mail['mid'])
if not mail:
self.session['lastmid'] = self.mail['mid']
self.goto_back()
self.mail = mail
self.reset_text(self.wrapper_mail(self.mail), 0)
def reply(self):
self.suspend('_reply_mail_o', mail=self.mail)
def back_to_maillist(self):
self.session['lastmid'] = self.mail['mid']
self.goto_back_history('get_mail')