core cleanup: separate code for sipe_backend_account_status_and_note()
[siplcs.git] / src / core / sipe-groupchat.c
blobec1cb56886bb77354fe04469f411b16e05ac67cd
1 /**
2 * @file sipe-groupchat.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 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 * XML XCCOS message specification
35 * <???> (searches on the internet currently reveal nothing)
38 * @TODO:
40 * -.cmd:getserverinfo
41 * <sib domain="<DOMAIN>" infoType="123" />
42 * rpl:getservinfo
43 * <sib infoType="123"
44 * serverTime="2010-09-14T14:26:17.6206356Z"
45 * searchLimit="999"
46 * messageSizeLimit="512"
47 * storySizeLimit="4096"
48 * rootUri="ma-cat://<DOMAIN>/<GUID>"
49 * dbVersion="3ea3a5a8-ef36-46cf-898f-7a5133931d63"
50 * />
52 * is there any information in there we would need/use?
54 * - cmd:getpref/rpl:getpref/cmd:setpref/rpl:setpref
55 * probably useless, as libpurple stores configuration locally
57 * can store base64 encoded "free text" in key/value fashion
58 * <cmd id="cmd:getpref" seqid="x">
59 * <data>
60 * <pref label="kedzie.GroupChannels"
61 * seqid="71"
62 * createdefault="true" />
63 * </data>
64 * </cmd>
65 * <cmd id="cmd:setpref" seqid="x">
66 * <data>
67 * <pref label="kedzie.GroupChannels"
68 * seqid="71"
69 * createdefault="false"
70 * content="<BASE64 text>" />
71 * </data>
72 * </cmd>
74 * use this to sync chats in buddy list on multiple clients?
76 * - cmd:bccontext
77 * send after cmd:join to trigger rpl:bccontext
79 * - rpl:bccontext
80 * according to available documentation delivers channel history, etc.
81 * [no log file examples]
82 * can we add the history to the window when we open the window, or would
83 * that confuse users that use history based on client log?
85 * - cmd:getinv
86 * <inv inviteId="1" domain="<DOMAIN>" />
87 * rpl:getinv
88 * ???
90 * according to documentation should provide list of outstanding invites.
91 * [no log file examples]
92 * should we automatically join those channels or ask user to join/add?
94 * - chatserver_command_message()
95 * needs to support multiple <chatgrp> nodes?
96 * [no log file examples]
98 * - create/delete chat rooms
99 * [no log file examples]
100 * are these related to this functionality?
102 * <cmd id="cmd:nodespermcreatechild" seqid="1">
103 * <data />
104 * </cmd>
105 * <rpl id="rpl:nodespermcreatechild" seqid="1">
106 * <commandid seqid="1" envid="xxx" />
107 * <resp code="200">SUCCESS_OK</resp>
108 * <data />
109 * </rpl>
111 * - file transfer (uses HTTPS PUT/GET via a filestore server)
112 * [no log file examples]
116 #ifdef HAVE_CONFIG_H
117 #include "config.h"
118 #endif
120 #include <stdlib.h>
121 #include <string.h>
123 #include <glib.h>
125 #include "sipe-common.h"
126 #include "sipmsg.h"
127 #include "sip-transport.h"
128 #include "sipe-backend.h"
129 #include "sipe-chat.h"
130 #include "sipe-core.h"
131 #include "sipe-core-private.h"
132 #include "sipe-dialog.h"
133 #include "sipe-groupchat.h"
134 #include "sipe-im.h"
135 #include "sipe-nls.h"
136 #include "sipe-schedule.h"
137 #include "sipe-session.h"
138 #include "sipe-utils.h"
139 #include "sipe-xml.h"
141 #define GROUPCHAT_RETRY_TIMEOUT 5*60 /* seconds */
144 * aib node - magic numbers?
146 * Example:
147 * <aib key="3984" value="0,1,2,3,4,5,7,9,10,12,13,14,15,16,17" />
148 * <aib key="12276" value="6,8,11" />
150 * "value" corresponds to the "id" attribute in uib nodes.
152 * @TODO: Confirm "guessed" meaning of the magic numbers:
153 * 3984 = normal users
154 * 12276 = channel operators
156 #define GROUPCHAT_AIB_KEY_USER "3984"
157 #define GROUPCHAT_AIB_KEY_CHANOP "12276"
159 struct sipe_groupchat {
160 struct sip_session *session;
161 gchar *domain;
162 GSList *join_queue;
163 GHashTable *uri_to_chat_session;
164 GHashTable *msgs;
165 guint envid;
166 gboolean connected;
169 struct sipe_groupchat_msg {
170 GHashTable *container;
171 struct sipe_chat_session *session;
172 gchar *content;
173 gchar *xccos;
174 guint envid;
177 /* GDestroyNotify */
178 static void sipe_groupchat_msg_free(gpointer data) {
179 struct sipe_groupchat_msg *msg = data;
180 g_free(msg->content);
181 g_free(msg->xccos);
182 g_free(msg);
185 /* GDestroyNotify */
186 static void sipe_groupchat_msg_remove(gpointer data) {
187 struct sipe_groupchat_msg *msg = data;
188 g_hash_table_remove(msg->container, &msg->envid);
191 static void sipe_groupchat_allocate(struct sipe_core_private *sipe_private)
193 struct sipe_groupchat *groupchat = g_new0(struct sipe_groupchat, 1);
195 groupchat->uri_to_chat_session = g_hash_table_new(g_str_hash, g_str_equal);
196 groupchat->msgs = g_hash_table_new_full(g_int_hash, g_int_equal,
197 NULL,
198 sipe_groupchat_msg_free);
199 groupchat->envid = rand();
200 groupchat->connected = FALSE;
201 sipe_private->groupchat = groupchat;
204 static void sipe_groupchat_free_join_queue(struct sipe_groupchat *groupchat)
206 GSList *entry = groupchat->join_queue;
207 while (entry) {
208 g_free(entry->data);
209 entry = entry->next;
211 g_slist_free(groupchat->join_queue);
212 groupchat->join_queue = NULL;
215 void sipe_groupchat_free(struct sipe_core_private *sipe_private)
217 struct sipe_groupchat *groupchat = sipe_private->groupchat;
218 if (groupchat) {
219 sipe_groupchat_free_join_queue(groupchat);
220 g_hash_table_destroy(groupchat->msgs);
221 g_hash_table_destroy(groupchat->uri_to_chat_session);
222 g_free(groupchat->domain);
223 g_free(groupchat);
224 sipe_private->groupchat = NULL;
228 static struct sipe_groupchat_msg *generate_xccos_message(struct sipe_groupchat *groupchat,
229 const gchar *content)
231 struct sipe_groupchat_msg *msg = g_new0(struct sipe_groupchat_msg, 1);
233 msg->container = groupchat->msgs;
234 msg->envid = groupchat->envid++;
235 msg->xccos = g_strdup_printf("<xccos ver=\"1\" envid=\"%u\" xmlns=\"urn:parlano:xml:ns:xccos\">"
236 "%s"
237 "</xccos>",
238 msg->envid,
239 content);
241 g_hash_table_insert(groupchat->msgs, &msg->envid, msg);
243 return(msg);
247 * Create short-lived dialog with ocschat@<domain> (or user specified value)
248 * This initiates the Group Chat feature
250 void sipe_groupchat_init(struct sipe_core_private *sipe_private)
252 const gchar *setting = sipe_backend_setting(SIPE_CORE_PUBLIC,
253 SIPE_SETTING_GROUPCHAT_USER);
254 gboolean user_set = !is_empty(setting);
255 gchar **parts = g_strsplit(user_set ? setting : sipe_private->username,
256 "@", 2);
257 gboolean domain_found = !is_empty(parts[1]);
258 const gchar *user = "ocschat";
259 const gchar *domain = parts[domain_found ? 1 : 0];
260 gchar *chat_uri;
261 struct sip_session *session;
262 struct sipe_groupchat *groupchat;
264 /* User specified valid 'user@company.com' */
265 if (user_set && domain_found && !is_empty(parts[0]))
266 user = parts[0];
268 SIPE_DEBUG_INFO("sipe_groupchat_init: username '%s' setting '%s' split '%s'/'%s' GC user %s@%s",
269 sipe_private->username, setting ? setting : "(null)", parts[0],
270 parts[1] ? parts[1] : "(null)", user, domain);
272 if (!sipe_private->groupchat)
273 sipe_groupchat_allocate(sipe_private);
274 groupchat = sipe_private->groupchat;
276 chat_uri = g_strdup_printf("sip:%s@%s", user, domain);
277 session = sipe_session_find_or_add_im(sipe_private,
278 chat_uri);
279 session->is_groupchat = TRUE;
280 sipe_im_invite(sipe_private, session, chat_uri,
281 NULL, NULL, NULL, FALSE);
283 g_free(groupchat->domain);
284 groupchat->domain = g_strdup(domain);
286 g_free(chat_uri);
287 g_strfreev(parts);
290 /* sipe_schedule_action */
291 static void groupchat_init_retry_cb(struct sipe_core_private *sipe_private,
292 SIPE_UNUSED_PARAMETER gpointer data)
294 sipe_groupchat_init(sipe_private);
297 static void groupchat_init_retry(struct sipe_core_private *sipe_private)
299 struct sipe_groupchat *groupchat = sipe_private->groupchat;
301 SIPE_DEBUG_INFO_NOFORMAT("groupchat_init_retry: trying again later...");
303 groupchat->session = NULL;
304 groupchat->connected = FALSE;
306 sipe_schedule_seconds(sipe_private,
307 "<+grouchat-retry>",
308 NULL,
309 GROUPCHAT_RETRY_TIMEOUT,
310 groupchat_init_retry_cb,
311 NULL);
314 void sipe_groupchat_invite_failed(struct sipe_core_private *sipe_private,
315 struct sip_session *session)
317 struct sipe_groupchat *groupchat = sipe_private->groupchat;
318 const gchar *setting = sipe_backend_setting(SIPE_CORE_PUBLIC,
319 SIPE_SETTING_GROUPCHAT_USER);
321 if (groupchat->session) {
322 /* response to group chat server invite */
323 SIPE_DEBUG_ERROR_NOFORMAT("can't connect to group chat server!");
324 } else {
325 /* response to initial invite */
326 SIPE_DEBUG_INFO_NOFORMAT("no group chat server found.");
329 sipe_session_close(sipe_private, session);
330 groupchat_init_retry(sipe_private);
332 if (!is_empty(setting)) {
333 gchar *msg = g_strdup_printf(_("Group Chat Proxy setting is incorrect:\n\n\t%s\n\nPlease update your Account."),
334 setting);
335 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
336 _("Couldn't find Group Chat server!"),
337 msg);
338 g_free(msg);
342 static gchar *generate_chanid_node(const gchar *uri, guint key)
344 /* ma-chan://<domain>/<value> */
345 gchar **parts = g_strsplit(uri, "/", 4);
346 gchar *chanid = NULL;
348 if (parts[2] && parts[3]) {
349 chanid = g_strdup_printf("<chanid key=\"%d\" domain=\"%s\" value=\"%s\"/>",
350 key, parts[2], parts[3]);
351 } else {
352 SIPE_DEBUG_ERROR("generate_chanid_node: mal-formed URI '%s'",
353 uri);
355 g_strfreev(parts);
357 return chanid;
360 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
361 const gchar *cmd);
363 void sipe_groupchat_invite_response(struct sipe_core_private *sipe_private,
364 struct sip_dialog *dialog)
366 struct sipe_groupchat *groupchat = sipe_private->groupchat;
368 SIPE_DEBUG_INFO_NOFORMAT("sipe_groupchat_invite_response");
370 if (!groupchat->session) {
371 /* response to initial invite */
372 struct sipe_groupchat_msg *msg = generate_xccos_message(groupchat,
373 "<cmd id=\"cmd:requri\" seqid=\"1\"><data/></cmd>");
374 sip_transport_info(sipe_private,
375 "Content-Type: text/plain\r\n",
376 msg->xccos,
377 dialog,
378 NULL);
379 sipe_groupchat_msg_remove(msg);
381 } else {
382 /* response to group chat server invite */
383 gchar *invcmd;
385 SIPE_DEBUG_INFO_NOFORMAT("connection to group chat server established.");
387 groupchat->connected = TRUE;
389 /* Any queued joins? */
390 if (groupchat->join_queue) {
391 GString *cmd = g_string_new("<cmd id=\"cmd:bjoin\" seqid=\"1\">"
392 "<data>");
393 GSList *entry;
394 guint i = 0;
396 /* We used g_slist_prepend() to create the list */
397 groupchat->join_queue = entry = g_slist_reverse(groupchat->join_queue);
398 while (entry) {
399 gchar *chanid = generate_chanid_node(entry->data, i++);
400 g_string_append(cmd, chanid);
401 g_free(chanid);
402 entry = entry->next;
404 sipe_groupchat_free_join_queue(groupchat);
406 g_string_append(cmd, "</data></cmd>");
407 chatserver_command(sipe_private, cmd->str);
408 g_string_free(cmd, TRUE);
411 /* Request outstanding invites from server */
412 invcmd = g_strdup_printf("<cmd id=\"cmd:getinv\" seqid=\"1\">"
413 "<data>"
414 "<inv inviteId=\"1\" domain=\"%s\"/>"
415 "</data>"
416 "</cmd>", groupchat->domain);
417 chatserver_command(sipe_private, invcmd);
418 g_free(invcmd);
422 /* TransCallback */
423 static gboolean chatserver_command_response(struct sipe_core_private *sipe_private,
424 struct sipmsg *msg,
425 struct transaction *trans)
427 if (msg->response != 200) {
428 struct sipe_groupchat_msg *gmsg = trans->payload->data;
429 struct sipe_chat_session *chat_session = gmsg->session;
431 SIPE_DEBUG_INFO("chatserver_command_response: failure %d", msg->response);
433 if (chat_session) {
434 gchar *label = g_strdup_printf(_("This message was not delivered to chat room '%s'"),
435 chat_session->title);
436 gchar *errmsg = g_strdup_printf("%s:\n<font color=\"#888888\"></b>%s<b></font>",
437 label, gmsg->content);
438 g_free(label);
439 sipe_backend_notify_message_error(SIPE_CORE_PUBLIC,
440 chat_session->backend,
441 NULL,
442 errmsg);
443 g_free(errmsg);
446 return TRUE;
449 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
450 const gchar *cmd)
452 struct sipe_groupchat *groupchat = sipe_private->groupchat;
453 struct sipe_groupchat_msg *msg = generate_xccos_message(groupchat, cmd);
455 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
456 groupchat->session->with);
458 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
459 struct transaction *trans = sip_transport_info(sipe_private,
460 "Content-Type: text/plain\r\n",
461 msg->xccos,
462 dialog,
463 chatserver_command_response);
465 payload->destroy = sipe_groupchat_msg_remove;
466 payload->data = msg;
467 trans->payload = payload;
469 return(msg);
472 static void chatserver_response_uri(struct sipe_core_private *sipe_private,
473 struct sip_session *session,
474 SIPE_UNUSED_PARAMETER guint result,
475 SIPE_UNUSED_PARAMETER const gchar *message,
476 const sipe_xml *xml)
478 const sipe_xml *uib = sipe_xml_child(xml, "uib");
479 const gchar *uri = sipe_xml_attribute(uib, "uri");
481 /* drop connection to ocschat@<domain> again */
482 sipe_session_close(sipe_private, session);
484 if (uri) {
485 struct sipe_groupchat *groupchat = sipe_private->groupchat;
487 SIPE_DEBUG_INFO("chatserver_response_uri: '%s'", uri);
489 groupchat->session = session = sipe_session_find_or_add_im(sipe_private,
490 uri);
492 session->is_groupchat = TRUE;
493 sipe_im_invite(sipe_private, session, uri, NULL, NULL, NULL, FALSE);
494 } else {
495 SIPE_DEBUG_WARNING_NOFORMAT("process_incoming_info_groupchat: no server URI found!");
496 groupchat_init_retry(sipe_private);
500 static void chatserver_response_channel_search(struct sipe_core_private *sipe_private,
501 SIPE_UNUSED_PARAMETER struct sip_session *session,
502 guint result,
503 const gchar *message,
504 const sipe_xml *xml)
506 struct sipe_core_public *sipe_public = SIPE_CORE_PUBLIC;
508 if (result != 200) {
509 sipe_backend_notify_error(sipe_public,
510 _("Error retrieving room list"),
511 message);
512 } else {
513 const sipe_xml *chanib;
515 for (chanib = sipe_xml_child(xml, "chanib");
516 chanib;
517 chanib = sipe_xml_twin(chanib)) {
518 const gchar *name = sipe_xml_attribute(chanib, "name");
519 const gchar *desc = sipe_xml_attribute(chanib, "description");
520 const gchar *uri = sipe_xml_attribute(chanib, "uri");
521 const sipe_xml *node;
522 guint user_count = 0;
523 guint32 flags = 0;
525 /* information */
526 for (node = sipe_xml_child(chanib, "info");
527 node;
528 node = sipe_xml_twin(node)) {
529 const gchar *id = sipe_xml_attribute(node, "id");
530 gchar *data;
532 if (!id) continue;
534 data = sipe_xml_data(node);
535 if (data) {
536 if (sipe_strcase_equal(id, "urn:parlano:ma:info:ucnt")) {
537 user_count = g_ascii_strtoll(data, NULL, 10);
538 } else if (sipe_strcase_equal(id, "urn:parlano:ma:info:visibilty")) {
539 if (sipe_strcase_equal(data, "private")) {
540 flags |= SIPE_GROUPCHAT_ROOM_PRIVATE;
543 g_free(data);
547 /* properties */
548 for (node = sipe_xml_child(chanib, "prop");
549 node;
550 node = sipe_xml_twin(node)) {
551 const gchar *id = sipe_xml_attribute(node, "id");
552 gchar *data;
554 if (!id) continue;
556 data = sipe_xml_data(node);
557 if (data) {
558 gboolean value = sipe_strcase_equal(data, "true");
559 g_free(data);
561 if (value) {
562 guint32 add = 0;
563 if (sipe_strcase_equal(id, "urn:parlano:ma:prop:filepost")) {
564 add = SIPE_GROUPCHAT_ROOM_FILEPOST;
565 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:invite")) {
566 add = SIPE_GROUPCHAT_ROOM_INVITE;
567 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:logged")) {
568 add = SIPE_GROUPCHAT_ROOM_LOGGED;
570 flags |= add;
575 SIPE_DEBUG_INFO("group chat channel '%s': '%s' (%s) with %u users, flags 0x%x",
576 name, desc, uri, user_count, flags);
577 sipe_backend_groupchat_room_add(sipe_public,
578 uri, name, desc,
579 user_count, flags);
583 sipe_backend_groupchat_room_terminate(sipe_public);
586 static gboolean is_chanop(const sipe_xml *aib)
588 return sipe_strequal(sipe_xml_attribute(aib, "key"),
589 GROUPCHAT_AIB_KEY_CHANOP);
592 static void add_user(struct sipe_chat_session *chat_session,
593 const gchar *uri,
594 gboolean new, gboolean chanop)
596 SIPE_DEBUG_INFO("add_user: %s%s%s to room %s (%s)",
597 new ? "new " : "",
598 chanop ? "chanop " : "",
599 uri,
600 chat_session->title, chat_session->id);
601 sipe_backend_chat_add(chat_session->backend, uri, new);
602 if (chanop)
603 sipe_backend_chat_operator(chat_session->backend, uri);
606 static void chatserver_response_join(struct sipe_core_private *sipe_private,
607 SIPE_UNUSED_PARAMETER struct sip_session *session,
608 guint result,
609 const gchar *message,
610 const sipe_xml *xml)
612 if (result != 200) {
613 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
614 _("Error joining chat room"),
615 message);
616 } else {
617 struct sipe_groupchat *groupchat = sipe_private->groupchat;
618 const sipe_xml *node;
619 GHashTable *user_ids = g_hash_table_new(g_str_hash, g_str_equal);
621 /* Extract user IDs & URIs and generate ID -> URI map */
622 for (node = sipe_xml_child(xml, "uib");
623 node;
624 node = sipe_xml_twin(node)) {
625 const gchar *id = sipe_xml_attribute(node, "id");
626 const gchar *uri = sipe_xml_attribute(node, "uri");
627 if (id && uri)
628 g_hash_table_insert(user_ids,
629 (gpointer) id,
630 (gpointer) uri);
633 /* Process channel data */
634 for (node = sipe_xml_child(xml, "chanib");
635 node;
636 node = sipe_xml_twin(node)) {
637 const gchar *uri = sipe_xml_attribute(node, "uri");
639 if (uri) {
640 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
641 uri);
642 gboolean new = (chat_session == NULL);
643 const gchar *attr = sipe_xml_attribute(node, "name");
644 char *self = sip_uri_self(sipe_private);
645 const sipe_xml *aib;
647 if (new) {
648 chat_session = sipe_chat_create_session(SIPE_CHAT_TYPE_GROUPCHAT,
649 sipe_xml_attribute(node,
650 "uri"),
651 attr ? attr : "");
652 g_hash_table_insert(groupchat->uri_to_chat_session,
653 chat_session->id,
654 chat_session);
656 SIPE_DEBUG_INFO("joined room '%s' (%s)",
657 chat_session->title,
658 chat_session->id);
659 chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
660 chat_session,
661 chat_session->title,
662 self);
663 } else {
664 SIPE_DEBUG_INFO("rejoining room '%s' (%s)",
665 chat_session->title,
666 chat_session->id);
667 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
668 chat_session->backend,
669 self,
670 chat_session->title);
672 g_free(self);
674 attr = sipe_xml_attribute(node, "topic");
675 if (attr) {
676 sipe_backend_chat_topic(chat_session->backend,
677 attr);
680 /* Process user map for channel */
681 for (aib = sipe_xml_child(node, "aib");
682 aib;
683 aib = sipe_xml_twin(aib)) {
684 const gchar *value = sipe_xml_attribute(aib, "value");
685 gboolean chanop = is_chanop(aib);
686 gchar **ids = g_strsplit(value, ",", 0);
688 if (ids) {
689 gchar **uid = ids;
691 while (*uid) {
692 const gchar *uri = g_hash_table_lookup(user_ids,
693 *uid);
694 if (uri)
695 add_user(chat_session,
696 uri,
697 FALSE,
698 chanop);
699 uid++;
702 g_strfreev(ids);
708 g_hash_table_destroy(user_ids);
712 static void chatserver_response_part(struct sipe_core_private *sipe_private,
713 SIPE_UNUSED_PARAMETER struct sip_session *session,
714 guint result,
715 const gchar *message,
716 const sipe_xml *xml)
718 if (result != 200) {
719 SIPE_DEBUG_WARNING("chatserver_response_part: failed with %d: %s. Dropping room",
720 result, message);
721 } else {
722 struct sipe_groupchat *groupchat = sipe_private->groupchat;
723 const gchar *uri = sipe_xml_attribute(sipe_xml_child(xml, "chanib"),
724 "uri");
725 struct sipe_chat_session *chat_session;
727 if (uri &&
728 (chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
729 uri))) {
731 SIPE_DEBUG_INFO("leaving room '%s' (%s)",
732 chat_session->title, chat_session->id);
734 g_hash_table_remove(groupchat->uri_to_chat_session,
735 uri);
736 sipe_chat_remove_session(chat_session);
738 } else {
739 SIPE_DEBUG_WARNING("chatserver_response_part: unknown chat room uri '%s'",
740 uri ? uri : "");
745 static void chatserver_notice_join(struct sipe_core_private *sipe_private,
746 SIPE_UNUSED_PARAMETER struct sip_session *session,
747 SIPE_UNUSED_PARAMETER guint result,
748 SIPE_UNUSED_PARAMETER const gchar *message,
749 const sipe_xml *xml)
751 struct sipe_groupchat *groupchat = sipe_private->groupchat;
752 const sipe_xml *uib;
754 for (uib = sipe_xml_child(xml, "uib");
755 uib;
756 uib = sipe_xml_twin(uib)) {
757 const gchar *uri = sipe_xml_attribute(uib, "uri");
759 if (uri) {
760 const sipe_xml *aib;
762 for (aib = sipe_xml_child(uib, "aib");
763 aib;
764 aib = sipe_xml_twin(aib)) {
765 const gchar *domain = sipe_xml_attribute(aib, "domain");
766 const gchar *path = sipe_xml_attribute(aib, "value");
768 if (domain && path) {
769 gchar *room_uri = g_strdup_printf("ma-chan://%s/%s",
770 domain, path);
771 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
772 room_uri);
773 if (chat_session)
774 add_user(chat_session,
775 uri,
776 TRUE,
777 is_chanop(aib));
779 g_free(room_uri);
786 static void chatserver_notice_part(struct sipe_core_private *sipe_private,
787 SIPE_UNUSED_PARAMETER struct sip_session *session,
788 SIPE_UNUSED_PARAMETER guint result,
789 SIPE_UNUSED_PARAMETER const gchar *message,
790 const sipe_xml *xml)
792 struct sipe_groupchat *groupchat = sipe_private->groupchat;
793 const sipe_xml *chanib;
795 for (chanib = sipe_xml_child(xml, "chanib");
796 chanib;
797 chanib = sipe_xml_twin(chanib)) {
798 const gchar *room_uri = sipe_xml_attribute(chanib, "uri");
800 if (room_uri) {
801 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
802 room_uri);
804 if (chat_session) {
805 const sipe_xml *uib;
807 for (uib = sipe_xml_child(chanib, "uib");
808 uib;
809 uib = sipe_xml_twin(uib)) {
810 const gchar *uri = sipe_xml_attribute(uib, "uri");
812 if (uri) {
813 SIPE_DEBUG_INFO("remove_user: %s from room %s (%s)",
814 uri,
815 chat_session->title,
816 chat_session->id);
817 sipe_backend_chat_remove(chat_session->backend,
818 uri);
826 static const struct response {
827 const gchar *key;
828 void (* const handler)(struct sipe_core_private *,
829 struct sip_session *,
830 guint result, const gchar *,
831 const sipe_xml *xml);
832 } response_table[] = {
833 { "rpl:requri", chatserver_response_uri },
834 { "rpl:chansrch", chatserver_response_channel_search },
835 { "rpl:join", chatserver_response_join },
836 { "rpl:bjoin", chatserver_response_join },
837 { "rpl:part", chatserver_response_part },
838 { "ntc:join", chatserver_notice_join },
839 { "ntc:bjoin", chatserver_notice_join },
840 { "ntc:part", chatserver_notice_part },
841 { NULL, NULL }
844 /* Handles rpl:XXX & ntc:YYY */
845 static void chatserver_response(struct sipe_core_private *sipe_private,
846 const sipe_xml *reply,
847 struct sip_session *session)
849 do {
850 const sipe_xml *resp, *data;
851 const gchar *id;
852 gchar *message;
853 guint result = 500;
854 const struct response *r;
856 id = sipe_xml_attribute(reply, "id");
857 if (!id) {
858 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: no reply ID found!");
859 continue;
862 resp = sipe_xml_child(reply, "resp");
863 if (resp) {
864 result = sipe_xml_int_attribute(resp, "code", 500);
865 message = sipe_xml_data(resp);
866 } else {
867 message = g_strdup("");
870 data = sipe_xml_child(reply, "data");
872 SIPE_DEBUG_INFO("chatserver_response: '%s' result (%d) %s",
873 id, result, message ? message : "");
875 for (r = response_table; r->key; r++) {
876 if (sipe_strcase_equal(id, r->key)) {
877 (*r->handler)(sipe_private, session, result, message, data);
878 break;
881 if (!r->key) {
882 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: ignoring unknown response");
885 g_free(message);
886 } while ((reply = sipe_xml_twin(reply)) != NULL);
889 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
890 const sipe_xml *chatgrp)
892 struct sipe_groupchat *groupchat = sipe_private->groupchat;
893 const gchar *uri = sipe_xml_attribute(chatgrp, "chanUri");
894 const gchar *from = sipe_xml_attribute(chatgrp, "author");
895 gchar *text = sipe_xml_data(sipe_xml_child(chatgrp, "chat"));
896 struct sipe_chat_session *chat_session;
897 gchar *escaped;
899 if (!uri || !from) {
900 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' received without chat room URI or author!",
901 text ? text : "");
902 g_free(text);
903 return;
906 chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
907 uri);
908 if (!chat_session) {
909 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' from '%s' received from unknown chat room '%s'!",
910 text ? text : "", from, uri);
911 g_free(text);
912 return;
915 /* libxml2 decodes all entities, but the backend expects HTML */
916 escaped = g_markup_escape_text(text, -1);
917 g_free(text);
918 sipe_backend_chat_message(SIPE_CORE_PUBLIC, chat_session->backend,
919 from, escaped);
920 g_free(escaped);
923 void process_incoming_info_groupchat(struct sipe_core_private *sipe_private,
924 struct sipmsg *msg,
925 struct sip_session *session)
927 sipe_xml *xml = sipe_xml_parse(msg->body, msg->bodylen);
928 const sipe_xml *node;
930 /* @TODO: is this always correct?*/
931 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
933 if (!xml) return;
935 if (((node = sipe_xml_child(xml, "rpl")) != NULL) ||
936 ((node = sipe_xml_child(xml, "ntc")) != NULL)) {
937 chatserver_response(sipe_private, node, session);
938 } else if ((node = sipe_xml_child(xml, "grpchat")) != NULL) {
939 chatserver_grpchat_message(sipe_private, node);
940 } else {
941 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_info_groupchat: ignoring unknown response");
944 sipe_xml_free(xml);
947 void sipe_groupchat_send(struct sipe_core_private *sipe_private,
948 struct sipe_chat_session *chat_session,
949 const gchar *what)
951 struct sipe_groupchat *groupchat = sipe_private->groupchat;
952 gchar *cmd, *self, *timestamp, *tmp;
953 struct sipe_groupchat_msg *msg;
955 if (!groupchat || !chat_session)
956 return;
958 SIPE_DEBUG_INFO("sipe_groupchat_send: '%s' to %s",
959 what, chat_session->id);
961 self = sip_uri_self(sipe_private);
962 timestamp = sipe_utils_time_to_str(time(NULL));
965 * 'what' is already XML-escaped, e.g.
967 * " -> &quot;
968 * > -> &gt;
969 * < -> &lt;
970 * & -> &amp;
972 * No need to escape them here.
974 * Only exception are line breaks which are encoded as <br>.
975 * Replace them with the correct XML tag <br/>.
977 tmp = replace(what, "<br>", "<br/>");
978 cmd = g_strdup_printf("<grpchat id=\"grpchat\" seqid=\"1\" chanUri=\"%s\" author=\"%s\" ts=\"%s\">"
979 "<chat>%s</chat>"
980 "</grpchat>",
981 chat_session->id, self, timestamp, tmp);
982 g_free(tmp);
983 g_free(timestamp);
984 g_free(self);
985 msg = chatserver_command(sipe_private, cmd);
986 g_free(cmd);
988 msg->session = chat_session;
989 msg->content = g_strdup(what);
992 void sipe_groupchat_leave(struct sipe_core_private *sipe_private,
993 struct sipe_chat_session *chat_session)
995 struct sipe_groupchat *groupchat = sipe_private->groupchat;
996 gchar *cmd;
998 if (!groupchat || !chat_session)
999 return;
1001 SIPE_DEBUG_INFO("sipe_groupchat_leave: %s", chat_session->id);
1003 cmd = g_strdup_printf("<cmd id=\"cmd:part\" seqid=\"1\">"
1004 "<data>"
1005 "<chanib uri=\"%s\"/>"
1006 "</data>"
1007 "</cmd>", chat_session->id);
1008 chatserver_command(sipe_private, cmd);
1009 g_free(cmd);
1012 gboolean sipe_core_groupchat_query_rooms(struct sipe_core_public *sipe_public)
1014 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1015 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1017 if (!groupchat || !groupchat->connected)
1018 return FALSE;
1020 chatserver_command(sipe_private,
1021 "<cmd id=\"cmd:chansrch\" seqid=\"1\">"
1022 "<data>"
1023 "<qib qtype=\"BYNAME\" criteria=\"\" extended=\"false\"/>"
1024 "</data>"
1025 "</cmd>");
1027 return TRUE;
1030 void sipe_core_groupchat_join(struct sipe_core_public *sipe_public,
1031 const gchar *uri)
1033 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1034 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1036 if (!g_str_has_prefix(uri, "ma-chan://"))
1037 return;
1039 if (!groupchat) {
1040 /* This happens when a user has set auto-join on a channel */
1041 sipe_groupchat_allocate(sipe_private);
1042 groupchat = sipe_private->groupchat;
1045 if (groupchat->connected) {
1046 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1047 uri);
1049 /* Already joined? */
1050 if (chat_session) {
1052 /* Yes, update backend session */
1053 SIPE_DEBUG_INFO("sipe_core_groupchat_join: show '%s' (%s)",
1054 chat_session->title,
1055 chat_session->id);
1056 sipe_backend_chat_show(chat_session->backend);
1058 } else {
1059 /* No, send command out directly */
1060 gchar *chanid = generate_chanid_node(uri, 0);
1061 if (chanid) {
1062 gchar *cmd = g_strdup_printf("<cmd id=\"cmd:join\" seqid=\"1\">"
1063 "<data>%s</data>"
1064 "</cmd>",
1065 chanid);
1066 SIPE_DEBUG_INFO("sipe_core_groupchat_join: join %s",
1067 uri);
1068 chatserver_command(sipe_private, cmd);
1069 g_free(cmd);
1070 g_free(chanid);
1073 } else {
1074 /* Add it to the queue but avoid duplicates */
1075 if (!g_slist_find_custom(groupchat->join_queue, uri,
1076 sipe_strcompare)) {
1077 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_groupchat_join: URI queued");
1078 groupchat->join_queue = g_slist_prepend(groupchat->join_queue,
1079 g_strdup(uri));
1084 void sipe_groupchat_rejoin(struct sipe_core_private *sipe_private,
1085 struct sipe_chat_session *chat_session)
1087 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1089 if (!groupchat) {
1090 /* First rejoined channel after reconnect will trigger this */
1091 sipe_groupchat_allocate(sipe_private);
1092 groupchat = sipe_private->groupchat;
1095 /* Remember "old" session, so that we don't recreate it at join */
1096 g_hash_table_insert(groupchat->uri_to_chat_session,
1097 chat_session->id,
1098 chat_session);
1099 sipe_core_groupchat_join(SIPE_CORE_PUBLIC, chat_session->id);
1103 Local Variables:
1104 mode: c
1105 c-file-style: "bsd"
1106 indent-tabs-mode: t
1107 tab-width: 8
1108 End: