void SIPRegistrarClient::invoke(const string& method, const AmArg& args, 
				AmArg& ret)
{
  if(method == "createRegistration"){
    ret.push(createRegistration(args.get(0).asCStr(),
				args.get(1).asCStr(),
				args.get(2).asCStr(),
				args.get(3).asCStr(),
				args.get(4).asCStr(),
				args.get(5).asCStr()
				).c_str());
  }
  else if(method == "removeRegistration"){
    removeRegistration(args.get(0).asCStr());
  } 
  else if(method == "getRegistrationState"){
    unsigned int state;
    unsigned int expires;
    if (instance()->getRegistrationState(args.get(0).asCStr(), 
					 state, expires)){
      ret.push(1);
      ret.push((int)state);
      ret.push((int)expires);
    } else {
      ret.push(AmArg((int)0));
    }
  } else if(method == "_list"){ 
    ret.push(AmArg("createRegistration"));
    ret.push(AmArg("removeRegistration"));
    ret.push(AmArg("getRegistrationState"));
  }  else
    throw AmDynInvoke::NotImplemented(method);
}
Example #2
0
void DIDial::invoke(const string& method, const AmArg& args, AmArg& ret)
{
    if(method == "dial"){
       ret.push(dialout(args.get(0).asCStr(), 
			args.get(1).asCStr(), 
			args.get(2).asCStr(), 
			args.get(3).asCStr()).c_str());
    } else if(method == "dial_auth"){
       ret.push(dialout_auth(args.get(0).asCStr(), 
			args.get(1).asCStr(), 
			args.get(2).asCStr(), 
			args.get(3).asCStr(),
			args.get(4).asCStr(), 
			args.get(5).asCStr(), 
			args.get(6).asCStr()
			).c_str());
    } else if(method == "dial_pin"){
       ret.push(dialout_pin(args.get(0).asCStr(), 
			    args.get(1).asCStr(), 
			    args.get(2).asCStr(), 
			    args.get(3).asCStr()
			).c_str());
    } else if(method == "help"){
      ret.push("dial <application> <user> <from> <to>");
      ret.push("dial_auth <application> <user> <from> <to> <realm> <auth_user> <auth_pwd>");
      ret.push("dial_pin <application> <dialout pin> <local_user> <to_user>");
    } else if(method == "_list"){ 
      ret.push(AmArg("dial"));
      ret.push(AmArg("dial_auth"));
      ret.push(AmArg("dial_pin"));
      ret.push(AmArg("help"));
    } else 
	throw AmDynInvoke::NotImplemented(method);
}
Example #3
0
void XMLRPC2DIServer::xmlrpcval2amarg(XmlRpcValue& v, AmArg& a, 
				      unsigned int start_index) {
  if (v.valid()) {
    for (int i=start_index; i<v.size();i++) {
      switch (v[i].getType()) {
      case XmlRpcValue::TypeInt:   { a.push(AmArg((int)v[i]));    }  break;
      case XmlRpcValue::TypeDouble:{ a.push(AmArg((double)v[i])); }  break;
      case XmlRpcValue::TypeString:{ a.push(AmArg(((string)v[i]).c_str())); }  break;
	// TODO: support more types (datetime, struct, ...)
      default:     throw XmlRpcException("unsupported parameter type", 400);
      };
    }
  } 
}
Example #4
0
void XMLRPC2DI::invoke(const string& method, 
		       const AmArg& args, AmArg& ret) {

  if(method == "newConnection"){
    args.assertArrayFmt("ssis"); // app, server, port, uri
    newConnection(args, ret);
  } else if(method == "sendRequest"){
    args.assertArrayFmt("ssa");   // app, method, args
    sendRequest(args, ret);
  } else if(method == "_list"){ 
    ret.push(AmArg("newConnection"));
    ret.push(AmArg("sendRequest"));
  }  else
    throw AmDynInvoke::NotImplemented(method);
  
}
void DSMCall::onInvite(const AmSipRequest& req) {
  // make B2B dialogs work in onInvite as well
  invite_req = req;

  if (!process_invite) {
    // re-INVITEs
    AmB2BCallerSession::onInvite(req);
    return;
  }
  process_invite = false;
    
  bool run_session_invite = engine.onInvite(req, this);

  avar[DSM_AVAR_REQUEST] = AmArg(&req);

  DBG("before runEvent(this, this, DSMCondition::Invite);\n");
  AmSipDialog::Status old_st = dlg.getStatus();
  engine.runEvent(this, this, DSMCondition::Invite, NULL);
  avar.erase(DSM_AVAR_REQUEST);

  if ( old_st != dlg.getStatus()
       //checkVar(DSM_CONNECT_SESSION, DSM_CONNECT_SESSION_FALSE)
      ) {
    DBG("session choose to not connect media\n");
    run_session_invite = false;     // don't accept audio 
  }    

  if (run_session_invite) 
    AmB2BCallerSession::onInvite(req);
}
void DSMCall::onRemoteDisappeared(const AmSipReply& reply) {
  map<string, string> params;
  params["code"] = int2str(reply.code);
  params["reason"] = reply.reason;
  params["hdrs"] = reply.hdrs;
  params["cseq"] = int2str(reply.cseq);

  params["dlg_status"] = dlgStatusStr(dlg.getStatus());

  // pass AmSipReply for use by modules
  DSMSipReply* dsm_reply = new DSMSipReply(&reply);
  avar[DSM_AVAR_REPLY] = AmArg(dsm_reply);

  engine.runEvent(this, this, DSMCondition::RemoteDisappeared, &params);

  delete dsm_reply;
  avar.erase(DSM_AVAR_REPLY);

  if (checkParam(DSM_PROCESSED, DSM_TRUE, &params)) {
    DBG("DSM script processed SIP onRemoteDisappeared reply '%u %s', returning\n",
	reply.code, reply.reason.c_str());
    return;
  }

  AmB2BCallerSession::onRemoteDisappeared(reply);
}
void DSMCall::onSipRequest(const AmSipRequest& req) {

  if (checkVar(DSM_ENABLE_REQUEST_EVENTS, DSM_TRUE)) {
    map<string, string> params;
    params["method"] = req.method;
    params["r_uri"] = req.r_uri;
    params["from"] = req.from;
    params["to"] = req.to;
    params["hdrs"] = req.hdrs;
    params["cseq"] = int2str(req.cseq);

    // pass AmSipRequest for use by mod_dlg
    DSMSipRequest* sip_req = new DSMSipRequest(&req);
    avar[DSM_AVAR_REQUEST] = AmArg(sip_req);
    
    engine.runEvent(this, this, DSMCondition::SipRequest, &params);

    delete sip_req;
    avar.erase(DSM_AVAR_REQUEST);

    if (checkParam(DSM_PROCESSED, DSM_TRUE, &params)) {
      DBG("DSM script processed SIP request '%s', returning\n", 
	  req.method.c_str());
      return;
    }
  }

  AmB2BCallerSession::onSipRequest(req);  
}
Example #8
0
void JsonRPCServerModule::invoke(const string& method, 
				 const AmArg& args, AmArg& ret) {
  if (method == "execRpc"){

    // todo: add connection id
    args.assertArrayFmt("sssisis");   // evq_link, notificationReceiver, requestReceiver, 
                                      // flags(i), host, port (i), method, [params]
    if (args.size() > 7)  {
      if (!isArgArray(args.get(7)) && !isArgStruct(args.get(7))) {
	ERROR("internal error: params to JSON-RPC must be struct or array\n");
	throw AmArg::TypeMismatchException();
      }
    }
    execRpc(args, ret);
    // sendRequestList(args, ret);
  } else if (method == "sendMessage"){
    args.assertArrayFmt("sisss");          // conn_id, type, method, id, reply_sink, [params]
    if (args.size() > 5) {
      if (!isArgArray(args.get(5)) && !isArgStruct(args.get(5))) {
	ERROR("internal error: params to JSON-RPC must be struct or array\n");
	throw AmArg::TypeMismatchException();
      }
    }
    sendMessage(args, ret);
  } else if (method == "execServerFunction"){ 
    args.assertArrayFmt("ss");          // method, id, params
    JsonRpcServer::execRpc(args.get(0).asCStr(), args.get(1).asCStr(), args.get(2), ret);
    // JsonRpcServer::execRpc(args, ret);
  } else if (method == "getServerPort"){
    ret.push(port);
  } else if(method == "_list"){ 
    ret.push(AmArg("execRpc"));
    ret.push(AmArg("sendMessage"));
    ret.push(AmArg("getServerPort"));
    ret.push(AmArg("execServerFunction"));
    // ret.push(AmArg("newConnection"));
    // ret.push(AmArg("sendRequest"));
    // ret.push(AmArg("sendRequestList"));
  }  else
    throw AmDynInvoke::NotImplemented(method);  
}
Example #9
0
void XMLRPC2DIServer::xmlrpcval2amarg(XmlRpcValue& v, AmArg& a, 
				      unsigned int start_index) {
  if (v.valid()) {
    for (int i=start_index; i<v.size();i++) {
      switch (v[i].getType()) {
      case XmlRpcValue::TypeInt:   { /* DBG("X->A INT\n");*/ a.push(AmArg((int)v[i]));    }  break;
      case XmlRpcValue::TypeDouble:{ /* DBG("X->A DBL\n");*/ a.push(AmArg((double)v[i])); }  break;
      case XmlRpcValue::TypeString:{ /* DBG("X->A STR\n");*/ a.push(AmArg(((string)v[i]).c_str())); }  break;
      case XmlRpcValue::TypeArray: { 
	// DBG("X->A ARR\n"); 
	a.push(AmArg());
	a[a.size()-1].assertArray(0);
	AmArg arr; 
	xmlrpcval2amarg(v[i], a[a.size()-1], 0);
      } break;
	// TODO: support more types (datetime, struct, ...)
      default:     throw XmlRpcException("unsupported parameter type", 400);
      };
    }
  } 
}
Example #10
0
// AmB2BSession methods
bool DSMCall::onOtherBye(const AmSipRequest& req) {
    DBG("* Got BYE from other leg\n");

    DSMSipRequest sip_req(&req);
    avar[DSM_AVAR_REQUEST] = AmArg((AmObject*)&sip_req);

    map<string, string> params;
    params["hdrs"] = req.hdrs; // todo: optimization - make this configurable
    engine.runEvent(this, this, DSMCondition::B2BOtherBye, &params);

    avar.erase(DSM_AVAR_REQUEST);

    return checkParam(DSM_PROCESSED, DSM_TRUE, &params);
}
Example #11
0
AmArg ConferenceRoomParticipant::asArgArray() {
  AmArg res;
  res.push(AmArg(localtag.c_str()));
  res.push(AmArg(number.c_str()));
  res.push(AmArg((int)status));
  res.push(AmArg(last_reason.c_str()));
  res.push(AmArg((int)muted));
  res.push(AmArg(participant_id));
  return res;
}
bool MonLogVarsAction::execute(AmSession* sess, 
			       DSMCondition::EventType event,
			       map<string,string>* event_params) {
  GET_SCSESSION();

  AmArg di_args,ret;
  di_args.push(AmArg(sess->getLocalTag().c_str()));

  for (map<string,string>::iterator it=
	 sc_sess->var.begin(); it != sc_sess->var.end();it++) {
    di_args.push(it->first.c_str());
    di_args.push(it->second.c_str());
  }
  AmSessionContainer::monitoring_di->invoke("log", di_args, ret);

  return false;
}
Example #13
0
void MsgStorage::userdir_open(string domain, string user, AmArg& ret) { 
  // TODO: block the directory from delete (increase lock)
  string path = msg_dir + "/" +  domain + "/" + user + "/";
  DBG("trying to list '%s'\n", path.c_str());
  DIR* dir = opendir(path.c_str());
  if (!dir) {
    ret.push(MSG_EUSRNOTFOUND);
    ret.push(AmArg()); // empty list
    return;
  }

  int err=0;
  struct dirent* entry;
  AmArg msglist;
  msglist.assertArray(0); // make it an array
  while( ((entry = readdir(dir)) != NULL) && (err == 0) ){
    string msgname(entry->d_name);
      if(!msgname.length() ||
	 msgname[0] == '.'){
	continue;
      }
    struct stat e_stat;
    if (stat((path+msgname).c_str(), &e_stat)) {
      ERROR("cannot stat '%s': %s\n", 
	    (path+msgname).c_str(),strerror(errno));
      continue;
    }
    AmArg msg;
    msg.push(msgname.c_str());
    // TODO: change the system here, st_atime/mtime/... 
    // is not really safe for saving read status!

    if (e_stat.st_atime != e_stat.st_mtime) {
      msg.push(0);      
    } else {      
      msg.push(1);      
    }
    msg.push((int)e_stat.st_size);      

    msglist.push(msg);
  }
  closedir(dir);
  // uh, this is really inefficient...
  ret.push(MSG_OK);
  ret.push(msglist);
}
Example #14
0
void DSMCall::onSipReply(const AmSipRequest& req,
                         const AmSipReply& reply,
                         AmBasicSipDialog::Status old_dlg_status)
{

    if (checkVar(DSM_ENABLE_REPLY_EVENTS, DSM_TRUE)) {
        map<string, string> params;
        params["code"] = int2str(reply.code);
        params["reason"] = reply.reason;
        params["hdrs"] = reply.hdrs;
        params["cseq"] = int2str(reply.cseq);

        params["dlg_status"] = dlg->getStatusStr();
        params["old_dlg_status"] = AmBasicSipDialog::getStatusStr(old_dlg_status);

        // pass AmSipReply for use by mod_dlg (? sending ACK?)
        DSMSipReply* dsm_reply = new DSMSipReply(&reply);
        avar[DSM_AVAR_REPLY] = AmArg(dsm_reply);

        engine.runEvent(this, this, DSMCondition::SipReply, &params);

        delete dsm_reply;
        avar.erase(DSM_AVAR_REPLY);

        if (checkParam(DSM_PROCESSED, DSM_TRUE, &params)) {
            DBG("DSM script processed SIP reply '%u %s', returning\n",
                reply.code, reply.reason.c_str());
            return;
        }
    }

    AmB2BCallerSession::onSipReply(req, reply, old_dlg_status);

    if ((old_dlg_status < AmSipDialog::Connected) &&
            (dlg->getStatus() == AmSipDialog::Disconnected)) {
        DBG("Outbound call failed with reply %d %s.\n",
            reply.code, reply.reason.c_str());
        map<string, string> params;
        params["code"] = int2str(reply.code);
        params["reason"] = reply.reason;
        engine.runEvent(this, this, DSMCondition::FailedCall, &params);
        setStopped();
    }
}
Example #15
0
PyObject* getPyLocals(DSMSession* sc_sess) {
  map<string, AmArg>::iterator l_it;
  SCPyDictArg* py_arg = NULL;
  AmObject* py_locals_obj;

  if (((l_it=sc_sess->avar.find("py_locals")) != sc_sess->avar.end()) && 
      (l_it->second.getType() == AmArg::AObject) && 
      ((py_locals_obj = l_it->second.asObject()) != NULL) &&
      ((py_arg = dynamic_cast<SCPyDictArg*>(py_locals_obj)) != NULL) &&
      (py_arg->pPyObject != NULL)
      ) {
    return py_arg->pPyObject;
  }

  PyObject* locals = PyDict_New();
  PyDict_SetItemString(locals, "dsm", SCPyModule::dsm_module);
  PyDict_SetItemString(locals, "session", SCPyModule::session_module);
  
  py_arg = new SCPyDictArg(locals);
  sc_sess->transferOwnership(py_arg);
  sc_sess->avar["py_locals"] = AmArg(py_arg);
  
  return locals;
}
Example #16
0
AmTimeoutEvent::AmTimeoutEvent(int timer_id)
  : AmPluginEvent(TIMEOUTEVENT_NAME)
{
  data.push(AmArg(timer_id));
}
Example #17
0
void DSMFactory::invoke(const string& method, const AmArg& args, 
				AmArg& ret)
{
  if (method == "postDSMEvent"){
    assertArgCStr(args.get(0))

    DSMEvent* ev = new DSMEvent();
    for (size_t i=0;i<args[1].size();i++)
      ev->params[args[1][i][0].asCStr()] = args[1][i][1].asCStr();

    if (AmSessionContainer::instance()->postEvent(args.get(0).asCStr(), ev)) {
      ret.push(AmArg(200));
      ret.push(AmArg("OK"));
    } else {
      ret.push(AmArg(404));
      ret.push(AmArg("Session not found"));
    }
  } else if (method == "reloadDSMs"){
    reloadDSMs(args,ret);
  } else if (method == "loadDSM"){
    args.assertArrayFmt("s");
    loadDSM(args,ret);
  } else if (method == "loadDSMWithPath"){
    args.assertArrayFmt("sss");
    loadDSMWithPaths(args,ret);
  } else if (method == "preloadModules"){
    preloadModules(args,ret);
  } else if (method == "preloadModule"){
    args.assertArrayFmt("ss");
    preloadModule(args,ret);
  } else if (method == "hasDSM"){
    args.assertArrayFmt("s");
    hasDSM(args,ret);      
  } else if (method == "listDSMs"){
    listDSMs(args,ret);
  } else if (method == "registerApplication"){
    args.assertArrayFmt("s");
    registerApplication(args,ret);
  } else if (method == "loadConfig"){
    args.assertArrayFmt("ss");
    loadConfig(args,ret);
  } else if(method == "_list"){ 
    ret.push(AmArg("postDSMEvent"));
    ret.push(AmArg("reloadDSMs"));
    ret.push(AmArg("loadDSM"));
    ret.push(AmArg("loadDSMWithPaths"));
    ret.push(AmArg("preloadModules"));
    ret.push(AmArg("preloadModule"));
    ret.push(AmArg("loadConfig"));
    ret.push(AmArg("hasDSM"));
    ret.push(AmArg("listDSMs"));
    ret.push(AmArg("registerApplication"));
  }  else
    throw AmDynInvoke::NotImplemented(method);
}
void DSMCall::process(AmEvent* event)
{

  DBG("DSMCall::process\n");

  if (event->event_id == DSM_EVENT_ID) {
    DSMEvent* dsm_event = dynamic_cast<DSMEvent*>(event);
    if (dsm_event) {      
      engine.runEvent(this, this, DSMCondition::DSMEvent, &dsm_event->params);
      return;
    }  
  }

  AmAudioEvent* audio_event = dynamic_cast<AmAudioEvent*>(event);
  if(audio_event && 
     ((audio_event->event_id == AmAudioEvent::cleared) || 
      (audio_event->event_id == AmAudioEvent::noAudio))){
    map<string, string> params;
    params["type"] = audio_event->event_id == AmAudioEvent::cleared?"cleared":"noAudio";
    engine.runEvent(this, this, DSMCondition::NoAudio, &params);
    return;
  }

  AmPluginEvent* plugin_event = dynamic_cast<AmPluginEvent*>(event);
  if(plugin_event && plugin_event->name == "timer_timeout") {
    int timer_id = plugin_event->data.get(0).asInt();
    map<string, string> params;
    params["id"] = int2str(timer_id);
    engine.runEvent(this, this, DSMCondition::Timer, &params);
  }

  AmPlaylistSeparatorEvent* sep_ev = dynamic_cast<AmPlaylistSeparatorEvent*>(event);
  if (sep_ev) {
    map<string, string> params;
    params["id"] = int2str(sep_ev->event_id);
    engine.runEvent(this, this, DSMCondition::PlaylistSeparator, &params);
  }

  ConferenceEvent * conf_ev = dynamic_cast<ConferenceEvent*>(event);
  if (conf_ev) {
    map<string, string> params;
    params["type"] = "conference_event";
    params["id"] = int2str(conf_ev->event_id);
    engine.runEvent(this, this, DSMCondition::DSMEvent, &params);
  }

  // todo: give modules the possibility to define/process events
  JsonRpcEvent* jsonrpc_ev = dynamic_cast<JsonRpcEvent*>(event);
  if (jsonrpc_ev) { 
    DBG("received jsonrpc event\n");

    JsonRpcResponseEvent* resp_ev = 
      dynamic_cast<JsonRpcResponseEvent*>(jsonrpc_ev);
    if (resp_ev) {
      map<string, string> params;
      params["ev_type"] = "JsonRpcResponse";
      params["id"] = resp_ev->response.id;
      params["is_error"] = resp_ev->response.is_error ? 
	"true":"false";

      // decode result for easy use from script
      varPrintArg(resp_ev->response.data, params, resp_ev->response.is_error ? "error": "result");

      // decode udata for easy use from script
      varPrintArg(resp_ev->udata, params, "udata");

      // save reference to full parameters as avar
      avar[DSM_AVAR_JSONRPCRESPONSEDATA] = AmArg(&resp_ev->response.data);
      avar[DSM_AVAR_JSONRPCRESPONSEUDATA] = AmArg(&resp_ev->udata);

      engine.runEvent(this, this, DSMCondition::JsonRpcResponse, &params);

      avar.erase(DSM_AVAR_JSONRPCRESPONSEUDATA);
      avar.erase(DSM_AVAR_JSONRPCRESPONSEDATA);
      return;
    }

    JsonRpcRequestEvent* req_ev = 
      dynamic_cast<JsonRpcRequestEvent*>(jsonrpc_ev);
    if (req_ev) {
      map<string, string> params;
      params["ev_type"] = "JsonRpcRequest";
      params["is_notify"] = req_ev->isNotification() ? 
	"true" : "false";
      params["method"] = req_ev->method;
      if (!req_ev->id.empty())
	params["id"] = req_ev->id;

      // decode request params result for easy use from script
      varPrintArg(req_ev->params, params, "params");

      // save reference to full parameters
      avar[DSM_AVAR_JSONRPCREQUESTDATA] = AmArg(&req_ev->params);

      engine.runEvent(this, this, DSMCondition::JsonRpcRequest, &params);

      avar.erase(DSM_AVAR_JSONRPCREQUESTDATA);
      return;
    }

  }

  if (event->event_id == E_SIP_SUBSCRIPTION) {
    SIPSubscriptionEvent* sub_ev = dynamic_cast<SIPSubscriptionEvent*>(event);
    if (sub_ev) {
      DBG("DSM Call received SIP Subscription Event\n");
      map<string, string> params;
      params["status"] = sub_ev->getStatusText();
      params["code"] = int2str(sub_ev->code);
      params["reason"] = sub_ev->reason;
      params["expires"] = int2str(sub_ev->expires);
      params["has_body"] = sub_ev->notify_body.get()?"true":"false";
      if (sub_ev->notify_body.get()) {
	avar[DSM_AVAR_SIPSUBSCRIPTION_BODY] = AmArg(sub_ev->notify_body.get());
      }
      engine.runEvent(this, this, DSMCondition::SIPSubscription, &params);
      avar.erase(DSM_AVAR_SIPSUBSCRIPTION_BODY);
    }
  }

  AmRtpTimeoutEvent* timeout_ev = dynamic_cast<AmRtpTimeoutEvent*>(event);
  if (timeout_ev) {
    map<string, string> params;
    params["type"] = "rtp_timeout";
    params["timeout_value"] = int2str(AmConfig::DeadRtpTime);
    engine.runEvent(this, this, DSMCondition::RTPTimeout, &params);
    return;
  }

  AmB2BCallerSession::process(event);
}
Example #19
0
void SystemDSM::process(AmEvent* event) {

  AmPluginEvent* plugin_event = dynamic_cast<AmPluginEvent*>(event);
  if(plugin_event && plugin_event->name == "timer_timeout") {
    int timer_id = plugin_event->data.get(0).asInt();
    map<string, string> params;
    params["id"] = int2str(timer_id);
    engine.runEvent(&dummy_session, this, DSMCondition::Timer, &params);
  }

  if (event->event_id == DSM_EVENT_ID) {
    DSMEvent* dsm_event = dynamic_cast<DSMEvent*>(event);
    if (dsm_event) {      
      engine.runEvent(&dummy_session, this, DSMCondition::DSMEvent, &dsm_event->params);
      return;
    }  
  }
  // todo: give modules the possibility to define/process events
  JsonRpcEvent* jsonrpc_ev = dynamic_cast<JsonRpcEvent*>(event);
  if (jsonrpc_ev) { 
    DBG("received jsonrpc event\n");

    JsonRpcResponseEvent* resp_ev = 
      dynamic_cast<JsonRpcResponseEvent*>(jsonrpc_ev);
    if (resp_ev) {
      map<string, string> params;
      params["ev_type"] = "JsonRpcResponse";
      params["id"] = resp_ev->response.id;
      params["is_error"] = resp_ev->response.is_error ? 
	"true":"false";

      // decode result for easy use from script
      varPrintArg(resp_ev->response.data, params, resp_ev->response.is_error ? "error": "result");

      // decode udata for easy use from script
      varPrintArg(resp_ev->udata, params, "udata");

      // save reference to full parameters
      avar[DSM_AVAR_JSONRPCRESPONSEDATA] = AmArg(&resp_ev->response.data);
      avar[DSM_AVAR_JSONRPCRESPONSEUDATA] = AmArg(&resp_ev->udata);

      engine.runEvent(&dummy_session, this, DSMCondition::JsonRpcResponse, &params);

      avar.erase(DSM_AVAR_JSONRPCRESPONSEUDATA);
      avar.erase(DSM_AVAR_JSONRPCRESPONSEDATA);
      return;
    }

    JsonRpcRequestEvent* req_ev = 
      dynamic_cast<JsonRpcRequestEvent*>(jsonrpc_ev);
    if (req_ev) {
      map<string, string> params;
      params["ev_type"] = "JsonRpcRequest";
      params["is_notify"] = req_ev->isNotification() ? 
	"true" : "false";
      params["method"] = req_ev->method;
      if (!req_ev->id.empty())
	params["id"] = req_ev->id;

      // decode request params result for easy use from script
      varPrintArg(req_ev->params, params, "params");

      // save reference to full parameters
      avar[DSM_AVAR_JSONRPCREQUESTDATA] = AmArg(&req_ev->params);

      engine.runEvent(&dummy_session, this, DSMCondition::JsonRpcRequest, &params);

      avar.erase(DSM_AVAR_JSONRPCREQUESTDATA);
      return;
    }

  }

  if (event->event_id == E_SIP_SUBSCRIPTION) {
    SIPSubscriptionEvent* sub_ev = dynamic_cast<SIPSubscriptionEvent*>(event);
    if (sub_ev) {
      DBG("SystemDSM received SIP Subscription Event\n");
      map<string, string> params;
      params["status"] = sub_ev->getStatusText();
      params["code"] = int2str(sub_ev->code);
      params["reason"] = sub_ev->reason;
      params["expires"] = int2str(sub_ev->expires);
      params["has_body"] = sub_ev->notify_body.get()?"true":"false";
      if (sub_ev->notify_body.get()) {
  	avar[DSM_AVAR_SIPSUBSCRIPTION_BODY] = AmArg(sub_ev->notify_body.get());
      }
      engine.runEvent(&dummy_session, this, DSMCondition::SIPSubscription, &params);
      avar.erase(DSM_AVAR_SIPSUBSCRIPTION_BODY);
    }
  }

  if (event->event_id == E_SYSTEM) {
    AmSystemEvent* sys_ev = dynamic_cast<AmSystemEvent*>(event);
    if(sys_ev){	
      DBG("SystemDSM received system Event\n");
      map<string, string> params;
      params["type"] = AmSystemEvent::getDescription(sys_ev->sys_event);
      engine.runEvent(&dummy_session, this, DSMCondition::System, &params);
      // stop_requested.set(true);
      return;
    }
  }

}
Example #20
0
void CCBLRedis::start(const string& cc_name, const string& ltag,
		       SBCCallProfile* call_profile,
		       int start_ts_sec, int start_ts_usec,
		       const AmArg& values, int timer_id, AmArg& res) {
  // start code here
  res.push(AmArg());
  AmArg& res_cmd = res[0];

#define MAX_ARGV_ITEMS    20
  const char* argv[MAX_ARGV_ITEMS];
  size_t argvlen[MAX_ARGV_ITEMS];
  
  unsigned int argv_max = 0;

  if (!values.hasMember("argc") ||
      str2i(values["argc"].asCStr(), argv_max) || (!argv_max)) {
    ERROR("deciphering argc\n");
    res_cmd[SBC_CC_ACTION] = SBC_CC_REFUSE_ACTION;
    res_cmd[SBC_CC_REFUSE_CODE] = 500;
    res_cmd[SBC_CC_REFUSE_REASON] = SIP_REPLY_SERVER_INTERNAL_ERROR;
    return;
  }

  unsigned int argv_index=0;
  string query;
  for (; argv_index<argv_max;argv_index++) {
    argv[argv_index] = values["argv_"+int2str(argv_index)].asCStr();
    argvlen[argv_index] = strlen(argv[argv_index]);
    if (query.length())
      query+=" ";
    query+=string(argv[argv_index], argvlen[argv_index]);
  }

  DBG("query to REDIS: '%s'\n", query.c_str());

  bool hit = false;

  unsigned int retries = 0;
  for (;retries<max_retries;retries++) {
    redisContext* redis_context = connection_pool.getActiveConnection();
    if (NULL == redis_context) {
      INFO("no connection to REDIS\n");
      if (!pass_on_bl_unavailable) {
	res_cmd[SBC_CC_ACTION] = SBC_CC_REFUSE_ACTION;
	res_cmd[SBC_CC_REFUSE_CODE] = 500;
	res_cmd[SBC_CC_REFUSE_REASON] = SIP_REPLY_SERVER_INTERNAL_ERROR;
      }
      return;
    }

    DBG("using redis connection [%p]\n", redis_context);

    redisReply* reply = (redisReply *)
      redisCommandArgv(redis_context, argv_index, argv, argvlen);

    int ret = handle_redis_reply(redis_context, reply, hit);

    if (ret == RWT_E_CONNECTION) {
      WARN("connection [%p] failed - retrying\n", redis_context);
      connection_pool.returnFailedConnection(redis_context);
      continue;
    }

    connection_pool.returnConnection(redis_context);
    break;
  }

  if (retries == max_retries) {
    if (!pass_on_bl_unavailable) {
      res_cmd[SBC_CC_ACTION] = SBC_CC_REFUSE_ACTION;
      res_cmd[SBC_CC_REFUSE_CODE] = 500;
      res_cmd[SBC_CC_REFUSE_REASON] = SIP_REPLY_SERVER_INTERNAL_ERROR;
    }
    return;
  }


  if (hit) {
    if (values.hasMember("action") && isArgCStr(values["action"]) && 
	values["action"] == "drop") {
      DBG("Blacklist: Dropping call\n");
      res_cmd[SBC_CC_ACTION] = SBC_CC_DROP_ACTION;
    } else {
      DBG("Blacklist: Refusing call\n");
      res_cmd[SBC_CC_ACTION] = SBC_CC_REFUSE_ACTION;
      res_cmd[SBC_CC_REFUSE_CODE] = 403;
      res_cmd[SBC_CC_REFUSE_REASON] = "Unauthorized";  
    }
  }
}