Skip to content

Commit 961bdad

Browse files
committed
Merge pull request #34 from moteus/master
Add. pop3 example [ci skip]
2 parents 76336c5 + eff44c9 commit 961bdad

File tree

1 file changed

+165
-0
lines changed

1 file changed

+165
-0
lines changed

examples/cURLv3/pop3.lua

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
-- Simple pop3 wrapper
2+
--
3+
-- @usage
4+
-- local mbox = pop3:new('pop3://pop3.yandex.ru')
5+
--
6+
-- -- Yandex works only with tls
7+
-- print('Open: ', mbox:open_tls('***', '***'))
8+
-- print('NOOP: ', mbox:noop())
9+
-- print('RETR: ', mbox:retr(1))
10+
--
11+
-- list = mbox:list()
12+
-- for no, size in ipairs(list)do
13+
-- ...
14+
-- end
15+
--
16+
-- mbox:close()
17+
--
18+
19+
local cURL = require "cURL.safe"
20+
21+
local find_ca_bundle = require "cURL.utils".find_ca_bundle
22+
23+
local function split(str, sep, plain)
24+
local b, res = 1, {}
25+
while b <= #str do
26+
local e, e2 = string.find(str, sep, b, plain)
27+
if e then
28+
table.insert(res, (string.sub(str, b, e-1)))
29+
b = e2 + 1
30+
else
31+
table.insert(res, (string.sub(str, b)))
32+
break
33+
end
34+
end
35+
return res
36+
end
37+
38+
local crln = '\r\n'
39+
local function writer(cb, ctx)
40+
local tail
41+
return function(str)
42+
if str then
43+
local t = split(tail and (tail .. str) or str, crln, true)
44+
if str:sub(-2) == crln then tail = nil
45+
else tail = table.remove(t) end
46+
for _, s in ipairs(t) do cb(ctx, s) end
47+
elseif tail then cb(ctx, tail) end
48+
end
49+
end
50+
51+
local pop3 = {} do
52+
pop3.__index = pop3
53+
54+
function pop3:new(host)
55+
return setmetatable({
56+
_url = assert(host)
57+
},self)
58+
end
59+
60+
local function open(self, user, password, ssl)
61+
self._easy, err = cURL.easy{
62+
url = self._url,
63+
username = user,
64+
password = password,
65+
customrequest = 'NOOP',
66+
nobody = true,
67+
}
68+
if not self._easy then return nil, err end
69+
70+
if ssl then
71+
-- For AVAST
72+
-- http://www.avast.com/en-eu/faq.php?article=AVKB91#artTitle
73+
local cainfo, capath = find_ca_bundle('MailShield.crt')
74+
if not cainfo then
75+
-- On Windows try find ca_bundle
76+
cainfo, capath = find_ca_bundle()
77+
end
78+
local ok, err = self._easy:setopt{
79+
use_ssl = cURL.USESSL_ALL,
80+
cainfo = cainfo,
81+
capath = capath,
82+
}
83+
if not ok then
84+
self:close()
85+
return nil, err
86+
end
87+
end
88+
89+
local ok, err = self._easy:perform()
90+
if not ok then
91+
self:close()
92+
return nil, err
93+
end
94+
95+
return self
96+
end
97+
98+
function pop3:open(user, password)
99+
return open(self, user, password, false)
100+
end
101+
102+
function pop3:open_tls(user, password)
103+
return open(self, user, password, true)
104+
end
105+
106+
function pop3:noop()
107+
local ok, err = self._easy:setopt{
108+
url = self._url,
109+
customrequest = 'NOOP',
110+
nobody = true,
111+
}
112+
if not ok then return nil, err end
113+
ok, err = self._easy:perform()
114+
if not ok then return nil, err end
115+
return self
116+
end
117+
118+
function pop3:list()
119+
local t = {}
120+
local ok, err = self._easy:setopt{
121+
url = self._url,
122+
customrequest = '',
123+
nobody = false,
124+
writefunction = writer(function(t, s) t[#t+1] = s end, t)
125+
}
126+
if not ok then return nil, err end
127+
128+
ok, err = self._easy:perform()
129+
130+
if not ok then return nil, err end
131+
return t
132+
end
133+
134+
function pop3:retr(n)
135+
local t = {}
136+
local ok, err = self._easy:setopt{
137+
url = self._url .. '/' .. n,
138+
customrequest = '',
139+
nobody = false,
140+
writefunction = writer(function(t, s) t[#t+1] = s end, t)
141+
}
142+
if not ok then return nil, err end
143+
144+
ok, err = self._easy:perform()
145+
146+
if not ok then return nil, err end
147+
return t
148+
end
149+
150+
function pop3:closed()
151+
return not not self._easy
152+
end
153+
154+
function pop3:close()
155+
if self._easy then
156+
self._easy:close()
157+
self._easy = false
158+
end
159+
end
160+
161+
end
162+
163+
return {
164+
new = function(...) return pop3:new() end;
165+
}

0 commit comments

Comments
 (0)