conf: allow to reject incoming AV conference call
[siplcs.git] / src / core / sipe-im.c
blobc3253b185388bac4ae29add37cbd047a9f110862
1 /**
2 * @file sipe-im.c
4 * pidgin-sipe
6 * Copyright (C) 2011 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <stdlib.h>
28 #include <string.h>
30 #include <glib.h>
32 #include "sipe-common.h"
33 #include "sipmsg.h"
34 #include "sip-transport.h"
35 #include "sipe-backend.h"
36 #include "sipe-chat.h"
37 #include "sipe-core.h"
38 #include "sipe-core-private.h"
39 #include "sipe-dialog.h"
40 #include "sipe-ft.h"
41 #include "sipe-groupchat.h"
42 #include "sipe-im.h"
43 #include "sipe-nls.h"
44 #include "sipe-session.h"
45 #include "sipe-user.h"
46 #include "sipe-utils.h"
47 #include "sipe-xml.h"
50 * Hash key template for unconfirmed messages
52 * Call-ID Recipient URI (or empty)
53 * | |
54 * | SIP method | CSeq
55 * | | | | */
56 #define UNCONFIRMED_KEY_TEMPLATE(method, cseq) "<%s><" method "><%s><" cseq
58 /* key must be g_free()'d */
59 static gchar *get_unconfirmed_message_key(const gchar *callid,
60 unsigned int cseq,
61 const gchar *with)
63 return(g_strdup_printf(UNCONFIRMED_KEY_TEMPLATE("%s", "%d>"),
64 callid,
65 with ? "MESSAGE" : "INVITE",
66 with ? with : "",
67 cseq));
70 static void insert_unconfirmed_message(struct sip_session *session,
71 struct sip_dialog *dialog,
72 const gchar *with,
73 const gchar *body,
74 const gchar *content_type)
76 gchar *key = get_unconfirmed_message_key(dialog->callid, dialog->cseq + 1, with);
77 struct queued_message *message = g_new0(struct queued_message, 1);
79 message->body = g_strdup(body);
80 if (content_type != NULL)
81 message->content_type = g_strdup(content_type);
82 message->cseq = dialog->cseq + 1;
84 g_hash_table_insert(session->unconfirmed_messages, key, message);
85 SIPE_DEBUG_INFO("insert_unconfirmed_message: added %s to list (count=%d)",
86 key, g_hash_table_size(session->unconfirmed_messages));
89 static gboolean remove_unconfirmed_message(struct sip_session *session,
90 const gchar *key)
92 gboolean found = g_hash_table_remove(session->unconfirmed_messages, key);
93 if (found) {
94 SIPE_DEBUG_INFO("remove_unconfirmed_message: removed %s from list (count=%d)",
95 key, g_hash_table_size(session->unconfirmed_messages));
96 } else {
97 SIPE_DEBUG_INFO("remove_unconfirmed_message: key %s not found",
98 key);
100 return(found);
103 static void sipe_refer_notify(struct sipe_core_private *sipe_private,
104 struct sip_session *session,
105 const gchar *who,
106 int status,
107 const gchar *desc)
109 gchar *hdr;
110 gchar *body;
111 struct sip_dialog *dialog = sipe_dialog_find(session, who);
113 hdr = g_strdup_printf(
114 "Event: refer\r\n"
115 "Subscription-State: %s\r\n"
116 "Content-Type: message/sipfrag\r\n",
117 status >= 200 ? "terminated" : "active");
119 body = g_strdup_printf(
120 "SIP/2.0 %d %s\r\n",
121 status, desc);
123 sip_transport_request(sipe_private,
124 "NOTIFY",
125 who,
126 who,
127 hdr,
128 body,
129 dialog,
130 NULL);
132 g_free(hdr);
133 g_free(body);
136 static gchar *get_buddy_alias(struct sipe_core_private *sipe_private,
137 const gchar *with)
139 sipe_backend_buddy pbuddy;
140 gchar *alias = NULL;
141 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
142 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
144 return(alias);
147 static gboolean process_invite_response(struct sipe_core_private *sipe_private,
148 struct sipmsg *msg,
149 struct transaction *trans)
151 gchar *with = parse_from(sipmsg_find_header(msg, "To"));
152 struct sip_session *session;
153 struct sip_dialog *dialog;
154 gchar *key;
155 struct queued_message *message;
156 struct sipmsg *request_msg = trans->msg;
158 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
159 gchar *referred_by;
161 session = sipe_session_find_chat_or_im(sipe_private, callid, with);
162 if (!session) {
163 SIPE_DEBUG_INFO_NOFORMAT("process_invite_response: unable to find IM session");
164 g_free(with);
165 return FALSE;
168 dialog = sipe_dialog_find(session, with);
169 if (!dialog) {
170 SIPE_DEBUG_INFO_NOFORMAT("process_invite_response: session outgoing dialog is NULL");
171 g_free(with);
172 return FALSE;
175 sipe_dialog_parse(dialog, msg, TRUE);
177 key = get_unconfirmed_message_key(dialog->callid, sipmsg_parse_cseq(msg), NULL);
178 message = g_hash_table_lookup(session->unconfirmed_messages, key);
180 if (msg->response != 200) {
181 gchar *alias = get_buddy_alias(sipe_private, with);
182 const char *warn_hdr = sipmsg_find_header(msg, "Warning");
183 int warning = -1;
185 SIPE_DEBUG_INFO_NOFORMAT("process_invite_response: INVITE response not 200");
187 if (warn_hdr) {
188 gchar **parts = g_strsplit(warn_hdr, " ", 2);
189 if (parts[0]) {
190 warning = atoi(parts[0]);
192 g_strfreev(parts);
195 /* cancel file transfer as rejected by server */
196 if (msg->response == 606 && /* Not acceptable all. */
197 warning == 309 && /* Message contents not allowed by policy */
198 message && g_str_has_prefix(message->content_type, "text/x-msmsgsinvite"))
200 GSList *parsed_body = sipe_ft_parse_msg_body(message->body);
201 sipe_ft_incoming_cancel(dialog, parsed_body);
202 sipe_utils_nameval_free(parsed_body);
205 if (message) {
206 /* generate error for each unprocessed message */
207 GSList *entry = session->outgoing_message_queue;
208 while (entry) {
209 struct queued_message *queued = entry->data;
210 sipe_user_present_message_undelivered(sipe_private, session, msg->response, warning, alias ? alias : with, queued->body);
211 entry = sipe_session_dequeue_message(session);
213 } else {
214 /* generate one error and remove all unprocessed messages */
215 gchar *tmp_msg = g_strdup_printf(_("Failed to invite %s"), alias ? alias : with);
216 sipe_user_present_error(sipe_private, session, tmp_msg);
217 g_free(tmp_msg);
218 while (sipe_session_dequeue_message(session));
220 g_free(alias);
222 remove_unconfirmed_message(session, key);
223 /* message is no longer valid */
224 g_free(key);
226 sipe_dialog_remove(session, with);
227 g_free(with);
229 if (session->is_groupchat) {
230 sipe_groupchat_invite_failed(sipe_private, session);
231 /* session is no longer valid */
234 return FALSE;
237 dialog->cseq = 0;
238 sip_transport_ack(sipe_private, dialog);
239 dialog->outgoing_invite = NULL;
240 dialog->is_established = TRUE;
242 referred_by = parse_from(sipmsg_find_header(request_msg, "Referred-By"));
243 if (referred_by) {
244 sipe_refer_notify(sipe_private, session, referred_by, 200, "OK");
245 g_free(referred_by);
248 /* add user to chat if it is a multiparty session */
249 if (session->chat_session &&
250 (session->chat_session->type == SIPE_CHAT_TYPE_MULTIPARTY)) {
251 sipe_backend_chat_add(session->chat_session->backend,
252 with,
253 TRUE);
256 if (session->is_groupchat) {
257 sipe_groupchat_invite_response(sipe_private, dialog);
260 if(g_slist_find_custom(dialog->supported, "ms-text-format", (GCompareFunc)g_ascii_strcasecmp)) {
261 SIPE_DEBUG_INFO_NOFORMAT("process_invite_response: remote system accepted message in INVITE");
262 sipe_session_dequeue_message(session);
265 sipe_im_process_queue(sipe_private, session);
267 remove_unconfirmed_message(session, key);
269 g_free(key);
270 g_free(with);
271 return TRUE;
274 /* EndPoints: "alice alisson" <sip:alice@atlanta.local>, <sip:bob@atlanta.local>;epid=ebca82d94d, <sip:carol@atlanta.local> */
275 static gchar *get_end_points(struct sipe_core_private *sipe_private,
276 struct sip_session *session)
278 gchar *res;
280 if (session == NULL) {
281 return NULL;
284 res = g_strdup_printf("<sip:%s>", sipe_private->username);
286 SIPE_DIALOG_FOREACH {
287 gchar *tmp = res;
288 res = g_strdup_printf("%s, <%s>", res, dialog->with);
289 g_free(tmp);
291 if (dialog->theirepid) {
292 tmp = res;
293 res = g_strdup_printf("%s;epid=%s", res, dialog->theirepid);
294 g_free(tmp);
296 } SIPE_DIALOG_FOREACH_END;
298 return res;
301 void
302 sipe_im_invite(struct sipe_core_private *sipe_private,
303 struct sip_session *session,
304 const gchar *who,
305 const gchar *msg_body,
306 const gchar *content_type,
307 const gchar *referred_by,
308 const gboolean is_triggered)
310 gchar *hdr;
311 gchar *to;
312 gchar *contact;
313 gchar *body;
314 gchar *self;
315 char *ms_text_format = NULL;
316 char *ms_conversation_id = NULL;
317 gchar *roster_manager;
318 gchar *end_points;
319 gchar *referred_by_str;
320 gboolean is_multiparty =
321 session->chat_session &&
322 (session->chat_session->type == SIPE_CHAT_TYPE_MULTIPARTY);
323 struct sip_dialog *dialog = sipe_dialog_find(session, who);
325 if (dialog && dialog->is_established) {
326 SIPE_DEBUG_INFO("session with %s already has a dialog open", who);
327 return;
330 if (!dialog) {
331 dialog = sipe_dialog_add(session);
332 dialog->callid = session->callid ? g_strdup(session->callid) : gencallid();
333 dialog->with = g_strdup(who);
336 if (!(dialog->ourtag)) {
337 dialog->ourtag = gentag();
340 to = sip_uri(who);
342 if (msg_body) {
343 char *msgtext = NULL;
344 char *base64_msg;
345 const gchar *msgr = "";
346 gchar *tmp = NULL;
348 if (!g_str_has_prefix(content_type, "text/x-msmsgsinvite")) {
349 char *msgformat;
350 gchar *msgr_value;
352 sipe_parse_html(msg_body, &msgformat, &msgtext);
353 SIPE_DEBUG_INFO("sipe_invite: msgformat=%s", msgformat);
355 msgr_value = sipmsg_get_msgr_string(msgformat);
356 g_free(msgformat);
357 if (msgr_value) {
358 msgr = tmp = g_strdup_printf(";msgr=%s", msgr_value);
359 g_free(msgr_value);
362 /* When Sipe reconnects after a crash, we are not able
363 * to send messages to contacts with which we had open
364 * conversations when the crash occured. Server sends
365 * error response with reason="This client has an IM
366 * session with the same conversation ID"
368 * Setting random Ms-Conversation-ID prevents this problem
369 * so we can continue the conversation. */
370 ms_conversation_id = g_strdup_printf("Ms-Conversation-ID: %u\r\n",
371 rand() % 1000000000);
372 } else {
373 msgtext = g_strdup(msg_body);
376 base64_msg = g_base64_encode((guchar*) msgtext, strlen(msgtext));
377 ms_text_format = g_strdup_printf("ms-text-format: %s; charset=UTF-8%s;ms-body=%s\r\n",
378 content_type ? content_type : "text/plain",
379 msgr,
380 base64_msg);
381 g_free(msgtext);
382 g_free(tmp);
383 g_free(base64_msg);
385 insert_unconfirmed_message(session, dialog, NULL,
386 msg_body, content_type);
389 contact = get_contact(sipe_private);
390 end_points = get_end_points(sipe_private, session);
391 self = sip_uri_self(sipe_private);
392 roster_manager = g_strdup_printf(
393 "Roster-Manager: %s\r\n"
394 "EndPoints: %s\r\n",
395 self,
396 end_points);
397 referred_by_str = referred_by ?
398 g_strdup_printf(
399 "Referred-By: %s\r\n",
400 referred_by)
401 : g_strdup("");
402 hdr = g_strdup_printf(
403 "Supported: ms-sender\r\n"
404 "%s"
405 "%s"
406 "%s"
407 "%s"
408 "Contact: %s\r\n%s"
409 "%s"
410 "Content-Type: application/sdp\r\n",
411 is_multiparty && sipe_strcase_equal(session->chat_session->id, self) ? roster_manager : "",
412 referred_by_str,
413 is_triggered ? "TriggeredInvite: TRUE\r\n" : "",
414 is_triggered || is_multiparty ? "Require: com.microsoft.rtc-multiparty\r\n" : "",
415 contact,
416 ms_text_format ? ms_text_format : "",
417 ms_conversation_id ? ms_conversation_id : "");
418 g_free(ms_text_format);
419 g_free(ms_conversation_id);
420 g_free(self);
422 body = g_strdup_printf(
423 "v=0\r\n"
424 "o=- 0 0 IN IP4 %s\r\n"
425 "s=session\r\n"
426 "c=IN IP4 %s\r\n"
427 "t=0 0\r\n"
428 "m=%s %d sip null\r\n"
429 "a=accept-types:" SDP_ACCEPT_TYPES "\r\n",
430 sipe_backend_network_ip_address(),
431 sipe_backend_network_ip_address(),
432 SIPE_CORE_PRIVATE_FLAG_IS(OCS2007) ? "message" : "x-ms-message",
433 sip_transport_port(sipe_private));
435 dialog->outgoing_invite = sip_transport_request(sipe_private,
436 "INVITE",
439 hdr,
440 body,
441 dialog,
442 process_invite_response);
444 g_free(to);
445 g_free(roster_manager);
446 g_free(end_points);
447 g_free(referred_by_str);
448 g_free(body);
449 g_free(hdr);
450 g_free(contact);
453 static gboolean
454 process_message_response(struct sipe_core_private *sipe_private,
455 struct sipmsg *msg,
456 SIPE_UNUSED_PARAMETER struct transaction *trans)
458 gboolean ret = TRUE;
459 gchar *with = parse_from(sipmsg_find_header(msg, "To"));
460 struct sip_session *session = sipe_session_find_im(sipe_private, with);
461 struct sip_dialog *dialog;
462 gchar *key;
463 struct queued_message *message;
465 if (!session) {
466 SIPE_DEBUG_INFO_NOFORMAT("process_message_response: unable to find IM session");
467 g_free(with);
468 return FALSE;
471 dialog = sipe_dialog_find(session, with);
472 if (!dialog) {
473 SIPE_DEBUG_INFO_NOFORMAT("process_message_response: session outgoing dialog is NULL");
474 g_free(with);
475 return FALSE;
478 key = get_unconfirmed_message_key(sipmsg_find_header(msg, "Call-ID"), sipmsg_parse_cseq(msg), with);
479 message = g_hash_table_lookup(session->unconfirmed_messages, key);
481 if (msg->response >= 400) {
482 const char *warn_hdr = sipmsg_find_header(msg, "Warning");
483 int warning = -1;
485 SIPE_DEBUG_INFO_NOFORMAT("process_message_response: MESSAGE response >= 400");
487 if (warn_hdr) {
488 gchar **parts = g_strsplit(warn_hdr, " ", 2);
489 if (parts[0]) {
490 warning = atoi(parts[0]);
492 g_strfreev(parts);
495 /* cancel file transfer as rejected by server */
496 if (msg->response == 606 && /* Not acceptable all. */
497 warning == 309 && /* Message contents not allowed by policy */
498 message && g_str_has_prefix(message->content_type, "text/x-msmsgsinvite"))
500 GSList *parsed_body = sipe_ft_parse_msg_body(msg->body);
501 sipe_ft_incoming_cancel(dialog, parsed_body);
502 sipe_utils_nameval_free(parsed_body);
505 /* drop dangling IM sessions: assume that BYE from remote never reached us */
506 if (msg->response == 408 || /* Request timeout */
507 msg->response == 480 || /* Temporarily Unavailable */
508 msg->response == 481) { /* Call/Transaction Does Not Exist */
509 sipe_im_cancel_dangling(sipe_private, session, dialog, with,
510 sipe_im_cancel_unconfirmed);
511 /* dialog is no longer valid */
512 } else {
513 gchar *alias = get_buddy_alias(sipe_private, with);
514 sipe_user_present_message_undelivered(sipe_private, session,
515 msg->response, warning,
516 alias ? alias : with,
517 message ? message->body : NULL);
518 remove_unconfirmed_message(session, key);
519 /* message is no longer valid */
520 g_free(alias);
523 ret = FALSE;
524 } else {
525 const gchar *message_id = sipmsg_find_header(msg, "Message-Id");
526 if (message_id) {
527 g_hash_table_insert(session->conf_unconfirmed_messages, g_strdup(message_id), g_strdup(message->body));
528 SIPE_DEBUG_INFO("process_message_response: added message with id %s to conf_unconfirmed_messages(count=%d)",
529 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
531 remove_unconfirmed_message(session, key);
534 g_free(key);
535 g_free(with);
537 if (ret) sipe_im_process_queue(sipe_private, session);
538 return ret;
541 static gboolean
542 process_message_timeout(struct sipe_core_private *sipe_private,
543 struct sipmsg *msg,
544 SIPE_UNUSED_PARAMETER struct transaction *trans)
546 gchar *with = parse_from(sipmsg_find_header(msg, "To"));
547 struct sip_session *session = sipe_session_find_im(sipe_private, with);
548 gchar *key;
549 gboolean found;
551 if (!session) {
552 SIPE_DEBUG_INFO_NOFORMAT("process_message_timeout: unable to find IM session");
553 g_free(with);
554 return TRUE;
557 /* Remove timed-out message from unconfirmed list */
558 key = get_unconfirmed_message_key(sipmsg_find_header(msg, "Call-ID"), sipmsg_parse_cseq(msg), with);
559 found = remove_unconfirmed_message(session, key);
560 g_free(key);
562 if (found) {
563 gchar *alias = get_buddy_alias(sipe_private, with);
564 sipe_user_present_message_undelivered(sipe_private, session, -1, -1,
565 alias ? alias : with,
566 msg->body);
567 g_free(alias);
570 g_free(with);
571 return TRUE;
574 static void sipe_im_send_message(struct sipe_core_private *sipe_private,
575 struct sip_dialog *dialog,
576 const gchar *msg_body,
577 const gchar *content_type)
579 gchar *hdr;
580 gchar *tmp;
581 char *msgtext = NULL;
582 const gchar *msgr = "";
583 gchar *tmp2 = NULL;
585 if (content_type == NULL)
586 content_type = "text/plain";
588 if (!g_str_has_prefix(content_type, "text/x-msmsgsinvite")) {
589 char *msgformat;
590 gchar *msgr_value;
592 sipe_parse_html(msg_body, &msgformat, &msgtext);
593 SIPE_DEBUG_INFO("sipe_send_message: msgformat=%s", msgformat);
595 msgr_value = sipmsg_get_msgr_string(msgformat);
596 g_free(msgformat);
597 if (msgr_value) {
598 msgr = tmp2 = g_strdup_printf(";msgr=%s", msgr_value);
599 g_free(msgr_value);
601 } else {
602 msgtext = g_strdup(msg_body);
605 tmp = get_contact(sipe_private);
606 //hdr = g_strdup("Content-Type: text/plain; charset=UTF-8\r\n");
607 //hdr = g_strdup("Content-Type: text/rtf\r\n");
608 //hdr = g_strdup("Content-Type: text/plain; charset=UTF-8;msgr=WAAtAE0ATQBTAC....AoADQA\r\nSupported: timer\r\n");
610 hdr = g_strdup_printf("Contact: %s\r\nContent-Type: %s; charset=UTF-8%s\r\n", tmp, content_type, msgr);
611 g_free(tmp);
612 g_free(tmp2);
614 sip_transport_request_timeout(sipe_private,
615 "MESSAGE",
616 dialog->with,
617 dialog->with,
618 hdr,
619 msgtext,
620 dialog,
621 process_message_response,
623 process_message_timeout);
624 g_free(msgtext);
625 g_free(hdr);
628 void sipe_im_process_queue(struct sipe_core_private *sipe_private,
629 struct sip_session *session)
631 GSList *entry2 = session->outgoing_message_queue;
632 while (entry2) {
633 struct queued_message *msg = entry2->data;
635 /* for multiparty chat or conference */
636 if (session->chat_session) {
637 gchar *who = sip_uri_self(sipe_private);
638 sipe_backend_chat_message(SIPE_CORE_PUBLIC,
639 session->chat_session->backend,
640 who,
641 msg->body);
642 g_free(who);
645 SIPE_DIALOG_FOREACH {
646 if (dialog->outgoing_invite) continue; /* do not send messages as INVITE is not responded. */
648 insert_unconfirmed_message(session, dialog, dialog->with,
649 msg->body, msg->content_type);
651 sipe_im_send_message(sipe_private, dialog, msg->body, msg->content_type);
652 } SIPE_DIALOG_FOREACH_END;
654 entry2 = sipe_session_dequeue_message(session);
658 struct unconfirmed_callback_data {
659 const gchar *prefix;
660 GSList *list;
663 struct unconfirmed_message {
664 const gchar *key;
665 const struct queued_message *msg;
668 static gint compare_cseq(gconstpointer a,
669 gconstpointer b)
671 return(((struct unconfirmed_message *) a)->msg->cseq -
672 ((struct unconfirmed_message *) b)->msg->cseq);
675 static void unconfirmed_message_callback(gpointer key,
676 gpointer value,
677 gpointer user_data)
679 const gchar *message_key = key;
680 struct unconfirmed_callback_data *data = user_data;
682 SIPE_DEBUG_INFO("unconfirmed_message_callback: key %s", message_key);
684 /* Put messages with the same prefix on a list sorted by CSeq */
685 if (g_str_has_prefix(message_key, data->prefix)) {
686 struct unconfirmed_message *msg = g_malloc(sizeof(struct unconfirmed_message));
687 msg->key = message_key;
688 msg->msg = value;
689 data->list = g_slist_insert_sorted(data->list, msg,
690 compare_cseq);
694 static void foreach_unconfirmed_message(struct sipe_core_private *sipe_private,
695 struct sip_session *session,
696 const gchar *callid,
697 const gchar *with,
698 unconfirmed_callback callback,
699 const gchar *callback_data)
701 gchar *prefix = g_strdup_printf(UNCONFIRMED_KEY_TEMPLATE("MESSAGE", ""),
702 callid, with);
703 struct unconfirmed_callback_data data = { prefix, NULL };
705 SIPE_DEBUG_INFO("foreach_unconfirmed_message: prefix %s", prefix);
707 /* Generate list of matching unconfirmed messages */
708 g_hash_table_foreach(session->unconfirmed_messages,
709 unconfirmed_message_callback,
710 &data);
711 g_free(prefix);
713 /* Process list unconfirmed messages */
714 if (data.list) {
715 GSList *entry;
717 while ((entry = data.list) != NULL) {
718 struct unconfirmed_message *unconfirmed = entry->data;
719 data.list = g_slist_remove(data.list, unconfirmed);
721 SIPE_DEBUG_INFO("foreach_unconfirmed_message: %s", unconfirmed->key);
722 (*callback)(sipe_private, session, unconfirmed->msg->body, callback_data);
724 g_hash_table_remove(session->unconfirmed_messages, unconfirmed->key);
725 g_free(unconfirmed);
730 static void cancel_callback(struct sipe_core_private *sipe_private,
731 struct sip_session *session,
732 const gchar *body,
733 const gchar *with)
735 sipe_user_present_message_undelivered(sipe_private, session,
736 -1, -1, with, body);
739 void sipe_im_cancel_unconfirmed(struct sipe_core_private *sipe_private,
740 struct sip_session *session,
741 const gchar *callid,
742 const gchar *with)
744 gchar *alias = get_buddy_alias(sipe_private, with);
746 SIPE_DEBUG_INFO("sipe_im_cancel_unconfirmed: with %s callid '%s'",
747 with, callid);
749 foreach_unconfirmed_message(sipe_private, session, callid, with,
750 cancel_callback, alias ? alias : with);
751 g_free(alias);
754 static void reenqueue_callback(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
755 struct sip_session *session,
756 const gchar *body,
757 SIPE_UNUSED_PARAMETER const gchar *with)
759 sipe_session_enqueue_message(session, body, NULL);
762 void sipe_im_reenqueue_unconfirmed(struct sipe_core_private *sipe_private,
763 struct sip_session *session,
764 const gchar *callid,
765 const gchar *with)
767 /* Remember original list, start with an empty list */
768 GSList *first = session->outgoing_message_queue;
769 session->outgoing_message_queue = NULL;
771 SIPE_DEBUG_INFO("sipe_im_reenqueue_unconfirmed: with %s callid '%s'",
772 with, callid);
774 /* Enqueue unconfirmed messages */
775 foreach_unconfirmed_message(sipe_private, session, callid, with,
776 reenqueue_callback, NULL);
778 /* Append or restore original list */
779 if (session->outgoing_message_queue) {
780 GSList *last = g_slist_last(session->outgoing_message_queue);
781 last->next = first;
782 } else {
783 session->outgoing_message_queue = first;
787 void sipe_core_im_send(struct sipe_core_public *sipe_public,
788 const gchar *who,
789 const gchar *what)
791 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
792 struct sip_session *session;
793 struct sip_dialog *dialog;
794 gchar *uri = sip_uri(who);
796 SIPE_DEBUG_INFO("sipe_core_im_send: '%s'", what);
798 session = sipe_session_find_or_add_im(sipe_private, uri);
799 dialog = sipe_dialog_find(session, uri);
801 /* Queue the message */
802 sipe_session_enqueue_message(session, what, NULL);
804 if (dialog && !dialog->outgoing_invite) {
805 sipe_im_process_queue(sipe_private, session);
806 } else if (!dialog || !dialog->outgoing_invite) {
807 /* Need to send the INVITE to get the outgoing dialog setup */
808 sipe_im_invite(sipe_private, session, uri, what, NULL, NULL, FALSE);
811 g_free(uri);
814 void sipe_im_cancel_dangling(struct sipe_core_private *sipe_private,
815 struct sip_session *session,
816 struct sip_dialog *dialog,
817 const gchar *with,
818 unconfirmed_callback callback)
820 SIPE_DEBUG_INFO_NOFORMAT("sipe_im_cancel_dangling: assuming dangling IM session, dropping it.");
821 sip_transport_bye(sipe_private, dialog);
823 (*callback)(sipe_private, session, dialog->callid, with);
825 /* We might not get a valid reply to our BYE,
826 so make sure the dialog is removed for sure. */
827 sipe_dialog_remove(session, with);
828 /* dialog is no longer valid */
831 void sipe_im_topic(struct sipe_core_private *sipe_private,
832 struct sip_session *session,
833 const gchar *topic)
835 g_free(session->subject);
836 session->subject = g_strdup(topic);
837 sipe_backend_im_topic(SIPE_CORE_PUBLIC, session->with, topic);
840 void process_incoming_info_conversation(struct sipe_core_private *sipe_private,
841 struct sipmsg *msg)
843 sipe_xml *xml = sipe_xml_parse(msg->body, msg->bodylen);
844 const gchar *from = NULL;
845 const gchar *subject = NULL;
848 if (!xml)
849 return;
851 if (sipe_strequal(sipe_xml_name(xml), "ConversationInfo")) {
852 const sipe_xml *node = sipe_xml_child(xml, "From");
853 if (node)
854 from = sipe_xml_attribute(node, "uri");
856 node = sipe_xml_child(xml, "Subject");
857 if (node)
858 subject = sipe_xml_data(node);
861 if (from && subject) {
862 struct sip_session *session;
863 session = sipe_session_find_im(sipe_private, from);
865 if (session)
866 sipe_im_topic(sipe_private, session, subject);
869 sipe_xml_free(xml);
871 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
875 Local Variables:
876 mode: c
877 c-file-style: "bsd"
878 indent-tabs-mode: t
879 tab-width: 8
880 End: