Fix #222: SIPE crashes when groupchat session expires (II)
[siplcs.git] / src / core / sipe-groupchat.c
blobf26ad47d6beb3c9edb901878db0ba196a3b7b07f
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 static void chatserver_command_error_notify(struct sipe_core_private *sipe_private,
468 struct sipe_chat_session *chat_session,
469 const gchar *content)
471 gchar *label = g_strdup_printf(_("This message was not delivered to chat room '%s'"),
472 chat_session->title);
473 gchar *errmsg = g_strdup_printf("%s:\n<font color=\"#888888\"></b>%s<b></font>",
474 label, content);
475 g_free(label);
476 sipe_backend_notify_message_error(SIPE_CORE_PUBLIC,
477 chat_session->backend,
478 NULL,
479 errmsg);
480 g_free(errmsg);
483 /* TransCallback */
484 static gboolean chatserver_command_response(struct sipe_core_private *sipe_private,
485 struct sipmsg *msg,
486 struct transaction *trans)
488 if (msg->response != 200) {
489 struct sipe_groupchat_msg *gmsg = trans->payload->data;
490 struct sipe_chat_session *chat_session = gmsg->session;
492 SIPE_DEBUG_INFO("chatserver_command_response: failure %d", msg->response);
494 if (chat_session)
495 chatserver_command_error_notify(sipe_private,
496 chat_session,
497 gmsg->content);
499 return TRUE;
502 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
503 const gchar *cmd)
505 struct sipe_groupchat *groupchat = sipe_private->groupchat;
506 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
507 groupchat->session->with);
508 struct sipe_groupchat_msg *msg = NULL;
510 if (dialog) {
511 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
512 struct transaction *trans;
514 msg = generate_xccos_message(groupchat, cmd);
515 trans = sip_transport_info(sipe_private,
516 "Content-Type: text/plain\r\n",
517 msg->xccos,
518 dialog,
519 chatserver_command_response);
521 payload->destroy = sipe_groupchat_msg_remove;
522 payload->data = msg;
523 trans->payload = payload;
526 return(msg);
529 static void chatserver_response_uri(struct sipe_core_private *sipe_private,
530 struct sip_session *session,
531 SIPE_UNUSED_PARAMETER guint result,
532 SIPE_UNUSED_PARAMETER const gchar *message,
533 const sipe_xml *xml)
535 const sipe_xml *uib = sipe_xml_child(xml, "uib");
536 const gchar *uri = sipe_xml_attribute(uib, "uri");
538 /* drop connection to ocschat@<domain> again */
539 sipe_session_close(sipe_private, session);
541 if (uri) {
542 struct sipe_groupchat *groupchat = sipe_private->groupchat;
544 SIPE_DEBUG_INFO("chatserver_response_uri: '%s'", uri);
546 groupchat->session = session = sipe_session_find_or_add_im(sipe_private,
547 uri);
549 session->is_groupchat = TRUE;
550 sipe_im_invite(sipe_private, session, uri, NULL, NULL, NULL, FALSE);
551 } else {
552 SIPE_DEBUG_WARNING_NOFORMAT("chatserver_response_uri: no server URI found!");
553 groupchat_init_retry(sipe_private);
557 static void chatserver_response_channel_search(struct sipe_core_private *sipe_private,
558 SIPE_UNUSED_PARAMETER struct sip_session *session,
559 guint result,
560 const gchar *message,
561 const sipe_xml *xml)
563 struct sipe_core_public *sipe_public = SIPE_CORE_PUBLIC;
565 if (result != 200) {
566 sipe_backend_notify_error(sipe_public,
567 _("Error retrieving room list"),
568 message);
569 } else {
570 const sipe_xml *chanib;
572 for (chanib = sipe_xml_child(xml, "chanib");
573 chanib;
574 chanib = sipe_xml_twin(chanib)) {
575 const gchar *name = sipe_xml_attribute(chanib, "name");
576 const gchar *desc = sipe_xml_attribute(chanib, "description");
577 const gchar *uri = sipe_xml_attribute(chanib, "uri");
578 const sipe_xml *node;
579 guint user_count = 0;
580 guint32 flags = 0;
582 /* information */
583 for (node = sipe_xml_child(chanib, "info");
584 node;
585 node = sipe_xml_twin(node)) {
586 const gchar *id = sipe_xml_attribute(node, "id");
587 gchar *data;
589 if (!id) continue;
591 data = sipe_xml_data(node);
592 if (data) {
593 if (sipe_strcase_equal(id, "urn:parlano:ma:info:ucnt")) {
594 user_count = g_ascii_strtoll(data, NULL, 10);
595 } else if (sipe_strcase_equal(id, "urn:parlano:ma:info:visibilty")) {
596 if (sipe_strcase_equal(data, "private")) {
597 flags |= SIPE_GROUPCHAT_ROOM_PRIVATE;
600 g_free(data);
604 /* properties */
605 for (node = sipe_xml_child(chanib, "prop");
606 node;
607 node = sipe_xml_twin(node)) {
608 const gchar *id = sipe_xml_attribute(node, "id");
609 gchar *data;
611 if (!id) continue;
613 data = sipe_xml_data(node);
614 if (data) {
615 gboolean value = sipe_strcase_equal(data, "true");
616 g_free(data);
618 if (value) {
619 guint32 add = 0;
620 if (sipe_strcase_equal(id, "urn:parlano:ma:prop:filepost")) {
621 add = SIPE_GROUPCHAT_ROOM_FILEPOST;
622 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:invite")) {
623 add = SIPE_GROUPCHAT_ROOM_INVITE;
624 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:logged")) {
625 add = SIPE_GROUPCHAT_ROOM_LOGGED;
627 flags |= add;
632 SIPE_DEBUG_INFO("group chat channel '%s': '%s' (%s) with %u users, flags 0x%x",
633 name, desc, uri, user_count, flags);
634 sipe_backend_groupchat_room_add(sipe_public,
635 uri, name, desc,
636 user_count, flags);
640 sipe_backend_groupchat_room_terminate(sipe_public);
643 static gboolean is_chanop(const sipe_xml *aib)
645 return sipe_strequal(sipe_xml_attribute(aib, "key"),
646 GROUPCHAT_AIB_KEY_CHANOP);
649 static void add_user(struct sipe_chat_session *chat_session,
650 const gchar *uri,
651 gboolean new, gboolean chanop)
653 SIPE_DEBUG_INFO("add_user: %s%s%s to room %s (%s)",
654 new ? "new " : "",
655 chanop ? "chanop " : "",
656 uri,
657 chat_session->title, chat_session->id);
658 sipe_backend_chat_add(chat_session->backend, uri, new);
659 if (chanop)
660 sipe_backend_chat_operator(chat_session->backend, uri);
663 static void chatserver_response_join(struct sipe_core_private *sipe_private,
664 SIPE_UNUSED_PARAMETER struct sip_session *session,
665 guint result,
666 const gchar *message,
667 const sipe_xml *xml)
669 if (result != 200) {
670 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
671 _("Error joining chat room"),
672 message);
673 } else {
674 struct sipe_groupchat *groupchat = sipe_private->groupchat;
675 const sipe_xml *node;
676 GHashTable *user_ids = g_hash_table_new(g_str_hash, g_str_equal);
678 /* Extract user IDs & URIs and generate ID -> URI map */
679 for (node = sipe_xml_child(xml, "uib");
680 node;
681 node = sipe_xml_twin(node)) {
682 const gchar *id = sipe_xml_attribute(node, "id");
683 const gchar *uri = sipe_xml_attribute(node, "uri");
684 if (id && uri)
685 g_hash_table_insert(user_ids,
686 (gpointer) id,
687 (gpointer) uri);
690 /* Process channel data */
691 for (node = sipe_xml_child(xml, "chanib");
692 node;
693 node = sipe_xml_twin(node)) {
694 const gchar *uri = sipe_xml_attribute(node, "uri");
696 if (uri) {
697 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
698 uri);
699 gboolean new = (chat_session == NULL);
700 const gchar *attr = sipe_xml_attribute(node, "name");
701 gchar *self = sip_uri_self(sipe_private);
702 const sipe_xml *aib;
704 if (new) {
705 chat_session = sipe_chat_create_session(SIPE_CHAT_TYPE_GROUPCHAT,
706 sipe_xml_attribute(node,
707 "uri"),
708 attr ? attr : "");
709 g_hash_table_insert(groupchat->uri_to_chat_session,
710 chat_session->id,
711 chat_session);
713 SIPE_DEBUG_INFO("joined room '%s' (%s)",
714 chat_session->title,
715 chat_session->id);
716 chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
717 chat_session,
718 chat_session->title,
719 self);
720 } else {
721 SIPE_DEBUG_INFO("rejoining room '%s' (%s)",
722 chat_session->title,
723 chat_session->id);
724 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
725 chat_session->backend,
726 self,
727 chat_session->title);
729 g_free(self);
731 attr = sipe_xml_attribute(node, "topic");
732 if (attr) {
733 sipe_backend_chat_topic(chat_session->backend,
734 attr);
737 /* Process user map for channel */
738 for (aib = sipe_xml_child(node, "aib");
739 aib;
740 aib = sipe_xml_twin(aib)) {
741 const gchar *value = sipe_xml_attribute(aib, "value");
742 gboolean chanop = is_chanop(aib);
743 gchar **ids = g_strsplit(value, ",", 0);
745 if (ids) {
746 gchar **uid = ids;
748 while (*uid) {
749 const gchar *uri = g_hash_table_lookup(user_ids,
750 *uid);
751 if (uri)
752 add_user(chat_session,
753 uri,
754 FALSE,
755 chanop);
756 uid++;
759 g_strfreev(ids);
763 /* Request last 25 entries from channel history */
764 self = g_strdup_printf("<cmd id=\"cmd:bccontext\" seqid=\"1\">"
765 "<data>"
766 "<chanib uri=\"%s\"/>"
767 "<bcq><last cnt=\"25\"/></bcq>"
768 "</data>"
769 "</cmd>", chat_session->id);
770 chatserver_command(sipe_private, self);
771 g_free(self);
775 g_hash_table_destroy(user_ids);
779 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
780 const sipe_xml *grpchat);
782 static void chatserver_response_history(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
783 SIPE_UNUSED_PARAMETER struct sip_session *session,
784 SIPE_UNUSED_PARAMETER guint result,
785 SIPE_UNUSED_PARAMETER const gchar *message,
786 const sipe_xml *xml)
788 const sipe_xml *grpchat;
790 for (grpchat = sipe_xml_child(xml, "chanib/msg");
791 grpchat;
792 grpchat = sipe_xml_twin(grpchat))
793 if (sipe_strequal(sipe_xml_attribute(grpchat, "id"),
794 "grpchat"))
795 chatserver_grpchat_message(sipe_private, grpchat);
798 static void chatserver_response_part(struct sipe_core_private *sipe_private,
799 SIPE_UNUSED_PARAMETER struct sip_session *session,
800 guint result,
801 const gchar *message,
802 const sipe_xml *xml)
804 if (result != 200) {
805 SIPE_DEBUG_WARNING("chatserver_response_part: failed with %d: %s. Dropping room",
806 result, message);
807 } else {
808 struct sipe_groupchat *groupchat = sipe_private->groupchat;
809 const gchar *uri = sipe_xml_attribute(sipe_xml_child(xml, "chanib"),
810 "uri");
811 struct sipe_chat_session *chat_session;
813 if (uri &&
814 (chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
815 uri))) {
817 SIPE_DEBUG_INFO("leaving room '%s' (%s)",
818 chat_session->title, chat_session->id);
820 g_hash_table_remove(groupchat->uri_to_chat_session,
821 uri);
822 sipe_chat_remove_session(chat_session);
824 } else {
825 SIPE_DEBUG_WARNING("chatserver_response_part: unknown chat room uri '%s'",
826 uri ? uri : "");
831 static void chatserver_notice_join(struct sipe_core_private *sipe_private,
832 SIPE_UNUSED_PARAMETER struct sip_session *session,
833 SIPE_UNUSED_PARAMETER guint result,
834 SIPE_UNUSED_PARAMETER const gchar *message,
835 const sipe_xml *xml)
837 struct sipe_groupchat *groupchat = sipe_private->groupchat;
838 const sipe_xml *uib;
840 for (uib = sipe_xml_child(xml, "uib");
841 uib;
842 uib = sipe_xml_twin(uib)) {
843 const gchar *uri = sipe_xml_attribute(uib, "uri");
845 if (uri) {
846 const sipe_xml *aib;
848 for (aib = sipe_xml_child(uib, "aib");
849 aib;
850 aib = sipe_xml_twin(aib)) {
851 const gchar *domain = sipe_xml_attribute(aib, "domain");
852 const gchar *path = sipe_xml_attribute(aib, "value");
854 if (domain && path) {
855 gchar *room_uri = g_strdup_printf("ma-chan://%s/%s",
856 domain, path);
857 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
858 room_uri);
859 if (chat_session)
860 add_user(chat_session,
861 uri,
862 TRUE,
863 is_chanop(aib));
865 g_free(room_uri);
872 static void chatserver_notice_part(struct sipe_core_private *sipe_private,
873 SIPE_UNUSED_PARAMETER struct sip_session *session,
874 SIPE_UNUSED_PARAMETER guint result,
875 SIPE_UNUSED_PARAMETER const gchar *message,
876 const sipe_xml *xml)
878 struct sipe_groupchat *groupchat = sipe_private->groupchat;
879 const sipe_xml *chanib;
881 for (chanib = sipe_xml_child(xml, "chanib");
882 chanib;
883 chanib = sipe_xml_twin(chanib)) {
884 const gchar *room_uri = sipe_xml_attribute(chanib, "uri");
886 if (room_uri) {
887 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
888 room_uri);
890 if (chat_session) {
891 const sipe_xml *uib;
893 for (uib = sipe_xml_child(chanib, "uib");
894 uib;
895 uib = sipe_xml_twin(uib)) {
896 const gchar *uri = sipe_xml_attribute(uib, "uri");
898 if (uri) {
899 SIPE_DEBUG_INFO("remove_user: %s from room %s (%s)",
900 uri,
901 chat_session->title,
902 chat_session->id);
903 sipe_backend_chat_remove(chat_session->backend,
904 uri);
912 static const struct response {
913 const gchar *key;
914 void (* const handler)(struct sipe_core_private *,
915 struct sip_session *,
916 guint result, const gchar *,
917 const sipe_xml *xml);
918 } response_table[] = {
919 { "rpl:requri", chatserver_response_uri },
920 { "rpl:chansrch", chatserver_response_channel_search },
921 { "rpl:join", chatserver_response_join },
922 { "rpl:bjoin", chatserver_response_join },
923 { "rpl:bccontext", chatserver_response_history },
924 { "rpl:part", chatserver_response_part },
925 { "ntc:join", chatserver_notice_join },
926 { "ntc:bjoin", chatserver_notice_join },
927 { "ntc:part", chatserver_notice_part },
928 { NULL, NULL }
931 /* Handles rpl:XXX & ntc:YYY */
932 static void chatserver_response(struct sipe_core_private *sipe_private,
933 const sipe_xml *reply,
934 struct sip_session *session)
936 do {
937 const sipe_xml *resp, *data;
938 const gchar *id;
939 gchar *message;
940 guint result = 500;
941 const struct response *r;
943 id = sipe_xml_attribute(reply, "id");
944 if (!id) {
945 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: no reply ID found!");
946 continue;
949 resp = sipe_xml_child(reply, "resp");
950 if (resp) {
951 result = sipe_xml_int_attribute(resp, "code", 500);
952 message = sipe_xml_data(resp);
953 } else {
954 message = g_strdup("");
957 data = sipe_xml_child(reply, "data");
959 SIPE_DEBUG_INFO("chatserver_response: '%s' result (%d) %s",
960 id, result, message ? message : "");
962 for (r = response_table; r->key; r++) {
963 if (sipe_strcase_equal(id, r->key)) {
964 (*r->handler)(sipe_private, session, result, message, data);
965 break;
968 if (!r->key) {
969 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: ignoring unknown response");
972 g_free(message);
973 } while ((reply = sipe_xml_twin(reply)) != NULL);
976 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
977 const sipe_xml *grpchat)
979 struct sipe_groupchat *groupchat = sipe_private->groupchat;
980 const gchar *uri = sipe_xml_attribute(grpchat, "chanUri");
981 const gchar *from = sipe_xml_attribute(grpchat, "author");
982 time_t when = sipe_utils_str_to_time(sipe_xml_attribute(grpchat, "ts"));
983 gchar *text = sipe_xml_data(sipe_xml_child(grpchat, "chat"));
984 struct sipe_chat_session *chat_session;
985 gchar *escaped;
987 if (!uri || !from) {
988 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' received without chat room URI or author!",
989 text ? text : "");
990 g_free(text);
991 return;
994 chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
995 uri);
996 if (!chat_session) {
997 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' from '%s' received from unknown chat room '%s'!",
998 text ? text : "", from, uri);
999 g_free(text);
1000 return;
1003 /* libxml2 decodes all entities, but the backend expects HTML */
1004 escaped = g_markup_escape_text(text, -1);
1005 g_free(text);
1006 sipe_backend_chat_message(SIPE_CORE_PUBLIC, chat_session->backend,
1007 from, when, escaped);
1008 g_free(escaped);
1011 void process_incoming_info_groupchat(struct sipe_core_private *sipe_private,
1012 struct sipmsg *msg,
1013 struct sip_session *session)
1015 sipe_xml *xml = sipe_xml_parse(msg->body, msg->bodylen);
1016 const sipe_xml *node;
1017 const gchar *callid;
1018 struct sip_dialog *dialog;
1020 callid = sipmsg_find_header(msg, "Call-ID");
1021 dialog = sipe_dialog_find(session, session->with);
1022 if (sipe_strequal(callid, dialog->callid)) {
1024 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1026 if (((node = sipe_xml_child(xml, "rpl")) != NULL) ||
1027 ((node = sipe_xml_child(xml, "ntc")) != NULL)) {
1028 chatserver_response(sipe_private, node, session);
1029 } else if ((node = sipe_xml_child(xml, "grpchat")) != NULL) {
1030 chatserver_grpchat_message(sipe_private, node);
1031 } else {
1032 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_info_groupchat: ignoring unknown response");
1035 } else {
1037 * Our last session got disconnected without proper shutdown,
1038 * e.g. by Pidgin crashing or network connection loss. When
1039 * we reconnect to the group chat the server will send INFO
1040 * messages to the current *AND* the obsolete Call-ID, until
1041 * the obsolete session expires.
1043 * Ignore these INFO messages to avoid, e.g. duplicate texts,
1044 * and respond with an error so that the server knows that we
1045 * consider this dialog to be terminated.
1047 SIPE_DEBUG_INFO("process_incoming_info_groupchat: ignoring unsolicited INFO message to obsolete Call-ID: %s",
1048 callid);
1050 sip_transport_response(sipe_private, msg, 487, "Request Terminated", NULL);
1053 sipe_xml_free(xml);
1056 void sipe_groupchat_send(struct sipe_core_private *sipe_private,
1057 struct sipe_chat_session *chat_session,
1058 const gchar *what)
1060 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1061 gchar *cmd, *self, *timestamp, *tmp;
1062 gchar **lines, **strvp;
1063 struct sipe_groupchat_msg *msg;
1065 if (!groupchat || !chat_session)
1066 return;
1068 SIPE_DEBUG_INFO("sipe_groupchat_send: '%s' to %s",
1069 what, chat_session->id);
1071 self = sip_uri_self(sipe_private);
1072 timestamp = sipe_utils_time_to_str(time(NULL));
1075 * 'what' is already XML-escaped, e.g.
1077 * " -> &quot;
1078 * > -> &gt;
1079 * < -> &lt;
1080 * & -> &amp;
1082 * Group Chat only accepts plain text, not full HTML. So we have to
1083 * strip all HTML tags and XML escape the text.
1085 * Line breaks are encoded as <br> and therefore need to be replaced
1086 * before stripping. In order to prevent HTML stripping to strip line
1087 * endings, we need to split the text into lines on <br>.
1089 lines = g_strsplit(what, "<br>", 0);
1090 for (strvp = lines; *strvp; strvp++) {
1091 /* replace array entry with HTML stripped & XML escaped version */
1092 gchar *stripped = sipe_backend_markup_strip_html(*strvp);
1093 gchar *escaped = g_markup_escape_text(stripped, -1);
1094 g_free(stripped);
1095 g_free(*strvp);
1096 *strvp = escaped;
1098 tmp = g_strjoinv("\r\n", lines);
1099 g_strfreev(lines);
1100 cmd = g_strdup_printf("<grpchat id=\"grpchat\" seqid=\"1\" chanUri=\"%s\" author=\"%s\" ts=\"%s\">"
1101 "<chat>%s</chat>"
1102 "</grpchat>",
1103 chat_session->id, self, timestamp, tmp);
1104 g_free(tmp);
1105 g_free(timestamp);
1106 g_free(self);
1107 msg = chatserver_command(sipe_private, cmd);
1108 g_free(cmd);
1110 if (msg) {
1111 msg->session = chat_session;
1112 msg->content = g_strdup(what);
1113 } else {
1114 chatserver_command_error_notify(sipe_private,
1115 chat_session,
1116 what);
1120 void sipe_groupchat_leave(struct sipe_core_private *sipe_private,
1121 struct sipe_chat_session *chat_session)
1123 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1124 gchar *cmd;
1126 if (!groupchat || !chat_session)
1127 return;
1129 SIPE_DEBUG_INFO("sipe_groupchat_leave: %s", chat_session->id);
1131 cmd = g_strdup_printf("<cmd id=\"cmd:part\" seqid=\"1\">"
1132 "<data>"
1133 "<chanib uri=\"%s\"/>"
1134 "</data>"
1135 "</cmd>", chat_session->id);
1136 chatserver_command(sipe_private, cmd);
1137 g_free(cmd);
1140 gboolean sipe_core_groupchat_query_rooms(struct sipe_core_public *sipe_public)
1142 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1143 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1145 if (!groupchat || !groupchat->connected)
1146 return FALSE;
1148 chatserver_command(sipe_private,
1149 "<cmd id=\"cmd:chansrch\" seqid=\"1\">"
1150 "<data>"
1151 "<qib qtype=\"BYNAME\" criteria=\"\" extended=\"false\"/>"
1152 "</data>"
1153 "</cmd>");
1155 return TRUE;
1158 void sipe_core_groupchat_join(struct sipe_core_public *sipe_public,
1159 const gchar *uri)
1161 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1162 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1164 if (!g_str_has_prefix(uri, "ma-chan://"))
1165 return;
1167 if (!groupchat) {
1168 /* This happens when a user has set auto-join on a channel */
1169 sipe_groupchat_allocate(sipe_private);
1170 groupchat = sipe_private->groupchat;
1173 if (groupchat->connected) {
1174 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1175 uri);
1177 /* Already joined? */
1178 if (chat_session) {
1180 /* Yes, update backend session */
1181 SIPE_DEBUG_INFO("sipe_core_groupchat_join: show '%s' (%s)",
1182 chat_session->title,
1183 chat_session->id);
1184 sipe_backend_chat_show(chat_session->backend);
1186 } else {
1187 /* No, send command out directly */
1188 gchar *chanid = generate_chanid_node(uri, 0);
1189 if (chanid) {
1190 gchar *cmd = g_strdup_printf("<cmd id=\"cmd:join\" seqid=\"1\">"
1191 "<data>%s</data>"
1192 "</cmd>",
1193 chanid);
1194 SIPE_DEBUG_INFO("sipe_core_groupchat_join: join %s",
1195 uri);
1196 chatserver_command(sipe_private, cmd);
1197 g_free(cmd);
1198 g_free(chanid);
1201 } else {
1202 /* Add it to the queue but avoid duplicates */
1203 if (!g_slist_find_custom(groupchat->join_queue, uri,
1204 sipe_strcompare)) {
1205 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_groupchat_join: URI queued");
1206 groupchat->join_queue = g_slist_prepend(groupchat->join_queue,
1207 g_strdup(uri));
1212 void sipe_groupchat_rejoin(struct sipe_core_private *sipe_private,
1213 struct sipe_chat_session *chat_session)
1215 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1217 if (!groupchat) {
1218 /* First rejoined channel after reconnect will trigger this */
1219 sipe_groupchat_allocate(sipe_private);
1220 groupchat = sipe_private->groupchat;
1223 /* Remember "old" session, so that we don't recreate it at join */
1224 g_hash_table_insert(groupchat->uri_to_chat_session,
1225 chat_session->id,
1226 chat_session);
1227 sipe_core_groupchat_join(SIPE_CORE_PUBLIC, chat_session->id);
1231 Local Variables:
1232 mode: c
1233 c-file-style: "bsd"
1234 indent-tabs-mode: t
1235 tab-width: 8
1236 End: