Fix #222: SIPE crashes when groupchat session expires (I)
[siplcs.git] / src / core / sipe-groupchat.c
blob72eb56b69477f689df96223dd4d438516634773f
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 /* sipe_schedule_action */
366 static void groupchat_update_cb(struct sipe_core_private *sipe_private,
367 SIPE_UNUSED_PARAMETER gpointer data)
369 struct sipe_groupchat *groupchat = sipe_private->groupchat;
370 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
371 groupchat->session->with);
373 if (dialog)
374 sip_transport_update(sipe_private, dialog);
375 sipe_schedule_seconds(sipe_private,
376 "<+groupchat-expires>",
377 NULL,
378 groupchat->expires,
379 groupchat_update_cb,
380 NULL);
383 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
384 const gchar *cmd);
386 void sipe_groupchat_invite_response(struct sipe_core_private *sipe_private,
387 struct sip_dialog *dialog,
388 struct sipmsg *response)
390 struct sipe_groupchat *groupchat = sipe_private->groupchat;
392 SIPE_DEBUG_INFO_NOFORMAT("sipe_groupchat_invite_response");
394 if (!groupchat->session) {
395 /* response to initial invite */
396 struct sipe_groupchat_msg *msg = generate_xccos_message(groupchat,
397 "<cmd id=\"cmd:requri\" seqid=\"1\"><data/></cmd>");
398 const gchar *session_expires = sipmsg_find_header(response,
399 "Session-Expires");
401 sip_transport_info(sipe_private,
402 "Content-Type: text/plain\r\n",
403 msg->xccos,
404 dialog,
405 NULL);
406 sipe_groupchat_msg_remove(msg);
408 if (session_expires) {
409 groupchat->expires = strtoul(session_expires, NULL, 10);
411 if (groupchat->expires) {
412 SIPE_DEBUG_INFO("sipe_groupchat_invite_response: session expires in %d seconds",
413 groupchat->expires);
415 if (groupchat->expires > 10)
416 groupchat->expires -= 10;
417 sipe_schedule_seconds(sipe_private,
418 "<+groupchat-expires>",
419 NULL,
420 groupchat->expires,
421 groupchat_update_cb,
422 NULL);
426 } else {
427 /* response to group chat server invite */
428 gchar *invcmd;
430 SIPE_DEBUG_INFO_NOFORMAT("connection to group chat server established.");
432 groupchat->connected = TRUE;
434 /* Any queued joins? */
435 if (groupchat->join_queue) {
436 GString *cmd = g_string_new("<cmd id=\"cmd:bjoin\" seqid=\"1\">"
437 "<data>");
438 GSList *entry;
439 guint i = 0;
441 /* We used g_slist_prepend() to create the list */
442 groupchat->join_queue = entry = g_slist_reverse(groupchat->join_queue);
443 while (entry) {
444 gchar *chanid = generate_chanid_node(entry->data, i++);
445 g_string_append(cmd, chanid);
446 g_free(chanid);
447 entry = entry->next;
449 sipe_groupchat_free_join_queue(groupchat);
451 g_string_append(cmd, "</data></cmd>");
452 chatserver_command(sipe_private, cmd->str);
453 g_string_free(cmd, TRUE);
456 /* Request outstanding invites from server */
457 invcmd = g_strdup_printf("<cmd id=\"cmd:getinv\" seqid=\"1\">"
458 "<data>"
459 "<inv inviteId=\"1\" domain=\"%s\"/>"
460 "</data>"
461 "</cmd>", groupchat->domain);
462 chatserver_command(sipe_private, invcmd);
463 g_free(invcmd);
467 /* TransCallback */
468 static gboolean chatserver_command_response(struct sipe_core_private *sipe_private,
469 struct sipmsg *msg,
470 struct transaction *trans)
472 if (msg->response != 200) {
473 struct sipe_groupchat_msg *gmsg = trans->payload->data;
474 struct sipe_chat_session *chat_session = gmsg->session;
476 SIPE_DEBUG_INFO("chatserver_command_response: failure %d", msg->response);
478 if (chat_session) {
479 gchar *label = g_strdup_printf(_("This message was not delivered to chat room '%s'"),
480 chat_session->title);
481 gchar *errmsg = g_strdup_printf("%s:\n<font color=\"#888888\"></b>%s<b></font>",
482 label, gmsg->content);
483 g_free(label);
484 sipe_backend_notify_message_error(SIPE_CORE_PUBLIC,
485 chat_session->backend,
486 NULL,
487 errmsg);
488 g_free(errmsg);
491 return TRUE;
494 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
495 const gchar *cmd)
497 struct sipe_groupchat *groupchat = sipe_private->groupchat;
498 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
499 groupchat->session->with);
500 struct sipe_groupchat_msg *msg = NULL;
502 if (dialog) {
503 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
504 struct transaction *trans;
506 msg = generate_xccos_message(groupchat, cmd);
507 trans = sip_transport_info(sipe_private,
508 "Content-Type: text/plain\r\n",
509 msg->xccos,
510 dialog,
511 chatserver_command_response);
513 payload->destroy = sipe_groupchat_msg_remove;
514 payload->data = msg;
515 trans->payload = payload;
518 return(msg);
521 static void chatserver_response_uri(struct sipe_core_private *sipe_private,
522 struct sip_session *session,
523 SIPE_UNUSED_PARAMETER guint result,
524 SIPE_UNUSED_PARAMETER const gchar *message,
525 const sipe_xml *xml)
527 const sipe_xml *uib = sipe_xml_child(xml, "uib");
528 const gchar *uri = sipe_xml_attribute(uib, "uri");
530 /* drop connection to ocschat@<domain> again */
531 sipe_session_close(sipe_private, session);
533 if (uri) {
534 struct sipe_groupchat *groupchat = sipe_private->groupchat;
536 SIPE_DEBUG_INFO("chatserver_response_uri: '%s'", uri);
538 groupchat->session = session = sipe_session_find_or_add_im(sipe_private,
539 uri);
541 session->is_groupchat = TRUE;
542 sipe_im_invite(sipe_private, session, uri, NULL, NULL, NULL, FALSE);
543 } else {
544 SIPE_DEBUG_WARNING_NOFORMAT("chatserver_response_uri: no server URI found!");
545 groupchat_init_retry(sipe_private);
549 static void chatserver_response_channel_search(struct sipe_core_private *sipe_private,
550 SIPE_UNUSED_PARAMETER struct sip_session *session,
551 guint result,
552 const gchar *message,
553 const sipe_xml *xml)
555 struct sipe_core_public *sipe_public = SIPE_CORE_PUBLIC;
557 if (result != 200) {
558 sipe_backend_notify_error(sipe_public,
559 _("Error retrieving room list"),
560 message);
561 } else {
562 const sipe_xml *chanib;
564 for (chanib = sipe_xml_child(xml, "chanib");
565 chanib;
566 chanib = sipe_xml_twin(chanib)) {
567 const gchar *name = sipe_xml_attribute(chanib, "name");
568 const gchar *desc = sipe_xml_attribute(chanib, "description");
569 const gchar *uri = sipe_xml_attribute(chanib, "uri");
570 const sipe_xml *node;
571 guint user_count = 0;
572 guint32 flags = 0;
574 /* information */
575 for (node = sipe_xml_child(chanib, "info");
576 node;
577 node = sipe_xml_twin(node)) {
578 const gchar *id = sipe_xml_attribute(node, "id");
579 gchar *data;
581 if (!id) continue;
583 data = sipe_xml_data(node);
584 if (data) {
585 if (sipe_strcase_equal(id, "urn:parlano:ma:info:ucnt")) {
586 user_count = g_ascii_strtoll(data, NULL, 10);
587 } else if (sipe_strcase_equal(id, "urn:parlano:ma:info:visibilty")) {
588 if (sipe_strcase_equal(data, "private")) {
589 flags |= SIPE_GROUPCHAT_ROOM_PRIVATE;
592 g_free(data);
596 /* properties */
597 for (node = sipe_xml_child(chanib, "prop");
598 node;
599 node = sipe_xml_twin(node)) {
600 const gchar *id = sipe_xml_attribute(node, "id");
601 gchar *data;
603 if (!id) continue;
605 data = sipe_xml_data(node);
606 if (data) {
607 gboolean value = sipe_strcase_equal(data, "true");
608 g_free(data);
610 if (value) {
611 guint32 add = 0;
612 if (sipe_strcase_equal(id, "urn:parlano:ma:prop:filepost")) {
613 add = SIPE_GROUPCHAT_ROOM_FILEPOST;
614 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:invite")) {
615 add = SIPE_GROUPCHAT_ROOM_INVITE;
616 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:logged")) {
617 add = SIPE_GROUPCHAT_ROOM_LOGGED;
619 flags |= add;
624 SIPE_DEBUG_INFO("group chat channel '%s': '%s' (%s) with %u users, flags 0x%x",
625 name, desc, uri, user_count, flags);
626 sipe_backend_groupchat_room_add(sipe_public,
627 uri, name, desc,
628 user_count, flags);
632 sipe_backend_groupchat_room_terminate(sipe_public);
635 static gboolean is_chanop(const sipe_xml *aib)
637 return sipe_strequal(sipe_xml_attribute(aib, "key"),
638 GROUPCHAT_AIB_KEY_CHANOP);
641 static void add_user(struct sipe_chat_session *chat_session,
642 const gchar *uri,
643 gboolean new, gboolean chanop)
645 SIPE_DEBUG_INFO("add_user: %s%s%s to room %s (%s)",
646 new ? "new " : "",
647 chanop ? "chanop " : "",
648 uri,
649 chat_session->title, chat_session->id);
650 sipe_backend_chat_add(chat_session->backend, uri, new);
651 if (chanop)
652 sipe_backend_chat_operator(chat_session->backend, uri);
655 static void chatserver_response_join(struct sipe_core_private *sipe_private,
656 SIPE_UNUSED_PARAMETER struct sip_session *session,
657 guint result,
658 const gchar *message,
659 const sipe_xml *xml)
661 if (result != 200) {
662 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
663 _("Error joining chat room"),
664 message);
665 } else {
666 struct sipe_groupchat *groupchat = sipe_private->groupchat;
667 const sipe_xml *node;
668 GHashTable *user_ids = g_hash_table_new(g_str_hash, g_str_equal);
670 /* Extract user IDs & URIs and generate ID -> URI map */
671 for (node = sipe_xml_child(xml, "uib");
672 node;
673 node = sipe_xml_twin(node)) {
674 const gchar *id = sipe_xml_attribute(node, "id");
675 const gchar *uri = sipe_xml_attribute(node, "uri");
676 if (id && uri)
677 g_hash_table_insert(user_ids,
678 (gpointer) id,
679 (gpointer) uri);
682 /* Process channel data */
683 for (node = sipe_xml_child(xml, "chanib");
684 node;
685 node = sipe_xml_twin(node)) {
686 const gchar *uri = sipe_xml_attribute(node, "uri");
688 if (uri) {
689 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
690 uri);
691 gboolean new = (chat_session == NULL);
692 const gchar *attr = sipe_xml_attribute(node, "name");
693 gchar *self = sip_uri_self(sipe_private);
694 const sipe_xml *aib;
696 if (new) {
697 chat_session = sipe_chat_create_session(SIPE_CHAT_TYPE_GROUPCHAT,
698 sipe_xml_attribute(node,
699 "uri"),
700 attr ? attr : "");
701 g_hash_table_insert(groupchat->uri_to_chat_session,
702 chat_session->id,
703 chat_session);
705 SIPE_DEBUG_INFO("joined room '%s' (%s)",
706 chat_session->title,
707 chat_session->id);
708 chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
709 chat_session,
710 chat_session->title,
711 self);
712 } else {
713 SIPE_DEBUG_INFO("rejoining room '%s' (%s)",
714 chat_session->title,
715 chat_session->id);
716 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
717 chat_session->backend,
718 self,
719 chat_session->title);
721 g_free(self);
723 attr = sipe_xml_attribute(node, "topic");
724 if (attr) {
725 sipe_backend_chat_topic(chat_session->backend,
726 attr);
729 /* Process user map for channel */
730 for (aib = sipe_xml_child(node, "aib");
731 aib;
732 aib = sipe_xml_twin(aib)) {
733 const gchar *value = sipe_xml_attribute(aib, "value");
734 gboolean chanop = is_chanop(aib);
735 gchar **ids = g_strsplit(value, ",", 0);
737 if (ids) {
738 gchar **uid = ids;
740 while (*uid) {
741 const gchar *uri = g_hash_table_lookup(user_ids,
742 *uid);
743 if (uri)
744 add_user(chat_session,
745 uri,
746 FALSE,
747 chanop);
748 uid++;
751 g_strfreev(ids);
755 /* Request last 25 entries from channel history */
756 self = g_strdup_printf("<cmd id=\"cmd:bccontext\" seqid=\"1\">"
757 "<data>"
758 "<chanib uri=\"%s\"/>"
759 "<bcq><last cnt=\"25\"/></bcq>"
760 "</data>"
761 "</cmd>", chat_session->id);
762 chatserver_command(sipe_private, self);
763 g_free(self);
767 g_hash_table_destroy(user_ids);
771 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
772 const sipe_xml *grpchat);
774 static void chatserver_response_history(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
775 SIPE_UNUSED_PARAMETER struct sip_session *session,
776 SIPE_UNUSED_PARAMETER guint result,
777 SIPE_UNUSED_PARAMETER const gchar *message,
778 const sipe_xml *xml)
780 const sipe_xml *grpchat;
782 for (grpchat = sipe_xml_child(xml, "chanib/msg");
783 grpchat;
784 grpchat = sipe_xml_twin(grpchat))
785 if (sipe_strequal(sipe_xml_attribute(grpchat, "id"),
786 "grpchat"))
787 chatserver_grpchat_message(sipe_private, grpchat);
790 static void chatserver_response_part(struct sipe_core_private *sipe_private,
791 SIPE_UNUSED_PARAMETER struct sip_session *session,
792 guint result,
793 const gchar *message,
794 const sipe_xml *xml)
796 if (result != 200) {
797 SIPE_DEBUG_WARNING("chatserver_response_part: failed with %d: %s. Dropping room",
798 result, message);
799 } else {
800 struct sipe_groupchat *groupchat = sipe_private->groupchat;
801 const gchar *uri = sipe_xml_attribute(sipe_xml_child(xml, "chanib"),
802 "uri");
803 struct sipe_chat_session *chat_session;
805 if (uri &&
806 (chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
807 uri))) {
809 SIPE_DEBUG_INFO("leaving room '%s' (%s)",
810 chat_session->title, chat_session->id);
812 g_hash_table_remove(groupchat->uri_to_chat_session,
813 uri);
814 sipe_chat_remove_session(chat_session);
816 } else {
817 SIPE_DEBUG_WARNING("chatserver_response_part: unknown chat room uri '%s'",
818 uri ? uri : "");
823 static void chatserver_notice_join(struct sipe_core_private *sipe_private,
824 SIPE_UNUSED_PARAMETER struct sip_session *session,
825 SIPE_UNUSED_PARAMETER guint result,
826 SIPE_UNUSED_PARAMETER const gchar *message,
827 const sipe_xml *xml)
829 struct sipe_groupchat *groupchat = sipe_private->groupchat;
830 const sipe_xml *uib;
832 for (uib = sipe_xml_child(xml, "uib");
833 uib;
834 uib = sipe_xml_twin(uib)) {
835 const gchar *uri = sipe_xml_attribute(uib, "uri");
837 if (uri) {
838 const sipe_xml *aib;
840 for (aib = sipe_xml_child(uib, "aib");
841 aib;
842 aib = sipe_xml_twin(aib)) {
843 const gchar *domain = sipe_xml_attribute(aib, "domain");
844 const gchar *path = sipe_xml_attribute(aib, "value");
846 if (domain && path) {
847 gchar *room_uri = g_strdup_printf("ma-chan://%s/%s",
848 domain, path);
849 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
850 room_uri);
851 if (chat_session)
852 add_user(chat_session,
853 uri,
854 TRUE,
855 is_chanop(aib));
857 g_free(room_uri);
864 static void chatserver_notice_part(struct sipe_core_private *sipe_private,
865 SIPE_UNUSED_PARAMETER struct sip_session *session,
866 SIPE_UNUSED_PARAMETER guint result,
867 SIPE_UNUSED_PARAMETER const gchar *message,
868 const sipe_xml *xml)
870 struct sipe_groupchat *groupchat = sipe_private->groupchat;
871 const sipe_xml *chanib;
873 for (chanib = sipe_xml_child(xml, "chanib");
874 chanib;
875 chanib = sipe_xml_twin(chanib)) {
876 const gchar *room_uri = sipe_xml_attribute(chanib, "uri");
878 if (room_uri) {
879 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
880 room_uri);
882 if (chat_session) {
883 const sipe_xml *uib;
885 for (uib = sipe_xml_child(chanib, "uib");
886 uib;
887 uib = sipe_xml_twin(uib)) {
888 const gchar *uri = sipe_xml_attribute(uib, "uri");
890 if (uri) {
891 SIPE_DEBUG_INFO("remove_user: %s from room %s (%s)",
892 uri,
893 chat_session->title,
894 chat_session->id);
895 sipe_backend_chat_remove(chat_session->backend,
896 uri);
904 static const struct response {
905 const gchar *key;
906 void (* const handler)(struct sipe_core_private *,
907 struct sip_session *,
908 guint result, const gchar *,
909 const sipe_xml *xml);
910 } response_table[] = {
911 { "rpl:requri", chatserver_response_uri },
912 { "rpl:chansrch", chatserver_response_channel_search },
913 { "rpl:join", chatserver_response_join },
914 { "rpl:bjoin", chatserver_response_join },
915 { "rpl:bccontext", chatserver_response_history },
916 { "rpl:part", chatserver_response_part },
917 { "ntc:join", chatserver_notice_join },
918 { "ntc:bjoin", chatserver_notice_join },
919 { "ntc:part", chatserver_notice_part },
920 { NULL, NULL }
923 /* Handles rpl:XXX & ntc:YYY */
924 static void chatserver_response(struct sipe_core_private *sipe_private,
925 const sipe_xml *reply,
926 struct sip_session *session)
928 do {
929 const sipe_xml *resp, *data;
930 const gchar *id;
931 gchar *message;
932 guint result = 500;
933 const struct response *r;
935 id = sipe_xml_attribute(reply, "id");
936 if (!id) {
937 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: no reply ID found!");
938 continue;
941 resp = sipe_xml_child(reply, "resp");
942 if (resp) {
943 result = sipe_xml_int_attribute(resp, "code", 500);
944 message = sipe_xml_data(resp);
945 } else {
946 message = g_strdup("");
949 data = sipe_xml_child(reply, "data");
951 SIPE_DEBUG_INFO("chatserver_response: '%s' result (%d) %s",
952 id, result, message ? message : "");
954 for (r = response_table; r->key; r++) {
955 if (sipe_strcase_equal(id, r->key)) {
956 (*r->handler)(sipe_private, session, result, message, data);
957 break;
960 if (!r->key) {
961 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: ignoring unknown response");
964 g_free(message);
965 } while ((reply = sipe_xml_twin(reply)) != NULL);
968 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
969 const sipe_xml *grpchat)
971 struct sipe_groupchat *groupchat = sipe_private->groupchat;
972 const gchar *uri = sipe_xml_attribute(grpchat, "chanUri");
973 const gchar *from = sipe_xml_attribute(grpchat, "author");
974 time_t when = sipe_utils_str_to_time(sipe_xml_attribute(grpchat, "ts"));
975 gchar *text = sipe_xml_data(sipe_xml_child(grpchat, "chat"));
976 struct sipe_chat_session *chat_session;
977 gchar *escaped;
979 if (!uri || !from) {
980 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' received without chat room URI or author!",
981 text ? text : "");
982 g_free(text);
983 return;
986 chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
987 uri);
988 if (!chat_session) {
989 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' from '%s' received from unknown chat room '%s'!",
990 text ? text : "", from, uri);
991 g_free(text);
992 return;
995 /* libxml2 decodes all entities, but the backend expects HTML */
996 escaped = g_markup_escape_text(text, -1);
997 g_free(text);
998 sipe_backend_chat_message(SIPE_CORE_PUBLIC, chat_session->backend,
999 from, when, escaped);
1000 g_free(escaped);
1003 void process_incoming_info_groupchat(struct sipe_core_private *sipe_private,
1004 struct sipmsg *msg,
1005 struct sip_session *session)
1007 sipe_xml *xml = sipe_xml_parse(msg->body, msg->bodylen);
1008 const sipe_xml *node;
1009 const gchar *callid;
1010 struct sip_dialog *dialog;
1012 callid = sipmsg_find_header(msg, "Call-ID");
1013 dialog = sipe_dialog_find(session, session->with);
1014 if (sipe_strequal(callid, dialog->callid)) {
1016 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1018 if (((node = sipe_xml_child(xml, "rpl")) != NULL) ||
1019 ((node = sipe_xml_child(xml, "ntc")) != NULL)) {
1020 chatserver_response(sipe_private, node, session);
1021 } else if ((node = sipe_xml_child(xml, "grpchat")) != NULL) {
1022 chatserver_grpchat_message(sipe_private, node);
1023 } else {
1024 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_info_groupchat: ignoring unknown response");
1027 } else {
1029 * Our last session got disconnected without proper shutdown,
1030 * e.g. by Pidgin crashing or network connection loss. When
1031 * we reconnect to the group chat the server will send INFO
1032 * messages to the current *AND* the obsolete Call-ID, until
1033 * the obsolete session expires.
1035 * Ignore these INFO messages to avoid, e.g. duplicate texts,
1036 * and respond with an error so that the server knows that we
1037 * consider this dialog to be terminated.
1039 SIPE_DEBUG_INFO("process_incoming_info_groupchat: ignoring unsolicited INFO message to obsolete Call-ID: %s",
1040 callid);
1042 sip_transport_response(sipe_private, msg, 487, "Request Terminated", NULL);
1045 sipe_xml_free(xml);
1048 void sipe_groupchat_send(struct sipe_core_private *sipe_private,
1049 struct sipe_chat_session *chat_session,
1050 const gchar *what)
1052 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1053 gchar *cmd, *self, *timestamp, *tmp;
1054 gchar **lines, **strvp;
1055 struct sipe_groupchat_msg *msg;
1057 if (!groupchat || !chat_session)
1058 return;
1060 SIPE_DEBUG_INFO("sipe_groupchat_send: '%s' to %s",
1061 what, chat_session->id);
1063 self = sip_uri_self(sipe_private);
1064 timestamp = sipe_utils_time_to_str(time(NULL));
1067 * 'what' is already XML-escaped, e.g.
1069 * " -> &quot;
1070 * > -> &gt;
1071 * < -> &lt;
1072 * & -> &amp;
1074 * Group Chat only accepts plain text, not full HTML. So we have to
1075 * strip all HTML tags and XML escape the text.
1077 * Line breaks are encoded as <br> and therefore need to be replaced
1078 * before stripping. In order to prevent HTML stripping to strip line
1079 * endings, we need to split the text into lines on <br>.
1081 lines = g_strsplit(what, "<br>", 0);
1082 for (strvp = lines; *strvp; strvp++) {
1083 /* replace array entry with HTML stripped & XML escaped version */
1084 gchar *stripped = sipe_backend_markup_strip_html(*strvp);
1085 gchar *escaped = g_markup_escape_text(stripped, -1);
1086 g_free(stripped);
1087 g_free(*strvp);
1088 *strvp = escaped;
1090 tmp = g_strjoinv("\r\n", lines);
1091 g_strfreev(lines);
1092 cmd = g_strdup_printf("<grpchat id=\"grpchat\" seqid=\"1\" chanUri=\"%s\" author=\"%s\" ts=\"%s\">"
1093 "<chat>%s</chat>"
1094 "</grpchat>",
1095 chat_session->id, self, timestamp, tmp);
1096 g_free(tmp);
1097 g_free(timestamp);
1098 g_free(self);
1099 msg = chatserver_command(sipe_private, cmd);
1100 g_free(cmd);
1102 if (msg) {
1103 msg->session = chat_session;
1104 msg->content = g_strdup(what);
1108 void sipe_groupchat_leave(struct sipe_core_private *sipe_private,
1109 struct sipe_chat_session *chat_session)
1111 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1112 gchar *cmd;
1114 if (!groupchat || !chat_session)
1115 return;
1117 SIPE_DEBUG_INFO("sipe_groupchat_leave: %s", chat_session->id);
1119 cmd = g_strdup_printf("<cmd id=\"cmd:part\" seqid=\"1\">"
1120 "<data>"
1121 "<chanib uri=\"%s\"/>"
1122 "</data>"
1123 "</cmd>", chat_session->id);
1124 chatserver_command(sipe_private, cmd);
1125 g_free(cmd);
1128 gboolean sipe_core_groupchat_query_rooms(struct sipe_core_public *sipe_public)
1130 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1131 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1133 if (!groupchat || !groupchat->connected)
1134 return FALSE;
1136 chatserver_command(sipe_private,
1137 "<cmd id=\"cmd:chansrch\" seqid=\"1\">"
1138 "<data>"
1139 "<qib qtype=\"BYNAME\" criteria=\"\" extended=\"false\"/>"
1140 "</data>"
1141 "</cmd>");
1143 return TRUE;
1146 void sipe_core_groupchat_join(struct sipe_core_public *sipe_public,
1147 const gchar *uri)
1149 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1150 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1152 if (!g_str_has_prefix(uri, "ma-chan://"))
1153 return;
1155 if (!groupchat) {
1156 /* This happens when a user has set auto-join on a channel */
1157 sipe_groupchat_allocate(sipe_private);
1158 groupchat = sipe_private->groupchat;
1161 if (groupchat->connected) {
1162 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1163 uri);
1165 /* Already joined? */
1166 if (chat_session) {
1168 /* Yes, update backend session */
1169 SIPE_DEBUG_INFO("sipe_core_groupchat_join: show '%s' (%s)",
1170 chat_session->title,
1171 chat_session->id);
1172 sipe_backend_chat_show(chat_session->backend);
1174 } else {
1175 /* No, send command out directly */
1176 gchar *chanid = generate_chanid_node(uri, 0);
1177 if (chanid) {
1178 gchar *cmd = g_strdup_printf("<cmd id=\"cmd:join\" seqid=\"1\">"
1179 "<data>%s</data>"
1180 "</cmd>",
1181 chanid);
1182 SIPE_DEBUG_INFO("sipe_core_groupchat_join: join %s",
1183 uri);
1184 chatserver_command(sipe_private, cmd);
1185 g_free(cmd);
1186 g_free(chanid);
1189 } else {
1190 /* Add it to the queue but avoid duplicates */
1191 if (!g_slist_find_custom(groupchat->join_queue, uri,
1192 sipe_strcompare)) {
1193 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_groupchat_join: URI queued");
1194 groupchat->join_queue = g_slist_prepend(groupchat->join_queue,
1195 g_strdup(uri));
1200 void sipe_groupchat_rejoin(struct sipe_core_private *sipe_private,
1201 struct sipe_chat_session *chat_session)
1203 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1205 if (!groupchat) {
1206 /* First rejoined channel after reconnect will trigger this */
1207 sipe_groupchat_allocate(sipe_private);
1208 groupchat = sipe_private->groupchat;
1211 /* Remember "old" session, so that we don't recreate it at join */
1212 g_hash_table_insert(groupchat->uri_to_chat_session,
1213 chat_session->id,
1214 chat_session);
1215 sipe_core_groupchat_join(SIPE_CORE_PUBLIC, chat_session->id);
1219 Local Variables:
1220 mode: c
1221 c-file-style: "bsd"
1222 indent-tabs-mode: t
1223 tab-width: 8
1224 End: