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'",
366 static void groupchat_update_cb(struct sipe_core_private
*sipe_private
,
368 static gboolean
groupchat_expired_session_response(struct sipe_core_private
*sipe_private
,
370 SIPE_UNUSED_PARAMETER
struct transaction
*trans
)
372 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
374 /* 481 Call Leg Does Not Exist -> server dropped session */
375 if (msg
->response
== 481) {
376 groupchat
->session
= NULL
;
377 groupchat
->connected
= FALSE
;
378 sipe_groupchat_init(sipe_private
);
380 sipe_schedule_seconds(sipe_private
,
381 "<+groupchat-expires>",
391 /* sipe_schedule_action */
392 static void groupchat_update_cb(struct sipe_core_private
*sipe_private
,
393 SIPE_UNUSED_PARAMETER gpointer data
)
395 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
397 if (groupchat
->session
) {
398 struct sip_dialog
*dialog
= sipe_dialog_find(groupchat
->session
,
399 groupchat
->session
->with
);
402 sip_transport_update(sipe_private
,
404 groupchat_expired_session_response
);
408 static struct sipe_groupchat_msg
*chatserver_command(struct sipe_core_private
*sipe_private
,
411 void sipe_groupchat_invite_response(struct sipe_core_private
*sipe_private
,
412 struct sip_dialog
*dialog
,
413 struct sipmsg
*response
)
415 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
417 SIPE_DEBUG_INFO_NOFORMAT("sipe_groupchat_invite_response");
419 if (!groupchat
->session
) {
420 /* response to initial invite */
421 struct sipe_groupchat_msg
*msg
= generate_xccos_message(groupchat
,
422 "<cmd id=\"cmd:requri\" seqid=\"1\"><data/></cmd>");
423 const gchar
*session_expires
= sipmsg_find_header(response
,
426 sip_transport_info(sipe_private
,
427 "Content-Type: text/plain\r\n",
431 sipe_groupchat_msg_remove(msg
);
433 if (session_expires
) {
434 groupchat
->expires
= strtoul(session_expires
, NULL
, 10);
436 if (groupchat
->expires
) {
437 SIPE_DEBUG_INFO("sipe_groupchat_invite_response: session expires in %d seconds",
440 if (groupchat
->expires
> 10)
441 groupchat
->expires
-= 10;
442 sipe_schedule_seconds(sipe_private
,
443 "<+groupchat-expires>",
452 /* response to group chat server invite */
455 SIPE_DEBUG_INFO_NOFORMAT("connection to group chat server established.");
457 groupchat
->connected
= TRUE
;
459 /* Any queued joins? */
460 if (groupchat
->join_queue
) {
461 GString
*cmd
= g_string_new("<cmd id=\"cmd:bjoin\" seqid=\"1\">"
466 /* We used g_slist_prepend() to create the list */
467 groupchat
->join_queue
= entry
= g_slist_reverse(groupchat
->join_queue
);
469 gchar
*chanid
= generate_chanid_node(entry
->data
, i
++);
470 g_string_append(cmd
, chanid
);
474 sipe_groupchat_free_join_queue(groupchat
);
476 g_string_append(cmd
, "</data></cmd>");
477 chatserver_command(sipe_private
, cmd
->str
);
478 g_string_free(cmd
, TRUE
);
481 /* Request outstanding invites from server */
482 invcmd
= g_strdup_printf("<cmd id=\"cmd:getinv\" seqid=\"1\">"
484 "<inv inviteId=\"1\" domain=\"%s\"/>"
486 "</cmd>", groupchat
->domain
);
487 chatserver_command(sipe_private
, invcmd
);
492 static void chatserver_command_error_notify(struct sipe_core_private
*sipe_private
,
493 struct sipe_chat_session
*chat_session
,
494 const gchar
*content
)
496 gchar
*label
= g_strdup_printf(_("This message was not delivered to chat room '%s'"),
497 chat_session
->title
);
498 gchar
*errmsg
= g_strdup_printf("%s:\n<font color=\"#888888\"></b>%s<b></font>",
501 sipe_backend_notify_message_error(SIPE_CORE_PUBLIC
,
502 chat_session
->backend
,
509 static gboolean
chatserver_command_response(struct sipe_core_private
*sipe_private
,
511 struct transaction
*trans
)
513 if (msg
->response
!= 200) {
514 struct sipe_groupchat_msg
*gmsg
= trans
->payload
->data
;
515 struct sipe_chat_session
*chat_session
= gmsg
->session
;
517 SIPE_DEBUG_INFO("chatserver_command_response: failure %d", msg
->response
);
520 chatserver_command_error_notify(sipe_private
,
524 groupchat_expired_session_response(sipe_private
, msg
, trans
);
529 static struct sipe_groupchat_msg
*chatserver_command(struct sipe_core_private
*sipe_private
,
532 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
533 struct sip_dialog
*dialog
= sipe_dialog_find(groupchat
->session
,
534 groupchat
->session
->with
);
535 struct sipe_groupchat_msg
*msg
= NULL
;
538 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
539 struct transaction
*trans
;
541 msg
= generate_xccos_message(groupchat
, cmd
);
542 trans
= sip_transport_info(sipe_private
,
543 "Content-Type: text/plain\r\n",
546 chatserver_command_response
);
548 payload
->destroy
= sipe_groupchat_msg_remove
;
550 trans
->payload
= payload
;
556 static void chatserver_response_uri(struct sipe_core_private
*sipe_private
,
557 struct sip_session
*session
,
558 SIPE_UNUSED_PARAMETER guint result
,
559 SIPE_UNUSED_PARAMETER
const gchar
*message
,
562 const sipe_xml
*uib
= sipe_xml_child(xml
, "uib");
563 const gchar
*uri
= sipe_xml_attribute(uib
, "uri");
565 /* drop connection to ocschat@<domain> again */
566 sipe_session_close(sipe_private
, session
);
569 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
571 SIPE_DEBUG_INFO("chatserver_response_uri: '%s'", uri
);
573 groupchat
->session
= session
= sipe_session_find_or_add_im(sipe_private
,
576 session
->is_groupchat
= TRUE
;
577 sipe_im_invite(sipe_private
, session
, uri
, NULL
, NULL
, NULL
, FALSE
);
579 SIPE_DEBUG_WARNING_NOFORMAT("chatserver_response_uri: no server URI found!");
580 groupchat_init_retry(sipe_private
);
584 static void chatserver_response_channel_search(struct sipe_core_private
*sipe_private
,
585 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
587 const gchar
*message
,
590 struct sipe_core_public
*sipe_public
= SIPE_CORE_PUBLIC
;
593 sipe_backend_notify_error(sipe_public
,
594 _("Error retrieving room list"),
597 const sipe_xml
*chanib
;
599 for (chanib
= sipe_xml_child(xml
, "chanib");
601 chanib
= sipe_xml_twin(chanib
)) {
602 const gchar
*name
= sipe_xml_attribute(chanib
, "name");
603 const gchar
*desc
= sipe_xml_attribute(chanib
, "description");
604 const gchar
*uri
= sipe_xml_attribute(chanib
, "uri");
605 const sipe_xml
*node
;
606 guint user_count
= 0;
610 for (node
= sipe_xml_child(chanib
, "info");
612 node
= sipe_xml_twin(node
)) {
613 const gchar
*id
= sipe_xml_attribute(node
, "id");
618 data
= sipe_xml_data(node
);
620 if (sipe_strcase_equal(id
, "urn:parlano:ma:info:ucnt")) {
621 user_count
= g_ascii_strtoll(data
, NULL
, 10);
622 } else if (sipe_strcase_equal(id
, "urn:parlano:ma:info:visibilty")) {
623 if (sipe_strcase_equal(data
, "private")) {
624 flags
|= SIPE_GROUPCHAT_ROOM_PRIVATE
;
632 for (node
= sipe_xml_child(chanib
, "prop");
634 node
= sipe_xml_twin(node
)) {
635 const gchar
*id
= sipe_xml_attribute(node
, "id");
640 data
= sipe_xml_data(node
);
642 gboolean value
= sipe_strcase_equal(data
, "true");
647 if (sipe_strcase_equal(id
, "urn:parlano:ma:prop:filepost")) {
648 add
= SIPE_GROUPCHAT_ROOM_FILEPOST
;
649 } else if (sipe_strcase_equal(id
, "urn:parlano:ma:prop:invite")) {
650 add
= SIPE_GROUPCHAT_ROOM_INVITE
;
651 } else if (sipe_strcase_equal(id
, "urn:parlano:ma:prop:logged")) {
652 add
= SIPE_GROUPCHAT_ROOM_LOGGED
;
659 SIPE_DEBUG_INFO("group chat channel '%s': '%s' (%s) with %u users, flags 0x%x",
660 name
, desc
, uri
, user_count
, flags
);
661 sipe_backend_groupchat_room_add(sipe_public
,
667 sipe_backend_groupchat_room_terminate(sipe_public
);
670 static gboolean
is_chanop(const sipe_xml
*aib
)
672 return sipe_strequal(sipe_xml_attribute(aib
, "key"),
673 GROUPCHAT_AIB_KEY_CHANOP
);
676 static void add_user(struct sipe_chat_session
*chat_session
,
678 gboolean
new, gboolean chanop
)
680 SIPE_DEBUG_INFO("add_user: %s%s%s to room %s (%s)",
682 chanop
? "chanop " : "",
684 chat_session
->title
, chat_session
->id
);
685 sipe_backend_chat_add(chat_session
->backend
, uri
, new);
687 sipe_backend_chat_operator(chat_session
->backend
, uri
);
690 static void chatserver_response_join(struct sipe_core_private
*sipe_private
,
691 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
693 const gchar
*message
,
697 sipe_backend_notify_error(SIPE_CORE_PUBLIC
,
698 _("Error joining chat room"),
701 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
702 const sipe_xml
*node
;
703 GHashTable
*user_ids
= g_hash_table_new(g_str_hash
, g_str_equal
);
705 /* Extract user IDs & URIs and generate ID -> URI map */
706 for (node
= sipe_xml_child(xml
, "uib");
708 node
= sipe_xml_twin(node
)) {
709 const gchar
*id
= sipe_xml_attribute(node
, "id");
710 const gchar
*uri
= sipe_xml_attribute(node
, "uri");
712 g_hash_table_insert(user_ids
,
717 /* Process channel data */
718 for (node
= sipe_xml_child(xml
, "chanib");
720 node
= sipe_xml_twin(node
)) {
721 const gchar
*uri
= sipe_xml_attribute(node
, "uri");
724 struct sipe_chat_session
*chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
726 gboolean
new = (chat_session
== NULL
);
727 const gchar
*attr
= sipe_xml_attribute(node
, "name");
728 gchar
*self
= sip_uri_self(sipe_private
);
732 chat_session
= sipe_chat_create_session(SIPE_CHAT_TYPE_GROUPCHAT
,
733 sipe_xml_attribute(node
,
736 g_hash_table_insert(groupchat
->uri_to_chat_session
,
740 SIPE_DEBUG_INFO("joined room '%s' (%s)",
743 chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
748 SIPE_DEBUG_INFO("rejoining room '%s' (%s)",
751 sipe_backend_chat_rejoin(SIPE_CORE_PUBLIC
,
752 chat_session
->backend
,
754 chat_session
->title
);
758 attr
= sipe_xml_attribute(node
, "topic");
760 sipe_backend_chat_topic(chat_session
->backend
,
764 /* Process user map for channel */
765 for (aib
= sipe_xml_child(node
, "aib");
767 aib
= sipe_xml_twin(aib
)) {
768 const gchar
*value
= sipe_xml_attribute(aib
, "value");
769 gboolean chanop
= is_chanop(aib
);
770 gchar
**ids
= g_strsplit(value
, ",", 0);
776 const gchar
*uri
= g_hash_table_lookup(user_ids
,
779 add_user(chat_session
,
790 /* Request last 25 entries from channel history */
791 self
= g_strdup_printf("<cmd id=\"cmd:bccontext\" seqid=\"1\">"
793 "<chanib uri=\"%s\"/>"
794 "<bcq><last cnt=\"25\"/></bcq>"
796 "</cmd>", chat_session
->id
);
797 chatserver_command(sipe_private
, self
);
802 g_hash_table_destroy(user_ids
);
806 static void chatserver_grpchat_message(struct sipe_core_private
*sipe_private
,
807 const sipe_xml
*grpchat
);
809 static void chatserver_response_history(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
810 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
811 SIPE_UNUSED_PARAMETER guint result
,
812 SIPE_UNUSED_PARAMETER
const gchar
*message
,
815 const sipe_xml
*grpchat
;
817 for (grpchat
= sipe_xml_child(xml
, "chanib/msg");
819 grpchat
= sipe_xml_twin(grpchat
))
820 if (sipe_strequal(sipe_xml_attribute(grpchat
, "id"),
822 chatserver_grpchat_message(sipe_private
, grpchat
);
825 static void chatserver_response_part(struct sipe_core_private
*sipe_private
,
826 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
828 const gchar
*message
,
832 SIPE_DEBUG_WARNING("chatserver_response_part: failed with %d: %s. Dropping room",
835 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
836 const gchar
*uri
= sipe_xml_attribute(sipe_xml_child(xml
, "chanib"),
838 struct sipe_chat_session
*chat_session
;
841 (chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
844 SIPE_DEBUG_INFO("leaving room '%s' (%s)",
845 chat_session
->title
, chat_session
->id
);
847 g_hash_table_remove(groupchat
->uri_to_chat_session
,
849 sipe_chat_remove_session(chat_session
);
852 SIPE_DEBUG_WARNING("chatserver_response_part: unknown chat room uri '%s'",
858 static void chatserver_notice_join(struct sipe_core_private
*sipe_private
,
859 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
860 SIPE_UNUSED_PARAMETER guint result
,
861 SIPE_UNUSED_PARAMETER
const gchar
*message
,
864 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
867 for (uib
= sipe_xml_child(xml
, "uib");
869 uib
= sipe_xml_twin(uib
)) {
870 const gchar
*uri
= sipe_xml_attribute(uib
, "uri");
875 for (aib
= sipe_xml_child(uib
, "aib");
877 aib
= sipe_xml_twin(aib
)) {
878 const gchar
*domain
= sipe_xml_attribute(aib
, "domain");
879 const gchar
*path
= sipe_xml_attribute(aib
, "value");
881 if (domain
&& path
) {
882 gchar
*room_uri
= g_strdup_printf("ma-chan://%s/%s",
884 struct sipe_chat_session
*chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
887 add_user(chat_session
,
899 static void chatserver_notice_part(struct sipe_core_private
*sipe_private
,
900 SIPE_UNUSED_PARAMETER
struct sip_session
*session
,
901 SIPE_UNUSED_PARAMETER guint result
,
902 SIPE_UNUSED_PARAMETER
const gchar
*message
,
905 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
906 const sipe_xml
*chanib
;
908 for (chanib
= sipe_xml_child(xml
, "chanib");
910 chanib
= sipe_xml_twin(chanib
)) {
911 const gchar
*room_uri
= sipe_xml_attribute(chanib
, "uri");
914 struct sipe_chat_session
*chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
920 for (uib
= sipe_xml_child(chanib
, "uib");
922 uib
= sipe_xml_twin(uib
)) {
923 const gchar
*uri
= sipe_xml_attribute(uib
, "uri");
926 SIPE_DEBUG_INFO("remove_user: %s from room %s (%s)",
930 sipe_backend_chat_remove(chat_session
->backend
,
939 static const struct response
{
941 void (* const handler
)(struct sipe_core_private
*,
942 struct sip_session
*,
943 guint result
, const gchar
*,
944 const sipe_xml
*xml
);
945 } response_table
[] = {
946 { "rpl:requri", chatserver_response_uri
},
947 { "rpl:chansrch", chatserver_response_channel_search
},
948 { "rpl:join", chatserver_response_join
},
949 { "rpl:bjoin", chatserver_response_join
},
950 { "rpl:bccontext", chatserver_response_history
},
951 { "rpl:part", chatserver_response_part
},
952 { "ntc:join", chatserver_notice_join
},
953 { "ntc:bjoin", chatserver_notice_join
},
954 { "ntc:part", chatserver_notice_part
},
958 /* Handles rpl:XXX & ntc:YYY */
959 static void chatserver_response(struct sipe_core_private
*sipe_private
,
960 const sipe_xml
*reply
,
961 struct sip_session
*session
)
964 const sipe_xml
*resp
, *data
;
968 const struct response
*r
;
970 id
= sipe_xml_attribute(reply
, "id");
972 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: no reply ID found!");
976 resp
= sipe_xml_child(reply
, "resp");
978 result
= sipe_xml_int_attribute(resp
, "code", 500);
979 message
= sipe_xml_data(resp
);
981 message
= g_strdup("");
984 data
= sipe_xml_child(reply
, "data");
986 SIPE_DEBUG_INFO("chatserver_response: '%s' result (%d) %s",
987 id
, result
, message
? message
: "");
989 for (r
= response_table
; r
->key
; r
++) {
990 if (sipe_strcase_equal(id
, r
->key
)) {
991 (*r
->handler
)(sipe_private
, session
, result
, message
, data
);
996 SIPE_DEBUG_INFO_NOFORMAT("chatserver_response: ignoring unknown response");
1000 } while ((reply
= sipe_xml_twin(reply
)) != NULL
);
1003 static void chatserver_grpchat_message(struct sipe_core_private
*sipe_private
,
1004 const sipe_xml
*grpchat
)
1006 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1007 const gchar
*uri
= sipe_xml_attribute(grpchat
, "chanUri");
1008 const gchar
*from
= sipe_xml_attribute(grpchat
, "author");
1009 time_t when
= sipe_utils_str_to_time(sipe_xml_attribute(grpchat
, "ts"));
1010 gchar
*text
= sipe_xml_data(sipe_xml_child(grpchat
, "chat"));
1011 struct sipe_chat_session
*chat_session
;
1014 if (!uri
|| !from
) {
1015 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' received without chat room URI or author!",
1021 chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
1023 if (!chat_session
) {
1024 SIPE_DEBUG_INFO("chatserver_grpchat_message: message '%s' from '%s' received from unknown chat room '%s'!",
1025 text
? text
: "", from
, uri
);
1030 /* libxml2 decodes all entities, but the backend expects HTML */
1031 escaped
= g_markup_escape_text(text
, -1);
1033 sipe_backend_chat_message(SIPE_CORE_PUBLIC
, chat_session
->backend
,
1034 from
, when
, escaped
);
1038 void process_incoming_info_groupchat(struct sipe_core_private
*sipe_private
,
1040 struct sip_session
*session
)
1042 sipe_xml
*xml
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1043 const sipe_xml
*node
;
1044 const gchar
*callid
;
1045 struct sip_dialog
*dialog
;
1047 callid
= sipmsg_find_header(msg
, "Call-ID");
1048 dialog
= sipe_dialog_find(session
, session
->with
);
1049 if (sipe_strequal(callid
, dialog
->callid
)) {
1051 sip_transport_response(sipe_private
, msg
, 200, "OK", NULL
);
1053 if (((node
= sipe_xml_child(xml
, "rpl")) != NULL
) ||
1054 ((node
= sipe_xml_child(xml
, "ntc")) != NULL
)) {
1055 chatserver_response(sipe_private
, node
, session
);
1056 } else if ((node
= sipe_xml_child(xml
, "grpchat")) != NULL
) {
1057 chatserver_grpchat_message(sipe_private
, node
);
1059 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_info_groupchat: ignoring unknown response");
1064 * Our last session got disconnected without proper shutdown,
1065 * e.g. by Pidgin crashing or network connection loss. When
1066 * we reconnect to the group chat the server will send INFO
1067 * messages to the current *AND* the obsolete Call-ID, until
1068 * the obsolete session expires.
1070 * Ignore these INFO messages to avoid, e.g. duplicate texts,
1071 * and respond with an error so that the server knows that we
1072 * consider this dialog to be terminated.
1074 SIPE_DEBUG_INFO("process_incoming_info_groupchat: ignoring unsolicited INFO message to obsolete Call-ID: %s",
1077 sip_transport_response(sipe_private
, msg
, 487, "Request Terminated", NULL
);
1083 void sipe_groupchat_send(struct sipe_core_private
*sipe_private
,
1084 struct sipe_chat_session
*chat_session
,
1087 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1088 gchar
*cmd
, *self
, *timestamp
, *tmp
;
1089 gchar
**lines
, **strvp
;
1090 struct sipe_groupchat_msg
*msg
;
1092 if (!groupchat
|| !chat_session
)
1095 SIPE_DEBUG_INFO("sipe_groupchat_send: '%s' to %s",
1096 what
, chat_session
->id
);
1098 self
= sip_uri_self(sipe_private
);
1099 timestamp
= sipe_utils_time_to_str(time(NULL
));
1102 * 'what' is already XML-escaped, e.g.
1109 * Group Chat only accepts plain text, not full HTML. So we have to
1110 * strip all HTML tags and XML escape the text.
1112 * Line breaks are encoded as <br> and therefore need to be replaced
1113 * before stripping. In order to prevent HTML stripping to strip line
1114 * endings, we need to split the text into lines on <br>.
1116 lines
= g_strsplit(what
, "<br>", 0);
1117 for (strvp
= lines
; *strvp
; strvp
++) {
1118 /* replace array entry with HTML stripped & XML escaped version */
1119 gchar
*stripped
= sipe_backend_markup_strip_html(*strvp
);
1120 gchar
*escaped
= g_markup_escape_text(stripped
, -1);
1125 tmp
= g_strjoinv("\r\n", lines
);
1127 cmd
= g_strdup_printf("<grpchat id=\"grpchat\" seqid=\"1\" chanUri=\"%s\" author=\"%s\" ts=\"%s\">"
1130 chat_session
->id
, self
, timestamp
, tmp
);
1134 msg
= chatserver_command(sipe_private
, cmd
);
1138 msg
->session
= chat_session
;
1139 msg
->content
= g_strdup(what
);
1141 chatserver_command_error_notify(sipe_private
,
1147 void sipe_groupchat_leave(struct sipe_core_private
*sipe_private
,
1148 struct sipe_chat_session
*chat_session
)
1150 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1153 if (!groupchat
|| !chat_session
)
1156 SIPE_DEBUG_INFO("sipe_groupchat_leave: %s", chat_session
->id
);
1158 cmd
= g_strdup_printf("<cmd id=\"cmd:part\" seqid=\"1\">"
1160 "<chanib uri=\"%s\"/>"
1162 "</cmd>", chat_session
->id
);
1163 chatserver_command(sipe_private
, cmd
);
1167 gboolean
sipe_core_groupchat_query_rooms(struct sipe_core_public
*sipe_public
)
1169 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1170 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1172 if (!groupchat
|| !groupchat
->connected
)
1175 chatserver_command(sipe_private
,
1176 "<cmd id=\"cmd:chansrch\" seqid=\"1\">"
1178 "<qib qtype=\"BYNAME\" criteria=\"\" extended=\"false\"/>"
1185 void sipe_core_groupchat_join(struct sipe_core_public
*sipe_public
,
1188 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1189 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1191 if (!g_str_has_prefix(uri
, "ma-chan://"))
1195 /* This happens when a user has set auto-join on a channel */
1196 sipe_groupchat_allocate(sipe_private
);
1197 groupchat
= sipe_private
->groupchat
;
1200 if (groupchat
->connected
) {
1201 struct sipe_chat_session
*chat_session
= g_hash_table_lookup(groupchat
->uri_to_chat_session
,
1204 /* Already joined? */
1207 /* Yes, update backend session */
1208 SIPE_DEBUG_INFO("sipe_core_groupchat_join: show '%s' (%s)",
1209 chat_session
->title
,
1211 sipe_backend_chat_show(chat_session
->backend
);
1214 /* No, send command out directly */
1215 gchar
*chanid
= generate_chanid_node(uri
, 0);
1217 gchar
*cmd
= g_strdup_printf("<cmd id=\"cmd:join\" seqid=\"1\">"
1221 SIPE_DEBUG_INFO("sipe_core_groupchat_join: join %s",
1223 chatserver_command(sipe_private
, cmd
);
1229 /* Add it to the queue but avoid duplicates */
1230 if (!g_slist_find_custom(groupchat
->join_queue
, uri
,
1232 SIPE_DEBUG_INFO_NOFORMAT("sipe_core_groupchat_join: URI queued");
1233 groupchat
->join_queue
= g_slist_prepend(groupchat
->join_queue
,
1239 void sipe_groupchat_rejoin(struct sipe_core_private
*sipe_private
,
1240 struct sipe_chat_session
*chat_session
)
1242 struct sipe_groupchat
*groupchat
= sipe_private
->groupchat
;
1245 /* First rejoined channel after reconnect will trigger this */
1246 sipe_groupchat_allocate(sipe_private
);
1247 groupchat
= sipe_private
->groupchat
;
1250 /* Remember "old" session, so that we don't recreate it at join */
1251 g_hash_table_insert(groupchat
->uri_to_chat_session
,
1254 sipe_core_groupchat_join(SIPE_CORE_PUBLIC
, chat_session
->id
);