diff options
-rw-r--r-- | src/mbox.lua | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/mbox.lua b/src/mbox.lua new file mode 100644 index 0000000..9cce9ff --- /dev/null +++ b/src/mbox.lua | |||
@@ -0,0 +1,45 @@ | |||
1 | local Public = {} | ||
2 | |||
3 | parse = Public | ||
4 | |||
5 | function Public.headers(headers_s) | ||
6 | local headers = {} | ||
7 | headers_s = "\n" .. headers_s .. "$$$:\n" | ||
8 | local i, j = 1, 1 | ||
9 | local name, value, _ | ||
10 | while 1 do | ||
11 | j = strfind(headers_s, "\n%S-:", i+1) | ||
12 | if not j then break end | ||
13 | _,_, name, value = strfind(strsub(headers_s, i+1, j-1), "(%S-):%s?(.*)") | ||
14 | value = gsub(value or "", "\r\n", "\n") | ||
15 | value = gsub(value, "\n%s*", " ") | ||
16 | name = strlower(name) | ||
17 | if headers[name] then headers[name] = headers[name] .. ", " .. value | ||
18 | else headers[name] = value end | ||
19 | i, j = j, i | ||
20 | end | ||
21 | headers["$$$"] = nil | ||
22 | return headers | ||
23 | end | ||
24 | |||
25 | function Public.message(message_s) | ||
26 | message_s = gsub(message_s, "^.-\n", "") | ||
27 | local _, headers_s, body | ||
28 | _, _, headers_s, body = strfind(message_s, "^(.-\n)\n(.*)") | ||
29 | headers_s = headers_s or "" | ||
30 | body = body or "" | ||
31 | return { headers = %Public.headers(headers_s), body = body } | ||
32 | end | ||
33 | |||
34 | function Public.mbox(mbox_s) | ||
35 | local mbox = {} | ||
36 | mbox_s = "\n" .. mbox_s .. "\nFrom " | ||
37 | local i, j = 1, 1 | ||
38 | while 1 do | ||
39 | j = strfind(mbox_s, "\nFrom ", i + 1) | ||
40 | if not j then break end | ||
41 | tinsert(mbox, %Public.message(strsub(mbox_s, i + 1, j - 1))) | ||
42 | i, j = j, i | ||
43 | end | ||
44 | return mbox | ||
45 | end | ||