Skip to content

Commit

Permalink
Merge pull request #200 from dronord/early_announce
Browse files Browse the repository at this point in the history
Looks OK, but I have not test the code.
  • Loading branch information
juha-h authored Jul 29, 2023
2 parents 0b3aebe + b6d99af commit 5e8b16d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 82 deletions.
2 changes: 1 addition & 1 deletion apps/early_announce/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ EarlyAnnounce.cpp

IF(MYSQLCPPCONN_FOUND)
INCLUDE_DIRECTORIES(${MYSQLCPPCONN_INCLUDE_DIR})
ADD_DEFINITIONS(-DUSE_MYSQL)
ADD_DEFINITIONS(-DWITH_MYSQL)
SET(sems_module_libs ${MYSQLCPPCONN_LIBRARIES})
ENDIF(MYSQLCPPCONN_FOUND)

Expand Down
158 changes: 88 additions & 70 deletions apps/early_announce/EarlyAnnounce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,24 @@

#define MOD_NAME "early_announce"

#ifdef USE_MYSQL
#ifdef WITH_MYSQL
#define DEFAULT_AUDIO_TABLE "default_audio"
#define DOMAIN_AUDIO_TABLE "domain_audio"
#define USER_AUDIO_TABLE "user_audio"
#endif

EXPORT_SESSION_FACTORY(EarlyAnnounceFactory,MOD_NAME);

#ifdef USE_MYSQL
string EarlyAnnounceFactory::AnnounceApplication;
string EarlyAnnounceFactory::AnnounceMessage;
string EarlyAnnounceFactory::DefaultLanguage;
#else
string EarlyAnnounceFactory::AnnouncePath;
string EarlyAnnounceFactory::AnnounceFile;
#ifdef WITH_MYSQL
string EarlyAnnounceFactory::application;
string EarlyAnnounceFactory::announce_message;
string EarlyAnnounceFactory::default_language;
#endif
string EarlyAnnounceFactory::announce_path;
string EarlyAnnounceFactory::default_file;

void (*get_announce_file)(const string &user, const string &domain,
const string &language, string *announce_file) = 0;

EarlyAnnounceFactory::ContB2B EarlyAnnounceFactory::ContinueB2B =
EarlyAnnounceFactory::Never;
Expand All @@ -58,32 +60,34 @@ EarlyAnnounceFactory::EarlyAnnounceFactory(const string& _app_name)
{
}

#ifdef USE_MYSQL
#ifdef WITH_MYSQL

static sql::Driver *driver;
static sql::Connection *connection;

int get_announce_msg(string application, string message, string user,
string domain, string language, string *audio_file)
int get_announce_msg(const string &user, const string &domain,
const string &language, string *audio_file)
{
string query_string;
string application (EarlyAnnounceFactory::application);
string message (EarlyAnnounceFactory::announce_message);

if (!user.empty()) {
*audio_file = string("/tmp/") + application + "_" +
*audio_file = string("/tmp/") + application + "_" +
message + "_" + domain + "_" + user + ".wav";
query_string = "select audio from " + string(USER_AUDIO_TABLE) +
" where application='" + application + "' and message='" +
message + "' and userid='" + user + "' and domain='" +
domain + "'";
} else if (!domain.empty()) {
*audio_file = string("/tmp/") + application + "_" +
*audio_file = string("/tmp/") + application + "_" +
message + "_" + domain + "_" + language + ".wav";
query_string = "select audio from " + string(DOMAIN_AUDIO_TABLE) +
" where application='" + application + "' and message='" +
message + "' and domain='" + domain + "' and language='" +
language + "'";
} else {
*audio_file = string("/tmp/") + application + "_" +
*audio_file = string("/tmp/") + application + "_" +
message + "_" + language + ".wav";
query_string = "select audio from " + string(DEFAULT_AUDIO_TABLE) +
" where application='" + application + "' and message='" +
Expand Down Expand Up @@ -123,8 +127,39 @@ int get_announce_msg(string application, string message, string user,
}
}

void get_announce_file_mysql(const string &user, const string &domain,
const string &language_param, string *announce_file)
{
string language(language_param);
if (language.empty()) language = EarlyAnnounceFactory::default_language;

get_announce_msg(user, domain, "", announce_file);
if (!announce_file->empty()) return;
get_announce_msg("", domain, language, announce_file);
if (!announce_file->empty()) return;
get_announce_msg("", "", language, announce_file);
}

#endif

void get_announce_file_fs(const string &user, const string &domain,
const string &language, string *announce_file)
{
*announce_file = EarlyAnnounceFactory::announce_path + domain
+ "/" + user + ".wav";

DBG("trying '%s'\n",announce_file->c_str());
if (file_exists(*announce_file))
return;

*announce_file = EarlyAnnounceFactory::announce_path + user + ".wav";
DBG("trying '%s'\n",announce_file->c_str());
if (file_exists(*announce_file))
return;

*announce_file = EarlyAnnounceFactory::announce_path + EarlyAnnounceFactory::default_file;
}

int EarlyAnnounceFactory::onLoad()
{
AmConfigReader cfg;
Expand All @@ -148,7 +183,9 @@ int EarlyAnnounceFactory::onLoad()
}
}

#ifdef USE_MYSQL
if (cfg.getParameter("use_mysql") == "yes") {

#ifdef WITH_MYSQL

/* Get default audio from MySQL */

Expand Down Expand Up @@ -180,19 +217,19 @@ int EarlyAnnounceFactory::onLoad()

mysql_ca_cert = cfg.getParameter("mysql_ca_cert");

AnnounceApplication = cfg.getParameter("application");
if (AnnounceApplication.empty()) {
AnnounceApplication = MOD_NAME;
application = cfg.getParameter("application");
if (application.empty()) {
application = MOD_NAME;
}

AnnounceMessage = cfg.getParameter("message");
if (AnnounceMessage.empty()) {
AnnounceMessage = "greeting_msg";
announce_message = cfg.getParameter("message");
if (announce_message.empty()) {
announce_message = "greeting_msg";
}

DefaultLanguage = cfg.getParameter("default_language");
if (DefaultLanguage.empty()) {
DefaultLanguage = "en";
default_language = cfg.getParameter("default_language");
if (default_language.empty()) {
default_language = "en";
}

try {
Expand Down Expand Up @@ -227,9 +264,11 @@ int EarlyAnnounceFactory::onLoad()
return -1;
}

get_announce_file = get_announce_file_mysql;

string announce_file;
if (!get_announce_msg(AnnounceApplication, AnnounceMessage, "", "",
DefaultLanguage, &announce_file)) {
if (!get_announce_msg("", "",
default_language, &announce_file)) {
return -1;
}
if (announce_file.empty()) {
Expand All @@ -239,23 +278,34 @@ int EarlyAnnounceFactory::onLoad()

#else

ERROR("MySQL support not compiled for " MOD_NAME "\n");
return -1;

#endif

} // use_mysql
else
{

/* Get default audio from file system */

AnnouncePath = cfg.getParameter("announce_path",ANNOUNCE_PATH);
if( !AnnouncePath.empty()
&& AnnouncePath[AnnouncePath.length()-1] != '/' )
AnnouncePath += "/";
announce_path = cfg.getParameter("announce_path",ANNOUNCE_PATH);
if( !announce_path.empty()
&& announce_path[announce_path.length()-1] != '/' )
announce_path += "/";

AnnounceFile = cfg.getParameter("default_announce",ANNOUNCE_FILE);
default_file = cfg.getParameter("default_announce",ANNOUNCE_FILE);

string announce_file = AnnouncePath + AnnounceFile;
string announce_file = announce_path + default_file;
if(!file_exists(announce_file)){
ERROR("default file for " MOD_NAME " module does not exist ('%s').\n",
announce_file.c_str());
return -1;
}

#endif
get_announce_file = get_announce_file_fs;

}

return 0;
}
Expand All @@ -278,44 +328,12 @@ void EarlyAnnounceDialog::onInvite(const AmSipRequest& req)
AmSession* EarlyAnnounceFactory::onInvite(const AmSipRequest& req, const string& app_name,
const map<string,string>& app_params)
{
string iptel_app_param = getHeader(req.hdrs, PARAM_HDR, true);
string language = get_header_keyvalue(iptel_app_param,"Language");
string announce_file = "";

#ifdef USE_MYSQL

string iptel_app_param = getHeader(req.hdrs, PARAM_HDR, true);
string language = get_header_keyvalue(iptel_app_param,"Language");
string announce_file = "";

if (language.empty()) language = DefaultLanguage;

get_announce_msg(AnnounceApplication, AnnounceMessage, req.user,
req.domain, "", &announce_file);
if (!announce_file.empty()) goto end;
get_announce_msg(AnnounceApplication, AnnounceMessage, "", req.domain,
language, &announce_file);
if (!announce_file.empty()) goto end;
get_announce_msg(AnnounceApplication, AnnounceMessage, "", "", language,
&announce_file);

#else

string announce_path = AnnouncePath;
string announce_file = announce_path + req.domain
+ "/" + req.user + ".wav";

DBG("trying '%s'\n",announce_file.c_str());
if(file_exists(announce_file))
goto end;

announce_file = announce_path + req.user + ".wav";
DBG("trying '%s'\n",announce_file.c_str());
if(file_exists(announce_file))
goto end;

announce_file = AnnouncePath + AnnounceFile;

#endif

end:
get_announce_file(req.domain, req.user, language, &announce_file);

return new EarlyAnnounceDialog(announce_file);
}

Expand Down
16 changes: 8 additions & 8 deletions apps/early_announce/EarlyAnnounce.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <string>
using std::string;

#ifdef USE_MYSQL
#ifdef WITH_MYSQL
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
Expand All @@ -47,14 +47,14 @@ class EarlyAnnounceFactory: public AmSessionFactory
{
public:

#ifdef USE_MYSQL
static string AnnounceApplication;
static string AnnounceMessage;
static string DefaultLanguage;
#else
static string AnnouncePath;
static string AnnounceFile;
#ifdef WITH_MYSQL
static string application;
static string announce_message;
static string default_language;
#endif
static string announce_path;
static string default_file;

enum ContB2B {
Always = 0,
Never,
Expand Down
10 changes: 7 additions & 3 deletions doc/Readme.early_announce.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ are relayed into the other leg.

Announcement file played from file system
-----------------------------------------
If early_announce application was compiled WITHOUT -DUSE_MYSQL option,
If use_mysql=no in early_announce.conf,
it searches for files to play following these rules:

1) <announce_path>/[RURI-Domain]/[RURI-User].wav
2) <announce_path>/[RURI-User].wav
3) <announce_path>/<default_announce>

Example sems (early_announce.conf) configuration for file system audio:
use_mysql=no
announce_path=wav/
default_announce=default_en.wav

Announcement file from MySQL DB
-------------------------------
If early_announce application was compiled with -DUSE_MYSQL option, it
supports third Session Parameter
If early_announce application was compiled with -DWITH_MYSQL option,
and use_mysql=yes in early_announce.conf, it supports third Session Parameter

Language two letter string or empty string (e.g. en)

Expand All @@ -54,6 +55,9 @@ file is fetched from database, it is stored in /tmp directory.

Example sems (early_announce.conf) configuration for MySQL audio:

# Use MySQL, not file system
use_mysql=yes

# Host where MySQL server is running (optional, defaults to 'localhost')
mysql_host=localhost

Expand Down

0 comments on commit 5e8b16d

Please sign in to comment.