generate_extension_conf.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. exten => %s,1,NoOp(%s)\n",\
  135. g_row[1],\
  136. g_row[1],\
  137. g_row[0]\
  138. );
  139. if(g_row[5] != NULL){
  140. fprintf(conf_ivr_fp,"same => n,Set(CHANNEL(language)=%s)\n",g_row[5]);
  141. }
  142. memset(prompt, 0, sizeof(prompt));
  143. strncpy(prompt,g_row[2],strlen(g_row[2])-4);
  144. fprintf(conf_ivr_fp,"\
  145. same => n,Set(COUNT=%s)\n\
  146. same => n(loop),Background(%s)\n",\
  147. g_row[3],\
  148. prompt
  149. );
  150. if(strcmp(g_row[4], "0") != 0 && g_row[4] != NULL){
  151. fprintf(conf_ivr_fp,"same => n,WaitExten(%s)\n",g_row[4]);
  152. }
  153. fprintf(conf_ivr_fp,"\
  154. same => n,Set(COUNT=$[${COUNT}-1])\n\
  155. same => n,GotoIf($[${COUNT} < 0]?:loop)\n\
  156. same => n,WaitExten(1)\n");
  157. KeysObject keysObject[MINI_SIZE];
  158. memset(keysObject, 0, sizeof(keysObject));
  159. if(g_row[7] != NULL){
  160. pJson = cJSON_Parse(g_row[7]);
  161. iCount = cJSON_GetArraySize(pJson);
  162. for(int i = 0;i < iCount;i++){
  163. pSub = cJSON_GetArrayItem(pJson,i);
  164. if(pSub != NULL){
  165. strcpy(keysObject[i].key,cJSON_GetObjectItem(pSub, "key")->valuestring);
  166. strcpy(keysObject[i].type,cJSON_GetObjectItem(pSub, "type")->valuestring);
  167. strcpy(keysObject[i].exten,cJSON_GetObjectItem(pSub, "exten")->valuestring);
  168. if(strcmp(keysObject[i].type, "hangup") == 0){
  169. fprintf(conf_ivr_fp,"exten => %s,1,Hangup()\n",keysObject[i].key);
  170. }
  171. else if(strcmp(keysObject[i].type, "extension") == 0){
  172. fprintf(conf_ivr_fp,"exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  173. }
  174. else if(strcmp(keysObject[i].type, "ivr") == 0){
  175. fprintf(conf_ivr_fp,"exten => %s,1,Goto(voicemenu-custom-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  176. }
  177. else if(strcmp(keysObject[i].type, "group") == 0){
  178. fprintf(conf_ivr_fp,"exten => %s,1,Goto(paging-group-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  179. }
  180. else if(strcmp(keysObject[i].type, "user") == 0){
  181. int id = 100000 + atoi(keysObject[i].exten);
  182. fprintf(conf_ivr_fp,"exten => %s,1,Goto(manager-queue-%d,s,1)\n",keysObject[i].key,id);
  183. }
  184. }
  185. }
  186. }
  187. fprintf(conf_ivr_fp,"\n\n");
  188. }
  189. /*
  190. [voicemenu-custom-%s]
  191. include = default
  192. exten = _IVR-X.,1,NoOp(%s)
  193. exten = _IVR-X.,n,NoOp(Default language)
  194. exten = _IVR-X.,n,Set(COUNT=3)
  195. exten = _IVR-X.,n(loop),Background(%s)
  196. exten = _IVR-X.,n,Set(COUNT=$[${COUNT}-1])
  197. exten = _IVR-X.,n,WaitExten(%s)
  198. exten = _IVR-X.,n,GotoIf($[${COUNT}>1]?6)
  199. exten = _IVR-X.,n,WaitExten(1)
  200. exten = t,1,Hangup
  201. */
  202. //读取拨号规则的数据,并写入配置文件
  203. if (executesql(QUERY_DIALRULE_SQL)){
  204. print_mysql_error(NULL);
  205. exit(1);
  206. }
  207. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  208. FILE *conf_dialrule_fp = fopen(EXTEN_DIALRULE_FILE, "w+");
  209. if (conf_dialrule_fp == NULL){
  210. perror("Open paging conf file Error: ");
  211. exit(1);
  212. }
  213. fprintf(conf_dialrule_fp, ";!\n\
  214. ;! Automatically generated configuration file\n\
  215. ;! Filename: extensions_dialrule_custom.conf (/etc/asterisk/extensions_dialrule_custom.conf)\n\
  216. ;! Generator: Generator DIALRULE\n\
  217. ;! Creation Date: %s\n\
  218. ;!\n\n\
  219. ",\
  220. mytime()\
  221. );
  222. fputs("[CallingRule_OutCall]\n",conf_dialrule_fp);
  223. //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
  224. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  225. if (g_row[0] == NULL || g_row[1] == NULL){
  226. printf("some feild is empty!\n");
  227. continue;
  228. }
  229. //PJSIP/${profix}${exten}
  230. memset(dialrule,0,sizeof(dialrule));
  231. sprintf(dialrule, "exten => _%s,1,Gosub(trunkdial-failover,s,1(${EXTEN},PJSIP/",g_row[1]);
  232. if(g_row[3] != NULL){
  233. strcat(dialrule,g_row[3]);
  234. }
  235. strcat(dialrule,"${EXTEN:");
  236. if(g_row[2] != NULL){
  237. strcat(dialrule,g_row[2]);
  238. }
  239. strcat(dialrule,"}");
  240. if(g_row[4] != NULL){
  241. strcat(dialrule,g_row[4]);
  242. }
  243. strcat(dialrule,"@${");
  244. strcat(dialrule,g_row[0]);
  245. strcat(dialrule,"}))\n");
  246. fputs(dialrule,conf_dialrule_fp);
  247. }
  248. //读取全局配置数据,并写入配置文件
  249. if (executesql(QUERY_GLOBAL_SQL)){
  250. print_mysql_error(NULL);
  251. exit(1);
  252. }
  253. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  254. FILE *conf_inbound_fp = fopen(EXTEN_INBOUND_FILE, "w+");
  255. if (conf_inbound_fp == NULL){
  256. perror("Open inbound conf file Error: ");
  257. exit(1);
  258. }
  259. FILE *conf_global_fp = fopen(EXTEN_GLOBAL_FILE, "w+");
  260. if (conf_global_fp == NULL){
  261. perror("Open global conf file Error: ");
  262. exit(1);
  263. }
  264. FILE *conf_calltrigger_fp = fopen(EXTEN_CALLTRIGGER_FILE, "w+");
  265. if (conf_calltrigger_fp == NULL){
  266. perror("Open calltrigger conf file Error: ");
  267. exit(1);
  268. }
  269. FILE *conf_featurecodes_fp = fopen(EXTEN_FEATURECODES_FILE, "w+");
  270. if (conf_featurecodes_fp == NULL){
  271. perror("Open featurecodes conf file Error: ");
  272. exit(1);
  273. }
  274. FILE *conf_sipsetting_fp = fopen(SIP_SETTINGS_FILE, "w+");
  275. if (conf_sipsetting_fp == NULL){
  276. perror("Open sip settings conf file Error: ");
  277. exit(1);
  278. }
  279. FILE *conf_rtpsetting_fp = fopen(RTP_SETTINGS_FILE, "w+");
  280. if (conf_rtpsetting_fp == NULL){
  281. perror("Open rtp settings conf file Error: ");
  282. exit(1);
  283. }
  284. fprintf(conf_inbound_fp, ";!\n\
  285. ;! Automatically generated configuration file\n\
  286. ;! Filename: extensions_inbound_custom.conf (/etc/asterisk/extensions_inbound_custom.conf)\n\
  287. ;! Generator: Generator INBOUND\n\
  288. ;! Creation Date: %s\n\
  289. ;!\n\n\
  290. ",\
  291. mytime()\
  292. );
  293. fprintf(conf_global_fp, ";!\n\
  294. ;! Automatically generated configuration file\n\
  295. ;! Filename: extensions_global_custom.conf (/etc/asterisk/extensions_global_custom.conf)\n\
  296. ;! Generator: Generator GLOBAL\n\
  297. ;! Creation Date: %s\n\
  298. ;!\n\n\
  299. ",\
  300. mytime()\
  301. );
  302. fprintf(conf_global_fp, "\
  303. AGISERVERHOST = %s\n\
  304. AGISERVERPORT = %s\n\
  305. ",\
  306. getenv("BROADCAST_GATEWAY"),\
  307. getenv("AGI_SERVER_PORT")\
  308. );
  309. fprintf(conf_calltrigger_fp, ";!\n\
  310. ;! Automatically generated configuration file\n\
  311. ;! Filename: extensions_call_trigger_custom.conf (/etc/asterisk/extensions_call_trigger_custom.conf)\n\
  312. ;! Generator: Generator TRIGGER\n\
  313. ;! Creation Date: %s\n\
  314. ;!\n\n\
  315. ",\
  316. mytime()\
  317. );
  318. fprintf(conf_featurecodes_fp, ";!\n\
  319. ;! Automatically generated configuration file\n\
  320. ;! Filename: extensions_featurecodes_custom.conf (/etc/asterisk/extensions_featurecodes_custom.conf)\n\
  321. ;! Generator: Generator FEATURECODES\n\
  322. ;! Creation Date: %s\n\
  323. ;!\n\n\
  324. [featurecodes]\n",\
  325. mytime()\
  326. );
  327. fprintf(conf_sipsetting_fp, ";!\n\
  328. ;! Automatically generated configuration file\n\
  329. ;! Filename: pjsip_transports.conf (/etc/asterisk/pjsip_transports.conf)\n\
  330. ;! Generator: Generator SIP SETTINGS\n\
  331. ;! Creation Date: %s\n\
  332. ;!\n\n\
  333. \n",\
  334. mytime()\
  335. );
  336. fprintf(conf_rtpsetting_fp, ";!\n\
  337. ;! Automatically generated configuration file\n\
  338. ;! Filename: rtp_settings.conf (/etc/asterisk/rtp_settings.conf)\n\
  339. ;! Generator: Generator RTP SETTINGS\n\
  340. ;! Creation Date: %s\n\
  341. ;!\n\n\
  342. \n",\
  343. mytime()\
  344. );
  345. //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
  346. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  347. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
  348. printf("some feild is empty!\n");
  349. continue;
  350. }
  351. if(strcmp(g_row[1],"pbx.voip.inbound") == 0){
  352. memset(inboundstr,0,sizeof(inboundstr));
  353. pJson = cJSON_Parse(g_row[2]);
  354. if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "hangup") == 0){
  355. sprintf(inboundstr,"\
  356. [direct-analog]\n\
  357. exten => direct,1,Hangup()\n\
  358. [direct-voip]\n\
  359. exten => direct,1,Hangup()\n"
  360. );
  361. }
  362. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "extension") == 0){
  363. sprintf(inboundstr,"\
  364. [direct-analog]\n\
  365. exten => direct,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n\
  366. [direct-voip]\n\
  367. exten => direct,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",\
  368. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  369. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  370. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  371. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  372. );
  373. }
  374. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "ivr") == 0){
  375. sprintf(inboundstr,"\
  376. [direct-analog]\n\
  377. exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n\
  378. [direct-voip]\n\
  379. exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n",\
  380. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  381. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  382. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  383. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  384. );
  385. }
  386. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "group") == 0){
  387. sprintf(inboundstr,"\
  388. [direct-analog]\n\
  389. exten => direct,1,Goto(paging-group-%s,%s,1)\n\
  390. [direct-voip]\n\
  391. exten => direct,1,Goto(paging-group-%s,%s,1)\n",\
  392. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  393. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  394. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  395. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  396. );
  397. }
  398. fputs(inboundstr,conf_inbound_fp);
  399. }
  400. else if(strcmp(g_row[1],"paging.prompt.config") == 0){
  401. pJson = cJSON_Parse(g_row[2]);
  402. fprintf(conf_global_fp, "\
  403. enPaging_prompt_start = %s\n\
  404. enPaging_prompt_end = %s\n\
  405. ",\
  406. cJSON_GetObjectItem(pJson, "start")->valuestring,\
  407. cJSON_GetObjectItem(pJson, "end")->valuestring\
  408. );
  409. if(cJSON_GetObjectItem(pJson, "startfile") != NULL){
  410. memset(prompt, 0, sizeof(prompt));
  411. if(strcmp(cJSON_GetObjectItem(pJson, "startfile")->valuestring,"start") == 0)
  412. strcpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring);
  413. else
  414. strncpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring,strlen(cJSON_GetObjectItem(pJson, "startfile")->valuestring)-4);
  415. fprintf(conf_global_fp, "\
  416. Paging_start_file = %s\n\
  417. ",\
  418. prompt\
  419. );
  420. }else{
  421. fprintf(conf_global_fp, "\
  422. Paging_start_file = start\n\
  423. ");
  424. }
  425. }
  426. else if(strcmp(g_row[1],"pbx.ringtime.config") == 0){
  427. pJson = cJSON_Parse(g_row[2]);
  428. if(cJSON_GetObjectItem(pJson, "ringtime") != NULL){
  429. fprintf(conf_global_fp, "\
  430. RINGTIME = %d\n\
  431. ",\
  432. cJSON_GetObjectItem(pJson, "ringtime")->valueint\
  433. );
  434. }else{
  435. fprintf(conf_global_fp, "\
  436. RINGTIME = 30\n\
  437. ");
  438. }
  439. }
  440. else if(strcmp(g_row[1],"pbx.nat.config") == 0){
  441. pJson = cJSON_Parse(g_row[2]);
  442. if(pJson && cJSON_GetObjectItem(pJson, "enable")->valueint == 1){
  443. memset(sip_udp_nat, 0, sizeof(sip_udp_nat));
  444. memset(sip_tcp_nat, 0, sizeof(sip_tcp_nat));
  445. memset(sip_tls_nat, 0, sizeof(sip_tls_nat));
  446. memset(localnet, 0, sizeof(localnet));
  447. cJSON *localnetArray = cJSON_GetObjectItem( pJson, "localnet");
  448. if(localnetArray != NULL){
  449. int array_size = cJSON_GetArraySize (localnetArray);
  450. for(int n = 0; n < array_size; n++){
  451. pSub = cJSON_GetArrayItem(localnetArray, n);
  452. if(NULL == pSub ){ continue ; }
  453. sprintf(localnet, "\
  454. local_net = %s\n\
  455. ",\
  456. pSub->valuestring\
  457. );
  458. }
  459. }
  460. sprintf(sip_udp_nat, "\
  461. external_media_address = %s\n\
  462. external_signaling_address = %s\n\
  463. %s\n\
  464. ",\
  465. cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
  466. cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
  467. localnet\
  468. );
  469. if(cJSON_GetObjectItem(pJson, "externtcpport") && cJSON_GetObjectItem(pJson, "externtcpport")->valueint != 0)
  470. {
  471. sprintf(sip_tcp_nat, "\
  472. external_media_address = %s\n\
  473. external_signaling_address = %s\n\
  474. external_signaling_port = %d\n\
  475. %s\n\
  476. ",\
  477. cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
  478. cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
  479. cJSON_GetObjectItem(pJson, "externtcpport")->valueint,\
  480. localnet\
  481. );
  482. }
  483. if(cJSON_GetObjectItem(pJson, "externtlsport") && cJSON_GetObjectItem(pJson, "externtlsport")->valueint != 0)
  484. {
  485. sprintf(sip_tls_nat, "\
  486. external_media_address = %s\n\
  487. external_signaling_address = %s\n\
  488. external_signaling_port = %d\n\
  489. %s\n\
  490. ",\
  491. cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
  492. cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
  493. cJSON_GetObjectItem(pJson, "externtlsport")->valueint,\
  494. localnet\
  495. );
  496. }
  497. }
  498. }
  499. else if(strcmp(g_row[1],"paging.record.config") == 0){
  500. pJson = cJSON_Parse(g_row[2]);
  501. if(cJSON_GetObjectItem(pJson, "paging_record") != NULL){
  502. fprintf(conf_global_fp, "\
  503. PAGING_RECORD = %s\n\
  504. ",\
  505. cJSON_GetObjectItem(pJson, "paging_record")->valuestring\
  506. );
  507. }
  508. if(cJSON_GetObjectItem(pJson, "intercom_record") != NULL){
  509. fprintf(conf_global_fp, "\
  510. INTERCOM_RECORD = %s\n\
  511. ",\
  512. cJSON_GetObjectItem(pJson, "intercom_record")->valuestring\
  513. );
  514. }
  515. if(cJSON_GetObjectItem(pJson, "conference_record") != NULL){
  516. fprintf(conf_global_fp, "\
  517. CONFERENCE_RECORD = %s\n\
  518. ",\
  519. cJSON_GetObjectItem(pJson, "conference_record")->valuestring\
  520. );
  521. }
  522. }
  523. else if(strcmp(g_row[1],"pbx.autoanswer.config") == 0){
  524. pJson = cJSON_Parse(g_row[2]);
  525. if(cJSON_GetObjectItem(pJson, "intercom_autoanswer") != NULL){
  526. fprintf(conf_global_fp, "\
  527. INTERCOM_AUTO = %s\n\
  528. ",\
  529. cJSON_GetObjectItem(pJson, "intercom_autoanswer")->valuestring\
  530. );
  531. }
  532. else
  533. {
  534. fprintf(conf_global_fp, "\
  535. INTERCOM_AUTO = yes\n\
  536. ");
  537. }
  538. if(cJSON_GetObjectItem(pJson, "paging_autoanswer") != NULL){
  539. fprintf(conf_global_fp, "\
  540. PAGING_AUTO = %s\n\
  541. ",\
  542. cJSON_GetObjectItem(pJson, "paging_autoanswer")->valuestring\
  543. );
  544. }
  545. else
  546. {
  547. fprintf(conf_global_fp, "\
  548. PAGING_AUTO = yes\n\
  549. ");
  550. }
  551. }
  552. else if(strcmp(g_row[1],"paging.calltrigger.config") == 0){
  553. pJson = cJSON_Parse(g_row[2]);
  554. fprintf(conf_calltrigger_fp, "\
  555. exten => _%s.,1,Gosub(calltrigger,s,1(${EXTEN}))\n\
  556. exten => _%s.,1,Gosub(calltrigger,s,1(${EXTEN}))\n\
  557. ",\
  558. cJSON_GetObjectItem(pJson, "start")->valuestring,\
  559. cJSON_GetObjectItem(pJson, "stop")->valuestring\
  560. );
  561. if(cJSON_HasObjectItem(pJson, "enRetriggered"))
  562. {
  563. fprintf(conf_global_fp, "\
  564. STOP_WHEN_RETRIGGERED = %s\n\
  565. ",\
  566. cJSON_GetObjectItem(pJson, "enRetriggered")->valuestring\
  567. );
  568. }
  569. else
  570. {
  571. fprintf(conf_global_fp, "\
  572. STOP_WHEN_RETRIGGERED = no\n\
  573. ");
  574. }
  575. }
  576. else if(strcmp(g_row[1],"pbx.sipsettings.config") == 0){
  577. pJson = cJSON_Parse(g_row[2]);
  578. if(cJSON_GetObjectItem(pJson, "udp"))
  579. {
  580. fprintf(conf_sipsetting_fp, "\
  581. [transport-udp]\n\
  582. type=transport\n\
  583. protocol=udp\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(pJson, "udp")->valueint\
  589. );
  590. if(sip_udp_nat)
  591. {
  592. fprintf(conf_sipsetting_fp, "%s", sip_udp_nat);
  593. }
  594. }
  595. pSub = cJSON_GetObjectItem(pJson, "tcp");
  596. if(pSub && cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  597. fprintf(conf_sipsetting_fp, "\
  598. [transport-tcp]\n\
  599. type=transport\n\
  600. protocol=tcp\n\
  601. bind=0.0.0.0:%d\n\
  602. tos=CS3\n\
  603. cos=3\n\
  604. allow_reload=no\n\
  605. ",cJSON_GetObjectItem(pSub, "port")->valueint\
  606. );
  607. if(sip_tcp_nat)
  608. {
  609. fprintf(conf_sipsetting_fp, "%s", sip_tcp_nat);
  610. }
  611. }
  612. pSub = cJSON_GetObjectItem(pJson, "tls");
  613. if(pSub && cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  614. fprintf(conf_sipsetting_fp, "\
  615. [transport-tls]\n\
  616. type=transport\n\
  617. protocol=tls\n\
  618. bind=0.0.0.0:%d\n\
  619. cert_file=/etc/asterisk/keys/asterisk.pem\n\
  620. ca_list_file=/etc/asterisk/keys/ca.crt\n\
  621. priv_key_file=/etc/asterisk/keys/asterisk.pem\n\
  622. method=tlsv1_3\n\
  623. tos=CS3\n\
  624. cos=3\n\
  625. allow_reload=no\n\
  626. ",cJSON_GetObjectItem(pSub, "port")->valueint\
  627. );
  628. if(sip_tls_nat)
  629. {
  630. fprintf(conf_sipsetting_fp, "%s", sip_tls_nat);
  631. }
  632. }
  633. fprintf(conf_sipsetting_fp, "\
  634. [transport-wss]\n\
  635. type=transport\n\
  636. protocol=wss\n\
  637. bind=0.0.0.0\n\
  638. allow_reload=no\n\
  639. ");
  640. pSub = cJSON_GetObjectItem(pJson, "rtp");
  641. if(pSub){
  642. fprintf(conf_rtpsetting_fp, "\
  643. rtpstart = %d\n\
  644. rtpend = %d\n\
  645. ",\
  646. cJSON_GetObjectItem(pSub, "start_port")->valueint,\
  647. cJSON_GetObjectItem(pSub, "end_port")->valueint\
  648. );
  649. }
  650. }
  651. else if(strcmp(g_row[1],"paging.featurecodes.config") == 0){
  652. pJson = cJSON_Parse(g_row[2]);
  653. pSub = cJSON_GetObjectItem(pJson, "bargein");
  654. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  655. 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));
  656. }
  657. pSub = cJSON_GetObjectItem(pJson, "clear");
  658. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  659. 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));
  660. }
  661. pSub = cJSON_GetObjectItem(pJson, "syp");
  662. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  663. 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));
  664. }
  665. pSub = cJSON_GetObjectItem(pJson, "whisper");
  666. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  667. 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));
  668. }
  669. pSub = cJSON_GetObjectItem(pJson, "wakeup");
  670. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  671. fprintf(conf_featurecodes_fp, "exten = %s,1,Gosub(wakeup-call,s,1(${CALLERID(num)}))\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  672. }
  673. pSub = cJSON_GetObjectItem(pJson, "dnd");
  674. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  675. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-dnd-on,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  676. }
  677. pSub = cJSON_GetObjectItem(pJson, "cf-alway");
  678. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  679. 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));
  680. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cf-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  681. }
  682. pSub = cJSON_GetObjectItem(pJson, "cf-busy");
  683. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  684. 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));
  685. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cfb-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  686. }
  687. pSub = cJSON_GetObjectItem(pJson, "cf-noanswer");
  688. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  689. 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));
  690. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cfu-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  691. }
  692. }
  693. }
  694. fclose(conf_dialrule_fp);
  695. fclose(conf_ivr_fp);
  696. fclose(conf_inbound_fp);
  697. fclose(conf_global_fp);
  698. fclose(conf_calltrigger_fp);
  699. fclose(conf_featurecodes_fp);
  700. fclose(conf_sipsetting_fp);
  701. mysql_free_result(g_res); // 释放结果集
  702. mysql_close(g_conn); // 关闭链接
  703. cJSON_Delete(pJson);
  704. }