Example #1
0
int MOD_CLS_NAME::preload() {
   AmConfigReader cfg;
   if(cfg.loadPluginConf(MOD_NAME)) {
     INFO("no module configuration for '%s' found, not preloading regular expressions\n",
	  MOD_NAME);
     return 0;
   }

   bool failed = false;
   for (std::map<string,string>::const_iterator it =
	  cfg.begin(); it != cfg.end(); it++) {
     if (add_regex(it->first, it->second)) {
       ERROR("compiling regex '%s' for '%s'\n",
	     it->second.c_str(), it->first.c_str());
       failed = true;
     } else {
       DBG("compiled regex '%s' as '%s'\n", it->second.c_str(), it->first.c_str());
     }
   }

   return failed? -1 : 0;
}
Example #2
0
int DSMFactory::onLoad()
{
  if (loaded)
    return 0;
  loaded = true;

  AmConfigReader cfg;
  if(cfg.loadFile(AmConfig::ModConfigPath + string(MOD_NAME ".conf")))
    return -1;
 
  // get application specific global parameters
  configureModule(cfg);
  
  vector<string> prompts_files = 
    explode(cfg.getParameter("load_prompts"), ",");
  for (vector<string>::iterator it=
	 prompts_files.begin(); it != prompts_files.end(); it++) {
    DBG("loading prompts from '%s'\n", it->c_str());
    std::ifstream ifs(it->c_str());
    string s;
    while (ifs.good() && !ifs.eof()) {
      getline(ifs, s);
      if (s.length() && s.find_first_not_of(" \t")!= string::npos &&
	  s[s.find_first_not_of(" \t")] != '#') {
        vector<string> p=explode(s, "=");
	if (p.size()==2) {
	  prompts.setPrompt(p[0], p[1], MOD_NAME);
	  DBG("added prompt '%s' as '%s'\n", 
	      p[0].c_str(), p[1].c_str());
	}
      }
    }
  }

  string prompt_sets_path = cfg.getParameter("prompts_sets_path");

  vector<string> prompt_sets_names = 
    explode(cfg.getParameter("load_prompts_sets"), ",");
  for (vector<string>::iterator it=
	 prompt_sets_names.begin(); it != prompt_sets_names.end(); it++) {
    string fname = prompt_sets_path.empty() ? "": prompt_sets_path + "/";
    fname += *it;
    DBG("loading prompts for '%s' (file '%s')\n", it->c_str(), fname.c_str());
    std::ifstream ifs(fname.c_str());
    string s;
    if (!ifs.good()) {
      WARN("prompts set file '%s' could not be read\n", fname.c_str());
    }
    AmPromptCollection* pc = new AmPromptCollection();
    while (ifs.good() && !ifs.eof()) {
      getline(ifs, s);
      if (s.length() && s.find_first_not_of(" \t")!= string::npos &&
	  s[s.find_first_not_of(" \t")] != '#') {
        vector<string> p=explode(s, "=");
	if (p.size()==2) {
	  pc->setPrompt(p[0], p[1], MOD_NAME);
	  DBG("set '%s' added prompt '%s' as '%s'\n", 
	      it->c_str(), p[0].c_str(), p[1].c_str());
	}
      }
    }
    prompt_sets[*it] = pc;
  }

  string DiagPath = cfg.getParameter("diag_path");
  if (DiagPath.length() && DiagPath[DiagPath.length()-1] != '/')
    DiagPath += '/';

  string ModPath = cfg.getParameter("mod_path");

  string preload_mods = cfg.getParameter("preload_mods");
  vector<string> preload_names = explode(preload_mods, ",");
  if (preload_names.size()) {
    DSMChartReader reader;
    for (vector<string>::iterator it=
	   preload_names.begin(); it != preload_names.end(); it++) {
      DBG("preloading '%s'...\n", it->c_str());
      if (!reader.importModule("import("+*it+")", ModPath)) {
	ERROR("importing module '%s' for preload\n", it->c_str());
	return -1;
      }
      DSMModule* last_loaded = reader.mods.back();
      if (last_loaded) {
 	if (last_loaded->preload()) {
 	  DBG("Error while preloading '%s'\n", it->c_str());
 	  return -1;
 	}
      }
    }
  }
  // TODO: pass preloaded mods to chart reader

  string LoadDiags = cfg.getParameter("load_diags");
  vector<string> diags_names = explode(LoadDiags, ",");
  for (vector<string>::iterator it=
	 diags_names.begin(); it != diags_names.end(); it++) {
    if (!diags.loadFile(DiagPath+*it+".dsm", *it, ModPath)) {
      ERROR("loading %s from %s\n", 
	    it->c_str(), (DiagPath+*it+".dsm").c_str());
      return -1;
    }
  }

  InboundStartDiag = cfg.getParameter("inbound_start_diag");
  if (InboundStartDiag.empty()) {
    INFO("no 'inbound_start_diag' set in config. inbound calls disabled.\n");
  }
  OutboundStartDiag = cfg.getParameter("outbound_start_diag");
  if (OutboundStartDiag.empty()) {
    INFO("no 'outbound_start_diag' set in config. outbound calls disabled.\n");
  }

  for (std::map<string,string>::const_iterator it = 
	 cfg.begin(); it != cfg.end(); it++)
    config[it->first] = it->second;

  RunInviteEvent = cfg.getParameter("run_invite_event")=="yes";

  return 0;
}
Example #3
0
bool DSMFactory::loadConfig(const string& conf_file_name, const string& conf_name, 
			    bool live_reload, DSMStateDiagramCollection* m_diags) {

  string script_name = conf_name.substr(0, conf_name.length()-5); // - .conf
  DBG("loading %s from %s ...\n", script_name.c_str(), conf_file_name.c_str());
  AmConfigReader cfg;
  if(cfg.loadFile(conf_file_name))
    return false;

  DSMScriptConfig script_config;
  script_config.RunInviteEvent = 
    cfg.getParameter("run_invite_event")=="yes";

  script_config.SetParamVariables = 
    cfg.getParameter("set_param_variables")=="yes";

  script_config.config_vars.insert(cfg.begin(), cfg.end());

  if (live_reload) {
    INFO("live DSM config reload does NOT reload prompts and prompt sets!\n");
    INFO("(see http://tracker.iptel.org/browse/SEMS-68)\n");
  } else {
    if (!loadPrompts(cfg))
      return false;

    if (!loadPromptSets(cfg))
      return false;
  }

  DSMStateDiagramCollection* used_diags;
  if (m_diags != NULL)
    used_diags = m_diags;     // got this from caller (main diags)
  else {
    // create a new set of diags 
    used_diags = script_config.diags = new DSMStateDiagramCollection();
  }

  if (!loadDiags(cfg, used_diags))
    return false;

  vector<string> registered_apps;
  if (!registerApps(cfg, used_diags, registered_apps))
    return false;

  ScriptConfigs_mut.lock();
  try {
    // set ScriptConfig to this for all registered apps' names
    for (vector<string>::iterator reg_app_it=
	   registered_apps.begin(); reg_app_it != registered_apps.end(); reg_app_it++) {
      string& app_name = *reg_app_it;
      // dispose of the old one, if it exists
      map<string, DSMScriptConfig>::iterator it=ScriptConfigs.find(app_name);
      if (it != ScriptConfigs.end()) {
	// may be in use by active call - don't delete but save to 
	// old_diags for garbage collection (destructor)
	if (it->second.diags != NULL)
	  old_diags.insert(it->second.diags);   
      }
      
      // overwrite with new config
      ScriptConfigs[app_name] = script_config;
    }
  } catch(...) {
    ScriptConfigs_mut.unlock();
    throw;
  }
  ScriptConfigs_mut.unlock();

  return true;
}
Example #4
0
int DSMFactory::onLoad()
{
  if (loaded)
    return 0;
  loaded = true;

  AmConfigReader cfg;
  if(cfg.loadFile(AmConfig::ModConfigPath + string(MOD_NAME ".conf")))
    return -1;
 
  // get application specific global parameters
  configureModule(cfg);

  DebugDSM = cfg.getParameter("debug_raw_dsm") == "yes";
 
  if (!loadPrompts(cfg))
    return -1;

  if (!loadPromptSets(cfg))
    return -1;

  if (!loadDiags(cfg, MainScriptConfig.diags))
    return -1;

  vector<string> registered_apps;
  if (!registerApps(cfg, MainScriptConfig.diags, registered_apps))
    return -1;

  InboundStartDiag = cfg.getParameter("inbound_start_diag");
  if (InboundStartDiag.empty()) {
    INFO("no 'inbound_start_diag' set in config. "
	 "inbound calls with application 'dsm' disabled.\n");
  }

  OutboundStartDiag = cfg.getParameter("outbound_start_diag");
  if (OutboundStartDiag.empty()) {
    INFO("no 'outbound_start_diag' set in config. "
	 "outbound calls with application 'dsm' disabled.\n");
  }

  if (!InboundStartDiag.empty())
    AmPlugIn::instance()->registerFactory4App("dsm",this);

  MainScriptConfig.config_vars.insert(cfg.begin(), cfg.end());

//   for (std::map<string,string>::const_iterator it = 
// 	 cfg.begin(); it != cfg.end(); it++) 
//     MainScriptConfig.config_vars[it->first] = it->second;

  MainScriptConfig.RunInviteEvent = cfg.getParameter("run_invite_event")=="yes";

  MainScriptConfig.SetParamVariables = cfg.getParameter("set_param_variables")=="yes";

#ifdef USE_MONITORING
  string monitoring_full_callgraph = cfg.getParameter("monitoring_full_stategraph");
  MonitoringFullCallgraph = monitoring_full_callgraph == "yes";
  DBG("%sogging full call graph (states) to monitoring.\n",
      MonitoringFullCallgraph?"L":"Not l");

  string monitoring_full_transitions = cfg.getParameter("monitoring_full_transitions");
  MonitoringFullTransitions = monitoring_full_transitions == "yes";
  DBG("%sogging full call graph (transitions) to monitoring.\n",
      MonitoringFullTransitions?"L":"Not l");

  string cfg_usecaller = cfg.getParameter("monitor_select_use_caller");
  if (cfg_usecaller.empty() || cfg_usecaller=="from") 
    MonSelectCaller = MonSelect_FROM;
  else if (cfg_usecaller=="no") 
    MonSelectCaller = MonSelect_NONE;
  else if (cfg_usecaller=="pai") 
    MonSelectCaller = MonSelect_PAI;
  else {
    ERROR("monitor_select_use_caller value '%s' not understood\n",
	  cfg_usecaller.c_str());
  }

  string cfg_usecallee = cfg.getParameter("monitor_select_use_callee");
  if (cfg_usecallee.empty() || cfg_usecallee=="ruri") 
    MonSelectCallee = MonSelect_RURI;
  else if (cfg_usecallee=="no") 
    MonSelectCallee = MonSelect_NONE;
  else if (cfg_usecallee=="from") 
    MonSelectCallee = MonSelect_FROM;
  else {
    ERROR("monitor_select_use_callee value '%s' not understood\n",
	  cfg_usecallee.c_str());
  }
#endif

  string conf_d_dir = cfg.getParameter("conf_dir");
  if (!conf_d_dir.empty()) {
    if (conf_d_dir[conf_d_dir.length()-1] != '/')
      conf_d_dir += '/';

    DBG("processing configurations in '%s'...\n", conf_d_dir.c_str());
    int err=0;
    struct dirent* entry;
    DIR* dir = opendir(conf_d_dir.c_str());
    
    if(!dir){
      ERROR("config loader (%s): %s\n", 
	    conf_d_dir.c_str(), strerror(errno));
      return -1;
    }
    while( ((entry = readdir(dir)) != NULL) && (err == 0) ){
      string conf_name = string(entry->d_name);
      
      if(conf_name.find(".conf",conf_name.length()-5) == string::npos ){
	continue;
      }
          
      string conf_file_name = conf_d_dir + conf_name;

      DBG("loading %s ...\n",conf_file_name.c_str());
      if (!loadConfig(conf_file_name, conf_name, false, NULL)) 
	return -1;

    }
    closedir(dir);
  }
  return 0;
}
Example #5
0
bool IvrFactory::loadScript(const string& path)
{
  PYLOCK;
    
  PyObject *modName=NULL,*mod=NULL,*dict=NULL,*dlg_class=NULL,*config=NULL;

  // load module configuration
  AmConfigReader cfg;
  string cfg_file = add2path(AmConfig::ModConfigPath,1,(path + ".conf").c_str());
  config = PyDict_New();
  if(!config){
    ERROR("could not allocate new dict for config\n");
    goto error2;
  }

  if(cfg.loadFile(cfg_file)){
    WARN("could not load config file at %s\n",cfg_file.c_str());
  } else {
    for(map<string,string>::const_iterator it = cfg.begin();
	it != cfg.end(); it++){
      PyDict_SetItem(config, 
		     PyString_FromString(it->first.c_str()),
		     PyString_FromString(it->second.c_str()));
    }
  }

  // set config ivr ivr_module while loading
  Py_INCREF(config);
  PyObject_SetAttrString(ivr_module,"config",config);
  
  // load module
  modName = PyString_FromString(path.c_str());
  
  mod     = PyImport_Import(modName);
  if (NULL != config) {
    // remove config ivr ivr_module while loading
    PyObject_DelAttrString(ivr_module, "config");
    Py_DECREF(config);
  }
    
  if(!mod){
    PyErr_Print();
    WARN("IvrFactory: Failed to load \"%s\"\n", path.c_str());

    // before python 2.4,
    // it can happen that the module
    // is still in the dictionnary.
    dict = PyImport_GetModuleDict();
    Py_INCREF(dict);
    if(PyDict_Contains(dict,modName)){
      PyDict_DelItem(dict,modName);
    }
    Py_DECREF(dict);
    Py_DECREF(modName);

    return false;
  }

  Py_DECREF(modName);
  dict = PyModule_GetDict(mod);
  dlg_class = PyDict_GetItemString(dict, "IvrDialog");

  if(!dlg_class){

    PyErr_Print();
    WARN("IvrFactory: class IvrDialog not found in \"%s\"\n", path.c_str());
    goto error1;
  }

  Py_INCREF(dlg_class);

  if(!PyObject_IsSubclass(dlg_class,(PyObject*)&IvrDialogBaseType)){

    WARN("IvrFactory: in \"%s\": IvrDialog is not a subtype of IvrDialogBase\n",
	 path.c_str());
    goto error2;
  }

  PyObject_SetAttrString(mod,"config",config);

  mod_reg.insert(std::make_pair(path,
			        IvrScriptDesc(mod,dlg_class)));

  return true;

 error2:
  Py_DECREF(dlg_class);
 error1:
  Py_DECREF(mod);

  return false;
}
Example #6
0
bool PySemsFactory::loadScript(const string& path)
{
  PYLOCK;
    
  PyObject *modName,*mod,*dict, *dlg_class, *config=NULL;
  PySemsScriptDesc::DialogType dt = PySemsScriptDesc::None;


  modName = PyString_FromString(path.c_str());
  mod     = PyImport_Import(modName);

  AmConfigReader cfg;
  string cfg_file = add2path(AmConfig::ModConfigPath,1,(path + ".conf").c_str());

  Py_DECREF(modName);

  if(!mod){
    PyErr_Print();
    WARN("PySemsFactory: Failed to load \"%s\"\n", path.c_str());

    dict = PyImport_GetModuleDict();
    Py_INCREF(dict);
    PyDict_DelItemString(dict,path.c_str());
    Py_DECREF(dict);

    return false;
  }

  dict = PyModule_GetDict(mod);
  dlg_class = PyDict_GetItemString(dict, "PySemsScript");

  if(!dlg_class){

    PyErr_Print();
    WARN("PySemsFactory: class PySemsDialog not found in \"%s\"\n", path.c_str());
    goto error1;
  }

  Py_INCREF(dlg_class);
    
  if(PyObject_IsSubclass(dlg_class,(PyObject *)sipClass_PySemsDialog)) {
    dt = PySemsScriptDesc::Dialog;
    DBG("Loaded a Dialog Script.\n");
  } else if (PyObject_IsSubclass(dlg_class,(PyObject *)sipClass_PySemsB2BDialog)) {
    DBG("Loaded a B2BDialog Script.\n");
    dt = PySemsScriptDesc::B2BDialog;
  } else if (PyObject_IsSubclass(dlg_class,(PyObject *)sipClass_PySemsB2ABDialog)) {
    DBG("Loaded a B2ABDialog Script.\n");
    dt = PySemsScriptDesc::B2ABDialog;
  } else {
    WARN("PySemsFactory: in \"%s\": PySemsScript is not a "
	 "subtype of PySemsDialog\n", path.c_str());

    goto error2;
  }

  if(cfg.loadFile(cfg_file)){
    ERROR("could not load config file at %s\n",cfg_file.c_str());
    goto error2;
  }

  config = PyDict_New();
  if(!config){
    ERROR("could not allocate new dict for config\n");
    goto error2;
  }

  for(map<string,string>::const_iterator it = cfg.begin();
      it != cfg.end(); it++){
	
    PyDict_SetItem(config, 
		   PyString_FromString(it->first.c_str()),
		   PyString_FromString(it->second.c_str()));
  }

  PyObject_SetAttrString(mod,"config",config);

  mod_reg.insert(std::make_pair(path,
			        PySemsScriptDesc(mod,dlg_class, dt)));

  return true;

 error2:
  Py_DECREF(dlg_class);
 error1:
  Py_DECREF(mod);

  return false;
}