security: fix target name memory leak in Kerberos
[siplcs.git] / src / core / sipe-conf.c
blobb64d8e6acfc9e4e8e50d6cf61e0006aed4107a16
1 /**
2 * @file sipe-conf.c
4 * pidgin-sipe
6 * Copyright (C) 2010-12 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 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
33 #include <glib.h>
35 #include "sipe-common.h"
36 #include "sipmsg.h"
37 #include "sip-transport.h"
38 #include "sipe-backend.h"
39 #include "sipe-buddy.h"
40 #include "sipe-chat.h"
41 #include "sipe-conf.h"
42 #include "sipe-core.h"
43 #include "sipe-core-private.h"
44 #include "sipe-dialog.h"
45 #include "sipe-im.h"
46 #include "sipe-nls.h"
47 #include "sipe-session.h"
48 #include "sipe-subscriptions.h"
49 #include "sipe-user.h"
50 #include "sipe-utils.h"
51 #include "sipe-xml.h"
53 #ifdef HAVE_VV
54 #define ENTITY_VIEW_AUDIO_VIDEO "<msci:entity-view entity=\"audio-video\"/>"
55 #else
56 #define ENTITY_VIEW_AUDIO_VIDEO
57 #endif
59 /**
60 * Add Conference request to FocusFactory.
61 * @param focus_factory_uri (%s) Ex.: sip:bob7@boston.local;gruu;opaque=app:conf:focusfactory
62 * @param from (%s) Ex.: sip:bob7@boston.local
63 * @param request_id (%d) Ex.: 1094520
64 * @param conference_id (%s) Ex.: 8386E6AEAAA41E4AA6627BA76D43B6D1
65 * @param expiry_time (%s) Ex.: 2009-07-13T17:57:09Z , Default duration: 7 hours
67 #define SIPE_SEND_CONF_ADD \
68 "<?xml version=\"1.0\"?>"\
69 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" "\
70 "xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
71 "C3PVersion=\"1\" "\
72 "to=\"%s\" "\
73 "from=\"%s\" "\
74 "requestId=\"%d\">"\
75 "<addConference>"\
76 "<ci:conference-info xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\">"\
77 "<ci:conference-description>"\
78 "<ci:subject/>"\
79 "<msci:conference-id>%s</msci:conference-id>"\
80 "<msci:expiry-time>%s</msci:expiry-time>"\
81 "<msci:admission-policy>openAuthenticated</msci:admission-policy>"\
82 "</ci:conference-description>"\
83 "<msci:conference-view>"\
84 "<msci:entity-view entity=\"chat\"/>"\
85 ENTITY_VIEW_AUDIO_VIDEO \
86 "</msci:conference-view>"\
87 "</ci:conference-info>"\
88 "</addConference>"\
89 "</request>"
91 /**
92 * AddUser request to Focus.
93 * Params:
94 * focus_URI, from, request_id, focus_URI, from, endpoint_GUID
96 #define SIPE_SEND_CONF_ADD_USER \
97 "<?xml version=\"1.0\"?>"\
98 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
99 "C3PVersion=\"1\" "\
100 "to=\"%s\" "\
101 "from=\"%s\" "\
102 "requestId=\"%d\">"\
103 "<addUser>"\
104 "<conferenceKeys confEntity=\"%s\"/>"\
105 "<ci:user xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"%s\">"\
106 "<ci:roles>"\
107 "<ci:entry>attendee</ci:entry>"\
108 "</ci:roles>"\
109 "<ci:endpoint entity=\"{%s}\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\"/>"\
110 "</ci:user>"\
111 "</addUser>"\
112 "</request>"
115 * ModifyUserRoles request to Focus. Makes user a leader.
116 * @param focus_uri (%s)
117 * @param from (%s)
118 * @param request_id (%d)
119 * @param focus_uri (%s)
120 * @param who (%s)
122 #define SIPE_SEND_CONF_MODIFY_USER_ROLES \
123 "<?xml version=\"1.0\"?>"\
124 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
125 "C3PVersion=\"1\" "\
126 "to=\"%s\" "\
127 "from=\"%s\" "\
128 "requestId=\"%d\">"\
129 "<modifyUserRoles>"\
130 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
131 "<user-roles xmlns=\"urn:ietf:params:xml:ns:conference-info\">"\
132 "<entry>presenter</entry>"\
133 "</user-roles>"\
134 "</modifyUserRoles>"\
135 "</request>"
138 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
139 * @param focus_uri (%s)
140 * @param from (%s)
141 * @param request_id (%d)
142 * @param focus_uri (%s)
143 * @param locked (%s) "true" or "false" values applicable
145 #define SIPE_SEND_CONF_MODIFY_CONF_LOCK \
146 "<?xml version=\"1.0\"?>"\
147 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
148 "C3PVersion=\"1\" "\
149 "to=\"%s\" "\
150 "from=\"%s\" "\
151 "requestId=\"%d\">"\
152 "<modifyConferenceLock>"\
153 "<conferenceKeys confEntity=\"%s\"/>"\
154 "<locked>%s</locked>"\
155 "</modifyConferenceLock>"\
156 "</request>"
159 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
160 * @param focus_uri (%s)
161 * @param from (%s)
162 * @param request_id (%d)
163 * @param focus_uri (%s)
164 * @param who (%s)
166 #define SIPE_SEND_CONF_DELETE_USER \
167 "<?xml version=\"1.0\"?>"\
168 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
169 "C3PVersion=\"1\" "\
170 "to=\"%s\" "\
171 "from=\"%s\" "\
172 "requestId=\"%d\">"\
173 "<deleteUser>"\
174 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
175 "</deleteUser>"\
176 "</request>"
179 * Invite counterparty to join conference.
180 * @param focus_uri (%s)
181 * @param subject (%s) of conference
183 #define SIPE_SEND_CONF_INVITE \
184 "<Conferencing version=\"2.0\">"\
185 "<focus-uri>%s</focus-uri>"\
186 "<subject>%s</subject>"\
187 "<im available=\"true\">"\
188 "<first-im/>"\
189 "</im>"\
190 "</Conferencing>"
193 * Generates random GUID.
194 * This method is borrowed from pidgin's msnutils.c
196 static char *
197 rand_guid()
199 return g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
200 rand() % 0xAAFF + 0x1111,
201 rand() % 0xAAFF + 0x1111,
202 rand() % 0xAAFF + 0x1111,
203 rand() % 0xAAFF + 0x1111,
204 rand() % 0xAAFF + 0x1111,
205 rand() % 0xAAFF + 0x1111,
206 rand() % 0xAAFF + 0x1111,
207 rand() % 0xAAFF + 0x1111);
211 * @param expires not respected if set to negative value (E.g. -1)
213 static void
214 sipe_subscribe_conference(struct sipe_core_private *sipe_private,
215 struct sip_session *session,
216 gboolean expires)
218 sipe_subscribe(sipe_private,
219 session->chat_session->id,
220 "conference",
221 "application/conference-info+xml",
222 expires ? "Expires: 0\r\n" : NULL,
223 NULL,
224 NULL);
227 /** Invite us to the focus callback */
228 static gboolean
229 process_invite_conf_focus_response(struct sipe_core_private *sipe_private,
230 struct sipmsg *msg,
231 SIPE_UNUSED_PARAMETER struct transaction *trans)
233 struct sip_session *session = NULL;
234 char *focus_uri = parse_from(sipmsg_find_header(msg, "To"));
236 session = sipe_session_find_conference(sipe_private, focus_uri);
238 if (!session) {
239 SIPE_DEBUG_INFO("process_invite_conf_focus_response: unable to find conf session with focus=%s", focus_uri);
240 g_free(focus_uri);
241 return FALSE;
244 if (!session->focus_dialog) {
245 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: session's focus_dialog is NULL");
246 g_free(focus_uri);
247 return FALSE;
250 sipe_dialog_parse(session->focus_dialog, msg, TRUE);
252 if (msg->response >= 200) {
253 /* send ACK to focus */
254 session->focus_dialog->cseq = 0;
255 sip_transport_ack(sipe_private, session->focus_dialog);
256 session->focus_dialog->outgoing_invite = NULL;
257 session->focus_dialog->is_established = TRUE;
260 if (msg->response >= 400) {
261 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
263 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: INVITE response is not 200. Failed to join focus.");
264 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
265 _("Failed to join the conference"),
266 reason ? reason : _("no reason given"));
267 g_free(reason);
269 sipe_session_remove(sipe_private, session);
270 g_free(focus_uri);
271 return FALSE;
272 } else if (msg->response == 200) {
273 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
274 const gchar *code = sipe_xml_attribute(xn_response, "code");
275 if (sipe_strequal(code, "success")) {
276 /* subscribe to focus */
277 sipe_subscribe_conference(sipe_private, session, FALSE);
278 #ifdef HAVE_VV
279 if (session->is_call)
280 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
281 session->chat_session);
282 #endif
284 sipe_xml_free(xn_response);
287 g_free(focus_uri);
288 return TRUE;
291 static gchar *
292 parse_ocs_focus_uri(const gchar *uri)
294 const gchar *confkey;
295 size_t uri_len;
297 if (!uri)
298 return NULL;
300 // URI can have this prefix if it was typed in by the user
301 if (g_str_has_prefix(uri, "meet:") || g_str_has_prefix(uri, "conf:")) {
302 uri += 5;
305 uri_len = strlen(uri);
307 if (!uri || !g_str_has_prefix(uri, "sip:") ||
308 uri_len == 4 || g_strstr_len(uri, -1, "%")) {
309 return NULL;
312 confkey = g_strstr_len(uri, -1, "?");
313 if (confkey) {
314 /* TODO: Investigate how conf-key field should be used,
315 * ignoring for now */
316 uri_len = confkey - uri;
319 return g_strndup(uri, uri_len);
322 static gchar *
323 parse_lync_join_url(const gchar *uri)
325 gchar *focus_uri = NULL;
326 gchar **parts;
327 int parts_count = 0;
329 if (!uri)
330 return NULL;
332 if (g_str_has_prefix(uri, "https://")) {
333 uri += 8;
334 } else if (g_str_has_prefix(uri, "http://")) {
335 uri += 7;
338 parts = g_strsplit(uri, "/", 0);
340 for (parts_count = 0; parts[parts_count]; ++parts_count);
341 if (parts_count >= 3) {
342 gchar *base_url = parts[0];
343 gchar *conference_id = parts[parts_count - 1];
344 gchar *organizer_alias = parts[parts_count - 2];
346 gchar **url_parts = g_strsplit(base_url, ".", 0);
347 int url_parts_count = 0;
348 for (url_parts_count = 0; url_parts[url_parts_count]; ++url_parts_count);
350 if (url_parts_count >= 3) {
351 focus_uri = g_strdup_printf("sip:%s@%s.%s;gruu;opaque=app:conf:focus:id:%s",
352 organizer_alias,
353 url_parts[url_parts_count - 2], url_parts[url_parts_count - 1],
354 conference_id);
357 g_strfreev(url_parts);
360 g_strfreev(parts);
362 return focus_uri;
365 struct sip_session *
366 sipe_core_conf_create(struct sipe_core_public *sipe_public,
367 const gchar *uri)
369 gchar *uri_ue = sipe_utils_uri_unescape(uri);
370 gchar *focus_uri;
371 struct sip_session *session = NULL;
373 focus_uri = parse_ocs_focus_uri(uri_ue);
374 if (!focus_uri) {
375 focus_uri = parse_lync_join_url(uri_ue);
378 if (focus_uri) {
379 session = sipe_conf_create(SIPE_CORE_PRIVATE, NULL, focus_uri);
380 g_free(focus_uri);
381 } else {
382 gchar *error = g_strdup_printf(_("\"%s\" is not a valid conference URI"),
383 uri ? uri : "");
384 sipe_backend_notify_error(sipe_public,
385 _("Failed to join the conference"),
386 error);
387 g_free(error);
390 g_free(uri_ue);
392 return session;
395 /** Create new session with Focus URI */
396 struct sip_session *
397 sipe_conf_create(struct sipe_core_private *sipe_private,
398 struct sipe_chat_session *chat_session,
399 const gchar *focus_uri)
401 gchar *hdr;
402 gchar *contact;
403 gchar *body;
404 gchar *self;
405 struct sip_session *session = sipe_session_add_chat(sipe_private,
406 chat_session,
407 FALSE,
408 focus_uri);
410 session->focus_dialog = g_new0(struct sip_dialog, 1);
411 session->focus_dialog->callid = gencallid();
412 session->focus_dialog->with = g_strdup(session->chat_session->id);
413 session->focus_dialog->endpoint_GUID = rand_guid();
414 session->focus_dialog->ourtag = gentag();
416 contact = get_contact(sipe_private);
417 hdr = g_strdup_printf(
418 "Supported: ms-sender\r\n"
419 "Contact: %s\r\n"
420 "Content-Type: application/cccp+xml\r\n",
421 contact);
422 g_free(contact);
424 /* @TODO put request_id to queue to further compare with incoming one */
425 /* focus_URI, from, request_id, focus_URI, from, endpoint_GUID */
426 self = sip_uri_self(sipe_private);
427 body = g_strdup_printf(
428 SIPE_SEND_CONF_ADD_USER,
429 session->focus_dialog->with,
430 self,
431 session->request_id++,
432 session->focus_dialog->with,
433 self,
434 session->focus_dialog->endpoint_GUID);
436 session->focus_dialog->outgoing_invite =
437 sip_transport_invite(sipe_private,
438 hdr,
439 body,
440 session->focus_dialog,
441 process_invite_conf_focus_response);
442 g_free(body);
443 g_free(hdr);
445 /* Rejoin existing session? */
446 if (chat_session) {
447 SIPE_DEBUG_INFO("sipe_conf_create: rejoin '%s' (%s)",
448 chat_session->title,
449 chat_session->id);
450 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
451 chat_session->backend,
452 self,
453 chat_session->title);
455 g_free(self);
457 return(session);
460 /** Modify User Role */
461 void
462 sipe_conf_modify_user_role(struct sipe_core_private *sipe_private,
463 struct sip_session *session,
464 const gchar* who)
466 gchar *hdr;
467 gchar *body;
468 gchar *self;
470 if (!session->focus_dialog || !session->focus_dialog->is_established) {
471 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_user_role: no dialog with focus, exiting.");
472 return;
475 hdr = g_strdup(
476 "Content-Type: application/cccp+xml\r\n");
478 /* @TODO put request_id to queue to further compare with incoming one */
479 self = sip_uri_self(sipe_private);
480 body = g_strdup_printf(
481 SIPE_SEND_CONF_MODIFY_USER_ROLES,
482 session->focus_dialog->with,
483 self,
484 session->request_id++,
485 session->focus_dialog->with,
486 who);
487 g_free(self);
489 sip_transport_info(sipe_private,
490 hdr,
491 body,
492 session->focus_dialog,
493 NULL);
494 g_free(body);
495 g_free(hdr);
499 * Check conference lock status
501 sipe_chat_lock_status sipe_core_chat_lock_status(struct sipe_core_public *sipe_public,
502 struct sipe_chat_session *chat_session)
504 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
505 sipe_chat_lock_status status = SIPE_CHAT_LOCK_STATUS_NOT_ALLOWED;
507 if (chat_session &&
508 (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE)) {
509 struct sip_session *session = sipe_session_find_chat(sipe_private,
510 chat_session);
511 if (session) {
512 gchar *self = sip_uri_self(sipe_private);
514 /* Only operators are allowed to change the lock status */
515 if (sipe_backend_chat_is_operator(chat_session->backend, self)) {
516 status = session->locked ?
517 SIPE_CHAT_LOCK_STATUS_LOCKED :
518 SIPE_CHAT_LOCK_STATUS_UNLOCKED;
521 g_free(self);
525 return(status);
529 * Modify Conference Lock
530 * Sends request to Focus.
531 * INFO method is a carrier of application/cccp+xml
533 void
534 sipe_core_chat_modify_lock(struct sipe_core_public *sipe_public,
535 struct sipe_chat_session *chat_session,
536 const gboolean locked)
538 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
539 gchar *hdr;
540 gchar *body;
541 gchar *self;
543 struct sip_session *session = sipe_session_find_chat(sipe_private,
544 chat_session);
546 if (!session) return;
547 if (!session->focus_dialog || !session->focus_dialog->is_established) {
548 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_conference_lock: no dialog with focus, exiting.");
549 return;
552 hdr = g_strdup(
553 "Content-Type: application/cccp+xml\r\n");
555 /* @TODO put request_id to queue to further compare with incoming one */
556 self = sip_uri_self(sipe_private);
557 body = g_strdup_printf(
558 SIPE_SEND_CONF_MODIFY_CONF_LOCK,
559 session->focus_dialog->with,
560 self,
561 session->request_id++,
562 session->focus_dialog->with,
563 locked ? "true" : "false");
564 g_free(self);
566 sip_transport_info(sipe_private,
567 hdr,
568 body,
569 session->focus_dialog,
570 NULL);
571 g_free(body);
572 g_free(hdr);
575 /** Modify Delete User */
576 void
577 sipe_conf_delete_user(struct sipe_core_private *sipe_private,
578 struct sip_session *session,
579 const gchar* who)
581 gchar *hdr;
582 gchar *body;
583 gchar *self;
585 if (!session->focus_dialog || !session->focus_dialog->is_established) {
586 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_delete_user: no dialog with focus, exiting.");
587 return;
590 hdr = g_strdup(
591 "Content-Type: application/cccp+xml\r\n");
593 /* @TODO put request_id to queue to further compare with incoming one */
594 self = sip_uri_self(sipe_private);
595 body = g_strdup_printf(
596 SIPE_SEND_CONF_DELETE_USER,
597 session->focus_dialog->with,
598 self,
599 session->request_id++,
600 session->focus_dialog->with,
601 who);
602 g_free(self);
604 sip_transport_info(sipe_private,
605 hdr,
606 body,
607 session->focus_dialog,
608 NULL);
609 g_free(body);
610 g_free(hdr);
613 /** Invite counterparty to join conference callback */
614 static gboolean
615 process_invite_conf_response(struct sipe_core_private *sipe_private,
616 struct sipmsg *msg,
617 SIPE_UNUSED_PARAMETER struct transaction *trans)
619 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
621 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
622 dialog->cseq = sipmsg_parse_cseq(msg);
623 dialog->with = parse_from(sipmsg_find_header(msg, "To"));
624 sipe_dialog_parse(dialog, msg, TRUE);
626 if (msg->response >= 200) {
627 /* send ACK to counterparty */
628 dialog->cseq--;
629 sip_transport_ack(sipe_private, dialog);
630 dialog->outgoing_invite = NULL;
631 dialog->is_established = TRUE;
634 if (msg->response >= 400) {
635 SIPE_DEBUG_INFO("process_invite_conf_response: INVITE response is not 200. Failed to invite %s.", dialog->with);
636 /* @TODO notify user of failure to invite counterparty */
637 sipe_dialog_free(dialog);
638 return FALSE;
641 if (msg->response >= 200) {
642 struct sip_session *session = sipe_session_find_im(sipe_private, dialog->with);
643 struct sip_dialog *im_dialog = sipe_dialog_find(session, dialog->with);
645 /* close IM session to counterparty */
646 if (im_dialog) {
647 sip_transport_bye(sipe_private, im_dialog);
648 sipe_dialog_remove(session, dialog->with);
652 sipe_dialog_free(dialog);
653 return TRUE;
657 * Invites counterparty to join conference.
659 void
660 sipe_invite_conf(struct sipe_core_private *sipe_private,
661 struct sip_session *session,
662 const gchar* who)
664 gchar *hdr;
665 gchar *contact;
666 gchar *body;
667 struct sip_dialog *dialog = NULL;
669 /* It will be short lived special dialog.
670 * Will not be stored in session.
672 dialog = g_new0(struct sip_dialog, 1);
673 dialog->callid = gencallid();
674 dialog->with = g_strdup(who);
675 dialog->ourtag = gentag();
677 contact = get_contact(sipe_private);
678 hdr = g_strdup_printf(
679 "Supported: ms-sender\r\n"
680 "Contact: %s\r\n"
681 "Content-Type: application/ms-conf-invite+xml\r\n",
682 contact);
683 g_free(contact);
685 body = g_strdup_printf(
686 SIPE_SEND_CONF_INVITE,
687 session->chat_session->id,
688 session->subject ? session->subject : ""
691 sip_transport_invite(sipe_private,
692 hdr,
693 body,
694 dialog,
695 process_invite_conf_response);
697 sipe_dialog_free(dialog);
698 g_free(body);
699 g_free(hdr);
702 /** Create conference callback */
703 static gboolean
704 process_conf_add_response(struct sipe_core_private *sipe_private,
705 struct sipmsg *msg,
706 struct transaction *trans)
708 if (msg->response >= 400) {
709 SIPE_DEBUG_INFO_NOFORMAT("process_conf_add_response: SERVICE response is not 200. Failed to create conference.");
710 /* @TODO notify user of failure to create conference */
711 return FALSE;
713 if (msg->response == 200) {
714 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
715 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code")))
717 gchar *who = trans->payload->data;
718 const sipe_xml *xn_conference_info = sipe_xml_child(xn_response, "addConference/conference-info");
719 struct sip_session *session = sipe_conf_create(sipe_private,
720 NULL,
721 sipe_xml_attribute(xn_conference_info,
722 "entity"));
724 SIPE_DEBUG_INFO("process_conf_add_response: session->focus_uri=%s",
725 session->chat_session->id);
727 session->pending_invite_queue = slist_insert_unique_sorted(
728 session->pending_invite_queue, g_strdup(who), (GCompareFunc)strcmp);
730 sipe_xml_free(xn_response);
733 return TRUE;
737 * Creates conference.
739 void
740 sipe_conf_add(struct sipe_core_private *sipe_private,
741 const gchar* who)
743 gchar *hdr;
744 gchar *conference_id;
745 gchar *contact;
746 gchar *body;
747 gchar *self;
748 struct transaction *trans;
749 struct sip_dialog *dialog = NULL;
750 time_t expiry = time(NULL) + 7*60*60; /* 7 hours */
751 char *expiry_time;
752 struct transaction_payload *payload;
754 contact = get_contact(sipe_private);
755 hdr = g_strdup_printf(
756 "Supported: ms-sender\r\n"
757 "Contact: %s\r\n"
758 "Content-Type: application/cccp+xml\r\n",
759 contact);
760 g_free(contact);
762 expiry_time = sipe_utils_time_to_str(expiry);
763 self = sip_uri_self(sipe_private);
764 conference_id = genconfid();
765 body = g_strdup_printf(
766 SIPE_SEND_CONF_ADD,
767 sipe_private->focus_factory_uri,
768 self,
769 rand(),
770 conference_id,
771 expiry_time);
772 g_free(self);
773 g_free(conference_id);
774 g_free(expiry_time);
776 trans = sip_transport_service(sipe_private,
777 sipe_private->focus_factory_uri,
778 hdr,
779 body,
780 process_conf_add_response);
782 payload = g_new0(struct transaction_payload, 1);
783 payload->destroy = g_free;
784 payload->data = g_strdup(who);
785 trans->payload = payload;
787 sipe_dialog_free(dialog);
788 g_free(body);
789 g_free(hdr);
792 static void
793 accept_incoming_invite_conf(struct sipe_core_private *sipe_private,
794 gchar *focus_uri,
795 gboolean audio,
796 struct sipmsg *msg)
798 struct sip_session *session;
799 gchar *newTag = gentag();
800 const gchar *oldHeader = sipmsg_find_header(msg, "To");
801 gchar *newHeader;
803 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
804 g_free(newTag);
805 sipmsg_remove_header_now(msg, "To");
806 sipmsg_add_header_now(msg, "To", newHeader);
807 g_free(newHeader);
809 /* acknowledge invite */
810 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
812 /* add self to conf */
813 session = sipe_conf_create(sipe_private, NULL, focus_uri);
814 session->is_call = audio;
817 struct conf_accept_ctx {
818 gchar *focus_uri;
819 struct sipmsg *msg;
820 struct sipe_user_ask_ctx *ask_ctx;
823 static void
824 conf_accept_ctx_free(struct conf_accept_ctx *ctx)
826 g_return_if_fail(ctx != NULL);
828 sipmsg_free(ctx->msg);
829 g_free(ctx->focus_uri);
830 g_free(ctx);
833 static void
834 conf_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
836 sipe_private->sessions_to_accept =
837 g_slist_remove(sipe_private->sessions_to_accept, ctx);
839 accept_incoming_invite_conf(sipe_private, ctx->focus_uri, TRUE, ctx->msg);
840 conf_accept_ctx_free(ctx);
843 static void
844 conf_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
846 sipe_private->sessions_to_accept =
847 g_slist_remove(sipe_private->sessions_to_accept, ctx);
849 sip_transport_response(sipe_private,
850 ctx->msg,
851 603, "Decline", NULL);
853 conf_accept_ctx_free(ctx);
856 void
857 sipe_conf_cancel_unaccepted(struct sipe_core_private *sipe_private,
858 struct sipmsg *msg)
860 const gchar *callid1 = msg ? sipmsg_find_header(msg, "Call-ID") : NULL;
861 GSList *it = sipe_private->sessions_to_accept;
862 while (it) {
863 struct conf_accept_ctx *ctx = it->data;
864 const gchar *callid2 = NULL;
866 if (msg && ctx->msg)
867 callid2 = sipmsg_find_header(ctx->msg, "Call-ID");
869 if (sipe_strequal(callid1, callid2)) {
870 GSList *tmp;
872 if (ctx->msg)
873 sip_transport_response(sipe_private, ctx->msg,
874 487, "Request Terminated", NULL);
876 if (msg)
877 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
879 sipe_user_close_ask(ctx->ask_ctx);
880 conf_accept_ctx_free(ctx);
882 tmp = it;
883 it = it->next;
885 sipe_private->sessions_to_accept =
886 g_slist_delete_link(sipe_private->sessions_to_accept, tmp);
888 if (callid1)
889 break;
890 } else
891 it = it->next;
895 static void
896 ask_accept_voice_conference(struct sipe_core_private *sipe_private,
897 const gchar *focus_uri,
898 struct sipmsg *msg,
899 SipeUserAskCb accept_cb,
900 SipeUserAskCb decline_cb)
902 gchar **parts;
903 gchar *alias;
904 gchar *ask_msg;
905 const gchar *novv_note;
906 struct conf_accept_ctx *ctx;
908 #ifdef HAVE_VV
909 novv_note = "";
910 #else
911 novv_note = _("\n\nAs this client was not compiled with voice call "
912 "support, if you accept, you will be able to contact "
913 "the other participants only via IM session.");
914 #endif
916 parts = g_strsplit(focus_uri, ";", 2);
917 alias = sipe_buddy_get_alias(sipe_private, parts[0]);
919 ask_msg = g_strdup_printf(_("%s wants to invite you "
920 "to the conference call%s"),
921 alias ? alias : parts[0], novv_note);
923 g_free(alias);
924 g_strfreev(parts);
926 ctx = g_new0(struct conf_accept_ctx, 1);
927 sipe_private->sessions_to_accept =
928 g_slist_append(sipe_private->sessions_to_accept, ctx);
930 ctx->focus_uri = g_strdup(focus_uri);
931 ctx->msg = msg ? sipmsg_copy(msg) : NULL;
932 ctx->ask_ctx = sipe_user_ask(sipe_private, ask_msg,
933 _("Accept"), accept_cb,
934 _("Decline"), decline_cb,
935 ctx);
937 g_free(ask_msg);
940 void
941 process_incoming_invite_conf(struct sipe_core_private *sipe_private,
942 struct sipmsg *msg)
944 sipe_xml *xn_conferencing = sipe_xml_parse(msg->body, msg->bodylen);
945 const sipe_xml *xn_focus_uri = sipe_xml_child(xn_conferencing, "focus-uri");
946 const sipe_xml *xn_audio = sipe_xml_child(xn_conferencing, "audio");
947 gchar *focus_uri = sipe_xml_data(xn_focus_uri);
948 gboolean audio = sipe_strequal(sipe_xml_attribute(xn_audio, "available"), "true");
950 sipe_xml_free(xn_conferencing);
952 SIPE_DEBUG_INFO("We have received invitation to Conference. Focus URI=%s", focus_uri);
954 if (audio) {
955 sip_transport_response(sipe_private, msg, 180, "Ringing", NULL);
956 ask_accept_voice_conference(sipe_private, focus_uri, msg,
957 (SipeUserAskCb) conf_accept_cb,
958 (SipeUserAskCb) conf_decline_cb);
960 } else {
961 accept_incoming_invite_conf(sipe_private, focus_uri, FALSE, msg);
964 g_free(focus_uri);
967 #ifdef HAVE_VV
969 static void
970 call_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
972 struct sip_session *session;
973 session = sipe_session_find_conference(sipe_private, ctx->focus_uri);
975 sipe_private->sessions_to_accept =
976 g_slist_remove(sipe_private->sessions_to_accept, ctx);
978 if (session) {
979 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
980 session->chat_session);
983 conf_accept_ctx_free(ctx);
986 static void
987 call_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
989 sipe_private->sessions_to_accept =
990 g_slist_remove(sipe_private->sessions_to_accept, ctx);
992 conf_accept_ctx_free(ctx);
995 #endif // HAVE_VV
997 void
998 sipe_process_conference(struct sipe_core_private *sipe_private,
999 struct sipmsg *msg)
1001 sipe_xml *xn_conference_info;
1002 const sipe_xml *node;
1003 const sipe_xml *xn_subject;
1004 const gchar *focus_uri;
1005 struct sip_session *session;
1006 gboolean just_joined = FALSE;
1007 #ifdef HAVE_VV
1008 gboolean audio_was_added = FALSE;
1009 #endif
1011 if (msg->response != 0 && msg->response != 200) return;
1013 if (msg->bodylen == 0 || msg->body == NULL || !sipe_strequal(sipmsg_find_header(msg, "Event"), "conference")) return;
1015 xn_conference_info = sipe_xml_parse(msg->body, msg->bodylen);
1016 if (!xn_conference_info) return;
1018 focus_uri = sipe_xml_attribute(xn_conference_info, "entity");
1019 session = sipe_session_find_conference(sipe_private, focus_uri);
1021 if (!session) {
1022 SIPE_DEBUG_INFO("sipe_process_conference: unable to find conf session with focus=%s", focus_uri);
1023 return;
1026 if (!session->chat_session->backend) {
1027 gchar *self = sip_uri_self(sipe_private);
1029 /* create chat */
1030 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1031 session->chat_session,
1032 session->chat_session->title,
1033 self);
1034 just_joined = TRUE;
1035 /* @TODO ask for full state (re-subscribe) if it was a partial one -
1036 * this is to obtain full list of conference participants.
1038 g_free(self);
1041 /* subject */
1042 if ((xn_subject = sipe_xml_child(xn_conference_info, "conference-description/subject"))) {
1043 g_free(session->subject);
1044 session->subject = sipe_xml_data(xn_subject);
1045 sipe_backend_chat_topic(session->chat_session->backend, session->subject);
1046 SIPE_DEBUG_INFO("sipe_process_conference: subject=%s", session->subject ? session->subject : "");
1049 /* IM MCU URI */
1050 if (!session->im_mcu_uri) {
1051 for (node = sipe_xml_child(xn_conference_info, "conference-description/conf-uris/entry");
1052 node;
1053 node = sipe_xml_twin(node))
1055 gchar *purpose = sipe_xml_data(sipe_xml_child(node, "purpose"));
1057 if (sipe_strequal("chat", purpose)) {
1058 g_free(purpose);
1059 session->im_mcu_uri = sipe_xml_data(sipe_xml_child(node, "uri"));
1060 SIPE_DEBUG_INFO("sipe_process_conference: im_mcu_uri=%s", session->im_mcu_uri);
1061 break;
1063 g_free(purpose);
1067 /* users */
1068 for (node = sipe_xml_child(xn_conference_info, "users/user"); node; node = sipe_xml_twin(node)) {
1069 const gchar *user_uri = sipe_xml_attribute(node, "entity");
1070 const gchar *state = sipe_xml_attribute(node, "state");
1071 gchar *role = sipe_xml_data(sipe_xml_child(node, "roles/entry"));
1072 gboolean is_operator = sipe_strequal(role, "presenter");
1073 gboolean is_in_im_mcu = FALSE;
1074 gchar *self = sip_uri_self(sipe_private);
1076 if (sipe_strequal("deleted", state)) {
1077 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1078 sipe_backend_chat_remove(session->chat_session->backend,
1079 user_uri);
1081 } else {
1082 /* endpoints */
1083 const sipe_xml *endpoint;
1084 for (endpoint = sipe_xml_child(node, "endpoint"); endpoint; endpoint = sipe_xml_twin(endpoint)) {
1085 const gchar *session_type;
1086 gchar *status = sipe_xml_data(sipe_xml_child(endpoint, "status"));
1087 gboolean connected = sipe_strequal("connected", status);
1088 g_free(status);
1090 if (!connected)
1091 continue;
1093 session_type = sipe_xml_attribute(endpoint, "session-type");
1095 if (sipe_strequal("chat", session_type)) {
1096 is_in_im_mcu = TRUE;
1097 if (!sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1098 sipe_backend_chat_add(session->chat_session->backend,
1099 user_uri,
1100 !just_joined && g_ascii_strcasecmp(user_uri, self));
1102 if (is_operator) {
1103 sipe_backend_chat_operator(session->chat_session->backend,
1104 user_uri);
1106 } else if (sipe_strequal("audio-video", session_type)) {
1107 #ifdef HAVE_VV
1108 if (!session->is_call)
1109 audio_was_added = TRUE;
1110 #endif
1113 if (!is_in_im_mcu) {
1114 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1115 sipe_backend_chat_remove(session->chat_session->backend,
1116 user_uri);
1120 g_free(role);
1121 g_free(self);
1124 #ifdef HAVE_VV
1125 if (audio_was_added) {
1126 session->is_call = TRUE;
1127 ask_accept_voice_conference(sipe_private, focus_uri, NULL,
1128 (SipeUserAskCb) call_accept_cb,
1129 (SipeUserAskCb) call_decline_cb);
1131 #endif
1133 /* entity-view, locked */
1134 for (node = sipe_xml_child(xn_conference_info, "conference-view/entity-view");
1135 node;
1136 node = sipe_xml_twin(node)) {
1138 const sipe_xml *xn_type = sipe_xml_child(node, "entity-state/media/entry/type");
1139 gchar *tmp = NULL;
1140 if (xn_type && sipe_strequal("chat", (tmp = sipe_xml_data(xn_type)))) {
1141 const sipe_xml *xn_locked = sipe_xml_child(node, "entity-state/locked");
1142 if (xn_locked) {
1143 gchar *locked = sipe_xml_data(xn_locked);
1144 gboolean prev_locked = session->locked;
1145 session->locked = sipe_strequal(locked, "true");
1146 if (prev_locked && !session->locked) {
1147 sipe_user_present_info(sipe_private, session,
1148 _("This conference is no longer locked. Additional participants can now join."));
1150 if (!prev_locked && session->locked) {
1151 sipe_user_present_info(sipe_private, session,
1152 _("This conference is locked. Nobody else can join the conference while it is locked."));
1155 SIPE_DEBUG_INFO("sipe_process_conference: session->locked=%s",
1156 session->locked ? "TRUE" : "FALSE");
1157 g_free(locked);
1160 g_free(tmp);
1162 sipe_xml_free(xn_conference_info);
1164 if (session->im_mcu_uri) {
1165 struct sip_dialog *dialog = sipe_dialog_find(session, session->im_mcu_uri);
1166 if (!dialog) {
1167 dialog = sipe_dialog_add(session);
1169 dialog->callid = g_strdup(session->callid);
1170 dialog->with = g_strdup(session->im_mcu_uri);
1172 /* send INVITE to IM MCU */
1173 sipe_im_invite(sipe_private, session, dialog->with, NULL, NULL, NULL, FALSE);
1177 sipe_process_pending_invite_queue(sipe_private, session);
1180 void
1181 sipe_conf_immcu_closed(struct sipe_core_private *sipe_private,
1182 struct sip_session *session)
1184 sipe_user_present_info(sipe_private, session,
1185 _("You have been disconnected from this conference."));
1186 sipe_backend_chat_close(session->chat_session->backend);
1189 void
1190 conf_session_close(struct sipe_core_private *sipe_private,
1191 struct sip_session *session)
1193 if (session) {
1194 /* unsubscribe from focus */
1195 sipe_subscribe_conference(sipe_private, session, TRUE);
1197 if (session->focus_dialog) {
1198 /* send BYE to focus */
1199 sip_transport_bye(sipe_private, session->focus_dialog);
1204 void
1205 sipe_process_imdn(struct sipe_core_private *sipe_private,
1206 struct sipmsg *msg)
1208 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1209 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1210 static struct sip_session *session;
1211 sipe_xml *xn_imdn;
1212 const sipe_xml *node;
1213 gchar *message_id;
1214 gchar *message;
1216 session = sipe_session_find_chat_or_im(sipe_private, callid, with);
1217 if (!session) {
1218 SIPE_DEBUG_INFO("sipe_process_imdn: unable to find conf session with callid=%s", callid);
1219 g_free(with);
1220 return;
1223 xn_imdn = sipe_xml_parse(msg->body, msg->bodylen);
1224 message_id = sipe_xml_data(sipe_xml_child(xn_imdn, "message-id"));
1226 message = g_hash_table_lookup(session->conf_unconfirmed_messages, message_id);
1228 /* recipient */
1229 for (node = sipe_xml_child(xn_imdn, "recipient"); node; node = sipe_xml_twin(node)) {
1230 gchar *tmp = parse_from(sipe_xml_attribute(node, "uri"));
1231 gchar *uri = parse_from(tmp);
1232 sipe_user_present_message_undelivered(sipe_private, session, -1, -1, uri, message);
1233 g_free(tmp);
1234 g_free(uri);
1237 sipe_xml_free(xn_imdn);
1239 g_hash_table_remove(session->conf_unconfirmed_messages, message_id);
1240 SIPE_DEBUG_INFO("sipe_process_imdn: removed message %s from conf_unconfirmed_messages(count=%d)",
1241 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
1242 g_free(message_id);
1243 g_free(with);
1246 void sipe_core_conf_make_leader(struct sipe_core_public *sipe_public,
1247 gpointer parameter,
1248 const gchar *buddy_name)
1250 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1251 struct sipe_chat_session *chat_session = parameter;
1252 struct sip_session *session;
1254 SIPE_DEBUG_INFO("sipe_core_conf_make_leader: chat_title=%s",
1255 chat_session->title);
1257 session = sipe_session_find_chat(sipe_private, chat_session);
1258 sipe_conf_modify_user_role(sipe_private, session, buddy_name);
1261 void sipe_core_conf_remove_from(struct sipe_core_public *sipe_public,
1262 gpointer parameter,
1263 const gchar *buddy_name)
1265 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1266 struct sipe_chat_session *chat_session = parameter;
1267 struct sip_session *session;
1269 SIPE_DEBUG_INFO("sipe_core_conf_remove_from: chat_title=%s",
1270 chat_session->title);
1272 session = sipe_session_find_chat(sipe_private, chat_session);
1273 sipe_conf_delete_user(sipe_private, session, buddy_name);
1277 Local Variables:
1278 mode: c
1279 c-file-style: "bsd"
1280 indent-tabs-mode: t
1281 tab-width: 8
1282 End: