Repository navigation
Expand file tree
/
Copy pathapi.lua
More file actions
174 lines (151 loc) · 4.21 KB
/
Copy pathapi.lua
File metadata and controls
174 lines (151 loc) · 4.21 KB
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
-- see: mail.md
-- translation
local S = mail.S
local f = string.format
mail.registered_on_receives = {}
function mail.register_on_receive(func)
mail.registered_on_receives[#mail.registered_on_receives + 1] = func
end
mail.registered_on_player_receives = {}
function mail.register_on_player_receive(func)
table.insert(mail.registered_on_player_receives, func)
end
mail.registered_recipient_handlers = {}
function mail.register_recipient_handler(func)
table.insert(mail.registered_recipient_handlers, func)
end
function mail.send(m)
if type(m.from) ~= "string" then
return false, "'from' is not a string"
end
if type(m.to or "") ~= "string" then
return false, "'to' is not a string"
end
if type(m.cc or "") ~= "string" then
return false, "'cc' is not a string"
end
if type(m.bcc or "") ~= "string" then
return false, "'bcc' is not a string"
end
if type(m.subject or "") ~= "string" then
return false, "'subject' is not a string"
end
if type(m.body) ~= "string" then
return false, "'body' is not a string"
end
-- defaults
m.subject = m.subject or "(No subject)"
-- normalize to, cc and bcc while compiling a list of all recipients
local recipients = {}
local undeliverable = {}
m.to = mail.concat_player_list(mail.extract_maillists(m.to, m.from))
m.to = mail.normalize_players_and_add_recipients(m.from, m.to, recipients, undeliverable)
if m.cc then
m.cc = mail.concat_player_list(mail.extract_maillists(m.cc, m.from))
m.cc = mail.normalize_players_and_add_recipients(mail.from, m.cc, recipients, undeliverable)
end
if m.bcc then
m.bcc = mail.concat_player_list(mail.extract_maillists(m.bcc, m.from))
m.bcc = mail.normalize_players_and_add_recipients(m.from, m.bcc, recipients, undeliverable)
end
if next(undeliverable) then -- table is not empty
local undeliverable_reason = { S("The mail could not be sent:") }
for _, reason in pairs(undeliverable) do
table.insert(undeliverable_reason, reason)
end
return false, table.concat(undeliverable_reason, "\n")
elseif not next(recipients) then
return false, S("You did not specify any valid recipient.")
end
local extra = {}
local extra_log
if m.cc then
table.insert(extra, "CC: " .. m.cc)
end
if m.bcc then
table.insert(extra, "BCC: " .. m.bcc)
end
if #extra > 0 then
extra_log = f(" (%s)", table.concat(extra, " - "))
else
extra_log = ""
end
core.log(
"action",
f("[mail] %q send mail to %q%s with subject %q and body %q", m.from, m.to, extra_log, m.subject, m.body)
)
local id
if m.id then
mail.delete_mail(m.from, m.id)
id = m.id
end
-- form the actual mail
local msg = {
id = id or mail.new_uuid(),
from = m.from,
to = m.to,
cc = m.cc,
bcc = m.bcc,
subject = m.subject,
body = m.body,
time = os.time(),
}
-- add in senders outbox
local entry = mail.get_storage_entry(m.from)
table.insert(entry.outbox, 1, msg)
mail.set_storage_entry(m.from, entry)
msg.spam = #mail.check_spam(msg) >= 1
-- add in every receivers inbox
for _, deliver in pairs(recipients) do
deliver(msg)
end
for i = 1, #mail.registered_on_receives do
if mail.registered_on_receives[i](m) then
break
end
end
return true
end
function mail.save_draft(m)
if type(m.from) ~= "string" then
return false, "'from' is not a string"
end
if type(m.to or "") ~= "string" then
return false, "'to' is not a string"
end
if type(m.cc or "") ~= "string" then
return false, "'cc' is not a string"
end
if type(m.bcc or "") ~= "string" then
return false, "'bcc' is not a string"
end
if type(m.subject or "") ~= "string" then
return false, "'subject' is not a string"
end
if type(m.body) ~= "string" then
return false, "'body' is not a string"
end
-- defaults
m.subject = m.subject or "(No subject)"
core.log("verbose", f("[mail] %q saves draft with subject %q and body %q", m.from, m.subject, m.body))
-- remove it is an update
local id
if m.id then
mail.delete_mail(m.from, m.id)
id = m.id
end
-- add (again ie. update) in sender drafts
local entry = mail.get_storage_entry(m.from)
table.insert(entry.drafts, 1, {
id = id or mail.new_uuid(),
from = m.from,
to = m.to,
cc = m.cc,
bcc = m.bcc,
subject = m.subject,
body = m.body,
time = os.time(),
})
mail.set_storage_entry(m.from, entry)
return true
end