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