applications/luci-wol: fix copyright
[project/luci.git] / applications / luci-wol / luasrc / model / cbi / wol.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2010 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11 ]]--
12
13 local sys = require "luci.sys"
14 local fs  = require "nixio.fs"
15
16 m = SimpleForm("wol", translate("Wake on LAN"),
17         translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
18
19 m.submit = translate("Wake up host")
20 m.reset  = false
21
22
23 local has_ewk = fs.access("/usr/bin/etherwake")
24 local has_wol = fs.access("/usr/bin/wol")
25
26 if luci.http.formvalue("cbi.submit") then
27         local host = luci.http.formvalue("cbid.wol.1.mac")
28         if host and #host > 0 then
29                 local cmd
30                 local util = luci.http.formvalue("cbid.wol.1.binary") or (
31                         has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
32                 )
33
34                 if util == "/usr/bin/etherwake" then
35                         local iface = luci.http.formvalue("cbid.wol.1.iface")
36                         cmd = "%s -D%s %q" %{
37                                 util, (iface ~= "" and " -i %q" % iface or ""), host
38                         }
39                 else
40                         cmd = "%s -v %q" %{ util, host }
41                 end
42
43                 is = m:section(SimpleSection)
44
45                 function is.render()
46                         luci.http.write(
47                                 "<p><br /><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
48                                         translate("Starting WoL utility:"), cmd
49                                 }
50                         )
51
52                         local p = io.popen(cmd .. " 2>&1")
53                         if p then
54                                 while true do
55                                         local l = p:read("*l")
56                                         if l then
57                                                 if #l > 100 then l = l:sub(1, 100) .. "..." end
58                                                 luci.http.write(l .. "<br />")
59                                         else
60                                                 break
61                                         end
62                                 end
63                                 p:close()
64                         end
65
66                         luci.http.write("</code><br /></p>")
67                 end
68         end
69 end
70
71
72 s = m:section(SimpleSection)
73
74 local arp = { }
75 local e, ip, mac, name
76
77 if has_ewk and has_wol then
78         bin = s:option(ListValue, "binary", translate("WoL program"),
79                 translate("Sometimes only one of both tools work. If one of fails, try the other one"))
80
81         bin:value("/usr/bin/etherwake", "Etherwake")
82         bin:value("/usr/bin/wol", "WoL")
83 end
84
85 if has_ewk then
86         iface = s:option(ListValue, "iface", translate("Network interface to use"),
87                 translate("Specifies the interface the WoL packet is sent on"))
88
89         if has_wol then
90                 iface:depends("binary", "/usr/bin/etherwake")
91         end
92
93         iface:value("", translate("Broadcast on all interfaces"))
94         
95         for _, e in ipairs(sys.net.devices()) do
96                 if e ~= "lo" then iface:value(e) end
97         end
98 end
99
100
101 for _, e in ipairs(sys.net.arptable()) do
102         arp[e["HW address"]] = { e["IP address"] }
103 end
104
105 for e in io.lines("/etc/ethers") do
106         mac, ip = e:match("^([a-f0-9]%S+) (%S+)")
107         if mac and ip then arp[mac] = { ip } end
108 end
109
110 for e in io.lines("/var/dhcp.leases") do
111         mac, ip, name = e:match("^%d+ (%S+) (%S+) (%S+)")
112         if mac and ip then arp[mac] = { ip, name ~= "*" and name } end
113 end
114
115 host = s:option(Value, "mac", translate("Host to wake up"),
116         translate("Choose the host to wake up or enter a custom MAC address to use"))
117
118 for mac, ip in pairs(arp) do
119         host:value(mac, "%s (%s)" %{ mac, ip[2] or ip[1] })
120 end
121
122
123 return m
124