Skip to content

Commit c010f93

Browse files
committedJan 21, 2016
fix ordering of local functions.
1 parent 40ee6f0 commit c010f93

File tree

5 files changed

+276
-276
lines changed

5 files changed

+276
-276
lines changed
 

‎core.lua

+28-28
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,34 @@
1515

1616
local _M = {}
1717

18+
--- Get the value for a key in a storage area.
19+
--
20+
-- This is the function that is exposed to sequences.
21+
--
22+
-- @string area
23+
-- The storage area.
24+
-- @string key
25+
-- The storage key.
26+
-- @return
27+
-- The value stored in the key of the storage area, or an empty string if
28+
-- none is found.
29+
local function protected_get_storage(area, key)
30+
return _M.get_storage(area, key, "")
31+
end
32+
33+
--- Get the value for a channel variable.
34+
--
35+
-- This is the function that is exposed to sequences.
36+
--
37+
-- @string var
38+
-- The name of the channel variable.
39+
-- @return
40+
-- The value stored in the channel variable, or an empty string if none is
41+
-- found.
42+
local function protected_get_variable(var)
43+
return _M.get_variable(var, "")
44+
end
45+
1846
--- Bootstrap the Jester environment.
1947
--
2048
-- @tab config
@@ -402,34 +430,6 @@ function _M.load_sequence(name, arguments)
402430
end
403431
end
404432

405-
--- Get the value for a key in a storage area.
406-
--
407-
-- This is the function that is exposed to sequences.
408-
--
409-
-- @string area
410-
-- The storage area.
411-
-- @string key
412-
-- The storage key.
413-
-- @return
414-
-- The value stored in the key of the storage area, or an empty string if
415-
-- none is found.
416-
local function protected_get_storage(area, key)
417-
return _M.get_storage(area, key, "")
418-
end
419-
420-
--- Get the value for a channel variable.
421-
--
422-
-- This is the function that is exposed to sequences.
423-
--
424-
-- @string var
425-
-- The name of the channel variable.
426-
-- @return
427-
-- The value stored in the channel variable, or an empty string if none is
428-
-- found.
429-
local function protected_get_variable(var)
430-
return _M.get_variable(var, "")
431-
end
432-
433433
--- Get the value for a key in a storage area.
434434
--
435435
-- @string area

‎modules/data/odbc.lua

+164-164
Original file line numberDiff line numberDiff line change
@@ -2,158 +2,56 @@ local core = require "jester.core"
22

33
local _M = {}
44

5-
--[[
6-
Load data from a database into storage.
7-
]]
8-
function _M.load_data(action)
9-
local fields = action.fields
10-
local filters = action.filters or {}
11-
local multiple = action.multiple
12-
local sort = action.sort
13-
local sort_order = action.sort_order
14-
local area = action.storage_area or "data"
15-
if fields then
16-
local dbh, conf = connect(action)
17-
local limit = multiple and "" or " LIMIT 1"
18-
-- Optional sort.
19-
local load_sort = sort and build_sort(sort, sort_order) or ""
20-
-- Build the query.
21-
local sql = "SELECT " .. build_load_fields(fields) .. " FROM " .. conf.table .. build_where(filters) .. load_sort .. limit
22-
core.debug_log("Executing query: %s", sql)
23-
local count, suffix = 0, ""
24-
-- Clean out storage area before loading in new data.
25-
core.clear_storage(area)
26-
-- Loop through the returned rows.
27-
assert(dbh:query(sql, function(row)
28-
count = count + 1
29-
for col, val in pairs(row) do
30-
-- Multi-row results get a row suffix.
31-
if multiple then
32-
suffix = "_" .. count
33-
end
34-
core.set_storage(area, col .. suffix, val)
35-
end
36-
end))
37-
dbh:release()
38-
-- Multi-row results get a special count key.
39-
if multiple then
40-
core.debug_log("Query rows returned: %d", count)
41-
core.set_storage(area, "__count", tonumber(count))
42-
end
43-
end
44-
end
5+
local escape
456

467
--[[
47-
Load data row counts from a database into storage.
8+
Escapes special characters for the MySQL database.
489
]]
49-
function _M.load_data_count(action)
50-
local count_field = action.count_field
51-
local filters = action.filters or {}
52-
local area = action.storage_area or "data"
53-
local key = action.storage_key or "count"
54-
if count_field then
55-
local dbh, conf = connect(action)
56-
-- Build the query.
57-
local sql = "SELECT COUNT(" .. count_field .. ") AS count FROM " .. conf.table .. build_where(filters)
58-
core.debug_log("Executing query: %s", sql)
59-
local count
60-
-- Loop through the returned rows.
61-
assert(dbh:query(sql, function(row) count = tonumber(row.count) end))
62-
dbh:release()
63-
core.set_storage(area, key, count)
64-
end
10+
local function mysql_escape(string)
11+
-- Single quote, double quote, backspace.
12+
string = string.gsub(string, "(['\"\\])", function(x) return "\\" .. x end)
13+
-- Null byte.
14+
string = string.gsub(string, "%z", "\\0")
15+
return string
6516
end
6617

6718
--[[
68-
Update data in a database.
19+
Escapes special characters for the Postgres database.
6920
]]
70-
function _M.update_data(action)
71-
local fields = action.fields
72-
local filters = action.filters or {}
73-
local update_type = action.update_type
74-
if fields then
75-
local dbh, conf = connect(action)
76-
local where = build_where(filters)
77-
local count = 0
78-
local message
79-
-- No type specified, or update forced, so try an update first.
80-
if not update_type or update_type == "update" then
81-
sql = "UPDATE " .. conf.table .. " SET " .. build_update_fields(fields) .. where
82-
core.debug_log("Executing query: %s", sql)
83-
assert(dbh:query(sql))
84-
message = "updated"
85-
count = dbh:affected_rows()
86-
end
87-
-- Insert forced, or no rows updated and update is not forced, so insert.
88-
if update_type == "insert" or (count == 0 and update_type ~= "update") then
89-
local insert_fields, values = build_insert(filters, fields)
90-
sql = "INSERT INTO " .. conf.table .. " (" .. insert_fields .. ") VALUES (" .. values .. ")"
91-
core.debug_log("Executing query: %s", sql)
92-
assert(dbh:query(sql))
93-
message = "inserted"
94-
count = 1
95-
end
96-
dbh:release()
97-
core.debug_log("Rows %s: %d", message, count)
98-
end
21+
local function pgsql_escape(string)
22+
-- Single quote.
23+
string = string.gsub(string, "'", "''")
24+
-- Null byte.
25+
string = string.gsub(string, "%z", "")
26+
return string
9927
end
10028

10129
--[[
102-
Delete data from a database.
30+
Escapes special characters for the SQLite database.
10331
]]
104-
function _M.delete_data(action)
105-
local filters = action.filters or {}
106-
local dbh, conf = connect(action)
107-
local sql = "DELETE FROM " .. conf.table .. build_where(filters)
108-
core.debug_log("Executing query: %s", sql)
109-
assert(dbh:query(sql))
110-
dbh:release()
32+
local function sqlite_escape(string)
33+
-- Single quote.
34+
string = string.gsub(string, "'", "''")
35+
-- Null byte.
36+
string = string.gsub(string, "%z", "")
37+
return string
11138
end
11239

11340
--[[
114-
Execute custom queries on a database, optionally returning data.
41+
Sets the escape function based on the database type.
11542
]]
116-
function _M.query_data(action)
117-
local query = action.query
118-
local return_fields = action.return_fields
119-
local area = action.storage_area or "data"
120-
local tokens = action.tokens
121-
local dbh, conf = connect(action)
122-
local sql
123-
124-
-- Token replacements.
125-
if tokens then
126-
require "jester.support.string"
127-
for k,v in pairs(tokens) do
128-
tokens[k] = escape(v)
129-
end
130-
sql = string.token_replace(query, tokens)
131-
else
132-
sql = query
133-
end
134-
core.debug_log("Executing query: %s", sql)
135-
if return_fields then
136-
local count = 0
137-
-- Clean out storage area before loading in new data.
138-
core.clear_storage(area)
139-
-- Loop through the returned rows.
140-
assert(dbh:query(sql, function(row)
141-
count = count + 1
142-
for col, val in pairs(row) do
143-
core.set_storage(area, col .. "_" .. count, val)
144-
end
145-
end))
146-
dbh:release()
147-
core.debug_log("Query rows returned: %d", count)
148-
core.set_storage(area, "__count", tonumber(count))
43+
local function set_escape(db_type)
44+
if db_type == "mysql" then
45+
escape = mysql_escape
46+
elseif db_type == "pgsql" then
47+
escape = pgsql_escape
48+
elseif db_type == "sqlite" then
49+
escape = sqlite_escape
14950
else
150-
assert(dbh:query(sql))
51+
error(string.format([[Unsupported database type %s]], db_type))
15152
end
152-
dbh:release()
15353
end
15454

155-
local escape
156-
15755
--[[
15856
Connect to a database.
15957
]]
@@ -283,51 +181,153 @@ local function build_sort(sort, sort_order)
283181
end
284182

285183
--[[
286-
Sets the escape function based on the database type.
184+
Load data from a database into storage.
287185
]]
288-
local function set_escape(db_type)
289-
if db_type == "mysql" then
290-
escape = mysql_escape
291-
elseif db_type == "pgsql" then
292-
escape = pgsql_escape
293-
elseif db_type == "sqlite" then
294-
escape = sqlite_escape
295-
else
296-
error(string.format([[Unsupported database type %s]], db_type))
186+
function _M.load_data(action)
187+
local fields = action.fields
188+
local filters = action.filters or {}
189+
local multiple = action.multiple
190+
local sort = action.sort
191+
local sort_order = action.sort_order
192+
local area = action.storage_area or "data"
193+
if fields then
194+
local dbh, conf = connect(action)
195+
local limit = multiple and "" or " LIMIT 1"
196+
-- Optional sort.
197+
local load_sort = sort and build_sort(sort, sort_order) or ""
198+
-- Build the query.
199+
local sql = "SELECT " .. build_load_fields(fields) .. " FROM " .. conf.table .. build_where(filters) .. load_sort .. limit
200+
core.debug_log("Executing query: %s", sql)
201+
local count, suffix = 0, ""
202+
-- Clean out storage area before loading in new data.
203+
core.clear_storage(area)
204+
-- Loop through the returned rows.
205+
assert(dbh:query(sql, function(row)
206+
count = count + 1
207+
for col, val in pairs(row) do
208+
-- Multi-row results get a row suffix.
209+
if multiple then
210+
suffix = "_" .. count
211+
end
212+
core.set_storage(area, col .. suffix, val)
213+
end
214+
end))
215+
dbh:release()
216+
-- Multi-row results get a special count key.
217+
if multiple then
218+
core.debug_log("Query rows returned: %d", count)
219+
core.set_storage(area, "__count", tonumber(count))
220+
end
297221
end
298222
end
299223

300224
--[[
301-
Escapes special characters for the MySQL database.
225+
Load data row counts from a database into storage.
302226
]]
303-
local function mysql_escape(string)
304-
-- Single quote, double quote, backspace.
305-
string = string.gsub(string, "(['\"\\])", function(x) return "\\" .. x end)
306-
-- Null byte.
307-
string = string.gsub(string, "%z", "\\0")
308-
return string
227+
function _M.load_data_count(action)
228+
local count_field = action.count_field
229+
local filters = action.filters or {}
230+
local area = action.storage_area or "data"
231+
local key = action.storage_key or "count"
232+
if count_field then
233+
local dbh, conf = connect(action)
234+
-- Build the query.
235+
local sql = "SELECT COUNT(" .. count_field .. ") AS count FROM " .. conf.table .. build_where(filters)
236+
core.debug_log("Executing query: %s", sql)
237+
local count
238+
-- Loop through the returned rows.
239+
assert(dbh:query(sql, function(row) count = tonumber(row.count) end))
240+
dbh:release()
241+
core.set_storage(area, key, count)
242+
end
309243
end
310244

311245
--[[
312-
Escapes special characters for the Postgres database.
246+
Update data in a database.
313247
]]
314-
local function pgsql_escape(string)
315-
-- Single quote.
316-
string = string.gsub(string, "'", "''")
317-
-- Null byte.
318-
string = string.gsub(string, "%z", "")
319-
return string
248+
function _M.update_data(action)
249+
local fields = action.fields
250+
local filters = action.filters or {}
251+
local update_type = action.update_type
252+
if fields then
253+
local dbh, conf = connect(action)
254+
local where = build_where(filters)
255+
local count = 0
256+
local message
257+
-- No type specified, or update forced, so try an update first.
258+
if not update_type or update_type == "update" then
259+
sql = "UPDATE " .. conf.table .. " SET " .. build_update_fields(fields) .. where
260+
core.debug_log("Executing query: %s", sql)
261+
assert(dbh:query(sql))
262+
message = "updated"
263+
count = dbh:affected_rows()
264+
end
265+
-- Insert forced, or no rows updated and update is not forced, so insert.
266+
if update_type == "insert" or (count == 0 and update_type ~= "update") then
267+
local insert_fields, values = build_insert(filters, fields)
268+
sql = "INSERT INTO " .. conf.table .. " (" .. insert_fields .. ") VALUES (" .. values .. ")"
269+
core.debug_log("Executing query: %s", sql)
270+
assert(dbh:query(sql))
271+
message = "inserted"
272+
count = 1
273+
end
274+
dbh:release()
275+
core.debug_log("Rows %s: %d", message, count)
276+
end
320277
end
321278

322279
--[[
323-
Escapes special characters for the SQLite database.
280+
Delete data from a database.
324281
]]
325-
local function sqlite_escape(string)
326-
-- Single quote.
327-
string = string.gsub(string, "'", "''")
328-
-- Null byte.
329-
string = string.gsub(string, "%z", "")
330-
return string
282+
function _M.delete_data(action)
283+
local filters = action.filters or {}
284+
local dbh, conf = connect(action)
285+
local sql = "DELETE FROM " .. conf.table .. build_where(filters)
286+
core.debug_log("Executing query: %s", sql)
287+
assert(dbh:query(sql))
288+
dbh:release()
289+
end
290+
291+
--[[
292+
Execute custom queries on a database, optionally returning data.
293+
]]
294+
function _M.query_data(action)
295+
local query = action.query
296+
local return_fields = action.return_fields
297+
local area = action.storage_area or "data"
298+
local tokens = action.tokens
299+
local dbh, conf = connect(action)
300+
local sql
301+
302+
-- Token replacements.
303+
if tokens then
304+
require "jester.support.string"
305+
for k,v in pairs(tokens) do
306+
tokens[k] = escape(v)
307+
end
308+
sql = string.token_replace(query, tokens)
309+
else
310+
sql = query
311+
end
312+
core.debug_log("Executing query: %s", sql)
313+
if return_fields then
314+
local count = 0
315+
-- Clean out storage area before loading in new data.
316+
core.clear_storage(area)
317+
-- Loop through the returned rows.
318+
assert(dbh:query(sql, function(row)
319+
count = count + 1
320+
for col, val in pairs(row) do
321+
core.set_storage(area, col .. "_" .. count, val)
322+
end
323+
end))
324+
dbh:release()
325+
core.debug_log("Query rows returned: %d", count)
326+
core.set_storage(area, "__count", tonumber(count))
327+
else
328+
assert(dbh:query(sql))
329+
end
330+
dbh:release()
331331
end
332332

333333
return _M

‎modules/dialplan_tools/init.lua

+33-33
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,39 @@ local core = require "jester.core"
107107

108108
local _M = {}
109109

110+
--[[
111+
Given a table of variables, return a string.
112+
]]
113+
local function build_variables(variables, v_type)
114+
local output = ""
115+
local string_pieces = {}
116+
for k, v in pairs(variables) do
117+
table.insert(string_pieces, k .. "='" .. v .."'")
118+
end
119+
if #string_pieces > 0 then
120+
output = table.concat(string_pieces, ",")
121+
if v_type == "global" then
122+
output = "{" .. output .. "}"
123+
else
124+
output = "[" .. output .. "]"
125+
end
126+
end
127+
return output
128+
end
129+
130+
--[[
131+
Given a the main variables table and location data, a channel and extension,
132+
return a fully constructed channel string for the channel.
133+
]]
134+
local function build_channel(variables, channel, extension, multivars, k)
135+
if multivars and variables[k] then
136+
channel_vars = build_variables(variables[k])
137+
else
138+
channel_vars = ""
139+
end
140+
return channel_vars .. channel .. extension
141+
end
142+
110143
--[[
111144
Executes a dialplan application.
112145
]]
@@ -198,37 +231,4 @@ function _M.bridge(action)
198231
end
199232
end
200233

201-
--[[
202-
Given a table of variables, return a string.
203-
]]
204-
local function build_variables(variables, v_type)
205-
local output = ""
206-
local string_pieces = {}
207-
for k, v in pairs(variables) do
208-
table.insert(string_pieces, k .. "='" .. v .."'")
209-
end
210-
if #string_pieces > 0 then
211-
output = table.concat(string_pieces, ",")
212-
if v_type == "global" then
213-
output = "{" .. output .. "}"
214-
else
215-
output = "[" .. output .. "]"
216-
end
217-
end
218-
return output
219-
end
220-
221-
--[[
222-
Given a the main variables table and location data, a channel and extension,
223-
return a fully constructed channel string for the channel.
224-
]]
225-
local function build_channel(variables, channel, extension, multivars, k)
226-
if multivars and variables[k] then
227-
channel_vars = build_variables(variables[k])
228-
else
229-
channel_vars = ""
230-
end
231-
return channel_vars .. channel .. extension
232-
end
233-
234234
return _M

‎modules/speech_to_text/att.lua

+37-37
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,43 @@ local core = require "jester.core"
22

33
local _M = {}
44

5+
--[[
6+
Get an access token from an AT&T API call.
7+
]]
8+
local function att_get_access_token(action)
9+
local app_key = action.app_key
10+
local app_secret = action.app_secret
11+
12+
local post_data = string.format("client_id=%s&client_secret=%s&grant_type=client_credentials&scope=SPEECH,TTS", app_key, app_secret)
13+
14+
local response = {}
15+
16+
local body, status_code, headers, status_description = https.request({
17+
method = "POST",
18+
headers = {
19+
["content-length"] = post_data:len(),
20+
},
21+
url = "https://api.att.com/oauth/v4/token",
22+
sink = ltn12.sink.table(response),
23+
source = ltn12.source.string(post_data),
24+
protocol = "tlsv1",
25+
})
26+
27+
if status_code == 200 then
28+
local response_string = table.concat(response)
29+
core.debug_log("JSON response string '%s'", response_string)
30+
local data = cjson.decode(response_string)
31+
for key, value in pairs(data) do
32+
if key == "access_token" then
33+
return value
34+
end
35+
end
36+
core.debug_log("ERROR: No access token found")
37+
else
38+
core.debug_log("ERROR: Request to AT&T token server failed: %s", status_description)
39+
end
40+
end
41+
542
local https = require 'ssl.https'
643
local ltn12 = require("ltn12")
744
local cjson = require("cjson")
@@ -52,41 +89,4 @@ function _M.speech_to_text_from_file(action, attributes)
5289
return status, translations
5390
end
5491

55-
--[[
56-
Get an access token from an AT&T API call.
57-
]]
58-
local function att_get_access_token(action)
59-
local app_key = action.app_key
60-
local app_secret = action.app_secret
61-
62-
local post_data = string.format("client_id=%s&client_secret=%s&grant_type=client_credentials&scope=SPEECH,TTS", app_key, app_secret)
63-
64-
local response = {}
65-
66-
local body, status_code, headers, status_description = https.request({
67-
method = "POST",
68-
headers = {
69-
["content-length"] = post_data:len(),
70-
},
71-
url = "https://api.att.com/oauth/v4/token",
72-
sink = ltn12.sink.table(response),
73-
source = ltn12.source.string(post_data),
74-
protocol = "tlsv1",
75-
})
76-
77-
if status_code == 200 then
78-
local response_string = table.concat(response)
79-
core.debug_log("JSON response string '%s'", response_string)
80-
local data = cjson.decode(response_string)
81-
for key, value in pairs(data) do
82-
if key == "access_token" then
83-
return value
84-
end
85-
end
86-
core.debug_log("ERROR: No access token found")
87-
else
88-
core.debug_log("ERROR: Request to AT&T token server failed: %s", status_description)
89-
end
90-
end
91-
9292
return _M

‎modules/speech_to_text/init.lua

+14-14
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,6 @@ local att = require("jester.modules.speech_to_text.att")
8383

8484
local _M = {}
8585

86-
--[[
87-
Speech to text using Google's API.
88-
]]
89-
--function speech_to_text_from_file_google(action)
90-
-- speech_to_text_from_file(action, google.speech_to_text_from_file)
91-
--end
92-
93-
--[[
94-
Speech to text using AT&T's API.
95-
]]
96-
function _M.speech_to_text_from_file_att(action)
97-
speech_to_text_from_file(action, att.speech_to_text_from_file)
98-
end
99-
10086
--[[
10187
Speech to text base function.
10288
@@ -128,4 +114,18 @@ local function speech_to_text_from_file(action, handler)
128114
end
129115
end
130116

117+
--[[
118+
Speech to text using Google's API.
119+
]]
120+
--function speech_to_text_from_file_google(action)
121+
-- speech_to_text_from_file(action, google.speech_to_text_from_file)
122+
--end
123+
124+
--[[
125+
Speech to text using AT&T's API.
126+
]]
127+
function _M.speech_to_text_from_file_att(action)
128+
speech_to_text_from_file(action, att.speech_to_text_from_file)
129+
end
130+
131131
return _M

0 commit comments

Comments
 (0)
Please sign in to comment.