digest: add support for OpenSSL 1.1.0
[siplcs.git] / src / core / sipe-notify.c
blob2886183662ec83248c56581a16a8c61ddc5ee8fe
1 /**
2 * @file sipe-notify.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2017 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Process incoming SIP NOTIFY/BENOTIFY messages
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
32 #include <stdlib.h>
33 #include <string.h>
35 #include <glib.h>
37 #include "sipmsg.h"
38 #include "sip-csta.h"
39 #include "sip-soap.h"
40 #include "sipe-backend.h"
41 #include "sipe-buddy.h"
42 #include "sipe-cal.h"
43 #include "sipe-conf.h"
44 #include "sipe-core.h"
45 #include "sipe-core-private.h"
46 #include "sipe-group.h"
47 #include "sipe-groupchat.h"
48 #include "sipe-media.h"
49 #include "sipe-mime.h"
50 #include "sipe-nls.h"
51 #include "sipe-notify.h"
52 #include "sipe-ocs2005.h"
53 #include "sipe-ocs2007.h"
54 #include "sipe-status.h"
55 #include "sipe-subscriptions.h"
56 #include "sipe-ucs.h"
57 #include "sipe-utils.h"
58 #include "sipe-xml.h"
60 /* OCS2005 */
61 static void sipe_process_provisioning(struct sipe_core_private *sipe_private,
62 struct sipmsg *msg)
64 sipe_xml *xn_provision;
65 const sipe_xml *node;
67 xn_provision = sipe_xml_parse(msg->body, msg->bodylen);
68 if ((node = sipe_xml_child(xn_provision, "user"))) {
69 SIPE_DEBUG_INFO("sipe_process_provisioning: uri=%s", sipe_xml_attribute(node, "uri"));
70 if ((node = sipe_xml_child(node, "line"))) {
71 const gchar *line_uri = sipe_xml_attribute(node, "uri");
72 const gchar *server = sipe_xml_attribute(node, "server");
73 SIPE_DEBUG_INFO("sipe_process_provisioning: line_uri=%s server=%s", line_uri, server);
74 sip_csta_open(sipe_private, line_uri, server);
77 sipe_xml_free(xn_provision);
80 /* OCS2007+ */
81 static void sipe_process_provisioning_v2(struct sipe_core_private *sipe_private,
82 struct sipmsg *msg)
84 #define READ_INT_FROM_NODE(node_name, field) { \
85 gchar *s = g_strstrip(sipe_xml_data(sipe_xml_child(node, node_name))); \
86 sipe_private->field = s ? atoi(s) : 0; \
87 g_free(s); }
89 sipe_xml *xn_provision_group_list;
90 const sipe_xml *node;
92 xn_provision_group_list = sipe_xml_parse(msg->body, msg->bodylen);
94 /* provisionGroup */
95 for (node = sipe_xml_child(xn_provision_group_list, "provisionGroup");
96 node;
97 node = sipe_xml_twin(node)) {
98 const gchar *node_name = sipe_xml_attribute(node, "name");
100 /* ServerConfiguration */
101 if (sipe_strequal("ServerConfiguration", node_name)) {
102 const gchar *dlx_uri_str = SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ?
103 "dlxExternalUrl" : "dlxInternalUrl";
104 const gchar *addressbook_uri_str = SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ?
105 "absExternalServerUrl" : "absInternalServerUrl";
106 gchar *ucPC2PCAVEncryption = NULL;
107 gchar *ucPortRangeEnabled = NULL;
109 g_free(sipe_private->focus_factory_uri);
110 sipe_private->focus_factory_uri = sipe_xml_data(sipe_xml_child(node, "focusFactoryUri"));
111 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->focus_factory_uri=%s",
112 sipe_private->focus_factory_uri ? sipe_private->focus_factory_uri : "");
114 g_free(sipe_private->dlx_uri);
115 sipe_private->dlx_uri = sipe_xml_data(sipe_xml_child(node, dlx_uri_str));
116 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->dlx_uri=%s",
117 sipe_private->dlx_uri ? sipe_private->dlx_uri : "");
119 g_free(sipe_private->addressbook_uri);
120 sipe_private->addressbook_uri = sipe_xml_data(sipe_xml_child(node, addressbook_uri_str));
121 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->addressbook_uri=%s",
122 sipe_private->addressbook_uri ? sipe_private->addressbook_uri : "");
124 #ifdef HAVE_VV
125 g_free(sipe_private->test_call_bot_uri);
126 sipe_private->test_call_bot_uri = sipe_xml_data(sipe_xml_child(node, "botSipUriForTestCall"));
127 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->test_call_bot_uri=%s",
128 sipe_private->test_call_bot_uri ? sipe_private->test_call_bot_uri : "");
130 g_free(sipe_private->mras_uri);
131 sipe_private->mras_uri = g_strstrip(sipe_xml_data(sipe_xml_child(node, "mrasUri")));
132 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->mras_uri=%s",
133 sipe_private->mras_uri ? sipe_private->mras_uri : "");
135 if (sipe_private->mras_uri)
136 sipe_media_get_av_edge_credentials(sipe_private);
137 #endif
139 ucPC2PCAVEncryption = g_strstrip(sipe_xml_data(sipe_xml_child(node, "ucPC2PCAVEncryption")));
140 if (sipe_strequal(ucPC2PCAVEncryption, "SupportEncryption")) {
141 sipe_private->server_av_encryption_policy = SIPE_ENCRYPTION_POLICY_OPTIONAL;
142 } else if (sipe_strequal(ucPC2PCAVEncryption, "DoNotSupportEncryption")) {
143 sipe_private->server_av_encryption_policy = SIPE_ENCRYPTION_POLICY_REJECTED;
144 } else {
145 // "RequireEncryption" or any unknown value.
146 sipe_private->server_av_encryption_policy = SIPE_ENCRYPTION_POLICY_REQUIRED;
148 g_free(ucPC2PCAVEncryption);
150 ucPortRangeEnabled = g_strstrip(sipe_xml_data(sipe_xml_child(node, "ucPortRangeEnabled")));
151 if (sipe_strequal(ucPortRangeEnabled, "true")) {
152 READ_INT_FROM_NODE("ucMinMediaPort", min_media_port)
153 READ_INT_FROM_NODE("ucMaxMediaPort", max_media_port)
154 READ_INT_FROM_NODE("ucMinAudioPort", min_audio_port)
155 READ_INT_FROM_NODE("ucMaxAudioPort", max_audio_port)
156 READ_INT_FROM_NODE("ucMinVideoPort", min_video_port)
157 READ_INT_FROM_NODE("ucMaxVideoPort", max_video_port)
158 READ_INT_FROM_NODE("ucMinAppSharingPort", min_appsharing_port)
159 READ_INT_FROM_NODE("ucMaxAppSharingPort", max_appsharing_port)
160 READ_INT_FROM_NODE("ucMinFileTransferPort", min_filetransfer_port)
161 READ_INT_FROM_NODE("ucMaxFileTransferPort", max_filetransfer_port)
162 } else {
163 sipe_private->min_media_port = 0;
164 sipe_private->max_media_port = 0;
165 sipe_private->min_audio_port = 0;
166 sipe_private->max_audio_port = 0;
167 sipe_private->min_video_port = 0;
168 sipe_private->max_video_port = 0;
169 sipe_private->min_appsharing_port = 0;
170 sipe_private->max_appsharing_port = 0;
171 sipe_private->min_filetransfer_port = 0;
172 sipe_private->max_filetransfer_port = 0;
174 g_free(ucPortRangeEnabled);
176 /* persistentChatConfiguration */
177 } else if (sipe_strequal("persistentChatConfiguration", node_name)) {
178 const sipe_xml *property;
179 gboolean enabled = FALSE;
180 gchar *uri = NULL;
182 for (property = sipe_xml_child(node, "propertyEntryList/property");
183 property;
184 property = sipe_xml_twin(property)) {
185 const gchar *name = sipe_xml_attribute(property, "name");
186 gchar *value = sipe_xml_data(property);
188 if (sipe_strequal(name, "EnablePersistentChat")) {
189 enabled = sipe_strequal(value, "true");
191 } else if (sipe_strequal(name, "DefaultPersistentChatPoolUri")) {
192 g_free(uri);
193 uri = value;
194 value = NULL;
196 g_free(value);
199 if (enabled) {
200 g_free(sipe_private->persistentChatPool_uri);
201 sipe_private->persistentChatPool_uri = g_strdup(sipe_get_no_sip_uri(uri));
202 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->persistentChatPool_uri=%s",
203 sipe_private->persistentChatPool_uri ? sipe_private->persistentChatPool_uri : "");
205 g_free(uri);
209 sipe_xml_free(xn_provision_group_list);
211 if (sipe_private->dlx_uri && sipe_private->addressbook_uri) {
212 /* Some buddies might have been added before we received this
213 * provisioning notify with DLX and addressbook URIs. Now we can
214 * trigger an update of their photos. */
215 sipe_buddy_refresh_photos(sipe_private);
218 if (sipe_private->focus_factory_uri) {
219 /* Fill the list of conferencing capabilities enabled on
220 * the server. */
221 sipe_conf_get_capabilities(sipe_private);
224 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
225 /* persistentChatPool_uri has been set at this point */
226 sipe_groupchat_init(sipe_private);
229 static void process_incoming_notify_rlmi_resub(struct sipe_core_private *sipe_private,
230 const gchar *data, unsigned len)
232 sipe_xml *xn_list;
233 const sipe_xml *xn_resource;
234 GHashTable *servers = g_hash_table_new_full(g_str_hash, g_str_equal,
235 g_free, NULL);
237 xn_list = sipe_xml_parse(data, len);
239 for (xn_resource = sipe_xml_child(xn_list, "resource");
240 xn_resource;
241 xn_resource = sipe_xml_twin(xn_resource) )
243 const char *uri, *state;
244 const sipe_xml *xn_instance;
246 xn_instance = sipe_xml_child(xn_resource, "instance");
247 if (!xn_instance) continue;
249 uri = sipe_xml_attribute(xn_resource, "uri");
250 state = sipe_xml_attribute(xn_instance, "state");
251 SIPE_DEBUG_INFO("process_incoming_notify_rlmi_resub: uri(%s),state(%s)", uri, state);
253 if (strstr(state, "resubscribe")) {
254 const char *poolFqdn = sipe_xml_attribute(xn_instance, "poolFqdn");
256 if (poolFqdn) { //[MS-PRES] Section 3.4.5.1.3 Processing Details
257 gchar *user = g_strdup(uri);
258 gchar *host = g_strdup(poolFqdn);
259 GSList *server = g_hash_table_lookup(servers,
260 host);
261 server = g_slist_append(server, user);
262 g_hash_table_insert(servers, host, server);
263 } else {
264 sipe_subscribe_presence_single(sipe_private,
265 uri,
266 uri);
271 /* Send out any deferred poolFqdn subscriptions */
272 g_hash_table_foreach(servers, (GHFunc) sipe_subscribe_poolfqdn_resource_uri, sipe_private);
273 g_hash_table_destroy(servers);
275 sipe_xml_free(xn_list);
279 * Update user phone
280 * Suitable for both 2005 and 2007 systems.
282 * @param uri buddy SIP URI with 'sip:' prefix whose info we want to change.
283 * @param phone_type
284 * @param phone may be modified to strip white space
285 * @param phone_display_string may be modified to strip white space
287 static void
288 sipe_update_user_phone(struct sipe_core_private *sipe_private,
289 const gchar *uri,
290 const gchar *phone_type,
291 gchar *phone,
292 gchar *phone_display_string)
294 sipe_buddy_info_fields phone_node = SIPE_BUDDY_INFO_WORK_PHONE; /* work phone by default */
295 sipe_buddy_info_fields phone_display_node = SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY; /* work phone by default */
297 if(!phone || strlen(phone) == 0) return;
299 if ((sipe_strequal(phone_type, "mobile") || sipe_strequal(phone_type, "cell"))) {
300 phone_node = SIPE_BUDDY_INFO_MOBILE_PHONE;
301 phone_display_node = SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY;
302 } else if (sipe_strequal(phone_type, "home")) {
303 phone_node = SIPE_BUDDY_INFO_HOME_PHONE;
304 phone_display_node = SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY;
305 } else if (sipe_strequal(phone_type, "other")) {
306 phone_node = SIPE_BUDDY_INFO_OTHER_PHONE;
307 phone_display_node = SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY;
308 } else if (sipe_strequal(phone_type, "custom1")) {
309 phone_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE;
310 phone_display_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY;
313 sipe_buddy_update_property(sipe_private, uri, phone_node, phone);
314 if (phone_display_string) {
315 sipe_buddy_update_property(sipe_private, uri, phone_display_node, phone_display_string);
319 static void process_incoming_notify_msrtc(struct sipe_core_private *sipe_private,
320 const gchar *data,
321 unsigned len)
323 char *activity = NULL;
324 const char *epid;
325 const char *status_id = NULL;
326 const char *name;
327 char *uri;
328 char *self_uri = sip_uri_self(sipe_private);
329 int avl;
330 int act;
331 const char *device_name = NULL;
332 const char *cal_start_time = NULL;
333 const char *cal_granularity = NULL;
334 char *cal_free_busy_base64 = NULL;
335 struct sipe_buddy *sbuddy;
336 const sipe_xml *node;
337 sipe_xml *xn_presentity;
338 const sipe_xml *xn_availability;
339 const sipe_xml *xn_activity;
340 const sipe_xml *xn_display_name;
341 const sipe_xml *xn_email;
342 const sipe_xml *xn_phone_number;
343 const sipe_xml *xn_userinfo;
344 const sipe_xml *xn_note;
345 const sipe_xml *xn_oof;
346 const sipe_xml *xn_state;
347 const sipe_xml *xn_contact;
348 char *note;
349 int user_avail;
350 const char *user_avail_nil;
351 int res_avail;
352 time_t user_avail_since = 0;
353 time_t activity_since = 0;
355 /* fix for Reuters environment on Linux */
356 if (data && strstr(data, "encoding=\"utf-16\"")) {
357 char *tmp_data;
358 tmp_data = sipe_utils_str_replace(data, "encoding=\"utf-16\"", "encoding=\"utf-8\"");
359 xn_presentity = sipe_xml_parse(tmp_data, strlen(tmp_data));
360 g_free(tmp_data);
361 } else {
362 xn_presentity = sipe_xml_parse(data, len);
365 xn_availability = sipe_xml_child(xn_presentity, "availability");
366 xn_activity = sipe_xml_child(xn_presentity, "activity");
367 xn_display_name = sipe_xml_child(xn_presentity, "displayName");
368 xn_email = sipe_xml_child(xn_presentity, "email");
369 xn_phone_number = sipe_xml_child(xn_presentity, "phoneNumber");
370 xn_userinfo = sipe_xml_child(xn_presentity, "userInfo");
371 xn_oof = xn_userinfo ? sipe_xml_child(xn_userinfo, "oof") : NULL;
372 xn_state = xn_userinfo ? sipe_xml_child(xn_userinfo, "states/state"): NULL;
373 user_avail = xn_state ? sipe_xml_int_attribute(xn_state, "avail", 0) : 0;
374 user_avail_since = xn_state ? sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since")) : 0;
375 user_avail_nil = xn_state ? sipe_xml_attribute(xn_state, "nil") : NULL;
376 xn_contact = xn_userinfo ? sipe_xml_child(xn_userinfo, "contact") : NULL;
377 xn_note = xn_userinfo ? sipe_xml_child(xn_userinfo, "note") : NULL;
378 note = xn_note ? sipe_xml_data(xn_note) : NULL;
380 if (sipe_strequal(user_avail_nil, "true")) { /* null-ed */
381 user_avail = 0;
382 user_avail_since = 0;
385 name = sipe_xml_attribute(xn_presentity, "uri"); /* without 'sip:' prefix */
386 uri = sip_uri_from_name(name);
387 avl = sipe_xml_int_attribute(xn_availability, "aggregate", 0);
388 epid = sipe_xml_attribute(xn_availability, "epid");
389 act = sipe_xml_int_attribute(xn_activity, "aggregate", 0);
391 status_id = sipe_ocs2005_status_from_activity_availability(act, avl);
392 activity = g_strdup(sipe_ocs2005_activity_description(act));
393 res_avail = sipe_ocs2007_availability_from_status(status_id, NULL);
394 if (user_avail > res_avail) {
395 res_avail = user_avail;
396 status_id = sipe_ocs2007_status_from_legacy_availability(user_avail, NULL);
399 if (xn_display_name) {
400 char *display_name = g_strdup(sipe_xml_attribute(xn_display_name, "displayName"));
401 char *email = xn_email ? g_strdup(sipe_xml_attribute(xn_email, "email")) : NULL;
402 char *phone_label = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "label")) : NULL;
403 char *phone_number = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "number")) : NULL;
404 char *tel_uri = sip_to_tel_uri(phone_number);
406 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
407 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
408 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
409 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, !is_empty(phone_label) ? phone_label : phone_number);
411 g_free(tel_uri);
412 g_free(phone_label);
413 g_free(phone_number);
414 g_free(email);
415 g_free(display_name);
418 if (xn_contact) {
419 /* tel */
420 for (node = sipe_xml_child(xn_contact, "tel"); node; node = sipe_xml_twin(node))
422 /* Ex.: <tel type="work">tel:+3222220000</tel> */
423 const char *phone_type = sipe_xml_attribute(node, "type");
424 char* phone = sipe_xml_data(node);
426 sipe_update_user_phone(sipe_private, uri, phone_type, phone, NULL);
428 g_free(phone);
432 if (xn_display_name || xn_contact)
433 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC, uri);
435 /* devicePresence */
436 for (node = sipe_xml_child(xn_presentity, "devices/devicePresence"); node; node = sipe_xml_twin(node)) {
437 const sipe_xml *xn_device_name;
438 const sipe_xml *xn_calendar_info;
439 const sipe_xml *xn_state;
440 char *state;
442 /* deviceName */
443 if (sipe_strequal(sipe_xml_attribute(node, "epid"), epid)) {
444 xn_device_name = sipe_xml_child(node, "deviceName");
445 device_name = xn_device_name ? sipe_xml_attribute(xn_device_name, "name") : NULL;
448 /* calendarInfo */
449 xn_calendar_info = sipe_xml_child(node, "calendarInfo");
450 if (xn_calendar_info) {
451 const char *cal_start_time_tmp = sipe_xml_attribute(xn_calendar_info, "startTime");
453 if (cal_start_time) {
454 time_t cal_start_time_t = sipe_utils_str_to_time(cal_start_time);
455 time_t cal_start_time_t_tmp = sipe_utils_str_to_time(cal_start_time_tmp);
457 if (cal_start_time_t_tmp > cal_start_time_t) {
458 cal_start_time = cal_start_time_tmp;
459 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
460 g_free(cal_free_busy_base64);
461 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
463 SIPE_DEBUG_INFO("process_incoming_notify_msrtc: startTime=%s granularity=%s cal_free_busy_base64=\n%s", cal_start_time, cal_granularity, cal_free_busy_base64);
465 } else {
466 cal_start_time = cal_start_time_tmp;
467 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
468 g_free(cal_free_busy_base64);
469 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
471 SIPE_DEBUG_INFO("process_incoming_notify_msrtc: startTime=%s granularity=%s cal_free_busy_base64=\n%s", cal_start_time, cal_granularity, cal_free_busy_base64);
475 /* state */
476 xn_state = sipe_xml_child(node, "states/state");
477 if (xn_state) {
478 int dev_avail = sipe_xml_int_attribute(xn_state, "avail", 0);
479 time_t dev_avail_since = sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since"));
481 state = sipe_xml_data(xn_state);
482 if (dev_avail_since > user_avail_since &&
483 dev_avail >= res_avail)
485 const gchar *new_desc;
486 res_avail = dev_avail;
487 if (!is_empty(state)) {
488 if (sipe_strequal(state, sipe_status_activity_to_token(SIPE_ACTIVITY_ON_PHONE))) {
489 g_free(activity);
490 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_ON_PHONE));
491 } else if (sipe_strequal(state, "presenting")) {
492 g_free(activity);
493 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_IN_CONF));
494 } else {
495 activity = state;
496 state = NULL;
498 activity_since = dev_avail_since;
500 status_id = sipe_ocs2007_status_from_legacy_availability(res_avail, NULL);
501 new_desc = sipe_ocs2007_legacy_activity_description(res_avail);
502 if (new_desc) {
503 g_free(activity);
504 activity = g_strdup(new_desc);
507 g_free(state);
511 /* oof */
512 if (xn_oof && res_avail >= 15000) { /* 12000 in 2007 */
513 g_free(activity);
514 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_OOF));
515 activity_since = 0;
518 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
519 if (sbuddy)
521 g_free(sbuddy->activity);
522 sbuddy->activity = activity;
523 activity = NULL;
525 sbuddy->activity_since = activity_since;
527 sbuddy->user_avail = user_avail;
528 sbuddy->user_avail_since = user_avail_since;
530 g_free(sbuddy->note);
531 sbuddy->note = NULL;
532 if (!is_empty(note)) { sbuddy->note = g_markup_escape_text(note, -1); }
534 sbuddy->is_oof_note = (xn_oof != NULL);
536 g_free(sbuddy->device_name);
537 sbuddy->device_name = NULL;
538 if (!is_empty(device_name)) { sbuddy->device_name = g_strdup(device_name); }
540 if (!is_empty(cal_free_busy_base64)) {
541 g_free(sbuddy->cal_start_time);
542 sbuddy->cal_start_time = g_strdup(cal_start_time);
544 sbuddy->cal_granularity = sipe_strcase_equal(cal_granularity, "PT15M") ? 15 : 0;
546 g_free(sbuddy->cal_free_busy_base64);
547 sbuddy->cal_free_busy_base64 = cal_free_busy_base64;
548 cal_free_busy_base64 = NULL;
550 g_free(sbuddy->cal_free_busy);
551 sbuddy->cal_free_busy = NULL;
554 sbuddy->last_non_cal_status_id = status_id;
555 g_free(sbuddy->last_non_cal_activity);
556 sbuddy->last_non_cal_activity = g_strdup(sbuddy->activity);
558 if (sipe_strcase_equal(sbuddy->name, self_uri)) {
559 if (!sipe_strequal(sbuddy->note, sipe_private->note)) /* not same */
561 if (sbuddy->is_oof_note)
562 SIPE_CORE_PRIVATE_FLAG_SET(OOF_NOTE);
563 else
564 SIPE_CORE_PRIVATE_FLAG_UNSET(OOF_NOTE);
566 g_free(sipe_private->note);
567 sipe_private->note = g_strdup(sbuddy->note);
569 sipe_private->note_since = time(NULL);
572 sipe_status_set_token(sipe_private,
573 sbuddy->last_non_cal_status_id);
576 g_free(cal_free_busy_base64);
577 g_free(activity);
579 SIPE_DEBUG_INFO("process_incoming_notify_msrtc: status(%s)", status_id);
580 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
581 sipe_status_token_to_activity(status_id));
583 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007) && sipe_strcase_equal(self_uri, uri)) {
584 sipe_ocs2005_user_info_has_updated(sipe_private, xn_userinfo);
587 g_free(note);
588 sipe_xml_free(xn_presentity);
589 g_free(uri);
590 g_free(self_uri);
593 static void process_incoming_notify_rlmi(struct sipe_core_private *sipe_private,
594 const gchar *data,
595 unsigned len)
597 const char *uri;
598 struct sipe_buddy *sbuddy = NULL;
599 sipe_xml *xn_categories;
600 const sipe_xml *xn_category;
601 const char *status = NULL;
602 gboolean do_update_status = FALSE;
603 gboolean has_note_cleaned = FALSE;
604 gboolean has_free_busy_cleaned = FALSE;
606 xn_categories = sipe_xml_parse(data, len);
607 uri = sipe_xml_attribute(xn_categories, "uri"); /* with 'sip:' prefix */
608 if (uri) {
609 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
612 if (!sbuddy) {
613 /* Got presence of a buddy not in our contact list, ignore. */
614 sipe_xml_free(xn_categories);
615 return;
618 for (xn_category = sipe_xml_child(xn_categories, "category");
619 xn_category ;
620 xn_category = sipe_xml_twin(xn_category) )
622 const sipe_xml *xn_node;
623 const char *tmp;
624 const char *attrVar = sipe_xml_attribute(xn_category, "name");
625 time_t publish_time = (tmp = sipe_xml_attribute(xn_category, "publishTime")) ?
626 sipe_utils_str_to_time(tmp) : 0;
628 /* contactCard */
629 if (sipe_strequal(attrVar, "contactCard"))
631 const sipe_xml *card = sipe_xml_child(xn_category, "contactCard");
633 if (card) {
634 const sipe_xml *node;
635 /* identity - Display Name and email */
636 node = sipe_xml_child(card, "identity");
637 if (node) {
638 char* display_name = sipe_xml_data(
639 sipe_xml_child(node, "name/displayName"));
640 char* email = sipe_xml_data(
641 sipe_xml_child(node, "email"));
643 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
644 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
646 g_free(display_name);
647 g_free(email);
649 /* company */
650 node = sipe_xml_child(card, "company");
651 if (node) {
652 char* company = sipe_xml_data(node);
653 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COMPANY, company);
654 g_free(company);
656 /* department */
657 node = sipe_xml_child(card, "department");
658 if (node) {
659 char* department = sipe_xml_data(node);
660 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DEPARTMENT, department);
661 g_free(department);
663 /* title */
664 node = sipe_xml_child(card, "title");
665 if (node) {
666 char* title = sipe_xml_data(node);
667 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_JOB_TITLE, title);
668 g_free(title);
670 /* office */
671 node = sipe_xml_child(card, "office");
672 if (node) {
673 char* office = sipe_xml_data(node);
674 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_OFFICE, office);
675 g_free(office);
677 /* site (url) */
678 node = sipe_xml_child(card, "url");
679 if (node) {
680 char* site = sipe_xml_data(node);
681 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_SITE, site);
682 g_free(site);
684 /* phone */
685 for (node = sipe_xml_child(card, "phone");
686 node;
687 node = sipe_xml_twin(node))
689 const char *phone_type = sipe_xml_attribute(node, "type");
690 char* phone = sipe_xml_data(sipe_xml_child(node, "uri"));
691 char* phone_display_string = sipe_xml_data(sipe_xml_child(node, "displayString"));
693 sipe_update_user_phone(sipe_private, uri, phone_type, phone, phone_display_string);
695 g_free(phone);
696 g_free(phone_display_string);
698 /* address */
699 for (node = sipe_xml_child(card, "address");
700 node;
701 node = sipe_xml_twin(node))
703 if (sipe_strequal(sipe_xml_attribute(node, "type"), "work")) {
704 char* street = sipe_xml_data(sipe_xml_child(node, "street"));
705 char* city = sipe_xml_data(sipe_xml_child(node, "city"));
706 char* state = sipe_xml_data(sipe_xml_child(node, "state"));
707 char* zipcode = sipe_xml_data(sipe_xml_child(node, "zipcode"));
708 char* country_code = sipe_xml_data(sipe_xml_child(node, "countryCode"));
710 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STREET, street);
711 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_CITY, city);
712 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STATE, state);
713 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_ZIPCODE, zipcode);
714 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COUNTRY, country_code);
716 g_free(street);
717 g_free(city);
718 g_free(state);
719 g_free(zipcode);
720 g_free(country_code);
722 break;
725 /* photo */
726 for (node = sipe_xml_child(card, "photo");
727 node;
728 node = sipe_xml_twin(node)) {
729 gchar *photo_url = sipe_xml_data(sipe_xml_child(node, "uri"));
730 gchar *hash = sipe_xml_data(sipe_xml_child(node, "hash"));
731 gboolean found = FALSE;
733 if (!is_empty(photo_url) && !is_empty(hash)) {
734 sipe_buddy_update_photo(sipe_private,
735 uri,
736 hash,
737 photo_url,
738 NULL);
739 found = TRUE;
742 g_free(hash);
743 g_free(photo_url);
745 if (found)
746 break;
750 /* note */
751 else if (sipe_strequal(attrVar, "note"))
753 if (!has_note_cleaned) {
754 has_note_cleaned = TRUE;
756 g_free(sbuddy->note);
757 sbuddy->note = NULL;
758 sbuddy->is_oof_note = FALSE;
759 sbuddy->note_since = publish_time;
761 do_update_status = TRUE;
763 if (publish_time >= sbuddy->note_since) {
764 /* clean up in case no 'note' element is supplied
765 * which indicate note removal in client
767 g_free(sbuddy->note);
768 sbuddy->note = NULL;
769 sbuddy->is_oof_note = FALSE;
770 sbuddy->note_since = publish_time;
772 xn_node = sipe_xml_child(xn_category, "note/body");
773 if (xn_node) {
774 char *tmp;
775 sbuddy->note = g_markup_escape_text((tmp = sipe_xml_data(xn_node)), -1);
776 g_free(tmp);
777 sbuddy->is_oof_note = sipe_strequal(sipe_xml_attribute(xn_node, "type"), "OOF");
778 sbuddy->note_since = publish_time;
780 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: uri(%s), note(%s)",
781 uri, sbuddy->note ? sbuddy->note : "");
783 /* to trigger UI refresh in case no status info is supplied in this update */
784 do_update_status = TRUE;
787 /* state */
788 else if(sipe_strequal(attrVar, "state"))
790 char *tmp;
791 int availability;
792 const sipe_xml *xn_availability;
793 const sipe_xml *xn_activity;
794 const sipe_xml *xn_device;
795 const sipe_xml *xn_meeting_subject;
796 const sipe_xml *xn_meeting_location;
797 const gchar *legacy_activity;
799 xn_node = sipe_xml_child(xn_category, "state");
800 if (!xn_node) continue;
801 xn_availability = sipe_xml_child(xn_node, "availability");
802 if (!xn_availability) continue;
803 xn_activity = sipe_xml_child(xn_node, "activity");
804 xn_meeting_subject = sipe_xml_child(xn_node, "meetingSubject");
805 xn_meeting_location = sipe_xml_child(xn_node, "meetingLocation");
807 tmp = sipe_xml_data(xn_availability);
808 availability = atoi(tmp);
809 g_free(tmp);
811 sbuddy->is_mobile = FALSE;
812 xn_device = sipe_xml_child(xn_node, "device");
813 if (xn_device) {
814 tmp = sipe_xml_data(xn_device);
815 sbuddy->is_mobile = !g_ascii_strcasecmp(tmp, "Mobile");
816 g_free(tmp);
819 /* activity */
820 g_free(sbuddy->activity);
821 sbuddy->activity = NULL;
822 if (xn_activity) {
823 const char *token = sipe_xml_attribute(xn_activity, "token");
824 const sipe_xml *xn_custom = sipe_xml_child(xn_activity, "custom");
826 /* from token */
827 if (!is_empty(token)) {
828 sbuddy->activity = g_strdup(sipe_core_activity_description(sipe_status_token_to_activity(token)));
830 /* from custom element */
831 if (xn_custom) {
832 char *custom = sipe_xml_data(xn_custom);
834 if (!is_empty(custom)) {
835 g_free(sbuddy->activity);
836 sbuddy->activity = custom;
837 custom = NULL;
839 g_free(custom);
842 /* meeting_subject */
843 g_free(sbuddy->meeting_subject);
844 sbuddy->meeting_subject = NULL;
845 if (xn_meeting_subject) {
846 char *meeting_subject = sipe_xml_data(xn_meeting_subject);
848 if (!is_empty(meeting_subject)) {
849 sbuddy->meeting_subject = meeting_subject;
850 meeting_subject = NULL;
852 g_free(meeting_subject);
854 /* meeting_location */
855 g_free(sbuddy->meeting_location);
856 sbuddy->meeting_location = NULL;
857 if (xn_meeting_location) {
858 char *meeting_location = sipe_xml_data(xn_meeting_location);
860 if (!is_empty(meeting_location)) {
861 sbuddy->meeting_location = meeting_location;
862 meeting_location = NULL;
864 g_free(meeting_location);
867 status = sipe_ocs2007_status_from_legacy_availability(availability, NULL);
868 legacy_activity = sipe_ocs2007_legacy_activity_description(availability);
869 if (sbuddy->activity && legacy_activity) {
870 gchar *tmp2 = sbuddy->activity;
872 sbuddy->activity = g_strdup_printf("%s, %s", sbuddy->activity, legacy_activity);
873 g_free(tmp2);
874 } else if (legacy_activity) {
875 sbuddy->activity = g_strdup(legacy_activity);
878 do_update_status = TRUE;
880 /* calendarData */
881 else if(sipe_strequal(attrVar, "calendarData"))
883 const sipe_xml *xn_free_busy = sipe_xml_child(xn_category, "calendarData/freeBusy");
884 const sipe_xml *xn_working_hours = sipe_xml_child(xn_category, "calendarData/WorkingHours");
886 if (xn_free_busy) {
887 if (!has_free_busy_cleaned) {
888 has_free_busy_cleaned = TRUE;
890 g_free(sbuddy->cal_start_time);
891 sbuddy->cal_start_time = NULL;
893 g_free(sbuddy->cal_free_busy_base64);
894 sbuddy->cal_free_busy_base64 = NULL;
896 g_free(sbuddy->cal_free_busy);
897 sbuddy->cal_free_busy = NULL;
899 sbuddy->cal_free_busy_published = publish_time;
902 if (publish_time >= sbuddy->cal_free_busy_published) {
903 g_free(sbuddy->cal_start_time);
904 sbuddy->cal_start_time = g_strdup(sipe_xml_attribute(xn_free_busy, "startTime"));
906 sbuddy->cal_granularity = sipe_strcase_equal(sipe_xml_attribute(xn_free_busy, "granularity"), "PT15M") ?
907 15 : 0;
909 g_free(sbuddy->cal_free_busy_base64);
910 sbuddy->cal_free_busy_base64 = sipe_xml_data(xn_free_busy);
912 g_free(sbuddy->cal_free_busy);
913 sbuddy->cal_free_busy = NULL;
915 sbuddy->cal_free_busy_published = publish_time;
917 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: startTime=%s granularity=%d cal_free_busy_base64=\n%s", sbuddy->cal_start_time, sbuddy->cal_granularity, sbuddy->cal_free_busy_base64);
921 if (xn_working_hours) {
922 sipe_cal_parse_working_hours(xn_working_hours, sbuddy);
927 if (do_update_status) {
928 guint activity;
930 if (status) {
931 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: %s", status);
932 activity = sipe_status_token_to_activity(status);
933 } else {
934 /* no status category in this update,
935 using contact's current status */
936 activity = sipe_backend_buddy_get_status(SIPE_CORE_PUBLIC,
937 uri);
940 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri, activity);
943 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC, uri);
945 sipe_xml_free(xn_categories);
948 static void sipe_buddy_status_from_activity(struct sipe_core_private *sipe_private,
949 const gchar *uri,
950 const gchar *activity,
951 gboolean is_online)
953 if (is_online) {
954 const gchar *status_id = NULL;
955 if (activity) {
956 if (sipe_strequal(activity,
957 sipe_status_activity_to_token(SIPE_ACTIVITY_BUSY))) {
958 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_BUSY);
959 } else if (sipe_strequal(activity,
960 sipe_status_activity_to_token(SIPE_ACTIVITY_AWAY))) {
961 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_AWAY);
965 if (!status_id) {
966 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_AVAILABLE);
969 SIPE_DEBUG_INFO("sipe_buddy_status_from_activity: status_id(%s)", status_id);
970 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
971 sipe_status_token_to_activity(status_id));
972 } else {
973 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
974 SIPE_ACTIVITY_OFFLINE);
978 static void process_incoming_notify_pidf(struct sipe_core_private *sipe_private,
979 const gchar *data,
980 unsigned len)
982 gchar *uri;
983 gchar *getbasic;
984 gchar *activity = NULL;
985 sipe_xml *pidf;
986 const sipe_xml *basicstatus = NULL, *tuple, *status;
987 gboolean isonline = FALSE;
988 const sipe_xml *display_name_node;
990 pidf = sipe_xml_parse(data, len);
991 if (!pidf) {
992 SIPE_DEBUG_INFO("process_incoming_notify_pidf: no parseable pidf:%s", data);
993 return;
996 if ((tuple = sipe_xml_child(pidf, "tuple")))
998 if ((status = sipe_xml_child(tuple, "status"))) {
999 basicstatus = sipe_xml_child(status, "basic");
1003 if (!basicstatus) {
1004 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic found");
1005 sipe_xml_free(pidf);
1006 return;
1009 getbasic = sipe_xml_data(basicstatus);
1010 if (!getbasic) {
1011 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic data found");
1012 sipe_xml_free(pidf);
1013 return;
1016 SIPE_DEBUG_INFO("process_incoming_notify_pidf: basic-status(%s)", getbasic);
1017 if (strstr(getbasic, "open")) {
1018 isonline = TRUE;
1020 g_free(getbasic);
1022 uri = sip_uri(sipe_xml_attribute(pidf, "entity")); /* with 'sip:' prefix */ /* AOL comes without the prefix */
1024 display_name_node = sipe_xml_child(pidf, "display-name");
1025 if (display_name_node) {
1026 char * display_name = sipe_xml_data(display_name_node);
1028 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
1029 g_free(display_name);
1031 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC, uri);
1034 if ((tuple = sipe_xml_child(pidf, "tuple"))) {
1035 if ((status = sipe_xml_child(tuple, "status"))) {
1036 if ((basicstatus = sipe_xml_child(status, "activities"))) {
1037 if ((basicstatus = sipe_xml_child(basicstatus, "activity"))) {
1038 activity = sipe_xml_data(basicstatus);
1039 SIPE_DEBUG_INFO("process_incoming_notify_pidf: activity(%s)", activity);
1045 sipe_buddy_status_from_activity(sipe_private,
1046 uri,
1047 activity,
1048 isonline);
1050 g_free(activity);
1051 g_free(uri);
1052 sipe_xml_free(pidf);
1055 static void sipe_presence_mime_cb(gpointer user_data, /* sipe_core_private */
1056 const GSList *fields,
1057 const gchar *body,
1058 gsize length)
1060 const gchar *type = sipe_utils_nameval_find(fields, "Content-Type");
1062 if (strstr(type,"application/rlmi+xml")) {
1063 process_incoming_notify_rlmi_resub(user_data, body, length);
1064 } else if (strstr(type, "text/xml+msrtc.pidf")) {
1065 process_incoming_notify_msrtc(user_data, body, length);
1066 } else {
1067 process_incoming_notify_rlmi(user_data, body, length);
1071 static void sipe_process_presence(struct sipe_core_private *sipe_private,
1072 struct sipmsg *msg)
1074 const char *ctype = sipmsg_find_header(msg, "Content-Type");
1076 SIPE_DEBUG_INFO("sipe_process_presence: Content-Type: %s", ctype ? ctype : "");
1078 if (ctype &&
1079 (strstr(ctype, "application/rlmi+xml") ||
1080 strstr(ctype, "application/msrtc-event-categories+xml")))
1082 if (strstr(ctype, "multipart"))
1084 sipe_mime_parts_foreach(ctype, msg->body, sipe_presence_mime_cb, sipe_private);
1086 else if(strstr(ctype, "application/msrtc-event-categories+xml") )
1088 process_incoming_notify_rlmi(sipe_private, msg->body, msg->bodylen);
1090 else if(strstr(ctype, "application/rlmi+xml"))
1092 process_incoming_notify_rlmi_resub(sipe_private, msg->body, msg->bodylen);
1095 else if(ctype && strstr(ctype, "text/xml+msrtc.pidf"))
1097 process_incoming_notify_msrtc(sipe_private, msg->body, msg->bodylen);
1099 else
1101 process_incoming_notify_pidf(sipe_private, msg->body, msg->bodylen);
1106 * Fires on deregistration event initiated by server.
1107 * [MS-SIPREGE] SIP extension.
1109 * OCS2007 Example
1111 * Content-Type: text/registration-event
1112 * subscription-state: terminated;expires=0
1113 * ms-diagnostics-public: 4141;reason="User disabled"
1115 * deregistered;event=rejected
1117 static void sipe_process_registration_notify(struct sipe_core_private *sipe_private,
1118 struct sipmsg *msg)
1120 const gchar *contenttype = sipmsg_find_header(msg, "Content-Type");
1121 gchar *event = NULL;
1122 gchar *reason = NULL;
1123 gchar *warning;
1125 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: deregistration received.");
1127 if (!g_ascii_strncasecmp(contenttype, "text/registration-event", 23)) {
1128 event = sipmsg_find_part_of_header(msg->body, "event=", NULL, NULL);
1129 //@TODO have proper parameter extraction _by_name_ func, case insesitive.
1130 event = event ? event : sipmsg_find_part_of_header(msg->body, "event=", ";", NULL);
1131 } else {
1132 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: unknown content type, exiting.");
1133 return;
1136 reason = sipmsg_get_ms_diagnostics_reason(msg);
1137 reason = reason ? reason : sipmsg_get_ms_diagnostics_public_reason(msg);
1138 if (!reason) { // for LCS2005
1139 if (event && sipe_strcase_equal(event, "unregistered")) {
1140 //reason = g_strdup(_("User logged out")); // [MS-OCER]
1141 reason = g_strdup(_("you are already signed in at another location"));
1142 } else if (event && sipe_strcase_equal(event, "rejected")) {
1143 reason = g_strdup(_("user disabled")); // [MS-OCER]
1144 } else if (event && sipe_strcase_equal(event, "deactivated")) {
1145 reason = g_strdup(_("user moved")); // [MS-OCER]
1148 g_free(event);
1149 warning = g_strdup_printf(_("You have been rejected by the server: %s"), reason ? reason : _("no reason given"));
1150 g_free(reason);
1152 sipe_backend_connection_error(SIPE_CORE_PUBLIC,
1153 SIPE_CONNECTION_ERROR_INVALID_USERNAME,
1154 warning);
1155 g_free(warning);
1159 /* Replace "~" with localized version of "Other Contacts" */
1160 static const gchar *get_group_name(const sipe_xml *node)
1162 const gchar *name = sipe_xml_attribute(node, "name");
1163 return(g_str_has_prefix(name, "~") ? _("Other Contacts") : name);
1166 static void add_new_group(struct sipe_core_private *sipe_private,
1167 const sipe_xml *node)
1169 sipe_group_add(sipe_private,
1170 get_group_name(node),
1171 NULL,
1172 NULL,
1173 sipe_xml_int_attribute(node, "id", 0));
1176 static void add_new_buddy(struct sipe_core_private *sipe_private,
1177 const sipe_xml *node,
1178 const gchar *uri)
1180 const gchar *name = sipe_xml_attribute(node, "name");
1181 struct sipe_buddy *buddy = NULL;
1182 gchar *tmp;
1183 gchar **item_groups;
1184 int i = 0;
1186 /* "name" attribute is a contact alias which user can manually assign by
1187 * renaming the item in the contact list. Empty string means no alias
1188 * and the display name from the contact card should be used instead. */
1189 if (name && strlen(name) == 0) {
1190 name = NULL;
1193 /* assign to group Other Contacts if nothing else received */
1194 tmp = g_strdup(sipe_xml_attribute(node, "groups"));
1195 if (is_empty(tmp)) {
1196 struct sipe_group *group = sipe_group_find_by_name(sipe_private,
1197 _("Other Contacts"));
1198 g_free(tmp);
1199 tmp = group ? g_strdup_printf("%d", group->id) : g_strdup("1");
1201 item_groups = g_strsplit(tmp, " ", 0);
1202 g_free(tmp);
1204 while (item_groups[i]) {
1205 struct sipe_group *group = sipe_group_find_by_id(sipe_private,
1206 g_ascii_strtod(item_groups[i],
1207 NULL));
1209 /* If couldn't find the right group for this contact, */
1210 /* then just put it in the first group we have */
1211 if (!group)
1212 group = sipe_group_first(sipe_private);
1214 if (group) {
1215 if (!buddy)
1216 buddy = sipe_buddy_add(sipe_private,
1217 uri,
1218 NULL,
1219 NULL);
1221 sipe_buddy_add_to_group(sipe_private,
1222 buddy,
1223 group,
1224 name);
1225 } else {
1226 SIPE_DEBUG_INFO("No group found for contact %s! Unable to add to buddy list",
1227 uri);
1230 i++;
1233 g_strfreev(item_groups);
1236 static gboolean sipe_process_roaming_contacts(struct sipe_core_private *sipe_private,
1237 struct sipmsg *msg)
1239 int len = msg->bodylen;
1241 const gchar *tmp = sipmsg_find_header(msg, "Event");
1242 const sipe_xml *item;
1243 sipe_xml *isc;
1244 guint delta;
1245 const sipe_xml *group_node;
1247 if (!g_str_has_prefix(tmp, "vnd-microsoft-roaming-contacts")) {
1248 return FALSE;
1251 /* Convert the contact from XML to backend Buddies */
1252 isc = sipe_xml_parse(msg->body, len);
1253 if (!isc) {
1254 return FALSE;
1257 /* [MS-SIP]: deltaNum MUST be non-zero */
1258 delta = sipe_xml_int_attribute(isc, "deltaNum", 0);
1259 if (delta) {
1260 sipe_private->deltanum_contacts = delta;
1264 * Process whole buddy list
1266 * - Only sent once
1267 * * up to Lync 2010
1268 * * Lync 2013 (and later) with buddy list not migrated
1270 * - Lync 2013 with buddy list migrated to Unified Contact Store (UCS)
1271 * * Notify piggy-backed on SUBSCRIBE response with empty list
1272 * * NOTIFY send by server with standard list (ignored by us)
1274 if (sipe_strequal(sipe_xml_name(isc), "contactList")) {
1275 const gchar *ucsmode = sipe_xml_attribute(isc, "ucsmode");
1277 SIPE_CORE_PRIVATE_FLAG_UNSET(LYNC2013);
1278 if (ucsmode) {
1279 gboolean migrated = sipe_strcase_equal(ucsmode,
1280 "migrated");
1281 SIPE_CORE_PRIVATE_FLAG_SET(LYNC2013);
1282 SIPE_LOG_INFO_NOFORMAT("sipe_process_roaming_contacts: contact list contains 'ucsmode' attribute (indicates Lync 2013+)");
1284 if (migrated)
1285 SIPE_LOG_INFO_NOFORMAT("sipe_process_roaming_contacts: contact list has been migrated to Unified Contact Store (UCS)");
1286 sipe_ucs_init(sipe_private, migrated);
1289 if (!sipe_ucs_is_migrated(sipe_private)) {
1290 /* Start processing contact list */
1291 sipe_backend_buddy_list_processing_start(SIPE_CORE_PUBLIC);
1293 /* Parse groups */
1294 for (group_node = sipe_xml_child(isc, "group"); group_node; group_node = sipe_xml_twin(group_node))
1295 add_new_group(sipe_private, group_node);
1297 /* Make sure we have at least one group */
1298 if (sipe_group_count(sipe_private) == 0) {
1299 sipe_group_create(sipe_private,
1300 NULL,
1301 _("Other Contacts"),
1302 NULL);
1305 /* Parse contacts */
1306 for (item = sipe_xml_child(isc, "contact"); item; item = sipe_xml_twin(item)) {
1307 const gchar *name = sipe_xml_attribute(item, "uri");
1308 gchar *uri = sip_uri_from_name(name);
1309 add_new_buddy(sipe_private, item, uri);
1310 g_free(uri);
1313 sipe_buddy_cleanup_local_list(sipe_private);
1315 /* Add self-contact if not there yet. 2005 systems. */
1316 /* This will resemble subscription to roaming_self in 2007 systems */
1317 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1318 gchar *self_uri = sip_uri_self(sipe_private);
1319 sipe_buddy_add(sipe_private,
1320 self_uri,
1321 NULL,
1322 NULL);
1323 g_free(self_uri);
1326 /* Finished processing contact list */
1327 sipe_backend_buddy_list_processing_finish(SIPE_CORE_PUBLIC);
1330 /* Process buddy list updates */
1331 } else if (sipe_strequal(sipe_xml_name(isc), "contactDelta")) {
1333 /* Process new groups */
1334 for (group_node = sipe_xml_child(isc, "addedGroup"); group_node; group_node = sipe_xml_twin(group_node))
1335 add_new_group(sipe_private, group_node);
1337 /* Process modified groups */
1338 for (group_node = sipe_xml_child(isc, "modifiedGroup"); group_node; group_node = sipe_xml_twin(group_node)) {
1339 struct sipe_group *group = sipe_group_find_by_id(sipe_private,
1340 (int)g_ascii_strtod(sipe_xml_attribute(group_node, "id"),
1341 NULL));
1342 if (group) {
1343 const gchar *name = get_group_name(group_node);
1345 if (!(is_empty(name) ||
1346 sipe_strequal(group->name, name)) &&
1347 sipe_group_rename(sipe_private,
1348 group,
1349 name))
1350 SIPE_DEBUG_INFO("Replaced group %d name with %s", group->id, name);
1354 /* Process new buddies */
1355 for (item = sipe_xml_child(isc, "addedContact"); item; item = sipe_xml_twin(item)) {
1356 add_new_buddy(sipe_private,
1357 item,
1358 sipe_xml_attribute(item, "uri"));
1361 /* Process modified buddies */
1362 for (item = sipe_xml_child(isc, "modifiedContact"); item; item = sipe_xml_twin(item)) {
1363 const gchar *uri = sipe_xml_attribute(item, "uri");
1364 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
1365 uri);
1367 if (buddy) {
1368 gchar **item_groups = g_strsplit(sipe_xml_attribute(item,
1369 "groups"),
1370 " ", 0);
1372 /* this should be defined. Otherwise we would get "deletedContact" */
1373 if (item_groups) {
1374 const gchar *name = sipe_xml_attribute(item, "name");
1375 gboolean empty_name = is_empty(name);
1376 GSList *found = NULL;
1377 int i = 0;
1379 while (item_groups[i]) {
1380 struct sipe_group *group = sipe_group_find_by_id(sipe_private,
1381 g_ascii_strtod(item_groups[i],
1382 NULL));
1383 /* ignore unkown groups */
1384 if (group) {
1385 sipe_backend_buddy b = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
1386 uri,
1387 group->name);
1389 /* add group to found list */
1390 found = g_slist_prepend(found, group);
1392 if (b) {
1393 /* new alias? */
1394 gchar *b_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
1397 if (!(empty_name ||
1398 sipe_strequal(b_alias, name))) {
1399 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
1401 name);
1402 SIPE_DEBUG_INFO("Replaced for buddy %s in group '%s' old alias '%s' with '%s'",
1403 uri, group->name, b_alias, name);
1405 g_free(b_alias);
1407 } else {
1408 const gchar *alias = empty_name ? uri : name;
1409 /* buddy was not in this group */
1410 sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
1411 uri,
1412 alias,
1413 group->name);
1414 sipe_buddy_insert_group(buddy, group);
1415 SIPE_DEBUG_INFO("Added buddy %s (alias '%s' to group '%s'",
1416 uri, alias, group->name);
1420 /* next group */
1421 i++;
1423 g_strfreev(item_groups);
1425 /* removed from groups? */
1426 sipe_buddy_update_groups(sipe_private,
1427 buddy,
1428 found);
1429 g_slist_free(found);
1434 /* Process deleted buddies */
1435 for (item = sipe_xml_child(isc, "deletedContact"); item; item = sipe_xml_twin(item)) {
1436 const gchar *uri = sipe_xml_attribute(item, "uri");
1437 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
1438 uri);
1440 if (buddy) {
1441 SIPE_DEBUG_INFO("Removing buddy %s", uri);
1442 sipe_buddy_remove(sipe_private, buddy);
1446 /* Process deleted groups
1448 * NOTE: all buddies will already have been removed from the
1449 * group prior to this. The log shows that OCS actually
1450 * sends two separate updates when you delete a group:
1452 * - first one with "modifiedContact" removing buddies
1453 * from the group, leaving it empty, and
1455 * - then one with "deletedGroup" removing the group
1457 for (group_node = sipe_xml_child(isc, "deletedGroup"); group_node; group_node = sipe_xml_twin(group_node))
1458 sipe_group_remove(sipe_private,
1459 sipe_group_find_by_id(sipe_private,
1460 (int)g_ascii_strtod(sipe_xml_attribute(group_node, "id"),
1461 NULL)));
1464 sipe_xml_free(isc);
1466 /* Subscribe to buddies, if contact list not migrated to UCS */
1467 if (!sipe_ucs_is_migrated(sipe_private))
1468 sipe_subscribe_presence_initial(sipe_private);
1470 /* for 2005 systems schedule contacts' status update
1471 * based on their calendar information
1473 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1474 sipe_ocs2005_schedule_status_update(sipe_private, time(NULL));
1477 return 0;
1480 static void sipe_process_roaming_acl(struct sipe_core_private *sipe_private,
1481 struct sipmsg *msg)
1483 guint delta;
1484 sipe_xml *xml;
1486 xml = sipe_xml_parse(msg->body, msg->bodylen);
1487 if (!xml)
1488 return;
1490 /* [MS-SIP]: deltaNum MUST be non-zero */
1491 delta = sipe_xml_int_attribute(xml, "deltaNum", 0);
1492 if (delta) {
1493 sipe_private->deltanum_acl = delta;
1496 sipe_xml_free(xml);
1499 struct sipe_auth_job {
1500 gchar *who;
1501 struct sipe_core_private *sipe_private;
1504 void sipe_core_contact_allow_deny(struct sipe_core_public *sipe_public,
1505 const gchar* who,
1506 gboolean allow)
1508 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1510 if (allow) {
1511 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: authorizing contact %s", who);
1512 } else {
1513 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: blocking contact %s", who);
1516 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1517 sipe_ocs2007_change_access_level(sipe_private,
1518 (allow ? -1 : 32000),
1519 "user",
1520 sipe_get_no_sip_uri(who));
1521 } else {
1522 sip_soap_ocs2005_setacl(sipe_private, who, allow);
1527 static void sipe_auth_user_cb(gpointer data)
1529 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1530 if (!job) return;
1532 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1533 job->who,
1534 TRUE);
1535 g_free(job);
1538 static void sipe_deny_user_cb(gpointer data)
1540 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1541 if (!job) return;
1543 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1544 job->who,
1545 FALSE);
1546 g_free(job);
1549 /* OCS2005- */
1550 static void sipe_process_presence_wpending (struct sipe_core_private *sipe_private,
1551 struct sipmsg * msg)
1553 sipe_xml *watchers;
1554 const sipe_xml *watcher;
1555 // Ensure it's either not a response (eg it's a BENOTIFY) or that it's a 200 OK response
1556 if (msg->response != 0 && msg->response != 200) return;
1558 if (msg->bodylen == 0 || msg->body == NULL || sipe_strequal(sipmsg_find_header(msg, "Event"), "msrtc.wpending")) return;
1560 watchers = sipe_xml_parse(msg->body, msg->bodylen);
1561 if (!watchers) return;
1563 for (watcher = sipe_xml_child(watchers, "watcher"); watcher; watcher = sipe_xml_twin(watcher)) {
1564 gchar * remote_user = g_strdup(sipe_xml_attribute(watcher, "uri"));
1565 gchar * alias = g_strdup(sipe_xml_attribute(watcher, "displayName"));
1566 gboolean on_list = sipe_buddy_find_by_uri(sipe_private, remote_user) != NULL;
1568 // TODO pull out optional displayName to pass as alias
1569 if (remote_user) {
1570 struct sipe_auth_job * job = g_new0(struct sipe_auth_job, 1);
1571 job->who = remote_user;
1572 job->sipe_private = sipe_private;
1573 sipe_backend_buddy_request_authorization(SIPE_CORE_PUBLIC,
1574 remote_user,
1575 alias,
1576 on_list,
1577 sipe_auth_user_cb,
1578 sipe_deny_user_cb,
1579 (gpointer)job);
1584 sipe_xml_free(watchers);
1585 return;
1589 * Dispatcher for all incoming subscription information
1590 * whether it comes from NOTIFY, BENOTIFY requests or
1591 * piggy-backed to subscription's OK responce.
1593 void process_incoming_notify(struct sipe_core_private *sipe_private,
1594 struct sipmsg *msg)
1596 const gchar *content_type = sipmsg_find_header(msg, "Content-Type");
1597 const gchar *event = sipmsg_find_header(msg, "Event");
1598 const gchar *subscription_state = sipmsg_find_header(msg, "subscription-state");
1600 SIPE_DEBUG_INFO("process_incoming_notify: subscription_state: %s", subscription_state ? subscription_state : "");
1602 /* implicit subscriptions */
1603 if (content_type && g_str_has_prefix(content_type, "application/ms-imdn+xml")) {
1604 sipe_process_imdn(sipe_private, msg);
1606 /* event subscriptions */
1607 } else if (event) {
1609 /* One-off subscriptions - sent with "Expires: 0" */
1610 if (sipe_strcase_equal(event, "vnd-microsoft-provisioning-v2")) {
1611 sipe_process_provisioning_v2(sipe_private, msg);
1612 } else if (sipe_strcase_equal(event, "vnd-microsoft-provisioning")) {
1613 sipe_process_provisioning(sipe_private, msg);
1614 } else if (sipe_strcase_equal(event, "presence")) {
1615 sipe_process_presence(sipe_private, msg);
1616 } else if (sipe_strcase_equal(event, "registration-notify")) {
1617 sipe_process_registration_notify(sipe_private, msg);
1619 /* Subscriptions with timeout */
1620 } else if (!subscription_state || strstr(subscription_state, "active")) {
1621 if (sipe_strcase_equal(event, "vnd-microsoft-roaming-contacts")) {
1622 sipe_process_roaming_contacts(sipe_private, msg);
1623 } else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-self")) {
1624 sipe_ocs2007_process_roaming_self(sipe_private, msg);
1625 } else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-ACL")) {
1626 sipe_process_roaming_acl(sipe_private, msg);
1627 } else if (sipe_strcase_equal(event, "presence.wpending")) {
1628 sipe_process_presence_wpending(sipe_private, msg);
1629 } else if (sipe_strcase_equal(event, "conference")) {
1630 sipe_process_conference(sipe_private, msg);
1637 Local Variables:
1638 mode: c
1639 c-file-style: "bsd"
1640 indent-tabs-mode: t
1641 tab-width: 8
1642 End: