Fix #222: SIPE crashes when groupchat session expires (V)
[siplcs.git] / src / core / sipe-groupchat.c
blob20e50a63ec88e5c7e8950a060b5b30685f5655ee
1 /**
2 * @file sipe-groupchat.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /**
24 * This module implements the OCS2007R2 Group Chat functionality
26 * Documentation references:
28 * Microsoft TechNet: Key Protocols and Windows Services Used by Group Chat
29 * <http://technet.microsoft.com/en-us/library/ee323484%28office.13%29.aspx>
30 * Microsoft TechNet: Group Chat Call Flows
31 * <http://technet.microsoft.com/en-us/library/ee323524%28office.13%29.aspx>
32 * Microsoft Office Communications Server 2007 R2 Technical Reference Guide
33 * <http://go.microsoft.com/fwlink/?LinkID=159649>
34 * Microsoft DevNet: [MS-XCCOSIP] Extensible Chat Control Over SIP
35 * <http://msdn.microsoft.com/en-us/library/hh624112.aspx>
36 * RFC 4028: Session Timers in the Session Initiation Protocol (SIP)
37 * <http://www.rfc-editor.org/rfc/rfc4028.txt>
40 * @TODO:
42 * -.cmd:getserverinfo
43 * <sib domain="<DOMAIN>" infoType="123" />
44 * rpl:getservinfo
45 * <sib infoType="123"
46 * serverTime="2010-09-14T14:26:17.6206356Z"
47 * searchLimit="999"
48 * messageSizeLimit="512"
49 * storySizeLimit="4096"
50 * rootUri="ma-cat://<DOMAIN>/<GUID>"
51 * dbVersion="3ea3a5a8-ef36-46cf-898f-7a5133931d63"
52 * />
54 * is there any information in there we would need/use?
56 * - cmd:getpref/rpl:getpref/cmd:setpref/rpl:setpref
57 * probably useless, as libpurple stores configuration locally
59 * can store base64 encoded "free text" in key/value fashion
60 * <cmd id="cmd:getpref" seqid="x">
61 * <data>
62 * <pref label="kedzie.GroupChannels"
63 * seqid="71"
64 * createdefault="true" />
65 * </data>
66 * </cmd>
67 * <cmd id="cmd:setpref" seqid="x">
68 * <data>
69 * <pref label="kedzie.GroupChannels"
70 * seqid="71"
71 * createdefault="false"
72 * content="<BASE64 text>" />
73 * </data>
74 * </cmd>
76 * use this to sync chats in buddy list on multiple clients?
78 * - cmd:getinv
79 * <inv inviteId="1" domain="<DOMAIN>" />
80 * rpl:getinv
81 * ???
83 * according to documentation should provide list of outstanding invites.
84 * [no log file examples]
85 * should we automatically join those channels or ask user to join/add?
87 * - chatserver_command_message()
88 * needs to support multiple <grpchat> nodes?
89 * [no log file examples]
91 * - create/delete chat rooms
92 * [no log file examples]
93 * are these related to this functionality?
95 * <cmd id="cmd:nodespermcreatechild" seqid="1">
96 * <data />
97 * </cmd>
98 * <rpl id="rpl:nodespermcreatechild" seqid="1">
99 * <commandid seqid="1" envid="xxx" />
100 * <resp code="200">SUCCESS_OK</resp>
101 * <data />
102 * </rpl>
104 * - file transfer (uses HTTPS PUT/GET via a filestore server)
105 * [no log file examples]
109 #ifdef HAVE_CONFIG_H
110 #include "config.h"
111 #endif
113 #include <stdlib.h>
114 #include <string.h>
116 #include <glib.h>
118 #include "sipe-common.h"
119 #include "sipmsg.h"
120 #include "sip-transport.h"
121 #include "sipe-backend.h"
122 #include "sipe-chat.h"
123 #include "sipe-core.h"
124 #include "sipe-core-private.h"
125 #include "sipe-dialog.h"
126 #include "sipe-groupchat.h"
127 #include "sipe-im.h"
128 #include "sipe-nls.h"
129 #include "sipe-schedule.h"
130 #include "sipe-session.h"
131 #include "sipe-utils.h"
132 #include "sipe-xml.h"
134 #define GROUPCHAT_RETRY_TIMEOUT 5*60 /* seconds */
137 * aib node - magic numbers?
139 * Example:
140 * <aib key="3984" value="0,1,2,3,4,5,7,9,10,12,13,14,15,16,17" />
141 * <aib key="12276" value="6,8,11" />
143 * "value" corresponds to the "id" attribute in uib nodes.
145 * @TODO: Confirm "guessed" meaning of the magic numbers:
146 * 3984 = normal users
147 * 12276 = channel operators
149 #define GROUPCHAT_AIB_KEY_USER "3984"
150 #define GROUPCHAT_AIB_KEY_CHANOP "12276"
152 struct sipe_groupchat {
153 struct sip_session *session;
154 gchar *domain;
155 GSList *join_queue;
156 GHashTable *uri_to_chat_session;
157 GHashTable *msgs;
158 guint envid;
159 guint expires;
160 gboolean connected;
163 struct sipe_groupchat_msg {
164 GHashTable *container;
165 struct sipe_chat_session *session;
166 gchar *content;
167 gchar *xccos;
168 guint envid;
171 /* GDestroyNotify */
172 static void sipe_groupchat_msg_free(gpointer data) {
173 struct sipe_groupchat_msg *msg = data;
174 g_free(msg->content);
175 g_free(msg->xccos);
176 g_free(msg);
179 /* GDestroyNotify */
180 static void sipe_groupchat_msg_remove(gpointer data) {
181 struct sipe_groupchat_msg *msg = data;
182 g_hash_table_remove(msg->container, &msg->envid);
185 static void sipe_groupchat_allocate(struct sipe_core_private *sipe_private)
187 struct sipe_groupchat *groupchat = g_new0(struct sipe_groupchat, 1);
189 groupchat->uri_to_chat_session = g_hash_table_new(g_str_hash, g_str_equal);
190 groupchat->msgs = g_hash_table_new_full(g_int_hash, g_int_equal,
191 NULL,
192 sipe_groupchat_msg_free);
193 groupchat->envid = rand();
194 groupchat->connected = FALSE;
195 sipe_private->groupchat = groupchat;
198 static void sipe_groupchat_free_join_queue(struct sipe_groupchat *groupchat)
200 sipe_utils_slist_free_full(groupchat->join_queue, g_free);
201 groupchat->join_queue = NULL;
204 void sipe_groupchat_free(struct sipe_core_private *sipe_private)
206 struct sipe_groupchat *groupchat = sipe_private->groupchat;
207 if (groupchat) {
208 sipe_groupchat_free_join_queue(groupchat);
209 g_hash_table_destroy(groupchat->msgs);
210 g_hash_table_destroy(groupchat->uri_to_chat_session);
211 g_free(groupchat->domain);
212 g_free(groupchat);
213 sipe_private->groupchat = NULL;
217 static struct sipe_groupchat_msg *generate_xccos_message(struct sipe_groupchat *groupchat,
218 const gchar *content)
220 struct sipe_groupchat_msg *msg = g_new0(struct sipe_groupchat_msg, 1);
222 msg->container = groupchat->msgs;
223 msg->envid = groupchat->envid++;
224 msg->xccos = g_strdup_printf("<xccos ver=\"1\" envid=\"%u\" xmlns=\"urn:parlano:xml:ns:xccos\">"
225 "%s"
226 "</xccos>",
227 msg->envid,
228 content);
230 g_hash_table_insert(groupchat->msgs, &msg->envid, msg);
232 return(msg);
236 * Create short-lived dialog with ocschat@<domain> (or user specified value)
237 * This initiates the Group Chat feature
239 void sipe_groupchat_init(struct sipe_core_private *sipe_private)
241 const gchar *setting = sipe_backend_setting(SIPE_CORE_PUBLIC,
242 SIPE_SETTING_GROUPCHAT_USER);
243 const gchar *persistent = sipe_private->persistentChatPool_uri;
244 gboolean user_set = !is_empty(setting);
245 gboolean provisioned = !is_empty(persistent);
246 gchar **parts = g_strsplit(user_set ? setting :
247 provisioned ? persistent :
248 sipe_private->username, "@", 2);
249 gboolean domain_found = !is_empty(parts[1]);
250 const gchar *user = "ocschat";
251 const gchar *domain = parts[domain_found ? 1 : 0];
252 gchar *chat_uri;
253 struct sip_session *session;
254 struct sipe_groupchat *groupchat;
256 /* User specified or provisioned URI is valid 'user@company.com' */
257 if ((user_set || provisioned) && domain_found && !is_empty(parts[0]))
258 user = parts[0];
260 SIPE_DEBUG_INFO("sipe_groupchat_init: username '%s' setting '%s' persistent '%s' split '%s'/'%s' GC user %s@%s",
261 sipe_private->username, setting ? setting : "(null)",
262 persistent ? persistent : "(null)",
263 parts[0], parts[1] ? parts[1] : "(null)", user, domain);
265 if (!sipe_private->groupchat)
266 sipe_groupchat_allocate(sipe_private);
267 groupchat = sipe_private->groupchat;
269 chat_uri = g_strdup_printf("sip:%s@%s", user, domain);
270 session = sipe_session_find_or_add_im(sipe_private,
271 chat_uri);
272 session->is_groupchat = TRUE;
273 sipe_im_invite(sipe_private, session, chat_uri,
274 NULL, NULL, NULL, FALSE);
276 g_free(groupchat->domain);
277 groupchat->domain = g_strdup(domain);
279 g_free(chat_uri);
280 g_strfreev(parts);
283 /* sipe_schedule_action */
284 static void groupchat_init_retry_cb(struct sipe_core_private *sipe_private,
285 SIPE_UNUSED_PARAMETER gpointer data)
287 sipe_groupchat_init(sipe_private);
290 static void groupchat_init_retry(struct sipe_core_private *sipe_private)
292 struct sipe_groupchat *groupchat = sipe_private->groupchat;
294 SIPE_DEBUG_INFO_NOFORMAT("groupchat_init_retry: trying again later...");
296 groupchat->session = NULL;
297 groupchat->connected = FALSE;
299 sipe_schedule_seconds(sipe_private,
300 "<+groupchat-retry>",
301 NULL,
302 GROUPCHAT_RETRY_TIMEOUT,
303 groupchat_init_retry_cb,
304 NULL);
307 void sipe_groupchat_invite_failed(struct sipe_core_private *sipe_private,
308 struct sip_session *session)
310 struct sipe_groupchat *groupchat = sipe_private->groupchat;
311 const gchar *setting = sipe_backend_setting(SIPE_CORE_PUBLIC,
312 SIPE_SETTING_GROUPCHAT_USER);
313 gboolean retry = FALSE;
315 if (groupchat->session) {
316 /* response to group chat server invite */
317 SIPE_DEBUG_ERROR_NOFORMAT("can't connect to group chat server!");
319 /* group chat server exists, but communication failed */
320 retry = TRUE;
321 } else {
322 /* response to initial invite */
323 SIPE_DEBUG_INFO_NOFORMAT("no group chat server found.");
326 sipe_session_close(sipe_private, session);
328 if (!is_empty(setting)) {
329 gchar *msg = g_strdup_printf(_("Group Chat Proxy setting is incorrect:\n\n\t%s\n\nPlease update your Account."),
330 setting);
331 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
332 _("Couldn't find Group Chat server!"),
333 msg);
334 g_free(msg);
336 /* user specified group chat settings: we should retry */
337 retry = TRUE;
340 if (retry) {
341 groupchat_init_retry(sipe_private);
342 } else {
343 SIPE_DEBUG_INFO_NOFORMAT("disabling group chat feature.");
347 static gchar *generate_chanid_node(const gchar *uri, guint key)
349 /* ma-chan://<domain>/<value> */
350 gchar **parts = g_strsplit(uri, "/", 4);
351 gchar *chanid = NULL;
353 if (parts[2] && parts[3]) {
354 chanid = g_strdup_printf("<chanid key=\"%d\" domain=\"%s\" value=\"%s\"/>",
355 key, parts[2], parts[3]);
356 } else {
357 SIPE_DEBUG_ERROR("generate_chanid_node: mal-formed URI '%s'",
358 uri);
360 g_strfreev(parts);
362 return chanid;
365 /* TransCallback */
366 static gboolean groupchat_expired_session_response(struct sipe_core_private *sipe_private,
367 struct sipmsg *msg,
368 SIPE_UNUSED_PARAMETER struct transaction *trans)
370 /* 481 Call Leg Does Not Exist -> server dropped session */
371 if (msg->response == 481) {
372 struct sipe_groupchat *groupchat = sipe_private->groupchat;
373 groupchat->session = NULL;
374 groupchat->connected = FALSE;
375 sipe_groupchat_init(sipe_private);
378 return(TRUE);
381 /* sipe_schedule_action */
382 static void groupchat_update_cb(struct sipe_core_private *sipe_private,
383 SIPE_UNUSED_PARAMETER gpointer data)
385 struct sipe_groupchat *groupchat = sipe_private->groupchat;
387 if (groupchat->session) {
388 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
389 groupchat->session->with);
391 if (dialog) {
392 sip_transport_update(sipe_private,
393 dialog,
394 groupchat_expired_session_response);
395 sipe_schedule_seconds(sipe_private,
396 "<+groupchat-expires>",
397 NULL,
398 groupchat->expires,
399 groupchat_update_cb,
400 NULL);
405 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
406 const gchar *cmd);
408 void sipe_groupchat_invite_response(struct sipe_core_private *sipe_private,
409 struct sip_dialog *dialog,
410 struct sipmsg *response)
412 struct sipe_groupchat *groupchat = sipe_private->groupchat;
414 SIPE_DEBUG_INFO_NOFORMAT("sipe_groupchat_invite_response");
416 if (!groupchat->session) {
417 /* response to initial invite */
418 struct sipe_groupchat_msg *msg = generate_xccos_message(groupchat,
419 "<cmd id=\"cmd:requri\" seqid=\"1\"><data/></cmd>");
420 const gchar *session_expires = sipmsg_find_header(response,
421 "Session-Expires");
423 sip_transport_info(sipe_private,
424 "Content-Type: text/plain\r\n",
425 msg->xccos,
426 dialog,
427 NULL);
428 sipe_groupchat_msg_remove(msg);
430 if (session_expires) {
431 groupchat->expires = strtoul(session_expires, NULL, 10);
433 if (groupchat->expires) {
434 SIPE_DEBUG_INFO("sipe_groupchat_invite_response: session expires in %d seconds",
435 groupchat->expires);
437 if (groupchat->expires > 10)
438 groupchat->expires -= 10;
439 sipe_schedule_seconds(sipe_private,
440 "<+groupchat-expires>",
441 NULL,
442 groupchat->expires,
443 groupchat_update_cb,
444 NULL);
448 } else {
449 /* response to group chat server invite */
450 gchar *invcmd;
452 SIPE_DEBUG_INFO_NOFORMAT("connection to group chat server established.");
454 groupchat->connected = TRUE;
456 /* Any queued joins? */
457 if (groupchat->join_queue) {
458 GString *cmd = g_string_new("<cmd id=\"cmd:bjoin\" seqid=\"1\">"
459 "<data>");
460 GSList *entry;
461 guint i = 0;
463 /* We used g_slist_prepend() to create the list */
464 groupchat->join_queue = entry = g_slist_reverse(groupchat->join_queue);
465 while (entry) {
466 gchar *chanid = generate_chanid_node(entry->data, i++);
467 g_string_append(cmd, chanid);
468 g_free(chanid);
469 entry = entry->next;
471 sipe_groupchat_free_join_queue(groupchat);
473 g_string_append(cmd, "</data></cmd>");
474 chatserver_command(sipe_private, cmd->str);
475 g_string_free(cmd, TRUE);
478 /* Request outstanding invites from server */
479 invcmd = g_strdup_printf("<cmd id=\"cmd:getinv\" seqid=\"1\">"
480 "<data>"
481 "<inv inviteId=\"1\" domain=\"%s\"/>"
482 "</data>"
483 "</cmd>", groupchat->domain);
484 chatserver_command(sipe_private, invcmd);
485 g_free(invcmd);
489 static void chatserver_command_error_notify(struct sipe_core_private *sipe_private,
490 struct sipe_chat_session *chat_session,
491 const gchar *content)
493 gchar *label = g_strdup_printf(_("This message was not delivered to chat room '%s'"),
494 chat_session->title);
495 gchar *errmsg = g_strdup_printf("%s:\n<font color=\"#888888\"></b>%s<b></font>",
496 label, content);
497 g_free(label);
498 sipe_backend_notify_message_error(SIPE_CORE_PUBLIC,
499 chat_session->backend,
500 NULL,
501 errmsg);
502 g_free(errmsg);
505 /* TransCallback */
506 static gboolean chatserver_command_response(struct sipe_core_private *sipe_private,
507 struct sipmsg *msg,
508 struct transaction *trans)
510 if (msg->response != 200) {
511 struct sipe_groupchat_msg *gmsg = trans->payload->data;
512 struct sipe_chat_session *chat_session = gmsg->session;
514 SIPE_DEBUG_INFO("chatserver_command_response: failure %d", msg->response);
516 if (chat_session)
517 chatserver_command_error_notify(sipe_private,
518 chat_session,
519 gmsg->content);
521 groupchat_expired_session_response(sipe_private, msg, trans);
523 return TRUE;
526 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
527 const gchar *cmd)
529 struct sipe_groupchat *groupchat = sipe_private->groupchat;
530 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
531 groupchat->session->with);
532 struct sipe_groupchat_msg *msg = NULL;
534 if (dialog) {
535 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
536 struct transaction *trans;
538 msg = generate_xccos_message(groupchat, cmd);
539 trans = sip_transport_info(sipe_private,
540 "Content-Type: text/plain\r\n",
541 msg->xccos,
542 dialog,
543 chatserver_command_response);
545 payload->destroy = sipe_groupchat_msg_remove;
546 payload->data = msg;
547 trans->payload = payload;
550 return(msg);
553 static void chatserver_response_uri(struct sipe_core_private *sipe_private,
554 struct sip_session *session,
555 SIPE_UNUSED_PARAMETER guint result,
556 SIPE_UNUSED_PARAMETER const gchar *message,
557 const sipe_xml *xml)
559 const sipe_xml *uib = sipe_xml_child(xml, "uib");
560 const gchar *uri = sipe_xml_attribute(uib, "uri");
562 /* drop connection to ocschat@<domain> again */
563 sipe_session_close(sipe_private, session);
565 if (uri) {
566 struct sipe_groupchat *groupchat = sipe_private->groupchat;
568 SIPE_DEBUG_INFO("chatserver_response_uri: '%s'", uri);
570 groupchat->session = session = sipe_session_find_or_add_im(sipe_private,
571 uri);
573 session->is_groupchat = TRUE;
574 sipe_im_invite(sipe_private, session, uri, NULL, NULL, NULL, FALSE);
575 } else {
576 SIPE_DEBUG_WARNING_NOFORMAT("chatserver_response_uri: no server URI found!");
577 groupchat_init_retry(sipe_private);
581 static void chatserver_response_channel_search(struct sipe_core_private *sipe_private,
582 SIPE_UNUSED_PARAMETER struct sip_session *session,
583 guint result,
584 const gchar *message,
585 const sipe_xml *xml)
587 struct sipe_core_public *sipe_public = SIPE_CORE_PUBLIC;
589 if (result != 200) {
590 sipe_backend_notify_error(sipe_public,
591 _("Error retrieving room list"),
592 message);
593 } else {
594 const sipe_xml *chanib;
596 for (chanib = sipe_xml_child(xml, "chanib");
597 chanib;
598 chanib = sipe_xml_twin(chanib)) {
599 const gchar *name = sipe_xml_attribute(chanib, "name");
600 const gchar *desc = sipe_xml_attribute(chanib, "description");
601 const gchar *uri = sipe_xml_attribute(chanib, "uri");
602 const sipe_xml *node;
603 guint user_count = 0;
604 guint32 flags = 0;
606 /* information */
607 for (node = sipe_xml_child(chanib, "info");
608 node;
609 node = sipe_xml_twin(node)) {
610 const gchar *id = sipe_xml_attribute(node, "id");
611 gchar *data;
613 if (!id) continue;
615 data = sipe_xml_data(node);
616 if (data) {
617 if (sipe_strcase_equal(id, "urn:parlano:ma:info:ucnt")) {
618 user_count = g_ascii_strtoll(data, NULL, 10);
619 } else if (sipe_strcase_equal(id, "urn:parlano:ma:info:visibilty")) {
620 if (sipe_strcase_equal(data, "private")) {
621 flags |= SIPE_GROUPCHAT_ROOM_PRIVATE;
624 g_free(data);
628 /* properties */
629 for (node = sipe_xml_child(chanib, "prop");
630 node;
631 node = sipe_xml_twin(node)) {
632 const gchar *id = sipe_xml_attribute(node, "id");
633 gchar *data;
635 if (!id) continue;
637 data = sipe_xml_data(node);
638 if (data) {
639 gboolean value = sipe_strcase_equal(data, "true");
640 g_free(data);
642 if (value) {
643 guint32 add = 0;
644 if (sipe_strcase_equal(id, "urn:parlano:ma:prop:filepost")) {
645 add = SIPE_GROUPCHAT_ROOM_FILEPOST;
646 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:invite")) {
647 add = SIPE_GROUPCHAT_ROOM_INVITE;
648 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:logged")) {
649 add = SIPE_GROUPCHAT_ROOM_LOGGED;
651 flags |= add;
656 SIPE_DEBUG_INFO("group chat channel '%s': '%s' (%s) with %u users, flags 0x%x",
657 name, desc, uri, user_count, flags);
658 sipe_backend_groupchat_room_add(sipe_public,
659 uri, name, desc,
660 user_count, flags);
664 sipe_backend_groupchat_room_terminate(sipe_public);
667 static gboolean is_chanop(const sipe_xml *aib)
669 return sipe_strequal(sipe_xml_attribute(aib, "key"),
670 GROUPCHAT_AIB_KEY_CHANOP);
673 static void add_user(struct sipe_chat_session *chat_session,
674 const gchar *uri,
675 gboolean new, gboolean chanop)
677 SIPE_DEBUG_INFO("add_user: %s%s%s to room %s (%s)",
678 new ? "new " : "",
679 chanop ? "chanop " : "",
680 uri,
681 chat_session->title, chat_session->id);
682 sipe_backend_chat_add(chat_session->backend, uri, new);
683 if (chanop)
684 sipe_backend_chat_operator(chat_session->backend, uri);
687 static void chatserver_response_join(struct sipe_core_private *sipe_private,
688 SIPE_UNUSED_PARAMETER struct sip_session *session,
689 guint result,
690 const gchar *message,
691 const sipe_xml *xml)
693 if (result != 200) {
694 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
695 _("Error joining chat room"),
696 message);
697 } else {
698 struct sipe_groupchat *groupchat = sipe_private->groupchat;
699 const sipe_xml *node;
700 GHashTable *user_ids = g_hash_table_new(g_str_hash, g_str_equal);
702 /* Extract user IDs & URIs and generate ID -> URI map */
703 for (node = sipe_xml_child(xml, "uib");
704 node;
705 node = sipe_xml_twin(node)) {
706 const gchar *id = sipe_xml_attribute(node, "id");
707 const gchar *uri = sipe_xml_attribute(node, "uri");
708 if (id && uri)
709 g_hash_table_insert(user_ids,
710 (gpointer) id,
711 (gpointer) uri);
714 /* Process channel data */
715 for (node = sipe_xml_child(xml, "chanib");
716 node;
717 node = sipe_xml_twin(node)) {
718 const gchar *uri = sipe_xml_attribute(node, "uri");
720 if (uri) {
721 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
722 uri);
723 gboolean new = (chat_session == NULL);
724 const gchar *attr = sipe_xml_attribute(node, "name");
725 gchar *self = sip_uri_self(sipe_private);
726 const sipe_xml *aib;
728 if (new) {
729 chat_session = sipe_chat_create_session(SIPE_CHAT_TYPE_GROUPCHAT,
730 sipe_xml_attribute(node,
731 "uri"),
732 attr ? attr : "");
733 g_hash_table_insert(groupchat->uri_to_chat_session,
734 chat_session->id,
735 chat_session);
737 SIPE_DEBUG_INFO("joined room '%s' (%s)",
738 chat_session->title,
739 chat_session->id);
740 chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
741 chat_session,
742 chat_session->title,
743 self);
744 } else {
745 SIPE_DEBUG_INFO("rejoining room '%s' (%s)",
746 chat_session->title,
747 chat_session->id);
748 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
749 chat_session->backend,
750 self,
751 chat_session->title);
753 g_free(self);
755 attr = sipe_xml_attribute(node, "topic");
756 if (attr) {
757 sipe_backend_chat_topic(chat_session->backend,
758 attr);
761 /* Process user map for channel */
762 for (aib = sipe_xml_child(node, "aib");
763 aib;
764 aib = sipe_xml_twin(aib)) {
765 const gchar *value = sipe_xml_attribute(aib, "value");
766 gboolean chanop = is_chanop(aib);
767 gchar **ids = g_strsplit(value, ",", 0);
769 if (ids) {
770 gchar **uid = ids;
772 while (*uid) {
773 const gchar *uri = g_hash_table_lookup(user_ids,
774 *uid);
775 if (uri)
776 add_user(chat_session,
777 uri,
778 FALSE,
779 chanop);
780 uid++;
783 g_strfreev(ids);
787 /* Request last 25 entries from channel history */
788 self = g_strdup_printf("<cmd id=\"cmd:bccontext\" seqid=\"1\">"
789 "<data>"
790 "<chanib uri=\"%s\"/>"
791 "<bcq><last cnt=\"25\"/></bcq>"
792 "</data>"
793 "</cmd>", chat_session->id);
794 chatserver_command(sipe_private, self);
795 g_free(self);
799 g_hash_table_destroy(user_ids);
803 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
804 const sipe_xml *grpchat);
806 static void chatserver_response_history(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
807 SIPE_UNUSED_PARAMETER struct sip_session *session,
808 SIPE_UNUSED_PARAMETER guint result,
809 SIPE_UNUSED_PARAMETER const gchar *message,
810 const sipe_xml *xml)
812 const sipe_xml *grpchat;
814 for (grpchat = sipe_xml_child(xml, "chanib/msg");
815 grpchat;
816 grpchat = sipe_xml_twin(grpchat))
817 if (sipe_strequal(sipe_xml_attribute(grpchat, "id"),
818 "grpchat"))
819 chatserver_grpchat_message(sipe_private, grpchat);
822 static void chatserver_response_part(struct sipe_core_private *sipe_private,
823 SIPE_UNUSED_PARAMETER struct sip_session *session,
824 guint result,
825 const gchar *message,
826 const sipe_xml *xml)
828 if (result != 200) {
829 SIPE_DEBUG_WARNING("chatserver_response_part: failed with %d: %s. Dropping room",
830 result, message);
831 } else {
832 struct sipe_groupchat *groupchat = sipe_private->groupchat;
833 const gchar *uri = sipe_xml_attribute(sipe_xml_child(xml, "chanib"),
834 "uri");
835 struct sipe_chat_session *chat_session;
837 if (uri &&
838 (chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
839 uri))) {
841 SIPE_DEBUG_INFO("leaving room '%s' (%s)",
842 chat_session->title, chat_session->id);
844 g_hash_table_remove(groupchat->uri_to_chat_session,
845 uri);
846 sipe_chat_remove_session(chat_session);
848 } else {
849 SIPE_DEBUG_WARNING("chatserver_response_part: unknown chat room uri '%s'",
850 uri ? uri : "");
855 static void chatserver_notice_join(struct sipe_core_private *sipe_private,
856 SIPE_UNUSED_PARAMETER struct sip_session *session,
857 SIPE_UNUSED_PARAMETER guint result,
858 SIPE_UNUSED_PARAMETER const gchar *message,
859 const sipe_xml *xml)
861 struct sipe_groupchat *groupchat = sipe_private->groupchat;
862 const sipe_xml *uib;
864 for (uib = sipe_xml_child(xml, "uib");
865 uib;
866 uib = sipe_xml_twin(uib)) {
867 const gchar *uri = sipe_xml_attribute(uib, "uri");
869 if (uri) {
870 const sipe_xml *aib;
872 for (aib = sipe_xml_child(uib, "aib");
873 aib;
874 aib = sipe_xml_twin(aib)) {
875 const gchar *domain = sipe_xml_attribute(aib, "domain");
876 const gchar *path = sipe_xml_attribute(aib, "value");
878 if (domain && path) {
879 gchar *room_uri = g_strdup_printf("ma-chan://%s/%s",
880 domain, path);
881 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
882 room_uri);
883 if (chat_session)
884 add_user(chat_session,
885 uri,
886 TRUE,
887 is_chanop(aib));
889 g_free(room_uri);
896 static void chatserver_notice_part(struct sipe_core_private *sipe_private,
897 SIPE_UNUSED_PARAMETER struct sip_session *session,
898 SIPE_UNUSED_PARAMETER guint result,
899 SIPE_UNUSED_PARAMETER const gchar *message,
900 const sipe_xml *xml)
902 struct sipe_groupchat *groupchat = sipe_private->groupchat;
903 const sipe_xml *chanib;
905 for (chanib = sipe_xml_child(xml, "chanib");
906 chanib;
907 chanib = sipe_xml_twin(chanib)) {
908 const gchar *room_uri = sipe_xml_attribute(chanib, "uri");
910 if (room_uri) {
911 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
912 room_uri);
914 if (chat_session) {
915 const sipe_xml *uib;
917 for (uib = sipe_xml_child(chanib, "uib");
918 uib;
919 uib = sipe_xml_twin(uib)) {
920 const gchar *uri = sipe_xml_attribute(uib, "uri");
922 if (uri) {
923 SIPE_DEBUG_INFO("remove_user: %s from room %s (%s)",
924 uri,
925 chat_session->title,
926 chat_session->id);
927 sipe_backend_chat_remove(chat_session->backend,
928 uri);
936 static const struct response {
937 const gchar *key;
938 void (* const handler)(struct sipe_core_private *,
939 struct sip_session *,
940 guint result, const gchar *,
941 const sipe_xml *xml);
942 } response_table[] = {
943 { "rpl:requri", chatserver_response_uri },
944 { "rpl:chansrch", chatserver_response_channel_search },
945 { "rpl:join", chatserver_response_join },
946 { "rpl:bjoin", chatserver_response_join },
947 { "rpl:bccontext", chatserver_response_history },
948 { "rpl:part", chatserver_response_part },
949 { "ntc:join", chatserver_notice_join },
950 { "ntc:bjoin", chatserver_notice_join },
951 { "ntc:part", chatserver_notice_part },
952 { NULL, NULL }
955 /* Handles rpl:XXX & ntc:YYY */
956 static void chatserver_response(struct sipe_core_private *sipe_private,
957 const sipe_xml *reply,
958 struct sip_session *session)
960 do {
961 const sipe_xml *resp, *data;
962 const gchar *id;
963 gchar *message;
964 guint result = 500;
965 const struct response *r;
967 id = sipe_xml_attribute(reply, "id");
968 if (!id) {
969 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: no reply ID found!");
970 continue;
973 resp = sipe_xml_child(reply, "resp");
974 if (resp) {
975 result = sipe_xml_int_attribute(resp, "code", 500);
976 message = sipe_xml_data(resp);
977 } else {
978 message = g_strdup("");
981 data = sipe_xml_child(reply, "data");
983 SIPE_DEBUG_INFO("chatserver_response: '%s' result (%d) %s",
984 id, result, message ? message : "");
986 for (r = response_table; r->key; r++) {
987 if (sipe_strcase_equal(id, r->key)) {
988 (*r->handler)(sipe_private, session, result, message, data);
989 break;
992 if (!r->key) {
993 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: ignoring unknown response");
996 g_free(message);
997 } while ((reply = sipe_xml_twin(reply)) != NULL);
1000 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
1001 const sipe_xml *grpchat)
1003 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1004 const gchar *uri = sipe_xml_attribute(grpchat, "chanUri");
1005 const gchar *from = sipe_xml_attribute(grpchat, "author");
1006 time_t when = sipe_utils_str_to_time(sipe_xml_attribute(grpchat, "ts"));
1007 gchar *text = sipe_xml_data(sipe_xml_child(grpchat, "chat"));
1008 struct sipe_chat_session *chat_session;
1009 gchar *escaped;
1011 if (!uri || !from) {
1012 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' received without chat room URI or author!",
1013 text ? text : "");
1014 g_free(text);
1015 return;
1018 chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1019 uri);
1020 if (!chat_session) {
1021 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' from '%s' received from unknown chat room '%s'!",
1022 text ? text : "", from, uri);
1023 g_free(text);
1024 return;
1027 /* libxml2 decodes all entities, but the backend expects HTML */
1028 escaped = g_markup_escape_text(text, -1);
1029 g_free(text);
1030 sipe_backend_chat_message(SIPE_CORE_PUBLIC, chat_session->backend,
1031 from, when, escaped);
1032 g_free(escaped);
1035 void process_incoming_info_groupchat(struct sipe_core_private *sipe_private,
1036 struct sipmsg *msg,
1037 struct sip_session *session)
1039 sipe_xml *xml = sipe_xml_parse(msg->body, msg->bodylen);
1040 const sipe_xml *node;
1041 const gchar *callid;
1042 struct sip_dialog *dialog;
1044 callid = sipmsg_find_header(msg, "Call-ID");
1045 dialog = sipe_dialog_find(session, session->with);
1046 if (sipe_strequal(callid, dialog->callid)) {
1048 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1050 if (((node = sipe_xml_child(xml, "rpl")) != NULL) ||
1051 ((node = sipe_xml_child(xml, "ntc")) != NULL)) {
1052 chatserver_response(sipe_private, node, session);
1053 } else if ((node = sipe_xml_child(xml, "grpchat")) != NULL) {
1054 chatserver_grpchat_message(sipe_private, node);
1055 } else {
1056 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_info_groupchat: ignoring unknown response");
1059 } else {
1061 * Our last session got disconnected without proper shutdown,
1062 * e.g. by Pidgin crashing or network connection loss. When
1063 * we reconnect to the group chat the server will send INFO
1064 * messages to the current *AND* the obsolete Call-ID, until
1065 * the obsolete session expires.
1067 * Ignore these INFO messages to avoid, e.g. duplicate texts,
1068 * and respond with an error so that the server knows that we
1069 * consider this dialog to be terminated.
1071 SIPE_DEBUG_INFO("process_incoming_info_groupchat: ignoring unsolicited INFO message to obsolete Call-ID: %s",
1072 callid);
1074 sip_transport_response(sipe_private, msg, 487, "Request Terminated", NULL);
1077 sipe_xml_free(xml);
1080 void sipe_groupchat_send(struct sipe_core_private *sipe_private,
1081 struct sipe_chat_session *chat_session,
1082 const gchar *what)
1084 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1085 gchar *cmd, *self, *timestamp, *tmp;
1086 gchar **lines, **strvp;
1087 struct sipe_groupchat_msg *msg;
1089 if (!groupchat || !chat_session)
1090 return;
1092 SIPE_DEBUG_INFO("sipe_groupchat_send: '%s' to %s",
1093 what, chat_session->id);
1095 self = sip_uri_self(sipe_private);
1096 timestamp = sipe_utils_time_to_str(time(NULL));
1099 * 'what' is already XML-escaped, e.g.
1101 * " -> &quot;
1102 * > -> &gt;
1103 * < -> &lt;
1104 * & -> &amp;
1106 * Group Chat only accepts plain text, not full HTML. So we have to
1107 * strip all HTML tags and XML escape the text.
1109 * Line breaks are encoded as <br> and therefore need to be replaced
1110 * before stripping. In order to prevent HTML stripping to strip line
1111 * endings, we need to split the text into lines on <br>.
1113 lines = g_strsplit(what, "<br>", 0);
1114 for (strvp = lines; *strvp; strvp++) {
1115 /* replace array entry with HTML stripped & XML escaped version */
1116 gchar *stripped = sipe_backend_markup_strip_html(*strvp);
1117 gchar *escaped = g_markup_escape_text(stripped, -1);
1118 g_free(stripped);
1119 g_free(*strvp);
1120 *strvp = escaped;
1122 tmp = g_strjoinv("\r\n", lines);
1123 g_strfreev(lines);
1124 cmd = g_strdup_printf("<grpchat id=\"grpchat\" seqid=\"1\" chanUri=\"%s\" author=\"%s\" ts=\"%s\">"
1125 "<chat>%s</chat>"
1126 "</grpchat>",
1127 chat_session->id, self, timestamp, tmp);
1128 g_free(tmp);
1129 g_free(timestamp);
1130 g_free(self);
1131 msg = chatserver_command(sipe_private, cmd);
1132 g_free(cmd);
1134 if (msg) {
1135 msg->session = chat_session;
1136 msg->content = g_strdup(what);
1137 } else {
1138 chatserver_command_error_notify(sipe_private,
1139 chat_session,
1140 what);
1144 void sipe_groupchat_leave(struct sipe_core_private *sipe_private,
1145 struct sipe_chat_session *chat_session)
1147 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1148 gchar *cmd;
1150 if (!groupchat || !chat_session)
1151 return;
1153 SIPE_DEBUG_INFO("sipe_groupchat_leave: %s", chat_session->id);
1155 cmd = g_strdup_printf("<cmd id=\"cmd:part\" seqid=\"1\">"
1156 "<data>"
1157 "<chanib uri=\"%s\"/>"
1158 "</data>"
1159 "</cmd>", chat_session->id);
1160 chatserver_command(sipe_private, cmd);
1161 g_free(cmd);
1164 gboolean sipe_core_groupchat_query_rooms(struct sipe_core_public *sipe_public)
1166 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1167 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1169 if (!groupchat || !groupchat->connected)
1170 return FALSE;
1172 chatserver_command(sipe_private,
1173 "<cmd id=\"cmd:chansrch\" seqid=\"1\">"
1174 "<data>"
1175 "<qib qtype=\"BYNAME\" criteria=\"\" extended=\"false\"/>"
1176 "</data>"
1177 "</cmd>");
1179 return TRUE;
1182 void sipe_core_groupchat_join(struct sipe_core_public *sipe_public,
1183 const gchar *uri)
1185 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1186 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1188 if (!g_str_has_prefix(uri, "ma-chan://"))
1189 return;
1191 if (!groupchat) {
1192 /* This happens when a user has set auto-join on a channel */
1193 sipe_groupchat_allocate(sipe_private);
1194 groupchat = sipe_private->groupchat;
1197 if (groupchat->connected) {
1198 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1199 uri);
1201 /* Already joined? */
1202 if (chat_session) {
1204 /* Yes, update backend session */
1205 SIPE_DEBUG_INFO("sipe_core_groupchat_join: show '%s' (%s)",
1206 chat_session->title,
1207 chat_session->id);
1208 sipe_backend_chat_show(chat_session->backend);
1210 } else {
1211 /* No, send command out directly */
1212 gchar *chanid = generate_chanid_node(uri, 0);
1213 if (chanid) {
1214 gchar *cmd = g_strdup_printf("<cmd id=\"cmd:join\" seqid=\"1\">"
1215 "<data>%s</data>"
1216 "</cmd>",
1217 chanid);
1218 SIPE_DEBUG_INFO("sipe_core_groupchat_join: join %s",
1219 uri);
1220 chatserver_command(sipe_private, cmd);
1221 g_free(cmd);
1222 g_free(chanid);
1225 } else {
1226 /* Add it to the queue but avoid duplicates */
1227 if (!g_slist_find_custom(groupchat->join_queue, uri,
1228 sipe_strcompare)) {
1229 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_groupchat_join: URI queued");
1230 groupchat->join_queue = g_slist_prepend(groupchat->join_queue,
1231 g_strdup(uri));
1236 void sipe_groupchat_rejoin(struct sipe_core_private *sipe_private,
1237 struct sipe_chat_session *chat_session)
1239 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1241 if (!groupchat) {
1242 /* First rejoined channel after reconnect will trigger this */
1243 sipe_groupchat_allocate(sipe_private);
1244 groupchat = sipe_private->groupchat;
1247 /* Remember "old" session, so that we don't recreate it at join */
1248 g_hash_table_insert(groupchat->uri_to_chat_session,
1249 chat_session->id,
1250 chat_session);
1251 sipe_core_groupchat_join(SIPE_CORE_PUBLIC, chat_session->id);
1255 Local Variables:
1256 mode: c
1257 c-file-style: "bsd"
1258 indent-tabs-mode: t
1259 tab-width: 8
1260 End: