]> sjero.net Git - linphone/blob - coreapi/chat.c
31f36013523e72a8164d0d53d5f260737e3a05ef
[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 #include "lpconfig.h"
28  
29  LinphoneChatRoom * linphone_core_create_chat_room(LinphoneCore *lc, const char *to){
30         LinphoneAddress *parsed_url=NULL;
31
32         if ((parsed_url=linphone_core_interpret_url(lc,to))!=NULL){
33                 LinphoneChatRoom *cr=ms_new0(LinphoneChatRoom,1);
34                 cr->lc=lc;
35                 cr->peer=linphone_address_as_string(parsed_url);
36                 cr->peer_url=parsed_url;
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         if (cr->op)
50                  sal_op_release(cr->op);
51  }
52
53
54 static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage* msg){
55         const char *route=NULL;
56         const char *identity=linphone_core_find_best_identity(cr->lc,cr->peer_url,&route);
57         SalOp *op=NULL;
58         LinphoneCall *call;
59         char* content_type;
60         
61         if (lp_config_get_int(cr->lc->config,"sip","chat_use_call_dialogs",1)){
62                 if((call = linphone_core_get_call_by_remote_address(cr->lc,cr->peer))!=NULL){
63                         if (call->state==LinphoneCallConnected ||
64                         call->state==LinphoneCallStreamsRunning ||
65                         call->state==LinphoneCallPaused ||
66                         call->state==LinphoneCallPausing ||
67                         call->state==LinphoneCallPausedByRemote){
68                                 ms_message("send SIP message through the existing call.");
69                                 op = call->op;
70                                 call->pending_message=msg;
71                         }
72                 }
73         }
74         if (op==NULL){
75                 /*sending out of calls*/
76                 op = sal_op_new(cr->lc->sal);
77                 sal_op_set_route(op,route);
78                 if (cr->op!=NULL){
79                         sal_op_release (cr->op);
80                         cr->op=NULL;
81                 }
82                 cr->op=op;
83                 sal_op_set_user_pointer(op, msg); /*if out of call, directly store msg*/
84         }
85         if (msg->external_body_url) {
86                 content_type=ms_strdup_printf("message/external-body; access-type=URL; URL=\"%s\"",msg->external_body_url);
87                 sal_message_send(op,identity,cr->peer,content_type,NULL);
88                 ms_free(content_type);
89         } else {
90                 sal_text_send(op, identity, cr->peer, msg->message);
91         }
92         
93         
94 }
95
96 void linphone_chat_room_send_message(LinphoneChatRoom *cr, const char *msg) {
97         _linphone_chat_room_send_message(cr,linphone_chat_room_create_message(cr,msg));
98 }
99 bool_t linphone_chat_room_matches(LinphoneChatRoom *cr, const LinphoneAddress *from){
100         if (linphone_address_get_username(cr->peer_url) && linphone_address_get_username(from) && 
101                 strcmp(linphone_address_get_username(cr->peer_url),linphone_address_get_username(from))==0) return TRUE;
102         return FALSE;
103 }
104
105 void linphone_chat_room_message_received(LinphoneChatRoom *cr, LinphoneCore *lc, LinphoneChatMessage *msg){
106         if (msg->message)
107                 //legacy API
108                 if (lc->vtable.text_received!=NULL) lc->vtable.text_received(lc, cr, msg->from, msg->message);
109         if (lc->vtable.message_received!=NULL) lc->vtable.message_received(lc, cr,msg);
110         
111 }
112
113 void linphone_core_message_received(LinphoneCore *lc, const char *from, const char *raw_msg,const char* external_url){
114         MSList *elem;
115         LinphoneChatRoom *cr=NULL;
116         LinphoneAddress *addr;
117         char *cleanfrom;
118         LinphoneChatMessage* msg;
119         addr=linphone_address_new(from);
120         linphone_address_clean(addr);
121         for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
122                 cr=(LinphoneChatRoom*)elem->data;
123                 if (linphone_chat_room_matches(cr,addr)){
124                         break;
125                 }
126                 cr=NULL;
127         }
128         cleanfrom=linphone_address_as_string(addr);
129         if (cr==NULL){
130                 /* create a new chat room */
131                 cr=linphone_core_create_chat_room(lc,cleanfrom);
132         }
133         msg = linphone_chat_room_create_message(cr, raw_msg);
134         linphone_chat_message_set_from(msg, cr->peer_url);
135         if (external_url) {
136                 linphone_chat_message_set_external_body_url(msg, external_url);
137         }
138         linphone_address_destroy(addr);
139         linphone_chat_room_message_received(cr,lc,msg);
140         ms_free(cleanfrom);
141 }
142
143 LinphoneCore* linphone_chat_room_get_lc(LinphoneChatRoom *cr){
144         return cr->lc;
145 }
146
147 LinphoneChatRoom* linphone_chat_message_get_chat_room(LinphoneChatMessage *msg){
148         return msg->chat_room;
149 }
150
151 void linphone_chat_room_set_user_data(LinphoneChatRoom *cr, void * ud){
152         cr->user_data=ud;
153 }
154 void * linphone_chat_room_get_user_data(LinphoneChatRoom *cr){
155         return cr->user_data;
156 }
157 const LinphoneAddress* linphone_chat_room_get_peer_address(LinphoneChatRoom *cr) {
158         return cr->peer_url;
159 }
160
161 LinphoneChatMessage* linphone_chat_room_create_message(const LinphoneChatRoom *cr,const char* message) {
162         LinphoneChatMessage* msg = ms_new0(LinphoneChatMessage,1);
163         msg->chat_room=(LinphoneChatRoom*)cr;
164         msg->message=message?ms_strdup(message):NULL;
165         return msg;
166 }
167
168 void linphone_chat_message_destroy(LinphoneChatMessage* msg) {
169         if (msg->message) ms_free(msg->message);
170         if (msg->external_body_url) ms_free(msg->external_body_url);
171         if (msg->from) linphone_address_destroy(msg->from);
172         ms_free(msg);
173 }
174
175 void linphone_chat_room_send_message2(LinphoneChatRoom *cr, LinphoneChatMessage* msg,LinphoneChatMessageStateChangeCb status_cb,void* ud) {
176         msg->cb=status_cb;
177         msg->cb_ud=ud;
178         _linphone_chat_room_send_message(cr, msg);
179 }
180
181 const char* linphone_chat_message_state_to_string(const LinphoneChatMessageState state) {
182         switch (state) {
183                 case LinphoneChatMessageStateIdle:return "LinphoneChatMessageStateIdle"; 
184                 case LinphoneChatMessageStateInProgress:return "LinphoneChatMessageStateInProgress";
185                 case LinphoneChatMessageStateDelivered:return "LinphoneChatMessageStateDelivered";
186                 case  LinphoneChatMessageStateNotDelivered:return "LinphoneChatMessageStateNotDelivered";
187                 default: return "Unknown state";
188         }
189         
190 }
191
192 char* linphone_chat_message_get_message(LinphoneChatMessage* msg) {
193         return msg->message;
194 }
195
196 const LinphoneAddress* linphone_chat_message_get_peer_address(LinphoneChatMessage *msg) {
197         return linphone_chat_room_get_peer_address(msg->chat_room);
198 }
199
200 /**
201  * user pointer set function
202  */
203 void linphone_chat_message_set_user_data(LinphoneChatMessage* message,void* ud) {
204         message->message_userdata=ud;
205 }
206 /**
207  * user pointer get function
208  */
209 void* linphone_chat_message_get_user_data(const LinphoneChatMessage* message) {
210         return message->message_userdata;
211 }
212
213 const char* linphone_chat_message_get_external_body_url(const LinphoneChatMessage* message) {
214         return message->external_body_url;
215 }
216
217 void linphone_chat_message_set_external_body_url(LinphoneChatMessage* message,const char* url) {
218         if (message->external_body_url) {
219                 ms_free(message->external_body_url);
220         }
221         message->external_body_url=url?ms_strdup(url):NULL;
222 }
223 void linphone_chat_message_set_from(LinphoneChatMessage* message, const LinphoneAddress* from) {
224         if(message->from) linphone_address_destroy(message->from);
225         message->from=linphone_address_clone(from);
226         
227 }
228 LinphoneAddress* linphone_chat_message_get_from(const LinphoneChatMessage* message) {
229         return message->from;
230 }
231 const char * linphone_chat_message_get_text(const LinphoneChatMessage* message) {
232         return message->message;
233 }
234 LinphoneChatMessage* linphone_chat_message_clone(const LinphoneChatMessage* msg) {
235         /*struct _LinphoneChatMessage {
236          char* message;
237          LinphoneChatRoom* chat_room;
238          LinphoneChatMessageStateChangeCb cb;
239          void* cb_ud;
240          void* message_userdata;
241          char* external_body_url;
242          LinphoneAddress* from;
243          };*/
244         LinphoneChatMessage* new_message = linphone_chat_room_create_message(msg->chat_room,msg->message);
245         if (msg->external_body_url) new_message->external_body_url=ms_strdup(msg->external_body_url);
246         new_message->cb=msg->cb;
247         new_message->cb_ud=msg->cb_ud;
248         new_message->message_userdata=msg->message_userdata;
249         new_message->cb=msg->cb;
250         if (msg->from) new_message->from=linphone_address_clone(msg->from);
251         return new_message;
252 }