Merge pull request #1654 from TDT-AG/pr/20180301-luci-several-fixes
[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 ipc = require "luci.ip"
6 local fs  = require "nixio.fs"
7
8 m = SimpleForm("wol", translate("Wake on LAN"),
9         translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
10
11 m.submit = translate("Wake up host")
12 m.reset  = false
13
14
15 local has_ewk = fs.access("/usr/bin/etherwake")
16 local has_wol = fs.access("/usr/bin/wol")
17
18
19 s = m:section(SimpleSection)
20
21 if has_ewk and has_wol then
22         bin = s:option(ListValue, "binary", translate("WoL program"),
23                 translate("Sometimes only one of the two tools works. If one fails, try the other one"))
24
25         bin:value("/usr/bin/etherwake", "Etherwake")
26         bin:value("/usr/bin/wol", "WoL")
27 end
28
29 if has_ewk then
30         iface = s:option(ListValue, "iface", translate("Network interface to use"),
31                 translate("Specifies the interface the WoL packet is sent on"))
32
33         if has_wol then
34                 iface:depends("binary", "/usr/bin/etherwake")
35         end
36
37         iface:value("", translate("Broadcast on all interfaces"))
38
39         for _, e in ipairs(sys.net.devices()) do
40                 if e ~= "lo" then iface:value(e) end
41         end
42 end
43
44
45 host = s:option(Value, "mac", translate("Host to wake up"),
46         translate("Choose the host to wake up or enter a custom MAC address to use"))
47
48 sys.net.mac_hints(function(mac, name)
49         host:value(mac, "%s (%s)" %{ mac, name })
50 end)
51
52 if has_ewk then
53         broadcast = s:option(Flag, "broadcast",
54                 translate("Send to broadcast address"))
55         if has_wol then
56                 broadcast:depends("binary", "/usr/bin/etherwake")
57         end
58 end
59
60 function host.write(self, s, val)
61         local host = luci.http.formvalue("cbid.wol.1.mac")
62         local mac = ipc.checkmac(host)
63         if mac then
64                 local cmd
65                 local util = luci.http.formvalue("cbid.wol.1.binary") or (
66                         has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
67                 )
68
69                 if util == "/usr/bin/etherwake" then
70                         local iface = luci.http.formvalue("cbid.wol.1.iface")
71                         local broadcast = luci.http.formvalue("cbid.wol.1.broadcast")
72                         cmd = "%s -D%s %s %q" %{
73                                 util, (iface ~= "" and " -i %q" % iface or ""),
74                                 (broadcast == "1" and " -b" or ""), mac
75                         }
76                 else
77                         cmd = "%s -v %q" %{ util, mac }
78                 end
79
80                 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
81                         translate("Starting WoL utility:"), cmd
82                 }
83
84                 local p = io.popen(cmd .. " 2>&1")
85                 if p then
86                         while true do
87                                 local l = p:read("*l")
88                                 if l then
89                                         if #l > 100 then l = l:sub(1, 100) .. "..." end
90                                         msg = msg .. l .. "<br />"
91                                 else
92                                         break
93                                 end
94                         end
95                         p:close()
96                 end
97
98                 msg = msg .. "</code></p>"
99
100                 m.message = msg
101         end
102 end
103
104
105 return m