tls: add Server Key Exchange message
[siplcs.git] / src / core / sipe-conf.c
blob14b73cb3fa87ee60591dcc7e7c0da06416f11de7
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 guint status,
351 SIPE_UNUSED_PARAMETER GSList *headers,
352 const gchar *body,
353 gpointer callback_data)
355 gchar *uri = callback_data;
357 if (status != (guint) SIPE_HTTP_STATUS_ABORTED) {
358 gchar *focus_uri = NULL;
360 if (body) {
362 * Extract focus URI from HTML, e.g.
364 * <a ... href="conf&#58;sip&#58;...ABCDEF&#37;3Frequired..." ... >
366 const gchar *start = g_strstr_len(body,
368 "href=\"conf");
369 if (start) {
370 const gchar *end;
372 start += 6;
373 end = strchr(start, '"');
375 if (end) {
376 gchar *html = g_strndup(start,
377 end - start);
379 /* decode HTML entities */
380 gchar *html_unescaped = sipe_backend_markup_strip_html(html);
381 g_free(html);
383 if (!is_empty(html_unescaped)) {
384 gchar *uri_unescaped = sipe_utils_uri_unescape(html_unescaped);
385 SIPE_DEBUG_INFO("sipe_conf_lync_url_cb: found focus URI '%s'",
386 uri_unescaped);
387 focus_uri = parse_ocs_focus_uri(uri_unescaped);
388 g_free(uri_unescaped);
390 g_free(html_unescaped);
395 /* If we can't find a focus URI then fall back to URL parser */
396 if (!focus_uri) {
397 SIPE_DEBUG_INFO("sipe_conf_lync_url_cb: no focus URI found. Falling back to parsing Lync URL '%s'",
398 uri);
399 focus_uri = parse_lync_join_url(uri);
402 if (focus_uri) {
403 sipe_conf_create(sipe_private, NULL, focus_uri);
404 g_free(focus_uri);
405 } else {
406 sipe_conf_error(sipe_private, uri);
410 g_free(uri);
413 static gboolean sipe_conf_check_for_lync_url(struct sipe_core_private *sipe_private,
414 gchar *uri)
416 if (!(g_str_has_prefix(uri, "https://") ||
417 g_str_has_prefix(uri, "http://")))
418 return(FALSE);
420 /* URL points to a HTML page with the conference focus URI */
421 return(sipe_http_request_get(sipe_private,
422 uri,
423 NULL,
424 sipe_conf_lync_url_cb,
425 uri)
426 != NULL);
429 void sipe_core_conf_create(struct sipe_core_public *sipe_public,
430 const gchar *uri)
432 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
433 gchar *uri_ue = sipe_utils_uri_unescape(uri);
435 SIPE_DEBUG_INFO("sipe_core_conf_create: URI '%s' unescaped '%s'",
436 uri ? uri : "<UNDEFINED>",
437 uri_ue ? uri_ue : "<UNDEFINED>");
439 /* takes ownership of "uri_ue" if successful */
440 if (!sipe_conf_check_for_lync_url(sipe_private, uri_ue)) {
441 gchar *focus_uri = parse_ocs_focus_uri(uri_ue);
443 if (focus_uri) {
444 sipe_conf_create(sipe_private, NULL, focus_uri);
445 g_free(focus_uri);
446 } else {
447 sipe_conf_error(sipe_private, uri);
450 g_free(uri_ue);
454 /** Create new session with Focus URI */
455 struct sip_session *
456 sipe_conf_create(struct sipe_core_private *sipe_private,
457 struct sipe_chat_session *chat_session,
458 const gchar *focus_uri)
460 /* addUser request to the focus.
462 * focus_URI, from, endpoint_GUID
464 static const gchar CCCP_ADD_USER[] =
465 "<addUser>"
466 "<conferenceKeys confEntity=\"%s\"/>"
467 "<ci:user xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"%s\">"
468 "<ci:roles>"
469 "<ci:entry>attendee</ci:entry>"
470 "</ci:roles>"
471 "<ci:endpoint entity=\"{%s}\" "
472 "xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\"/>"
473 "</ci:user>"
474 "</addUser>";
476 gchar *self;
477 struct sip_session *session = sipe_session_add_chat(sipe_private,
478 chat_session,
479 FALSE,
480 focus_uri);
482 session->focus_dialog = g_new0(struct sip_dialog, 1);
483 session->focus_dialog->callid = gencallid();
484 session->focus_dialog->with = g_strdup(session->chat_session->id);
485 session->focus_dialog->endpoint_GUID = rand_guid();
486 session->focus_dialog->ourtag = gentag();
488 self = sip_uri_self(sipe_private);
489 session->focus_dialog->outgoing_invite =
490 cccp_request(sipe_private, "INVITE",
491 session->focus_dialog->with, session->focus_dialog,
492 process_invite_conf_focus_response,
493 CCCP_ADD_USER,
494 session->focus_dialog->with, self,
495 session->focus_dialog->endpoint_GUID);
497 /* Rejoin existing session? */
498 if (chat_session) {
499 SIPE_DEBUG_INFO("sipe_conf_create: rejoin '%s' (%s)",
500 chat_session->title,
501 chat_session->id);
502 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
503 chat_session->backend,
504 self,
505 chat_session->title);
507 g_free(self);
509 return(session);
512 /** Modify User Role */
513 void
514 sipe_conf_modify_user_role(struct sipe_core_private *sipe_private,
515 struct sip_session *session,
516 const gchar* who)
518 /* modifyUserRoles request to the focus. Makes user a leader.
520 * focus_uri (%s)
521 * who (%s)
523 static const gchar CCCP_MODIFY_USER_ROLES[] =
524 "<modifyUserRoles>"
525 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"
526 "<user-roles xmlns=\"urn:ietf:params:xml:ns:conference-info\">"
527 "<entry>presenter</entry>"
528 "</user-roles>"
529 "</modifyUserRoles>";
531 if (!session->focus_dialog || !session->focus_dialog->is_established) {
532 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_user_role: no dialog with focus, exiting.");
533 return;
536 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
537 session->focus_dialog, NULL,
538 CCCP_MODIFY_USER_ROLES,
539 session->focus_dialog->with, who);
543 * Check conference lock status
545 sipe_chat_lock_status sipe_core_chat_lock_status(struct sipe_core_public *sipe_public,
546 struct sipe_chat_session *chat_session)
548 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
549 sipe_chat_lock_status status = SIPE_CHAT_LOCK_STATUS_NOT_ALLOWED;
551 if (chat_session &&
552 (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE)) {
553 struct sip_session *session = sipe_session_find_chat(sipe_private,
554 chat_session);
555 if (session) {
556 gchar *self = sip_uri_self(sipe_private);
558 /* Only operators are allowed to change the lock status */
559 if (sipe_backend_chat_is_operator(chat_session->backend, self)) {
560 status = session->locked ?
561 SIPE_CHAT_LOCK_STATUS_LOCKED :
562 SIPE_CHAT_LOCK_STATUS_UNLOCKED;
565 g_free(self);
569 return(status);
573 * Modify Conference Lock
574 * Sends request to Focus.
575 * INFO method is a carrier of application/cccp+xml
577 void
578 sipe_core_chat_modify_lock(struct sipe_core_public *sipe_public,
579 struct sipe_chat_session *chat_session,
580 const gboolean locked)
582 /* modifyConferenceLock request to the focus. Locks/unlocks conference.
584 * focus_uri (%s)
585 * locked (%s) "true" or "false" values applicable
587 static const gchar CCCP_MODIFY_CONFERENCE_LOCK[] =
588 "<modifyConferenceLock>"
589 "<conferenceKeys confEntity=\"%s\"/>"
590 "<locked>%s</locked>"
591 "</modifyConferenceLock>";
593 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
595 struct sip_session *session = sipe_session_find_chat(sipe_private,
596 chat_session);
598 if (!session) return;
599 if (!session->focus_dialog || !session->focus_dialog->is_established) {
600 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_conference_lock: no dialog with focus, exiting.");
601 return;
604 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
605 session->focus_dialog, NULL,
606 CCCP_MODIFY_CONFERENCE_LOCK,
607 session->focus_dialog->with,
608 locked ? "true" : "false");
611 /** Modify Delete User */
612 void
613 sipe_conf_delete_user(struct sipe_core_private *sipe_private,
614 struct sip_session *session,
615 const gchar* who)
617 /* deleteUser request to the focus. Removes a user from the conference.
619 * focus_uri (%s)
620 * who (%s)
622 static const gchar CCCP_DELETE_USER[] =
623 "<deleteUser>"
624 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"
625 "</deleteUser>";
627 if (!session->focus_dialog || !session->focus_dialog->is_established) {
628 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_delete_user: no dialog with focus, exiting.");
629 return;
632 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
633 session->focus_dialog, NULL,
634 CCCP_DELETE_USER,
635 session->focus_dialog->with, who);
638 /** Invite counterparty to join conference callback */
639 static gboolean
640 process_invite_conf_response(struct sipe_core_private *sipe_private,
641 struct sipmsg *msg,
642 SIPE_UNUSED_PARAMETER struct transaction *trans)
644 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
646 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
647 dialog->cseq = sipmsg_parse_cseq(msg);
648 dialog->with = parse_from(sipmsg_find_header(msg, "To"));
649 sipe_dialog_parse(dialog, msg, TRUE);
651 if (msg->response >= 200) {
652 /* send ACK to counterparty */
653 dialog->cseq--;
654 sip_transport_ack(sipe_private, dialog);
655 dialog->outgoing_invite = NULL;
656 dialog->is_established = TRUE;
659 if (msg->response >= 400) {
660 SIPE_DEBUG_INFO("process_invite_conf_response: INVITE response is not 200. Failed to invite %s.", dialog->with);
661 /* @TODO notify user of failure to invite counterparty */
662 sipe_dialog_free(dialog);
663 return FALSE;
666 if (msg->response >= 200) {
667 struct sip_session *session = sipe_session_find_im(sipe_private, dialog->with);
668 struct sip_dialog *im_dialog = sipe_dialog_find(session, dialog->with);
670 /* close IM session to counterparty */
671 if (im_dialog) {
672 sip_transport_bye(sipe_private, im_dialog);
673 sipe_dialog_remove(session, dialog->with);
677 sipe_dialog_free(dialog);
678 return TRUE;
682 * Invites counterparty to join conference.
684 void
685 sipe_invite_conf(struct sipe_core_private *sipe_private,
686 struct sip_session *session,
687 const gchar* who)
689 gchar *hdr;
690 gchar *contact;
691 gchar *body;
692 struct sip_dialog *dialog = NULL;
694 /* It will be short lived special dialog.
695 * Will not be stored in session.
697 dialog = g_new0(struct sip_dialog, 1);
698 dialog->callid = gencallid();
699 dialog->with = g_strdup(who);
700 dialog->ourtag = gentag();
702 contact = get_contact(sipe_private);
703 hdr = g_strdup_printf(
704 "Supported: ms-sender\r\n"
705 "Contact: %s\r\n"
706 "Content-Type: application/ms-conf-invite+xml\r\n",
707 contact);
708 g_free(contact);
710 body = g_strdup_printf(
711 SIPE_SEND_CONF_INVITE,
712 session->chat_session->id,
713 session->subject ? session->subject : ""
716 sip_transport_invite(sipe_private,
717 hdr,
718 body,
719 dialog,
720 process_invite_conf_response);
722 sipe_dialog_free(dialog);
723 g_free(body);
724 g_free(hdr);
727 /** Create conference callback */
728 static gboolean
729 process_conf_add_response(struct sipe_core_private *sipe_private,
730 struct sipmsg *msg,
731 struct transaction *trans)
733 if (msg->response >= 400) {
734 SIPE_DEBUG_INFO_NOFORMAT("process_conf_add_response: SERVICE response is not 200. Failed to create conference.");
735 /* @TODO notify user of failure to create conference */
736 return FALSE;
738 if (msg->response == 200) {
739 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
740 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code")))
742 gchar *who = trans->payload->data;
743 const sipe_xml *xn_conference_info = sipe_xml_child(xn_response, "addConference/conference-info");
744 struct sip_session *session = sipe_conf_create(sipe_private,
745 NULL,
746 sipe_xml_attribute(xn_conference_info,
747 "entity"));
749 SIPE_DEBUG_INFO("process_conf_add_response: session->focus_uri=%s",
750 session->chat_session->id);
752 session->pending_invite_queue = sipe_utils_slist_insert_unique_sorted(session->pending_invite_queue,
753 g_strdup(who),
754 (GCompareFunc)strcmp,
755 g_free);
757 sipe_xml_free(xn_response);
760 return TRUE;
764 * Creates conference.
766 void
767 sipe_conf_add(struct sipe_core_private *sipe_private,
768 const gchar* who)
770 gchar *conference_id;
771 struct transaction *trans;
772 time_t expiry = time(NULL) + 7*60*60; /* 7 hours */
773 char *expiry_time;
774 struct transaction_payload *payload;
776 /* addConference request to the focus factory.
778 * conference_id (%s) Ex.: 8386E6AEAAA41E4AA6627BA76D43B6D1
779 * expiry_time (%s) Ex.: 2009-07-13T17:57:09Z
780 * conference_view (%s) Ex.: <msci:entity-view entity="chat"/>
782 static const gchar CCCP_ADD_CONFERENCE[] =
783 "<addConference>"
784 "<ci:conference-info xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" "
785 "entity=\"\" "
786 "xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\">"
787 "<ci:conference-description>"
788 "<ci:subject/>"
789 "<msci:conference-id>%s</msci:conference-id>"
790 "<msci:expiry-time>%s</msci:expiry-time>"
791 "<msci:admission-policy>openAuthenticated</msci:admission-policy>"
792 "</ci:conference-description>"
793 "<msci:conference-view>%s</msci:conference-view>"
794 "</ci:conference-info>"
795 "</addConference>";
797 static const gchar *DESIRED_MCU_TYPES[] = {
798 "chat",
799 #ifdef HAVE_VV
800 "audio-video",
801 #endif
802 NULL
805 GString *conference_view = g_string_new("");
806 const gchar **type;
808 for (type = DESIRED_MCU_TYPES; *type; ++type ) {
809 if (sipe_conf_supports_mcu_type(sipe_private, *type)) {
810 g_string_append(conference_view, "<msci:entity-view entity=\"");
811 g_string_append(conference_view, *type);
812 g_string_append(conference_view, "\"/>");
816 expiry_time = sipe_utils_time_to_str(expiry);
817 conference_id = genconfid();
818 trans = cccp_request(sipe_private, "SERVICE", sipe_private->focus_factory_uri,
819 NULL, process_conf_add_response,
820 CCCP_ADD_CONFERENCE,
821 conference_id, expiry_time, conference_view->str);
822 g_free(conference_id);
823 g_free(expiry_time);
824 g_string_free(conference_view, TRUE);
826 payload = g_new0(struct transaction_payload, 1);
827 payload->destroy = g_free;
828 payload->data = g_strdup(who);
829 trans->payload = payload;
832 static void
833 accept_incoming_invite_conf(struct sipe_core_private *sipe_private,
834 gchar *focus_uri,
835 gboolean audio,
836 struct sipmsg *msg)
838 struct sip_session *session;
839 gchar *newTag = gentag();
840 const gchar *oldHeader = sipmsg_find_header(msg, "To");
841 gchar *newHeader;
843 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
844 g_free(newTag);
845 sipmsg_remove_header_now(msg, "To");
846 sipmsg_add_header_now(msg, "To", newHeader);
847 g_free(newHeader);
849 /* acknowledge invite */
850 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
852 /* add self to conf */
853 session = sipe_conf_create(sipe_private, NULL, focus_uri);
854 session->is_call = audio;
857 struct conf_accept_ctx {
858 gchar *focus_uri;
859 struct sipmsg *msg;
860 struct sipe_user_ask_ctx *ask_ctx;
863 static void
864 conf_accept_ctx_free(struct conf_accept_ctx *ctx)
866 g_return_if_fail(ctx != NULL);
868 sipmsg_free(ctx->msg);
869 g_free(ctx->focus_uri);
870 g_free(ctx);
873 static void
874 conf_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
876 sipe_private->sessions_to_accept =
877 g_slist_remove(sipe_private->sessions_to_accept, ctx);
879 accept_incoming_invite_conf(sipe_private, ctx->focus_uri, TRUE, ctx->msg);
880 conf_accept_ctx_free(ctx);
883 static void
884 conf_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
886 sipe_private->sessions_to_accept =
887 g_slist_remove(sipe_private->sessions_to_accept, ctx);
889 sip_transport_response(sipe_private,
890 ctx->msg,
891 603, "Decline", NULL);
893 conf_accept_ctx_free(ctx);
896 void
897 sipe_conf_cancel_unaccepted(struct sipe_core_private *sipe_private,
898 struct sipmsg *msg)
900 const gchar *callid1 = msg ? sipmsg_find_header(msg, "Call-ID") : NULL;
901 GSList *it = sipe_private->sessions_to_accept;
902 while (it) {
903 struct conf_accept_ctx *ctx = it->data;
904 const gchar *callid2 = NULL;
906 if (msg && ctx->msg)
907 callid2 = sipmsg_find_header(ctx->msg, "Call-ID");
909 if (sipe_strequal(callid1, callid2)) {
910 GSList *tmp;
912 if (ctx->msg)
913 sip_transport_response(sipe_private, ctx->msg,
914 487, "Request Terminated", NULL);
916 if (msg)
917 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
919 sipe_user_close_ask(ctx->ask_ctx);
920 conf_accept_ctx_free(ctx);
922 tmp = it;
923 it = it->next;
925 sipe_private->sessions_to_accept =
926 g_slist_delete_link(sipe_private->sessions_to_accept, tmp);
928 if (callid1)
929 break;
930 } else
931 it = it->next;
935 static void
936 ask_accept_voice_conference(struct sipe_core_private *sipe_private,
937 const gchar *focus_uri,
938 struct sipmsg *msg,
939 SipeUserAskCb accept_cb,
940 SipeUserAskCb decline_cb)
942 gchar **parts;
943 gchar *alias;
944 gchar *ask_msg;
945 const gchar *novv_note;
946 struct conf_accept_ctx *ctx;
948 #ifdef HAVE_VV
949 novv_note = "";
950 #else
951 novv_note = _("\n\nAs this client was not compiled with voice call "
952 "support, if you accept, you will be able to contact "
953 "the other participants only via IM session.");
954 #endif
956 parts = g_strsplit(focus_uri, ";", 2);
957 alias = sipe_buddy_get_alias(sipe_private, parts[0]);
959 ask_msg = g_strdup_printf(_("%s wants to invite you "
960 "to the conference call%s"),
961 alias ? alias : parts[0], novv_note);
963 g_free(alias);
964 g_strfreev(parts);
966 ctx = g_new0(struct conf_accept_ctx, 1);
967 sipe_private->sessions_to_accept =
968 g_slist_append(sipe_private->sessions_to_accept, ctx);
970 ctx->focus_uri = g_strdup(focus_uri);
971 ctx->msg = msg ? sipmsg_copy(msg) : NULL;
972 ctx->ask_ctx = sipe_user_ask(sipe_private, ask_msg,
973 _("Accept"), accept_cb,
974 _("Decline"), decline_cb,
975 ctx);
977 g_free(ask_msg);
980 void
981 process_incoming_invite_conf(struct sipe_core_private *sipe_private,
982 struct sipmsg *msg)
984 sipe_xml *xn_conferencing = sipe_xml_parse(msg->body, msg->bodylen);
985 const sipe_xml *xn_focus_uri = sipe_xml_child(xn_conferencing, "focus-uri");
986 const sipe_xml *xn_audio = sipe_xml_child(xn_conferencing, "audio");
987 gchar *focus_uri = sipe_xml_data(xn_focus_uri);
988 gboolean audio = sipe_strequal(sipe_xml_attribute(xn_audio, "available"), "true");
990 sipe_xml_free(xn_conferencing);
992 SIPE_DEBUG_INFO("We have received invitation to Conference. Focus URI=%s", focus_uri);
994 if (audio) {
995 sip_transport_response(sipe_private, msg, 180, "Ringing", NULL);
996 ask_accept_voice_conference(sipe_private, focus_uri, msg,
997 (SipeUserAskCb) conf_accept_cb,
998 (SipeUserAskCb) conf_decline_cb);
1000 } else {
1001 accept_incoming_invite_conf(sipe_private, focus_uri, FALSE, msg);
1004 g_free(focus_uri);
1007 #ifdef HAVE_VV
1009 static void
1010 call_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
1012 struct sip_session *session;
1013 session = sipe_session_find_conference(sipe_private, ctx->focus_uri);
1015 sipe_private->sessions_to_accept =
1016 g_slist_remove(sipe_private->sessions_to_accept, ctx);
1018 if (session) {
1019 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
1020 session->chat_session);
1023 conf_accept_ctx_free(ctx);
1026 static void
1027 call_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
1029 sipe_private->sessions_to_accept =
1030 g_slist_remove(sipe_private->sessions_to_accept, ctx);
1032 conf_accept_ctx_free(ctx);
1035 #endif // HAVE_VV
1037 void
1038 sipe_process_conference(struct sipe_core_private *sipe_private,
1039 struct sipmsg *msg)
1041 sipe_xml *xn_conference_info;
1042 const sipe_xml *node;
1043 const sipe_xml *xn_subject;
1044 const gchar *focus_uri;
1045 struct sip_session *session;
1046 gboolean just_joined = FALSE;
1047 #ifdef HAVE_VV
1048 gboolean audio_was_added = FALSE;
1049 #endif
1051 if (msg->response != 0 && msg->response != 200) return;
1053 if (msg->bodylen == 0 || msg->body == NULL || !sipe_strequal(sipmsg_find_header(msg, "Event"), "conference")) return;
1055 xn_conference_info = sipe_xml_parse(msg->body, msg->bodylen);
1056 if (!xn_conference_info) return;
1058 focus_uri = sipe_xml_attribute(xn_conference_info, "entity");
1059 session = sipe_session_find_conference(sipe_private, focus_uri);
1061 if (!session) {
1062 SIPE_DEBUG_INFO("sipe_process_conference: unable to find conf session with focus=%s", focus_uri);
1063 return;
1066 if (!session->chat_session->backend) {
1067 gchar *self = sip_uri_self(sipe_private);
1069 /* create chat */
1070 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1071 session->chat_session,
1072 session->chat_session->title,
1073 self);
1074 just_joined = TRUE;
1075 /* @TODO ask for full state (re-subscribe) if it was a partial one -
1076 * this is to obtain full list of conference participants.
1078 g_free(self);
1081 /* subject */
1082 if ((xn_subject = sipe_xml_child(xn_conference_info, "conference-description/subject"))) {
1083 g_free(session->subject);
1084 session->subject = sipe_xml_data(xn_subject);
1085 sipe_backend_chat_topic(session->chat_session->backend, session->subject);
1086 SIPE_DEBUG_INFO("sipe_process_conference: subject=%s", session->subject ? session->subject : "");
1089 /* IM MCU URI */
1090 if (!session->im_mcu_uri) {
1091 for (node = sipe_xml_child(xn_conference_info, "conference-description/conf-uris/entry");
1092 node;
1093 node = sipe_xml_twin(node))
1095 gchar *purpose = sipe_xml_data(sipe_xml_child(node, "purpose"));
1097 if (sipe_strequal("chat", purpose)) {
1098 g_free(purpose);
1099 session->im_mcu_uri = sipe_xml_data(sipe_xml_child(node, "uri"));
1100 SIPE_DEBUG_INFO("sipe_process_conference: im_mcu_uri=%s", session->im_mcu_uri);
1101 break;
1103 g_free(purpose);
1107 /* users */
1108 for (node = sipe_xml_child(xn_conference_info, "users/user"); node; node = sipe_xml_twin(node)) {
1109 const gchar *user_uri = sipe_xml_attribute(node, "entity");
1110 const gchar *state = sipe_xml_attribute(node, "state");
1111 gchar *role = sipe_xml_data(sipe_xml_child(node, "roles/entry"));
1112 gboolean is_operator = sipe_strequal(role, "presenter");
1113 gboolean is_in_im_mcu = FALSE;
1114 gchar *self = sip_uri_self(sipe_private);
1116 if (sipe_strequal("deleted", state)) {
1117 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1118 sipe_backend_chat_remove(session->chat_session->backend,
1119 user_uri);
1121 } else {
1122 /* endpoints */
1123 const sipe_xml *endpoint;
1124 for (endpoint = sipe_xml_child(node, "endpoint"); endpoint; endpoint = sipe_xml_twin(endpoint)) {
1125 const gchar *session_type;
1126 gchar *status = sipe_xml_data(sipe_xml_child(endpoint, "status"));
1127 gboolean connected = sipe_strequal("connected", status);
1128 g_free(status);
1130 if (!connected)
1131 continue;
1133 session_type = sipe_xml_attribute(endpoint, "session-type");
1135 if (sipe_strequal("chat", session_type)) {
1136 is_in_im_mcu = TRUE;
1137 if (!sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1138 sipe_backend_chat_add(session->chat_session->backend,
1139 user_uri,
1140 !just_joined && g_ascii_strcasecmp(user_uri, self));
1142 if (is_operator) {
1143 sipe_backend_chat_operator(session->chat_session->backend,
1144 user_uri);
1146 } else if (sipe_strequal("audio-video", session_type)) {
1147 #ifdef HAVE_VV
1148 if (!session->is_call)
1149 audio_was_added = TRUE;
1150 #endif
1153 if (!is_in_im_mcu) {
1154 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1155 sipe_backend_chat_remove(session->chat_session->backend,
1156 user_uri);
1160 g_free(role);
1161 g_free(self);
1164 #ifdef HAVE_VV
1165 if (audio_was_added) {
1166 session->is_call = TRUE;
1167 ask_accept_voice_conference(sipe_private, focus_uri, NULL,
1168 (SipeUserAskCb) call_accept_cb,
1169 (SipeUserAskCb) call_decline_cb);
1171 #endif
1173 /* entity-view, locked */
1174 for (node = sipe_xml_child(xn_conference_info, "conference-view/entity-view");
1175 node;
1176 node = sipe_xml_twin(node)) {
1178 const sipe_xml *xn_type = sipe_xml_child(node, "entity-state/media/entry/type");
1179 gchar *tmp = NULL;
1180 if (xn_type && sipe_strequal("chat", (tmp = sipe_xml_data(xn_type)))) {
1181 const sipe_xml *xn_locked = sipe_xml_child(node, "entity-state/locked");
1182 if (xn_locked) {
1183 gchar *locked = sipe_xml_data(xn_locked);
1184 gboolean prev_locked = session->locked;
1185 session->locked = sipe_strequal(locked, "true");
1186 if (prev_locked && !session->locked) {
1187 sipe_user_present_info(sipe_private, session,
1188 _("This conference is no longer locked. Additional participants can now join."));
1190 if (!prev_locked && session->locked) {
1191 sipe_user_present_info(sipe_private, session,
1192 _("This conference is locked. Nobody else can join the conference while it is locked."));
1195 SIPE_DEBUG_INFO("sipe_process_conference: session->locked=%s",
1196 session->locked ? "TRUE" : "FALSE");
1197 g_free(locked);
1200 g_free(tmp);
1202 sipe_xml_free(xn_conference_info);
1204 if (session->im_mcu_uri) {
1205 struct sip_dialog *dialog = sipe_dialog_find(session, session->im_mcu_uri);
1206 if (!dialog) {
1207 dialog = sipe_dialog_add(session);
1209 dialog->callid = g_strdup(session->callid);
1210 dialog->with = g_strdup(session->im_mcu_uri);
1212 /* send INVITE to IM MCU */
1213 sipe_im_invite(sipe_private, session, dialog->with, NULL, NULL, NULL, FALSE);
1217 sipe_process_pending_invite_queue(sipe_private, session);
1220 void
1221 sipe_conf_immcu_closed(struct sipe_core_private *sipe_private,
1222 struct sip_session *session)
1224 sipe_user_present_info(sipe_private, session,
1225 _("You have been disconnected from this conference."));
1226 sipe_backend_chat_close(session->chat_session->backend);
1229 void
1230 conf_session_close(struct sipe_core_private *sipe_private,
1231 struct sip_session *session)
1233 if (session) {
1234 /* unsubscribe from focus */
1235 sipe_subscribe_conference(sipe_private,
1236 session->chat_session->id, TRUE);
1238 if (session->focus_dialog) {
1239 /* send BYE to focus */
1240 sip_transport_bye(sipe_private, session->focus_dialog);
1245 void
1246 sipe_process_imdn(struct sipe_core_private *sipe_private,
1247 struct sipmsg *msg)
1249 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1250 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1251 static struct sip_session *session;
1252 sipe_xml *xn_imdn;
1253 const sipe_xml *node;
1254 gchar *message_id;
1255 gchar *message;
1257 session = sipe_session_find_chat_or_im(sipe_private, callid, with);
1258 if (!session) {
1259 SIPE_DEBUG_INFO("sipe_process_imdn: unable to find conf session with callid=%s", callid);
1260 g_free(with);
1261 return;
1264 xn_imdn = sipe_xml_parse(msg->body, msg->bodylen);
1265 message_id = sipe_xml_data(sipe_xml_child(xn_imdn, "message-id"));
1267 message = g_hash_table_lookup(session->conf_unconfirmed_messages, message_id);
1269 /* recipient */
1270 for (node = sipe_xml_child(xn_imdn, "recipient"); node; node = sipe_xml_twin(node)) {
1271 gchar *tmp = parse_from(sipe_xml_attribute(node, "uri"));
1272 gchar *uri = parse_from(tmp);
1273 gchar *status = sipe_xml_data(sipe_xml_child(node, "status"));
1274 guint error = status ? g_ascii_strtoull(status, NULL, 10) : 0;
1275 /* default to error if missing or conversion failed */
1276 if ((error == 0) || (error >= 300))
1277 sipe_user_present_message_undelivered(sipe_private,
1278 session,
1279 error,
1281 uri,
1282 message);
1283 g_free(status);
1284 g_free(tmp);
1285 g_free(uri);
1288 sipe_xml_free(xn_imdn);
1290 g_hash_table_remove(session->conf_unconfirmed_messages, message_id);
1291 SIPE_DEBUG_INFO("sipe_process_imdn: removed message %s from conf_unconfirmed_messages(count=%d)",
1292 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
1293 g_free(message_id);
1294 g_free(with);
1297 void sipe_core_conf_make_leader(struct sipe_core_public *sipe_public,
1298 gpointer parameter,
1299 const gchar *buddy_name)
1301 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1302 struct sipe_chat_session *chat_session = parameter;
1303 struct sip_session *session;
1305 SIPE_DEBUG_INFO("sipe_core_conf_make_leader: chat_title=%s",
1306 chat_session->title);
1308 session = sipe_session_find_chat(sipe_private, chat_session);
1309 sipe_conf_modify_user_role(sipe_private, session, buddy_name);
1312 void sipe_core_conf_remove_from(struct sipe_core_public *sipe_public,
1313 gpointer parameter,
1314 const gchar *buddy_name)
1316 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1317 struct sipe_chat_session *chat_session = parameter;
1318 struct sip_session *session;
1320 SIPE_DEBUG_INFO("sipe_core_conf_remove_from: chat_title=%s",
1321 chat_session->title);
1323 session = sipe_session_find_chat(sipe_private, chat_session);
1324 sipe_conf_delete_user(sipe_private, session, buddy_name);
1328 Local Variables:
1329 mode: c
1330 c-file-style: "bsd"
1331 indent-tabs-mode: t
1332 tab-width: 8
1333 End: