Commit 59364f1b authored by Ted Trask's avatar Ted Trask

Added support for postgresql using DSN config (and making database config obsolete)

parent c2066252
......@@ -11,17 +11,43 @@ session = require("session")
-- Set variables
local configfile = "/etc/freeswitchvmail.conf"
local recording_path = "/var/lib/freeswitch/voicemail/"
local db_path = "/var/lib/freeswitch/db/"
local configcontent = fs.read_file(configfile) or ""
local config = format.parse_ini_file(configcontent, "") or {}
config.database = config.database or "/var/lib/freeswitch/db/voicemail_default.db"
config.database = config.database or db_path.."voicemail_default.db"
config.domain = config.domain or "voicemail"
config.event_socket_ip = config.event_socket_ip or "127.0.0.1"
config.event_socket_port = config.event_socket_port or "8021"
config.event_socket_password = config.event_socket_password or "ClueCon"
config.callback_command = config.callback_command or "originate {ignore_early_media=true,origination_caller_id_name='Voicemail',origination_caller_id_number='Voicemail'}sofia/gateway/asterlink.com/$1 &playback($2)"
local recording_path = "/var/lib/freeswitch/voicemail/"
local vmaildb = db.create(db.engine.sqlite3, config.database)
local vmaildb
local dbengine = db.engine.sqlite3
if not config.dsn then
-- Backward compatible assumed sqlite3 with full path in config.database
config.dsn = "sqlite://"..config.database
end
if string.find(config.dsn, "^sqlite://") then
local params = string.match(config.dsn, "^sqlite://(.*)") or ""
if not string.find(params, "^/") then
params = db_path..params
end
vmaildb = db.create(dbengine, params)
elseif string.find(config.dsn, "^pgsql://") then
local params = string.match(config.dsn, "^pgsql://(.*)") or ""
dbengine = db.engine.postgresql
-- Sample params: hostaddr=127.0.0.1 dbname=freeswitch user=freeswitch password= options='-c client_min_messages=NOTICE' application_name='freeswitch'
-- Second sample: hostaddr=127.0.0.1 dbname=voicemail user=postgres password= options=''
local hostaddr = string.match(params, "hostaddr=(%S*)") or "127.0.0.1"
local dbname = string.match(params, "dbname=(%S*)") or "freeswitch"
local user = string.match(params, "user=(%S*)")
local password = string.match(params, "password=(%S*)")
vmaildb = db.create(dbengine, dbname, user, password, hostaddr)
else
-- Failure - we do not support ODBC database
end
vmaildb.table_creation_scripts = {
voicemail_users = {
"CREATE TABLE voicemail_users (uid INTEGER PRIMARY KEY, username VARCHAR(255) UNIQUE)",
......@@ -91,7 +117,9 @@ end
local databasedisconnect = function()
vmaildb.databasedisconnect()
posix.chown(config.database, posix.getpasswd("freeswitch", "uid") or 0, posix.getpasswd("freeswitch", "gid") or 0)
if dbengine == db.engine.sqlite3 then
posix.chown(vmaildb.database, posix.getpasswd("freeswitch", "uid") or 0, posix.getpasswd("freeswitch", "gid") or 0)
end
end
local generatewhereclause = function(username, message, foldername, uid)
......@@ -238,8 +266,8 @@ local function validateconfig(newconfig)
newconfig.value.domain.errtxt = "Cannot be blank"
success = false
end
if newconfig.value.database.value == "" then
newconfig.value.database.errtxt = "Cannot be blank"
if newconfig.value.dsn.value == "" then
newconfig.value.dsn.errtxt = "Cannot be blank"
success = false
end
if newconfig.value.event_socket_ip.value == "" then
......@@ -342,7 +370,7 @@ end
mymodule.get_config = function()
local result = {}
result.domain = cfe({ value=config.domain, label="Domain" })
result.database = cfe({ value=config.database, label="Database" })
result.dsn = cfe({ value=config.dsn, label="Data Source Name (DSN)" })
result.event_socket_ip = cfe({ value=config.event_socket_ip, label="FS Event Socket IP" })
result.event_socket_port = cfe({ value=config.event_socket_port, label="FS Event Socket Port" })
result.event_socket_password = cfe({ value=config.event_socket_password, label="FS Event Socket Password" })
......@@ -710,7 +738,7 @@ mymodule.set_delete_user = function(self, deleterequest)
end
mymodule.get_bunchsettings = function (self)
local bunch = cfe({ name="list", label="List", type="longtext", descr="Each line will be seen as a new extension. To separate fields make use of colons. Follow the formmat:\n\nExtension:Name:Lastname:Password"})
local bunch = cfe({ name="list", label="List", type="longtext", descr="Each line will be seen as a new extension. To separate fields make use of colons. Follow the format:\n\nExtension:Name:Lastname:Password"})
return cfe({ type="group", value={bunch=bunch}, label="Voicemail Users List" })
end
......@@ -748,7 +776,7 @@ mymodule.set_bunchsettings = function (self, bunchdata)
entry.value.lastname.value=lastname
entry.value["vm-password"].value=password
if validateentry(entry) then
sql = "INSERT INTO voicemail_users VALUES(null, '"..vmaildb.escape(username).."')"
sql = "INSERT INTO voicemail_users VALUES(default, '"..vmaildb.escape(username).."')"
vmaildb.runsqlcommand(sql)
sql = "SELECT uid FROM voicemail_users where username ='"..vmaildb.escape(username).."'";
uid = vmaildb.getselectresponse(sql)
......@@ -820,7 +848,7 @@ mymodule.update_usersettings = function(self, usersettings, action, create)
errtxt = "User does not exist"
else
if create then
sql = "INSERT INTO voicemail_users VALUES(null, '"..vmaildb.escape(usersettings.value.username.value).."')"
sql = "INSERT INTO voicemail_users VALUES(default, '"..vmaildb.escape(usersettings.value.username.value).."')"
vmaildb.runsqlcommand(sql)
end
success,errtxt = setuserparams(usersettings.value)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment