]> sjero.net Git - linphone/blob - coreapi/chat.c
Aac-eld add missing header according to RFC3640 3.3.6
[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 /**
30  * @addtogroup chatroom
31  * @{
32  */
33
34 /**
35  * Create a new chat room for messaging from a sip uri like sip:joe@sip.linphone.org
36  * @param lc #LinphoneCore object
37  * @param to destination address for messages
38  * @return #LinphoneChatRoom where messaging can take place.
39  */
40 LinphoneChatRoom * linphone_core_create_chat_room(LinphoneCore *lc, const char *to){
41         LinphoneAddress *parsed_url=NULL;
42
43         if ((parsed_url=linphone_core_interpret_url(lc,to))!=NULL){
44                 LinphoneChatRoom *cr=ms_new0(LinphoneChatRoom,1);
45                 cr->lc=lc;
46                 cr->peer=linphone_address_as_string(parsed_url);
47                 cr->peer_url=parsed_url;
48                 lc->chatrooms=ms_list_append(lc->chatrooms,(void *)cr);
49                 return cr;
50         }
51         return NULL;
52 }
53  
54 /**
55  * Destroy a LinphoneChatRoom.
56  * @param cr #LinphoneChatRoom object
57  */
58 void linphone_chat_room_destroy(LinphoneChatRoom *cr){
59         LinphoneCore *lc=cr->lc;
60         lc->chatrooms=ms_list_remove(lc->chatrooms,(void *) cr);
61         linphone_address_destroy(cr->peer_url);
62         ms_free(cr->peer);
63 }
64
65
66
67 static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage* msg){
68         const char *route=NULL;
69         const char *identity=linphone_core_find_best_identity(cr->lc,cr->peer_url,&route);
70         SalOp *op=NULL;
71         LinphoneCall *call;
72         char* content_type;
73         time_t t=time(NULL);
74         
75         if (lp_config_get_int(cr->lc->config,"sip","chat_use_call_dialogs",0)){
76                 if((call = linphone_core_get_call_by_remote_address(cr->lc,cr->peer))!=NULL){
77                         if (call->state==LinphoneCallConnected ||
78                         call->state==LinphoneCallStreamsRunning ||
79                         call->state==LinphoneCallPaused ||
80                         call->state==LinphoneCallPausing ||
81                         call->state==LinphoneCallPausedByRemote){
82                                 ms_message("send SIP message through the existing call.");
83                                 op = call->op;
84                                 call->pending_message=msg;
85                         }
86                 }
87         }
88         msg->time=t;
89         if (op==NULL){
90                 /*sending out of calls*/
91                 op = sal_op_new(cr->lc->sal);
92                 sal_op_set_route(op,route);
93                 sal_op_set_user_pointer(op, msg); /*if out of call, directly store msg*/
94                 if (msg->custom_headers){
95                         sal_op_set_custom_header(op,msg->custom_headers);
96                         msg->custom_headers=NULL; /*transfered to the SalOp*/
97                 }
98         }
99         if (msg->external_body_url) {
100                 content_type=ms_strdup_printf("message/external-body; access-type=URL; URL=\"%s\"",msg->external_body_url);
101                 sal_message_send(op,identity,cr->peer,content_type, NULL);
102                 ms_free(content_type);
103         } else {
104                 sal_text_send(op, identity, cr->peer,msg->message);
105         }
106         msg->dir=LinphoneChatMessageOutgoing;
107         msg->from=linphone_address_new(identity);
108         linphone_chat_message_store(msg);
109 }
110
111 /**
112  * Send a message to peer member of this chat room.
113  * @deprecated linphone_chat_room_send_message2() gives more control on the message expedition.
114  * @param cr #LinphoneChatRoom object
115  * @param msg message to be sent
116  */
117 void linphone_chat_room_send_message(LinphoneChatRoom *cr, const char *msg) {
118         _linphone_chat_room_send_message(cr,linphone_chat_room_create_message(cr,msg));
119 }
120
121 bool_t linphone_chat_room_matches(LinphoneChatRoom *cr, const LinphoneAddress *from){
122         if (linphone_address_get_username(cr->peer_url) && linphone_address_get_username(from) && 
123                 strcmp(linphone_address_get_username(cr->peer_url),linphone_address_get_username(from))==0) return TRUE;
124         return FALSE;
125 }
126
127 void linphone_chat_room_message_received(LinphoneChatRoom *cr, LinphoneCore *lc, LinphoneChatMessage *msg){
128         if (msg->message)
129                 //legacy API
130                 if (lc->vtable.text_received!=NULL) lc->vtable.text_received(lc, cr, msg->from, msg->message);
131         if (lc->vtable.message_received!=NULL) lc->vtable.message_received(lc, cr,msg);
132         
133 }
134
135 /**
136  * Retrieve an existing chat room whose peer is the supplied address, if exists.
137  * @param lc the linphone core
138  * @param add a linphone address.
139  * @returns the matching chatroom, or NULL if no such chatroom exists.
140 **/
141 LinphoneChatRoom *linphone_core_get_chat_room(LinphoneCore *lc, const LinphoneAddress *addr){
142         LinphoneChatRoom *cr=NULL;
143         MSList *elem;
144         for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
145                 cr=(LinphoneChatRoom*)elem->data;
146                 if (linphone_chat_room_matches(cr,addr)){
147                         break;
148                 }
149                 cr=NULL;
150         }
151         return cr;
152 }
153
154 void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessage *sal_msg){
155         
156         LinphoneChatRoom *cr=NULL;
157         LinphoneAddress *addr;
158         char *cleanfrom;
159         char *from;
160         LinphoneChatMessage* msg;
161         const SalCustomHeader *ch;
162         
163         addr=linphone_address_new(sal_msg->from);
164         linphone_address_clean(addr);
165         cr=linphone_core_get_chat_room(lc,addr);
166         cleanfrom=linphone_address_as_string(addr);
167         from=linphone_address_as_string_uri_only(addr);
168         if (cr==NULL){
169                 /* create a new chat room */
170                 cr=linphone_core_create_chat_room(lc,cleanfrom);
171         }
172         msg = linphone_chat_room_create_message(cr, sal_msg->text);
173         linphone_chat_message_set_from(msg, cr->peer_url);
174         
175         {
176                 LinphoneAddress *to;
177                 to=sal_op_get_to(op) ? linphone_address_new(sal_op_get_to(op)) : linphone_address_new(linphone_core_get_identity(lc));
178                 msg->to=to;
179         }
180         
181         msg->time=sal_msg->time;
182         msg->state=LinphoneChatMessageStateDelivered;
183         msg->is_read=FALSE;
184         ch=sal_op_get_custom_header(op);
185         if (ch) msg->custom_headers=sal_custom_header_clone(ch);
186         
187         if (sal_msg->url) {
188                 linphone_chat_message_set_external_body_url(msg, sal_msg->url);
189         }
190         linphone_address_destroy(addr);
191         linphone_chat_message_store(msg);
192         linphone_chat_room_message_received(cr,lc,msg);
193         ms_free(cleanfrom);
194         ms_free(from);
195 }
196
197 /**
198  * Returns back pointer to LinphoneCore object.
199 **/
200 LinphoneCore* linphone_chat_room_get_lc(LinphoneChatRoom *cr){
201         return cr->lc;
202 }
203
204 /**
205  * Assign a user pointer to the chat room.
206 **/
207 void linphone_chat_room_set_user_data(LinphoneChatRoom *cr, void * ud){
208         cr->user_data=ud;
209 }
210
211 /**
212  * Retrieve the user pointer associated with the chat room.
213 **/
214 void * linphone_chat_room_get_user_data(LinphoneChatRoom *cr){
215         return cr->user_data;
216 }
217
218 /**
219  * get peer address \link linphone_core_create_chat_room() associated to \endlink this #LinphoneChatRoom
220  * @param cr #LinphoneChatRoom object
221  * @return #LinphoneAddress peer address
222  */
223 const LinphoneAddress* linphone_chat_room_get_peer_address(LinphoneChatRoom *cr) {
224         return cr->peer_url;
225 }
226
227 /**
228  * Create a message attached to a dedicated chat room;
229  * @param cr the chat room.
230  * @param message text message, NULL if absent.
231  * @return a new #LinphoneChatMessage
232  */
233 LinphoneChatMessage* linphone_chat_room_create_message(LinphoneChatRoom *cr, const char* message) {
234         LinphoneChatMessage* msg = ms_new0(LinphoneChatMessage,1);
235         msg->chat_room=(LinphoneChatRoom*)cr;
236         msg->message=message?ms_strdup(message):NULL;
237         msg->is_read=TRUE;
238         return msg;
239 }
240
241 /**
242  * Send a message to peer member of this chat room.
243  * @param cr #LinphoneChatRoom object
244  * @param msg #LinphoneChatMessage message to be sent
245  * @param status_cb LinphoneChatMessageStateChangeCb status callback invoked when message is delivered or could not be delivered. May be NULL
246  * @param ud user data for the status cb.
247  * @note The LinphoneChatMessage must not be destroyed until the the callback is called.
248  */
249 void linphone_chat_room_send_message2(LinphoneChatRoom *cr, LinphoneChatMessage* msg,LinphoneChatMessageStateChangeCb status_cb, void* ud) {
250         msg->cb=status_cb;
251         msg->cb_ud=ud;
252         msg->state=LinphoneChatMessageStateInProgress;
253         _linphone_chat_room_send_message(cr, msg);
254 }
255
256 /**
257  * Returns a #LinphoneChatMessageState as a string.
258  */
259 const char* linphone_chat_message_state_to_string(const LinphoneChatMessageState state) {
260         switch (state) {
261                 case LinphoneChatMessageStateIdle:return "LinphoneChatMessageStateIdle"; 
262                 case LinphoneChatMessageStateInProgress:return "LinphoneChatMessageStateInProgress";
263                 case LinphoneChatMessageStateDelivered:return "LinphoneChatMessageStateDelivered";
264                 case  LinphoneChatMessageStateNotDelivered:return "LinphoneChatMessageStateNotDelivered";
265                 default: return "Unknown state";
266         }
267         
268 }
269
270 /**
271  * Returns the chatroom this message belongs to.
272 **/
273 LinphoneChatRoom* linphone_chat_message_get_chat_room(LinphoneChatMessage *msg){
274         return msg->chat_room;
275 }
276
277 /**
278  * Returns the peer (remote) address for the message.
279 **/
280 const LinphoneAddress* linphone_chat_message_get_peer_address(LinphoneChatMessage *msg) {
281         return linphone_chat_room_get_peer_address(msg->chat_room);
282 }
283
284 /**
285  *User pointer set function
286  */
287 void linphone_chat_message_set_user_data(LinphoneChatMessage* message,void* ud) {
288         message->message_userdata=ud;
289 }
290
291 /**
292  * User pointer get function
293  */
294 void* linphone_chat_message_get_user_data(const LinphoneChatMessage* message) {
295         return message->message_userdata;
296 }
297
298 /**
299  * Linphone message can carry external body as defined by rfc2017
300  * @param message #LinphoneChatMessage
301  * @return external body url or NULL if not present.
302  */
303 const char* linphone_chat_message_get_external_body_url(const LinphoneChatMessage* message) {
304         return message->external_body_url;
305 }
306
307 /**
308  * Linphone message can carry external body as defined by rfc2017
309  * 
310  * @param message a LinphoneChatMessage  
311  * @param url ex: access-type=URL; URL="http://www.foo.com/file"
312  */
313 void linphone_chat_message_set_external_body_url(LinphoneChatMessage* message,const char* url) {
314         if (message->external_body_url) {
315                 ms_free(message->external_body_url);
316         }
317         message->external_body_url=url?ms_strdup(url):NULL;
318 }
319
320 /**
321  * Set origin of the message
322  *@param message #LinphoneChatMessage obj
323  *@param from #LinphoneAddress origin of this message (copied)
324  */
325 void linphone_chat_message_set_from(LinphoneChatMessage* message, const LinphoneAddress* from) {
326         if(message->from) linphone_address_destroy(message->from);
327         message->from=linphone_address_clone(from);
328 }
329
330 /**
331  * Get origin of the message 
332  *@param message #LinphoneChatMessage obj
333  *@return #LinphoneAddress
334  */
335 const LinphoneAddress* linphone_chat_message_get_from(const LinphoneChatMessage* message) {
336         return message->from;
337 }
338
339 /**
340  * Get destination of the message 
341  *@param message #LinphoneChatMessage obj
342  *@return #LinphoneAddress
343  */
344 const LinphoneAddress* linphone_chat_message_get_to(const LinphoneChatMessage* message){
345         if (message->to) return message->to;
346         if (message->dir==LinphoneChatMessageOutgoing){
347                 return message->chat_room->peer_url;
348         }
349         return NULL;
350 }
351
352 /**
353  * Returns the origin address of a message if it was a outgoing message, or the destination address if it was an incoming message.
354  *@param message #LinphoneChatMessage obj
355  *@return #LinphoneAddress
356  */
357 LinphoneAddress *linphone_chat_message_get_local_address(const LinphoneChatMessage* message){
358         return message->dir==LinphoneChatMessageOutgoing ? message->from : message->to;
359 }
360
361 /**
362  * Get the time the message was sent.
363  */
364 time_t linphone_chat_message_get_time(const LinphoneChatMessage* message) {
365         return message->time;
366 }
367
368 /**
369  * Get the state of the message
370  *@param message #LinphoneChatMessage obj
371  *@return #LinphoneChatMessageState
372  */
373 LinphoneChatMessageState linphone_chat_message_get_state(const LinphoneChatMessage* message) {
374         return message->state;
375 }
376
377 /**
378  * Get text part of this message
379  * @return text or NULL if no text.
380  */
381 const char * linphone_chat_message_get_text(const LinphoneChatMessage* message) {
382         return message->message;
383 }
384
385 /**
386  * Add custom headers to the message.
387  * @param message the message
388  * @param header_name name of the header_name
389  * @param header_value header value
390 **/
391 void linphone_chat_message_add_custom_header(LinphoneChatMessage* message, const char *header_name, const char *header_value){
392         message->custom_headers=sal_custom_header_append(message->custom_headers,header_name,header_value);
393 }
394
395 /**
396  * Retrieve a custom header value given its name.
397  * @param message the message
398  * @param header_name header name searched
399 **/
400 const char * linphone_chat_message_get_custom_header(LinphoneChatMessage* message, const char *header_name){
401         return sal_custom_header_find(message->custom_headers,header_name);
402 }
403
404 /**
405  * Duplicate a LinphoneChatMessage
406 **/
407 LinphoneChatMessage* linphone_chat_message_clone(const LinphoneChatMessage* msg) {
408         /*struct _LinphoneChatMessage {
409          char* message;
410          LinphoneChatRoom* chat_room;
411          LinphoneChatMessageStateChangeCb cb;
412          void* cb_ud;
413          void* message_userdata;
414          char* external_body_url;
415          LinphoneAddress* from;
416          time_t time;
417          SalCustomHeader *custom_headers;
418          LinphoneChatMessageState state;
419          };*/
420         LinphoneChatMessage* new_message = linphone_chat_room_create_message(msg->chat_room,msg->message);
421         if (msg->external_body_url) new_message->external_body_url=ms_strdup(msg->external_body_url);
422         new_message->cb=msg->cb;
423         new_message->cb_ud=msg->cb_ud;
424         new_message->message_userdata=msg->message_userdata;
425         new_message->cb=msg->cb;
426         new_message->time=msg->time;
427         new_message->state=msg->state;
428         if (msg->from) new_message->from=linphone_address_clone(msg->from);
429         return new_message;
430 }
431
432 /**
433  * Destroys a LinphoneChatMessage.
434 **/
435 void linphone_chat_message_destroy(LinphoneChatMessage* msg) {
436         if (msg->message) ms_free(msg->message);
437         if (msg->external_body_url) ms_free(msg->external_body_url);
438         if (msg->from) linphone_address_destroy(msg->from);
439         if (msg->to) linphone_address_destroy(msg->to);
440         if (msg->custom_headers) sal_custom_header_free(msg->custom_headers);
441         ms_free(msg);
442 }
443
444
445 /**
446  * @}
447  */
448
449