generate_user_conf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 2048
  26. #define MIDLE_SIZE 512
  27. #define MINI_SIZE 64
  28. #define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
  29. #define EXTEN_USER_QUEUE_FILE "/etc/asterisk/extensions_users_queue_custom.conf"
  30. #define QUEUES_FILE "/etc/asterisk/queues.conf"
  31. #define KEYVALLEN 100
  32. #define VERSION "V1.0.1"
  33. #define QUERY_PAGING_USER_SQL "select id,phones,strategy,ring_duration,noanswer_dest from t_paging_users"
  34. char g_host_name[MINI_SIZE];
  35. char g_user_name[MINI_SIZE];
  36. char g_password[MINI_SIZE];
  37. char g_db_name[MINI_SIZE];
  38. const unsigned int g_db_port = 3306;
  39. char sql_tmp[MIDLE_SIZE];
  40. char exten_tmp[MAX_SIZE];
  41. //读取配置文件函数----功能:删除左边空格
  42. char *l_trim(char *szOutput, const char *szInput)
  43. {
  44. assert(szInput != NULL);
  45. assert(szOutput != NULL);
  46. assert(szOutput != szInput);
  47. for (NULL; *szInput != '\0' && isspace(*szInput); ++szInput)
  48. {
  49. ;
  50. }
  51. return strcpy(szOutput, szInput);
  52. }
  53. // 删除右边的空格
  54. char *r_trim(char *szOutput, const char *szInput)
  55. {
  56. char *p = NULL;
  57. assert(szInput != NULL);
  58. assert(szOutput != NULL);
  59. assert(szOutput != szInput);
  60. strcpy(szOutput, szInput);
  61. for(p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  62. {
  63. ;
  64. }
  65. *(++p) = '\0';
  66. return szOutput;
  67. }
  68. // 删除两边的空格
  69. char *a_trim(char *szOutput, const char *szInput)
  70. {
  71. char *p = NULL;
  72. assert(szInput != NULL);
  73. assert(szOutput != NULL);
  74. l_trim(szOutput, szInput);
  75. for (p = szOutput + strlen(szOutput) - 1; p >= szOutput && isspace(*p); --p)
  76. {
  77. ;
  78. }
  79. *(++p) = '\0';
  80. return szOutput;
  81. }
  82. //main函数接口 参数1:配置文件路径 参数2:配置文件的那一部分,如general 参数3:键名 参数4:键值
  83. int GetProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal )
  84. {
  85. char appname[32], keyname[32];
  86. char *buf, *c;
  87. char buf_i[KEYVALLEN], buf_o[KEYVALLEN];
  88. FILE *fp;
  89. int found = 0; /* 1 AppName 2 KeyName */
  90. if( (fp = fopen( profile, "r" )) == NULL )
  91. {
  92. printf( "openfile [%s] error [%s]\n", profile, strerror(errno) );
  93. return(-1);
  94. }
  95. fseek( fp, 0, SEEK_SET );
  96. memset( appname, 0, sizeof(appname) );
  97. sprintf( appname, "[%s]", AppName );
  98. while( !feof(fp) && fgets( buf_i, KEYVALLEN, fp ) != NULL )
  99. {
  100. l_trim(buf_o, buf_i);
  101. if( strlen(buf_o) <= 0 )
  102. continue;
  103. buf = NULL;
  104. buf = buf_o;
  105. if( found == 0 )
  106. {
  107. if( buf[0] != '[' )
  108. {
  109. continue;
  110. }
  111. else if ( strncmp(buf, appname, strlen(appname)) == 0 )
  112. {
  113. found = 1;
  114. continue;
  115. }
  116. }
  117. else if( found == 1 )
  118. {
  119. if( buf[0] == '#' )
  120. {
  121. continue;
  122. }
  123. else if ( buf[0] == '[' )
  124. {
  125. break;
  126. }
  127. else
  128. {
  129. if( (c = (char *)strchr(buf, '=')) == NULL )
  130. continue;
  131. memset( keyname, 0, sizeof(keyname) );
  132. sscanf( buf, "%[^=|^ |^\t]", keyname );
  133. if( strcmp(keyname, KeyName) == 0 )
  134. {
  135. sscanf( ++c, "%[^\n]", KeyVal );
  136. char *KeyVal_o = (char *)malloc(strlen(KeyVal) + 1);
  137. if(KeyVal_o != NULL)
  138. {
  139. memset(KeyVal_o, 0, sizeof(KeyVal_o));
  140. a_trim(KeyVal_o, KeyVal);
  141. if(KeyVal_o && strlen(KeyVal_o) > 0)
  142. strcpy(KeyVal, KeyVal_o);
  143. free(KeyVal_o);
  144. KeyVal_o = NULL;
  145. }
  146. found = 2;
  147. break;
  148. }
  149. else
  150. {
  151. continue;
  152. }
  153. }
  154. }
  155. }
  156. fclose( fp );
  157. if( found == 2 )
  158. return(0);
  159. else
  160. return(-1);
  161. }
  162. char * mytime(){
  163. time_t my_time;
  164. time(&my_time);
  165. char *time_string = ctime(&my_time);
  166. if (time_string[strlen(time_string) - 1] == '\n')
  167. {
  168. time_string[strlen(time_string) - 1] = '\0';
  169. }
  170. return time_string;
  171. }
  172. void print_mysql_error(const char *msg) { // 打印最后一次错误
  173. if (msg)
  174. printf("%s: %s\n", msg, mysql_error(g_conn));
  175. else
  176. puts(mysql_error(g_conn));
  177. }
  178. int executesql(const char * sql) {
  179. /*query the database according the sql*/
  180. if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失败
  181. return -1; // 表示失败
  182. return 0; // 成功执行
  183. }
  184. int init_mysql() { // 初始化连接
  185. // init the database connection
  186. g_conn = mysql_init(NULL);
  187. /* connect the database */
  188. if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失败
  189. return -1;
  190. // 是否连接已经可用
  191. if (executesql("set names utf8")) // 如果失败
  192. return -1;
  193. return 0; // 返回成功
  194. }
  195. int main(int argc, char **argv) {
  196. cJSON *pJson,*pSub,*dJson;
  197. int iCount=0;
  198. char noanswer_dest[64];
  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_USER_SQL)){
  218. print_mysql_error(NULL);
  219. exit(1);
  220. }
  221. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  222. FILE *conf_user_queue_fp = fopen(EXTEN_USER_QUEUE_FILE, "w+");
  223. FILE *conf_queues_fp = fopen(QUEUES_FILE, "w+");
  224. if (conf_user_queue_fp == NULL){
  225. perror("Open paging conf file Error: ");
  226. exit(1);
  227. }
  228. fprintf(conf_user_queue_fp, ";!\n\
  229. ;! Automatically generated configuration file\n\
  230. ;! Filename: extensions_users_queue_custom.conf (/etc/asterisk/extensions_users_queue_custom.conf)\n\
  231. ;! Generator: Generator Paging\n\
  232. ;! Creation Date: %s\n\
  233. ;!\n\n\
  234. ",\
  235. mytime()\
  236. );
  237. if (conf_queues_fp == NULL){
  238. perror("Open paging conf file Error: ");
  239. exit(1);
  240. }
  241. fprintf(conf_queues_fp, ";!\n\
  242. ;! Automatically generated configuration file\n\
  243. ;! Filename: queues.conf (/etc/asterisk/queues.conf)\n\
  244. ;! Generator: Generator Paging\n\
  245. ;! Creation Date: %s\n\
  246. ;!\n\n\
  247. \n\
  248. [general]\n\
  249. persistentmembers = yes\n\
  250. \n\
  251. ",\
  252. mytime()\
  253. );
  254. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  255. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL){
  256. printf("some feild is empty!\n");
  257. continue;
  258. }
  259. if(g_row[1] != NULL){
  260. pJson = cJSON_Parse(g_row[1]);
  261. iCount = cJSON_GetArraySize(pJson);
  262. if(iCount > 0){
  263. int q = 100000 + atoi(g_row[0]);
  264. fprintf(conf_user_queue_fp, "[manager-queue-%d]\n", q);
  265. fprintf(conf_queues_fp, "\
  266. [Q%d]\n\
  267. setinterfacevar = yes\n\
  268. setqueueentryvar = yes\n\
  269. strategy = %s\n\
  270. timeout = 30\n\
  271. wrapuptime = 0\n\
  272. autofill = yes\n\
  273. autopause = no\n\
  274. ringinuse = no\n\
  275. maxlen = 8\n\
  276. context = queue-custom\n\
  277. joinempty = no\n\
  278. leavewhenempty = paused,unavailable,invalid,unknown\n\
  279. periodic-announce-frequency = 0\n\
  280. reportholdtime = no\n\
  281. announce-frequency = 0\n\
  282. announce-holdtime = no\n\
  283. announce-position = no\n\
  284. queue-youarenext =\n\
  285. queue-callswaiting =\n\
  286. queue-holdtime =\n\
  287. queue-minutes =\n\
  288. queue-thankyou =\n\
  289. musicclass = queuemusic\n\
  290. \n",q,g_row[2]);
  291. dJson = cJSON_Parse(g_row[4]);
  292. memset(noanswer_dest,0,sizeof(noanswer_dest));
  293. if(dJson)
  294. {
  295. if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "hangup") == 0){
  296. strcpy(noanswer_dest,"Goto(hangup,s,1)");
  297. }
  298. else if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "extension") == 0){
  299. sprintf(noanswer_dest,"Goto(default,%s,1)",cJSON_GetObjectItem(dJson, "exten")->valuestring);
  300. }
  301. else if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "user") == 0){
  302. int id = 100000 + cJSON_GetObjectItem(dJson, "id")->valueint;
  303. sprintf(noanswer_dest,"Goto(manager-queue-%d,s,1)",id);
  304. }
  305. else if(strcmp(cJSON_GetObjectItem(dJson, "type")->valuestring, "outcall") == 0){
  306. sprintf(noanswer_dest,"Goto(CallingRule_OutCall,%s,1)",cJSON_GetObjectItem(dJson, "exten")->valuestring);
  307. }
  308. }
  309. else
  310. {
  311. strcpy(noanswer_dest,"Goto(hangup,s,1)");
  312. }
  313. printf("parse success\n");
  314. if(iCount > 0)
  315. {
  316. if(strcmp(g_row[2],"ringall") == 0){
  317. for(int i = 0;i < iCount;i++){
  318. pSub = cJSON_GetArrayItem(pJson,i);
  319. if(pSub != NULL){
  320. fprintf(conf_queues_fp, "member => SIP/%s\n",pSub->valuestring);
  321. fprintf(conf_user_queue_fp, "exten => %s,1,Macro(queue,Q%d,${EXTEN},%s)\n", pSub->valuestring,q,g_row[3]);
  322. fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest);
  323. }
  324. }
  325. fprintf(conf_user_queue_fp, "exten => s,1,Macro(queue,Q%d,%s,%s)\n", q, pSub->valuestring,g_row[3]);
  326. fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest);
  327. }else{
  328. for(int i = 0;i < iCount;i++){
  329. pSub = cJSON_GetArrayItem(pJson,i);
  330. if(pSub != NULL){
  331. fprintf(conf_queues_fp, "member => SIP/%s,%d\n",pSub->valuestring,i);
  332. fprintf(conf_user_queue_fp, "exten => %s,1,Macro(queue,Q%d,${EXTEN},%s)\n", pSub->valuestring,q,g_row[3]);
  333. fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest);
  334. }
  335. }
  336. fprintf(conf_user_queue_fp, "exten => s,1,Macro(queue,Q%d,%s,%s)\n", q, pSub->valuestring,g_row[3]);
  337. fprintf(conf_user_queue_fp, "same => n,%s\n", noanswer_dest);
  338. }
  339. }
  340. }
  341. }
  342. }
  343. fclose(conf_queues_fp);
  344. fclose(conf_user_queue_fp);
  345. mysql_free_result(g_res); // 释放结果集
  346. mysql_free_result(d_res);
  347. mysql_close(g_conn); // 关闭链接
  348. cJSON_Delete(dJson);
  349. cJSON_Delete(pJson);
  350. }