webticket: add failure message to callback
[siplcs.git] / src / core / sipe-notify.c
bloba1b12916d0f94b70f17980a0d60bededec3dab1b
1 /**
2 * @file sipe-notify.c
4 * pidgin-sipe
6 * Copyright (C) 2011-12 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 "sipe-common.h"
38 #include "sipmsg.h"
39 #include "sip-csta.h"
40 #include "sip-soap.h"
41 #include "sip-transport.h"
42 #include "sipe-backend.h"
43 #include "sipe-buddy.h"
44 #include "sipe-cal.h"
45 #include "sipe-conf.h"
46 #include "sipe-core.h"
47 #include "sipe-core-private.h"
48 #include "sipe-group.h"
49 #include "sipe-media.h"
50 #include "sipe-mime.h"
51 #include "sipe-nls.h"
52 #include "sipe-notify.h"
53 #include "sipe-ocs2005.h"
54 #include "sipe-ocs2007.h"
55 #include "sipe-schedule.h"
56 #include "sipe-status.h"
57 #include "sipe-subscriptions.h"
58 #include "sipe-utils.h"
59 #include "sipe-xml.h"
61 /* OCS2005 */
62 static void sipe_process_provisioning(struct sipe_core_private *sipe_private,
63 struct sipmsg *msg)
65 sipe_xml *xn_provision;
66 const sipe_xml *node;
68 xn_provision = sipe_xml_parse(msg->body, msg->bodylen);
69 if ((node = sipe_xml_child(xn_provision, "user"))) {
70 SIPE_DEBUG_INFO("sipe_process_provisioning: uri=%s", sipe_xml_attribute(node, "uri"));
71 if ((node = sipe_xml_child(node, "line"))) {
72 const gchar *line_uri = sipe_xml_attribute(node, "uri");
73 const gchar *server = sipe_xml_attribute(node, "server");
74 SIPE_DEBUG_INFO("sipe_process_provisioning: line_uri=%s server=%s", line_uri, server);
75 sip_csta_open(sipe_private, line_uri, server);
78 sipe_xml_free(xn_provision);
81 /* OCS2007+ */
82 static void sipe_process_provisioning_v2(struct sipe_core_private *sipe_private,
83 struct sipmsg *msg)
85 sipe_xml *xn_provision_group_list;
86 const sipe_xml *node;
88 xn_provision_group_list = sipe_xml_parse(msg->body, msg->bodylen);
90 /* provisionGroup */
91 for (node = sipe_xml_child(xn_provision_group_list, "provisionGroup"); node; node = sipe_xml_twin(node)) {
92 if (sipe_strequal("ServerConfiguration", sipe_xml_attribute(node, "name"))) {
93 const gchar *dlx_uri_str = SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ?
94 "dlxExternalUrl" : "dlxInternalUrl";
95 const gchar *addressbook_uri_str = SIPE_CORE_PRIVATE_FLAG_IS(REMOTE_USER) ?
96 "absExternalServerUrl" : "absInternalServerUrl";
98 g_free(sipe_private->focus_factory_uri);
99 sipe_private->focus_factory_uri = sipe_xml_data(sipe_xml_child(node, "focusFactoryUri"));
100 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->focus_factory_uri=%s",
101 sipe_private->focus_factory_uri ? sipe_private->focus_factory_uri : "");
103 g_free(sipe_private->dlx_uri);
104 sipe_private->dlx_uri = sipe_xml_data(sipe_xml_child(node, dlx_uri_str));
105 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->dlx_uri=%s",
106 sipe_private->dlx_uri ? sipe_private->dlx_uri : "");
108 g_free(sipe_private->addressbook_uri);
109 sipe_private->addressbook_uri = sipe_xml_data(sipe_xml_child(node, addressbook_uri_str));
110 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->addressbook_uri=%s",
111 sipe_private->addressbook_uri ? sipe_private->addressbook_uri : "");
113 #ifdef HAVE_VV
114 g_free(sipe_private->test_call_bot_uri);
115 sipe_private->test_call_bot_uri = sipe_xml_data(sipe_xml_child(node, "botSipUriForTestCall"));
116 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->test_call_bot_uri=%s",
117 sipe_private->test_call_bot_uri ? sipe_private->test_call_bot_uri : "");
119 g_free(sipe_private->mras_uri);
120 sipe_private->mras_uri = g_strstrip(sipe_xml_data(sipe_xml_child(node, "mrasUri")));
121 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->mras_uri=%s",
122 sipe_private->mras_uri ? sipe_private->mras_uri : "");
124 if (sipe_private->mras_uri)
125 sipe_media_get_av_edge_credentials(sipe_private);
126 #endif
127 break;
130 sipe_xml_free(xn_provision_group_list);
132 if (sipe_private->dlx_uri && sipe_private->addressbook_uri) {
133 /* Some buddies might have been added before we received this
134 * provisioning notify with DLX and addressbook URIs. Now we can
135 * trigger an update of their photos. */
136 sipe_buddy_refresh_photos(sipe_private);
140 static void process_incoming_notify_rlmi_resub(struct sipe_core_private *sipe_private,
141 const gchar *data, unsigned len)
143 sipe_xml *xn_list;
144 const sipe_xml *xn_resource;
145 GHashTable *servers = g_hash_table_new_full(g_str_hash, g_str_equal,
146 g_free, NULL);
147 GSList *server;
148 gchar *host;
150 xn_list = sipe_xml_parse(data, len);
152 for (xn_resource = sipe_xml_child(xn_list, "resource");
153 xn_resource;
154 xn_resource = sipe_xml_twin(xn_resource) )
156 const char *uri, *state;
157 const sipe_xml *xn_instance;
159 xn_instance = sipe_xml_child(xn_resource, "instance");
160 if (!xn_instance) continue;
162 uri = sipe_xml_attribute(xn_resource, "uri");
163 state = sipe_xml_attribute(xn_instance, "state");
164 SIPE_DEBUG_INFO("process_incoming_notify_rlmi_resub: uri(%s),state(%s)", uri, state);
166 if (strstr(state, "resubscribe")) {
167 const char *poolFqdn = sipe_xml_attribute(xn_instance, "poolFqdn");
169 if (poolFqdn) { //[MS-PRES] Section 3.4.5.1.3 Processing Details
170 gchar *user = g_strdup(uri);
171 host = g_strdup(poolFqdn);
172 server = g_hash_table_lookup(servers, host);
173 server = g_slist_append(server, user);
174 g_hash_table_insert(servers, host, server);
175 } else {
176 sipe_subscribe_presence_single(sipe_private,
177 (void *) uri);
182 /* Send out any deferred poolFqdn subscriptions */
183 g_hash_table_foreach(servers, (GHFunc) sipe_subscribe_poolfqdn_resource_uri, sipe_private);
184 g_hash_table_destroy(servers);
186 sipe_xml_free(xn_list);
190 * Update user phone
191 * Suitable for both 2005 and 2007 systems.
193 * @param uri buddy SIP URI with 'sip:' prefix whose info we want to change.
194 * @param phone_type
195 * @param phone may be modified to strip white space
196 * @param phone_display_string may be modified to strip white space
198 static void
199 sipe_update_user_phone(struct sipe_core_private *sipe_private,
200 const gchar *uri,
201 const gchar *phone_type,
202 gchar *phone,
203 gchar *phone_display_string)
205 sipe_buddy_info_fields phone_node = SIPE_BUDDY_INFO_WORK_PHONE; /* work phone by default */
206 sipe_buddy_info_fields phone_display_node = SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY; /* work phone by default */
208 if(!phone || strlen(phone) == 0) return;
210 if ((sipe_strequal(phone_type, "mobile") || sipe_strequal(phone_type, "cell"))) {
211 phone_node = SIPE_BUDDY_INFO_MOBILE_PHONE;
212 phone_display_node = SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY;
213 } else if (sipe_strequal(phone_type, "home")) {
214 phone_node = SIPE_BUDDY_INFO_HOME_PHONE;
215 phone_display_node = SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY;
216 } else if (sipe_strequal(phone_type, "other")) {
217 phone_node = SIPE_BUDDY_INFO_OTHER_PHONE;
218 phone_display_node = SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY;
219 } else if (sipe_strequal(phone_type, "custom1")) {
220 phone_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE;
221 phone_display_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY;
224 sipe_buddy_update_property(sipe_private, uri, phone_node, phone);
225 if (phone_display_string) {
226 sipe_buddy_update_property(sipe_private, uri, phone_display_node, phone_display_string);
230 static void process_incoming_notify_msrtc(struct sipe_core_private *sipe_private,
231 const gchar *data,
232 unsigned len)
234 char *activity = NULL;
235 const char *epid;
236 const char *status_id = NULL;
237 const char *name;
238 char *uri;
239 char *self_uri = sip_uri_self(sipe_private);
240 int avl;
241 int act;
242 const char *device_name = NULL;
243 const char *cal_start_time = NULL;
244 const char *cal_granularity = NULL;
245 char *cal_free_busy_base64 = NULL;
246 struct sipe_buddy *sbuddy;
247 const sipe_xml *node;
248 sipe_xml *xn_presentity;
249 const sipe_xml *xn_availability;
250 const sipe_xml *xn_activity;
251 const sipe_xml *xn_display_name;
252 const sipe_xml *xn_email;
253 const sipe_xml *xn_phone_number;
254 const sipe_xml *xn_userinfo;
255 const sipe_xml *xn_note;
256 const sipe_xml *xn_oof;
257 const sipe_xml *xn_state;
258 const sipe_xml *xn_contact;
259 char *note;
260 int user_avail;
261 const char *user_avail_nil;
262 int res_avail;
263 time_t user_avail_since = 0;
264 time_t activity_since = 0;
266 /* fix for Reuters environment on Linux */
267 if (data && strstr(data, "encoding=\"utf-16\"")) {
268 char *tmp_data;
269 tmp_data = replace(data, "encoding=\"utf-16\"", "encoding=\"utf-8\"");
270 xn_presentity = sipe_xml_parse(tmp_data, strlen(tmp_data));
271 g_free(tmp_data);
272 } else {
273 xn_presentity = sipe_xml_parse(data, len);
276 xn_availability = sipe_xml_child(xn_presentity, "availability");
277 xn_activity = sipe_xml_child(xn_presentity, "activity");
278 xn_display_name = sipe_xml_child(xn_presentity, "displayName");
279 xn_email = sipe_xml_child(xn_presentity, "email");
280 xn_phone_number = sipe_xml_child(xn_presentity, "phoneNumber");
281 xn_userinfo = sipe_xml_child(xn_presentity, "userInfo");
282 xn_oof = xn_userinfo ? sipe_xml_child(xn_userinfo, "oof") : NULL;
283 xn_state = xn_userinfo ? sipe_xml_child(xn_userinfo, "states/state"): NULL;
284 user_avail = xn_state ? sipe_xml_int_attribute(xn_state, "avail", 0) : 0;
285 user_avail_since = xn_state ? sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since")) : 0;
286 user_avail_nil = xn_state ? sipe_xml_attribute(xn_state, "nil") : NULL;
287 xn_contact = xn_userinfo ? sipe_xml_child(xn_userinfo, "contact") : NULL;
288 xn_note = xn_userinfo ? sipe_xml_child(xn_userinfo, "note") : NULL;
289 note = xn_note ? sipe_xml_data(xn_note) : NULL;
291 if (sipe_strequal(user_avail_nil, "true")) { /* null-ed */
292 user_avail = 0;
293 user_avail_since = 0;
296 name = sipe_xml_attribute(xn_presentity, "uri"); /* without 'sip:' prefix */
297 uri = sip_uri_from_name(name);
298 avl = sipe_xml_int_attribute(xn_availability, "aggregate", 0);
299 epid = sipe_xml_attribute(xn_availability, "epid");
300 act = sipe_xml_int_attribute(xn_activity, "aggregate", 0);
302 status_id = sipe_ocs2005_status_from_activity_availability(act, avl);
303 activity = g_strdup(sipe_ocs2005_activity_description(act));
304 res_avail = sipe_ocs2007_availability_from_status(status_id, NULL);
305 if (user_avail > res_avail) {
306 res_avail = user_avail;
307 status_id = sipe_ocs2007_status_from_legacy_availability(user_avail, NULL);
310 if (xn_display_name) {
311 char *display_name = g_strdup(sipe_xml_attribute(xn_display_name, "displayName"));
312 char *email = xn_email ? g_strdup(sipe_xml_attribute(xn_email, "email")) : NULL;
313 char *phone_label = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "label")) : NULL;
314 char *phone_number = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "number")) : NULL;
315 char *tel_uri = sip_to_tel_uri(phone_number);
317 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
318 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
319 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
320 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, !is_empty(phone_label) ? phone_label : phone_number);
322 g_free(tel_uri);
323 g_free(phone_label);
324 g_free(phone_number);
325 g_free(email);
326 g_free(display_name);
329 if (xn_contact) {
330 /* tel */
331 for (node = sipe_xml_child(xn_contact, "tel"); node; node = sipe_xml_twin(node))
333 /* Ex.: <tel type="work">tel:+3222220000</tel> */
334 const char *phone_type = sipe_xml_attribute(node, "type");
335 char* phone = sipe_xml_data(node);
337 sipe_update_user_phone(sipe_private, uri, phone_type, phone, NULL);
339 g_free(phone);
343 if (xn_display_name || xn_contact)
344 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC, uri);
346 /* devicePresence */
347 for (node = sipe_xml_child(xn_presentity, "devices/devicePresence"); node; node = sipe_xml_twin(node)) {
348 const sipe_xml *xn_device_name;
349 const sipe_xml *xn_calendar_info;
350 const sipe_xml *xn_state;
351 char *state;
353 /* deviceName */
354 if (sipe_strequal(sipe_xml_attribute(node, "epid"), epid)) {
355 xn_device_name = sipe_xml_child(node, "deviceName");
356 device_name = xn_device_name ? sipe_xml_attribute(xn_device_name, "name") : NULL;
359 /* calendarInfo */
360 xn_calendar_info = sipe_xml_child(node, "calendarInfo");
361 if (xn_calendar_info) {
362 const char *cal_start_time_tmp = sipe_xml_attribute(xn_calendar_info, "startTime");
364 if (cal_start_time) {
365 time_t cal_start_time_t = sipe_utils_str_to_time(cal_start_time);
366 time_t cal_start_time_t_tmp = sipe_utils_str_to_time(cal_start_time_tmp);
368 if (cal_start_time_t_tmp > cal_start_time_t) {
369 cal_start_time = cal_start_time_tmp;
370 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
371 g_free(cal_free_busy_base64);
372 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
374 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);
376 } else {
377 cal_start_time = cal_start_time_tmp;
378 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
379 g_free(cal_free_busy_base64);
380 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
382 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);
386 /* state */
387 xn_state = sipe_xml_child(node, "states/state");
388 if (xn_state) {
389 int dev_avail = sipe_xml_int_attribute(xn_state, "avail", 0);
390 time_t dev_avail_since = sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since"));
392 state = sipe_xml_data(xn_state);
393 if (dev_avail_since > user_avail_since &&
394 dev_avail >= res_avail)
396 const gchar *new_desc;
397 res_avail = dev_avail;
398 if (!is_empty(state)) {
399 if (sipe_strequal(state, sipe_status_activity_to_token(SIPE_ACTIVITY_ON_PHONE))) {
400 g_free(activity);
401 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_ON_PHONE));
402 } else if (sipe_strequal(state, "presenting")) {
403 g_free(activity);
404 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_IN_CONF));
405 } else {
406 activity = state;
407 state = NULL;
409 activity_since = dev_avail_since;
411 status_id = sipe_ocs2007_status_from_legacy_availability(res_avail, NULL);
412 new_desc = sipe_ocs2007_legacy_activity_description(res_avail);
413 if (new_desc) {
414 g_free(activity);
415 activity = g_strdup(new_desc);
418 g_free(state);
422 /* oof */
423 if (xn_oof && res_avail >= 15000) { /* 12000 in 2007 */
424 g_free(activity);
425 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_OOF));
426 activity_since = 0;
429 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
430 if (sbuddy)
432 g_free(sbuddy->activity);
433 sbuddy->activity = activity;
434 activity = NULL;
436 sbuddy->activity_since = activity_since;
438 sbuddy->user_avail = user_avail;
439 sbuddy->user_avail_since = user_avail_since;
441 g_free(sbuddy->note);
442 sbuddy->note = NULL;
443 if (!is_empty(note)) { sbuddy->note = g_markup_escape_text(note, -1); }
445 sbuddy->is_oof_note = (xn_oof != NULL);
447 g_free(sbuddy->device_name);
448 sbuddy->device_name = NULL;
449 if (!is_empty(device_name)) { sbuddy->device_name = g_strdup(device_name); }
451 if (!is_empty(cal_free_busy_base64)) {
452 g_free(sbuddy->cal_start_time);
453 sbuddy->cal_start_time = g_strdup(cal_start_time);
455 sbuddy->cal_granularity = sipe_strcase_equal(cal_granularity, "PT15M") ? 15 : 0;
457 g_free(sbuddy->cal_free_busy_base64);
458 sbuddy->cal_free_busy_base64 = cal_free_busy_base64;
459 cal_free_busy_base64 = NULL;
461 g_free(sbuddy->cal_free_busy);
462 sbuddy->cal_free_busy = NULL;
465 sbuddy->last_non_cal_status_id = status_id;
466 g_free(sbuddy->last_non_cal_activity);
467 sbuddy->last_non_cal_activity = g_strdup(sbuddy->activity);
469 if (sipe_strcase_equal(sbuddy->name, self_uri)) {
470 if (!sipe_strequal(sbuddy->note, sipe_private->note)) /* not same */
472 if (sbuddy->is_oof_note)
473 SIPE_CORE_PRIVATE_FLAG_SET(OOF_NOTE);
474 else
475 SIPE_CORE_PRIVATE_FLAG_UNSET(OOF_NOTE);
477 g_free(sipe_private->note);
478 sipe_private->note = g_strdup(sbuddy->note);
480 sipe_private->note_since = time(NULL);
483 sipe_status_set_token(sipe_private,
484 sbuddy->last_non_cal_status_id);
487 g_free(cal_free_busy_base64);
488 g_free(activity);
490 SIPE_DEBUG_INFO("process_incoming_notify_msrtc: status(%s)", status_id);
491 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
492 sipe_status_token_to_activity(status_id));
494 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007) && sipe_strcase_equal(self_uri, uri)) {
495 sipe_ocs2005_user_info_has_updated(sipe_private, xn_userinfo);
498 g_free(note);
499 sipe_xml_free(xn_presentity);
500 g_free(uri);
501 g_free(self_uri);
504 static void process_incoming_notify_rlmi(struct sipe_core_private *sipe_private,
505 const gchar *data,
506 unsigned len)
508 const char *uri;
509 sipe_xml *xn_categories;
510 const sipe_xml *xn_category;
511 const char *status = NULL;
512 gboolean do_update_status = FALSE;
513 gboolean has_note_cleaned = FALSE;
514 gboolean has_free_busy_cleaned = FALSE;
516 xn_categories = sipe_xml_parse(data, len);
517 uri = sipe_xml_attribute(xn_categories, "uri"); /* with 'sip:' prefix */
519 for (xn_category = sipe_xml_child(xn_categories, "category");
520 xn_category ;
521 xn_category = sipe_xml_twin(xn_category) )
523 const sipe_xml *xn_node;
524 const char *tmp;
525 const char *attrVar = sipe_xml_attribute(xn_category, "name");
526 time_t publish_time = (tmp = sipe_xml_attribute(xn_category, "publishTime")) ?
527 sipe_utils_str_to_time(tmp) : 0;
529 /* contactCard */
530 if (sipe_strequal(attrVar, "contactCard"))
532 const sipe_xml *card = sipe_xml_child(xn_category, "contactCard");
534 if (card) {
535 const sipe_xml *node;
536 /* identity - Display Name and email */
537 node = sipe_xml_child(card, "identity");
538 if (node) {
539 char* display_name = sipe_xml_data(
540 sipe_xml_child(node, "name/displayName"));
541 char* email = sipe_xml_data(
542 sipe_xml_child(node, "email"));
544 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
545 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
547 g_free(display_name);
548 g_free(email);
550 /* company */
551 node = sipe_xml_child(card, "company");
552 if (node) {
553 char* company = sipe_xml_data(node);
554 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COMPANY, company);
555 g_free(company);
557 /* department */
558 node = sipe_xml_child(card, "department");
559 if (node) {
560 char* department = sipe_xml_data(node);
561 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DEPARTMENT, department);
562 g_free(department);
564 /* title */
565 node = sipe_xml_child(card, "title");
566 if (node) {
567 char* title = sipe_xml_data(node);
568 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_JOB_TITLE, title);
569 g_free(title);
571 /* office */
572 node = sipe_xml_child(card, "office");
573 if (node) {
574 char* office = sipe_xml_data(node);
575 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_OFFICE, office);
576 g_free(office);
578 /* site (url) */
579 node = sipe_xml_child(card, "url");
580 if (node) {
581 char* site = sipe_xml_data(node);
582 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_SITE, site);
583 g_free(site);
585 /* phone */
586 for (node = sipe_xml_child(card, "phone");
587 node;
588 node = sipe_xml_twin(node))
590 const char *phone_type = sipe_xml_attribute(node, "type");
591 char* phone = sipe_xml_data(sipe_xml_child(node, "uri"));
592 char* phone_display_string = sipe_xml_data(sipe_xml_child(node, "displayString"));
594 sipe_update_user_phone(sipe_private, uri, phone_type, phone, phone_display_string);
596 g_free(phone);
597 g_free(phone_display_string);
599 /* address */
600 for (node = sipe_xml_child(card, "address");
601 node;
602 node = sipe_xml_twin(node))
604 if (sipe_strequal(sipe_xml_attribute(node, "type"), "work")) {
605 char* street = sipe_xml_data(sipe_xml_child(node, "street"));
606 char* city = sipe_xml_data(sipe_xml_child(node, "city"));
607 char* state = sipe_xml_data(sipe_xml_child(node, "state"));
608 char* zipcode = sipe_xml_data(sipe_xml_child(node, "zipcode"));
609 char* country_code = sipe_xml_data(sipe_xml_child(node, "countryCode"));
611 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STREET, street);
612 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_CITY, city);
613 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STATE, state);
614 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_ZIPCODE, zipcode);
615 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COUNTRY, country_code);
617 g_free(street);
618 g_free(city);
619 g_free(state);
620 g_free(zipcode);
621 g_free(country_code);
623 break;
628 /* note */
629 else if (sipe_strequal(attrVar, "note"))
631 if (uri) {
632 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
634 if (!has_note_cleaned) {
635 has_note_cleaned = TRUE;
637 g_free(sbuddy->note);
638 sbuddy->note = NULL;
639 sbuddy->is_oof_note = FALSE;
640 sbuddy->note_since = publish_time;
642 do_update_status = TRUE;
644 if (sbuddy && (publish_time >= sbuddy->note_since)) {
645 /* clean up in case no 'note' element is supplied
646 * which indicate note removal in client
648 g_free(sbuddy->note);
649 sbuddy->note = NULL;
650 sbuddy->is_oof_note = FALSE;
651 sbuddy->note_since = publish_time;
653 xn_node = sipe_xml_child(xn_category, "note/body");
654 if (xn_node) {
655 char *tmp;
656 sbuddy->note = g_markup_escape_text((tmp = sipe_xml_data(xn_node)), -1);
657 g_free(tmp);
658 sbuddy->is_oof_note = sipe_strequal(sipe_xml_attribute(xn_node, "type"), "OOF");
659 sbuddy->note_since = publish_time;
661 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: uri(%s), note(%s)",
662 uri, sbuddy->note ? sbuddy->note : "");
664 /* to trigger UI refresh in case no status info is supplied in this update */
665 do_update_status = TRUE;
669 /* state */
670 else if(sipe_strequal(attrVar, "state"))
672 char *tmp;
673 int availability;
674 const sipe_xml *xn_availability;
675 const sipe_xml *xn_activity;
676 const sipe_xml *xn_meeting_subject;
677 const sipe_xml *xn_meeting_location;
678 struct sipe_buddy *sbuddy = uri ? g_hash_table_lookup(sipe_private->buddies, uri) : NULL;
680 xn_node = sipe_xml_child(xn_category, "state");
681 if (!xn_node) continue;
682 xn_availability = sipe_xml_child(xn_node, "availability");
683 if (!xn_availability) continue;
684 xn_activity = sipe_xml_child(xn_node, "activity");
685 xn_meeting_subject = sipe_xml_child(xn_node, "meetingSubject");
686 xn_meeting_location = sipe_xml_child(xn_node, "meetingLocation");
688 tmp = sipe_xml_data(xn_availability);
689 availability = atoi(tmp);
690 g_free(tmp);
692 /* activity, meeting_subject, meeting_location */
693 if (sbuddy) {
694 const gchar *tmp;
696 /* activity */
697 g_free(sbuddy->activity);
698 sbuddy->activity = NULL;
699 if (xn_activity) {
700 const char *token = sipe_xml_attribute(xn_activity, "token");
701 const sipe_xml *xn_custom = sipe_xml_child(xn_activity, "custom");
703 /* from token */
704 if (!is_empty(token)) {
705 sbuddy->activity = g_strdup(sipe_core_activity_description(sipe_status_token_to_activity(token)));
707 /* from custom element */
708 if (xn_custom) {
709 char *custom = sipe_xml_data(xn_custom);
711 if (!is_empty(custom)) {
712 g_free(sbuddy->activity);
713 sbuddy->activity = custom;
714 custom = NULL;
716 g_free(custom);
719 /* meeting_subject */
720 g_free(sbuddy->meeting_subject);
721 sbuddy->meeting_subject = NULL;
722 if (xn_meeting_subject) {
723 char *meeting_subject = sipe_xml_data(xn_meeting_subject);
725 if (!is_empty(meeting_subject)) {
726 sbuddy->meeting_subject = meeting_subject;
727 meeting_subject = NULL;
729 g_free(meeting_subject);
731 /* meeting_location */
732 g_free(sbuddy->meeting_location);
733 sbuddy->meeting_location = NULL;
734 if (xn_meeting_location) {
735 char *meeting_location = sipe_xml_data(xn_meeting_location);
737 if (!is_empty(meeting_location)) {
738 sbuddy->meeting_location = meeting_location;
739 meeting_location = NULL;
741 g_free(meeting_location);
744 status = sipe_ocs2007_status_from_legacy_availability(availability, NULL);
745 tmp = sipe_ocs2007_legacy_activity_description(availability);
746 if (sbuddy->activity && tmp) {
747 gchar *tmp2 = sbuddy->activity;
749 sbuddy->activity = g_strdup_printf("%s, %s", sbuddy->activity, tmp);
750 g_free(tmp2);
751 } else if (tmp) {
752 sbuddy->activity = g_strdup(tmp);
756 do_update_status = TRUE;
758 /* calendarData */
759 else if(sipe_strequal(attrVar, "calendarData"))
761 struct sipe_buddy *sbuddy = uri ? g_hash_table_lookup(sipe_private->buddies, uri) : NULL;
762 const sipe_xml *xn_free_busy = sipe_xml_child(xn_category, "calendarData/freeBusy");
763 const sipe_xml *xn_working_hours = sipe_xml_child(xn_category, "calendarData/WorkingHours");
765 if (sbuddy && xn_free_busy) {
766 if (!has_free_busy_cleaned) {
767 has_free_busy_cleaned = TRUE;
769 g_free(sbuddy->cal_start_time);
770 sbuddy->cal_start_time = NULL;
772 g_free(sbuddy->cal_free_busy_base64);
773 sbuddy->cal_free_busy_base64 = NULL;
775 g_free(sbuddy->cal_free_busy);
776 sbuddy->cal_free_busy = NULL;
778 sbuddy->cal_free_busy_published = publish_time;
781 if (publish_time >= sbuddy->cal_free_busy_published) {
782 g_free(sbuddy->cal_start_time);
783 sbuddy->cal_start_time = g_strdup(sipe_xml_attribute(xn_free_busy, "startTime"));
785 sbuddy->cal_granularity = sipe_strcase_equal(sipe_xml_attribute(xn_free_busy, "granularity"), "PT15M") ?
786 15 : 0;
788 g_free(sbuddy->cal_free_busy_base64);
789 sbuddy->cal_free_busy_base64 = sipe_xml_data(xn_free_busy);
791 g_free(sbuddy->cal_free_busy);
792 sbuddy->cal_free_busy = NULL;
794 sbuddy->cal_free_busy_published = publish_time;
796 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);
800 if (sbuddy && xn_working_hours) {
801 sipe_cal_parse_working_hours(xn_working_hours, sbuddy);
806 if (do_update_status) {
807 guint activity;
809 if (status) {
810 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: %s", status);
811 activity = sipe_status_token_to_activity(status);
812 } else {
813 /* no status category in this update,
814 using contact's current status */
815 activity = sipe_backend_buddy_get_status(SIPE_CORE_PUBLIC,
816 uri);
819 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri, activity);
822 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC, uri);
824 sipe_xml_free(xn_categories);
827 static void sipe_buddy_status_from_activity(struct sipe_core_private *sipe_private,
828 const gchar *uri,
829 const gchar *activity,
830 gboolean is_online)
832 if (is_online) {
833 const gchar *status_id = NULL;
834 if (activity) {
835 if (sipe_strequal(activity,
836 sipe_status_activity_to_token(SIPE_ACTIVITY_BUSY))) {
837 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_BUSY);
838 } else if (sipe_strequal(activity,
839 sipe_status_activity_to_token(SIPE_ACTIVITY_AWAY))) {
840 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_AWAY);
844 if (!status_id) {
845 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_AVAILABLE);
848 SIPE_DEBUG_INFO("sipe_buddy_status_from_activity: status_id(%s)", status_id);
849 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
850 sipe_status_token_to_activity(status_id));
851 } else {
852 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
853 SIPE_ACTIVITY_OFFLINE);
857 static void process_incoming_notify_pidf(struct sipe_core_private *sipe_private,
858 const gchar *data,
859 unsigned len)
861 gchar *uri;
862 gchar *getbasic;
863 gchar *activity = NULL;
864 sipe_xml *pidf;
865 const sipe_xml *basicstatus = NULL, *tuple, *status;
866 gboolean isonline = FALSE;
867 const sipe_xml *display_name_node;
869 pidf = sipe_xml_parse(data, len);
870 if (!pidf) {
871 SIPE_DEBUG_INFO("process_incoming_notify_pidf: no parseable pidf:%s", data);
872 return;
875 if ((tuple = sipe_xml_child(pidf, "tuple")))
877 if ((status = sipe_xml_child(tuple, "status"))) {
878 basicstatus = sipe_xml_child(status, "basic");
882 if (!basicstatus) {
883 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic found");
884 sipe_xml_free(pidf);
885 return;
888 getbasic = sipe_xml_data(basicstatus);
889 if (!getbasic) {
890 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic data found");
891 sipe_xml_free(pidf);
892 return;
895 SIPE_DEBUG_INFO("process_incoming_notify_pidf: basic-status(%s)", getbasic);
896 if (strstr(getbasic, "open")) {
897 isonline = TRUE;
899 g_free(getbasic);
901 uri = sip_uri(sipe_xml_attribute(pidf, "entity")); /* with 'sip:' prefix */ /* AOL comes without the prefix */
903 display_name_node = sipe_xml_child(pidf, "display-name");
904 if (display_name_node) {
905 char * display_name = sipe_xml_data(display_name_node);
907 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
908 g_free(display_name);
910 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC, uri);
913 if ((tuple = sipe_xml_child(pidf, "tuple"))) {
914 if ((status = sipe_xml_child(tuple, "status"))) {
915 if ((basicstatus = sipe_xml_child(status, "activities"))) {
916 if ((basicstatus = sipe_xml_child(basicstatus, "activity"))) {
917 activity = sipe_xml_data(basicstatus);
918 SIPE_DEBUG_INFO("process_incoming_notify_pidf: activity(%s)", activity);
924 sipe_buddy_status_from_activity(sipe_private,
925 uri,
926 activity,
927 isonline);
929 g_free(activity);
930 g_free(uri);
931 sipe_xml_free(pidf);
934 static void sipe_presence_mime_cb(gpointer user_data, /* sipe_core_private */
935 const GSList *fields,
936 const gchar *body,
937 gsize length)
939 const gchar *type = sipe_utils_nameval_find(fields, "Content-Type");
941 if (strstr(type,"application/rlmi+xml")) {
942 process_incoming_notify_rlmi_resub(user_data, body, length);
943 } else if (strstr(type, "text/xml+msrtc.pidf")) {
944 process_incoming_notify_msrtc(user_data, body, length);
945 } else {
946 process_incoming_notify_rlmi(user_data, body, length);
950 static void sipe_process_presence(struct sipe_core_private *sipe_private,
951 struct sipmsg *msg)
953 const char *ctype = sipmsg_find_header(msg, "Content-Type");
955 SIPE_DEBUG_INFO("sipe_process_presence: Content-Type: %s", ctype ? ctype : "");
957 if (ctype &&
958 (strstr(ctype, "application/rlmi+xml") ||
959 strstr(ctype, "application/msrtc-event-categories+xml")))
961 if (strstr(ctype, "multipart"))
963 sipe_mime_parts_foreach(ctype, msg->body, sipe_presence_mime_cb, sipe_private);
965 else if(strstr(ctype, "application/msrtc-event-categories+xml") )
967 process_incoming_notify_rlmi(sipe_private, msg->body, msg->bodylen);
969 else if(strstr(ctype, "application/rlmi+xml"))
971 process_incoming_notify_rlmi_resub(sipe_private, msg->body, msg->bodylen);
974 else if(ctype && strstr(ctype, "text/xml+msrtc.pidf"))
976 process_incoming_notify_msrtc(sipe_private, msg->body, msg->bodylen);
978 else
980 process_incoming_notify_pidf(sipe_private, msg->body, msg->bodylen);
985 * Fires on deregistration event initiated by server.
986 * [MS-SIPREGE] SIP extension.
988 * OCS2007 Example
990 * Content-Type: text/registration-event
991 * subscription-state: terminated;expires=0
992 * ms-diagnostics-public: 4141;reason="User disabled"
994 * deregistered;event=rejected
996 static void sipe_process_registration_notify(struct sipe_core_private *sipe_private,
997 struct sipmsg *msg)
999 const gchar *contenttype = sipmsg_find_header(msg, "Content-Type");
1000 gchar *event = NULL;
1001 gchar *reason = NULL;
1002 gchar *warning;
1004 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: deregistration received.");
1006 if (!g_ascii_strncasecmp(contenttype, "text/registration-event", 23)) {
1007 event = sipmsg_find_part_of_header(msg->body, "event=", NULL, NULL);
1008 //@TODO have proper parameter extraction _by_name_ func, case insesitive.
1009 event = event ? event : sipmsg_find_part_of_header(msg->body, "event=", ";", NULL);
1010 } else {
1011 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: unknown content type, exiting.");
1012 return;
1015 reason = sipmsg_get_ms_diagnostics_reason(msg);
1016 reason = reason ? reason : sipmsg_get_ms_diagnostics_public_reason(msg);
1017 if (!reason) { // for LCS2005
1018 if (event && sipe_strcase_equal(event, "unregistered")) {
1019 //reason = g_strdup(_("User logged out")); // [MS-OCER]
1020 reason = g_strdup(_("you are already signed in at another location"));
1021 } else if (event && sipe_strcase_equal(event, "rejected")) {
1022 reason = g_strdup(_("user disabled")); // [MS-OCER]
1023 } else if (event && sipe_strcase_equal(event, "deactivated")) {
1024 reason = g_strdup(_("user moved")); // [MS-OCER]
1027 g_free(event);
1028 warning = g_strdup_printf(_("You have been rejected by the server: %s"), reason ? reason : _("no reason given"));
1029 g_free(reason);
1031 sipe_backend_connection_error(SIPE_CORE_PUBLIC,
1032 SIPE_CONNECTION_ERROR_INVALID_USERNAME,
1033 warning);
1034 g_free(warning);
1039 * Removes entries from local buddy list
1040 * that does not correspond ones in the roaming contact list.
1042 static void sipe_cleanup_local_blist(struct sipe_core_private *sipe_private)
1044 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
1045 NULL, NULL);
1046 GSList *entry = buddies;
1047 struct sipe_buddy *buddy;
1048 sipe_backend_buddy b;
1049 gchar *bname;
1050 gchar *gname;
1052 SIPE_DEBUG_INFO("sipe_cleanup_local_blist: overall %d backend buddies (including clones)", g_slist_length(buddies));
1053 SIPE_DEBUG_INFO("sipe_cleanup_local_blist: %d sipe buddies (unique)", g_hash_table_size(sipe_private->buddies));
1054 while (entry) {
1055 b = entry->data;
1056 gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC, b);
1057 bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC, b);
1058 buddy = g_hash_table_lookup(sipe_private->buddies, bname);
1059 if(buddy) {
1060 gboolean in_sipe_groups = FALSE;
1061 GSList *entry2 = buddy->groups;
1062 while (entry2) {
1063 struct sipe_group *group = entry2->data;
1064 if (sipe_strequal(group->name, gname)) {
1065 in_sipe_groups = TRUE;
1066 break;
1068 entry2 = entry2->next;
1070 if(!in_sipe_groups) {
1071 SIPE_DEBUG_INFO("*** REMOVING %s from blist group: %s as not having this group in roaming list", bname, gname);
1072 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, b);
1074 } else {
1075 SIPE_DEBUG_INFO("*** REMOVING %s from blist group: %s as this buddy not in roaming list", bname, gname);
1076 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, b);
1078 g_free(bname);
1079 g_free(gname);
1080 entry = entry->next;
1082 g_slist_free(buddies);
1086 * A callback for g_hash_table_foreach
1088 static void sipe_buddy_subscribe_cb(char *buddy_name,
1089 SIPE_UNUSED_PARAMETER struct sipe_buddy *buddy,
1090 struct sipe_core_private *sipe_private)
1092 guint time_range = (g_hash_table_size(sipe_private->buddies) * 1000) / 25; /* time interval for 25 requests per sec. In msec. */
1095 * g_hash_table_size() can never return 0, otherwise this function
1096 * wouldn't be called :-) But to keep Coverity happy...
1098 if (time_range) {
1099 gchar *action_name = sipe_utils_presence_key(buddy_name);
1100 guint timeout = ((guint) rand()) / (RAND_MAX / time_range) + 1; /* random period within the range but never 0! */
1102 sipe_schedule_mseconds(sipe_private,
1103 action_name,
1104 g_strdup(buddy_name),
1105 timeout,
1106 sipe_subscribe_presence_single,
1107 g_free);
1108 g_free(action_name);
1112 /* Replace "~" with localized version of "Other Contacts" */
1113 static const gchar *get_group_name(const sipe_xml *node)
1115 const gchar *name = sipe_xml_attribute(node, "name");
1116 return(g_str_has_prefix(name, "~") ? _("Other Contacts") : name);
1119 static void add_new_group(struct sipe_core_private *sipe_private,
1120 const sipe_xml *node)
1122 struct sipe_group *group = g_new0(struct sipe_group, 1);
1124 group->name = g_strdup(get_group_name(node));
1125 group->id = (int)g_ascii_strtod(sipe_xml_attribute(node, "id"), NULL);
1127 sipe_group_add(sipe_private, group);
1130 static void add_new_buddy(struct sipe_core_private *sipe_private,
1131 const sipe_xml *node,
1132 const gchar *uri,
1133 const gchar *alias)
1135 const gchar *name = sipe_xml_attribute(node, "name");
1136 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
1137 gchar *normalized_uri = g_ascii_strdown(uri, -1);
1138 struct sipe_buddy *buddy = NULL;
1139 gchar *tmp;
1140 gchar **item_groups;
1141 int i = 0;
1143 /* assign to group Other Contacts if nothing else received */
1144 tmp = g_strdup(sipe_xml_attribute(node, "groups"));
1145 if (is_empty(tmp)) {
1146 struct sipe_group *group = sipe_group_find_by_name(sipe_private,
1147 _("Other Contacts"));
1148 g_free(tmp);
1149 tmp = group ? g_strdup_printf("%d", group->id) : g_strdup("1");
1151 item_groups = g_strsplit(tmp, " ", 0);
1152 g_free(tmp);
1154 while (item_groups[i]) {
1155 struct sipe_group *group = sipe_group_find_by_id(sipe_private,
1156 g_ascii_strtod(item_groups[i],
1157 NULL));
1159 /* If couldn't find the right group for this contact, */
1160 /* then just put it in the first group we have */
1161 if ((group == NULL) &&
1162 (g_slist_length(sipe_private->groups) > 0))
1163 group = sipe_private->groups->data;
1165 if (group) {
1166 sipe_backend_buddy b = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
1167 normalized_uri,
1168 group->name);
1169 gchar *b_alias;
1171 if (!b) {
1172 b = sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
1173 normalized_uri,
1174 alias,
1175 group->name);
1176 SIPE_DEBUG_INFO("Created new buddy %s with alias %s",
1177 normalized_uri, alias);
1180 b_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, b);
1181 if (sipe_strcase_equal(alias, b_alias) &&
1182 !is_empty(name)) {
1183 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
1185 name);
1186 SIPE_DEBUG_INFO("Replaced for buddy %s in group '%s' old alias '%s' with '%s'",
1187 normalized_uri, group->name, b_alias, name);
1189 g_free(b_alias);
1191 if (!buddy)
1192 buddy = sipe_buddy_add(sipe_private,
1193 normalized_uri);
1195 buddy->groups = slist_insert_unique_sorted(buddy->groups,
1196 group,
1197 (GCompareFunc)sipe_group_compare);
1199 SIPE_DEBUG_INFO("Added buddy %s to group %s",
1200 buddy->name, group->name);
1201 } else {
1202 SIPE_DEBUG_INFO("No group found for contact %s! Unable to add to buddy list",
1203 name);
1206 i++;
1209 g_strfreev(item_groups);
1210 g_free(normalized_uri);
1213 static gboolean sipe_process_roaming_contacts(struct sipe_core_private *sipe_private,
1214 struct sipmsg *msg)
1216 int len = msg->bodylen;
1218 const gchar *tmp = sipmsg_find_header(msg, "Event");
1219 const sipe_xml *item;
1220 sipe_xml *isc;
1221 guint delta;
1222 const sipe_xml *group_node;
1224 if (!g_str_has_prefix(tmp, "vnd-microsoft-roaming-contacts")) {
1225 return FALSE;
1228 /* Convert the contact from XML to backend Buddies */
1229 isc = sipe_xml_parse(msg->body, len);
1230 if (!isc) {
1231 return FALSE;
1234 /* [MS-SIP]: deltaNum MUST be non-zero */
1235 delta = sipe_xml_int_attribute(isc, "deltaNum", 0);
1236 if (delta) {
1237 sipe_private->deltanum_contacts = delta;
1240 /* Process whole buddy list (only sent once?) */
1241 if (sipe_strequal(sipe_xml_name(isc), "contactList")) {
1243 /* Start processing contact list */
1244 sipe_backend_buddy_list_processing_start(SIPE_CORE_PUBLIC);
1246 /* Parse groups */
1247 for (group_node = sipe_xml_child(isc, "group"); group_node; group_node = sipe_xml_twin(group_node))
1248 add_new_group(sipe_private, group_node);
1250 /* Make sure we have at least one group */
1251 if (g_slist_length(sipe_private->groups) == 0) {
1252 sipe_group_create(sipe_private, _("Other Contacts"), NULL);
1255 /* Parse contacts */
1256 for (item = sipe_xml_child(isc, "contact"); item; item = sipe_xml_twin(item)) {
1257 const gchar *name = sipe_xml_attribute(item, "uri");
1258 gchar *uri = sip_uri_from_name(name);
1259 add_new_buddy(sipe_private, item, uri, name);
1260 g_free(uri);
1263 sipe_cleanup_local_blist(sipe_private);
1265 /* Add self-contact if not there yet. 2005 systems. */
1266 /* This will resemble subscription to roaming_self in 2007 systems */
1267 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1268 gchar *self_uri = sip_uri_self(sipe_private);
1269 sipe_buddy_add(sipe_private, self_uri);
1270 g_free(self_uri);
1273 /* Finished processing contact list */
1274 sipe_backend_buddy_list_processing_finish(SIPE_CORE_PUBLIC);
1276 /* Process buddy list updates */
1277 } else if (sipe_strequal(sipe_xml_name(isc), "contactDelta")) {
1279 /* Process new groups */
1280 for (group_node = sipe_xml_child(isc, "addedGroup"); group_node; group_node = sipe_xml_twin(group_node))
1281 add_new_group(sipe_private, group_node);
1283 /* Process modified groups */
1284 for (group_node = sipe_xml_child(isc, "modifiedGroup"); group_node; group_node = sipe_xml_twin(group_node)) {
1285 struct sipe_group *group = sipe_group_find_by_id(sipe_private,
1286 (int)g_ascii_strtod(sipe_xml_attribute(group_node, "id"),
1287 NULL));
1288 if (group) {
1289 const gchar *name = get_group_name(group_node);
1291 if (!(is_empty(name) ||
1292 sipe_strequal(group->name, name)) &&
1293 sipe_group_rename(sipe_private,
1294 group,
1295 name))
1296 SIPE_DEBUG_INFO("Replaced group %d name with %s", group->id, name);
1300 /* Process new buddies */
1301 for (item = sipe_xml_child(isc, "addedContact"); item; item = sipe_xml_twin(item)) {
1302 const gchar *uri = sipe_xml_attribute(item, "uri");
1303 const gchar *name = sipe_get_no_sip_uri(uri);
1304 add_new_buddy(sipe_private, item, uri, name);
1307 /* Process modified buddies */
1308 for (item = sipe_xml_child(isc, "modifiedContact"); item; item = sipe_xml_twin(item)) {
1309 const gchar *uri = sipe_xml_attribute(item, "uri");
1310 struct sipe_buddy *buddy = g_hash_table_lookup(sipe_private->buddies,
1311 uri);
1313 if (buddy) {
1314 gchar **item_groups = g_strsplit(sipe_xml_attribute(item,
1315 "groups"),
1316 " ", 0);
1318 /* this should be defined. Otherwise we would get "deletedContact" */
1319 if (item_groups) {
1320 const gchar *name = sipe_xml_attribute(item, "name");
1321 gboolean empty_name = is_empty(name);
1322 GSList *found = NULL;
1323 GSList *entry;
1324 int i = 0;
1326 while (item_groups[i]) {
1327 struct sipe_group *group = sipe_group_find_by_id(sipe_private,
1328 g_ascii_strtod(item_groups[i],
1329 NULL));
1330 /* ignore unkown groups */
1331 if (group) {
1332 sipe_backend_buddy b = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
1333 uri,
1334 group->name);
1336 /* add group to found list */
1337 found = g_slist_prepend(found, group);
1339 if (b) {
1340 /* new alias? */
1341 gchar *b_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
1344 if (!(empty_name ||
1345 sipe_strequal(b_alias, name))) {
1346 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
1348 name);
1349 SIPE_DEBUG_INFO("Replaced for buddy %s in group '%s' old alias '%s' with '%s'",
1350 uri, group->name, b_alias, name);
1352 g_free(b_alias);
1354 } else {
1355 const gchar *alias = empty_name ? uri : name;
1356 /* buddy was not in this group */
1357 sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
1358 uri,
1359 alias,
1360 group->name);
1361 buddy->groups = slist_insert_unique_sorted(buddy->groups,
1362 group,
1363 (GCompareFunc) sipe_group_compare);
1364 SIPE_DEBUG_INFO("Added buddy %s (alias '%s' to group '%s'",
1365 uri, alias, group->name);
1369 /* next group */
1370 i++;
1372 g_strfreev(item_groups);
1374 /* removed from groups? */
1375 entry = buddy->groups;
1376 while (entry) {
1377 GSList *remove_link = entry;
1378 struct sipe_group *group = remove_link->data;
1380 /* next buddy group */
1381 entry = entry->next;
1383 /* old group NOT found in new list? */
1384 if (g_slist_find(found, group) == NULL) {
1385 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
1386 uri,
1387 group->name);
1388 SIPE_DEBUG_INFO("Removing buddy %s from group '%s'",
1389 uri, group->name);
1390 /* this should never be NULL */
1391 if (oldb)
1392 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
1393 oldb);
1394 buddy->groups = g_slist_remove_link(buddy->groups,
1395 remove_link);
1398 g_slist_free(found);
1403 /* Process deleted buddies */
1404 for (item = sipe_xml_child(isc, "deletedContact"); item; item = sipe_xml_twin(item)) {
1405 const gchar *uri = sipe_xml_attribute(item, "uri");
1406 struct sipe_buddy *buddy = g_hash_table_lookup(sipe_private->buddies,
1407 uri);
1409 if (buddy) {
1410 GSList *entry = buddy->groups;
1412 SIPE_DEBUG_INFO("Removing buddy %s", uri);
1413 while (entry) {
1414 struct sipe_group *group = entry->data;
1415 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
1416 uri,
1417 group->name);
1418 /* this should never be NULL */
1419 if (oldb)
1420 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
1421 oldb);
1423 /* next buddy group */
1424 entry = entry->next;
1426 sipe_buddy_remove(sipe_private, buddy);
1430 /* Process deleted groups
1432 * NOTE: all buddies will already have been removed from the
1433 * group prior to this. The log shows that OCS actually
1434 * sends two separate updates when you delete a group:
1436 * - first one with "modifiedContact" removing buddies
1437 * from the group, leaving it empty, and
1439 * - then one with "deletedGroup" removing the group
1441 for (group_node = sipe_xml_child(isc, "deletedGroup"); group_node; group_node = sipe_xml_twin(group_node))
1442 sipe_group_remove(sipe_private,
1443 sipe_group_find_by_id(sipe_private,
1444 (int)g_ascii_strtod(sipe_xml_attribute(group_node, "id"),
1445 NULL)));
1448 sipe_xml_free(isc);
1450 /* subscribe to buddies */
1451 if (!SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
1452 /* do it once, then count Expire field to schedule resubscribe */
1453 if (SIPE_CORE_PRIVATE_FLAG_IS(BATCHED_SUPPORT)) {
1454 sipe_subscribe_presence_batched(sipe_private);
1455 } else {
1456 g_hash_table_foreach(sipe_private->buddies,
1457 (GHFunc)sipe_buddy_subscribe_cb,
1458 sipe_private);
1460 SIPE_CORE_PRIVATE_FLAG_SET(SUBSCRIBED_BUDDIES);
1462 /* for 2005 systems schedule contacts' status update
1463 * based on their calendar information
1465 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1466 sipe_ocs2005_schedule_status_update(sipe_private, time(NULL));
1469 return 0;
1472 static void sipe_process_roaming_acl(struct sipe_core_private *sipe_private,
1473 struct sipmsg *msg)
1475 guint delta;
1476 sipe_xml *xml;
1478 xml = sipe_xml_parse(msg->body, msg->bodylen);
1479 if (!xml)
1480 return;
1482 /* [MS-SIP]: deltaNum MUST be non-zero */
1483 delta = sipe_xml_int_attribute(xml, "deltaNum", 0);
1484 if (delta) {
1485 sipe_private->deltanum_acl = delta;
1488 sipe_xml_free(xml);
1491 struct sipe_auth_job {
1492 gchar *who;
1493 struct sipe_core_private *sipe_private;
1496 void sipe_core_contact_allow_deny(struct sipe_core_public *sipe_public,
1497 const gchar* who,
1498 gboolean allow)
1500 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1502 if (allow) {
1503 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: authorizing contact %s", who);
1504 } else {
1505 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: blocking contact %s", who);
1508 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1509 sipe_ocs2007_change_access_level(sipe_private,
1510 (allow ? -1 : 32000),
1511 "user",
1512 sipe_get_no_sip_uri(who));
1513 } else {
1514 sip_soap_ocs2005_setacl(sipe_private, who, allow);
1519 static void sipe_auth_user_cb(gpointer data)
1521 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1522 if (!job) return;
1524 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1525 job->who,
1526 TRUE);
1527 g_free(job);
1530 static void sipe_deny_user_cb(gpointer data)
1532 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1533 if (!job) return;
1535 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1536 job->who,
1537 FALSE);
1538 g_free(job);
1541 /* OCS2005- */
1542 static void sipe_process_presence_wpending (struct sipe_core_private *sipe_private,
1543 struct sipmsg * msg)
1545 sipe_xml *watchers;
1546 const sipe_xml *watcher;
1547 // Ensure it's either not a response (eg it's a BENOTIFY) or that it's a 200 OK response
1548 if (msg->response != 0 && msg->response != 200) return;
1550 if (msg->bodylen == 0 || msg->body == NULL || sipe_strequal(sipmsg_find_header(msg, "Event"), "msrtc.wpending")) return;
1552 watchers = sipe_xml_parse(msg->body, msg->bodylen);
1553 if (!watchers) return;
1555 for (watcher = sipe_xml_child(watchers, "watcher"); watcher; watcher = sipe_xml_twin(watcher)) {
1556 gchar * remote_user = g_strdup(sipe_xml_attribute(watcher, "uri"));
1557 gchar * alias = g_strdup(sipe_xml_attribute(watcher, "displayName"));
1558 gboolean on_list = g_hash_table_lookup(sipe_private->buddies, remote_user) != NULL;
1560 // TODO pull out optional displayName to pass as alias
1561 if (remote_user) {
1562 struct sipe_auth_job * job = g_new0(struct sipe_auth_job, 1);
1563 job->who = remote_user;
1564 job->sipe_private = sipe_private;
1565 sipe_backend_buddy_request_authorization(SIPE_CORE_PUBLIC,
1566 remote_user,
1567 alias,
1568 on_list,
1569 sipe_auth_user_cb,
1570 sipe_deny_user_cb,
1571 (gpointer)job);
1576 sipe_xml_free(watchers);
1577 return;
1580 static void sipe_presence_timeout_mime_cb(gpointer user_data,
1581 SIPE_UNUSED_PARAMETER const GSList *fields,
1582 const gchar *body,
1583 gsize length)
1585 GSList **buddies = user_data;
1586 sipe_xml *xml = sipe_xml_parse(body, length);
1588 if (xml && !sipe_strequal(sipe_xml_name(xml), "list")) {
1589 const gchar *uri = sipe_xml_attribute(xml, "uri");
1590 const sipe_xml *xn_category;
1593 * automaton: presence is never expected to change
1595 * see: http://msdn.microsoft.com/en-us/library/ee354295(office.13).aspx
1597 for (xn_category = sipe_xml_child(xml, "category");
1598 xn_category;
1599 xn_category = sipe_xml_twin(xn_category)) {
1600 if (sipe_strequal(sipe_xml_attribute(xn_category, "name"),
1601 "contactCard")) {
1602 const sipe_xml *node = sipe_xml_child(xn_category, "contactCard/automaton");
1603 if (node) {
1604 char *boolean = sipe_xml_data(node);
1605 if (sipe_strequal(boolean, "true")) {
1606 SIPE_DEBUG_INFO("sipe_process_presence_timeout: %s is an automaton: - not subscribing to presence updates",
1607 uri);
1608 uri = NULL;
1610 g_free(boolean);
1612 break;
1616 if (uri) {
1617 *buddies = g_slist_append(*buddies, sip_uri(uri));
1621 sipe_xml_free(xml);
1624 static void sipe_process_presence_timeout(struct sipe_core_private *sipe_private,
1625 struct sipmsg *msg,
1626 const gchar *who,
1627 int timeout)
1629 const char *ctype = sipmsg_find_header(msg, "Content-Type");
1630 gchar *action_name = sipe_utils_presence_key(who);
1632 SIPE_DEBUG_INFO("sipe_process_presence_timeout: Content-Type: %s", ctype ? ctype : "");
1634 if (ctype &&
1635 strstr(ctype, "multipart") &&
1636 (strstr(ctype, "application/rlmi+xml") ||
1637 strstr(ctype, "application/msrtc-event-categories+xml"))) {
1638 GSList *buddies = NULL;
1640 sipe_mime_parts_foreach(ctype, msg->body, sipe_presence_timeout_mime_cb, &buddies);
1642 if (buddies)
1643 sipe_subscribe_presence_batched_schedule(sipe_private,
1644 action_name,
1645 who,
1646 buddies,
1647 timeout);
1649 } else {
1650 sipe_schedule_seconds(sipe_private,
1651 action_name,
1652 g_strdup(who),
1653 timeout,
1654 sipe_subscribe_presence_single,
1655 g_free);
1656 SIPE_DEBUG_INFO("Resubscription single contact with batched support(%s) in %d", who, timeout);
1658 g_free(action_name);
1662 * Dispatcher for all incoming subscription information
1663 * whether it comes from NOTIFY, BENOTIFY requests or
1664 * piggy-backed to subscription's OK responce.
1666 * @param request whether initiated from BE/NOTIFY request or OK-response message.
1667 * @param benotify whether initiated from NOTIFY or BENOTIFY request.
1669 void process_incoming_notify(struct sipe_core_private *sipe_private,
1670 struct sipmsg *msg,
1671 gboolean request, gboolean benotify)
1673 const gchar *content_type = sipmsg_find_header(msg, "Content-Type");
1674 const gchar *event = sipmsg_find_header(msg, "Event");
1675 const gchar *subscription_state = sipmsg_find_header(msg, "subscription-state");
1677 SIPE_DEBUG_INFO("process_incoming_notify: subscription_state: %s", subscription_state ? subscription_state : "");
1679 /* implicit subscriptions */
1680 if (content_type && g_str_has_prefix(content_type, "application/ms-imdn+xml")) {
1681 sipe_process_imdn(sipe_private, msg);
1684 if (event) {
1685 /* for one off subscriptions (send with Expire: 0) */
1686 if (sipe_strcase_equal(event, "vnd-microsoft-provisioning-v2"))
1688 sipe_process_provisioning_v2(sipe_private, msg);
1690 else if (sipe_strcase_equal(event, "vnd-microsoft-provisioning"))
1692 sipe_process_provisioning(sipe_private, msg);
1694 else if (sipe_strcase_equal(event, "presence"))
1696 sipe_process_presence(sipe_private, msg);
1698 else if (sipe_strcase_equal(event, "registration-notify"))
1700 sipe_process_registration_notify(sipe_private, msg);
1703 if (!subscription_state || strstr(subscription_state, "active"))
1705 if (sipe_strcase_equal(event, "vnd-microsoft-roaming-contacts"))
1707 sipe_process_roaming_contacts(sipe_private, msg);
1709 else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-self"))
1711 sipe_ocs2007_process_roaming_self(sipe_private, msg);
1713 else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-ACL"))
1715 sipe_process_roaming_acl(sipe_private, msg);
1717 else if (sipe_strcase_equal(event, "presence.wpending"))
1719 sipe_process_presence_wpending(sipe_private, msg);
1721 else if (sipe_strcase_equal(event, "conference"))
1723 sipe_process_conference(sipe_private, msg);
1728 /* The server sends status 'terminated' */
1729 if (subscription_state && strstr(subscription_state, "terminated") ) {
1730 gchar *who = parse_from(sipmsg_find_header(msg, request ? "From" : "To"));
1731 gchar *key = sipe_utils_subscription_key(event, who);
1733 SIPE_DEBUG_INFO("process_incoming_notify: server says that subscription to %s was terminated.", who);
1734 g_free(who);
1736 sipe_subscriptions_remove(sipe_private, key);
1737 g_free(key);
1740 if (!request && event) {
1741 const gchar *expires_header = sipmsg_find_header(msg, "Expires");
1742 int timeout = expires_header ? strtol(expires_header, NULL, 10) : 0;
1743 SIPE_DEBUG_INFO("process_incoming_notify: subscription expires:%d", timeout);
1745 if (timeout) {
1746 /* 2 min ahead of expiration */
1747 timeout = (timeout - 120) > 120 ? (timeout - 120) : timeout;
1749 if (sipe_strcase_equal(event, "presence.wpending") &&
1750 g_slist_find_custom(sipe_private->allowed_events, "presence.wpending", (GCompareFunc)g_ascii_strcasecmp))
1752 gchar *action_name = g_strdup_printf("<%s>", "presence.wpending");
1753 sipe_schedule_seconds(sipe_private,
1754 action_name,
1755 NULL,
1756 timeout,
1757 sipe_subscribe_presence_wpending,
1758 NULL);
1759 g_free(action_name);
1761 else if (sipe_strcase_equal(event, "presence") &&
1762 g_slist_find_custom(sipe_private->allowed_events, "presence", (GCompareFunc)g_ascii_strcasecmp))
1764 gchar *who = parse_from(sipmsg_find_header(msg, "To"));
1765 gchar *action_name = sipe_utils_presence_key(who);
1767 if (SIPE_CORE_PRIVATE_FLAG_IS(BATCHED_SUPPORT)) {
1768 sipe_process_presence_timeout(sipe_private, msg, who, timeout);
1770 else {
1771 sipe_schedule_seconds(sipe_private,
1772 action_name,
1773 g_strdup(who),
1774 timeout,
1775 sipe_subscribe_presence_single,
1776 g_free);
1777 SIPE_DEBUG_INFO("Resubscription single contact (%s) in %d", who, timeout);
1779 g_free(action_name);
1780 g_free(who);
1785 /* The client responses on received a NOTIFY message */
1786 if (request && !benotify)
1788 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1793 Local Variables:
1794 mode: c
1795 c-file-style: "bsd"
1796 indent-tabs-mode: t
1797 tab-width: 8
1798 End: