Ver código fonte

推送事件

quit
Yu.ding 4 anos atrás
pai
commit
13f038f017

+ 91 - 51
usr/src/generate_context_conf.c

@@ -36,7 +36,7 @@ MYSQL_ROW d_row; // 字符串数组,mysql 记录行
 #define VERSION "V1.0.1"
 
 #define QUERY_INTERCOM_SQL "select id,exten from t_paging_devices where type_id in('2','5')"
-#define QUERY_IPPHONE_SQL "select exten from t_paging_devices where type_id = '3'"
+#define QUERY_IPPHONE_SQL "select id,exten,user_id from t_paging_devices where type_id = '3'"
 
 char g_host_name[MINI_SIZE];
 char g_user_name[MINI_SIZE];
@@ -315,7 +315,9 @@ mytime()\
         }
         int setout = 1;
         fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
+        fprintf(conf_fp, "include => call-trigger\n");
         memset(sql_tmp,0,sizeof(sql_tmp));
+        //获取对讲终端所在队列的号码
         sprintf(sql_tmp,"select exten from t_paging_deviceGroups JOIN t_paging_groups on t_paging_groups.id = t_paging_deviceGroups.GroupId where DeviceId = %s",g_row[0]);
         if (executesql(sql_tmp)){
             print_mysql_error(NULL);
@@ -330,6 +332,23 @@ mytime()\
             fprintf(conf_fp, "include => extens-group-%s\n", d_row[0]);
         }
         memset(sql_tmp,0,sizeof(sql_tmp));
+        //获取对讲终端所在队列的管理员ID
+        sprintf(sql_tmp,"select UserId from t_paging_deviceGroups JOIN t_paging_userGroups on t_paging_deviceGroups.GroupId = t_paging_userGroups.GroupId where DeviceId = %s group by UserId",g_row[0]);
+        if (executesql(sql_tmp)){
+            print_mysql_error(NULL);
+            exit(1);
+        }
+        d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
+        while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
+            if (d_row[0] == NULL){
+                printf("some feild is empty!\n");
+                continue;
+            }
+            int q = 100000 + atoi(d_row[0]);
+            fprintf(conf_fp, "include => manager-queue-%d\n", q);
+        }
+
+        memset(sql_tmp,0,sizeof(sql_tmp));
         sprintf(sql_tmp,"select exten,t_paging_userServices.tPagingServiceId as service_id from t_paging_groups\
         JOIN t_paging_userGroups on t_paging_groups.id = t_paging_userGroups.GroupId\
         JOIN t_paging_userServices on t_paging_userServices.UserId = t_paging_userGroups.UserId\
@@ -359,6 +378,7 @@ mytime()\
                     break;
             }
         }
+
     }
 
     //IP话机根据所绑定的用户赋予所在group的权限。
@@ -370,64 +390,84 @@ mytime()\
     g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
     
     while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
-        if (g_row[0] == NULL){
+        if (g_row[0] == NULL || g_row[1] == NULL){
             printf("some feild is empty!\n");
             continue;
         }
         int setout = 1;
-        fprintf(conf_fp, "[DialRule_%s]\n",g_row[0]);
-        memset(sql_tmp,0,sizeof(sql_tmp));
-        sprintf(sql_tmp,"select exten,t_paging_userServices.tPagingServiceId as service_id from t_paging_groups\
-        JOIN t_paging_userGroups on t_paging_groups.id = t_paging_userGroups.GroupId\
-        JOIN t_paging_userServices on t_paging_userServices.UserId = t_paging_userGroups.UserId\
-        where t_paging_userServices.tPagingServiceId in(1,4,9) and t_paging_userGroups.UserId\
-        in(select t_paging_users.id from t_paging_users join t_paging_devices on t_paging_users.id = t_paging_devices.user_id\
-        where t_paging_devices.exten=%s) order by t_paging_userServices.tPagingServiceId",g_row[0]);
-        if (executesql(sql_tmp)){
-            print_mysql_error(NULL);
-            exit(1);
-        }
-        d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
-        while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
-            if (d_row[0] == NULL || d_row[1] == NULL){
-                printf("some feild is empty!\n");
-                continue;
+        fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
+        fprintf(conf_fp, "include => call-trigger\n");
+        if(g_row[2]){
+            memset(sql_tmp,0,sizeof(sql_tmp));
+            sprintf(sql_tmp,"select exten,t_paging_userServices.tPagingServiceId as service_id from t_paging_groups\
+            JOIN t_paging_userGroups on t_paging_groups.id = t_paging_userGroups.GroupId\
+            JOIN t_paging_userServices on t_paging_userServices.UserId = t_paging_userGroups.UserId\
+            where t_paging_userServices.tPagingServiceId in(1,4,9) and t_paging_userGroups.UserId\
+            in(select t_paging_users.id from t_paging_users join t_paging_devices on t_paging_users.id = t_paging_devices.user_id\
+            where t_paging_devices.exten=%s) order by t_paging_userServices.tPagingServiceId",g_row[1]);
+            if (executesql(sql_tmp)){
+                print_mysql_error(NULL);
+                exit(1);
             }
-            int id = atoi(d_row[1]);
-            switch(id){
-                case 1:
-                    fprintf(conf_fp, "include => paging-group-%s\n", d_row[0]);
-                    break;
-                case 4:
-                    fprintf(conf_fp, "include => extens-group-%s\n", d_row[0]);
-                    break;
-                case 9:
-                    if(setout){
-                        fprintf(conf_fp, "include => CallingRule_OutCall\n");
-                        setout = 0;
-                    }
-                    break;
+            d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
+            while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
+                if (d_row[0] == NULL || d_row[1] == NULL){
+                    printf("some feild is empty!\n");
+                    continue;
+                }
+                int id = atoi(d_row[1]);
+                switch(id){
+                    case 1:
+                        fprintf(conf_fp, "include => paging-group-%s\n", d_row[0]);
+                        break;
+                    case 4:
+                        fprintf(conf_fp, "include => extens-group-%s\n", d_row[0]);
+                        break;
+                    case 9:
+                        if(setout){
+                            fprintf(conf_fp, "include => CallingRule_OutCall\n");
+                            setout = 0;
+                        }
+                        break;
+                }
             }
-        }
-        memset(sql_tmp,0,sizeof(sql_tmp));
-        sprintf(sql_tmp,"select t_paging_users.id,t_paging_users.level from t_paging_users join t_paging_devices on t_paging_users.id = t_paging_devices.user_id\
-        where t_paging_devices.exten=%s",g_row[0]);
-        if (executesql(sql_tmp)){
-            print_mysql_error(NULL);
-            exit(1);
-        }
-        d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
-        while((d_row=mysql_fetch_row(d_res))){
-            if (d_row[0] == NULL || d_row[1] == NULL){
-                printf("some feild is empty!\n");
-                continue;
+            memset(sql_tmp,0,sizeof(sql_tmp));
+            sprintf(sql_tmp,"select t_paging_users.id,t_paging_users.level from t_paging_users join t_paging_devices on t_paging_users.id = t_paging_devices.user_id\
+            where t_paging_devices.exten=%s",g_row[1]);
+            if (executesql(sql_tmp)){
+                print_mysql_error(NULL);
+                exit(1);
+            }
+            d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
+            while((d_row=mysql_fetch_row(d_res))){
+                if (d_row[0] == NULL || d_row[1] == NULL){
+                    printf("some feild is empty!\n");
+                    continue;
+                }
+                fprintf(conf_fp, "include => DialRule_users\n");
+                fprintf(conf_fp, "include => featurecodes\n");
+                fprintf(conf_users_fp, "exten => %s,1,Macro(stdexten,%s,SIP/%s)\n",g_row[1],g_row[1],g_row[1]);
+                fprintf(global_fp, "USER_ID_%s = %s\n",g_row[1],d_row[0]);
+                fprintf(global_fp, "USER_LEVEL_%s = %s\n",g_row[1],d_row[1]);
+            }
+        }else{
+            memset(sql_tmp,0,sizeof(sql_tmp));
+            sprintf(sql_tmp,"select UserId from t_paging_deviceGroups JOIN t_paging_userGroups on t_paging_deviceGroups.GroupId = t_paging_userGroups.GroupId where DeviceId = %s group by UserId",g_row[0]);
+            if (executesql(sql_tmp)){
+                print_mysql_error(NULL);
+                exit(1);
+            }
+            d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
+            while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
+                if (d_row[0] == NULL){
+                    printf("some feild is empty!\n");
+                    continue;
+                }
+                int q = 100000 + atoi(d_row[0]);
+                fprintf(conf_fp, "include => manager-queue-%d\n", q);
             }
-            fprintf(conf_fp, "include => DialRule_users\n");
-            fprintf(conf_fp, "include => featurecodes\n");
-            fprintf(conf_users_fp, "exten => %s,1,Macro(stdexten,%s,SIP/%s)\n",g_row[0],g_row[0],g_row[0]);
-            fprintf(global_fp, "USER_ID_%s = %s\n",g_row[0],d_row[0]);
-            fprintf(global_fp, "USER_LEVEL_%s = %s\n",g_row[0],d_row[1]);
         }
+        
     }
 fclose(conf_fp);
 fclose(conf_users_fp);

+ 29 - 0
usr/src/generate_extension_conf.c

@@ -14,6 +14,7 @@ Description : Generate trunk info from mysql to turnk conf file
 #include <errno.h>
 #include <assert.h>
 #include <time.h>
+#include <ctype.h>
 #include <cjson/cJSON.h>
 #include <mysql/mysql.h>
 
@@ -29,6 +30,7 @@ MYSQL_ROW g_row; // 字符串数组,mysql 记录行
 #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 KEYVALLEN 100
 #define VERSION "V1.0.1"
 
@@ -400,6 +402,7 @@ if (executesql(QUERY_GLOBAL_SQL)){
 g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
 FILE *conf_inbound_fp = fopen(EXTEN_INBOUND_FILE, "w+");
 FILE *conf_global_fp = fopen(EXTEN_GLOBAL_FILE, "w+");
+FILE *conf_calltrigger_fp = fopen(EXTEN_CALLTRIGGER_FILE, "w+");
 
 if (conf_inbound_fp == NULL){
         perror("Open paging conf file Error: ");
@@ -411,6 +414,11 @@ if (conf_global_fp == NULL){
         exit(1);
     }
 
+if (conf_calltrigger_fp == NULL){
+        perror("Open paging 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\
@@ -431,6 +439,16 @@ mytime()\
 mytime()\
 );
 
+    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 GLOBAL\n\
+;! Creation Date: %s\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){
@@ -483,6 +501,16 @@ cJSON_GetObjectItem(pJson, "start")->valuestring,\
 cJSON_GetObjectItem(pJson, "end")->valuestring\
 );
         }
+        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, "end")->valuestring\
+);
+        }
     }
 
 
@@ -490,5 +518,6 @@ fclose(conf_dialrule_fp);
 fclose(conf_ivr_fp);
 mysql_free_result(g_res); // 释放结果集
 mysql_close(g_conn); // 关闭链接
+cJSON_Delete(pJson);
 }
 

+ 17 - 9
usr/src/generate_group_conf.c

@@ -43,6 +43,7 @@ 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 *l_trim(char *szOutput, const char *szInput)
@@ -294,20 +295,25 @@ while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
     fprintf(conf_extens_fp, "[extens-group-%s]\n",g_row[2]);
     fprintf(conf_extens_fp, "include => cdr-action\n");
     memset(sql_tmp,0,sizeof(sql_tmp));
-    sprintf(sql_tmp,"select exten,type_id 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')",g_row[0]);
+    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;
         }
-        sprintf(exten_tmp, "%sSIP/%s&", exten_tmp, d_row[0]);
+        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]);
+        }
+        
         int id = atoi(d_row[1]);
         switch(id){
             case 1:
@@ -324,6 +330,7 @@ while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
     }
     if(strlen(exten_tmp) > 0){
         exten_tmp[strlen(exten_tmp) - 1] = '\0';
+        dest_tmp[strlen(dest_tmp) - 1] = '\0';
     }
 
     fprintf(conf_extens_fp, "\n");
@@ -337,17 +344,18 @@ same => n,SIPAddHeader(${CALLINFO})\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,UserEvent(controlEvent,userid:${USERID},src:${SRCEXTEN},dest:${DESTS},uuid:${UUID},status:stop)\n\
-same => n,Wait(2)\n\
-same => n,Playback(beep)\n\
-same => n,Page(%s,b(paging-update-status^s^1)q)\n\
+same => n,Macro(get-user-level,${SRCEXTEN},)\n\
+same => n,ExecIf($['foo${enPaging_prompt_start}'='fooyes']?Set(STARTPROMPT=qA(start)))\n\
+same => n,UserEvent(controlEvent,sessionlevel:${SESSION_LEVEL},sessionuserid:${SESSION_USERID},src:${SRCEXTEN},dest:%s,uuid:${UUID},status:stop)\n\
+same => n,Wait(1)\n\
+same => n,Page(%s,b(paging-update-status^s^1)${STARTPROMPT})\n\
 same => n,Hangup\
 \n\n", \
 g_row[2],\
 g_row[2],\
 g_row[1],\
-exten_tmp,\
-g_row[2]\
+dest_tmp,\
+exten_tmp\
 );
     }
 fclose(conf_paging_fp);

+ 334 - 0
usr/src/generate_user_conf.c

@@ -0,0 +1,334 @@
+/*
+============================================================================
+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 记录行
+
+#define MAX_TRUNK_SIZE 256
+#define MAX_SIZE 2048
+#define MIDLE_SIZE 512
+#define MINI_SIZE 64
+#define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
+#define EXTEN_USER_QUEUE_FILE "/etc/asterisk/extensions_users_queue_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 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 *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;
+/*
+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));
+
+GetProfileString(MYSQL_CONNECT_CONF, "general", "dbserverip", g_host_name);
+GetProfileString(MYSQL_CONNECT_CONF, "general", "dbuser", g_user_name);
+GetProfileString(MYSQL_CONNECT_CONF, "general", "dbpasswd", g_password);
+GetProfileString(MYSQL_CONNECT_CONF, "general", "dbname", 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);
+}
+
+g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
+FILE *conf_user_queue_fp = fopen(EXTEN_USER_QUEUE_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_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\
+",\
+mytime()\
+);
+
+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(g_row[1] != NULL){
+        pJson = cJSON_Parse(g_row[1]);
+        iCount = cJSON_GetArraySize(pJson);
+        if(iCount > 0){
+            int q = 100000 + atoi(g_row[0]);
+            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 = 20\n\
+wrapuptime = 0\n\
+autofill = yes\n\
+autopause = no\n\
+ringinuse = no\n\
+maxlen = 8\n\
+context = queue-custom\n\
+joinempty = no\n\
+leavewhenempty = no\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,g_row[2]);
+
+            for(int i = 0;i < iCount;i++){
+                pSub = cJSON_GetArrayItem(pJson,i);
+                if(pSub != NULL){
+                    fprintf(conf_queues_fp, "member => SIP/%s,%d\n",pSub->valuestring,i);
+                    fprintf(conf_user_queue_fp, "exten => %s,1,Macro(queue,Q%d,${EXTEN})\n", pSub->valuestring,q);
+                }
+            }
+        }
+    }
+}
+fclose(conf_queues_fp);
+fclose(conf_user_queue_fp);
+mysql_free_result(g_res); // 释放结果集
+mysql_free_result(d_res);
+mysql_close(g_conn); // 关闭链接
+cJSON_Delete(pJson);
+}