forked from pygy/LuLPeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpop3.lua
256 lines (219 loc) · 6.46 KB
/
pop3.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
-- Taken from https://github.com/moteus/lua-pop3/
-- Copyright (C) 2013 Alexey Melnichuk.
-- See the license at the end of this file.
local lpeg = require(arg[1]):register()
local get_address_list do
-- local function prequire(...)
-- local ok, mod = pcall(require, ...)
-- return ok and mod, mod
-- end
local re = lpeg.re
if re then
local function try_load_get_address_list()
-- @todo unquot quoted name
local mail_pat = re.compile[[
groups <- (group (%s* ([,;] %s*)+ group)*) -> {}
group <- (
{:name: <phrase> :} %s* <addr> /
{:name: <uq_phrase> :} %s* "<" <addr_spec> ">" /
<addr> %s* {:name: <phrase> :} /
"<" <addr_spec> ">" %s* {:name: <uq_phrase> :} /
<addr> /
{:name: <phrase> :}
) -> {}
uq_phrase <- <uq_atom> (%s+ <uq_atom>)*
uq_atom <- [^<>,; ]+
phrase <- <word> ([%s.]+ <word>)* / <quoted_string>
word <- <atom> ! <domain_addr>
atom <- [^] %c()<>@,;:\".[]+
quoted_string <- '"' ([^"\%nl] / "\" .)* '"'
addr <- <addr_spec> / "<" <addr_spec> ">"
addr_spec <- {:addr: <addr_chars> <domain_addr> :}
domain_addr <- "@" <addr_chars>
addr_chars <- [_%a%d][-._%a%d]*
]]
return function(str)
if (not str) or (str == '') then
return nil
end
return mail_pat:match(str)
end
end
local ok, fn = pcall(try_load_get_address_list)
if ok then get_address_list = fn end
if get_address_list then -- test --
local cmp_t
local function cmp_v(v1,v2)
local flag = true
if type(v1) == 'table' then
flag = (type(v2) == 'table') and cmp_t(v1, v2)
else
flag = (v1 == v2)
end
return flag
end
function cmp_t(t1,t2)
for k in pairs(t2)do
if t1[k] == nil then
return false
end
end
for k,v in pairs(t1)do
if not cmp_v(t2[k],v) then
return false
end
end
return true
end
local tests = {}
local tests_index={}
local test = function(str, result)
local t
if type(result) == 'string' then
local res = assert(tests_index[str])
t = {result, result = res.result}
assert(result ~= str)
tests_index[result] = t;
else
t = {str,result=result}
tests_index[str] = t;
end
return table.insert(tests,t)
end
assert(get_address_list() == nil)
assert(get_address_list('') == nil)
test([[[email protected]]],
{{
addr = "[email protected]"
}}
)
test([[[email protected]]],[[<[email protected]>]])
test([["[email protected]"]],
{{
name = '"[email protected]"'
}}
)
test([[Subscriber YPAG.RU <[email protected]>]],
{{
name = "Subscriber YPAG.RU",
addr = "[email protected]"
}}
)
test([[Subscriber YPAG.RU <[email protected]>]], [[<[email protected]> Subscriber YPAG.RU]])
test([["Subscriber YPAG.RU" <[email protected]>]],
{{
name = '"Subscriber YPAG.RU"',
addr = "[email protected]"
}}
)
test([["Subscriber YPAG.RU" <[email protected]>]],[[<[email protected]> "Subscriber YPAG.RU"]])
test([["Subscriber ;,YPAG.RU" <[email protected]>]],
{{
name = '"Subscriber ;,YPAG.RU"',
addr = "[email protected]"
}}
)
test([[Subscriber ;,YPAG.RU <[email protected]>]],
{
{
name = "Subscriber"
},
{
name = "YPAG.RU",
addr = "[email protected]"
}
}
)
test([["Subscriber ;,YPAG.RU" <[email protected]>]],[[<[email protected]> "Subscriber ;,YPAG.RU"]])
{
{
addr = "[email protected]"
},
{
addr = "[email protected]"
},
{
addr = "[email protected]"
}
}
)
test([["[email protected]" <[email protected]>]],
{
{
name = "\"[email protected]\"",
addr = "[email protected]"
}
}
)
test([[[email protected] <[email protected]>]],
{
{
name = "[email protected]",
addr = "[email protected]"
}
}
)
test([[MailList: рассылка номер 78236 <[email protected]>]],
{
{
name = "MailList: рассылка номер 78236",
addr = "[email protected]"
}
}
)
test([[<[email protected]>, "Info Mail List" <[email protected]>, Сакен Матов <[email protected]>, "Evgeny Zhembrovsky \(ezhembro\)" <[email protected]> ]],
{
{
addr = "[email protected]"
},
{
name = "\"Info Mail List\"",
addr = "[email protected]"
},
{
name = "Сакен Матов",
addr = "[email protected]"
},
{
name = "\"Evgeny Zhembrovsky \\(ezhembro\\)\"",
addr = "[email protected]"
}
}
)
for _,test_case in ipairs(tests)do
local res = get_address_list(test_case[1])
if not cmp_v(res, test_case.result ) then
-- require "pprint"
print"----------------------------------------------"
print("ERROR:", test_case[1])
print"EXPECTED:"
-- pprint(test_case.result)
print"RESULT:"
-- pprint(res)
end
end
end -- test --
end -- require "re" --
end -- get_address_list --
-------------------------------------------------------------------------
-- Copyright (C) 2013 Alexey Melnichuk.
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
-- ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
-- TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
-- PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
-- SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
-- ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
-- ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
-- OR OTHER DEALINGS IN THE SOFTWARE.