Debian Codegraph @

lualib/lua_smtp.lua

raw
1 --[[
2 Copyright (c) 2022, Vsevolod Stakhov <[email protected]>
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 ]]--
16
17 local rspamd_tcp = require "rspamd_tcp"
18 local rspamd_text = require "rspamd_text"
19 local lua_util = require "lua_util"
20
21 local exports = {}
22
23 local CRLF = '\r\n'
24 local default_timeout = 10.0
25
26 --[[[
27 -- @function lua_smtp.sendmail(task, message, opts, callback)
28 --]]
29 local function sendmail(opts, message, callback)
30 local stage = 'connect'
31
32 local function mail_cb(err, data, conn)
33 local function no_error_write(merr)
34 if merr then
35 callback(false, string.format('error on stage %s: %s',
36 stage, merr))
37 if conn then
38 conn:close()
39 end
40
41 return false
42 end
43
44 return true
45 end
46
47 local function no_error_read(merr, mdata, wantcode)
48 wantcode = wantcode or '2'
49 if merr then
50 callback(false, string.format('error on stage %s: %s',
51 stage, merr))
52 if conn then
53 conn:close()
54 end
55
56 return false
57 end
58 if mdata then
59 if type(mdata) ~= 'string' then
60 mdata = tostring(mdata)
61 end
62 if string.sub(mdata, 1, 1) ~= wantcode then
63 callback(false, string.format('bad smtp response on stage %s: "%s" when "%s" expected',
64 stage, mdata, wantcode))
65 if conn then
66 conn:close()
67 end
68 return false
69 end
70 else
71 callback(false, string.format('no data on stage %s',
72 stage))
73 if conn then
74 conn:close()
75 end
76 return false
77 end
78 return true
79 end
80
81 -- After quit
82 local function all_done_cb(merr, mdata)
83 if conn then
84 conn:close()
85 end
86
87 callback(true, nil)
88
89 return true
90 end
91
92 -- QUIT stage
93 local function quit_done_cb(_, _)
94 conn:add_read(all_done_cb, CRLF)
95 end
96 local function quit_cb(merr, mdata)
97 if no_error_read(merr, mdata) then
98 conn:add_write(quit_done_cb, 'QUIT' .. CRLF)
99 end
100 end
101 local function pre_quit_cb(merr, _)
102 if no_error_write(merr) then
103 stage = 'quit'
104 conn:add_read(quit_cb, CRLF)
105 end
106 end
107
108 -- DATA stage
109 local function data_done_cb(merr, mdata)
110 if no_error_read(merr, mdata, '3') then
111 -- Normalize line endings to CRLF for SMTP compliance
112 local function normalize_to_crlf(msg)
113 if type(msg) == 'userdata' then
114 -- rspamd_text object
115 return msg:normalize_newlines("crlf")
116 elseif type(msg) == 'string' then
117 -- Convert string to text, normalize, back to string
118 local txt = rspamd_text.fromstring(msg)
119 txt:normalize_newlines("crlf")
120 return txt:str()
121 end
122 return msg
123 end
124
125 if type(message) == 'string' or type(message) == 'userdata' then
126 message = normalize_to_crlf(message)
127 conn:add_write(pre_quit_cb, { message, CRLF .. '.' .. CRLF })
128 else
129 -- Array of chunks
130 for i = 1, #message do
131 message[i] = normalize_to_crlf(message[i])
132 end
133 table.insert(message, CRLF .. '.' .. CRLF)
134 conn:add_write(pre_quit_cb, message)
135 end
136 end
137 end
138 local function data_cb(merr, _)
139 if no_error_write(merr) then
140 conn:add_read(data_done_cb, CRLF)
141 end
142 end
143
144 -- RCPT phase
145 local next_recipient
146 local function rcpt_done_cb_gen(i)
147 return function(merr, mdata)
148 if no_error_read(merr, mdata) then
149 if i == #opts.recipients then
150 conn:add_write(data_cb, 'DATA' .. CRLF)
151 else
152 next_recipient(i + 1)
153 end
154 end
155 end
156 end
157
158 local function rcpt_cb_gen(i)
159 return function(merr, _)
160 if no_error_write(merr, '2') then
161 conn:add_read(rcpt_done_cb_gen(i), CRLF)
162 end
163 end
164 end
165
166 next_recipient = function(i)
167 conn:add_write(rcpt_cb_gen(i),
168 string.format('RCPT TO: <%s>%s', opts.recipients[i], CRLF))
169 end
170
171 -- FROM stage
172 local function from_done_cb(merr, mdata)
173 -- We need to iterate over recipients sequentially
174 if no_error_read(merr, mdata, '2') then
175 stage = 'rcpt'
176 next_recipient(1)
177 end
178 end
179 local function from_cb(merr, _)
180 if no_error_write(merr) then
181 conn:add_read(from_done_cb, CRLF)
182 end
183 end
184 local function hello_done_cb(merr, mdata)
185 if no_error_read(merr, mdata) then
186 stage = 'from'
187 conn:add_write(from_cb, string.format(
188 'MAIL FROM: <%s>%s', opts.from, CRLF))
189 end
190 end
191
192 -- HELO stage
193 local function hello_cb(merr)
194 if no_error_write(merr) then
195 conn:add_read(hello_done_cb, CRLF)
196 end
197 end
198 if no_error_read(err, data) then
199 stage = 'helo'
200 conn:add_write(hello_cb, string.format('HELO %s%s',
201 opts.helo, CRLF))
202 end
203 end
204
205 if type(opts.recipients) == 'string' then
206 opts.recipients = { opts.recipients }
207 end
208
209 local tcp_opts = lua_util.shallowcopy(opts)
210 tcp_opts.stop_pattern = CRLF
211 tcp_opts.timeout = opts.timeout or default_timeout
212 tcp_opts.callback = mail_cb
213
214 if not rspamd_tcp.request(tcp_opts) then
215 callback(false, 'cannot make a TCP connection')
216 end
217 end
218
219 exports.sendmail = sendmail
220
221 return exports