summaryrefslogtreecommitdiffstats
path: root/config/templates/linksysata-template.lua
blob: f125a97cd0370343cc5de79b0894e1f96801e12a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<%
-- Linksys ATA Configuration File Template
local values = ...

--[[ -- Parameters
values["device"]["adminpassword"] = ""
values["device"]["digitmap"] = ""
values["device"]["mac"] = ""
values["device"]["musiconhold"] = ""
values["device"]["registrar"] = ""
values["device"]["sntpserver"] = ""
values["device"]["template"] = "/etc/provisioning/templates/linksysata-template.lua"
values["device"]["timezone"] = ""
values["regX"]["callerid"] = ""
values["regX"]["extension"] = ""
values["regX"]["forwardall"] = ""
values["regX"]["forwardallenable"] = false
values["regX"]["forwardbusy"] = ""
values["regX"]["forwardbusyenable"] = false
values["regX"]["forwardnoanswer"] = ""
values["regX"]["forwardnoanswerenable"] = false
values["regX"]["password"] = ""
-- Unimplemented -- values["services"]["callhistoryenable"] = true
values["services"]["callwaitingenable"] = true
values["services"]["forwarding"] = true
values["services"]["hotlinedestination"] = ""
values["services"]["hotlineenable"] = false
-- Unimplemented -- values["services"]["mailbox"] = ""
-- Unimplemented -- values["services"]["speeddialenable"] = true
--]]

local function yesno ( bool )
	if bool then
		return "Yes"
	else
		return "No"
	end
end

local function xml_elem(elem,value,permissions,i)
	local e = elem
	if i then e = elem.."_"..tostring(i).."_" end
	local output = {"<", e, ' ua="'}
	output[#output+1] = (permissions or "na")..'">'
	if type(value) == "boolean" then
		output[#output+1] = yesno(value)
	elseif value then
		output[#output+1] = value
	end
	output[#output+1] = "</"..e..">"
	io.write(table.concat(output).."\n")
end
%>
<?xml version="1.0" encoding="utf-8"?>

<flat-profile>

<%
xml_elem("Admin_Passwd", values.device.adminpassword, "na")
xml_elem("User_Password", values.device.adminpassword, "na")
xml_elem("HostName", values.reg1.extension, "na")
xml_elem("Primary_NTP_Server", values.device.sntpserver, "na")
xml_elem("CW_Setting", (values.services and values.services.callwaitingenable), "na")

-- Handle Time Zone parameter
--   See http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
--     TZ variable, with no leading colon
--   Syntax: stdoffset[dst[offset][,start[/time],end[/time]]]
--   Examples: 'GMT0' 'EST5EDT,M3.2.0,M11.1.0' '<GMT+5>5'
--  Parse time zone variable
require('posixtz')
local tz = posixtz.parse(values.device.timezone)
if tz then
	-- convert POSIX sign (W of GMT is '+') to Linksys (E of GMT is '+')
	if tonumber(tz.offset.hour) == 0 then
		xml_elem("Time_Zone", "GMT", "na")
	else
		if tz.offset.sign == "-" then
			tz.offset.sign = "+"
		else
			tz.offset.sign = "-"
		end
		xml_elem("Time_Zone", "GMT"..tz.offset.sign..string.format("%02i", tonumber(tz.offset.hour or 0))..":00", "na")
	end

	local function dstrule(t)
		local date, time

		if t.day then
			date = t.month.."/"..t.day.."/0"
		else
			-- POSIX weekday is between 0 (Sun) and 6 (Sat)
			-- Linksys weekday is between 1=Mon, 7=Sun
			if tonumber(t.weekday) == 0 then
				t.weekday = 7
			end
			-- POSIX week is between 1 and 5, where 1st,2nd..4th, and 5=last
			-- Linksys is by day
			if tonumber(t.week) == 5 then
				t.week = -1
			else
				t.week = (t.week * 7)-6
			end
			date = t.month.."/"..t.week.."/"..t.weekday
		end


		-- Handle explicit hour for DST change (posixtz only handles hour for now)
		if t.hour then
			time = string.format("%02i", t.hour)..":00:00"
		else
			time = "2:00:00"
		end

		return date.."/"..time
	end
	
	if tz.dst then
		xml_elem("Daylight_Saving_Time_Enable", "Yes", "na")
		xml_elem("Daylight_Saving_Time_Rule", "start="..dstrule(tz.dst.start)..";end="..dstrule(tz.dst.stop)..";save=1", "na")
	else
		xml_elem("Daylight_Saving_Time_Enable", "No", "na")
	end
end

for pg, pg_t in pairs(values) do
	-- Is it of the form regX ?
	local num = string.match(pg, 'reg(%d+)')
	if num then
		if pg_t.extension ~= "" then
			xml_elem("Line_Enable", "Yes", "na", num)
			xml_elem("MOH_Server", values.device.musiconhold, "na", num)
			xml_elem("Proxy", values.device.registrar, "na", num)
			xml_elem("Call_Waiting_Serv", (values.services and values.services.callwaitingenable), "na", num)
			if values.services and values.services.hotlineenable then
				-- Tested and found delay of 0 didn't work, so using 1 second
				-- Need to escape < and >
				xml_elem("Dial_Plan", "( P1&lt;:"..values.services.hotlinedestination.."&gt; )", "na", num)
			else
				xml_elem("Dial_Plan", "( "..values.device.digitmap.." )", "na", num)
			end

			if pg_t.callerid == "" then
				pg_t.callerid = pg_t.extension
			end
			xml_elem("Display_Name", pg_t.callerid, "na", num)
			xml_elem("User_ID", pg_t.extension, "na", num)
			xml_elem("Password", pg_t.password, "na", num)

			local forwarding = {	{param="forwardall", xml="Cfwd_All_"},
						{param="forwardbusy", xml="Cfwd_Busy_"},
						{param="forwardnoanswer", xml="Cfwd_No_Ans_"} }
			for i,f in ipairs(forwarding) do
				if values.services.forwarding and pg_t[f.param.."enable"] then
					xml_elem(f.xml.."Serv", "Yes", "na", num)
					xml_elem(f.xml.."Dest", pg_t[f.param], "na", num)
				else
					xml_elem(f.xml.."Serv", "No", "na", num)
					xml_elem(f.xml.."Dest", "", "na", num)
				end
			end
		else
			-- Cannot disable the lines, or check-sync will not work, just set User ID to ""
			xml_elem("Line_Enable", "Yes", "na", num)
			xml_elem("User_ID", "", "na", num)
		end
	end
end %>

</flat-profile>