l10n: sync translations with transifex.net (cs, hu, pl, ru, tr)
[siplcs.git] / src / core / sipe-notify.c
blobcfc71433c5f4701274062a2d6d4409564adc5c42
1 /**
2 * @file sipe-notify.c
4 * pidgin-sipe
6 * Copyright (C) 2011-2013 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 sipe_xml *xn_provision_group_list;
85 const sipe_xml *node;
87 xn_provision_group_list = sipe_xml_parse(msg->body, msg->bodylen);
89 /* provisionGroup */
90 for (node = sipe_xml_child(xn_provision_group_list, "provisionGroup");
91 node;
92 node = sipe_xml_twin(node)) {
93 const gchar *node_name = sipe_xml_attribute(node, "name");
95 /* ServerConfiguration */
96 if (sipe_strequal("ServerConfiguration", node_name)) {
97 const gchar *dlx_uri_str = SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ?
98 "dlxExternalUrl" : "dlxInternalUrl";
99 const gchar *addressbook_uri_str = SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ?
100 "absExternalServerUrl" : "absInternalServerUrl";
102 g_free(sipe_private->focus_factory_uri);
103 sipe_private->focus_factory_uri = sipe_xml_data(sipe_xml_child(node, "focusFactoryUri"));
104 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->focus_factory_uri=%s",
105 sipe_private->focus_factory_uri ? sipe_private->focus_factory_uri : "");
107 g_free(sipe_private->dlx_uri);
108 sipe_private->dlx_uri = sipe_xml_data(sipe_xml_child(node, dlx_uri_str));
109 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->dlx_uri=%s",
110 sipe_private->dlx_uri ? sipe_private->dlx_uri : "");
112 g_free(sipe_private->addressbook_uri);
113 sipe_private->addressbook_uri = sipe_xml_data(sipe_xml_child(node, addressbook_uri_str));
114 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->addressbook_uri=%s",
115 sipe_private->addressbook_uri ? sipe_private->addressbook_uri : "");
117 #ifdef HAVE_VV
118 g_free(sipe_private->test_call_bot_uri);
119 sipe_private->test_call_bot_uri = sipe_xml_data(sipe_xml_child(node, "botSipUriForTestCall"));
120 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->test_call_bot_uri=%s",
121 sipe_private->test_call_bot_uri ? sipe_private->test_call_bot_uri : "");
123 g_free(sipe_private->mras_uri);
124 sipe_private->mras_uri = g_strstrip(sipe_xml_data(sipe_xml_child(node, "mrasUri")));
125 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->mras_uri=%s",
126 sipe_private->mras_uri ? sipe_private->mras_uri : "");
128 if (sipe_private->mras_uri)
129 sipe_media_get_av_edge_credentials(sipe_private);
130 #endif
132 /* persistentChatConfiguration */
133 } else if (sipe_strequal("persistentChatConfiguration", node_name)) {
134 const sipe_xml *property;
135 gboolean enabled = FALSE;
136 gchar *uri = NULL;
138 for (property = sipe_xml_child(node, "propertyEntryList/property");
139 property;
140 property = sipe_xml_twin(property)) {
141 const gchar *name = sipe_xml_attribute(property, "name");
142 gchar *value = sipe_xml_data(property);
144 if (sipe_strequal(name, "EnablePersistentChat")) {
145 enabled = sipe_strequal(value, "true");
147 } else if (sipe_strequal(name, "DefaultPersistentChatPoolUri")) {
148 g_free(uri);
149 uri = value;
150 value = NULL;
152 g_free(value);
155 if (enabled) {
156 g_free(sipe_private->persistentChatPool_uri);
157 sipe_private->persistentChatPool_uri = g_strdup(sipe_get_no_sip_uri(uri));
158 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->persistentChatPool_uri=%s",
159 sipe_private->persistentChatPool_uri ? sipe_private->persistentChatPool_uri : "");
161 g_free(uri);
165 sipe_xml_free(xn_provision_group_list);
167 if (sipe_private->dlx_uri && sipe_private->addressbook_uri) {
168 /* Some buddies might have been added before we received this
169 * provisioning notify with DLX and addressbook URIs. Now we can
170 * trigger an update of their photos. */
171 sipe_buddy_refresh_photos(sipe_private);
174 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
175 /* persistentChatPool_uri has been set at this point */
176 sipe_groupchat_init(sipe_private);
179 static void process_incoming_notify_rlmi_resub(struct sipe_core_private *sipe_private,
180 const gchar *data, unsigned len)
182 sipe_xml *xn_list;
183 const sipe_xml *xn_resource;
184 GHashTable *servers = g_hash_table_new_full(g_str_hash, g_str_equal,
185 g_free, NULL);
187 xn_list = sipe_xml_parse(data, len);
189 for (xn_resource = sipe_xml_child(xn_list, "resource");
190 xn_resource;
191 xn_resource = sipe_xml_twin(xn_resource) )
193 const char *uri, *state;
194 const sipe_xml *xn_instance;
196 xn_instance = sipe_xml_child(xn_resource, "instance");
197 if (!xn_instance) continue;
199 uri = sipe_xml_attribute(xn_resource, "uri");
200 state = sipe_xml_attribute(xn_instance, "state");
201 SIPE_DEBUG_INFO("process_incoming_notify_rlmi_resub: uri(%s),state(%s)", uri, state);
203 if (strstr(state, "resubscribe")) {
204 const char *poolFqdn = sipe_xml_attribute(xn_instance, "poolFqdn");
206 if (poolFqdn) { //[MS-PRES] Section 3.4.5.1.3 Processing Details
207 gchar *user = g_strdup(uri);
208 gchar *host = g_strdup(poolFqdn);
209 GSList *server = g_hash_table_lookup(servers,
210 host);
211 server = g_slist_append(server, user);
212 g_hash_table_insert(servers, host, server);
213 } else {
214 sipe_subscribe_presence_single(sipe_private,
215 uri,
216 uri);
221 /* Send out any deferred poolFqdn subscriptions */
222 g_hash_table_foreach(servers, (GHFunc) sipe_subscribe_poolfqdn_resource_uri, sipe_private);
223 g_hash_table_destroy(servers);
225 sipe_xml_free(xn_list);
229 * Update user phone
230 * Suitable for both 2005 and 2007 systems.
232 * @param uri buddy SIP URI with 'sip:' prefix whose info we want to change.
233 * @param phone_type
234 * @param phone may be modified to strip white space
235 * @param phone_display_string may be modified to strip white space
237 static void
238 sipe_update_user_phone(struct sipe_core_private *sipe_private,
239 const gchar *uri,
240 const gchar *phone_type,
241 gchar *phone,
242 gchar *phone_display_string)
244 sipe_buddy_info_fields phone_node = SIPE_BUDDY_INFO_WORK_PHONE; /* work phone by default */
245 sipe_buddy_info_fields phone_display_node = SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY; /* work phone by default */
247 if(!phone || strlen(phone) == 0) return;
249 if ((sipe_strequal(phone_type, "mobile") || sipe_strequal(phone_type, "cell"))) {
250 phone_node = SIPE_BUDDY_INFO_MOBILE_PHONE;
251 phone_display_node = SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY;
252 } else if (sipe_strequal(phone_type, "home")) {
253 phone_node = SIPE_BUDDY_INFO_HOME_PHONE;
254 phone_display_node = SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY;
255 } else if (sipe_strequal(phone_type, "other")) {
256 phone_node = SIPE_BUDDY_INFO_OTHER_PHONE;
257 phone_display_node = SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY;
258 } else if (sipe_strequal(phone_type, "custom1")) {
259 phone_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE;
260 phone_display_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY;
263 sipe_buddy_update_property(sipe_private, uri, phone_node, phone);
264 if (phone_display_string) {
265 sipe_buddy_update_property(sipe_private, uri, phone_display_node, phone_display_string);
269 static void process_incoming_notify_msrtc(struct sipe_core_private *sipe_private,
270 const gchar *data,
271 unsigned len)
273 char *activity = NULL;
274 const char *epid;
275 const char *status_id = NULL;
276 const char *name;
277 char *uri;
278 char *self_uri = sip_uri_self(sipe_private);
279 int avl;
280 int act;
281 const char *device_name = NULL;
282 const char *cal_start_time = NULL;
283 const char *cal_granularity = NULL;
284 char *cal_free_busy_base64 = NULL;
285 struct sipe_buddy *sbuddy;
286 const sipe_xml *node;
287 sipe_xml *xn_presentity;
288 const sipe_xml *xn_availability;
289 const sipe_xml *xn_activity;
290 const sipe_xml *xn_display_name;
291 const sipe_xml *xn_email;
292 const sipe_xml *xn_phone_number;
293 const sipe_xml *xn_userinfo;
294 const sipe_xml *xn_note;
295 const sipe_xml *xn_oof;
296 const sipe_xml *xn_state;
297 const sipe_xml *xn_contact;
298 char *note;
299 int user_avail;
300 const char *user_avail_nil;
301 int res_avail;
302 time_t user_avail_since = 0;
303 time_t activity_since = 0;
305 /* fix for Reuters environment on Linux */
306 if (data && strstr(data, "encoding=\"utf-16\"")) {
307 char *tmp_data;
308 tmp_data = replace(data, "encoding=\"utf-16\"", "encoding=\"utf-8\"");
309 xn_presentity = sipe_xml_parse(tmp_data, strlen(tmp_data));
310 g_free(tmp_data);
311 } else {
312 xn_presentity = sipe_xml_parse(data, len);
315 xn_availability = sipe_xml_child(xn_presentity, "availability");
316 xn_activity = sipe_xml_child(xn_presentity, "activity");
317 xn_display_name = sipe_xml_child(xn_presentity, "displayName");
318 xn_email = sipe_xml_child(xn_presentity, "email");
319 xn_phone_number = sipe_xml_child(xn_presentity, "phoneNumber");
320 xn_userinfo = sipe_xml_child(xn_presentity, "userInfo");
321 xn_oof = xn_userinfo ? sipe_xml_child(xn_userinfo, "oof") : NULL;
322 xn_state = xn_userinfo ? sipe_xml_child(xn_userinfo, "states/state"): NULL;
323 user_avail = xn_state ? sipe_xml_int_attribute(xn_state, "avail", 0) : 0;
324 user_avail_since = xn_state ? sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since")) : 0;
325 user_avail_nil = xn_state ? sipe_xml_attribute(xn_state, "nil") : NULL;
326 xn_contact = xn_userinfo ? sipe_xml_child(xn_userinfo, "contact") : NULL;
327 xn_note = xn_userinfo ? sipe_xml_child(xn_userinfo, "note") : NULL;
328 note = xn_note ? sipe_xml_data(xn_note) : NULL;
330 if (sipe_strequal(user_avail_nil, "true")) { /* null-ed */
331 user_avail = 0;
332 user_avail_since = 0;
335 name = sipe_xml_attribute(xn_presentity, "uri"); /* without 'sip:' prefix */
336 uri = sip_uri_from_name(name);
337 avl = sipe_xml_int_attribute(xn_availability, "aggregate", 0);
338 epid = sipe_xml_attribute(xn_availability, "epid");
339 act = sipe_xml_int_attribute(xn_activity, "aggregate", 0);
341 status_id = sipe_ocs2005_status_from_activity_availability(act, avl);
342 activity = g_strdup(sipe_ocs2005_activity_description(act));
343 res_avail = sipe_ocs2007_availability_from_status(status_id, NULL);
344 if (user_avail > res_avail) {
345 res_avail = user_avail;
346 status_id = sipe_ocs2007_status_from_legacy_availability(user_avail, NULL);
349 if (xn_display_name) {
350 char *display_name = g_strdup(sipe_xml_attribute(xn_display_name, "displayName"));
351 char *email = xn_email ? g_strdup(sipe_xml_attribute(xn_email, "email")) : NULL;
352 char *phone_label = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "label")) : NULL;
353 char *phone_number = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "number")) : NULL;
354 char *tel_uri = sip_to_tel_uri(phone_number);
356 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
357 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
358 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
359 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, !is_empty(phone_label) ? phone_label : phone_number);
361 g_free(tel_uri);
362 g_free(phone_label);
363 g_free(phone_number);
364 g_free(email);
365 g_free(display_name);
368 if (xn_contact) {
369 /* tel */
370 for (node = sipe_xml_child(xn_contact, "tel"); node; node = sipe_xml_twin(node))
372 /* Ex.: <tel type="work">tel:+3222220000</tel> */
373 const char *phone_type = sipe_xml_attribute(node, "type");
374 char* phone = sipe_xml_data(node);
376 sipe_update_user_phone(sipe_private, uri, phone_type, phone, NULL);
378 g_free(phone);
382 if (xn_display_name || xn_contact)
383 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC, uri);
385 /* devicePresence */
386 for (node = sipe_xml_child(xn_presentity, "devices/devicePresence"); node; node = sipe_xml_twin(node)) {
387 const sipe_xml *xn_device_name;
388 const sipe_xml *xn_calendar_info;
389 const sipe_xml *xn_state;
390 char *state;
392 /* deviceName */
393 if (sipe_strequal(sipe_xml_attribute(node, "epid"), epid)) {
394 xn_device_name = sipe_xml_child(node, "deviceName");
395 device_name = xn_device_name ? sipe_xml_attribute(xn_device_name, "name") : NULL;
398 /* calendarInfo */
399 xn_calendar_info = sipe_xml_child(node, "calendarInfo");
400 if (xn_calendar_info) {
401 const char *cal_start_time_tmp = sipe_xml_attribute(xn_calendar_info, "startTime");
403 if (cal_start_time) {
404 time_t cal_start_time_t = sipe_utils_str_to_time(cal_start_time);
405 time_t cal_start_time_t_tmp = sipe_utils_str_to_time(cal_start_time_tmp);
407 if (cal_start_time_t_tmp > cal_start_time_t) {
408 cal_start_time = cal_start_time_tmp;
409 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
410 g_free(cal_free_busy_base64);
411 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
413 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);
415 } else {
416 cal_start_time = cal_start_time_tmp;
417 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
418 g_free(cal_free_busy_base64);
419 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
421 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);
425 /* state */
426 xn_state = sipe_xml_child(node, "states/state");
427 if (xn_state) {
428 int dev_avail = sipe_xml_int_attribute(xn_state, "avail", 0);
429 time_t dev_avail_since = sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since"));
431 state = sipe_xml_data(xn_state);
432 if (dev_avail_since > user_avail_since &&
433 dev_avail >= res_avail)
435 const gchar *new_desc;
436 res_avail = dev_avail;
437 if (!is_empty(state)) {
438 if (sipe_strequal(state, sipe_status_activity_to_token(SIPE_ACTIVITY_ON_PHONE))) {
439 g_free(activity);
440 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_ON_PHONE));
441 } else if (sipe_strequal(state, "presenting")) {
442 g_free(activity);
443 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_IN_CONF));
444 } else {
445 activity = state;
446 state = NULL;
448 activity_since = dev_avail_since;
450 status_id = sipe_ocs2007_status_from_legacy_availability(res_avail, NULL);
451 new_desc = sipe_ocs2007_legacy_activity_description(res_avail);
452 if (new_desc) {
453 g_free(activity);
454 activity = g_strdup(new_desc);
457 g_free(state);
461 /* oof */
462 if (xn_oof && res_avail >= 15000) { /* 12000 in 2007 */
463 g_free(activity);
464 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_OOF));
465 activity_since = 0;
468 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
469 if (sbuddy)
471 g_free(sbuddy->activity);
472 sbuddy->activity = activity;
473 activity = NULL;
475 sbuddy->activity_since = activity_since;
477 sbuddy->user_avail = user_avail;
478 sbuddy->user_avail_since = user_avail_since;
480 g_free(sbuddy->note);
481 sbuddy->note = NULL;
482 if (!is_empty(note)) { sbuddy->note = g_markup_escape_text(note, -1); }
484 sbuddy->is_oof_note = (xn_oof != NULL);
486 g_free(sbuddy->device_name);
487 sbuddy->device_name = NULL;
488 if (!is_empty(device_name)) { sbuddy->device_name = g_strdup(device_name); }
490 if (!is_empty(cal_free_busy_base64)) {
491 g_free(sbuddy->cal_start_time);
492 sbuddy->cal_start_time = g_strdup(cal_start_time);
494 sbuddy->cal_granularity = sipe_strcase_equal(cal_granularity, "PT15M") ? 15 : 0;
496 g_free(sbuddy->cal_free_busy_base64);
497 sbuddy->cal_free_busy_base64 = cal_free_busy_base64;
498 cal_free_busy_base64 = NULL;
500 g_free(sbuddy->cal_free_busy);
501 sbuddy->cal_free_busy = NULL;
504 sbuddy->last_non_cal_status_id = status_id;
505 g_free(sbuddy->last_non_cal_activity);
506 sbuddy->last_non_cal_activity = g_strdup(sbuddy->activity);
508 if (sipe_strcase_equal(sbuddy->name, self_uri)) {
509 if (!sipe_strequal(sbuddy->note, sipe_private->note)) /* not same */
511 if (sbuddy->is_oof_note)
512 SIPE_CORE_PRIVATE_FLAG_SET(OOF_NOTE);
513 else
514 SIPE_CORE_PRIVATE_FLAG_UNSET(OOF_NOTE);
516 g_free(sipe_private->note);
517 sipe_private->note = g_strdup(sbuddy->note);
519 sipe_private->note_since = time(NULL);
522 sipe_status_set_token(sipe_private,
523 sbuddy->last_non_cal_status_id);
526 g_free(cal_free_busy_base64);
527 g_free(activity);
529 SIPE_DEBUG_INFO("process_incoming_notify_msrtc: status(%s)", status_id);
530 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
531 sipe_status_token_to_activity(status_id));
533 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007) && sipe_strcase_equal(self_uri, uri)) {
534 sipe_ocs2005_user_info_has_updated(sipe_private, xn_userinfo);
537 g_free(note);
538 sipe_xml_free(xn_presentity);
539 g_free(uri);
540 g_free(self_uri);
543 static void process_incoming_notify_rlmi(struct sipe_core_private *sipe_private,
544 const gchar *data,
545 unsigned len)
547 const char *uri;
548 struct sipe_buddy *sbuddy = NULL;
549 sipe_xml *xn_categories;
550 const sipe_xml *xn_category;
551 const char *status = NULL;
552 gboolean do_update_status = FALSE;
553 gboolean has_note_cleaned = FALSE;
554 gboolean has_free_busy_cleaned = FALSE;
556 xn_categories = sipe_xml_parse(data, len);
557 uri = sipe_xml_attribute(xn_categories, "uri"); /* with 'sip:' prefix */
558 if (uri) {
559 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
562 if (!sbuddy) {
563 /* Got presence of a buddy not in our contact list, ignore. */
564 sipe_xml_free(xn_categories);
565 return;
568 for (xn_category = sipe_xml_child(xn_categories, "category");
569 xn_category ;
570 xn_category = sipe_xml_twin(xn_category) )
572 const sipe_xml *xn_node;
573 const char *tmp;
574 const char *attrVar = sipe_xml_attribute(xn_category, "name");
575 time_t publish_time = (tmp = sipe_xml_attribute(xn_category, "publishTime")) ?
576 sipe_utils_str_to_time(tmp) : 0;
578 /* contactCard */
579 if (sipe_strequal(attrVar, "contactCard"))
581 const sipe_xml *card = sipe_xml_child(xn_category, "contactCard");
583 if (card) {
584 const sipe_xml *node;
585 /* identity - Display Name and email */
586 node = sipe_xml_child(card, "identity");
587 if (node) {
588 char* display_name = sipe_xml_data(
589 sipe_xml_child(node, "name/displayName"));
590 char* email = sipe_xml_data(
591 sipe_xml_child(node, "email"));
593 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
594 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
596 g_free(display_name);
597 g_free(email);
599 /* company */
600 node = sipe_xml_child(card, "company");
601 if (node) {
602 char* company = sipe_xml_data(node);
603 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COMPANY, company);
604 g_free(company);
606 /* department */
607 node = sipe_xml_child(card, "department");
608 if (node) {
609 char* department = sipe_xml_data(node);
610 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DEPARTMENT, department);
611 g_free(department);
613 /* title */
614 node = sipe_xml_child(card, "title");
615 if (node) {
616 char* title = sipe_xml_data(node);
617 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_JOB_TITLE, title);
618 g_free(title);
620 /* office */
621 node = sipe_xml_child(card, "office");
622 if (node) {
623 char* office = sipe_xml_data(node);
624 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_OFFICE, office);
625 g_free(office);
627 /* site (url) */
628 node = sipe_xml_child(card, "url");
629 if (node) {
630 char* site = sipe_xml_data(node);
631 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_SITE, site);
632 g_free(site);
634 /* phone */
635 for (node = sipe_xml_child(card, "phone");
636 node;
637 node = sipe_xml_twin(node))
639 const char *phone_type = sipe_xml_attribute(node, "type");
640 char* phone = sipe_xml_data(sipe_xml_child(node, "uri"));
641 char* phone_display_string = sipe_xml_data(sipe_xml_child(node, "displayString"));
643 sipe_update_user_phone(sipe_private, uri, phone_type, phone, phone_display_string);
645 g_free(phone);
646 g_free(phone_display_string);
648 /* address */
649 for (node = sipe_xml_child(card, "address");
650 node;
651 node = sipe_xml_twin(node))
653 if (sipe_strequal(sipe_xml_attribute(node, "type"), "work")) {
654 char* street = sipe_xml_data(sipe_xml_child(node, "street"));
655 char* city = sipe_xml_data(sipe_xml_child(node, "city"));
656 char* state = sipe_xml_data(sipe_xml_child(node, "state"));
657 char* zipcode = sipe_xml_data(sipe_xml_child(node, "zipcode"));
658 char* country_code = sipe_xml_data(sipe_xml_child(node, "countryCode"));
660 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STREET, street);
661 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_CITY, city);
662 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STATE, state);
663 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_ZIPCODE, zipcode);
664 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COUNTRY, country_code);
666 g_free(street);
667 g_free(city);
668 g_free(state);
669 g_free(zipcode);
670 g_free(country_code);
672 break;
677 /* note */
678 else if (sipe_strequal(attrVar, "note"))
680 if (!has_note_cleaned) {
681 has_note_cleaned = TRUE;
683 g_free(sbuddy->note);
684 sbuddy->note = NULL;
685 sbuddy->is_oof_note = FALSE;
686 sbuddy->note_since = publish_time;
688 do_update_status = TRUE;
690 if (publish_time >= sbuddy->note_since) {
691 /* clean up in case no 'note' element is supplied
692 * which indicate note removal in client
694 g_free(sbuddy->note);
695 sbuddy->note = NULL;
696 sbuddy->is_oof_note = FALSE;
697 sbuddy->note_since = publish_time;
699 xn_node = sipe_xml_child(xn_category, "note/body");
700 if (xn_node) {
701 char *tmp;
702 sbuddy->note = g_markup_escape_text((tmp = sipe_xml_data(xn_node)), -1);
703 g_free(tmp);
704 sbuddy->is_oof_note = sipe_strequal(sipe_xml_attribute(xn_node, "type"), "OOF");
705 sbuddy->note_since = publish_time;
707 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: uri(%s), note(%s)",
708 uri, sbuddy->note ? sbuddy->note : "");
710 /* to trigger UI refresh in case no status info is supplied in this update */
711 do_update_status = TRUE;
714 /* state */
715 else if(sipe_strequal(attrVar, "state"))
717 char *tmp;
718 int availability;
719 const sipe_xml *xn_availability;
720 const sipe_xml *xn_activity;
721 const sipe_xml *xn_meeting_subject;
722 const sipe_xml *xn_meeting_location;
723 const gchar *legacy_activity;
725 xn_node = sipe_xml_child(xn_category, "state");
726 if (!xn_node) continue;
727 xn_availability = sipe_xml_child(xn_node, "availability");
728 if (!xn_availability) continue;
729 xn_activity = sipe_xml_child(xn_node, "activity");
730 xn_meeting_subject = sipe_xml_child(xn_node, "meetingSubject");
731 xn_meeting_location = sipe_xml_child(xn_node, "meetingLocation");
733 tmp = sipe_xml_data(xn_availability);
734 availability = atoi(tmp);
735 g_free(tmp);
737 /* activity */
738 g_free(sbuddy->activity);
739 sbuddy->activity = NULL;
740 if (xn_activity) {
741 const char *token = sipe_xml_attribute(xn_activity, "token");
742 const sipe_xml *xn_custom = sipe_xml_child(xn_activity, "custom");
744 /* from token */
745 if (!is_empty(token)) {
746 sbuddy->activity = g_strdup(sipe_core_activity_description(sipe_status_token_to_activity(token)));
748 /* from custom element */
749 if (xn_custom) {
750 char *custom = sipe_xml_data(xn_custom);
752 if (!is_empty(custom)) {
753 g_free(sbuddy->activity);
754 sbuddy->activity = custom;
755 custom = NULL;
757 g_free(custom);
760 /* meeting_subject */
761 g_free(sbuddy->meeting_subject);
762 sbuddy->meeting_subject = NULL;
763 if (xn_meeting_subject) {
764 char *meeting_subject = sipe_xml_data(xn_meeting_subject);
766 if (!is_empty(meeting_subject)) {
767 sbuddy->meeting_subject = meeting_subject;
768 meeting_subject = NULL;
770 g_free(meeting_subject);
772 /* meeting_location */
773 g_free(sbuddy->meeting_location);
774 sbuddy->meeting_location = NULL;
775 if (xn_meeting_location) {
776 char *meeting_location = sipe_xml_data(xn_meeting_location);
778 if (!is_empty(meeting_location)) {
779 sbuddy->meeting_location = meeting_location;
780 meeting_location = NULL;
782 g_free(meeting_location);
785 status = sipe_ocs2007_status_from_legacy_availability(availability, NULL);
786 legacy_activity = sipe_ocs2007_legacy_activity_description(availability);
787 if (sbuddy->activity && legacy_activity) {
788 gchar *tmp2 = sbuddy->activity;
790 sbuddy->activity = g_strdup_printf("%s, %s", sbuddy->activity, legacy_activity);
791 g_free(tmp2);
792 } else if (legacy_activity) {
793 sbuddy->activity = g_strdup(legacy_activity);
796 do_update_status = TRUE;
798 /* calendarData */
799 else if(sipe_strequal(attrVar, "calendarData"))
801 const sipe_xml *xn_free_busy = sipe_xml_child(xn_category, "calendarData/freeBusy");
802 const sipe_xml *xn_working_hours = sipe_xml_child(xn_category, "calendarData/WorkingHours");
804 if (xn_free_busy) {
805 if (!has_free_busy_cleaned) {
806 has_free_busy_cleaned = TRUE;
808 g_free(sbuddy->cal_start_time);
809 sbuddy->cal_start_time = NULL;
811 g_free(sbuddy->cal_free_busy_base64);
812 sbuddy->cal_free_busy_base64 = NULL;
814 g_free(sbuddy->cal_free_busy);
815 sbuddy->cal_free_busy = NULL;
817 sbuddy->cal_free_busy_published = publish_time;
820 if (publish_time >= sbuddy->cal_free_busy_published) {
821 g_free(sbuddy->cal_start_time);
822 sbuddy->cal_start_time = g_strdup(sipe_xml_attribute(xn_free_busy, "startTime"));
824 sbuddy->cal_granularity = sipe_strcase_equal(sipe_xml_attribute(xn_free_busy, "granularity"), "PT15M") ?
825 15 : 0;
827 g_free(sbuddy->cal_free_busy_base64);
828 sbuddy->cal_free_busy_base64 = sipe_xml_data(xn_free_busy);
830 g_free(sbuddy->cal_free_busy);
831 sbuddy->cal_free_busy = NULL;
833 sbuddy->cal_free_busy_published = publish_time;
835 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);
839 if (xn_working_hours) {
840 sipe_cal_parse_working_hours(xn_working_hours, sbuddy);
845 if (do_update_status) {
846 guint activity;
848 if (status) {
849 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: %s", status);
850 activity = sipe_status_token_to_activity(status);
851 } else {
852 /* no status category in this update,
853 using contact's current status */
854 activity = sipe_backend_buddy_get_status(SIPE_CORE_PUBLIC,
855 uri);
858 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri, activity);
861 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC, uri);
863 sipe_xml_free(xn_categories);
866 static void sipe_buddy_status_from_activity(struct sipe_core_private *sipe_private,
867 const gchar *uri,
868 const gchar *activity,
869 gboolean is_online)
871 if (is_online) {
872 const gchar *status_id = NULL;
873 if (activity) {
874 if (sipe_strequal(activity,
875 sipe_status_activity_to_token(SIPE_ACTIVITY_BUSY))) {
876 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_BUSY);
877 } else if (sipe_strequal(activity,
878 sipe_status_activity_to_token(SIPE_ACTIVITY_AWAY))) {
879 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_AWAY);
883 if (!status_id) {
884 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_AVAILABLE);
887 SIPE_DEBUG_INFO("sipe_buddy_status_from_activity: status_id(%s)", status_id);
888 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
889 sipe_status_token_to_activity(status_id));
890 } else {
891 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
892 SIPE_ACTIVITY_OFFLINE);
896 static void process_incoming_notify_pidf(struct sipe_core_private *sipe_private,
897 const gchar *data,
898 unsigned len)
900 gchar *uri;
901 gchar *getbasic;
902 gchar *activity = NULL;
903 sipe_xml *pidf;
904 const sipe_xml *basicstatus = NULL, *tuple, *status;
905 gboolean isonline = FALSE;
906 const sipe_xml *display_name_node;
908 pidf = sipe_xml_parse(data, len);
909 if (!pidf) {
910 SIPE_DEBUG_INFO("process_incoming_notify_pidf: no parseable pidf:%s", data);
911 return;
914 if ((tuple = sipe_xml_child(pidf, "tuple")))
916 if ((status = sipe_xml_child(tuple, "status"))) {
917 basicstatus = sipe_xml_child(status, "basic");
921 if (!basicstatus) {
922 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic found");
923 sipe_xml_free(pidf);
924 return;
927 getbasic = sipe_xml_data(basicstatus);
928 if (!getbasic) {
929 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic data found");
930 sipe_xml_free(pidf);
931 return;
934 SIPE_DEBUG_INFO("process_incoming_notify_pidf: basic-status(%s)", getbasic);
935 if (strstr(getbasic, "open")) {
936 isonline = TRUE;
938 g_free(getbasic);
940 uri = sip_uri(sipe_xml_attribute(pidf, "entity")); /* with 'sip:' prefix */ /* AOL comes without the prefix */
942 display_name_node = sipe_xml_child(pidf, "display-name");
943 if (display_name_node) {
944 char * display_name = sipe_xml_data(display_name_node);
946 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
947 g_free(display_name);
949 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC, uri);
952 if ((tuple = sipe_xml_child(pidf, "tuple"))) {
953 if ((status = sipe_xml_child(tuple, "status"))) {
954 if ((basicstatus = sipe_xml_child(status, "activities"))) {
955 if ((basicstatus = sipe_xml_child(basicstatus, "activity"))) {
956 activity = sipe_xml_data(basicstatus);
957 SIPE_DEBUG_INFO("process_incoming_notify_pidf: activity(%s)", activity);
963 sipe_buddy_status_from_activity(sipe_private,
964 uri,
965 activity,
966 isonline);
968 g_free(activity);
969 g_free(uri);
970 sipe_xml_free(pidf);
973 static void sipe_presence_mime_cb(gpointer user_data, /* sipe_core_private */
974 const GSList *fields,
975 const gchar *body,
976 gsize length)
978 const gchar *type = sipe_utils_nameval_find(fields, "Content-Type");
980 if (strstr(type,"application/rlmi+xml")) {
981 process_incoming_notify_rlmi_resub(user_data, body, length);
982 } else if (strstr(type, "text/xml+msrtc.pidf")) {
983 process_incoming_notify_msrtc(user_data, body, length);
984 } else {
985 process_incoming_notify_rlmi(user_data, body, length);
989 static void sipe_process_presence(struct sipe_core_private *sipe_private,
990 struct sipmsg *msg)
992 const char *ctype = sipmsg_find_header(msg, "Content-Type");
994 SIPE_DEBUG_INFO("sipe_process_presence: Content-Type: %s", ctype ? ctype : "");
996 if (ctype &&
997 (strstr(ctype, "application/rlmi+xml") ||
998 strstr(ctype, "application/msrtc-event-categories+xml")))
1000 if (strstr(ctype, "multipart"))
1002 sipe_mime_parts_foreach(ctype, msg->body, sipe_presence_mime_cb, sipe_private);
1004 else if(strstr(ctype, "application/msrtc-event-categories+xml") )
1006 process_incoming_notify_rlmi(sipe_private, msg->body, msg->bodylen);
1008 else if(strstr(ctype, "application/rlmi+xml"))
1010 process_incoming_notify_rlmi_resub(sipe_private, msg->body, msg->bodylen);
1013 else if(ctype && strstr(ctype, "text/xml+msrtc.pidf"))
1015 process_incoming_notify_msrtc(sipe_private, msg->body, msg->bodylen);
1017 else
1019 process_incoming_notify_pidf(sipe_private, msg->body, msg->bodylen);
1024 * Fires on deregistration event initiated by server.
1025 * [MS-SIPREGE] SIP extension.
1027 * OCS2007 Example
1029 * Content-Type: text/registration-event
1030 * subscription-state: terminated;expires=0
1031 * ms-diagnostics-public: 4141;reason="User disabled"
1033 * deregistered;event=rejected
1035 static void sipe_process_registration_notify(struct sipe_core_private *sipe_private,
1036 struct sipmsg *msg)
1038 const gchar *contenttype = sipmsg_find_header(msg, "Content-Type");
1039 gchar *event = NULL;
1040 gchar *reason = NULL;
1041 gchar *warning;
1043 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: deregistration received.");
1045 if (!g_ascii_strncasecmp(contenttype, "text/registration-event", 23)) {
1046 event = sipmsg_find_part_of_header(msg->body, "event=", NULL, NULL);
1047 //@TODO have proper parameter extraction _by_name_ func, case insesitive.
1048 event = event ? event : sipmsg_find_part_of_header(msg->body, "event=", ";", NULL);
1049 } else {
1050 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: unknown content type, exiting.");
1051 return;
1054 reason = sipmsg_get_ms_diagnostics_reason(msg);
1055 reason = reason ? reason : sipmsg_get_ms_diagnostics_public_reason(msg);
1056 if (!reason) { // for LCS2005
1057 if (event && sipe_strcase_equal(event, "unregistered")) {
1058 //reason = g_strdup(_("User logged out")); // [MS-OCER]
1059 reason = g_strdup(_("you are already signed in at another location"));
1060 } else if (event && sipe_strcase_equal(event, "rejected")) {
1061 reason = g_strdup(_("user disabled")); // [MS-OCER]
1062 } else if (event && sipe_strcase_equal(event, "deactivated")) {
1063 reason = g_strdup(_("user moved")); // [MS-OCER]
1066 g_free(event);
1067 warning = g_strdup_printf(_("You have been rejected by the server: %s"), reason ? reason : _("no reason given"));
1068 g_free(reason);
1070 sipe_backend_connection_error(SIPE_CORE_PUBLIC,
1071 SIPE_CONNECTION_ERROR_INVALID_USERNAME,
1072 warning);
1073 g_free(warning);
1077 /* Replace "~" with localized version of "Other Contacts" */
1078 static const gchar *get_group_name(const sipe_xml *node)
1080 const gchar *name = sipe_xml_attribute(node, "name");
1081 return(g_str_has_prefix(name, "~") ? _("Other Contacts") : name);
1084 static void add_new_group(struct sipe_core_private *sipe_private,
1085 const sipe_xml *node)
1087 sipe_group_add(sipe_private,
1088 get_group_name(node),
1089 NULL,
1090 NULL,
1091 sipe_xml_int_attribute(node, "id", 0));
1094 static void add_new_buddy(struct sipe_core_private *sipe_private,
1095 const sipe_xml *node,
1096 const gchar *uri)
1098 const gchar *name = sipe_xml_attribute(node, "name");
1099 struct sipe_buddy *buddy = NULL;
1100 gchar *tmp;
1101 gchar **item_groups;
1102 int i = 0;
1104 /* assign to group Other Contacts if nothing else received */
1105 tmp = g_strdup(sipe_xml_attribute(node, "groups"));
1106 if (is_empty(tmp)) {
1107 struct sipe_group *group = sipe_group_find_by_name(sipe_private,
1108 _("Other Contacts"));
1109 g_free(tmp);
1110 tmp = group ? g_strdup_printf("%d", group->id) : g_strdup("1");
1112 item_groups = g_strsplit(tmp, " ", 0);
1113 g_free(tmp);
1115 while (item_groups[i]) {
1116 struct sipe_group *group = sipe_group_find_by_id(sipe_private,
1117 g_ascii_strtod(item_groups[i],
1118 NULL));
1120 /* If couldn't find the right group for this contact, */
1121 /* then just put it in the first group we have */
1122 if (!group)
1123 group = sipe_group_first(sipe_private);
1125 if (group) {
1126 if (!buddy)
1127 buddy = sipe_buddy_add(sipe_private,
1128 uri,
1129 NULL,
1130 NULL);
1132 sipe_buddy_add_to_group(sipe_private,
1133 buddy,
1134 group,
1135 name);
1136 } else {
1137 SIPE_DEBUG_INFO("No group found for contact %s! Unable to add to buddy list",
1138 name);
1141 i++;
1144 g_strfreev(item_groups);
1147 static gboolean sipe_process_roaming_contacts(struct sipe_core_private *sipe_private,
1148 struct sipmsg *msg)
1150 int len = msg->bodylen;
1152 const gchar *tmp = sipmsg_find_header(msg, "Event");
1153 const sipe_xml *item;
1154 sipe_xml *isc;
1155 guint delta;
1156 const sipe_xml *group_node;
1158 if (!g_str_has_prefix(tmp, "vnd-microsoft-roaming-contacts")) {
1159 return FALSE;
1162 /* Convert the contact from XML to backend Buddies */
1163 isc = sipe_xml_parse(msg->body, len);
1164 if (!isc) {
1165 return FALSE;
1168 /* [MS-SIP]: deltaNum MUST be non-zero */
1169 delta = sipe_xml_int_attribute(isc, "deltaNum", 0);
1170 if (delta) {
1171 sipe_private->deltanum_contacts = delta;
1175 * Process whole buddy list
1177 * - Only sent once
1178 * * up to Lync 2010
1179 * * Lync 2013 (and later) with buddy list not migrated
1181 * - Lync 2013 with buddy list migrated to Unified Contact Store (UCS)
1182 * * Notify piggy-backed on SUBSCRIBE response with empty list
1183 * * NOTIFY send by server with standard list (ignored by us)
1185 if (sipe_strequal(sipe_xml_name(isc), "contactList")) {
1186 const gchar *ucsmode = sipe_xml_attribute(isc, "ucsmode");
1188 SIPE_CORE_PRIVATE_FLAG_UNSET(LYNC2013);
1189 if (ucsmode) {
1190 gboolean migrated = sipe_strcase_equal(ucsmode,
1191 "migrated");
1192 SIPE_CORE_PRIVATE_FLAG_SET(LYNC2013);
1193 SIPE_DEBUG_INFO_NOFORMAT("contact list contains 'ucsmode' attribute (indicates Lync 2013+)");
1195 if (migrated)
1196 SIPE_DEBUG_INFO_NOFORMAT("contact list has been migrated to Unified Contact Store (UCS)");
1197 sipe_ucs_init(sipe_private, migrated);
1200 if (!sipe_ucs_is_migrated(sipe_private)) {
1201 /* Start processing contact list */
1202 sipe_backend_buddy_list_processing_start(SIPE_CORE_PUBLIC);
1204 /* Parse groups */
1205 for (group_node = sipe_xml_child(isc, "group"); group_node; group_node = sipe_xml_twin(group_node))
1206 add_new_group(sipe_private, group_node);
1208 /* Make sure we have at least one group */
1209 if (sipe_group_count(sipe_private) == 0) {
1210 sipe_group_create(sipe_private,
1211 NULL,
1212 _("Other Contacts"),
1213 NULL);
1216 /* Parse contacts */
1217 for (item = sipe_xml_child(isc, "contact"); item; item = sipe_xml_twin(item)) {
1218 const gchar *name = sipe_xml_attribute(item, "uri");
1219 gchar *uri = sip_uri_from_name(name);
1220 add_new_buddy(sipe_private, item, uri);
1221 g_free(uri);
1224 sipe_buddy_cleanup_local_list(sipe_private);
1226 /* Add self-contact if not there yet. 2005 systems. */
1227 /* This will resemble subscription to roaming_self in 2007 systems */
1228 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1229 gchar *self_uri = sip_uri_self(sipe_private);
1230 sipe_buddy_add(sipe_private,
1231 self_uri,
1232 NULL,
1233 NULL);
1234 g_free(self_uri);
1237 /* Finished processing contact list */
1238 sipe_backend_buddy_list_processing_finish(SIPE_CORE_PUBLIC);
1241 /* Process buddy list updates */
1242 } else if (sipe_strequal(sipe_xml_name(isc), "contactDelta")) {
1244 /* Process new groups */
1245 for (group_node = sipe_xml_child(isc, "addedGroup"); group_node; group_node = sipe_xml_twin(group_node))
1246 add_new_group(sipe_private, group_node);
1248 /* Process modified groups */
1249 for (group_node = sipe_xml_child(isc, "modifiedGroup"); group_node; group_node = sipe_xml_twin(group_node)) {
1250 struct sipe_group *group = sipe_group_find_by_id(sipe_private,
1251 (int)g_ascii_strtod(sipe_xml_attribute(group_node, "id"),
1252 NULL));
1253 if (group) {
1254 const gchar *name = get_group_name(group_node);
1256 if (!(is_empty(name) ||
1257 sipe_strequal(group->name, name)) &&
1258 sipe_group_rename(sipe_private,
1259 group,
1260 name))
1261 SIPE_DEBUG_INFO("Replaced group %d name with %s", group->id, name);
1265 /* Process new buddies */
1266 for (item = sipe_xml_child(isc, "addedContact"); item; item = sipe_xml_twin(item)) {
1267 add_new_buddy(sipe_private,
1268 item,
1269 sipe_xml_attribute(item, "uri"));
1272 /* Process modified buddies */
1273 for (item = sipe_xml_child(isc, "modifiedContact"); item; item = sipe_xml_twin(item)) {
1274 const gchar *uri = sipe_xml_attribute(item, "uri");
1275 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
1276 uri);
1278 if (buddy) {
1279 gchar **item_groups = g_strsplit(sipe_xml_attribute(item,
1280 "groups"),
1281 " ", 0);
1283 /* this should be defined. Otherwise we would get "deletedContact" */
1284 if (item_groups) {
1285 const gchar *name = sipe_xml_attribute(item, "name");
1286 gboolean empty_name = is_empty(name);
1287 GSList *found = NULL;
1288 int i = 0;
1290 while (item_groups[i]) {
1291 struct sipe_group *group = sipe_group_find_by_id(sipe_private,
1292 g_ascii_strtod(item_groups[i],
1293 NULL));
1294 /* ignore unkown groups */
1295 if (group) {
1296 sipe_backend_buddy b = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
1297 uri,
1298 group->name);
1300 /* add group to found list */
1301 found = g_slist_prepend(found, group);
1303 if (b) {
1304 /* new alias? */
1305 gchar *b_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
1308 if (!(empty_name ||
1309 sipe_strequal(b_alias, name))) {
1310 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
1312 name);
1313 SIPE_DEBUG_INFO("Replaced for buddy %s in group '%s' old alias '%s' with '%s'",
1314 uri, group->name, b_alias, name);
1316 g_free(b_alias);
1318 } else {
1319 const gchar *alias = empty_name ? uri : name;
1320 /* buddy was not in this group */
1321 sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
1322 uri,
1323 alias,
1324 group->name);
1325 sipe_buddy_insert_group(buddy, group);
1326 SIPE_DEBUG_INFO("Added buddy %s (alias '%s' to group '%s'",
1327 uri, alias, group->name);
1331 /* next group */
1332 i++;
1334 g_strfreev(item_groups);
1336 /* removed from groups? */
1337 sipe_buddy_update_groups(sipe_private,
1338 buddy,
1339 found);
1340 g_slist_free(found);
1345 /* Process deleted buddies */
1346 for (item = sipe_xml_child(isc, "deletedContact"); item; item = sipe_xml_twin(item)) {
1347 const gchar *uri = sipe_xml_attribute(item, "uri");
1348 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
1349 uri);
1351 if (buddy) {
1352 SIPE_DEBUG_INFO("Removing buddy %s", uri);
1353 sipe_buddy_remove(sipe_private, buddy);
1357 /* Process deleted groups
1359 * NOTE: all buddies will already have been removed from the
1360 * group prior to this. The log shows that OCS actually
1361 * sends two separate updates when you delete a group:
1363 * - first one with "modifiedContact" removing buddies
1364 * from the group, leaving it empty, and
1366 * - then one with "deletedGroup" removing the group
1368 for (group_node = sipe_xml_child(isc, "deletedGroup"); group_node; group_node = sipe_xml_twin(group_node))
1369 sipe_group_remove(sipe_private,
1370 sipe_group_find_by_id(sipe_private,
1371 (int)g_ascii_strtod(sipe_xml_attribute(group_node, "id"),
1372 NULL)));
1375 sipe_xml_free(isc);
1377 /* Subscribe to buddies, if contact list not migrated to UCS */
1378 if (!sipe_ucs_is_migrated(sipe_private))
1379 sipe_subscribe_presence_initial(sipe_private);
1381 /* for 2005 systems schedule contacts' status update
1382 * based on their calendar information
1384 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1385 sipe_ocs2005_schedule_status_update(sipe_private, time(NULL));
1388 return 0;
1391 static void sipe_process_roaming_acl(struct sipe_core_private *sipe_private,
1392 struct sipmsg *msg)
1394 guint delta;
1395 sipe_xml *xml;
1397 xml = sipe_xml_parse(msg->body, msg->bodylen);
1398 if (!xml)
1399 return;
1401 /* [MS-SIP]: deltaNum MUST be non-zero */
1402 delta = sipe_xml_int_attribute(xml, "deltaNum", 0);
1403 if (delta) {
1404 sipe_private->deltanum_acl = delta;
1407 sipe_xml_free(xml);
1410 struct sipe_auth_job {
1411 gchar *who;
1412 struct sipe_core_private *sipe_private;
1415 void sipe_core_contact_allow_deny(struct sipe_core_public *sipe_public,
1416 const gchar* who,
1417 gboolean allow)
1419 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1421 if (allow) {
1422 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: authorizing contact %s", who);
1423 } else {
1424 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: blocking contact %s", who);
1427 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1428 sipe_ocs2007_change_access_level(sipe_private,
1429 (allow ? -1 : 32000),
1430 "user",
1431 sipe_get_no_sip_uri(who));
1432 } else {
1433 sip_soap_ocs2005_setacl(sipe_private, who, allow);
1438 static void sipe_auth_user_cb(gpointer data)
1440 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1441 if (!job) return;
1443 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1444 job->who,
1445 TRUE);
1446 g_free(job);
1449 static void sipe_deny_user_cb(gpointer data)
1451 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1452 if (!job) return;
1454 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1455 job->who,
1456 FALSE);
1457 g_free(job);
1460 /* OCS2005- */
1461 static void sipe_process_presence_wpending (struct sipe_core_private *sipe_private,
1462 struct sipmsg * msg)
1464 sipe_xml *watchers;
1465 const sipe_xml *watcher;
1466 // Ensure it's either not a response (eg it's a BENOTIFY) or that it's a 200 OK response
1467 if (msg->response != 0 && msg->response != 200) return;
1469 if (msg->bodylen == 0 || msg->body == NULL || sipe_strequal(sipmsg_find_header(msg, "Event"), "msrtc.wpending")) return;
1471 watchers = sipe_xml_parse(msg->body, msg->bodylen);
1472 if (!watchers) return;
1474 for (watcher = sipe_xml_child(watchers, "watcher"); watcher; watcher = sipe_xml_twin(watcher)) {
1475 gchar * remote_user = g_strdup(sipe_xml_attribute(watcher, "uri"));
1476 gchar * alias = g_strdup(sipe_xml_attribute(watcher, "displayName"));
1477 gboolean on_list = sipe_buddy_find_by_uri(sipe_private, remote_user) != NULL;
1479 // TODO pull out optional displayName to pass as alias
1480 if (remote_user) {
1481 struct sipe_auth_job * job = g_new0(struct sipe_auth_job, 1);
1482 job->who = remote_user;
1483 job->sipe_private = sipe_private;
1484 sipe_backend_buddy_request_authorization(SIPE_CORE_PUBLIC,
1485 remote_user,
1486 alias,
1487 on_list,
1488 sipe_auth_user_cb,
1489 sipe_deny_user_cb,
1490 (gpointer)job);
1495 sipe_xml_free(watchers);
1496 return;
1500 * Dispatcher for all incoming subscription information
1501 * whether it comes from NOTIFY, BENOTIFY requests or
1502 * piggy-backed to subscription's OK responce.
1504 void process_incoming_notify(struct sipe_core_private *sipe_private,
1505 struct sipmsg *msg)
1507 const gchar *content_type = sipmsg_find_header(msg, "Content-Type");
1508 const gchar *event = sipmsg_find_header(msg, "Event");
1509 const gchar *subscription_state = sipmsg_find_header(msg, "subscription-state");
1511 SIPE_DEBUG_INFO("process_incoming_notify: subscription_state: %s", subscription_state ? subscription_state : "");
1513 /* implicit subscriptions */
1514 if (content_type && g_str_has_prefix(content_type, "application/ms-imdn+xml")) {
1515 sipe_process_imdn(sipe_private, msg);
1517 /* event subscriptions */
1518 } else if (event) {
1520 /* One-off subscriptions - sent with "Expires: 0" */
1521 if (sipe_strcase_equal(event, "vnd-microsoft-provisioning-v2")) {
1522 sipe_process_provisioning_v2(sipe_private, msg);
1523 } else if (sipe_strcase_equal(event, "vnd-microsoft-provisioning")) {
1524 sipe_process_provisioning(sipe_private, msg);
1525 } else if (sipe_strcase_equal(event, "presence")) {
1526 sipe_process_presence(sipe_private, msg);
1527 } else if (sipe_strcase_equal(event, "registration-notify")) {
1528 sipe_process_registration_notify(sipe_private, msg);
1530 /* Subscriptions with timeout */
1531 } else if (!subscription_state || strstr(subscription_state, "active")) {
1532 if (sipe_strcase_equal(event, "vnd-microsoft-roaming-contacts")) {
1533 sipe_process_roaming_contacts(sipe_private, msg);
1534 } else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-self")) {
1535 sipe_ocs2007_process_roaming_self(sipe_private, msg);
1536 } else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-ACL")) {
1537 sipe_process_roaming_acl(sipe_private, msg);
1538 } else if (sipe_strcase_equal(event, "presence.wpending")) {
1539 sipe_process_presence_wpending(sipe_private, msg);
1540 } else if (sipe_strcase_equal(event, "conference")) {
1541 sipe_process_conference(sipe_private, msg);
1548 Local Variables:
1549 mode: c
1550 c-file-style: "bsd"
1551 indent-tabs-mode: t
1552 tab-width: 8
1553 End: