-
Notifications
You must be signed in to change notification settings - Fork 3
/
mail.py
121 lines (107 loc) · 4.52 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
#!/usr/bin/env python
from Util import Util
import mailbox
import json
from errors import *
DEFAULT_MAIL_VIEW_COUNT = 20
MAIL_SIZE_UNLIMITED = -1
class Mail:
@staticmethod
def GET(svc, session, params, action):
if (session is None): raise Unauthorized('login first')
if not session.CheckScope('bbs'): raise NoPerm("out of scope")
if (action == 'list'):
folder = svc.get_str(params, 'folder', 'inbox')
start = svc.get_int(params, 'start', 0)
end = svc.get_int(params, 'end', 0)
count = svc.get_int(params, 'count', 0)
svc.writedata(Mail.List(session.GetUser(), folder, start, count, end))
elif (action == 'view'):
folder = svc.get_str(params, 'folder', 'inbox')
index = svc.get_int(params, 'index')
start = svc.get_int(params, 'start', 0)
count = svc.get_int(params, 'count', 0)
if start < 0 or count < 0:
raise WrongArgs('start or count < 0')
svc.writedata(Mail.View(session.GetUser(), folder, index, start, count))
elif (action == 'check_unread'):
folder = svc.get_str(params, 'folder', 'inbox')
index = svc.get_int(params, 'index', 0)
result = {'unread': Mail.CheckUnread(session.GetUser(), folder, index)}
svc.writedata(json.dumps(result))
elif action == 'quote':
folder = svc.get_str(params, 'folder', 'inbox')
mode = svc.get_str(params, 'mode', 'S')
index = svc.get_int(params, 'index')
(title, content) = session.GetUser().mbox.quote_mail(folder, mode,
index)
result = {'title': title, 'content': content}
svc.writedata(json.dumps(result))
else:
raise WrongArgs('unknown action')
@staticmethod
def POST(svc, session, params, action):
if session is None: raise Unauthorized('login first')
if not session.CheckScope('bbs'): raise NoPerm('out of scope')
if action == 'send':
title = svc.get_str(params, 'title')
content = svc.get_str(params, 'content')
receiver_id = svc.get_str(params, 'to')
signature_id = svc.get_int(params, 'signature_id', 0)
save_in_sent = svc.get_bool(params, 'save_in_sent', True)
session.GetUser().SendMailTo(receiver_id, title, content,
signature_id, session, save_in_sent)
result = {'result': 'ok'}
svc.writedata(json.dumps(result))
else:
raise WrongArgs('unknown action')
@staticmethod
def List(user, folder, start, count, end):
mbox = mailbox.MailBox(user.GetName())
folder = mbox.get_folder(folder)
total = folder.count()
start, end = Util.CheckRange(start, end, count, DEFAULT_MAIL_VIEW_COUNT, total)
if (start <= end and start >= 1 and end <= total):
result = '{ "start": %d, "end": %d, "mails": [\n' % (start, end)
first = True
for i in range(start - 1, end):
entry = folder.get_entry(i)
if entry is None:
continue
if not first:
result += ',\n'
post = entry.GetInfo('mail')
post['id'] = i+1
result += json.dumps(post)
first = False
result += '\n]}'
return result
else:
raise OutOfRange('out of range')
@staticmethod
def View(user, folder, index, start, count):
mbox = mailbox.MailBox(user.GetName())
folder = mbox.get_folder(folder)
entry = folder.get_entry(index - 1)
if (entry is None):
raise OutOfRange('out of range')
post = folder.get_content(index - 1)
if (post is None):
raise OutOfRange('out of range')
info = dict(entry.GetInfo().items() + post.GetInfo(start, count).items())
info['id'] = index
if not entry.IsRead():
entry.SetRead(True)
folder.set_entry(index - 1, entry)
# session.GetUserInfo().SetMailCheck()
return json.dumps(info)
@staticmethod
def CheckUnread(user, folder = 'inbox', index = 0):
mbox = mailbox.MailBox(user.GetName())
folder = mbox.get_folder(folder)
if (index == 0):
index = folder.count()
entry = folder.get_entry(index - 1)
if (entry is None):
raise OutOfRange('index out of range')
return not entry.IsRead()