generate_context_conf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 <cjson/cJSON.h>
  18. #include <mysql/mysql.h>
  19. MYSQL *g_conn; // mysql 连接
  20. MYSQL_RES *g_res; // mysql intercom记录集
  21. MYSQL_ROW g_row; // 字符串数组,mysql 记录行
  22. MYSQL_RES *d_res; // mysql phone记录集
  23. MYSQL_ROW d_row; // 字符串数组,mysql 记录行
  24. #define MAX_TRUNK_SIZE 256
  25. #define MAX_SIZE 1024
  26. #define MIDLE_SIZE 512
  27. #define MINI_SIZE 64
  28. #define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
  29. #define EXTEN_CONTEXT_FILE "/etc/asterisk/extensions_context_custom.conf"
  30. #define EXTEN_USERS_CONTEXT_FILE "/etc/asterisk/extensions_users_context_custom.conf"
  31. #define EXTEN_USERS_GLOBAL_FILE "/etc/asterisk/extensions_users_global_custom.conf"
  32. #define KEYVALLEN 100
  33. #define VERSION "V1.0.1"
  34. #define QUERY_INTERCOM_SQL "select id,exten from t_paging_devices where type_id in('2','5')"
  35. #define QUERY_IPPHONE_SQL "select id,exten,user_id from t_paging_devices where type_id in('3','6')"
  36. char g_host_name[MINI_SIZE];
  37. char g_user_name[MINI_SIZE];
  38. char g_password[MINI_SIZE];
  39. char g_db_name[MINI_SIZE];
  40. const unsigned int g_db_port = 3306;
  41. char sql_tmp[MAX_SIZE];
  42. //读取配置文件函数----功能:删除左边空格
  43. char *l_trim(char *szOutput, const char *szInput)
  44. {
  45. assert(szInput != NULL);
  46. assert(szOutput != NULL);
  47. assert(szOutput != szInput);
  48. for (NULL; *szInput != '\0' && isspace(*szInput); ++szInput)
  49. {
  50. ;
  51. }
  52. return strcpy(szOutput, szInput);
  53. }
  54. // 删除右边的空格
  55. char *r_trim(char *szOutput, const char *szInput)
  56. {
  57. char *p = NULL;
  58. assert(szInput != NULL);
  59. assert(szOutput != NULL);
  60. assert(szOutput != szInput);
  61. strcpy(szOutput, szInput);
  62. for(p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  63. {
  64. ;
  65. }
  66. *(++p) = '\0';
  67. return szOutput;
  68. }
  69. // 删除两边的空格
  70. char *a_trim(char *szOutput, const char *szInput)
  71. {
  72. char *p = NULL;
  73. assert(szInput != NULL);
  74. assert(szOutput != NULL);
  75. l_trim(szOutput, szInput);
  76. for (p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  77. {
  78. ;
  79. }
  80. *(++p) = '\0';
  81. return szOutput;
  82. }
  83. //main函数接口 参数1:配置文件路径 参数2:配置文件的那一部分,如general 参数3:键名 参数4:键值
  84. int GetProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal )
  85. {
  86. char appname[32], keyname[32];
  87. char *buf, *c;
  88. char buf_i[KEYVALLEN], buf_o[KEYVALLEN];
  89. FILE *fp;
  90. int found = 0; /* 1 AppName 2 KeyName */
  91. if( (fp = fopen( profile, "r" )) == NULL )
  92. {
  93. printf( "openfile [%s] error [%s]\n", profile, strerror(errno) );
  94. return(-1);
  95. }
  96. fseek( fp, 0, SEEK_SET );
  97. memset( appname, 0, sizeof(appname) );
  98. sprintf( appname, "[%s]", AppName );
  99. while( !feof(fp) && fgets( buf_i, KEYVALLEN, fp ) != NULL )
  100. {
  101. l_trim(buf_o, buf_i);
  102. if( strlen(buf_o) <= 0 )
  103. continue;
  104. buf = NULL;
  105. buf = buf_o;
  106. if( found == 0 )
  107. {
  108. if( buf[0] != '[' )
  109. {
  110. continue;
  111. }
  112. else if ( strncmp(buf, appname, strlen(appname)) == 0 )
  113. {
  114. found = 1;
  115. continue;
  116. }
  117. }
  118. else if( found == 1 )
  119. {
  120. if( buf[0] == '#' )
  121. {
  122. continue;
  123. }
  124. else if ( buf[0] == '[' )
  125. {
  126. break;
  127. }
  128. else
  129. {
  130. if( (c = (char *)strchr(buf, '=')) == NULL )
  131. continue;
  132. memset( keyname, 0, sizeof(keyname) );
  133. sscanf( buf, "%[^=|^ |^\t]", keyname );
  134. if( strcmp(keyname, KeyName) == 0 )
  135. {
  136. sscanf( ++c, "%[^\n]", KeyVal );
  137. char *KeyVal_o = (char *)malloc(strlen(KeyVal) + 1);
  138. if(KeyVal_o != NULL)
  139. {
  140. memset(KeyVal_o, 0, sizeof(KeyVal_o));
  141. a_trim(KeyVal_o, KeyVal);
  142. if(KeyVal_o && strlen(KeyVal_o) > 0)
  143. strcpy(KeyVal, KeyVal_o);
  144. free(KeyVal_o);
  145. KeyVal_o = NULL;
  146. }
  147. found = 2;
  148. break;
  149. }
  150. else
  151. {
  152. continue;
  153. }
  154. }
  155. }
  156. }
  157. fclose( fp );
  158. if( found == 2 )
  159. return(0);
  160. else
  161. return(-1);
  162. }
  163. char * mytime(){
  164. time_t my_time;
  165. time(&my_time);
  166. char *time_string = ctime(&my_time);
  167. if (time_string[strlen(time_string) - 1] == '\n')
  168. {
  169. time_string[strlen(time_string) - 1] = '\0';
  170. }
  171. return time_string;
  172. }
  173. void print_mysql_error(const char *msg) { // 打印最后一次错误
  174. if (msg)
  175. printf("%s: %s\n", msg, mysql_error(g_conn));
  176. else
  177. puts(mysql_error(g_conn));
  178. }
  179. int executesql(const char * sql) {
  180. /*query the database according the sql*/
  181. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  182. return -1; // 表示失败
  183. return 0; // 成功执行
  184. }
  185. int init_mysql() { // 初始化连接
  186. // init the database connection
  187. g_conn = mysql_init(NULL);
  188. /* connect the database */
  189. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  190. return -1;
  191. // 是否连接已经可用
  192. if (executesql("set names utf8")) // 如果失败
  193. return -1;
  194. return 0; // 返回成功
  195. }
  196. int main(int argc, char **argv) {
  197. /*
  198. memset(g_host_name, 0, sizeof(g_host_name));
  199. memset(g_user_name, 0, sizeof(g_user_name));
  200. memset(g_password, 0, sizeof(g_password));
  201. memset(g_db_name, 0, sizeof(g_db_name));
  202. GetProfileString(MYSQL_CONNECT_CONF, "general", "dbserverip", g_host_name);
  203. GetProfileString(MYSQL_CONNECT_CONF, "general", "dbuser", g_user_name);
  204. GetProfileString(MYSQL_CONNECT_CONF, "general", "dbpasswd", g_password);
  205. GetProfileString(MYSQL_CONNECT_CONF, "general", "dbname", g_db_name);
  206. */
  207. strcpy(g_host_name,getenv("MYSQL"));
  208. strcpy(g_user_name,getenv("MYSQL_USER"));
  209. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  210. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  211. FILE *conf_fp = fopen(EXTEN_CONTEXT_FILE, "w+");
  212. FILE *conf_users_fp = fopen(EXTEN_USERS_CONTEXT_FILE, "w+");
  213. FILE *global_fp = fopen(EXTEN_USERS_GLOBAL_FILE, "w+");
  214. if (conf_fp == NULL){
  215. perror("Open context conf file Error: ");
  216. exit(1);
  217. }
  218. fprintf(conf_fp, ";!\n\
  219. ;! Automatically generated configuration file\n\
  220. ;! Filename: extensions_context_custom.conf (/etc/asterisk/extensions_context_custom.conf)\n\
  221. ;! Generator: Generator Context\n\
  222. ;! Creation Date: %s\n\
  223. ;!\n\n\
  224. ",\
  225. mytime()\
  226. );
  227. if (conf_users_fp == NULL){
  228. perror("Open context conf file Error: ");
  229. exit(1);
  230. }
  231. fprintf(conf_users_fp, ";!\n\
  232. ;! Automatically generated configuration file\n\
  233. ;! Filename: extensions_users_context_custom.conf (/etc/asterisk/extensions_users_context_custom.conf)\n\
  234. ;! Generator: Generator User Context\n\
  235. ;! Creation Date: %s\n\
  236. ;!\n\n\
  237. [DialRule_users]\n\
  238. ",\
  239. mytime()\
  240. );
  241. if (global_fp == NULL){
  242. perror("Open context conf file Error: ");
  243. exit(1);
  244. }
  245. fprintf(global_fp, ";!\n\
  246. ;! Automatically generated configuration file\n\
  247. ;! Filename: extensions_users_global_custom.conf (/etc/asterisk/extensions_users_global_custom.conf)\n\
  248. ;! Generator: Generator User Global\n\
  249. ;! Creation Date: %s\n\
  250. ;!\n\n\
  251. ",\
  252. mytime()\
  253. );
  254. if (init_mysql()){
  255. print_mysql_error(NULL);
  256. exit(1);
  257. }
  258. //将所有类型为对讲终端的设备赋予所在group的权限。
  259. if (executesql(QUERY_INTERCOM_SQL)){
  260. print_mysql_error(NULL);
  261. exit(1);
  262. }
  263. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  264. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  265. if (g_row[0] == NULL || g_row[1] == NULL){
  266. printf("some feild is empty!\n");
  267. continue;
  268. }
  269. int setout = 1;
  270. fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
  271. fprintf(conf_fp, "include => cdr-action\n");
  272. fprintf(conf_fp, "include => call-trigger\n");
  273. memset(sql_tmp,0,sizeof(sql_tmp));
  274. //获取对讲终端所在队列的号码
  275. 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]);
  276. if (executesql(sql_tmp)){
  277. print_mysql_error(NULL);
  278. exit(1);
  279. }
  280. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  281. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  282. if (d_row[0] == NULL){
  283. printf("some feild is empty!\n");
  284. continue;
  285. }
  286. fprintf(conf_fp, "include => extens-group-%s\n", d_row[0]);
  287. fprintf(conf_fp, "include => phones-group-%s\n", d_row[0]);
  288. }
  289. memset(sql_tmp,0,sizeof(sql_tmp));
  290. //获取对讲终端所在队列的管理员ID
  291. 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]);
  292. if (executesql(sql_tmp)){
  293. print_mysql_error(NULL);
  294. exit(1);
  295. }
  296. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  297. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  298. if (d_row[0] == NULL){
  299. printf("some feild is empty!\n");
  300. continue;
  301. }
  302. int q = 100000 + atoi(d_row[0]);
  303. fprintf(conf_fp, "include => manager-queue-%d\n", q);
  304. }
  305. memset(sql_tmp,0,sizeof(sql_tmp));
  306. sprintf(sql_tmp,"select exten from t_paging_groups JOIN t_paging_userGroups on\
  307. t_paging_groups.id = t_paging_userGroups.GroupId where t_paging_userGroups.UserId\
  308. in(select UserId from t_paging_deviceGroups JOIN t_paging_userGroups on\
  309. t_paging_deviceGroups.GroupId = t_paging_userGroups.GroupId where DeviceId = %s) group by exten",g_row[0]);
  310. if (executesql(sql_tmp)){
  311. print_mysql_error(NULL);
  312. exit(1);
  313. }
  314. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  315. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  316. if (d_row[0] == NULL){
  317. printf("some feild is empty!\n");
  318. continue;
  319. }
  320. fprintf(conf_fp, "include => paging-group-%s\n", d_row[0]);
  321. }
  322. }
  323. //IP话机根据所绑定的用户赋予所在group的权限。
  324. if (executesql(QUERY_IPPHONE_SQL)){
  325. print_mysql_error(NULL);
  326. exit(1);
  327. }
  328. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  329. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  330. if (g_row[0] == NULL || g_row[1] == NULL){
  331. printf("some feild is empty!\n");
  332. continue;
  333. }
  334. int setout = 1;
  335. fprintf(conf_fp, "[DialRule_%s]\n",g_row[1]);
  336. fprintf(conf_fp, "include => cdr-action\n");
  337. fprintf(conf_fp, "include => featurecodes\n");
  338. //获取对讲终端所在队列的号码
  339. memset(sql_tmp,0,sizeof(sql_tmp));
  340. 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]);
  341. if (executesql(sql_tmp)){
  342. print_mysql_error(NULL);
  343. exit(1);
  344. }
  345. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  346. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  347. if (d_row[0] == NULL){
  348. printf("some feild is empty!\n");
  349. continue;
  350. }
  351. fprintf(conf_fp, "include => phones-group-%s\n", d_row[0]);
  352. fprintf(conf_fp, "include => paging-group-%s\n", d_row[0]);
  353. }
  354. if(g_row[2]){
  355. memset(sql_tmp,0,sizeof(sql_tmp));
  356. sprintf(sql_tmp,"select t_paging_users.id,t_paging_users.level from t_paging_users join t_paging_devices on t_paging_users.id = t_paging_devices.user_id\
  357. where t_paging_devices.exten=%s",g_row[1]); //查询分机号所属的用户ID和等级
  358. if (executesql(sql_tmp)){
  359. print_mysql_error(NULL);
  360. exit(1);
  361. }
  362. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  363. while((d_row=mysql_fetch_row(d_res))){
  364. if (d_row[0] == NULL || d_row[1] == NULL){
  365. printf("some feild is empty!\n");
  366. continue;
  367. }
  368. fprintf(conf_fp, "include => DialRule_users\n");
  369. fprintf(conf_users_fp, "exten => %s,1,Macro(stdexten,%s,SIP/%s)\n",g_row[1],g_row[1],g_row[1]);
  370. fprintf(global_fp, "USER_ID_%s = %s\n",g_row[1],d_row[0]);
  371. fprintf(global_fp, "USER_LEVEL_%s = %s\n",g_row[1],d_row[1]);
  372. }
  373. memset(sql_tmp,0,sizeof(sql_tmp));
  374. sprintf(sql_tmp,"select exten,t_paging_userServices.tPagingServiceId as service_id from t_paging_groups\
  375. JOIN t_paging_userGroups on t_paging_groups.id = t_paging_userGroups.GroupId\
  376. JOIN t_paging_userServices on t_paging_userServices.UserId = t_paging_userGroups.UserId\
  377. where t_paging_userServices.tPagingServiceId in(1,4,7,9) and t_paging_userGroups.UserId\
  378. in(select t_paging_users.id from t_paging_users join t_paging_devices on t_paging_users.id = t_paging_devices.user_id\
  379. where t_paging_devices.exten=%s) order by t_paging_userServices.tPagingServiceId",g_row[1]); //查询分机所属管理员的权限以及所管理的组
  380. if (executesql(sql_tmp)){
  381. print_mysql_error(NULL);
  382. exit(1);
  383. }
  384. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  385. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  386. if (d_row[0] == NULL || d_row[1] == NULL){
  387. printf("some feild is empty!\n");
  388. continue;
  389. }
  390. int id = atoi(d_row[1]);
  391. switch(id){
  392. case 1:
  393. fprintf(conf_fp, "include => paging-group-%s\n", d_row[0]);
  394. break;
  395. case 4:
  396. fprintf(conf_fp, "include => extens-group-%s\n", d_row[0]);
  397. break;
  398. case 7:
  399. fprintf(conf_fp, "include => call-trigger\n");
  400. break;
  401. case 9:
  402. if(setout){
  403. fprintf(conf_fp, "include => CallingRule_OutCall\n");
  404. setout = 0;
  405. }
  406. break;
  407. }
  408. }
  409. }else{
  410. memset(sql_tmp,0,sizeof(sql_tmp));
  411. 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]);
  412. if (executesql(sql_tmp)){
  413. print_mysql_error(NULL);
  414. exit(1);
  415. }
  416. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  417. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  418. if (d_row[0] == NULL){
  419. printf("some feild is empty!\n");
  420. continue;
  421. }
  422. int q = 100000 + atoi(d_row[0]);
  423. fprintf(conf_fp, "include => manager-queue-%d\n", q);
  424. }
  425. }
  426. }
  427. fclose(conf_fp);
  428. fclose(conf_users_fp);
  429. fclose(global_fp);
  430. mysql_free_result(g_res); // 释放结果集
  431. mysql_free_result(d_res); // 释放结果集
  432. mysql_close(g_conn); // 关闭链接
  433. }