chat.c 3.58 KB
Newer Older
aymeric's avatar
aymeric committed
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
/***************************************************************************
 *            chat.c
 *
 *  Sun Jun  5 19:34:18 2005
 *  Copyright  2005  Simon Morlat
 *  Email simon dot morlat at linphone dot org
 ****************************************************************************/

/*
 *  This program 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.
 *
 *  This program 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 "linphonecore.h"
 #include "private.h"
 
 LinphoneChatRoom * linphone_core_create_chat_room(LinphoneCore *lc, const char *to){
29
	LinphoneAddress *parsed_url=NULL;
30 31

	if ((parsed_url=linphone_core_interpret_url(lc,to))!=NULL){
aymeric's avatar
aymeric committed
32 33
		LinphoneChatRoom *cr=ms_new0(LinphoneChatRoom,1);
		cr->lc=lc;
34
		cr->peer=linphone_address_as_string(parsed_url);
aymeric's avatar
aymeric committed
35 36 37 38 39 40 41 42 43 44 45
		cr->peer_url=parsed_url;
		lc->chatrooms=ms_list_append(lc->chatrooms,(void *)cr);
		return cr;
	}
	return NULL;
 }
 
 
 void linphone_chat_room_destroy(LinphoneChatRoom *cr){
	LinphoneCore *lc=cr->lc;
	lc->chatrooms=ms_list_remove(lc->chatrooms,(void *) cr);
46
	linphone_address_destroy(cr->peer_url);
aymeric's avatar
aymeric committed
47
	ms_free(cr->peer);
48 49
	if (cr->op)
		 sal_op_release(cr->op);
aymeric's avatar
aymeric committed
50 51 52
 }
 
void linphone_chat_room_send_message(LinphoneChatRoom *cr, const char *msg){
53 54
	const char *route=NULL;
	const char *identity=linphone_core_find_best_identity(cr->lc,cr->peer_url,&route);
Simon Morlat's avatar
Simon Morlat committed
55
	SalOp *op;
56 57
	LinphoneCall *call;
	if((call = linphone_core_get_call_by_remote_address(cr->lc,cr->peer))!=NULL)
Simon Morlat's avatar
Simon Morlat committed
58 59
	{
		ms_message("send SIP message into the call\n");
60
		op = call->op;
Simon Morlat's avatar
Simon Morlat committed
61 62 63 64
	}
	else
	{
		op = sal_op_new(cr->lc->sal);
65 66 67 68 69 70
		sal_op_set_route(op,route);
		if (cr->op!=NULL){
			sal_op_release (cr->op);
			cr->op=NULL;
		}
		cr->op=op;
Simon Morlat's avatar
Simon Morlat committed
71
	}
72
	sal_text_send(op,identity,cr->peer,msg);
aymeric's avatar
aymeric committed
73 74
}

75 76 77
bool_t linphone_chat_room_matches(LinphoneChatRoom *cr, const LinphoneAddress *from){
	if (linphone_address_get_username(cr->peer_url) && linphone_address_get_username(from) && 
		strcmp(linphone_address_get_username(cr->peer_url),linphone_address_get_username(from))==0) return TRUE;
aymeric's avatar
aymeric committed
78 79 80 81 82 83 84
	return FALSE;
}

void linphone_chat_room_text_received(LinphoneChatRoom *cr, LinphoneCore *lc, const char *from, const char *msg){
	if (lc->vtable.text_received!=NULL) lc->vtable.text_received(lc, cr, from, msg);
}

85
void linphone_core_text_received(LinphoneCore *lc, const char *from, const char *msg){
aymeric's avatar
aymeric committed
86 87
	MSList *elem;
	LinphoneChatRoom *cr=NULL;
88 89
	LinphoneAddress *addr;
	char *cleanfrom;
aymeric's avatar
aymeric committed
90

91 92
	addr=linphone_address_new(from);
	linphone_address_clean(addr);
aymeric's avatar
aymeric committed
93 94
	for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
		cr=(LinphoneChatRoom*)elem->data;
95
		if (linphone_chat_room_matches(cr,addr)){
aymeric's avatar
aymeric committed
96 97 98 99
			break;
		}
		cr=NULL;
	}
100
	cleanfrom=linphone_address_as_string(addr);
aymeric's avatar
aymeric committed
101 102
	if (cr==NULL){
		/* create a new chat room */
103
		cr=linphone_core_create_chat_room(lc,cleanfrom);
aymeric's avatar
aymeric committed
104
	}
105 106 107
	linphone_address_destroy(addr);
	linphone_chat_room_text_received(cr,lc,cleanfrom,msg);
	ms_free(cleanfrom);
aymeric's avatar
aymeric committed
108 109 110 111 112 113 114 115 116
}


void linphone_chat_room_set_user_data(LinphoneChatRoom *cr, void * ud){
	cr->user_data=ud;
}
void * linphone_chat_room_get_user_data(LinphoneChatRoom *cr){
	return cr->user_data;
}