core: update sipe_core_conf_create() API
[siplcs.git] / src / core / sipe-conf.c
blobcbf2de557a10af1156ce16002c2b47c4c6d6b36b
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-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 void sipe_core_conf_create(struct sipe_core_public *sipe_public,
338 const gchar *uri)
340 gchar *uri_ue = sipe_utils_uri_unescape(uri);
341 gchar *focus_uri;
343 SIPE_DEBUG_INFO("sipe_core_conf_create: URI '%s' unescaped '%s'",
344 uri ? uri : "<UNDEFINED>",
345 uri_ue ? uri_ue : "<UNDEFINED>");
347 focus_uri = parse_ocs_focus_uri(uri_ue);
348 if (!focus_uri) {
349 focus_uri = parse_lync_join_url(uri_ue);
352 if (focus_uri) {
353 sipe_conf_create(SIPE_CORE_PRIVATE, NULL, focus_uri);
354 g_free(focus_uri);
355 } else {
356 gchar *error = g_strdup_printf(_("\"%s\" is not a valid conference URI"),
357 uri ? uri : "");
358 sipe_backend_notify_error(sipe_public,
359 _("Failed to join the conference"),
360 error);
361 g_free(error);
364 g_free(uri_ue);
367 /** Create new session with Focus URI */
368 struct sip_session *
369 sipe_conf_create(struct sipe_core_private *sipe_private,
370 struct sipe_chat_session *chat_session,
371 const gchar *focus_uri)
373 /* addUser request to the focus.
375 * focus_URI, from, endpoint_GUID
377 static const gchar CCCP_ADD_USER[] =
378 "<addUser>"
379 "<conferenceKeys confEntity=\"%s\"/>"
380 "<ci:user xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"%s\">"
381 "<ci:roles>"
382 "<ci:entry>attendee</ci:entry>"
383 "</ci:roles>"
384 "<ci:endpoint entity=\"{%s}\" "
385 "xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\"/>"
386 "</ci:user>"
387 "</addUser>";
389 gchar *self;
390 struct sip_session *session = sipe_session_add_chat(sipe_private,
391 chat_session,
392 FALSE,
393 focus_uri);
395 session->focus_dialog = g_new0(struct sip_dialog, 1);
396 session->focus_dialog->callid = gencallid();
397 session->focus_dialog->with = g_strdup(session->chat_session->id);
398 session->focus_dialog->endpoint_GUID = rand_guid();
399 session->focus_dialog->ourtag = gentag();
401 self = sip_uri_self(sipe_private);
402 session->focus_dialog->outgoing_invite =
403 cccp_request(sipe_private, "INVITE",
404 session->focus_dialog->with, session->focus_dialog,
405 process_invite_conf_focus_response,
406 CCCP_ADD_USER,
407 session->focus_dialog->with, self,
408 session->focus_dialog->endpoint_GUID);
410 /* Rejoin existing session? */
411 if (chat_session) {
412 SIPE_DEBUG_INFO("sipe_conf_create: rejoin '%s' (%s)",
413 chat_session->title,
414 chat_session->id);
415 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
416 chat_session->backend,
417 self,
418 chat_session->title);
420 g_free(self);
422 return(session);
425 /** Modify User Role */
426 void
427 sipe_conf_modify_user_role(struct sipe_core_private *sipe_private,
428 struct sip_session *session,
429 const gchar* who)
431 /* modifyUserRoles request to the focus. Makes user a leader.
433 * focus_uri (%s)
434 * who (%s)
436 static const gchar CCCP_MODIFY_USER_ROLES[] =
437 "<modifyUserRoles>"
438 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"
439 "<user-roles xmlns=\"urn:ietf:params:xml:ns:conference-info\">"
440 "<entry>presenter</entry>"
441 "</user-roles>"
442 "</modifyUserRoles>";
444 if (!session->focus_dialog || !session->focus_dialog->is_established) {
445 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_user_role: no dialog with focus, exiting.");
446 return;
449 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
450 session->focus_dialog, NULL,
451 CCCP_MODIFY_USER_ROLES,
452 session->focus_dialog->with, who);
456 * Check conference lock status
458 sipe_chat_lock_status sipe_core_chat_lock_status(struct sipe_core_public *sipe_public,
459 struct sipe_chat_session *chat_session)
461 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
462 sipe_chat_lock_status status = SIPE_CHAT_LOCK_STATUS_NOT_ALLOWED;
464 if (chat_session &&
465 (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE)) {
466 struct sip_session *session = sipe_session_find_chat(sipe_private,
467 chat_session);
468 if (session) {
469 gchar *self = sip_uri_self(sipe_private);
471 /* Only operators are allowed to change the lock status */
472 if (sipe_backend_chat_is_operator(chat_session->backend, self)) {
473 status = session->locked ?
474 SIPE_CHAT_LOCK_STATUS_LOCKED :
475 SIPE_CHAT_LOCK_STATUS_UNLOCKED;
478 g_free(self);
482 return(status);
486 * Modify Conference Lock
487 * Sends request to Focus.
488 * INFO method is a carrier of application/cccp+xml
490 void
491 sipe_core_chat_modify_lock(struct sipe_core_public *sipe_public,
492 struct sipe_chat_session *chat_session,
493 const gboolean locked)
495 /* modifyConferenceLock request to the focus. Locks/unlocks conference.
497 * focus_uri (%s)
498 * locked (%s) "true" or "false" values applicable
500 static const gchar CCCP_MODIFY_CONFERENCE_LOCK[] =
501 "<modifyConferenceLock>"
502 "<conferenceKeys confEntity=\"%s\"/>"
503 "<locked>%s</locked>"
504 "</modifyConferenceLock>";
506 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
508 struct sip_session *session = sipe_session_find_chat(sipe_private,
509 chat_session);
511 if (!session) return;
512 if (!session->focus_dialog || !session->focus_dialog->is_established) {
513 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_conference_lock: no dialog with focus, exiting.");
514 return;
517 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
518 session->focus_dialog, NULL,
519 CCCP_MODIFY_CONFERENCE_LOCK,
520 session->focus_dialog->with,
521 locked ? "true" : "false");
524 /** Modify Delete User */
525 void
526 sipe_conf_delete_user(struct sipe_core_private *sipe_private,
527 struct sip_session *session,
528 const gchar* who)
530 /* deleteUser request to the focus. Removes a user from the conference.
532 * focus_uri (%s)
533 * who (%s)
535 static const gchar CCCP_DELETE_USER[] =
536 "<deleteUser>"
537 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"
538 "</deleteUser>";
540 if (!session->focus_dialog || !session->focus_dialog->is_established) {
541 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_delete_user: no dialog with focus, exiting.");
542 return;
545 cccp_request(sipe_private, "INFO", session->focus_dialog->with,
546 session->focus_dialog, NULL,
547 CCCP_DELETE_USER,
548 session->focus_dialog->with, who);
551 /** Invite counterparty to join conference callback */
552 static gboolean
553 process_invite_conf_response(struct sipe_core_private *sipe_private,
554 struct sipmsg *msg,
555 SIPE_UNUSED_PARAMETER struct transaction *trans)
557 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
559 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
560 dialog->cseq = sipmsg_parse_cseq(msg);
561 dialog->with = parse_from(sipmsg_find_header(msg, "To"));
562 sipe_dialog_parse(dialog, msg, TRUE);
564 if (msg->response >= 200) {
565 /* send ACK to counterparty */
566 dialog->cseq--;
567 sip_transport_ack(sipe_private, dialog);
568 dialog->outgoing_invite = NULL;
569 dialog->is_established = TRUE;
572 if (msg->response >= 400) {
573 SIPE_DEBUG_INFO("process_invite_conf_response: INVITE response is not 200. Failed to invite %s.", dialog->with);
574 /* @TODO notify user of failure to invite counterparty */
575 sipe_dialog_free(dialog);
576 return FALSE;
579 if (msg->response >= 200) {
580 struct sip_session *session = sipe_session_find_im(sipe_private, dialog->with);
581 struct sip_dialog *im_dialog = sipe_dialog_find(session, dialog->with);
583 /* close IM session to counterparty */
584 if (im_dialog) {
585 sip_transport_bye(sipe_private, im_dialog);
586 sipe_dialog_remove(session, dialog->with);
590 sipe_dialog_free(dialog);
591 return TRUE;
595 * Invites counterparty to join conference.
597 void
598 sipe_invite_conf(struct sipe_core_private *sipe_private,
599 struct sip_session *session,
600 const gchar* who)
602 gchar *hdr;
603 gchar *contact;
604 gchar *body;
605 struct sip_dialog *dialog = NULL;
607 /* It will be short lived special dialog.
608 * Will not be stored in session.
610 dialog = g_new0(struct sip_dialog, 1);
611 dialog->callid = gencallid();
612 dialog->with = g_strdup(who);
613 dialog->ourtag = gentag();
615 contact = get_contact(sipe_private);
616 hdr = g_strdup_printf(
617 "Supported: ms-sender\r\n"
618 "Contact: %s\r\n"
619 "Content-Type: application/ms-conf-invite+xml\r\n",
620 contact);
621 g_free(contact);
623 body = g_strdup_printf(
624 SIPE_SEND_CONF_INVITE,
625 session->chat_session->id,
626 session->subject ? session->subject : ""
629 sip_transport_invite(sipe_private,
630 hdr,
631 body,
632 dialog,
633 process_invite_conf_response);
635 sipe_dialog_free(dialog);
636 g_free(body);
637 g_free(hdr);
640 /** Create conference callback */
641 static gboolean
642 process_conf_add_response(struct sipe_core_private *sipe_private,
643 struct sipmsg *msg,
644 struct transaction *trans)
646 if (msg->response >= 400) {
647 SIPE_DEBUG_INFO_NOFORMAT("process_conf_add_response: SERVICE response is not 200. Failed to create conference.");
648 /* @TODO notify user of failure to create conference */
649 return FALSE;
651 if (msg->response == 200) {
652 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
653 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code")))
655 gchar *who = trans->payload->data;
656 const sipe_xml *xn_conference_info = sipe_xml_child(xn_response, "addConference/conference-info");
657 struct sip_session *session = sipe_conf_create(sipe_private,
658 NULL,
659 sipe_xml_attribute(xn_conference_info,
660 "entity"));
662 SIPE_DEBUG_INFO("process_conf_add_response: session->focus_uri=%s",
663 session->chat_session->id);
665 session->pending_invite_queue = sipe_utils_slist_insert_unique_sorted(session->pending_invite_queue,
666 g_strdup(who),
667 (GCompareFunc)strcmp,
668 g_free);
670 sipe_xml_free(xn_response);
673 return TRUE;
677 * Creates conference.
679 void
680 sipe_conf_add(struct sipe_core_private *sipe_private,
681 const gchar* who)
683 gchar *conference_id;
684 struct transaction *trans;
685 time_t expiry = time(NULL) + 7*60*60; /* 7 hours */
686 char *expiry_time;
687 struct transaction_payload *payload;
689 /* addConference request to the focus factory.
691 * conference_id (%s) Ex.: 8386E6AEAAA41E4AA6627BA76D43B6D1
692 * expiry_time (%s) Ex.: 2009-07-13T17:57:09Z
693 * conference_view (%s) Ex.: <msci:entity-view entity="chat"/>
695 static const gchar CCCP_ADD_CONFERENCE[] =
696 "<addConference>"
697 "<ci:conference-info xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" "
698 "entity=\"\" "
699 "xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\">"
700 "<ci:conference-description>"
701 "<ci:subject/>"
702 "<msci:conference-id>%s</msci:conference-id>"
703 "<msci:expiry-time>%s</msci:expiry-time>"
704 "<msci:admission-policy>openAuthenticated</msci:admission-policy>"
705 "</ci:conference-description>"
706 "<msci:conference-view>%s</msci:conference-view>"
707 "</ci:conference-info>"
708 "</addConference>";
710 static const gchar *DESIRED_MCU_TYPES[] = {
711 "chat",
712 #ifdef HAVE_VV
713 "audio-video",
714 #endif
715 NULL
718 GString *conference_view = g_string_new("");
719 const gchar **type;
721 for (type = DESIRED_MCU_TYPES; *type; ++type ) {
722 if (sipe_conf_supports_mcu_type(sipe_private, *type)) {
723 g_string_append(conference_view, "<msci:entity-view entity=\"");
724 g_string_append(conference_view, *type);
725 g_string_append(conference_view, "\"/>");
729 expiry_time = sipe_utils_time_to_str(expiry);
730 conference_id = genconfid();
731 trans = cccp_request(sipe_private, "SERVICE", sipe_private->focus_factory_uri,
732 NULL, process_conf_add_response,
733 CCCP_ADD_CONFERENCE,
734 conference_id, expiry_time, conference_view->str);
735 g_free(conference_id);
736 g_free(expiry_time);
737 g_string_free(conference_view, TRUE);
739 payload = g_new0(struct transaction_payload, 1);
740 payload->destroy = g_free;
741 payload->data = g_strdup(who);
742 trans->payload = payload;
745 static void
746 accept_incoming_invite_conf(struct sipe_core_private *sipe_private,
747 gchar *focus_uri,
748 gboolean audio,
749 struct sipmsg *msg)
751 struct sip_session *session;
752 gchar *newTag = gentag();
753 const gchar *oldHeader = sipmsg_find_header(msg, "To");
754 gchar *newHeader;
756 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
757 g_free(newTag);
758 sipmsg_remove_header_now(msg, "To");
759 sipmsg_add_header_now(msg, "To", newHeader);
760 g_free(newHeader);
762 /* acknowledge invite */
763 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
765 /* add self to conf */
766 session = sipe_conf_create(sipe_private, NULL, focus_uri);
767 session->is_call = audio;
770 struct conf_accept_ctx {
771 gchar *focus_uri;
772 struct sipmsg *msg;
773 struct sipe_user_ask_ctx *ask_ctx;
776 static void
777 conf_accept_ctx_free(struct conf_accept_ctx *ctx)
779 g_return_if_fail(ctx != NULL);
781 sipmsg_free(ctx->msg);
782 g_free(ctx->focus_uri);
783 g_free(ctx);
786 static void
787 conf_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
789 sipe_private->sessions_to_accept =
790 g_slist_remove(sipe_private->sessions_to_accept, ctx);
792 accept_incoming_invite_conf(sipe_private, ctx->focus_uri, TRUE, ctx->msg);
793 conf_accept_ctx_free(ctx);
796 static void
797 conf_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
799 sipe_private->sessions_to_accept =
800 g_slist_remove(sipe_private->sessions_to_accept, ctx);
802 sip_transport_response(sipe_private,
803 ctx->msg,
804 603, "Decline", NULL);
806 conf_accept_ctx_free(ctx);
809 void
810 sipe_conf_cancel_unaccepted(struct sipe_core_private *sipe_private,
811 struct sipmsg *msg)
813 const gchar *callid1 = msg ? sipmsg_find_header(msg, "Call-ID") : NULL;
814 GSList *it = sipe_private->sessions_to_accept;
815 while (it) {
816 struct conf_accept_ctx *ctx = it->data;
817 const gchar *callid2 = NULL;
819 if (msg && ctx->msg)
820 callid2 = sipmsg_find_header(ctx->msg, "Call-ID");
822 if (sipe_strequal(callid1, callid2)) {
823 GSList *tmp;
825 if (ctx->msg)
826 sip_transport_response(sipe_private, ctx->msg,
827 487, "Request Terminated", NULL);
829 if (msg)
830 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
832 sipe_user_close_ask(ctx->ask_ctx);
833 conf_accept_ctx_free(ctx);
835 tmp = it;
836 it = it->next;
838 sipe_private->sessions_to_accept =
839 g_slist_delete_link(sipe_private->sessions_to_accept, tmp);
841 if (callid1)
842 break;
843 } else
844 it = it->next;
848 static void
849 ask_accept_voice_conference(struct sipe_core_private *sipe_private,
850 const gchar *focus_uri,
851 struct sipmsg *msg,
852 SipeUserAskCb accept_cb,
853 SipeUserAskCb decline_cb)
855 gchar **parts;
856 gchar *alias;
857 gchar *ask_msg;
858 const gchar *novv_note;
859 struct conf_accept_ctx *ctx;
861 #ifdef HAVE_VV
862 novv_note = "";
863 #else
864 novv_note = _("\n\nAs this client was not compiled with voice call "
865 "support, if you accept, you will be able to contact "
866 "the other participants only via IM session.");
867 #endif
869 parts = g_strsplit(focus_uri, ";", 2);
870 alias = sipe_buddy_get_alias(sipe_private, parts[0]);
872 ask_msg = g_strdup_printf(_("%s wants to invite you "
873 "to the conference call%s"),
874 alias ? alias : parts[0], novv_note);
876 g_free(alias);
877 g_strfreev(parts);
879 ctx = g_new0(struct conf_accept_ctx, 1);
880 sipe_private->sessions_to_accept =
881 g_slist_append(sipe_private->sessions_to_accept, ctx);
883 ctx->focus_uri = g_strdup(focus_uri);
884 ctx->msg = msg ? sipmsg_copy(msg) : NULL;
885 ctx->ask_ctx = sipe_user_ask(sipe_private, ask_msg,
886 _("Accept"), accept_cb,
887 _("Decline"), decline_cb,
888 ctx);
890 g_free(ask_msg);
893 void
894 process_incoming_invite_conf(struct sipe_core_private *sipe_private,
895 struct sipmsg *msg)
897 sipe_xml *xn_conferencing = sipe_xml_parse(msg->body, msg->bodylen);
898 const sipe_xml *xn_focus_uri = sipe_xml_child(xn_conferencing, "focus-uri");
899 const sipe_xml *xn_audio = sipe_xml_child(xn_conferencing, "audio");
900 gchar *focus_uri = sipe_xml_data(xn_focus_uri);
901 gboolean audio = sipe_strequal(sipe_xml_attribute(xn_audio, "available"), "true");
903 sipe_xml_free(xn_conferencing);
905 SIPE_DEBUG_INFO("We have received invitation to Conference. Focus URI=%s", focus_uri);
907 if (audio) {
908 sip_transport_response(sipe_private, msg, 180, "Ringing", NULL);
909 ask_accept_voice_conference(sipe_private, focus_uri, msg,
910 (SipeUserAskCb) conf_accept_cb,
911 (SipeUserAskCb) conf_decline_cb);
913 } else {
914 accept_incoming_invite_conf(sipe_private, focus_uri, FALSE, msg);
917 g_free(focus_uri);
920 #ifdef HAVE_VV
922 static void
923 call_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
925 struct sip_session *session;
926 session = sipe_session_find_conference(sipe_private, ctx->focus_uri);
928 sipe_private->sessions_to_accept =
929 g_slist_remove(sipe_private->sessions_to_accept, ctx);
931 if (session) {
932 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
933 session->chat_session);
936 conf_accept_ctx_free(ctx);
939 static void
940 call_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
942 sipe_private->sessions_to_accept =
943 g_slist_remove(sipe_private->sessions_to_accept, ctx);
945 conf_accept_ctx_free(ctx);
948 #endif // HAVE_VV
950 void
951 sipe_process_conference(struct sipe_core_private *sipe_private,
952 struct sipmsg *msg)
954 sipe_xml *xn_conference_info;
955 const sipe_xml *node;
956 const sipe_xml *xn_subject;
957 const gchar *focus_uri;
958 struct sip_session *session;
959 gboolean just_joined = FALSE;
960 #ifdef HAVE_VV
961 gboolean audio_was_added = FALSE;
962 #endif
964 if (msg->response != 0 && msg->response != 200) return;
966 if (msg->bodylen == 0 || msg->body == NULL || !sipe_strequal(sipmsg_find_header(msg, "Event"), "conference")) return;
968 xn_conference_info = sipe_xml_parse(msg->body, msg->bodylen);
969 if (!xn_conference_info) return;
971 focus_uri = sipe_xml_attribute(xn_conference_info, "entity");
972 session = sipe_session_find_conference(sipe_private, focus_uri);
974 if (!session) {
975 SIPE_DEBUG_INFO("sipe_process_conference: unable to find conf session with focus=%s", focus_uri);
976 return;
979 if (!session->chat_session->backend) {
980 gchar *self = sip_uri_self(sipe_private);
982 /* create chat */
983 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
984 session->chat_session,
985 session->chat_session->title,
986 self);
987 just_joined = TRUE;
988 /* @TODO ask for full state (re-subscribe) if it was a partial one -
989 * this is to obtain full list of conference participants.
991 g_free(self);
994 /* subject */
995 if ((xn_subject = sipe_xml_child(xn_conference_info, "conference-description/subject"))) {
996 g_free(session->subject);
997 session->subject = sipe_xml_data(xn_subject);
998 sipe_backend_chat_topic(session->chat_session->backend, session->subject);
999 SIPE_DEBUG_INFO("sipe_process_conference: subject=%s", session->subject ? session->subject : "");
1002 /* IM MCU URI */
1003 if (!session->im_mcu_uri) {
1004 for (node = sipe_xml_child(xn_conference_info, "conference-description/conf-uris/entry");
1005 node;
1006 node = sipe_xml_twin(node))
1008 gchar *purpose = sipe_xml_data(sipe_xml_child(node, "purpose"));
1010 if (sipe_strequal("chat", purpose)) {
1011 g_free(purpose);
1012 session->im_mcu_uri = sipe_xml_data(sipe_xml_child(node, "uri"));
1013 SIPE_DEBUG_INFO("sipe_process_conference: im_mcu_uri=%s", session->im_mcu_uri);
1014 break;
1016 g_free(purpose);
1020 /* users */
1021 for (node = sipe_xml_child(xn_conference_info, "users/user"); node; node = sipe_xml_twin(node)) {
1022 const gchar *user_uri = sipe_xml_attribute(node, "entity");
1023 const gchar *state = sipe_xml_attribute(node, "state");
1024 gchar *role = sipe_xml_data(sipe_xml_child(node, "roles/entry"));
1025 gboolean is_operator = sipe_strequal(role, "presenter");
1026 gboolean is_in_im_mcu = FALSE;
1027 gchar *self = sip_uri_self(sipe_private);
1029 if (sipe_strequal("deleted", state)) {
1030 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1031 sipe_backend_chat_remove(session->chat_session->backend,
1032 user_uri);
1034 } else {
1035 /* endpoints */
1036 const sipe_xml *endpoint;
1037 for (endpoint = sipe_xml_child(node, "endpoint"); endpoint; endpoint = sipe_xml_twin(endpoint)) {
1038 const gchar *session_type;
1039 gchar *status = sipe_xml_data(sipe_xml_child(endpoint, "status"));
1040 gboolean connected = sipe_strequal("connected", status);
1041 g_free(status);
1043 if (!connected)
1044 continue;
1046 session_type = sipe_xml_attribute(endpoint, "session-type");
1048 if (sipe_strequal("chat", session_type)) {
1049 is_in_im_mcu = TRUE;
1050 if (!sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1051 sipe_backend_chat_add(session->chat_session->backend,
1052 user_uri,
1053 !just_joined && g_ascii_strcasecmp(user_uri, self));
1055 if (is_operator) {
1056 sipe_backend_chat_operator(session->chat_session->backend,
1057 user_uri);
1059 } else if (sipe_strequal("audio-video", session_type)) {
1060 #ifdef HAVE_VV
1061 if (!session->is_call)
1062 audio_was_added = TRUE;
1063 #endif
1066 if (!is_in_im_mcu) {
1067 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1068 sipe_backend_chat_remove(session->chat_session->backend,
1069 user_uri);
1073 g_free(role);
1074 g_free(self);
1077 #ifdef HAVE_VV
1078 if (audio_was_added) {
1079 session->is_call = TRUE;
1080 ask_accept_voice_conference(sipe_private, focus_uri, NULL,
1081 (SipeUserAskCb) call_accept_cb,
1082 (SipeUserAskCb) call_decline_cb);
1084 #endif
1086 /* entity-view, locked */
1087 for (node = sipe_xml_child(xn_conference_info, "conference-view/entity-view");
1088 node;
1089 node = sipe_xml_twin(node)) {
1091 const sipe_xml *xn_type = sipe_xml_child(node, "entity-state/media/entry/type");
1092 gchar *tmp = NULL;
1093 if (xn_type && sipe_strequal("chat", (tmp = sipe_xml_data(xn_type)))) {
1094 const sipe_xml *xn_locked = sipe_xml_child(node, "entity-state/locked");
1095 if (xn_locked) {
1096 gchar *locked = sipe_xml_data(xn_locked);
1097 gboolean prev_locked = session->locked;
1098 session->locked = sipe_strequal(locked, "true");
1099 if (prev_locked && !session->locked) {
1100 sipe_user_present_info(sipe_private, session,
1101 _("This conference is no longer locked. Additional participants can now join."));
1103 if (!prev_locked && session->locked) {
1104 sipe_user_present_info(sipe_private, session,
1105 _("This conference is locked. Nobody else can join the conference while it is locked."));
1108 SIPE_DEBUG_INFO("sipe_process_conference: session->locked=%s",
1109 session->locked ? "TRUE" : "FALSE");
1110 g_free(locked);
1113 g_free(tmp);
1115 sipe_xml_free(xn_conference_info);
1117 if (session->im_mcu_uri) {
1118 struct sip_dialog *dialog = sipe_dialog_find(session, session->im_mcu_uri);
1119 if (!dialog) {
1120 dialog = sipe_dialog_add(session);
1122 dialog->callid = g_strdup(session->callid);
1123 dialog->with = g_strdup(session->im_mcu_uri);
1125 /* send INVITE to IM MCU */
1126 sipe_im_invite(sipe_private, session, dialog->with, NULL, NULL, NULL, FALSE);
1130 sipe_process_pending_invite_queue(sipe_private, session);
1133 void
1134 sipe_conf_immcu_closed(struct sipe_core_private *sipe_private,
1135 struct sip_session *session)
1137 sipe_user_present_info(sipe_private, session,
1138 _("You have been disconnected from this conference."));
1139 sipe_backend_chat_close(session->chat_session->backend);
1142 void
1143 conf_session_close(struct sipe_core_private *sipe_private,
1144 struct sip_session *session)
1146 if (session) {
1147 /* unsubscribe from focus */
1148 sipe_subscribe_conference(sipe_private,
1149 session->chat_session->id, TRUE);
1151 if (session->focus_dialog) {
1152 /* send BYE to focus */
1153 sip_transport_bye(sipe_private, session->focus_dialog);
1158 void
1159 sipe_process_imdn(struct sipe_core_private *sipe_private,
1160 struct sipmsg *msg)
1162 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1163 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1164 static struct sip_session *session;
1165 sipe_xml *xn_imdn;
1166 const sipe_xml *node;
1167 gchar *message_id;
1168 gchar *message;
1170 session = sipe_session_find_chat_or_im(sipe_private, callid, with);
1171 if (!session) {
1172 SIPE_DEBUG_INFO("sipe_process_imdn: unable to find conf session with callid=%s", callid);
1173 g_free(with);
1174 return;
1177 xn_imdn = sipe_xml_parse(msg->body, msg->bodylen);
1178 message_id = sipe_xml_data(sipe_xml_child(xn_imdn, "message-id"));
1180 message = g_hash_table_lookup(session->conf_unconfirmed_messages, message_id);
1182 /* recipient */
1183 for (node = sipe_xml_child(xn_imdn, "recipient"); node; node = sipe_xml_twin(node)) {
1184 gchar *tmp = parse_from(sipe_xml_attribute(node, "uri"));
1185 gchar *uri = parse_from(tmp);
1186 gchar *status = sipe_xml_data(sipe_xml_child(node, "status"));
1187 guint error = status ? g_ascii_strtoull(status, NULL, 10) : 0;
1188 /* default to error if missing or conversion failed */
1189 if ((error == 0) || (error >= 300))
1190 sipe_user_present_message_undelivered(sipe_private,
1191 session,
1192 error,
1194 uri,
1195 message);
1196 g_free(status);
1197 g_free(tmp);
1198 g_free(uri);
1201 sipe_xml_free(xn_imdn);
1203 g_hash_table_remove(session->conf_unconfirmed_messages, message_id);
1204 SIPE_DEBUG_INFO("sipe_process_imdn: removed message %s from conf_unconfirmed_messages(count=%d)",
1205 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
1206 g_free(message_id);
1207 g_free(with);
1210 void sipe_core_conf_make_leader(struct sipe_core_public *sipe_public,
1211 gpointer parameter,
1212 const gchar *buddy_name)
1214 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1215 struct sipe_chat_session *chat_session = parameter;
1216 struct sip_session *session;
1218 SIPE_DEBUG_INFO("sipe_core_conf_make_leader: chat_title=%s",
1219 chat_session->title);
1221 session = sipe_session_find_chat(sipe_private, chat_session);
1222 sipe_conf_modify_user_role(sipe_private, session, buddy_name);
1225 void sipe_core_conf_remove_from(struct sipe_core_public *sipe_public,
1226 gpointer parameter,
1227 const gchar *buddy_name)
1229 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1230 struct sipe_chat_session *chat_session = parameter;
1231 struct sip_session *session;
1233 SIPE_DEBUG_INFO("sipe_core_conf_remove_from: chat_title=%s",
1234 chat_session->title);
1236 session = sipe_session_find_chat(sipe_private, chat_session);
1237 sipe_conf_delete_user(sipe_private, session, buddy_name);
1241 Local Variables:
1242 mode: c
1243 c-file-style: "bsd"
1244 indent-tabs-mode: t
1245 tab-width: 8
1246 End: