2002-07-08 23:55:01 +02:00
|
|
|
local Public = {}
|
|
|
|
|
2002-12-03 00:34:41 +01:00
|
|
|
mbox = Public
|
2002-07-08 23:55:01 +02:00
|
|
|
|
2002-12-03 00:34:41 +01:00
|
|
|
function Public.split_message(message_s)
|
|
|
|
local message = {}
|
2003-03-20 01:24:44 +01:00
|
|
|
message_s = string.gsub(message_s, "\r\n", "\n")
|
|
|
|
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
|
2003-03-20 01:24:44 +01:00
|
|
|
string.gsub(message_s, "^\n(.*)", function (b) %message.body = b end)
|
2002-12-03 00:34:41 +01:00
|
|
|
end
|
|
|
|
if not message.headers and not message.body then
|
|
|
|
message.headers = message_s
|
|
|
|
end
|
|
|
|
return message.headers or "", message.body or ""
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.split_headers(headers_s)
|
|
|
|
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[ ]+", " ")
|
|
|
|
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
|
|
|
|
|
|
|
|
function Public.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+", "")
|
|
|
|
local _, __, name, value = string.find(header_s, "([^%s:]-):%s*(.*)")
|
2002-12-03 00:34:41 +01:00
|
|
|
return name, value
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.parse_headers(headers_s)
|
|
|
|
local headers_t = %Public.split_headers(headers_s)
|
2002-07-08 23:55:01 +02:00
|
|
|
local headers = {}
|
2003-03-20 01:24:44 +01:00
|
|
|
for i = 1, table.getn(headers_t) do
|
2002-12-03 00:34:41 +01:00
|
|
|
local name, value = %Public.parse_header(headers_t[i])
|
|
|
|
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
|
|
|
|
|
2002-12-03 00:34:41 +01:00
|
|
|
function Public.parse_from(from)
|
2003-03-20 01:24:44 +01:00
|
|
|
local _, __, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>")
|
2002-12-03 00:34:41 +01:00
|
|
|
if not address then
|
2003-03-20 01:24:44 +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
|
2003-03-20 01:24:44 +01:00
|
|
|
name = string.gsub(name, '"', "")
|
2002-12-03 00:34:41 +01:00
|
|
|
return name, address
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.split_mbox(mbox_s)
|
|
|
|
mbox = {}
|
2003-03-20 01:24:44 +01:00
|
|
|
mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n"
|
2002-12-03 00:34:41 +01:00
|
|
|
local nj, i, j = 1, 1, 1
|
|
|
|
while 1 do
|
2003-03-20 01:24:44 +01:00
|
|
|
i, nj = string.find(mbox_s, "\n\nFrom .-\n", j)
|
2002-12-03 00:34:41 +01:00
|
|
|
if not i then break end
|
2003-03-20 01:24:44 +01:00
|
|
|
local message = string.sub(mbox_s, j, i-1)
|
|
|
|
table.insert(mbox, message)
|
2002-12-03 00:34:41 +01:00
|
|
|
j = nj+1
|
|
|
|
end
|
|
|
|
return mbox
|
|
|
|
end
|
|
|
|
|
2003-03-20 01:24:44 +01:00
|
|
|
function Public.parse(mbox_s)
|
2002-12-03 00:34:41 +01:00
|
|
|
local mbox = %Public.split_mbox(mbox_s)
|
2003-03-20 01:24:44 +01:00
|
|
|
for i = 1, table.getn(mbox) do
|
2002-12-03 00:34:41 +01:00
|
|
|
mbox[i] = %Public.parse_message(mbox[i])
|
|
|
|
end
|
|
|
|
return mbox
|
|
|
|
end
|
|
|
|
|
|
|
|
function Public.parse_message(message_s)
|
|
|
|
local message = {}
|
|
|
|
message.headers, message.body = %Public.split_message(message_s)
|
|
|
|
message.headers = %Public.parse_headers(message.headers)
|
|
|
|
return message
|
2002-07-08 23:55:01 +02:00
|
|
|
end
|