generate_user_conf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 *u_res; // mysql user记录集
  21. MYSQL_ROW u_row; // 字符串数组,mysql 记录行
  22. MYSQL_RES *g_res; // mysql group记录集
  23. MYSQL_ROW g_row; // 字符串数组,mysql 记录行
  24. MYSQL_RES *wb_res; // mysql webrtc记录
  25. MYSQL_ROW wb_row; // 字符串数组,mysql 记录行
  26. MYSQL_RES *s_res; // mysql service记录集
  27. MYSQL_ROW s_row; // 字符串数组,mysql 记录行
  28. #define FALSE 0
  29. #define TRUE 1
  30. #define MAX_TRUNK_SIZE 256
  31. #define MAX_SIZE 2048
  32. #define MIDLE_SIZE 512
  33. #define MINI_SIZE 64
  34. #define EXTEN_USER_QUEUE_FILE "/etc/asterisk/extensions_users_queue_custom.conf"
  35. #define EXTEN_USERS_CONTEXT_FILE "/etc/asterisk/extensions_users_context_custom.conf"
  36. #define EXTEN_USER_CONTEXT_FILE "/etc/asterisk/extensions_user_context.conf"
  37. #define EXTEN_USERS_GLOBAL_FILE "/etc/asterisk/extensions_users_global_custom.conf"
  38. #define QUEUES_FILE "/etc/asterisk/queues.conf"
  39. #define KEYVALLEN 100
  40. #define VERSION "V1.0.1"
  41. #define QUERY_PAGING_USER_SQL "select id,phones,strategy,ring_duration,noanswer_dest,webexten_id,level from t_paging_users"
  42. char g_host_name[MINI_SIZE];
  43. char g_user_name[MINI_SIZE];
  44. char g_password[MINI_SIZE];
  45. char g_db_name[MINI_SIZE];
  46. const unsigned int g_db_port = 3306;
  47. char sql_tmp[MIDLE_SIZE];
  48. char exten_tmp[MAX_SIZE];
  49. char * mytime(){
  50. time_t my_time;
  51. time(&my_time);
  52. char *time_string = ctime(&my_time);
  53. if (time_string[strlen(time_string) - 1] == '\n')
  54. {
  55. time_string[strlen(time_string) - 1] = '\0';
  56. }
  57. return time_string;
  58. }
  59. void print_mysql_error(const char *msg) { // 打印最后一次错误
  60. if (msg)
  61. printf("%s: %s\n", msg, mysql_error(g_conn));
  62. else
  63. puts(mysql_error(g_conn));
  64. }
  65. int executesql(const char * sql) {
  66. /*query the database according the sql*/
  67. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  68. return -1; // 表示失败
  69. return 0; // 成功执行
  70. }
  71. int init_mysql() { // 初始化连接
  72. // init the database connection
  73. g_conn = mysql_init(NULL);
  74. /* connect the database */
  75. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  76. return -1;
  77. // 是否连接已经可用
  78. if (executesql("set names utf8")) // 如果失败
  79. return -1;
  80. return 0; // 返回成功
  81. }
  82. int main(int argc, char **argv) {
  83. cJSON *pJson = NULL,*pSub = NULL,*dJson = NULL;
  84. int iCount=0;
  85. char noanswer_dest[64], dialrule[4096], strtmp[64];
  86. int paging, intercom, task, outcall;
  87. memset(g_host_name, 0, sizeof(g_host_name));
  88. memset(g_user_name, 0, sizeof(g_user_name));
  89. memset(g_password, 0, sizeof(g_password));
  90. memset(g_db_name, 0, sizeof(g_db_name));
  91. strcpy(g_host_name,getenv("MYSQL"));
  92. strcpy(g_user_name,getenv("MYSQL_USER"));
  93. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  94. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  95. if (init_mysql()){
  96. print_mysql_error(NULL);
  97. exit(1);
  98. }
  99. if (executesql(QUERY_PAGING_USER_SQL)){
  100. print_mysql_error(NULL);
  101. exit(1);
  102. }
  103. u_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  104. FILE *conf_user_queue_fp = fopen(EXTEN_USER_QUEUE_FILE, "w+");
  105. FILE *conf_users_fp = fopen(EXTEN_USERS_CONTEXT_FILE, "w+");
  106. FILE *conf_user_context_fp = fopen(EXTEN_USER_CONTEXT_FILE, "w+");
  107. FILE *global_fp = fopen(EXTEN_USERS_GLOBAL_FILE, "w+");
  108. FILE *conf_queues_fp = fopen(QUEUES_FILE, "w+");
  109. if (conf_user_queue_fp == NULL){
  110. perror("Open paging conf file Error: ");
  111. exit(1);
  112. }
  113. fprintf(conf_user_queue_fp, ";!\n\
  114. ;! Automatically generated configuration file\n\
  115. ;! Filename: extensions_users_queue_custom.conf (/etc/asterisk/extensions_users_queue_custom.conf)\n\
  116. ;! Generator: Generator Paging\n\
  117. ;! Creation Date: %s\n\
  118. ;!\n\n\
  119. ",\
  120. mytime()\
  121. );
  122. if (conf_users_fp == NULL){
  123. perror("Open context conf file Error: ");
  124. exit(1);
  125. }
  126. fprintf(conf_users_fp, ";!\n\
  127. ;! Automatically generated configuration file\n\
  128. ;! Filename: extensions_users_context_custom.conf (/etc/asterisk/extensions_users_context_custom.conf)\n\
  129. ;! Generator: Generator Users Context\n\
  130. ;! Creation Date: %s\n\
  131. ;!\n\n\
  132. [DialRule_users]\n\
  133. ",\
  134. mytime()\
  135. );
  136. if (conf_user_context_fp == NULL){
  137. perror("Open user context conf file Error: ");
  138. exit(1);
  139. }
  140. fprintf(conf_user_context_fp, ";!\n\
  141. ;! Automatically generated configuration file\n\
  142. ;! Filename: extensions_user_context.conf (/etc/asterisk/extensions_user_context.conf)\n\
  143. ;! Generator: Generator User Context\n\
  144. ;! Creation Date: %s\n\
  145. ;!\n\n\
  146. ",\
  147. mytime()\
  148. );
  149. if (global_fp == NULL){
  150. perror("Open context conf file Error: ");
  151. exit(1);
  152. }
  153. fprintf(global_fp, ";!\n\
  154. ;! Automatically generated configuration file\n\
  155. ;! Filename: extensions_users_global_custom.conf (/etc/asterisk/extensions_users_global_custom.conf)\n\
  156. ;! Generator: Generator User Global\n\
  157. ;! Creation Date: %s\n\
  158. ;!\n\n\
  159. ",\
  160. mytime()\
  161. );
  162. if (conf_queues_fp == NULL){
  163. perror("Open paging conf file Error: ");
  164. exit(1);
  165. }
  166. fprintf(conf_queues_fp, ";!\n\
  167. ;! Automatically generated configuration file\n\
  168. ;! Filename: queues.conf (/etc/asterisk/queues.conf)\n\
  169. ;! Generator: Generator Paging\n\
  170. ;! Creation Date: %s\n\
  171. ;!\n\n\
  172. \n\
  173. [general]\n\
  174. persistentmembers = yes\n\
  175. \n\
  176. ",\
  177. mytime()\
  178. );
  179. while ((u_row=mysql_fetch_row(u_res))){ // 打印结果集
  180. if (u_row[0] == NULL || (u_row[1] == NULL && u_row[5] == NULL) || u_row[2] == NULL){
  181. printf("some feild is empty!\n");
  182. continue;
  183. }
  184. int id = atoi(u_row[0]);
  185. if(id == 1) continue;
  186. if(u_row[1] != NULL || u_row[5] != NULL){
  187. if(u_row[1])
  188. {
  189. pJson = cJSON_Parse(u_row[1]);
  190. iCount = cJSON_GetArraySize(pJson);
  191. }
  192. if(iCount > 0 || u_row[5] != NULL){
  193. int q = 100000 + id;
  194. fprintf(conf_user_queue_fp, "[manager-queue-%d]\n", q);
  195. fprintf(conf_queues_fp, "\
  196. [Q%d]\n\
  197. setinterfacevar = yes\n\
  198. setqueueentryvar = yes\n\
  199. strategy = %s\n\
  200. timeout = 30\n\
  201. wrapuptime = 0\n\
  202. autofill = yes\n\
  203. autopause = no\n\
  204. ringinuse = no\n\
  205. maxlen = 8\n\
  206. context = queue-custom\n\
  207. joinempty = no\n\
  208. leavewhenempty = paused,unavailable,invalid,unknown\n\
  209. periodic-announce-frequency = 0\n\
  210. reportholdtime = no\n\
  211. announce-frequency = 0\n\
  212. announce-holdtime = no\n\
  213. announce-position = no\n\
  214. queue-youarenext =\n\
  215. queue-callswaiting =\n\
  216. queue-holdtime =\n\
  217. queue-minutes =\n\
  218. queue-thankyou =\n\
  219. musicclass = queuemusic\n\
  220. \n",q,u_row[2]);
  221. //获取操作员权限
  222. paging = intercom = task = outcall = FALSE;
  223. memset(sql_tmp,0,sizeof(sql_tmp));
  224. sprintf(sql_tmp,"select tPagingServiceId from t_paging_userServices where UserId = %d",id);
  225. if (executesql(sql_tmp)){
  226. print_mysql_error(NULL);
  227. exit(1);
  228. }
  229. s_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  230. while ((s_row=mysql_fetch_row(s_res))){ // 打印结果集
  231. int id = atoi(s_row[0]);
  232. switch (id)
  233. {
  234. case 1:
  235. paging = TRUE;
  236. break;
  237. case 4:
  238. intercom = TRUE;
  239. break;
  240. case 7:
  241. task = TRUE;
  242. break;
  243. case 9:
  244. outcall = TRUE;
  245. break;
  246. }
  247. }
  248. //获取操作员所在队列的号码
  249. memset(sql_tmp,0,sizeof(sql_tmp));
  250. 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);
  251. if (executesql(sql_tmp)){
  252. print_mysql_error(NULL);
  253. exit(1);
  254. }
  255. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  256. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  257. if (g_row[0] == NULL){
  258. printf("some feild is empty!\n");
  259. continue;
  260. }
  261. sprintf(dialrule, "include => phones-group-%s\n", g_row[0]);
  262. if(paging)
  263. {
  264. sprintf(strtmp,"include => paging-group-%s\n", g_row[0]);
  265. strcat(dialrule, strtmp);
  266. }
  267. if(intercom)
  268. {
  269. sprintf(strtmp, "include => extens-group-%s\n", g_row[0]);
  270. strcat(dialrule, strtmp);
  271. }
  272. }
  273. strcat(dialrule, "include => DialRule_users\n");
  274. if(task)
  275. strcat(dialrule, "include => call-trigger\n");
  276. if(outcall)
  277. strcat(dialrule, "include => CallingRule_OutCall\n");
  278. dJson = cJSON_Parse(u_row[4]);
  279. memset(noanswer_dest,0,sizeof(noanswer_dest));
  280. if(dJson)
  281. {
  282. if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "hangup") == 0){
  283. strcpy(noanswer_dest,"Goto(hangup,s,1)");
  284. }
  285. else if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "extension") == 0){
  286. sprintf(noanswer_dest,"Goto(default,%s,1)",cJSON_GetObjectItem(dJson, "exten")->valuestring);
  287. }
  288. else if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "user") == 0){
  289. int id = 100000 + cJSON_GetObjectItem(dJson, "id")->valueint;
  290. sprintf(noanswer_dest,"Goto(manager-queue-%d,s,1)",id);
  291. }
  292. else if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "outcall") == 0){
  293. sprintf(noanswer_dest,"Goto(CallingRule_OutCall,%s,1)",cJSON_GetObjectItem(dJson, "exten")->valuestring);
  294. }
  295. }
  296. else
  297. {
  298. strcpy(noanswer_dest,"Goto(hangup,s,1)");
  299. }
  300. printf("parse success\n");
  301. if(u_row[5] != NULL)
  302. {
  303. memset(sql_tmp,0,sizeof(sql_tmp));
  304. sprintf(sql_tmp,"select exten from t_pbx_users_webrtc where id = %s",u_row[5]);
  305. if (executesql(sql_tmp)){
  306. print_mysql_error(NULL);
  307. exit(1);
  308. }
  309. wb_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  310. while ((wb_row=mysql_fetch_row(wb_res))){ // 打印结果集
  311. if (wb_row[0] == NULL){
  312. printf("some feild is empty!\n");
  313. continue;
  314. }
  315. fprintf(conf_queues_fp, "member => PJSIP/%s\n",wb_row[0]);
  316. fprintf(conf_users_fp, "exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",wb_row[0],wb_row[0],wb_row[0]);
  317. fprintf(global_fp, "USER_ID_%s = %d\n",wb_row[0],id);
  318. fprintf(global_fp, "USER_LEVEL_%s = %s\n",wb_row[0],u_row[6]);
  319. fprintf(conf_user_queue_fp, "exten => %s,1,Gosub(queue,s,1(Q%d,${EXTEN},%s))\n", wb_row[0],q,u_row[3]);
  320. fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest);
  321. fprintf(conf_user_context_fp, "[DialRule_%s]\n",wb_row[0]);
  322. fprintf(conf_user_context_fp, "include => cdr-action\n");
  323. fprintf(conf_user_context_fp, "include => featurecodes\n");
  324. fprintf(conf_user_context_fp, "%s\n", dialrule);
  325. }
  326. }
  327. if(iCount > 0)
  328. {
  329. if(strcmp(u_row[2],"ringall") == 0){
  330. for(int i = 0;i < iCount;i++){
  331. pSub = cJSON_GetArrayItem(pJson,i);
  332. if(pSub != NULL){
  333. fprintf(conf_queues_fp, "member => PJSIP/%s\n",pSub->valuestring);
  334. fprintf(conf_users_fp, "exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",pSub->valuestring,pSub->valuestring,pSub->valuestring);
  335. fprintf(global_fp, "USER_ID_%s = %d\n",pSub->valuestring,id);
  336. fprintf(global_fp, "USER_LEVEL_%s = %s\n",pSub->valuestring,u_row[6]);
  337. fprintf(conf_user_queue_fp, "exten => %s,1,Gosub(queue,s,1(Q%d,${EXTEN},%s))\n", pSub->valuestring,q,u_row[3]);
  338. fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest);
  339. fprintf(conf_user_context_fp, "[DialRule_%s]\n",pSub->valuestring);
  340. fprintf(conf_user_context_fp, "include => cdr-action\n");
  341. fprintf(conf_user_context_fp, "include => featurecodes\n");
  342. fprintf(conf_user_context_fp, "%s\n", dialrule);
  343. }
  344. }
  345. fprintf(conf_user_queue_fp, "exten => s,1,Gosub(queue,s,1(Q%d,%s,%s))\n", q, pSub->valuestring,u_row[3]);
  346. fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest);
  347. }else{
  348. for(int i = 0;i < iCount;i++){
  349. pSub = cJSON_GetArrayItem(pJson,i);
  350. if(pSub != NULL){
  351. fprintf(conf_queues_fp, "member => PJSIP/%s,%d\n",pSub->valuestring,i);
  352. fprintf(conf_users_fp, "exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",pSub->valuestring,pSub->valuestring,pSub->valuestring);
  353. fprintf(global_fp, "USER_ID_%s = %d\n",pSub->valuestring,id);
  354. fprintf(global_fp, "USER_LEVEL_%s = %s\n",pSub->valuestring,u_row[6]);
  355. fprintf(conf_user_queue_fp, "exten => %s,1,Gosub(queue,s,1(Q%d,${EXTEN},%s))\n", pSub->valuestring,q,u_row[3]);
  356. fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest);
  357. fprintf(conf_user_context_fp, "[DialRule_%s]\n",pSub->valuestring);
  358. fprintf(conf_user_context_fp, "include => cdr-action\n");
  359. fprintf(conf_user_context_fp, "include => featurecodes\n");
  360. fprintf(conf_user_context_fp, "%s\n", dialrule);
  361. }
  362. }
  363. fprintf(conf_user_queue_fp, "exten => s,1,Gosub(queue,s,1(Q%d,%s,%s))\n", q, pSub->valuestring,u_row[3]);
  364. fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest);
  365. }
  366. }
  367. }
  368. }
  369. }
  370. fclose(conf_queues_fp);
  371. fclose(conf_user_queue_fp);
  372. fclose(conf_users_fp);
  373. fclose(conf_user_context_fp);
  374. fclose(global_fp);
  375. mysql_free_result(u_res); // 释放结果集
  376. mysql_free_result(g_res);
  377. mysql_free_result(s_res);
  378. mysql_close(g_conn); // 关闭链接
  379. if(dJson) cJSON_Delete(dJson);
  380. if(pJson) cJSON_Delete(pJson);
  381. }