2 * @file sipe-subscriptions.c
6 * Copyright (C) 2010-11 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "sipe-common.h"
29 #include "sip-transport.h"
30 #include "sipe-backend.h"
31 #include "sipe-buddy.h"
32 #include "sipe-core.h"
33 #include "sipe-core-private.h"
34 #include "sipe-dialog.h"
35 #include "sipe-notify.h"
36 #include "sipe-schedule.h"
37 #include "sipe-subscriptions.h"
38 #include "sipe-utils.h"
40 /* RFC3265 subscription */
41 struct sip_subscription
{
42 struct sip_dialog dialog
;
46 static void sipe_subscription_free(struct sip_subscription
*subscription
)
48 if (!subscription
) return;
50 g_free(subscription
->event
);
51 /* NOTE: use cast to prevent BAD_FREE warning from Coverity */
52 sipe_dialog_free((struct sip_dialog
*) subscription
);
55 void sipe_subscriptions_init(struct sipe_core_private
*sipe_private
)
57 sipe_private
->subscriptions
= g_hash_table_new_full(g_str_hash
,
60 (GDestroyNotify
)sipe_subscription_free
);
63 static void sipe_unsubscribe_cb(SIPE_UNUSED_PARAMETER gpointer key
,
64 gpointer value
, gpointer user_data
)
66 struct sip_subscription
*subscription
= value
;
67 struct sip_dialog
*dialog
= &subscription
->dialog
;
68 struct sipe_core_private
*sipe_private
= user_data
;
69 gchar
*contact
= get_contact(sipe_private
);
70 gchar
*hdr
= g_strdup_printf(
73 "Contact: %s\r\n", subscription
->event
, contact
);
76 /* Rate limit to max. 25 requests per seconds */
77 g_usleep(1000000 / 25);
79 sip_transport_subscribe(sipe_private
,
89 void sipe_subscriptions_unsubscribe(struct sipe_core_private
*sipe_private
)
92 g_hash_table_foreach(sipe_private
->subscriptions
,
98 void sipe_subscriptions_destroy(struct sipe_core_private
*sipe_private
)
100 g_hash_table_destroy(sipe_private
->subscriptions
);
103 void sipe_subscriptions_remove(struct sipe_core_private
*sipe_private
,
106 if (g_hash_table_lookup(sipe_private
->subscriptions
, key
)) {
107 g_hash_table_remove(sipe_private
->subscriptions
, key
);
108 SIPE_DEBUG_INFO("sipe_subscriptions_remove: %s", key
);
112 static gboolean
process_subscribe_response(struct sipe_core_private
*sipe_private
,
114 struct transaction
*trans
)
116 gchar
*with
= parse_from(sipmsg_find_header(msg
, "To"));
117 const gchar
*event
= sipmsg_find_header(msg
, "Event");
120 /* The case with 2005 Public IM Connectivity (PIC) - no Event header */
122 struct sipmsg
*request_msg
= trans
->msg
;
123 event
= sipmsg_find_header(request_msg
, "Event");
126 key
= sipe_utils_subscription_key(event
, with
);
128 /* 200 OK; 481 Call Leg Does Not Exist */
129 if (key
&& (msg
->response
== 200 || msg
->response
== 481)) {
130 sipe_subscriptions_remove(sipe_private
, key
);
133 /* create/store subscription dialog if not yet */
134 if (key
&& (msg
->response
== 200)) {
135 struct sip_subscription
*subscription
= g_new0(struct sip_subscription
, 1);
136 g_hash_table_insert(sipe_private
->subscriptions
,
140 subscription
->dialog
.callid
= g_strdup(sipmsg_find_header(msg
, "Call-ID"));
141 subscription
->dialog
.cseq
= sipmsg_parse_cseq(msg
);
142 subscription
->dialog
.with
= g_strdup(with
);
143 subscription
->event
= g_strdup(event
);
144 sipe_dialog_parse(&subscription
->dialog
, msg
, TRUE
);
146 SIPE_DEBUG_INFO("process_subscribe_response: subscription dialog added for: %s", key
);
152 if (sipmsg_find_header(msg
, "ms-piggyback-cseq"))
154 process_incoming_notify(sipe_private
, msg
, FALSE
, FALSE
);
160 * common subscription code
162 void sipe_subscribe(struct sipe_core_private
*sipe_private
,
166 const gchar
*addheaders
,
168 struct sip_dialog
*dialog
)
170 gchar
*contact
= get_contact(sipe_private
);
171 gchar
*hdr
= g_strdup_printf(
174 "Supported: com.microsoft.autoextend\r\n"
175 "Supported: ms-benotify\r\n"
176 "Proxy-Require: ms-benotify\r\n"
177 "Supported: ms-piggyback-first-notify\r\n"
182 addheaders
? addheaders
: "",
187 sip_transport_subscribe(sipe_private
,
192 process_subscribe_response
);
198 * common subscription code for self-subscriptions
200 static void sipe_subscribe_self(struct sipe_core_private
*sipe_private
,
203 const gchar
*addheaders
,
205 struct sip_dialog
*dialog
)
207 gchar
*self
= sip_uri_self(sipe_private
);
209 sipe_subscribe(sipe_private
,
220 static struct sip_dialog
*sipe_subscribe_dialog(struct sipe_core_private
*sipe_private
,
223 struct sip_dialog
*dialog
= g_hash_table_lookup(sipe_private
->subscriptions
,
225 SIPE_DEBUG_INFO("sipe_subscribe_dialog: dialog for '%s' is %s", key
, dialog
? "not NULL" : "NULL");
229 static void sipe_subscribe_presence_buddy(struct sipe_core_private
*sipe_private
,
231 const gchar
*request
,
234 gchar
*key
= sipe_utils_presence_key(uri
);
236 sip_transport_subscribe(sipe_private
,
240 sipe_subscribe_dialog(sipe_private
, key
),
241 process_subscribe_response
);
246 void sipe_subscribe_presence_wpending(struct sipe_core_private
*sipe_private
,
247 SIPE_UNUSED_PARAMETER
void *unused
)
249 gchar
*key
= sipe_utils_subscription_key("presence.wpending", NULL
);
251 sipe_subscribe_self(sipe_private
,
253 "text/xml+msrtc.wpending",
256 sipe_subscribe_dialog(sipe_private
, key
));
262 * Subscribe roaming ACL
264 void sipe_subscribe_roaming_acl(struct sipe_core_private
*sipe_private
)
266 sipe_subscribe_self(sipe_private
,
267 "vnd-microsoft-roaming-ACL",
268 "application/vnd-microsoft-roaming-acls+xml",
275 * Subscribe roaming contacts
277 void sipe_subscribe_roaming_contacts(struct sipe_core_private
*sipe_private
)
279 sipe_subscribe_self(sipe_private
,
280 "vnd-microsoft-roaming-contacts",
281 "application/vnd-microsoft-roaming-contacts+xml",
290 void sipe_subscribe_roaming_provisioning(struct sipe_core_private
*sipe_private
)
292 sipe_subscribe_self(sipe_private
,
293 "vnd-microsoft-provisioning",
294 "application/vnd-microsoft-roaming-provisioning+xml",
301 * Subscription for provisioning information to help with initial
302 * configuration. This subscription is a one-time query (denoted by the
303 * Expires header, which asks for 0 seconds for the subscription lifetime).
304 * This subscription asks for server configuration, meeting policies, and
305 * policy settings that Communicator must enforce.
307 void sipe_subscribe_roaming_provisioning_v2(struct sipe_core_private
*sipe_private
)
309 sipe_subscribe_self(sipe_private
,
310 "vnd-microsoft-provisioning-v2",
311 "application/vnd-microsoft-roaming-provisioning-v2+xml",
313 "Content-Type: application/vnd-microsoft-roaming-provisioning-v2+xml\r\n",
314 "<provisioningGroupList xmlns=\"http://schemas.microsoft.com/2006/09/sip/provisioninggrouplist\">"
315 "<provisioningGroup name=\"ServerConfiguration\"/><provisioningGroup name=\"meetingPolicy\"/>"
316 "<provisioningGroup name=\"ucPolicy\"/>"
317 "</provisioningGroupList>",
322 * To request for presence information about the user, access level settings
323 * that have already been configured by the user to control who has access to
324 * what information, and the list of contacts who currently have outstanding
327 * We wait for (BE)NOTIFY messages with some info change (categories,
328 * containers, subscribers)
330 void sipe_subscribe_roaming_self(struct sipe_core_private
*sipe_private
)
332 sipe_subscribe_self(sipe_private
,
333 "vnd-microsoft-roaming-self",
334 "application/vnd-microsoft-roaming-self+xml",
335 "Content-Type: application/vnd-microsoft-roaming-self+xml\r\n",
336 "<roamingList xmlns=\"http://schemas.microsoft.com/2006/09/sip/roaming-self\">"
337 "<roaming type=\"categories\"/>"
338 "<roaming type=\"containers\"/>"
339 "<roaming type=\"subscribers\"/></roamingList>",
344 * Single Category SUBSCRIBE [MS-PRES] ; To send when the server returns a 200 OK message with state="resubscribe" in response.
345 * The user sends a single SUBSCRIBE request to the subscribed contact.
346 * The To-URI and the URI listed in the resource list MUST be the same for a single category SUBSCRIBE request.
349 void sipe_subscribe_presence_single(struct sipe_core_private
*sipe_private
,
352 gchar
*to
= sip_uri((gchar
*)buddy_name
);
353 gchar
*tmp
= get_contact(sipe_private
);
355 gchar
*content
= NULL
;
356 gchar
*autoextend
= "";
357 gchar
*content_type
= "";
358 struct sipe_buddy
*sbuddy
= g_hash_table_lookup(sipe_private
->buddies
, to
);
359 gchar
*context
= sbuddy
&& sbuddy
->just_added
? "><context/></resource>" : "/>";
361 if (sbuddy
) sbuddy
->just_added
= FALSE
;
363 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
364 content_type
= "Content-Type: application/msrtc-adrl-categorylist+xml\r\n";
366 autoextend
= "Supported: com.microsoft.autoextend\r\n";
369 request
= g_strdup_printf("Accept: application/msrtc-event-categories+xml, text/xml+msrtc.pidf, application/xpidf+xml, application/pidf+xml, application/rlmi+xml, multipart/related\r\n"
370 "Supported: ms-piggyback-first-notify\r\n"
371 "%s%sSupported: ms-benotify\r\n"
372 "Proxy-Require: ms-benotify\r\n"
373 "Event: presence\r\n"
379 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
380 content
= g_strdup_printf("<batchSub xmlns=\"http://schemas.microsoft.com/2006/01/sip/batch-subscribe\" uri=\"sip:%s\" name=\"\">\n"
381 "<action name=\"subscribe\" id=\"63792024\"><adhocList>\n"
382 "<resource uri=\"%s\"%s\n"
384 "<categoryList xmlns=\"http://schemas.microsoft.com/2006/09/sip/categorylist\">\n"
385 "<category name=\"calendarData\"/>\n"
386 "<category name=\"contactCard\"/>\n"
387 "<category name=\"note\"/>\n"
388 "<category name=\"state\"/>\n"
392 sipe_private
->username
,
399 sipe_subscribe_presence_buddy(sipe_private
, to
, request
, content
);
407 * Support for Batch Category SUBSCRIBE [MS-PRES] - msrtc-event-categories+xml OCS 2007
408 * Support for Batch Category SUBSCRIBE [MS-SIP] - adrl+xml LCS 2005
409 * The user sends an initial batched category SUBSCRIBE request against all contacts on his roaming list in only a request
410 * A batch category SUBSCRIBE request MUST have the same To-URI and From-URI.
411 * This header will be send only if adhoclist there is a "Supported: adhoclist" in REGISTER answer else will be send a Single Category SUBSCRIBE
413 static void sipe_subscribe_presence_batched_to(struct sipe_core_private
*sipe_private
,
414 gchar
*resources_uri
,
417 gchar
*contact
= get_contact(sipe_private
);
422 gchar
*autoextend
= "";
425 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
426 require
= ", categoryList";
427 accept
= ", application/msrtc-event-categories+xml, application/xpidf+xml, application/pidf+xml";
428 content_type
= "application/msrtc-adrl-categorylist+xml";
429 content
= g_strdup_printf("<batchSub xmlns=\"http://schemas.microsoft.com/2006/01/sip/batch-subscribe\" uri=\"sip:%s\" name=\"\">\n"
430 "<action name=\"subscribe\" id=\"63792024\">\n"
431 "<adhocList>\n%s</adhocList>\n"
432 "<categoryList xmlns=\"http://schemas.microsoft.com/2006/09/sip/categorylist\">\n"
433 "<category name=\"calendarData\"/>\n"
434 "<category name=\"contactCard\"/>\n"
435 "<category name=\"note\"/>\n"
436 "<category name=\"state\"/>\n"
440 sipe_private
->username
,
443 autoextend
= "Supported: com.microsoft.autoextend\r\n";
444 content_type
= "application/adrl+xml";
445 content
= g_strdup_printf("<adhoclist xmlns=\"urn:ietf:params:xml:ns:adrl\" uri=\"sip:%s\" name=\"sip:%s\">\n"
446 "<create xmlns=\"\">\n%s</create>\n"
448 sipe_private
->username
,
449 sipe_private
->username
,
452 g_free(resources_uri
);
454 request
= g_strdup_printf("Require: adhoclist%s\r\n"
455 "Supported: eventlist\r\n"
456 "Accept: application/rlmi+xml, multipart/related, text/xml+msrtc.pidf%s\r\n"
457 "Supported: ms-piggyback-first-notify\r\n"
458 "%sSupported: ms-benotify\r\n"
459 "Proxy-Require: ms-benotify\r\n"
460 "Event: presence\r\n"
461 "Content-Type: %s\r\n"
470 sipe_subscribe_presence_buddy(sipe_private
, to
, request
, content
);
477 struct presence_batched_routed
{
482 static void sipe_subscribe_presence_batched_routed_free(gpointer payload
)
484 struct presence_batched_routed
*data
= payload
;
485 GSList
*buddies
= data
->buddies
;
487 g_free(buddies
->data
);
488 buddies
= buddies
->next
;
490 g_slist_free(data
->buddies
);
495 static void sipe_subscribe_presence_batched_routed(struct sipe_core_private
*sipe_private
,
498 struct presence_batched_routed
*data
= payload
;
499 GSList
*buddies
= data
->buddies
;
500 gchar
*resources_uri
= g_strdup("");
502 gchar
*tmp
= resources_uri
;
503 resources_uri
= g_strdup_printf("%s<resource uri=\"%s\"/>\n", tmp
, (char *) buddies
->data
);
505 buddies
= buddies
->next
;
507 sipe_subscribe_presence_batched_to(sipe_private
, resources_uri
,
508 g_strdup(data
->host
));
511 void sipe_subscribe_presence_batched_schedule(struct sipe_core_private
*sipe_private
,
512 const gchar
*action_name
,
517 struct presence_batched_routed
*payload
= g_malloc(sizeof(struct presence_batched_routed
));
518 payload
->host
= g_strdup(who
);
519 payload
->buddies
= buddies
;
520 sipe_schedule_seconds(sipe_private
,
524 sipe_subscribe_presence_batched_routed
,
525 sipe_subscribe_presence_batched_routed_free
);
526 SIPE_DEBUG_INFO("Resubscription multiple contacts with batched support & route(%s) in %d", who
, timeout
);
529 static void sipe_subscribe_resource_uri_with_context(const gchar
*name
,
531 gchar
**resources_uri
)
533 struct sipe_buddy
*sbuddy
= (struct sipe_buddy
*)value
;
534 gchar
*context
= sbuddy
&& sbuddy
->just_added
? "><context/></resource>" : "/>";
535 gchar
*tmp
= *resources_uri
;
537 if (sbuddy
) sbuddy
->just_added
= FALSE
; /* should be enought to include context one time */
539 *resources_uri
= g_strdup_printf("%s<resource uri=\"%s\"%s\n", tmp
, name
, context
);
543 static void sipe_subscribe_resource_uri(const char *name
,
544 SIPE_UNUSED_PARAMETER gpointer value
,
545 gchar
**resources_uri
)
547 gchar
*tmp
= *resources_uri
;
548 *resources_uri
= g_strdup_printf("%s<resource uri=\"%s\"/>\n", tmp
, name
);
552 void sipe_subscribe_presence_batched(struct sipe_core_private
*sipe_private
)
554 gchar
*to
= sip_uri_self(sipe_private
);
555 gchar
*resources_uri
= g_strdup("");
556 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
557 g_hash_table_foreach(sipe_private
->buddies
, (GHFunc
) sipe_subscribe_resource_uri_with_context
, &resources_uri
);
559 g_hash_table_foreach(sipe_private
->buddies
, (GHFunc
) sipe_subscribe_resource_uri
, &resources_uri
);
562 sipe_subscribe_presence_batched_to(sipe_private
, resources_uri
, to
);
565 void sipe_subscribe_poolfqdn_resource_uri(const char *host
,
567 struct sipe_core_private
*sipe_private
)
569 struct presence_batched_routed
*payload
= g_malloc(sizeof(struct presence_batched_routed
));
570 SIPE_DEBUG_INFO("process_incoming_notify_rlmi_resub: pool(%s)", host
);
571 payload
->host
= g_strdup(host
);
572 payload
->buddies
= server
;
573 sipe_subscribe_presence_batched_routed(sipe_private
,
575 sipe_subscribe_presence_batched_routed_free(payload
);