tls: add a hard-coded client_hello message
[siplcs.git] / src / core / sipe-conf.c
blob7478d404e3518a7763df886b7cede3e5111c4815
1 /**
2 * @file sipe-conf.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 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 #ifdef HAVE_VV
47 #include "sipe-media.h"
48 #endif
49 #include "sipe-nls.h"
50 #include "sipe-session.h"
51 #include "sipe-subscriptions.h"
52 #include "sipe-user.h"
53 #include "sipe-utils.h"
54 #include "sipe-xml.h"
55 #include "sipe.h"
57 #ifdef HAVE_VV
58 #define ENTITY_VIEW_AUDIO_VIDEO "<msci:entity-view entity=\"audio-video\"/>"
59 #else
60 #define ENTITY_VIEW_AUDIO_VIDEO
61 #endif
63 /**
64 * Add Conference request to FocusFactory.
65 * @param focus_factory_uri (%s) Ex.: sip:bob7@boston.local;gruu;opaque=app:conf:focusfactory
66 * @param from (%s) Ex.: sip:bob7@boston.local
67 * @param request_id (%d) Ex.: 1094520
68 * @param conference_id (%s) Ex.: 8386E6AEAAA41E4AA6627BA76D43B6D1
69 * @param expiry_time (%s) Ex.: 2009-07-13T17:57:09Z , Default duration: 7 hours
71 #define SIPE_SEND_CONF_ADD \
72 "<?xml version=\"1.0\"?>"\
73 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" "\
74 "xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
75 "C3PVersion=\"1\" "\
76 "to=\"%s\" "\
77 "from=\"%s\" "\
78 "requestId=\"%d\">"\
79 "<addConference>"\
80 "<ci:conference-info xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\">"\
81 "<ci:conference-description>"\
82 "<ci:subject/>"\
83 "<msci:conference-id>%s</msci:conference-id>"\
84 "<msci:expiry-time>%s</msci:expiry-time>"\
85 "<msci:admission-policy>openAuthenticated</msci:admission-policy>"\
86 "</ci:conference-description>"\
87 "<msci:conference-view>"\
88 "<msci:entity-view entity=\"chat\"/>"\
89 ENTITY_VIEW_AUDIO_VIDEO \
90 "</msci:conference-view>"\
91 "</ci:conference-info>"\
92 "</addConference>"\
93 "</request>"
95 /**
96 * AddUser request to Focus.
97 * Params:
98 * focus_URI, from, request_id, focus_URI, from, endpoint_GUID
100 #define SIPE_SEND_CONF_ADD_USER \
101 "<?xml version=\"1.0\"?>"\
102 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
103 "C3PVersion=\"1\" "\
104 "to=\"%s\" "\
105 "from=\"%s\" "\
106 "requestId=\"%d\">"\
107 "<addUser>"\
108 "<conferenceKeys confEntity=\"%s\"/>"\
109 "<ci:user xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"%s\">"\
110 "<ci:roles>"\
111 "<ci:entry>attendee</ci:entry>"\
112 "</ci:roles>"\
113 "<ci:endpoint entity=\"{%s}\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\"/>"\
114 "</ci:user>"\
115 "</addUser>"\
116 "</request>"
119 * ModifyUserRoles request to Focus. Makes user a leader.
120 * @param focus_uri (%s)
121 * @param from (%s)
122 * @param request_id (%d)
123 * @param focus_uri (%s)
124 * @param who (%s)
126 #define SIPE_SEND_CONF_MODIFY_USER_ROLES \
127 "<?xml version=\"1.0\"?>"\
128 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
129 "C3PVersion=\"1\" "\
130 "to=\"%s\" "\
131 "from=\"%s\" "\
132 "requestId=\"%d\">"\
133 "<modifyUserRoles>"\
134 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
135 "<user-roles xmlns=\"urn:ietf:params:xml:ns:conference-info\">"\
136 "<entry>presenter</entry>"\
137 "</user-roles>"\
138 "</modifyUserRoles>"\
139 "</request>"
142 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
143 * @param focus_uri (%s)
144 * @param from (%s)
145 * @param request_id (%d)
146 * @param focus_uri (%s)
147 * @param locked (%s) "true" or "false" values applicable
149 #define SIPE_SEND_CONF_MODIFY_CONF_LOCK \
150 "<?xml version=\"1.0\"?>"\
151 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
152 "C3PVersion=\"1\" "\
153 "to=\"%s\" "\
154 "from=\"%s\" "\
155 "requestId=\"%d\">"\
156 "<modifyConferenceLock>"\
157 "<conferenceKeys confEntity=\"%s\"/>"\
158 "<locked>%s</locked>"\
159 "</modifyConferenceLock>"\
160 "</request>"
163 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
164 * @param focus_uri (%s)
165 * @param from (%s)
166 * @param request_id (%d)
167 * @param focus_uri (%s)
168 * @param who (%s)
170 #define SIPE_SEND_CONF_DELETE_USER \
171 "<?xml version=\"1.0\"?>"\
172 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
173 "C3PVersion=\"1\" "\
174 "to=\"%s\" "\
175 "from=\"%s\" "\
176 "requestId=\"%d\">"\
177 "<deleteUser>"\
178 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
179 "</deleteUser>"\
180 "</request>"
183 * Invite counterparty to join conference.
184 * @param focus_uri (%s)
185 * @param subject (%s) of conference
187 #define SIPE_SEND_CONF_INVITE \
188 "<Conferencing version=\"2.0\">"\
189 "<focus-uri>%s</focus-uri>"\
190 "<subject>%s</subject>"\
191 "<im available=\"true\">"\
192 "<first-im/>"\
193 "</im>"\
194 "</Conferencing>"
197 * Generates random GUID.
198 * This method is borrowed from pidgin's msnutils.c
200 static char *
201 rand_guid()
203 return g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
204 rand() % 0xAAFF + 0x1111,
205 rand() % 0xAAFF + 0x1111,
206 rand() % 0xAAFF + 0x1111,
207 rand() % 0xAAFF + 0x1111,
208 rand() % 0xAAFF + 0x1111,
209 rand() % 0xAAFF + 0x1111,
210 rand() % 0xAAFF + 0x1111,
211 rand() % 0xAAFF + 0x1111);
215 * @param expires not respected if set to negative value (E.g. -1)
217 static void
218 sipe_subscribe_conference(struct sipe_core_private *sipe_private,
219 struct sip_session *session,
220 gboolean expires)
222 sipe_subscribe(sipe_private,
223 session->chat_session->id,
224 "conference",
225 "application/conference-info+xml",
226 expires ? "Expires: 0\r\n" : NULL,
227 NULL,
228 NULL);
231 /** Invite us to the focus callback */
232 static gboolean
233 process_invite_conf_focus_response(struct sipe_core_private *sipe_private,
234 struct sipmsg *msg,
235 SIPE_UNUSED_PARAMETER struct transaction *trans)
237 struct sip_session *session = NULL;
238 char *focus_uri = parse_from(sipmsg_find_header(msg, "To"));
240 session = sipe_session_find_conference(sipe_private, focus_uri);
242 if (!session) {
243 SIPE_DEBUG_INFO("process_invite_conf_focus_response: unable to find conf session with focus=%s", focus_uri);
244 g_free(focus_uri);
245 return FALSE;
248 if (!session->focus_dialog) {
249 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: session's focus_dialog is NULL");
250 g_free(focus_uri);
251 return FALSE;
254 sipe_dialog_parse(session->focus_dialog, msg, TRUE);
256 if (msg->response >= 200) {
257 /* send ACK to focus */
258 session->focus_dialog->cseq = 0;
259 sip_transport_ack(sipe_private, session->focus_dialog);
260 session->focus_dialog->outgoing_invite = NULL;
261 session->focus_dialog->is_established = TRUE;
264 if (msg->response >= 400) {
265 gchar *reason = sipmsg_get_ms_diagnostics_reason(msg);
267 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: INVITE response is not 200. Failed to join focus.");
268 sipe_backend_notify_error(_("Failed to join the conference"),
269 reason ? reason : _("no reason given"));
270 g_free(reason);
272 sipe_session_remove(sipe_private, session);
273 g_free(focus_uri);
274 return FALSE;
275 } else if (msg->response == 200) {
276 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
277 const gchar *code = sipe_xml_attribute(xn_response, "code");
278 if (sipe_strequal(code, "success")) {
279 /* subscribe to focus */
280 sipe_subscribe_conference(sipe_private, session, FALSE);
281 #ifdef HAVE_VV
282 if (session->is_call)
283 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
284 session->chat_session);
285 #endif
287 sipe_xml_free(xn_response);
290 g_free(focus_uri);
291 return TRUE;
294 struct sip_session *
295 sipe_core_conf_create(struct sipe_core_public *sipe_public,
296 const gchar *focus_uri)
298 gchar *buf;
299 const gchar *focus_uri_ue;
300 struct sip_session *session = NULL;
302 focus_uri_ue = buf = sipe_utils_uri_unescape(focus_uri);
304 // URI can have this prefix if it was typed in by the user
305 if (g_str_has_prefix(focus_uri_ue, "meet:"))
306 focus_uri_ue += 5;
308 if (!focus_uri_ue || !g_str_has_prefix(focus_uri_ue, "sip:") ||
309 strlen(focus_uri_ue) == 4 || g_strstr_len(focus_uri_ue, -1, "%")) {
310 gchar *error = g_strdup_printf(_("\"%s\" is not a valid focus URI"),
311 focus_uri ? focus_uri : "");
312 sipe_backend_notify_error(_("Failed to join the conference"),
313 error);
314 g_free(error);
315 } else {
316 gchar *querystr = g_strstr_len(focus_uri_ue, -1, "?");
317 if (querystr) {
318 /* TODO: Investigate how conf-key field should be used,
319 * ignoring for now */
320 *querystr = '\0';
323 session = sipe_conf_create(SIPE_CORE_PRIVATE, NULL, focus_uri_ue);
326 g_free(buf);
328 return session;
331 /** Create new session with Focus URI */
332 struct sip_session *
333 sipe_conf_create(struct sipe_core_private *sipe_private,
334 struct sipe_chat_session *chat_session,
335 const gchar *focus_uri)
337 gchar *hdr;
338 gchar *contact;
339 gchar *body;
340 gchar *self;
341 struct sip_session *session = sipe_session_add_chat(sipe_private,
342 chat_session,
343 FALSE,
344 focus_uri);
346 session->focus_dialog = g_new0(struct sip_dialog, 1);
347 session->focus_dialog->callid = gencallid();
348 session->focus_dialog->with = g_strdup(session->chat_session->id);
349 session->focus_dialog->endpoint_GUID = rand_guid();
350 session->focus_dialog->ourtag = gentag();
352 contact = get_contact(sipe_private);
353 hdr = g_strdup_printf(
354 "Supported: ms-sender\r\n"
355 "Contact: %s\r\n"
356 "Content-Type: application/cccp+xml\r\n",
357 contact);
358 g_free(contact);
360 /* @TODO put request_id to queue to further compare with incoming one */
361 /* focus_URI, from, request_id, focus_URI, from, endpoint_GUID */
362 self = sip_uri_self(sipe_private);
363 body = g_strdup_printf(
364 SIPE_SEND_CONF_ADD_USER,
365 session->focus_dialog->with,
366 self,
367 session->request_id++,
368 session->focus_dialog->with,
369 self,
370 session->focus_dialog->endpoint_GUID);
372 session->focus_dialog->outgoing_invite =
373 sip_transport_invite(sipe_private,
374 hdr,
375 body,
376 session->focus_dialog,
377 process_invite_conf_focus_response);
378 g_free(body);
379 g_free(hdr);
381 /* Rejoin existing session? */
382 if (chat_session) {
383 SIPE_DEBUG_INFO("sipe_conf_create: rejoin '%s' (%s)",
384 chat_session->title,
385 chat_session->id);
386 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
387 chat_session->backend,
388 self,
389 chat_session->title);
391 g_free(self);
393 return(session);
396 /** Modify User Role */
397 void
398 sipe_conf_modify_user_role(struct sipe_core_private *sipe_private,
399 struct sip_session *session,
400 const gchar* who)
402 gchar *hdr;
403 gchar *body;
404 gchar *self;
406 if (!session->focus_dialog || !session->focus_dialog->is_established) {
407 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_user_role: no dialog with focus, exiting.");
408 return;
411 hdr = g_strdup(
412 "Content-Type: application/cccp+xml\r\n");
414 /* @TODO put request_id to queue to further compare with incoming one */
415 self = sip_uri_self(sipe_private);
416 body = g_strdup_printf(
417 SIPE_SEND_CONF_MODIFY_USER_ROLES,
418 session->focus_dialog->with,
419 self,
420 session->request_id++,
421 session->focus_dialog->with,
422 who);
423 g_free(self);
425 sip_transport_info(sipe_private,
426 hdr,
427 body,
428 session->focus_dialog,
429 NULL);
430 g_free(body);
431 g_free(hdr);
435 * Check conference lock status
437 sipe_chat_lock_status sipe_core_chat_lock_status(struct sipe_core_public *sipe_public,
438 struct sipe_chat_session *chat_session)
440 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
441 sipe_chat_lock_status status = SIPE_CHAT_LOCK_STATUS_NOT_ALLOWED;
443 if (chat_session &&
444 (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE)) {
445 struct sip_session *session = sipe_session_find_chat(sipe_private,
446 chat_session);
447 if (session) {
448 gchar *self = sip_uri_self(sipe_private);
450 /* Only operators are allowed to change the lock status */
451 if (sipe_backend_chat_is_operator(chat_session->backend, self)) {
452 status = session->locked ?
453 SIPE_CHAT_LOCK_STATUS_LOCKED :
454 SIPE_CHAT_LOCK_STATUS_UNLOCKED;
457 g_free(self);
461 return(status);
465 * Modify Conference Lock
466 * Sends request to Focus.
467 * INFO method is a carrier of application/cccp+xml
469 void
470 sipe_core_chat_modify_lock(struct sipe_core_public *sipe_public,
471 struct sipe_chat_session *chat_session,
472 const gboolean locked)
474 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
475 gchar *hdr;
476 gchar *body;
477 gchar *self;
479 struct sip_session *session = sipe_session_find_chat(sipe_private,
480 chat_session);
482 if (!session) return;
483 if (!session->focus_dialog || !session->focus_dialog->is_established) {
484 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_conference_lock: no dialog with focus, exiting.");
485 return;
488 hdr = g_strdup(
489 "Content-Type: application/cccp+xml\r\n");
491 /* @TODO put request_id to queue to further compare with incoming one */
492 self = sip_uri_self(sipe_private);
493 body = g_strdup_printf(
494 SIPE_SEND_CONF_MODIFY_CONF_LOCK,
495 session->focus_dialog->with,
496 self,
497 session->request_id++,
498 session->focus_dialog->with,
499 locked ? "true" : "false");
500 g_free(self);
502 sip_transport_info(sipe_private,
503 hdr,
504 body,
505 session->focus_dialog,
506 NULL);
507 g_free(body);
508 g_free(hdr);
511 /** Modify Delete User */
512 void
513 sipe_conf_delete_user(struct sipe_core_private *sipe_private,
514 struct sip_session *session,
515 const gchar* who)
517 gchar *hdr;
518 gchar *body;
519 gchar *self;
521 if (!session->focus_dialog || !session->focus_dialog->is_established) {
522 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_delete_user: no dialog with focus, exiting.");
523 return;
526 hdr = g_strdup(
527 "Content-Type: application/cccp+xml\r\n");
529 /* @TODO put request_id to queue to further compare with incoming one */
530 self = sip_uri_self(sipe_private);
531 body = g_strdup_printf(
532 SIPE_SEND_CONF_DELETE_USER,
533 session->focus_dialog->with,
534 self,
535 session->request_id++,
536 session->focus_dialog->with,
537 who);
538 g_free(self);
540 sip_transport_info(sipe_private,
541 hdr,
542 body,
543 session->focus_dialog,
544 NULL);
545 g_free(body);
546 g_free(hdr);
549 /** Invite counterparty to join conference callback */
550 static gboolean
551 process_invite_conf_response(struct sipe_core_private *sipe_private,
552 struct sipmsg *msg,
553 SIPE_UNUSED_PARAMETER struct transaction *trans)
555 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
557 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
558 dialog->cseq = sipmsg_parse_cseq(msg);
559 dialog->with = parse_from(sipmsg_find_header(msg, "To"));
560 sipe_dialog_parse(dialog, msg, TRUE);
562 if (msg->response >= 200) {
563 /* send ACK to counterparty */
564 dialog->cseq--;
565 sip_transport_ack(sipe_private, dialog);
566 dialog->outgoing_invite = NULL;
567 dialog->is_established = TRUE;
570 if (msg->response >= 400) {
571 SIPE_DEBUG_INFO("process_invite_conf_response: INVITE response is not 200. Failed to invite %s.", dialog->with);
572 /* @TODO notify user of failure to invite counterparty */
573 sipe_dialog_free(dialog);
574 return FALSE;
577 if (msg->response >= 200) {
578 struct sip_session *session = sipe_session_find_im(sipe_private, dialog->with);
579 struct sip_dialog *im_dialog = sipe_dialog_find(session, dialog->with);
581 /* close IM session to counterparty */
582 if (im_dialog) {
583 sip_transport_bye(sipe_private, im_dialog);
584 sipe_dialog_remove(session, dialog->with);
588 sipe_dialog_free(dialog);
589 return TRUE;
593 * Invites counterparty to join conference.
595 void
596 sipe_invite_conf(struct sipe_core_private *sipe_private,
597 struct sip_session *session,
598 const gchar* who)
600 gchar *hdr;
601 gchar *contact;
602 gchar *body;
603 struct sip_dialog *dialog = NULL;
605 /* It will be short lived special dialog.
606 * Will not be stored in session.
608 dialog = g_new0(struct sip_dialog, 1);
609 dialog->callid = gencallid();
610 dialog->with = g_strdup(who);
611 dialog->ourtag = gentag();
613 contact = get_contact(sipe_private);
614 hdr = g_strdup_printf(
615 "Supported: ms-sender\r\n"
616 "Contact: %s\r\n"
617 "Content-Type: application/ms-conf-invite+xml\r\n",
618 contact);
619 g_free(contact);
621 body = g_strdup_printf(
622 SIPE_SEND_CONF_INVITE,
623 session->chat_session->id,
624 session->subject ? session->subject : ""
627 sip_transport_invite(sipe_private,
628 hdr,
629 body,
630 dialog,
631 process_invite_conf_response);
633 sipe_dialog_free(dialog);
634 g_free(body);
635 g_free(hdr);
638 /** Create conference callback */
639 static gboolean
640 process_conf_add_response(struct sipe_core_private *sipe_private,
641 struct sipmsg *msg,
642 struct transaction *trans)
644 if (msg->response >= 400) {
645 SIPE_DEBUG_INFO_NOFORMAT("process_conf_add_response: SERVICE response is not 200. Failed to create conference.");
646 /* @TODO notify user of failure to create conference */
647 return FALSE;
649 if (msg->response == 200) {
650 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
651 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code")))
653 gchar *who = trans->payload->data;
654 const sipe_xml *xn_conference_info = sipe_xml_child(xn_response, "addConference/conference-info");
655 struct sip_session *session = sipe_conf_create(sipe_private,
656 NULL,
657 sipe_xml_attribute(xn_conference_info,
658 "entity"));
660 SIPE_DEBUG_INFO("process_conf_add_response: session->focus_uri=%s",
661 session->chat_session->id);
663 session->pending_invite_queue = slist_insert_unique_sorted(
664 session->pending_invite_queue, g_strdup(who), (GCompareFunc)strcmp);
666 sipe_xml_free(xn_response);
669 return TRUE;
673 * Creates conference.
675 void
676 sipe_conf_add(struct sipe_core_private *sipe_private,
677 const gchar* who)
679 gchar *hdr;
680 gchar *conference_id;
681 gchar *contact;
682 gchar *body;
683 gchar *self;
684 struct transaction *trans;
685 struct sip_dialog *dialog = NULL;
686 time_t expiry = time(NULL) + 7*60*60; /* 7 hours */
687 char *expiry_time;
688 struct transaction_payload *payload;
690 contact = get_contact(sipe_private);
691 hdr = g_strdup_printf(
692 "Supported: ms-sender\r\n"
693 "Contact: %s\r\n"
694 "Content-Type: application/cccp+xml\r\n",
695 contact);
696 g_free(contact);
698 expiry_time = sipe_utils_time_to_str(expiry);
699 self = sip_uri_self(sipe_private);
700 conference_id = genconfid();
701 body = g_strdup_printf(
702 SIPE_SEND_CONF_ADD,
703 sipe_private->focus_factory_uri,
704 self,
705 rand(),
706 conference_id,
707 expiry_time);
708 g_free(self);
709 g_free(conference_id);
710 g_free(expiry_time);
712 trans = sip_transport_service(sipe_private,
713 sipe_private->focus_factory_uri,
714 hdr,
715 body,
716 process_conf_add_response);
718 payload = g_new0(struct transaction_payload, 1);
719 payload->destroy = g_free;
720 payload->data = g_strdup(who);
721 trans->payload = payload;
723 sipe_dialog_free(dialog);
724 g_free(body);
725 g_free(hdr);
728 static void
729 accept_incoming_invite_conf(struct sipe_core_private *sipe_private,
730 gchar *focus_uri,
731 gboolean audio,
732 struct sipmsg *msg)
734 struct sip_session *session;
735 gchar *newTag = gentag();
736 const gchar *oldHeader = sipmsg_find_header(msg, "To");
737 gchar *newHeader;
739 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
740 g_free(newTag);
741 sipmsg_remove_header_now(msg, "To");
742 sipmsg_add_header_now(msg, "To", newHeader);
743 g_free(newHeader);
745 /* acknowledge invite */
746 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
748 /* add self to conf */
749 session = sipe_conf_create(sipe_private, NULL, focus_uri);
750 session->is_call = audio;
753 struct conf_accept_ctx {
754 gchar *focus_uri;
755 struct sipmsg *msg;
756 struct sipe_user_ask_ctx *ask_ctx;
759 static void
760 conf_accept_ctx_free(struct conf_accept_ctx *ctx)
762 g_return_if_fail(ctx != NULL);
764 sipmsg_free(ctx->msg);
765 g_free(ctx->focus_uri);
766 g_free(ctx);
769 static void
770 conf_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
772 sipe_private->sessions_to_accept =
773 g_slist_remove(sipe_private->sessions_to_accept, ctx);
775 accept_incoming_invite_conf(sipe_private, ctx->focus_uri, TRUE, ctx->msg);
776 conf_accept_ctx_free(ctx);
779 static void
780 conf_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
782 sipe_private->sessions_to_accept =
783 g_slist_remove(sipe_private->sessions_to_accept, ctx);
785 sip_transport_response(sipe_private,
786 ctx->msg,
787 603, "Decline", NULL);
789 conf_accept_ctx_free(ctx);
792 void
793 sipe_conf_cancel_unaccepted(struct sipe_core_private *sipe_private,
794 struct sipmsg *msg)
796 const gchar *callid1 = msg ? sipmsg_find_header(msg, "Call-ID") : NULL;
797 GSList *it = sipe_private->sessions_to_accept;
798 while (it) {
799 struct conf_accept_ctx *ctx = it->data;
800 const gchar *callid2 = NULL;
802 if (msg && ctx->msg)
803 callid2 = sipmsg_find_header(ctx->msg, "Call-ID");
805 if (sipe_strequal(callid1, callid2)) {
806 GSList *tmp;
808 if (ctx->msg)
809 sip_transport_response(sipe_private, ctx->msg,
810 487, "Request Terminated", NULL);
812 if (msg)
813 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
815 sipe_user_close_ask(ctx->ask_ctx);
816 conf_accept_ctx_free(ctx);
818 tmp = it;
819 it = it->next;
821 sipe_private->sessions_to_accept =
822 g_slist_delete_link(sipe_private->sessions_to_accept, tmp);
824 if (callid1)
825 break;
826 } else
827 it = it->next;
831 static void
832 ask_accept_voice_conference(struct sipe_core_private *sipe_private,
833 const gchar *focus_uri,
834 struct sipmsg *msg,
835 SipeUserAskCb accept_cb,
836 SipeUserAskCb decline_cb)
838 gchar **parts;
839 gchar *alias;
840 gchar *ask_msg;
841 gchar *novv_note;
842 struct conf_accept_ctx *ctx;
844 #ifdef HAVE_VV
845 novv_note = "";
846 #else
847 novv_note = _("\n\nAs this client was not compiled with voice call "
848 "support, if you accept, you will be able to contact "
849 "the other participants only via IM session.");
850 #endif
852 parts = g_strsplit(focus_uri, ";", 2);
853 alias = sipe_buddy_get_alias(sipe_private, parts[0]);
855 ask_msg = g_strdup_printf(_("%s wants to invite you "
856 "to the conference call%s"),
857 alias ? alias : parts[0], novv_note);
859 g_free(alias);
860 g_strfreev(parts);
862 ctx = g_new0(struct conf_accept_ctx, 1);
863 sipe_private->sessions_to_accept =
864 g_slist_append(sipe_private->sessions_to_accept, ctx);
866 ctx->focus_uri = g_strdup(focus_uri);
867 ctx->msg = msg ? sipmsg_copy(msg) : NULL;
868 ctx->ask_ctx = sipe_user_ask(sipe_private, ask_msg,
869 _("Accept"), accept_cb,
870 _("Decline"), decline_cb,
871 ctx);
873 g_free(ask_msg);
876 void
877 process_incoming_invite_conf(struct sipe_core_private *sipe_private,
878 struct sipmsg *msg)
880 sipe_xml *xn_conferencing = sipe_xml_parse(msg->body, msg->bodylen);
881 const sipe_xml *xn_focus_uri = sipe_xml_child(xn_conferencing, "focus-uri");
882 const sipe_xml *xn_audio = sipe_xml_child(xn_conferencing, "audio");
883 gchar *focus_uri = sipe_xml_data(xn_focus_uri);
884 gboolean audio = sipe_strequal(sipe_xml_attribute(xn_audio, "available"), "true");
886 sipe_xml_free(xn_conferencing);
888 SIPE_DEBUG_INFO("We have received invitation to Conference. Focus URI=%s", focus_uri);
890 if (audio) {
891 sip_transport_response(sipe_private, msg, 180, "Ringing", NULL);
892 ask_accept_voice_conference(sipe_private, focus_uri, msg,
893 (SipeUserAskCb) conf_accept_cb,
894 (SipeUserAskCb) conf_decline_cb);
896 } else {
897 accept_incoming_invite_conf(sipe_private, focus_uri, FALSE, msg);
900 g_free(focus_uri);
903 #ifdef HAVE_VV
905 static void
906 call_accept_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
908 struct sip_session *session;
909 session = sipe_session_find_conference(sipe_private, ctx->focus_uri);
911 sipe_private->sessions_to_accept =
912 g_slist_remove(sipe_private->sessions_to_accept, ctx);
914 if (session) {
915 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
916 session->chat_session);
919 conf_accept_ctx_free(ctx);
922 static void
923 call_decline_cb(struct sipe_core_private *sipe_private, struct conf_accept_ctx *ctx)
925 sipe_private->sessions_to_accept =
926 g_slist_remove(sipe_private->sessions_to_accept, ctx);
928 conf_accept_ctx_free(ctx);
931 #endif // HAVE_VV
933 void
934 sipe_process_conference(struct sipe_core_private *sipe_private,
935 struct sipmsg *msg)
937 sipe_xml *xn_conference_info;
938 const sipe_xml *node;
939 const sipe_xml *xn_subject;
940 const gchar *focus_uri;
941 struct sip_session *session;
942 gboolean just_joined = FALSE;
943 #ifdef HAVE_VV
944 gboolean audio_was_added = FALSE;
945 #endif
947 if (msg->response != 0 && msg->response != 200) return;
949 if (msg->bodylen == 0 || msg->body == NULL || !sipe_strequal(sipmsg_find_header(msg, "Event"), "conference")) return;
951 xn_conference_info = sipe_xml_parse(msg->body, msg->bodylen);
952 if (!xn_conference_info) return;
954 focus_uri = sipe_xml_attribute(xn_conference_info, "entity");
955 session = sipe_session_find_conference(sipe_private, focus_uri);
957 if (!session) {
958 SIPE_DEBUG_INFO("sipe_process_conference: unable to find conf session with focus=%s", focus_uri);
959 return;
962 if (!session->chat_session->backend) {
963 gchar *self = sip_uri_self(sipe_private);
965 /* create chat */
966 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
967 session->chat_session,
968 session->chat_session->title,
969 self);
970 just_joined = TRUE;
971 /* @TODO ask for full state (re-subscribe) if it was a partial one -
972 * this is to obtain full list of conference participants.
974 g_free(self);
977 /* subject */
978 if ((xn_subject = sipe_xml_child(xn_conference_info, "conference-description/subject"))) {
979 g_free(session->subject);
980 session->subject = sipe_xml_data(xn_subject);
981 sipe_backend_chat_topic(session->chat_session->backend, session->subject);
982 SIPE_DEBUG_INFO("sipe_process_conference: subject=%s", session->subject ? session->subject : "");
985 /* IM MCU URI */
986 if (!session->im_mcu_uri) {
987 for (node = sipe_xml_child(xn_conference_info, "conference-description/conf-uris/entry");
988 node;
989 node = sipe_xml_twin(node))
991 gchar *purpose = sipe_xml_data(sipe_xml_child(node, "purpose"));
993 if (sipe_strequal("chat", purpose)) {
994 g_free(purpose);
995 session->im_mcu_uri = sipe_xml_data(sipe_xml_child(node, "uri"));
996 SIPE_DEBUG_INFO("sipe_process_conference: im_mcu_uri=%s", session->im_mcu_uri);
997 break;
999 g_free(purpose);
1003 /* users */
1004 for (node = sipe_xml_child(xn_conference_info, "users/user"); node; node = sipe_xml_twin(node)) {
1005 const gchar *user_uri = sipe_xml_attribute(node, "entity");
1006 const gchar *state = sipe_xml_attribute(node, "state");
1007 gchar *role = sipe_xml_data(sipe_xml_child(node, "roles/entry"));
1008 gboolean is_operator = sipe_strequal(role, "presenter");
1009 gboolean is_in_im_mcu = FALSE;
1010 gchar *self = sip_uri_self(sipe_private);
1012 if (sipe_strequal("deleted", state)) {
1013 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1014 sipe_backend_chat_remove(session->chat_session->backend,
1015 user_uri);
1017 } else {
1018 /* endpoints */
1019 const sipe_xml *endpoint;
1020 for (endpoint = sipe_xml_child(node, "endpoint"); endpoint; endpoint = sipe_xml_twin(endpoint)) {
1021 const gchar *session_type;
1022 gchar *status = sipe_xml_data(sipe_xml_child(endpoint, "status"));
1023 gboolean connected = sipe_strequal("connected", status);
1024 g_free(status);
1026 if (!connected)
1027 continue;
1029 session_type = sipe_xml_attribute(endpoint, "session-type");
1031 if (sipe_strequal("chat", session_type)) {
1032 is_in_im_mcu = TRUE;
1033 if (!sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1034 sipe_backend_chat_add(session->chat_session->backend,
1035 user_uri,
1036 !just_joined && g_strcasecmp(user_uri, self));
1038 if (is_operator) {
1039 sipe_backend_chat_operator(session->chat_session->backend,
1040 user_uri);
1042 } else if (sipe_strequal("audio-video", session_type)) {
1043 #ifdef HAVE_VV
1044 if (!session->is_call)
1045 audio_was_added = TRUE;
1046 #endif
1049 if (!is_in_im_mcu) {
1050 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
1051 sipe_backend_chat_remove(session->chat_session->backend,
1052 user_uri);
1056 g_free(role);
1057 g_free(self);
1060 #ifdef HAVE_VV
1061 if (audio_was_added) {
1062 session->is_call = TRUE;
1063 ask_accept_voice_conference(sipe_private, focus_uri, NULL,
1064 (SipeUserAskCb) call_accept_cb,
1065 (SipeUserAskCb) call_decline_cb);
1067 #endif
1069 /* entity-view, locked */
1070 for (node = sipe_xml_child(xn_conference_info, "conference-view/entity-view");
1071 node;
1072 node = sipe_xml_twin(node)) {
1074 const sipe_xml *xn_type = sipe_xml_child(node, "entity-state/media/entry/type");
1075 gchar *tmp = NULL;
1076 if (xn_type && sipe_strequal("chat", (tmp = sipe_xml_data(xn_type)))) {
1077 const sipe_xml *xn_locked = sipe_xml_child(node, "entity-state/locked");
1078 if (xn_locked) {
1079 gchar *locked = sipe_xml_data(xn_locked);
1080 gboolean prev_locked = session->locked;
1081 session->locked = sipe_strequal(locked, "true");
1082 if (prev_locked && !session->locked) {
1083 sipe_user_present_info(sipe_private, session,
1084 _("This conference is no longer locked. Additional participants can now join."));
1086 if (!prev_locked && session->locked) {
1087 sipe_user_present_info(sipe_private, session,
1088 _("This conference is locked. Nobody else can join the conference while it is locked."));
1091 SIPE_DEBUG_INFO("sipe_process_conference: session->locked=%s",
1092 session->locked ? "TRUE" : "FALSE");
1093 g_free(locked);
1096 g_free(tmp);
1098 sipe_xml_free(xn_conference_info);
1100 if (session->im_mcu_uri) {
1101 struct sip_dialog *dialog = sipe_dialog_find(session, session->im_mcu_uri);
1102 if (!dialog) {
1103 dialog = sipe_dialog_add(session);
1105 dialog->callid = g_strdup(session->callid);
1106 dialog->with = g_strdup(session->im_mcu_uri);
1108 /* send INVITE to IM MCU */
1109 sipe_im_invite(sipe_private, session, dialog->with, NULL, NULL, NULL, FALSE);
1113 sipe_process_pending_invite_queue(sipe_private, session);
1116 void
1117 sipe_conf_immcu_closed(struct sipe_core_private *sipe_private,
1118 struct sip_session *session)
1120 sipe_user_present_info(sipe_private, session,
1121 _("You have been disconnected from this conference."));
1122 sipe_backend_chat_close(session->chat_session->backend);
1125 void
1126 conf_session_close(struct sipe_core_private *sipe_private,
1127 struct sip_session *session)
1129 if (session) {
1130 /* unsubscribe from focus */
1131 sipe_subscribe_conference(sipe_private, session, TRUE);
1133 if (session->focus_dialog) {
1134 /* send BYE to focus */
1135 sip_transport_bye(sipe_private, session->focus_dialog);
1140 void
1141 sipe_process_imdn(struct sipe_core_private *sipe_private,
1142 struct sipmsg *msg)
1144 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
1145 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
1146 static struct sip_session *session;
1147 sipe_xml *xn_imdn;
1148 const sipe_xml *node;
1149 gchar *message_id;
1150 gchar *message;
1152 session = sipe_session_find_chat_or_im(sipe_private, callid, with);
1153 if (!session) {
1154 SIPE_DEBUG_INFO("sipe_process_imdn: unable to find conf session with callid=%s", callid);
1155 g_free(with);
1156 return;
1159 xn_imdn = sipe_xml_parse(msg->body, msg->bodylen);
1160 message_id = sipe_xml_data(sipe_xml_child(xn_imdn, "message-id"));
1162 message = g_hash_table_lookup(session->conf_unconfirmed_messages, message_id);
1164 /* recipient */
1165 for (node = sipe_xml_child(xn_imdn, "recipient"); node; node = sipe_xml_twin(node)) {
1166 gchar *tmp = parse_from(sipe_xml_attribute(node, "uri"));
1167 gchar *uri = parse_from(tmp);
1168 sipe_user_present_message_undelivered(sipe_private, session, -1, -1, uri, message);
1169 g_free(tmp);
1170 g_free(uri);
1173 sipe_xml_free(xn_imdn);
1175 g_hash_table_remove(session->conf_unconfirmed_messages, message_id);
1176 SIPE_DEBUG_INFO("sipe_process_imdn: removed message %s from conf_unconfirmed_messages(count=%d)",
1177 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
1178 g_free(message_id);
1179 g_free(with);
1184 Local Variables:
1185 mode: c
1186 c-file-style: "bsd"
1187 indent-tabs-mode: t
1188 tab-width: 8
1189 End: