123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- /*
- ============================================================================
- Name : generate_paging_conf.sh
- Author : ssc
- Version : v1.0
- Copyright : ZYCOO copyright
- Description : Generate paging info from mysql to paging 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 group记录集
- MYSQL_ROW g_row; // 字符串数组,mysql 记录行
- MYSQL_RES *d_res; // mysql device记录集
- MYSQL_ROW d_row; // 字符串数组,mysql 记录行
- MYSQL_RES *r_res; // mysql device记录集
- MYSQL_ROW r_row; // 字符串数组,mysql 记录行
- #define MAX_TRUNK_SIZE 256
- #define MAX_SIZE 20480
- #define MIDLE_SIZE 512
- #define MINI_SIZE 64
- #define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
- #define EXTEN_PAGING_FILE "/etc/asterisk/extensions_paging_custom.conf"
- #define EXTEN_EXTENS_FILE "/etc/asterisk/extensions_extens_custom.conf"
- #define EXTEN_IPPHONES_FILE "/etc/asterisk/extensions_phones_custom.conf"
- #define EXTEN_HINTS_FILE "/etc/asterisk/extensions_hints_custom.conf"
- #define KEYVALLEN 100
- #define VERSION "V1.0.1"
- #define QUERY_PAGING_GROUP_SQL "select id,name,exten,paging_mode,paging_volume from t_paging_groups"
- 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 dest_tmp[MAX_SIZE];
- char page_option[MINI_SIZE];
- char page_volume[MINI_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;
-
- 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_GROUP_SQL)){
- print_mysql_error(NULL);
- exit(1);
- }
- g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
- FILE *conf_paging_fp = fopen(EXTEN_PAGING_FILE, "w+");
- FILE *conf_extens_fp = fopen(EXTEN_EXTENS_FILE, "w+");
- FILE *conf_ipphones_fp = fopen(EXTEN_IPPHONES_FILE, "w+");
- FILE *conf_hints_fp = fopen(EXTEN_HINTS_FILE, "w+");
- if (conf_paging_fp == NULL){
- perror("Open paging conf file Error: ");
- exit(1);
- }
- fprintf(conf_paging_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_paging_custom.conf (/etc/asterisk/extensions_paging_custom.conf)\n\
- ;! Generator: Generator Paging\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- ",\
- mytime()\
- );
- if (conf_extens_fp == NULL){
- perror("Open extens conf file Error: ");
- exit(1);
- }
- fprintf(conf_extens_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_extens_custom.conf (/etc/asterisk/extensions_extens_custom.conf)\n\
- ;! Generator: Generator Extens\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- ",\
- mytime()\
- );
- if (conf_ipphones_fp == NULL){
- perror("Open ipphones conf file Error: ");
- exit(1);
- }
- fprintf(conf_ipphones_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_phones_custom.conf (/etc/asterisk/extensions_phones_custom.conf)\n\
- ;! Generator: Generator phones\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- ",\
- mytime()\
- );
- if (conf_hints_fp == NULL){
- perror("Open hints conf file Error: ");
- exit(1);
- }
- fprintf(conf_hints_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_hints_custom.conf (/etc/asterisk/extensions_hints_custom.conf)\n\
- ;! Generator: Generator hints\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- [default]\n\
- ",\
- mytime()\
- );
- while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
- if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL || g_row[3] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- fprintf(conf_extens_fp, "[extens-group-%s]\n",g_row[2]);
- fprintf(conf_ipphones_fp, "[phones-group-%s]\n",g_row[2]);
- memset(sql_tmp,0,sizeof(sql_tmp));
- sprintf(sql_tmp,"select exten,type_id,allowed_pa from t_paging_deviceGroups JOIN t_paging_devices on t_paging_devices.id = t_paging_deviceGroups.DeviceId\
- where GroupId = %s and t_paging_devices.type_id in('1','2','3','5') and t_paging_devices.user_id is NULL",g_row[0]);
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- exit(1);
- }
- d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
- memset(exten_tmp,0,sizeof(exten_tmp));
- memset(dest_tmp,0,sizeof(dest_tmp));
- while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
- if (d_row[0] == NULL || d_row[1] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- if(strcmp(d_row[2],"yes") == 0){
- //sprintf(exten_tmp, "%sSIP/%s&", exten_tmp, d_row[0]);
- //sprintf(dest_tmp, "%s%s|", dest_tmp, d_row[0]);
- strcat(exten_tmp,"PJSIP/");
- strcat(exten_tmp,d_row[0]);
- strcat(exten_tmp,"&");
- strcat(dest_tmp,d_row[0]);
- strcat(dest_tmp,"|");
- }
-
- int id = atoi(d_row[1]);
- switch(id){
- case 1:
- fprintf(conf_extens_fp, "exten => %s,1,Gosub(page,s,1(%s,PJSIP/%s))\n",d_row[0],d_row[0],d_row[0]);
- break;
- case 2:
- fprintf(conf_extens_fp, "exten => %s,1,Gosub(intercom,s,1(%s,PJSIP/%s))\n",d_row[0],d_row[0],d_row[0]);
- break;
- case 3:
- case 7:
- fprintf(conf_ipphones_fp, "exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",d_row[0],d_row[0],d_row[0]);
- break;
- }
- }
- if(strlen(exten_tmp) > 0){
- exten_tmp[strlen(exten_tmp) - 1] = '\0';
- dest_tmp[strlen(dest_tmp) - 1] = '\0';
- }
-
- fprintf(conf_extens_fp, "\n");
- memset(page_option, 0, sizeof(page_option));
- if(strcmp(g_row[3],"duplex") == 0){
- strcat(page_option,"d");
- }
- memset(page_volume, '\0', sizeof(page_volume));
- if(g_row[4] != NULL)
- {
- pJson = cJSON_Parse(g_row[4]);
- if(pJson && cJSON_HasObjectItem(pJson, "enableForce"))
- {
- if(cJSON_IsBool(cJSON_GetObjectItem(pJson, "enableForce")) && cJSON_IsTrue(cJSON_GetObjectItem(pJson, "enableForce")))
- {
- sprintf(page_volume,"\\;volume=%d",cJSON_GetObjectItem(pJson, "softvolume")->valueint);
- }
- }
- }
-
- fprintf(conf_paging_fp, "\
- [paging-group-%s]\n\
- exten => %s,1,NoOp(%s)\n\
- same => n,MSet(__SRCEXTEN=${CALLERID(num)},__DESTS=%s,DATE=${STRFTIME(${EPOCH},,%%Y%%m%%d)},__UUID=${UNIQUEID},__VOLSTR=%s)\n\
- same => n,GotoIf($[\"foo${PAGING_RECORD}\" != \"fooyes\"]?unrc)\n\
- same => n,System(/bin/sh /etc/scripts/shell_scripts.sh mkrcdir paging ${DATE})\n\
- same => n,Set(FILENAME=paging/${DATE}/paging-${SRCEXTEN}-${UUID}.wav)\n\
- same => n,MixMonitor(${FILENAME},b)\n\
- same => n(unrc),Gusub(get-user-level,s,1(${SRCEXTEN},))\n\
- same => n,AGI(agi://${AGISERVERHOST}:${AGISERVERPORT},paging,${DESTS},${SESSION_LEVEL},${SESSION_USERID})\n\
- same => n,ExecIf(${ISNULL(${DESTS})}?Hangup())\n\
- same => n,ExecIf($['foo${enPaging_prompt_start}'='fooyes']?Set(STARTPROMPT=qA(${Paging_start_file})))\n\
- same => n,UserEvent(controlEvent,sessionlevel:${SESSION_LEVEL},sessionuserid:${SESSION_USERID},src:${SRCEXTEN},dest:${DESTS},uuid:${UUID},status:paging)\n\
- same => n,Wait(1)\n\
- same => n,GotoIf(${ISNULL(${DESTCHANS})}?default)\n\
- same => n,MSet(startT=${STRFTIME(${EPOCH},,%%s)},__CALLEE=${DESTS},__calltype=paging,DEVICE_STATE(Custom:${EXTEN})=INUSE,__GROUPID=${EXTEN})\n\
- same => n,Page(${DESTCHANS},%sb(paging-update-status^s^1)${STARTPROMPT})\n\
- same => n,Hangup\n\
- same => n(default),MSet(startT=${STRFTIME(${EPOCH},,%%s)},__CALLEE=${DESTS},__calltype=paging,DEVICE_STATE(Custom:${EXTEN})=INUSE,__GROUPID=${EXTEN})\n\
- same => n,Page(%s,%sb(paging-update-status^s^1)${STARTPROMPT})\n\
- same => n,Hangup\
- \n\n", \
- g_row[2],\
- g_row[2],\
- g_row[1],\
- dest_tmp,\
- page_volume,\
- page_option,\
- exten_tmp,\
- page_option\
- );
- fprintf(conf_hints_fp,"\
- exten => %s,hint,Custom:%s\n\
- ",\
- g_row[2],\
- g_row[2]\
- );
- }
- memset(sql_tmp,0,sizeof(sql_tmp));
- sprintf(sql_tmp,"select conditions from t_paging_tasks where type='numberrule'");
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- exit(1);
- }
-
- r_res = mysql_store_result(g_conn);
- while(r_row=mysql_fetch_row(r_res))
- {
- pJson = cJSON_Parse(r_row[0]);
- if(cJSON_GetObjectItem(pJson, "extension"))
- {
- fprintf(conf_hints_fp,"\
- exten => *11%s,hint,Custom:*11%s\n\
- ",\
- cJSON_GetObjectItem(pJson, "extension")->valuestring,\
- cJSON_GetObjectItem(pJson, "extension")->valuestring\
- );
- }
- }
- fclose(conf_paging_fp);
- fclose(conf_extens_fp);
- fclose(conf_ipphones_fp);
- fclose(conf_hints_fp);
- mysql_free_result(g_res); // 释放结果集
- mysql_free_result(d_res);
- mysql_free_result(r_res);
- mysql_close(g_conn); // 关闭链接
- if(pJson) cJSON_Delete(pJson);
- }
|