/* * $Id$ * * Copyright (C) 2009 Teltech Systems Inc. * * 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 * * For a license to use the SEMS software under conditions * other than those described here, or to purchase support for this * software, please contact iptel.org by e-mail at the following addresses: * info@iptel.org * * SEMS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "ModUtils.h" #include "log.h" #include "AmUtils.h" #include #include "DSMSession.h" #include "AmSession.h" SC_EXPORT(SCUtilsModule); SCUtilsModule::SCUtilsModule() { } SCUtilsModule::~SCUtilsModule() { } void splitCmd(const string& from_str, string& cmd, string& params) { size_t b_pos = from_str.find('('); if (b_pos != string::npos) { cmd = from_str.substr(0, b_pos); params = from_str.substr(b_pos + 1, from_str.rfind(')') - b_pos -1); } else cmd = from_str; } DSMAction* SCUtilsModule::getAction(const string& from_str) { string cmd; string params; splitCmd(from_str, cmd, params); DEF_CMD("utils.playCountRight", SCUPlayCountRightAction); DEF_CMD("utils.playCountLeft", SCUPlayCountLeftAction); DEF_CMD("utils.getNewId", SCGetNewIdAction); DEF_CMD("utils.spell", SCUSpellAction); return NULL; } DSMCondition* SCUtilsModule::getCondition(const string& from_str) { string cmd; string params; splitCmd(from_str, cmd, params); return NULL; } bool utils_play_count(DSMSession* sc_sess, unsigned int cnt, const string& basedir, const string& suffix, bool right) { if (cnt <= 20) { sc_sess->playFile(basedir+int2str(cnt)+suffix, false); return false; } for (int i=9;i>1;i--) { div_t num = div(cnt, (int)exp10(i)); if (num.quot) { sc_sess->playFile(basedir+int2str(int(num.quot * exp10(i)))+suffix, false); } cnt = num.rem; } if (!cnt) return false; if ((cnt <= 20) || (!(cnt%10))) { sc_sess->playFile(basedir+int2str(cnt)+suffix, false); return false; } div_t num = div(cnt, 10); if (right) { // language has single digits before 10s sc_sess->playFile(basedir+int2str(num.quot * 10)+suffix, false); sc_sess->playFile(basedir+("x"+int2str(num.rem))+suffix, false); } else { // language has single digits before 10s sc_sess->playFile(basedir+("x"+int2str(num.rem))+suffix, false); sc_sess->playFile(basedir+int2str(num.quot * 10)+suffix, false); } return false; } CONST_ACTION_2P(SCUPlayCountRightAction, ',', true); EXEC_ACTION_START(SCUPlayCountRightAction) { string basedir = resolveVars(par2, sess, sc_sess, event_params); unsigned int cnt = 0; if (str2i(resolveVars(par1, sess, sc_sess, event_params),cnt)) { ERROR("could not parse count '%s'\n", resolveVars(par1, sess, sc_sess, event_params).c_str()); sc_sess->SET_ERRNO(DSM_ERRNO_UNKNOWN_ARG); return false; } return utils_play_count(sc_sess, cnt, basedir, ".wav", true); } EXEC_ACTION_END; CONST_ACTION_2P(SCUPlayCountLeftAction, ',', true); EXEC_ACTION_START(SCUPlayCountLeftAction) { string basedir = resolveVars(par2, sess, sc_sess, event_params); unsigned int cnt = 0; if (str2i(resolveVars(par1, sess, sc_sess, event_params),cnt)) { ERROR("could not parse count '%s'\n", resolveVars(par1, sess, sc_sess, event_params).c_str()); sc_sess->SET_ERRNO(DSM_ERRNO_UNKNOWN_ARG); return false; } return utils_play_count(sc_sess, cnt, basedir, ".wav", false); } EXEC_ACTION_END; EXEC_ACTION_START(SCGetNewIdAction) { string d = resolveVars(arg, sess, sc_sess, event_params); sc_sess->var[d]=AmSession::getNewId(); } EXEC_ACTION_END; CONST_ACTION_2P(SCUSpellAction, ',', true); EXEC_ACTION_START(SCUSpellAction) { string basedir = resolveVars(par2, sess, sc_sess, event_params); string play_string = resolveVars(par1, sess, sc_sess, event_params); DBG("spelling '%s'\n", play_string.c_str()); for (size_t i=0;iplayFile(basedir+play_string[i]+".wav", false); } EXEC_ACTION_END;