/* ============================================================================ Name : generate_paging_conf.sh Author : ssc Version : v1.0 Copyright : ZYCOO copyright Description : Generate paging info from mysql to paging conf file ============================================================================ */ #include #include #include #include #include #include #include #include #include MYSQL *g_conn; // mysql 连接 MYSQL_RES *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); // 关闭链接 }