Fix #3198585: Extra line breaks
[siplcs.git] / src / core / sipe-conf.c
blob39d7f479c2ef6e0b93cba9e1a1e9c9a7324b05ae
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-chat.h"
40 #include "sipe-conf.h"
41 #include "sipe-core.h"
42 #include "sipe-core-private.h"
43 #include "sipe-dialog.h"
44 #include "sipe-im.h"
45 #ifdef HAVE_VV
46 #include "sipe-media.h"
47 #endif
48 #include "sipe-nls.h"
49 #include "sipe-session.h"
50 #include "sipe-subscriptions.h"
51 #include "sipe-user.h"
52 #include "sipe-utils.h"
53 #include "sipe-xml.h"
54 #include "sipe.h"
56 #ifdef HAVE_VV
57 #define ENTITY_VIEW_AUDIO_VIDEO "<msci:entity-view entity=\"audio-video\"/>"
58 #else
59 #define ENTITY_VIEW_AUDIO_VIDEO
60 #endif
62 /**
63 * Add Conference request to FocusFactory.
64 * @param focus_factory_uri (%s) Ex.: sip:bob7@boston.local;gruu;opaque=app:conf:focusfactory
65 * @param from (%s) Ex.: sip:bob7@boston.local
66 * @param request_id (%d) Ex.: 1094520
67 * @param conference_id (%s) Ex.: 8386E6AEAAA41E4AA6627BA76D43B6D1
68 * @param expiry_time (%s) Ex.: 2009-07-13T17:57:09Z , Default duration: 7 hours
70 #define SIPE_SEND_CONF_ADD \
71 "<?xml version=\"1.0\"?>"\
72 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" "\
73 "xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
74 "C3PVersion=\"1\" "\
75 "to=\"%s\" "\
76 "from=\"%s\" "\
77 "requestId=\"%d\">"\
78 "<addConference>"\
79 "<ci:conference-info xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\">"\
80 "<ci:conference-description>"\
81 "<ci:subject/>"\
82 "<msci:conference-id>%s</msci:conference-id>"\
83 "<msci:expiry-time>%s</msci:expiry-time>"\
84 "<msci:admission-policy>openAuthenticated</msci:admission-policy>"\
85 "</ci:conference-description>"\
86 "<msci:conference-view>"\
87 "<msci:entity-view entity=\"chat\"/>"\
88 ENTITY_VIEW_AUDIO_VIDEO \
89 "</msci:conference-view>"\
90 "</ci:conference-info>"\
91 "</addConference>"\
92 "</request>"
94 /**
95 * AddUser request to Focus.
96 * Params:
97 * focus_URI, from, request_id, focus_URI, from, endpoint_GUID
99 #define SIPE_SEND_CONF_ADD_USER \
100 "<?xml version=\"1.0\"?>"\
101 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
102 "C3PVersion=\"1\" "\
103 "to=\"%s\" "\
104 "from=\"%s\" "\
105 "requestId=\"%d\">"\
106 "<addUser>"\
107 "<conferenceKeys confEntity=\"%s\"/>"\
108 "<ci:user xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"%s\">"\
109 "<ci:roles>"\
110 "<ci:entry>attendee</ci:entry>"\
111 "</ci:roles>"\
112 "<ci:endpoint entity=\"{%s}\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\"/>"\
113 "</ci:user>"\
114 "</addUser>"\
115 "</request>"
118 * ModifyUserRoles request to Focus. Makes user a leader.
119 * @param focus_uri (%s)
120 * @param from (%s)
121 * @param request_id (%d)
122 * @param focus_uri (%s)
123 * @param who (%s)
125 #define SIPE_SEND_CONF_MODIFY_USER_ROLES \
126 "<?xml version=\"1.0\"?>"\
127 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
128 "C3PVersion=\"1\" "\
129 "to=\"%s\" "\
130 "from=\"%s\" "\
131 "requestId=\"%d\">"\
132 "<modifyUserRoles>"\
133 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
134 "<user-roles xmlns=\"urn:ietf:params:xml:ns:conference-info\">"\
135 "<entry>presenter</entry>"\
136 "</user-roles>"\
137 "</modifyUserRoles>"\
138 "</request>"
141 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
142 * @param focus_uri (%s)
143 * @param from (%s)
144 * @param request_id (%d)
145 * @param focus_uri (%s)
146 * @param locked (%s) "true" or "false" values applicable
148 #define SIPE_SEND_CONF_MODIFY_CONF_LOCK \
149 "<?xml version=\"1.0\"?>"\
150 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
151 "C3PVersion=\"1\" "\
152 "to=\"%s\" "\
153 "from=\"%s\" "\
154 "requestId=\"%d\">"\
155 "<modifyConferenceLock>"\
156 "<conferenceKeys confEntity=\"%s\"/>"\
157 "<locked>%s</locked>"\
158 "</modifyConferenceLock>"\
159 "</request>"
162 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
163 * @param focus_uri (%s)
164 * @param from (%s)
165 * @param request_id (%d)
166 * @param focus_uri (%s)
167 * @param who (%s)
169 #define SIPE_SEND_CONF_DELETE_USER \
170 "<?xml version=\"1.0\"?>"\
171 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
172 "C3PVersion=\"1\" "\
173 "to=\"%s\" "\
174 "from=\"%s\" "\
175 "requestId=\"%d\">"\
176 "<deleteUser>"\
177 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
178 "</deleteUser>"\
179 "</request>"
182 * Invite counterparty to join conference.
183 * @param focus_uri (%s)
184 * @param subject (%s) of conference
186 #define SIPE_SEND_CONF_INVITE \
187 "<Conferencing version=\"2.0\">"\
188 "<focus-uri>%s</focus-uri>"\
189 "<subject>%s</subject>"\
190 "<im available=\"true\">"\
191 "<first-im/>"\
192 "</im>"\
193 "</Conferencing>"
196 * Generates random GUID.
197 * This method is borrowed from pidgin's msnutils.c
199 static char *
200 rand_guid()
202 return g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
203 rand() % 0xAAFF + 0x1111,
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);
214 * @param expires not respected if set to negative value (E.g. -1)
216 static void
217 sipe_subscribe_conference(struct sipe_core_private *sipe_private,
218 struct sip_session *session,
219 gboolean expires)
221 sipe_subscribe(sipe_private,
222 session->chat_session->id,
223 "conference",
224 "application/conference-info+xml",
225 expires ? "Expires: 0\r\n" : NULL,
226 NULL,
227 NULL);
230 /** Invite us to the focus callback */
231 static gboolean
232 process_invite_conf_focus_response(struct sipe_core_private *sipe_private,
233 struct sipmsg *msg,
234 SIPE_UNUSED_PARAMETER struct transaction *trans)
236 struct sip_session *session = NULL;
237 char *focus_uri = parse_from(sipmsg_find_header(msg, "To"));
239 session = sipe_session_find_conference(sipe_private, focus_uri);
241 if (!session) {
242 SIPE_DEBUG_INFO("process_invite_conf_focus_response: unable to find conf session with focus=%s", focus_uri);
243 g_free(focus_uri);
244 return FALSE;
247 if (!session->focus_dialog) {
248 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: session's focus_dialog is NULL");
249 g_free(focus_uri);
250 return FALSE;
253 sipe_dialog_parse(session->focus_dialog, msg, TRUE);
255 if (msg->response >= 200) {
256 /* send ACK to focus */
257 session->focus_dialog->cseq = 0;
258 sip_transport_ack(sipe_private, session->focus_dialog);
259 session->focus_dialog->outgoing_invite = NULL;
260 session->focus_dialog->is_established = TRUE;
263 if (msg->response >= 400) {
264 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: INVITE response is not 200. Failed to join focus.");
265 /* @TODO notify user of failure to join focus */
266 sipe_session_remove(sipe_private, session);
267 g_free(focus_uri);
268 return FALSE;
269 } else if (msg->response == 200) {
270 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
271 const gchar *code = sipe_xml_attribute(xn_response, "code");
272 if (sipe_strequal(code, "success")) {
273 /* subscribe to focus */
274 sipe_subscribe_conference(sipe_private, session, FALSE);
275 #ifdef HAVE_VV
276 if (session->is_call)
277 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
278 session->chat_session);
279 #endif
281 sipe_xml_free(xn_response);
284 g_free(focus_uri);
285 return TRUE;
288 /** Create new session with Focus URI */
289 struct sip_session *
290 sipe_conf_create(struct sipe_core_private *sipe_private,
291 struct sipe_chat_session *chat_session,
292 const gchar *focus_uri)
294 gchar *hdr;
295 gchar *contact;
296 gchar *body;
297 gchar *self;
298 struct sip_session *session = sipe_session_add_chat(sipe_private,
299 chat_session,
300 FALSE,
301 focus_uri);
303 session->focus_dialog = g_new0(struct sip_dialog, 1);
304 session->focus_dialog->callid = gencallid();
305 session->focus_dialog->with = g_strdup(session->chat_session->id);
306 session->focus_dialog->endpoint_GUID = rand_guid();
307 session->focus_dialog->ourtag = gentag();
309 contact = get_contact(sipe_private);
310 hdr = g_strdup_printf(
311 "Supported: ms-sender\r\n"
312 "Contact: %s\r\n"
313 "Content-Type: application/cccp+xml\r\n",
314 contact);
315 g_free(contact);
317 /* @TODO put request_id to queue to further compare with incoming one */
318 /* focus_URI, from, request_id, focus_URI, from, endpoint_GUID */
319 self = sip_uri_self(sipe_private);
320 body = g_strdup_printf(
321 SIPE_SEND_CONF_ADD_USER,
322 session->focus_dialog->with,
323 self,
324 session->request_id++,
325 session->focus_dialog->with,
326 self,
327 session->focus_dialog->endpoint_GUID);
329 session->focus_dialog->outgoing_invite =
330 sip_transport_invite(sipe_private,
331 hdr,
332 body,
333 session->focus_dialog,
334 process_invite_conf_focus_response);
335 g_free(body);
336 g_free(hdr);
338 /* Rejoin existing session? */
339 if (chat_session) {
340 SIPE_DEBUG_INFO("sipe_conf_create: rejoin '%s' (%s)",
341 chat_session->title,
342 chat_session->id);
343 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
344 chat_session->backend,
345 self,
346 chat_session->title);
348 g_free(self);
350 return(session);
353 /** Modify User Role */
354 void
355 sipe_conf_modify_user_role(struct sipe_core_private *sipe_private,
356 struct sip_session *session,
357 const gchar* who)
359 gchar *hdr;
360 gchar *body;
361 gchar *self;
363 if (!session->focus_dialog || !session->focus_dialog->is_established) {
364 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_user_role: no dialog with focus, exiting.");
365 return;
368 hdr = g_strdup(
369 "Content-Type: application/cccp+xml\r\n");
371 /* @TODO put request_id to queue to further compare with incoming one */
372 self = sip_uri_self(sipe_private);
373 body = g_strdup_printf(
374 SIPE_SEND_CONF_MODIFY_USER_ROLES,
375 session->focus_dialog->with,
376 self,
377 session->request_id++,
378 session->focus_dialog->with,
379 who);
380 g_free(self);
382 sip_transport_info(sipe_private,
383 hdr,
384 body,
385 session->focus_dialog,
386 NULL);
387 g_free(body);
388 g_free(hdr);
392 * Check conference lock status
394 sipe_chat_lock_status sipe_core_chat_lock_status(struct sipe_core_public *sipe_public,
395 struct sipe_chat_session *chat_session)
397 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
398 sipe_chat_lock_status status = SIPE_CHAT_LOCK_STATUS_NOT_ALLOWED;
400 if (chat_session &&
401 (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE)) {
402 struct sip_session *session = sipe_session_find_chat(sipe_private,
403 chat_session);
404 if (session) {
405 gchar *self = sip_uri_self(sipe_private);
407 /* Only operators are allowed to change the lock status */
408 if (sipe_backend_chat_is_operator(chat_session->backend, self)) {
409 status = session->locked ?
410 SIPE_CHAT_LOCK_STATUS_LOCKED :
411 SIPE_CHAT_LOCK_STATUS_UNLOCKED;
414 g_free(self);
418 return(status);
422 * Modify Conference Lock
423 * Sends request to Focus.
424 * INFO method is a carrier of application/cccp+xml
426 void
427 sipe_core_chat_modify_lock(struct sipe_core_public *sipe_public,
428 struct sipe_chat_session *chat_session,
429 const gboolean locked)
431 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
432 gchar *hdr;
433 gchar *body;
434 gchar *self;
436 struct sip_session *session = sipe_session_find_chat(sipe_private,
437 chat_session);
439 if (!session) return;
440 if (!session->focus_dialog || !session->focus_dialog->is_established) {
441 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_conference_lock: no dialog with focus, exiting.");
442 return;
445 hdr = g_strdup(
446 "Content-Type: application/cccp+xml\r\n");
448 /* @TODO put request_id to queue to further compare with incoming one */
449 self = sip_uri_self(sipe_private);
450 body = g_strdup_printf(
451 SIPE_SEND_CONF_MODIFY_CONF_LOCK,
452 session->focus_dialog->with,
453 self,
454 session->request_id++,
455 session->focus_dialog->with,
456 locked ? "true" : "false");
457 g_free(self);
459 sip_transport_info(sipe_private,
460 hdr,
461 body,
462 session->focus_dialog,
463 NULL);
464 g_free(body);
465 g_free(hdr);
468 /** Modify Delete User */
469 void
470 sipe_conf_delete_user(struct sipe_core_private *sipe_private,
471 struct sip_session *session,
472 const gchar* who)
474 gchar *hdr;
475 gchar *body;
476 gchar *self;
478 if (!session->focus_dialog || !session->focus_dialog->is_established) {
479 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_delete_user: no dialog with focus, exiting.");
480 return;
483 hdr = g_strdup(
484 "Content-Type: application/cccp+xml\r\n");
486 /* @TODO put request_id to queue to further compare with incoming one */
487 self = sip_uri_self(sipe_private);
488 body = g_strdup_printf(
489 SIPE_SEND_CONF_DELETE_USER,
490 session->focus_dialog->with,
491 self,
492 session->request_id++,
493 session->focus_dialog->with,
494 who);
495 g_free(self);
497 sip_transport_info(sipe_private,
498 hdr,
499 body,
500 session->focus_dialog,
501 NULL);
502 g_free(body);
503 g_free(hdr);
506 /** Invite counterparty to join conference callback */
507 static gboolean
508 process_invite_conf_response(struct sipe_core_private *sipe_private,
509 struct sipmsg *msg,
510 SIPE_UNUSED_PARAMETER struct transaction *trans)
512 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
514 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
515 dialog->cseq = sipmsg_parse_cseq(msg);
516 dialog->with = parse_from(sipmsg_find_header(msg, "To"));
517 sipe_dialog_parse(dialog, msg, TRUE);
519 if (msg->response >= 200) {
520 /* send ACK to counterparty */
521 dialog->cseq--;
522 sip_transport_ack(sipe_private, dialog);
523 dialog->outgoing_invite = NULL;
524 dialog->is_established = TRUE;
527 if (msg->response >= 400) {
528 SIPE_DEBUG_INFO("process_invite_conf_response: INVITE response is not 200. Failed to invite %s.", dialog->with);
529 /* @TODO notify user of failure to invite counterparty */
530 sipe_dialog_free(dialog);
531 return FALSE;
534 if (msg->response >= 200) {
535 struct sip_session *session = sipe_session_find_im(sipe_private, dialog->with);
536 struct sip_dialog *im_dialog = sipe_dialog_find(session, dialog->with);
538 /* close IM session to counterparty */
539 if (im_dialog) {
540 sip_transport_bye(sipe_private, im_dialog);
541 sipe_dialog_remove(session, dialog->with);
545 sipe_dialog_free(dialog);
546 return TRUE;
550 * Invites counterparty to join conference.
552 void
553 sipe_invite_conf(struct sipe_core_private *sipe_private,
554 struct sip_session *session,
555 const gchar* who)
557 gchar *hdr;
558 gchar *contact;
559 gchar *body;
560 struct sip_dialog *dialog = NULL;
562 /* It will be short lived special dialog.
563 * Will not be stored in session.
565 dialog = g_new0(struct sip_dialog, 1);
566 dialog->callid = gencallid();
567 dialog->with = g_strdup(who);
568 dialog->ourtag = gentag();
570 contact = get_contact(sipe_private);
571 hdr = g_strdup_printf(
572 "Supported: ms-sender\r\n"
573 "Contact: %s\r\n"
574 "Content-Type: application/ms-conf-invite+xml\r\n",
575 contact);
576 g_free(contact);
578 body = g_strdup_printf(
579 SIPE_SEND_CONF_INVITE,
580 session->chat_session->id,
581 session->subject ? session->subject : ""
584 sip_transport_invite(sipe_private,
585 hdr,
586 body,
587 dialog,
588 process_invite_conf_response);
590 sipe_dialog_free(dialog);
591 g_free(body);
592 g_free(hdr);
595 /** Create conference callback */
596 static gboolean
597 process_conf_add_response(struct sipe_core_private *sipe_private,
598 struct sipmsg *msg,
599 struct transaction *trans)
601 if (msg->response >= 400) {
602 SIPE_DEBUG_INFO_NOFORMAT("process_conf_add_response: SERVICE response is not 200. Failed to create conference.");
603 /* @TODO notify user of failure to create conference */
604 return FALSE;
606 if (msg->response == 200) {
607 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
608 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code")))
610 gchar *who = trans->payload->data;
611 const sipe_xml *xn_conference_info = sipe_xml_child(xn_response, "addConference/conference-info");
612 struct sip_session *session = sipe_conf_create(sipe_private,
613 NULL,
614 sipe_xml_attribute(xn_conference_info,
615 "entity"));
617 SIPE_DEBUG_INFO("process_conf_add_response: session->focus_uri=%s",
618 session->chat_session->id);
620 session->pending_invite_queue = slist_insert_unique_sorted(
621 session->pending_invite_queue, g_strdup(who), (GCompareFunc)strcmp);
623 sipe_xml_free(xn_response);
626 return TRUE;
630 * Creates conference.
632 void
633 sipe_conf_add(struct sipe_core_private *sipe_private,
634 const gchar* who)
636 gchar *hdr;
637 gchar *conference_id;
638 gchar *contact;
639 gchar *body;
640 gchar *self;
641 struct transaction *trans;
642 struct sip_dialog *dialog = NULL;
643 time_t expiry = time(NULL) + 7*60*60; /* 7 hours */
644 char *expiry_time;
645 struct transaction_payload *payload;
647 contact = get_contact(sipe_private);
648 hdr = g_strdup_printf(
649 "Supported: ms-sender\r\n"
650 "Contact: %s\r\n"
651 "Content-Type: application/cccp+xml\r\n",
652 contact);
653 g_free(contact);
655 expiry_time = sipe_utils_time_to_str(expiry);
656 self = sip_uri_self(sipe_private);
657 conference_id = genconfid();
658 body = g_strdup_printf(
659 SIPE_SEND_CONF_ADD,
660 sipe_private->focus_factory_uri,
661 self,
662 rand(),
663 conference_id,
664 expiry_time);
665 g_free(self);
666 g_free(conference_id);
667 g_free(expiry_time);
669 trans = sip_transport_service(sipe_private,
670 sipe_private->focus_factory_uri,
671 hdr,
672 body,
673 process_conf_add_response);
675 payload = g_new0(struct transaction_payload, 1);
676 payload->destroy = g_free;
677 payload->data = g_strdup(who);
678 trans->payload = payload;
680 sipe_dialog_free(dialog);
681 g_free(body);
682 g_free(hdr);
685 void
686 process_incoming_invite_conf(struct sipe_core_private *sipe_private,
687 struct sipmsg *msg)
689 sipe_xml *xn_conferencing = sipe_xml_parse(msg->body, msg->bodylen);
690 const sipe_xml *xn_focus_uri = sipe_xml_child(xn_conferencing, "focus-uri");
691 const sipe_xml *xn_audio = sipe_xml_child(xn_conferencing, "audio");
692 gchar *focus_uri = sipe_xml_data(xn_focus_uri);
693 gboolean audio = sipe_strequal(sipe_xml_attribute(xn_audio, "available"), "true");
694 gchar *newTag = gentag();
695 const gchar *oldHeader = sipmsg_find_header(msg, "To");
696 gchar *newHeader;
697 struct sip_session *session;
699 sipe_xml_free(xn_conferencing);
701 /* send OK */
702 SIPE_DEBUG_INFO("We have received invitation to Conference. Focus URI=%s", focus_uri);
704 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
705 g_free(newTag);
706 sipmsg_remove_header_now(msg, "To");
707 sipmsg_add_header_now(msg, "To", newHeader);
708 g_free(newHeader);
710 /* acknowledge invite */
711 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
713 /* add self to conf */
714 session = sipe_conf_create(sipe_private, NULL, focus_uri);
715 session->is_call = audio;
716 g_free(focus_uri);
719 void
720 sipe_process_conference(struct sipe_core_private *sipe_private,
721 struct sipmsg *msg)
723 sipe_xml *xn_conference_info;
724 const sipe_xml *node;
725 const sipe_xml *xn_subject;
726 const gchar *focus_uri;
727 struct sip_session *session;
728 gboolean just_joined = FALSE;
729 #ifdef HAVE_VV
730 gboolean audio_was_added = FALSE;
731 #endif
733 if (msg->response != 0 && msg->response != 200) return;
735 if (msg->bodylen == 0 || msg->body == NULL || !sipe_strequal(sipmsg_find_header(msg, "Event"), "conference")) return;
737 xn_conference_info = sipe_xml_parse(msg->body, msg->bodylen);
738 if (!xn_conference_info) return;
740 focus_uri = sipe_xml_attribute(xn_conference_info, "entity");
741 session = sipe_session_find_conference(sipe_private, focus_uri);
743 if (!session) {
744 SIPE_DEBUG_INFO("sipe_process_conference: unable to find conf session with focus=%s", focus_uri);
745 return;
748 if (!session->chat_session->backend) {
749 gchar *self = sip_uri_self(sipe_private);
751 /* create chat */
752 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
753 session->chat_session,
754 session->chat_session->title,
755 self);
756 just_joined = TRUE;
757 /* @TODO ask for full state (re-subscribe) if it was a partial one -
758 * this is to obtain full list of conference participants.
760 g_free(self);
763 /* subject */
764 if ((xn_subject = sipe_xml_child(xn_conference_info, "conference-description/subject"))) {
765 g_free(session->subject);
766 session->subject = sipe_xml_data(xn_subject);
767 sipe_backend_chat_topic(session->chat_session->backend, session->subject);
768 SIPE_DEBUG_INFO("sipe_process_conference: subject=%s", session->subject ? session->subject : "");
771 /* IM MCU URI */
772 if (!session->im_mcu_uri) {
773 for (node = sipe_xml_child(xn_conference_info, "conference-description/conf-uris/entry");
774 node;
775 node = sipe_xml_twin(node))
777 gchar *purpose = sipe_xml_data(sipe_xml_child(node, "purpose"));
779 if (sipe_strequal("chat", purpose)) {
780 g_free(purpose);
781 session->im_mcu_uri = sipe_xml_data(sipe_xml_child(node, "uri"));
782 SIPE_DEBUG_INFO("sipe_process_conference: im_mcu_uri=%s", session->im_mcu_uri);
783 break;
785 g_free(purpose);
789 /* users */
790 for (node = sipe_xml_child(xn_conference_info, "users/user"); node; node = sipe_xml_twin(node)) {
791 const gchar *user_uri = sipe_xml_attribute(node, "entity");
792 const gchar *state = sipe_xml_attribute(node, "state");
793 gchar *role = sipe_xml_data(sipe_xml_child(node, "roles/entry"));
794 gboolean is_operator = sipe_strequal(role, "presenter");
795 gboolean is_in_im_mcu = FALSE;
796 gchar *self = sip_uri_self(sipe_private);
798 if (sipe_strequal("deleted", state)) {
799 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
800 sipe_backend_chat_remove(session->chat_session->backend,
801 user_uri);
803 } else {
804 /* endpoints */
805 const sipe_xml *endpoint;
806 for (endpoint = sipe_xml_child(node, "endpoint"); endpoint; endpoint = sipe_xml_twin(endpoint)) {
807 const gchar *session_type;
808 gchar *status = sipe_xml_data(sipe_xml_child(endpoint, "status"));
809 gboolean connected = sipe_strequal("connected", status);
810 g_free(status);
812 if (!connected)
813 continue;
815 session_type = sipe_xml_attribute(endpoint, "session-type");
817 if (sipe_strequal("chat", session_type)) {
818 is_in_im_mcu = TRUE;
819 if (!sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
820 sipe_backend_chat_add(session->chat_session->backend,
821 user_uri,
822 !just_joined && g_strcasecmp(user_uri, self));
824 if (is_operator) {
825 sipe_backend_chat_operator(session->chat_session->backend,
826 user_uri);
828 } else if (sipe_strequal("audio-video", session_type)) {
829 #ifdef HAVE_VV
830 if (!session->is_call)
831 audio_was_added = TRUE;
832 #endif
835 if (!is_in_im_mcu) {
836 if (sipe_backend_chat_find(session->chat_session->backend, user_uri)) {
837 sipe_backend_chat_remove(session->chat_session->backend,
838 user_uri);
842 g_free(role);
843 g_free(self);
846 #ifdef HAVE_VV
847 if (audio_was_added) {
848 session->is_call = TRUE;
849 sipe_core_media_connect_conference(SIPE_CORE_PUBLIC,
850 session->chat_session);
852 #endif
854 /* entity-view, locked */
855 for (node = sipe_xml_child(xn_conference_info, "conference-view/entity-view");
856 node;
857 node = sipe_xml_twin(node)) {
859 const sipe_xml *xn_type = sipe_xml_child(node, "entity-state/media/entry/type");
860 gchar *tmp = NULL;
861 if (xn_type && sipe_strequal("chat", (tmp = sipe_xml_data(xn_type)))) {
862 const sipe_xml *xn_locked = sipe_xml_child(node, "entity-state/locked");
863 if (xn_locked) {
864 gchar *locked = sipe_xml_data(xn_locked);
865 gboolean prev_locked = session->locked;
866 session->locked = sipe_strequal(locked, "true");
867 if (prev_locked && !session->locked) {
868 sipe_user_present_info(sipe_private, session,
869 _("This conference is no longer locked. Additional participants can now join."));
871 if (!prev_locked && session->locked) {
872 sipe_user_present_info(sipe_private, session,
873 _("This conference is locked. Nobody else can join the conference while it is locked."));
876 SIPE_DEBUG_INFO("sipe_process_conference: session->locked=%s",
877 session->locked ? "TRUE" : "FALSE");
878 g_free(locked);
881 g_free(tmp);
883 sipe_xml_free(xn_conference_info);
885 if (session->im_mcu_uri) {
886 struct sip_dialog *dialog = sipe_dialog_find(session, session->im_mcu_uri);
887 if (!dialog) {
888 dialog = sipe_dialog_add(session);
890 dialog->callid = g_strdup(session->callid);
891 dialog->with = g_strdup(session->im_mcu_uri);
893 /* send INVITE to IM MCU */
894 sipe_im_invite(sipe_private, session, dialog->with, NULL, NULL, NULL, FALSE);
898 sipe_process_pending_invite_queue(sipe_private, session);
901 void
902 sipe_conf_immcu_closed(struct sipe_core_private *sipe_private,
903 struct sip_session *session)
905 sipe_user_present_info(sipe_private, session,
906 _("You have been disconnected from this conference."));
907 sipe_backend_chat_close(session->chat_session->backend);
910 void
911 conf_session_close(struct sipe_core_private *sipe_private,
912 struct sip_session *session)
914 if (session) {
915 /* unsubscribe from focus */
916 sipe_subscribe_conference(sipe_private, session, TRUE);
918 if (session->focus_dialog) {
919 /* send BYE to focus */
920 sip_transport_bye(sipe_private, session->focus_dialog);
925 void
926 sipe_process_imdn(struct sipe_core_private *sipe_private,
927 struct sipmsg *msg)
929 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
930 const gchar *callid = sipmsg_find_header(msg, "Call-ID");
931 static struct sip_session *session;
932 sipe_xml *xn_imdn;
933 const sipe_xml *node;
934 gchar *message_id;
935 gchar *message;
937 session = sipe_session_find_chat_or_im(sipe_private, callid, with);
938 if (!session) {
939 SIPE_DEBUG_INFO("sipe_process_imdn: unable to find conf session with callid=%s", callid);
940 g_free(with);
941 return;
944 xn_imdn = sipe_xml_parse(msg->body, msg->bodylen);
945 message_id = sipe_xml_data(sipe_xml_child(xn_imdn, "message-id"));
947 message = g_hash_table_lookup(session->conf_unconfirmed_messages, message_id);
949 /* recipient */
950 for (node = sipe_xml_child(xn_imdn, "recipient"); node; node = sipe_xml_twin(node)) {
951 gchar *tmp = parse_from(sipe_xml_attribute(node, "uri"));
952 gchar *uri = parse_from(tmp);
953 sipe_user_present_message_undelivered(sipe_private, session, -1, -1, uri, message);
954 g_free(tmp);
955 g_free(uri);
958 sipe_xml_free(xn_imdn);
960 g_hash_table_remove(session->conf_unconfirmed_messages, message_id);
961 SIPE_DEBUG_INFO("sipe_process_imdn: removed message %s from conf_unconfirmed_messages(count=%d)",
962 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
963 g_free(message_id);
964 g_free(with);
969 Local Variables:
970 mode: c
971 c-file-style: "bsd"
972 indent-tabs-mode: t
973 tab-width: 8
974 End: