diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-03-20 00:24:44 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2003-03-20 00:24:44 +0000 |
| commit | 53857360bb1ca9cd2080b69d930763ae59db9b06 (patch) | |
| tree | 6c1bc6d6462695cf9048801b2244f7fd0cd21ad5 /src/mbox.lua | |
| parent | 7da19138e37c4e0123860f1fecbceb80c3d2627d (diff) | |
| download | luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.tar.gz luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.tar.bz2 luasocket-53857360bb1ca9cd2080b69d930763ae59db9b06.zip | |
Finish port to Lua 5. Everything is working fine.
Still doesn't work in Windows.
Diffstat (limited to 'src/mbox.lua')
| -rw-r--r-- | src/mbox.lua | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/src/mbox.lua b/src/mbox.lua index 2969111..4a72331 100644 --- a/src/mbox.lua +++ b/src/mbox.lua | |||
| @@ -4,11 +4,11 @@ mbox = Public | |||
| 4 | 4 | ||
| 5 | function Public.split_message(message_s) | 5 | function Public.split_message(message_s) |
| 6 | local message = {} | 6 | local message = {} |
| 7 | message_s = gsub(message_s, "\r\n", "\n") | 7 | message_s = string.gsub(message_s, "\r\n", "\n") |
| 8 | gsub(message_s, "^(.-\n)\n", function (h) %message.headers = h end) | 8 | string.gsub(message_s, "^(.-\n)\n", function (h) %message.headers = h end) |
| 9 | gsub(message_s, "^.-\n\n(.*)", function (b) %message.body = b end) | 9 | string.gsub(message_s, "^.-\n\n(.*)", function (b) %message.body = b end) |
| 10 | if not message.body then | 10 | if not message.body then |
| 11 | gsub(message_s, "^\n(.*)", function (b) %message.body = b end) | 11 | string.gsub(message_s, "^\n(.*)", function (b) %message.body = b end) |
| 12 | end | 12 | end |
| 13 | if not message.headers and not message.body then | 13 | if not message.headers and not message.body then |
| 14 | message.headers = message_s | 14 | message.headers = message_s |
| @@ -18,26 +18,26 @@ end | |||
| 18 | 18 | ||
| 19 | function Public.split_headers(headers_s) | 19 | function Public.split_headers(headers_s) |
| 20 | local headers = {} | 20 | local headers = {} |
| 21 | headers_s = gsub(headers_s, "\r\n", "\n") | 21 | headers_s = string.gsub(headers_s, "\r\n", "\n") |
| 22 | headers_s = gsub(headers_s, "\n[ ]+", " ") | 22 | headers_s = string.gsub(headers_s, "\n[ ]+", " ") |
| 23 | gsub("\n" .. headers_s, "\n([^\n]+)", function (h) tinsert(%headers, h) end) | 23 | string.gsub("\n" .. headers_s, "\n([^\n]+)", function (h) table.insert(%headers, h) end) |
| 24 | return headers | 24 | return headers |
| 25 | end | 25 | end |
| 26 | 26 | ||
| 27 | function Public.parse_header(header_s) | 27 | function Public.parse_header(header_s) |
| 28 | header_s = gsub(header_s, "\n[ ]+", " ") | 28 | header_s = string.gsub(header_s, "\n[ ]+", " ") |
| 29 | header_s = gsub(header_s, "\n+", "") | 29 | header_s = string.gsub(header_s, "\n+", "") |
| 30 | local _, __, name, value = strfind(header_s, "([^%s:]-):%s*(.*)") | 30 | local _, __, name, value = string.find(header_s, "([^%s:]-):%s*(.*)") |
| 31 | return name, value | 31 | return name, value |
| 32 | end | 32 | end |
| 33 | 33 | ||
| 34 | function Public.parse_headers(headers_s) | 34 | function Public.parse_headers(headers_s) |
| 35 | local headers_t = %Public.split_headers(headers_s) | 35 | local headers_t = %Public.split_headers(headers_s) |
| 36 | local headers = {} | 36 | local headers = {} |
| 37 | for i = 1, getn(headers_t) do | 37 | for i = 1, table.getn(headers_t) do |
| 38 | local name, value = %Public.parse_header(headers_t[i]) | 38 | local name, value = %Public.parse_header(headers_t[i]) |
| 39 | if name then | 39 | if name then |
| 40 | name = strlower(name) | 40 | name = string.lower(name) |
| 41 | if headers[name] then | 41 | if headers[name] then |
| 42 | headers[name] = headers[name] .. ", " .. value | 42 | headers[name] = headers[name] .. ", " .. value |
| 43 | else headers[name] = value end | 43 | else headers[name] = value end |
| @@ -47,34 +47,34 @@ function Public.parse_headers(headers_s) | |||
| 47 | end | 47 | end |
| 48 | 48 | ||
| 49 | function Public.parse_from(from) | 49 | function Public.parse_from(from) |
| 50 | local _, __, name, address = strfind(from, "^%s*(.-)%s*%<(.-)%>") | 50 | local _, __, name, address = string.find(from, "^%s*(.-)%s*%<(.-)%>") |
| 51 | if not address then | 51 | if not address then |
| 52 | _, __, address = strfind(from, "%s*(.+)%s*") | 52 | _, __, address = string.find(from, "%s*(.+)%s*") |
| 53 | end | 53 | end |
| 54 | name = name or "" | 54 | name = name or "" |
| 55 | address = address or "" | 55 | address = address or "" |
| 56 | if name == "" then name = address end | 56 | if name == "" then name = address end |
| 57 | name = gsub(name, '"', "") | 57 | name = string.gsub(name, '"', "") |
| 58 | return name, address | 58 | return name, address |
| 59 | end | 59 | end |
| 60 | 60 | ||
| 61 | function Public.split_mbox(mbox_s) | 61 | function Public.split_mbox(mbox_s) |
| 62 | mbox = {} | 62 | mbox = {} |
| 63 | mbox_s = gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" | 63 | mbox_s = string.gsub(mbox_s, "\r\n", "\n") .."\n\nFrom \n" |
| 64 | local nj, i, j = 1, 1, 1 | 64 | local nj, i, j = 1, 1, 1 |
| 65 | while 1 do | 65 | while 1 do |
| 66 | i, nj = strfind(mbox_s, "\n\nFrom .-\n", j) | 66 | i, nj = string.find(mbox_s, "\n\nFrom .-\n", j) |
| 67 | if not i then break end | 67 | if not i then break end |
| 68 | local message = strsub(mbox_s, j, i-1) | 68 | local message = string.sub(mbox_s, j, i-1) |
| 69 | tinsert(mbox, message) | 69 | table.insert(mbox, message) |
| 70 | j = nj+1 | 70 | j = nj+1 |
| 71 | end | 71 | end |
| 72 | return mbox | 72 | return mbox |
| 73 | end | 73 | end |
| 74 | 74 | ||
| 75 | function Public.parse_mbox(mbox_s) | 75 | function Public.parse(mbox_s) |
| 76 | local mbox = %Public.split_mbox(mbox_s) | 76 | local mbox = %Public.split_mbox(mbox_s) |
| 77 | for i = 1, getn(mbox) do | 77 | for i = 1, table.getn(mbox) do |
| 78 | mbox[i] = %Public.parse_message(mbox[i]) | 78 | mbox[i] = %Public.parse_message(mbox[i]) |
| 79 | end | 79 | end |
| 80 | return mbox | 80 | return mbox |
