This repository was archived by the owner on Nov 20, 2020. It is now read-only.
forked from lua-stdlib/lua-stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmbox.lua
More file actions
53 lines (50 loc) · 1.3 KB
/
mbox.lua
File metadata and controls
53 lines (50 loc) · 1.3 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
--- mbox parser.
-- Based on code by Diego Nahab.
module ("mbox", package.seeall)
local function headers (s)
local header = {}
s = "\n" .. s .. "$$$:\n"
local i, j = 1, 1
while true do
j = string.find (s, "\n%S-:", i + 1)
if not j then
break
end
local _, _, name, val = string.find (string.sub (s, i + 1, j - 1),
"(%S-):(.*)")
val = string.gsub (val or "", "\r\n", "\n")
val = string.gsub (val, "\n%s*", " ")
name = string.lower (name)
if header[name] then
header[name] = header[name] .. ", " .. val
else
header[name] = val
end
i, j = j, i
end
header["$$$"] = nil
return header
end
local function message (s)
s = string.gsub (s, "^.-\n", "")
local _, s, body
_, _, s, body = string.find(s, "^(.-\n)\n(.*)")
return {header = headers (s or ""), body = body or ""}
end
--- Parse a mailbox into messages.
-- @param s mailbox as a string
-- @return list of messages, each of form <code>{header = {...}, body = "..."}</code>
function parse (s)
local mbox = {}
s = "\n" .. s .. "\nFrom "
local i, j = 1, 1
while true do
j = string.find (s, "\nFrom ", i + 1)
if not j then
break
end
table.insert (mbox, message (string.sub (s, i + 1, j - 1)))
i, j = j, i
end
return mbox
end