conference: send HTTP request for Lync URLs
[siplcs.git] / src / core / sipe-conf.c
blob3b02619cd856688d48d6f594ce727367fb0b0e2a
1 /**
2 * @file sipe-conf.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2015 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-http.h"
55 #include "sipe-im.h"
56 #include "sipe-nls.h"
57 #include "sipe-session.h"
58 #include "sipe-subscriptions.h"
59 #include "sipe-user.h"
60 #include "sipe-utils.h"
61 #include "sipe-xml.h"
63 /**
64 * Invite counterparty to join conference.
65 * @param focus_uri (%s)
66 * @param subject (%s) of conference
68 #define SIPE_SEND_CONF_INVITE \
69 "<Conferencing version=\"2.0\">"\
70 "<focus-uri>%s</focus-uri>"\
71 "<subject>%s</subject>"\
72 "<im available=\"true\">"\
73 "<first-im/>"\
74 "</im>"\
75 "</Conferencing>"
77 static struct transaction *
78 cccp_request(struct sipe_core_private *sipe_private, const gchar *method,
79 const gchar *with, struct sip_dialog *dialog,
80 TransCallback callback, const gchar *body, ...)
82 gchar *headers;
83 gchar *request;
84 gchar *request_body;
86 gchar *self = sip_uri_self(sipe_private);
88 va_list args;
90 struct transaction *trans;
92 headers = g_strdup_printf(
93 "Supported: ms-sender\r\n"
94 "Contact: %s\r\n"
95 "Content-Type: application/cccp+xml\r\n",
96 sipe_private->contact);
98 /* TODO: put request_id to queue to further compare with incoming one */
99 request = g_strdup_printf(
100 "<?xml version=\"1.0\"?>"
101 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" "
102 "xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "
103 "C3PVersion=\"1\" "
104 "to=\"%s\" "
105 "from=\"%s\" "
106 "requestId=\"%d\">"
107 "%s"
108 "</request>",
109 with,
110 self,
111 sipe_private->cccp_request_id++,
112 body);
113 g_free(self);
115 va_start(args, body);
116 request_body = g_strdup_vprintf(request, args);
117 va_end(args);
119 g_free(request);
121 trans = sip_transport_request(sipe_private,
122 method,
123 with,
124 with,
125 headers,
126 request_body,
127 dialog,
128 callback);
130 g_free(headers);
131 g_free(request_body);
133 return trans;
136 static gboolean
137 process_conf_get_capabilities(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
138 struct sipmsg *msg,
139 SIPE_UNUSED_PARAMETER struct transaction *trans)
141 if (msg->response >= 400) {
142 SIPE_DEBUG_INFO_NOFORMAT("process_conf_get_capabilities: "
143 "getConferencingCapabilities failed.");
144 return FALSE;
146 if (msg->response == 200) {
147 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
149 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code"))) {
150 const sipe_xml *node = sipe_xml_child(xn_response, "getConferencingCapabilities/mcu-types/mcuType");
151 for (;node; node = sipe_xml_twin(node)) {
152 sipe_private->conf_mcu_types =
153 g_slist_append(sipe_private->conf_mcu_types,
154 sipe_xml_data(node));
158 sipe_xml_free(xn_response);
161 return TRUE;
164 void
165 sipe_conf_get_capabilities(struct sipe_core_private *sipe_private)
167 cccp_request(sipe_private, "SERVICE",
168 sipe_private->focus_factory_uri,
169 NULL,
170 process_conf_get_capabilities,
171 "<getConferencingCapabilities />");
174 gboolean
175 sipe_conf_supports_mcu_type(struct sipe_core_private *sipe_private,
176 const gchar *type)
178 return g_slist_find_custom(sipe_private->conf_mcu_types, type,
179 sipe_strcompare) != NULL;
183 * Generates random GUID.
184 * This method is borrowed from pidgin's msnutils.c
186 static char *
187 rand_guid()
189 return g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
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,
197 rand() % 0xAAFF + 0x1111);
200 /** Invite us to the focus callback */
201 static gboolean
202 process_invite_conf_focus_response(struct sipe_core_private *sipe_private,
203 struct sipmsg *msg,
204 SIPE_UNUSED_PARAMETER struct transaction *trans)
206 struct sip_session *session = NULL;
207 char *focus_uri = parse_from(sipmsg_find_header(msg, "To"));
209 session = sipe_session_find_conference(sipe_private, focus_uri);
211 if (!session) {
212 SIPE_DEBUG_INFO("process_invite_conf_focus_response: unable to find conf session with focus=%s", focus_uri);
213 g_free(focus_uri);
214 return FALSE;
217 if (!session->focus_dialog) {
218 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: session's focus_dialog is NULL");
219 g_free(focus_uri);
220 return FALSE;
223 sipe_dialog_parse(session->focus_dialog, msg, TRUE);
225 if (msg->response >= 200) {
226 /* send ACK to focus */
227 session->focus_dialog->cseq = 0;
228 sip_transport_ack(sipe_private, session->focus_dialog);
229 session->focus_dialog->outgoing_invite = NULL;
230 session->focus_dialog->is_established = TRUE;
233 if (msg->response >= 400) {
234 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
236 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: INVITE response is not 200. Failed to join focus.");
237 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
238 _("Failed to join the conference"),
239 reason ? reason : _("no reason given"));
240 g_free(reason);
242 sipe_session_remove(sipe_private, session);
243 g_free(focus_uri);
244 return FALSE;
245 } else if (msg->response == 200) {
246 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
247 const gchar *code = sipe_xml_attribute(xn_response, "code");
248 if (sipe_strequal(code, "success")) {
249 /* subscribe to focus */
250 sipe_subscribe_conference(sipe_private,
251 session->chat_session->id,
252 FALSE);
253 #ifdef HAVE_VV
254 if (session->is_call)
255 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
256 session->chat_session);
257 #endif
259 sipe_xml_free(xn_response);
262 g_free(focus_uri);
263 return TRUE;
266 static gchar *
267 parse_ocs_focus_uri(const gchar *uri)
269 const gchar *confkey;
270 size_t uri_len;
272 if (!uri)
273 return NULL;
275 // URI can have this prefix if it was typed in by the user
276 if (g_str_has_prefix(uri, "meet:") || g_str_has_prefix(uri, "conf:")) {
277 uri += 5;
280 uri_len = strlen(uri);
282 if (!uri || !g_str_has_prefix(uri, "sip:") ||
283 uri_len == 4 || g_strstr_len(uri, -1, "%")) {
284 return NULL;
287 confkey = g_strstr_len(uri, -1, "?");
288 if (confkey) {
289 /* TODO: Investigate how conf-key field should be used,
290 * ignoring for now */
291 uri_len = confkey - uri;
294 return g_strndup(uri, uri_len);
297 static gchar *
298 parse_lync_join_url(const gchar *uri)
300 gchar *focus_uri = NULL;
301 gchar **parts;
302 int parts_count = 0;
304 if (!uri)
305 return NULL;
307 if (g_str_has_prefix(uri, "https://")) {
308 uri += 8;
309 } else if (g_str_has_prefix(uri, "http://")) {
310 uri += 7;
313 parts = g_strsplit(uri, "/", 0);
315 for (parts_count = 0; parts[parts_count]; ++parts_count);
316 if (parts_count >= 3) {
317 const gchar *conference_id = parts[parts_count - 1];
318 const gchar *organizer_alias = parts[parts_count - 2];
320 gchar **domain_parts = g_strsplit(parts[0], ".", 2);
322 /* we need to drop the first sub-domain from the URL */
323 if (domain_parts[0] && domain_parts[1]) {
324 focus_uri = g_strdup_printf("sip:%s@%s;gruu;opaque=app:conf:focus:id:%s",
325 organizer_alias,
326 domain_parts[1],
327 conference_id);
330 g_strfreev(domain_parts);
333 g_strfreev(parts);
335 return focus_uri;
338 static void sipe_conf_error(struct sipe_core_private *sipe_private,
339 const gchar *uri)
341 gchar *error = g_strdup_printf(_("\"%s\" is not a valid conference URI"),
342 uri ? uri : "");
343 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
344 _("Failed to join the conference"),
345 error);
346 g_free(error);
349 static void sipe_conf_lync_url_cb(struct sipe_core_private *sipe_private,
350 SIPE_UNUSED_PARAMETER guint status,
351 SIPE_UNUSED_PARAMETER GSList *headers,
352 SIPE_UNUSED_PARAMETER const gchar *body,
353 gpointer callback_data)
355 gchar *uri = callback_data;
356 gchar *focus_uri = parse_lync_join_url(uri);
358 if (focus_uri) {
359 sipe_conf_create(sipe_private, NULL, focus_uri);
360 g_free(focus_uri);
361 } else {
362 sipe_conf_error(sipe_private, uri);
365 g_free(uri);
368 static gboolean sipe_conf_check_for_lync_url(struct sipe_core_private *sipe_private,
369 gchar *uri)
371 if (!(g_str_has_prefix(uri, "https://") ||
372 g_str_has_prefix(uri, "http://")))
373 return(FALSE);
375 /* URL points to a HTML page with the conference focus URI */
376 return(sipe_http_request_get(sipe_private,
377 uri,
378 NULL,
379 sipe_conf_lync_url_cb,
380 uri)
381 != NULL);
384 void sipe_core_conf_create(struct sipe_core_public *sipe_public,
385 const gchar *uri)
387 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
388 gchar *uri_ue = sipe_utils_uri_unescape(uri);
390 SIPE_DEBUG_INFO("sipe_core_conf_create: URI '%s' unescaped '%s'",
391 uri ? uri : "<UNDEFINED>",
392 uri_ue ? uri_ue : "<UNDEFINED>");
394 /* takes ownership of "uri_ue" if successful */
395 if (!sipe_conf_check_for_lync_url(sipe_private, uri_ue)) {
396 gchar *focus_uri = parse_ocs_focus_uri(uri_ue);
398 if (focus_uri) {
399 sipe_conf_create(sipe_private, NULL, focus_uri);
400 g_free(focus_uri);
401 } else {
402 sipe_conf_error(sipe_private, uri);
405 g_free(uri_ue);
409 /** Create new session with Focus URI */
410 struct sip_session *
411 sipe_conf_create(struct sipe_core_private *sipe_private,
412 struct sipe_chat_session *chat_session,
413 const gchar *focus_uri)
415 /* addUser request to the focus.
417 * focus_URI, from, endpoint_GUID
419 static const gchar CCCP_ADD_USER[] =
420 "<addUser>"
421 "<conferenceKeys confEntity=\"%s\"/>"
422 "<ci:user xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"%s\">"
423 "<ci:roles>"
424 "<ci:entry>attendee</ci:entry>"
425 "</ci:roles>"
426 "<ci:endpoint entity=\"{%s}\" "
427 "xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\"/>"
428 "</ci:user>"
429 "</addUser>";
431 gchar *self;
432 struct sip_session *session = sipe_session_add_chat(sipe_private,
433 chat_session,
434 FALSE,
435 focus_uri);
437 session->focus_dialog = g_new0(struct sip_dialog, 1);
438 session->focus_dialog->callid = gencallid();
439 session->focus_dialog->with = g_strdup(session->chat_session->id);
440 session->focus_dialog->endpoint_GUID = rand_guid();
441 session->focus_dialog->ourtag = gentag();
443 self = sip_uri_self(sipe_private);
444 session->focus_dialog->outgoing_invite =
445 cccp_request(sipe_private, "INVITE",
446 session->focus_dialog->with, session->focus_dialog,
447 process_invite_conf_focus_response,
448 CCCP_ADD_USER,
449 session->focus_dialog->with, self,
450 session->focus_dialog->endpoint_GUID);
452 /* Rejoin existing session? */
453 if (chat_session) {
454 SIPE_DEBUG_INFO("sipe_conf_create: rejoin '%s' (%s)",
455 chat_session->title,
456 chat_session->id);
457 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
458 chat_session->backend,
459 self,
460 chat_session->title);
462 g_free(self);
464 return(session);
467 /** Modify User Role */
468 void
469 sipe_conf_modify_user_role(struct sipe_core_private *sipe_private,
470 struct sip_session *session,
471 const gchar* who)
473 /* modifyUserRoles request to the focus. Makes user a leader.
475 * focus_uri (%s)
476 * who (%s)
478 static const gchar CCCP_MODIFY_USER_ROLES[] =
479 "<modifyUserRoles>"
480 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"
481 "<user-roles xmlns=\"urn:ietf:params:xml:ns:conference-info\">"
482 "<entry>presenter</entry>"
483 "</user-roles>"
484 "</modifyUserRoles>";
486 if (!session->focus_dialog || !session->focus_dialog->is_established) {
487 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_user_role: no dialog with focus, exiting.");
488 return;
491 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
492 session->focus_dialog, NULL,
493 CCCP_MODIFY_USER_ROLES,
494 session->focus_dialog->with, who);
498 * Check conference lock status
500 sipe_chat_lock_status sipe_core_chat_lock_status(struct sipe_core_public *sipe_public,
501 struct sipe_chat_session *chat_session)
503 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
504 sipe_chat_lock_status status = SIPE_CHAT_LOCK_STATUS_NOT_ALLOWED;
506 if (chat_session &&
507 (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE)) {
508 struct sip_session *session = sipe_session_find_chat(sipe_private,
509 chat_session);
510 if (session) {
511 gchar *self = sip_uri_self(sipe_private);
513 /* Only operators are allowed to change the lock status */
514 if (sipe_backend_chat_is_operator(chat_session->backend, self)) {
515 status = session->locked ?
516 SIPE_CHAT_LOCK_STATUS_LOCKED :
517 SIPE_CHAT_LOCK_STATUS_UNLOCKED;
520 g_free(self);
524 return(status);
528 * Modify Conference Lock
529 * Sends request to Focus.
530 * INFO method is a carrier of application/cccp+xml
532 void
533 sipe_core_chat_modify_lock(struct sipe_core_public *sipe_public,
534 struct sipe_chat_session *chat_session,
535 const gboolean locked)
537 /* modifyConferenceLock request to the focus. Locks/unlocks conference.
539 * focus_uri (%s)
540 * locked (%s) "true" or "false" values applicable
542 static const gchar CCCP_MODIFY_CONFERENCE_LOCK[] =
543 "<modifyConferenceLock>"
544 "<conferenceKeys confEntity=\"%s\"/>"
545 "<locked>%s</locked>"
546 "</modifyConferenceLock>";
548 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
550 struct sip_session *session = sipe_session_find_chat(sipe_private,
551 chat_session);
553 if (!session) return;
554 if (!session->focus_dialog || !session->focus_dialog->is_established) {
555 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_conference_lock: no dialog with focus, exiting.");
556 return;
559 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
560 session->focus_dialog, NULL,
561 CCCP_MODIFY_CONFERENCE_LOCK,
562 session->focus_dialog->with,
563 locked ? "true" : "false");
566 /** Modify Delete User */
567 void
568 sipe_conf_delete_user(struct sipe_core_private *sipe_private,
569 struct sip_session *session,
570 const gchar* who)
572 /* deleteUser request to the focus. Removes a user from the conference.
574 * focus_uri (%s)
575 * who (%s)
577 static const gchar CCCP_DELETE_USER[] =
578 "<deleteUser>"
579 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"
580 "</deleteUser>";
582 if (!session->focus_dialog || !session->focus_dialog->is_established) {
583 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_delete_user: no dialog with focus, exiting.");
584 return;
587 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
588 session->focus_dialog, NULL,
589 CCCP_DELETE_USER,
590 session->focus_dialog->with, who);
593 /** Invite counterparty to join conference callback */
594 static gboolean
595 process_invite_conf_response(struct sipe_core_private *sipe_private,
596 struct sipmsg *msg,
597 SIPE_UNUSED_PARAMETER struct transaction *trans)
599 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
601 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
602 dialog->cseq = sipmsg_parse_cseq(msg);
603 dialog->with = parse_from(sipmsg_find_header(msg, "To"));
604 sipe_dialog_parse(dialog, msg, TRUE);
606 if (msg->response >= 200) {
607 /* send ACK to counterparty */
608 dialog->cseq--;
609 sip_transport_ack(sipe_private, dialog);
610 dialog->outgoing_invite = NULL;
611 dialog->is_established = TRUE;
614 if (msg->response >= 400) {
615 SIPE_DEBUG_INFO("process_invite_conf_response: INVITE response is not 200. Failed to invite %s.", dialog->with);
616 /* @TODO notify user of failure to invite counterparty */
617 sipe_dialog_free(dialog);
618 return FALSE;
621 if (msg->response >= 200) {
622 struct sip_session *session = sipe_session_find_im(sipe_private, dialog->with);
623 struct sip_dialog *im_dialog = sipe_dialog_find(session, dialog->with);
625 /* close IM session to counterparty */
626 if (im_dialog) {
627 sip_transport_bye(sipe_private, im_dialog);
628 sipe_dialog_remove(session, dialog->with);
632 sipe_dialog_free(dialog);
633 return TRUE;
637 * Invites counterparty to join conference.
639 void
640 sipe_invite_conf(struct sipe_core_private *sipe_private,
641 struct sip_session *session,
642 const gchar* who)
644 gchar *hdr;
645 gchar *contact;
646 gchar *body;
647 struct sip_dialog *dialog = NULL;
649 /* It will be short lived special dialog.
650 * Will not be stored in session.
652 dialog = g_new0(struct sip_dialog, 1);
653 dialog->callid = gencallid();
654 dialog->with = g_strdup(who);
655 dialog->ourtag = gentag();
657 contact = get_contact(sipe_private);
658 hdr = g_strdup_printf(
659 "Supported: ms-sender\r\n"
660 "Contact: %s\r\n"
661 "Content-Type: application/ms-conf-invite+xml\r\n",
662 contact);
663 g_free(contact);
665 body = g_strdup_printf(
666 SIPE_SEND_CONF_INVITE,
667 session->chat_session->id,
668 session->subject ? session->subject : ""
671 sip_transport_invite(sipe_private,
672 hdr,
673 body,
674 dialog,
675 process_invite_conf_response);
677 sipe_dialog_free(dialog);
678 g_free(body);
679 g_free(hdr);
682 /** Create conference callback */
683 static gboolean
684 process_conf_add_response(struct sipe_core_private *sipe_private,
685 struct sipmsg *msg,
686 struct transaction *trans)
688 if (msg->response >= 400) {
689 SIPE_DEBUG_INFO_NOFORMAT("process_conf_add_response: SERVICE response is not 200. Failed to create conference.");
690 /* @TODO notify user of failure to create conference */
691 return FALSE;
693 if (msg->response == 200) {
694 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
695 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code")))
697 gchar *who = trans->payload->data;
698 const sipe_xml *xn_conference_info = sipe_xml_child(xn_response, "addConference/conference-info");
699 struct sip_session *session = sipe_conf_create(sipe_private,
700 NULL,
701 sipe_xml_attribute(xn_conference_info,
702 "entity"));
704 SIPE_DEBUG_INFO("process_conf_add_response: session->focus_uri=%s",
705 session->chat_session->id);
707 session->pending_invite_queue = sipe_utils_slist_insert_unique_sorted(session->pending_invite_queue,
708 g_strdup(who),
709 (GCompareFunc)strcmp,
710 g_free);
712 sipe_xml_free(xn_response);
715 return TRUE;
719 * Creates conference.
721 void
722 sipe_conf_add(struct sipe_core_private *sipe_private,
723 const gchar* who)
725 gchar *conference_id;
726 struct transaction *trans;
727 time_t expiry = time(NULL) + 7*60*60; /* 7 hours */
728 char *expiry_time;
729 struct transaction_payload *payload;
731 /* addConference request to the focus factory.
733 * conference_id (%s) Ex.: 8386E6AEAAA41E4AA6627BA76D43B6D1
734 * expiry_time (%s) Ex.: 2009-07-13T17:57:09Z
735 * conference_view (%s) Ex.: <msci:entity-view entity="chat"/>
737 static const gchar CCCP_ADD_CONFERENCE[] =
738 "<addConference>"
739 "<ci:conference-info xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" "
740 "entity=\"\" "
741 "xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\">"
742 "<ci:conference-description>"
743 "<ci:subject/>"
744 "<msci:conference-id>%s</msci:conference-id>"
745 "<msci:expiry-time>%s</msci:expiry-time>"
746 "<msci:admission-policy>openAuthenticated</msci:admission-policy>"
747 "</ci:conference-description>"
748 "<msci:conference-view>%s</msci:conference-view>"
749 "</ci:conference-info>"
750 "</addConference>";
752 static const gchar *DESIRED_MCU_TYPES[] = {
753 "chat",
754 #ifdef HAVE_VV
755 "audio-video",
756 #endif
757 NULL
760 GString *conference_view = g_string_new("");
761 const gchar **type;
763 for (type = DESIRED_MCU_TYPES; *type; ++type ) {
764 if (sipe_conf_supports_mcu_type(sipe_private, *type)) {
765 g_string_append(conference_view, "<msci:entity-view entity=\"");
766 g_string_append(conference_view, *type);
767 g_string_append(conference_view, "\"/>");
771 expiry_time = sipe_utils_time_to_str(expiry);
772 conference_id = genconfid();
773 trans = cccp_request(sipe_private, "SERVICE", sipe_private->focus_factory_uri,
774 NULL, process_conf_add_response,
775 CCCP_ADD_CONFERENCE,
776 conference_id, expiry_time, conference_view->str);
777 g_free(conference_id);
778 g_free(expiry_time);
779 g_string_free(conference_view, TRUE);
781 payload = g_new0(struct transaction_payload, 1);
782 payload->destroy = g_free;
783 payload->data = g_strdup(who);
784 trans->payload = payload;
787 static void
788 accept_incoming_invite_conf(struct sipe_core_private *sipe_private,
789 gchar *focus_uri,
790 gboolean audio,
791 struct sipmsg *msg)
793 struct sip_session *session;
794 gchar *newTag = gentag();
795 const gchar *oldHeader = sipmsg_find_header(msg, "To");
796 gchar *newHeader;
798 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
799 g_free(newTag);
800 sipmsg_remove_header_now(msg, "To");
801 sipmsg_add_header_now(msg, "To", newHeader);
802 g_free(newHeader);
804 /* acknowledge invite */
805 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
807 /* add self to conf */
808 session = sipe_conf_create(sipe_private, NULL, focus_uri);
809 session->is_call = audio;
812 struct conf_accept_ctx {
813 gchar *focus_uri;
814 struct sipmsg *msg;
815 struct sipe_user_ask_ctx *ask_ctx;
818 static void
819 conf_accept_ctx_free(struct conf_accept_ctx *ctx)
821 g_return_if_fail(ctx != NULL);
823 sipmsg_free(ctx->msg);
824 g_free(ctx->focus_uri);
825 g_free(ctx);
828 static void
829 conf_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
831 sipe_private->sessions_to_accept =
832 g_slist_remove(sipe_private->sessions_to_accept, ctx);
834 accept_incoming_invite_conf(sipe_private, ctx->focus_uri, TRUE, ctx->msg);
835 conf_accept_ctx_free(ctx);
838 static void
839 conf_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
841 sipe_private->sessions_to_accept =
842 g_slist_remove(sipe_private->sessions_to_accept, ctx);
844 sip_transport_response(sipe_private,
845 ctx->msg,
846 603, "Decline", NULL);
848 conf_accept_ctx_free(ctx);
851 void
852 sipe_conf_cancel_unaccepted(struct sipe_core_private *sipe_private,
853 struct sipmsg *msg)
855 const gchar *callid1 = msg ? sipmsg_find_header(msg, "Call-ID") : NULL;
856 GSList *it = sipe_private->sessions_to_accept;
857 while (it) {
858 struct conf_accept_ctx *ctx = it->data;
859 const gchar *callid2 = NULL;
861 if (msg && ctx->msg)
862 callid2 = sipmsg_find_header(ctx->msg, "Call-ID");
864 if (sipe_strequal(callid1, callid2)) {
865 GSList *tmp;
867 if (ctx->msg)
868 sip_transport_response(sipe_private, ctx->msg,
869 487, "Request Terminated", NULL);
871 if (msg)
872 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
874 sipe_user_close_ask(ctx->ask_ctx);
875 conf_accept_ctx_free(ctx);
877 tmp = it;
878 it = it->next;
880 sipe_private->sessions_to_accept =
881 g_slist_delete_link(sipe_private->sessions_to_accept, tmp);
883 if (callid1)
884 break;
885 } else
886 it = it->next;
890 static void
891 ask_accept_voice_conference(struct sipe_core_private *sipe_private,
892 const gchar *focus_uri,
893 struct sipmsg *msg,
894 SipeUserAskCb accept_cb,
895 SipeUserAskCb decline_cb)
897 gchar **parts;
898 gchar *alias;
899 gchar *ask_msg;
900 const gchar *novv_note;
901 struct conf_accept_ctx *ctx;
903 #ifdef HAVE_VV
904 novv_note = "";
905 #else
906 novv_note = _("\n\nAs this client was not compiled with voice call "
907 "support, if you accept, you will be able to contact "
908 "the other participants only via IM session.");
909 #endif
911 parts = g_strsplit(focus_uri, ";", 2);
912 alias = sipe_buddy_get_alias(sipe_private, parts[0]);
914 ask_msg = g_strdup_printf(_("%s wants to invite you "
915 "to the conference call%s"),
916 alias ? alias : parts[0], novv_note);
918 g_free(alias);
919 g_strfreev(parts);
921 ctx = g_new0(struct conf_accept_ctx, 1);
922 sipe_private->sessions_to_accept =
923 g_slist_append(sipe_private->sessions_to_accept, ctx);
925 ctx->focus_uri = g_strdup(focus_uri);
926 ctx->msg = msg ? sipmsg_copy(msg) : NULL;
927 ctx->ask_ctx = sipe_user_ask(sipe_private, ask_msg,
928 _("Accept"), accept_cb,
929 _("Decline"), decline_cb,
930 ctx);
932 g_free(ask_msg);
935 void
936 process_incoming_invite_conf(struct sipe_core_private *sipe_private,
937 struct sipmsg *msg)
939 sipe_xml *xn_conferencing = sipe_xml_parse(msg->body, msg->bodylen);
940 const sipe_xml *xn_focus_uri = sipe_xml_child(xn_conferencing, "focus-uri");
941 const sipe_xml *xn_audio = sipe_xml_child(xn_conferencing, "audio");
942 gchar *focus_uri = sipe_xml_data(xn_focus_uri);
943 gboolean audio = sipe_strequal(sipe_xml_attribute(xn_audio, "available"), "true");
945 sipe_xml_free(xn_conferencing);
947 SIPE_DEBUG_INFO("We have received invitation to Conference. Focus URI=%s", focus_uri);
949 if (audio) {
950 sip_transport_response(sipe_private, msg, 180, "Ringing", NULL);
951 ask_accept_voice_conference(sipe_private, focus_uri, msg,
952 (SipeUserAskCb) conf_accept_cb,
953 (SipeUserAskCb) conf_decline_cb);
955 } else {
956 accept_incoming_invite_conf(sipe_private, focus_uri, FALSE, msg);
959 g_free(focus_uri);
962 #ifdef HAVE_VV
964 static void
965 call_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
967 struct sip_session *session;
968 session = sipe_session_find_conference(sipe_private, ctx->focus_uri);
970 sipe_private->sessions_to_accept =
971 g_slist_remove(sipe_private->sessions_to_accept, ctx);
973 if (session) {
974 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
975 session->chat_session);
978 conf_accept_ctx_free(ctx);
981 static void
982 call_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
984 sipe_private->sessions_to_accept =
985 g_slist_remove(sipe_private->sessions_to_accept, ctx);
987 conf_accept_ctx_free(ctx);
990 #endif // HAVE_VV
992 void
993 sipe_process_conference(struct sipe_core_private *sipe_private,
994 struct sipmsg *msg)
996 sipe_xml *xn_conference_info;
997 const sipe_xml *node;
998 const sipe_xml *xn_subject;
999 const gchar *focus_uri;
1000 struct sip_session *session;
1001 gboolean just_joined = FALSE;
1002 #ifdef HAVE_VV
1003 gboolean audio_was_added = FALSE;
1004 #endif
1006 if (msg->response != 0 && msg->response != 200) return;
1008 if (msg->bodylen == 0 || msg->body == NULL || !sipe_strequal(sipmsg_find_header(msg, "Event"), "conference")) return;
1010 xn_conference_info = sipe_xml_parse(msg->body, msg->bodylen);
1011 if (!xn_conference_info) return;
1013 focus_uri = sipe_xml_attribute(xn_conference_info, "entity");
1014 session = sipe_session_find_conference(sipe_private, focus_uri);
1016 if (!session) {
1017 SIPE_DEBUG_INFO("sipe_process_conference: unable to find conf session with focus=%s", focus_uri);
1018 return;
1021 if (!session->chat_session->backend) {
1022 gchar *self = sip_uri_self(sipe_private);
1024 /* create chat */
1025 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1026 session->chat_session,
1027 session->chat_session->title,
1028 self);
1029 just_joined = TRUE;
1030 /* @TODO ask for full state (re-subscribe) if it was a partial one -
1031 * this is to obtain full list of conference participants.
1033 g_free(self);
1036 /* subject */
1037 if ((xn_subject = sipe_xml_child(xn_conference_info, "conference-description/subject"))) {
1038 g_free(session->subject);
1039 session->subject = sipe_xml_data(xn_subject);
1040 sipe_backend_chat_topic(session->chat_session->backend, session->subject);
1041 SIPE_DEBUG_INFO("sipe_process_conference: subject=%s", session->subject ? session->subject : "");
1044 /* IM MCU URI */
1045 if (!session->im_mcu_uri) {
1046 for (node = sipe_xml_child(xn_conference_info, "conference-description/conf-uris/entry");
1047 node;
1048 node = sipe_xml_twin(node))
1050 gchar *purpose = sipe_xml_data(sipe_xml_child(node, "purpose"));
1052 if (sipe_strequal("chat", purpose)) {
1053 g_free(purpose);
1054 session->im_mcu_uri = sipe_xml_data(sipe_xml_child(node, "uri"));
1055 SIPE_DEBUG_INFO("sipe_process_conference: im_mcu_uri=%s", session->im_mcu_uri);
1056 break;
1058 g_free(purpose);
1062 /* users */
1063 for (node = sipe_xml_child(xn_conference_info, "users/user"); node; node = sipe_xml_twin(node)) {
1064 const gchar *user_uri = sipe_xml_attribute(node, "entity");
1065 const gchar *state = sipe_xml_attribute(node, "state");
1066 gchar *role = sipe_xml_data(sipe_xml_child(node, "roles/entry"));
1067 gboolean is_operator = sipe_strequal(role, "presenter");
1068 gboolean is_in_im_mcu = FALSE;
1069 gchar *self = sip_uri_self(sipe_private);
1071 if (sipe_strequal("deleted", state)) {
1072 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1073 sipe_backend_chat_remove(session->chat_session->backend,
1074 user_uri);
1076 } else {
1077 /* endpoints */
1078 const sipe_xml *endpoint;
1079 for (endpoint = sipe_xml_child(node, "endpoint"); endpoint; endpoint = sipe_xml_twin(endpoint)) {
1080 const gchar *session_type;
1081 gchar *status = sipe_xml_data(sipe_xml_child(endpoint, "status"));
1082 gboolean connected = sipe_strequal("connected", status);
1083 g_free(status);
1085 if (!connected)
1086 continue;
1088 session_type = sipe_xml_attribute(endpoint, "session-type");
1090 if (sipe_strequal("chat", session_type)) {
1091 is_in_im_mcu = TRUE;
1092 if (!sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1093 sipe_backend_chat_add(session->chat_session->backend,
1094 user_uri,
1095 !just_joined && g_ascii_strcasecmp(user_uri, self));
1097 if (is_operator) {
1098 sipe_backend_chat_operator(session->chat_session->backend,
1099 user_uri);
1101 } else if (sipe_strequal("audio-video", session_type)) {
1102 #ifdef HAVE_VV
1103 if (!session->is_call)
1104 audio_was_added = TRUE;
1105 #endif
1108 if (!is_in_im_mcu) {
1109 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1110 sipe_backend_chat_remove(session->chat_session->backend,
1111 user_uri);
1115 g_free(role);
1116 g_free(self);
1119 #ifdef HAVE_VV
1120 if (audio_was_added) {
1121 session->is_call = TRUE;
1122 ask_accept_voice_conference(sipe_private, focus_uri, NULL,
1123 (SipeUserAskCb) call_accept_cb,
1124 (SipeUserAskCb) call_decline_cb);
1126 #endif
1128 /* entity-view, locked */
1129 for (node = sipe_xml_child(xn_conference_info, "conference-view/entity-view");
1130 node;
1131 node = sipe_xml_twin(node)) {
1133 const sipe_xml *xn_type = sipe_xml_child(node, "entity-state/media/entry/type");
1134 gchar *tmp = NULL;
1135 if (xn_type && sipe_strequal("chat", (tmp = sipe_xml_data(xn_type)))) {
1136 const sipe_xml *xn_locked = sipe_xml_child(node, "entity-state/locked");
1137 if (xn_locked) {
1138 gchar *locked = sipe_xml_data(xn_locked);
1139 gboolean prev_locked = session->locked;
1140 session->locked = sipe_strequal(locked, "true");
1141 if (prev_locked && !session->locked) {
1142 sipe_user_present_info(sipe_private, session,
1143 _("This conference is no longer locked. Additional participants can now join."));
1145 if (!prev_locked && session->locked) {
1146 sipe_user_present_info(sipe_private, session,
1147 _("This conference is locked. Nobody else can join the conference while it is locked."));
1150 SIPE_DEBUG_INFO("sipe_process_conference: session->locked=%s",
1151 session->locked ? "TRUE" : "FALSE");
1152 g_free(locked);
1155 g_free(tmp);
1157 sipe_xml_free(xn_conference_info);
1159 if (session->im_mcu_uri) {
1160 struct sip_dialog *dialog = sipe_dialog_find(session, session->im_mcu_uri);
1161 if (!dialog) {
1162 dialog = sipe_dialog_add(session);
1164 dialog->callid = g_strdup(session->callid);
1165 dialog->with = g_strdup(session->im_mcu_uri);
1167 /* send INVITE to IM MCU */
1168 sipe_im_invite(sipe_private, session, dialog->with, NULL, NULL, NULL, FALSE);
1172 sipe_process_pending_invite_queue(sipe_private, session);
1175 void
1176 sipe_conf_immcu_closed(struct sipe_core_private *sipe_private,
1177 struct sip_session *session)
1179 sipe_user_present_info(sipe_private, session,
1180 _("You have been disconnected from this conference."));
1181 sipe_backend_chat_close(session->chat_session->backend);
1184 void
1185 conf_session_close(struct sipe_core_private *sipe_private,
1186 struct sip_session *session)
1188 if (session) {
1189 /* unsubscribe from focus */
1190 sipe_subscribe_conference(sipe_private,
1191 session->chat_session->id, TRUE);
1193 if (session->focus_dialog) {
1194 /* send BYE to focus */
1195 sip_transport_bye(sipe_private, session->focus_dialog);
1200 void
1201 sipe_process_imdn(struct sipe_core_private *sipe_private,
1202 struct sipmsg *msg)
1204 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1205 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1206 static struct sip_session *session;
1207 sipe_xml *xn_imdn;
1208 const sipe_xml *node;
1209 gchar *message_id;
1210 gchar *message;
1212 session = sipe_session_find_chat_or_im(sipe_private, callid, with);
1213 if (!session) {
1214 SIPE_DEBUG_INFO("sipe_process_imdn: unable to find conf session with callid=%s", callid);
1215 g_free(with);
1216 return;
1219 xn_imdn = sipe_xml_parse(msg->body, msg->bodylen);
1220 message_id = sipe_xml_data(sipe_xml_child(xn_imdn, "message-id"));
1222 message = g_hash_table_lookup(session->conf_unconfirmed_messages, message_id);
1224 /* recipient */
1225 for (node = sipe_xml_child(xn_imdn, "recipient"); node; node = sipe_xml_twin(node)) {
1226 gchar *tmp = parse_from(sipe_xml_attribute(node, "uri"));
1227 gchar *uri = parse_from(tmp);
1228 gchar *status = sipe_xml_data(sipe_xml_child(node, "status"));
1229 guint error = status ? g_ascii_strtoull(status, NULL, 10) : 0;
1230 /* default to error if missing or conversion failed */
1231 if ((error == 0) || (error >= 300))
1232 sipe_user_present_message_undelivered(sipe_private,
1233 session,
1234 error,
1236 uri,
1237 message);
1238 g_free(status);
1239 g_free(tmp);
1240 g_free(uri);
1243 sipe_xml_free(xn_imdn);
1245 g_hash_table_remove(session->conf_unconfirmed_messages, message_id);
1246 SIPE_DEBUG_INFO("sipe_process_imdn: removed message %s from conf_unconfirmed_messages(count=%d)",
1247 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
1248 g_free(message_id);
1249 g_free(with);
1252 void sipe_core_conf_make_leader(struct sipe_core_public *sipe_public,
1253 gpointer parameter,
1254 const gchar *buddy_name)
1256 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1257 struct sipe_chat_session *chat_session = parameter;
1258 struct sip_session *session;
1260 SIPE_DEBUG_INFO("sipe_core_conf_make_leader: chat_title=%s",
1261 chat_session->title);
1263 session = sipe_session_find_chat(sipe_private, chat_session);
1264 sipe_conf_modify_user_role(sipe_private, session, buddy_name);
1267 void sipe_core_conf_remove_from(struct sipe_core_public *sipe_public,
1268 gpointer parameter,
1269 const gchar *buddy_name)
1271 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1272 struct sipe_chat_session *chat_session = parameter;
1273 struct sip_session *session;
1275 SIPE_DEBUG_INFO("sipe_core_conf_remove_from: chat_title=%s",
1276 chat_session->title);
1278 session = sipe_session_find_chat(sipe_private, chat_session);
1279 sipe_conf_delete_user(sipe_private, session, buddy_name);
1283 Local Variables:
1284 mode: c
1285 c-file-style: "bsd"
1286 indent-tabs-mode: t
1287 tab-width: 8
1288 End: