2013-06-05 12:36:51 +02:00
|
|
|
local _M = {}
|
2002-07-08 23:55:01 +02:00
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
if module then
|
2022-03-18 12:12:39 +01:00
|
|
|
mbox = _M -- luacheck: ignore
|
|
|
|
end
|
2002-07-08 23:55:01 +02:00
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
function _M.split_message(message_s)
|
2002-12-03 00:34:41 +01:00
|
|
|
local message = {}
|
2003-03-20 01:24:44 +01:00
|
|
|
message_s = string.gsub(message_s, "\r\n", "\n")
|
2011-05-25 22:57:22 +02:00
|
|
|
string.gsub(message_s, "^(.-\n)\n", function (h) message.headers = h end)
|
|
|
|
string.gsub(message_s, "^.-\n\n(.*)", function (b) message.body = b end)
|
2002-12-03 00:34:41 +01:00
|
|
|
if not message.body then
|
2011-05-25 22:57:22 +02:00
|
|
|
string.gsub(message_s, "^\n(.*)", function (b) message.body = b end)
|
2002-12-03 00:34:41 +01:00
|
|
|
end
|
2005-11-22 09:33:29 +01:00
|
|
|
if not message.headers and not message.body then
|
2002-12-03 00:34:41 +01:00
|
|
|
message.headers = message_s
|
|
|
|
end
|
|
|
|
return message.headers or "", message.body or ""
|
|
|
|
end
|
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
function _M.split_headers(headers_s)
|
2002-12-03 00:34:41 +01:00
|
|
|
local headers = {}
|
2003-03-20 01:24:44 +01:00
|
|
|
headers_s = string.gsub(headers_s, "\r\n", "\n")
|
|
|
|
headers_s = string.gsub(headers_s, "\n[ ]+", " ")
|
2003-05-25 03:54:13 +02:00
|
|
|
string.gsub("\n" .. headers_s, "\n([^\n]+)", function (h) table.insert(headers, h) end)
|
2002-12-03 00:34:41 +01:00
|
|
|
return headers
|
|
|
|
end
|
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
function _M.parse_header(header_s)
|
2003-03-20 01:24:44 +01:00
|
|
|
header_s = string.gsub(header_s, "\n[ ]+", " ")
|
|
|
|
header_s = string.gsub(header_s, "\n+", "")
|
2022-03-18 12:12:39 +01:00
|
|
|
local _, _, name, value = string.find(header_s, "([^%s:]-):%s*(.*)")
|
2002-12-03 00:34:41 +01:00
|
|
|
return name, value
|
|
|
|
end
|
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
function _M.parse_headers(headers_s)
|
|
|
|
local headers_t = _M.split_headers(headers_s)
|
2002-07-08 23:55:01 +02:00
|
|
|
local headers = {}
|
2013-02-25 23:28:28 +01:00
|
|
|
for i = 1, #headers_t do
|
2013-06-05 12:36:51 +02:00
|
|
|
local name, value = _M.parse_header(headers_t[i])
|
2002-12-03 00:34:41 +01:00
|
|
|
if name then
|
2003-03-20 01:24:44 +01:00
|
|
|
name = string.lower(name)
|
2002-12-03 00:34:41 +01:00
|
|
|
if headers[name] then
|
|
|
|
headers[name] = headers[name] .. ", " .. value
|
|
|
|
else headers[name] = value end
|
|
|
|
end
|
2002-07-08 23:55:01 +02:00
|
|
|
end
|
|
|
|
return headers
|
|
|
|
end
|
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
function _M.parse_from(from)
|
2022-03-18 12:12:39 +01:00
|
|
|
local _, _, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>")
|
2002-12-03 00:34:41 +01:00
|
|
|
if not address then
|
2022-03-18 12:12:39 +01:00
|
|
|
_, _, address = string.find(from, "%s*(.+)%s*")
|
2002-07-08 23:55:01 +02:00
|
|
|
end
|
2002-12-03 00:34:41 +01:00
|
|
|
name = name or ""
|
|
|
|
address = address or ""
|
|
|
|
if name == "" then name = address end
|
2011-05-25 22:57:22 +02:00
|
|
|
name = string.gsub(name, '"', "")
|
2002-12-03 00:34:41 +01:00
|
|
|
return name, address
|
|
|
|
end
|
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
function _M.split_mbox(mbox_s)
|
2016-02-11 13:57:56 +01:00
|
|
|
local mbox = {}
|
2011-05-25 22:57:22 +02:00
|
|
|
mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n"
|
2022-03-18 12:12:39 +01:00
|
|
|
local nj, i
|
|
|
|
local j = 1
|
2011-05-25 22:57:22 +02:00
|
|
|
while 1 do
|
|
|
|
i, nj = string.find(mbox_s, "\n\nFrom .-\n", j)
|
|
|
|
if not i then break end
|
|
|
|
local message = string.sub(mbox_s, j, i-1)
|
|
|
|
table.insert(mbox, message)
|
|
|
|
j = nj+1
|
|
|
|
end
|
|
|
|
return mbox
|
2002-12-03 00:34:41 +01:00
|
|
|
end
|
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
function _M.parse(mbox_s)
|
|
|
|
local mbox = _M.split_mbox(mbox_s)
|
2013-02-25 23:28:28 +01:00
|
|
|
for i = 1, #mbox do
|
2013-06-05 12:36:51 +02:00
|
|
|
mbox[i] = _M.parse_message(mbox[i])
|
2011-05-25 22:57:22 +02:00
|
|
|
end
|
|
|
|
return mbox
|
2002-12-03 00:34:41 +01:00
|
|
|
end
|
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
function _M.parse_message(message_s)
|
2002-12-03 00:34:41 +01:00
|
|
|
local message = {}
|
2013-06-05 12:36:51 +02:00
|
|
|
message.headers, message.body = _M.split_message(message_s)
|
|
|
|
message.headers = _M.parse_headers(message.headers)
|
2002-12-03 00:34:41 +01:00
|
|
|
return message
|
2002-07-08 23:55:01 +02:00
|
|
|
end
|
2013-05-27 10:45:09 +02:00
|
|
|
|
2013-06-05 12:36:51 +02:00
|
|
|
return _M
|