imc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /*
  2. * $Id$
  3. *
  4. * imc module - instant messaging conferencing implementation
  5. *
  6. * Copyright (C) 2006 Voice Sistem S.R.L.
  7. *
  8. * This file is part of Kamailio, a free SIP server.
  9. *
  10. * Kamailio is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * Kamailio is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * History:
  25. * ---------
  26. * 2006-10-06 first version (anca)
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <sys/types.h>
  32. #include <sys/ipc.h>
  33. #include <unistd.h>
  34. #include <fcntl.h>
  35. #include <time.h>
  36. #include "../../lib/srdb1/db.h"
  37. #include "../../lib/srdb1/db_res.h"
  38. #include "../../sr_module.h"
  39. #include "../../dprint.h"
  40. #include "../../ut.h"
  41. #include "../../timer.h"
  42. #include "../../str.h"
  43. #include "../../mem/shm_mem.h"
  44. #include "../../lib/srdb1/db.h"
  45. #include "../../parser/parse_from.h"
  46. #include "../../parser/parse_content.h"
  47. #include "../../parser/contact/parse_contact.h"
  48. #include "../../resolve.h"
  49. #include "../../hashes.h"
  50. #include "../../lib/kmi/mi.h"
  51. #include "../../modules/tm/tm_load.h"
  52. #include "imc_mng.h"
  53. #include "imc_cmd.h"
  54. MODULE_VERSION
  55. /** header variables */
  56. str imc_hdr_ctype = { "Content-Type: text/plain\r\n", 26};
  57. char hdr_buf[1024];
  58. str all_hdrs;
  59. /** parameters */
  60. db1_con_t *imc_db = NULL;
  61. db_func_t imc_dbf;
  62. static str db_url = str_init(DEFAULT_DB_URL);
  63. str outbound_proxy = {NULL, 0};
  64. static str rooms_table = str_init("imc_rooms");
  65. static str members_table = str_init("imc_members");
  66. static str imc_col_username = str_init("username");
  67. static str imc_col_domain = str_init("domain");
  68. static str imc_col_flag = str_init("flag");
  69. static str imc_col_room = str_init("room");
  70. static str imc_col_name = str_init("name");
  71. imc_hentry_p _imc_htable = NULL;
  72. int imc_hash_size = 4;
  73. str imc_cmd_start_str = str_init(IMC_CMD_START_STR);
  74. char imc_cmd_start_char;
  75. str extra_hdrs = {NULL, 0};
  76. /** module functions */
  77. static int mod_init(void);
  78. static int child_init(int);
  79. static int imc_manager(struct sip_msg*, char *, char *);
  80. static struct mi_root* imc_mi_list_rooms(struct mi_root* cmd, void* param);
  81. static struct mi_root* imc_mi_list_members(struct mi_root* cmd, void* param);
  82. static void destroy(void);
  83. /** TM bind */
  84. struct tm_binds tmb;
  85. /** TM callback function */
  86. void inv_callback( struct cell *t, int type, struct tmcb_params *ps);
  87. static cmd_export_t cmds[]={
  88. {"imc_manager", (cmd_function)imc_manager, 0, 0, 0, REQUEST_ROUTE},
  89. {0,0,0,0,0,0}
  90. };
  91. static param_export_t params[]={
  92. {"db_url", STR_PARAM, &db_url.s},
  93. {"hash_size", INT_PARAM, &imc_hash_size},
  94. {"imc_cmd_start_char", STR_PARAM, &imc_cmd_start_str.s},
  95. {"rooms_table", STR_PARAM, &rooms_table.s},
  96. {"members_table", STR_PARAM, &members_table.s},
  97. {"outbound_proxy", STR_PARAM, &outbound_proxy.s},
  98. {"extra_hdrs", STR_PARAM, &extra_hdrs.s},
  99. {0,0,0}
  100. };
  101. #ifdef STATISTICS
  102. #include "../../lib/kcore/statistics.h"
  103. stat_var* imc_active_rooms;
  104. stat_export_t imc_stats[] = {
  105. {"active_rooms" , 0, &imc_active_rooms },
  106. {0,0,0}
  107. };
  108. #endif
  109. static mi_export_t mi_cmds[] = {
  110. { "imc_list_rooms", imc_mi_list_rooms, MI_NO_INPUT_FLAG, 0, 0 },
  111. { "imc_list_members", imc_mi_list_members, 0, 0, 0 },
  112. { 0, 0, 0, 0, 0}
  113. };
  114. /** module exports */
  115. struct module_exports exports= {
  116. "imc", /* module name */
  117. DEFAULT_DLFLAGS, /* dlopen flags */
  118. cmds, /* exported commands */
  119. params, /* exported parameters */
  120. #ifdef STATISTICS
  121. imc_stats,
  122. #else
  123. 0, /* exported statistics */
  124. #endif
  125. mi_cmds, /* exported MI functions */
  126. 0, /* exported pseudo-variables */
  127. 0, /* extra processes */
  128. mod_init, /* mod init */
  129. 0, /* response handler */
  130. (destroy_function) destroy, /* destroy function */
  131. child_init /* child init */
  132. };
  133. /**
  134. * the initiating function
  135. */
  136. int add_from_db(void)
  137. {
  138. imc_member_p member = NULL;
  139. int i, j, flag;
  140. db_key_t mq_result_cols[4], mquery_cols[2];
  141. db_key_t rq_result_cols[4];
  142. db_val_t mquery_vals[2];
  143. db1_res_t *r_res= NULL;
  144. db1_res_t *m_res= NULL;
  145. db_row_t *m_row = NULL, *r_row = NULL;
  146. db_val_t *m_row_vals, *r_row_vals = NULL;
  147. str name, domain;
  148. imc_room_p room = NULL;
  149. int er_ret = -1;
  150. rq_result_cols[0] = &imc_col_name;
  151. rq_result_cols[1] = &imc_col_domain;
  152. rq_result_cols[2] = &imc_col_flag;
  153. mq_result_cols[0] = &imc_col_username;
  154. mq_result_cols[1] = &imc_col_domain;
  155. mq_result_cols[2] = &imc_col_flag;
  156. mquery_cols[0] = &imc_col_room;
  157. mquery_vals[0].type = DB1_STR;
  158. mquery_vals[0].nul = 0;
  159. if(imc_dbf.use_table(imc_db, &rooms_table)< 0)
  160. {
  161. LM_ERR("use_table failed\n");
  162. return -1;
  163. }
  164. if(imc_dbf.query(imc_db,0, 0, 0, rq_result_cols,0, 3, 0,&r_res)< 0)
  165. {
  166. LM_ERR("failed to querry table\n");
  167. return -1;
  168. }
  169. if(r_res && r_res->n<=0)
  170. {
  171. LM_INFO("the query returned no result\n");
  172. imc_dbf.free_result(imc_db, r_res);
  173. r_res = NULL;
  174. return 0;
  175. }
  176. LM_DBG("found %d rooms\n", r_res->n);
  177. for(i =0 ; i< r_res->n ; i++)
  178. {
  179. /*add rooms*/
  180. r_row = &r_res->rows[i];
  181. r_row_vals = ROW_VALUES(r_row);
  182. name.s = r_row_vals[0].val.str_val.s;
  183. name.len = strlen(name.s);
  184. domain.s = r_row_vals[1].val.str_val.s;
  185. domain.len = strlen(domain.s);
  186. flag = r_row_vals[2].val.int_val;
  187. room = imc_add_room(&name, &domain, flag);
  188. if(room == NULL)
  189. {
  190. LM_ERR("failed to add room\n ");
  191. goto error;
  192. }
  193. /* add members */
  194. if(imc_dbf.use_table(imc_db, &members_table)< 0)
  195. {
  196. LM_ERR("use_table failed\n ");
  197. goto error;
  198. }
  199. mquery_vals[0].val.str_val= room->uri;
  200. if(imc_dbf.query(imc_db, mquery_cols, 0, mquery_vals, mq_result_cols,
  201. 1, 3, 0, &m_res)< 0)
  202. {
  203. LM_ERR("failed to querry table\n");
  204. goto error;
  205. }
  206. if(m_res && m_res->n <=0)
  207. {
  208. LM_INFO("the query returned no result\n");
  209. er_ret = 0;
  210. goto error; /* each room must have at least one member*/
  211. }
  212. for(j =0; j< m_res->n; j++)
  213. {
  214. m_row = &m_res->rows[j];
  215. m_row_vals = ROW_VALUES(m_row);
  216. name.s = m_row_vals[0].val.str_val.s;
  217. name.len = strlen(name.s);
  218. domain.s = m_row_vals[1].val.str_val.s;
  219. domain.len = strlen(domain.s);
  220. flag = m_row_vals[2].val.int_val;
  221. LM_DBG("adding memeber: [name]=%.*s [domain]=%.*s"
  222. " in [room]= %.*s\n",name.len, name.s, domain.len,domain.s,
  223. room->uri.len, room->uri.s);
  224. member = imc_add_member(room, &name, &domain, flag);
  225. if(member == NULL)
  226. {
  227. LM_ERR("failed to adding member\n ");
  228. goto error;
  229. }
  230. imc_release_room(room);
  231. }
  232. if(m_res)
  233. {
  234. imc_dbf.free_result(imc_db, m_res);
  235. m_res = NULL;
  236. }
  237. }
  238. if(imc_dbf.use_table(imc_db, &members_table)< 0)
  239. {
  240. LM_ERR("use table failed\n ");
  241. goto error;
  242. }
  243. if(imc_dbf.delete(imc_db, 0, 0 , 0, 0) < 0)
  244. {
  245. LM_ERR("failed to delete information from db\n");
  246. goto error;
  247. }
  248. if(imc_dbf.use_table(imc_db, &rooms_table)< 0)
  249. {
  250. LM_ERR("use table failed\n ");
  251. goto error;
  252. }
  253. if(imc_dbf.delete(imc_db, 0, 0 , 0, 0) < 0)
  254. {
  255. LM_ERR("failed to delete information from db\n");
  256. goto error;
  257. }
  258. if(r_res)
  259. {
  260. imc_dbf.free_result(imc_db, r_res);
  261. r_res = NULL;
  262. }
  263. if(m_res)
  264. {
  265. imc_dbf.free_result(imc_db, m_res);
  266. m_res = NULL;
  267. }
  268. return 0;
  269. error:
  270. if(r_res)
  271. {
  272. imc_dbf.free_result(imc_db, r_res);
  273. r_res = NULL;
  274. }
  275. if(m_res)
  276. {
  277. imc_dbf.free_result(imc_db, m_res);
  278. m_res = NULL;
  279. }
  280. if(room)
  281. imc_release_room(room);
  282. return er_ret;
  283. }
  284. static int mod_init(void)
  285. {
  286. #ifdef STATISTICS
  287. /* register statistics */
  288. if (register_module_stats( exports.name, imc_stats)!=0 ) {
  289. LM_ERR("failed to register core statistics\n");
  290. return -1;
  291. }
  292. #endif
  293. if(register_mi_mod(exports.name, mi_cmds)!=0)
  294. {
  295. LM_ERR("failed to register MI commands\n");
  296. return -1;
  297. }
  298. if(imc_hash_size <= 0)
  299. {
  300. LM_ERR("invalid hash size\n");
  301. return -1;
  302. }
  303. imc_hash_size = 1 << imc_hash_size;
  304. if(imc_htable_init() < 0)
  305. {
  306. LM_ERR("initializing hash table\n");
  307. return -1;
  308. }
  309. imc_cmd_start_str.len = strlen(imc_cmd_start_str.s);
  310. if(outbound_proxy.s)
  311. outbound_proxy.len = strlen(outbound_proxy.s);
  312. rooms_table.len = strlen(rooms_table.s);
  313. members_table.len = strlen(members_table.s);
  314. if (extra_hdrs.s) {
  315. extra_hdrs.len = strlen(extra_hdrs.s);
  316. if (extra_hdrs.len + imc_hdr_ctype.len > 1024) {
  317. LM_ERR("extra_hdrs too long\n");
  318. return -1;
  319. }
  320. all_hdrs.s = &(hdr_buf[0]);
  321. memcpy(all_hdrs.s, imc_hdr_ctype.s, imc_hdr_ctype.len);
  322. memcpy(all_hdrs.s + imc_hdr_ctype.len, extra_hdrs.s,
  323. extra_hdrs.len);
  324. all_hdrs.len = extra_hdrs.len + imc_hdr_ctype.len;
  325. } else {
  326. all_hdrs = imc_hdr_ctype;
  327. }
  328. /* binding to mysql module */
  329. db_url.len = strlen(db_url.s);
  330. LM_DBG("db_url=%s/%d/%p\n", ZSW(db_url.s), db_url.len, db_url.s);
  331. if (db_bind_mod(&db_url, &imc_dbf))
  332. {
  333. LM_DBG("database module not found\n");
  334. return -1;
  335. }
  336. imc_db = imc_dbf.init(&db_url);
  337. if (!imc_db)
  338. {
  339. LM_ERR("failed to connect to the database\n");
  340. return -1;
  341. }
  342. /* read the informations stored in db */
  343. if(add_from_db() <0)
  344. {
  345. LM_ERR("failed to get information from db\n");
  346. return -1;
  347. }
  348. /* load TM API */
  349. if (load_tm_api(&tmb)!=0) {
  350. LM_ERR("unable to load tm api\n");
  351. return -1;
  352. }
  353. imc_cmd_start_char = imc_cmd_start_str.s[0];
  354. if(imc_db)
  355. imc_dbf.close(imc_db);
  356. imc_db = NULL;
  357. return 0;
  358. }
  359. /**
  360. * child init
  361. */
  362. static int child_init(int rank)
  363. {
  364. if (rank==PROC_INIT || rank==PROC_TCP_MAIN)
  365. return 0; /* do nothing for the main process */
  366. if (imc_dbf.init==0)
  367. {
  368. LM_ERR("database not bound\n");
  369. return -1;
  370. }
  371. imc_db = imc_dbf.init(&db_url);
  372. if (!imc_db)
  373. {
  374. LM_ERR("child %d: Error while connecting database\n", rank);
  375. return -1;
  376. }
  377. else
  378. {
  379. if (imc_dbf.use_table(imc_db, &rooms_table) < 0)
  380. {
  381. LM_ERR("child %d: Error in use_table '%.*s'\n", rank, rooms_table.len, rooms_table.s);
  382. return -1;
  383. }
  384. if (imc_dbf.use_table(imc_db, &members_table) < 0)
  385. {
  386. LM_ERR("child %d: Error in use_table '%.*s'\n", rank, members_table.len, members_table.s);
  387. return -1;
  388. }
  389. LM_DBG("child %d: Database connection opened successfully\n", rank);
  390. }
  391. return 0;
  392. }
  393. static int imc_manager(struct sip_msg* msg, char *str1, char *str2)
  394. {
  395. imc_cmd_t cmd;
  396. str body;
  397. struct sip_uri from_uri, *pto_uri=NULL, *pfrom_uri=NULL;
  398. struct to_body *pfrom;
  399. int ret = -1;
  400. body.s = get_body( msg );
  401. if (body.s==0)
  402. {
  403. LM_ERR("cannot extract body from msg\n");
  404. goto error;
  405. }
  406. /* lungimea corpului mesajului */
  407. if (!msg->content_length)
  408. {
  409. LM_ERR("no Content-Length\n");
  410. goto error;
  411. }
  412. body.len = get_content_length( msg );
  413. if(body.len <= 0)
  414. {
  415. LM_DBG("empty body!\n");
  416. goto error;
  417. }
  418. if(parse_sip_msg_uri(msg)<0)
  419. {
  420. LM_ERR("failed to parse r-uri\n");
  421. goto error;
  422. }
  423. pto_uri=&msg->parsed_uri;
  424. if(parse_from_header(msg)<0)
  425. {
  426. LM_ERR("failed to parse From header\n");
  427. goto error;
  428. }
  429. pfrom = (struct to_body*)msg->from->parsed;
  430. if(parse_uri(pfrom->uri.s, pfrom->uri.len, &from_uri)<0){
  431. LM_ERR("failed to parse From URI\n");
  432. goto error;
  433. }
  434. pfrom_uri=&from_uri;
  435. if(body.s[0]== imc_cmd_start_char)
  436. {
  437. LM_DBG("found command\n");
  438. if(imc_parse_cmd(body.s, body.len, &cmd)<0)
  439. {
  440. LM_ERR("failed to parse imc cmd!\n");
  441. ret = -20;
  442. goto error;
  443. }
  444. switch(cmd.type)
  445. {
  446. case IMC_CMDID_CREATE:
  447. if(imc_handle_create(msg, &cmd, pfrom_uri, pto_uri)<0)
  448. {
  449. LM_ERR("failed to handle 'create'\n");
  450. ret = -30;
  451. goto error;
  452. }
  453. break;
  454. case IMC_CMDID_JOIN:
  455. if(imc_handle_join(msg, &cmd, pfrom_uri, pto_uri)<0)
  456. {
  457. LM_ERR("failed to handle 'join'\n");
  458. ret = -40;
  459. goto error;
  460. }
  461. break;
  462. case IMC_CMDID_INVITE:
  463. if(imc_handle_invite(msg, &cmd, pfrom_uri, pto_uri)<0)
  464. {
  465. LM_ERR("failed to handle 'invite'\n");
  466. ret = -50;
  467. goto error;
  468. }
  469. break;
  470. case IMC_CMDID_ACCEPT:
  471. if(imc_handle_accept(msg, &cmd, pfrom_uri, pto_uri)<0)
  472. {
  473. LM_ERR("failed to handle 'accept'\n");
  474. ret = -60;
  475. goto error;
  476. }
  477. break;
  478. case IMC_CMDID_DENY:
  479. if(imc_handle_deny(msg, &cmd, pfrom_uri, pto_uri)<0)
  480. {
  481. LM_ERR("failed to handle 'deny'\n");
  482. ret = -70;
  483. goto error;
  484. }
  485. break;
  486. case IMC_CMDID_REMOVE:
  487. if(imc_handle_remove(msg, &cmd, pfrom_uri, pto_uri)<0)
  488. {
  489. LM_ERR("failed to handle 'remove'\n");
  490. ret = -80;
  491. goto error;
  492. }
  493. break;
  494. case IMC_CMDID_EXIT:
  495. if(imc_handle_exit(msg, &cmd, pfrom_uri, pto_uri)<0)
  496. {
  497. LM_ERR("failed to handle 'exit'\n");
  498. ret = -90;
  499. goto error;
  500. }
  501. break;
  502. case IMC_CMDID_LIST:
  503. if(imc_handle_list(msg, &cmd, pfrom_uri, pto_uri)<0)
  504. {
  505. LM_ERR("failed to handle 'list'\n");
  506. ret = -100;
  507. goto error;
  508. }
  509. break;
  510. case IMC_CMDID_DESTROY:
  511. if(imc_handle_destroy(msg, &cmd, pfrom_uri, pto_uri)<0)
  512. {
  513. LM_ERR("failed to handle 'destroy'\n");
  514. ret = -110;
  515. goto error;
  516. }
  517. break;
  518. case IMC_CMDID_HELP:
  519. if(imc_handle_help(msg, &cmd, &pfrom->uri,
  520. (msg->new_uri.s)?&msg->new_uri:&msg->first_line.u.request.uri)<0)
  521. {
  522. LM_ERR("failed to handle 'help'\n");
  523. ret = -120;
  524. goto error;
  525. }
  526. break;
  527. default:
  528. if(imc_handle_unknown(msg, &cmd, &pfrom->uri,
  529. (msg->new_uri.s)?&msg->new_uri:&msg->first_line.u.request.uri)<0)
  530. {
  531. LM_ERR("failed to handle 'unknown'\n");
  532. ret = -130;
  533. goto error;
  534. }
  535. }
  536. goto done;
  537. }
  538. if(imc_handle_message(msg, &body, pfrom_uri, pto_uri)<0)
  539. {
  540. LM_ERR("failed to handle 'message'\n");
  541. ret = -200;
  542. goto error;
  543. }
  544. done:
  545. return 1;
  546. error:
  547. return ret;
  548. }
  549. /**
  550. * destroy module
  551. */
  552. static void destroy(void)
  553. {
  554. imc_room_p irp = NULL;
  555. imc_member_p member = NULL;
  556. int i;
  557. db_key_t mq_cols[4];
  558. db_val_t mq_vals[4];
  559. db_key_t rq_cols[4];
  560. db_val_t rq_vals[4];
  561. if(imc_db==NULL)
  562. goto done;
  563. mq_cols[0] = &imc_col_username;
  564. mq_vals[0].type = DB1_STR;
  565. mq_vals[0].nul = 0;
  566. mq_cols[1] = &imc_col_domain;
  567. mq_vals[1].type = DB1_STR;
  568. mq_vals[1].nul = 0;
  569. mq_cols[2] = &imc_col_flag;
  570. mq_vals[2].type = DB1_INT;
  571. mq_vals[2].nul = 0;
  572. mq_cols[3] = &imc_col_room;
  573. mq_vals[3].type = DB1_STR;
  574. mq_vals[3].nul = 0;
  575. rq_cols[0] = &imc_col_name;
  576. rq_vals[0].type = DB1_STR;
  577. rq_vals[0].nul = 0;
  578. rq_cols[1] = &imc_col_domain;
  579. rq_vals[1].type = DB1_STR;
  580. rq_vals[1].nul = 0;
  581. rq_cols[2] = &imc_col_flag;
  582. rq_vals[2].type = DB1_INT;
  583. rq_vals[2].nul = 0;
  584. for(i=0; i<imc_hash_size; i++)
  585. {
  586. irp = _imc_htable[i].rooms;
  587. while(irp)
  588. {
  589. rq_vals[0].val.str_val = irp->name;
  590. rq_vals[1].val.str_val = irp->domain;
  591. rq_vals[2].val.int_val = irp->flags;
  592. if(imc_dbf.use_table(imc_db, &rooms_table)< 0)
  593. {
  594. LM_ERR("use_table failed\n");
  595. return;
  596. }
  597. if(imc_dbf.insert(imc_db, rq_cols, rq_vals, 3)<0)
  598. {
  599. LM_ERR("failed to insert into table imc_rooms\n");
  600. return;
  601. }
  602. LM_DBG("room %d %.*s\n", i, irp->name.len, irp->name.s);
  603. member = irp->members;
  604. while(member)
  605. {
  606. mq_vals[0].val.str_val = member->user;
  607. mq_vals[1].val.str_val = member->domain;
  608. mq_vals[2].val.int_val = member->flags;
  609. mq_vals[3].val.str_val = irp->uri;
  610. if(imc_dbf.use_table(imc_db, &members_table)< 0)
  611. {
  612. LM_ERR("use_table failed\n");
  613. return;
  614. }
  615. if(imc_dbf.insert(imc_db, mq_cols, mq_vals, 4)<0)
  616. {
  617. LM_ERR("failed to insert into table imc_rooms\n");
  618. return;
  619. }
  620. member = member->next;
  621. }
  622. irp = irp->next;
  623. }
  624. }
  625. done:
  626. imc_htable_destroy();
  627. }
  628. /************************* MI ***********************/
  629. static struct mi_root* imc_mi_list_rooms(struct mi_root* cmd_tree, void* param)
  630. {
  631. int i, len;
  632. struct mi_root* rpl_tree= NULL;
  633. struct mi_node* rpl= NULL;
  634. struct mi_node* node= NULL;
  635. struct mi_attr* attr= NULL;
  636. imc_room_p irp = NULL;
  637. char* p = NULL;
  638. rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  639. if(rpl_tree == NULL)
  640. return 0;
  641. rpl = &rpl_tree->node;
  642. for(i=0; i<imc_hash_size; i++)
  643. {
  644. lock_get(&_imc_htable[i].lock);
  645. irp = _imc_htable[i].rooms;
  646. while(irp){
  647. node = add_mi_node_child(rpl, 0, "ROOM", 4, 0, 0);
  648. if( node == NULL)
  649. goto error;
  650. attr= add_mi_attr(node, MI_DUP_VALUE, "URI", 3, irp->uri.s,
  651. irp->uri.len);
  652. if(attr == NULL)
  653. goto error;
  654. p = int2str(irp->nr_of_members, &len);
  655. attr= add_mi_attr(node, 0, "MEMBERS", 7,p, len );
  656. if(attr == NULL)
  657. goto error;
  658. attr= add_mi_attr(node, MI_DUP_VALUE, "OWNER", 5,
  659. irp->members->uri.s, irp->members->uri.len);
  660. if(attr == NULL)
  661. goto error;
  662. irp = irp->next;
  663. }
  664. lock_release(&_imc_htable[i].lock);
  665. }
  666. return rpl_tree;
  667. error:
  668. lock_release(&_imc_htable[i].lock);
  669. free_mi_tree(rpl_tree);
  670. return 0;
  671. }
  672. static struct mi_root* imc_mi_list_members(struct mi_root* cmd_tree,
  673. void* param)
  674. {
  675. int i, len;
  676. struct mi_root* rpl_tree = NULL;
  677. struct mi_node* node= NULL;
  678. struct mi_node* node_r= NULL;
  679. struct mi_attr* attr= NULL;
  680. char rnbuf[256];
  681. str room_name;
  682. imc_room_p room;
  683. struct sip_uri inv_uri, *pinv_uri;
  684. imc_member_p imp=NULL;
  685. char* p = NULL;
  686. node= cmd_tree->node.kids;
  687. if(node == NULL|| node->next!=NULL)
  688. return 0;
  689. /* room name */
  690. room_name.s = rnbuf;
  691. room_name.len= node->value.len;
  692. memcpy(room_name.s, node->value.s, node->value.len);
  693. if(room_name.s == NULL || room_name.len == 0)
  694. {
  695. LM_ERR(" no room name!\n");
  696. return init_mi_tree( 404, "room name not found", 19);
  697. }
  698. rnbuf[room_name.len] = '\0';
  699. if(*room_name.s=='\0' || *room_name.s=='.')
  700. {
  701. LM_INFO("empty room name\n");
  702. return init_mi_tree( 400, "empty param", 11);
  703. }
  704. /* find room */
  705. parse_uri(room_name.s,room_name.len, &inv_uri);
  706. pinv_uri=&inv_uri;
  707. room=imc_get_room(&pinv_uri->user, &pinv_uri->host);
  708. if(room==NULL)
  709. {
  710. LM_ERR("no such room!\n");
  711. return init_mi_tree( 404, "no such room", 14);
  712. }
  713. rpl_tree = init_mi_tree( 200, MI_OK_S, MI_OK_LEN);
  714. if(rpl_tree == NULL)
  715. return 0;
  716. node_r = add_mi_node_child( &rpl_tree->node, MI_DUP_VALUE, "ROOM", 4,
  717. room_name.s, room_name.len);
  718. if(node_r == NULL)
  719. goto error;
  720. imp = room->members;
  721. i=0;
  722. while(imp)
  723. {
  724. i++;
  725. node = add_mi_node_child(node_r, MI_DUP_VALUE, "MEMBER",6, imp->uri.s,
  726. imp->uri.len);
  727. if(node == NULL)
  728. goto error;
  729. imp = imp->next;
  730. }
  731. p = int2str(i, &len);
  732. attr= add_mi_attr(node_r, MI_DUP_VALUE, "NR_OF_MEMBERS", 13, p, len);
  733. if(attr == 0)
  734. goto error;
  735. imc_release_room(room);
  736. return rpl_tree;
  737. error:
  738. imc_release_room(room);
  739. free_mi_tree(rpl_tree);
  740. return 0;
  741. }