generate_context_conf.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. ============================================================================
  3. Name : generate_context_conf.sh
  4. Author : ssc
  5. Version : v1.0
  6. Copyright : ZYCOO copyright
  7. Description : Generate context info from mysql to context 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 <mysql/mysql.h>
  18. MYSQL *g_conn; // mysql 连接
  19. MYSQL_RES *g_res; // mysql intercom记录集
  20. MYSQL_ROW g_row; // 字符串数组,mysql 记录行
  21. MYSQL_RES *d_res; // mysql phone记录集
  22. MYSQL_ROW d_row; // 字符串数组,mysql 记录行
  23. #define MAX_TRUNK_SIZE 256
  24. #define MAX_SIZE 1024
  25. #define MIDLE_SIZE 512
  26. #define MINI_SIZE 64
  27. #define EXTEN_CONTEXT_FILE "/etc/asterisk/extensions_context_custom.conf"
  28. #define KEYVALLEN 100
  29. #define VERSION "V1.0.1"
  30. #define QUERY_INTERCOM_SQL "select id,exten from t_paging_devices where type_id in('2','5')"
  31. #define QUERY_IPPHONE_SQL "select id,exten,user_id from t_paging_devices where type_id in('3','6')"
  32. char g_host_name[MINI_SIZE];
  33. char g_user_name[MINI_SIZE];
  34. char g_password[MINI_SIZE];
  35. char g_db_name[MINI_SIZE];
  36. const unsigned int g_db_port = 3306;
  37. char sql_tmp[MAX_SIZE];
  38. char * mytime(){
  39. time_t my_time;
  40. time(&my_time);
  41. char *time_string = ctime(&my_time);
  42. if (time_string[strlen(time_string) - 1] == '\n')
  43. {
  44. time_string[strlen(time_string) - 1] = '\0';
  45. }
  46. return time_string;
  47. }
  48. void print_mysql_error(const char *msg) { // 打印最后一次错误
  49. if (msg)
  50. printf("%s: %s\n", msg, mysql_error(g_conn));
  51. else
  52. puts(mysql_error(g_conn));
  53. }
  54. int executesql(const char * sql) {
  55. /*query the database according the sql*/
  56. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  57. return -1; // 表示失败
  58. return 0; // 成功执行
  59. }
  60. int init_mysql() { // 初始化连接
  61. // init the database connection
  62. g_conn = mysql_init(NULL);
  63. /* connect the database */
  64. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  65. return -1;
  66. // 是否连接已经可用
  67. if (executesql("set names utf8")) // 如果失败
  68. return -1;
  69. return 0; // 返回成功
  70. }
  71. int main(int argc, char **argv) {
  72. memset(g_host_name, 0, sizeof(g_host_name));
  73. memset(g_user_name, 0, sizeof(g_user_name));
  74. memset(g_password, 0, sizeof(g_password));
  75. memset(g_db_name, 0, sizeof(g_db_name));
  76. strcpy(g_host_name,getenv("MYSQL"));
  77. strcpy(g_user_name,getenv("MYSQL_USER"));
  78. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  79. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  80. FILE *conf_fp = fopen(EXTEN_CONTEXT_FILE, "w+");
  81. if (conf_fp == NULL){
  82. perror("Open context conf file Error: ");
  83. exit(1);
  84. }
  85. fprintf(conf_fp, ";!\n\
  86. ;! Automatically generated configuration file\n\
  87. ;! Filename: extensions_context_custom.conf (/etc/asterisk/extensions_context_custom.conf)\n\
  88. ;! Generator: Generator Context\n\
  89. ;! Creation Date: %s\n\
  90. ;!\n\n\
  91. ",\
  92. mytime()\
  93. );
  94. if (init_mysql()){
  95. print_mysql_error(NULL);
  96. exit(1);
  97. }
  98. //将所有类型为对讲终端的设备赋予所在group的权限。
  99. if (executesql(QUERY_INTERCOM_SQL)){
  100. print_mysql_error(NULL);
  101. exit(1);
  102. }
  103. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  104. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  105. if (g_row[0] == NULL || g_row[1] == NULL){
  106. printf("some feild is empty!\n");
  107. continue;
  108. }
  109. fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
  110. fprintf(conf_fp, "include => cdr-action\n");
  111. fprintf(conf_fp, "include => call-trigger\n");
  112. memset(sql_tmp,0,sizeof(sql_tmp));
  113. //获取对讲终端所在队列的号码
  114. 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]);
  115. if (executesql(sql_tmp)){
  116. print_mysql_error(NULL);
  117. exit(1);
  118. }
  119. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  120. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  121. if (d_row[0] == NULL){
  122. printf("some feild is empty!\n");
  123. continue;
  124. }
  125. fprintf(conf_fp, "include => extens-group-%s\n", d_row[0]);
  126. fprintf(conf_fp, "include => phones-group-%s\n", d_row[0]);
  127. }
  128. memset(sql_tmp,0,sizeof(sql_tmp));
  129. //获取对讲终端所在队列的管理员ID
  130. 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]);
  131. if (executesql(sql_tmp)){
  132. print_mysql_error(NULL);
  133. exit(1);
  134. }
  135. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  136. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  137. if (d_row[0] == NULL){
  138. printf("some feild is empty!\n");
  139. continue;
  140. }
  141. int q = 100000 + atoi(d_row[0]);
  142. fprintf(conf_fp, "include => manager-queue-%d\n", q);
  143. }
  144. memset(sql_tmp,0,sizeof(sql_tmp));
  145. sprintf(sql_tmp,"select exten from t_paging_groups JOIN t_paging_userGroups on\
  146. t_paging_groups.id = t_paging_userGroups.GroupId where t_paging_userGroups.UserId\
  147. in(select UserId from t_paging_deviceGroups JOIN t_paging_userGroups on\
  148. t_paging_deviceGroups.GroupId = t_paging_userGroups.GroupId where DeviceId = %s) group by exten",g_row[0]);
  149. if (executesql(sql_tmp)){
  150. print_mysql_error(NULL);
  151. exit(1);
  152. }
  153. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  154. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  155. if (d_row[0] == NULL){
  156. printf("some feild is empty!\n");
  157. continue;
  158. }
  159. fprintf(conf_fp, "include => paging-group-%s\n", d_row[0]);
  160. }
  161. }
  162. //IP话机根据所绑定的用户赋予所在group的权限。
  163. if (executesql(QUERY_IPPHONE_SQL)){
  164. print_mysql_error(NULL);
  165. exit(1);
  166. }
  167. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  168. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  169. if (g_row[0] == NULL || g_row[1] == NULL){
  170. printf("some feild is empty!\n");
  171. continue;
  172. }
  173. if(g_row[2] == NULL){
  174. fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
  175. fprintf(conf_fp, "include => cdr-action\n");
  176. fprintf(conf_fp, "include => featurecodes\n");
  177. //获取对讲终端所在队列的号码
  178. memset(sql_tmp,0,sizeof(sql_tmp));
  179. 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]);
  180. if (executesql(sql_tmp)){
  181. print_mysql_error(NULL);
  182. exit(1);
  183. }
  184. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  185. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  186. if (d_row[0] == NULL){
  187. printf("some feild is empty!\n");
  188. continue;
  189. }
  190. fprintf(conf_fp, "include => phones-group-%s\n", d_row[0]);
  191. fprintf(conf_fp, "include => paging-group-%s\n", d_row[0]);
  192. }
  193. memset(sql_tmp,0,sizeof(sql_tmp));
  194. 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]);
  195. if (executesql(sql_tmp)){
  196. print_mysql_error(NULL);
  197. exit(1);
  198. }
  199. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  200. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  201. if (d_row[0] == NULL){
  202. printf("some feild is empty!\n");
  203. continue;
  204. }
  205. int q = 100000 + atoi(d_row[0]);
  206. fprintf(conf_fp, "include => manager-queue-%d\n", q);
  207. }
  208. }
  209. }
  210. fclose(conf_fp);
  211. mysql_free_result(g_res); // 释放结果集
  212. mysql_free_result(d_res); // 释放结果集
  213. mysql_close(g_conn); // 关闭链接
  214. }