generate_group_conf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. MYSQL_RES *r_res; // mysql device记录集
  25. MYSQL_ROW r_row; // 字符串数组,mysql 记录行
  26. #define MAX_TRUNK_SIZE 256
  27. #define MAX_SIZE 20480
  28. #define MIDLE_SIZE 512
  29. #define MINI_SIZE 64
  30. #define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
  31. #define EXTEN_PAGING_FILE "/etc/asterisk/extensions_paging_custom.conf"
  32. #define EXTEN_EXTENS_FILE "/etc/asterisk/extensions_extens_custom.conf"
  33. #define EXTEN_IPPHONES_FILE "/etc/asterisk/extensions_phones_custom.conf"
  34. #define EXTEN_HINTS_FILE "/etc/asterisk/extensions_hints_custom.conf"
  35. #define KEYVALLEN 100
  36. #define VERSION "V1.0.1"
  37. #define QUERY_PAGING_GROUP_SQL "select id,name,exten,paging_mode,paging_volume from t_paging_groups"
  38. char g_host_name[MINI_SIZE];
  39. char g_user_name[MINI_SIZE];
  40. char g_password[MINI_SIZE];
  41. char g_db_name[MINI_SIZE];
  42. const unsigned int g_db_port = 3306;
  43. char sql_tmp[MIDLE_SIZE];
  44. char exten_tmp[MAX_SIZE];
  45. char dest_tmp[MAX_SIZE];
  46. char page_option[MINI_SIZE];
  47. char page_volume[MINI_SIZE];
  48. //读取配置文件函数----功能:删除左边空格
  49. char *l_trim(char *szOutput, const char *szInput)
  50. {
  51. assert(szInput != NULL);
  52. assert(szOutput != NULL);
  53. assert(szOutput != szInput);
  54. for (NULL; *szInput != '\0' && isspace(*szInput); ++szInput)
  55. {
  56. ;
  57. }
  58. return strcpy(szOutput, szInput);
  59. }
  60. // 删除右边的空格
  61. char *r_trim(char *szOutput, const char *szInput)
  62. {
  63. char *p = NULL;
  64. assert(szInput != NULL);
  65. assert(szOutput != NULL);
  66. assert(szOutput != szInput);
  67. strcpy(szOutput, szInput);
  68. for(p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  69. {
  70. ;
  71. }
  72. *(++p) = '\0';
  73. return szOutput;
  74. }
  75. // 删除两边的空格
  76. char *a_trim(char *szOutput, const char *szInput)
  77. {
  78. char *p = NULL;
  79. assert(szInput != NULL);
  80. assert(szOutput != NULL);
  81. l_trim(szOutput, szInput);
  82. for (p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  83. {
  84. ;
  85. }
  86. *(++p) = '\0';
  87. return szOutput;
  88. }
  89. //main函数接口 参数1:配置文件路径 参数2:配置文件的那一部分,如general 参数3:键名 参数4:键值
  90. int GetProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal )
  91. {
  92. char appname[32], keyname[32];
  93. char *buf, *c;
  94. char buf_i[KEYVALLEN], buf_o[KEYVALLEN];
  95. FILE *fp;
  96. int found = 0; /* 1 AppName 2 KeyName */
  97. if( (fp = fopen( profile, "r" )) == NULL )
  98. {
  99. printf( "openfile [%s] error [%s]\n", profile, strerror(errno) );
  100. return(-1);
  101. }
  102. fseek( fp, 0, SEEK_SET );
  103. memset( appname, 0, sizeof(appname) );
  104. sprintf( appname, "[%s]", AppName );
  105. while( !feof(fp) && fgets( buf_i, KEYVALLEN, fp ) != NULL )
  106. {
  107. l_trim(buf_o, buf_i);
  108. if( strlen(buf_o) <= 0 )
  109. continue;
  110. buf = NULL;
  111. buf = buf_o;
  112. if( found == 0 )
  113. {
  114. if( buf[0] != '[' )
  115. {
  116. continue;
  117. }
  118. else if ( strncmp(buf, appname, strlen(appname)) == 0 )
  119. {
  120. found = 1;
  121. continue;
  122. }
  123. }
  124. else if( found == 1 )
  125. {
  126. if( buf[0] == '#' )
  127. {
  128. continue;
  129. }
  130. else if ( buf[0] == '[' )
  131. {
  132. break;
  133. }
  134. else
  135. {
  136. if( (c = (char *)strchr(buf, '=')) == NULL )
  137. continue;
  138. memset( keyname, 0, sizeof(keyname) );
  139. sscanf( buf, "%[^=|^ |^\t]", keyname );
  140. if( strcmp(keyname, KeyName) == 0 )
  141. {
  142. sscanf( ++c, "%[^\n]", KeyVal );
  143. char *KeyVal_o = (char *)malloc(strlen(KeyVal) + 1);
  144. if(KeyVal_o != NULL)
  145. {
  146. memset(KeyVal_o, 0, sizeof(KeyVal_o));
  147. a_trim(KeyVal_o, KeyVal);
  148. if(KeyVal_o && strlen(KeyVal_o) > 0)
  149. strcpy(KeyVal, KeyVal_o);
  150. free(KeyVal_o);
  151. KeyVal_o = NULL;
  152. }
  153. found = 2;
  154. break;
  155. }
  156. else
  157. {
  158. continue;
  159. }
  160. }
  161. }
  162. }
  163. fclose( fp );
  164. if( found == 2 )
  165. return(0);
  166. else
  167. return(-1);
  168. }
  169. char * mytime(){
  170. time_t my_time;
  171. time(&my_time);
  172. char *time_string = ctime(&my_time);
  173. if (time_string[strlen(time_string) - 1] == '\n')
  174. {
  175. time_string[strlen(time_string) - 1] = '\0';
  176. }
  177. return time_string;
  178. }
  179. void print_mysql_error(const char *msg) { // 打印最后一次错误
  180. if (msg)
  181. printf("%s: %s\n", msg, mysql_error(g_conn));
  182. else
  183. puts(mysql_error(g_conn));
  184. }
  185. int executesql(const char * sql) {
  186. /*query the database according the sql*/
  187. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  188. return -1; // 表示失败
  189. return 0; // 成功执行
  190. }
  191. int init_mysql() { // 初始化连接
  192. // init the database connection
  193. g_conn = mysql_init(NULL);
  194. /* connect the database */
  195. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  196. return -1;
  197. // 是否连接已经可用
  198. if (executesql("set names utf8")) // 如果失败
  199. return -1;
  200. return 0; // 返回成功
  201. }
  202. int main(int argc, char **argv) {
  203. cJSON *pJson;
  204. memset(g_host_name, 0, sizeof(g_host_name));
  205. memset(g_user_name, 0, sizeof(g_user_name));
  206. memset(g_password, 0, sizeof(g_password));
  207. memset(g_db_name, 0, sizeof(g_db_name));
  208. strcpy(g_host_name,getenv("MYSQL"));
  209. strcpy(g_user_name,getenv("MYSQL_USER"));
  210. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  211. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  212. if (init_mysql()){
  213. print_mysql_error(NULL);
  214. exit(1);
  215. }
  216. if (executesql(QUERY_PAGING_GROUP_SQL)){
  217. print_mysql_error(NULL);
  218. exit(1);
  219. }
  220. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  221. FILE *conf_paging_fp = fopen(EXTEN_PAGING_FILE, "w+");
  222. FILE *conf_extens_fp = fopen(EXTEN_EXTENS_FILE, "w+");
  223. FILE *conf_ipphones_fp = fopen(EXTEN_IPPHONES_FILE, "w+");
  224. FILE *conf_hints_fp = fopen(EXTEN_HINTS_FILE, "w+");
  225. if (conf_paging_fp == NULL){
  226. perror("Open paging conf file Error: ");
  227. exit(1);
  228. }
  229. fprintf(conf_paging_fp, ";!\n\
  230. ;! Automatically generated configuration file\n\
  231. ;! Filename: extensions_paging_custom.conf (/etc/asterisk/extensions_paging_custom.conf)\n\
  232. ;! Generator: Generator Paging\n\
  233. ;! Creation Date: %s\n\
  234. ;!\n\n\
  235. ",\
  236. mytime()\
  237. );
  238. if (conf_extens_fp == NULL){
  239. perror("Open extens conf file Error: ");
  240. exit(1);
  241. }
  242. fprintf(conf_extens_fp, ";!\n\
  243. ;! Automatically generated configuration file\n\
  244. ;! Filename: extensions_extens_custom.conf (/etc/asterisk/extensions_extens_custom.conf)\n\
  245. ;! Generator: Generator Extens\n\
  246. ;! Creation Date: %s\n\
  247. ;!\n\n\
  248. ",\
  249. mytime()\
  250. );
  251. if (conf_ipphones_fp == NULL){
  252. perror("Open ipphones conf file Error: ");
  253. exit(1);
  254. }
  255. fprintf(conf_ipphones_fp, ";!\n\
  256. ;! Automatically generated configuration file\n\
  257. ;! Filename: extensions_phones_custom.conf (/etc/asterisk/extensions_phones_custom.conf)\n\
  258. ;! Generator: Generator phones\n\
  259. ;! Creation Date: %s\n\
  260. ;!\n\n\
  261. ",\
  262. mytime()\
  263. );
  264. if (conf_hints_fp == NULL){
  265. perror("Open hints conf file Error: ");
  266. exit(1);
  267. }
  268. fprintf(conf_hints_fp, ";!\n\
  269. ;! Automatically generated configuration file\n\
  270. ;! Filename: extensions_hints_custom.conf (/etc/asterisk/extensions_hints_custom.conf)\n\
  271. ;! Generator: Generator hints\n\
  272. ;! Creation Date: %s\n\
  273. ;!\n\n\
  274. [default]\n\
  275. ",\
  276. mytime()\
  277. );
  278. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  279. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL || g_row[3] == NULL){
  280. printf("some feild is empty!\n");
  281. continue;
  282. }
  283. fprintf(conf_extens_fp, "[extens-group-%s]\n",g_row[2]);
  284. fprintf(conf_ipphones_fp, "[phones-group-%s]\n",g_row[2]);
  285. memset(sql_tmp,0,sizeof(sql_tmp));
  286. 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\
  287. where GroupId = %s and t_paging_devices.type_id in('1','2','3','5') and t_paging_devices.user_id is NULL",g_row[0]);
  288. if (executesql(sql_tmp)){
  289. print_mysql_error(NULL);
  290. exit(1);
  291. }
  292. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  293. memset(exten_tmp,0,sizeof(exten_tmp));
  294. memset(dest_tmp,0,sizeof(dest_tmp));
  295. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  296. if (d_row[0] == NULL || d_row[1] == NULL){
  297. printf("some feild is empty!\n");
  298. continue;
  299. }
  300. if(strcmp(d_row[2],"yes") == 0){
  301. //sprintf(exten_tmp, "%sSIP/%s&", exten_tmp, d_row[0]);
  302. //sprintf(dest_tmp, "%s%s|", dest_tmp, d_row[0]);
  303. strcat(exten_tmp,"SIP/");
  304. strcat(exten_tmp,d_row[0]);
  305. strcat(exten_tmp,"&");
  306. strcat(dest_tmp,d_row[0]);
  307. strcat(dest_tmp,"|");
  308. }
  309. int id = atoi(d_row[1]);
  310. switch(id){
  311. case 1:
  312. fprintf(conf_extens_fp, "exten => %s,1,Macro(page,%s,SIP/%s)\n",d_row[0],d_row[0],d_row[0]);
  313. break;
  314. case 2:
  315. fprintf(conf_extens_fp, "exten => %s,1,Macro(intercom,%s,SIP/%s)\n",d_row[0],d_row[0],d_row[0]);
  316. break;
  317. case 3:
  318. case 7:
  319. fprintf(conf_ipphones_fp, "exten => %s,1,Macro(stdexten,%s,SIP/%s)\n",d_row[0],d_row[0],d_row[0]);
  320. break;
  321. }
  322. }
  323. if(strlen(exten_tmp) > 0){
  324. exten_tmp[strlen(exten_tmp) - 1] = '\0';
  325. dest_tmp[strlen(dest_tmp) - 1] = '\0';
  326. }
  327. fprintf(conf_extens_fp, "\n");
  328. memset(page_option, 0, sizeof(page_option));
  329. if(strcmp(g_row[3],"duplex") == 0){
  330. strcat(page_option,"d");
  331. }
  332. memset(page_volume, '\0', sizeof(page_volume));
  333. if(g_row[4] != NULL)
  334. {
  335. pJson = cJSON_Parse(g_row[4]);
  336. if(pJson && cJSON_HasObjectItem(pJson, "enableForce"))
  337. {
  338. if(cJSON_IsBool(cJSON_GetObjectItem(pJson, "enableForce")) && cJSON_IsTrue(cJSON_GetObjectItem(pJson, "enableForce")))
  339. {
  340. sprintf(page_volume,"\\;volume=%d",cJSON_GetObjectItem(pJson, "softvolume")->valueint);
  341. }
  342. }
  343. }
  344. fprintf(conf_paging_fp, "\
  345. [paging-group-%s]\n\
  346. exten => %s,1,NoOp(%s)\n\
  347. same => n,MSet(__SRCEXTEN=${CALLERID(num)},__DESTS=%s,DATE=${STRFTIME(${EPOCH},,%%Y%%m%%d)},__UUID=${UNIQUEID})\n\
  348. same => n,GotoIf($[\"foo${PAGING_AUTO}\" = \"foono\"]?manual)\n\
  349. same => n,MSet(ALERTINFO=Alert-Info: Ring Answer,CALLINFO=Call-Info: <uri>\\;answer-after=0%s)\n\
  350. same => n,SIPAddHeader(${ALERTINFO})\n\
  351. same => n,SIPAddHeader(${CALLINFO})\n\
  352. same => n(manual),GotoIf($[\"foo${PAGING_RECORD}\" != \"fooyes\"]?unrc)\n\
  353. same => n,System(/bin/sh /etc/scripts/shell_scripts.sh mkrcdir paging ${DATE})\n\
  354. same => n,Set(FILENAME=paging/${DATE}/paging-${SRCEXTEN}-${UUID}.wav)\n\
  355. same => n,MixMonitor(${FILENAME},b)\n\
  356. same => n(unrc),Macro(get-user-level,${SRCEXTEN},)\n\
  357. same => n,AGI(agi://${AGISERVERHOST}:${AGISERVERPORT},paging,${DESTS},${SESSION_LEVEL},${SESSION_USERID})\n\
  358. same => n,ExecIf(${ISNULL(${DESTS})}?Hangup())\n\
  359. same => n,ExecIf($['foo${enPaging_prompt_start}'='fooyes']?Set(STARTPROMPT=qA(${Paging_start_file})))\n\
  360. same => n,UserEvent(controlEvent,sessionlevel:${SESSION_LEVEL},sessionuserid:${SESSION_USERID},src:${SRCEXTEN},dest:${DESTS},uuid:${UUID},status:paging)\n\
  361. same => n,Wait(1)\n\
  362. same => n,GotoIf(${ISNULL(${DESTCHANS})}?default)\n\
  363. same => n,MSet(startT=${STRFTIME(${EPOCH},,%%s)},__CALLEE=${DESTS},__calltype=paging,DEVICE_STATE(Custom:${EXTEN})=INUSE,__GROUPID=${EXTEN})\n\
  364. same => n,Page(${DESTCHANS},%sb(paging-update-status^s^1)${STARTPROMPT})\n\
  365. same => n,Hangup\n\
  366. same => n(default),MSet(startT=${STRFTIME(${EPOCH},,%%s)},__CALLEE=${DESTS},__calltype=paging,DEVICE_STATE(Custom:${EXTEN})=INUSE,__GROUPID=${EXTEN})\n\
  367. same => n,Page(%s,%sb(paging-update-status^s^1)${STARTPROMPT})\n\
  368. same => n,Hangup\
  369. \n\n", \
  370. g_row[2],\
  371. g_row[2],\
  372. g_row[1],\
  373. dest_tmp,\
  374. page_volume,\
  375. page_option,\
  376. exten_tmp,\
  377. page_option\
  378. );
  379. fprintf(conf_hints_fp,"\
  380. exten => %s,hint,Custom:%s\n\
  381. ",\
  382. g_row[2],\
  383. g_row[2]\
  384. );
  385. }
  386. memset(sql_tmp,0,sizeof(sql_tmp));
  387. sprintf(sql_tmp,"select conditions from t_paging_tasks where type='numberrule'");
  388. if (executesql(sql_tmp)){
  389. print_mysql_error(NULL);
  390. exit(1);
  391. }
  392. r_res = mysql_store_result(g_conn);
  393. while(r_row=mysql_fetch_row(r_res))
  394. {
  395. pJson = cJSON_Parse(r_row[0]);
  396. if(cJSON_GetObjectItem(pJson, "extension"))
  397. {
  398. fprintf(conf_hints_fp,"\
  399. exten => *11%s,hint,Custom:*11%s\n\
  400. ",\
  401. cJSON_GetObjectItem(pJson, "extension")->valuestring,\
  402. cJSON_GetObjectItem(pJson, "extension")->valuestring\
  403. );
  404. }
  405. }
  406. fclose(conf_paging_fp);
  407. fclose(conf_extens_fp);
  408. fclose(conf_hints_fp);
  409. mysql_free_result(g_res); // 释放结果集
  410. mysql_free_result(d_res);
  411. mysql_free_result(r_res);
  412. mysql_close(g_conn); // 关闭链接
  413. cJSON_Delete(pJson);
  414. }