generate_group_conf_acorp.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. ============================================================================
  3. Name : generate_paging_conf.sh
  4. Author : ssc
  5. Version : v1.0
  6. Copyright : ZYCOO copyright
  7. Description : Generate paging info from mysql to paging conf file
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <assert.h>
  15. #include <time.h>
  16. #include <ctype.h>
  17. #include <cjson/cJSON.h>
  18. #include <mysql/mysql.h>
  19. MYSQL *g_conn; // mysql 连接
  20. MYSQL_RES *g_res; // mysql group记录集
  21. MYSQL_ROW g_row; // 字符串数组,mysql 记录行
  22. MYSQL_RES *d_res; // mysql device记录集
  23. MYSQL_ROW d_row; // 字符串数组,mysql 记录行
  24. #define MAX_TRUNK_SIZE 256
  25. #define MAX_SIZE 5120
  26. #define MIDLE_SIZE 512
  27. #define MINI_SIZE 64
  28. #define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
  29. #define EXTEN_PAGING_FILE "/etc/asterisk/extensions_paging_custom.conf"
  30. #define EXTEN_EXTENS_FILE "/etc/asterisk/extensions_extens_custom.conf"
  31. #define EXTEN_IPPHONES_FILE "/etc/asterisk/extensions_phones_custom.conf"
  32. #define KEYVALLEN 100
  33. #define VERSION "V1.0.1"
  34. #define QUERY_PAGING_GROUP_SQL "select id,name,exten,paging_mode from t_paging_groups"
  35. char g_host_name[MINI_SIZE];
  36. char g_user_name[MINI_SIZE];
  37. char g_password[MINI_SIZE];
  38. char g_db_name[MINI_SIZE];
  39. const unsigned int g_db_port = 3306;
  40. char sql_tmp[MIDLE_SIZE];
  41. char exten_tmp[MAX_SIZE];
  42. char dest_tmp[MAX_SIZE];
  43. char page_option[MINI_SIZE];
  44. char * mytime(){
  45. time_t my_time;
  46. time(&my_time);
  47. char *time_string = ctime(&my_time);
  48. if (time_string[strlen(time_string) - 1] == '\n')
  49. {
  50. time_string[strlen(time_string) - 1] = '\0';
  51. }
  52. return time_string;
  53. }
  54. void print_mysql_error(const char *msg) { // 打印最后一次错误
  55. if (msg)
  56. printf("%s: %s\n", msg, mysql_error(g_conn));
  57. else
  58. puts(mysql_error(g_conn));
  59. }
  60. int executesql(const char * sql) {
  61. /*query the database according the sql*/
  62. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  63. return -1; // 表示失败
  64. return 0; // 成功执行
  65. }
  66. int init_mysql() { // 初始化连接
  67. // init the database connection
  68. g_conn = mysql_init(NULL);
  69. /* connect the database */
  70. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  71. return -1;
  72. // 是否连接已经可用
  73. if (executesql("set names utf8")) // 如果失败
  74. return -1;
  75. return 0; // 返回成功
  76. }
  77. int main(int argc, char **argv) {
  78. memset(g_host_name, 0, sizeof(g_host_name));
  79. memset(g_user_name, 0, sizeof(g_user_name));
  80. memset(g_password, 0, sizeof(g_password));
  81. memset(g_db_name, 0, sizeof(g_db_name));
  82. strcpy(g_host_name,getenv("MYSQL"));
  83. strcpy(g_user_name,getenv("MYSQL_USER"));
  84. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  85. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  86. if (init_mysql()){
  87. print_mysql_error(NULL);
  88. exit(1);
  89. }
  90. if (executesql(QUERY_PAGING_GROUP_SQL)){
  91. print_mysql_error(NULL);
  92. exit(1);
  93. }
  94. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  95. FILE *conf_paging_fp = fopen(EXTEN_PAGING_FILE, "w+");
  96. FILE *conf_extens_fp = fopen(EXTEN_EXTENS_FILE, "w+");
  97. FILE *conf_ipphones_fp = fopen(EXTEN_IPPHONES_FILE, "w+");
  98. if (conf_paging_fp == NULL){
  99. perror("Open paging conf file Error: ");
  100. exit(1);
  101. }
  102. fprintf(conf_paging_fp, ";!\n\
  103. ;! Automatically generated configuration file\n\
  104. ;! Filename: extensions_paging_custom.conf (/etc/asterisk/extensions_paging_custom.conf)\n\
  105. ;! Generator: Generator Paging\n\
  106. ;! Creation Date: %s\n\
  107. ;!\n\n\
  108. ",\
  109. mytime()\
  110. );
  111. if (conf_extens_fp == NULL){
  112. perror("Open extens conf file Error: ");
  113. exit(1);
  114. }
  115. fprintf(conf_extens_fp, ";!\n\
  116. ;! Automatically generated configuration file\n\
  117. ;! Filename: extensions_extens_custom.conf (/etc/asterisk/extensions_extens_custom.conf)\n\
  118. ;! Generator: Generator Extens\n\
  119. ;! Creation Date: %s\n\
  120. ;!\n\n\
  121. ",\
  122. mytime()\
  123. );
  124. if (conf_ipphones_fp == NULL){
  125. perror("Open ipphones conf file Error: ");
  126. exit(1);
  127. }
  128. fprintf(conf_ipphones_fp, ";!\n\
  129. ;! Automatically generated configuration file\n\
  130. ;! Filename: extensions_phones_custom.conf (/etc/asterisk/extensions_phones_custom.conf)\n\
  131. ;! Generator: Generator phones\n\
  132. ;! Creation Date: %s\n\
  133. ;!\n\n\
  134. ",\
  135. mytime()\
  136. );
  137. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  138. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL || g_row[3] == NULL){
  139. printf("some feild is empty!\n");
  140. continue;
  141. }
  142. fprintf(conf_extens_fp, "[extens-group-%s]\n",g_row[2]);
  143. fprintf(conf_ipphones_fp, "[phones-group-%s]\n",g_row[2]);
  144. memset(sql_tmp,0,sizeof(sql_tmp));
  145. 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\
  146. where GroupId = %s and t_paging_devices.type_id in('1','2','3','5')",g_row[0]);
  147. if (executesql(sql_tmp)){
  148. print_mysql_error(NULL);
  149. exit(1);
  150. }
  151. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  152. memset(exten_tmp,0,sizeof(exten_tmp));
  153. memset(dest_tmp,0,sizeof(dest_tmp));
  154. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  155. if (d_row[0] == NULL || d_row[1] == NULL){
  156. printf("some feild is empty!\n");
  157. continue;
  158. }
  159. //sprintf(exten_tmp, "%sSIP/%s&", exten_tmp, d_row[0]);
  160. //sprintf(dest_tmp, "%s%s|", dest_tmp, d_row[0]);
  161. strcat(exten_tmp,"PJSIP/");
  162. strcat(exten_tmp,d_row[0]);
  163. strcat(exten_tmp,"&");
  164. strcat(dest_tmp,d_row[0]);
  165. strcat(dest_tmp,"|");
  166. int id = atoi(d_row[1]);
  167. switch(id){
  168. case 1:
  169. fprintf(conf_extens_fp, "exten => %s,1,Gosub(page,s,1(%s,PJSIP/%s))\n",d_row[0],d_row[0],d_row[0]);
  170. break;
  171. case 2:
  172. fprintf(conf_extens_fp, "exten => %s,1,Gosub(intercom,s,1(%s,PJSIP/%s))\n",d_row[0],d_row[0],d_row[0]);
  173. break;
  174. case 3:
  175. case 7:
  176. fprintf(conf_ipphones_fp, "exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",d_row[0],d_row[0],d_row[0]);
  177. break;
  178. }
  179. }
  180. if(strlen(exten_tmp) > 0){
  181. exten_tmp[strlen(exten_tmp) - 1] = '\0';
  182. dest_tmp[strlen(dest_tmp) - 1] = '\0';
  183. }
  184. fprintf(conf_extens_fp, "\n");
  185. memset(page_option, 0, sizeof(page_option));
  186. if(strcmp(g_row[3],"duplex") == 0){
  187. strcat(page_option,"d");
  188. }
  189. fprintf(conf_paging_fp, "\
  190. [paging-group-%s]\n\
  191. exten => %s,1,NoOp(%s)\n\
  192. same => n,MSet(__SRCEXTEN=${CALLERID(num)},__DESTS=%s,DATE=${STRFTIME(${EPOCH},,%%Y%%m%%d)},__UUID=${UNIQUEID})\n\
  193. same => n,GotoIf($[\"foo${PAGING_RECORD}\" != \"fooyes\"]?unrc)\n\
  194. same => n,System(/bin/sh /etc/scripts/shell_scripts.sh mkrcdir paging ${DATE})\n\
  195. same => n,Set(FILENAME=paging/${DATE}/paging-${SRCEXTEN}-${UUID}.wav)\n\
  196. same => n,MixMonitor(${FILENAME},b)\n\
  197. same => n(unrc),Wait(0.5)\n\
  198. same => n,Set(__CONFNO=${EXTEN})\n\
  199. same => n,MSet(startT=${STRFTIME(${EPOCH},,%%s)},__CALLEE=${DESTS},__calltype=paging)\n\
  200. same => n,MSet(CONFBRIDGE(user,quiet)=yes,CONFBRIDGE(user,marked)=yes)\n\
  201. same => n,AGI(joinmeetme.agi,${CONFNO},${SRCEXTEN},${DESTS})\n\
  202. same => n,ConfBridge(${CONFNO})\n\
  203. same => n,Hangup\n\
  204. exten => h,1,Goto(checkmeetme,s,1)\
  205. \n\n", \
  206. g_row[2],\
  207. g_row[2],\
  208. g_row[1],\
  209. dest_tmp\
  210. );
  211. }
  212. fclose(conf_paging_fp);
  213. fclose(conf_extens_fp);
  214. mysql_free_result(g_res); // 释放结果集
  215. mysql_free_result(d_res);
  216. mysql_close(g_conn); // 关闭链接
  217. }