2 * @file sipe-groupchat.c
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
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>
43 * <sib domain="<DOMAIN>" infoType="123" />
46 * serverTime="2010-09-14T14:26:17.6206356Z"
48 * messageSizeLimit="512"
49 * storySizeLimit="4096"
50 * rootUri="ma-cat://<DOMAIN>/<GUID>"
51 * dbVersion="3ea3a5a8-ef36-46cf-898f-7a5133931d63"
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">
62 * <pref label="kedzie.GroupChannels"
64 * createdefault="true" />
67 * <cmd id="cmd:setpref" seqid="x">
69 * <pref label="kedzie.GroupChannels"
71 * createdefault="false"
72 * content="<BASE64 text>" />
76 * use this to sync chats in buddy list on multiple clients?
79 * <inv inviteId="1" domain="<DOMAIN>" />
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">
98 * <rpl id="rpl:nodespermcreatechild" seqid="1">
99 * <commandid seqid="1" envid="xxx" />
100 * <resp code="200">SUCCESS_OK</resp>
104 * - file transfer (uses HTTPS PUT/GET via a filestore server)
105 * [no log file examples]
118 #include "sipe-common.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"
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?
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
;
156 GHashTable
*uri_to_chat_session
;
163 struct sipe_groupchat_msg
{
164 GHashTable
*container
;
165 struct sipe_chat_session
*session
;
172 static void sipe_groupchat_msg_free(gpointer data
) {
173 struct sipe_groupchat_msg
*msg
= data
;
174 g_free(msg
->content
);
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
,
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
;
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
);
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\">"
230 g_hash_table_insert(groupchat
->msgs
, &msg
->envid
, 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];
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]))
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
,
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
);
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>",
302 GROUPCHAT_RETRY_TIMEOUT
,
303 groupchat_init_retry_cb
,
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 */
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."),
331 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
332 _("Couldn't find Group Chat server!"),
336 /* user specified group chat settings: we should retry */
341 groupchat_init_retry(sipe_private
);
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]);
357 SIPE_DEBUG_ERROR("generate_chanid_node: mal-formed URI '%s'",
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
);
374 sip_transport_update(sipe_private
, dialog
);
375 sipe_schedule_seconds(sipe_private
,
376 "<+groupchat-expires>",
383 static struct sipe_groupchat_msg
*chatserver_command(struct sipe_core_private
*sipe_private
,
386 void sipe_groupchat_invite_response(struct sipe_core_private
*sipe_private
,
387 struct sip_dialog
*dialog
,
388 struct sipmsg
*response
)
390 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
392 SIPE_DEBUG_INFO_NOFORMAT("sipe_groupchat_invite_response");
394 if (!groupchat
->session
) {
395 /* response to initial invite */
396 struct sipe_groupchat_msg
*msg
= generate_xccos_message(groupchat
,
397 "<cmd id=\"cmd:requri\" seqid=\"1\"><data/></cmd>");
398 const gchar
*session_expires
= sipmsg_find_header(response
,
401 sip_transport_info(sipe_private
,
402 "Content-Type: text/plain\r\n",
406 sipe_groupchat_msg_remove(msg
);
408 if (session_expires
) {
409 groupchat
->expires
= strtoul(session_expires
, NULL
, 10);
411 if (groupchat
->expires
) {
412 SIPE_DEBUG_INFO("sipe_groupchat_invite_response: session expires in %d seconds",
415 if (groupchat
->expires
> 10)
416 groupchat
->expires
-= 10;
417 sipe_schedule_seconds(sipe_private
,
418 "<+groupchat-expires>",
427 /* response to group chat server invite */
430 SIPE_DEBUG_INFO_NOFORMAT("connection to group chat server established.");
432 groupchat
->connected
= TRUE
;
434 /* Any queued joins? */
435 if (groupchat
->join_queue
) {
436 GString
*cmd
= g_string_new("<cmd id=\"cmd:bjoin\" seqid=\"1\">"
441 /* We used g_slist_prepend() to create the list */
442 groupchat
->join_queue
= entry
= g_slist_reverse(groupchat
->join_queue
);
444 gchar
*chanid
= generate_chanid_node(entry
->data
, i
++);
445 g_string_append(cmd
, chanid
);
449 sipe_groupchat_free_join_queue(groupchat
);
451 g_string_append(cmd
, "</data></cmd>");
452 chatserver_command(sipe_private
, cmd
->str
);
453 g_string_free(cmd
, TRUE
);
456 /* Request outstanding invites from server */
457 invcmd
= g_strdup_printf("<cmd id=\"cmd:getinv\" seqid=\"1\">"
459 "<inv inviteId=\"1\" domain=\"%s\"/>"
461 "</cmd>", groupchat
->domain
);
462 chatserver_command(sipe_private
, invcmd
);
468 static gboolean
chatserver_command_response(struct sipe_core_private
*sipe_private
,
470 struct transaction
*trans
)
472 if (msg
->response
!= 200) {
473 struct sipe_groupchat_msg
*gmsg
= trans
->payload
->data
;
474 struct sipe_chat_session
*chat_session
= gmsg
->session
;
476 SIPE_DEBUG_INFO("chatserver_command_response: failure %d", msg
->response
);
479 gchar
*label
= g_strdup_printf(_("This message was not delivered to chat room '%s'"),
480 chat_session
->title
);
481 gchar
*errmsg
= g_strdup_printf("%s:\n<font color=\"#888888\"></b>%s<b></font>",
482 label
, gmsg
->content
);
484 sipe_backend_notify_message_error(SIPE_CORE_PUBLIC
,
485 chat_session
->backend
,
494 static struct sipe_groupchat_msg
*chatserver_command(struct sipe_core_private
*sipe_private
,
497 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
498 struct sip_dialog
*dialog
= sipe_dialog_find(groupchat
->session
,
499 groupchat
->session
->with
);
500 struct sipe_groupchat_msg
*msg
= NULL
;
503 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
504 struct transaction
*trans
;
506 msg
= generate_xccos_message(groupchat
, cmd
);
507 trans
= sip_transport_info(sipe_private
,
508 "Content-Type: text/plain\r\n",
511 chatserver_command_response
);
513 payload
->destroy
= sipe_groupchat_msg_remove
;
515 trans
->payload
= payload
;
521 static void chatserver_response_uri(struct sipe_core_private
*sipe_private
,
522 struct sip_session
*session
,
523 SIPE_UNUSED_PARAMETER guint result
,
524 SIPE_UNUSED_PARAMETER
const gchar
*message
,
527 const sipe_xml
*uib
= sipe_xml_child(xml
, "uib");
528 const gchar
*uri
= sipe_xml_attribute(uib
, "uri");
530 /* drop connection to ocschat@<domain> again */
531 sipe_session_close(sipe_private
, session
);
534 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
536 SIPE_DEBUG_INFO("chatserver_response_uri: '%s'", uri
);
538 groupchat
->session
= session
= sipe_session_find_or_add_im(sipe_private
,
541 session
->is_groupchat
= TRUE
;
542 sipe_im_invite(sipe_private
, session
, uri
, NULL
, NULL
, NULL
, FALSE
);
544 SIPE_DEBUG_WARNING_NOFORMAT("chatserver_response_uri: no server URI found!");
545 groupchat_init_retry(sipe_private
);
549 static void chatserver_response_channel_search(struct sipe_core_private
*sipe_private
,
550 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
552 const gchar
*message
,
555 struct sipe_core_public
*sipe_public
= SIPE_CORE_PUBLIC
;
558 sipe_backend_notify_error(sipe_public
,
559 _("Error retrieving room list"),
562 const sipe_xml
*chanib
;
564 for (chanib
= sipe_xml_child(xml
, "chanib");
566 chanib
= sipe_xml_twin(chanib
)) {
567 const gchar
*name
= sipe_xml_attribute(chanib
, "name");
568 const gchar
*desc
= sipe_xml_attribute(chanib
, "description");
569 const gchar
*uri
= sipe_xml_attribute(chanib
, "uri");
570 const sipe_xml
*node
;
571 guint user_count
= 0;
575 for (node
= sipe_xml_child(chanib
, "info");
577 node
= sipe_xml_twin(node
)) {
578 const gchar
*id
= sipe_xml_attribute(node
, "id");
583 data
= sipe_xml_data(node
);
585 if (sipe_strcase_equal(id
, "urn:parlano:ma:info:ucnt")) {
586 user_count
= g_ascii_strtoll(data
, NULL
, 10);
587 } else if (sipe_strcase_equal(id
, "urn:parlano:ma:info:visibilty")) {
588 if (sipe_strcase_equal(data
, "private")) {
589 flags
|= SIPE_GROUPCHAT_ROOM_PRIVATE
;
597 for (node
= sipe_xml_child(chanib
, "prop");
599 node
= sipe_xml_twin(node
)) {
600 const gchar
*id
= sipe_xml_attribute(node
, "id");
605 data
= sipe_xml_data(node
);
607 gboolean value
= sipe_strcase_equal(data
, "true");
612 if (sipe_strcase_equal(id
, "urn:parlano:ma:prop:filepost")) {
613 add
= SIPE_GROUPCHAT_ROOM_FILEPOST
;
614 } else if (sipe_strcase_equal(id
, "urn:parlano:ma:prop:invite")) {
615 add
= SIPE_GROUPCHAT_ROOM_INVITE
;
616 } else if (sipe_strcase_equal(id
, "urn:parlano:ma:prop:logged")) {
617 add
= SIPE_GROUPCHAT_ROOM_LOGGED
;
624 SIPE_DEBUG_INFO("group chat channel '%s': '%s' (%s) with %u users, flags 0x%x",
625 name
, desc
, uri
, user_count
, flags
);
626 sipe_backend_groupchat_room_add(sipe_public
,
632 sipe_backend_groupchat_room_terminate(sipe_public
);
635 static gboolean
is_chanop(const sipe_xml
*aib
)
637 return sipe_strequal(sipe_xml_attribute(aib
, "key"),
638 GROUPCHAT_AIB_KEY_CHANOP
);
641 static void add_user(struct sipe_chat_session
*chat_session
,
643 gboolean
new, gboolean chanop
)
645 SIPE_DEBUG_INFO("add_user: %s%s%s to room %s (%s)",
647 chanop
? "chanop " : "",
649 chat_session
->title
, chat_session
->id
);
650 sipe_backend_chat_add(chat_session
->backend
, uri
, new);
652 sipe_backend_chat_operator(chat_session
->backend
, uri
);
655 static void chatserver_response_join(struct sipe_core_private
*sipe_private
,
656 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
658 const gchar
*message
,
662 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
663 _("Error joining chat room"),
666 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
667 const sipe_xml
*node
;
668 GHashTable
*user_ids
= g_hash_table_new(g_str_hash
, g_str_equal
);
670 /* Extract user IDs & URIs and generate ID -> URI map */
671 for (node
= sipe_xml_child(xml
, "uib");
673 node
= sipe_xml_twin(node
)) {
674 const gchar
*id
= sipe_xml_attribute(node
, "id");
675 const gchar
*uri
= sipe_xml_attribute(node
, "uri");
677 g_hash_table_insert(user_ids
,
682 /* Process channel data */
683 for (node
= sipe_xml_child(xml
, "chanib");
685 node
= sipe_xml_twin(node
)) {
686 const gchar
*uri
= sipe_xml_attribute(node
, "uri");
689 struct sipe_chat_session
*chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
691 gboolean
new = (chat_session
== NULL
);
692 const gchar
*attr
= sipe_xml_attribute(node
, "name");
693 gchar
*self
= sip_uri_self(sipe_private
);
697 chat_session
= sipe_chat_create_session(SIPE_CHAT_TYPE_GROUPCHAT
,
698 sipe_xml_attribute(node
,
701 g_hash_table_insert(groupchat
->uri_to_chat_session
,
705 SIPE_DEBUG_INFO("joined room '%s' (%s)",
708 chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
713 SIPE_DEBUG_INFO("rejoining room '%s' (%s)",
716 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC
,
717 chat_session
->backend
,
719 chat_session
->title
);
723 attr
= sipe_xml_attribute(node
, "topic");
725 sipe_backend_chat_topic(chat_session
->backend
,
729 /* Process user map for channel */
730 for (aib
= sipe_xml_child(node
, "aib");
732 aib
= sipe_xml_twin(aib
)) {
733 const gchar
*value
= sipe_xml_attribute(aib
, "value");
734 gboolean chanop
= is_chanop(aib
);
735 gchar
**ids
= g_strsplit(value
, ",", 0);
741 const gchar
*uri
= g_hash_table_lookup(user_ids
,
744 add_user(chat_session
,
755 /* Request last 25 entries from channel history */
756 self
= g_strdup_printf("<cmd id=\"cmd:bccontext\" seqid=\"1\">"
758 "<chanib uri=\"%s\"/>"
759 "<bcq><last cnt=\"25\"/></bcq>"
761 "</cmd>", chat_session
->id
);
762 chatserver_command(sipe_private
, self
);
767 g_hash_table_destroy(user_ids
);
771 static void chatserver_grpchat_message(struct sipe_core_private
*sipe_private
,
772 const sipe_xml
*grpchat
);
774 static void chatserver_response_history(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
775 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
776 SIPE_UNUSED_PARAMETER guint result
,
777 SIPE_UNUSED_PARAMETER
const gchar
*message
,
780 const sipe_xml
*grpchat
;
782 for (grpchat
= sipe_xml_child(xml
, "chanib/msg");
784 grpchat
= sipe_xml_twin(grpchat
))
785 if (sipe_strequal(sipe_xml_attribute(grpchat
, "id"),
787 chatserver_grpchat_message(sipe_private
, grpchat
);
790 static void chatserver_response_part(struct sipe_core_private
*sipe_private
,
791 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
793 const gchar
*message
,
797 SIPE_DEBUG_WARNING("chatserver_response_part: failed with %d: %s. Dropping room",
800 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
801 const gchar
*uri
= sipe_xml_attribute(sipe_xml_child(xml
, "chanib"),
803 struct sipe_chat_session
*chat_session
;
806 (chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
809 SIPE_DEBUG_INFO("leaving room '%s' (%s)",
810 chat_session
->title
, chat_session
->id
);
812 g_hash_table_remove(groupchat
->uri_to_chat_session
,
814 sipe_chat_remove_session(chat_session
);
817 SIPE_DEBUG_WARNING("chatserver_response_part: unknown chat room uri '%s'",
823 static void chatserver_notice_join(struct sipe_core_private
*sipe_private
,
824 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
825 SIPE_UNUSED_PARAMETER guint result
,
826 SIPE_UNUSED_PARAMETER
const gchar
*message
,
829 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
832 for (uib
= sipe_xml_child(xml
, "uib");
834 uib
= sipe_xml_twin(uib
)) {
835 const gchar
*uri
= sipe_xml_attribute(uib
, "uri");
840 for (aib
= sipe_xml_child(uib
, "aib");
842 aib
= sipe_xml_twin(aib
)) {
843 const gchar
*domain
= sipe_xml_attribute(aib
, "domain");
844 const gchar
*path
= sipe_xml_attribute(aib
, "value");
846 if (domain
&& path
) {
847 gchar
*room_uri
= g_strdup_printf("ma-chan://%s/%s",
849 struct sipe_chat_session
*chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
852 add_user(chat_session
,
864 static void chatserver_notice_part(struct sipe_core_private
*sipe_private
,
865 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
866 SIPE_UNUSED_PARAMETER guint result
,
867 SIPE_UNUSED_PARAMETER
const gchar
*message
,
870 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
871 const sipe_xml
*chanib
;
873 for (chanib
= sipe_xml_child(xml
, "chanib");
875 chanib
= sipe_xml_twin(chanib
)) {
876 const gchar
*room_uri
= sipe_xml_attribute(chanib
, "uri");
879 struct sipe_chat_session
*chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
885 for (uib
= sipe_xml_child(chanib
, "uib");
887 uib
= sipe_xml_twin(uib
)) {
888 const gchar
*uri
= sipe_xml_attribute(uib
, "uri");
891 SIPE_DEBUG_INFO("remove_user: %s from room %s (%s)",
895 sipe_backend_chat_remove(chat_session
->backend
,
904 static const struct response
{
906 void (* const handler
)(struct sipe_core_private
*,
907 struct sip_session
*,
908 guint result
, const gchar
*,
909 const sipe_xml
*xml
);
910 } response_table
[] = {
911 { "rpl:requri", chatserver_response_uri
},
912 { "rpl:chansrch", chatserver_response_channel_search
},
913 { "rpl:join", chatserver_response_join
},
914 { "rpl:bjoin", chatserver_response_join
},
915 { "rpl:bccontext", chatserver_response_history
},
916 { "rpl:part", chatserver_response_part
},
917 { "ntc:join", chatserver_notice_join
},
918 { "ntc:bjoin", chatserver_notice_join
},
919 { "ntc:part", chatserver_notice_part
},
923 /* Handles rpl:XXX & ntc:YYY */
924 static void chatserver_response(struct sipe_core_private
*sipe_private
,
925 const sipe_xml
*reply
,
926 struct sip_session
*session
)
929 const sipe_xml
*resp
, *data
;
933 const struct response
*r
;
935 id
= sipe_xml_attribute(reply
, "id");
937 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: no reply ID found!");
941 resp
= sipe_xml_child(reply
, "resp");
943 result
= sipe_xml_int_attribute(resp
, "code", 500);
944 message
= sipe_xml_data(resp
);
946 message
= g_strdup("");
949 data
= sipe_xml_child(reply
, "data");
951 SIPE_DEBUG_INFO("chatserver_response: '%s' result (%d) %s",
952 id
, result
, message
? message
: "");
954 for (r
= response_table
; r
->key
; r
++) {
955 if (sipe_strcase_equal(id
, r
->key
)) {
956 (*r
->handler
)(sipe_private
, session
, result
, message
, data
);
961 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: ignoring unknown response");
965 } while ((reply
= sipe_xml_twin(reply
)) != NULL
);
968 static void chatserver_grpchat_message(struct sipe_core_private
*sipe_private
,
969 const sipe_xml
*grpchat
)
971 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
972 const gchar
*uri
= sipe_xml_attribute(grpchat
, "chanUri");
973 const gchar
*from
= sipe_xml_attribute(grpchat
, "author");
974 time_t when
= sipe_utils_str_to_time(sipe_xml_attribute(grpchat
, "ts"));
975 gchar
*text
= sipe_xml_data(sipe_xml_child(grpchat
, "chat"));
976 struct sipe_chat_session
*chat_session
;
980 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' received without chat room URI or author!",
986 chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
989 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' from '%s' received from unknown chat room '%s'!",
990 text
? text
: "", from
, uri
);
995 /* libxml2 decodes all entities, but the backend expects HTML */
996 escaped
= g_markup_escape_text(text
, -1);
998 sipe_backend_chat_message(SIPE_CORE_PUBLIC
, chat_session
->backend
,
999 from
, when
, escaped
);
1003 void process_incoming_info_groupchat(struct sipe_core_private
*sipe_private
,
1005 struct sip_session
*session
)
1007 sipe_xml
*xml
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1008 const sipe_xml
*node
;
1009 const gchar
*callid
;
1010 struct sip_dialog
*dialog
;
1012 callid
= sipmsg_find_header(msg
, "Call-ID");
1013 dialog
= sipe_dialog_find(session
, session
->with
);
1014 if (sipe_strequal(callid
, dialog
->callid
)) {
1016 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1018 if (((node
= sipe_xml_child(xml
, "rpl")) != NULL
) ||
1019 ((node
= sipe_xml_child(xml
, "ntc")) != NULL
)) {
1020 chatserver_response(sipe_private
, node
, session
);
1021 } else if ((node
= sipe_xml_child(xml
, "grpchat")) != NULL
) {
1022 chatserver_grpchat_message(sipe_private
, node
);
1024 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_info_groupchat: ignoring unknown response");
1029 * Our last session got disconnected without proper shutdown,
1030 * e.g. by Pidgin crashing or network connection loss. When
1031 * we reconnect to the group chat the server will send INFO
1032 * messages to the current *AND* the obsolete Call-ID, until
1033 * the obsolete session expires.
1035 * Ignore these INFO messages to avoid, e.g. duplicate texts,
1036 * and respond with an error so that the server knows that we
1037 * consider this dialog to be terminated.
1039 SIPE_DEBUG_INFO("process_incoming_info_groupchat: ignoring unsolicited INFO message to obsolete Call-ID: %s",
1042 sip_transport_response(sipe_private
, msg
, 487, "Request Terminated", NULL
);
1048 void sipe_groupchat_send(struct sipe_core_private
*sipe_private
,
1049 struct sipe_chat_session
*chat_session
,
1052 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1053 gchar
*cmd
, *self
, *timestamp
, *tmp
;
1054 gchar
**lines
, **strvp
;
1055 struct sipe_groupchat_msg
*msg
;
1057 if (!groupchat
|| !chat_session
)
1060 SIPE_DEBUG_INFO("sipe_groupchat_send: '%s' to %s",
1061 what
, chat_session
->id
);
1063 self
= sip_uri_self(sipe_private
);
1064 timestamp
= sipe_utils_time_to_str(time(NULL
));
1067 * 'what' is already XML-escaped, e.g.
1074 * Group Chat only accepts plain text, not full HTML. So we have to
1075 * strip all HTML tags and XML escape the text.
1077 * Line breaks are encoded as <br> and therefore need to be replaced
1078 * before stripping. In order to prevent HTML stripping to strip line
1079 * endings, we need to split the text into lines on <br>.
1081 lines
= g_strsplit(what
, "<br>", 0);
1082 for (strvp
= lines
; *strvp
; strvp
++) {
1083 /* replace array entry with HTML stripped & XML escaped version */
1084 gchar
*stripped
= sipe_backend_markup_strip_html(*strvp
);
1085 gchar
*escaped
= g_markup_escape_text(stripped
, -1);
1090 tmp
= g_strjoinv("\r\n", lines
);
1092 cmd
= g_strdup_printf("<grpchat id=\"grpchat\" seqid=\"1\" chanUri=\"%s\" author=\"%s\" ts=\"%s\">"
1095 chat_session
->id
, self
, timestamp
, tmp
);
1099 msg
= chatserver_command(sipe_private
, cmd
);
1103 msg
->session
= chat_session
;
1104 msg
->content
= g_strdup(what
);
1108 void sipe_groupchat_leave(struct sipe_core_private
*sipe_private
,
1109 struct sipe_chat_session
*chat_session
)
1111 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1114 if (!groupchat
|| !chat_session
)
1117 SIPE_DEBUG_INFO("sipe_groupchat_leave: %s", chat_session
->id
);
1119 cmd
= g_strdup_printf("<cmd id=\"cmd:part\" seqid=\"1\">"
1121 "<chanib uri=\"%s\"/>"
1123 "</cmd>", chat_session
->id
);
1124 chatserver_command(sipe_private
, cmd
);
1128 gboolean
sipe_core_groupchat_query_rooms(struct sipe_core_public
*sipe_public
)
1130 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1131 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1133 if (!groupchat
|| !groupchat
->connected
)
1136 chatserver_command(sipe_private
,
1137 "<cmd id=\"cmd:chansrch\" seqid=\"1\">"
1139 "<qib qtype=\"BYNAME\" criteria=\"\" extended=\"false\"/>"
1146 void sipe_core_groupchat_join(struct sipe_core_public
*sipe_public
,
1149 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1150 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1152 if (!g_str_has_prefix(uri
, "ma-chan://"))
1156 /* This happens when a user has set auto-join on a channel */
1157 sipe_groupchat_allocate(sipe_private
);
1158 groupchat
= sipe_private
->groupchat
;
1161 if (groupchat
->connected
) {
1162 struct sipe_chat_session
*chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
1165 /* Already joined? */
1168 /* Yes, update backend session */
1169 SIPE_DEBUG_INFO("sipe_core_groupchat_join: show '%s' (%s)",
1170 chat_session
->title
,
1172 sipe_backend_chat_show(chat_session
->backend
);
1175 /* No, send command out directly */
1176 gchar
*chanid
= generate_chanid_node(uri
, 0);
1178 gchar
*cmd
= g_strdup_printf("<cmd id=\"cmd:join\" seqid=\"1\">"
1182 SIPE_DEBUG_INFO("sipe_core_groupchat_join: join %s",
1184 chatserver_command(sipe_private
, cmd
);
1190 /* Add it to the queue but avoid duplicates */
1191 if (!g_slist_find_custom(groupchat
->join_queue
, uri
,
1193 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_groupchat_join: URI queued");
1194 groupchat
->join_queue
= g_slist_prepend(groupchat
->join_queue
,
1200 void sipe_groupchat_rejoin(struct sipe_core_private
*sipe_private
,
1201 struct sipe_chat_session
*chat_session
)
1203 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1206 /* First rejoined channel after reconnect will trigger this */
1207 sipe_groupchat_allocate(sipe_private
);
1208 groupchat
= sipe_private
->groupchat
;
1211 /* Remember "old" session, so that we don't recreate it at join */
1212 g_hash_table_insert(groupchat
->uri_to_chat_session
,
1215 sipe_core_groupchat_join(SIPE_CORE_PUBLIC
, chat_session
->id
);