fix client_focused()
[wmiirc-lua.git] / plugins / network.lua
blob3ccc93573984ada3b2ce17c74e08c1646a9ad850
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 txt = txt .. "@(" .. str_ssid .. ")"
88 end
89 end
91 device["widget"]:show(txt)
92 end
95 local function generate_lists()
96 wmii.log("generating interface list")
97 local strings = wmii.get_conf("network.interfaces.wired")
98 for str in strings:gmatch("%w+") do
99 devices[#devices+1] = {
100 name = str,
101 widget = wmii.widget:new ("350_network_" .. str),
102 wireless = false
104 wmii.log("found " .. str)
106 local strings = wmii.get_conf("network.interfaces.wireless")
107 for str in strings:gmatch("%w+") do
108 devices[#devices+1] = {
109 name = str,
110 widget = wmii.widget:new ("350_network_" .. str),
111 wireless = true
113 wmii.log("found " .. str)
118 local function update ()
119 for _,device in pairs(devices) do
120 create_device_string(device)
124 local function network_timer ( timer )
125 if #devices == 0 then
126 generate_lists()
128 update()
129 return 60
133 timer = wmii.timer:new (network_timer, 1)