l10n: Updates to Portuguese (Brazilian) (pt_BR) translation
[siplcs.git] / src / core / sipe-conf.c
blobbca7b9d4f369fba0ecdf3fa03e3b085a5dc1a328
1 /**
2 * @file sipe-conf.c
4 * pidgin-sipe
6 * Copyright (C) 2010 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 "conversation.h"
37 #include "sipe-common.h"
38 #include "sipmsg.h"
39 #include "sip-sec.h"
40 #include "sipe-backend.h"
41 #include "sipe-chat.h"
42 #include "sipe-conf.h"
43 #include "sipe-dialog.h"
44 #include "sipe-nls.h"
45 #include "sipe-session.h"
46 #include "sipe-utils.h"
47 #include "sipe-xml.h"
48 #include "sipe.h"
50 /**
51 * Add Conference request to FocusFactory.
52 * @param focus_factory_uri (%s) Ex.: sip:bob7@boston.local;gruu;opaque=app:conf:focusfactory
53 * @param from (%s) Ex.: sip:bob7@boston.local
54 * @param request_id (%d) Ex.: 1094520
55 * @param conference_id (%s) Ex.: 8386E6AEAAA41E4AA6627BA76D43B6D1
56 * @param expiry_time (%s) Ex.: 2009-07-13T17:57:09Z , Default duration: 7 hours
58 #define SIPE_SEND_CONF_ADD \
59 "<?xml version=\"1.0\"?>"\
60 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" "\
61 "xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
62 "C3PVersion=\"1\" "\
63 "to=\"%s\" "\
64 "from=\"%s\" "\
65 "requestId=\"%d\">"\
66 "<addConference>"\
67 "<ci:conference-info xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\">"\
68 "<ci:conference-description>"\
69 "<ci:subject/>"\
70 "<msci:conference-id>%s</msci:conference-id>"\
71 "<msci:expiry-time>%s</msci:expiry-time>"\
72 "<msci:admission-policy>openAuthenticated</msci:admission-policy>"\
73 "</ci:conference-description>"\
74 "<msci:conference-view>"\
75 "<msci:entity-view entity=\"chat\"/>"\
76 "</msci:conference-view>"\
77 "</ci:conference-info>"\
78 "</addConference>"\
79 "</request>"
81 /**
82 * AddUser request to Focus.
83 * Params:
84 * focus_URI, from, request_id, focus_URI, from, endpoint_GUID
86 #define SIPE_SEND_CONF_ADD_USER \
87 "<?xml version=\"1.0\"?>"\
88 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
89 "C3PVersion=\"1\" "\
90 "to=\"%s\" "\
91 "from=\"%s\" "\
92 "requestId=\"%d\">"\
93 "<addUser>"\
94 "<conferenceKeys confEntity=\"%s\"/>"\
95 "<ci:user xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"%s\">"\
96 "<ci:roles>"\
97 "<ci:entry>attendee</ci:entry>"\
98 "</ci:roles>"\
99 "<ci:endpoint entity=\"{%s}\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\"/>"\
100 "</ci:user>"\
101 "</addUser>"\
102 "</request>"
105 * ModifyUserRoles request to Focus. Makes user a leader.
106 * @param focus_uri (%s)
107 * @param from (%s)
108 * @param request_id (%d)
109 * @param focus_uri (%s)
110 * @param who (%s)
112 #define SIPE_SEND_CONF_MODIFY_USER_ROLES \
113 "<?xml version=\"1.0\"?>"\
114 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
115 "C3PVersion=\"1\" "\
116 "to=\"%s\" "\
117 "from=\"%s\" "\
118 "requestId=\"%d\">"\
119 "<modifyUserRoles>"\
120 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
121 "<user-roles xmlns=\"urn:ietf:params:xml:ns:conference-info\">"\
122 "<entry>presenter</entry>"\
123 "</user-roles>"\
124 "</modifyUserRoles>"\
125 "</request>"
128 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
129 * @param focus_uri (%s)
130 * @param from (%s)
131 * @param request_id (%d)
132 * @param focus_uri (%s)
133 * @param locked (%s) "true" or "false" values applicable
135 #define SIPE_SEND_CONF_MODIFY_CONF_LOCK \
136 "<?xml version=\"1.0\"?>"\
137 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
138 "C3PVersion=\"1\" "\
139 "to=\"%s\" "\
140 "from=\"%s\" "\
141 "requestId=\"%d\">"\
142 "<modifyConferenceLock>"\
143 "<conferenceKeys confEntity=\"%s\"/>"\
144 "<locked>%s</locked>"\
145 "</modifyConferenceLock>"\
146 "</request>"
149 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
150 * @param focus_uri (%s)
151 * @param from (%s)
152 * @param request_id (%d)
153 * @param focus_uri (%s)
154 * @param who (%s)
156 #define SIPE_SEND_CONF_DELETE_USER \
157 "<?xml version=\"1.0\"?>"\
158 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
159 "C3PVersion=\"1\" "\
160 "to=\"%s\" "\
161 "from=\"%s\" "\
162 "requestId=\"%d\">"\
163 "<deleteUser>"\
164 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
165 "</deleteUser>"\
166 "</request>"
169 * Invite counterparty to join conference.
170 * @param focus_uri (%s)
171 * @param subject (%s) of conference
173 #define SIPE_SEND_CONF_INVITE \
174 "<Conferencing version=\"2.0\">"\
175 "<focus-uri>%s</focus-uri>"\
176 "<subject>%s</subject>"\
177 "<im available=\"true\">"\
178 "<first-im/>"\
179 "</im>"\
180 "</Conferencing>"
183 * Generates random GUID.
184 * This method is borrowed from pidgin's msnutils.c
186 static char *
187 rand_guid()
189 return g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
190 rand() % 0xAAFF + 0x1111,
191 rand() % 0xAAFF + 0x1111,
192 rand() % 0xAAFF + 0x1111,
193 rand() % 0xAAFF + 0x1111,
194 rand() % 0xAAFF + 0x1111,
195 rand() % 0xAAFF + 0x1111,
196 rand() % 0xAAFF + 0x1111,
197 rand() % 0xAAFF + 0x1111);
201 * @param expires not respected if set to negative value (E.g. -1)
203 static void
204 sipe_subscribe_conference(struct sipe_account_data *sip,
205 struct sip_session *session,
206 const int expires)
208 gchar *expires_hdr = (expires >= 0) ? g_strdup_printf("Expires: %d\r\n", expires) : g_strdup("");
209 gchar *contact = get_contact(sip);
210 gchar *hdr = g_strdup_printf(
211 "Event: conference\r\n"
212 "%s"
213 "Accept: application/conference-info+xml\r\n"
214 "Supported: com.microsoft.autoextend\r\n"
215 "Supported: ms-benotify\r\n"
216 "Proxy-Require: ms-benotify\r\n"
217 "Supported: ms-piggyback-first-notify\r\n"
218 "Contact: %s\r\n",
219 expires_hdr,
220 contact);
221 g_free(expires_hdr);
222 g_free(contact);
224 send_sip_request(sip->gc,
225 "SUBSCRIBE",
226 session->focus_uri,
227 session->focus_uri,
228 hdr,
230 NULL,
231 process_subscribe_response);
232 g_free(hdr);
235 /** Invite us to the focus callback */
236 static gboolean
237 process_invite_conf_focus_response(struct sipe_account_data *sip,
238 struct sipmsg *msg,
239 SIPE_UNUSED_PARAMETER struct transaction *trans)
241 struct sip_session *session = NULL;
242 char *focus_uri = parse_from(sipmsg_find_header(msg, "To"));
244 session = sipe_session_find_conference(sip, focus_uri);
246 if (!session) {
247 SIPE_DEBUG_INFO("process_invite_conf_focus_response: unable to find conf session with focus=%s", focus_uri);
248 g_free(focus_uri);
249 return FALSE;
252 if (!session->focus_dialog) {
253 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: session's focus_dialog is NULL");
254 g_free(focus_uri);
255 return FALSE;
258 sipe_dialog_parse(session->focus_dialog, msg, TRUE);
260 if (msg->response >= 200) {
261 /* send ACK to focus */
262 session->focus_dialog->cseq = 0;
263 send_sip_request(sip->gc, "ACK", session->focus_dialog->with, session->focus_dialog->with, NULL, NULL, session->focus_dialog, NULL);
264 session->focus_dialog->outgoing_invite = NULL;
265 session->focus_dialog->is_established = TRUE;
268 if (msg->response >= 400) {
269 SIPE_DEBUG_INFO_NOFORMAT("process_invite_conf_focus_response: INVITE response is not 200. Failed to join focus.");
270 /* @TODO notify user of failure to join focus */
271 sipe_session_remove(sip, session);
272 g_free(focus_uri);
273 return FALSE;
274 } else if (msg->response == 200) {
275 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
276 const gchar *code = sipe_xml_attribute(xn_response, "code");
277 if (sipe_strequal(code, "success")) {
278 /* subscribe to focus */
279 sipe_subscribe_conference(sip, session, -1);
281 sipe_xml_free(xn_response);
284 g_free(focus_uri);
285 return TRUE;
288 /** Invite us to the focus */
289 void
290 sipe_invite_conf_focus(struct sipe_account_data *sip,
291 struct sip_session *session)
293 gchar *hdr;
294 gchar *contact;
295 gchar *body;
296 gchar *self;
298 if (session->focus_dialog && session->focus_dialog->is_established) {
299 SIPE_DEBUG_INFO("session with %s already has a dialog open", session->focus_uri);
300 return;
303 if(!session->focus_dialog) {
304 session->focus_dialog = g_new0(struct sip_dialog, 1);
305 session->focus_dialog->callid = gencallid();
306 session->focus_dialog->with = g_strdup(session->focus_uri);
307 session->focus_dialog->endpoint_GUID = rand_guid();
309 if (!(session->focus_dialog->ourtag)) {
310 session->focus_dialog->ourtag = gentag();
313 contact = get_contact(sip);
314 hdr = g_strdup_printf(
315 "Supported: ms-sender\r\n"
316 "Contact: %s\r\n"
317 "Content-Type: application/cccp+xml\r\n",
318 contact);
319 g_free(contact);
321 /* @TODO put request_id to queue to further compare with incoming one */
322 /* focus_URI, from, request_id, focus_URI, from, endpoint_GUID */
323 self = sip_uri_self(sip);
324 body = g_strdup_printf(
325 SIPE_SEND_CONF_ADD_USER,
326 session->focus_dialog->with,
327 self,
328 session->request_id++,
329 session->focus_dialog->with,
330 self,
331 session->focus_dialog->endpoint_GUID);
332 g_free(self);
334 session->focus_dialog->outgoing_invite = send_sip_request(sip->gc,
335 "INVITE",
336 session->focus_dialog->with,
337 session->focus_dialog->with,
338 hdr,
339 body,
340 session->focus_dialog,
341 process_invite_conf_focus_response);
342 g_free(body);
343 g_free(hdr);
346 /** Modify User Role */
347 void
348 sipe_conf_modify_user_role(struct sipe_account_data *sip,
349 struct sip_session *session,
350 const gchar* who)
352 gchar *hdr;
353 gchar *body;
354 gchar *self;
356 if (!session->focus_dialog || !session->focus_dialog->is_established) {
357 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_user_role: no dialog with focus, exiting.");
358 return;
361 hdr = g_strdup(
362 "Content-Type: application/cccp+xml\r\n");
364 /* @TODO put request_id to queue to further compare with incoming one */
365 self = sip_uri_self(sip);
366 body = g_strdup_printf(
367 SIPE_SEND_CONF_MODIFY_USER_ROLES,
368 session->focus_dialog->with,
369 self,
370 session->request_id++,
371 session->focus_dialog->with,
372 who);
373 g_free(self);
375 send_sip_request(sip->gc,
376 "INFO",
377 session->focus_dialog->with,
378 session->focus_dialog->with,
379 hdr,
380 body,
381 session->focus_dialog,
382 NULL);
383 g_free(body);
384 g_free(hdr);
387 /** Modify Conference Lock */
388 void
389 sipe_conf_modify_conference_lock(struct sipe_account_data *sip,
390 struct sip_session *session,
391 const gboolean locked)
393 gchar *hdr;
394 gchar *body;
395 gchar *self;
397 if (!session->focus_dialog || !session->focus_dialog->is_established) {
398 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_modify_conference_lock: no dialog with focus, exiting.");
399 return;
402 hdr = g_strdup(
403 "Content-Type: application/cccp+xml\r\n");
405 /* @TODO put request_id to queue to further compare with incoming one */
406 self = sip_uri_self(sip);
407 body = g_strdup_printf(
408 SIPE_SEND_CONF_MODIFY_CONF_LOCK,
409 session->focus_dialog->with,
410 self,
411 session->request_id++,
412 session->focus_dialog->with,
413 locked ? "true" : "false");
414 g_free(self);
416 send_sip_request(sip->gc,
417 "INFO",
418 session->focus_dialog->with,
419 session->focus_dialog->with,
420 hdr,
421 body,
422 session->focus_dialog,
423 NULL);
424 g_free(body);
425 g_free(hdr);
428 /** Modify Delete User */
429 void
430 sipe_conf_delete_user(struct sipe_account_data *sip,
431 struct sip_session *session,
432 const gchar* who)
434 gchar *hdr;
435 gchar *body;
436 gchar *self;
438 if (!session->focus_dialog || !session->focus_dialog->is_established) {
439 SIPE_DEBUG_INFO_NOFORMAT("sipe_conf_delete_user: no dialog with focus, exiting.");
440 return;
443 hdr = g_strdup(
444 "Content-Type: application/cccp+xml\r\n");
446 /* @TODO put request_id to queue to further compare with incoming one */
447 self = sip_uri_self(sip);
448 body = g_strdup_printf(
449 SIPE_SEND_CONF_DELETE_USER,
450 session->focus_dialog->with,
451 self,
452 session->request_id++,
453 session->focus_dialog->with,
454 who);
455 g_free(self);
457 send_sip_request(sip->gc,
458 "INFO",
459 session->focus_dialog->with,
460 session->focus_dialog->with,
461 hdr,
462 body,
463 session->focus_dialog,
464 NULL);
465 g_free(body);
466 g_free(hdr);
469 /** Invite counterparty to join conference callback */
470 static gboolean
471 process_invite_conf_response(struct sipe_account_data *sip,
472 struct sipmsg *msg,
473 SIPE_UNUSED_PARAMETER struct transaction *trans)
475 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
477 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
478 dialog->cseq = parse_cseq(sipmsg_find_header(msg, "CSeq"));
479 dialog->with = parse_from(sipmsg_find_header(msg, "To"));
480 sipe_dialog_parse(dialog, msg, TRUE);
482 if (msg->response >= 200) {
483 /* send ACK to counterparty */
484 dialog->cseq--;
485 send_sip_request(sip->gc, "ACK", dialog->with, dialog->with, NULL, NULL, dialog, NULL);
486 dialog->outgoing_invite = NULL;
487 dialog->is_established = TRUE;
490 if (msg->response >= 400) {
491 SIPE_DEBUG_INFO("process_invite_conf_response: INVITE response is not 200. Failed to invite %s.", dialog->with);
492 /* @TODO notify user of failure to invite counterparty */
493 sipe_dialog_free(dialog);
494 return FALSE;
497 if (msg->response >= 200) {
498 struct sip_session *session = sipe_session_find_im(sip, dialog->with);
499 struct sip_dialog *im_dialog = sipe_dialog_find(session, dialog->with);
501 /* close IM session to counterparty */
502 if (im_dialog) {
503 send_sip_request(sip->gc, "BYE", im_dialog->with, im_dialog->with, NULL, NULL, im_dialog, NULL);
504 sipe_dialog_remove(session, dialog->with);
508 sipe_dialog_free(dialog);
509 return TRUE;
513 * Invites counterparty to join conference.
515 void
516 sipe_invite_conf(struct sipe_account_data *sip,
517 struct sip_session *session,
518 const gchar* who)
520 gchar *hdr;
521 gchar *contact;
522 gchar *body;
523 struct sip_dialog *dialog = NULL;
525 /* It will be short lived special dialog.
526 * Will not be stored in session.
528 dialog = g_new0(struct sip_dialog, 1);
529 dialog->callid = gencallid();
530 dialog->with = g_strdup(who);
531 dialog->ourtag = gentag();
533 contact = get_contact(sip);
534 hdr = g_strdup_printf(
535 "Supported: ms-sender\r\n"
536 "Contact: %s\r\n"
537 "Content-Type: application/ms-conf-invite+xml\r\n",
538 contact);
539 g_free(contact);
541 body = g_strdup_printf(
542 SIPE_SEND_CONF_INVITE,
543 session->focus_uri,
544 session->subject ? session->subject : ""
547 send_sip_request( sip->gc,
548 "INVITE",
549 dialog->with,
550 dialog->with,
551 hdr,
552 body,
553 dialog,
554 process_invite_conf_response);
556 sipe_dialog_free(dialog);
557 g_free(body);
558 g_free(hdr);
561 /** Create conference callback */
562 static gboolean
563 process_conf_add_response(struct sipe_account_data *sip,
564 struct sipmsg *msg,
565 struct transaction *trans)
567 if (msg->response >= 400) {
568 SIPE_DEBUG_INFO_NOFORMAT("process_conf_add_response: SERVICE response is not 200. Failed to create conference.");
569 /* @TODO notify user of failure to create conference */
570 return FALSE;
572 if (msg->response == 200) {
573 sipe_xml *xn_response = sipe_xml_parse(msg->body, msg->bodylen);
574 if (sipe_strequal("success", sipe_xml_attribute(xn_response, "code")))
576 gchar *who = trans->payload->data;
577 struct sip_session *session;
578 const sipe_xml *xn_conference_info = sipe_xml_child(xn_response, "addConference/conference-info");
580 session = sipe_session_add_chat(sip);
581 session->is_multiparty = FALSE;
582 session->focus_uri = g_strdup(sipe_xml_attribute(xn_conference_info, "entity"));
583 SIPE_DEBUG_INFO("process_conf_add_response: session->focus_uri=%s",
584 session->focus_uri ? session->focus_uri : "");
586 session->pending_invite_queue = slist_insert_unique_sorted(
587 session->pending_invite_queue, g_strdup(who), (GCompareFunc)strcmp);
589 /* add self to conf */
590 sipe_invite_conf_focus(sip, session);
592 sipe_xml_free(xn_response);
595 return TRUE;
599 * Creates conference.
601 void
602 sipe_conf_add(struct sipe_account_data *sip,
603 const gchar* who)
605 gchar *hdr;
606 gchar *conference_id;
607 gchar *contact;
608 gchar *body;
609 gchar *self;
610 struct transaction *trans;
611 struct sip_dialog *dialog = NULL;
612 time_t expiry = time(NULL) + 7*60*60; /* 7 hours */
613 char *expiry_time;
614 struct transaction_payload *payload;
616 contact = get_contact(sip);
617 hdr = g_strdup_printf(
618 "Supported: ms-sender\r\n"
619 "Contact: %s\r\n"
620 "Content-Type: application/cccp+xml\r\n",
621 contact);
622 g_free(contact);
624 expiry_time = sipe_utils_time_to_str(expiry);
625 self = sip_uri_self(sip);
626 conference_id = genconfid();
627 body = g_strdup_printf(
628 SIPE_SEND_CONF_ADD,
629 sip->focus_factory_uri,
630 self,
631 rand(),
632 conference_id,
633 expiry_time);
634 g_free(self);
635 g_free(conference_id);
636 g_free(expiry_time);
638 trans = send_sip_request( sip->gc,
639 "SERVICE",
640 sip->focus_factory_uri,
641 sip->focus_factory_uri,
642 hdr,
643 body,
644 NULL,
645 process_conf_add_response);
647 payload = g_new0(struct transaction_payload, 1);
648 payload->destroy = g_free;
649 payload->data = g_strdup(who);
650 trans->payload = payload;
652 sipe_dialog_free(dialog);
653 g_free(body);
654 g_free(hdr);
657 void
658 process_incoming_invite_conf(struct sipe_account_data *sip,
659 struct sipmsg *msg)
661 struct sip_session *session = NULL;
662 struct sip_dialog *dialog = NULL;
663 sipe_xml *xn_conferencing = sipe_xml_parse(msg->body, msg->bodylen);
664 const sipe_xml *xn_focus_uri = sipe_xml_child(xn_conferencing, "focus-uri");
665 char *focus_uri = sipe_xml_data(xn_focus_uri);
666 gchar *newTag = gentag();
667 const gchar *oldHeader = sipmsg_find_header(msg, "To");
668 gchar *newHeader;
670 sipe_xml_free(xn_conferencing);
672 /* send OK */
673 SIPE_DEBUG_INFO("We have received invitation to Conference. Focus URI=%s", focus_uri);
675 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
676 sipmsg_remove_header_now(msg, "To");
677 sipmsg_add_header_now(msg, "To", newHeader);
678 g_free(newHeader);
680 /* temporary dialog with invitor */
681 /* take data before 'msg' will be modified by send_sip_response */
682 dialog = g_new0(struct sip_dialog, 1);
683 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
684 dialog->cseq = parse_cseq(sipmsg_find_header(msg, "CSeq"));
685 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
686 sipe_dialog_parse(dialog, msg, FALSE);
688 send_sip_response(sip->gc, msg, 200, "OK", NULL);
690 session = sipe_session_add_chat(sip);
691 session->focus_uri = focus_uri;
692 session->is_multiparty = FALSE;
694 /* send BYE to invitor */
695 send_sip_request(sip->gc, "BYE", dialog->with, dialog->with, NULL, NULL, dialog, NULL);
696 sipe_dialog_free(dialog);
698 /* add self to conf */
699 sipe_invite_conf_focus(sip, session);
702 void
703 sipe_process_conference(struct sipe_account_data *sip,
704 struct sipmsg *msg)
706 sipe_xml *xn_conference_info;
707 const sipe_xml *node;
708 const sipe_xml *xn_subject;
709 const gchar *focus_uri;
710 struct sip_session *session;
711 gboolean just_joined = FALSE;
713 if (msg->response != 0 && msg->response != 200) return;
715 if (msg->bodylen == 0 || msg->body == NULL || !sipe_strequal(sipmsg_find_header(msg, "Event"), "conference")) return;
717 xn_conference_info = sipe_xml_parse(msg->body, msg->bodylen);
718 if (!xn_conference_info) return;
720 focus_uri = sipe_xml_attribute(xn_conference_info, "entity");
721 session = sipe_session_find_conference(sip, focus_uri);
723 if (!session) {
724 SIPE_DEBUG_INFO("sipe_process_conference: unable to find conf session with focus=%s", focus_uri);
725 return;
728 if (session->focus_uri && !session->conv) {
729 gchar *chat_title = sipe_chat_get_name(session->focus_uri);
730 gchar *self = sip_uri_self(sip);
731 /* can't be find by chat id as it won't survive acc reinstantation */
732 PurpleConversation *conv = NULL;
734 if (chat_title) {
735 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT,
736 chat_title,
737 sip->account);
739 /* to be able to rejoin existing chat/window */
740 if (conv && !purple_conv_chat_has_left(PURPLE_CONV_CHAT(conv))) {
741 PURPLE_CONV_CHAT(conv)->left = TRUE;
743 /* create prpl chat */
744 session->conv = serv_got_joined_chat(sip->gc, session->chat_id, chat_title);
745 session->chat_title = chat_title;
746 purple_conv_chat_set_nick(PURPLE_CONV_CHAT(session->conv), self);
747 just_joined = TRUE;
748 /* @TODO ask for full state (re-subscribe) if it was a partial one -
749 * this is to obtain full list of conference participants.
751 g_free(self);
754 /* subject */
755 if ((xn_subject = sipe_xml_child(xn_conference_info, "conference-description/subject"))) {
756 g_free(session->subject);
757 session->subject = sipe_xml_data(xn_subject);
758 purple_conv_chat_set_topic(PURPLE_CONV_CHAT(session->conv), NULL, session->subject);
759 SIPE_DEBUG_INFO("sipe_process_conference: subject=%s", session->subject ? session->subject : "");
762 /* IM MCU URI */
763 if (!session->im_mcu_uri) {
764 for (node = sipe_xml_child(xn_conference_info, "conference-description/conf-uris/entry");
765 node;
766 node = sipe_xml_twin(node))
768 gchar *purpose = sipe_xml_data(sipe_xml_child(node, "purpose"));
770 if (sipe_strequal("chat", purpose)) {
771 g_free(purpose);
772 session->im_mcu_uri = sipe_xml_data(sipe_xml_child(node, "uri"));
773 SIPE_DEBUG_INFO("sipe_process_conference: im_mcu_uri=%s", session->im_mcu_uri);
774 break;
776 g_free(purpose);
780 /* users */
781 for (node = sipe_xml_child(xn_conference_info, "users/user"); node; node = sipe_xml_twin(node)) {
782 const gchar *user_uri = sipe_xml_attribute(node, "entity");
783 const gchar *state = sipe_xml_attribute(node, "state");
784 gchar *role = sipe_xml_data(sipe_xml_child(node, "roles/entry"));
785 PurpleConvChatBuddyFlags flags = PURPLE_CBFLAGS_NONE;
786 PurpleConvChat *chat = PURPLE_CONV_CHAT(session->conv);
787 gboolean is_in_im_mcu = FALSE;
788 gchar *self = sip_uri_self(sip);
790 if (sipe_strequal(role, "presenter")) {
791 flags |= PURPLE_CBFLAGS_OP;
794 if (sipe_strequal("deleted", state)) {
795 if (purple_conv_chat_find_user(chat, user_uri)) {
796 purple_conv_chat_remove_user(chat, user_uri, NULL /* reason */);
798 } else {
799 /* endpoints */
800 const sipe_xml *endpoint;
801 for (endpoint = sipe_xml_child(node, "endpoint"); endpoint; endpoint = sipe_xml_twin(endpoint)) {
802 if (sipe_strequal("chat", sipe_xml_attribute(endpoint, "session-type"))) {
803 gchar *status = sipe_xml_data(sipe_xml_child(endpoint, "status"));
804 if (sipe_strequal("connected", status)) {
805 is_in_im_mcu = TRUE;
806 if (!purple_conv_chat_find_user(chat, user_uri)) {
807 purple_conv_chat_add_user(chat, user_uri, NULL, flags,
808 !just_joined && g_strcasecmp(user_uri, self));
809 } else {
810 purple_conv_chat_user_set_flags(chat, user_uri, flags);
813 g_free(status);
814 break;
817 if (!is_in_im_mcu) {
818 if (purple_conv_chat_find_user(chat, user_uri)) {
819 purple_conv_chat_remove_user(chat, user_uri, NULL /* reason */);
823 g_free(role);
824 g_free(self);
827 /* entity-view, locked */
828 for (node = sipe_xml_child(xn_conference_info, "conference-view/entity-view");
829 node;
830 node = sipe_xml_twin(node)) {
832 const sipe_xml *xn_type = sipe_xml_child(node, "entity-state/media/entry/type");
833 gchar *tmp = NULL;
834 if (xn_type && sipe_strequal("chat", (tmp = sipe_xml_data(xn_type)))) {
835 const sipe_xml *xn_locked = sipe_xml_child(node, "entity-state/locked");
836 if (xn_locked) {
837 gchar *locked = sipe_xml_data(xn_locked);
838 gboolean prev_locked = session->locked;
839 session->locked = sipe_strequal(locked, "true");
840 if (prev_locked && !session->locked) {
841 sipe_present_info(sip, session,
842 _("This conference is no longer locked. Additional participants can now join."));
844 if (!prev_locked && session->locked) {
845 sipe_present_info(sip, session,
846 _("This conference is locked. Nobody else can join the conference while it is locked."));
849 SIPE_DEBUG_INFO("sipe_process_conference: session->locked=%s",
850 session->locked ? "TRUE" : "FALSE");
851 g_free(locked);
854 g_free(tmp);
856 sipe_xml_free(xn_conference_info);
858 if (session->im_mcu_uri) {
859 struct sip_dialog *dialog = sipe_dialog_find(session, session->im_mcu_uri);
860 if (!dialog) {
861 dialog = sipe_dialog_add(session);
863 dialog->callid = g_strdup(session->callid);
864 dialog->with = g_strdup(session->im_mcu_uri);
866 /* send INVITE to IM MCU */
867 sipe_invite(sip, session, dialog->with, NULL, NULL, NULL, FALSE);
871 sipe_process_pending_invite_queue(sip, session);
874 void
875 sipe_conf_immcu_closed(struct sipe_account_data *sip,
876 struct sip_session *session)
878 sipe_present_info(sip, session,
879 _("You have been disconnected from this conference."));
880 purple_conv_chat_clear_users(PURPLE_CONV_CHAT(session->conv));
883 void
884 conf_session_close(struct sipe_account_data *sip,
885 struct sip_session *session)
887 if (session) {
888 /* unsubscribe from focus */
889 sipe_subscribe_conference(sip, session, 0);
891 if (session->focus_dialog) {
892 /* send BYE to focus */
893 send_sip_request(sip->gc,
894 "BYE",
895 session->focus_dialog->with,
896 session->focus_dialog->with,
897 NULL,
898 NULL,
899 session->focus_dialog,
900 NULL);
905 void
906 sipe_process_imdn(struct sipe_account_data *sip,
907 struct sipmsg *msg)
909 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
910 const gchar *call_id = sipmsg_find_header(msg, "Call-ID");
911 static struct sip_session *session;
912 sipe_xml *xn_imdn;
913 const sipe_xml *node;
914 gchar *message_id;
915 gchar *message;
917 session = sipe_session_find_chat_by_callid(sip, call_id);
918 if (!session) {
919 session = sipe_session_find_im(sip, with);
921 if (!session) {
922 SIPE_DEBUG_INFO("sipe_process_imdn: unable to find conf session with call_id=%s", call_id);
923 g_free(with);
924 return;
927 xn_imdn = sipe_xml_parse(msg->body, msg->bodylen);
928 message_id = sipe_xml_data(sipe_xml_child(xn_imdn, "message-id"));
930 message = g_hash_table_lookup(session->conf_unconfirmed_messages, message_id);
932 /* recipient */
933 for (node = sipe_xml_child(xn_imdn, "recipient"); node; node = sipe_xml_twin(node)) {
934 gchar *tmp = parse_from(sipe_xml_attribute(node, "uri"));
935 gchar *uri = parse_from(tmp);
936 sipe_present_message_undelivered_err(sip, session, -1, -1, uri, message);
937 g_free(tmp);
938 g_free(uri);
941 sipe_xml_free(xn_imdn);
943 g_hash_table_remove(session->conf_unconfirmed_messages, message_id);
944 SIPE_DEBUG_INFO("sipe_process_imdn: removed message %s from conf_unconfirmed_messages(count=%d)",
945 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
946 g_free(message_id);
947 g_free(with);
952 Local Variables:
953 mode: c
954 c-file-style: "bsd"
955 indent-tabs-mode: t
956 tab-width: 8
957 End: