Fix #245: "Unable to resolve DNS SRV record" error when joining conference
[siplcs.git] / src / core / sipe-groupchat.c
blob294cbff502de9724cc79211614ddee476f940fa1
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 void groupchat_update_cb(struct sipe_core_private *sipe_private,
367 gpointer data);
368 static gboolean groupchat_expired_session_response(struct sipe_core_private *sipe_private,
369 struct sipmsg *msg,
370 SIPE_UNUSED_PARAMETER struct transaction *trans)
372 struct sipe_groupchat *groupchat = sipe_private->groupchat;
374 /* 481 Call Leg Does Not Exist -> server dropped session */
375 if (msg->response == 481) {
376 struct sip_session *session = groupchat->session;
377 struct sip_dialog *dialog = sipe_dialog_find(session,
378 session->with);
380 /* close dialog from our side */
381 sip_transport_bye(sipe_private, dialog);
382 sipe_dialog_remove(session, session->with);
383 /* dialog is no longer valid */
385 /* re-initialize groupchat session */
386 groupchat->session = NULL;
387 groupchat->connected = FALSE;
388 sipe_groupchat_init(sipe_private);
389 } else {
390 sipe_schedule_seconds(sipe_private,
391 "<+groupchat-expires>",
392 NULL,
393 groupchat->expires,
394 groupchat_update_cb,
395 NULL);
398 return(TRUE);
401 /* sipe_schedule_action */
402 static void groupchat_update_cb(struct sipe_core_private *sipe_private,
403 SIPE_UNUSED_PARAMETER gpointer data)
405 struct sipe_groupchat *groupchat = sipe_private->groupchat;
407 if (groupchat->session) {
408 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
409 groupchat->session->with);
411 if (dialog)
412 sip_transport_update(sipe_private,
413 dialog,
414 groupchat_expired_session_response);
418 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
419 const gchar *cmd);
421 void sipe_groupchat_invite_response(struct sipe_core_private *sipe_private,
422 struct sip_dialog *dialog,
423 struct sipmsg *response)
425 struct sipe_groupchat *groupchat = sipe_private->groupchat;
427 SIPE_DEBUG_INFO_NOFORMAT("sipe_groupchat_invite_response");
429 if (!groupchat->session) {
430 /* response to initial invite */
431 struct sipe_groupchat_msg *msg = generate_xccos_message(groupchat,
432 "<cmd id=\"cmd:requri\" seqid=\"1\"><data/></cmd>");
433 const gchar *session_expires = sipmsg_find_header(response,
434 "Session-Expires");
436 sip_transport_info(sipe_private,
437 "Content-Type: text/plain\r\n",
438 msg->xccos,
439 dialog,
440 NULL);
441 sipe_groupchat_msg_remove(msg);
443 if (session_expires) {
444 groupchat->expires = strtoul(session_expires, NULL, 10);
446 if (groupchat->expires) {
447 SIPE_DEBUG_INFO("sipe_groupchat_invite_response: session expires in %d seconds",
448 groupchat->expires);
450 if (groupchat->expires > 10)
451 groupchat->expires -= 10;
452 sipe_schedule_seconds(sipe_private,
453 "<+groupchat-expires>",
454 NULL,
455 groupchat->expires,
456 groupchat_update_cb,
457 NULL);
461 } else {
462 /* response to group chat server invite */
463 gchar *invcmd;
465 SIPE_DEBUG_INFO_NOFORMAT("connection to group chat server established.");
467 groupchat->connected = TRUE;
469 /* Any queued joins? */
470 if (groupchat->join_queue) {
471 GString *cmd = g_string_new("<cmd id=\"cmd:bjoin\" seqid=\"1\">"
472 "<data>");
473 GSList *entry;
474 guint i = 0;
476 /* We used g_slist_prepend() to create the list */
477 groupchat->join_queue = entry = g_slist_reverse(groupchat->join_queue);
478 while (entry) {
479 gchar *chanid = generate_chanid_node(entry->data, i++);
480 g_string_append(cmd, chanid);
481 g_free(chanid);
482 entry = entry->next;
484 sipe_groupchat_free_join_queue(groupchat);
486 g_string_append(cmd, "</data></cmd>");
487 chatserver_command(sipe_private, cmd->str);
488 g_string_free(cmd, TRUE);
491 /* Request outstanding invites from server */
492 invcmd = g_strdup_printf("<cmd id=\"cmd:getinv\" seqid=\"1\">"
493 "<data>"
494 "<inv inviteId=\"1\" domain=\"%s\"/>"
495 "</data>"
496 "</cmd>", groupchat->domain);
497 chatserver_command(sipe_private, invcmd);
498 g_free(invcmd);
502 static void chatserver_command_error_notify(struct sipe_core_private *sipe_private,
503 struct sipe_chat_session *chat_session,
504 const gchar *content)
506 gchar *label = g_strdup_printf(_("This message was not delivered to chat room '%s'"),
507 chat_session->title);
508 gchar *errmsg = g_strdup_printf("%s:\n<font color=\"#888888\"></b>%s<b></font>",
509 label, content);
510 g_free(label);
511 sipe_backend_notify_message_error(SIPE_CORE_PUBLIC,
512 chat_session->backend,
513 NULL,
514 errmsg);
515 g_free(errmsg);
518 /* TransCallback */
519 static gboolean chatserver_command_response(struct sipe_core_private *sipe_private,
520 struct sipmsg *msg,
521 struct transaction *trans)
523 if (msg->response != 200) {
524 struct sipe_groupchat_msg *gmsg = trans->payload->data;
525 struct sipe_chat_session *chat_session = gmsg->session;
527 SIPE_DEBUG_INFO("chatserver_command_response: failure %d", msg->response);
529 if (chat_session)
530 chatserver_command_error_notify(sipe_private,
531 chat_session,
532 gmsg->content);
534 groupchat_expired_session_response(sipe_private, msg, trans);
536 return TRUE;
539 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
540 const gchar *cmd)
542 struct sipe_groupchat *groupchat = sipe_private->groupchat;
543 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
544 groupchat->session->with);
545 struct sipe_groupchat_msg *msg = NULL;
547 if (dialog) {
548 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
549 struct transaction *trans;
551 msg = generate_xccos_message(groupchat, cmd);
552 trans = sip_transport_info(sipe_private,
553 "Content-Type: text/plain\r\n",
554 msg->xccos,
555 dialog,
556 chatserver_command_response);
558 payload->destroy = sipe_groupchat_msg_remove;
559 payload->data = msg;
560 trans->payload = payload;
563 return(msg);
566 static void chatserver_response_uri(struct sipe_core_private *sipe_private,
567 struct sip_session *session,
568 SIPE_UNUSED_PARAMETER guint result,
569 SIPE_UNUSED_PARAMETER const gchar *message,
570 const sipe_xml *xml)
572 const sipe_xml *uib = sipe_xml_child(xml, "uib");
573 const gchar *uri = sipe_xml_attribute(uib, "uri");
575 /* drop connection to ocschat@<domain> again */
576 sipe_session_close(sipe_private, session);
578 if (uri) {
579 struct sipe_groupchat *groupchat = sipe_private->groupchat;
581 SIPE_DEBUG_INFO("chatserver_response_uri: '%s'", uri);
583 groupchat->session = session = sipe_session_find_or_add_im(sipe_private,
584 uri);
586 session->is_groupchat = TRUE;
587 sipe_im_invite(sipe_private, session, uri, NULL, NULL, NULL, FALSE);
588 } else {
589 SIPE_DEBUG_WARNING_NOFORMAT("chatserver_response_uri: no server URI found!");
590 groupchat_init_retry(sipe_private);
594 static void chatserver_response_channel_search(struct sipe_core_private *sipe_private,
595 SIPE_UNUSED_PARAMETER struct sip_session *session,
596 guint result,
597 const gchar *message,
598 const sipe_xml *xml)
600 struct sipe_core_public *sipe_public = SIPE_CORE_PUBLIC;
602 if (result != 200) {
603 sipe_backend_notify_error(sipe_public,
604 _("Error retrieving room list"),
605 message);
606 } else {
607 const sipe_xml *chanib;
609 for (chanib = sipe_xml_child(xml, "chanib");
610 chanib;
611 chanib = sipe_xml_twin(chanib)) {
612 const gchar *name = sipe_xml_attribute(chanib, "name");
613 const gchar *desc = sipe_xml_attribute(chanib, "description");
614 const gchar *uri = sipe_xml_attribute(chanib, "uri");
615 const sipe_xml *node;
616 guint user_count = 0;
617 guint32 flags = 0;
619 /* information */
620 for (node = sipe_xml_child(chanib, "info");
621 node;
622 node = sipe_xml_twin(node)) {
623 const gchar *id = sipe_xml_attribute(node, "id");
624 gchar *data;
626 if (!id) continue;
628 data = sipe_xml_data(node);
629 if (data) {
630 if (sipe_strcase_equal(id, "urn:parlano:ma:info:ucnt")) {
631 user_count = g_ascii_strtoll(data, NULL, 10);
632 } else if (sipe_strcase_equal(id, "urn:parlano:ma:info:visibilty")) {
633 if (sipe_strcase_equal(data, "private")) {
634 flags |= SIPE_GROUPCHAT_ROOM_PRIVATE;
637 g_free(data);
641 /* properties */
642 for (node = sipe_xml_child(chanib, "prop");
643 node;
644 node = sipe_xml_twin(node)) {
645 const gchar *id = sipe_xml_attribute(node, "id");
646 gchar *data;
648 if (!id) continue;
650 data = sipe_xml_data(node);
651 if (data) {
652 gboolean value = sipe_strcase_equal(data, "true");
653 g_free(data);
655 if (value) {
656 guint32 add = 0;
657 if (sipe_strcase_equal(id, "urn:parlano:ma:prop:filepost")) {
658 add = SIPE_GROUPCHAT_ROOM_FILEPOST;
659 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:invite")) {
660 add = SIPE_GROUPCHAT_ROOM_INVITE;
661 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:logged")) {
662 add = SIPE_GROUPCHAT_ROOM_LOGGED;
664 flags |= add;
669 SIPE_DEBUG_INFO("group chat channel '%s': '%s' (%s) with %u users, flags 0x%x",
670 name, desc, uri, user_count, flags);
671 sipe_backend_groupchat_room_add(sipe_public,
672 uri, name, desc,
673 user_count, flags);
677 sipe_backend_groupchat_room_terminate(sipe_public);
680 static gboolean is_chanop(const sipe_xml *aib)
682 return sipe_strequal(sipe_xml_attribute(aib, "key"),
683 GROUPCHAT_AIB_KEY_CHANOP);
686 static void add_user(struct sipe_chat_session *chat_session,
687 const gchar *uri,
688 gboolean new, gboolean chanop)
690 SIPE_DEBUG_INFO("add_user: %s%s%s to room %s (%s)",
691 new ? "new " : "",
692 chanop ? "chanop " : "",
693 uri,
694 chat_session->title, chat_session->id);
695 sipe_backend_chat_add(chat_session->backend, uri, new);
696 if (chanop)
697 sipe_backend_chat_operator(chat_session->backend, uri);
700 static void chatserver_response_join(struct sipe_core_private *sipe_private,
701 SIPE_UNUSED_PARAMETER struct sip_session *session,
702 guint result,
703 const gchar *message,
704 const sipe_xml *xml)
706 if (result != 200) {
707 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
708 _("Error joining chat room"),
709 message);
710 } else {
711 struct sipe_groupchat *groupchat = sipe_private->groupchat;
712 const sipe_xml *node;
713 GHashTable *user_ids = g_hash_table_new(g_str_hash, g_str_equal);
715 /* Extract user IDs & URIs and generate ID -> URI map */
716 for (node = sipe_xml_child(xml, "uib");
717 node;
718 node = sipe_xml_twin(node)) {
719 const gchar *id = sipe_xml_attribute(node, "id");
720 const gchar *uri = sipe_xml_attribute(node, "uri");
721 if (id && uri)
722 g_hash_table_insert(user_ids,
723 (gpointer) id,
724 (gpointer) uri);
727 /* Process channel data */
728 for (node = sipe_xml_child(xml, "chanib");
729 node;
730 node = sipe_xml_twin(node)) {
731 const gchar *uri = sipe_xml_attribute(node, "uri");
733 if (uri) {
734 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
735 uri);
736 gboolean new = (chat_session == NULL);
737 const gchar *attr = sipe_xml_attribute(node, "name");
738 gchar *self = sip_uri_self(sipe_private);
739 const sipe_xml *aib;
741 if (new) {
742 chat_session = sipe_chat_create_session(SIPE_CHAT_TYPE_GROUPCHAT,
743 sipe_xml_attribute(node,
744 "uri"),
745 attr ? attr : "");
746 g_hash_table_insert(groupchat->uri_to_chat_session,
747 chat_session->id,
748 chat_session);
750 SIPE_DEBUG_INFO("joined room '%s' (%s)",
751 chat_session->title,
752 chat_session->id);
753 chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
754 chat_session,
755 chat_session->title,
756 self);
757 } else {
758 SIPE_DEBUG_INFO("rejoining room '%s' (%s)",
759 chat_session->title,
760 chat_session->id);
761 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
762 chat_session->backend,
763 self,
764 chat_session->title);
766 g_free(self);
768 attr = sipe_xml_attribute(node, "topic");
769 if (attr) {
770 sipe_backend_chat_topic(chat_session->backend,
771 attr);
774 /* Process user map for channel */
775 for (aib = sipe_xml_child(node, "aib");
776 aib;
777 aib = sipe_xml_twin(aib)) {
778 const gchar *value = sipe_xml_attribute(aib, "value");
779 gboolean chanop = is_chanop(aib);
780 gchar **ids = g_strsplit(value, ",", 0);
782 if (ids) {
783 gchar **uid = ids;
785 while (*uid) {
786 const gchar *uri = g_hash_table_lookup(user_ids,
787 *uid);
788 if (uri)
789 add_user(chat_session,
790 uri,
791 FALSE,
792 chanop);
793 uid++;
796 g_strfreev(ids);
800 /* Request last 25 entries from channel history */
801 self = g_strdup_printf("<cmd id=\"cmd:bccontext\" seqid=\"1\">"
802 "<data>"
803 "<chanib uri=\"%s\"/>"
804 "<bcq><last cnt=\"25\"/></bcq>"
805 "</data>"
806 "</cmd>", chat_session->id);
807 chatserver_command(sipe_private, self);
808 g_free(self);
812 g_hash_table_destroy(user_ids);
816 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
817 const sipe_xml *grpchat);
819 static void chatserver_response_history(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
820 SIPE_UNUSED_PARAMETER struct sip_session *session,
821 SIPE_UNUSED_PARAMETER guint result,
822 SIPE_UNUSED_PARAMETER const gchar *message,
823 const sipe_xml *xml)
825 const sipe_xml *grpchat;
827 for (grpchat = sipe_xml_child(xml, "chanib/msg");
828 grpchat;
829 grpchat = sipe_xml_twin(grpchat))
830 if (sipe_strequal(sipe_xml_attribute(grpchat, "id"),
831 "grpchat"))
832 chatserver_grpchat_message(sipe_private, grpchat);
835 static void chatserver_response_part(struct sipe_core_private *sipe_private,
836 SIPE_UNUSED_PARAMETER struct sip_session *session,
837 guint result,
838 const gchar *message,
839 const sipe_xml *xml)
841 if (result != 200) {
842 SIPE_DEBUG_WARNING("chatserver_response_part: failed with %d: %s. Dropping room",
843 result, message);
844 } else {
845 struct sipe_groupchat *groupchat = sipe_private->groupchat;
846 const gchar *uri = sipe_xml_attribute(sipe_xml_child(xml, "chanib"),
847 "uri");
848 struct sipe_chat_session *chat_session;
850 if (uri &&
851 (chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
852 uri))) {
854 SIPE_DEBUG_INFO("leaving room '%s' (%s)",
855 chat_session->title, chat_session->id);
857 g_hash_table_remove(groupchat->uri_to_chat_session,
858 uri);
859 sipe_chat_remove_session(chat_session);
861 } else {
862 SIPE_DEBUG_WARNING("chatserver_response_part: unknown chat room uri '%s'",
863 uri ? uri : "");
868 static void chatserver_notice_join(struct sipe_core_private *sipe_private,
869 SIPE_UNUSED_PARAMETER struct sip_session *session,
870 SIPE_UNUSED_PARAMETER guint result,
871 SIPE_UNUSED_PARAMETER const gchar *message,
872 const sipe_xml *xml)
874 struct sipe_groupchat *groupchat = sipe_private->groupchat;
875 const sipe_xml *uib;
877 for (uib = sipe_xml_child(xml, "uib");
878 uib;
879 uib = sipe_xml_twin(uib)) {
880 const gchar *uri = sipe_xml_attribute(uib, "uri");
882 if (uri) {
883 const sipe_xml *aib;
885 for (aib = sipe_xml_child(uib, "aib");
886 aib;
887 aib = sipe_xml_twin(aib)) {
888 const gchar *domain = sipe_xml_attribute(aib, "domain");
889 const gchar *path = sipe_xml_attribute(aib, "value");
891 if (domain && path) {
892 gchar *room_uri = g_strdup_printf("ma-chan://%s/%s",
893 domain, path);
894 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
895 room_uri);
896 if (chat_session)
897 add_user(chat_session,
898 uri,
899 TRUE,
900 is_chanop(aib));
902 g_free(room_uri);
909 static void chatserver_notice_part(struct sipe_core_private *sipe_private,
910 SIPE_UNUSED_PARAMETER struct sip_session *session,
911 SIPE_UNUSED_PARAMETER guint result,
912 SIPE_UNUSED_PARAMETER const gchar *message,
913 const sipe_xml *xml)
915 struct sipe_groupchat *groupchat = sipe_private->groupchat;
916 const sipe_xml *chanib;
918 for (chanib = sipe_xml_child(xml, "chanib");
919 chanib;
920 chanib = sipe_xml_twin(chanib)) {
921 const gchar *room_uri = sipe_xml_attribute(chanib, "uri");
923 if (room_uri) {
924 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
925 room_uri);
927 if (chat_session) {
928 const sipe_xml *uib;
930 for (uib = sipe_xml_child(chanib, "uib");
931 uib;
932 uib = sipe_xml_twin(uib)) {
933 const gchar *uri = sipe_xml_attribute(uib, "uri");
935 if (uri) {
936 SIPE_DEBUG_INFO("remove_user: %s from room %s (%s)",
937 uri,
938 chat_session->title,
939 chat_session->id);
940 sipe_backend_chat_remove(chat_session->backend,
941 uri);
949 static const struct response {
950 const gchar *key;
951 void (* const handler)(struct sipe_core_private *,
952 struct sip_session *,
953 guint result, const gchar *,
954 const sipe_xml *xml);
955 } response_table[] = {
956 { "rpl:requri", chatserver_response_uri },
957 { "rpl:chansrch", chatserver_response_channel_search },
958 { "rpl:join", chatserver_response_join },
959 { "rpl:bjoin", chatserver_response_join },
960 { "rpl:bccontext", chatserver_response_history },
961 { "rpl:part", chatserver_response_part },
962 { "ntc:join", chatserver_notice_join },
963 { "ntc:bjoin", chatserver_notice_join },
964 { "ntc:part", chatserver_notice_part },
965 { NULL, NULL }
968 /* Handles rpl:XXX & ntc:YYY */
969 static void chatserver_response(struct sipe_core_private *sipe_private,
970 const sipe_xml *reply,
971 struct sip_session *session)
973 do {
974 const sipe_xml *resp, *data;
975 const gchar *id;
976 gchar *message;
977 guint result = 500;
978 const struct response *r;
980 id = sipe_xml_attribute(reply, "id");
981 if (!id) {
982 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: no reply ID found!");
983 continue;
986 resp = sipe_xml_child(reply, "resp");
987 if (resp) {
988 result = sipe_xml_int_attribute(resp, "code", 500);
989 message = sipe_xml_data(resp);
990 } else {
991 message = g_strdup("");
994 data = sipe_xml_child(reply, "data");
996 SIPE_DEBUG_INFO("chatserver_response: '%s' result (%d) %s",
997 id, result, message ? message : "");
999 for (r = response_table; r->key; r++) {
1000 if (sipe_strcase_equal(id, r->key)) {
1001 (*r->handler)(sipe_private, session, result, message, data);
1002 break;
1005 if (!r->key) {
1006 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: ignoring unknown response");
1009 g_free(message);
1010 } while ((reply = sipe_xml_twin(reply)) != NULL);
1013 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
1014 const sipe_xml *grpchat)
1016 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1017 const gchar *uri = sipe_xml_attribute(grpchat, "chanUri");
1018 const gchar *from = sipe_xml_attribute(grpchat, "author");
1019 time_t when = sipe_utils_str_to_time(sipe_xml_attribute(grpchat, "ts"));
1020 gchar *text = sipe_xml_data(sipe_xml_child(grpchat, "chat"));
1021 struct sipe_chat_session *chat_session;
1022 gchar *escaped;
1024 if (!uri || !from) {
1025 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' received without chat room URI or author!",
1026 text ? text : "");
1027 g_free(text);
1028 return;
1031 chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1032 uri);
1033 if (!chat_session) {
1034 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' from '%s' received from unknown chat room '%s'!",
1035 text ? text : "", from, uri);
1036 g_free(text);
1037 return;
1040 /* libxml2 decodes all entities, but the backend expects HTML */
1041 escaped = g_markup_escape_text(text, -1);
1042 g_free(text);
1043 sipe_backend_chat_message(SIPE_CORE_PUBLIC, chat_session->backend,
1044 from, when, escaped);
1045 g_free(escaped);
1048 void process_incoming_info_groupchat(struct sipe_core_private *sipe_private,
1049 struct sipmsg *msg,
1050 struct sip_session *session)
1052 sipe_xml *xml = sipe_xml_parse(msg->body, msg->bodylen);
1053 const sipe_xml *node;
1054 const gchar *callid;
1055 struct sip_dialog *dialog;
1057 callid = sipmsg_find_header(msg, "Call-ID");
1058 dialog = sipe_dialog_find(session, session->with);
1059 if (sipe_strequal(callid, dialog->callid)) {
1061 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1063 if (((node = sipe_xml_child(xml, "rpl")) != NULL) ||
1064 ((node = sipe_xml_child(xml, "ntc")) != NULL)) {
1065 chatserver_response(sipe_private, node, session);
1066 } else if ((node = sipe_xml_child(xml, "grpchat")) != NULL) {
1067 chatserver_grpchat_message(sipe_private, node);
1068 } else {
1069 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_info_groupchat: ignoring unknown response");
1072 } else {
1074 * Our last session got disconnected without proper shutdown,
1075 * e.g. by Pidgin crashing or network connection loss. When
1076 * we reconnect to the group chat the server will send INFO
1077 * messages to the current *AND* the obsolete Call-ID, until
1078 * the obsolete session expires.
1080 * Ignore these INFO messages to avoid, e.g. duplicate texts,
1081 * and respond with an error so that the server knows that we
1082 * consider this dialog to be terminated.
1084 SIPE_DEBUG_INFO("process_incoming_info_groupchat: ignoring unsolicited INFO message to obsolete Call-ID: %s",
1085 callid);
1087 sip_transport_response(sipe_private, msg, 487, "Request Terminated", NULL);
1090 sipe_xml_free(xml);
1093 void sipe_groupchat_send(struct sipe_core_private *sipe_private,
1094 struct sipe_chat_session *chat_session,
1095 const gchar *what)
1097 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1098 gchar *cmd, *self, *timestamp, *tmp;
1099 gchar **lines, **strvp;
1100 struct sipe_groupchat_msg *msg;
1102 if (!groupchat || !chat_session)
1103 return;
1105 SIPE_DEBUG_INFO("sipe_groupchat_send: '%s' to %s",
1106 what, chat_session->id);
1108 self = sip_uri_self(sipe_private);
1109 timestamp = sipe_utils_time_to_str(time(NULL));
1112 * 'what' is already XML-escaped, e.g.
1114 * " -> &quot;
1115 * > -> &gt;
1116 * < -> &lt;
1117 * & -> &amp;
1119 * Group Chat only accepts plain text, not full HTML. So we have to
1120 * strip all HTML tags and XML escape the text.
1122 * Line breaks are encoded as <br> and therefore need to be replaced
1123 * before stripping. In order to prevent HTML stripping to strip line
1124 * endings, we need to split the text into lines on <br>.
1126 lines = g_strsplit(what, "<br>", 0);
1127 for (strvp = lines; *strvp; strvp++) {
1128 /* replace array entry with HTML stripped & XML escaped version */
1129 gchar *stripped = sipe_backend_markup_strip_html(*strvp);
1130 gchar *escaped = g_markup_escape_text(stripped, -1);
1131 g_free(stripped);
1132 g_free(*strvp);
1133 *strvp = escaped;
1135 tmp = g_strjoinv("\r\n", lines);
1136 g_strfreev(lines);
1137 cmd = g_strdup_printf("<grpchat id=\"grpchat\" seqid=\"1\" chanUri=\"%s\" author=\"%s\" ts=\"%s\">"
1138 "<chat>%s</chat>"
1139 "</grpchat>",
1140 chat_session->id, self, timestamp, tmp);
1141 g_free(tmp);
1142 g_free(timestamp);
1143 g_free(self);
1144 msg = chatserver_command(sipe_private, cmd);
1145 g_free(cmd);
1147 if (msg) {
1148 msg->session = chat_session;
1149 msg->content = g_strdup(what);
1150 } else {
1151 chatserver_command_error_notify(sipe_private,
1152 chat_session,
1153 what);
1157 void sipe_groupchat_leave(struct sipe_core_private *sipe_private,
1158 struct sipe_chat_session *chat_session)
1160 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1161 gchar *cmd;
1163 if (!groupchat || !chat_session)
1164 return;
1166 SIPE_DEBUG_INFO("sipe_groupchat_leave: %s", chat_session->id);
1168 cmd = g_strdup_printf("<cmd id=\"cmd:part\" seqid=\"1\">"
1169 "<data>"
1170 "<chanib uri=\"%s\"/>"
1171 "</data>"
1172 "</cmd>", chat_session->id);
1173 chatserver_command(sipe_private, cmd);
1174 g_free(cmd);
1177 gboolean sipe_core_groupchat_query_rooms(struct sipe_core_public *sipe_public)
1179 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1180 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1182 if (!groupchat || !groupchat->connected)
1183 return FALSE;
1185 chatserver_command(sipe_private,
1186 "<cmd id=\"cmd:chansrch\" seqid=\"1\">"
1187 "<data>"
1188 "<qib qtype=\"BYNAME\" criteria=\"\" extended=\"false\"/>"
1189 "</data>"
1190 "</cmd>");
1192 return TRUE;
1195 void sipe_core_groupchat_join(struct sipe_core_public *sipe_public,
1196 const gchar *uri)
1198 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1199 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1201 if (!g_str_has_prefix(uri, "ma-chan://"))
1202 return;
1204 if (!groupchat) {
1205 /* This happens when a user has set auto-join on a channel */
1206 sipe_groupchat_allocate(sipe_private);
1207 groupchat = sipe_private->groupchat;
1210 if (groupchat->connected) {
1211 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1212 uri);
1214 /* Already joined? */
1215 if (chat_session) {
1217 /* Yes, update backend session */
1218 SIPE_DEBUG_INFO("sipe_core_groupchat_join: show '%s' (%s)",
1219 chat_session->title,
1220 chat_session->id);
1221 sipe_backend_chat_show(chat_session->backend);
1223 } else {
1224 /* No, send command out directly */
1225 gchar *chanid = generate_chanid_node(uri, 0);
1226 if (chanid) {
1227 gchar *cmd = g_strdup_printf("<cmd id=\"cmd:join\" seqid=\"1\">"
1228 "<data>%s</data>"
1229 "</cmd>",
1230 chanid);
1231 SIPE_DEBUG_INFO("sipe_core_groupchat_join: join %s",
1232 uri);
1233 chatserver_command(sipe_private, cmd);
1234 g_free(cmd);
1235 g_free(chanid);
1238 } else {
1239 /* Add it to the queue but avoid duplicates */
1240 if (!g_slist_find_custom(groupchat->join_queue, uri,
1241 sipe_strcompare)) {
1242 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_groupchat_join: URI queued");
1243 groupchat->join_queue = g_slist_prepend(groupchat->join_queue,
1244 g_strdup(uri));
1249 void sipe_groupchat_rejoin(struct sipe_core_private *sipe_private,
1250 struct sipe_chat_session *chat_session)
1252 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1254 if (!groupchat) {
1255 /* First rejoined channel after reconnect will trigger this */
1256 sipe_groupchat_allocate(sipe_private);
1257 groupchat = sipe_private->groupchat;
1260 /* Remember "old" session, so that we don't recreate it at join */
1261 g_hash_table_insert(groupchat->uri_to_chat_session,
1262 chat_session->id,
1263 chat_session);
1264 sipe_core_groupchat_join(SIPE_CORE_PUBLIC, chat_session->id);
1268 Local Variables:
1269 mode: c
1270 c-file-style: "bsd"
1271 indent-tabs-mode: t
1272 tab-width: 8
1273 End: