Fix #245: "Unable to resolve DNS SRV record" error when joining conference
[siplcs.git] / src / core / sipe-conf.c
blobdb1a9e4cfcdc8a8e984db13458c7abd30840bbd3
1 /**
2 * @file sipe-conf.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2014 SIPE Project <http://sipe.sourceforge.net/>
7 * Copyright (C) 2009 pier11 <pier11@operamail.com>
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.
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.
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
25 /**
26 * Documentation references:
28 * Microsoft DevNet: [MS-CONFIM]: Centralized Conference Control Protocol:
29 * Instant Messaging Extensions
30 * <http://msdn.microsoft.com/en-us/library/cc431500%28v=office.12%29.aspx>
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
42 #include <glib.h>
44 #include "sipe-common.h"
45 #include "sipmsg.h"
46 #include "sip-transport.h"
47 #include "sipe-backend.h"
48 #include "sipe-buddy.h"
49 #include "sipe-chat.h"
50 #include "sipe-conf.h"
51 #include "sipe-core.h"
52 #include "sipe-core-private.h"
53 #include "sipe-dialog.h"
54 #include "sipe-im.h"
55 #include "sipe-nls.h"
56 #include "sipe-session.h"
57 #include "sipe-subscriptions.h"
58 #include "sipe-user.h"
59 #include "sipe-utils.h"
60 #include "sipe-xml.h"
62 /**
63 * Invite counterparty to join conference.
64 * @param focus_uri (%s)
65 * @param subject (%s) of conference
67 #define SIPE_SEND_CONF_INVITE \
68 "<Conferencing version=\"2.0\">"\
69 "<focus-uri>%s</focus-uri>"\
70 "<subject>%s</subject>"\
71 "<im available=\"true\">"\
72 "<first-im/>"\
73 "</im>"\
74 "</Conferencing>"
76 static struct transaction *
77 cccp_request(struct sipe_core_private *sipe_private, const gchar *method,
78 const gchar *with, struct sip_dialog *dialog,
79 TransCallback callback, const gchar *body, ...)
81 gchar *headers;
82 gchar *request;
83 gchar *request_body;
85 gchar *self = sip_uri_self(sipe_private);
87 va_list args;
89 struct transaction *trans;
91 headers = g_strdup_printf(
92 "Supported: ms-sender\r\n"
93 "Contact: %s\r\n"
94 "Content-Type: application/cccp+xml\r\n",
95 sipe_private->contact);
97 /* TODO: put request_id to queue to further compare with incoming one */
98 request = g_strdup_printf(
99 "<?xml version=\"1.0\"?>"
100 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" "
101 "xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "
102 "C3PVersion=\"1\" "
103 "to=\"%s\" "
104 "from=\"%s\" "
105 "requestId=\"%d\">"
106 "%s"
107 "</request>",
108 with,
109 self,
110 sipe_private->cccp_request_id++,
111 body);
112 g_free(self);
114 va_start(args, body);
115 request_body = g_strdup_vprintf(request, args);
116 va_end(args);
118 g_free(request);
120 trans = sip_transport_request(sipe_private,
121 method,
122 with,
123 with,
124 headers,
125 request_body,
126 dialog,
127 callback);
129 g_free(headers);
130 g_free(request_body);
132 return trans;
135 static gboolean
136 process_conf_get_capabilities(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
137 struct sipmsg *msg,
138 SIPE_UNUSED_PARAMETER struct transaction *trans)
140 if (msg->response >= 400) {
141 SIPE_DEBUG_INFO_NOFORMAT("process_conf_get_capabilities: "
142 "getConferencingCapabilities failed.");
143 return FALSE;
145 if (msg->response == 200) {
146 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
148 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code"))) {
149 const sipe_xml *node = sipe_xml_child(xn_response, "getConferencingCapabilities/mcu-types/mcuType");
150 for (;node; node = sipe_xml_twin(node)) {
151 sipe_private->conf_mcu_types =
152 g_slist_append(sipe_private->conf_mcu_types,
153 sipe_xml_data(node));
157 sipe_xml_free(xn_response);
160 return TRUE;
163 void
164 sipe_conf_get_capabilities(struct sipe_core_private *sipe_private)
166 cccp_request(sipe_private, "SERVICE",
167 sipe_private->focus_factory_uri,
168 NULL,
169 process_conf_get_capabilities,
170 "<getConferencingCapabilities />");
173 gboolean
174 sipe_conf_supports_mcu_type(struct sipe_core_private *sipe_private,
175 const gchar *type)
177 return g_slist_find_custom(sipe_private->conf_mcu_types, type,
178 sipe_strcompare) != NULL;
182 * Generates random GUID.
183 * This method is borrowed from pidgin's msnutils.c
185 static char *
186 rand_guid()
188 return g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
189 rand() % 0xAAFF + 0x1111,
190 rand() % 0xAAFF + 0x1111,
191 rand() % 0xAAFF + 0x1111,
192 rand() % 0xAAFF + 0x1111,
193 rand() % 0xAAFF + 0x1111,
194 rand() % 0xAAFF + 0x1111,
195 rand() % 0xAAFF + 0x1111,
196 rand() % 0xAAFF + 0x1111);
199 /** Invite us to the focus callback */
200 static gboolean
201 process_invite_conf_focus_response(struct sipe_core_private *sipe_private,
202 struct sipmsg *msg,
203 SIPE_UNUSED_PARAMETER struct transaction *trans)
205 struct sip_session *session = NULL;
206 char *focus_uri = parse_from(sipmsg_find_header(msg, "To"));
208 session = sipe_session_find_conference(sipe_private, focus_uri);
210 if (!session) {
211 SIPE_DEBUG_INFO("process_invite_conf_focus_response: unable to find conf session with focus=%s", focus_uri);
212 g_free(focus_uri);
213 return FALSE;
216 if (!session->focus_dialog) {
217 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: session's focus_dialog is NULL");
218 g_free(focus_uri);
219 return FALSE;
222 sipe_dialog_parse(session->focus_dialog, msg, TRUE);
224 if (msg->response >= 200) {
225 /* send ACK to focus */
226 session->focus_dialog->cseq = 0;
227 sip_transport_ack(sipe_private, session->focus_dialog);
228 session->focus_dialog->outgoing_invite = NULL;
229 session->focus_dialog->is_established = TRUE;
232 if (msg->response >= 400) {
233 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
235 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: INVITE response is not 200. Failed to join focus.");
236 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
237 _("Failed to join the conference"),
238 reason ? reason : _("no reason given"));
239 g_free(reason);
241 sipe_session_remove(sipe_private, session);
242 g_free(focus_uri);
243 return FALSE;
244 } else if (msg->response == 200) {
245 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
246 const gchar *code = sipe_xml_attribute(xn_response, "code");
247 if (sipe_strequal(code, "success")) {
248 /* subscribe to focus */
249 sipe_subscribe_conference(sipe_private,
250 session->chat_session->id,
251 FALSE);
252 #ifdef HAVE_VV
253 if (session->is_call)
254 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
255 session->chat_session);
256 #endif
258 sipe_xml_free(xn_response);
261 g_free(focus_uri);
262 return TRUE;
265 static gchar *
266 parse_ocs_focus_uri(const gchar *uri)
268 const gchar *confkey;
269 size_t uri_len;
271 if (!uri)
272 return NULL;
274 // URI can have this prefix if it was typed in by the user
275 if (g_str_has_prefix(uri, "meet:") || g_str_has_prefix(uri, "conf:")) {
276 uri += 5;
279 uri_len = strlen(uri);
281 if (!uri || !g_str_has_prefix(uri, "sip:") ||
282 uri_len == 4 || g_strstr_len(uri, -1, "%")) {
283 return NULL;
286 confkey = g_strstr_len(uri, -1, "?");
287 if (confkey) {
288 /* TODO: Investigate how conf-key field should be used,
289 * ignoring for now */
290 uri_len = confkey - uri;
293 return g_strndup(uri, uri_len);
296 static gchar *
297 parse_lync_join_url(const gchar *uri)
299 gchar *focus_uri = NULL;
300 gchar **parts;
301 int parts_count = 0;
303 if (!uri)
304 return NULL;
306 if (g_str_has_prefix(uri, "https://")) {
307 uri += 8;
308 } else if (g_str_has_prefix(uri, "http://")) {
309 uri += 7;
312 parts = g_strsplit(uri, "/", 0);
314 for (parts_count = 0; parts[parts_count]; ++parts_count);
315 if (parts_count >= 3) {
316 const gchar *conference_id = parts[parts_count - 1];
317 const gchar *organizer_alias = parts[parts_count - 2];
319 gchar **domain_parts = g_strsplit(parts[0], ".", 2);
321 /* we need to drop the first sub-domain from the URL */
322 if (domain_parts[0] && domain_parts[1]) {
323 focus_uri = g_strdup_printf("sip:%s@%s;gruu;opaque=app:conf:focus:id:%s",
324 organizer_alias,
325 domain_parts[1],
326 conference_id);
329 g_strfreev(domain_parts);
332 g_strfreev(parts);
334 return focus_uri;
337 struct sip_session *
338 sipe_core_conf_create(struct sipe_core_public *sipe_public,
339 const gchar *uri)
341 gchar *uri_ue = sipe_utils_uri_unescape(uri);
342 gchar *focus_uri;
343 struct sip_session *session = NULL;
345 SIPE_DEBUG_INFO("sipe_core_conf_create: URI '%s' unescaped '%s'",
346 uri ? uri : "<UNDEFINED>",
347 uri_ue ? uri_ue : "<UNDEFINED>");
349 focus_uri = parse_ocs_focus_uri(uri_ue);
350 if (!focus_uri) {
351 focus_uri = parse_lync_join_url(uri_ue);
354 if (focus_uri) {
355 session = sipe_conf_create(SIPE_CORE_PRIVATE, NULL, focus_uri);
356 g_free(focus_uri);
357 } else {
358 gchar *error = g_strdup_printf(_("\"%s\" is not a valid conference URI"),
359 uri ? uri : "");
360 sipe_backend_notify_error(sipe_public,
361 _("Failed to join the conference"),
362 error);
363 g_free(error);
366 g_free(uri_ue);
368 return session;
371 /** Create new session with Focus URI */
372 struct sip_session *
373 sipe_conf_create(struct sipe_core_private *sipe_private,
374 struct sipe_chat_session *chat_session,
375 const gchar *focus_uri)
377 /* addUser request to the focus.
379 * focus_URI, from, endpoint_GUID
381 static const gchar CCCP_ADD_USER[] =
382 "<addUser>"
383 "<conferenceKeys confEntity=\"%s\"/>"
384 "<ci:user xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"%s\">"
385 "<ci:roles>"
386 "<ci:entry>attendee</ci:entry>"
387 "</ci:roles>"
388 "<ci:endpoint entity=\"{%s}\" "
389 "xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\"/>"
390 "</ci:user>"
391 "</addUser>";
393 gchar *self;
394 struct sip_session *session = sipe_session_add_chat(sipe_private,
395 chat_session,
396 FALSE,
397 focus_uri);
399 session->focus_dialog = g_new0(struct sip_dialog, 1);
400 session->focus_dialog->callid = gencallid();
401 session->focus_dialog->with = g_strdup(session->chat_session->id);
402 session->focus_dialog->endpoint_GUID = rand_guid();
403 session->focus_dialog->ourtag = gentag();
405 self = sip_uri_self(sipe_private);
406 session->focus_dialog->outgoing_invite =
407 cccp_request(sipe_private, "INVITE",
408 session->focus_dialog->with, session->focus_dialog,
409 process_invite_conf_focus_response,
410 CCCP_ADD_USER,
411 session->focus_dialog->with, self,
412 session->focus_dialog->endpoint_GUID);
414 /* Rejoin existing session? */
415 if (chat_session) {
416 SIPE_DEBUG_INFO("sipe_conf_create: rejoin '%s' (%s)",
417 chat_session->title,
418 chat_session->id);
419 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
420 chat_session->backend,
421 self,
422 chat_session->title);
424 g_free(self);
426 return(session);
429 /** Modify User Role */
430 void
431 sipe_conf_modify_user_role(struct sipe_core_private *sipe_private,
432 struct sip_session *session,
433 const gchar* who)
435 /* modifyUserRoles request to the focus. Makes user a leader.
437 * focus_uri (%s)
438 * who (%s)
440 static const gchar CCCP_MODIFY_USER_ROLES[] =
441 "<modifyUserRoles>"
442 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"
443 "<user-roles xmlns=\"urn:ietf:params:xml:ns:conference-info\">"
444 "<entry>presenter</entry>"
445 "</user-roles>"
446 "</modifyUserRoles>";
448 if (!session->focus_dialog || !session->focus_dialog->is_established) {
449 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_user_role: no dialog with focus, exiting.");
450 return;
453 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
454 session->focus_dialog, NULL,
455 CCCP_MODIFY_USER_ROLES,
456 session->focus_dialog->with, who);
460 * Check conference lock status
462 sipe_chat_lock_status sipe_core_chat_lock_status(struct sipe_core_public *sipe_public,
463 struct sipe_chat_session *chat_session)
465 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
466 sipe_chat_lock_status status = SIPE_CHAT_LOCK_STATUS_NOT_ALLOWED;
468 if (chat_session &&
469 (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE)) {
470 struct sip_session *session = sipe_session_find_chat(sipe_private,
471 chat_session);
472 if (session) {
473 gchar *self = sip_uri_self(sipe_private);
475 /* Only operators are allowed to change the lock status */
476 if (sipe_backend_chat_is_operator(chat_session->backend, self)) {
477 status = session->locked ?
478 SIPE_CHAT_LOCK_STATUS_LOCKED :
479 SIPE_CHAT_LOCK_STATUS_UNLOCKED;
482 g_free(self);
486 return(status);
490 * Modify Conference Lock
491 * Sends request to Focus.
492 * INFO method is a carrier of application/cccp+xml
494 void
495 sipe_core_chat_modify_lock(struct sipe_core_public *sipe_public,
496 struct sipe_chat_session *chat_session,
497 const gboolean locked)
499 /* modifyConferenceLock request to the focus. Locks/unlocks conference.
501 * focus_uri (%s)
502 * locked (%s) "true" or "false" values applicable
504 static const gchar CCCP_MODIFY_CONFERENCE_LOCK[] =
505 "<modifyConferenceLock>"
506 "<conferenceKeys confEntity=\"%s\"/>"
507 "<locked>%s</locked>"
508 "</modifyConferenceLock>";
510 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
512 struct sip_session *session = sipe_session_find_chat(sipe_private,
513 chat_session);
515 if (!session) return;
516 if (!session->focus_dialog || !session->focus_dialog->is_established) {
517 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_conference_lock: no dialog with focus, exiting.");
518 return;
521 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
522 session->focus_dialog, NULL,
523 CCCP_MODIFY_CONFERENCE_LOCK,
524 session->focus_dialog->with,
525 locked ? "true" : "false");
528 /** Modify Delete User */
529 void
530 sipe_conf_delete_user(struct sipe_core_private *sipe_private,
531 struct sip_session *session,
532 const gchar* who)
534 /* deleteUser request to the focus. Removes a user from the conference.
536 * focus_uri (%s)
537 * who (%s)
539 static const gchar CCCP_DELETE_USER[] =
540 "<deleteUser>"
541 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"
542 "</deleteUser>";
544 if (!session->focus_dialog || !session->focus_dialog->is_established) {
545 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_delete_user: no dialog with focus, exiting.");
546 return;
549 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
550 session->focus_dialog, NULL,
551 CCCP_DELETE_USER,
552 session->focus_dialog->with, who);
555 /** Invite counterparty to join conference callback */
556 static gboolean
557 process_invite_conf_response(struct sipe_core_private *sipe_private,
558 struct sipmsg *msg,
559 SIPE_UNUSED_PARAMETER struct transaction *trans)
561 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
563 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
564 dialog->cseq = sipmsg_parse_cseq(msg);
565 dialog->with = parse_from(sipmsg_find_header(msg, "To"));
566 sipe_dialog_parse(dialog, msg, TRUE);
568 if (msg->response >= 200) {
569 /* send ACK to counterparty */
570 dialog->cseq--;
571 sip_transport_ack(sipe_private, dialog);
572 dialog->outgoing_invite = NULL;
573 dialog->is_established = TRUE;
576 if (msg->response >= 400) {
577 SIPE_DEBUG_INFO("process_invite_conf_response: INVITE response is not 200. Failed to invite %s.", dialog->with);
578 /* @TODO notify user of failure to invite counterparty */
579 sipe_dialog_free(dialog);
580 return FALSE;
583 if (msg->response >= 200) {
584 struct sip_session *session = sipe_session_find_im(sipe_private, dialog->with);
585 struct sip_dialog *im_dialog = sipe_dialog_find(session, dialog->with);
587 /* close IM session to counterparty */
588 if (im_dialog) {
589 sip_transport_bye(sipe_private, im_dialog);
590 sipe_dialog_remove(session, dialog->with);
594 sipe_dialog_free(dialog);
595 return TRUE;
599 * Invites counterparty to join conference.
601 void
602 sipe_invite_conf(struct sipe_core_private *sipe_private,
603 struct sip_session *session,
604 const gchar* who)
606 gchar *hdr;
607 gchar *contact;
608 gchar *body;
609 struct sip_dialog *dialog = NULL;
611 /* It will be short lived special dialog.
612 * Will not be stored in session.
614 dialog = g_new0(struct sip_dialog, 1);
615 dialog->callid = gencallid();
616 dialog->with = g_strdup(who);
617 dialog->ourtag = gentag();
619 contact = get_contact(sipe_private);
620 hdr = g_strdup_printf(
621 "Supported: ms-sender\r\n"
622 "Contact: %s\r\n"
623 "Content-Type: application/ms-conf-invite+xml\r\n",
624 contact);
625 g_free(contact);
627 body = g_strdup_printf(
628 SIPE_SEND_CONF_INVITE,
629 session->chat_session->id,
630 session->subject ? session->subject : ""
633 sip_transport_invite(sipe_private,
634 hdr,
635 body,
636 dialog,
637 process_invite_conf_response);
639 sipe_dialog_free(dialog);
640 g_free(body);
641 g_free(hdr);
644 /** Create conference callback */
645 static gboolean
646 process_conf_add_response(struct sipe_core_private *sipe_private,
647 struct sipmsg *msg,
648 struct transaction *trans)
650 if (msg->response >= 400) {
651 SIPE_DEBUG_INFO_NOFORMAT("process_conf_add_response: SERVICE response is not 200. Failed to create conference.");
652 /* @TODO notify user of failure to create conference */
653 return FALSE;
655 if (msg->response == 200) {
656 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
657 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code")))
659 gchar *who = trans->payload->data;
660 const sipe_xml *xn_conference_info = sipe_xml_child(xn_response, "addConference/conference-info");
661 struct sip_session *session = sipe_conf_create(sipe_private,
662 NULL,
663 sipe_xml_attribute(xn_conference_info,
664 "entity"));
666 SIPE_DEBUG_INFO("process_conf_add_response: session->focus_uri=%s",
667 session->chat_session->id);
669 session->pending_invite_queue = sipe_utils_slist_insert_unique_sorted(session->pending_invite_queue,
670 g_strdup(who),
671 (GCompareFunc)strcmp,
672 g_free);
674 sipe_xml_free(xn_response);
677 return TRUE;
681 * Creates conference.
683 void
684 sipe_conf_add(struct sipe_core_private *sipe_private,
685 const gchar* who)
687 gchar *conference_id;
688 struct transaction *trans;
689 time_t expiry = time(NULL) + 7*60*60; /* 7 hours */
690 char *expiry_time;
691 struct transaction_payload *payload;
693 /* addConference request to the focus factory.
695 * conference_id (%s) Ex.: 8386E6AEAAA41E4AA6627BA76D43B6D1
696 * expiry_time (%s) Ex.: 2009-07-13T17:57:09Z
697 * conference_view (%s) Ex.: <msci:entity-view entity="chat"/>
699 static const gchar CCCP_ADD_CONFERENCE[] =
700 "<addConference>"
701 "<ci:conference-info xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" "
702 "entity=\"\" "
703 "xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\">"
704 "<ci:conference-description>"
705 "<ci:subject/>"
706 "<msci:conference-id>%s</msci:conference-id>"
707 "<msci:expiry-time>%s</msci:expiry-time>"
708 "<msci:admission-policy>openAuthenticated</msci:admission-policy>"
709 "</ci:conference-description>"
710 "<msci:conference-view>%s</msci:conference-view>"
711 "</ci:conference-info>"
712 "</addConference>";
714 static const gchar *DESIRED_MCU_TYPES[] = {
715 "chat",
716 #ifdef HAVE_VV
717 "audio-video",
718 #endif
719 NULL
722 GString *conference_view = g_string_new("");
723 const gchar **type;
725 for (type = DESIRED_MCU_TYPES; *type; ++type ) {
726 if (sipe_conf_supports_mcu_type(sipe_private, *type)) {
727 g_string_append(conference_view, "<msci:entity-view entity=\"");
728 g_string_append(conference_view, *type);
729 g_string_append(conference_view, "\"/>");
733 expiry_time = sipe_utils_time_to_str(expiry);
734 conference_id = genconfid();
735 trans = cccp_request(sipe_private, "SERVICE", sipe_private->focus_factory_uri,
736 NULL, process_conf_add_response,
737 CCCP_ADD_CONFERENCE,
738 conference_id, expiry_time, conference_view->str);
739 g_free(conference_id);
740 g_free(expiry_time);
741 g_string_free(conference_view, TRUE);
743 payload = g_new0(struct transaction_payload, 1);
744 payload->destroy = g_free;
745 payload->data = g_strdup(who);
746 trans->payload = payload;
749 static void
750 accept_incoming_invite_conf(struct sipe_core_private *sipe_private,
751 gchar *focus_uri,
752 gboolean audio,
753 struct sipmsg *msg)
755 struct sip_session *session;
756 gchar *newTag = gentag();
757 const gchar *oldHeader = sipmsg_find_header(msg, "To");
758 gchar *newHeader;
760 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
761 g_free(newTag);
762 sipmsg_remove_header_now(msg, "To");
763 sipmsg_add_header_now(msg, "To", newHeader);
764 g_free(newHeader);
766 /* acknowledge invite */
767 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
769 /* add self to conf */
770 session = sipe_conf_create(sipe_private, NULL, focus_uri);
771 session->is_call = audio;
774 struct conf_accept_ctx {
775 gchar *focus_uri;
776 struct sipmsg *msg;
777 struct sipe_user_ask_ctx *ask_ctx;
780 static void
781 conf_accept_ctx_free(struct conf_accept_ctx *ctx)
783 g_return_if_fail(ctx != NULL);
785 sipmsg_free(ctx->msg);
786 g_free(ctx->focus_uri);
787 g_free(ctx);
790 static void
791 conf_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
793 sipe_private->sessions_to_accept =
794 g_slist_remove(sipe_private->sessions_to_accept, ctx);
796 accept_incoming_invite_conf(sipe_private, ctx->focus_uri, TRUE, ctx->msg);
797 conf_accept_ctx_free(ctx);
800 static void
801 conf_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
803 sipe_private->sessions_to_accept =
804 g_slist_remove(sipe_private->sessions_to_accept, ctx);
806 sip_transport_response(sipe_private,
807 ctx->msg,
808 603, "Decline", NULL);
810 conf_accept_ctx_free(ctx);
813 void
814 sipe_conf_cancel_unaccepted(struct sipe_core_private *sipe_private,
815 struct sipmsg *msg)
817 const gchar *callid1 = msg ? sipmsg_find_header(msg, "Call-ID") : NULL;
818 GSList *it = sipe_private->sessions_to_accept;
819 while (it) {
820 struct conf_accept_ctx *ctx = it->data;
821 const gchar *callid2 = NULL;
823 if (msg && ctx->msg)
824 callid2 = sipmsg_find_header(ctx->msg, "Call-ID");
826 if (sipe_strequal(callid1, callid2)) {
827 GSList *tmp;
829 if (ctx->msg)
830 sip_transport_response(sipe_private, ctx->msg,
831 487, "Request Terminated", NULL);
833 if (msg)
834 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
836 sipe_user_close_ask(ctx->ask_ctx);
837 conf_accept_ctx_free(ctx);
839 tmp = it;
840 it = it->next;
842 sipe_private->sessions_to_accept =
843 g_slist_delete_link(sipe_private->sessions_to_accept, tmp);
845 if (callid1)
846 break;
847 } else
848 it = it->next;
852 static void
853 ask_accept_voice_conference(struct sipe_core_private *sipe_private,
854 const gchar *focus_uri,
855 struct sipmsg *msg,
856 SipeUserAskCb accept_cb,
857 SipeUserAskCb decline_cb)
859 gchar **parts;
860 gchar *alias;
861 gchar *ask_msg;
862 const gchar *novv_note;
863 struct conf_accept_ctx *ctx;
865 #ifdef HAVE_VV
866 novv_note = "";
867 #else
868 novv_note = _("\n\nAs this client was not compiled with voice call "
869 "support, if you accept, you will be able to contact "
870 "the other participants only via IM session.");
871 #endif
873 parts = g_strsplit(focus_uri, ";", 2);
874 alias = sipe_buddy_get_alias(sipe_private, parts[0]);
876 ask_msg = g_strdup_printf(_("%s wants to invite you "
877 "to the conference call%s"),
878 alias ? alias : parts[0], novv_note);
880 g_free(alias);
881 g_strfreev(parts);
883 ctx = g_new0(struct conf_accept_ctx, 1);
884 sipe_private->sessions_to_accept =
885 g_slist_append(sipe_private->sessions_to_accept, ctx);
887 ctx->focus_uri = g_strdup(focus_uri);
888 ctx->msg = msg ? sipmsg_copy(msg) : NULL;
889 ctx->ask_ctx = sipe_user_ask(sipe_private, ask_msg,
890 _("Accept"), accept_cb,
891 _("Decline"), decline_cb,
892 ctx);
894 g_free(ask_msg);
897 void
898 process_incoming_invite_conf(struct sipe_core_private *sipe_private,
899 struct sipmsg *msg)
901 sipe_xml *xn_conferencing = sipe_xml_parse(msg->body, msg->bodylen);
902 const sipe_xml *xn_focus_uri = sipe_xml_child(xn_conferencing, "focus-uri");
903 const sipe_xml *xn_audio = sipe_xml_child(xn_conferencing, "audio");
904 gchar *focus_uri = sipe_xml_data(xn_focus_uri);
905 gboolean audio = sipe_strequal(sipe_xml_attribute(xn_audio, "available"), "true");
907 sipe_xml_free(xn_conferencing);
909 SIPE_DEBUG_INFO("We have received invitation to Conference. Focus URI=%s", focus_uri);
911 if (audio) {
912 sip_transport_response(sipe_private, msg, 180, "Ringing", NULL);
913 ask_accept_voice_conference(sipe_private, focus_uri, msg,
914 (SipeUserAskCb) conf_accept_cb,
915 (SipeUserAskCb) conf_decline_cb);
917 } else {
918 accept_incoming_invite_conf(sipe_private, focus_uri, FALSE, msg);
921 g_free(focus_uri);
924 #ifdef HAVE_VV
926 static void
927 call_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
929 struct sip_session *session;
930 session = sipe_session_find_conference(sipe_private, ctx->focus_uri);
932 sipe_private->sessions_to_accept =
933 g_slist_remove(sipe_private->sessions_to_accept, ctx);
935 if (session) {
936 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
937 session->chat_session);
940 conf_accept_ctx_free(ctx);
943 static void
944 call_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
946 sipe_private->sessions_to_accept =
947 g_slist_remove(sipe_private->sessions_to_accept, ctx);
949 conf_accept_ctx_free(ctx);
952 #endif // HAVE_VV
954 void
955 sipe_process_conference(struct sipe_core_private *sipe_private,
956 struct sipmsg *msg)
958 sipe_xml *xn_conference_info;
959 const sipe_xml *node;
960 const sipe_xml *xn_subject;
961 const gchar *focus_uri;
962 struct sip_session *session;
963 gboolean just_joined = FALSE;
964 #ifdef HAVE_VV
965 gboolean audio_was_added = FALSE;
966 #endif
968 if (msg->response != 0 && msg->response != 200) return;
970 if (msg->bodylen == 0 || msg->body == NULL || !sipe_strequal(sipmsg_find_header(msg, "Event"), "conference")) return;
972 xn_conference_info = sipe_xml_parse(msg->body, msg->bodylen);
973 if (!xn_conference_info) return;
975 focus_uri = sipe_xml_attribute(xn_conference_info, "entity");
976 session = sipe_session_find_conference(sipe_private, focus_uri);
978 if (!session) {
979 SIPE_DEBUG_INFO("sipe_process_conference: unable to find conf session with focus=%s", focus_uri);
980 return;
983 if (!session->chat_session->backend) {
984 gchar *self = sip_uri_self(sipe_private);
986 /* create chat */
987 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
988 session->chat_session,
989 session->chat_session->title,
990 self);
991 just_joined = TRUE;
992 /* @TODO ask for full state (re-subscribe) if it was a partial one -
993 * this is to obtain full list of conference participants.
995 g_free(self);
998 /* subject */
999 if ((xn_subject = sipe_xml_child(xn_conference_info, "conference-description/subject"))) {
1000 g_free(session->subject);
1001 session->subject = sipe_xml_data(xn_subject);
1002 sipe_backend_chat_topic(session->chat_session->backend, session->subject);
1003 SIPE_DEBUG_INFO("sipe_process_conference: subject=%s", session->subject ? session->subject : "");
1006 /* IM MCU URI */
1007 if (!session->im_mcu_uri) {
1008 for (node = sipe_xml_child(xn_conference_info, "conference-description/conf-uris/entry");
1009 node;
1010 node = sipe_xml_twin(node))
1012 gchar *purpose = sipe_xml_data(sipe_xml_child(node, "purpose"));
1014 if (sipe_strequal("chat", purpose)) {
1015 g_free(purpose);
1016 session->im_mcu_uri = sipe_xml_data(sipe_xml_child(node, "uri"));
1017 SIPE_DEBUG_INFO("sipe_process_conference: im_mcu_uri=%s", session->im_mcu_uri);
1018 break;
1020 g_free(purpose);
1024 /* users */
1025 for (node = sipe_xml_child(xn_conference_info, "users/user"); node; node = sipe_xml_twin(node)) {
1026 const gchar *user_uri = sipe_xml_attribute(node, "entity");
1027 const gchar *state = sipe_xml_attribute(node, "state");
1028 gchar *role = sipe_xml_data(sipe_xml_child(node, "roles/entry"));
1029 gboolean is_operator = sipe_strequal(role, "presenter");
1030 gboolean is_in_im_mcu = FALSE;
1031 gchar *self = sip_uri_self(sipe_private);
1033 if (sipe_strequal("deleted", state)) {
1034 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1035 sipe_backend_chat_remove(session->chat_session->backend,
1036 user_uri);
1038 } else {
1039 /* endpoints */
1040 const sipe_xml *endpoint;
1041 for (endpoint = sipe_xml_child(node, "endpoint"); endpoint; endpoint = sipe_xml_twin(endpoint)) {
1042 const gchar *session_type;
1043 gchar *status = sipe_xml_data(sipe_xml_child(endpoint, "status"));
1044 gboolean connected = sipe_strequal("connected", status);
1045 g_free(status);
1047 if (!connected)
1048 continue;
1050 session_type = sipe_xml_attribute(endpoint, "session-type");
1052 if (sipe_strequal("chat", session_type)) {
1053 is_in_im_mcu = TRUE;
1054 if (!sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1055 sipe_backend_chat_add(session->chat_session->backend,
1056 user_uri,
1057 !just_joined && g_ascii_strcasecmp(user_uri, self));
1059 if (is_operator) {
1060 sipe_backend_chat_operator(session->chat_session->backend,
1061 user_uri);
1063 } else if (sipe_strequal("audio-video", session_type)) {
1064 #ifdef HAVE_VV
1065 if (!session->is_call)
1066 audio_was_added = TRUE;
1067 #endif
1070 if (!is_in_im_mcu) {
1071 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1072 sipe_backend_chat_remove(session->chat_session->backend,
1073 user_uri);
1077 g_free(role);
1078 g_free(self);
1081 #ifdef HAVE_VV
1082 if (audio_was_added) {
1083 session->is_call = TRUE;
1084 ask_accept_voice_conference(sipe_private, focus_uri, NULL,
1085 (SipeUserAskCb) call_accept_cb,
1086 (SipeUserAskCb) call_decline_cb);
1088 #endif
1090 /* entity-view, locked */
1091 for (node = sipe_xml_child(xn_conference_info, "conference-view/entity-view");
1092 node;
1093 node = sipe_xml_twin(node)) {
1095 const sipe_xml *xn_type = sipe_xml_child(node, "entity-state/media/entry/type");
1096 gchar *tmp = NULL;
1097 if (xn_type && sipe_strequal("chat", (tmp = sipe_xml_data(xn_type)))) {
1098 const sipe_xml *xn_locked = sipe_xml_child(node, "entity-state/locked");
1099 if (xn_locked) {
1100 gchar *locked = sipe_xml_data(xn_locked);
1101 gboolean prev_locked = session->locked;
1102 session->locked = sipe_strequal(locked, "true");
1103 if (prev_locked && !session->locked) {
1104 sipe_user_present_info(sipe_private, session,
1105 _("This conference is no longer locked. Additional participants can now join."));
1107 if (!prev_locked && session->locked) {
1108 sipe_user_present_info(sipe_private, session,
1109 _("This conference is locked. Nobody else can join the conference while it is locked."));
1112 SIPE_DEBUG_INFO("sipe_process_conference: session->locked=%s",
1113 session->locked ? "TRUE" : "FALSE");
1114 g_free(locked);
1117 g_free(tmp);
1119 sipe_xml_free(xn_conference_info);
1121 if (session->im_mcu_uri) {
1122 struct sip_dialog *dialog = sipe_dialog_find(session, session->im_mcu_uri);
1123 if (!dialog) {
1124 dialog = sipe_dialog_add(session);
1126 dialog->callid = g_strdup(session->callid);
1127 dialog->with = g_strdup(session->im_mcu_uri);
1129 /* send INVITE to IM MCU */
1130 sipe_im_invite(sipe_private, session, dialog->with, NULL, NULL, NULL, FALSE);
1134 sipe_process_pending_invite_queue(sipe_private, session);
1137 void
1138 sipe_conf_immcu_closed(struct sipe_core_private *sipe_private,
1139 struct sip_session *session)
1141 sipe_user_present_info(sipe_private, session,
1142 _("You have been disconnected from this conference."));
1143 sipe_backend_chat_close(session->chat_session->backend);
1146 void
1147 conf_session_close(struct sipe_core_private *sipe_private,
1148 struct sip_session *session)
1150 if (session) {
1151 /* unsubscribe from focus */
1152 sipe_subscribe_conference(sipe_private,
1153 session->chat_session->id, TRUE);
1155 if (session->focus_dialog) {
1156 /* send BYE to focus */
1157 sip_transport_bye(sipe_private, session->focus_dialog);
1162 void
1163 sipe_process_imdn(struct sipe_core_private *sipe_private,
1164 struct sipmsg *msg)
1166 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1167 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1168 static struct sip_session *session;
1169 sipe_xml *xn_imdn;
1170 const sipe_xml *node;
1171 gchar *message_id;
1172 gchar *message;
1174 session = sipe_session_find_chat_or_im(sipe_private, callid, with);
1175 if (!session) {
1176 SIPE_DEBUG_INFO("sipe_process_imdn: unable to find conf session with callid=%s", callid);
1177 g_free(with);
1178 return;
1181 xn_imdn = sipe_xml_parse(msg->body, msg->bodylen);
1182 message_id = sipe_xml_data(sipe_xml_child(xn_imdn, "message-id"));
1184 message = g_hash_table_lookup(session->conf_unconfirmed_messages, message_id);
1186 /* recipient */
1187 for (node = sipe_xml_child(xn_imdn, "recipient"); node; node = sipe_xml_twin(node)) {
1188 gchar *tmp = parse_from(sipe_xml_attribute(node, "uri"));
1189 gchar *uri = parse_from(tmp);
1190 gchar *status = sipe_xml_data(sipe_xml_child(node, "status"));
1191 guint error = status ? g_ascii_strtoull(status, NULL, 10) : 0;
1192 /* default to error if missing or conversion failed */
1193 if ((error == 0) || (error >= 300))
1194 sipe_user_present_message_undelivered(sipe_private,
1195 session,
1196 error,
1198 uri,
1199 message);
1200 g_free(status);
1201 g_free(tmp);
1202 g_free(uri);
1205 sipe_xml_free(xn_imdn);
1207 g_hash_table_remove(session->conf_unconfirmed_messages, message_id);
1208 SIPE_DEBUG_INFO("sipe_process_imdn: removed message %s from conf_unconfirmed_messages(count=%d)",
1209 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
1210 g_free(message_id);
1211 g_free(with);
1214 void sipe_core_conf_make_leader(struct sipe_core_public *sipe_public,
1215 gpointer parameter,
1216 const gchar *buddy_name)
1218 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1219 struct sipe_chat_session *chat_session = parameter;
1220 struct sip_session *session;
1222 SIPE_DEBUG_INFO("sipe_core_conf_make_leader: chat_title=%s",
1223 chat_session->title);
1225 session = sipe_session_find_chat(sipe_private, chat_session);
1226 sipe_conf_modify_user_role(sipe_private, session, buddy_name);
1229 void sipe_core_conf_remove_from(struct sipe_core_public *sipe_public,
1230 gpointer parameter,
1231 const gchar *buddy_name)
1233 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1234 struct sipe_chat_session *chat_session = parameter;
1235 struct sip_session *session;
1237 SIPE_DEBUG_INFO("sipe_core_conf_remove_from: chat_title=%s",
1238 chat_session->title);
1240 session = sipe_session_find_chat(sipe_private, chat_session);
1241 sipe_conf_delete_user(sipe_private, session, buddy_name);
1245 Local Variables:
1246 mode: c
1247 c-file-style: "bsd"
1248 indent-tabs-mode: t
1249 tab-width: 8
1250 End: