branches/luci-0.8: remove bitrot
[project/luci.git] / applications / luci-openvpn / luasrc / model / cbi / openvpn.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.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 $Id: openvpn.lua 3551 2008-10-10 11:48:04Z jow $
13 ]]--
14
15 require("luci.fs")
16 require("luci.ip")
17 require("luci.sys")
18 require("luci.model.uci")
19
20
21 local uci = luci.model.uci.cursor()
22
23 local m = Map("openvpn", translate("openvpn"))
24 local s = m:section( TypedSection, "openvpn", translate("openvpn_overview"), translate("openvpn_overview_desc") )
25 s.template = "cbi/tblsection"
26 s.template_addremove = "openvpn/cbi-select-input-add"
27 s.addremove = true
28 s.add_select_options = { }
29 s.extedit = luci.dispatcher.build_url(
30 "admin", "services", "openvpn", "basic", "%s"
31 )
32
33 uci:load("openvpn_recipes")
34 uci:foreach( "openvpn_recipes", "openvpn_recipe",
35 function(section)
36 s.add_select_options[section['.name']] =
37 section['_description'] or section['.name']
38 end
39 )
40
41 function s.parse(self, section)
42 local recipe = luci.http.formvalue(
43 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
44 self.sectiontype .. ".select"
45 )
46
47 if recipe and not s.add_select_options[recipe] then
48 self.invalid_cts = true
49 else
50 TypedSection.parse( self, section )
51 end
52 end
53
54 function s.create(self, name)
55 local recipe = luci.http.formvalue(
56 luci.cbi.CREATE_PREFIX .. self.config .. "." ..
57 self.sectiontype .. ".select"
58 )
59
60 uci:section(
61 "openvpn", "openvpn", name,
62 uci:get_all( "openvpn_recipes", recipe )
63 )
64
65 uci:delete("openvpn", name, "_role")
66 uci:delete("openvpn", name, "_description")
67 uci:save("openvpn")
68
69 luci.http.redirect( self.extedit:format(name) )
70 end
71
72
73 s:option( Flag, "enable", translate("openvpn_enable") )
74
75 local active = s:option( DummyValue, "_active", translate("openvpn_active") )
76 function active.cfgvalue(self, section)
77 if luci.fs.isfile("/var/run/openvpn_%s.pid" % section) then
78 local pid = io.lines("/var/run/openvpn_%s.pid" % section)()
79 if pid and #pid > 0 and tonumber(pid) ~= nil then
80 return (luci.sys.process.signal(pid, 0))
81 and translatef("openvpn_active_yes", pid)
82 or translate("openvpn_active_no")
83 end
84 end
85 return translate("openvpn_active_no")
86 end
87
88 local port = s:option( DummyValue, "port", translate("openvpn_port") )
89 function port.cfgvalue(self, section)
90 local val = AbstractValue.cfgvalue(self, section)
91 return val or "1194"
92 end
93
94 local proto = s:option( DummyValue, "proto", translate("openvpn_proto") )
95 function proto.cfgvalue(self, section)
96 local val = AbstractValue.cfgvalue(self, section)
97 return val or "udp"
98 end
99
100
101 return m