merge branch changes into trunk
[project/luci.git] / applications / luci-ddns / luasrc / model / cbi / ddns / ddns.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11         http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 require("luci.tools.webadmin")
17
18 local is_mini = (luci.dispatcher.context.path[1] == "mini")
19
20
21 m = Map("ddns", translate("Dynamic DNS"),
22         translate("Dynamic DNS allows that your router can be reached with " ..
23                 "a fixed hostname while having a dynamically changing " ..
24                 "IP address."))
25
26 s = m:section(TypedSection, "service", "")
27 s.addremove = true
28 s.anonymous = false
29
30 s:option(Flag, "enabled", translate("Enable"))
31
32 interface = s:option(ListValue, "interface", translate("Event interface"), translate("On which interface up should start the ddns script process."))
33 luci.tools.webadmin.cbi_add_networks(interface)
34 interface.default = "wan"
35
36 svc = s:option(ListValue, "service_name", translate("Service"))
37 svc.rmempty = false
38
39 local services = { }
40 local fd = io.open("/usr/lib/ddns/services", "r")
41 if fd then
42         local ln
43         repeat
44                 ln = fd:read("*l")
45                 local s = ln and ln:match('^%s*"([^"]+)"')
46                 if s then services[#services+1] = s end
47         until not ln
48         fd:close()
49 end
50
51 local v
52 for _, v in luci.util.vspairs(services) do
53         svc:value(v)
54 end
55
56 function svc.cfgvalue(...)
57         local v = Value.cfgvalue(...)
58         if not v or #v == 0 then
59                 return "-"
60         else
61                 return v
62         end
63 end
64
65 function svc.write(self, section, value)
66         if value == "-" then
67                 m.uci:delete("ddns", section, self.option)
68         else
69                 Value.write(self, section, value)
70         end
71 end
72
73 svc:value("-", "-- "..translate("custom").." --")
74
75 url = s:option(Value, "update_url", translate("Custom update-URL"))
76 url:depends("service_name", "-")
77 url.rmempty = true
78
79 s:option(Value, "domain", translate("Hostname")).rmempty = true
80 s:option(Value, "username", translate("Username")).rmempty = true
81 pw = s:option(Value, "password", translate("Password"))
82 pw.rmempty = true
83 pw.password = true
84
85
86 if is_mini then
87         s.defaults.ip_source = "network"
88         s.defaults.ip_network = "wan"
89 else
90
91         src = s:option(ListValue, "ip_source",
92                 translate("Source of IP address"))
93         src:value("network", translate("network"))
94         src:value("interface", translate("interface"))
95         src:value("web", translate("URL"))
96
97         iface = s:option(ListValue, "ip_network", translate("Network"))
98         iface:depends("ip_source", "network")
99         iface.rmempty = true
100         luci.tools.webadmin.cbi_add_networks(iface)
101
102         iface = s:option(ListValue, "ip_interface", translate("Interface"))
103         iface:depends("ip_source", "interface")
104         iface.rmempty = true
105         for k, v in pairs(luci.sys.net.devices()) do
106                 iface:value(v)
107         end
108
109         web = s:option(Value, "ip_url", translate("URL"))
110         web:depends("ip_source", "web")
111         web.rmempty = true
112 end
113
114
115 s:option(Value, "check_interval",
116         translate("Check for changed IP every")).default = 10
117 unit = s:option(ListValue, "check_unit", translate("Check-time unit"))
118 unit.default = "minutes"
119 unit:value("minutes", translate("min"))
120 unit:value("hours", translate("h"))
121
122 s:option(Value, "force_interval", translate("Force update every")).default = 72
123 unit = s:option(ListValue, "force_unit", translate("Force-time unit"))
124 unit.default = "hours"
125 unit:value("minutes", translate("min"))
126 unit:value("hours", translate("h"))
127
128
129 return m