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