proto: fix feature detection of installed protocol handlers
[project/luci.git] / protocols / ppp / luasrc / model / network / proto_ppp.lua
1 --[[
2 LuCI - Network model - 3G, PPP, PPtP, PPPoE and PPPoA protocol extension
3
4 Copyright 2011 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 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 ]]--
19
20 local netmod = luci.model.network
21
22 local _, p
23 for _, p in ipairs({"ppp", "pptp", "pppoe", "pppoa", "3g"}) do
24
25         local proto = netmod:register_protocol(p)
26
27         function proto.get_i18n(self)
28                 if p == "ppp" then
29                         return luci.i18n.translate("PPP")
30                 elseif p == "pptp" then
31                         return luci.i18n.translate("PPtP")
32                 elseif p == "3g" then
33                         return luci.i18n.translate("UMTS/GPRS/EV-DO")
34                 elseif p == "pppoe" then
35                         return luci.i18n.translate("PPPoE")
36                 elseif p == "pppoa" then
37                         return luci.i18n.translate("PPPoATM")
38                 end
39         end
40
41         function proto.ifname(self)
42                 return p .. "-" .. self.sid
43         end
44
45         function proto.opkg_package(self)
46                 if p == "ppp" or p == "pptp" then
47                         return p
48                 elseif p == "3g" then
49                         return "comgt"
50                 elseif p == "pppoe" then
51                         return "ppp-mod-pppoe"
52                 elseif p == "pppoa" then
53                         return "ppp-mod-pppoa"
54                 end
55         end
56
57         function proto.is_installed(self)
58                 if p == "pppoa" then
59                         return (nixio.fs.glob("/usr/lib/pppd/*/pppoatm.so")() ~= nil)
60                 elseif p == "pppoe" then
61                         return (nixio.fs.glob("/usr/lib/pppd/*/rp-pppoe.so")() ~= nil)
62                 elseif p == "3g" then
63                         return nixio.fs.access("/lib/netifd/proto/3g.sh")
64                 else
65                         return nixio.fs.access("/lib/netifd/proto/ppp.sh")
66                 end
67         end
68
69         function proto.is_floating(self)
70                 return (p ~= "pppoe")
71         end
72
73         function proto.is_virtual(self)
74                 return true
75         end
76
77         function proto.get_interfaces(self)
78                 if self:is_floating() then
79                         return nil
80                 else
81                         return netmod.protocol.get_interfaces(self)
82                 end
83         end
84
85         function proto.contains_interface(self, ifc)
86                 if self:is_floating() then
87                         return (netmod:ifnameof(ifc) == self:ifname())
88                 else
89                         return netmod.protocol.contains_interface(self, ifc)
90                 end
91         end
92
93         netmod:register_pattern_virtual("^%s-%%w" % p)
94 end