Mercurial > luasocket
comparison src/mime.lua @ 0:4b915342e2a8
LuaSocket 2.0.2 + CMake build description.
author | Eric Wing <ewing . public |-at-| gmail . com> |
---|---|
date | Tue, 26 Aug 2008 18:40:01 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4b915342e2a8 |
---|---|
1 ----------------------------------------------------------------------------- | |
2 -- MIME support for the Lua language. | |
3 -- Author: Diego Nehab | |
4 -- Conforming to RFCs 2045-2049 | |
5 -- RCS ID: $Id: mime.lua,v 1.29 2007/06/11 23:44:54 diego Exp $ | |
6 ----------------------------------------------------------------------------- | |
7 | |
8 ----------------------------------------------------------------------------- | |
9 -- Declare module and import dependencies | |
10 ----------------------------------------------------------------------------- | |
11 local base = _G | |
12 local ltn12 = require("ltn12") | |
13 local mime = require("mime.core") | |
14 local io = require("io") | |
15 local string = require("string") | |
16 module("mime") | |
17 | |
18 -- encode, decode and wrap algorithm tables | |
19 encodet = {} | |
20 decodet = {} | |
21 wrapt = {} | |
22 | |
23 -- creates a function that chooses a filter by name from a given table | |
24 local function choose(table) | |
25 return function(name, opt1, opt2) | |
26 if base.type(name) ~= "string" then | |
27 name, opt1, opt2 = "default", name, opt1 | |
28 end | |
29 local f = table[name or "nil"] | |
30 if not f then | |
31 base.error("unknown key (" .. base.tostring(name) .. ")", 3) | |
32 else return f(opt1, opt2) end | |
33 end | |
34 end | |
35 | |
36 -- define the encoding filters | |
37 encodet['base64'] = function() | |
38 return ltn12.filter.cycle(b64, "") | |
39 end | |
40 | |
41 encodet['quoted-printable'] = function(mode) | |
42 return ltn12.filter.cycle(qp, "", | |
43 (mode == "binary") and "=0D=0A" or "\r\n") | |
44 end | |
45 | |
46 -- define the decoding filters | |
47 decodet['base64'] = function() | |
48 return ltn12.filter.cycle(unb64, "") | |
49 end | |
50 | |
51 decodet['quoted-printable'] = function() | |
52 return ltn12.filter.cycle(unqp, "") | |
53 end | |
54 | |
55 local function format(chunk) | |
56 if chunk then | |
57 if chunk == "" then return "''" | |
58 else return string.len(chunk) end | |
59 else return "nil" end | |
60 end | |
61 | |
62 -- define the line-wrap filters | |
63 wrapt['text'] = function(length) | |
64 length = length or 76 | |
65 return ltn12.filter.cycle(wrp, length, length) | |
66 end | |
67 wrapt['base64'] = wrapt['text'] | |
68 wrapt['default'] = wrapt['text'] | |
69 | |
70 wrapt['quoted-printable'] = function() | |
71 return ltn12.filter.cycle(qpwrp, 76, 76) | |
72 end | |
73 | |
74 -- function that choose the encoding, decoding or wrap algorithm | |
75 encode = choose(encodet) | |
76 decode = choose(decodet) | |
77 wrap = choose(wrapt) | |
78 | |
79 -- define the end-of-line normalization filter | |
80 function normalize(marker) | |
81 return ltn12.filter.cycle(eol, 0, marker) | |
82 end | |
83 | |
84 -- high level stuffing filter | |
85 function stuff() | |
86 return ltn12.filter.cycle(dot, 2) | |
87 end |