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. memset(sip_udp_nat, 0, sizeof(sip_udp_nat));
  86. memset(sip_tcp_nat, 0, sizeof(sip_tcp_nat));
  87. memset(sip_tls_nat, 0, sizeof(sip_tls_nat));
  88. typedef struct keys_action {
  89. char key[MINI_SIZE];
  90. char type[MINI_SIZE];
  91. char exten[MINI_SIZE];
  92. } KeysObject;
  93. strcpy(g_host_name,getenv("MYSQL"));
  94. strcpy(g_user_name,getenv("MYSQL_USER"));
  95. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  96. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  97. if (init_mysql()){
  98. print_mysql_error(NULL);
  99. exit(1);
  100. }
  101. //读取自动话务员的数据,并写入配置文件
  102. if (executesql(QUERY_IVR_SQL)){
  103. print_mysql_error(NULL);
  104. exit(1);
  105. }
  106. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  107. FILE *conf_ivr_fp = fopen(EXTEN_IVR_FILE, "w+");
  108. if (conf_ivr_fp == NULL){
  109. perror("Open paging conf file Error: ");
  110. exit(1);
  111. }
  112. fprintf(conf_ivr_fp, ";!\n\
  113. ;! Automatically generated configuration file\n\
  114. ;! Filename: extensions_ivr_custom.conf (/etc/asterisk/extensions_ivr_custom.conf)\n\
  115. ;! Generator: Generator IVR\n\
  116. ;! Creation Date: %s\n\
  117. ;!\n\n\
  118. ",\
  119. mytime()\
  120. );
  121. //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
  122. //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
  123. //| id | name | exten | prompt | loops | timeout | language | dialplan | keys_action | createdAt | updatedAt |
  124. //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
  125. //| 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 |
  126. //| 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 |
  127. //| 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 |
  128. //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
  129. //3 rows in set (0.00 sec)
  130. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  131. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
  132. printf("some feild is empty!\n");
  133. continue;
  134. }
  135. fprintf(conf_ivr_fp,"\
  136. [voicemenu-custom-%s]\n\
  137. exten => %s,1,NoOp(%s)\n",\
  138. g_row[1],\
  139. g_row[1],\
  140. g_row[0]\
  141. );
  142. if(g_row[5] != NULL){
  143. fprintf(conf_ivr_fp,"same => n,Set(CHANNEL(language)=%s)\n",g_row[5]);
  144. }
  145. memset(prompt, 0, sizeof(prompt));
  146. strncpy(prompt,g_row[2],strlen(g_row[2])-4);
  147. fprintf(conf_ivr_fp,"\
  148. same => n,Set(COUNT=%s)\n\
  149. same => n(loop),Background(%s)\n",\
  150. g_row[3],\
  151. prompt
  152. );
  153. if(strcmp(g_row[4], "0") != 0 && g_row[4] != NULL){
  154. fprintf(conf_ivr_fp,"same => n,WaitExten(%s)\n",g_row[4]);
  155. }
  156. fprintf(conf_ivr_fp,"\
  157. same => n,Set(COUNT=$[${COUNT}-1])\n\
  158. same => n,GotoIf($[${COUNT} < 0]?:loop)\n\
  159. same => n,WaitExten(1)\n");
  160. KeysObject keysObject[MINI_SIZE];
  161. memset(keysObject, 0, sizeof(keysObject));
  162. if(g_row[7] != NULL){
  163. pJson = cJSON_Parse(g_row[7]);
  164. iCount = cJSON_GetArraySize(pJson);
  165. for(int i = 0;i < iCount;i++){
  166. pSub = cJSON_GetArrayItem(pJson,i);
  167. if(pSub != NULL){
  168. strcpy(keysObject[i].key,cJSON_GetObjectItem(pSub, "key")->valuestring);
  169. strcpy(keysObject[i].type,cJSON_GetObjectItem(pSub, "type")->valuestring);
  170. strcpy(keysObject[i].exten,cJSON_GetObjectItem(pSub, "exten")->valuestring);
  171. if(strcmp(keysObject[i].type, "hangup") == 0){
  172. fprintf(conf_ivr_fp,"exten => %s,1,Hangup()\n",keysObject[i].key);
  173. }
  174. else if(strcmp(keysObject[i].type, "extension") == 0){
  175. fprintf(conf_ivr_fp,"exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  176. }
  177. else if(strcmp(keysObject[i].type, "ivr") == 0){
  178. fprintf(conf_ivr_fp,"exten => %s,1,Goto(voicemenu-custom-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  179. }
  180. else if(strcmp(keysObject[i].type, "group") == 0){
  181. fprintf(conf_ivr_fp,"exten => %s,1,Goto(paging-group-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  182. }
  183. else if(strcmp(keysObject[i].type, "user") == 0){
  184. int id = 100000 + atoi(keysObject[i].exten);
  185. fprintf(conf_ivr_fp,"exten => %s,1,Goto(manager-queue-%d,s,1)\n",keysObject[i].key,id);
  186. }
  187. }
  188. }
  189. }
  190. fprintf(conf_ivr_fp,"\n\n");
  191. }
  192. /*
  193. [voicemenu-custom-%s]
  194. include = default
  195. exten = _IVR-X.,1,NoOp(%s)
  196. exten = _IVR-X.,n,NoOp(Default language)
  197. exten = _IVR-X.,n,Set(COUNT=3)
  198. exten = _IVR-X.,n(loop),Background(%s)
  199. exten = _IVR-X.,n,Set(COUNT=$[${COUNT}-1])
  200. exten = _IVR-X.,n,WaitExten(%s)
  201. exten = _IVR-X.,n,GotoIf($[${COUNT}>1]?6)
  202. exten = _IVR-X.,n,WaitExten(1)
  203. exten = t,1,Hangup
  204. */
  205. //读取拨号规则的数据,并写入配置文件
  206. if (executesql(QUERY_DIALRULE_SQL)){
  207. print_mysql_error(NULL);
  208. exit(1);
  209. }
  210. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  211. FILE *conf_dialrule_fp = fopen(EXTEN_DIALRULE_FILE, "w+");
  212. if (conf_dialrule_fp == NULL){
  213. perror("Open paging conf file Error: ");
  214. exit(1);
  215. }
  216. fprintf(conf_dialrule_fp, ";!\n\
  217. ;! Automatically generated configuration file\n\
  218. ;! Filename: extensions_dialrule_custom.conf (/etc/asterisk/extensions_dialrule_custom.conf)\n\
  219. ;! Generator: Generator DIALRULE\n\
  220. ;! Creation Date: %s\n\
  221. ;!\n\n\
  222. ",\
  223. mytime()\
  224. );
  225. fputs("[CallingRule_OutCall]\n",conf_dialrule_fp);
  226. //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
  227. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  228. if (g_row[0] == NULL || g_row[1] == NULL){
  229. printf("some feild is empty!\n");
  230. continue;
  231. }
  232. //PJSIP/${profix}${exten}
  233. memset(dialrule,0,sizeof(dialrule));
  234. sprintf(dialrule, "exten => _%s,1,Gosub(trunkdial-failover,s,1(${EXTEN},PJSIP/",g_row[1]);
  235. if(g_row[3] != NULL){
  236. strcat(dialrule,g_row[3]);
  237. }
  238. strcat(dialrule,"${EXTEN:");
  239. if(g_row[2] != NULL){
  240. strcat(dialrule,g_row[2]);
  241. }
  242. strcat(dialrule,"}");
  243. if(g_row[4] != NULL){
  244. strcat(dialrule,g_row[4]);
  245. }
  246. strcat(dialrule,"@${");
  247. strcat(dialrule,g_row[0]);
  248. strcat(dialrule,"}))\n");
  249. fputs(dialrule,conf_dialrule_fp);
  250. }
  251. //读取全局配置数据,并写入配置文件
  252. if (executesql(QUERY_GLOBAL_SQL)){
  253. print_mysql_error(NULL);
  254. exit(1);
  255. }
  256. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  257. FILE *conf_inbound_fp = fopen(EXTEN_INBOUND_FILE, "w+");
  258. if (conf_inbound_fp == NULL){
  259. perror("Open inbound conf file Error: ");
  260. exit(1);
  261. }
  262. FILE *conf_global_fp = fopen(EXTEN_GLOBAL_FILE, "w+");
  263. if (conf_global_fp == NULL){
  264. perror("Open global conf file Error: ");
  265. exit(1);
  266. }
  267. FILE *conf_calltrigger_fp = fopen(EXTEN_CALLTRIGGER_FILE, "w+");
  268. if (conf_calltrigger_fp == NULL){
  269. perror("Open calltrigger conf file Error: ");
  270. exit(1);
  271. }
  272. FILE *conf_featurecodes_fp = fopen(EXTEN_FEATURECODES_FILE, "w+");
  273. if (conf_featurecodes_fp == NULL){
  274. perror("Open featurecodes conf file Error: ");
  275. exit(1);
  276. }
  277. FILE *conf_sipsetting_fp = fopen(SIP_SETTINGS_FILE, "w+");
  278. if (conf_sipsetting_fp == NULL){
  279. perror("Open sip settings conf file Error: ");
  280. exit(1);
  281. }
  282. FILE *conf_rtpsetting_fp = fopen(RTP_SETTINGS_FILE, "w+");
  283. if (conf_rtpsetting_fp == NULL){
  284. perror("Open rtp settings conf file Error: ");
  285. exit(1);
  286. }
  287. fprintf(conf_inbound_fp, ";!\n\
  288. ;! Automatically generated configuration file\n\
  289. ;! Filename: extensions_inbound_custom.conf (/etc/asterisk/extensions_inbound_custom.conf)\n\
  290. ;! Generator: Generator INBOUND\n\
  291. ;! Creation Date: %s\n\
  292. ;!\n\n\
  293. ",\
  294. mytime()\
  295. );
  296. fprintf(conf_global_fp, ";!\n\
  297. ;! Automatically generated configuration file\n\
  298. ;! Filename: extensions_global_custom.conf (/etc/asterisk/extensions_global_custom.conf)\n\
  299. ;! Generator: Generator GLOBAL\n\
  300. ;! Creation Date: %s\n\
  301. ;!\n\n\
  302. ",\
  303. mytime()\
  304. );
  305. fprintf(conf_global_fp, "\
  306. AGISERVERHOST = %s\n\
  307. AGISERVERPORT = %s\n\
  308. ",\
  309. getenv("BROADCAST_GATEWAY"),\
  310. getenv("AGI_SERVER_PORT")\
  311. );
  312. fprintf(conf_calltrigger_fp, ";!\n\
  313. ;! Automatically generated configuration file\n\
  314. ;! Filename: extensions_call_trigger_custom.conf (/etc/asterisk/extensions_call_trigger_custom.conf)\n\
  315. ;! Generator: Generator TRIGGER\n\
  316. ;! Creation Date: %s\n\
  317. ;!\n\n\
  318. ",\
  319. mytime()\
  320. );
  321. fprintf(conf_featurecodes_fp, ";!\n\
  322. ;! Automatically generated configuration file\n\
  323. ;! Filename: extensions_featurecodes_custom.conf (/etc/asterisk/extensions_featurecodes_custom.conf)\n\
  324. ;! Generator: Generator FEATURECODES\n\
  325. ;! Creation Date: %s\n\
  326. ;!\n\n\
  327. [featurecodes]\n",\
  328. mytime()\
  329. );
  330. fprintf(conf_sipsetting_fp, ";!\n\
  331. ;! Automatically generated configuration file\n\
  332. ;! Filename: pjsip_transports.conf (/etc/asterisk/pjsip_transports.conf)\n\
  333. ;! Generator: Generator SIP SETTINGS\n\
  334. ;! Creation Date: %s\n\
  335. ;!\n\n\
  336. \n",\
  337. mytime()\
  338. );
  339. fprintf(conf_rtpsetting_fp, ";!\n\
  340. ;! Automatically generated configuration file\n\
  341. ;! Filename: rtp_settings.conf (/etc/asterisk/rtp_settings.conf)\n\
  342. ;! Generator: Generator RTP SETTINGS\n\
  343. ;! Creation Date: %s\n\
  344. ;!\n\n\
  345. \n",\
  346. mytime()\
  347. );
  348. //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
  349. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  350. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
  351. printf("some feild is empty!\n");
  352. continue;
  353. }
  354. if(strcmp(g_row[1],"pbx.voip.inbound") == 0){
  355. memset(inboundstr,0,sizeof(inboundstr));
  356. pJson = cJSON_Parse(g_row[2]);
  357. if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "hangup") == 0){
  358. sprintf(inboundstr,"\
  359. [direct-analog]\n\
  360. exten => direct,1,Hangup()\n\
  361. [direct-voip]\n\
  362. exten => direct,1,Hangup()\n"
  363. );
  364. }
  365. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "extension") == 0){
  366. sprintf(inboundstr,"\
  367. [direct-analog]\n\
  368. exten => direct,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n\
  369. [direct-voip]\n\
  370. exten => direct,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",\
  371. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  372. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  373. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  374. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  375. );
  376. }
  377. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "ivr") == 0){
  378. sprintf(inboundstr,"\
  379. [direct-analog]\n\
  380. exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n\
  381. [direct-voip]\n\
  382. exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n",\
  383. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  384. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  385. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  386. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  387. );
  388. }
  389. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "group") == 0){
  390. sprintf(inboundstr,"\
  391. [direct-analog]\n\
  392. exten => direct,1,Goto(paging-group-%s,%s,1)\n\
  393. [direct-voip]\n\
  394. exten => direct,1,Goto(paging-group-%s,%s,1)\n",\
  395. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  396. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  397. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  398. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  399. );
  400. }
  401. fputs(inboundstr,conf_inbound_fp);
  402. }
  403. else if(strcmp(g_row[1],"paging.prompt.config") == 0){
  404. pJson = cJSON_Parse(g_row[2]);
  405. fprintf(conf_global_fp, "\
  406. enPaging_prompt_start = %s\n\
  407. enPaging_prompt_end = %s\n\
  408. ",\
  409. cJSON_GetObjectItem(pJson, "start")->valuestring,\
  410. cJSON_GetObjectItem(pJson, "end")->valuestring\
  411. );
  412. if(cJSON_GetObjectItem(pJson, "startfile") != NULL){
  413. memset(prompt, 0, sizeof(prompt));
  414. if(strcmp(cJSON_GetObjectItem(pJson, "startfile")->valuestring,"start") == 0)
  415. strcpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring);
  416. else
  417. strncpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring,strlen(cJSON_GetObjectItem(pJson, "startfile")->valuestring)-4);
  418. fprintf(conf_global_fp, "\
  419. Paging_start_file = %s\n\
  420. ",\
  421. prompt\
  422. );
  423. }else{
  424. fprintf(conf_global_fp, "\
  425. Paging_start_file = start\n\
  426. ");
  427. }
  428. }
  429. else if(strcmp(g_row[1],"pbx.ringtime.config") == 0){
  430. pJson = cJSON_Parse(g_row[2]);
  431. if(cJSON_GetObjectItem(pJson, "ringtime") != NULL){
  432. fprintf(conf_global_fp, "\
  433. RINGTIME = %d\n\
  434. ",\
  435. cJSON_GetObjectItem(pJson, "ringtime")->valueint\
  436. );
  437. }else{
  438. fprintf(conf_global_fp, "\
  439. RINGTIME = 30\n\
  440. ");
  441. }
  442. }
  443. else if(strcmp(g_row[1],"pbx.nat.config") == 0){
  444. pJson = cJSON_Parse(g_row[2]);
  445. if(pJson && cJSON_GetObjectItem(pJson, "enable")->valueint == 1){
  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(strlen(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(strlen(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(strlen(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. }