]> sjero.net Git - linphone/blob - coreapi/chat.c
73a5f7c9a1ef08205c46d87613de7adbe253ab94
[linphone] / coreapi / chat.c
1 /***************************************************************************
2  *            chat.c
3  *
4  *  Sun Jun  5 19:34:18 2005
5  *  Copyright  2005  Simon Morlat
6  *  Email simon dot morlat at linphone dot org
7  ****************************************************************************/
8
9 /*
10  *  This program 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  *  This program 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  
25  #include "linphonecore.h"
26  #include "private.h"
27  
28  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){
32                 LinphoneChatRoom *cr=ms_new0(LinphoneChatRoom,1);
33                 cr->lc=lc;
34                 cr->peer=linphone_address_as_string(parsed_url);
35                 cr->peer_url=parsed_url;
36                 cr->route=ms_strdup(linphone_core_get_route(lc));
37                 lc->chatrooms=ms_list_append(lc->chatrooms,(void *)cr);
38                 return cr;
39         }
40         return NULL;
41  }
42  
43  
44  void linphone_chat_room_destroy(LinphoneChatRoom *cr){
45         LinphoneCore *lc=cr->lc;
46         lc->chatrooms=ms_list_remove(lc->chatrooms,(void *) cr);
47         linphone_address_destroy(cr->peer_url);
48         ms_free(cr->peer);
49         ms_free(cr->route);
50  }
51  
52 void linphone_chat_room_send_message(LinphoneChatRoom *cr, const char *msg){
53         const char *identity=linphone_core_get_identity(cr->lc);
54         SalOp *op;
55         if(linphone_core_is_in_communication_with(cr->lc,cr->peer))
56         {
57                 ms_message("send SIP message into the call\n");
58                 op = cr->lc->call->op;
59         }
60         else
61         {
62                 op = sal_op_new(cr->lc->sal);
63                 sal_op_set_route(op,cr->route);
64         }
65         sal_text_send(op,identity,cr->peer,msg);
66 }
67
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;
71         return FALSE;
72 }
73
74 void linphone_chat_room_text_received(LinphoneChatRoom *cr, LinphoneCore *lc, const char *from, const char *msg){
75         if (lc->vtable.text_received!=NULL) lc->vtable.text_received(lc, cr, from, msg);
76 }
77
78 void linphone_core_text_received(LinphoneCore *lc, const char *from, const char *msg){
79         MSList *elem;
80         LinphoneChatRoom *cr=NULL;
81         LinphoneAddress *addr;
82         char *cleanfrom;
83
84         addr=linphone_address_new(from);
85         linphone_address_clean(addr);
86         for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
87                 cr=(LinphoneChatRoom*)elem->data;
88                 if (linphone_chat_room_matches(cr,addr)){
89                         break;
90                 }
91                 cr=NULL;
92         }
93         cleanfrom=linphone_address_as_string(addr);
94         if (cr==NULL){
95                 /* create a new chat room */
96                 cr=linphone_core_create_chat_room(lc,cleanfrom);
97         }
98         linphone_address_destroy(addr);
99         linphone_chat_room_text_received(cr,lc,cleanfrom,msg);
100         ms_free(cleanfrom);
101 }
102
103
104 void linphone_chat_room_set_user_data(LinphoneChatRoom *cr, void * ud){
105         cr->user_data=ud;
106 }
107 void * linphone_chat_room_get_user_data(LinphoneChatRoom *cr){
108         return cr->user_data;
109 }