reworked to remove awk and grep calls
[wmiirc-lua.git] / plugins / network.lua
blobc0a40d79a3781f8cda5e6ac0e4b403286eb8d18f
1 --[[
2 =pod
4 =head1 NAME
6 network.lua - wmiirc-lua plugin for monitoring network interfaces
8 =head1 SYNOPSIS
10 -- in your wmiirc.lua:
11 wmii.load_plugin("network")
14 =head1 DESCRIPTION
16 For the options you can define something like
18 wmii.set_conf("network.interfaces.wired", "eth0")
19 wmii.set_conf("network.interfaces.wireless", "wlan0")
21 which will show informations about the wireless device wlan0 and
22 the non-wireless device eth0
24 =head1 SEE ALSO
26 L<wmii(1)>, L<lua(1)>
28 =head1 AUTHOR
30 Jan-David Quesel <jdq@gmx.net>
32 =head1 LICENCE AND COPYRIGHT
34 Copyright (c) 2008, Jan-David Quesel <jdq@gmx.net>
36 This is free software. You may redistribute copies of it under the terms of
37 the GNU General Public License L<http://www.gnu.org/licenses/gpl.html>. There
38 is NO WARRANTY, to the extent permitted by law.
40 =cut
41 --]]
43 local wmii = require("wmii")
44 local io = require("io")
45 local string = require("string")
46 local pairs = pairs
48 module("network")
49 api_version = 0.1
51 wmii.set_conf("network.interfaces.wired", "eth0")
52 wmii.set_conf("network.interfaces.wireless", "")
54 local devices = { }
55 -- ------------------------------------------------------------
56 -- MODULE VARIABLES
57 local timer = nil
59 local function _command ( cmd )
61 if (cmd) then
62 wmii.log( "about to run " .. cmd)
63 local file = io.popen( cmd)
64 local status = file:read("*a")
65 file:close()
67 return status
68 --return status:match("[^\n]*")
69 else
70 return ""
71 end
72 end
74 local function create_device_string(device)
75 local ip = "ifconfig " .. device["name"]
76 local ipstr = _command(ip)
77 ipstr = ipstr:gmatch("inet addr:([0-9.]+)")()
78 if ipstr == nil or ipstr == "" then
79 txt = device["name"] .. ": down"
80 else
81 ipstr = ipstr.sub(ipstr, 1, ipstr.len(ipstr))
82 txt = device["name"] .. ": " .. ipstr
83 if device["wireless"] then
84 local ssid = "iwconfig " .. device["name"]
85 local str_ssid = _command(ssid)
86 str_ssid = str_ssid:gmatch("ESSID:\"(.*)\"")()
87 str_ssid = str_ssid.sub(str_ssid, 2, str_ssid.len(str_ssid)-3)
88 txt = txt .. "@(" .. str_ssid .. ")"
89 end
90 end
92 device["widget"]:show(txt)
93 end
96 local function generate_lists()
97 wmii.log("generating interface list")
98 local strings = wmii.get_conf("network.interfaces.wired")
99 for str in strings:gmatch("%w+") do
100 devices[#devices+1] = {
101 name = str,
102 widget = wmii.widget:new ("350_network_" .. str),
103 wireless = false
105 wmii.log("found " .. str)
107 local strings = wmii.get_conf("network.interfaces.wireless")
108 for str in strings:gmatch("%w+") do
109 devices[#devices+1] = {
110 name = str,
111 widget = wmii.widget:new ("350_network_" .. str),
112 wireless = true
114 wmii.log("found " .. str)
119 local function update ()
120 for _,device in pairs(devices) do
121 create_device_string(device)
125 local function network_timer ( timer )
126 if #devices == 0 then
127 generate_lists()
129 update()
130 return 60
134 timer = wmii.timer:new (network_timer, 1)