diff --git a/apps/examples/db_announce/Makefile b/apps/examples/db_announce/Makefile new file mode 100644 index 00000000..634ff556 --- /dev/null +++ b/apps/examples/db_announce/Makefile @@ -0,0 +1,6 @@ +NAME=announcement +VERSION=0.10.0-1 + +LIBDIR=. + +include ../ivr/Makefile.ivr_application diff --git a/apps/examples/db_announce/Readme.db_announce b/apps/examples/db_announce/Readme.db_announce new file mode 100644 index 00000000..6a71f302 --- /dev/null +++ b/apps/examples/db_announce/Readme.db_announce @@ -0,0 +1,48 @@ +This is IVR/Python based version of announcement application that keeps +greeting messages in MySQL database. + +It assumes the following database schema: + +CREATE TABLE default_audio ( + id int(10) unsigned NOT NULL auto_increment, + application varchar(32) NOT NULL, + message varchar(32) NOT NULL, + `language` char(2) NOT NULL default '', + audio mediumblob NOT NULL, + PRIMARY KEY (id), + UNIQUE KEY application (application,message,`language`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + + +CREATE TABLE domain_audio ( + id int(10) unsigned NOT NULL auto_increment, + application varchar(32) NOT NULL, + message varchar(32) NOT NULL, + domain varchar(128) NOT NULL, + `language` char(2) NOT NULL default '', + audio mediumblob NOT NULL, + PRIMARY KEY (id), + UNIQUE KEY application (application,message,domain,`language`) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +CREATE TABLE user_audio ( + id int(10) unsigned NOT NULL auto_increment, + application varchar(32) NOT NULL, + message varchar(32) NOT NULL, + domain varchar(128) NOT NULL, + userid varchar(64) NOT NULL, + audio mediumblob NOT NULL, + PRIMARY KEY (id), + UNIQUE KEY application (application,message,domain,userid) +) ENGINE=MyISAM DEFAULT CHARSET=latin1; + +Greeting message is first looked for in user_audio table, then in +domain_audio table, and finally in default_audio table. Application +field value needs to be 'announcement' and message field value +'greeting_msg'. If language is not available, language field value +needs to be ''. + +If you want to use this example application as replacement of +apps/announcement application, you must (in order to avoid conflict) add +announcement as an excluded module in apps/Makefile and then copy this +directory to apps directory under name 'announcement'. diff --git a/apps/examples/db_announce/announcement.py b/apps/examples/db_announce/announcement.py new file mode 100644 index 00000000..ac665744 --- /dev/null +++ b/apps/examples/db_announce/announcement.py @@ -0,0 +1,157 @@ +# DB based Announcement application for SEMS + +# Copyright 2007 Juha Heinanen +# +# This file is part of sems, a free SIP media server. +# +# sems is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# Use, copying, modification, and distribution without written +# permission is not allowed. + +import os, MySQLdb + +from log import * +from ivr import * + +APPLICATION = 'announcement' + +GREETING_MSG = 'greeting_msg' + +class IvrDialog(IvrDialogBase): + + DB_HOST = 'localhost' + DB_USER = 'sems' + DB_PASSWD = '' + DB_DB = 'sems' + + def __init__(self): + + try: + if config['mysql_server']: + self.DB_HOST = config['mysql_server'] + except KeyError: + pass + + try: + if config['mysql_user']: + self.DB_USER = config['mysql_user'] + except KeyError: + pass + + try: + if config['mysql_passwd']: + self.DB_PASSWD = config['mysql_passwd'] + except KeyError: + pass + + try: + if config['mysql_db']: + self.DB_DB = config['mysql_db'] + except KeyError: + pass + + try: + self.db = MySQLdb.connect(host=self.DB_HOST,\ + user=self.DB_USER,\ + passwd=self.DB_PASSWD,\ + db=self.DB_DB) + except MySQLdb.Error, e: + error(APPLICATION + ": cannot open database: " +\ + str(e.args[0]) + ":" + e.args[1]) + return False + + self.audio = dict() + + return True + + def onSessionStart(self, hdrs): + + if not self.__init__(): + self.bye() + self.stopSession() + + self.language = getHeader(hdrs, "P-Language") + + if not self.language: + self.language = " " + + if not self.findAudioMsg(GREETING_MSG, 3): + self.sendBye() + return + + self.enqueue(self.audio[GREETING_MSG], None) + + def onEmptyQueue(self): + + if not self.queueIsEmpty(): + return + + self.sendBye() + + return + + def onBye(self): + + self.db.close() + self.stopSession() + return + + def sendBye(self): + + self.db.close() + self.bye() + self.stopSession() + return + + def findAudioMsg(self, msg, start): + + wav = IvrAudioFile() + + try: + + cursor = self.db.cursor() + + if start > 2: + + cursor.execute("SELECT audio FROM user_audio WHERE application='" + APPLICATION + "' AND message='" + msg + "' AND domain='" + self.dialog.domain + "' AND userid='" + self.dialog.user + "'") + + if cursor.rowcount > 0: + self.getFromTemp(cursor.fetchone()[0], msg, wav) + cursor.close() + return True + + if start > 1: + + cursor.execute("SELECT audio FROM domain_audio WHERE application='" + APPLICATION + "' AND message='" + msg + "' AND domain='" + self.dialog.domain + "' AND language='" + self.language + "'") + + if cursor.rowcount > 0: + self.getFromTemp(cursor.fetchone()[0], msg, wav) + cursor.close() + return True + + cursor.execute("SELECT audio FROM default_audio WHERE application='" + APPLICATION + "' AND message='" + msg + "' AND language='" + (self.language) + "'") + + if cursor.rowcount > 0: + self.getFromTemp(cursor.fetchone()[0], msg, wav) + cursor.close() + return True + else: + error(APPLICATION + ": default " + msg + " is missing!") + cursor.close() + return False + + except MySQLdb.Error, e: + error(APPLICATION + ": error in accessing database: " +\ + str(e.args[0]) + ":" + e.args[1]) + return False + + def getFromTemp(self, audio, msg, wav): + + fp = os.tmpfile() + fp.write(audio) + fp.seek(0) + wav.fpopen("tmp.wav", AUDIO_READ, fp) + self.audio[msg] = wav