123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888 |
- /*
- ============================================================================
- Name : generate_trunk_conf.sh
- Author : ssc
- Version : v1.0
- Copyright : ZYCOO copyright
- Description : Generate trunk info from mysql to turnk conf file
- ============================================================================
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <assert.h>
- #include <time.h>
- #include <ctype.h>
- #include <cjson/cJSON.h>
- #include <mysql/mysql.h>
- MYSQL *g_conn; // mysql 连接
- MYSQL_RES *g_res; // mysql 记录集
- MYSQL_ROW g_row; // 字符串数组,mysql 记录行
- #define MAX_SIZE 2048
- #define MIDLE_SIZE 512
- #define MINI_SIZE 64
- #define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
- #define EXTEN_IVR_FILE "/etc/asterisk/extensions_ivr_custom.conf"
- #define EXTEN_DIALRULE_FILE "/etc/asterisk/extensions_dialrule_custom.conf"
- #define EXTEN_INBOUND_FILE "/etc/asterisk/extensions_inbound_custom.conf"
- #define EXTEN_GLOBAL_FILE "/etc/asterisk/extensions_global_custom.conf"
- #define EXTEN_CALLTRIGGER_FILE "/etc/asterisk/extensions_call_trigger_custom.conf"
- #define EXTEN_FEATURECODES_FILE "/etc/asterisk/extensions_featurecodes_custom.conf"
- #define SIP_NAT_FILE "/etc/asterisk/sip_nat.conf"
- #define SIP_SETTINGS_FILE "/etc/asterisk/sip_settings.conf"
- #define RTP_SETTINGS_FILE "/etc/asterisk/rtp_settings.conf"
- #define KEYVALLEN 100
- #define VERSION "V1.0.1"
- #define QUERY_IVR_SQL "select name,exten,prompt,loops,timeout,language,dialplan,keys_action from t_pbx_ivr"
- #define QUERY_DIALRULE_SQL "select t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after from t_pbx_dialrule join t_pbx_users_voiptrunk on t_pbx_users_voiptrunk.id=t_pbx_dialrule.trunk_id"
- #define QUERY_GLOBAL_SQL "select * from t_global_config"
- char g_host_name[MINI_SIZE];
- char g_user_name[MINI_SIZE];
- char g_password[MINI_SIZE];
- char g_db_name[MINI_SIZE];
- const unsigned int g_db_port = 3306;
- char dialrule[MIDLE_SIZE];
- char ivrstr[MAX_SIZE];
- char inboundstr[MIDLE_SIZE];
- char prompt[MINI_SIZE];
- //读取配置文件函数----功能:删除左边空格
- char *l_trim(char *szOutput, const char *szInput)
- {
- assert(szInput != NULL);
- assert(szOutput != NULL);
- assert(szOutput != szInput);
- for (NULL; *szInput != '\0' && isspace(*szInput); ++szInput)
- {
- ;
- }
- return strcpy(szOutput, szInput);
- }
- /* 删除右边的空格 */
- char *r_trim(char *szOutput, const char *szInput)
- {
- char *p = NULL;
- assert(szInput != NULL);
- assert(szOutput != NULL);
- assert(szOutput != szInput);
- strcpy(szOutput, szInput);
- for(p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
- {
- ;
- }
- *(++p) = '\0';
- return szOutput;
- }
- /* 删除两边的空格 */
- char *a_trim(char *szOutput, const char *szInput)
- {
- char *p = NULL;
- assert(szInput != NULL);
- assert(szOutput != NULL);
- l_trim(szOutput, szInput);
- for (p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
- {
- ;
- }
- *(++p) = '\0';
- return szOutput;
- }
- //main函数接口 参数1:配置文件路径 参数2:配置文件的那一部分,如general 参数3:键名 参数4:键值
- int GetProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal )
- {
- char appname[32], keyname[32];
- char *buf, *c;
- char buf_i[KEYVALLEN], buf_o[KEYVALLEN];
- FILE *fp;
- int found = 0; /* 1 AppName 2 KeyName */
- if( (fp = fopen( profile, "r" )) == NULL )
- {
- printf( "openfile [%s] error [%s]\n", profile, strerror(errno) );
- return(-1);
- }
- fseek( fp, 0, SEEK_SET );
- memset( appname, 0, sizeof(appname) );
- sprintf( appname, "[%s]", AppName );
- while( !feof(fp) && fgets( buf_i, KEYVALLEN, fp ) != NULL )
- {
- l_trim(buf_o, buf_i);
- if( strlen(buf_o) <= 0 )
- continue;
- buf = NULL;
- buf = buf_o;
- if( found == 0 )
- {
- if( buf[0] != '[' )
- {
- continue;
- }
- else if ( strncmp(buf, appname, strlen(appname)) == 0 )
- {
- found = 1;
- continue;
- }
- }
- else if( found == 1 )
- {
- if( buf[0] == '#' )
- {
- continue;
- }
- else if ( buf[0] == '[' )
- {
- break;
- }
- else
- {
- if( (c = (char *)strchr(buf, '=')) == NULL )
- continue;
- memset( keyname, 0, sizeof(keyname) );
- sscanf( buf, "%[^=|^ |^\t]", keyname );
- if( strcmp(keyname, KeyName) == 0 )
- {
- sscanf( ++c, "%[^\n]", KeyVal );
- char *KeyVal_o = (char *)malloc(strlen(KeyVal) + 1);
- if(KeyVal_o != NULL)
- {
- memset(KeyVal_o, 0, sizeof(KeyVal_o));
- a_trim(KeyVal_o, KeyVal);
- if(KeyVal_o && strlen(KeyVal_o) > 0)
- strcpy(KeyVal, KeyVal_o);
- free(KeyVal_o);
- KeyVal_o = NULL;
- }
- found = 2;
- break;
- }
- else
- {
- continue;
- }
- }
- }
- }
- fclose( fp );
- if( found == 2 )
- return(0);
- else
- return(-1);
- }
- char * mytime(){
- time_t my_time;
- time(&my_time);
- char *time_string = ctime(&my_time);
- if (time_string[strlen(time_string) - 1] == '\n')
- {
- time_string[strlen(time_string) - 1] = '\0';
- }
- return time_string;
- }
- void print_mysql_error(const char *msg) { // 打印最后一次错误
- if (msg)
- printf("%s: %s\n", msg, mysql_error(g_conn));
- else
- puts(mysql_error(g_conn));
- }
- int executesql(const char * sql) {
- /*query the database according the sql*/
- if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
- return -1; // 表示失败
- return 0; // 成功执行
- }
- int init_mysql() { // 初始化连接
- // init the database connection
- g_conn = mysql_init(NULL);
- /* connect the database */
- if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
- return -1;
- // 是否连接已经可用
- if (executesql("set names utf8")) // 如果失败
- return -1;
- return 0; // 返回成功
- }
- int main(int argc, char **argv) {
- cJSON *pJson,*pSub;
- int iCount=0;
- typedef struct keys_action {
- char key[MINI_SIZE];
- char type[MINI_SIZE];
- char exten[MINI_SIZE];
- } KeysObject;
- strcpy(g_host_name,getenv("MYSQL"));
- strcpy(g_user_name,getenv("MYSQL_USER"));
- strcpy(g_password,getenv("MYSQL_PASSWORD"));
- strcpy(g_db_name,getenv("MYSQL_DATABASE"));
- if (init_mysql()){
- print_mysql_error(NULL);
- exit(1);
- }
- //读取自动话务员的数据,并写入配置文件
- if (executesql(QUERY_IVR_SQL)){
- print_mysql_error(NULL);
- exit(1);
- }
- g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
- FILE *conf_ivr_fp = fopen(EXTEN_IVR_FILE, "w+");
- if (conf_ivr_fp == NULL){
- perror("Open paging conf file Error: ");
- exit(1);
- }
- fprintf(conf_ivr_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_ivr_custom.conf (/etc/asterisk/extensions_ivr_custom.conf)\n\
- ;! Generator: Generator IVR\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- ",\
- mytime()\
- );
- //name:g_row[0],exten:g_row[1],prompt:g_row[2],loops:g_row[3],timeout:g_row[4],language:g_row[5],dialplan:g_row[6],keys_action
- //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
- //| id | name | exten | prompt | loops | timeout | language | dialplan | keys_action | createdAt | updatedAt |
- //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
- //| 1 | 上班时间 | 6500 | /etc/asterisk/sysconf/prompts/welcome | 1 | 3 | NULL | default | [{"key": "i", "type": "hangup", "exten": ""}, {"key": "t", "type": "hangup", "exten": ""}, {"key": "0", "type": "extension", "exten": "8001"}] | 2019-07-31 17:58:00 | 2019-08-05 01:30:48 |
- //| 2 | 下班时间 | 6501 | /etc/asterisk/sysconf/prompts/closed | 1 | 3 | NULL | default | [{"key": "i", "type": "hangup", "exten": ""}, {"key": "t", "type": "hangup", "exten": ""}] | 2019-07-31 17:58:00 | 2019-08-06 01:30:24 |
- //| 4 | test | 6505 | /etc/asterisk/sysconf/prompts/welcome | 1 | 3 | NULL | default | [{"key": "i", "type": "hangup", "exten": ""}, {"key": "t", "type": "hangup", "exten": ""}, {"key": "1", "type": "extension", "exten": "8500"}] | 2019-08-05 01:33:58 | 2019-08-05 13:02:10 |
- //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
- //3 rows in set (0.00 sec)
- while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
- if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- fprintf(conf_ivr_fp,"\
- [voicemenu-custom-%s]\n\
- include => %s\n\
- exten => %s,1,NoOp(%s)\n",\
- g_row[1],\
- g_row[6],\
- g_row[1],\
- g_row[0]\
- );
- if(g_row[5] != NULL){
- fprintf(conf_ivr_fp,"same => n,Set(CHANNEL(language)=%s)\n",g_row[5]);
- }
- memset(prompt, 0, sizeof(prompt));
- strncpy(prompt,g_row[2],strlen(g_row[2])-4);
- fprintf(conf_ivr_fp,"\
- same => n,Set(COUNT=%s)\n\
- same => n(loop),Background(%s)\n",\
- g_row[3],\
- prompt
- );
- if(strcmp(g_row[4], "0") != 0 && g_row[4] != NULL){
- fprintf(conf_ivr_fp,"same => n,WaitExten(%s)\n",g_row[4]);
- }
- fprintf(conf_ivr_fp,"\
- same => n,Set(COUNT=$[${COUNT}-1])\n\
- same => n,GotoIf($[${COUNT} < 0]?:loop)\n\
- same => n,WaitExten(1)\n");
- KeysObject keysObject[MINI_SIZE];
- memset(keysObject, 0, sizeof(keysObject));
- if(g_row[7] != NULL){
- pJson = cJSON_Parse(g_row[7]);
- iCount = cJSON_GetArraySize(pJson);
- for(int i = 0;i < iCount;i++){
- pSub = cJSON_GetArrayItem(pJson,i);
- if(pSub != NULL){
- strcpy(keysObject[i].key,cJSON_GetObjectItem(pSub, "key")->valuestring);
- strcpy(keysObject[i].type,cJSON_GetObjectItem(pSub, "type")->valuestring);
- strcpy(keysObject[i].exten,cJSON_GetObjectItem(pSub, "exten")->valuestring);
- if(strcmp(keysObject[i].type, "hangup") == 0){
- fprintf(conf_ivr_fp,"exten => %s,1,Hangup()\n",keysObject[i].key);
- }
- else if(strcmp(keysObject[i].type, "extension") == 0){
- fprintf(conf_ivr_fp,"exten => %s,1,Goto(default,%s,1)\n",keysObject[i].key,keysObject[i].exten);
- }
- else if(strcmp(keysObject[i].type, "ivr") == 0){
- fprintf(conf_ivr_fp,"exten => %s,1,Goto(voicemenu-custom-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
- }
- else if(strcmp(keysObject[i].type, "group") == 0){
- fprintf(conf_ivr_fp,"exten => %s,1,Goto(paging-group-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
- }
- else if(strcmp(keysObject[i].type, "user") == 0){
- int id = 100000 + atoi(keysObject[i].exten);
- fprintf(conf_ivr_fp,"exten => %s,1,Goto(manager-queue-%d,s,1)\n",keysObject[i].key,id);
- }
- }
- }
- }
- fprintf(conf_ivr_fp,"\n\n");
- }
- /*
- while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
- if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- sprintf(ivrstr,"\
- [voicemenu-custom-%s]\n\
- include => %s\n\
- exten => %s,1,NoOp(%s)",\
- g_row[1],\
- g_row[6],\
- g_row[1],\
- g_row[0]\
- );
- if(g_row[5] != NULL){
- sprintf(ivrstr,"%s\nsame => n,Set(CHANNEL(language)=%s)",ivrstr,g_row[5]);
- }
- memset(prompt, 0, sizeof(prompt));
- strncpy(prompt,g_row[2],strlen(g_row[2])-4);
- sprintf(ivrstr,"%s\n\
- same => n,Set(COUNT=%s)\n\
- same => n(loop),Background(%s)",ivrstr,\
- g_row[3],\
- prompt
- );
- if(strcmp(g_row[4], "0") != 0 && g_row[4] != NULL){
- sprintf(ivrstr,"%s\nsame => n,WaitExten(%s)",ivrstr,g_row[4]);
- }
- sprintf(ivrstr,"%s\n\
- same => n,Set(COUNT=$[${COUNT}-1])\n\
- same => n,GotoIf($[${COUNT} < 0]?:loop)\n\
- same => n,WaitExten(1)",ivrstr
- );
- KeysObject keysObject[MINI_SIZE];
- memset(keysObject, 0, sizeof(keysObject));
- if(g_row[7] != NULL){
- pJson = cJSON_Parse(g_row[7]);
- iCount = cJSON_GetArraySize(pJson);
- for(int i = 0;i < iCount;i++){
- pSub = cJSON_GetArrayItem(pJson,i);
- if(pSub != NULL){
- strcpy(keysObject[i].key,cJSON_GetObjectItem(pSub, "key")->valuestring);
- strcpy(keysObject[i].type,cJSON_GetObjectItem(pSub, "type")->valuestring);
- strcpy(keysObject[i].exten,cJSON_GetObjectItem(pSub, "exten")->valuestring);
- if(strcmp(keysObject[i].type, "hangup") == 0){
- sprintf(ivrstr,"%s\nexten => %s,1,Hangup()",ivrstr,keysObject[i].key);
- }
- else if(strcmp(keysObject[i].type, "extension") == 0){
- sprintf(ivrstr,"%s\nexten => %s,1,Goto(default,%s,1)",ivrstr,keysObject[i].key,keysObject[i].exten);
- }
- else if(strcmp(keysObject[i].type, "ivr") == 0){
- sprintf(ivrstr,"%s\nexten => %s,1,Goto(voicemenu-custom-%s,%s,1)",ivrstr,keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
- }
- else if(strcmp(keysObject[i].type, "group") == 0){
- sprintf(ivrstr,"%s\nexten => %s,1,Goto(paging-group-%s,%s,1)",ivrstr,keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
- }
- }
- }
- }
- strcat(ivrstr,"\n\n");
- fputs(ivrstr,conf_ivr_fp);
- }
- /*
- [voicemenu-custom-%s]
- include = default
- exten = _IVR-X.,1,NoOp(%s)
- exten = _IVR-X.,n,NoOp(Default language)
- exten = _IVR-X.,n,Set(COUNT=3)
- exten = _IVR-X.,n(loop),Background(%s)
- exten = _IVR-X.,n,Set(COUNT=$[${COUNT}-1])
- exten = _IVR-X.,n,WaitExten(%s)
- exten = _IVR-X.,n,GotoIf($[${COUNT}>1]?6)
- exten = _IVR-X.,n,WaitExten(1)
- exten = t,1,Hangup
- */
- //读取拨号规则的数据,并写入配置文件
- if (executesql(QUERY_DIALRULE_SQL)){
- print_mysql_error(NULL);
- exit(1);
- }
- g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
- FILE *conf_dialrule_fp = fopen(EXTEN_DIALRULE_FILE, "w+");
- if (conf_dialrule_fp == NULL){
- perror("Open paging conf file Error: ");
- exit(1);
- }
- fprintf(conf_dialrule_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_dialrule_custom.conf (/etc/asterisk/extensions_dialrule_custom.conf)\n\
- ;! Generator: Generator DIALRULE\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- ",\
- mytime()\
- );
- fputs("[CallingRule_OutCall]\n",conf_dialrule_fp);
- //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
- while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
- if (g_row[0] == NULL || g_row[1] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- memset(dialrule,0,sizeof(dialrule));
- sprintf(dialrule, "exten => _%s,1,Macro(trunkdial-failover,${EXTEN},${%s}",g_row[1], g_row[0]);
- if(g_row[3] != NULL){
- strcat(dialrule,g_row[3]);
- }
- strcat(dialrule,"${EXTEN:");
- if(g_row[2] != NULL){
- strcat(dialrule,g_row[2]);
- }
- strcat(dialrule,"}");
- if(g_row[4] != NULL){
- strcat(dialrule,g_row[4]);
- }
- strcat(dialrule,")\n");
- fputs(dialrule,conf_dialrule_fp);
- }
- //读取全局配置数据,并写入配置文件
- if (executesql(QUERY_GLOBAL_SQL)){
- print_mysql_error(NULL);
- exit(1);
- }
- g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
- FILE *conf_inbound_fp = fopen(EXTEN_INBOUND_FILE, "w+");
- if (conf_inbound_fp == NULL){
- perror("Open inbound conf file Error: ");
- exit(1);
- }
- FILE *conf_global_fp = fopen(EXTEN_GLOBAL_FILE, "w+");
- if (conf_global_fp == NULL){
- perror("Open global conf file Error: ");
- exit(1);
- }
- FILE *conf_calltrigger_fp = fopen(EXTEN_CALLTRIGGER_FILE, "w+");
- if (conf_calltrigger_fp == NULL){
- perror("Open calltrigger conf file Error: ");
- exit(1);
- }
- FILE *conf_featurecodes_fp = fopen(EXTEN_FEATURECODES_FILE, "w+");
- if (conf_featurecodes_fp == NULL){
- perror("Open featurecodes conf file Error: ");
- exit(1);
- }
- FILE *conf_sip_nat_fp = fopen(SIP_NAT_FILE, "w+");
- if (conf_sip_nat_fp == NULL){
- perror("Open sip nat conf file Error: ");
- exit(1);
- }
- FILE *conf_sipsetting_fp = fopen(SIP_SETTINGS_FILE, "w+");
- if (conf_sipsetting_fp == NULL){
- perror("Open sip settings conf file Error: ");
- exit(1);
- }
- FILE *conf_rtpsetting_fp = fopen(RTP_SETTINGS_FILE, "w+");
- if (conf_rtpsetting_fp == NULL){
- perror("Open rtp settings conf file Error: ");
- exit(1);
- }
- fprintf(conf_inbound_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_inbound_custom.conf (/etc/asterisk/extensions_inbound_custom.conf)\n\
- ;! Generator: Generator INBOUND\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- ",\
- mytime()\
- );
- fprintf(conf_global_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_global_custom.conf (/etc/asterisk/extensions_global_custom.conf)\n\
- ;! Generator: Generator GLOBAL\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- ",\
- mytime()\
- );
- fprintf(conf_global_fp, "\
- AGISERVERHOST = %s\n\
- AGISERVERPORT = %s\n\
- ",\
- getenv("BROADCAST_GATEWAY"),\
- getenv("AGI_SERVER_PORT")\
- );
- fprintf(conf_calltrigger_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_call_trigger_custom.conf (/etc/asterisk/extensions_call_trigger_custom.conf)\n\
- ;! Generator: Generator TRIGGER\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- ",\
- mytime()\
- );
- fprintf(conf_featurecodes_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_featurecodes_custom.conf (/etc/asterisk/extensions_featurecodes_custom.conf)\n\
- ;! Generator: Generator FEATURECODES\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- [featurecodes]\n",\
- mytime()\
- );
- fprintf(conf_sip_nat_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: sip_nat.conf (/etc/asterisk/sip_nat.conf)\n\
- ;! Generator: Generator SIP NAT\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- \n",\
- mytime()\
- );
- fprintf(conf_sipsetting_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: sip_settings.conf (/etc/asterisk/sip_settings.conf)\n\
- ;! Generator: Generator SIP SETTINGS\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- \n",\
- mytime()\
- );
- fprintf(conf_rtpsetting_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: rtp_settings.conf (/etc/asterisk/rtp_settings.conf)\n\
- ;! Generator: Generator RTP SETTINGS\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- \n",\
- mytime()\
- );
- //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
- while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
- if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- if(strcmp(g_row[1],"pbx.voip.inbound") == 0){
- memset(inboundstr,0,sizeof(inboundstr));
- pJson = cJSON_Parse(g_row[2]);
- if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "hangup") == 0){
- sprintf(inboundstr,"\
- [direct-analog]\n\
- exten => direct,1,Hangup()\n\
- [direct-voip]\n\
- exten => direct,1,Hangup()\n"
- );
- }
- else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "extension") == 0){
- sprintf(inboundstr,"\
- [direct-analog]\n\
- exten => direct,1,Goto(default,%s,1)\n\
- [direct-voip]\n\
- exten => direct,1,Goto(default,%s,1)\n",\
- cJSON_GetObjectItem(pJson, "exten")->valuestring,\
- cJSON_GetObjectItem(pJson, "exten")->valuestring\
- );
- }
- else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "ivr") == 0){
- sprintf(inboundstr,"\
- [direct-analog]\n\
- exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n\
- [direct-voip]\n\
- exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n",\
- cJSON_GetObjectItem(pJson, "exten")->valuestring,\
- cJSON_GetObjectItem(pJson, "exten")->valuestring,\
- cJSON_GetObjectItem(pJson, "exten")->valuestring,\
- cJSON_GetObjectItem(pJson, "exten")->valuestring\
- );
- }
- else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "group") == 0){
- sprintf(inboundstr,"\
- [direct-analog]\n\
- exten => direct,1,Goto(paging-group-%s,%s,1)\n\
- [direct-voip]\n\
- exten => direct,1,Goto(paging-group-%s,%s,1)\n",\
- cJSON_GetObjectItem(pJson, "exten")->valuestring,\
- cJSON_GetObjectItem(pJson, "exten")->valuestring,\
- cJSON_GetObjectItem(pJson, "exten")->valuestring,\
- cJSON_GetObjectItem(pJson, "exten")->valuestring\
- );
- }
- fputs(inboundstr,conf_inbound_fp);
- }
- else if(strcmp(g_row[1],"paging.prompt.config") == 0){
- pJson = cJSON_Parse(g_row[2]);
- fprintf(conf_global_fp, "\
- enPaging_prompt_start = %s\n\
- enPaging_prompt_end = %s\n\
- ",\
- cJSON_GetObjectItem(pJson, "start")->valuestring,\
- cJSON_GetObjectItem(pJson, "end")->valuestring\
- );
- if(cJSON_GetObjectItem(pJson, "startfile") != NULL){
- memset(prompt, 0, sizeof(prompt));
- if(strcmp(cJSON_GetObjectItem(pJson, "startfile")->valuestring,"start") == 0)
- strcpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring);
- else
- strncpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring,strlen(cJSON_GetObjectItem(pJson, "startfile")->valuestring)-4);
- fprintf(conf_global_fp, "\
- Paging_start_file = %s\n\
- ",\
- prompt\
- );
- }else{
- fprintf(conf_global_fp, "\
- Paging_start_file = start\n\
- ");
- }
- }
- else if(strcmp(g_row[1],"pbx.ringtime.config") == 0){
- pJson = cJSON_Parse(g_row[2]);
- if(cJSON_GetObjectItem(pJson, "ringtime") != NULL){
- fprintf(conf_global_fp, "\
- RINGTIME = %d\n\
- ",\
- cJSON_GetObjectItem(pJson, "ringtime")->valueint\
- );
- }else{
- fprintf(conf_global_fp, "\
- RINGTIME = 30\n\
- ");
- }
- }
- else if(strcmp(g_row[1],"pbx.nat.config") == 0){
- pJson = cJSON_Parse(g_row[2]);
- if(pJson && cJSON_GetObjectItem(pJson, "enable")->valueint == 1){
- fprintf(conf_sip_nat_fp, "\
- externaddr = %s\n\
- externhost = %s\n\
- externrefresh = %d\n\
- ",\
- cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
- cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
- cJSON_GetObjectItem(pJson, "externrefresh")->valueint\
- );
- if(cJSON_GetObjectItem(pJson, "externtcpport") && cJSON_GetObjectItem(pJson, "externtcpport")->valueint != 0)
- {
- fprintf(conf_sip_nat_fp, "\
- externtcpport = %d\n\
- ",\
- cJSON_GetObjectItem(pJson, "externtcpport")->valueint\
- );
- }
- if(cJSON_GetObjectItem(pJson, "externtlsport") && cJSON_GetObjectItem(pJson, "externtlsport")->valueint != 0)
- {
- fprintf(conf_sip_nat_fp, "\
- externtlsport = %d\n\
- ",\
- cJSON_GetObjectItem(pJson, "externtlsport")->valueint\
- );
- }
- cJSON *localnetArray = cJSON_GetObjectItem( pJson, "localnet");
- if(localnetArray != NULL){
- int array_size = cJSON_GetArraySize (localnetArray);
- for(int n = 0; n < array_size; n++){
- pSub = cJSON_GetArrayItem(localnetArray, n);
- if(NULL == pSub ){ continue ; }
- fprintf(conf_sip_nat_fp, "\
- localnet = %s\n\
- ",\
- pSub->valuestring\
- );
- }
- }
- }
- }
- else if(strcmp(g_row[1],"paging.record.config") == 0){
- pJson = cJSON_Parse(g_row[2]);
- if(cJSON_GetObjectItem(pJson, "paging_record") != NULL){
- fprintf(conf_global_fp, "\
- PAGING_RECORD = %s\n\
- ",\
- cJSON_GetObjectItem(pJson, "paging_record")->valuestring\
- );
- }
- if(cJSON_GetObjectItem(pJson, "intercom_record") != NULL){
- fprintf(conf_global_fp, "\
- INTERCOM_RECORD = %s\n\
- ",\
- cJSON_GetObjectItem(pJson, "intercom_record")->valuestring\
- );
- }
- if(cJSON_GetObjectItem(pJson, "conference_record") != NULL){
- fprintf(conf_global_fp, "\
- CONFERENCE_RECORD = %s\n\
- ",\
- cJSON_GetObjectItem(pJson, "conference_record")->valuestring\
- );
- }
- }
- else if(strcmp(g_row[1],"pbx.autoanswer.config") == 0){
- pJson = cJSON_Parse(g_row[2]);
- if(cJSON_GetObjectItem(pJson, "intercom_autoanswer") != NULL){
- fprintf(conf_global_fp, "\
- INTERCOM_AUTO = %s\n\
- ",\
- cJSON_GetObjectItem(pJson, "intercom_autoanswer")->valuestring\
- );
- }
- else
- {
- fprintf(conf_global_fp, "\
- INTERCOM_AUTO = yes\n\
- ");
- }
- if(cJSON_GetObjectItem(pJson, "paging_autoanswer") != NULL){
- fprintf(conf_global_fp, "\
- PAGING_AUTO = %s\n\
- ",\
- cJSON_GetObjectItem(pJson, "paging_autoanswer")->valuestring\
- );
- }
- else
- {
- fprintf(conf_global_fp, "\
- PAGING_AUTO = yes\n\
- ");
- }
- }
- else if(strcmp(g_row[1],"paging.calltrigger.config") == 0){
- pJson = cJSON_Parse(g_row[2]);
- fprintf(conf_calltrigger_fp, "\
- exten => _%s.,1,Macro(calltrigger,${EXTEN})\n\
- exten => _%s.,1,Macro(calltrigger,${EXTEN})\n\
- ",\
- cJSON_GetObjectItem(pJson, "start")->valuestring,\
- cJSON_GetObjectItem(pJson, "stop")->valuestring\
- );
- }
- else if(strcmp(g_row[1],"pbx.sipsettings.config") == 0){
- pJson = cJSON_Parse(g_row[2]);
- if(cJSON_GetObjectItem(pJson, "udp"))
- {
- fprintf(conf_sipsetting_fp, "udpbindaddr = 0.0.0.0:%d\n",cJSON_GetObjectItem(pJson, "udp")->valueint);
- }
- pSub = cJSON_GetObjectItem(pJson, "tcp");
- if(pSub && cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_sipsetting_fp, "\
- tcpenable = yes\n\
- tcpbindaddr = 0.0.0.0:%d\n\
- ",\
- cJSON_GetObjectItem(pSub, "port")->valueint\
- );
- }
- pSub = cJSON_GetObjectItem(pJson, "tls");
- if(pSub && cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_sipsetting_fp, "\
- tlsenable = yes\n\
- tlsbindaddr = 0.0.0.0:%d\n\
- ",\
- cJSON_GetObjectItem(pSub, "port")->valueint\
- );
- }
- pSub = cJSON_GetObjectItem(pJson, "rtp");
- if(pSub){
- fprintf(conf_rtpsetting_fp, "\
- rtpstart = %d\n\
- rtpend = %d\n\
- ",\
- cJSON_GetObjectItem(pSub, "start_port")->valueint,\
- cJSON_GetObjectItem(pSub, "end_port")->valueint\
- );
- }
- }
- else if(strcmp(g_row[1],"paging.featurecodes.config") == 0){
- pJson = cJSON_Parse(g_row[2]);
- pSub = cJSON_GetObjectItem(pJson, "bargein");
- if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_featurecodes_fp, "exten = _%s.,1,Macro(spy-barge,${EXTEN:%ld},${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
- }
- pSub = cJSON_GetObjectItem(pJson, "clear");
- if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_featurecodes_fp, "exten = _%s.,1,Macro(exten-clear,${EXTEN:%ld},${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
- }
- pSub = cJSON_GetObjectItem(pJson, "syp");
- if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_featurecodes_fp, "exten = _%s.,1,Macro(spy-normal,${EXTEN:%ld},${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
- }
- pSub = cJSON_GetObjectItem(pJson, "whisper");
- if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_featurecodes_fp, "exten = _%s.,1,Macro(spy-whisper,${EXTEN:%ld},${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
- }
- pSub = cJSON_GetObjectItem(pJson, "wakeup");
- if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_featurecodes_fp, "exten = %s,1,Macro(wakeup-call,${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
- }
- pSub = cJSON_GetObjectItem(pJson, "dnd");
- if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-dnd-on,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
- }
- pSub = cJSON_GetObjectItem(pJson, "cf-alway");
- if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_featurecodes_fp, "exten = _%s.,1,Goto(app-cf-on,cf-${EXTEN:%ld},1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
- fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cf-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
- }
- pSub = cJSON_GetObjectItem(pJson, "cf-busy");
- if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_featurecodes_fp, "exten = _%s.,1,Goto(app-cfb-on,cf-${EXTEN:%ld},1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
- fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cfb-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
- }
- pSub = cJSON_GetObjectItem(pJson, "cf-noanswer");
- if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
- fprintf(conf_featurecodes_fp, "exten = _%s.,1,Goto(app-cfu-on,cf-${EXTEN:%ld},1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
- fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cfu-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
- }
- }
- }
- fclose(conf_dialrule_fp);
- fclose(conf_ivr_fp);
- fclose(conf_inbound_fp);
- fclose(conf_global_fp);
- fclose(conf_calltrigger_fp);
- fclose(conf_featurecodes_fp);
- fclose(conf_sip_nat_fp);
- fclose(conf_sipsetting_fp);
- mysql_free_result(g_res); // 释放结果集
- mysql_close(g_conn); // 关闭链接
- cJSON_Delete(pJson);
- }
|