Yu.Ding 5 bulan lalu
induk
melakukan
659e295d17
13 mengubah file dengan 1680 tambahan dan 1672 penghapusan
  1. 36 0
      Makefile
  2. 0 128
      fail2ban_conf.c
  3. 0 128
      fail2ban_init.c
  4. 0 36
      fail2ban_rule.c
  5. 12 240
      generate_context_conf.c
  6. 101 254
      generate_extension_conf.c
  7. 13 143
      generate_group_conf.c
  8. 246 0
      generate_group_conf_acorp.c
  9. 598 0
      generate_pjsip_conf.c
  10. 389 582
      generate_trunk_conf.c
  11. 200 161
      generate_user_conf.c
  12. 85 0
      getaudio_time.c
  13. TEMPAT SAMPAH
      record_playtime

+ 36 - 0
Makefile

@@ -0,0 +1,36 @@
+
+.PHONY:pjsip user clean install uninstall
+
+pjsip:
+	${CC} -o generate_pjsip_conf generate_pjsip_conf.c -lm -lmysqlclient
+
+trunk:
+	${CC} -o generate_trunk_conf generate_trunk_conf.c -lm -lmysqlclient
+
+user:
+	${CC} -o generate_user_conf generate_user_conf.c -lm -lmysqlclient -lcjson
+
+context:
+	${CC} -o generate_context_conf generate_context_conf.c -lm -lmysqlclient
+
+exten:
+	${CC} -o generate_extension_conf generate_extension_conf.c -lm -lmysqlclient -lcjson
+
+group:
+	${CC} -o generate_group_conf generate_group_conf.c -lm -lmysqlclient -lcjson
+
+f2b_init:
+	${CC} -o fail2ban_init fail2ban_init.c -lm -lmysqlclient
+
+f2b_conf:
+	${CC} -o fail2ban_conf fail2ban_conf.c -lm -lmysqlclient
+
+ast_init:
+	${CC} -o ast_init ast_init.c -lm -lmysqlclient -lhiredis
+
+audio_time:
+	${CC} -o getaudio_time getaudio_time.c
+
+clean:
+	rm -f *.o ${TARGET}
+

+ 0 - 128
fail2ban_conf.c

@@ -41,134 +41,6 @@ char g_password[MINI_SIZE];
 char g_db_name[MINI_SIZE];
 const unsigned int g_db_port = 3306;
 
-//读取配置文件函数----功能:删除左边空格
-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);

+ 0 - 128
fail2ban_init.c

@@ -41,134 +41,6 @@ char g_password[MINI_SIZE];
 char g_db_name[MINI_SIZE];
 const unsigned int g_db_port = 3306;
 
-//读取配置文件函数----功能:删除左边空格
-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);

+ 0 - 36
fail2ban_rule.c

@@ -24,40 +24,8 @@
 #define SIZE_K 1024
 /*该程序的功能是从数据库中读取fail2ban的配置信息然后写到fail2ban的配置文件中然后重启fail2ban服务使配置生效,界面配置fail2ban的时候调用,需要编译*/
 
-char *getconfig(const char *file_path, const char *name)
-{
-	char str[SIZE] = {0};
-	char *p = NULL, *value = NULL;
-	int tmp = 0, len = 0;
-
-	FILE *fp = fopen(file_path, "r");
-	while(fgets(str, SIZE, fp)){
-		if(strstr(str, name)){
-			p = strstr(str, "=");
-			len = p - str;
-			value = malloc(50);
-			bzero(value, 50);
-			while(str[len] != '\n'){
-				if(str[len] == ' ' || str[len] == '='){
-					len++;
-					continue;
-				}else
-					value[tmp++] = str[len++];
-			}
-			//printf("%s : %s\n",name, value);
-			break;
-		}
-		
-	}
-	return value;
-}
-
 int connect_mysql(MYSQL *conn)
 {
-	//char *dbserver = getconfig(DBCONFIG, "dbserverip");
-	//char *dbuser = getconfig(DBCONFIG, "dbuser");
-	//char *dbpasswd = getconfig(DBCONFIG, "dbpasswd");
-	//char *dbname = getconfig(DBCONFIG, "dbname");
 	char dbserver[64];
 	char dbuser[64];
 	char dbpasswd[64];
@@ -69,9 +37,7 @@ int connect_mysql(MYSQL *conn)
 	strcpy(dbpasswd,getenv("MYSQL_PASSWORD"));
 	strcpy(dbname,getenv("MYSQL_DATABASE"));
 
-	printf("connect---1\n");
 	conn = mysql_init(NULL);
-	printf("connect---2\n");
 	if(!mysql_real_connect(conn, dbserver, dbuser, dbpasswd, dbname,dbport,NULL,0)){
 		printf("error:%s\n",mysql_error(conn));	
 		return -1;
@@ -79,7 +45,6 @@ int connect_mysql(MYSQL *conn)
 	// 是否连接已经可用
 	if (mysql_query(conn,"set names utf8")) // 如果失败
 		return -1;
-	printf("connect---3\n");
 	return 0;
 }
 
@@ -253,7 +218,6 @@ char *get_fb_config(char *buf)
 				strcat(ignored,(char *)row1[1]);
 				strcat(ignored," ");
 			}
-			//char *sshport = getconfig("/etc/asterisk/service.conf","ssh_port");
 			sprintf(tmp,"[SSH]\nenabled = %s\nignoreip = 127.0.0.1/32 %s \nport = 22\nfilter = sshd\nlogpath = /var/log/auth.log\nmaxretry = %s\nfindtime = %s\nbantime = %s\n\n",in, ignored, row[2], row[3], row[4]);
 			//sprintf(tmp,"[SSH]\nenabled = %s\nignoreip = 127.0.0.1/32 %s \nport = %s\nfilter = sshd\nlogpath = /var/log/auth.log\nmaxretry = %s\nfindtime = %s\nbantime = %s\n\n",in, ignored, sshport, row[2], row[3], row[4]);
 			//free(sshport);

+ 12 - 240
generate_context_conf.c

@@ -15,7 +15,6 @@ Description : Generate context info from mysql to context conf file
 #include <assert.h>
 #include <time.h>
 #include <ctype.h>
-#include <cjson/cJSON.h>
 
 #include <mysql/mysql.h>
 
@@ -29,10 +28,7 @@ MYSQL_ROW d_row; // 字符串数组,mysql 记录行
 #define MAX_SIZE 1024
 #define MIDLE_SIZE 512
 #define MINI_SIZE 64
-#define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
 #define EXTEN_CONTEXT_FILE "/etc/asterisk/extensions_context_custom.conf"
-#define EXTEN_USERS_CONTEXT_FILE "/etc/asterisk/extensions_users_context_custom.conf"
-#define EXTEN_USERS_GLOBAL_FILE "/etc/asterisk/extensions_users_global_custom.conf"
 #define KEYVALLEN 100
 #define VERSION "V1.0.1"
 
@@ -46,134 +42,6 @@ char g_db_name[MINI_SIZE];
 const unsigned int g_db_port = 3306;
 char sql_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);
@@ -218,25 +86,18 @@ return 0; // 返回成功
 
 
 int main(int argc, char **argv) {
-/*
+
 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"));
 
 FILE *conf_fp = fopen(EXTEN_CONTEXT_FILE, "w+");
-FILE *conf_users_fp = fopen(EXTEN_USERS_CONTEXT_FILE, "w+");
-FILE *global_fp = fopen(EXTEN_USERS_GLOBAL_FILE, "w+");
 
 if (conf_fp == NULL){
         perror("Open context conf file Error: ");
@@ -251,37 +112,6 @@ if (conf_fp == NULL){
 ;!\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 User Context\n\
-;! Creation Date: %s\n\
-;!\n\n\
-[DialRule_users]\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 (init_mysql()){
@@ -301,7 +131,6 @@ mytime()\
             printf("some feild is empty!\n");
             continue;
         }
-        int setout = 1;
         fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
         fprintf(conf_fp, "include => cdr-action\n");
         fprintf(conf_fp, "include => call-trigger\n");
@@ -371,83 +200,28 @@ mytime()\
             printf("some feild is empty!\n");
             continue;
         }
-        int setout = 1;
-        fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
-        fprintf(conf_fp, "include => cdr-action\n");
-        fprintf(conf_fp, "include => featurecodes\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);
-            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;
-            }
-            fprintf(conf_fp, "include => phones-group-%s\n", d_row[0]);
-            fprintf(conf_fp, "include => paging-group-%s\n", d_row[0]);
-        }
-        if(g_row[2]){
-            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]);     //查询分机号所属的用户ID和等级
-            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_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]);
-            }
+        if(g_row[2] == NULL){
+            fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
+            fprintf(conf_fp, "include => cdr-action\n");
+            fprintf(conf_fp, "include => featurecodes\n");
+            
+            //获取对讲终端所在队列的号码
             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,7,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]);        //查询分机所属管理员的权限以及所管理的组
+            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);
                 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){
+                if (d_row[0] == 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 7:
-                        fprintf(conf_fp, "include => call-trigger\n");
-                        break;
-                    case 9:
-                        if(setout){
-                            fprintf(conf_fp, "include => CallingRule_OutCall\n");
-                            setout = 0;
-                        }
-                        break;
-                }
+                fprintf(conf_fp, "include => phones-group-%s\n", d_row[0]);
+                fprintf(conf_fp, "include => paging-group-%s\n", d_row[0]);
             }
-        }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)){
@@ -467,8 +241,6 @@ mytime()\
         
     }
 fclose(conf_fp);
-fclose(conf_users_fp);
-fclose(global_fp);
 mysql_free_result(g_res); // 释放结果集
 mysql_free_result(d_res); // 释放结果集
 mysql_close(g_conn); // 关闭链接

+ 101 - 254
generate_extension_conf.c

@@ -32,8 +32,7 @@ MYSQL_ROW g_row; // 字符串数组,mysql 记录行
 #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 SIP_SETTINGS_FILE "/etc/asterisk/pjsip_transports.conf"
 #define RTP_SETTINGS_FILE "/etc/asterisk/rtp_settings.conf"
 #define KEYVALLEN 100
 #define VERSION "V1.0.1"
@@ -52,135 +51,6 @@ 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);
@@ -226,6 +96,7 @@ return 0; // 返回成功
 int main(int argc, char **argv) {
 cJSON *pJson,*pSub;
 int iCount=0;
+char sip_udp_nat[2048], sip_tcp_nat[2048], sip_tls_nat[2048], localnet[1792];
 
 typedef struct keys_action {
     char key[MINI_SIZE];
@@ -341,69 +212,6 @@ same => n,WaitExten(1)\n");
     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)
@@ -450,7 +258,7 @@ fputs("[CallingRule_OutCall]\n",conf_dialrule_fp);
         }
 
         memset(dialrule,0,sizeof(dialrule));
-        sprintf(dialrule, "exten => _%s,1,Macro(trunkdial-failover,${EXTEN},${%s}",g_row[1], g_row[0]);
+        sprintf(dialrule, "exten => _%s,1,Gosub(trunkdial-failover,s,1(${EXTEN},${%s}",g_row[1], g_row[0]);
         if(g_row[3] != NULL){
             strcat(dialrule,g_row[3]);
         }
@@ -462,7 +270,7 @@ fputs("[CallingRule_OutCall]\n",conf_dialrule_fp);
         if(g_row[4] != NULL){
             strcat(dialrule,g_row[4]);
         }
-        strcat(dialrule,")\n");
+        strcat(dialrule,"))\n");
         fputs(dialrule,conf_dialrule_fp);
     }
 
@@ -498,12 +306,6 @@ if (conf_featurecodes_fp == NULL){
     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: ");
@@ -561,21 +363,11 @@ mytime()\
 ;!\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\
+;! Filename: pjsip_transports.conf (/etc/asterisk/pjsip_transports.conf)\n\
 ;! Generator: Generator SIP SETTINGS\n\
 ;! Creation Date: %s\n\
 ;!\n\n\
@@ -690,43 +482,59 @@ 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\
+                memset(sip_udp_nat, 0, sizeof(sip_udp_nat));
+                memset(sip_tcp_nat, 0, sizeof(sip_tcp_nat));
+                memset(sip_tls_nat, 0, sizeof(sip_tls_nat));
+                memset(localnet, 0, sizeof(localnet));
+                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 ; }
+                        sprintf(localnet, "\
+local_net = %s\n\
+",\
+pSub->valuestring\
+);
+                    }
+                }
+                sprintf(sip_udp_nat, "\
+external_media_address = %s\n\
+external_signaling_address = %s\n\
+%s\n\
 ",\
 cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
 cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
-cJSON_GetObjectItem(pJson, "externrefresh")->valueint\
+localnet\
 );
                 if(cJSON_GetObjectItem(pJson, "externtcpport") && cJSON_GetObjectItem(pJson, "externtcpport")->valueint != 0)
                 {
-                    fprintf(conf_sip_nat_fp, "\
-externtcpport = %d\n\
+                    sprintf(sip_tcp_nat, "\
+external_media_address = %s\n\
+external_signaling_address = %s\n\
+external_signaling_port = %d\n\
+%s\n\
 ",\
-cJSON_GetObjectItem(pJson, "externtcpport")->valueint\
+cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
+cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
+cJSON_GetObjectItem(pJson, "externtcpport")->valueint,\
+localnet\
 );
                 }
                 if(cJSON_GetObjectItem(pJson, "externtlsport") && cJSON_GetObjectItem(pJson, "externtlsport")->valueint != 0)
                 {
-                    fprintf(conf_sip_nat_fp, "\
-externtlsport = %d\n\
+                    sprintf(sip_tls_nat, "\
+external_media_address = %s\n\
+external_signaling_address = %s\n\
+external_signaling_port = %d\n\
+%s\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\
+cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
+cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
+cJSON_GetObjectItem(pJson, "externtlsport")->valueint,\
+localnet\
 );
-                    }
                 }
             }
         }
@@ -786,8 +594,8 @@ 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\
+exten => _%s.,1,Gosub(calltrigger,s,1(${EXTEN}))\n\
+exten => _%s.,1,Gusub(calltrigger,s,1(${EXTEN}))\n\
 ",\
 cJSON_GetObjectItem(pJson, "start")->valuestring,\
 cJSON_GetObjectItem(pJson, "stop")->valuestring\
@@ -797,26 +605,66 @@ cJSON_GetObjectItem(pJson, "stop")->valuestring\
             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);
+                fprintf(conf_sipsetting_fp, "\
+[transport-udp]\n\
+type=transport\n\
+protocol=udp\n\
+bind=0.0.0.0:%d\n\
+tos=CS3\n\
+cos=3\n\
+allow_reload=no\n\
+",cJSON_GetObjectItem(pJson, "udp")->valueint\
+);
+                if(sip_udp_nat)
+                {
+                    fprintf(conf_sipsetting_fp, "%s", sip_udp_nat);
+                }
             }
             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\
+[transport-tcp]\n\
+type=transport\n\
+protocol=tcp\n\
+bind=0.0.0.0:%d\n\
+tos=CS3\n\
+cos=3\n\
+allow_reload=no\n\
+",cJSON_GetObjectItem(pSub, "port")->valueint\
 );
+                if(sip_tcp_nat)
+                {
+                    fprintf(conf_sipsetting_fp, "%s", sip_tcp_nat);
+                }
             }
             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\
+[transport-tls]\n\
+type=transport\n\
+protocol=tls\n\
+bind=0.0.0.0:%d\n\
+cert_file=/etc/asterisk/keys/asterisk.pem\n\
+ca_list_file=/etc/asterisk/keys/ca.crt\n\
+priv_key_file=/etc/asterisk/keys/asterisk.pem\n\
+method=tlsv1_3\n\
+tos=CS3\n\
+cos=3\n\
+allow_reload=no\n\
+",cJSON_GetObjectItem(pSub, "port")->valueint\
 );
+                if(sip_tls_nat)
+                {
+                    fprintf(conf_sipsetting_fp, "%s", sip_tls_nat);
+                }
             }
+            fprintf(conf_sipsetting_fp, "\
+[transport-wss]\n\
+type=transport\n\
+protocol=wss\n\
+bind=0.0.0.0\n\
+allow_reload=no\n\
+");
             pSub = cJSON_GetObjectItem(pJson, "rtp");
             if(pSub){
                 fprintf(conf_rtpsetting_fp, "\
@@ -832,23 +680,23 @@ cJSON_GetObjectItem(pSub, "end_port")->valueint\
             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));
+                fprintf(conf_featurecodes_fp, "exten = _%s.,1,Gosub(spy-barge,s,1(${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));
+                fprintf(conf_featurecodes_fp, "exten = _%s.,1,Gosub(exten-clear,s,1(${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));
+                fprintf(conf_featurecodes_fp, "exten = _%s.,1,Gosub(spy-normal,s,1(${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));
+                fprintf(conf_featurecodes_fp, "exten = _%s.,1,Gosub(spy-whisper,s,1(${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);
+                fprintf(conf_featurecodes_fp, "exten = %s,1,Gosub(wakeup-call,s,1(${CALLERID(num)}))\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
             }
             pSub = cJSON_GetObjectItem(pJson, "dnd");
             if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
@@ -879,7 +727,6 @@ 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); // 关闭链接

+ 13 - 143
generate_group_conf.c

@@ -52,134 +52,6 @@ char dest_tmp[MAX_SIZE];
 char page_option[MINI_SIZE];
 char page_volume[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);
@@ -223,7 +95,7 @@ return 0; // 返回成功
 }
 
 int main(int argc, char **argv) {
-cJSON *pJson;
+cJSON *pJson = NULL;
     
 memset(g_host_name, 0, sizeof(g_host_name));
 memset(g_user_name, 0, sizeof(g_user_name));
@@ -336,7 +208,7 @@ while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
         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,"SIP/");
+            strcat(exten_tmp,"PJSIP/");
             strcat(exten_tmp,d_row[0]);
             strcat(exten_tmp,"&");
             strcat(dest_tmp,d_row[0]);
@@ -346,14 +218,14 @@ while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
         int id = atoi(d_row[1]);
         switch(id){
             case 1:
-                fprintf(conf_extens_fp, "exten => %s,1,Macro(page,%s,SIP/%s)\n",d_row[0],d_row[0],d_row[0]);
+                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,Macro(intercom,%s,SIP/%s)\n",d_row[0],d_row[0],d_row[0]);
+                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,Macro(stdexten,%s,SIP/%s)\n",d_row[0],d_row[0],d_row[0]);
+                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;
         }
     }
@@ -361,7 +233,7 @@ while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
         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));
@@ -380,20 +252,16 @@ while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
             }
         }
     }
-
+    
     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})\n\
-same => n,GotoIf($[\"foo${PAGING_AUTO}\" = \"foono\"]?manual)\n\
-same => n,MSet(ALERTINFO=Alert-Info: Ring Answer,CALLINFO=Call-Info: <uri>\\;answer-after=0%s)\n\
-same => n,SIPAddHeader(${ALERTINFO})\n\
-same => n,SIPAddHeader(${CALLINFO})\n\
-same => n(manual),GotoIf($[\"foo${PAGING_RECORD}\" != \"fooyes\"]?unrc)\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),Macro(get-user-level,${SRCEXTEN},)\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\
@@ -429,6 +297,7 @@ g_row[2]\
         print_mysql_error(NULL);
         exit(1);
     }
+    
     r_res = mysql_store_result(g_conn);
     while(r_row=mysql_fetch_row(r_res))
     {
@@ -445,10 +314,11 @@ 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); // 关闭链接
-cJSON_Delete(pJson);
+if(pJson) cJSON_Delete(pJson);
 }

+ 246 - 0
generate_group_conf_acorp.c

@@ -0,0 +1,246 @@
+/*
+============================================================================
+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 5120
+#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 KEYVALLEN 100
+#define VERSION "V1.0.1"
+
+#define QUERY_PAGING_GROUP_SQL "select id,name,exten,paging_mode 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 * 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) {
+
+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+");
+
+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()\
+);
+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')",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]);
+        //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");
+    }
+
+    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})\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),Wait(0.5)\n\
+same => n,Set(__CONFNO=${EXTEN})\n\
+same => n,MSet(startT=${STRFTIME(${EPOCH},,%%s)},__CALLEE=${DESTS},__calltype=paging)\n\
+same => n,MSet(CONFBRIDGE(user,quiet)=yes,CONFBRIDGE(user,marked)=yes)\n\
+same => n,AGI(joinmeetme.agi,${CONFNO},${SRCEXTEN},${DESTS})\n\
+same => n,ConfBridge(${CONFNO})\n\
+same => n,Hangup\n\
+exten => h,1,Goto(checkmeetme,s,1)\
+\n\n", \
+g_row[2],\
+g_row[2],\
+g_row[1],\
+dest_tmp\
+);
+    }
+fclose(conf_paging_fp);
+fclose(conf_extens_fp);
+mysql_free_result(g_res); // 释放结果集
+mysql_free_result(d_res);
+mysql_close(g_conn); // 关闭链接
+}

+ 598 - 0
generate_pjsip_conf.c

@@ -0,0 +1,598 @@
+/*
+ = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = 
+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 <mysql/mysql.h>
+
+MYSQL *g_conn; // mysql 连接
+MYSQL_RES *d_res; // mysql device记录集
+MYSQL_ROW d_row; // 字符串数组,mysql 记录行
+MYSQL_RES *u_res; // mysql user记录集
+MYSQL_ROW u_row; // 字符串数组,mysql 记录行
+
+#define MAX_TRUNK_SIZE 256
+#define MAX_SIZE 2048
+#define MIDLE_SIZE 512
+#define MINI_SIZE 64
+#define PJSIP_DEV_AORS "/etc/asterisk/pjsip_dev_aors.conf"
+#define PJSIP_DEV_AUTHS "/etc/asterisk/pjsip_dev_auths.conf"
+#define PJSIP_DEV_EPS "/etc/asterisk/pjsip_dev_eps.conf"
+#define PJSIP_USR_AORS "/etc/asterisk/pjsip_usr_aors.conf"
+#define PJSIP_USR_AUTHS "/etc/asterisk/pjsip_usr_auths.conf"
+#define PJSIP_USR_EPS "/etc/asterisk/pjsip_usr_eps.conf"
+#define EXTEN_HINTS "/etc/asterisk/extensions_hints_exten.conf"
+#define KEYVALLEN 100
+#define VERSION "V1.0.1"
+
+#define QUERY_SIP_DEV_SQL "SELECT id,exten,cid_number,qualify_timeout,qualify_frequency,context,fullname,secret,videosupport,transport,default_expiration,ulaw,alaw,g722,opus,h264,recordin,recordout from t_pbx_users_extension order by exten asc"
+#define QUERY_SIP_USR_SQL "SELECT id,exten,cid_number,qualify_timeout,qualify_frequency,context,fullname,secret,videosupport,transport,default_expiration,ulaw,alaw,g722,opus,h264,dtlscertfile,dtlscafile,recordin,recordout from t_pbx_users_webrtc order by exten asc"
+
+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 * 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; // 返回成功
+}
+
+/*
+** DATABASE TABLE "t_pbx_users_extension"
+  `id` int(16) NOT NULL AUTO_INCREMENT,
+  `exten` varchar(32) NOT NULL,
+  `cid_number` varchar(32) NOT NULL DEFAULT '',
+  `qualify_timeout` smallint NOT NULL DEFAULT 60,
+  `qualify_frequency` smallint NOT NULL DEFAULT 120,
+  `call-limit` varchar(2) NOT NULL DEFAULT '5',
+  `host` varchar(16) NOT NULL DEFAULT 'dynamic',
+  `context` varchar(64) NOT NULL DEFAULT '',
+  `fullname` varchar(64) NOT NULL DEFAULT '',
+  `secret` varchar(128) NOT NULL DEFAULT '',
+  `outboundcid` varchar(32) NOT NULL DEFAULT '',
+  `email` varchar(64) NOT NULL DEFAULT '',
+  `nat` varchar(32) NOT NULL DEFAULT 'force_rport,comedia',
+  `directmedia` varchar(3) NOT NULL DEFAULT 'no',
+  `dtmfmode` varchar(8) NOT NULL DEFAULT 'rfc2833',
+  `videosupport` varchar(3) NOT NULL DEFAULT 'no',
+  `transport` varchar(11) NOT NULL DEFAULT 'udp',
+  `encryption` varchar(3) NOT NULL DEFAULT 'no',
+  `srtpcapable` varchar(3) NOT NULL DEFAULT 'no',
+  `default_expiration` smallint NOT NULL DEFAULT 600,
+  `ulaw` bool NOT NULL DEFAULT 1,
+  `alaw` bool NOT NULL DEFAULT 1,
+  `g722` bool NOT NULL DEFAULT 1,
+  `g729` bool NOT NULL DEFAULT 0,
+  `g726` bool NOT NULL DEFAULT 0,
+  `gsm` bool NOT NULL DEFAULT 0,
+  `speex` bool NOT NULL DEFAULT 0,
+  `h261` bool NOT NULL DEFAULT 0,
+  `h263` bool NOT NULL DEFAULT 0,
+  `h263p` bool NOT NULL DEFAULT 0,
+  `h264` bool NOT NULL DEFAULT 0,
+  `vp8` bool DEFAULT 0,
+  `opus` bool DEFAULT 0,
+  `recordin` varchar(32) NOT NULL DEFAULT '1',
+  `recordout` varchar(32) NOT NULL DEFAULT '1',
+*/
+
+int main(int argc, char **argv) {
+
+    char codecs[64];
+
+    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_SIP_DEV_SQL)){
+        print_mysql_error(NULL);
+        exit(1);
+    }
+
+    d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
+    FILE *conf_dev_aors = fopen(PJSIP_DEV_AORS, "w+");
+    FILE *conf_dev_auths = fopen(PJSIP_DEV_AUTHS, "w+");
+    FILE *conf_dev_eps = fopen(PJSIP_DEV_EPS, "w+");
+    FILE *conf_exten_hints = fopen(EXTEN_HINTS, "w+");
+
+    if (conf_dev_aors == NULL){
+        perror("Open aors conf file Error: ");
+        exit(1);
+    }
+
+    fprintf(conf_dev_aors, ";!\n\
+;! Automatically generated configuration file\n\
+;! Filename: pjsip_dev_aors.conf (/etc/asterisk/pjsip_dev_aors.conf)\n\
+;! Generator: Generator PJSIP AORS\n\
+;! Creation Date: %s\n\
+;!\n\n\
+",\
+mytime()\
+);
+
+    if (conf_dev_auths == NULL){
+        perror("Open auths conf file Error: ");
+        exit(1);
+    }
+
+    fprintf(conf_dev_auths, ";!\n\
+;! Automatically generated configuration file\n\
+;! Filename: pjsip_dev_auths.conf (/etc/asterisk/pjsip_dev_auths.conf)\n\
+;! Generator: Generator PJSIP AUTHS\n\
+;! Creation Date: %s\n\
+;!\n\n\
+",\
+mytime()\
+);
+
+if (conf_dev_eps == NULL){
+    perror("Open endpoint conf file Error: ");
+    exit(1);
+}
+
+    fprintf(conf_dev_eps, ";!\n\
+;! Automatically generated configuration file\n\
+;! Filename: pjsip_dev_auths.conf (/etc/asterisk/pjsip_dev_eps.conf)\n\
+;! Generator: Generator PJSIP ENDPOINTS\n\
+;! Creation Date: %s\n\
+;!\n\n\
+",\
+mytime()\
+);
+
+    fprintf(conf_exten_hints, ";!\n\
+;! Automatically generated configuration file\n\
+;! Filename: extensions_hints_exten.conf (/etc/asterisk/extensions_hints_exten.conf)\n\
+;! Generator: Generator Extension Hints\n\
+;! Creation Date: %s\n\
+;!\n\n\
+[channelhints_exten]\n\
+",\
+mytime()\
+);
+
+    while ((d_row = mysql_fetch_row(d_res)))
+    { // 打印结果集
+        if (d_row[1] == NULL || d_row[6] == NULL || d_row[9] == NULL)
+        {
+            printf("some feild is empty!\n");
+            continue;
+        }
+        fprintf(conf_dev_aors, "\
+[%s]\n\
+type = aor\n\
+max_contacts = 1\n\
+remove_existing = yes\n\
+remove_unavailable = yes\n\
+default_expiration = %s\n\
+qualify_timeout = %s\n\
+qualify_frequency = %s\n\
+authenticate_qualify = no\n\
+support_path = yes\n\
+\n",\
+d_row[1],\
+d_row[10],\
+d_row[3],\
+d_row[4]\
+);
+        fprintf(conf_dev_auths, "\
+[%s]\n\
+type = auth\n\
+auth_type = userpass\n\
+password = %s\n\
+username = %s\n\
+\n",\
+d_row[1],\
+d_row[7],\
+d_row[1]\
+);
+        memset(codecs, 0, sizeof(codecs));
+        if(d_row[11])
+        {
+            strcpy(codecs,"ulaw");
+        }
+
+        if(d_row[12])
+        {
+            if(strlen(codecs))
+            {
+                strcat(codecs,",alaw");
+            }
+            else
+            {
+                strcpy(codecs,"alaw");
+            }
+        }
+
+        if(d_row[13])
+        {
+            if(strlen(codecs))
+            {
+                strcat(codecs,",g722");
+            }
+            else
+            {
+                strcpy(codecs,"g722");
+            }
+        }
+
+        if(d_row[14])
+        {
+            if(strlen(codecs))
+            {
+                strcat(codecs,",opus");
+            }
+            else
+            {
+                strcpy(codecs,"opus");
+            }
+        }
+
+        if(d_row[15])
+        {
+            if(strlen(codecs))
+            {
+                strcat(codecs,",h264");
+            }
+            else
+            {
+                strcpy(codecs,"h264");
+            }
+        }
+
+        fprintf(conf_dev_eps, "\
+[%s]\n\
+type = endpoint\n\
+transport = transport-%s\n\
+aors = %s\n\
+auth = %s\n\
+context = %s\n\
+allow = %s\n\
+direct_media = no\n\
+connected_line_method = update\n\
+direct_media_method = update\n\
+direct_media_glare_mitigation = incoming\n\
+disable_direct_media_on_nat = yes\n\
+dtmf_mode = rfc4733\n\
+force_rport = yes\n\
+ice_support = no\n\
+identify_by = username\n\
+moh_suggest = default\n\
+outbound_proxy = \n\
+rewrite_contact = no\n\
+rtp_symmetric = no\n\
+send_diversion = no\n\
+send_pai = no\n\
+send_rpid = no\n\
+callerid = %s <%s>\n\
+media_encryption = no\n\
+inband_progress = no\n\
+call_group = 1\n\
+pickup_group = 1\n\
+device_state_busy_at = 0\n\
+language = \n\
+allow_transfer = yes\n\
+allow_subscribe = yes\n\
+sdp_session = IP Audio Center\n\
+tos_audio = ef\n\
+tos_video = AF41\n\
+cos_audio = 5\n\
+cos_video = 4\n\
+media_use_received_transport = no\n\
+user_eq_phone = no\n\
+rtp_keepalive = 60\n\
+rtp_timeout = 60\n\
+rtp_timeout_hold = 300\n\
+;acl = remote_extensions_acl\n\
+rtcp_mux = no\n\
+codec_prefs_incoming_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
+codec_prefs_incoming_offer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
+codec_prefs_outgoing_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
+codec_prefs_outgoing_offer = prefer:pending, operation:union, keep:all, transcode:allow\n\
+\n",\
+d_row[1],\
+d_row[9],\
+d_row[1],\
+d_row[1],\
+d_row[5],\
+codecs,\
+d_row[6],\
+d_row[1]\
+);
+        fprintf(conf_exten_hints, "\
+exten => %s,hint,PJSIP/%s,CustomPresence:%s\n\
+\n",\
+d_row[1],\
+d_row[1],\
+d_row[1]\
+);
+        }
+
+if (executesql(QUERY_SIP_USR_SQL)){
+    print_mysql_error(NULL);
+    exit(1);
+}
+
+u_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
+FILE *conf_usr_aors = fopen(PJSIP_USR_AORS, "w+");
+FILE *conf_usr_auths = fopen(PJSIP_USR_AUTHS, "w+");
+FILE *conf_usr_eps = fopen(PJSIP_USR_EPS, "w+");
+
+if (conf_usr_aors == NULL){
+    perror("Open user aors conf file Error: ");
+    exit(1);
+}
+
+    fprintf(conf_usr_aors, ";!\n\
+;! Automatically generated configuration file\n\
+;! Filename: pjsip_usr_aors.conf (/etc/asterisk/pjsip_usr_aors.conf)\n\
+;! Generator: Generator PJSIP AORS\n\
+;! Creation Date: %s\n\
+;!\n\n\
+",\
+mytime()\
+);
+
+if (conf_usr_auths == NULL){
+    perror("Open user auths conf file Error: ");
+    exit(1);
+}
+
+    fprintf(conf_usr_auths, ";!\n\
+;! Automatically generated configuration file\n\
+;! Filename: pjsip_usr_auths.conf (/etc/asterisk/pjsip_usr_auths.conf)\n\
+;! Generator: Generator PJSIP AUTHS\n\
+;! Creation Date: %s\n\
+;!\n\n\
+",\
+mytime()\
+);
+
+if (conf_usr_eps == NULL){
+    perror("Open user endpoint conf file Error: ");
+    exit(1);
+}
+
+    fprintf(conf_usr_eps, ";!\n\
+;! Automatically generated configuration file\n\
+;! Filename: pjsip_usr_auths.conf (/etc/asterisk/pjsip_usr_eps.conf)\n\
+;! Generator: Generator PJSIP ENDPOINTS\n\
+;! Creation Date: %s\n\
+;!\n\n\
+",\
+mytime()\
+);
+
+    while ((u_row = mysql_fetch_row(u_res)))
+    { // 打印结果集
+        if (u_row[1] == NULL || u_row[6] == NULL || u_row[9] == NULL)
+        {
+            printf("some feild is empty!\n");
+            continue;
+        }
+        fprintf(conf_usr_aors, "\
+[%s]\n\
+type = aor\n\
+max_contacts = 1\n\
+remove_existing = yes\n\
+remove_unavailable = yes\n\
+default_expiration = %s\n\
+qualify_timeout = %s\n\
+qualify_frequency = %s\n\
+authenticate_qualify = no\n\
+support_path = yes\n\
+\n",\
+u_row[1],\
+u_row[10],\
+u_row[3],\
+u_row[4]\
+);
+        fprintf(conf_usr_auths, "\
+[%s]\n\
+type = auth\n\
+auth_type = userpass\n\
+password = %s\n\
+username = %s\n\
+\n",\
+u_row[1],\
+u_row[7],\
+u_row[1]\
+);
+        memset(codecs, 0, sizeof(codecs));
+        if(atoi(u_row[11]))
+        {
+            strcpy(codecs,"ulaw");
+        }
+
+        if(atoi(u_row[12]))
+        {
+            if(strlen(codecs))
+            {
+                strcat(codecs,",alaw");
+            }
+            else
+            {
+                strcpy(codecs,"alaw");
+            }
+        }
+
+        if(atoi(u_row[13]))
+        {
+            if(strlen(codecs))
+            {
+                strcat(codecs,",g722");
+            }
+            else
+            {
+                strcpy(codecs,"g722");
+            }
+        }
+
+        if(atoi(u_row[14]))
+        {
+            if(strlen(codecs))
+            {
+                strcat(codecs,",opus");
+            }
+            else
+            {
+                strcpy(codecs,"opus");
+            }
+        }
+
+        if(atoi(u_row[15]))
+        {
+            if(strlen(codecs))
+            {
+                strcat(codecs,",h264");
+            }
+            else
+            {
+                strcpy(codecs,"h264");
+            }
+        }
+
+        fprintf(conf_usr_eps, "\
+[%s]\n\
+type = endpoint\n\
+transport = transport-wss\n\
+aors = %s\n\
+auth = %s\n\
+context = %s\n\
+allow = %s\n\
+direct_media = no\n\
+connected_line_method = update\n\
+direct_media_method = update\n\
+direct_media_glare_mitigation = incoming\n\
+disable_direct_media_on_nat = yes\n\
+dtmf_mode = rfc4733\n\
+force_rport = no\n\
+webrtc = yes\n\
+identify_by = username\n\
+moh_suggest = default\n\
+outbound_proxy  = \n\
+rewrite_contact = no\n\
+rtp_symmetric = no\n\
+send_diversion = no\n\
+send_pai =  no \n\
+send_rpid =  no \n\
+callerid = %s <%s>\n\
+use_avpf = yes\n\
+inband_progress = no\n\
+call_group = 1\n\
+pickup_group = 1\n\
+device_state_busy_at = 1\n\
+language = en\n\
+allow_transfer = yes\n\
+allow_subscribe = yes\n\
+sdp_session = IP Audio Center\n\
+tos_audio = ef\n\
+tos_video = AF41\n\
+cos_audio = 5\n\
+cos_video = 4\n\
+dtls_cert_file = %s\n\
+dtls_ca_file = %s\n\
+user_eq_phone = no\n\
+rtp_keepalive = 60\n\
+rtp_timeout = 60\n\
+rtp_timeout_hold = 300\n\
+;acl = local_extensions_acl\n\
+rtcp_mux = yes\n\
+codec_prefs_incoming_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
+codec_prefs_incoming_offer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
+codec_prefs_outgoing_answer = prefer:pending, operation:intersect, keep:all, transcode:allow\n\
+codec_prefs_outgoing_offer = prefer:pending, operation:union, keep:all, transcode:allow\n\
+\n",\
+u_row[1],\
+u_row[1],\
+u_row[1],\
+u_row[5],\
+codecs,\
+u_row[6],\
+u_row[1],\
+u_row[16],\
+u_row[17]\
+);
+        fprintf(conf_exten_hints, "\
+exten => %s,hint,PJSIP/%s,CustomPresence:%s\n\
+\n",\
+u_row[1],\
+u_row[1],\
+u_row[1]\
+);
+        }
+
+fclose(conf_dev_aors);
+fclose(conf_dev_auths);
+fclose(conf_dev_eps);
+fclose(conf_usr_aors);
+fclose(conf_usr_auths);
+fclose(conf_usr_eps);
+fclose(conf_exten_hints);
+mysql_free_result(d_res); // 释放结果集
+mysql_free_result(u_res);
+mysql_close(g_conn); // 关闭链接
+}

File diff ditekan karena terlalu besar
+ 389 - 582
generate_trunk_conf.c


+ 200 - 161
generate_user_conf.c

@@ -20,22 +20,30 @@ Description : Generate paging info from mysql to paging conf file
 #include <mysql/mysql.h>
 
 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 *d_res; // mysql device记录集
-MYSQL_ROW d_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 MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
 #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 from t_paging_users"
+#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];
@@ -45,134 +53,6 @@ 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);
@@ -216,20 +96,16 @@ return 0; // 返回成功
 }
 
 int main(int argc, char **argv) {
-cJSON *pJson,*pSub,*dJson;
+cJSON *pJson = NULL,*pSub = NULL,*dJson = NULL;
 int iCount=0;
-char noanswer_dest[64];
-/*
+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));
 
-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"));
@@ -244,8 +120,11 @@ if (executesql(QUERY_PAGING_USER_SQL)){
     exit(1);
 }
 
-g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
+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){
@@ -263,6 +142,52 @@ if (conf_user_queue_fp == NULL){
 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);
@@ -282,16 +207,22 @@ persistentmembers = yes\n\
 mytime()\
 );
 
-while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
-    if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
+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;
     }
-    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]);
+    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\
@@ -318,8 +249,72 @@ queue-holdtime =\n\
 queue-minutes =\n\
 queue-thankyou =\n\
 musicclass = queuemusic\n\
-\n",q,g_row[2]);
-            dJson = cJSON_Parse(g_row[4]);
+\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直接使用服务器上的记录集
+            while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
+                if (g_row[0] == NULL){
+                    printf("some feild is empty!\n");
+                    continue;
+                }
+                
+                sprintf(dialrule, "include => phones-group-%s\n", g_row[0]);
+
+                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)
             {
@@ -342,29 +337,69 @@ musicclass = queuemusic\n\
                 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(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(g_row[2],"ringall") == 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 => SIP/%s\n",pSub->valuestring);
-                            fprintf(conf_user_queue_fp, "exten => %s,1,Macro(queue,Q%d,${EXTEN},%s)\n", pSub->valuestring,q,g_row[3]);
+                            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,Macro(queue,Q%d,%s,%s)\n", q, pSub->valuestring,g_row[3]);
+                    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 => SIP/%s,%d\n",pSub->valuestring,i);
-                            fprintf(conf_user_queue_fp, "exten => %s,1,Macro(queue,Q%d,${EXTEN},%s)\n", pSub->valuestring,q,g_row[3]);
+                            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,Macro(queue,Q%d,%s,%s)\n", q, pSub->valuestring,g_row[3]);
+                    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);
                 }
             }
@@ -373,9 +408,13 @@ musicclass = queuemusic\n\
 }
 fclose(conf_queues_fp);
 fclose(conf_user_queue_fp);
-mysql_free_result(g_res); // 释放结果集
-mysql_free_result(d_res);
+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); // 关闭链接
-cJSON_Delete(dJson);
-cJSON_Delete(pJson);
+if(dJson) cJSON_Delete(dJson);
+if(pJson) cJSON_Delete(pJson);
 }

+ 85 - 0
getaudio_time.c

@@ -0,0 +1,85 @@
+#include <stdio.h>
+#include <stdint.h>
+ 
+typedef struct {
+    char chunkId[4];
+    uint32_t chunkSize;
+    char format[4];
+    char fmt[4];
+    uint32_t fmt_size;
+    uint16_t audio_format;
+    uint16_t num_channels;
+    uint32_t sample_rate;
+    uint32_t byte_rate;
+    uint16_t block_align;
+    uint16_t bits_per_sample;
+    char data[4];
+    uint32_t data_size;
+} WavHeader;
+ 
+int main(int argc, char *argv[]) {
+    FILE *file;
+    WavHeader header;
+    uint32_t subchunk2Size;
+    uint32_t byteRate;
+    // uint32_t sampleRate;
+    // uint32_t numChannels;
+    // uint32_t bitsPerSample;
+    uint32_t dataSize;
+    double duration;
+ 
+    if (argc < 2) {
+        printf("Usage: %s <filename>\n", argv[0]);
+        return 1;
+    }
+ 
+    file = fopen(argv[1], "rb");
+    if (!file) {
+        perror("Error opening file");
+        return 1;
+    }
+ 
+    // 读取WAV文件头
+    fread(&header, sizeof(WavHeader), 1, file);
+ 
+    // 确保是RIFF WAVE CHUNK
+    if (header.chunkId[0] != 'R' || header.chunkId[1] != 'I' || header.chunkId[2] != 'F' || header.chunkId[3] != 'F'
+        || header.format[0] != 'W' || header.format[1] != 'A' || header.format[2] != 'V' || header.format[3] != 'E') {
+        // printf("Not a valid WAV file.\n");
+        printf("0");
+        fclose(file);
+        return 1;
+    }
+ 
+    // 跳过子块大小和格式信息
+    // fseek(file, 12, SEEK_CUR);
+ 
+    // 读取子块大小和字节率
+    // fread(&subchunk2Size, sizeof(uint32_t), 1, file);
+    // fread(&byteRate, sizeof(uint32_t), 1, file);
+ 
+    // 读取样本率和通道数
+    // fread(&sampleRate, sizeof(uint32_t), 1, file);
+    // fread(&numChannels, sizeof(uint32_t), 1, file);
+ 
+    // 读取样本位深度
+    // fread(&bitsPerSample, sizeof(uint32_t), 1, file);
+ 
+    // 计算数据大小
+    // dataSize = subchunk2Size;
+ 
+    // 计算时长(秒)
+    duration = (double)header.chunkSize / (header.bits_per_sample * header.num_channels * header.sample_rate / 8);
+ 
+    // printf("Channels: %hu \n", header.num_channels);
+    // printf("Sample Rate: %u \n", header.sample_rate);
+    // printf("Byte Rate: %hu \n", header.byte_rate);
+    // printf("Bits per Sample: %hu \n", header.bits_per_sample);
+    // printf("Data Size: %u \n", header.data_size);
+
+    // printf("Duration %.2f seconds", duration);
+    printf("%.2f", duration);
+ 
+    fclose(file);
+    return 0;
+}

TEMPAT SAMPAH
record_playtime