Debian Codegraph @

vicious/contrib/pop_all.lua

raw symbols SCIP
1 -- contrib/pop_all.lua
2 -- Copyright (c) 2010 Boris Bolgradov
3 -- Copyright (C) 2017 Jörg Thalheim <[email protected]>
4 --
5 -- This file is part of Vicious.
6 --
7 -- Vicious is free software: you can redistribute it and/or modify
8 -- it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation, either version 2 of the
10 -- License, or (at your option) any later version.
11 --
12 -- Vicious is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
16 --
17 -- You should have received a copy of the GNU General Public License
18 -- along with Vicious. If not, see <https://www.gnu.org/licenses/>.
19
20 ---------------------------------------------------
21 -- This widget type depends on luasocket.
22 --
23 -- Widget arguments are host, port, username and
24 -- password, i.e.:
25 -- {"mail.myhost.com", 110, "John", "132435"}
26 ---------------------------------------------------
27
28 -- {{{ Grab environment
29 local tonumber = tonumber
30 local setmetatable = setmetatable
31 local sock_avail, socket = pcall(function()
32 return require("socket")
33 end)
34 -- }}}
35
36
37 -- POP: provides the count of new messages in a POP3 mailbox
38 -- vicious.contrib.pop
39 local pop_all = {}
40
41
42 -- {{{ POP3 count widget type
43 local function worker(format, warg)
44 if not sock_avail or (not warg or #warg ~= 4) then
45 return {"N/A"}
46 end
47
48 local host, port = warg[1], tonumber(warg[2])
49 local user, pass = warg[3], warg[4]
50
51 local client = socket.tcp()
52 client:settimeout(3)
53 client:connect(host, port)
54 client:receive("*l")
55 client:send("USER " .. user .. "\r\n")
56 client:receive("*l")
57 client:send("PASS " .. pass .. "\r\n")
58 client:receive("*l")
59 client:send("STAT" .. "\r\n")
60 local response = client:receive("*l")
61 client:close()
62
63 if response:find("%+OK") then
64 response = response:match("%+OK (%d+)")
65 end
66
67 return {response}
68 end
69 -- }}}
70
71 return setmetatable(pop_all, { __call = function(_, ...) return worker(...) end })