update ChangeLog
[siplcs.git] / src / core / sipe-groupchat.c
blob17cea0945545b5109f2a76a41629ce57cb330f57
1 /**
2 * @file sipe-groupchat.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /**
24 * This module implements the OCS2007R2 Group Chat functionality
26 * Documentation references:
28 * Microsoft TechNet: Key Protocols and Windows Services Used by Group Chat
29 * <http://technet.microsoft.com/en-us/library/ee323484%28office.13%29.aspx>
30 * Microsoft TechNet: Group Chat Call Flows
31 * <http://technet.microsoft.com/en-us/library/ee323524%28office.13%29.aspx>
32 * Microsoft Office Communications Server 2007 R2 Technical Reference Guide
33 * <http://go.microsoft.com/fwlink/?LinkID=159649>
34 * Microsoft DevNet: [MS-XCCOSIP] Extensible Chat Control Over SIP
35 * <http://msdn.microsoft.com/en-us/library/hh624112.aspx>
36 * RFC 4028: Session Timers in the Session Initiation Protocol (SIP)
37 * <http://www.rfc-editor.org/rfc/rfc4028.txt>
40 * @TODO:
42 * -.cmd:getserverinfo
43 * <sib domain="<DOMAIN>" infoType="123" />
44 * rpl:getservinfo
45 * <sib infoType="123"
46 * serverTime="2010-09-14T14:26:17.6206356Z"
47 * searchLimit="999"
48 * messageSizeLimit="512"
49 * storySizeLimit="4096"
50 * rootUri="ma-cat://<DOMAIN>/<GUID>"
51 * dbVersion="3ea3a5a8-ef36-46cf-898f-7a5133931d63"
52 * />
54 * is there any information in there we would need/use?
56 * - cmd:getpref/rpl:getpref/cmd:setpref/rpl:setpref
57 * probably useless, as libpurple stores configuration locally
59 * can store base64 encoded "free text" in key/value fashion
60 * <cmd id="cmd:getpref" seqid="x">
61 * <data>
62 * <pref label="kedzie.GroupChannels"
63 * seqid="71"
64 * createdefault="true" />
65 * </data>
66 * </cmd>
67 * <cmd id="cmd:setpref" seqid="x">
68 * <data>
69 * <pref label="kedzie.GroupChannels"
70 * seqid="71"
71 * createdefault="false"
72 * content="<BASE64 text>" />
73 * </data>
74 * </cmd>
76 * use this to sync chats in buddy list on multiple clients?
78 * - cmd:getinv
79 * <inv inviteId="1" domain="<DOMAIN>" />
80 * rpl:getinv
81 * ???
83 * according to documentation should provide list of outstanding invites.
84 * [no log file examples]
85 * should we automatically join those channels or ask user to join/add?
87 * - chatserver_command_message()
88 * needs to support multiple <grpchat> nodes?
89 * [no log file examples]
91 * - create/delete chat rooms
92 * [no log file examples]
93 * are these related to this functionality?
95 * <cmd id="cmd:nodespermcreatechild" seqid="1">
96 * <data />
97 * </cmd>
98 * <rpl id="rpl:nodespermcreatechild" seqid="1">
99 * <commandid seqid="1" envid="xxx" />
100 * <resp code="200">SUCCESS_OK</resp>
101 * <data />
102 * </rpl>
104 * - file transfer (uses HTTPS PUT/GET via a filestore server)
105 * [no log file examples]
109 #ifdef HAVE_CONFIG_H
110 #include "config.h"
111 #endif
113 #include <stdlib.h>
114 #include <string.h>
116 #include <glib.h>
118 #include "sipe-common.h"
119 #include "sipmsg.h"
120 #include "sip-transport.h"
121 #include "sipe-backend.h"
122 #include "sipe-chat.h"
123 #include "sipe-core.h"
124 #include "sipe-core-private.h"
125 #include "sipe-dialog.h"
126 #include "sipe-groupchat.h"
127 #include "sipe-im.h"
128 #include "sipe-nls.h"
129 #include "sipe-schedule.h"
130 #include "sipe-session.h"
131 #include "sipe-utils.h"
132 #include "sipe-xml.h"
134 #define GROUPCHAT_RETRY_TIMEOUT 5*60 /* seconds */
137 * aib node - magic numbers?
139 * Example:
140 * <aib key="3984" value="0,1,2,3,4,5,7,9,10,12,13,14,15,16,17" />
141 * <aib key="12276" value="6,8,11" />
143 * "value" corresponds to the "id" attribute in uib nodes.
145 * @TODO: Confirm "guessed" meaning of the magic numbers:
146 * 3984 = normal users
147 * 12276 = channel operators
149 #define GROUPCHAT_AIB_KEY_USER "3984"
150 #define GROUPCHAT_AIB_KEY_CHANOP "12276"
152 struct sipe_groupchat {
153 struct sip_session *session;
154 gchar *domain;
155 GSList *join_queue;
156 GHashTable *uri_to_chat_session;
157 GHashTable *msgs;
158 guint envid;
159 guint expires;
160 gboolean connected;
163 struct sipe_groupchat_msg {
164 GHashTable *container;
165 struct sipe_chat_session *session;
166 gchar *content;
167 gchar *xccos;
168 guint envid;
171 /* GDestroyNotify */
172 static void sipe_groupchat_msg_free(gpointer data) {
173 struct sipe_groupchat_msg *msg = data;
174 g_free(msg->content);
175 g_free(msg->xccos);
176 g_free(msg);
179 /* GDestroyNotify */
180 static void sipe_groupchat_msg_remove(gpointer data) {
181 struct sipe_groupchat_msg *msg = data;
182 g_hash_table_remove(msg->container, &msg->envid);
185 static void sipe_groupchat_allocate(struct sipe_core_private *sipe_private)
187 struct sipe_groupchat *groupchat = g_new0(struct sipe_groupchat, 1);
189 groupchat->uri_to_chat_session = g_hash_table_new(g_str_hash, g_str_equal);
190 groupchat->msgs = g_hash_table_new_full(g_int_hash, g_int_equal,
191 NULL,
192 sipe_groupchat_msg_free);
193 groupchat->envid = rand();
194 groupchat->connected = FALSE;
195 sipe_private->groupchat = groupchat;
198 static void sipe_groupchat_free_join_queue(struct sipe_groupchat *groupchat)
200 sipe_utils_slist_free_full(groupchat->join_queue, g_free);
201 groupchat->join_queue = NULL;
204 void sipe_groupchat_free(struct sipe_core_private *sipe_private)
206 struct sipe_groupchat *groupchat = sipe_private->groupchat;
207 if (groupchat) {
208 sipe_groupchat_free_join_queue(groupchat);
209 g_hash_table_destroy(groupchat->msgs);
210 g_hash_table_destroy(groupchat->uri_to_chat_session);
211 g_free(groupchat->domain);
212 g_free(groupchat);
213 sipe_private->groupchat = NULL;
217 static struct sipe_groupchat_msg *generate_xccos_message(struct sipe_groupchat *groupchat,
218 const gchar *content)
220 struct sipe_groupchat_msg *msg = g_new0(struct sipe_groupchat_msg, 1);
222 msg->container = groupchat->msgs;
223 msg->envid = groupchat->envid++;
224 msg->xccos = g_strdup_printf("<xccos ver=\"1\" envid=\"%u\" xmlns=\"urn:parlano:xml:ns:xccos\">"
225 "%s"
226 "</xccos>",
227 msg->envid,
228 content);
230 g_hash_table_insert(groupchat->msgs, &msg->envid, msg);
232 return(msg);
236 * Create short-lived dialog with ocschat@<domain> (or user specified value)
237 * This initiates the Group Chat feature
239 void sipe_groupchat_init(struct sipe_core_private *sipe_private)
241 const gchar *setting = sipe_backend_setting(SIPE_CORE_PUBLIC,
242 SIPE_SETTING_GROUPCHAT_USER);
243 const gchar *persistent = sipe_private->persistentChatPool_uri;
244 gboolean user_set = !is_empty(setting);
245 gboolean provisioned = !is_empty(persistent);
246 gchar **parts = g_strsplit(user_set ? setting :
247 provisioned ? persistent :
248 sipe_private->username, "@", 2);
249 gboolean domain_found = !is_empty(parts[1]);
250 const gchar *user = "ocschat";
251 const gchar *domain = parts[domain_found ? 1 : 0];
252 gchar *chat_uri;
253 struct sip_session *session;
254 struct sipe_groupchat *groupchat;
256 /* User specified or provisioned URI is valid 'user@company.com' */
257 if ((user_set || provisioned) && domain_found && !is_empty(parts[0]))
258 user = parts[0];
260 SIPE_DEBUG_INFO("sipe_groupchat_init: username '%s' setting '%s' persistent '%s' split '%s'/'%s' GC user %s@%s",
261 sipe_private->username, setting ? setting : "(null)",
262 persistent ? persistent : "(null)",
263 parts[0], parts[1] ? parts[1] : "(null)", user, domain);
265 if (!sipe_private->groupchat)
266 sipe_groupchat_allocate(sipe_private);
267 groupchat = sipe_private->groupchat;
269 chat_uri = g_strdup_printf("sip:%s@%s", user, domain);
270 session = sipe_session_find_or_add_im(sipe_private,
271 chat_uri);
272 session->is_groupchat = TRUE;
273 sipe_im_invite(sipe_private, session, chat_uri,
274 NULL, NULL, NULL, FALSE);
276 g_free(groupchat->domain);
277 groupchat->domain = g_strdup(domain);
279 g_free(chat_uri);
280 g_strfreev(parts);
283 /* sipe_schedule_action */
284 static void groupchat_init_retry_cb(struct sipe_core_private *sipe_private,
285 SIPE_UNUSED_PARAMETER gpointer data)
287 sipe_groupchat_init(sipe_private);
290 static void groupchat_init_retry(struct sipe_core_private *sipe_private)
292 struct sipe_groupchat *groupchat = sipe_private->groupchat;
294 SIPE_DEBUG_INFO_NOFORMAT("groupchat_init_retry: trying again later...");
296 groupchat->session = NULL;
297 groupchat->connected = FALSE;
299 sipe_schedule_seconds(sipe_private,
300 "<+groupchat-retry>",
301 NULL,
302 GROUPCHAT_RETRY_TIMEOUT,
303 groupchat_init_retry_cb,
304 NULL);
307 void sipe_groupchat_invite_failed(struct sipe_core_private *sipe_private,
308 struct sip_session *session)
310 struct sipe_groupchat *groupchat = sipe_private->groupchat;
311 const gchar *setting = sipe_backend_setting(SIPE_CORE_PUBLIC,
312 SIPE_SETTING_GROUPCHAT_USER);
313 gboolean retry = FALSE;
315 if (groupchat->session) {
316 /* response to group chat server invite */
317 SIPE_DEBUG_ERROR_NOFORMAT("can't connect to group chat server!");
319 /* group chat server exists, but communication failed */
320 retry = TRUE;
321 } else {
322 /* response to initial invite */
323 SIPE_DEBUG_INFO_NOFORMAT("no group chat server found.");
326 sipe_session_close(sipe_private, session);
328 if (!is_empty(setting)) {
329 gchar *msg = g_strdup_printf(_("Group Chat Proxy setting is incorrect:\n\n\t%s\n\nPlease update your Account."),
330 setting);
331 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
332 _("Couldn't find Group Chat server!"),
333 msg);
334 g_free(msg);
336 /* user specified group chat settings: we should retry */
337 retry = TRUE;
340 if (retry) {
341 groupchat_init_retry(sipe_private);
342 } else {
343 SIPE_DEBUG_INFO_NOFORMAT("disabling group chat feature.");
347 static gchar *generate_chanid_node(const gchar *uri, guint key)
349 /* ma-chan://<domain>/<value> */
350 gchar **parts = g_strsplit(uri, "/", 4);
351 gchar *chanid = NULL;
353 if (parts[2] && parts[3]) {
354 chanid = g_strdup_printf("<chanid key=\"%d\" domain=\"%s\" value=\"%s\"/>",
355 key, parts[2], parts[3]);
356 } else {
357 SIPE_DEBUG_ERROR("generate_chanid_node: mal-formed URI '%s'",
358 uri);
360 g_strfreev(parts);
362 return chanid;
365 /* sipe_schedule_action */
366 static void groupchat_update_cb(struct sipe_core_private *sipe_private,
367 SIPE_UNUSED_PARAMETER gpointer data)
369 struct sipe_groupchat *groupchat = sipe_private->groupchat;
370 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
371 groupchat->session->with);
373 sip_transport_update(sipe_private, dialog);
374 sipe_schedule_seconds(sipe_private,
375 "<+groupchat-expires>",
376 NULL,
377 groupchat->expires,
378 groupchat_update_cb,
379 NULL);
382 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
383 const gchar *cmd);
385 void sipe_groupchat_invite_response(struct sipe_core_private *sipe_private,
386 struct sip_dialog *dialog,
387 struct sipmsg *response)
389 struct sipe_groupchat *groupchat = sipe_private->groupchat;
391 SIPE_DEBUG_INFO_NOFORMAT("sipe_groupchat_invite_response");
393 if (!groupchat->session) {
394 /* response to initial invite */
395 struct sipe_groupchat_msg *msg = generate_xccos_message(groupchat,
396 "<cmd id=\"cmd:requri\" seqid=\"1\"><data/></cmd>");
397 const gchar *session_expires = sipmsg_find_header(response,
398 "Session-Expires");
400 sip_transport_info(sipe_private,
401 "Content-Type: text/plain\r\n",
402 msg->xccos,
403 dialog,
404 NULL);
405 sipe_groupchat_msg_remove(msg);
407 if (session_expires) {
408 groupchat->expires = strtoul(session_expires, NULL, 10);
410 if (groupchat->expires) {
411 SIPE_DEBUG_INFO("sipe_groupchat_invite_response: session expires in %d seconds",
412 groupchat->expires);
414 if (groupchat->expires > 10)
415 groupchat->expires -= 10;
416 sipe_schedule_seconds(sipe_private,
417 "<+groupchat-expires>",
418 NULL,
419 groupchat->expires,
420 groupchat_update_cb,
421 NULL);
425 } else {
426 /* response to group chat server invite */
427 gchar *invcmd;
429 SIPE_DEBUG_INFO_NOFORMAT("connection to group chat server established.");
431 groupchat->connected = TRUE;
433 /* Any queued joins? */
434 if (groupchat->join_queue) {
435 GString *cmd = g_string_new("<cmd id=\"cmd:bjoin\" seqid=\"1\">"
436 "<data>");
437 GSList *entry;
438 guint i = 0;
440 /* We used g_slist_prepend() to create the list */
441 groupchat->join_queue = entry = g_slist_reverse(groupchat->join_queue);
442 while (entry) {
443 gchar *chanid = generate_chanid_node(entry->data, i++);
444 g_string_append(cmd, chanid);
445 g_free(chanid);
446 entry = entry->next;
448 sipe_groupchat_free_join_queue(groupchat);
450 g_string_append(cmd, "</data></cmd>");
451 chatserver_command(sipe_private, cmd->str);
452 g_string_free(cmd, TRUE);
455 /* Request outstanding invites from server */
456 invcmd = g_strdup_printf("<cmd id=\"cmd:getinv\" seqid=\"1\">"
457 "<data>"
458 "<inv inviteId=\"1\" domain=\"%s\"/>"
459 "</data>"
460 "</cmd>", groupchat->domain);
461 chatserver_command(sipe_private, invcmd);
462 g_free(invcmd);
466 /* TransCallback */
467 static gboolean chatserver_command_response(struct sipe_core_private *sipe_private,
468 struct sipmsg *msg,
469 struct transaction *trans)
471 if (msg->response != 200) {
472 struct sipe_groupchat_msg *gmsg = trans->payload->data;
473 struct sipe_chat_session *chat_session = gmsg->session;
475 SIPE_DEBUG_INFO("chatserver_command_response: failure %d", msg->response);
477 if (chat_session) {
478 gchar *label = g_strdup_printf(_("This message was not delivered to chat room '%s'"),
479 chat_session->title);
480 gchar *errmsg = g_strdup_printf("%s:\n<font color=\"#888888\"></b>%s<b></font>",
481 label, gmsg->content);
482 g_free(label);
483 sipe_backend_notify_message_error(SIPE_CORE_PUBLIC,
484 chat_session->backend,
485 NULL,
486 errmsg);
487 g_free(errmsg);
490 return TRUE;
493 static struct sipe_groupchat_msg *chatserver_command(struct sipe_core_private *sipe_private,
494 const gchar *cmd)
496 struct sipe_groupchat *groupchat = sipe_private->groupchat;
497 struct sipe_groupchat_msg *msg = generate_xccos_message(groupchat, cmd);
499 struct sip_dialog *dialog = sipe_dialog_find(groupchat->session,
500 groupchat->session->with);
502 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
503 struct transaction *trans = sip_transport_info(sipe_private,
504 "Content-Type: text/plain\r\n",
505 msg->xccos,
506 dialog,
507 chatserver_command_response);
509 payload->destroy = sipe_groupchat_msg_remove;
510 payload->data = msg;
511 trans->payload = payload;
513 return(msg);
516 static void chatserver_response_uri(struct sipe_core_private *sipe_private,
517 struct sip_session *session,
518 SIPE_UNUSED_PARAMETER guint result,
519 SIPE_UNUSED_PARAMETER const gchar *message,
520 const sipe_xml *xml)
522 const sipe_xml *uib = sipe_xml_child(xml, "uib");
523 const gchar *uri = sipe_xml_attribute(uib, "uri");
525 /* drop connection to ocschat@<domain> again */
526 sipe_session_close(sipe_private, session);
528 if (uri) {
529 struct sipe_groupchat *groupchat = sipe_private->groupchat;
531 SIPE_DEBUG_INFO("chatserver_response_uri: '%s'", uri);
533 groupchat->session = session = sipe_session_find_or_add_im(sipe_private,
534 uri);
536 session->is_groupchat = TRUE;
537 sipe_im_invite(sipe_private, session, uri, NULL, NULL, NULL, FALSE);
538 } else {
539 SIPE_DEBUG_WARNING_NOFORMAT("chatserver_response_uri: no server URI found!");
540 groupchat_init_retry(sipe_private);
544 static void chatserver_response_channel_search(struct sipe_core_private *sipe_private,
545 SIPE_UNUSED_PARAMETER struct sip_session *session,
546 guint result,
547 const gchar *message,
548 const sipe_xml *xml)
550 struct sipe_core_public *sipe_public = SIPE_CORE_PUBLIC;
552 if (result != 200) {
553 sipe_backend_notify_error(sipe_public,
554 _("Error retrieving room list"),
555 message);
556 } else {
557 const sipe_xml *chanib;
559 for (chanib = sipe_xml_child(xml, "chanib");
560 chanib;
561 chanib = sipe_xml_twin(chanib)) {
562 const gchar *name = sipe_xml_attribute(chanib, "name");
563 const gchar *desc = sipe_xml_attribute(chanib, "description");
564 const gchar *uri = sipe_xml_attribute(chanib, "uri");
565 const sipe_xml *node;
566 guint user_count = 0;
567 guint32 flags = 0;
569 /* information */
570 for (node = sipe_xml_child(chanib, "info");
571 node;
572 node = sipe_xml_twin(node)) {
573 const gchar *id = sipe_xml_attribute(node, "id");
574 gchar *data;
576 if (!id) continue;
578 data = sipe_xml_data(node);
579 if (data) {
580 if (sipe_strcase_equal(id, "urn:parlano:ma:info:ucnt")) {
581 user_count = g_ascii_strtoll(data, NULL, 10);
582 } else if (sipe_strcase_equal(id, "urn:parlano:ma:info:visibilty")) {
583 if (sipe_strcase_equal(data, "private")) {
584 flags |= SIPE_GROUPCHAT_ROOM_PRIVATE;
587 g_free(data);
591 /* properties */
592 for (node = sipe_xml_child(chanib, "prop");
593 node;
594 node = sipe_xml_twin(node)) {
595 const gchar *id = sipe_xml_attribute(node, "id");
596 gchar *data;
598 if (!id) continue;
600 data = sipe_xml_data(node);
601 if (data) {
602 gboolean value = sipe_strcase_equal(data, "true");
603 g_free(data);
605 if (value) {
606 guint32 add = 0;
607 if (sipe_strcase_equal(id, "urn:parlano:ma:prop:filepost")) {
608 add = SIPE_GROUPCHAT_ROOM_FILEPOST;
609 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:invite")) {
610 add = SIPE_GROUPCHAT_ROOM_INVITE;
611 } else if (sipe_strcase_equal(id, "urn:parlano:ma:prop:logged")) {
612 add = SIPE_GROUPCHAT_ROOM_LOGGED;
614 flags |= add;
619 SIPE_DEBUG_INFO("group chat channel '%s': '%s' (%s) with %u users, flags 0x%x",
620 name, desc, uri, user_count, flags);
621 sipe_backend_groupchat_room_add(sipe_public,
622 uri, name, desc,
623 user_count, flags);
627 sipe_backend_groupchat_room_terminate(sipe_public);
630 static gboolean is_chanop(const sipe_xml *aib)
632 return sipe_strequal(sipe_xml_attribute(aib, "key"),
633 GROUPCHAT_AIB_KEY_CHANOP);
636 static void add_user(struct sipe_chat_session *chat_session,
637 const gchar *uri,
638 gboolean new, gboolean chanop)
640 SIPE_DEBUG_INFO("add_user: %s%s%s to room %s (%s)",
641 new ? "new " : "",
642 chanop ? "chanop " : "",
643 uri,
644 chat_session->title, chat_session->id);
645 sipe_backend_chat_add(chat_session->backend, uri, new);
646 if (chanop)
647 sipe_backend_chat_operator(chat_session->backend, uri);
650 static void chatserver_response_join(struct sipe_core_private *sipe_private,
651 SIPE_UNUSED_PARAMETER struct sip_session *session,
652 guint result,
653 const gchar *message,
654 const sipe_xml *xml)
656 if (result != 200) {
657 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
658 _("Error joining chat room"),
659 message);
660 } else {
661 struct sipe_groupchat *groupchat = sipe_private->groupchat;
662 const sipe_xml *node;
663 GHashTable *user_ids = g_hash_table_new(g_str_hash, g_str_equal);
665 /* Extract user IDs & URIs and generate ID -> URI map */
666 for (node = sipe_xml_child(xml, "uib");
667 node;
668 node = sipe_xml_twin(node)) {
669 const gchar *id = sipe_xml_attribute(node, "id");
670 const gchar *uri = sipe_xml_attribute(node, "uri");
671 if (id && uri)
672 g_hash_table_insert(user_ids,
673 (gpointer) id,
674 (gpointer) uri);
677 /* Process channel data */
678 for (node = sipe_xml_child(xml, "chanib");
679 node;
680 node = sipe_xml_twin(node)) {
681 const gchar *uri = sipe_xml_attribute(node, "uri");
683 if (uri) {
684 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
685 uri);
686 gboolean new = (chat_session == NULL);
687 const gchar *attr = sipe_xml_attribute(node, "name");
688 gchar *self = sip_uri_self(sipe_private);
689 const sipe_xml *aib;
691 if (new) {
692 chat_session = sipe_chat_create_session(SIPE_CHAT_TYPE_GROUPCHAT,
693 sipe_xml_attribute(node,
694 "uri"),
695 attr ? attr : "");
696 g_hash_table_insert(groupchat->uri_to_chat_session,
697 chat_session->id,
698 chat_session);
700 SIPE_DEBUG_INFO("joined room '%s' (%s)",
701 chat_session->title,
702 chat_session->id);
703 chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
704 chat_session,
705 chat_session->title,
706 self);
707 } else {
708 SIPE_DEBUG_INFO("rejoining room '%s' (%s)",
709 chat_session->title,
710 chat_session->id);
711 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC,
712 chat_session->backend,
713 self,
714 chat_session->title);
716 g_free(self);
718 attr = sipe_xml_attribute(node, "topic");
719 if (attr) {
720 sipe_backend_chat_topic(chat_session->backend,
721 attr);
724 /* Process user map for channel */
725 for (aib = sipe_xml_child(node, "aib");
726 aib;
727 aib = sipe_xml_twin(aib)) {
728 const gchar *value = sipe_xml_attribute(aib, "value");
729 gboolean chanop = is_chanop(aib);
730 gchar **ids = g_strsplit(value, ",", 0);
732 if (ids) {
733 gchar **uid = ids;
735 while (*uid) {
736 const gchar *uri = g_hash_table_lookup(user_ids,
737 *uid);
738 if (uri)
739 add_user(chat_session,
740 uri,
741 FALSE,
742 chanop);
743 uid++;
746 g_strfreev(ids);
750 /* Request last 25 entries from channel history */
751 self = g_strdup_printf("<cmd id=\"cmd:bccontext\" seqid=\"1\">"
752 "<data>"
753 "<chanib uri=\"%s\"/>"
754 "<bcq><last cnt=\"25\"/></bcq>"
755 "</data>"
756 "</cmd>", chat_session->id);
757 chatserver_command(sipe_private, self);
758 g_free(self);
762 g_hash_table_destroy(user_ids);
766 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
767 const sipe_xml *grpchat);
769 static void chatserver_response_history(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
770 SIPE_UNUSED_PARAMETER struct sip_session *session,
771 SIPE_UNUSED_PARAMETER guint result,
772 SIPE_UNUSED_PARAMETER const gchar *message,
773 const sipe_xml *xml)
775 const sipe_xml *grpchat;
777 for (grpchat = sipe_xml_child(xml, "chanib/msg");
778 grpchat;
779 grpchat = sipe_xml_twin(grpchat))
780 if (sipe_strequal(sipe_xml_attribute(grpchat, "id"),
781 "grpchat"))
782 chatserver_grpchat_message(sipe_private, grpchat);
785 static void chatserver_response_part(struct sipe_core_private *sipe_private,
786 SIPE_UNUSED_PARAMETER struct sip_session *session,
787 guint result,
788 const gchar *message,
789 const sipe_xml *xml)
791 if (result != 200) {
792 SIPE_DEBUG_WARNING("chatserver_response_part: failed with %d: %s. Dropping room",
793 result, message);
794 } else {
795 struct sipe_groupchat *groupchat = sipe_private->groupchat;
796 const gchar *uri = sipe_xml_attribute(sipe_xml_child(xml, "chanib"),
797 "uri");
798 struct sipe_chat_session *chat_session;
800 if (uri &&
801 (chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
802 uri))) {
804 SIPE_DEBUG_INFO("leaving room '%s' (%s)",
805 chat_session->title, chat_session->id);
807 g_hash_table_remove(groupchat->uri_to_chat_session,
808 uri);
809 sipe_chat_remove_session(chat_session);
811 } else {
812 SIPE_DEBUG_WARNING("chatserver_response_part: unknown chat room uri '%s'",
813 uri ? uri : "");
818 static void chatserver_notice_join(struct sipe_core_private *sipe_private,
819 SIPE_UNUSED_PARAMETER struct sip_session *session,
820 SIPE_UNUSED_PARAMETER guint result,
821 SIPE_UNUSED_PARAMETER const gchar *message,
822 const sipe_xml *xml)
824 struct sipe_groupchat *groupchat = sipe_private->groupchat;
825 const sipe_xml *uib;
827 for (uib = sipe_xml_child(xml, "uib");
828 uib;
829 uib = sipe_xml_twin(uib)) {
830 const gchar *uri = sipe_xml_attribute(uib, "uri");
832 if (uri) {
833 const sipe_xml *aib;
835 for (aib = sipe_xml_child(uib, "aib");
836 aib;
837 aib = sipe_xml_twin(aib)) {
838 const gchar *domain = sipe_xml_attribute(aib, "domain");
839 const gchar *path = sipe_xml_attribute(aib, "value");
841 if (domain && path) {
842 gchar *room_uri = g_strdup_printf("ma-chan://%s/%s",
843 domain, path);
844 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
845 room_uri);
846 if (chat_session)
847 add_user(chat_session,
848 uri,
849 TRUE,
850 is_chanop(aib));
852 g_free(room_uri);
859 static void chatserver_notice_part(struct sipe_core_private *sipe_private,
860 SIPE_UNUSED_PARAMETER struct sip_session *session,
861 SIPE_UNUSED_PARAMETER guint result,
862 SIPE_UNUSED_PARAMETER const gchar *message,
863 const sipe_xml *xml)
865 struct sipe_groupchat *groupchat = sipe_private->groupchat;
866 const sipe_xml *chanib;
868 for (chanib = sipe_xml_child(xml, "chanib");
869 chanib;
870 chanib = sipe_xml_twin(chanib)) {
871 const gchar *room_uri = sipe_xml_attribute(chanib, "uri");
873 if (room_uri) {
874 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
875 room_uri);
877 if (chat_session) {
878 const sipe_xml *uib;
880 for (uib = sipe_xml_child(chanib, "uib");
881 uib;
882 uib = sipe_xml_twin(uib)) {
883 const gchar *uri = sipe_xml_attribute(uib, "uri");
885 if (uri) {
886 SIPE_DEBUG_INFO("remove_user: %s from room %s (%s)",
887 uri,
888 chat_session->title,
889 chat_session->id);
890 sipe_backend_chat_remove(chat_session->backend,
891 uri);
899 static const struct response {
900 const gchar *key;
901 void (* const handler)(struct sipe_core_private *,
902 struct sip_session *,
903 guint result, const gchar *,
904 const sipe_xml *xml);
905 } response_table[] = {
906 { "rpl:requri", chatserver_response_uri },
907 { "rpl:chansrch", chatserver_response_channel_search },
908 { "rpl:join", chatserver_response_join },
909 { "rpl:bjoin", chatserver_response_join },
910 { "rpl:bccontext", chatserver_response_history },
911 { "rpl:part", chatserver_response_part },
912 { "ntc:join", chatserver_notice_join },
913 { "ntc:bjoin", chatserver_notice_join },
914 { "ntc:part", chatserver_notice_part },
915 { NULL, NULL }
918 /* Handles rpl:XXX & ntc:YYY */
919 static void chatserver_response(struct sipe_core_private *sipe_private,
920 const sipe_xml *reply,
921 struct sip_session *session)
923 do {
924 const sipe_xml *resp, *data;
925 const gchar *id;
926 gchar *message;
927 guint result = 500;
928 const struct response *r;
930 id = sipe_xml_attribute(reply, "id");
931 if (!id) {
932 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: no reply ID found!");
933 continue;
936 resp = sipe_xml_child(reply, "resp");
937 if (resp) {
938 result = sipe_xml_int_attribute(resp, "code", 500);
939 message = sipe_xml_data(resp);
940 } else {
941 message = g_strdup("");
944 data = sipe_xml_child(reply, "data");
946 SIPE_DEBUG_INFO("chatserver_response: '%s' result (%d) %s",
947 id, result, message ? message : "");
949 for (r = response_table; r->key; r++) {
950 if (sipe_strcase_equal(id, r->key)) {
951 (*r->handler)(sipe_private, session, result, message, data);
952 break;
955 if (!r->key) {
956 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: ignoring unknown response");
959 g_free(message);
960 } while ((reply = sipe_xml_twin(reply)) != NULL);
963 static void chatserver_grpchat_message(struct sipe_core_private *sipe_private,
964 const sipe_xml *grpchat)
966 struct sipe_groupchat *groupchat = sipe_private->groupchat;
967 const gchar *uri = sipe_xml_attribute(grpchat, "chanUri");
968 const gchar *from = sipe_xml_attribute(grpchat, "author");
969 time_t when = sipe_utils_str_to_time(sipe_xml_attribute(grpchat, "ts"));
970 gchar *text = sipe_xml_data(sipe_xml_child(grpchat, "chat"));
971 struct sipe_chat_session *chat_session;
972 gchar *escaped;
974 if (!uri || !from) {
975 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' received without chat room URI or author!",
976 text ? text : "");
977 g_free(text);
978 return;
981 chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
982 uri);
983 if (!chat_session) {
984 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' from '%s' received from unknown chat room '%s'!",
985 text ? text : "", from, uri);
986 g_free(text);
987 return;
990 /* libxml2 decodes all entities, but the backend expects HTML */
991 escaped = g_markup_escape_text(text, -1);
992 g_free(text);
993 sipe_backend_chat_message(SIPE_CORE_PUBLIC, chat_session->backend,
994 from, when, escaped);
995 g_free(escaped);
998 void process_incoming_info_groupchat(struct sipe_core_private *sipe_private,
999 struct sipmsg *msg,
1000 struct sip_session *session)
1002 sipe_xml *xml = sipe_xml_parse(msg->body, msg->bodylen);
1003 const sipe_xml *node;
1004 const gchar *callid;
1005 struct sip_dialog *dialog;
1007 callid = sipmsg_find_header(msg, "Call-ID");
1008 dialog = sipe_dialog_find(session, session->with);
1009 if (sipe_strequal(callid, dialog->callid)) {
1011 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1013 if (((node = sipe_xml_child(xml, "rpl")) != NULL) ||
1014 ((node = sipe_xml_child(xml, "ntc")) != NULL)) {
1015 chatserver_response(sipe_private, node, session);
1016 } else if ((node = sipe_xml_child(xml, "grpchat")) != NULL) {
1017 chatserver_grpchat_message(sipe_private, node);
1018 } else {
1019 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_info_groupchat: ignoring unknown response");
1022 } else {
1024 * Our last session got disconnected without proper shutdown,
1025 * e.g. by Pidgin crashing or network connection loss. When
1026 * we reconnect to the group chat the server will send INFO
1027 * messages to the current *AND* the obsolete Call-ID, until
1028 * the obsolete session expires.
1030 * Ignore these INFO messages to avoid, e.g. duplicate texts,
1031 * and respond with an error so that the server knows that we
1032 * consider this dialog to be terminated.
1034 SIPE_DEBUG_INFO("process_incoming_info_groupchat: ignoring unsolicited INFO message to obsolete Call-ID: %s",
1035 callid);
1037 sip_transport_response(sipe_private, msg, 487, "Request Terminated", NULL);
1040 sipe_xml_free(xml);
1043 void sipe_groupchat_send(struct sipe_core_private *sipe_private,
1044 struct sipe_chat_session *chat_session,
1045 const gchar *what)
1047 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1048 gchar *cmd, *self, *timestamp, *tmp;
1049 gchar **lines, **strvp;
1050 struct sipe_groupchat_msg *msg;
1052 if (!groupchat || !chat_session)
1053 return;
1055 SIPE_DEBUG_INFO("sipe_groupchat_send: '%s' to %s",
1056 what, chat_session->id);
1058 self = sip_uri_self(sipe_private);
1059 timestamp = sipe_utils_time_to_str(time(NULL));
1062 * 'what' is already XML-escaped, e.g.
1064 * " -> &quot;
1065 * > -> &gt;
1066 * < -> &lt;
1067 * & -> &amp;
1069 * Group Chat only accepts plain text, not full HTML. So we have to
1070 * strip all HTML tags and XML escape the text.
1072 * Line breaks are encoded as <br> and therefore need to be replaced
1073 * before stripping. In order to prevent HTML stripping to strip line
1074 * endings, we need to split the text into lines on <br>.
1076 lines = g_strsplit(what, "<br>", 0);
1077 for (strvp = lines; *strvp; strvp++) {
1078 /* replace array entry with HTML stripped & XML escaped version */
1079 gchar *stripped = sipe_backend_markup_strip_html(*strvp);
1080 gchar *escaped = g_markup_escape_text(stripped, -1);
1081 g_free(stripped);
1082 g_free(*strvp);
1083 *strvp = escaped;
1085 tmp = g_strjoinv("\r\n", lines);
1086 g_strfreev(lines);
1087 cmd = g_strdup_printf("<grpchat id=\"grpchat\" seqid=\"1\" chanUri=\"%s\" author=\"%s\" ts=\"%s\">"
1088 "<chat>%s</chat>"
1089 "</grpchat>",
1090 chat_session->id, self, timestamp, tmp);
1091 g_free(tmp);
1092 g_free(timestamp);
1093 g_free(self);
1094 msg = chatserver_command(sipe_private, cmd);
1095 g_free(cmd);
1097 msg->session = chat_session;
1098 msg->content = g_strdup(what);
1101 void sipe_groupchat_leave(struct sipe_core_private *sipe_private,
1102 struct sipe_chat_session *chat_session)
1104 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1105 gchar *cmd;
1107 if (!groupchat || !chat_session)
1108 return;
1110 SIPE_DEBUG_INFO("sipe_groupchat_leave: %s", chat_session->id);
1112 cmd = g_strdup_printf("<cmd id=\"cmd:part\" seqid=\"1\">"
1113 "<data>"
1114 "<chanib uri=\"%s\"/>"
1115 "</data>"
1116 "</cmd>", chat_session->id);
1117 chatserver_command(sipe_private, cmd);
1118 g_free(cmd);
1121 gboolean sipe_core_groupchat_query_rooms(struct sipe_core_public *sipe_public)
1123 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1124 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1126 if (!groupchat || !groupchat->connected)
1127 return FALSE;
1129 chatserver_command(sipe_private,
1130 "<cmd id=\"cmd:chansrch\" seqid=\"1\">"
1131 "<data>"
1132 "<qib qtype=\"BYNAME\" criteria=\"\" extended=\"false\"/>"
1133 "</data>"
1134 "</cmd>");
1136 return TRUE;
1139 void sipe_core_groupchat_join(struct sipe_core_public *sipe_public,
1140 const gchar *uri)
1142 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1143 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1145 if (!g_str_has_prefix(uri, "ma-chan://"))
1146 return;
1148 if (!groupchat) {
1149 /* This happens when a user has set auto-join on a channel */
1150 sipe_groupchat_allocate(sipe_private);
1151 groupchat = sipe_private->groupchat;
1154 if (groupchat->connected) {
1155 struct sipe_chat_session *chat_session = g_hash_table_lookup(groupchat->uri_to_chat_session,
1156 uri);
1158 /* Already joined? */
1159 if (chat_session) {
1161 /* Yes, update backend session */
1162 SIPE_DEBUG_INFO("sipe_core_groupchat_join: show '%s' (%s)",
1163 chat_session->title,
1164 chat_session->id);
1165 sipe_backend_chat_show(chat_session->backend);
1167 } else {
1168 /* No, send command out directly */
1169 gchar *chanid = generate_chanid_node(uri, 0);
1170 if (chanid) {
1171 gchar *cmd = g_strdup_printf("<cmd id=\"cmd:join\" seqid=\"1\">"
1172 "<data>%s</data>"
1173 "</cmd>",
1174 chanid);
1175 SIPE_DEBUG_INFO("sipe_core_groupchat_join: join %s",
1176 uri);
1177 chatserver_command(sipe_private, cmd);
1178 g_free(cmd);
1179 g_free(chanid);
1182 } else {
1183 /* Add it to the queue but avoid duplicates */
1184 if (!g_slist_find_custom(groupchat->join_queue, uri,
1185 sipe_strcompare)) {
1186 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_groupchat_join: URI queued");
1187 groupchat->join_queue = g_slist_prepend(groupchat->join_queue,
1188 g_strdup(uri));
1193 void sipe_groupchat_rejoin(struct sipe_core_private *sipe_private,
1194 struct sipe_chat_session *chat_session)
1196 struct sipe_groupchat *groupchat = sipe_private->groupchat;
1198 if (!groupchat) {
1199 /* First rejoined channel after reconnect will trigger this */
1200 sipe_groupchat_allocate(sipe_private);
1201 groupchat = sipe_private->groupchat;
1204 /* Remember "old" session, so that we don't recreate it at join */
1205 g_hash_table_insert(groupchat->uri_to_chat_session,
1206 chat_session->id,
1207 chat_session);
1208 sipe_core_groupchat_join(SIPE_CORE_PUBLIC, chat_session->id);
1212 Local Variables:
1213 mode: c
1214 c-file-style: "bsd"
1215 indent-tabs-mode: t
1216 tab-width: 8
1217 End: