util-soc-imp-mime.lua /size: 2373 b    last modification: 2020-07-01 14:35
1
-- original file : mime.lua
2
-- for more into : see util-soc.lua
3 4
local
type
,
tostring
=
type
,
tostring
5 6
local
mime
=
mime
or
package
.
loaded
.
mime
or
require
(
"
mime.core
"
)
7
local
ltn12
=
ltn12
or
package
.
loaded
.
ltn12
or
require
(
"
ltn12
"
)
8 9
local
filtercycle
=
ltn12
.
filter
.
cycle
10 11
local
function
report
(
fmt
,
first
,
...
)
12
if
logs
then
13
report
=
logs
and
logs
.
reporter
(
"
mime
"
)
14
report
(
fmt
,
first
,
...
)
15
elseif
fmt
then
16
fmt
=
"
mime:
"
.
.
fmt
17
if
first
then
18
print
(
format
(
fmt
,
first
,
...
)
)
19
else
20
print
(
fmt
)
21
end
22
end
23
end
24 25
mime
.
report
=
report
26 27
local
encodet
=
{
}
28
local
decodet
=
{
}
29
local
wrapt
=
{
}
30 31
mime
.
encodet
=
encodet
32
mime
.
decodet
=
decodet
33
mime
.
wrapt
=
wrapt
34 35
local
mime_b64
=
mime
.
b64
36
local
mime_qp
=
mime
.
qp
37
local
mime_unb64
=
mime
.
unb64
38
local
mime_unqp
=
mime
.
unqp
39
local
mime_wrp
=
mime
.
wrp
40
local
mime_qpwrp
=
mime
.
qpwrp
41
local
mime_eol
=
mime_eol
42
local
mime_dot
=
mime_dot
43 44
encodet
[
'
base64
'
]
=
function
(
)
45
return
filtercycle
(
mime_b64
,
"
"
)
46
end
47 48
encodet
[
'
quoted-printable
'
]
=
function
(
mode
)
49
return
filtercycle
(
mime_qp
,
"
"
,
mode
=
=
"
binary
"
and
"
=0D=0A
"
or
"
\r\n
"
)
50
end
51 52
decodet
[
'
base64
'
]
=
function
(
)
53
return
filtercycle
(
mime_unb64
,
"
"
)
54
end
55 56
decodet
[
'
quoted-printable
'
]
=
function
(
)
57
return
filtercycle
(
mime_unqp
,
"
"
)
58
end
59 60
local
wraptext
=
function
(
length
)
61
if
not
length
then
62
length
=
76
63
end
64
return
filtercycle
(
mime_wrp
,
length
,
length
)
65
end
66 67
local
wrapquoted
=
function
(
)
68
return
filtercycle
(
mime_qpwrp
,
76
,
76
)
69
end
70 71
wrapt
[
'
text
'
]
=
wraptext
72
wrapt
[
'
base64
'
]
=
wraptext
73
wrapt
[
'
default
'
]
=
wraptext
74
wrapt
[
'
quoted-printable
'
]
=
wrapquoted
75 76
function
mime
.
normalize
(
marker
)
77
return
filtercycle
(
mime_eol
,
0
,
marker
)
78
end
79 80
function
mime
.
stuff
(
)
81
return
filtercycle
(
mime_dot
,
2
)
82
end
83 84
local
function
choose
(
list
)
85
return
function
(
name
,
opt1
,
opt2
)
86
if
type
(
name
)
~
=
"
string
"
then
87
name
,
opt1
,
opt2
=
"
default
"
,
name
,
opt1
88
end
89
local
filter
=
list
[
name
or
"
nil
"
]
90
if
filter
then
91
return
filter
(
opt1
,
opt2
)
92
else
93
report
(
"
error: unknown key '%s'
"
,
tostring
(
name
)
)
94
end
95
end
96
end
97 98
mime
.
encode
=
choose
(
encodet
)
99
mime
.
decode
=
choose
(
decodet
)
100
mime
.
wrap
=
choose
(
wrapt
)
101 102
package
.
loaded
[
"
mime
"
]
=
mime
103 104
return
mime
105