generate_group_conf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. MYSQL_RES *r_res; // mysql device记录集
  25. MYSQL_ROW r_row; // 字符串数组,mysql 记录行
  26. #define MAX_TRUNK_SIZE 256
  27. #define MAX_SIZE 20480
  28. #define MIDLE_SIZE 512
  29. #define MINI_SIZE 64
  30. #define MYSQL_CONNECT_CONF "/etc/asterisk/exten_gen.ini"
  31. #define EXTEN_PAGING_FILE "/etc/asterisk/extensions_paging_custom.conf"
  32. #define EXTEN_EXTENS_FILE "/etc/asterisk/extensions_extens_custom.conf"
  33. #define EXTEN_IPPHONES_FILE "/etc/asterisk/extensions_phones_custom.conf"
  34. #define EXTEN_HINTS_FILE "/etc/asterisk/extensions_hints_custom.conf"
  35. #define KEYVALLEN 100
  36. #define VERSION "V1.0.1"
  37. #define QUERY_PAGING_GROUP_SQL "select id,name,exten,paging_mode,paging_volume from t_paging_groups"
  38. char g_host_name[MINI_SIZE];
  39. char g_user_name[MINI_SIZE];
  40. char g_password[MINI_SIZE];
  41. char g_db_name[MINI_SIZE];
  42. const unsigned int g_db_port = 3306;
  43. char sql_tmp[MIDLE_SIZE];
  44. char exten_tmp[MAX_SIZE];
  45. char dest_tmp[MAX_SIZE];
  46. char page_option[MINI_SIZE];
  47. char page_volume[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 = NULL;
  83. memset(g_host_name, 0, sizeof(g_host_name));
  84. memset(g_user_name, 0, sizeof(g_user_name));
  85. memset(g_password, 0, sizeof(g_password));
  86. memset(g_db_name, 0, sizeof(g_db_name));
  87. strcpy(g_host_name,getenv("MYSQL"));
  88. strcpy(g_user_name,getenv("MYSQL_USER"));
  89. strcpy(g_password,getenv("MYSQL_PASSWORD"));
  90. strcpy(g_db_name,getenv("MYSQL_DATABASE"));
  91. if (init_mysql()){
  92. print_mysql_error(NULL);
  93. exit(1);
  94. }
  95. if (executesql(QUERY_PAGING_GROUP_SQL)){
  96. print_mysql_error(NULL);
  97. exit(1);
  98. }
  99. g_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  100. FILE *conf_paging_fp = fopen(EXTEN_PAGING_FILE, "w+");
  101. FILE *conf_extens_fp = fopen(EXTEN_EXTENS_FILE, "w+");
  102. FILE *conf_ipphones_fp = fopen(EXTEN_IPPHONES_FILE, "w+");
  103. FILE *conf_hints_fp = fopen(EXTEN_HINTS_FILE, "w+");
  104. if (conf_paging_fp == NULL){
  105. perror("Open paging conf file Error: ");
  106. exit(1);
  107. }
  108. fprintf(conf_paging_fp, ";!\n\
  109. ;! Automatically generated configuration file\n\
  110. ;! Filename: extensions_paging_custom.conf (/etc/asterisk/extensions_paging_custom.conf)\n\
  111. ;! Generator: Generator Paging\n\
  112. ;! Creation Date: %s\n\
  113. ;!\n\n\
  114. ",\
  115. mytime()\
  116. );
  117. if (conf_extens_fp == NULL){
  118. perror("Open extens conf file Error: ");
  119. exit(1);
  120. }
  121. fprintf(conf_extens_fp, ";!\n\
  122. ;! Automatically generated configuration file\n\
  123. ;! Filename: extensions_extens_custom.conf (/etc/asterisk/extensions_extens_custom.conf)\n\
  124. ;! Generator: Generator Extens\n\
  125. ;! Creation Date: %s\n\
  126. ;!\n\n\
  127. ",\
  128. mytime()\
  129. );
  130. if (conf_ipphones_fp == NULL){
  131. perror("Open ipphones conf file Error: ");
  132. exit(1);
  133. }
  134. fprintf(conf_ipphones_fp, ";!\n\
  135. ;! Automatically generated configuration file\n\
  136. ;! Filename: extensions_phones_custom.conf (/etc/asterisk/extensions_phones_custom.conf)\n\
  137. ;! Generator: Generator phones\n\
  138. ;! Creation Date: %s\n\
  139. ;!\n\n\
  140. ",\
  141. mytime()\
  142. );
  143. if (conf_hints_fp == NULL){
  144. perror("Open hints conf file Error: ");
  145. exit(1);
  146. }
  147. fprintf(conf_hints_fp, ";!\n\
  148. ;! Automatically generated configuration file\n\
  149. ;! Filename: extensions_hints_custom.conf (/etc/asterisk/extensions_hints_custom.conf)\n\
  150. ;! Generator: Generator hints\n\
  151. ;! Creation Date: %s\n\
  152. ;!\n\n\
  153. [default]\n\
  154. ",\
  155. mytime()\
  156. );
  157. while ((g_row=mysql_fetch_row(g_res))){ // 打印结果集
  158. if (g_row[0] == NULL || g_row[1] == NULL || g_row[2] == NULL || g_row[3] == NULL){
  159. printf("some feild is empty!\n");
  160. continue;
  161. }
  162. fprintf(conf_extens_fp, "[extens-group-%s]\n",g_row[2]);
  163. fprintf(conf_ipphones_fp, "[phones-group-%s]\n",g_row[2]);
  164. memset(sql_tmp,0,sizeof(sql_tmp));
  165. 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\
  166. where GroupId = %s and t_paging_devices.type_id in('1','2','3','5') and t_paging_devices.user_id is NULL",g_row[0]);
  167. if (executesql(sql_tmp)){
  168. print_mysql_error(NULL);
  169. exit(1);
  170. }
  171. d_res = mysql_store_result(g_conn); // 从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录集
  172. memset(exten_tmp,0,sizeof(exten_tmp));
  173. memset(dest_tmp,0,sizeof(dest_tmp));
  174. while ((d_row=mysql_fetch_row(d_res))){ // 打印结果集
  175. if (d_row[0] == NULL || d_row[1] == NULL){
  176. printf("some feild is empty!\n");
  177. continue;
  178. }
  179. if(strcmp(d_row[2],"yes") == 0){
  180. //sprintf(exten_tmp, "%sSIP/%s&", exten_tmp, d_row[0]);
  181. //sprintf(dest_tmp, "%s%s|", dest_tmp, d_row[0]);
  182. strcat(exten_tmp,"PJSIP/");
  183. strcat(exten_tmp,d_row[0]);
  184. strcat(exten_tmp,"&");
  185. strcat(dest_tmp,d_row[0]);
  186. strcat(dest_tmp,"|");
  187. }
  188. int id = atoi(d_row[1]);
  189. switch(id){
  190. case 1:
  191. fprintf(conf_extens_fp, "exten => %s,1,Gosub(page,s,1(%s,PJSIP/%s))\n",d_row[0],d_row[0],d_row[0]);
  192. break;
  193. case 2:
  194. fprintf(conf_extens_fp, "exten => %s,1,Gosub(intercom,s,1(%s,PJSIP/%s))\n",d_row[0],d_row[0],d_row[0]);
  195. break;
  196. case 3:
  197. case 7:
  198. fprintf(conf_ipphones_fp, "exten => %s,1,Gosub(stdexten,s,1(%s,PJSIP/%s))\n",d_row[0],d_row[0],d_row[0]);
  199. break;
  200. }
  201. }
  202. if(strlen(exten_tmp) > 0){
  203. exten_tmp[strlen(exten_tmp) - 1] = '\0';
  204. dest_tmp[strlen(dest_tmp) - 1] = '\0';
  205. }
  206. fprintf(conf_extens_fp, "\n");
  207. memset(page_option, 0, sizeof(page_option));
  208. if(strcmp(g_row[3],"duplex") == 0){
  209. strcat(page_option,"d");
  210. }
  211. memset(page_volume, '\0', sizeof(page_volume));
  212. if(g_row[4] != NULL)
  213. {
  214. pJson = cJSON_Parse(g_row[4]);
  215. if(pJson && cJSON_HasObjectItem(pJson, "enableForce"))
  216. {
  217. if(cJSON_IsBool(cJSON_GetObjectItem(pJson, "enableForce")) && cJSON_IsTrue(cJSON_GetObjectItem(pJson, "enableForce")))
  218. {
  219. sprintf(page_volume,"\\;volume=%d",cJSON_GetObjectItem(pJson, "softvolume")->valueint);
  220. }
  221. }
  222. }
  223. fprintf(conf_paging_fp, "\
  224. [paging-group-%s]\n\
  225. exten => %s,1,NoOp(%s)\n\
  226. same => n,MSet(__SRCEXTEN=${CALLERID(num)},__DESTS=%s,DATE=${STRFTIME(${EPOCH},,%%Y%%m%%d)},__UUID=${UNIQUEID},__VOLSTR=%s)\n\
  227. same => n,GotoIf($[\"foo${PAGING_RECORD}\" != \"fooyes\"]?unrc)\n\
  228. same => n,System(/bin/sh /etc/scripts/shell_scripts.sh mkrcdir paging ${DATE})\n\
  229. same => n,Set(FILENAME=paging/${DATE}/paging-${SRCEXTEN}-${UUID}.wav)\n\
  230. same => n,MixMonitor(${FILENAME},b)\n\
  231. same => n(unrc),Gusub(get-user-level,s,1(${SRCEXTEN},))\n\
  232. same => n,AGI(agi://${AGISERVERHOST}:${AGISERVERPORT},paging,${DESTS},${SESSION_LEVEL},${SESSION_USERID})\n\
  233. same => n,ExecIf(${ISNULL(${DESTS})}?Hangup())\n\
  234. same => n,ExecIf($['foo${enPaging_prompt_start}'='fooyes']?Set(STARTPROMPT=qA(${Paging_start_file})))\n\
  235. same => n,UserEvent(controlEvent,sessionlevel:${SESSION_LEVEL},sessionuserid:${SESSION_USERID},src:${SRCEXTEN},dest:${DESTS},uuid:${UUID},status:paging)\n\
  236. same => n,Wait(1)\n\
  237. same => n,GotoIf(${ISNULL(${DESTCHANS})}?default)\n\
  238. same => n,MSet(startT=${STRFTIME(${EPOCH},,%%s)},__CALLEE=${DESTS},__calltype=paging,DEVICE_STATE(Custom:${EXTEN})=INUSE,__GROUPID=${EXTEN})\n\
  239. same => n,Page(${DESTCHANS},%sb(paging-update-status^s^1)${STARTPROMPT})\n\
  240. same => n,Hangup\n\
  241. same => n(default),MSet(startT=${STRFTIME(${EPOCH},,%%s)},__CALLEE=${DESTS},__calltype=paging,DEVICE_STATE(Custom:${EXTEN})=INUSE,__GROUPID=${EXTEN})\n\
  242. same => n,Page(%s,%sb(paging-update-status^s^1)${STARTPROMPT})\n\
  243. same => n,Hangup\
  244. \n\n", \
  245. g_row[2],\
  246. g_row[2],\
  247. g_row[1],\
  248. dest_tmp,\
  249. page_volume,\
  250. page_option,\
  251. exten_tmp,\
  252. page_option\
  253. );
  254. fprintf(conf_hints_fp,"\
  255. exten => %s,hint,Custom:%s\n\
  256. ",\
  257. g_row[2],\
  258. g_row[2]\
  259. );
  260. }
  261. memset(sql_tmp,0,sizeof(sql_tmp));
  262. sprintf(sql_tmp,"select conditions from t_paging_tasks where type='numberrule'");
  263. if (executesql(sql_tmp)){
  264. print_mysql_error(NULL);
  265. exit(1);
  266. }
  267. r_res = mysql_store_result(g_conn);
  268. while(r_row=mysql_fetch_row(r_res))
  269. {
  270. pJson = cJSON_Parse(r_row[0]);
  271. if(cJSON_GetObjectItem(pJson, "extension"))
  272. {
  273. fprintf(conf_hints_fp,"\
  274. exten => *11%s,hint,Custom:*11%s\n\
  275. ",\
  276. cJSON_GetObjectItem(pJson, "extension")->valuestring,\
  277. cJSON_GetObjectItem(pJson, "extension")->valuestring\
  278. );
  279. }
  280. }
  281. fclose(conf_paging_fp);
  282. fclose(conf_extens_fp);
  283. fclose(conf_ipphones_fp);
  284. fclose(conf_hints_fp);
  285. mysql_free_result(g_res); // 释放结果集
  286. mysql_free_result(d_res);
  287. mysql_free_result(r_res);
  288. mysql_close(g_conn); // 关闭链接
  289. if(pJson) cJSON_Delete(pJson);
  290. }