25
25
#include "linphonecore.h"
26
26
#include "private.h"
27
#include <eXosip2/eXosip.h>
29
28
LinphoneChatRoom * linphone_core_create_chat_room(LinphoneCore *lc, const char *to){
31
osip_from_t *parsed_url=NULL;
33
if (linphone_core_interpret_url(lc,to,&real_url,&parsed_url,&route)){
29
LinphoneAddress *parsed_url=NULL;
31
if ((parsed_url=linphone_core_interpret_url(lc,to))!=NULL){
34
32
LinphoneChatRoom *cr=ms_new0(LinphoneChatRoom,1);
34
cr->peer=linphone_address_as_string(parsed_url);
37
35
cr->peer_url=parsed_url;
36
cr->route=ms_strdup(linphone_core_get_route(lc));
39
37
lc->chatrooms=ms_list_append(lc->chatrooms,(void *)cr);
46
44
void linphone_chat_room_destroy(LinphoneChatRoom *cr){
47
45
LinphoneCore *lc=cr->lc;
48
46
lc->chatrooms=ms_list_remove(lc->chatrooms,(void *) cr);
49
osip_from_free(cr->peer_url);
47
linphone_address_destroy(cr->peer_url);
51
49
ms_free(cr->route);
54
52
void linphone_chat_room_send_message(LinphoneChatRoom *cr, const char *msg){
55
53
const char *identity=linphone_core_get_identity(cr->lc);
56
osip_message_t *sip=NULL;
57
eXosip_message_build_request(&sip,"MESSAGE",cr->peer,identity,cr->route);
58
osip_message_set_content_type(sip,"text/plain");
59
osip_message_set_body(sip,msg,strlen(msg));
60
eXosip_message_send_request(sip);
55
if(linphone_core_is_in_communication_with(cr->lc,cr->peer))
57
ms_message("send SIP message into the call\n");
58
op = cr->lc->call->op;
62
op = sal_op_new(cr->lc->sal);
63
sal_op_set_route(op,cr->route);
65
sal_text_send(op,identity,cr->peer,msg);
63
bool_t linphone_chat_room_matches(LinphoneChatRoom *cr, osip_from_t *from){
64
if (cr->peer_url->url->username && from->url->username &&
65
strcmp(cr->peer_url->url->username,from->url->username)==0) return TRUE;
68
bool_t linphone_chat_room_matches(LinphoneChatRoom *cr, const LinphoneAddress *from){
69
if (linphone_address_get_username(cr->peer_url) && linphone_address_get_username(from) &&
70
strcmp(linphone_address_get_username(cr->peer_url),linphone_address_get_username(from))==0) return TRUE;
70
75
if (lc->vtable.text_received!=NULL) lc->vtable.text_received(lc, cr, from, msg);
73
void linphone_core_text_received(LinphoneCore *lc, eXosip_event_t *ev){
78
void linphone_core_text_received(LinphoneCore *lc, const char *from, const char *msg){
76
80
LinphoneChatRoom *cr=NULL;
81
LinphoneAddress *addr;
78
osip_from_t *from_url=ev->request->from;
79
osip_body_t *body=NULL;
81
osip_message_get_body(ev->request,0,&body);
83
ms_error("Could not get text message from SIP body");
87
from_2char_without_params(from_url,&cleanfrom);
84
addr=linphone_address_new(from);
85
linphone_address_clean(addr);
88
86
for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
89
87
cr=(LinphoneChatRoom*)elem->data;
90
if (linphone_chat_room_matches(cr,from_url)){
88
if (linphone_chat_room_matches(cr,addr)){
93
cleanfrom=linphone_address_as_string(addr);
96
95
/* create a new chat room */
97
96
cr=linphone_core_create_chat_room(lc,cleanfrom);
98
linphone_address_destroy(addr);
99
99
linphone_chat_room_text_received(cr,lc,cleanfrom,msg);
100
osip_free(cleanfrom);