Updated to release 1.7.1
[siplcs.git] / src / sipe-conf.c
blob22ca0fe178c3e1d9f5d6fc1122f4335f1a599301
1 /**
2 * @file sipe-conf.c
4 * pidgin-sipe
6 * Copyright (C) 2009 pier11 <pier11@operamail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <string.h>
25 #include <glib.h>
27 #include "debug.h"
29 #include "sipe.h"
30 #include "sipe-chat.h"
31 #include "sipe-conf.h"
32 #include "sipe-dialog.h"
33 #include "sipe-nls.h"
34 #include "sipe-session.h"
35 #include "sipe-utils.h"
37 /**
38 * Add Conference request to FocusFactory.
39 * @param focus_factory_uri (%s) Ex.: sip:bob7@boston.local;gruu;opaque=app:conf:focusfactory
40 * @param from (%s) Ex.: sip:bob7@boston.local
41 * @param request_id (%d) Ex.: 1094520
42 * @param conference_id (%s) Ex.: 8386E6AEAAA41E4AA6627BA76D43B6D1
43 * @param expiry_time (%s) Ex.: 2009-07-13T17:57:09Z , Default duration: 7 hours
45 #define SIPE_SEND_CONF_ADD \
46 "<?xml version=\"1.0\"?>"\
47 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" "\
48 "xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
49 "C3PVersion=\"1\" "\
50 "to=\"%s\" "\
51 "from=\"%s\" "\
52 "requestId=\"%d\">"\
53 "<addConference>"\
54 "<ci:conference-info xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\">"\
55 "<ci:conference-description>"\
56 "<ci:subject/>"\
57 "<msci:conference-id>%s</msci:conference-id>"\
58 "<msci:expiry-time>%s</msci:expiry-time>"\
59 "<msci:admission-policy>openAuthenticated</msci:admission-policy>"\
60 "</ci:conference-description>"\
61 "<msci:conference-view>"\
62 "<msci:entity-view entity=\"chat\"/>"\
63 "</msci:conference-view>"\
64 "</ci:conference-info>"\
65 "</addConference>"\
66 "</request>"
68 /**
69 * AddUser request to Focus.
70 * Params:
71 * focus_URI, from, request_id, focus_URI, from, endpoint_GUID
73 #define SIPE_SEND_CONF_ADD_USER \
74 "<?xml version=\"1.0\"?>"\
75 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
76 "C3PVersion=\"1\" "\
77 "to=\"%s\" "\
78 "from=\"%s\" "\
79 "requestId=\"%d\">"\
80 "<addUser>"\
81 "<conferenceKeys confEntity=\"%s\"/>"\
82 "<ci:user xmlns:ci=\"urn:ietf:params:xml:ns:conference-info\" entity=\"%s\">"\
83 "<ci:roles>"\
84 "<ci:entry>attendee</ci:entry>"\
85 "</ci:roles>"\
86 "<ci:endpoint entity=\"{%s}\" xmlns:msci=\"http://schemas.microsoft.com/rtc/2005/08/confinfoextensions\"/>"\
87 "</ci:user>"\
88 "</addUser>"\
89 "</request>"
91 /**
92 * ModifyUserRoles request to Focus. Makes user a leader.
93 * @param focus_uri (%s)
94 * @param from (%s)
95 * @param request_id (%d)
96 * @param focus_uri (%s)
97 * @param who (%s)
99 #define SIPE_SEND_CONF_MODIFY_USER_ROLES \
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 "<modifyUserRoles>"\
107 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
108 "<user-roles xmlns=\"urn:ietf:params:xml:ns:conference-info\">"\
109 "<entry>presenter</entry>"\
110 "</user-roles>"\
111 "</modifyUserRoles>"\
112 "</request>"
115 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
116 * @param focus_uri (%s)
117 * @param from (%s)
118 * @param request_id (%d)
119 * @param focus_uri (%s)
120 * @param locked (%s) "true" or "false" values applicable
122 #define SIPE_SEND_CONF_MODIFY_CONF_LOCK \
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 "<modifyConferenceLock>"\
130 "<conferenceKeys confEntity=\"%s\"/>"\
131 "<locked>%s</locked>"\
132 "</modifyConferenceLock>"\
133 "</request>"
136 * ModifyConferenceLock request to Focus. Locks/unlocks conference.
137 * @param focus_uri (%s)
138 * @param from (%s)
139 * @param request_id (%d)
140 * @param focus_uri (%s)
141 * @param who (%s)
143 #define SIPE_SEND_CONF_DELETE_USER \
144 "<?xml version=\"1.0\"?>"\
145 "<request xmlns=\"urn:ietf:params:xml:ns:cccp\" xmlns:mscp=\"http://schemas.microsoft.com/rtc/2005/08/cccpextensions\" "\
146 "C3PVersion=\"1\" "\
147 "to=\"%s\" "\
148 "from=\"%s\" "\
149 "requestId=\"%d\">"\
150 "<deleteUser>"\
151 "<userKeys confEntity=\"%s\" userEntity=\"%s\"/>"\
152 "</deleteUser>"\
153 "</request>"
156 * Invite counterparty to join conference.
157 * @param focus_uri (%s)
158 * @param subject (%s) of conference
160 #define SIPE_SEND_CONF_INVITE \
161 "<Conferencing version=\"2.0\">"\
162 "<focus-uri>%s</focus-uri>"\
163 "<subject>%s</subject>"\
164 "<im available=\"true\">"\
165 "<first-im/>"\
166 "</im>"\
167 "</Conferencing>"
170 * Generates random GUID.
171 * This method is borrowed from pidgin's msnutils.c
173 static char *
174 rand_guid()
176 return g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
177 rand() % 0xAAFF + 0x1111,
178 rand() % 0xAAFF + 0x1111,
179 rand() % 0xAAFF + 0x1111,
180 rand() % 0xAAFF + 0x1111,
181 rand() % 0xAAFF + 0x1111,
182 rand() % 0xAAFF + 0x1111,
183 rand() % 0xAAFF + 0x1111,
184 rand() % 0xAAFF + 0x1111);
188 * @param expires not respected if set to negative value (E.g. -1)
190 static void
191 sipe_subscribe_conference(struct sipe_account_data *sip,
192 struct sip_session *session,
193 const int expires)
195 gchar *expires_hdr = (expires >= 0) ? g_strdup_printf("Expires: %d\r\n", expires) : g_strdup("");
196 gchar *contact = get_contact(sip);
197 gchar *hdr = g_strdup_printf(
198 "Event: conference\r\n"
199 "%s"
200 "Accept: application/conference-info+xml\r\n"
201 "Supported: com.microsoft.autoextend\r\n"
202 "Supported: ms-benotify\r\n"
203 "Proxy-Require: ms-benotify\r\n"
204 "Supported: ms-piggyback-first-notify\r\n"
205 "Contact: %s\r\n",
206 expires_hdr,
207 contact);
208 g_free(expires_hdr);
209 g_free(contact);
211 send_sip_request(sip->gc,
212 "SUBSCRIBE",
213 session->focus_uri,
214 session->focus_uri,
215 hdr,
217 NULL,
218 process_subscribe_response);
219 g_free(hdr);
222 /** Invite us to the focus callback */
223 static gboolean
224 process_invite_conf_focus_response(struct sipe_account_data *sip,
225 struct sipmsg *msg,
226 SIPE_UNUSED_PARAMETER struct transaction *trans)
228 struct sip_session *session = NULL;
229 char *focus_uri = parse_from(sipmsg_find_header(msg, "To"));
231 session = sipe_session_find_conference(sip, focus_uri);
233 if (!session) {
234 purple_debug_info("sipe", "process_invite_conf_focus_response: unable to find conf session with focus=%s\n", focus_uri);
235 g_free(focus_uri);
236 return FALSE;
239 if (!session->focus_dialog) {
240 purple_debug_info("sipe", "process_invite_conf_focus_response: session's focus_dialog is NULL\n");
241 g_free(focus_uri);
242 return FALSE;
245 sipe_dialog_parse(session->focus_dialog, msg, TRUE);
247 if (msg->response >= 200) {
248 /* send ACK to focus */
249 session->focus_dialog->cseq = 0;
250 send_sip_request(sip->gc, "ACK", session->focus_dialog->with, session->focus_dialog->with, NULL, NULL, session->focus_dialog, NULL);
251 session->focus_dialog->outgoing_invite = NULL;
252 session->focus_dialog->is_established = TRUE;
255 if (msg->response >= 400) {
256 purple_debug_info("sipe", "process_invite_conf_focus_response: INVITE response is not 200. Failed to join focus.\n");
257 /* @TODO notify user of failure to join focus */
258 sipe_session_remove(sip, session);
259 g_free(focus_uri);
260 return FALSE;
261 } else if (msg->response == 200) {
262 xmlnode *xn_response = xmlnode_from_str(msg->body, msg->bodylen);
263 const gchar *code = xmlnode_get_attrib(xn_response, "code");
264 if (!strcmp(code, "success")) {
265 /* subscribe to focus */
266 sipe_subscribe_conference(sip, session, -1);
268 xmlnode_free(xn_response);
271 g_free(focus_uri);
272 return TRUE;
275 /** Invite us to the focus */
276 void
277 sipe_invite_conf_focus(struct sipe_account_data *sip,
278 struct sip_session *session)
280 gchar *hdr;
281 gchar *contact;
282 gchar *body;
283 gchar *self;
285 if (session->focus_dialog && session->focus_dialog->is_established) {
286 purple_debug_info("sipe", "session with %s already has a dialog open\n", session->focus_uri);
287 return;
290 if(!session->focus_dialog) {
291 session->focus_dialog = g_new0(struct sip_dialog, 1);
292 session->focus_dialog->callid = gencallid();
293 session->focus_dialog->with = g_strdup(session->focus_uri);
294 session->focus_dialog->endpoint_GUID = rand_guid();
296 if (!(session->focus_dialog->ourtag)) {
297 session->focus_dialog->ourtag = gentag();
300 contact = get_contact(sip);
301 hdr = g_strdup_printf(
302 "Supported: ms-sender\r\n"
303 "Contact: %s\r\n"
304 "Content-Type: application/cccp+xml\r\n",
305 contact);
306 g_free(contact);
308 /* @TODO put request_id to queue to further compare with incoming one */
309 /* focus_URI, from, request_id, focus_URI, from, endpoint_GUID */
310 self = sip_uri_self(sip);
311 body = g_strdup_printf(
312 SIPE_SEND_CONF_ADD_USER,
313 session->focus_dialog->with,
314 self,
315 session->request_id++,
316 session->focus_dialog->with,
317 self,
318 session->focus_dialog->endpoint_GUID);
319 g_free(self);
321 session->focus_dialog->outgoing_invite = send_sip_request(sip->gc,
322 "INVITE",
323 session->focus_dialog->with,
324 session->focus_dialog->with,
325 hdr,
326 body,
327 session->focus_dialog,
328 process_invite_conf_focus_response);
329 g_free(body);
330 g_free(hdr);
333 /** Modify User Role */
334 void
335 sipe_conf_modify_user_role(struct sipe_account_data *sip,
336 struct sip_session *session,
337 const gchar* who)
339 gchar *hdr;
340 gchar *body;
341 gchar *self;
343 if (!session->focus_dialog || !session->focus_dialog->is_established) {
344 purple_debug_info("sipe", "sipe_conf_modify_user_role: no dialog with focus, exiting.\n");
345 return;
348 hdr = g_strdup(
349 "Content-Type: application/cccp+xml\r\n");
351 /* @TODO put request_id to queue to further compare with incoming one */
352 self = sip_uri_self(sip);
353 body = g_strdup_printf(
354 SIPE_SEND_CONF_MODIFY_USER_ROLES,
355 session->focus_dialog->with,
356 self,
357 session->request_id++,
358 session->focus_dialog->with,
359 who);
360 g_free(self);
362 send_sip_request(sip->gc,
363 "INFO",
364 session->focus_dialog->with,
365 session->focus_dialog->with,
366 hdr,
367 body,
368 session->focus_dialog,
369 NULL);
370 g_free(body);
371 g_free(hdr);
374 /** Modify Conference Lock */
375 void
376 sipe_conf_modify_conference_lock(struct sipe_account_data *sip,
377 struct sip_session *session,
378 const gboolean locked)
380 gchar *hdr;
381 gchar *body;
382 gchar *self;
384 if (!session->focus_dialog || !session->focus_dialog->is_established) {
385 purple_debug_info("sipe", "sipe_conf_modify_conference_lock: no dialog with focus, exiting.\n");
386 return;
389 hdr = g_strdup(
390 "Content-Type: application/cccp+xml\r\n");
392 /* @TODO put request_id to queue to further compare with incoming one */
393 self = sip_uri_self(sip);
394 body = g_strdup_printf(
395 SIPE_SEND_CONF_MODIFY_CONF_LOCK,
396 session->focus_dialog->with,
397 self,
398 session->request_id++,
399 session->focus_dialog->with,
400 locked ? "true" : "false");
401 g_free(self);
403 send_sip_request(sip->gc,
404 "INFO",
405 session->focus_dialog->with,
406 session->focus_dialog->with,
407 hdr,
408 body,
409 session->focus_dialog,
410 NULL);
411 g_free(body);
412 g_free(hdr);
415 /** Modify Delete User */
416 void
417 sipe_conf_delete_user(struct sipe_account_data *sip,
418 struct sip_session *session,
419 const gchar* who)
421 gchar *hdr;
422 gchar *body;
423 gchar *self;
425 if (!session->focus_dialog || !session->focus_dialog->is_established) {
426 purple_debug_info("sipe", "sipe_conf_delete_user: no dialog with focus, exiting.\n");
427 return;
430 hdr = g_strdup(
431 "Content-Type: application/cccp+xml\r\n");
433 /* @TODO put request_id to queue to further compare with incoming one */
434 self = sip_uri_self(sip);
435 body = g_strdup_printf(
436 SIPE_SEND_CONF_DELETE_USER,
437 session->focus_dialog->with,
438 self,
439 session->request_id++,
440 session->focus_dialog->with,
441 who);
442 g_free(self);
444 send_sip_request(sip->gc,
445 "INFO",
446 session->focus_dialog->with,
447 session->focus_dialog->with,
448 hdr,
449 body,
450 session->focus_dialog,
451 NULL);
452 g_free(body);
453 g_free(hdr);
456 /** Invite counterparty to join conference callback */
457 static gboolean
458 process_invite_conf_response(struct sipe_account_data *sip,
459 struct sipmsg *msg,
460 SIPE_UNUSED_PARAMETER struct transaction *trans)
462 struct sip_dialog *dialog = g_new0(struct sip_dialog, 1);
464 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
465 dialog->cseq = parse_cseq(sipmsg_find_header(msg, "CSeq"));
466 dialog->with = parse_from(sipmsg_find_header(msg, "To"));
467 sipe_dialog_parse(dialog, msg, TRUE);
469 if (msg->response >= 200) {
470 /* send ACK to counterparty */
471 dialog->cseq--;
472 send_sip_request(sip->gc, "ACK", dialog->with, dialog->with, NULL, NULL, dialog, NULL);
473 dialog->outgoing_invite = NULL;
474 dialog->is_established = TRUE;
477 if (msg->response >= 400) {
478 purple_debug_info("sipe", "process_invite_conf_response: INVITE response is not 200. Failed to invite %s.\n", dialog->with);
479 /* @TODO notify user of failure to invite counterparty */
480 sipe_dialog_free(dialog);
481 return FALSE;
484 if (msg->response >= 200) {
485 struct sip_session *session = sipe_session_find_im(sip, dialog->with);
486 struct sip_dialog *im_dialog = sipe_dialog_find(session, dialog->with);
488 /* close IM session to counterparty */
489 if (im_dialog) {
490 send_sip_request(sip->gc, "BYE", im_dialog->with, im_dialog->with, NULL, NULL, im_dialog, NULL);
491 sipe_dialog_remove(session, dialog->with);
495 sipe_dialog_free(dialog);
496 return TRUE;
500 * Invites counterparty to join conference.
502 void
503 sipe_invite_conf(struct sipe_account_data *sip,
504 struct sip_session *session,
505 const gchar* who)
507 gchar *hdr;
508 gchar *contact;
509 gchar *body;
510 struct sip_dialog *dialog = NULL;
512 /* It will be short lived special dialog.
513 * Will not be stored in session.
515 dialog = g_new0(struct sip_dialog, 1);
516 dialog->callid = gencallid();
517 dialog->with = g_strdup(who);
518 dialog->ourtag = gentag();
520 contact = get_contact(sip);
521 hdr = g_strdup_printf(
522 "Supported: ms-sender\r\n"
523 "Contact: %s\r\n"
524 "Content-Type: application/ms-conf-invite+xml\r\n",
525 contact);
526 g_free(contact);
528 body = g_strdup_printf(
529 SIPE_SEND_CONF_INVITE,
530 session->focus_uri,
531 session->subject ? session->subject : ""
534 send_sip_request( sip->gc,
535 "INVITE",
536 dialog->with,
537 dialog->with,
538 hdr,
539 body,
540 dialog,
541 process_invite_conf_response);
543 sipe_dialog_free(dialog);
544 g_free(body);
545 g_free(hdr);
548 /** Create conference callback */
549 static gboolean
550 process_conf_add_response(struct sipe_account_data *sip,
551 struct sipmsg *msg,
552 struct transaction *trans)
554 if (msg->response >= 400) {
555 purple_debug_info("sipe", "process_conf_add_response: SERVICE response is not 200. Failed to create conference.\n");
556 /* @TODO notify user of failure to create conference */
557 return FALSE;
559 if (msg->response == 200) {
560 xmlnode *xn_response = xmlnode_from_str(msg->body, msg->bodylen);
561 if (!strcmp("success", xmlnode_get_attrib(xn_response, "code")))
563 gchar *who = trans->payload->data;
564 struct sip_session *session;
565 xmlnode *xn_conference_info = xmlnode_get_descendant(xn_response, "addConference", "conference-info", NULL);
567 session = sipe_session_add_chat(sip);
568 session->is_multiparty = FALSE;
569 session->focus_uri = g_strdup(xmlnode_get_attrib(xn_conference_info, "entity"));
570 purple_debug_info("sipe", "process_conf_add_response: session->focus_uri=%s\n",
571 session->focus_uri ? session->focus_uri : "");
573 session->pending_invite_queue = slist_insert_unique_sorted(
574 session->pending_invite_queue, g_strdup(who), (GCompareFunc)strcmp);
576 /* add self to conf */
577 sipe_invite_conf_focus(sip, session);
579 xmlnode_free(xn_response);
582 return TRUE;
586 * Creates conference.
588 void
589 sipe_conf_add(struct sipe_account_data *sip,
590 const gchar* who)
592 gchar *hdr;
593 gchar *conference_id;
594 gchar *contact;
595 gchar *body;
596 gchar *self;
597 struct transaction *trans;
598 struct sip_dialog *dialog = NULL;
599 time_t expiry = time(NULL) + 7*60*60; /* 7 hours */
600 const char *expiry_time;
601 struct transaction_payload *payload;
603 contact = get_contact(sip);
604 hdr = g_strdup_printf(
605 "Supported: ms-sender\r\n"
606 "Contact: %s\r\n"
607 "Content-Type: application/cccp+xml\r\n",
608 contact);
609 g_free(contact);
611 expiry_time = purple_utf8_strftime("%Y-%m-%dT%H:%M:%SZ", gmtime(&expiry));
612 self = sip_uri_self(sip);
613 conference_id = genconfid();
614 body = g_strdup_printf(
615 SIPE_SEND_CONF_ADD,
616 sip->focus_factory_uri,
617 self,
618 rand(),
619 conference_id,
620 expiry_time);
621 g_free(conference_id);
622 g_free(self);
624 trans = send_sip_request( sip->gc,
625 "SERVICE",
626 sip->focus_factory_uri,
627 sip->focus_factory_uri,
628 hdr,
629 body,
630 NULL,
631 process_conf_add_response);
633 payload = g_new0(struct transaction_payload, 1);
634 payload->destroy = g_free;
635 payload->data = g_strdup(who);
636 trans->payload = payload;
638 sipe_dialog_free(dialog);
639 g_free(body);
640 g_free(hdr);
643 void
644 process_incoming_invite_conf(struct sipe_account_data *sip,
645 struct sipmsg *msg)
647 struct sip_session *session = NULL;
648 struct sip_dialog *dialog = NULL;
649 xmlnode *xn_conferencing = xmlnode_from_str(msg->body, msg->bodylen);
650 xmlnode *xn_focus_uri = xmlnode_get_child(xn_conferencing, "focus-uri");
651 char *focus_uri = xmlnode_get_data(xn_focus_uri);
652 gchar *newTag = gentag();
653 gchar *oldHeader = sipmsg_find_header(msg, "To");
654 gchar *newHeader;
656 xmlnode_free(xn_conferencing);
658 /* send OK */
659 purple_debug_info("sipe", "We have received invitation to Conference. Focus URI=%s\n", focus_uri);
661 newHeader = g_strdup_printf("%s;tag=%s", oldHeader, newTag);
662 sipmsg_remove_header_now(msg, "To");
663 sipmsg_add_header_now(msg, "To", newHeader);
664 g_free(newHeader);
666 /* temporary dialog with invitor */
667 /* take data before 'msg' will be modified by send_sip_response */
668 dialog = g_new0(struct sip_dialog, 1);
669 dialog->callid = g_strdup(sipmsg_find_header(msg, "Call-ID"));
670 dialog->cseq = parse_cseq(sipmsg_find_header(msg, "CSeq"));
671 dialog->with = parse_from(sipmsg_find_header(msg, "From"));
672 sipe_dialog_parse(dialog, msg, FALSE);
674 send_sip_response(sip->gc, msg, 200, "OK", NULL);
676 session = sipe_session_add_chat(sip);
677 session->focus_uri = focus_uri;
678 session->is_multiparty = FALSE;
680 /* send BYE to invitor */
681 send_sip_request(sip->gc, "BYE", dialog->with, dialog->with, NULL, NULL, dialog, NULL);
682 sipe_dialog_free(dialog);
684 /* add self to conf */
685 sipe_invite_conf_focus(sip, session);
688 void
689 sipe_process_conference(struct sipe_account_data *sip,
690 struct sipmsg *msg)
692 xmlnode *xn_conference_info;
693 xmlnode *node;
694 xmlnode *xn_subject;
695 const gchar *focus_uri;
696 struct sip_session *session;
697 gboolean just_joined = FALSE;
699 if (msg->response != 0 && msg->response != 200) return;
701 if (msg->bodylen == 0 || msg->body == NULL || strcmp(sipmsg_find_header(msg, "Event"), "conference")) return;
703 xn_conference_info = xmlnode_from_str(msg->body, msg->bodylen);
704 if (!xn_conference_info) return;
706 focus_uri = xmlnode_get_attrib(xn_conference_info, "entity");
707 session = sipe_session_find_conference(sip, focus_uri);
709 if (!session) {
710 purple_debug_info("sipe", "sipe_process_conference: unable to find conf session with focus=%s\n", focus_uri);
711 return;
714 if (session->focus_uri && !session->conv) {
715 gchar *chat_title = sipe_chat_get_name(session->focus_uri);
716 gchar *self = sip_uri_self(sip);
717 /* can't be find by chat id as it won't survive acc reinstantation */
718 PurpleConversation *conv = NULL;
720 if (chat_title) {
721 conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT,
722 chat_title,
723 sip->account);
725 /* to be able to rejoin existing chat/window */
726 if (conv && !purple_conv_chat_has_left(PURPLE_CONV_CHAT(conv))) {
727 PURPLE_CONV_CHAT(conv)->left = TRUE;
729 /* create prpl chat */
730 session->conv = serv_got_joined_chat(sip->gc, session->chat_id, chat_title);
731 session->chat_title = chat_title;
732 purple_conv_chat_set_nick(PURPLE_CONV_CHAT(session->conv), self);
733 just_joined = TRUE;
734 /* @TODO ask for full state (re-subscribe) if it was a partial one -
735 * this is to obtain full list of conference participants.
737 g_free(self);
740 /* subject */
741 if ((xn_subject = xmlnode_get_descendant(xn_conference_info, "conference-description", "subject", NULL))) {
742 g_free(session->subject);
743 session->subject = xmlnode_get_data(xn_subject);
744 purple_conv_chat_set_topic(PURPLE_CONV_CHAT(session->conv), NULL, session->subject);
745 purple_debug_info("sipe", "sipe_process_conference: subject=%s\n", session->subject ? session->subject : "");
748 /* IM MCU URI */
749 if (!session->im_mcu_uri) {
750 for (node = xmlnode_get_descendant(xn_conference_info, "conference-description", "conf-uris", "entry", NULL);
751 node;
752 node = xmlnode_get_next_twin(node))
754 gchar *purpose = xmlnode_get_data(xmlnode_get_child(node, "purpose"));
756 if (purpose && !strcmp("chat", purpose)) {
757 g_free(purpose);
758 session->im_mcu_uri = xmlnode_get_data(xmlnode_get_child(node, "uri"));
759 purple_debug_info("sipe", "sipe_process_conference: im_mcu_uri=%s\n", session->im_mcu_uri);
760 break;
762 g_free(purpose);
766 /* users */
767 for (node = xmlnode_get_descendant(xn_conference_info, "users", "user", NULL); node; node = xmlnode_get_next_twin(node)) {
768 xmlnode *endpoint = NULL;
769 const gchar *user_uri = xmlnode_get_attrib(node, "entity");
770 const gchar *state = xmlnode_get_attrib(node, "state");
771 gchar *role = xmlnode_get_data(xmlnode_get_descendant(node, "roles", "entry", NULL));
772 PurpleConvChatBuddyFlags flags = PURPLE_CBFLAGS_NONE;
773 PurpleConvChat *chat = PURPLE_CONV_CHAT(session->conv);
774 gboolean is_in_im_mcu = FALSE;
775 gchar *self = sip_uri_self(sip);
777 if (role && !strcmp(role, "presenter")) {
778 flags |= PURPLE_CBFLAGS_OP;
781 if (!strcmp("deleted", state)) {
782 if (purple_conv_chat_find_user(chat, user_uri)) {
783 purple_conv_chat_remove_user(chat, user_uri, NULL /* reason */);
785 } else {
786 /* endpoints */
787 for (endpoint = xmlnode_get_child(node, "endpoint"); endpoint; endpoint = xmlnode_get_next_twin(endpoint)) {
788 if (!strcmp("chat", xmlnode_get_attrib(endpoint, "session-type"))) {
789 gchar *status = xmlnode_get_data(xmlnode_get_child(endpoint, "status"));
790 if (!strcmp("connected", status)) {
791 is_in_im_mcu = TRUE;
792 if (!purple_conv_chat_find_user(chat, user_uri)) {
793 purple_conv_chat_add_user(chat, user_uri, NULL, flags,
794 !just_joined && g_strcasecmp(user_uri, self));
795 } else {
796 purple_conv_chat_user_set_flags(chat, user_uri, flags);
799 g_free(status);
800 break;
803 if (!is_in_im_mcu) {
804 if (purple_conv_chat_find_user(chat, user_uri)) {
805 purple_conv_chat_remove_user(chat, user_uri, NULL /* reason */);
809 g_free(role);
810 g_free(self);
813 /* entity-view, locked */
814 for (node = xmlnode_get_descendant(xn_conference_info, "conference-view", "entity-view", NULL);
815 node;
816 node = xmlnode_get_next_twin(node)) {
818 xmlnode *xn_type = xmlnode_get_descendant(node, "entity-state", "media", "entry", "type", NULL);
819 gchar *tmp;
820 if (xn_type && !strcmp("chat", (tmp = xmlnode_get_data(xn_type)))) {
821 xmlnode *xn_locked = xmlnode_get_descendant(node, "entity-state", "locked", NULL);
822 if (xn_locked) {
823 gchar *locked = xmlnode_get_data(xn_locked);
824 gboolean prev_locked = session->locked;
825 session->locked = (locked && !strcmp(locked, "true")) ? TRUE : FALSE;
826 if (prev_locked && !session->locked) {
827 sipe_present_info(sip, session,
828 _("This conference is no longer locked. Additional participants can now join."));
830 if (!prev_locked && session->locked) {
831 sipe_present_info(sip, session,
832 _("This conference is locked. Nobody else can join the conference while it is locked."));
835 purple_debug_info("sipe", "sipe_process_conference: session->locked=%s\n",
836 session->locked ? "TRUE" : "FALSE");
837 g_free(locked);
839 g_free(tmp);
842 xmlnode_free(xn_conference_info);
844 if (session->im_mcu_uri) {
845 struct sip_dialog *dialog = sipe_dialog_find(session, session->im_mcu_uri);
846 if (!dialog) {
847 dialog = sipe_dialog_add(session);
849 dialog->callid = g_strdup(session->callid);
850 dialog->with = g_strdup(session->im_mcu_uri);
852 /* send INVITE to IM MCU */
853 sipe_invite(sip, session, dialog->with, NULL, NULL, FALSE);
857 sipe_process_pending_invite_queue(sip, session);
860 void
861 sipe_conf_immcu_closed(struct sipe_account_data *sip,
862 struct sip_session *session)
864 sipe_present_info(sip, session,
865 _("You have been disconnected from this conference."));
866 purple_conv_chat_clear_users(PURPLE_CONV_CHAT(session->conv));
869 void
870 conf_session_close(struct sipe_account_data *sip,
871 struct sip_session *session)
873 if (session) {
874 /* unsubscribe from focus */
875 sipe_subscribe_conference(sip, session, 0);
877 if (session->focus_dialog) {
878 /* send BYE to focus */
879 send_sip_request(sip->gc,
880 "BYE",
881 session->focus_dialog->with,
882 session->focus_dialog->with,
883 NULL,
884 NULL,
885 session->focus_dialog,
886 NULL);
891 void
892 sipe_process_imdn(struct sipe_account_data *sip,
893 struct sipmsg *msg)
895 gchar *with = parse_from(sipmsg_find_header(msg, "From"));
896 gchar *call_id = sipmsg_find_header(msg, "Call-ID");
897 static struct sip_session *session;
898 xmlnode *xn_imdn;
899 xmlnode *node;
900 gchar *message_id;
901 gchar *message;
903 session = sipe_session_find_chat_by_callid(sip, call_id);
904 if (!session) {
905 session = sipe_session_find_im(sip, with);
907 if (!session) {
908 purple_debug_info("sipe", "sipe_process_imdn: unable to find conf session with call_id=%s\n", call_id);
909 g_free(with);
910 return;
913 xn_imdn = xmlnode_from_str(msg->body, msg->bodylen);
914 message_id = xmlnode_get_data(xmlnode_get_child(xn_imdn, "message-id"));
916 message = g_hash_table_lookup(session->conf_unconfirmed_messages, message_id);
918 /* recipient */
919 for (node = xmlnode_get_child(xn_imdn, "recipient"); node; node = xmlnode_get_next_twin(node)) {
920 gchar *tmp = parse_from(xmlnode_get_attrib(node, "uri"));
921 gchar *uri = parse_from(tmp);
922 sipe_present_message_undelivered_err(sip, session, -1, uri, message);
923 g_free(tmp);
924 g_free(uri);
927 xmlnode_free(xn_imdn);
929 g_hash_table_remove(session->conf_unconfirmed_messages, message_id);
930 purple_debug_info("sipe", "sipe_process_imdn: removed message %s from conf_unconfirmed_messages(count=%d)\n",
931 message_id, g_hash_table_size(session->conf_unconfirmed_messages));
932 g_free(message_id);
933 g_free(with);
938 Local Variables:
939 mode: c
940 c-file-style: "bsd"
941 indent-tabs-mode: t
942 tab-width: 8
943 End: