summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/configuration/freeswitch_xml.lua
blob: 5f4602cebf22280acf0d0177e1b837e98226d906 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
-- ConfigurationModule: FreeSwitchXml
--
module(...,package.seeall)

FreeSwitchXml = {}

-- Create FreeSwitchXml object
function FreeSwitchXml.new(self, object)
  object = object or {}
  setmetatable(object, self)
  self.__index = self
  return object
end

function FreeSwitchXml.param(self, name, value)
  return '<param name="' .. tostring(name) .. '" value="' .. tostring(value) .. '"/>'
end

function FreeSwitchXml.variable(self, name, value)
  return '<variable name="' .. tostring(name) .. '" value="' .. tostring(value) .. '"/>'
end

function FreeSwitchXml.document(self, sections_xml)
  if type(sections_xml) == "string" then
    sections_xml = { sections_xml }
  elseif type(sections_xml) == "nil" then
    sections_xml = { "" }
  end

  local xml_string=
[[<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="freeswitch/xml">
]] .. table.concat(sections_xml, "\n") .. [[

</document>]]

  return xml_string
end

function FreeSwitchXml.directory(self, entries_xml, domain)
   if type(entries_xml) == "string" then
    entries_xml = { entries_xml }
  elseif type(entries_xml) == "nil" then
    entries_xml = { "" }
  end

  local xml_string =
[[
<section name="directory">
<domain name="]] .. domain .. [[">
<params>
<param name="dial-string" value="${sofia_contact(${dialed_user}@${dialed_domain})}"/>
</params>
]] .. table.concat(entries_xml, "\n") .. [[

</domain>
</section>]]
  return xml_string
end

function FreeSwitchXml.group_default(self, entries_xml)
   if type(entries_xml) == "string" then
    entries_xml = { entries_xml }
  elseif type(entries_xml) == "nil" then
    entries_xml = { "" }
  end

  local xml_string =
[[
<groups>
<group name="default">
<users>
]] .. table.concat(entries_xml, "\n") .. [[

</users>
</group>
</groups>]]
  return xml_string
end

function FreeSwitchXml.user(self, user, params)
  params = params or {};

  params['password'] = user.password;
  params['vm-password'] = user.voicemail_pin;

  local variables = {
    user_context           = "default",
    gs_from_gateway        = "false",
    gs_account_id          = user.id,
    gs_account_uuid        = user.uuid,
    gs_account_type        = "SipAccount",
    gs_account_state       = user.state,
    gs_account_caller_name = user.caller_name,
    gs_account_owner_type  = user.sip_accountable_type,
    gs_account_owner_id    = user.sip_accountable_id    
  }

  local params_xml = {}
  for name, value in pairs(params) do
    params_xml[#params_xml+1] = self:param(name, value)
  end

  local variables_xml = {}
  for name, value in pairs(variables) do
    variables_xml[#variables_xml+1] = self:variable(name, value)
  end

  local xml_string =
[[
<user id="]] .. user.auth_name .. [[">
<params>
]] .. table.concat(params_xml, "\n") .. [[

</params>
<variables>
]] .. table.concat(variables_xml, "\n") .. [[

</variables>
</user>]]
  return xml_string
end

function FreeSwitchXml.gateway_user(self, user, gateway_name, auth_name)
  user.id = user.id or 0

  local params = {
    ['password']    = user.password,
  }

  local variables = {
    user_context = "default",
    gs_from_gateway = "true",
    gs_gateway_name = gateway_name,
    gs_gateway_id   = user.id
  }

  local params_xml = {}
  for name, value in pairs(params) do
    params_xml[#params_xml+1] = self:param(name, value)
  end

  local variables_xml = {}
  for name, value in pairs(variables) do
    variables_xml[#variables_xml+1] = self:variable(name, value)
  end

  local xml_string =
[[
<user id="]] .. auth_name .. [[">
<params>
]] .. table.concat(params_xml, "\n") .. [[

</params>
<variables>
]] .. table.concat(variables_xml, "\n") .. [[

</variables>
</user>]]
  return xml_string
end

function FreeSwitchXml.sofia(self, parameters, profiles_xml)
  if type(profiles_xml) == "string" then
    profiles_xml = { profiles_xml }
  elseif type(profiles_xml) == "nil" then
    profiles_xml = { "" }
  end

  local params_xml = {}
  for name, value in pairs(parameters) do
    params_xml[#params_xml+1] = self:param(name, value)
  end

  local xml_string =
[[
<section name="configuration" description="FreeSwitch configuration for Sofia Profile">
<configuration name="sofia.conf" description="Sofia SIP Configuration">
<global_settings>
]] .. table.concat(params_xml, "\n") .. [[

</global_settings>
<profiles>
]] .. table.concat(profiles_xml, "\n") .. [[

</profiles>
</configuration>
</section>]]
  return xml_string
end

function FreeSwitchXml.sofia_profile(self, profile_name, parameters, gateways_xml)
  params_xml = {}
  for name, value in pairs(parameters) do
    params_xml[#params_xml+1] = self:param(name, value)
  end

  if type(gateways_xml) == "string" then
    gateways_xml = { gateways_xml }
  elseif type(gateways_xml) == "nil" then
    gateways_xml = { "" }
  end

  local xml_string =
[[
<profile name="]] .. profile_name .. [[">
<aliases>
</aliases>
<gateways>
]] .. table.concat(gateways_xml, "\n") .. [[

</gateways>
<domains>
<domain name="all" alias="true" parse="false"/>
</domains>
<settings>
]] .. table.concat(params_xml, "\n") .. [[

</settings>
</profile>]]
  return xml_string
end

function FreeSwitchXml.gateway(self, gateway_name, parameters)
  local params_xml = {}
  if parameters then    
    for name, value in pairs(parameters) do
      params_xml[#params_xml+1] = self:param(name, value)
    end
  end

  local xml_string =
[[
<gateway name="]] .. gateway_name .. [[">
]] .. table.concat(params_xml, "\n") .. [[

</gateway>]]
  return xml_string
end

function FreeSwitchXml.conference(self, profiles_xml)
  if type(profiles_xml) == "string" then
    profiles_xml = { profiles_xml }
  elseif type(profiles_xml) == "nil" then
    profiles_xml = { "" }
  end

  local xml_string =
[[
<section name="configuration" description="FreeSwitch configuration for Sofia Profile">
<configuration name="conference.conf" description="Conference configuration">
<advertise>
</advertise>
<caller-controls>
<group name="speaker">
<control action="mute"/>
<control action="deaf mute" digits="*"/>
<control action="energy up" digits="9"/>
<control action="energy equ" digits="8"/>
<control action="energy dn" digits="7"/>
<control action="vol talk up" digits="3"/>
<control action="vol talk zero" digits="2"/>
<control action="vol talk dn" digits="1"/>
<control action="vol listen up" digits="6"/>
<control action="vol listen zero" digits="5"/>
<control action="vol listen dn" digits="4"/>
<control action="hangup" digits="#"/>
</group>
<group name="moderator">
<control action="mute" digits="0"/>
<control action="deaf mute" digits="*"/>
<control action="energy up" digits="9"/>
<control action="energy equ" digits="8"/>
<control action="energy dn" digits="7"/>
<control action="vol talk up" digits="3"/>
<control action="vol talk zero" digits="2"/>
<control action="vol talk dn" digits="1"/>
<control action="vol listen up" digits="6"/>
<control action="vol listen zero" digits="5"/>
<control action="vol listen dn" digits="4"/>
<control action="hangup" digits="#"/>
</group>
</caller-controls>
<profiles>
]] .. table.concat(profiles_xml, "\n") .. [[

</profiles>
</configuration>
</section>]]
  return xml_string
end

function FreeSwitchXml.conference_profile(self, profile_name, parameters)
  params_xml = {}
  for name, value in pairs(parameters) do
    params_xml[#params_xml+1] = self:param(name, value)
  end

  local xml_string =
[[
<profile name="]] .. profile_name .. [[">
]] .. table.concat(params_xml, "\n") .. [[

</profile>]]
  return xml_string
end