Fix #222: SIPE crashes when groupchat session expires (IV)
[siplcs.git] / src / core / sipe-groupchat.c
blobf90909e59c9253f162ab9621ba173fad98bcc8d0
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;
386 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
387 groupchat->session->with);
389 if (dialog)
390 sip_transport_update(sipe_private,
391 dialog,
392 groupchat_expired_session_response);
393 sipe_schedule_seconds(sipe_private,
394 "<+groupchat-expires>",
395 NULL,
396 groupchat->expires,
397 groupchat_update_cb,
398 NULL);
401 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
402 const gchar *cmd);
404 void sipe_groupchat_invite_response(struct sipe_core_private *sipe_private,
405 struct sip_dialog *dialog,
406 struct sipmsg *response)
408 struct sipe_groupchat *groupchat = sipe_private->groupchat;
410 SIPE_DEBUG_INFO_NOFORMAT("sipe_groupchat_invite_response");
412 if (!groupchat->session) {
413 /* response to initial invite */
414 struct sipe_groupchat_msg *msg = generate_xccos_message(groupchat,
415 "<cmd id=\"cmd:requri\" seqid=\"1\"><data/></cmd>");
416 const gchar *session_expires = sipmsg_find_header(response,
417 "Session-Expires");
419 sip_transport_info(sipe_private,
420 "Content-Type: text/plain\r\n",
421 msg->xccos,
422 dialog,
423 NULL);
424 sipe_groupchat_msg_remove(msg);
426 if (session_expires) {
427 groupchat->expires = strtoul(session_expires, NULL, 10);
429 if (groupchat->expires) {
430 SIPE_DEBUG_INFO("sipe_groupchat_invite_response: session expires in %d seconds",
431 groupchat->expires);
433 if (groupchat->expires > 10)
434 groupchat->expires -= 10;
435 sipe_schedule_seconds(sipe_private,
436 "<+groupchat-expires>",
437 NULL,
438 groupchat->expires,
439 groupchat_update_cb,
440 NULL);
444 } else {
445 /* response to group chat server invite */
446 gchar *invcmd;
448 SIPE_DEBUG_INFO_NOFORMAT("connection to group chat server established.");
450 groupchat->connected = TRUE;
452 /* Any queued joins? */
453 if (groupchat->join_queue) {
454 GString *cmd = g_string_new("<cmd id=\"cmd:bjoin\" seqid=\"1\">"
455 "<data>");
456 GSList *entry;
457 guint i = 0;
459 /* We used g_slist_prepend() to create the list */
460 groupchat->join_queue = entry = g_slist_reverse(groupchat->join_queue);
461 while (entry) {
462 gchar *chanid = generate_chanid_node(entry->data, i++);
463 g_string_append(cmd, chanid);
464 g_free(chanid);
465 entry = entry->next;
467 sipe_groupchat_free_join_queue(groupchat);
469 g_string_append(cmd, "</data></cmd>");
470 chatserver_command(sipe_private, cmd->str);
471 g_string_free(cmd, TRUE);
474 /* Request outstanding invites from server */
475 invcmd = g_strdup_printf("<cmd id=\"cmd:getinv\" seqid=\"1\">"
476 "<data>"
477 "<inv inviteId=\"1\" domain=\"%s\"/>"
478 "</data>"
479 "</cmd>", groupchat->domain);
480 chatserver_command(sipe_private, invcmd);
481 g_free(invcmd);
485 static void chatserver_command_error_notify(struct sipe_core_private *sipe_private,
486 struct sipe_chat_session *chat_session,
487 const gchar *content)
489 gchar *label = g_strdup_printf(_("This message was not delivered to chat room '%s'"),
490 chat_session->title);
491 gchar *errmsg = g_strdup_printf("%s:\n<font color=\"#888888\"></b>%s<b></font>",
492 label, content);
493 g_free(label);
494 sipe_backend_notify_message_error(SIPE_CORE_PUBLIC,
495 chat_session->backend,
496 NULL,
497 errmsg);
498 g_free(errmsg);
501 /* TransCallback */
502 static gboolean chatserver_command_response(struct sipe_core_private *sipe_private,
503 struct sipmsg *msg,
504 struct transaction *trans)
506 if (msg->response != 200) {
507 struct sipe_groupchat_msg *gmsg = trans->payload->data;
508 struct sipe_chat_session *chat_session = gmsg->session;
510 SIPE_DEBUG_INFO("chatserver_command_response: failure %d", msg->response);
512 if (chat_session)
513 chatserver_command_error_notify(sipe_private,
514 chat_session,
515 gmsg->content);
517 groupchat_expired_session_response(sipe_private, msg, trans);
519 return TRUE;
522 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
523 const gchar *cmd)
525 struct sipe_groupchat *groupchat = sipe_private->groupchat;
526 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
527 groupchat->session->with);
528 struct sipe_groupchat_msg *msg = NULL;
530 if (dialog) {
531 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
532 struct transaction *trans;
534 msg = generate_xccos_message(groupchat, cmd);
535 trans = sip_transport_info(sipe_private,
536 "Content-Type: text/plain\r\n",
537 msg->xccos,
538 dialog,
539 chatserver_command_response);
541 payload->destroy = sipe_groupchat_msg_remove;
542 payload->data = msg;
543 trans->payload = payload;
546 return(msg);
549 static void chatserver_response_uri(struct sipe_core_private *sipe_private,
550 struct sip_session *session,
551 SIPE_UNUSED_PARAMETER guint result,
552 SIPE_UNUSED_PARAMETER const gchar *message,
553 const sipe_xml *xml)
555 const sipe_xml *uib = sipe_xml_child(xml, "uib");
556 const gchar *uri = sipe_xml_attribute(uib, "uri");
558 /* drop connection to ocschat@<domain> again */
559 sipe_session_close(sipe_private, session);
561 if (uri) {
562 struct sipe_groupchat *groupchat = sipe_private->groupchat;
564 SIPE_DEBUG_INFO("chatserver_response_uri: '%s'", uri);
566 groupchat->session = session = sipe_session_find_or_add_im(sipe_private,
567 uri);
569 session->is_groupchat = TRUE;
570 sipe_im_invite(sipe_private, session, uri, NULL, NULL, NULL, FALSE);
571 } else {
572 SIPE_DEBUG_WARNING_NOFORMAT("chatserver_response_uri: no server URI found!");
573 groupchat_init_retry(sipe_private);
577 static void chatserver_response_channel_search(struct sipe_core_private *sipe_private,
578 SIPE_UNUSED_PARAMETER struct sip_session *session,
579 guint result,
580 const gchar *message,
581 const sipe_xml *xml)
583 struct sipe_core_public *sipe_public = SIPE_CORE_PUBLIC;
585 if (result != 200) {
586 sipe_backend_notify_error(sipe_public,
587 _("Error retrieving room list"),
588 message);
589 } else {
590 const sipe_xml *chanib;
592 for (chanib = sipe_xml_child(xml, "chanib");
593 chanib;
594 chanib = sipe_xml_twin(chanib)) {
595 const gchar *name = sipe_xml_attribute(chanib, "name");
596 const gchar *desc = sipe_xml_attribute(chanib, "description");
597 const gchar *uri = sipe_xml_attribute(chanib, "uri");
598 const sipe_xml *node;
599 guint user_count = 0;
600 guint32 flags = 0;
602 /* information */
603 for (node = sipe_xml_child(chanib, "info");
604 node;
605 node = sipe_xml_twin(node)) {
606 const gchar *id = sipe_xml_attribute(node, "id");
607 gchar *data;
609 if (!id) continue;
611 data = sipe_xml_data(node);
612 if (data) {
613 if (sipe_strcase_equal(id, "urn:parlano:ma:info:ucnt")) {
614 user_count = g_ascii_strtoll(data, NULL, 10);
615 } else if (sipe_strcase_equal(id, "urn:parlano:ma:info:visibilty")) {
616 if (sipe_strcase_equal(data, "private")) {
617 flags |= SIPE_GROUPCHAT_ROOM_PRIVATE;
620 g_free(data);
624 /* properties */
625 for (node = sipe_xml_child(chanib, "prop");
626 node;
627 node = sipe_xml_twin(node)) {
628 const gchar *id = sipe_xml_attribute(node, "id");
629 gchar *data;
631 if (!id) continue;
633 data = sipe_xml_data(node);
634 if (data) {
635 gboolean value = sipe_strcase_equal(data, "true");
636 g_free(data);
638 if (value) {
639 guint32 add = 0;
640 if (sipe_strcase_equal(id, "urn:parlano:ma:prop:filepost")) {
641 add = SIPE_GROUPCHAT_ROOM_FILEPOST;
642 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:invite")) {
643 add = SIPE_GROUPCHAT_ROOM_INVITE;
644 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:logged")) {
645 add = SIPE_GROUPCHAT_ROOM_LOGGED;
647 flags |= add;
652 SIPE_DEBUG_INFO("group chat channel '%s': '%s' (%s) with %u users, flags 0x%x",
653 name, desc, uri, user_count, flags);
654 sipe_backend_groupchat_room_add(sipe_public,
655 uri, name, desc,
656 user_count, flags);
660 sipe_backend_groupchat_room_terminate(sipe_public);
663 static gboolean is_chanop(const sipe_xml *aib)
665 return sipe_strequal(sipe_xml_attribute(aib, "key"),
666 GROUPCHAT_AIB_KEY_CHANOP);
669 static void add_user(struct sipe_chat_session *chat_session,
670 const gchar *uri,
671 gboolean new, gboolean chanop)
673 SIPE_DEBUG_INFO("add_user: %s%s%s to room %s (%s)",
674 new ? "new " : "",
675 chanop ? "chanop " : "",
676 uri,
677 chat_session->title, chat_session->id);
678 sipe_backend_chat_add(chat_session->backend, uri, new);
679 if (chanop)
680 sipe_backend_chat_operator(chat_session->backend, uri);
683 static void chatserver_response_join(struct sipe_core_private *sipe_private,
684 SIPE_UNUSED_PARAMETER struct sip_session *session,
685 guint result,
686 const gchar *message,
687 const sipe_xml *xml)
689 if (result != 200) {
690 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
691 _("Error joining chat room"),
692 message);
693 } else {
694 struct sipe_groupchat *groupchat = sipe_private->groupchat;
695 const sipe_xml *node;
696 GHashTable *user_ids = g_hash_table_new(g_str_hash, g_str_equal);
698 /* Extract user IDs & URIs and generate ID -> URI map */
699 for (node = sipe_xml_child(xml, "uib");
700 node;
701 node = sipe_xml_twin(node)) {
702 const gchar *id = sipe_xml_attribute(node, "id");
703 const gchar *uri = sipe_xml_attribute(node, "uri");
704 if (id && uri)
705 g_hash_table_insert(user_ids,
706 (gpointer) id,
707 (gpointer) uri);
710 /* Process channel data */
711 for (node = sipe_xml_child(xml, "chanib");
712 node;
713 node = sipe_xml_twin(node)) {
714 const gchar *uri = sipe_xml_attribute(node, "uri");
716 if (uri) {
717 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
718 uri);
719 gboolean new = (chat_session == NULL);
720 const gchar *attr = sipe_xml_attribute(node, "name");
721 gchar *self = sip_uri_self(sipe_private);
722 const sipe_xml *aib;
724 if (new) {
725 chat_session = sipe_chat_create_session(SIPE_CHAT_TYPE_GROUPCHAT,
726 sipe_xml_attribute(node,
727 "uri"),
728 attr ? attr : "");
729 g_hash_table_insert(groupchat->uri_to_chat_session,
730 chat_session->id,
731 chat_session);
733 SIPE_DEBUG_INFO("joined room '%s' (%s)",
734 chat_session->title,
735 chat_session->id);
736 chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
737 chat_session,
738 chat_session->title,
739 self);
740 } else {
741 SIPE_DEBUG_INFO("rejoining room '%s' (%s)",
742 chat_session->title,
743 chat_session->id);
744 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
745 chat_session->backend,
746 self,
747 chat_session->title);
749 g_free(self);
751 attr = sipe_xml_attribute(node, "topic");
752 if (attr) {
753 sipe_backend_chat_topic(chat_session->backend,
754 attr);
757 /* Process user map for channel */
758 for (aib = sipe_xml_child(node, "aib");
759 aib;
760 aib = sipe_xml_twin(aib)) {
761 const gchar *value = sipe_xml_attribute(aib, "value");
762 gboolean chanop = is_chanop(aib);
763 gchar **ids = g_strsplit(value, ",", 0);
765 if (ids) {
766 gchar **uid = ids;
768 while (*uid) {
769 const gchar *uri = g_hash_table_lookup(user_ids,
770 *uid);
771 if (uri)
772 add_user(chat_session,
773 uri,
774 FALSE,
775 chanop);
776 uid++;
779 g_strfreev(ids);
783 /* Request last 25 entries from channel history */
784 self = g_strdup_printf("<cmd id=\"cmd:bccontext\" seqid=\"1\">"
785 "<data>"
786 "<chanib uri=\"%s\"/>"
787 "<bcq><last cnt=\"25\"/></bcq>"
788 "</data>"
789 "</cmd>", chat_session->id);
790 chatserver_command(sipe_private, self);
791 g_free(self);
795 g_hash_table_destroy(user_ids);
799 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
800 const sipe_xml *grpchat);
802 static void chatserver_response_history(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
803 SIPE_UNUSED_PARAMETER struct sip_session *session,
804 SIPE_UNUSED_PARAMETER guint result,
805 SIPE_UNUSED_PARAMETER const gchar *message,
806 const sipe_xml *xml)
808 const sipe_xml *grpchat;
810 for (grpchat = sipe_xml_child(xml, "chanib/msg");
811 grpchat;
812 grpchat = sipe_xml_twin(grpchat))
813 if (sipe_strequal(sipe_xml_attribute(grpchat, "id"),
814 "grpchat"))
815 chatserver_grpchat_message(sipe_private, grpchat);
818 static void chatserver_response_part(struct sipe_core_private *sipe_private,
819 SIPE_UNUSED_PARAMETER struct sip_session *session,
820 guint result,
821 const gchar *message,
822 const sipe_xml *xml)
824 if (result != 200) {
825 SIPE_DEBUG_WARNING("chatserver_response_part: failed with %d: %s. Dropping room",
826 result, message);
827 } else {
828 struct sipe_groupchat *groupchat = sipe_private->groupchat;
829 const gchar *uri = sipe_xml_attribute(sipe_xml_child(xml, "chanib"),
830 "uri");
831 struct sipe_chat_session *chat_session;
833 if (uri &&
834 (chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
835 uri))) {
837 SIPE_DEBUG_INFO("leaving room '%s' (%s)",
838 chat_session->title, chat_session->id);
840 g_hash_table_remove(groupchat->uri_to_chat_session,
841 uri);
842 sipe_chat_remove_session(chat_session);
844 } else {
845 SIPE_DEBUG_WARNING("chatserver_response_part: unknown chat room uri '%s'",
846 uri ? uri : "");
851 static void chatserver_notice_join(struct sipe_core_private *sipe_private,
852 SIPE_UNUSED_PARAMETER struct sip_session *session,
853 SIPE_UNUSED_PARAMETER guint result,
854 SIPE_UNUSED_PARAMETER const gchar *message,
855 const sipe_xml *xml)
857 struct sipe_groupchat *groupchat = sipe_private->groupchat;
858 const sipe_xml *uib;
860 for (uib = sipe_xml_child(xml, "uib");
861 uib;
862 uib = sipe_xml_twin(uib)) {
863 const gchar *uri = sipe_xml_attribute(uib, "uri");
865 if (uri) {
866 const sipe_xml *aib;
868 for (aib = sipe_xml_child(uib, "aib");
869 aib;
870 aib = sipe_xml_twin(aib)) {
871 const gchar *domain = sipe_xml_attribute(aib, "domain");
872 const gchar *path = sipe_xml_attribute(aib, "value");
874 if (domain && path) {
875 gchar *room_uri = g_strdup_printf("ma-chan://%s/%s",
876 domain, path);
877 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
878 room_uri);
879 if (chat_session)
880 add_user(chat_session,
881 uri,
882 TRUE,
883 is_chanop(aib));
885 g_free(room_uri);
892 static void chatserver_notice_part(struct sipe_core_private *sipe_private,
893 SIPE_UNUSED_PARAMETER struct sip_session *session,
894 SIPE_UNUSED_PARAMETER guint result,
895 SIPE_UNUSED_PARAMETER const gchar *message,
896 const sipe_xml *xml)
898 struct sipe_groupchat *groupchat = sipe_private->groupchat;
899 const sipe_xml *chanib;
901 for (chanib = sipe_xml_child(xml, "chanib");
902 chanib;
903 chanib = sipe_xml_twin(chanib)) {
904 const gchar *room_uri = sipe_xml_attribute(chanib, "uri");
906 if (room_uri) {
907 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
908 room_uri);
910 if (chat_session) {
911 const sipe_xml *uib;
913 for (uib = sipe_xml_child(chanib, "uib");
914 uib;
915 uib = sipe_xml_twin(uib)) {
916 const gchar *uri = sipe_xml_attribute(uib, "uri");
918 if (uri) {
919 SIPE_DEBUG_INFO("remove_user: %s from room %s (%s)",
920 uri,
921 chat_session->title,
922 chat_session->id);
923 sipe_backend_chat_remove(chat_session->backend,
924 uri);
932 static const struct response {
933 const gchar *key;
934 void (* const handler)(struct sipe_core_private *,
935 struct sip_session *,
936 guint result, const gchar *,
937 const sipe_xml *xml);
938 } response_table[] = {
939 { "rpl:requri", chatserver_response_uri },
940 { "rpl:chansrch", chatserver_response_channel_search },
941 { "rpl:join", chatserver_response_join },
942 { "rpl:bjoin", chatserver_response_join },
943 { "rpl:bccontext", chatserver_response_history },
944 { "rpl:part", chatserver_response_part },
945 { "ntc:join", chatserver_notice_join },
946 { "ntc:bjoin", chatserver_notice_join },
947 { "ntc:part", chatserver_notice_part },
948 { NULL, NULL }
951 /* Handles rpl:XXX & ntc:YYY */
952 static void chatserver_response(struct sipe_core_private *sipe_private,
953 const sipe_xml *reply,
954 struct sip_session *session)
956 do {
957 const sipe_xml *resp, *data;
958 const gchar *id;
959 gchar *message;
960 guint result = 500;
961 const struct response *r;
963 id = sipe_xml_attribute(reply, "id");
964 if (!id) {
965 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: no reply ID found!");
966 continue;
969 resp = sipe_xml_child(reply, "resp");
970 if (resp) {
971 result = sipe_xml_int_attribute(resp, "code", 500);
972 message = sipe_xml_data(resp);
973 } else {
974 message = g_strdup("");
977 data = sipe_xml_child(reply, "data");
979 SIPE_DEBUG_INFO("chatserver_response: '%s' result (%d) %s",
980 id, result, message ? message : "");
982 for (r = response_table; r->key; r++) {
983 if (sipe_strcase_equal(id, r->key)) {
984 (*r->handler)(sipe_private, session, result, message, data);
985 break;
988 if (!r->key) {
989 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: ignoring unknown response");
992 g_free(message);
993 } while ((reply = sipe_xml_twin(reply)) != NULL);
996 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
997 const sipe_xml *grpchat)
999 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1000 const gchar *uri = sipe_xml_attribute(grpchat, "chanUri");
1001 const gchar *from = sipe_xml_attribute(grpchat, "author");
1002 time_t when = sipe_utils_str_to_time(sipe_xml_attribute(grpchat, "ts"));
1003 gchar *text = sipe_xml_data(sipe_xml_child(grpchat, "chat"));
1004 struct sipe_chat_session *chat_session;
1005 gchar *escaped;
1007 if (!uri || !from) {
1008 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' received without chat room URI or author!",
1009 text ? text : "");
1010 g_free(text);
1011 return;
1014 chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1015 uri);
1016 if (!chat_session) {
1017 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' from '%s' received from unknown chat room '%s'!",
1018 text ? text : "", from, uri);
1019 g_free(text);
1020 return;
1023 /* libxml2 decodes all entities, but the backend expects HTML */
1024 escaped = g_markup_escape_text(text, -1);
1025 g_free(text);
1026 sipe_backend_chat_message(SIPE_CORE_PUBLIC, chat_session->backend,
1027 from, when, escaped);
1028 g_free(escaped);
1031 void process_incoming_info_groupchat(struct sipe_core_private *sipe_private,
1032 struct sipmsg *msg,
1033 struct sip_session *session)
1035 sipe_xml *xml = sipe_xml_parse(msg->body, msg->bodylen);
1036 const sipe_xml *node;
1037 const gchar *callid;
1038 struct sip_dialog *dialog;
1040 callid = sipmsg_find_header(msg, "Call-ID");
1041 dialog = sipe_dialog_find(session, session->with);
1042 if (sipe_strequal(callid, dialog->callid)) {
1044 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1046 if (((node = sipe_xml_child(xml, "rpl")) != NULL) ||
1047 ((node = sipe_xml_child(xml, "ntc")) != NULL)) {
1048 chatserver_response(sipe_private, node, session);
1049 } else if ((node = sipe_xml_child(xml, "grpchat")) != NULL) {
1050 chatserver_grpchat_message(sipe_private, node);
1051 } else {
1052 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_info_groupchat: ignoring unknown response");
1055 } else {
1057 * Our last session got disconnected without proper shutdown,
1058 * e.g. by Pidgin crashing or network connection loss. When
1059 * we reconnect to the group chat the server will send INFO
1060 * messages to the current *AND* the obsolete Call-ID, until
1061 * the obsolete session expires.
1063 * Ignore these INFO messages to avoid, e.g. duplicate texts,
1064 * and respond with an error so that the server knows that we
1065 * consider this dialog to be terminated.
1067 SIPE_DEBUG_INFO("process_incoming_info_groupchat: ignoring unsolicited INFO message to obsolete Call-ID: %s",
1068 callid);
1070 sip_transport_response(sipe_private, msg, 487, "Request Terminated", NULL);
1073 sipe_xml_free(xml);
1076 void sipe_groupchat_send(struct sipe_core_private *sipe_private,
1077 struct sipe_chat_session *chat_session,
1078 const gchar *what)
1080 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1081 gchar *cmd, *self, *timestamp, *tmp;
1082 gchar **lines, **strvp;
1083 struct sipe_groupchat_msg *msg;
1085 if (!groupchat || !chat_session)
1086 return;
1088 SIPE_DEBUG_INFO("sipe_groupchat_send: '%s' to %s",
1089 what, chat_session->id);
1091 self = sip_uri_self(sipe_private);
1092 timestamp = sipe_utils_time_to_str(time(NULL));
1095 * 'what' is already XML-escaped, e.g.
1097 * " -> &quot;
1098 * > -> &gt;
1099 * < -> &lt;
1100 * & -> &amp;
1102 * Group Chat only accepts plain text, not full HTML. So we have to
1103 * strip all HTML tags and XML escape the text.
1105 * Line breaks are encoded as <br> and therefore need to be replaced
1106 * before stripping. In order to prevent HTML stripping to strip line
1107 * endings, we need to split the text into lines on <br>.
1109 lines = g_strsplit(what, "<br>", 0);
1110 for (strvp = lines; *strvp; strvp++) {
1111 /* replace array entry with HTML stripped & XML escaped version */
1112 gchar *stripped = sipe_backend_markup_strip_html(*strvp);
1113 gchar *escaped = g_markup_escape_text(stripped, -1);
1114 g_free(stripped);
1115 g_free(*strvp);
1116 *strvp = escaped;
1118 tmp = g_strjoinv("\r\n", lines);
1119 g_strfreev(lines);
1120 cmd = g_strdup_printf("<grpchat id=\"grpchat\" seqid=\"1\" chanUri=\"%s\" author=\"%s\" ts=\"%s\">"
1121 "<chat>%s</chat>"
1122 "</grpchat>",
1123 chat_session->id, self, timestamp, tmp);
1124 g_free(tmp);
1125 g_free(timestamp);
1126 g_free(self);
1127 msg = chatserver_command(sipe_private, cmd);
1128 g_free(cmd);
1130 if (msg) {
1131 msg->session = chat_session;
1132 msg->content = g_strdup(what);
1133 } else {
1134 chatserver_command_error_notify(sipe_private,
1135 chat_session,
1136 what);
1140 void sipe_groupchat_leave(struct sipe_core_private *sipe_private,
1141 struct sipe_chat_session *chat_session)
1143 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1144 gchar *cmd;
1146 if (!groupchat || !chat_session)
1147 return;
1149 SIPE_DEBUG_INFO("sipe_groupchat_leave: %s", chat_session->id);
1151 cmd = g_strdup_printf("<cmd id=\"cmd:part\" seqid=\"1\">"
1152 "<data>"
1153 "<chanib uri=\"%s\"/>"
1154 "</data>"
1155 "</cmd>", chat_session->id);
1156 chatserver_command(sipe_private, cmd);
1157 g_free(cmd);
1160 gboolean sipe_core_groupchat_query_rooms(struct sipe_core_public *sipe_public)
1162 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1163 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1165 if (!groupchat || !groupchat->connected)
1166 return FALSE;
1168 chatserver_command(sipe_private,
1169 "<cmd id=\"cmd:chansrch\" seqid=\"1\">"
1170 "<data>"
1171 "<qib qtype=\"BYNAME\" criteria=\"\" extended=\"false\"/>"
1172 "</data>"
1173 "</cmd>");
1175 return TRUE;
1178 void sipe_core_groupchat_join(struct sipe_core_public *sipe_public,
1179 const gchar *uri)
1181 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1182 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1184 if (!g_str_has_prefix(uri, "ma-chan://"))
1185 return;
1187 if (!groupchat) {
1188 /* This happens when a user has set auto-join on a channel */
1189 sipe_groupchat_allocate(sipe_private);
1190 groupchat = sipe_private->groupchat;
1193 if (groupchat->connected) {
1194 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1195 uri);
1197 /* Already joined? */
1198 if (chat_session) {
1200 /* Yes, update backend session */
1201 SIPE_DEBUG_INFO("sipe_core_groupchat_join: show '%s' (%s)",
1202 chat_session->title,
1203 chat_session->id);
1204 sipe_backend_chat_show(chat_session->backend);
1206 } else {
1207 /* No, send command out directly */
1208 gchar *chanid = generate_chanid_node(uri, 0);
1209 if (chanid) {
1210 gchar *cmd = g_strdup_printf("<cmd id=\"cmd:join\" seqid=\"1\">"
1211 "<data>%s</data>"
1212 "</cmd>",
1213 chanid);
1214 SIPE_DEBUG_INFO("sipe_core_groupchat_join: join %s",
1215 uri);
1216 chatserver_command(sipe_private, cmd);
1217 g_free(cmd);
1218 g_free(chanid);
1221 } else {
1222 /* Add it to the queue but avoid duplicates */
1223 if (!g_slist_find_custom(groupchat->join_queue, uri,
1224 sipe_strcompare)) {
1225 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_groupchat_join: URI queued");
1226 groupchat->join_queue = g_slist_prepend(groupchat->join_queue,
1227 g_strdup(uri));
1232 void sipe_groupchat_rejoin(struct sipe_core_private *sipe_private,
1233 struct sipe_chat_session *chat_session)
1235 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1237 if (!groupchat) {
1238 /* First rejoined channel after reconnect will trigger this */
1239 sipe_groupchat_allocate(sipe_private);
1240 groupchat = sipe_private->groupchat;
1243 /* Remember "old" session, so that we don't recreate it at join */
1244 g_hash_table_insert(groupchat->uri_to_chat_session,
1245 chat_session->id,
1246 chat_session);
1247 sipe_core_groupchat_join(SIPE_CORE_PUBLIC, chat_session->id);
1251 Local Variables:
1252 mode: c
1253 c-file-style: "bsd"
1254 indent-tabs-mode: t
1255 tab-width: 8
1256 End: