Debian Codegraph @

vicious/contrib/wpa_all.lua

raw symbols SCIP
1 -- contrib/wpa_all.lua
2 -- Copyright (C) 2012 jinleileiking <[email protected]>
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 -- {{{ Grab environment
21 local tonumber = tonumber
22 local math = { ceil = math.ceil }
23 local setmetatable = setmetatable
24 local helpers = require("vicious.helpers")
25 local io = {
26 open = io.open,
27 popen = io.popen
28 }
29 local string = {
30 find = string.find,
31 match = string.match
32 }
33 -- }}}
34
35
36 -- Wifi: provides wireless information for a requested interface
37 local wpa_all = {}
38
39 local info = {
40 ["{ssid}"] = "N/A",
41 ["{bssid}"] = "N/A",
42 ["{ip}"] = "N/A",
43 ["{qual}"] = "N/A",
44 }
45
46 -- {{{ Wireless widget type
47 local function worker(format, warg)
48 if not warg then return info end
49
50 local wpa_cmd = "wpa_cli -i'" .. warg .. "' status 2>&1"
51 local f = io.popen(wpa_cmd)
52 local output = f:read("*all")
53 f:close()
54
55 if not output then return info end
56
57 state = string.match(output, 'wpa_state=([%a]+)') or 'N/A'
58 info["{bssid}"] = string.match(output, 'bssid=([%d%a:]+)') or 'N/A'
59 info["{ssid}"] = string.match(output, 'ssid=([%a]+)') or 'N/A'
60 info["{ip}"] = string.match(output, 'ip_address=([%d.]+)') or 'N/A'
61
62 if not state == 'COMPLETED' then
63 return info
64 end
65
66 local wpa_cmd = "wpa_cli -i'" .. warg .. "' bss " .. bssid .. " 2>&1"
67 local f = io.popen(wpa_cmd)
68 local output = f:read("*all")
69 f:close()
70
71 if not output then return info end
72
73 info["{qual}"] = string.match(output, 'qual=([%d]+)')
74
75 return info
76 end
77 -- }}}
78
79 return setmetatable(wpa_all, { __call = function(_, ...) return worker(...) end })