generate_group_conf.c.acorp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. ============================================================================
  3. Name : generate_paging_conf.sh
  4. Author : ssc
  5. Version : v1.0
  6. Copyright : ZYCOO copyright
  7. Description : Generate paging info from mysql to paging conf file
  8. ============================================================================
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <assert.h>
  15. #include <time.h>
  16. #include <ctype.h>
  17. #include <cjson/cJSON.h>
  18. #include <mysql/mysql.h>
  19. MYSQL *g_conn; // mysql 连接
  20. MYSQL_RES *g_res; // mysql group记录集
  21. MYSQL_ROW g_row; // 字符串数组,mysql 记录行
  22. MYSQL_RES *d_res; // mysql device记录集
  23. MYSQL_ROW d_row; // 字符串数组,mysql 记录行
  24. #define MAX_TRUNK_SIZE 256
  25. #define MAX_SIZE 5120
  26. #define MIDLE_SIZE 512
  27. #define MINI_SIZE 64
  28. #define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
  29. #define EXTEN_PAGING_FILE "/etc/asterisk/extensions_paging_custom.conf"
  30. #define EXTEN_EXTENS_FILE "/etc/asterisk/extensions_extens_custom.conf"
  31. #define EXTEN_IPPHONES_FILE "/etc/asterisk/extensions_phones_custom.conf"
  32. #define KEYVALLEN 100
  33. #define VERSION "V1.0.1"
  34. #define QUERY_PAGING_GROUP_SQL "select id,name,exten,paging_mode from t_paging_groups"
  35. char g_host_name[MINI_SIZE];
  36. char g_user_name[MINI_SIZE];
  37. char g_password[MINI_SIZE];
  38. char g_db_name[MINI_SIZE];
  39. const unsigned int g_db_port = 3306;
  40. char sql_tmp[MIDLE_SIZE];
  41. char exten_tmp[MAX_SIZE];
  42. char dest_tmp[MAX_SIZE];
  43. char page_option[MINI_SIZE];
  44. //读取配置文件函数----功能:删除左边空格
  45. char *l_trim(char *szOutput, const char *szInput)
  46. {
  47. assert(szInput != NULL);
  48. assert(szOutput != NULL);
  49. assert(szOutput != szInput);
  50. for (NULL; *szInput != '\0' && isspace(*szInput); ++szInput)
  51. {
  52. ;
  53. }
  54. return strcpy(szOutput, szInput);
  55. }
  56. // 删除右边的空格
  57. char *r_trim(char *szOutput, const char *szInput)
  58. {
  59. char *p = NULL;
  60. assert(szInput != NULL);
  61. assert(szOutput != NULL);
  62. assert(szOutput != szInput);
  63. strcpy(szOutput, szInput);
  64. for(p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  65. {
  66. ;
  67. }
  68. *(++p) = '\0';
  69. return szOutput;
  70. }
  71. // 删除两边的空格
  72. char *a_trim(char *szOutput, const char *szInput)
  73. {
  74. char *p = NULL;
  75. assert(szInput != NULL);
  76. assert(szOutput != NULL);
  77. l_trim(szOutput, szInput);
  78. for (p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  79. {
  80. ;
  81. }
  82. *(++p) = '\0';
  83. return szOutput;
  84. }
  85. //main函数接口 参数1:配置文件路径 参数2:配置文件的那一部分,如general 参数3:键名 参数4:键值
  86. int GetProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal )
  87. {
  88. char appname[32], keyname[32];
  89. char *buf, *c;
  90. char buf_i[KEYVALLEN], buf_o[KEYVALLEN];
  91. FILE *fp;
  92. int found = 0; /* 1 AppName 2 KeyName */
  93. if( (fp = fopen( profile, "r" )) == NULL )
  94. {
  95. printf( "openfile [%s] error [%s]\n", profile, strerror(errno) );
  96. return(-1);
  97. }
  98. fseek( fp, 0, SEEK_SET );
  99. memset( appname, 0, sizeof(appname) );
  100. sprintf( appname, "[%s]", AppName );
  101. while( !feof(fp) && fgets( buf_i, KEYVALLEN, fp ) != NULL )
  102. {
  103. l_trim(buf_o, buf_i);
  104. if( strlen(buf_o) <= 0 )
  105. continue;
  106. buf = NULL;
  107. buf = buf_o;
  108. if( found == 0 )
  109. {
  110. if( buf[0] != '[' )
  111. {
  112. continue;
  113. }
  114. else if ( strncmp(buf, appname, strlen(appname)) == 0 )
  115. {
  116. found = 1;
  117. continue;
  118. }
  119. }
  120. else if( found == 1 )
  121. {
  122. if( buf[0] == '#' )
  123. {
  124. continue;
  125. }
  126. else if ( buf[0] == '[' )
  127. {
  128. break;
  129. }
  130. else
  131. {
  132. if( (c = (char *)strchr(buf, '=')) == NULL )
  133. continue;
  134. memset( keyname, 0, sizeof(keyname) );
  135. sscanf( buf, "%[^=|^ |^\t]", keyname );
  136. if( strcmp(keyname, KeyName) == 0 )
  137. {
  138. sscanf( ++c, "%[^\n]", KeyVal );
  139. char *KeyVal_o = (char *)malloc(strlen(KeyVal) + 1);
  140. if(KeyVal_o != NULL)
  141. {
  142. memset(KeyVal_o, 0, sizeof(KeyVal_o));
  143. a_trim(KeyVal_o, KeyVal);
  144. if(KeyVal_o && strlen(KeyVal_o) > 0)
  145. strcpy(KeyVal, KeyVal_o);
  146. free(KeyVal_o);
  147. KeyVal_o = NULL;
  148. }
  149. found = 2;
  150. break;
  151. }
  152. else
  153. {
  154. continue;
  155. }
  156. }
  157. }
  158. }
  159. fclose( fp );
  160. if( found == 2 )
  161. return(0);
  162. else
  163. return(-1);
  164. }
  165. char * mytime(){
  166. time_t my_time;
  167. time(&my_time);
  168. char *time_string = ctime(&my_time);
  169. if (time_string[strlen(time_string) - 1] == '\n')
  170. {
  171. time_string[strlen(time_string) - 1] = '\0';
  172. }
  173. return time_string;
  174. }
  175. void print_mysql_error(const char *msg) { // 打印最后一次错误
  176. if (msg)
  177. printf("%s: %s\n", msg, mysql_error(g_conn));
  178. else
  179. puts(mysql_error(g_conn));
  180. }
  181. int executesql(const char * sql) {
  182. /*query the database according the sql*/
  183. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  184. return -1; // 表示失败
  185. return 0; // 成功执行
  186. }
  187. int init_mysql() { // 初始化连接
  188. // init the database connection
  189. g_conn = mysql_init(NULL);
  190. /* connect the database */
  191. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  192. return -1;
  193. // 是否连接已经可用
  194. if (executesql("set names utf8")) // 如果失败
  195. return -1;
  196. return 0; // 返回成功
  197. }
  198. int main(int argc, char **argv) {
  199. /*
  200. memset(g_host_name, 0, sizeof(g_host_name));
  201. memset(g_user_name, 0, sizeof(g_user_name));
  202. memset(g_password, 0, sizeof(g_password));
  203. memset(g_db_name, 0, sizeof(g_db_name));
  204. GetProfileString(MYSQL_CONNECT_CONF, "general", "dbserverip", g_host_name);
  205. GetProfileString(MYSQL_CONNECT_CONF, "general", "dbuser", g_user_name);
  206. GetProfileString(MYSQL_CONNECT_CONF, "general", "dbpasswd", g_password);
  207. GetProfileString(MYSQL_CONNECT_CONF, "general", "dbname", g_db_name);
  208. */
  209. strcpy(g_host_name,getenv("MYSQL"));
  210. strcpy(g_user_name,getenv("MYSQL_USER"));
  211. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  212. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  213. if (init_mysql()){
  214. print_mysql_error(NULL);
  215. exit(1);
  216. }
  217. if (executesql(QUERY_PAGING_GROUP_SQL)){
  218. print_mysql_error(NULL);
  219. exit(1);
  220. }
  221. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  222. FILE *conf_paging_fp = fopen(EXTEN_PAGING_FILE, "w+");
  223. FILE *conf_extens_fp = fopen(EXTEN_EXTENS_FILE, "w+");
  224. FILE *conf_ipphones_fp = fopen(EXTEN_IPPHONES_FILE, "w+");
  225. if (conf_paging_fp == NULL){
  226. perror("Open paging conf file Error: ");
  227. exit(1);
  228. }
  229. fprintf(conf_paging_fp, ";!\n\
  230. ;! Automatically generated configuration file\n\
  231. ;! Filename: extensions_paging_custom.conf (/etc/asterisk/extensions_paging_custom.conf)\n\
  232. ;! Generator: Generator Paging\n\
  233. ;! Creation Date: %s\n\
  234. ;!\n\n\
  235. ",\
  236. mytime()\
  237. );
  238. if (conf_extens_fp == NULL){
  239. perror("Open extens conf file Error: ");
  240. exit(1);
  241. }
  242. fprintf(conf_extens_fp, ";!\n\
  243. ;! Automatically generated configuration file\n\
  244. ;! Filename: extensions_extens_custom.conf (/etc/asterisk/extensions_extens_custom.conf)\n\
  245. ;! Generator: Generator Extens\n\
  246. ;! Creation Date: %s\n\
  247. ;!\n\n\
  248. ",\
  249. mytime()\
  250. );
  251. if (conf_ipphones_fp == NULL){
  252. perror("Open ipphones conf file Error: ");
  253. exit(1);
  254. }
  255. fprintf(conf_ipphones_fp, ";!\n\
  256. ;! Automatically generated configuration file\n\
  257. ;! Filename: extensions_phones_custom.conf (/etc/asterisk/extensions_phones_custom.conf)\n\
  258. ;! Generator: Generator phones\n\
  259. ;! Creation Date: %s\n\
  260. ;!\n\n\
  261. ",\
  262. mytime()\
  263. );
  264. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  265. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL || g_row[3] == NULL){
  266. printf("some feild is empty!\n");
  267. continue;
  268. }
  269. fprintf(conf_extens_fp, "[extens-group-%s]\n",g_row[2]);
  270. fprintf(conf_ipphones_fp, "[phones-group-%s]\n",g_row[2]);
  271. memset(sql_tmp,0,sizeof(sql_tmp));
  272. sprintf(sql_tmp,"select exten,type_id,allowed_pa from t_paging_deviceGroups JOIN t_paging_devices on t_paging_devices.id = t_paging_deviceGroups.DeviceId\
  273. where GroupId = %s and t_paging_devices.type_id in('1','2','3','5')",g_row[0]);
  274. if (executesql(sql_tmp)){
  275. print_mysql_error(NULL);
  276. exit(1);
  277. }
  278. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  279. memset(exten_tmp,0,sizeof(exten_tmp));
  280. memset(dest_tmp,0,sizeof(dest_tmp));
  281. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  282. if (d_row[0] == NULL || d_row[1] == NULL){
  283. printf("some feild is empty!\n");
  284. continue;
  285. }
  286. //sprintf(exten_tmp, "%sSIP/%s&", exten_tmp, d_row[0]);
  287. //sprintf(dest_tmp, "%s%s|", dest_tmp, d_row[0]);
  288. strcat(exten_tmp,"SIP/");
  289. strcat(exten_tmp,d_row[0]);
  290. strcat(exten_tmp,"&");
  291. strcat(dest_tmp,d_row[0]);
  292. strcat(dest_tmp,"|");
  293. int id = atoi(d_row[1]);
  294. switch(id){
  295. case 1:
  296. fprintf(conf_extens_fp, "exten => %s,1,Macro(page,%s,SIP/%s)\n",d_row[0],d_row[0],d_row[0]);
  297. break;
  298. case 2:
  299. fprintf(conf_extens_fp, "exten => %s,1,Macro(intercom,%s,SIP/%s)\n",d_row[0],d_row[0],d_row[0]);
  300. break;
  301. case 3:
  302. case 7:
  303. fprintf(conf_ipphones_fp, "exten => %s,1,Macro(stdexten,%s,SIP/%s)\n",d_row[0],d_row[0],d_row[0]);
  304. break;
  305. }
  306. }
  307. if(strlen(exten_tmp) > 0){
  308. exten_tmp[strlen(exten_tmp) - 1] = '\0';
  309. dest_tmp[strlen(dest_tmp) - 1] = '\0';
  310. }
  311. fprintf(conf_extens_fp, "\n");
  312. memset(page_option, 0, sizeof(page_option));
  313. if(strcmp(g_row[3],"duplex") == 0){
  314. strcat(page_option,"d");
  315. }
  316. fprintf(conf_paging_fp, "\
  317. [paging-group-%s]\n\
  318. exten => %s,1,NoOp(%s)\n\
  319. same => n,MSet(__SRCEXTEN=${CALLERID(num)},__DESTS=%s,DATE=${STRFTIME(${EPOCH},,%%Y%%m%%d)},__UUID=${UNIQUEID})\n\
  320. same => n,GotoIf($[\"foo${PAGING_RECORD}\" != \"fooyes\"]?unrc)\n\
  321. same => n,System(/bin/sh /etc/scripts/shell_scripts.sh mkrcdir paging ${DATE})\n\
  322. same => n,Set(FILENAME=paging/${DATE}/paging-${SRCEXTEN}-${UUID}.wav)\n\
  323. same => n,MixMonitor(${FILENAME},b)\n\
  324. same => n(unrc),Wait(0.5)\n\
  325. same => n,Set(__CONFNO=${EXTEN})\n\
  326. same => n,MSet(startT=${STRFTIME(${EPOCH},,%%s)},__CALLEE=${DESTS},__calltype=paging)\n\
  327. same => n,MSet(CONFBRIDGE(user,quiet)=yes,CONFBRIDGE(user,marked)=yes)\n\
  328. same => n,AGI(joinmeetme.agi,${CONFNO},${SRCEXTEN},${DESTS})\n\
  329. same => n,ConfBridge(${CONFNO})\n\
  330. same => n,Hangup\n\
  331. exten => h,1,Goto(checkmeetme,s,1)\
  332. \n\n", \
  333. g_row[2],\
  334. g_row[2],\
  335. g_row[1],\
  336. dest_tmp\
  337. );
  338. }
  339. fclose(conf_paging_fp);
  340. fclose(conf_extens_fp);
  341. mysql_free_result(g_res); // 释放结果集
  342. mysql_free_result(d_res);
  343. mysql_close(g_conn); // 关闭链接
  344. }