static void cpl_get(rpc_t* rpc, void* c) { str user; str script = STR_NULL; if (rpc->scan(c, "S", &user) < 1) { rpc->fault(c, 400, "Username parameter expected"); return; } DBG("DEBUG:cpl-c:cpl_get: user=%.*s\n", user.len, user.s); /* check user+host */ if (check_userhost( user.s, user.s+user.len)!=0) { LOG(L_ERR,"ERROR:cpl-c:cpl_load: invalid user@host [%.*s]\n", user.len,user.s); rpc->fault(c, 400, "Bad user@host '%.*s'", user.len, user.s); return; } /* get the script for this user */ if (get_user_script(&user, &script, 0)==-1) { rpc->fault(c, 500, "Database query failed"); return; } rpc->add(c, "S", &script); if (script.s) shm_free( script.s ); }
struct mi_root * mi_cpl_get(struct mi_root *cmd_tree, void *param) { struct mi_node *cmd; struct sip_uri uri; struct mi_root* rpl_tree; str script = {0,0}; str user; cmd = &cmd_tree->node; /* check if there is only one parameter*/ if(!(cmd->kids && cmd->kids->next== NULL)) return init_mi_tree( 400, MI_MISSING_PARM_S, MI_MISSING_PARM_LEN); /* check user+host */ user = cmd->kids->value; if (parse_uri( user.s, user.len, &uri)!=0) { LM_ERR("invalid user@host [%.*s]\n", user.len,user.s); return init_mi_tree( 400, USRHOST_ERR_S, USRHOST_ERR_LEN ); } LM_DBG("user@host=%.*s@%.*s\n", uri.user.len,uri.user.s,uri.host.len,uri.host.s); /* get the script for this user */ str query_str = str_init("cpl_xml"); if (get_user_script( &uri.user, cpl_env.use_domain?&uri.host:0, &script, &query_str)==-1) return init_mi_tree( 500, DB_GET_ERR_S, DB_GET_ERR_LEN ); /* write the response into response file - even if script is null */ rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN); if (rpl_tree!=NULL) add_mi_node_child( &rpl_tree->node, MI_DUP_VALUE, 0, 0, script.s, script.len); if (script.s) shm_free( script.s ); return rpl_tree; }
static inline int do_script_download(struct sip_msg *msg) { str user = STR_NULL; str script = STR_NULL; /* get the destination user name */ if (get_dest_user( msg, &user, 0)==-1) goto error; /* get the user's xml script from the database */ if (get_user_script(&user, &script, 0)==-1) goto error; /* add a lump with content-type hdr */ if (add_lump_rpl( msg, CONTENT_TYPE_HDR, CONTENT_TYPE_HDR_LEN, LUMP_RPL_HDR)==0) { LOG(L_ERR,"ERROR:cpl-c:do_script_download: cannot build hdr lump\n"); cpl_err = &intern_err; goto error; } if (script.s!=0) { /*DBG("script len=%d\n--------\n%.*s\n--------\n", script.len, script.len, script.s);*/ /* user has a script -> add a body lump */ if ( add_lump_rpl( msg, script.s, script.len, LUMP_RPL_BODY)==0) { LOG(L_ERR,"ERROR:cpl-c:do_script_download: cannot build " "body lump\n"); cpl_err = &intern_err; goto error; } /* build_lump_rpl duplicates the added text, so free the original */ shm_free( script.s ); } return 0; error: if (script.s) shm_free(script.s); return -1; }
static inline int do_script_download(struct sip_msg *msg) { str username = {0,0}; str domain = {0,0}; str script = {0,0}; /* get the destination user name */ if (get_dest_user( msg, &username, &domain)!=0) goto error; /* get the user's xml script from the database */ if (get_user_script( &username, cpl_env.use_domain?&domain:0, &script, &cpl_xml_col)==-1) goto error; /* add a lump with content-type hdr */ if (add_lump_rpl( msg, CONTENT_TYPE_HDR, CONTENT_TYPE_HDR_LEN, LUMP_RPL_HDR)==0) { LM_ERR("cannot build hdr lump\n"); cpl_err = &intern_err; goto error; } if (script.s!=0) { /* user has a script -> add a body lump */ if ( add_lump_rpl( msg, script.s, script.len, LUMP_RPL_BODY)==0) { LM_ERR("cannot build body lump\n"); cpl_err = &intern_err; goto error; } /* build_lump_rpl duplicates the added text, so free the original */ shm_free( script.s ); } return 0; error: if (script.s) shm_free(script.s); return -1; }
/* Params: * str1 - as unsigned int - can be CPL_RUN_INCOMING or CPL_RUN_OUTGOING * str2 - as unsigned int - flags regarding state(less)|(ful) */ static int cpl_invoke_script(struct sip_msg* msg, char* str1, char* str2) { struct cpl_interpreter *cpl_intr; str username = {0,0}; str domain = {0,0}; str loc; str script; /* get the user_name */ if ( ((unsigned long)str1)&CPL_RUN_INCOMING ) { /* if it's incoming -> get the destination user name */ if (get_dest_user( msg, &username, &domain)==-1) goto error0; } else { /* if it's outgoing -> get the origin user name */ if (get_orig_user( msg, &username, &domain)==-1) goto error0; } /* get the script for this user */ if (get_user_script(&username, cpl_env.use_domain?&domain:0, &script, &cpl_bin_col)==-1) goto error0; /* has the user a non-empty script? if not, return normally, allowing the * script execution to continue */ if ( !script.s || !script.len ) return 2; /* build a new script interpreter */ if ( (cpl_intr=new_cpl_interpreter(msg,&script))==0 ) goto error1; /* set the flags */ cpl_intr->flags =(unsigned int)((unsigned long)str1)|((unsigned long)str2); /* build user AOR */ if (build_user_AOR( &username, &domain, &(cpl_intr->user), 0)!=0 ) goto error2; /* for OUTGOING we need also the destination user for init. with him * the location set */ if ( ((unsigned long)str1)&CPL_RUN_OUTGOING ) { /* build user initial location -> get the destination user name */ if (get_dest_user( msg, &username, &domain)==-1) goto error2; if (build_user_AOR( &username, &domain, &loc, 1)!=0 ) goto error2; if (add_location( &(cpl_intr->loc_set), &loc, 0, 10,CPL_LOC_DUPL)==-1){ shm_free(loc.s); goto error2; } shm_free(loc.s); } /* run the script */ switch (cpl_run_script( cpl_intr )) { case SCRIPT_DEFAULT: if ( cpl_intr->flags&CPL_DO_NOT_FREE ) cpl_intr->flags |= CPL_ENDED; else free_cpl_interpreter( cpl_intr ); return 1; /* execution of ser's script will continue */ case SCRIPT_END: if ( cpl_intr->flags&CPL_DO_NOT_FREE ) cpl_intr->flags |= CPL_ENDED; else free_cpl_interpreter( cpl_intr ); case SCRIPT_TO_BE_CONTINUED: return 0; /* break the SER script */ case SCRIPT_RUN_ERROR: case SCRIPT_FORMAT_ERROR: goto error2; } return 1; error2: if ( cpl_intr->flags&CPL_DO_NOT_FREE ) cpl_intr->flags |= CPL_ENDED; else free_cpl_interpreter( cpl_intr ); return -1; error1: shm_free(script.s); error0: return -1; }
/* Params: * str1 - as unsigned int - can be CPL_RUN_INCOMING or CPL_RUN_OUTGOING * str2 - as unsigned int - flags regarding state(less)|(ful) * str3 - URI in SPVE structure */ static int cpl_invoke_script3(struct sip_msg* msg, char* str1, char* str2, char *str3) { struct cpl_interpreter *cpl_intr; str username = {0,0}; str domain = {0,0}; str uri = {0,0}; sip_uri_t puri; str loc; str script; /* get the user_name */ if(str3==NULL) { if ( ((unsigned long)str1)&CPL_RUN_INCOMING ) { /* if it's incoming -> get the destination user name */ if (get_dest_user( msg, &username, &domain)==-1) goto error0; } else { /* if it's outgoing -> get the origin user name */ if (get_orig_user( msg, &username, &domain)==-1) goto error0; } } else { if(fixup_get_svalue(msg, (gparam_p)str3, &uri)!=0) { LM_ERR("invalid uri parameter"); goto error0; } if (parse_uri(uri.s, uri.len, &puri) || !puri.user.len) { LM_ERR("unable to extract user name from URI param\n"); return -1; } username = puri.user; domain = puri.host; } /* get the script for this user */ if (get_user_script(&username, cpl_env.use_domain?&domain:0, &script, &cpl_bin_col)==-1) goto error0; /* has the user a non-empty script? if not, return normally, allowing the * script execution to continue */ if ( !script.s || !script.len ) return 1; /* build a new script interpreter */ if ( (cpl_intr=new_cpl_interpreter(msg,&script))==0 ) goto error1; /* set the flags */ cpl_intr->flags =(unsigned int)((unsigned long)str1)|((unsigned long)str2); /* build user AOR */ if (build_user_AOR( &username, &domain, &(cpl_intr->user), 0)!=0 ) goto error2; /* for OUTGOING we need also the destination user for init. with him * the location set */ if ( ((unsigned long)str1)&CPL_RUN_OUTGOING ) { /* build user initial location -> get the destination user name */ if (get_dest_user( msg, &username, &domain)==-1) goto error2; if (build_user_AOR( &username, &domain, &loc, 1)!=0 ) goto error2; if (add_location( &(cpl_intr->loc_set), &loc, 0, 10, 0/*no dup*/)==-1) goto error2; } /* run the script */ switch (cpl_run_script( cpl_intr )) { case SCRIPT_DEFAULT: free_cpl_interpreter( cpl_intr ); return 1; /* execution of ser's script will continue */ case SCRIPT_END: free_cpl_interpreter( cpl_intr ); case SCRIPT_TO_BE_CONTINUED: return 0; /* break the SER script */ case SCRIPT_RUN_ERROR: case SCRIPT_FORMAT_ERROR: goto error2; } return 1; error2: free_cpl_interpreter( cpl_intr ); return -1; error1: shm_free(script.s); error0: return -1; }
/* Params: * str1 - as unsigned int - can be CPL_RUN_INCOMING or CPL_RUN_OUTGOING * str2 - as unsigned int - flags regarding state(less)|(ful) */ static int cpl_invoke_script(struct sip_msg* msg, char* str1, char* str2) { struct cpl_interpreter *cpl_intr; str user; str loc; str script; /* get the user_name */ if ( ((unsigned long)str1)&CPL_RUN_INCOMING ) { /* if it's incoming -> get the destination user name */ if (get_dest_user( msg, &user, BUILD_UH_SHM)==-1) goto error0; } else { /* if it's outgoing -> get the origin user name */ if (get_orig_user( msg, &user, BUILD_UH_SHM)==-1) goto error0; } /* get the script for this user */ if (get_user_script(&user, &script, 1)==-1) goto error1; /* has the user a non-empty script? if not, return normally, allowing ser to * continue its script */ if ( !script.s || !script.len ) { shm_free(user.s); return 1; } /* build a new script interpreter */ if ( (cpl_intr=new_cpl_interpreter(msg,&script))==0 ) goto error2; /* set the flags */ cpl_intr->flags =(unsigned int)((unsigned long)str1)|((unsigned long)str2); /* attache the user */ cpl_intr->user = user; /* for OUTGOING we need also the destination user for init. with him * the location set */ if ( ((unsigned long)str1)&CPL_RUN_OUTGOING ) { if (get_dest_user( msg, &loc,BUILD_UH_ADDSIP)==-1) goto error3; if (add_location( &(cpl_intr->loc_set), &loc,10,CPL_LOC_DUPL)==-1) goto error3; } /* since the script interpretation can take some time, it will be better to * send a 100 back to prevent the UAC to retransmit if ( cpl_tmb.t_reply( msg, (int)100, "Running cpl script" )!=1 ) { LOG(L_ERR,"ERROR:cpl_invoke_script: unable to send 100 reply!\n"); goto error3; } * this should be done from script - it's much sooner ;-) */ /* run the script */ switch (cpl_run_script( cpl_intr )) { case SCRIPT_DEFAULT: free_cpl_interpreter( cpl_intr ); return 1; /* execution of ser's script will continue */ case SCRIPT_END: free_cpl_interpreter( cpl_intr ); case SCRIPT_TO_BE_CONTINUED: return 0; /* break the SER script */ case SCRIPT_RUN_ERROR: case SCRIPT_FORMAT_ERROR: goto error3; } return 1; error3: free_cpl_interpreter( cpl_intr ); return -1; error2: shm_free(script.s); error1: shm_free(user.s); error0: return -1; }