Rework LuCI build system
[project/luci.git] / applications / luci-app-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 interface = s:option(ListValue, "interface", translate("Event interface"), translate("Network on which the ddns-updater scripts will be started"))
30 luci.tools.webadmin.cbi_add_networks(interface)
31 interface.default = "wan"
32
33 svc = s:option(ListValue, "service_name", translate("Service"))
34 svc.rmempty = false
35 svc.default = "dyndns.org"
36
37 local services = { }
38 local fd = io.open("/usr/lib/ddns/services", "r")
39 if fd then
40         local ln
41         repeat
42                 ln = fd:read("*l")
43                 local s = ln and ln:match('^%s*"([^"]+)"')
44                 if s then services[#services+1] = s end
45         until not ln
46         fd:close()
47 end
48
49 local v
50 for _, v in luci.util.vspairs(services) do
51         svc:value(v)
52 end
53
54 function svc.cfgvalue(...)
55         local v = Value.cfgvalue(...)
56         if not v or #v == 0 then
57                 return "-"
58         else
59                 return v
60         end
61 end
62
63 function svc.write(self, section, value)
64         if value == "-" then
65                 m.uci:delete("ddns", section, self.option)
66         else
67                 Value.write(self, section, value)
68         end
69 end
70
71 svc:value("-", "-- "..translate("custom").." --")
72
73 local url = s:option(Value, "update_url", translate("Custom update-URL"))
74 url:depends("service_name", "-")
75 url.rmempty = true
76
77 local hostname = s:option(Value, "domain", translate("Hostname"))
78 hostname.rmempty = true
79 hostname.default = "mypersonaldomain.dyndns.org"
80 hostname.datatype = "host"
81
82 local username = s:option(Value, "username", translate("Username"))
83 username.rmempty = true
84
85 local pw = s:option(Value, "password", translate("Password"))
86 pw.rmempty = true
87 pw.password = true
88
89 require("luci.tools.webadmin")
90
91 local src = s:option(ListValue, "ip_source",
92         translate("Source of IP address"))
93 src.default = "network"
94 src:value("network", translate("network"))
95 src:value("interface", translate("interface"))
96 src:value("web", translate("URL"))
97
98 local iface = s:option(ListValue, "ip_network", translate("Network"))
99 iface:depends("ip_source", "network")
100 iface.rmempty = true
101 iface.default = "wan"
102 luci.tools.webadmin.cbi_add_networks(iface)
103 iface = s:option(ListValue, "ip_interface", translate("Interface"))
104 iface:depends("ip_source", "interface")
105 iface.rmempty = true
106 for k, v in pairs(luci.sys.net.devices()) do
107         iface:value(v)
108 end
109
110 local web = s:option(Value, "ip_url", translate("URL"))
111 web:depends("ip_source", "web")
112 web.default = "http://checkip.dyndns.com/"
113 web.rmempty = true
114
115
116 local ci = s:option(Value, "check_interval", translate("Check for changed IP every")) 
117 ci.datatype = "and(uinteger,min(1))" 
118 ci.default = 10 
119
120 local unit = s:option(ListValue, "check_unit", translate("Check-time unit"))
121 unit.default = "minutes"
122 unit:value("minutes", translate("min"))
123 unit:value("hours", translate("h"))
124
125 fi = s:option(Value, "force_interval", translate("Force update every")) 
126 fi.datatype = "and(uinteger,min(1))" 
127 fi.default = 72 
128
129 local unit = s:option(ListValue, "force_unit", translate("Force-time unit"))
130 unit.default = "hours"
131 unit:value("minutes", translate("min"))
132 unit:value("hours", translate("h"))
133
134
135 return m