/
imc_cmd.c
1292 lines (1123 loc) · 31.3 KB
/
imc_cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: imc_cmd.c 5016 2008-11-23 15:28:02Z saguti $
*
* imc module - instant messaging conferencing implementation
*
* Copyright (C) 2006 Voice Sistem S.R.L.
*
* This file is part of opensips, a free SIP server.
*
* opensips is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version
*
* opensips is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include "../../mem/shm_mem.h"
#include "../../mem/mem.h"
#include "../../sr_module.h"
#include "../../dprint.h"
#include "../../parser/parse_uri.h"
#include "imc.h"
#include "imc_cmd.h"
#define IMC_BUF_SIZE 1024
static char imc_body_buf[IMC_BUF_SIZE];
static str imc_msg_type = { "MESSAGE", 7 };
static str imc_hdr_ctype = { "Content-Type: text/plain\r\n", 26};
int imc_send_message(str *src, str *dst, str *headers, str *body);
int imc_room_broadcast(imc_room_p room, str *ctype, str *body);
void imc_inv_callback( struct cell *t, int type, struct tmcb_params *ps);
/**
* parse cmd
*/
int imc_parse_cmd(char *buf, int len, imc_cmd_p cmd)
{
char *p;
int i;
if(buf==NULL || len<=0 || cmd==NULL)
{
LM_ERR("invalid parameters\n");
return -1;
}
memset(cmd, 0, sizeof(imc_cmd_t));
if(buf[0]!=imc_cmd_start_char)
{
LM_ERR("invalid command [%.*s]\n", len, buf);
return -1;
}
p = &buf[1];
cmd->name.s = p;
while(*p && p<buf+len)
{
if(*p==' ' || *p=='\t' || *p=='\r' || *p=='\n')
break;
p++;
}
if(cmd->name.s == p)
{
LM_ERR("no command in [%.*s]\n", len, buf);
return -1;
}
cmd->name.len = p - cmd->name.s;
/* identify the command */
if(cmd->name.len==(sizeof("create")-1)
&& !strncasecmp(cmd->name.s, "create", cmd->name.len))
{
cmd->type = IMC_CMDID_CREATE;
} else if(cmd->name.len==(sizeof("join")-1)
&& !strncasecmp(cmd->name.s, "join", cmd->name.len)) {
cmd->type = IMC_CMDID_JOIN;
} else if(cmd->name.len==(sizeof("invite")-1)
&& !strncasecmp(cmd->name.s, "invite", cmd->name.len)) {
cmd->type = IMC_CMDID_INVITE;
} else if(cmd->name.len==(sizeof("accept")-1)
&& !strncasecmp(cmd->name.s, "accept", cmd->name.len)) {
cmd->type = IMC_CMDID_ACCEPT;
} else if(cmd->name.len==(sizeof("deny")-1)
&& !strncasecmp(cmd->name.s, "deny", cmd->name.len)) {
cmd->type = IMC_CMDID_DENY;
} else if(cmd->name.len==(sizeof("remove")-1)
&& !strncasecmp(cmd->name.s, "remove", cmd->name.len)) {
cmd->type = IMC_CMDID_REMOVE;
} else if(cmd->name.len==(sizeof("exit")-1)
&& !strncasecmp(cmd->name.s, "exit", cmd->name.len)) {
cmd->type = IMC_CMDID_EXIT;
} else if(cmd->name.len==(sizeof("list")-1)
&& !strncasecmp(cmd->name.s, "list", cmd->name.len)) {
cmd->type = IMC_CMDID_LIST;
} else if(cmd->name.len==(sizeof("destroy")-1)
&& !strncasecmp(cmd->name.s, "destroy", cmd->name.len)) {
cmd->type = IMC_CMDID_DESTROY;
} else if(cmd->name.len==(sizeof("rename")-1)
&& !strncasecmp(cmd->name.s, "rename", cmd->name.len)) {
cmd->type = IMC_CMDID_RENAME;
} else if(cmd->name.len==(sizeof("help")-1)
&& !strncasecmp(cmd->name.s, "help", cmd->name.len)) {
cmd->type = IMC_CMDID_HELP;
goto done;
} else {
cmd->type = IMC_CMDID_UNKNOWN;
goto done;
}
if(*p=='\0' || p>=buf+len)
goto done;
i=0;
do {
while(p<buf+len && (*p==' ' || *p=='\t'))
p++;
if(p>=buf+len || *p=='\0' || *p=='\r' || *p=='\n')
goto done;
cmd->param[i].s = p;
while(p<buf+len)
{
if(*p=='\0' || *p==' ' || *p=='\t' || *p=='\r' || *p=='\n')
break;
p++;
}
cmd->param[i].len = p - cmd->param[i].s;
i++;
if(i>=IMC_CMD_MAX_PARAM)
break;
} while(1);
done:
LM_ERR("command: [%.*s]\n", cmd->name.len, cmd->name.s);
for(i=0; i<IMC_CMD_MAX_PARAM; i++)
{
if(cmd->param[i].len<=0)
break;
LM_DBG("parameter %d=[%.*s]\n", i, cmd->param[i].len, cmd->param[i].s);
}
return 0;
}
/**
*
*/
int imc_handle_create(struct sip_msg* msg, imc_cmd_t *cmd,
struct sip_uri *src, struct sip_uri *dst, str *fullname)
{
imc_room_p room = 0;
imc_member_p member = 0;
int flag_room = 0;
int flag_member = 0;
str body;
room = imc_get_room(&cmd->param[0], &dst->host);
if(room== NULL)
{
LM_DBG("new room [%.*s]\n", cmd->param[0].len, cmd->param[0].s);
if(cmd->param[1].len==IMC_ROOM_PRIVATE_LEN
&& !strncasecmp(cmd->param[1].s, IMC_ROOM_PRIVATE,
cmd->param[1].len))
{
flag_room |= IMC_ROOM_PRIV;
LM_DBG("room with private flag on\n");
}
room= imc_add_room(&cmd->param[0], &dst->host, flag_room);
if(room == NULL)
{
LM_ERR("failed to add new room\n");
goto error;
}
LM_DBG("added room uri= %.*s\n", room->uri.len, room->uri.s);
flag_member |= IMC_MEMBER_OWNER;
/* adding the owner as the forst member*/
member= imc_add_member(room, &src->user, &src->host, fullname, flag_member);
if(member == NULL)
{
LM_ERR("failed to add owner [%.*s]\n", src->user.len, src->user.s);
goto error;
}
LM_DBG("added the owner (%.*s) as the first member "
"[%.*s]\n", member->fullname.len, member->fullname.s, member->uri.len, member->uri.s);
/* send info message */
body.s = "*** room was created";
body.len = sizeof("*** room was created")-1;
imc_send_message(&room->uri, &member->uri, &imc_hdr_ctype, &body);
goto done;
}
/* room already exists */
LM_DBG("room [%.*s] already created\n", cmd->param[0].len, cmd->param[0].s);
if(!(room->flags & IMC_ROOM_PRIV))
{
LM_DBG("checking if the user [%.*s] is a member\n",
src->user.len, src->user.s);
member= imc_get_member(room, &src->user, &src->host);
if(member== NULL)
{
member= imc_add_member(room, &src->user, &src->host, fullname, flag_member);
if(member == NULL)
{
LM_ERR("failed to add member [%.*s]\n",
src->user.len, src->user.s);
goto error;
}
LM_DBG("added as member [%.*s] (%.*s)\n",member->uri.len, member->uri.s, member->fullname.len, member->fullname.s);
/* send info message */
body.s = imc_body_buf;
body.len = snprintf(body.s, IMC_BUF_SIZE,
"*** [%.*s] (%.*s) has joined the room",
member->uri.len, member->uri.s, member->fullname.len, member->fullname.s);
if(body.len>0)
imc_room_broadcast(room, &imc_hdr_ctype, &body);
}
}
done:
if(room!=NULL)
imc_release_room(room);
return 0;
error:
if(room!=NULL)
imc_release_room(room);
return -1;
}
/**
*
*/
int imc_handle_join(struct sip_msg* msg, imc_cmd_t *cmd,
struct sip_uri *src, struct sip_uri *dst, str *fullname)
{
imc_room_p room = 0;
imc_member_p member = 0;
int flag_room = 0;
int flag_member = 0;
str room_name;
str body;
room_name = cmd->param[0].s?cmd->param[0]:dst->user;
room=imc_get_room(&room_name, &dst->host);
if(room== NULL || (room->flags&IMC_ROOM_DELETED))
{
LM_DBG("could not find room [%.*s]- adding\n",
room_name.len, room_name.s);
room= imc_add_room(&room_name, &dst->host, flag_room);
if(room == NULL)
{
LM_ERR("failed to add new room [%.*s]\n",
room_name.len, room_name.s);
goto error;
}
LM_DBG("created a new room [%.*s]\n", room->name.len, room->name.s);
flag_member |= IMC_MEMBER_OWNER;
member= imc_add_member(room, &src->user, &src->host, fullname, flag_member);
if(member == NULL)
{
LM_ERR("failed to add new member [%.*s]\n",
src->user.len, src->user.s);
goto error;
}
/* send info message */
body.s = "*** room was created";
body.len = sizeof("*** room was created")-1;
imc_send_message(&room->uri, &member->uri, &imc_hdr_ctype, &body);
goto done;
}
/* room exists */
LM_DBG("found room [%.*s]\n", room_name.len, room_name.s);
member= imc_get_member(room, &src->user, &src->host);
if(!(room->flags & IMC_ROOM_PRIV))
{
LM_DBG("room [%.*s] is public\n", room_name.len, room_name.s);
if(member== NULL)
{
LM_DBG("adding new member [%.*s]\n", src->user.len, src->user.s);
member= imc_add_member(room, &src->user,
&src->host, fullname, flag_member);
if(member == NULL)
{
LM_ERR("adding new user [%.*s]\n", src->user.len, src->user.s);
goto error;
}
goto build_inform;
} else {
LM_DBG("member [%.*s] (%.*s) is in room already\n",
member->uri.len,member->uri.s, member->fullname.len, member->fullname.s );
}
} else {
if(member==NULL)
{
LM_ERR("attept to join private room [%.*s] from user [%.*s]\n",
room_name.len, room_name.s, src->user.len, src->user.s);
goto build_inform;
}
if(member->flags & IMC_MEMBER_INVITED)
member->flags &= ~IMC_MEMBER_INVITED;
}
build_inform:
/* send info message */
body.s = imc_body_buf;
body.len = snprintf(body.s, IMC_BUF_SIZE, "*** [%.*s] (%.*s) has joined the room",
member->uri.len, member->uri.s, member->fullname.len, member->fullname.s);
if(body.len>0)
imc_room_broadcast(room, &imc_hdr_ctype, &body);
done:
if(room!=NULL)
imc_release_room(room);
return 0;
error:
if(room!=NULL)
imc_release_room(room);
return -1;
}
/**
*
*/
int imc_handle_invite(struct sip_msg* msg, imc_cmd_t *cmd,
struct sip_uri *src, struct sip_uri *dst)
{
imc_room_p room = 0;
imc_member_p member = 0;
int flag_member = 0;
int size = 0;
int i = 0;
int add_domain = 0;
int add_sip = 0;
str uri = {0, 0};
str body;
str room_name;
struct sip_uri inv_uri;
del_member_t *cback_param = NULL;
int result;
size = cmd->param[0].len+2 ;
add_domain = 1;
add_sip = 0;
while (i<size )
{
if(cmd->param[0].s[i]== '@')
{
add_domain = 0;
break;
}
i++;
}
if(add_domain)
size += dst->host.len;
if(cmd->param[0].len<4 || strncasecmp(cmd->param[0].s, "sip:", 4)!=0)
{
size += 4;
add_sip = 1;
}
uri.s = (char*)pkg_malloc(size *sizeof(char));
if(uri.s == NULL)
{
LM_ERR("no more pkg memory\n");
goto error;
}
size= 0;
if(add_sip)
{
strcpy(uri.s, "sip:");
size=4;
}
memcpy(uri.s+size, cmd->param[0].s, cmd->param[0].len);
size += cmd->param[0].len;
if(add_domain)
{
uri.s[size] = '@';
size++;
memcpy(uri.s+ size, dst->host.s, dst->host.len);
size+= dst->host.len;
}
uri.len = size;
if(parse_uri(uri.s, uri.len, &inv_uri)!=0)
{
LM_ERR("bad uri [%.*s]!\n", uri.len, uri.s);
goto error;
}
room_name = (cmd->param[1].s)?cmd->param[1]:dst->user;
room = imc_get_room(&room_name, &dst->host);
if(room== NULL || (room->flags&IMC_ROOM_DELETED))
{
LM_ERR("the room does not exist [%.*s]!\n",
room_name.len, room_name.s);
goto error;
}
member= imc_get_member(room, &src->user, &src->host);
if(member==NULL)
{
LM_ERR("user [%.*s] is not member of[%.*s]!\n",
src->user.len, src->user.s, room_name.len, room_name.s);
goto error;
}
if(!(member->flags & IMC_MEMBER_OWNER) &&
!(member->flags & IMC_MEMBER_ADMIN))
{
LM_ERR("user [%.*s] has no right to invite"
" other users!\n", src->user.len, src->user.s);
goto error;
}
member= imc_get_member(room, &inv_uri.user, &inv_uri.host);
if(member!=NULL)
{
LM_ERR("user [%.*s] is already member"
" of the room!\n", inv_uri.user.len, inv_uri.user.s);
goto error;
}
flag_member |= IMC_MEMBER_INVITED;
member=imc_add_member(room, &inv_uri.user, &inv_uri.host, &inv_uri.user /* TODO fullname dummy */, flag_member);
if(member == NULL)
{
LM_ERR("adding member [%.*s]\n",
inv_uri.user.len, inv_uri.user.s);
goto error;
}
body.len = 13 + member->uri.len - 4/* sip: */ + 28;
if(body.len>=IMC_BUF_SIZE || member->uri.len>=IMC_BUF_SIZE
|| room->uri.len>=IMC_BUF_SIZE)
{
LM_ERR("buffer size overflow\n");
goto error;
}
body.s = imc_body_buf;
memcpy(body.s, "INVITE from: ", 13);
memcpy(body.s+13, member->uri.s + 4, member->uri.len - 4);
memcpy(body.s+ 9 + member->uri.len, "(Type: '#accept' or '#deny')", 28);
body.s[body.len] = '\0';
LM_DBG("to=[%.*s]\nfrom=[%.*s]\nbody=[%.*s]\n",
member->uri.len,member->uri.s,room->uri.len, room->uri.s,
body.len, body.s);
cback_param = (del_member_t*)shm_malloc(sizeof(del_member_t));
if(cback_param==NULL)
{
LM_ERR("no more shm\n");
goto error;
}
memset(cback_param, 0, sizeof(del_member_t));
cback_param->room_name = room->name;
cback_param->room_domain = room->domain;
cback_param->member_name = member->user;
cback_param->member_domain = member->domain;
cback_param->inv_uri = member->uri;
/*?!?! possible race with 'remove user' */
result= tmb.t_request(&imc_msg_type, /* Request Method */
&member->uri, /* Request-URI */
&member->uri, /* To */
&room->uri, /* From */
&imc_hdr_ctype, /* Extra headers */
&body, /* Message body */
(outbound_proxy.s)?&outbound_proxy:NULL,/* outbound proxy*/
imc_inv_callback, /* callback function*/
(void*)(cback_param) /* callback param*/
);
if(result< 0)
{
LM_ERR("in tm send request\n");
shm_free(cback_param);
goto error;
}
if(uri.s!=NULL)
pkg_free(uri.s);
imc_release_room(room);
return 0;
error:
if(uri.s!=0)
pkg_free(uri.s);
if(room!=NULL)
imc_release_room(room);
return -1;
}
/**
*
*/
int imc_handle_accept(struct sip_msg* msg, imc_cmd_t *cmd,
struct sip_uri *src, struct sip_uri *dst)
{
imc_room_p room = 0;
imc_member_p member = 0;
str room_name;
str body;
/* accepting the invitation */
room_name = cmd->param[0].s?cmd->param[0]:dst->user;
room=imc_get_room(&room_name, &dst->host);
if(room== NULL || (room->flags&IMC_ROOM_DELETED))
{
LM_ERR("room [%.*s] is not created!\n", room_name.len, room_name.s);
goto error;
}
/* if aready invited add as a member */
member=imc_get_member(room, &src->user, &src->host);
if(member==NULL || !(member->flags & IMC_MEMBER_INVITED))
{
LM_ERR("user [%.*s] not invited in the room!\n",
src->user.len, src->user.s);
goto error;
}
member->flags &= ~IMC_MEMBER_INVITED;
/* send info message */
body.s = imc_body_buf;
body.len = snprintf(body.s, IMC_BUF_SIZE, "*** [%.*s] (%.*s) has joined the room",
member->uri.len, member->uri.s, member->fullname.len, member->fullname.s);
if(body.len>0)
imc_room_broadcast(room, &imc_hdr_ctype, &body);
imc_release_room(room);
return 0;
error:
if(room!=NULL)
imc_release_room(room);
return -1;
}
/**
*
*/
int imc_handle_remove(struct sip_msg* msg, imc_cmd_t *cmd,
struct sip_uri *src, struct sip_uri *dst)
{
imc_room_p room = 0;
imc_member_p member = 0;
str room_name;
str body;
str uri = {0, 0};
int size =0;
int i = 0;
int add_domain = 0;
int add_sip = 0;
struct sip_uri inv_uri;
size= cmd->param[0].len+2;
add_domain = 1;
while (i<size )
{
if(cmd->param[0].s[i]== '@')
{
add_domain =0;
break;
}
i++;
}
if(add_domain)
size += dst->host.len;
if(cmd->param[0].len<=4 || strncasecmp(cmd->param[0].s, "sip:", 4)!=0)
{
size+= 4;
add_sip = 1;
}
uri.s = (char*)pkg_malloc(size*sizeof(char));
if(uri.s == NULL)
{
LM_ERR("no more pkg memory\n");
goto error;
}
size= 0;
if(add_sip)
{
strcpy(uri.s, "sip:");
size = 4;
}
memcpy(uri.s+size, cmd->param[0].s, cmd->param[0].len);
size+= cmd->param[0].len;
if(add_domain)
{
uri.s[size] = '@';
size++;
memcpy(uri.s+size, dst->host.s, dst->host.len);
size+= dst->host.len;
}
uri.len = size;
if(parse_uri(uri.s, uri.len, &inv_uri)<0)
{
LM_ERR("invalid uri [%.*s]\n", uri.len, uri.s);
goto error;
}
room_name = cmd->param[1].s?cmd->param[1]:dst->user;
room= imc_get_room(&room_name, &dst->host);
if(room==NULL || (room->flags&IMC_ROOM_DELETED))
{
LM_ERR("room [%.*s]does not exist!\n", room_name.len, room_name.s);
goto error;
}
/* verify if the user who sent the request is a member in the room
* and has the right to remove other users */
member= imc_get_member(room, &src->user, &src->host);
if(member== NULL)
{
LM_ERR("user [%.*s] is not member of room [%.*s]!\n",
src->user.len, src->user.s, room_name.len, room_name.s);
goto error;
}
if(!(member->flags & IMC_MEMBER_OWNER) &&
!(member->flags & IMC_MEMBER_ADMIN))
{
LM_ERR("user [%.*s] has no right to remove other users [%.*s]!\n",
src->user.len, src->user.s, uri.len, uri.s);
goto error;
}
/* verify if the user that is to be removed is a member of the room */
member= imc_get_member(room, &inv_uri.user, &inv_uri.host);
if(member== NULL)
{
LM_ERR("user [%.*s] is not member of room [%.*s]!\n",
inv_uri.user.len, inv_uri.user.s, room_name.len, room_name.s);
goto error;
}
if(member->flags & IMC_MEMBER_OWNER)
{
LM_ERR("user [%.*s] is owner of room [%.*s]"
" -- cannot be removed!\n", inv_uri.user.len, inv_uri.user.s,
room_name.len, room_name.s);
goto error;
}
/* send message to the removed person */
body.s = "You have been removed from this room";
body.len = strlen(body.s);
LM_DBG("to: [%.*s]\nfrom: [%.*s]\nbody: [%.*s]\n",
member->uri.len, member->uri.s , room->uri.len, room->uri.s,
body.len, body.s);
imc_send_message(&room->uri, &member->uri, &imc_hdr_ctype, &body);
member->flags |= IMC_MEMBER_DELETED;
imc_del_member(room, &inv_uri.user, &inv_uri.host);
body.s = imc_body_buf;
body.len = snprintf(body.s, IMC_BUF_SIZE, "*** [%.*s] (%.*s) has been removed from the room",
member->uri.len, member->uri.s, member->fullname.len, member->fullname.s);
if(body.len>0)
imc_room_broadcast(room, &imc_hdr_ctype, &body);
if(uri.s!=0)
pkg_free(uri.s);
imc_release_room(room);
return 0;
error:
if(uri.s!=0)
pkg_free(uri.s);
if(room!=NULL)
imc_release_room(room);
return -1;
}
/**
*
*/
int imc_handle_deny(struct sip_msg* msg, imc_cmd_t *cmd,
struct sip_uri *src, struct sip_uri *dst)
{
imc_room_p room = 0;
imc_member_p member = 0;
str room_name;
// str body;
/* denying an invitation */
room_name = cmd->param[0].s?cmd->param[0]:dst->user;
room= imc_get_room(&room_name, &dst->host);
if(room== NULL || (room->flags&IMC_ROOM_DELETED))
{
LM_ERR("room [%.*s] does not exist!\n", room_name.len, room_name.s);
goto error;
}
/* If the user is an invited member, delete it froim the list */
member= imc_get_member(room, &src->user, &src->host);
if(member==NULL || !(member->flags & IMC_MEMBER_INVITED))
{
LM_ERR("user [%.*s] was not invited in room [%.*s]!\n",
src->user.len, src->user.s, room_name.len, room_name.s);
goto error;
}
#if 0
/* send info message */
body.s = imc_body_buf;
body.len = snprintf(body.s, IMC_BUF_SIZE,
"The user [%.*s] has denied the invitation",
src->user.len, src->user.s);
if(body.len>0)
imc_send_message(&room->uri, &memeber->uri, &imc_hdr_ctype, &body);
#endif
LM_ERR("user [%.*s] declined invitation in room [%.*s]!\n",
src->user.len, src->user.s, room_name.len, room_name.s);
imc_del_member(room, &src->user, &src->host);
imc_release_room(room);
return 0;
error:
if(room!=NULL)
imc_release_room(room);
return -1;
}
/**
*
*/
int imc_handle_list(struct sip_msg* msg, imc_cmd_t *cmd,
struct sip_uri *src, struct sip_uri *dst)
{
imc_room_p room = 0;
imc_member_p member = 0;
imc_member_p imp = 0;
str room_name;
str body;
char *p;
/* the user wants to leave the room */
room_name = cmd->param[0].s?cmd->param[0]:dst->user;
room= imc_get_room(&room_name, &dst->host);
if(room== NULL || (room->flags&IMC_ROOM_DELETED))
{
LM_ERR("room [%.*s] does not exist!\n", room_name.len, room_name.s);
goto error;
}
/* verify if the user is a member of the room */
member = imc_get_member(room, &src->user, &src->host);
if(member == NULL)
{
LM_ERR("user [%.*s] is not member of room [%.*s]!\n",
src->user.len, src->user.s, room_name.len, room_name.s);
goto error;
}
p = imc_body_buf;
strncpy(p, "Members:\n", 9);
p+=9;
imp = room->members;
while(imp)
{
if((imp->flags&IMC_MEMBER_INVITED)||(imp->flags&IMC_MEMBER_DELETED)
|| (imp->flags&IMC_MEMBER_SKIP))
{
imp = imp->next;
continue;
}
if(imp->flags & IMC_MEMBER_OWNER)
*p++ = '*';
else if(imp->flags & IMC_MEMBER_ADMIN)
*p++ = '~';
strncpy(p, imp->uri.s, imp->uri.len);
p += imp->uri.len;
/* S: add fullname display in brackets behind URI */
*p++ = ' ';
*p++ = '(';
strncpy(p, imp->fullname.s, imp->fullname.len);
p += imp->fullname.len;
*p++ = ')';
/* E */
*p++ = '\n';
imp = imp->next;
}
imc_release_room(room);
/* write over last '\n' */
*(--p) = 0;
body.s = imc_body_buf;
body.len = p-body.s;
LM_DBG("members = [%.*s]\n", body.len, body.s);
imc_send_message(&room->uri, &member->uri, &imc_hdr_ctype, &body);
return 0;
error:
if(room!=NULL)
imc_release_room(room);
return -1;
}
/**
*
*/
int imc_handle_exit(struct sip_msg* msg, imc_cmd_t *cmd,
struct sip_uri *src, struct sip_uri *dst)
{
imc_room_p room = 0;
imc_member_p member = 0;
str room_name;
str body;
/* the user wants to leave the room */
room_name = cmd->param[0].s?cmd->param[0]:dst->user;
room= imc_get_room(&room_name, &dst->host);
if(room== NULL || (room->flags&IMC_ROOM_DELETED))
{
LM_ERR("room [%.*s] does not exist!\n", room_name.len, room_name.s);
goto error;
}
/* verify if the user is a member of the room */
member= imc_get_member(room, &src->user, &src->host);
if(member== NULL)
{
LM_ERR("user [%.*s] is not member of room [%.*s]!\n",
src->user.len, src->user.s, room_name.len, room_name.s);
goto error;
}
if(member->flags & IMC_MEMBER_OWNER)
{
/*If the user is the owner of the room, the room is distroyed */
room->flags |=IMC_ROOM_DELETED;
body.s = imc_body_buf;
strcpy(body.s, "The room has been destroyed");
body.len = strlen(body.s);
imc_room_broadcast(room, &imc_hdr_ctype, &body);
imc_release_room(room);
imc_del_room(&room_name, &dst->host);
room = NULL;
goto done;
} else {
/* delete user */
member->flags |= IMC_MEMBER_DELETED;
imc_del_member(room, &src->user, &src->host);
body.s = imc_body_buf;
body.len = snprintf(body.s, IMC_BUF_SIZE,
"The user [%.*s] has left the room",
src->user.len, src->user.s);
if(body.len>0)
imc_room_broadcast(room, &imc_hdr_ctype, &body);
}
done:
if(room!=NULL)
imc_release_room(room);
return 0;
error:
if(room!=NULL)
imc_release_room(room);
return -1;
}
/**
*
*/
int imc_handle_destroy(struct sip_msg* msg, imc_cmd_t *cmd,
struct sip_uri *src, struct sip_uri *dst)
{
imc_room_p room = 0;
imc_member_p member = 0;
str room_name;
str body;
/* distrugere camera */
room_name = cmd->param[0].s?cmd->param[0]:dst->user;
room= imc_get_room(&room_name, &dst->host);
if(room== NULL || (room->flags&IMC_ROOM_DELETED))
{
LM_ERR("room [%.*s] does not exist!\n", room_name.len, room_name.s);
goto error;
}
/* verify is the user is a member of the room*/
member= imc_get_member(room, &src->user, &src->host);
if(member== NULL)
{
LM_ERR("user [%.*s] is not a member of room [%.*s]!\n",
src->user.len, src->user.s, room_name.len, room_name.s);
goto error;
}
if(!(member->flags & IMC_MEMBER_OWNER))
{
LM_ERR("user [%.*s] is not owner of room [%.*s] -- cannot destroy it"
"!\n", src->user.len, src->user.s, room_name.len, room_name.s);
goto error;
}
room->flags |= IMC_ROOM_DELETED;
body.s = imc_body_buf;
strcpy(body.s, "The room has been destroyed");
body.len = strlen(body.s);
/* braodcast message */
imc_room_broadcast(room, &imc_hdr_ctype, &body);
imc_release_room(room);
LM_DBG("deleting room\n");
imc_del_room(&room_name, &dst->host);
return 0;
error:
if(room!=NULL)
imc_release_room(room);
return -1;
}
/**
*
*/
int imc_handle_help(struct sip_msg* msg, imc_cmd_t *cmd, str *src, str *dst)
{
str body;
body.s = IMC_HELP_MSG;
body.len = IMC_HELP_MSG_LEN;
LM_DBG("to: [%.*s] from: [%.*s]\n", src->len, src->s, dst->len, dst->s);
tmb.t_request(&imc_msg_type, /* Request method */
NULL, /* Request-URI */
src, /* To */
dst, /* From */
&imc_hdr_ctype, /* Headers */
&body, /* Body */
(outbound_proxy.s)?&outbound_proxy:NULL,/* outbound proxy */
NULL, /* callback function */
NULL /* callback parameter*/
);
return 0;
}
/**