generate_extension_conf.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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_NAT_FILE "/etc/asterisk/sip_nat.conf"
  33. #define SIP_SETTINGS_FILE "/etc/asterisk/sip_settings.conf"
  34. #define RTP_SETTINGS_FILE "/etc/asterisk/rtp_settings.conf"
  35. #define KEYVALLEN 100
  36. #define VERSION "V1.0.1"
  37. #define QUERY_IVR_SQL "select name,exten,prompt,loops,timeout,language,dialplan,keys_action from t_pbx_ivr"
  38. #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"
  39. #define QUERY_GLOBAL_SQL "select * from t_global_config"
  40. char g_host_name[MINI_SIZE];
  41. char g_user_name[MINI_SIZE];
  42. char g_password[MINI_SIZE];
  43. char g_db_name[MINI_SIZE];
  44. const unsigned int g_db_port = 3306;
  45. char dialrule[MIDLE_SIZE];
  46. char ivrstr[MAX_SIZE];
  47. char inboundstr[MIDLE_SIZE];
  48. char prompt[MINI_SIZE];
  49. //读取配置文件函数----功能:删除左边空格
  50. char *l_trim(char *szOutput, const char *szInput)
  51. {
  52. assert(szInput != NULL);
  53. assert(szOutput != NULL);
  54. assert(szOutput != szInput);
  55. for (NULL; *szInput != '\0' && isspace(*szInput); ++szInput)
  56. {
  57. ;
  58. }
  59. return strcpy(szOutput, szInput);
  60. }
  61. /* 删除右边的空格 */
  62. char *r_trim(char *szOutput, const char *szInput)
  63. {
  64. char *p = NULL;
  65. assert(szInput != NULL);
  66. assert(szOutput != NULL);
  67. assert(szOutput != szInput);
  68. strcpy(szOutput, szInput);
  69. for(p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  70. {
  71. ;
  72. }
  73. *(++p) = '\0';
  74. return szOutput;
  75. }
  76. /* 删除两边的空格 */
  77. char *a_trim(char *szOutput, const char *szInput)
  78. {
  79. char *p = NULL;
  80. assert(szInput != NULL);
  81. assert(szOutput != NULL);
  82. l_trim(szOutput, szInput);
  83. for (p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  84. {
  85. ;
  86. }
  87. *(++p) = '\0';
  88. return szOutput;
  89. }
  90. //main函数接口 参数1:配置文件路径 参数2:配置文件的那一部分,如general 参数3:键名 参数4:键值
  91. int GetProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal )
  92. {
  93. char appname[32], keyname[32];
  94. char *buf, *c;
  95. char buf_i[KEYVALLEN], buf_o[KEYVALLEN];
  96. FILE *fp;
  97. int found = 0; /* 1 AppName 2 KeyName */
  98. if( (fp = fopen( profile, "r" )) == NULL )
  99. {
  100. printf( "openfile [%s] error [%s]\n", profile, strerror(errno) );
  101. return(-1);
  102. }
  103. fseek( fp, 0, SEEK_SET );
  104. memset( appname, 0, sizeof(appname) );
  105. sprintf( appname, "[%s]", AppName );
  106. while( !feof(fp) && fgets( buf_i, KEYVALLEN, fp ) != NULL )
  107. {
  108. l_trim(buf_o, buf_i);
  109. if( strlen(buf_o) <= 0 )
  110. continue;
  111. buf = NULL;
  112. buf = buf_o;
  113. if( found == 0 )
  114. {
  115. if( buf[0] != '[' )
  116. {
  117. continue;
  118. }
  119. else if ( strncmp(buf, appname, strlen(appname)) == 0 )
  120. {
  121. found = 1;
  122. continue;
  123. }
  124. }
  125. else if( found == 1 )
  126. {
  127. if( buf[0] == '#' )
  128. {
  129. continue;
  130. }
  131. else if ( buf[0] == '[' )
  132. {
  133. break;
  134. }
  135. else
  136. {
  137. if( (c = (char *)strchr(buf, '=')) == NULL )
  138. continue;
  139. memset( keyname, 0, sizeof(keyname) );
  140. sscanf( buf, "%[^=|^ |^\t]", keyname );
  141. if( strcmp(keyname, KeyName) == 0 )
  142. {
  143. sscanf( ++c, "%[^\n]", KeyVal );
  144. char *KeyVal_o = (char *)malloc(strlen(KeyVal) + 1);
  145. if(KeyVal_o != NULL)
  146. {
  147. memset(KeyVal_o, 0, sizeof(KeyVal_o));
  148. a_trim(KeyVal_o, KeyVal);
  149. if(KeyVal_o && strlen(KeyVal_o) > 0)
  150. strcpy(KeyVal, KeyVal_o);
  151. free(KeyVal_o);
  152. KeyVal_o = NULL;
  153. }
  154. found = 2;
  155. break;
  156. }
  157. else
  158. {
  159. continue;
  160. }
  161. }
  162. }
  163. }
  164. fclose( fp );
  165. if( found == 2 )
  166. return(0);
  167. else
  168. return(-1);
  169. }
  170. char * mytime(){
  171. time_t my_time;
  172. time(&my_time);
  173. char *time_string = ctime(&my_time);
  174. if (time_string[strlen(time_string) - 1] == '\n')
  175. {
  176. time_string[strlen(time_string) - 1] = '\0';
  177. }
  178. return time_string;
  179. }
  180. void print_mysql_error(const char *msg) { // 打印最后一次错误
  181. if (msg)
  182. printf("%s: %s\n", msg, mysql_error(g_conn));
  183. else
  184. puts(mysql_error(g_conn));
  185. }
  186. int executesql(const char * sql) {
  187. /*query the database according the sql*/
  188. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  189. return -1; // 表示失败
  190. return 0; // 成功执行
  191. }
  192. int init_mysql() { // 初始化连接
  193. // init the database connection
  194. g_conn = mysql_init(NULL);
  195. /* connect the database */
  196. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  197. return -1;
  198. // 是否连接已经可用
  199. if (executesql("set names utf8")) // 如果失败
  200. return -1;
  201. return 0; // 返回成功
  202. }
  203. int main(int argc, char **argv) {
  204. cJSON *pJson,*pSub;
  205. int iCount=0;
  206. typedef struct keys_action {
  207. char key[MINI_SIZE];
  208. char type[MINI_SIZE];
  209. char exten[MINI_SIZE];
  210. } KeysObject;
  211. strcpy(g_host_name,getenv("MYSQL"));
  212. strcpy(g_user_name,getenv("MYSQL_USER"));
  213. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  214. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  215. if (init_mysql()){
  216. print_mysql_error(NULL);
  217. exit(1);
  218. }
  219. //读取自动话务员的数据,并写入配置文件
  220. if (executesql(QUERY_IVR_SQL)){
  221. print_mysql_error(NULL);
  222. exit(1);
  223. }
  224. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  225. FILE *conf_ivr_fp = fopen(EXTEN_IVR_FILE, "w+");
  226. if (conf_ivr_fp == NULL){
  227. perror("Open paging conf file Error: ");
  228. exit(1);
  229. }
  230. fprintf(conf_ivr_fp, ";!\n\
  231. ;! Automatically generated configuration file\n\
  232. ;! Filename: extensions_ivr_custom.conf (/etc/asterisk/extensions_ivr_custom.conf)\n\
  233. ;! Generator: Generator IVR\n\
  234. ;! Creation Date: %s\n\
  235. ;!\n\n\
  236. ",\
  237. mytime()\
  238. );
  239. //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
  240. //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
  241. //| id | name | exten | prompt | loops | timeout | language | dialplan | keys_action | createdAt | updatedAt |
  242. //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
  243. //| 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 |
  244. //| 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 |
  245. //| 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 |
  246. //+----+--------------+-------+---------------------------------------+-------+---------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------+---------------------+---------------------+
  247. //3 rows in set (0.00 sec)
  248. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  249. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
  250. printf("some feild is empty!\n");
  251. continue;
  252. }
  253. fprintf(conf_ivr_fp,"\
  254. [voicemenu-custom-%s]\n\
  255. include => %s\n\
  256. exten => %s,1,NoOp(%s)\n",\
  257. g_row[1],\
  258. g_row[6],\
  259. g_row[1],\
  260. g_row[0]\
  261. );
  262. if(g_row[5] != NULL){
  263. fprintf(conf_ivr_fp,"same => n,Set(CHANNEL(language)=%s)\n",g_row[5]);
  264. }
  265. memset(prompt, 0, sizeof(prompt));
  266. strncpy(prompt,g_row[2],strlen(g_row[2])-4);
  267. fprintf(conf_ivr_fp,"\
  268. same => n,Set(COUNT=%s)\n\
  269. same => n(loop),Background(%s)\n",\
  270. g_row[3],\
  271. prompt
  272. );
  273. if(strcmp(g_row[4], "0") != 0 && g_row[4] != NULL){
  274. fprintf(conf_ivr_fp,"same => n,WaitExten(%s)\n",g_row[4]);
  275. }
  276. fprintf(conf_ivr_fp,"\
  277. same => n,Set(COUNT=$[${COUNT}-1])\n\
  278. same => n,GotoIf($[${COUNT} < 0]?:loop)\n\
  279. same => n,WaitExten(1)\n");
  280. KeysObject keysObject[MINI_SIZE];
  281. memset(keysObject, 0, sizeof(keysObject));
  282. if(g_row[7] != NULL){
  283. pJson = cJSON_Parse(g_row[7]);
  284. iCount = cJSON_GetArraySize(pJson);
  285. for(int i = 0;i < iCount;i++){
  286. pSub = cJSON_GetArrayItem(pJson,i);
  287. if(pSub != NULL){
  288. strcpy(keysObject[i].key,cJSON_GetObjectItem(pSub, "key")->valuestring);
  289. strcpy(keysObject[i].type,cJSON_GetObjectItem(pSub, "type")->valuestring);
  290. strcpy(keysObject[i].exten,cJSON_GetObjectItem(pSub, "exten")->valuestring);
  291. if(strcmp(keysObject[i].type, "hangup") == 0){
  292. fprintf(conf_ivr_fp,"exten => %s,1,Hangup()\n",keysObject[i].key);
  293. }
  294. else if(strcmp(keysObject[i].type, "extension") == 0){
  295. fprintf(conf_ivr_fp,"exten => %s,1,Goto(default,%s,1)\n",keysObject[i].key,keysObject[i].exten);
  296. }
  297. else if(strcmp(keysObject[i].type, "ivr") == 0){
  298. fprintf(conf_ivr_fp,"exten => %s,1,Goto(voicemenu-custom-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  299. }
  300. else if(strcmp(keysObject[i].type, "group") == 0){
  301. fprintf(conf_ivr_fp,"exten => %s,1,Goto(paging-group-%s,%s,1)\n",keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  302. }
  303. else if(strcmp(keysObject[i].type, "user") == 0){
  304. int id = 100000 + atoi(keysObject[i].exten);
  305. fprintf(conf_ivr_fp,"exten => %s,1,Goto(manager-queue-%d,s,1)\n",keysObject[i].key,id);
  306. }
  307. }
  308. }
  309. }
  310. fprintf(conf_ivr_fp,"\n\n");
  311. }
  312. /*
  313. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  314. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
  315. printf("some feild is empty!\n");
  316. continue;
  317. }
  318. sprintf(ivrstr,"\
  319. [voicemenu-custom-%s]\n\
  320. include => %s\n\
  321. exten => %s,1,NoOp(%s)",\
  322. g_row[1],\
  323. g_row[6],\
  324. g_row[1],\
  325. g_row[0]\
  326. );
  327. if(g_row[5] != NULL){
  328. sprintf(ivrstr,"%s\nsame => n,Set(CHANNEL(language)=%s)",ivrstr,g_row[5]);
  329. }
  330. memset(prompt, 0, sizeof(prompt));
  331. strncpy(prompt,g_row[2],strlen(g_row[2])-4);
  332. sprintf(ivrstr,"%s\n\
  333. same => n,Set(COUNT=%s)\n\
  334. same => n(loop),Background(%s)",ivrstr,\
  335. g_row[3],\
  336. prompt
  337. );
  338. if(strcmp(g_row[4], "0") != 0 && g_row[4] != NULL){
  339. sprintf(ivrstr,"%s\nsame => n,WaitExten(%s)",ivrstr,g_row[4]);
  340. }
  341. sprintf(ivrstr,"%s\n\
  342. same => n,Set(COUNT=$[${COUNT}-1])\n\
  343. same => n,GotoIf($[${COUNT} < 0]?:loop)\n\
  344. same => n,WaitExten(1)",ivrstr
  345. );
  346. KeysObject keysObject[MINI_SIZE];
  347. memset(keysObject, 0, sizeof(keysObject));
  348. if(g_row[7] != NULL){
  349. pJson = cJSON_Parse(g_row[7]);
  350. iCount = cJSON_GetArraySize(pJson);
  351. for(int i = 0;i < iCount;i++){
  352. pSub = cJSON_GetArrayItem(pJson,i);
  353. if(pSub != NULL){
  354. strcpy(keysObject[i].key,cJSON_GetObjectItem(pSub, "key")->valuestring);
  355. strcpy(keysObject[i].type,cJSON_GetObjectItem(pSub, "type")->valuestring);
  356. strcpy(keysObject[i].exten,cJSON_GetObjectItem(pSub, "exten")->valuestring);
  357. if(strcmp(keysObject[i].type, "hangup") == 0){
  358. sprintf(ivrstr,"%s\nexten => %s,1,Hangup()",ivrstr,keysObject[i].key);
  359. }
  360. else if(strcmp(keysObject[i].type, "extension") == 0){
  361. sprintf(ivrstr,"%s\nexten => %s,1,Goto(default,%s,1)",ivrstr,keysObject[i].key,keysObject[i].exten);
  362. }
  363. else if(strcmp(keysObject[i].type, "ivr") == 0){
  364. sprintf(ivrstr,"%s\nexten => %s,1,Goto(voicemenu-custom-%s,%s,1)",ivrstr,keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  365. }
  366. else if(strcmp(keysObject[i].type, "group") == 0){
  367. sprintf(ivrstr,"%s\nexten => %s,1,Goto(paging-group-%s,%s,1)",ivrstr,keysObject[i].key,keysObject[i].exten,keysObject[i].exten);
  368. }
  369. }
  370. }
  371. }
  372. strcat(ivrstr,"\n\n");
  373. fputs(ivrstr,conf_ivr_fp);
  374. }
  375. /*
  376. [voicemenu-custom-%s]
  377. include = default
  378. exten = _IVR-X.,1,NoOp(%s)
  379. exten = _IVR-X.,n,NoOp(Default language)
  380. exten = _IVR-X.,n,Set(COUNT=3)
  381. exten = _IVR-X.,n(loop),Background(%s)
  382. exten = _IVR-X.,n,Set(COUNT=$[${COUNT}-1])
  383. exten = _IVR-X.,n,WaitExten(%s)
  384. exten = _IVR-X.,n,GotoIf($[${COUNT}>1]?6)
  385. exten = _IVR-X.,n,WaitExten(1)
  386. exten = t,1,Hangup
  387. */
  388. //读取拨号规则的数据,并写入配置文件
  389. if (executesql(QUERY_DIALRULE_SQL)){
  390. print_mysql_error(NULL);
  391. exit(1);
  392. }
  393. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  394. FILE *conf_dialrule_fp = fopen(EXTEN_DIALRULE_FILE, "w+");
  395. if (conf_dialrule_fp == NULL){
  396. perror("Open paging conf file Error: ");
  397. exit(1);
  398. }
  399. fprintf(conf_dialrule_fp, ";!\n\
  400. ;! Automatically generated configuration file\n\
  401. ;! Filename: extensions_dialrule_custom.conf (/etc/asterisk/extensions_dialrule_custom.conf)\n\
  402. ;! Generator: Generator DIALRULE\n\
  403. ;! Creation Date: %s\n\
  404. ;!\n\n\
  405. ",\
  406. mytime()\
  407. );
  408. fputs("[CallingRule_OutCall]\n",conf_dialrule_fp);
  409. //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
  410. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  411. if (g_row[0] == NULL || g_row[1] == NULL){
  412. printf("some feild is empty!\n");
  413. continue;
  414. }
  415. memset(dialrule,0,sizeof(dialrule));
  416. sprintf(dialrule, "exten => _%s,1,Macro(trunkdial-failover,${EXTEN},${%s}",g_row[1], g_row[0]);
  417. if(g_row[3] != NULL){
  418. strcat(dialrule,g_row[3]);
  419. }
  420. strcat(dialrule,"${EXTEN:");
  421. if(g_row[2] != NULL){
  422. strcat(dialrule,g_row[2]);
  423. }
  424. strcat(dialrule,"}");
  425. if(g_row[4] != NULL){
  426. strcat(dialrule,g_row[4]);
  427. }
  428. strcat(dialrule,")\n");
  429. fputs(dialrule,conf_dialrule_fp);
  430. }
  431. //读取全局配置数据,并写入配置文件
  432. if (executesql(QUERY_GLOBAL_SQL)){
  433. print_mysql_error(NULL);
  434. exit(1);
  435. }
  436. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  437. FILE *conf_inbound_fp = fopen(EXTEN_INBOUND_FILE, "w+");
  438. if (conf_inbound_fp == NULL){
  439. perror("Open inbound conf file Error: ");
  440. exit(1);
  441. }
  442. FILE *conf_global_fp = fopen(EXTEN_GLOBAL_FILE, "w+");
  443. if (conf_global_fp == NULL){
  444. perror("Open global conf file Error: ");
  445. exit(1);
  446. }
  447. FILE *conf_calltrigger_fp = fopen(EXTEN_CALLTRIGGER_FILE, "w+");
  448. if (conf_calltrigger_fp == NULL){
  449. perror("Open calltrigger conf file Error: ");
  450. exit(1);
  451. }
  452. FILE *conf_featurecodes_fp = fopen(EXTEN_FEATURECODES_FILE, "w+");
  453. if (conf_featurecodes_fp == NULL){
  454. perror("Open featurecodes conf file Error: ");
  455. exit(1);
  456. }
  457. FILE *conf_sip_nat_fp = fopen(SIP_NAT_FILE, "w+");
  458. if (conf_sip_nat_fp == NULL){
  459. perror("Open sip nat conf file Error: ");
  460. exit(1);
  461. }
  462. FILE *conf_sipsetting_fp = fopen(SIP_SETTINGS_FILE, "w+");
  463. if (conf_sipsetting_fp == NULL){
  464. perror("Open sip settings conf file Error: ");
  465. exit(1);
  466. }
  467. FILE *conf_rtpsetting_fp = fopen(RTP_SETTINGS_FILE, "w+");
  468. if (conf_rtpsetting_fp == NULL){
  469. perror("Open rtp settings conf file Error: ");
  470. exit(1);
  471. }
  472. fprintf(conf_inbound_fp, ";!\n\
  473. ;! Automatically generated configuration file\n\
  474. ;! Filename: extensions_inbound_custom.conf (/etc/asterisk/extensions_inbound_custom.conf)\n\
  475. ;! Generator: Generator INBOUND\n\
  476. ;! Creation Date: %s\n\
  477. ;!\n\n\
  478. ",\
  479. mytime()\
  480. );
  481. fprintf(conf_global_fp, ";!\n\
  482. ;! Automatically generated configuration file\n\
  483. ;! Filename: extensions_global_custom.conf (/etc/asterisk/extensions_global_custom.conf)\n\
  484. ;! Generator: Generator GLOBAL\n\
  485. ;! Creation Date: %s\n\
  486. ;!\n\n\
  487. ",\
  488. mytime()\
  489. );
  490. fprintf(conf_global_fp, "\
  491. AGISERVERHOST = %s\n\
  492. AGISERVERPORT = %s\n\
  493. ",\
  494. getenv("BROADCAST_GATEWAY"),\
  495. getenv("AGI_SERVER_PORT")\
  496. );
  497. fprintf(conf_calltrigger_fp, ";!\n\
  498. ;! Automatically generated configuration file\n\
  499. ;! Filename: extensions_call_trigger_custom.conf (/etc/asterisk/extensions_call_trigger_custom.conf)\n\
  500. ;! Generator: Generator TRIGGER\n\
  501. ;! Creation Date: %s\n\
  502. ;!\n\n\
  503. ",\
  504. mytime()\
  505. );
  506. fprintf(conf_featurecodes_fp, ";!\n\
  507. ;! Automatically generated configuration file\n\
  508. ;! Filename: extensions_featurecodes_custom.conf (/etc/asterisk/extensions_featurecodes_custom.conf)\n\
  509. ;! Generator: Generator FEATURECODES\n\
  510. ;! Creation Date: %s\n\
  511. ;!\n\n\
  512. [featurecodes]\n",\
  513. mytime()\
  514. );
  515. fprintf(conf_sip_nat_fp, ";!\n\
  516. ;! Automatically generated configuration file\n\
  517. ;! Filename: sip_nat.conf (/etc/asterisk/sip_nat.conf)\n\
  518. ;! Generator: Generator SIP NAT\n\
  519. ;! Creation Date: %s\n\
  520. ;!\n\n\
  521. \n",\
  522. mytime()\
  523. );
  524. fprintf(conf_sipsetting_fp, ";!\n\
  525. ;! Automatically generated configuration file\n\
  526. ;! Filename: sip_settings.conf (/etc/asterisk/sip_settings.conf)\n\
  527. ;! Generator: Generator SIP SETTINGS\n\
  528. ;! Creation Date: %s\n\
  529. ;!\n\n\
  530. \n",\
  531. mytime()\
  532. );
  533. fprintf(conf_rtpsetting_fp, ";!\n\
  534. ;! Automatically generated configuration file\n\
  535. ;! Filename: rtp_settings.conf (/etc/asterisk/rtp_settings.conf)\n\
  536. ;! Generator: Generator RTP SETTINGS\n\
  537. ;! Creation Date: %s\n\
  538. ;!\n\n\
  539. \n",\
  540. mytime()\
  541. );
  542. //t_pbx_users_voiptrunk.trunk as trunk,rule,del_prefix,add_before,add_after
  543. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  544. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
  545. printf("some feild is empty!\n");
  546. continue;
  547. }
  548. if(strcmp(g_row[1],"pbx.voip.inbound") == 0){
  549. memset(inboundstr,0,sizeof(inboundstr));
  550. pJson = cJSON_Parse(g_row[2]);
  551. if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "hangup") == 0){
  552. sprintf(inboundstr,"\
  553. [direct-analog]\n\
  554. exten => direct,1,Hangup()\n\
  555. [direct-voip]\n\
  556. exten => direct,1,Hangup()\n"
  557. );
  558. }
  559. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "extension") == 0){
  560. sprintf(inboundstr,"\
  561. [direct-analog]\n\
  562. exten => direct,1,Goto(default,%s,1)\n\
  563. [direct-voip]\n\
  564. exten => direct,1,Goto(default,%s,1)\n",\
  565. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  566. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  567. );
  568. }
  569. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "ivr") == 0){
  570. sprintf(inboundstr,"\
  571. [direct-analog]\n\
  572. exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n\
  573. [direct-voip]\n\
  574. exten => direct,1,Goto(voicemenu-custom-%s,%s,1)\n",\
  575. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  576. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  577. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  578. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  579. );
  580. }
  581. else if(strcmp(cJSON_GetObjectItem(pJson, "type")->valuestring, "group") == 0){
  582. sprintf(inboundstr,"\
  583. [direct-analog]\n\
  584. exten => direct,1,Goto(paging-group-%s,%s,1)\n\
  585. [direct-voip]\n\
  586. exten => direct,1,Goto(paging-group-%s,%s,1)\n",\
  587. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  588. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  589. cJSON_GetObjectItem(pJson, "exten")->valuestring,\
  590. cJSON_GetObjectItem(pJson, "exten")->valuestring\
  591. );
  592. }
  593. fputs(inboundstr,conf_inbound_fp);
  594. }
  595. else if(strcmp(g_row[1],"paging.prompt.config") == 0){
  596. pJson = cJSON_Parse(g_row[2]);
  597. fprintf(conf_global_fp, "\
  598. enPaging_prompt_start = %s\n\
  599. enPaging_prompt_end = %s\n\
  600. ",\
  601. cJSON_GetObjectItem(pJson, "start")->valuestring,\
  602. cJSON_GetObjectItem(pJson, "end")->valuestring\
  603. );
  604. if(cJSON_GetObjectItem(pJson, "startfile") != NULL){
  605. memset(prompt, 0, sizeof(prompt));
  606. if(strcmp(cJSON_GetObjectItem(pJson, "startfile")->valuestring,"start") == 0)
  607. strcpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring);
  608. else
  609. strncpy(prompt,cJSON_GetObjectItem(pJson, "startfile")->valuestring,strlen(cJSON_GetObjectItem(pJson, "startfile")->valuestring)-4);
  610. fprintf(conf_global_fp, "\
  611. Paging_start_file = %s\n\
  612. ",\
  613. prompt\
  614. );
  615. }else{
  616. fprintf(conf_global_fp, "\
  617. Paging_start_file = start\n\
  618. ");
  619. }
  620. }
  621. else if(strcmp(g_row[1],"pbx.ringtime.config") == 0){
  622. pJson = cJSON_Parse(g_row[2]);
  623. if(cJSON_GetObjectItem(pJson, "ringtime") != NULL){
  624. fprintf(conf_global_fp, "\
  625. RINGTIME = %d\n\
  626. ",\
  627. cJSON_GetObjectItem(pJson, "ringtime")->valueint\
  628. );
  629. }else{
  630. fprintf(conf_global_fp, "\
  631. RINGTIME = 30\n\
  632. ");
  633. }
  634. }
  635. else if(strcmp(g_row[1],"pbx.nat.config") == 0){
  636. pJson = cJSON_Parse(g_row[2]);
  637. if(pJson && cJSON_GetObjectItem(pJson, "enable")->valueint == 1){
  638. fprintf(conf_sip_nat_fp, "\
  639. externaddr = %s\n\
  640. externhost = %s\n\
  641. externrefresh = %d\n\
  642. ",\
  643. cJSON_GetObjectItem(pJson, "externaddr")->valuestring,\
  644. cJSON_GetObjectItem(pJson, "externhost")->valuestring,\
  645. cJSON_GetObjectItem(pJson, "externrefresh")->valueint\
  646. );
  647. if(cJSON_GetObjectItem(pJson, "externtcpport") && cJSON_GetObjectItem(pJson, "externtcpport")->valueint != 0)
  648. {
  649. fprintf(conf_sip_nat_fp, "\
  650. externtcpport = %d\n\
  651. ",\
  652. cJSON_GetObjectItem(pJson, "externtcpport")->valueint\
  653. );
  654. }
  655. if(cJSON_GetObjectItem(pJson, "externtlsport") && cJSON_GetObjectItem(pJson, "externtlsport")->valueint != 0)
  656. {
  657. fprintf(conf_sip_nat_fp, "\
  658. externtlsport = %d\n\
  659. ",\
  660. cJSON_GetObjectItem(pJson, "externtlsport")->valueint\
  661. );
  662. }
  663. cJSON *localnetArray = cJSON_GetObjectItem( pJson, "localnet");
  664. if(localnetArray != NULL){
  665. int array_size = cJSON_GetArraySize (localnetArray);
  666. for(int n = 0; n < array_size; n++){
  667. pSub = cJSON_GetArrayItem(localnetArray, n);
  668. if(NULL == pSub ){ continue ; }
  669. fprintf(conf_sip_nat_fp, "\
  670. localnet = %s\n\
  671. ",\
  672. pSub->valuestring\
  673. );
  674. }
  675. }
  676. }
  677. }
  678. else if(strcmp(g_row[1],"paging.record.config") == 0){
  679. pJson = cJSON_Parse(g_row[2]);
  680. if(cJSON_GetObjectItem(pJson, "paging_record") != NULL){
  681. fprintf(conf_global_fp, "\
  682. PAGING_RECORD = %s\n\
  683. ",\
  684. cJSON_GetObjectItem(pJson, "paging_record")->valuestring\
  685. );
  686. }
  687. if(cJSON_GetObjectItem(pJson, "intercom_record") != NULL){
  688. fprintf(conf_global_fp, "\
  689. INTERCOM_RECORD = %s\n\
  690. ",\
  691. cJSON_GetObjectItem(pJson, "intercom_record")->valuestring\
  692. );
  693. }
  694. if(cJSON_GetObjectItem(pJson, "conference_record") != NULL){
  695. fprintf(conf_global_fp, "\
  696. CONFERENCE_RECORD = %s\n\
  697. ",\
  698. cJSON_GetObjectItem(pJson, "conference_record")->valuestring\
  699. );
  700. }
  701. }
  702. else if(strcmp(g_row[1],"pbx.autoanswer.config") == 0){
  703. pJson = cJSON_Parse(g_row[2]);
  704. if(cJSON_GetObjectItem(pJson, "intercom_autoanswer") != NULL){
  705. fprintf(conf_global_fp, "\
  706. INTERCOM_AUTO = %s\n\
  707. ",\
  708. cJSON_GetObjectItem(pJson, "intercom_autoanswer")->valuestring\
  709. );
  710. }
  711. else
  712. {
  713. fprintf(conf_global_fp, "\
  714. INTERCOM_AUTO = yes\n\
  715. ");
  716. }
  717. if(cJSON_GetObjectItem(pJson, "paging_autoanswer") != NULL){
  718. fprintf(conf_global_fp, "\
  719. PAGING_AUTO = %s\n\
  720. ",\
  721. cJSON_GetObjectItem(pJson, "paging_autoanswer")->valuestring\
  722. );
  723. }
  724. else
  725. {
  726. fprintf(conf_global_fp, "\
  727. PAGING_AUTO = yes\n\
  728. ");
  729. }
  730. }
  731. else if(strcmp(g_row[1],"paging.calltrigger.config") == 0){
  732. pJson = cJSON_Parse(g_row[2]);
  733. fprintf(conf_calltrigger_fp, "\
  734. exten => _%s.,1,Macro(calltrigger,${EXTEN})\n\
  735. exten => _%s.,1,Macro(calltrigger,${EXTEN})\n\
  736. ",\
  737. cJSON_GetObjectItem(pJson, "start")->valuestring,\
  738. cJSON_GetObjectItem(pJson, "stop")->valuestring\
  739. );
  740. }
  741. else if(strcmp(g_row[1],"pbx.sipsettings.config") == 0){
  742. pJson = cJSON_Parse(g_row[2]);
  743. if(cJSON_GetObjectItem(pJson, "udp"))
  744. {
  745. fprintf(conf_sipsetting_fp, "udpbindaddr = 0.0.0.0:%d\n",cJSON_GetObjectItem(pJson, "udp")->valueint);
  746. }
  747. pSub = cJSON_GetObjectItem(pJson, "tcp");
  748. if(pSub && cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  749. fprintf(conf_sipsetting_fp, "\
  750. tcpenable = yes\n\
  751. tcpbindaddr = 0.0.0.0:%d\n\
  752. ",\
  753. cJSON_GetObjectItem(pSub, "port")->valueint\
  754. );
  755. }
  756. pSub = cJSON_GetObjectItem(pJson, "tls");
  757. if(pSub && cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  758. fprintf(conf_sipsetting_fp, "\
  759. tlsenable = yes\n\
  760. tlsbindaddr = 0.0.0.0:%d\n\
  761. ",\
  762. cJSON_GetObjectItem(pSub, "port")->valueint\
  763. );
  764. }
  765. pSub = cJSON_GetObjectItem(pJson, "rtp");
  766. if(pSub){
  767. fprintf(conf_rtpsetting_fp, "\
  768. rtpstart = %d\n\
  769. rtpend = %d\n\
  770. ",\
  771. cJSON_GetObjectItem(pSub, "start_port")->valueint,\
  772. cJSON_GetObjectItem(pSub, "end_port")->valueint\
  773. );
  774. }
  775. }
  776. else if(strcmp(g_row[1],"paging.featurecodes.config") == 0){
  777. pJson = cJSON_Parse(g_row[2]);
  778. pSub = cJSON_GetObjectItem(pJson, "bargein");
  779. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  780. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Macro(spy-barge,${EXTEN:%ld},${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  781. }
  782. pSub = cJSON_GetObjectItem(pJson, "clear");
  783. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  784. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Macro(exten-clear,${EXTEN:%ld},${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  785. }
  786. pSub = cJSON_GetObjectItem(pJson, "syp");
  787. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  788. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Macro(spy-normal,${EXTEN:%ld},${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  789. }
  790. pSub = cJSON_GetObjectItem(pJson, "whisper");
  791. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  792. fprintf(conf_featurecodes_fp, "exten = _%s.,1,Macro(spy-whisper,${EXTEN:%ld},${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring,strlen(cJSON_GetObjectItem(pSub, "code")->valuestring));
  793. }
  794. pSub = cJSON_GetObjectItem(pJson, "wakeup");
  795. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  796. fprintf(conf_featurecodes_fp, "exten = %s,1,Macro(wakeup-call,${CALLERID(num)})\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  797. }
  798. pSub = cJSON_GetObjectItem(pJson, "dnd");
  799. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  800. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-dnd-on,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  801. }
  802. pSub = cJSON_GetObjectItem(pJson, "cf-alway");
  803. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  804. 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));
  805. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cf-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  806. }
  807. pSub = cJSON_GetObjectItem(pJson, "cf-busy");
  808. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  809. 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));
  810. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cfb-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  811. }
  812. pSub = cJSON_GetObjectItem(pJson, "cf-noanswer");
  813. if(cJSON_GetObjectItem(pSub, "enable")->valueint == 1){
  814. 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));
  815. fprintf(conf_featurecodes_fp, "exten = %s,1,Goto(app-cfu-off,s,1)\n",cJSON_GetObjectItem(pSub, "code")->valuestring);
  816. }
  817. }
  818. }
  819. fclose(conf_dialrule_fp);
  820. fclose(conf_ivr_fp);
  821. fclose(conf_inbound_fp);
  822. fclose(conf_global_fp);
  823. fclose(conf_calltrigger_fp);
  824. fclose(conf_featurecodes_fp);
  825. fclose(conf_sip_nat_fp);
  826. fclose(conf_sipsetting_fp);
  827. mysql_free_result(g_res); // 释放结果集
  828. mysql_close(g_conn); // 关闭链接
  829. cJSON_Delete(pJson);
  830. }