aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2002-07-08 21:55:01 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2002-07-08 21:55:01 +0000
commit0fc2302221e80a8f2d55808320073024a02517e8 (patch)
tree7406a1132095619cae44daf6e145aeaa8f835288
parentdaff3db32ed52aa18dbe1e8c613535fbe0c622c7 (diff)
downloadluasocket-0fc2302221e80a8f2d55808320073024a02517e8.tar.gz
luasocket-0fc2302221e80a8f2d55808320073024a02517e8.tar.bz2
luasocket-0fc2302221e80a8f2d55808320073024a02517e8.zip
Initial revision
-rw-r--r--src/mbox.lua45
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 @@
1local Public = {}
2
3parse = Public
4
5function 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
23end
24
25function 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 }
32end
33
34function 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
45end