generate_extension_conf.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. ============================================================================
  3. Name : generate_trunk_conf.sh
  4. Author : ssc
  5. Version : v1.0
  6. Copyright : ZYCOO copyright
  7. Description : Generate trunk info from mysql to turnk 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 记录集
  21. MYSQL_ROW g_row; // 字符串数组,mysql 记录行
  22. #define MAX_SIZE 2048
  23. #define MIDLE_SIZE 512
  24. #define MINI_SIZE 64
  25. #define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
  26. #define EXTEN_IVR_FILE "/etc/asterisk/extensions_ivr_custom.conf"
  27. #define EXTEN_DIALRULE_FILE "/etc/asterisk/extensions_dialrule_custom.conf"
  28. #define EXTEN_INBOUND_FILE "/etc/asterisk/extensions_inbound_custom.conf"
  29. #define EXTEN_GLOBAL_FILE "/etc/asterisk/extensions_global_custom.conf"
  30. #define EXTEN_CALLTRIGGER_FILE "/etc/asterisk/extensions_call_trigger_custom.conf"
  31. #define EXTEN_FEATURECODES_FILE "/etc/asterisk/extensions_featurecodes_custom.conf"
  32. #define SIP_SETTINGS_FILE "/etc/asterisk/pjsip_transports.conf"
  33. #define RTP_SETTINGS_FILE "/etc/asterisk/rtp_settings.conf"
  34. #define KEYVALLEN 100
  35. #define VERSION "V1.0.1"
  36. #define QUERY_IVR_SQL "select name,exten,prompt,loops,timeout,language,dialplan,keys_action from t_pbx_ivr"
  37. #define QUERY_DIALRULE_SQL "select t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after from t_pbx_dialrule join t_pbx_users_voiptrunk on t_pbx_users_voiptrunk.id=t_pbx_dialrule.trunk_id"
  38. #define QUERY_GLOBAL_SQL "select * from t_global_config"
  39. char g_host_name[MINI_SIZE];
  40. char g_user_name[MINI_SIZE];
  41. char g_password[MINI_SIZE];
  42. char g_db_name[MINI_SIZE];
  43. const unsigned int g_db_port = 3306;
  44. char dialrule[MIDLE_SIZE];
  45. char ivrstr[MAX_SIZE];
  46. char inboundstr[MIDLE_SIZE];
  47. char prompt[MINI_SIZE];
  48. char * mytime(){
  49. time_t my_time;
  50. time(&my_time);
  51. char *time_string = ctime(&my_time);
  52. if (time_string[strlen(time_string) - 1] == '\n')
  53. {
  54. time_string[strlen(time_string) - 1] = '\0';
  55. }
  56. return time_string;
  57. }
  58. void print_mysql_error(const char *msg) { // 打印最后一次错误
  59. if (msg)
  60. printf("%s: %s\n", msg, mysql_error(g_conn));
  61. else
  62. puts(mysql_error(g_conn));
  63. }
  64. int executesql(const char * sql) {
  65. /*query the database according the sql*/
  66. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  67. return -1; // 表示失败
  68. return 0; // 成功执行
  69. }
  70. int init_mysql() { // 初始化连接
  71. // init the database connection
  72. g_conn = mysql_init(NULL);
  73. /* connect the database */
  74. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  75. return -1;
  76. // 是否连接已经可用
  77. if (executesql("set names utf8")) // 如果失败
  78. return -1;
  79. return 0; // 返回成功
  80. }
  81. int main(int argc, char **argv) {
  82. cJSON *pJson,*pSub;
  83. int iCount=0;
  84. char sip_udp_nat[2048], sip_tcp_nat[2048], sip_tls_nat[2048], localnet[1792];
  85. typedef struct keys_action {
  86. char key[MINI_SIZE];
  87. char type[MINI_SIZE];
  88. char exten[MINI_SIZE];
  89. } KeysObject;
  90. strcpy(g_host_name,getenv("MYSQL"));
  91. strcpy(g_user_name,getenv("MYSQL_USER"));
  92. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  93. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  94. if (init_mysql()){
  95. print_mysql_error(NULL);
  96. exit(1);
  97. }
  98. //读取自动话务员的数据,并写入配置文件
  99. if (executesql(QUERY_IVR_SQL)){
  100. print_mysql_error(NULL);
  101. exit(1);
  102. }
  103. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  104. FILE *conf_ivr_fp = fopen(EXTEN_IVR_FILE, "w+");
  105. if (conf_ivr_fp == NULL){
  106. perror("Open paging conf file Error: ");
  107. exit(1);
  108. }
  109. fprintf(conf_ivr_fp, ";!\n\
  110. ;! Automatically generated configuration file\n\
  111. ;! Filename: extensions_ivr_custom.conf (/etc/asterisk/extensions_ivr_custom.conf)\n\
  112. ;! Generator: Generator IVR\n\
  113. ;! Creation Date: %s\n\
  114. ;!\n\n\
  115. ",\
  116. mytime()\
  117. );
  118. //name:g_row[0],exten:g_row[1],prompt:g_row[2],loops:g_row[3],timeout:g_row[4],language:g_row[5],dialplan:g_row[6],keys_action
  119. //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
  120. //| id | name | exten | prompt | loops | timeout | language | dialplan | keys_action | createdAt | updatedAt |
  121. //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
  122. //| 1 | 上班时间 | 6500 | /etc/asterisk/sysconf/prompts/welcome | 1 | 3 | NULL | default | [{"key": "i", "type": "hangup", "exten": ""}, {"key": "t", "type": "hangup", "exten": ""}, {"key": "0", "type": "extension", "exten": "8001"}] | 2019-07-31 17:58:00 | 2019-08-05 01:30:48 |
  123. //| 2 | 下班时间 | 6501 | /etc/asterisk/sysconf/prompts/closed | 1 | 3 | NULL | default | [{"key": "i", "type": "hangup", "exten": ""}, {"key": "t", "type": "hangup", "exten": ""}] | 2019-07-31 17:58:00 | 2019-08-06 01:30:24 |
  124. //| 4 | test | 6505 | /etc/asterisk/sysconf/prompts/welcome | 1 | 3 | NULL | default | [{"key": "i", "type": "hangup", "exten": ""}, {"key": "t", "type": "hangup", "exten": ""}, {"key": "1", "type": "extension", "exten": "8500"}] | 2019-08-05 01:33:58 | 2019-08-05 13:02:10 |
  125. //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
  126. //3 rows in set (0.00 sec)
  127. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  128. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
  129. printf("some feild is empty!\n");
  130. continue;
  131. }
  132. fprintf(conf_ivr_fp,"\
  133. [voicemenu-custom-%s]\n\
  134. include => %s\n\
  135. exten => %s,1,NoOp(%s)\n",\
  136. g_row[1],\
  137. g_row[6],\
  138. g_row[1],\
  139. g_row[0]\
  140. );
  141. if(g_row[5] != NULL){
  142. fprintf(conf_ivr_fp,"same => n,Set(CHANNEL(language)=%s)\n",g_row[5]);
  143. }
  144. memset(prompt, 0, sizeof(prompt));
  145. strncpy(prompt,g_row[2],strlen(g_row[2])-4);
  146. fprintf(conf_ivr_fp,"\
  147. same => n,Set(COUNT=%s)\n\
  148. same => n(loop),Background(%s)\n",\
  149. g_row[3],\
  150. prompt
  151. );
  152. if(strcmp(g_row[4], "0") != 0 && g_row[4] != NULL){
  153. fprintf(conf_ivr_fp,"same => n,WaitExten(%s)\n",g_row[4]);
  154. }
  155. fprintf(conf_ivr_fp,"\
  156. same => n,Set(COUNT=$[${COUNT}-1])\n\
  157. same => n,GotoIf($[${COUNT} < 0]?:loop)\n\
  158. same => n,WaitExten(1)\n");
  159. KeysObject keysObject[MINI_SIZE];
  160. memset(keysObject, 0, sizeof(keysObject));
  161. if(g_row[7] != NULL){
  162. pJson = cJSON_Parse(g_row[7]);
  163. iCount = cJSON_GetArraySize(pJson);
  164. for(int i = 0;i < iCount;i++){
  165. pSub = cJSON_GetArrayItem(pJson,i);
  166. if(pSub != NULL){
  167. strcpy(keysObject[i].key,cJSON_GetObjectItem(pSub, "key")->valuestring);
  168. strcpy(keysObject[i].type,cJSON_GetObjectItem(pSub, "type")->valuestring);
  169. strcpy(keysObject[i].exten,cJSON_GetObjectItem(pSub, "exten")->valuestring);
  170. if(strcmp(keysObject[i].type, "hangup") == 0){
  171. fprintf(conf_ivr_fp,"exten => %s,1,Hangup()\n",keysObject[i].key);
  172. }
  173. else if(strcmp(keysObject[i].type, "extension") == 0){
  174. fprintf(conf_ivr_fp,"exten => %s,1,Goto(default,%s,1)\n",keysObject[i].key,keysObject[i].exten);
  175. }
  176. else if(strcmp(keysObject[i].type, "ivr") == 0){
  177. fprintf(conf_ivr_fp,"exten => %s,1,Goto(voicemenu-custom-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  178. }
  179. else if(strcmp(keysObject[i].type, "group") == 0){
  180. fprintf(conf_ivr_fp,"exten => %s,1,Goto(paging-group-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  181. }
  182. else if(strcmp(keysObject[i].type, "user") == 0){
  183. int id = 100000 + atoi(keysObject[i].exten);
  184. fprintf(conf_ivr_fp,"exten => %s,1,Goto(manager-queue-%d,s,1)\n",keysObject[i].key,id);
  185. }
  186. }
  187. }
  188. }
  189. fprintf(conf_ivr_fp,"\n\n");
  190. }
  191. /*
  192. [voicemenu-custom-%s]
  193. include = default
  194. exten = _IVR-X.,1,NoOp(%s)
  195. exten = _IVR-X.,n,NoOp(Default language)
  196. exten = _IVR-X.,n,Set(COUNT=3)
  197. exten = _IVR-X.,n(loop),Background(%s)
  198. exten = _IVR-X.,n,Set(COUNT=$[${COUNT}-1])
  199. exten = _IVR-X.,n,WaitExten(%s)
  200. exten = _IVR-X.,n,GotoIf($[${COUNT}>1]?6)
  201. exten = _IVR-X.,n,WaitExten(1)
  202. exten = t,1,Hangup
  203. */
  204. //读取拨号规则的数据,并写入配置文件
  205. if (executesql(QUERY_DIALRULE_SQL)){
  206. print_mysql_error(NULL);
  207. exit(1);
  208. }
  209. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  210. FILE *conf_dialrule_fp = fopen(EXTEN_DIALRULE_FILE, "w+");
  211. if (conf_dialrule_fp == NULL){
  212. perror("Open paging conf file Error: ");
  213. exit(1);
  214. }
  215. fprintf(conf_dialrule_fp, ";!\n\
  216. ;! Automatically generated configuration file\n\
  217. ;! Filename: extensions_dialrule_custom.conf (/etc/asterisk/extensions_dialrule_custom.conf)\n\
  218. ;! Generator: Generator DIALRULE\n\
  219. ;! Creation Date: %s\n\
  220. ;!\n\n\
  221. ",\
  222. mytime()\
  223. );
  224. fputs("[CallingRule_OutCall]\n",conf_dialrule_fp);
  225. //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
  226. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  227. if (g_row[0] == NULL || g_row[1] == NULL){
  228. printf("some feild is empty!\n");
  229. continue;
  230. }
  231. memset(dialrule,0,sizeof(dialrule));
  232. sprintf(dialrule, "exten => _%s,1,Gosub(trunkdial-failover,s,1(${EXTEN},${%s}",g_row[1], g_row[0]);
  233. if(g_row[3] != NULL){
  234. strcat(dialrule,g_row[3]);
  235. }
  236. strcat(dialrule,"${EXTEN:");
  237. if(g_row[2] != NULL){
  238. strcat(dialrule,g_row[2]);
  239. }
  240. strcat(dialrule,"}");
  241. if(g_row[4] != NULL){
  242. strcat(dialrule,g_row[4]);
  243. }
  244. strcat(dialrule,"))\n");
  245. fputs(dialrule,conf_dialrule_fp);
  246. }
  247. //读取全局配置数据,并写入配置文件
  248. if (executesql(QUERY_GLOBAL_SQL)){
  249. print_mysql_error(NULL);
  250. exit(1);
  251. }
  252. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  253. FILE *conf_inbound_fp = fopen(EXTEN_INBOUND_FILE, "w+");
  254. if (conf_inbound_fp == NULL){
  255. perror("Open inbound conf file Error: ");
  256. exit(1);
  257. }
  258. FILE *conf_global_fp = fopen(EXTEN_GLOBAL_FILE, "w+");
  259. if (conf_global_fp == NULL){
  260. perror("Open global conf file Error: ");
  261. exit(1);
  262. }
  263. FILE *conf_calltrigger_fp = fopen(EXTEN_CALLTRIGGER_FILE, "w+");
  264. if (conf_calltrigger_fp == NULL){
  265. perror("Open calltrigger conf file Error: ");
  266. exit(1);
  267. }
  268. FILE *conf_featurecodes_fp = fopen(EXTEN_FEATURECODES_FILE, "w+");
  269. if (conf_featurecodes_fp == NULL){
  270. perror("Open featurecodes conf file Error: ");
  271. exit(1);
  272. }
  273. FILE *conf_sipsetting_fp = fopen(SIP_SETTINGS_FILE, "w+");
  274. if (conf_sipsetting_fp == NULL){
  275. perror("Open sip settings conf file Error: ");
  276. exit(1);
  277. }
  278. FILE *conf_rtpsetting_fp = fopen(RTP_SETTINGS_FILE, "w+");
  279. if (conf_rtpsetting_fp == NULL){
  280. perror("Open rtp settings conf file Error: ");
  281. exit(1);
  282. }
  283. fprintf(conf_inbound_fp, ";!\n\
  284. ;! Automatically generated configuration file\n\
  285. ;! Filename: extensions_inbound_custom.conf (/etc/asterisk/extensions_inbound_custom.conf)\n\
  286. ;! Generator: Generator INBOUND\n\
  287. ;! Creation Date: %s\n\
  288. ;!\n\n\
  289. ",\
  290. mytime()\
  291. );
  292. fprintf(conf_global_fp, ";!\n\
  293. ;! Automatically generated configuration file\n\
  294. ;! Filename: extensions_global_custom.conf (/etc/asterisk/extensions_global_custom.conf)\n\
  295. ;! Generator: Generator GLOBAL\n\
  296. ;! Creation Date: %s\n\
  297. ;!\n\n\
  298. ",\
  299. mytime()\
  300. );
  301. fprintf(conf_global_fp, "\
  302. AGISERVERHOST = %s\n\
  303. AGISERVERPORT = %s\n\
  304. ",\
  305. getenv("BROADCAST_GATEWAY"),\
  306. getenv("AGI_SERVER_PORT")\
  307. );
  308. fprintf(conf_calltrigger_fp, ";!\n\
  309. ;! Automatically generated configuration file\n\
  310. ;! Filename: extensions_call_trigger_custom.conf (/etc/asterisk/extensions_call_trigger_custom.conf)\n\
  311. ;! Generator: Generator TRIGGER\n\
  312. ;! Creation Date: %s\n\
  313. ;!\n\n\
  314. ",\
  315. mytime()\
  316. );
  317. fprintf(conf_featurecodes_fp, ";!\n\
  318. ;! Automatically generated configuration file\n\
  319. ;! Filename: extensions_featurecodes_custom.conf (/etc/asterisk/extensions_featurecodes_custom.conf)\n\
  320. ;! Generator: Generator FEATURECODES\n\
  321. ;! Creation Date: %s\n\
  322. ;!\n\n\
  323. [featurecodes]\n",\
  324. mytime()\
  325. );
  326. fprintf(conf_sipsetting_fp, ";!\n\
  327. ;! Automatically generated configuration file\n\
  328. ;! Filename: pjsip_transports.conf (/etc/asterisk/pjsip_transports.conf)\n\
  329. ;! Generator: Generator SIP SETTINGS\n\
  330. ;! Creation Date: %s\n\
  331. ;!\n\n\
  332. \n",\
  333. mytime()\
  334. );
  335. fprintf(conf_rtpsetting_fp, ";!\n\
  336. ;! Automatically generated configuration file\n\
  337. ;! Filename: rtp_settings.conf (/etc/asterisk/rtp_settings.conf)\n\
  338. ;! Generator: Generator RTP SETTINGS\n\
  339. ;! Creation Date: %s\n\
  340. ;!\n\n\
  341. \n",\
  342. mytime()\
  343. );
  344. //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
  345. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  346. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
  347. printf("some feild is empty!\n");
  348. continue;
  349. }
  350. if(strcmp(g_row[1],"pbx.voip.inbound") == 0){
  351. memset(inboundstr,0,sizeof(inboundstr));
  352. pJson = cJSON_Parse(g_row[2]);
  353. if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "hangup") == 0){
  354. sprintf(inboundstr,"\
  355. [direct-analog]\n\
  356. exten => direct,1,Hangup()\n\
  357. [direct-voip]\n\
  358. exten => direct,1,Hangup()\n"
  359. );
  360. }
  361. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "extension") == 0){
  362. sprintf(inboundstr,"\
  363. [direct-analog]\n\
  364. exten => direct,1,Goto(default,%s,1)\n\
  365. [direct-voip]\n\
  366. exten => direct,1,Goto(default,%s,1)\n",\
  367. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  368. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  369. );
  370. }
  371. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "ivr") == 0){
  372. sprintf(inboundstr,"\
  373. [direct-analog]\n\
  374. exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n\
  375. [direct-voip]\n\
  376. exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n",\
  377. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  378. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  379. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  380. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  381. );
  382. }
  383. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "group") == 0){
  384. sprintf(inboundstr,"\
  385. [direct-analog]\n\
  386. exten => direct,1,Goto(paging-group-%s,%s,1)\n\
  387. [direct-voip]\n\
  388. exten => direct,1,Goto(paging-group-%s,%s,1)\n",\
  389. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  390. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  391. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  392. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  393. );
  394. }
  395. fputs(inboundstr,conf_inbound_fp);
  396. }
  397. else if(strcmp(g_row[1],"paging.prompt.config") == 0){
  398. pJson = cJSON_Parse(g_row[2]);
  399. fprintf(conf_global_fp, "\
  400. enPaging_prompt_start = %s\n\
  401. enPaging_prompt_end = %s\n\
  402. ",\
  403. cJSON_GetObjectItem(pJson, "start")->valuestring,\
  404. cJSON_GetObjectItem(pJson, "end")->valuestring\
  405. );
  406. if(cJSON_GetObjectItem(pJson, "startfile") != NULL){
  407. memset(prompt, 0, sizeof(prompt));
  408. if(strcmp(cJSON_GetObjectItem(pJson, "startfile")->valuestring,"start") == 0)
  409. strcpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring);
  410. else
  411. strncpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring,strlen(cJSON_GetObjectItem(pJson, "startfile")->valuestring)-4);
  412. fprintf(conf_global_fp, "\
  413. Paging_start_file = %s\n\
  414. ",\
  415. prompt\
  416. );
  417. }else{
  418. fprintf(conf_global_fp, "\
  419. Paging_start_file = start\n\
  420. ");
  421. }
  422. }
  423. else if(strcmp(g_row[1],"pbx.ringtime.config") == 0){
  424. pJson = cJSON_Parse(g_row[2]);
  425. if(cJSON_GetObjectItem(pJson, "ringtime") != NULL){
  426. fprintf(conf_global_fp, "\
  427. RINGTIME = %d\n\
  428. ",\
  429. cJSON_GetObjectItem(pJson, "ringtime")->valueint\
  430. );
  431. }else{
  432. fprintf(conf_global_fp, "\
  433. RINGTIME = 30\n\
  434. ");
  435. }
  436. }
  437. else if(strcmp(g_row[1],"pbx.nat.config") == 0){
  438. pJson = cJSON_Parse(g_row[2]);
  439. if(pJson && cJSON_GetObjectItem(pJson, "enable")->valueint == 1){
  440. memset(sip_udp_nat, 0, sizeof(sip_udp_nat));
  441. memset(sip_tcp_nat, 0, sizeof(sip_tcp_nat));
  442. memset(sip_tls_nat, 0, sizeof(sip_tls_nat));
  443. memset(localnet, 0, sizeof(localnet));
  444. cJSON *localnetArray = cJSON_GetObjectItem( pJson, "localnet");
  445. if(localnetArray != NULL){
  446. int array_size = cJSON_GetArraySize (localnetArray);
  447. for(int n = 0; n < array_size; n++){
  448. pSub = cJSON_GetArrayItem(localnetArray, n);
  449. if(NULL == pSub ){ continue ; }
  450. sprintf(localnet, "\
  451. local_net = %s\n\
  452. ",\
  453. pSub->valuestring\
  454. );
  455. }
  456. }
  457. sprintf(sip_udp_nat, "\
  458. external_media_address = %s\n\
  459. external_signaling_address = %s\n\
  460. %s\n\
  461. ",\
  462. cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
  463. cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
  464. localnet\
  465. );
  466. if(cJSON_GetObjectItem(pJson, "externtcpport") && cJSON_GetObjectItem(pJson, "externtcpport")->valueint != 0)
  467. {
  468. sprintf(sip_tcp_nat, "\
  469. external_media_address = %s\n\
  470. external_signaling_address = %s\n\
  471. external_signaling_port = %d\n\
  472. %s\n\
  473. ",\
  474. cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
  475. cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
  476. cJSON_GetObjectItem(pJson, "externtcpport")->valueint,\
  477. localnet\
  478. );
  479. }
  480. if(cJSON_GetObjectItem(pJson, "externtlsport") && cJSON_GetObjectItem(pJson, "externtlsport")->valueint != 0)
  481. {
  482. sprintf(sip_tls_nat, "\
  483. external_media_address = %s\n\
  484. external_signaling_address = %s\n\
  485. external_signaling_port = %d\n\
  486. %s\n\
  487. ",\
  488. cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
  489. cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
  490. cJSON_GetObjectItem(pJson, "externtlsport")->valueint,\
  491. localnet\
  492. );
  493. }
  494. }
  495. }
  496. else if(strcmp(g_row[1],"paging.record.config") == 0){
  497. pJson = cJSON_Parse(g_row[2]);
  498. if(cJSON_GetObjectItem(pJson, "paging_record") != NULL){
  499. fprintf(conf_global_fp, "\
  500. PAGING_RECORD = %s\n\
  501. ",\
  502. cJSON_GetObjectItem(pJson, "paging_record")->valuestring\
  503. );
  504. }
  505. if(cJSON_GetObjectItem(pJson, "intercom_record") != NULL){
  506. fprintf(conf_global_fp, "\
  507. INTERCOM_RECORD = %s\n\
  508. ",\
  509. cJSON_GetObjectItem(pJson, "intercom_record")->valuestring\
  510. );
  511. }
  512. if(cJSON_GetObjectItem(pJson, "conference_record") != NULL){
  513. fprintf(conf_global_fp, "\
  514. CONFERENCE_RECORD = %s\n\
  515. ",\
  516. cJSON_GetObjectItem(pJson, "conference_record")->valuestring\
  517. );
  518. }
  519. }
  520. else if(strcmp(g_row[1],"pbx.autoanswer.config") == 0){
  521. pJson = cJSON_Parse(g_row[2]);
  522. if(cJSON_GetObjectItem(pJson, "intercom_autoanswer") != NULL){
  523. fprintf(conf_global_fp, "\
  524. INTERCOM_AUTO = %s\n\
  525. ",\
  526. cJSON_GetObjectItem(pJson, "intercom_autoanswer")->valuestring\
  527. );
  528. }
  529. else
  530. {
  531. fprintf(conf_global_fp, "\
  532. INTERCOM_AUTO = yes\n\
  533. ");
  534. }
  535. if(cJSON_GetObjectItem(pJson, "paging_autoanswer") != NULL){
  536. fprintf(conf_global_fp, "\
  537. PAGING_AUTO = %s\n\
  538. ",\
  539. cJSON_GetObjectItem(pJson, "paging_autoanswer")->valuestring\
  540. );
  541. }
  542. else
  543. {
  544. fprintf(conf_global_fp, "\
  545. PAGING_AUTO = yes\n\
  546. ");
  547. }
  548. }
  549. else if(strcmp(g_row[1],"paging.calltrigger.config") == 0){
  550. pJson = cJSON_Parse(g_row[2]);
  551. fprintf(conf_calltrigger_fp, "\
  552. exten => _%s.,1,Gosub(calltrigger,s,1(${EXTEN}))\n\
  553. exten => _%s.,1,Gusub(calltrigger,s,1(${EXTEN}))\n\
  554. ",\
  555. cJSON_GetObjectItem(pJson, "start")->valuestring,\
  556. cJSON_GetObjectItem(pJson, "stop")->valuestring\
  557. );
  558. }
  559. else if(strcmp(g_row[1],"pbx.sipsettings.config") == 0){
  560. pJson = cJSON_Parse(g_row[2]);
  561. if(cJSON_GetObjectItem(pJson, "udp"))
  562. {
  563. fprintf(conf_sipsetting_fp, "\
  564. [transport-udp]\n\
  565. type=transport\n\
  566. protocol=udp\n\
  567. bind=0.0.0.0:%d\n\
  568. tos=CS3\n\
  569. cos=3\n\
  570. allow_reload=no\n\
  571. ",cJSON_GetObjectItem(pJson, "udp")->valueint\
  572. );
  573. if(sip_udp_nat)
  574. {
  575. fprintf(conf_sipsetting_fp, "%s", sip_udp_nat);
  576. }
  577. }
  578. pSub = cJSON_GetObjectItem(pJson, "tcp");
  579. if(pSub && cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  580. fprintf(conf_sipsetting_fp, "\
  581. [transport-tcp]\n\
  582. type=transport\n\
  583. protocol=tcp\n\
  584. bind=0.0.0.0:%d\n\
  585. tos=CS3\n\
  586. cos=3\n\
  587. allow_reload=no\n\
  588. ",cJSON_GetObjectItem(pSub, "port")->valueint\
  589. );
  590. if(sip_tcp_nat)
  591. {
  592. fprintf(conf_sipsetting_fp, "%s", sip_tcp_nat);
  593. }
  594. }
  595. pSub = cJSON_GetObjectItem(pJson, "tls");
  596. if(pSub && cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  597. fprintf(conf_sipsetting_fp, "\
  598. [transport-tls]\n\
  599. type=transport\n\
  600. protocol=tls\n\
  601. bind=0.0.0.0:%d\n\
  602. cert_file=/etc/asterisk/keys/asterisk.pem\n\
  603. ca_list_file=/etc/asterisk/keys/ca.crt\n\
  604. priv_key_file=/etc/asterisk/keys/asterisk.pem\n\
  605. method=tlsv1_3\n\
  606. tos=CS3\n\
  607. cos=3\n\
  608. allow_reload=no\n\
  609. ",cJSON_GetObjectItem(pSub, "port")->valueint\
  610. );
  611. if(sip_tls_nat)
  612. {
  613. fprintf(conf_sipsetting_fp, "%s", sip_tls_nat);
  614. }
  615. }
  616. fprintf(conf_sipsetting_fp, "\
  617. [transport-wss]\n\
  618. type=transport\n\
  619. protocol=wss\n\
  620. bind=0.0.0.0\n\
  621. allow_reload=no\n\
  622. ");
  623. pSub = cJSON_GetObjectItem(pJson, "rtp");
  624. if(pSub){
  625. fprintf(conf_rtpsetting_fp, "\
  626. rtpstart = %d\n\
  627. rtpend = %d\n\
  628. ",\
  629. cJSON_GetObjectItem(pSub, "start_port")->valueint,\
  630. cJSON_GetObjectItem(pSub, "end_port")->valueint\
  631. );
  632. }
  633. }
  634. else if(strcmp(g_row[1],"paging.featurecodes.config") == 0){
  635. pJson = cJSON_Parse(g_row[2]);
  636. pSub = cJSON_GetObjectItem(pJson, "bargein");
  637. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  638. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Gosub(spy-barge,s,1(${EXTEN:%ld},${CALLERID(num)}))\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  639. }
  640. pSub = cJSON_GetObjectItem(pJson, "clear");
  641. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  642. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Gosub(exten-clear,s,1(${EXTEN:%ld},${CALLERID(num)}))\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  643. }
  644. pSub = cJSON_GetObjectItem(pJson, "syp");
  645. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  646. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Gosub(spy-normal,s,1(${EXTEN:%ld},${CALLERID(num)}))\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  647. }
  648. pSub = cJSON_GetObjectItem(pJson, "whisper");
  649. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  650. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Gosub(spy-whisper,s,1(${EXTEN:%ld},${CALLERID(num)}))\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  651. }
  652. pSub = cJSON_GetObjectItem(pJson, "wakeup");
  653. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  654. fprintf(conf_featurecodes_fp, "exten = %s,1,Gosub(wakeup-call,s,1(${CALLERID(num)}))\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  655. }
  656. pSub = cJSON_GetObjectItem(pJson, "dnd");
  657. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  658. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-dnd-on,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  659. }
  660. pSub = cJSON_GetObjectItem(pJson, "cf-alway");
  661. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  662. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Goto(app-cf-on,cf-${EXTEN:%ld},1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  663. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cf-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  664. }
  665. pSub = cJSON_GetObjectItem(pJson, "cf-busy");
  666. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  667. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Goto(app-cfb-on,cf-${EXTEN:%ld},1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  668. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cfb-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  669. }
  670. pSub = cJSON_GetObjectItem(pJson, "cf-noanswer");
  671. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  672. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Goto(app-cfu-on,cf-${EXTEN:%ld},1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  673. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cfu-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  674. }
  675. }
  676. }
  677. fclose(conf_dialrule_fp);
  678. fclose(conf_ivr_fp);
  679. fclose(conf_inbound_fp);
  680. fclose(conf_global_fp);
  681. fclose(conf_calltrigger_fp);
  682. fclose(conf_featurecodes_fp);
  683. fclose(conf_sipsetting_fp);
  684. mysql_free_result(g_res); // 释放结果集
  685. mysql_close(g_conn); // 关闭链接
  686. cJSON_Delete(pJson);
  687. }