/* ============================================================================ Name : generate_paging_conf.sh Author : ssc Version : v1.0 Copyright : ZYCOO copyright Description : Generate paging info from mysql to paging conf file ============================================================================ */ #include #include #include #include #include #include #include #include #include MYSQL *g_conn; // mysql 连接 MYSQL_RES *u_res; // mysql user记录集 MYSQL_ROW u_row; // 字符串数组,mysql 记录行 MYSQL_RES *g_res; // mysql group记录集 MYSQL_ROW g_row; // 字符串数组,mysql 记录行 MYSQL_RES *wb_res; // mysql webrtc记录 MYSQL_ROW wb_row; // 字符串数组,mysql 记录行 MYSQL_RES *s_res; // mysql service记录集 MYSQL_ROW s_row; // 字符串数组,mysql 记录行 #define FALSE 0 #define TRUE 1 #define MAX_TRUNK_SIZE 256 #define MAX_SIZE 2048 #define MIDLE_SIZE 512 #define MINI_SIZE 64 #define EXTEN_USER_QUEUE_FILE "/etc/asterisk/extensions_users_queue_custom.conf" #define EXTEN_USERS_CONTEXT_FILE "/etc/asterisk/extensions_users_context_custom.conf" #define EXTEN_USER_CONTEXT_FILE "/etc/asterisk/extensions_user_context.conf" #define EXTEN_USERS_GLOBAL_FILE "/etc/asterisk/extensions_users_global_custom.conf" #define QUEUES_FILE "/etc/asterisk/queues.conf" #define KEYVALLEN 100 #define VERSION "V1.0.1" #define QUERY_PAGING_USER_SQL "select id,phones,strategy,ring_duration,noanswer_dest,webexten_id,level from t_paging_users" 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 sql_tmp[MIDLE_SIZE]; char exten_tmp[MAX_SIZE]; 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 = NULL,*pSub = NULL,*dJson = NULL; int iCount=0; char noanswer_dest[64], dialrule[4096], strtmp[64]; int paging, intercom, task, outcall; memset(g_host_name, 0, sizeof(g_host_name)); memset(g_user_name, 0, sizeof(g_user_name)); memset(g_password, 0, sizeof(g_password)); memset(g_db_name, 0, sizeof(g_db_name)); 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_PAGING_USER_SQL)){ print_mysql_error(NULL); exit(1); } u_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集 FILE *conf_user_queue_fp = fopen(EXTEN_USER_QUEUE_FILE, "w+"); FILE *conf_users_fp = fopen(EXTEN_USERS_CONTEXT_FILE, "w+"); FILE *conf_user_context_fp = fopen(EXTEN_USER_CONTEXT_FILE, "w+"); FILE *global_fp = fopen(EXTEN_USERS_GLOBAL_FILE, "w+"); FILE *conf_queues_fp = fopen(QUEUES_FILE, "w+"); if (conf_user_queue_fp == NULL){ perror("Open paging conf file Error: "); exit(1); } fprintf(conf_user_queue_fp, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: extensions_users_queue_custom.conf (/etc/asterisk/extensions_users_queue_custom.conf)\n\ ;! Generator: Generator Paging\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_users_fp == NULL){ perror("Open context conf file Error: "); exit(1); } fprintf(conf_users_fp, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: extensions_users_context_custom.conf (/etc/asterisk/extensions_users_context_custom.conf)\n\ ;! Generator: Generator Users Context\n\ ;! Creation Date: %s\n\ ;!\n\n\ [DialRule_users]\n\ ",\ mytime()\ ); if (conf_user_context_fp == NULL){ perror("Open user context conf file Error: "); exit(1); } fprintf(conf_user_context_fp, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: extensions_user_context.conf (/etc/asterisk/extensions_user_context.conf)\n\ ;! Generator: Generator User Context\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (global_fp == NULL){ perror("Open context conf file Error: "); exit(1); } fprintf(global_fp, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: extensions_users_global_custom.conf (/etc/asterisk/extensions_users_global_custom.conf)\n\ ;! Generator: Generator User Global\n\ ;! Creation Date: %s\n\ ;!\n\n\ ",\ mytime()\ ); if (conf_queues_fp == NULL){ perror("Open paging conf file Error: "); exit(1); } fprintf(conf_queues_fp, ";!\n\ ;! Automatically generated configuration file\n\ ;! Filename: queues.conf (/etc/asterisk/queues.conf)\n\ ;! Generator: Generator Paging\n\ ;! Creation Date: %s\n\ ;!\n\n\ \n\ [general]\n\ persistentmembers = yes\n\ \n\ ",\ mytime()\ ); while ((u_row=mysql_fetch_row(u_res))){ // 打印结果集 if (u_row[0] == NULL || (u_row[1] == NULL && u_row[5] == NULL) || u_row[2] == NULL){ printf("some feild is empty!\n"); continue; } int id = atoi(u_row[0]); if(id == 1) continue; if(u_row[1] != NULL || u_row[5] != NULL){ if(u_row[1]) { pJson = cJSON_Parse(u_row[1]); iCount = cJSON_GetArraySize(pJson); } if(iCount > 0 || u_row[5] != NULL){ int q = 100000 + id; fprintf(conf_user_queue_fp, "[manager-queue-%d]\n", q); fprintf(conf_queues_fp, "\ [Q%d]\n\ setinterfacevar = yes\n\ setqueueentryvar = yes\n\ strategy = %s\n\ timeout = 30\n\ wrapuptime = 0\n\ autofill = yes\n\ autopause = no\n\ ringinuse = no\n\ maxlen = 8\n\ context = queue-custom\n\ joinempty = no\n\ leavewhenempty = paused,unavailable,invalid,unknown\n\ periodic-announce-frequency = 0\n\ reportholdtime = no\n\ announce-frequency = 0\n\ announce-holdtime = no\n\ announce-position = no\n\ queue-youarenext =\n\ queue-callswaiting =\n\ queue-holdtime =\n\ queue-minutes =\n\ queue-thankyou =\n\ musicclass = queuemusic\n\ \n",q,u_row[2]); //获取操作员权限 paging = intercom = task = outcall = FALSE; memset(sql_tmp,0,sizeof(sql_tmp)); sprintf(sql_tmp,"select tPagingServiceId from t_paging_userServices where UserId = %d",id); if (executesql(sql_tmp)){ print_mysql_error(NULL); exit(1); } s_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集 while ((s_row=mysql_fetch_row(s_res))){ // 打印结果集 int id = atoi(s_row[0]); switch (id) { case 1: paging = TRUE; break; case 4: intercom = TRUE; break; case 7: task = TRUE; break; case 9: outcall = TRUE; break; } } //获取操作员所在队列的号码 memset(sql_tmp,0,sizeof(sql_tmp)); sprintf(sql_tmp,"select exten from t_paging_userGroups JOIN t_paging_groups on t_paging_groups.id = t_paging_userGroups.GroupId where UserId = %d",id); if (executesql(sql_tmp)){ print_mysql_error(NULL); exit(1); } g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集 memset(dialrule, '\0', sizeof(dialrule)); while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集 if (g_row[0] == NULL){ printf("some feild is empty!\n"); continue; } sprintf(strtmp,"include => paging-group-%s\n", g_row[0]); strcat(dialrule, strtmp); if(paging) { sprintf(strtmp,"include => paging-group-%s\n", g_row[0]); strcat(dialrule, strtmp); } if(intercom) { sprintf(strtmp, "include => extens-group-%s\n", g_row[0]); strcat(dialrule, strtmp); } } strcat(dialrule, "include => DialRule_users\n"); if(task) strcat(dialrule, "include => call-trigger\n"); if(outcall) strcat(dialrule, "include => CallingRule_OutCall\n"); dJson = cJSON_Parse(u_row[4]); memset(noanswer_dest,0,sizeof(noanswer_dest)); if(dJson) { if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "hangup") == 0){ strcpy(noanswer_dest,"Goto(hangup,s,1)"); } else if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "extension") == 0){ sprintf(noanswer_dest,"Goto(default,%s,1)",cJSON_GetObjectItem(dJson, "exten")->valuestring); } else if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "user") == 0){ int id = 100000 + cJSON_GetObjectItem(dJson, "id")->valueint; sprintf(noanswer_dest,"Goto(manager-queue-%d,s,1)",id); } else if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "outcall") == 0){ sprintf(noanswer_dest,"Goto(CallingRule_OutCall,%s,1)",cJSON_GetObjectItem(dJson, "exten")->valuestring); } } else { strcpy(noanswer_dest,"Goto(hangup,s,1)"); } printf("parse success\n"); if(u_row[5] != NULL) { memset(sql_tmp,0,sizeof(sql_tmp)); sprintf(sql_tmp,"select exten from t_pbx_users_webrtc where id = %s",u_row[5]); if (executesql(sql_tmp)){ print_mysql_error(NULL); exit(1); } wb_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集 while ((wb_row=mysql_fetch_row(wb_res))){ // 打印结果集 if (wb_row[0] == NULL){ printf("some feild is empty!\n"); continue; } fprintf(conf_queues_fp, "member => PJSIP/%s\n",wb_row[0]); fprintf(conf_users_fp, "exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",wb_row[0],wb_row[0],wb_row[0]); fprintf(global_fp, "USER_ID_%s = %d\n",wb_row[0],id); fprintf(global_fp, "USER_LEVEL_%s = %s\n",wb_row[0],u_row[6]); fprintf(global_fp, "WEB_USER_%s = %d\n",wb_row[0],id); fprintf(conf_user_queue_fp, "exten => %s,1,Gosub(queue,s,1(Q%d,${EXTEN},%s))\n", wb_row[0],q,u_row[3]); fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest); fprintf(conf_user_context_fp, "[DialRule_%s]\n",wb_row[0]); fprintf(conf_user_context_fp, "include => cdr-action\n"); fprintf(conf_user_context_fp, "include => featurecodes\n"); fprintf(conf_user_context_fp, "%s\n", dialrule); } } if(iCount > 0) { if(strcmp(u_row[2],"ringall") == 0){ for(int i = 0;i < iCount;i++){ pSub = cJSON_GetArrayItem(pJson,i); if(pSub != NULL){ fprintf(conf_queues_fp, "member => PJSIP/%s\n",pSub->valuestring); fprintf(conf_users_fp, "exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",pSub->valuestring,pSub->valuestring,pSub->valuestring); fprintf(global_fp, "USER_ID_%s = %d\n",pSub->valuestring,id); fprintf(global_fp, "USER_LEVEL_%s = %s\n",pSub->valuestring,u_row[6]); fprintf(conf_user_queue_fp, "exten => %s,1,Gosub(queue,s,1(Q%d,${EXTEN},%s))\n", pSub->valuestring,q,u_row[3]); fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest); fprintf(conf_user_context_fp, "[DialRule_%s]\n",pSub->valuestring); fprintf(conf_user_context_fp, "include => cdr-action\n"); fprintf(conf_user_context_fp, "include => featurecodes\n"); fprintf(conf_user_context_fp, "%s\n", dialrule); } } fprintf(conf_user_queue_fp, "exten => s,1,Gosub(queue,s,1(Q%d,%s,%s))\n", q, pSub->valuestring,u_row[3]); fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest); }else{ for(int i = 0;i < iCount;i++){ pSub = cJSON_GetArrayItem(pJson,i); if(pSub != NULL){ fprintf(conf_queues_fp, "member => PJSIP/%s,%d\n",pSub->valuestring,i); fprintf(conf_users_fp, "exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",pSub->valuestring,pSub->valuestring,pSub->valuestring); fprintf(global_fp, "USER_ID_%s = %d\n",pSub->valuestring,id); fprintf(global_fp, "USER_LEVEL_%s = %s\n",pSub->valuestring,u_row[6]); fprintf(conf_user_queue_fp, "exten => %s,1,Gosub(queue,s,1(Q%d,${EXTEN},%s))\n", pSub->valuestring,q,u_row[3]); fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest); fprintf(conf_user_context_fp, "[DialRule_%s]\n",pSub->valuestring); fprintf(conf_user_context_fp, "include => cdr-action\n"); fprintf(conf_user_context_fp, "include => featurecodes\n"); fprintf(conf_user_context_fp, "%s\n", dialrule); } } fprintf(conf_user_queue_fp, "exten => s,1,Gosub(queue,s,1(Q%d,%s,%s))\n", q, pSub->valuestring,u_row[3]); fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest); } } } } } fclose(conf_queues_fp); fclose(conf_user_queue_fp); fclose(conf_users_fp); fclose(conf_user_context_fp); fclose(global_fp); mysql_free_result(u_res); // 释放结果集 mysql_free_result(g_res); mysql_free_result(s_res); mysql_close(g_conn); // 关闭链接 if(dJson) cJSON_Delete(dJson); if(pJson) cJSON_Delete(pJson); }