123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- /*
- ============================================================================
- Name : generate_context_conf.sh
- Author : ssc
- Version : v1.0
- Copyright : ZYCOO copyright
- Description : Generate context info from mysql to context 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 *g_res; // mysql intercom记录集
- MYSQL_ROW g_row; // 字符串数组,mysql 记录行
- MYSQL_RES *d_res; // mysql phone记录集
- MYSQL_ROW d_row; // 字符串数组,mysql 记录行
- #define MAX_TRUNK_SIZE 256
- #define MAX_SIZE 1024
- #define MIDLE_SIZE 512
- #define MINI_SIZE 64
- #define EXTEN_CONTEXT_FILE "/etc/asterisk/extensions_context_custom.conf"
- #define KEYVALLEN 100
- #define VERSION "V1.0.1"
- #define QUERY_INTERCOM_SQL "select id,exten from t_paging_devices where type_id in('2','5')"
- #define QUERY_IPPHONE_SQL "select id,exten,user_id from t_paging_devices where type_id in('3','6')"
- 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[MAX_SIZE];
- char * mytime(){
- time_t my_time;
- time(&my_time);
- char *time_string = ctime(&my_time);
- if (time_string[strlen(time_string) - 1] == '\n')
- {
- time_string[strlen(time_string) - 1] = '\0';
- }
- return time_string;
- }
- void print_mysql_error(const char *msg) { // 打印最后一次错误
- if (msg)
- printf("%s: %s\n", msg, mysql_error(g_conn));
- else
- puts(mysql_error(g_conn));
- }
- int executesql(const char * sql) {
- /*query the database according the sql*/
- if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
- return -1; // 表示失败
- return 0; // 成功执行
- }
- int init_mysql() { // 初始化连接
- // init the database connection
- g_conn = mysql_init(NULL);
- /* connect the database */
- if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
- return -1;
- // 是否连接已经可用
- if (executesql("set names utf8")) // 如果失败
- return -1;
- return 0; // 返回成功
- }
- int main(int argc, char **argv) {
- 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"));
- FILE *conf_fp = fopen(EXTEN_CONTEXT_FILE, "w+");
- if (conf_fp == NULL){
- perror("Open context conf file Error: ");
- exit(1);
- }
- fprintf(conf_fp, ";!\n\
- ;! Automatically generated configuration file\n\
- ;! Filename: extensions_context_custom.conf (/etc/asterisk/extensions_context_custom.conf)\n\
- ;! Generator: Generator Context\n\
- ;! Creation Date: %s\n\
- ;!\n\n\
- ",\
- mytime()\
- );
- if (init_mysql()){
- print_mysql_error(NULL);
- exit(1);
- }
- //将所有类型为对讲终端的设备赋予所在group的权限。
- if (executesql(QUERY_INTERCOM_SQL)){
- 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 || g_row[1] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
- fprintf(conf_fp, "include => cdr-action\n");
- fprintf(conf_fp, "include => call-trigger\n");
- memset(sql_tmp,0,sizeof(sql_tmp));
- //获取对讲终端所在队列的号码
- sprintf(sql_tmp,"select exten from t_paging_deviceGroups JOIN t_paging_groups on t_paging_groups.id = t_paging_deviceGroups.GroupId where DeviceId = %s",g_row[0]);
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- 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 => extens-group-%s\n", d_row[0]);
- fprintf(conf_fp, "include => phones-group-%s\n", d_row[0]);
- }
- memset(sql_tmp,0,sizeof(sql_tmp));
- //获取对讲终端所在队列的管理员ID
- sprintf(sql_tmp,"select UserId from t_paging_deviceGroups JOIN t_paging_userGroups on t_paging_deviceGroups.GroupId = t_paging_userGroups.GroupId where DeviceId = %s group by UserId",g_row[0]);
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- exit(1);
- }
- d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
- while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
- if (d_row[0] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- int q = 100000 + atoi(d_row[0]);
- fprintf(conf_fp, "include => manager-queue-%d\n", q);
- }
- memset(sql_tmp,0,sizeof(sql_tmp));
- sprintf(sql_tmp,"select exten from t_paging_groups JOIN t_paging_userGroups on\
- t_paging_groups.id = t_paging_userGroups.GroupId where t_paging_userGroups.UserId\
- in(select UserId from t_paging_deviceGroups JOIN t_paging_userGroups on\
- t_paging_deviceGroups.GroupId = t_paging_userGroups.GroupId where DeviceId = %s) group by exten",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 => paging-group-%s\n", d_row[0]);
- }
- }
- //IP话机根据所绑定的用户赋予所在group的权限。
- if (executesql(QUERY_IPPHONE_SQL)){
- 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 || g_row[1] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- 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 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]);
- }
-
- memset(sql_tmp,0,sizeof(sql_tmp));
- sprintf(sql_tmp,"select UserId from t_paging_deviceGroups JOIN t_paging_userGroups on t_paging_deviceGroups.GroupId = t_paging_userGroups.GroupId where DeviceId = %s group by UserId",g_row[0]);
- if (executesql(sql_tmp)){
- print_mysql_error(NULL);
- exit(1);
- }
- d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
- while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
- if (d_row[0] == NULL){
- printf("some feild is empty!\n");
- continue;
- }
- int q = 100000 + atoi(d_row[0]);
- fprintf(conf_fp, "include => manager-queue-%d\n", q);
- }
- }
-
- }
- fclose(conf_fp);
- mysql_free_result(g_res); // 释放结果集
- mysql_free_result(d_res); // 释放结果集
- mysql_close(g_conn); // 关闭链接
- }
|