Update my email addresses in the license headers
[project/luci.git] / applications / luci-app-wol / luasrc / model / cbi / wol.lua
1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local sys = require "luci.sys"
5 local fs = require "nixio.fs"
6
7 m = SimpleForm("wol", translate("Wake on LAN"),
8 translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
9
10 m.submit = translate("Wake up host")
11 m.reset = false
12
13
14 local has_ewk = fs.access("/usr/bin/etherwake")
15 local has_wol = fs.access("/usr/bin/wol")
16
17
18 s = m:section(SimpleSection)
19
20 if has_ewk and has_wol then
21 bin = s:option(ListValue, "binary", translate("WoL program"),
22 translate("Sometimes only one of the two tools works. If one fails, try the other one"))
23
24 bin:value("/usr/bin/etherwake", "Etherwake")
25 bin:value("/usr/bin/wol", "WoL")
26 end
27
28 if has_ewk then
29 iface = s:option(ListValue, "iface", translate("Network interface to use"),
30 translate("Specifies the interface the WoL packet is sent on"))
31
32 if has_wol then
33 iface:depends("binary", "/usr/bin/etherwake")
34 end
35
36 iface:value("", translate("Broadcast on all interfaces"))
37
38 for _, e in ipairs(sys.net.devices()) do
39 if e ~= "lo" then iface:value(e) end
40 end
41 end
42
43
44 host = s:option(Value, "mac", translate("Host to wake up"),
45 translate("Choose the host to wake up or enter a custom MAC address to use"))
46
47 sys.net.mac_hints(function(mac, name)
48 host:value(mac, "%s (%s)" %{ mac, name })
49 end)
50
51
52 function host.write(self, s, val)
53 local host = luci.http.formvalue("cbid.wol.1.mac")
54 if host and #host > 0 and host:match("^[a-fA-F0-9:]+$") then
55 local cmd
56 local util = luci.http.formvalue("cbid.wol.1.binary") or (
57 has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
58 )
59
60 if util == "/usr/bin/etherwake" then
61 local iface = luci.http.formvalue("cbid.wol.1.iface")
62 cmd = "%s -D%s %q" %{
63 util, (iface ~= "" and " -i %q" % iface or ""), host
64 }
65 else
66 cmd = "%s -v %q" %{ util, host }
67 end
68
69 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
70 translate("Starting WoL utility:"), cmd
71 }
72
73 local p = io.popen(cmd .. " 2>&1")
74 if p then
75 while true do
76 local l = p:read("*l")
77 if l then
78 if #l > 100 then l = l:sub(1, 100) .. "..." end
79 msg = msg .. l .. "<br />"
80 else
81 break
82 end
83 end
84 p:close()
85 end
86
87 msg = msg .. "</code></p>"
88
89 m.message = msg
90 end
91 end
92
93
94 return m