core: store addressbook URI in sipe_core_private
[siplcs.git] / src / core / sipe-notify.c
blobfb78c718928f37c18c3a3e4fed11632bd66cfa7c
1 /**
2 * @file sipe-notify.c
4 * pidgin-sipe
6 * Copyright (C) 2011 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);
133 static void process_incoming_notify_rlmi_resub(struct sipe_core_private *sipe_private,
134 const gchar *data, unsigned len)
136 sipe_xml *xn_list;
137 const sipe_xml *xn_resource;
138 GHashTable *servers = g_hash_table_new_full(g_str_hash, g_str_equal,
139 g_free, NULL);
140 GSList *server;
141 gchar *host;
143 xn_list = sipe_xml_parse(data, len);
145 for (xn_resource = sipe_xml_child(xn_list, "resource");
146 xn_resource;
147 xn_resource = sipe_xml_twin(xn_resource) )
149 const char *uri, *state;
150 const sipe_xml *xn_instance;
152 xn_instance = sipe_xml_child(xn_resource, "instance");
153 if (!xn_instance) continue;
155 uri = sipe_xml_attribute(xn_resource, "uri");
156 state = sipe_xml_attribute(xn_instance, "state");
157 SIPE_DEBUG_INFO("process_incoming_notify_rlmi_resub: uri(%s),state(%s)", uri, state);
159 if (strstr(state, "resubscribe")) {
160 const char *poolFqdn = sipe_xml_attribute(xn_instance, "poolFqdn");
162 if (poolFqdn) { //[MS-PRES] Section 3.4.5.1.3 Processing Details
163 gchar *user = g_strdup(uri);
164 host = g_strdup(poolFqdn);
165 server = g_hash_table_lookup(servers, host);
166 server = g_slist_append(server, user);
167 g_hash_table_insert(servers, host, server);
168 } else {
169 sipe_subscribe_presence_single(sipe_private,
170 (void *) uri);
175 /* Send out any deferred poolFqdn subscriptions */
176 g_hash_table_foreach(servers, (GHFunc) sipe_subscribe_poolfqdn_resource_uri, sipe_private);
177 g_hash_table_destroy(servers);
179 sipe_xml_free(xn_list);
183 * Update user phone
184 * Suitable for both 2005 and 2007 systems.
186 * @param uri buddy SIP URI with 'sip:' prefix whose info we want to change.
187 * @param phone_type
188 * @param phone may be modified to strip white space
189 * @param phone_display_string may be modified to strip white space
191 static void
192 sipe_update_user_phone(struct sipe_core_private *sipe_private,
193 const gchar *uri,
194 const gchar *phone_type,
195 gchar *phone,
196 gchar *phone_display_string)
198 sipe_buddy_info_fields phone_node = SIPE_BUDDY_INFO_WORK_PHONE; /* work phone by default */
199 sipe_buddy_info_fields phone_display_node = SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY; /* work phone by default */
201 if(!phone || strlen(phone) == 0) return;
203 if ((sipe_strequal(phone_type, "mobile") || sipe_strequal(phone_type, "cell"))) {
204 phone_node = SIPE_BUDDY_INFO_MOBILE_PHONE;
205 phone_display_node = SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY;
206 } else if (sipe_strequal(phone_type, "home")) {
207 phone_node = SIPE_BUDDY_INFO_HOME_PHONE;
208 phone_display_node = SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY;
209 } else if (sipe_strequal(phone_type, "other")) {
210 phone_node = SIPE_BUDDY_INFO_OTHER_PHONE;
211 phone_display_node = SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY;
212 } else if (sipe_strequal(phone_type, "custom1")) {
213 phone_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE;
214 phone_display_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY;
217 sipe_buddy_update_property(sipe_private, uri, phone_node, phone);
218 if (phone_display_string) {
219 sipe_buddy_update_property(sipe_private, uri, phone_display_node, phone_display_string);
223 static void process_incoming_notify_msrtc(struct sipe_core_private *sipe_private,
224 const gchar *data,
225 unsigned len)
227 char *activity = NULL;
228 const char *epid;
229 const char *status_id = NULL;
230 const char *name;
231 char *uri;
232 char *self_uri = sip_uri_self(sipe_private);
233 int avl;
234 int act;
235 const char *device_name = NULL;
236 const char *cal_start_time = NULL;
237 const char *cal_granularity = NULL;
238 char *cal_free_busy_base64 = NULL;
239 struct sipe_buddy *sbuddy;
240 const sipe_xml *node;
241 sipe_xml *xn_presentity;
242 const sipe_xml *xn_availability;
243 const sipe_xml *xn_activity;
244 const sipe_xml *xn_display_name;
245 const sipe_xml *xn_email;
246 const sipe_xml *xn_phone_number;
247 const sipe_xml *xn_userinfo;
248 const sipe_xml *xn_note;
249 const sipe_xml *xn_oof;
250 const sipe_xml *xn_state;
251 const sipe_xml *xn_contact;
252 char *note;
253 int user_avail;
254 const char *user_avail_nil;
255 int res_avail;
256 time_t user_avail_since = 0;
257 time_t activity_since = 0;
259 /* fix for Reuters environment on Linux */
260 if (data && strstr(data, "encoding=\"utf-16\"")) {
261 char *tmp_data;
262 tmp_data = replace(data, "encoding=\"utf-16\"", "encoding=\"utf-8\"");
263 xn_presentity = sipe_xml_parse(tmp_data, strlen(tmp_data));
264 g_free(tmp_data);
265 } else {
266 xn_presentity = sipe_xml_parse(data, len);
269 xn_availability = sipe_xml_child(xn_presentity, "availability");
270 xn_activity = sipe_xml_child(xn_presentity, "activity");
271 xn_display_name = sipe_xml_child(xn_presentity, "displayName");
272 xn_email = sipe_xml_child(xn_presentity, "email");
273 xn_phone_number = sipe_xml_child(xn_presentity, "phoneNumber");
274 xn_userinfo = sipe_xml_child(xn_presentity, "userInfo");
275 xn_oof = xn_userinfo ? sipe_xml_child(xn_userinfo, "oof") : NULL;
276 xn_state = xn_userinfo ? sipe_xml_child(xn_userinfo, "states/state"): NULL;
277 user_avail = xn_state ? sipe_xml_int_attribute(xn_state, "avail", 0) : 0;
278 user_avail_since = xn_state ? sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since")) : 0;
279 user_avail_nil = xn_state ? sipe_xml_attribute(xn_state, "nil") : NULL;
280 xn_contact = xn_userinfo ? sipe_xml_child(xn_userinfo, "contact") : NULL;
281 xn_note = xn_userinfo ? sipe_xml_child(xn_userinfo, "note") : NULL;
282 note = xn_note ? sipe_xml_data(xn_note) : NULL;
284 if (sipe_strequal(user_avail_nil, "true")) { /* null-ed */
285 user_avail = 0;
286 user_avail_since = 0;
289 name = sipe_xml_attribute(xn_presentity, "uri"); /* without 'sip:' prefix */
290 uri = sip_uri_from_name(name);
291 avl = sipe_xml_int_attribute(xn_availability, "aggregate", 0);
292 epid = sipe_xml_attribute(xn_availability, "epid");
293 act = sipe_xml_int_attribute(xn_activity, "aggregate", 0);
295 status_id = sipe_ocs2005_status_from_activity_availability(act, avl);
296 activity = g_strdup(sipe_ocs2005_activity_description(act));
297 res_avail = sipe_ocs2007_availability_from_status(status_id, NULL);
298 if (user_avail > res_avail) {
299 res_avail = user_avail;
300 status_id = sipe_ocs2007_status_from_legacy_availability(user_avail);
303 if (xn_display_name) {
304 char *display_name = g_strdup(sipe_xml_attribute(xn_display_name, "displayName"));
305 char *email = xn_email ? g_strdup(sipe_xml_attribute(xn_email, "email")) : NULL;
306 char *phone_label = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "label")) : NULL;
307 char *phone_number = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "number")) : NULL;
308 char *tel_uri = sip_to_tel_uri(phone_number);
310 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
311 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
312 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
313 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, !is_empty(phone_label) ? phone_label : phone_number);
315 g_free(tel_uri);
316 g_free(phone_label);
317 g_free(phone_number);
318 g_free(email);
319 g_free(display_name);
322 if (xn_contact) {
323 /* tel */
324 for (node = sipe_xml_child(xn_contact, "tel"); node; node = sipe_xml_twin(node))
326 /* Ex.: <tel type="work">tel:+3222220000</tel> */
327 const char *phone_type = sipe_xml_attribute(node, "type");
328 char* phone = sipe_xml_data(node);
330 sipe_update_user_phone(sipe_private, uri, phone_type, phone, NULL);
332 g_free(phone);
336 /* devicePresence */
337 for (node = sipe_xml_child(xn_presentity, "devices/devicePresence"); node; node = sipe_xml_twin(node)) {
338 const sipe_xml *xn_device_name;
339 const sipe_xml *xn_calendar_info;
340 const sipe_xml *xn_state;
341 char *state;
343 /* deviceName */
344 if (sipe_strequal(sipe_xml_attribute(node, "epid"), epid)) {
345 xn_device_name = sipe_xml_child(node, "deviceName");
346 device_name = xn_device_name ? sipe_xml_attribute(xn_device_name, "name") : NULL;
349 /* calendarInfo */
350 xn_calendar_info = sipe_xml_child(node, "calendarInfo");
351 if (xn_calendar_info) {
352 const char *cal_start_time_tmp = sipe_xml_attribute(xn_calendar_info, "startTime");
354 if (cal_start_time) {
355 time_t cal_start_time_t = sipe_utils_str_to_time(cal_start_time);
356 time_t cal_start_time_t_tmp = sipe_utils_str_to_time(cal_start_time_tmp);
358 if (cal_start_time_t_tmp > cal_start_time_t) {
359 cal_start_time = cal_start_time_tmp;
360 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
361 g_free(cal_free_busy_base64);
362 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
364 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);
366 } else {
367 cal_start_time = cal_start_time_tmp;
368 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
369 g_free(cal_free_busy_base64);
370 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
372 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 /* state */
377 xn_state = sipe_xml_child(node, "states/state");
378 if (xn_state) {
379 int dev_avail = sipe_xml_int_attribute(xn_state, "avail", 0);
380 time_t dev_avail_since = sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since"));
382 state = sipe_xml_data(xn_state);
383 if (dev_avail_since > user_avail_since &&
384 dev_avail >= res_avail)
386 const gchar *new_desc;
387 res_avail = dev_avail;
388 if (!is_empty(state)) {
389 if (sipe_strequal(state, sipe_status_activity_to_token(SIPE_ACTIVITY_ON_PHONE))) {
390 g_free(activity);
391 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_ON_PHONE));
392 } else if (sipe_strequal(state, "presenting")) {
393 g_free(activity);
394 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_IN_CONF));
395 } else {
396 activity = state;
397 state = NULL;
399 activity_since = dev_avail_since;
401 status_id = sipe_ocs2007_status_from_legacy_availability(res_avail);
402 new_desc = sipe_ocs2007_legacy_activity_description(res_avail);
403 if (new_desc) {
404 g_free(activity);
405 activity = g_strdup(new_desc);
408 g_free(state);
412 /* oof */
413 if (xn_oof && res_avail >= 15000) { /* 12000 in 2007 */
414 g_free(activity);
415 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_OOF));
416 activity_since = 0;
419 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
420 if (sbuddy)
422 g_free(sbuddy->activity);
423 sbuddy->activity = activity;
424 activity = NULL;
426 sbuddy->activity_since = activity_since;
428 sbuddy->user_avail = user_avail;
429 sbuddy->user_avail_since = user_avail_since;
431 g_free(sbuddy->note);
432 sbuddy->note = NULL;
433 if (!is_empty(note)) { sbuddy->note = g_markup_escape_text(note, -1); }
435 sbuddy->is_oof_note = (xn_oof != NULL);
437 g_free(sbuddy->device_name);
438 sbuddy->device_name = NULL;
439 if (!is_empty(device_name)) { sbuddy->device_name = g_strdup(device_name); }
441 if (!is_empty(cal_free_busy_base64)) {
442 g_free(sbuddy->cal_start_time);
443 sbuddy->cal_start_time = g_strdup(cal_start_time);
445 sbuddy->cal_granularity = sipe_strcase_equal(cal_granularity, "PT15M") ? 15 : 0;
447 g_free(sbuddy->cal_free_busy_base64);
448 sbuddy->cal_free_busy_base64 = cal_free_busy_base64;
449 cal_free_busy_base64 = NULL;
451 g_free(sbuddy->cal_free_busy);
452 sbuddy->cal_free_busy = NULL;
455 sbuddy->last_non_cal_status_id = status_id;
456 g_free(sbuddy->last_non_cal_activity);
457 sbuddy->last_non_cal_activity = g_strdup(sbuddy->activity);
459 if (sipe_strcase_equal(sbuddy->name, self_uri)) {
460 if (!sipe_strequal(sbuddy->note, sipe_private->note)) /* not same */
462 if (sbuddy->is_oof_note)
463 SIPE_CORE_PRIVATE_FLAG_SET(OOF_NOTE);
464 else
465 SIPE_CORE_PRIVATE_FLAG_UNSET(OOF_NOTE);
467 g_free(sipe_private->note);
468 sipe_private->note = g_strdup(sbuddy->note);
470 sipe_private->note_since = time(NULL);
473 sipe_status_set_token(sipe_private,
474 sbuddy->last_non_cal_status_id);
477 g_free(cal_free_busy_base64);
478 g_free(activity);
480 SIPE_DEBUG_INFO("process_incoming_notify_msrtc: status(%s)", status_id);
481 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
482 sipe_status_token_to_activity(status_id));
484 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007) && sipe_strcase_equal(self_uri, uri)) {
485 sipe_ocs2005_user_info_has_updated(sipe_private, xn_userinfo);
488 g_free(note);
489 sipe_xml_free(xn_presentity);
490 g_free(uri);
491 g_free(self_uri);
494 static void process_incoming_notify_rlmi(struct sipe_core_private *sipe_private,
495 const gchar *data,
496 unsigned len)
498 const char *uri;
499 sipe_xml *xn_categories;
500 const sipe_xml *xn_category;
501 const char *status = NULL;
502 gboolean do_update_status = FALSE;
503 gboolean has_note_cleaned = FALSE;
504 gboolean has_free_busy_cleaned = FALSE;
506 xn_categories = sipe_xml_parse(data, len);
507 uri = sipe_xml_attribute(xn_categories, "uri"); /* with 'sip:' prefix */
509 for (xn_category = sipe_xml_child(xn_categories, "category");
510 xn_category ;
511 xn_category = sipe_xml_twin(xn_category) )
513 const sipe_xml *xn_node;
514 const char *tmp;
515 const char *attrVar = sipe_xml_attribute(xn_category, "name");
516 time_t publish_time = (tmp = sipe_xml_attribute(xn_category, "publishTime")) ?
517 sipe_utils_str_to_time(tmp) : 0;
519 /* contactCard */
520 if (sipe_strequal(attrVar, "contactCard"))
522 const sipe_xml *card = sipe_xml_child(xn_category, "contactCard");
524 if (card) {
525 const sipe_xml *node;
526 /* identity - Display Name and email */
527 node = sipe_xml_child(card, "identity");
528 if (node) {
529 char* display_name = sipe_xml_data(
530 sipe_xml_child(node, "name/displayName"));
531 char* email = sipe_xml_data(
532 sipe_xml_child(node, "email"));
534 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
535 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
537 g_free(display_name);
538 g_free(email);
540 /* company */
541 node = sipe_xml_child(card, "company");
542 if (node) {
543 char* company = sipe_xml_data(node);
544 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COMPANY, company);
545 g_free(company);
547 /* department */
548 node = sipe_xml_child(card, "department");
549 if (node) {
550 char* department = sipe_xml_data(node);
551 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DEPARTMENT, department);
552 g_free(department);
554 /* title */
555 node = sipe_xml_child(card, "title");
556 if (node) {
557 char* title = sipe_xml_data(node);
558 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_JOB_TITLE, title);
559 g_free(title);
561 /* office */
562 node = sipe_xml_child(card, "office");
563 if (node) {
564 char* office = sipe_xml_data(node);
565 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_OFFICE, office);
566 g_free(office);
568 /* site (url) */
569 node = sipe_xml_child(card, "url");
570 if (node) {
571 char* site = sipe_xml_data(node);
572 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_SITE, site);
573 g_free(site);
575 /* phone */
576 for (node = sipe_xml_child(card, "phone");
577 node;
578 node = sipe_xml_twin(node))
580 const char *phone_type = sipe_xml_attribute(node, "type");
581 char* phone = sipe_xml_data(sipe_xml_child(node, "uri"));
582 char* phone_display_string = sipe_xml_data(sipe_xml_child(node, "displayString"));
584 sipe_update_user_phone(sipe_private, uri, phone_type, phone, phone_display_string);
586 g_free(phone);
587 g_free(phone_display_string);
589 /* address */
590 for (node = sipe_xml_child(card, "address");
591 node;
592 node = sipe_xml_twin(node))
594 if (sipe_strequal(sipe_xml_attribute(node, "type"), "work")) {
595 char* street = sipe_xml_data(sipe_xml_child(node, "street"));
596 char* city = sipe_xml_data(sipe_xml_child(node, "city"));
597 char* state = sipe_xml_data(sipe_xml_child(node, "state"));
598 char* zipcode = sipe_xml_data(sipe_xml_child(node, "zipcode"));
599 char* country_code = sipe_xml_data(sipe_xml_child(node, "countryCode"));
601 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STREET, street);
602 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_CITY, city);
603 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STATE, state);
604 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_ZIPCODE, zipcode);
605 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COUNTRY, country_code);
607 g_free(street);
608 g_free(city);
609 g_free(state);
610 g_free(zipcode);
611 g_free(country_code);
613 break;
618 /* note */
619 else if (sipe_strequal(attrVar, "note"))
621 if (uri) {
622 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
624 if (!has_note_cleaned) {
625 has_note_cleaned = TRUE;
627 g_free(sbuddy->note);
628 sbuddy->note = NULL;
629 sbuddy->is_oof_note = FALSE;
630 sbuddy->note_since = publish_time;
632 do_update_status = TRUE;
634 if (sbuddy && (publish_time >= sbuddy->note_since)) {
635 /* clean up in case no 'note' element is supplied
636 * which indicate note removal in client
638 g_free(sbuddy->note);
639 sbuddy->note = NULL;
640 sbuddy->is_oof_note = FALSE;
641 sbuddy->note_since = publish_time;
643 xn_node = sipe_xml_child(xn_category, "note/body");
644 if (xn_node) {
645 char *tmp;
646 sbuddy->note = g_markup_escape_text((tmp = sipe_xml_data(xn_node)), -1);
647 g_free(tmp);
648 sbuddy->is_oof_note = sipe_strequal(sipe_xml_attribute(xn_node, "type"), "OOF");
649 sbuddy->note_since = publish_time;
651 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: uri(%s), note(%s)",
652 uri, sbuddy->note ? sbuddy->note : "");
654 /* to trigger UI refresh in case no status info is supplied in this update */
655 do_update_status = TRUE;
659 /* state */
660 else if(sipe_strequal(attrVar, "state"))
662 char *tmp;
663 int availability;
664 const sipe_xml *xn_availability;
665 const sipe_xml *xn_activity;
666 const sipe_xml *xn_meeting_subject;
667 const sipe_xml *xn_meeting_location;
668 struct sipe_buddy *sbuddy = uri ? g_hash_table_lookup(sipe_private->buddies, uri) : NULL;
670 xn_node = sipe_xml_child(xn_category, "state");
671 if (!xn_node) continue;
672 xn_availability = sipe_xml_child(xn_node, "availability");
673 if (!xn_availability) continue;
674 xn_activity = sipe_xml_child(xn_node, "activity");
675 xn_meeting_subject = sipe_xml_child(xn_node, "meetingSubject");
676 xn_meeting_location = sipe_xml_child(xn_node, "meetingLocation");
678 tmp = sipe_xml_data(xn_availability);
679 availability = atoi(tmp);
680 g_free(tmp);
682 /* activity, meeting_subject, meeting_location */
683 if (sbuddy) {
684 const gchar *tmp;
686 /* activity */
687 g_free(sbuddy->activity);
688 sbuddy->activity = NULL;
689 if (xn_activity) {
690 const char *token = sipe_xml_attribute(xn_activity, "token");
691 const sipe_xml *xn_custom = sipe_xml_child(xn_activity, "custom");
693 /* from token */
694 if (!is_empty(token)) {
695 sbuddy->activity = g_strdup(sipe_core_activity_description(sipe_status_token_to_activity(token)));
697 /* from custom element */
698 if (xn_custom) {
699 char *custom = sipe_xml_data(xn_custom);
701 if (!is_empty(custom)) {
702 g_free(sbuddy->activity);
703 sbuddy->activity = custom;
704 custom = NULL;
706 g_free(custom);
709 /* meeting_subject */
710 g_free(sbuddy->meeting_subject);
711 sbuddy->meeting_subject = NULL;
712 if (xn_meeting_subject) {
713 char *meeting_subject = sipe_xml_data(xn_meeting_subject);
715 if (!is_empty(meeting_subject)) {
716 sbuddy->meeting_subject = meeting_subject;
717 meeting_subject = NULL;
719 g_free(meeting_subject);
721 /* meeting_location */
722 g_free(sbuddy->meeting_location);
723 sbuddy->meeting_location = NULL;
724 if (xn_meeting_location) {
725 char *meeting_location = sipe_xml_data(xn_meeting_location);
727 if (!is_empty(meeting_location)) {
728 sbuddy->meeting_location = meeting_location;
729 meeting_location = NULL;
731 g_free(meeting_location);
734 status = sipe_ocs2007_status_from_legacy_availability(availability);
735 tmp = sipe_ocs2007_legacy_activity_description(availability);
736 if (sbuddy->activity && tmp) {
737 gchar *tmp2 = sbuddy->activity;
739 sbuddy->activity = g_strdup_printf("%s, %s", sbuddy->activity, tmp);
740 g_free(tmp2);
741 } else if (tmp) {
742 sbuddy->activity = g_strdup(tmp);
746 do_update_status = TRUE;
748 /* calendarData */
749 else if(sipe_strequal(attrVar, "calendarData"))
751 struct sipe_buddy *sbuddy = uri ? g_hash_table_lookup(sipe_private->buddies, uri) : NULL;
752 const sipe_xml *xn_free_busy = sipe_xml_child(xn_category, "calendarData/freeBusy");
753 const sipe_xml *xn_working_hours = sipe_xml_child(xn_category, "calendarData/WorkingHours");
755 if (sbuddy && xn_free_busy) {
756 if (!has_free_busy_cleaned) {
757 has_free_busy_cleaned = TRUE;
759 g_free(sbuddy->cal_start_time);
760 sbuddy->cal_start_time = NULL;
762 g_free(sbuddy->cal_free_busy_base64);
763 sbuddy->cal_free_busy_base64 = NULL;
765 g_free(sbuddy->cal_free_busy);
766 sbuddy->cal_free_busy = NULL;
768 sbuddy->cal_free_busy_published = publish_time;
771 if (publish_time >= sbuddy->cal_free_busy_published) {
772 g_free(sbuddy->cal_start_time);
773 sbuddy->cal_start_time = g_strdup(sipe_xml_attribute(xn_free_busy, "startTime"));
775 sbuddy->cal_granularity = sipe_strcase_equal(sipe_xml_attribute(xn_free_busy, "granularity"), "PT15M") ?
776 15 : 0;
778 g_free(sbuddy->cal_free_busy_base64);
779 sbuddy->cal_free_busy_base64 = sipe_xml_data(xn_free_busy);
781 g_free(sbuddy->cal_free_busy);
782 sbuddy->cal_free_busy = NULL;
784 sbuddy->cal_free_busy_published = publish_time;
786 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);
790 if (sbuddy && xn_working_hours) {
791 sipe_cal_parse_working_hours(xn_working_hours, sbuddy);
796 if (do_update_status) {
797 guint activity;
799 if (status) {
800 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: %s", status);
801 activity = sipe_status_token_to_activity(status);
802 } else {
803 /* no status category in this update,
804 using contact's current status */
805 activity = sipe_backend_buddy_get_status(SIPE_CORE_PUBLIC,
806 uri);
809 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri, activity);
812 sipe_xml_free(xn_categories);
815 static void sipe_buddy_status_from_activity(struct sipe_core_private *sipe_private,
816 const gchar *uri,
817 const gchar *activity,
818 gboolean is_online)
820 if (is_online) {
821 const gchar *status_id = NULL;
822 if (activity) {
823 if (sipe_strequal(activity,
824 sipe_status_activity_to_token(SIPE_ACTIVITY_BUSY))) {
825 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_BUSY);
826 } else if (sipe_strequal(activity,
827 sipe_status_activity_to_token(SIPE_ACTIVITY_AWAY))) {
828 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_AWAY);
832 if (!status_id) {
833 status_id = sipe_status_activity_to_token(SIPE_ACTIVITY_AVAILABLE);
836 SIPE_DEBUG_INFO("sipe_buddy_status_from_activity: status_id(%s)", status_id);
837 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
838 sipe_status_token_to_activity(status_id));
839 } else {
840 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
841 SIPE_ACTIVITY_OFFLINE);
845 static void process_incoming_notify_pidf(struct sipe_core_private *sipe_private,
846 const gchar *data,
847 unsigned len)
849 gchar *uri;
850 gchar *getbasic;
851 gchar *activity = NULL;
852 sipe_xml *pidf;
853 const sipe_xml *basicstatus = NULL, *tuple, *status;
854 gboolean isonline = FALSE;
855 const sipe_xml *display_name_node;
857 pidf = sipe_xml_parse(data, len);
858 if (!pidf) {
859 SIPE_DEBUG_INFO("process_incoming_notify_pidf: no parseable pidf:%s", data);
860 return;
863 if ((tuple = sipe_xml_child(pidf, "tuple")))
865 if ((status = sipe_xml_child(tuple, "status"))) {
866 basicstatus = sipe_xml_child(status, "basic");
870 if (!basicstatus) {
871 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic found");
872 sipe_xml_free(pidf);
873 return;
876 getbasic = sipe_xml_data(basicstatus);
877 if (!getbasic) {
878 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic data found");
879 sipe_xml_free(pidf);
880 return;
883 SIPE_DEBUG_INFO("process_incoming_notify_pidf: basic-status(%s)", getbasic);
884 if (strstr(getbasic, "open")) {
885 isonline = TRUE;
887 g_free(getbasic);
889 uri = sip_uri(sipe_xml_attribute(pidf, "entity")); /* with 'sip:' prefix */ /* AOL comes without the prefix */
891 display_name_node = sipe_xml_child(pidf, "display-name");
892 if (display_name_node) {
893 char * display_name = sipe_xml_data(display_name_node);
895 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
896 g_free(display_name);
899 if ((tuple = sipe_xml_child(pidf, "tuple"))) {
900 if ((status = sipe_xml_child(tuple, "status"))) {
901 if ((basicstatus = sipe_xml_child(status, "activities"))) {
902 if ((basicstatus = sipe_xml_child(basicstatus, "activity"))) {
903 activity = sipe_xml_data(basicstatus);
904 SIPE_DEBUG_INFO("process_incoming_notify_pidf: activity(%s)", activity);
910 sipe_buddy_status_from_activity(sipe_private,
911 uri,
912 activity,
913 isonline);
915 g_free(activity);
916 g_free(uri);
917 sipe_xml_free(pidf);
920 static void sipe_presence_mime_cb(gpointer user_data, /* sipe_core_private */
921 const GSList *fields,
922 const gchar *body,
923 gsize length)
925 const gchar *type = sipe_utils_nameval_find(fields, "Content-Type");
927 if (strstr(type,"application/rlmi+xml")) {
928 process_incoming_notify_rlmi_resub(user_data, body, length);
929 } else if (strstr(type, "text/xml+msrtc.pidf")) {
930 process_incoming_notify_msrtc(user_data, body, length);
931 } else {
932 process_incoming_notify_rlmi(user_data, body, length);
936 static void sipe_process_presence(struct sipe_core_private *sipe_private,
937 struct sipmsg *msg)
939 const char *ctype = sipmsg_find_header(msg, "Content-Type");
941 SIPE_DEBUG_INFO("sipe_process_presence: Content-Type: %s", ctype ? ctype : "");
943 if (ctype &&
944 (strstr(ctype, "application/rlmi+xml") ||
945 strstr(ctype, "application/msrtc-event-categories+xml")))
947 if (strstr(ctype, "multipart"))
949 sipe_mime_parts_foreach(ctype, msg->body, sipe_presence_mime_cb, sipe_private);
951 else if(strstr(ctype, "application/msrtc-event-categories+xml") )
953 process_incoming_notify_rlmi(sipe_private, msg->body, msg->bodylen);
955 else if(strstr(ctype, "application/rlmi+xml"))
957 process_incoming_notify_rlmi_resub(sipe_private, msg->body, msg->bodylen);
960 else if(ctype && strstr(ctype, "text/xml+msrtc.pidf"))
962 process_incoming_notify_msrtc(sipe_private, msg->body, msg->bodylen);
964 else
966 process_incoming_notify_pidf(sipe_private, msg->body, msg->bodylen);
971 * Fires on deregistration event initiated by server.
972 * [MS-SIPREGE] SIP extension.
974 * OCS2007 Example
976 * Content-Type: text/registration-event
977 * subscription-state: terminated;expires=0
978 * ms-diagnostics-public: 4141;reason="User disabled"
980 * deregistered;event=rejected
982 static void sipe_process_registration_notify(struct sipe_core_private *sipe_private,
983 struct sipmsg *msg)
985 const gchar *contenttype = sipmsg_find_header(msg, "Content-Type");
986 gchar *event = NULL;
987 gchar *reason = NULL;
988 gchar *warning;
990 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: deregistration received.");
992 if (!g_ascii_strncasecmp(contenttype, "text/registration-event", 23)) {
993 event = sipmsg_find_part_of_header(msg->body, "event=", NULL, NULL);
994 //@TODO have proper parameter extraction _by_name_ func, case insesitive.
995 event = event ? event : sipmsg_find_part_of_header(msg->body, "event=", ";", NULL);
996 } else {
997 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: unknown content type, exiting.");
998 return;
1001 reason = sipmsg_get_ms_diagnostics_reason(msg);
1002 reason = reason ? reason : sipmsg_get_ms_diagnostics_public_reason(msg);
1003 if (!reason) { // for LCS2005
1004 if (event && sipe_strcase_equal(event, "unregistered")) {
1005 //reason = g_strdup(_("User logged out")); // [MS-OCER]
1006 reason = g_strdup(_("you are already signed in at another location"));
1007 } else if (event && sipe_strcase_equal(event, "rejected")) {
1008 reason = g_strdup(_("user disabled")); // [MS-OCER]
1009 } else if (event && sipe_strcase_equal(event, "deactivated")) {
1010 reason = g_strdup(_("user moved")); // [MS-OCER]
1013 g_free(event);
1014 warning = g_strdup_printf(_("You have been rejected by the server: %s"), reason ? reason : _("no reason given"));
1015 g_free(reason);
1017 sipe_backend_connection_error(SIPE_CORE_PUBLIC,
1018 SIPE_CONNECTION_ERROR_INVALID_USERNAME,
1019 warning);
1020 g_free(warning);
1025 * Removes entries from local buddy list
1026 * that does not correspond ones in the roaming contact list.
1028 static void sipe_cleanup_local_blist(struct sipe_core_private *sipe_private)
1030 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
1031 NULL, NULL);
1032 GSList *entry = buddies;
1033 struct sipe_buddy *buddy;
1034 sipe_backend_buddy b;
1035 gchar *bname;
1036 gchar *gname;
1038 SIPE_DEBUG_INFO("sipe_cleanup_local_blist: overall %d backend buddies (including clones)", g_slist_length(buddies));
1039 SIPE_DEBUG_INFO("sipe_cleanup_local_blist: %d sipe buddies (unique)", g_hash_table_size(sipe_private->buddies));
1040 while (entry) {
1041 b = entry->data;
1042 gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC, b);
1043 bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC, b);
1044 buddy = g_hash_table_lookup(sipe_private->buddies, bname);
1045 if(buddy) {
1046 gboolean in_sipe_groups = FALSE;
1047 GSList *entry2 = buddy->groups;
1048 while (entry2) {
1049 struct sipe_group *group = entry2->data;
1050 if (sipe_strequal(group->name, gname)) {
1051 in_sipe_groups = TRUE;
1052 break;
1054 entry2 = entry2->next;
1056 if(!in_sipe_groups) {
1057 SIPE_DEBUG_INFO("*** REMOVING %s from blist group: %s as not having this group in roaming list", bname, gname);
1058 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, b);
1060 } else {
1061 SIPE_DEBUG_INFO("*** REMOVING %s from blist group: %s as this buddy not in roaming list", bname, gname);
1062 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, b);
1064 g_free(bname);
1065 g_free(gname);
1066 entry = entry->next;
1068 g_slist_free(buddies);
1072 * A callback for g_hash_table_foreach
1074 static void sipe_buddy_subscribe_cb(char *buddy_name,
1075 SIPE_UNUSED_PARAMETER struct sipe_buddy *buddy,
1076 struct sipe_core_private *sipe_private)
1078 gchar *action_name = sipe_utils_presence_key(buddy_name);
1079 /* g_hash_table_size() can never return 0, otherwise this function wouldn't be called :-) */
1080 guint time_range = (g_hash_table_size(sipe_private->buddies) * 1000) / 25; /* time interval for 25 requests per sec. In msec. */
1081 guint timeout = ((guint) rand()) / (RAND_MAX / time_range) + 1; /* random period within the range but never 0! */
1083 sipe_schedule_mseconds(sipe_private,
1084 action_name,
1085 g_strdup(buddy_name),
1086 timeout,
1087 sipe_subscribe_presence_single,
1088 g_free);
1089 g_free(action_name);
1092 static gboolean sipe_process_roaming_contacts(struct sipe_core_private *sipe_private,
1093 struct sipmsg *msg)
1095 int len = msg->bodylen;
1097 const gchar *tmp = sipmsg_find_header(msg, "Event");
1098 const sipe_xml *item;
1099 sipe_xml *isc;
1100 guint delta;
1101 const sipe_xml *group_node;
1102 if (!g_str_has_prefix(tmp, "vnd-microsoft-roaming-contacts")) {
1103 return FALSE;
1106 /* Convert the contact from XML to backend Buddies */
1107 isc = sipe_xml_parse(msg->body, len);
1108 if (!isc) {
1109 return FALSE;
1112 /* [MS-SIP]: deltaNum MUST be non-zero */
1113 delta = sipe_xml_int_attribute(isc, "deltaNum", 0);
1114 if (delta) {
1115 sipe_private->deltanum_contacts = delta;
1118 if (sipe_strequal(sipe_xml_name(isc), "contactList")) {
1120 /* Parse groups */
1121 for (group_node = sipe_xml_child(isc, "group"); group_node; group_node = sipe_xml_twin(group_node)) {
1122 struct sipe_group * group = g_new0(struct sipe_group, 1);
1123 const char *name = sipe_xml_attribute(group_node, "name");
1125 if (g_str_has_prefix(name, "~")) {
1126 name = _("Other Contacts");
1128 group->name = g_strdup(name);
1129 group->id = (int)g_ascii_strtod(sipe_xml_attribute(group_node, "id"), NULL);
1131 sipe_group_add(sipe_private, group);
1134 // Make sure we have at least one group
1135 if (g_slist_length(sipe_private->groups) == 0) {
1136 sipe_group_create(sipe_private, _("Other Contacts"), NULL);
1139 /* Parse contacts */
1140 for (item = sipe_xml_child(isc, "contact"); item; item = sipe_xml_twin(item)) {
1141 const gchar *uri = sipe_xml_attribute(item, "uri");
1142 const gchar *name = sipe_xml_attribute(item, "name");
1143 gchar *buddy_name;
1144 struct sipe_buddy *buddy = NULL;
1145 gchar *tmp;
1146 gchar **item_groups;
1147 int i = 0;
1149 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
1150 tmp = sip_uri_from_name(uri);
1151 buddy_name = g_ascii_strdown(tmp, -1);
1152 g_free(tmp);
1154 /* assign to group Other Contacts if nothing else received */
1155 tmp = g_strdup(sipe_xml_attribute(item, "groups"));
1156 if(is_empty(tmp)) {
1157 struct sipe_group *group = sipe_group_find_by_name(sipe_private, _("Other Contacts"));
1158 g_free(tmp);
1159 tmp = group ? g_strdup_printf("%d", group->id) : g_strdup("1");
1161 item_groups = g_strsplit(tmp, " ", 0);
1162 g_free(tmp);
1164 while (item_groups[i]) {
1165 struct sipe_group *group = sipe_group_find_by_id(sipe_private, g_ascii_strtod(item_groups[i], NULL));
1167 // If couldn't find the right group for this contact, just put them in the first group we have
1168 if (group == NULL && g_slist_length(sipe_private->groups) > 0) {
1169 group = sipe_private->groups->data;
1172 if (group != NULL) {
1173 gchar *b_alias;
1174 sipe_backend_buddy b = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, buddy_name, group->name);
1175 if (!b){
1176 b = sipe_backend_buddy_add(SIPE_CORE_PUBLIC, buddy_name, uri, group->name);
1177 SIPE_DEBUG_INFO("Created new buddy %s with alias %s", buddy_name, uri);
1180 b_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, b);
1181 if (sipe_strcase_equal(uri, b_alias)) {
1182 if (name != NULL && strlen(name) != 0) {
1183 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, b, name);
1185 SIPE_DEBUG_INFO("Replaced buddy %s alias with %s", buddy_name, name);
1188 g_free(b_alias);
1190 if (!buddy) {
1191 buddy = sipe_buddy_add(sipe_private, buddy_name);
1194 buddy->groups = slist_insert_unique_sorted(buddy->groups, group, (GCompareFunc)sipe_group_compare);
1196 SIPE_DEBUG_INFO("Added buddy %s to group %s", buddy->name, group->name);
1197 } else {
1198 SIPE_DEBUG_INFO("No group found for contact %s! Unable to add to buddy list",
1199 name);
1202 i++;
1203 } // while, contact groups
1204 g_strfreev(item_groups);
1205 g_free(buddy_name);
1207 } // for, contacts
1209 sipe_cleanup_local_blist(sipe_private);
1211 /* Add self-contact if not there yet. 2005 systems. */
1212 /* This will resemble subscription to roaming_self in 2007 systems */
1213 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1214 gchar *self_uri = sip_uri_self(sipe_private);
1215 sipe_buddy_add(sipe_private, self_uri);
1216 g_free(self_uri);
1219 sipe_xml_free(isc);
1221 /* subscribe to buddies */
1222 if (!SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
1223 /* do it once, then count Expire field to schedule resubscribe */
1224 if (SIPE_CORE_PRIVATE_FLAG_IS(BATCHED_SUPPORT)) {
1225 sipe_subscribe_presence_batched(sipe_private);
1226 } else {
1227 g_hash_table_foreach(sipe_private->buddies,
1228 (GHFunc)sipe_buddy_subscribe_cb,
1229 sipe_private);
1231 SIPE_CORE_PRIVATE_FLAG_SET(SUBSCRIBED_BUDDIES);
1233 /* for 2005 systems schedule contacts' status update
1234 * based on their calendar information
1236 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1237 sipe_ocs2005_schedule_status_update(sipe_private, time(NULL));
1240 return 0;
1243 static void sipe_process_roaming_acl(struct sipe_core_private *sipe_private,
1244 struct sipmsg *msg)
1246 guint delta;
1247 sipe_xml *xml;
1249 xml = sipe_xml_parse(msg->body, msg->bodylen);
1250 if (!xml)
1251 return;
1253 /* [MS-SIP]: deltaNum MUST be non-zero */
1254 delta = sipe_xml_int_attribute(xml, "deltaNum", 0);
1255 if (delta) {
1256 sipe_private->deltanum_acl = delta;
1259 sipe_xml_free(xml);
1262 struct sipe_auth_job {
1263 gchar *who;
1264 struct sipe_core_private *sipe_private;
1267 void sipe_core_contact_allow_deny(struct sipe_core_public *sipe_public,
1268 const gchar* who,
1269 gboolean allow)
1271 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1273 if (allow) {
1274 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: authorizing contact %s", who);
1275 } else {
1276 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: blocking contact %s", who);
1279 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1280 sipe_ocs2007_change_access_level(sipe_private,
1281 (allow ? -1 : 32000),
1282 "user",
1283 sipe_get_no_sip_uri(who));
1284 } else {
1285 sip_soap_ocs2005_setacl(sipe_private, who, allow);
1290 static void sipe_auth_user_cb(gpointer data)
1292 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1293 if (!job) return;
1295 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1296 job->who,
1297 TRUE);
1298 g_free(job);
1301 static void sipe_deny_user_cb(gpointer data)
1303 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1304 if (!job) return;
1306 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1307 job->who,
1308 FALSE);
1309 g_free(job);
1312 /* OCS2005- */
1313 static void sipe_process_presence_wpending (struct sipe_core_private *sipe_private,
1314 struct sipmsg * msg)
1316 sipe_xml *watchers;
1317 const sipe_xml *watcher;
1318 // Ensure it's either not a response (eg it's a BENOTIFY) or that it's a 200 OK response
1319 if (msg->response != 0 && msg->response != 200) return;
1321 if (msg->bodylen == 0 || msg->body == NULL || sipe_strequal(sipmsg_find_header(msg, "Event"), "msrtc.wpending")) return;
1323 watchers = sipe_xml_parse(msg->body, msg->bodylen);
1324 if (!watchers) return;
1326 for (watcher = sipe_xml_child(watchers, "watcher"); watcher; watcher = sipe_xml_twin(watcher)) {
1327 gchar * remote_user = g_strdup(sipe_xml_attribute(watcher, "uri"));
1328 gchar * alias = g_strdup(sipe_xml_attribute(watcher, "displayName"));
1329 gboolean on_list = g_hash_table_lookup(sipe_private->buddies, remote_user) != NULL;
1331 // TODO pull out optional displayName to pass as alias
1332 if (remote_user) {
1333 struct sipe_auth_job * job = g_new0(struct sipe_auth_job, 1);
1334 job->who = remote_user;
1335 job->sipe_private = sipe_private;
1336 sipe_backend_buddy_request_authorization(SIPE_CORE_PUBLIC,
1337 remote_user,
1338 alias,
1339 on_list,
1340 sipe_auth_user_cb,
1341 sipe_deny_user_cb,
1342 (gpointer)job);
1347 sipe_xml_free(watchers);
1348 return;
1351 static void sipe_presence_timeout_mime_cb(gpointer user_data,
1352 SIPE_UNUSED_PARAMETER const GSList *fields,
1353 const gchar *body,
1354 gsize length)
1356 GSList **buddies = user_data;
1357 sipe_xml *xml = sipe_xml_parse(body, length);
1359 if (xml && !sipe_strequal(sipe_xml_name(xml), "list")) {
1360 const gchar *uri = sipe_xml_attribute(xml, "uri");
1361 const sipe_xml *xn_category;
1364 * automaton: presence is never expected to change
1366 * see: http://msdn.microsoft.com/en-us/library/ee354295(office.13).aspx
1368 for (xn_category = sipe_xml_child(xml, "category");
1369 xn_category;
1370 xn_category = sipe_xml_twin(xn_category)) {
1371 if (sipe_strequal(sipe_xml_attribute(xn_category, "name"),
1372 "contactCard")) {
1373 const sipe_xml *node = sipe_xml_child(xn_category, "contactCard/automaton");
1374 if (node) {
1375 char *boolean = sipe_xml_data(node);
1376 if (sipe_strequal(boolean, "true")) {
1377 SIPE_DEBUG_INFO("sipe_process_presence_timeout: %s is an automaton: - not subscribing to presence updates",
1378 uri);
1379 uri = NULL;
1381 g_free(boolean);
1383 break;
1387 if (uri) {
1388 *buddies = g_slist_append(*buddies, sip_uri(uri));
1392 sipe_xml_free(xml);
1395 static void sipe_process_presence_timeout(struct sipe_core_private *sipe_private,
1396 struct sipmsg *msg,
1397 const gchar *who,
1398 int timeout)
1400 const char *ctype = sipmsg_find_header(msg, "Content-Type");
1401 gchar *action_name = sipe_utils_presence_key(who);
1403 SIPE_DEBUG_INFO("sipe_process_presence_timeout: Content-Type: %s", ctype ? ctype : "");
1405 if (ctype &&
1406 strstr(ctype, "multipart") &&
1407 (strstr(ctype, "application/rlmi+xml") ||
1408 strstr(ctype, "application/msrtc-event-categories+xml"))) {
1409 GSList *buddies = NULL;
1411 sipe_mime_parts_foreach(ctype, msg->body, sipe_presence_timeout_mime_cb, &buddies);
1413 if (buddies)
1414 sipe_subscribe_presence_batched_schedule(sipe_private,
1415 action_name,
1416 who,
1417 buddies,
1418 timeout);
1420 } else {
1421 sipe_schedule_seconds(sipe_private,
1422 action_name,
1423 g_strdup(who),
1424 timeout,
1425 sipe_subscribe_presence_single,
1426 g_free);
1427 SIPE_DEBUG_INFO("Resubscription single contact with batched support(%s) in %d", who, timeout);
1429 g_free(action_name);
1433 * Dispatcher for all incoming subscription information
1434 * whether it comes from NOTIFY, BENOTIFY requests or
1435 * piggy-backed to subscription's OK responce.
1437 * @param request whether initiated from BE/NOTIFY request or OK-response message.
1438 * @param benotify whether initiated from NOTIFY or BENOTIFY request.
1440 void process_incoming_notify(struct sipe_core_private *sipe_private,
1441 struct sipmsg *msg,
1442 gboolean request, gboolean benotify)
1444 const gchar *content_type = sipmsg_find_header(msg, "Content-Type");
1445 const gchar *event = sipmsg_find_header(msg, "Event");
1446 const gchar *subscription_state = sipmsg_find_header(msg, "subscription-state");
1448 SIPE_DEBUG_INFO("process_incoming_notify: subscription_state: %s", subscription_state ? subscription_state : "");
1450 /* implicit subscriptions */
1451 if (content_type && g_str_has_prefix(content_type, "application/ms-imdn+xml")) {
1452 sipe_process_imdn(sipe_private, msg);
1455 if (event) {
1456 /* for one off subscriptions (send with Expire: 0) */
1457 if (sipe_strcase_equal(event, "vnd-microsoft-provisioning-v2"))
1459 sipe_process_provisioning_v2(sipe_private, msg);
1461 else if (sipe_strcase_equal(event, "vnd-microsoft-provisioning"))
1463 sipe_process_provisioning(sipe_private, msg);
1465 else if (sipe_strcase_equal(event, "presence"))
1467 sipe_process_presence(sipe_private, msg);
1469 else if (sipe_strcase_equal(event, "registration-notify"))
1471 sipe_process_registration_notify(sipe_private, msg);
1474 if (!subscription_state || strstr(subscription_state, "active"))
1476 if (sipe_strcase_equal(event, "vnd-microsoft-roaming-contacts"))
1478 sipe_process_roaming_contacts(sipe_private, msg);
1480 else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-self"))
1482 sipe_ocs2007_process_roaming_self(sipe_private, msg);
1484 else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-ACL"))
1486 sipe_process_roaming_acl(sipe_private, msg);
1488 else if (sipe_strcase_equal(event, "presence.wpending"))
1490 sipe_process_presence_wpending(sipe_private, msg);
1492 else if (sipe_strcase_equal(event, "conference"))
1494 sipe_process_conference(sipe_private, msg);
1499 /* The server sends status 'terminated' */
1500 if (subscription_state && strstr(subscription_state, "terminated") ) {
1501 gchar *who = parse_from(sipmsg_find_header(msg, request ? "From" : "To"));
1502 gchar *key = sipe_utils_subscription_key(event, who);
1504 SIPE_DEBUG_INFO("process_incoming_notify: server says that subscription to %s was terminated.", who);
1505 g_free(who);
1507 sipe_subscriptions_remove(sipe_private, key);
1508 g_free(key);
1511 if (!request && event) {
1512 const gchar *expires_header = sipmsg_find_header(msg, "Expires");
1513 int timeout = expires_header ? strtol(expires_header, NULL, 10) : 0;
1514 SIPE_DEBUG_INFO("process_incoming_notify: subscription expires:%d", timeout);
1516 if (timeout) {
1517 /* 2 min ahead of expiration */
1518 timeout = (timeout - 120) > 120 ? (timeout - 120) : timeout;
1520 if (sipe_strcase_equal(event, "presence.wpending") &&
1521 g_slist_find_custom(sipe_private->allowed_events, "presence.wpending", (GCompareFunc)g_ascii_strcasecmp))
1523 gchar *action_name = g_strdup_printf("<%s>", "presence.wpending");
1524 sipe_schedule_seconds(sipe_private,
1525 action_name,
1526 NULL,
1527 timeout,
1528 sipe_subscribe_presence_wpending,
1529 NULL);
1530 g_free(action_name);
1532 else if (sipe_strcase_equal(event, "presence") &&
1533 g_slist_find_custom(sipe_private->allowed_events, "presence", (GCompareFunc)g_ascii_strcasecmp))
1535 gchar *who = parse_from(sipmsg_find_header(msg, "To"));
1536 gchar *action_name = sipe_utils_presence_key(who);
1538 if (SIPE_CORE_PRIVATE_FLAG_IS(BATCHED_SUPPORT)) {
1539 sipe_process_presence_timeout(sipe_private, msg, who, timeout);
1541 else {
1542 sipe_schedule_seconds(sipe_private,
1543 action_name,
1544 g_strdup(who),
1545 timeout,
1546 sipe_subscribe_presence_single,
1547 g_free);
1548 SIPE_DEBUG_INFO("Resubscription single contact (%s) in %d", who, timeout);
1550 g_free(action_name);
1551 g_free(who);
1556 /* The client responses on received a NOTIFY message */
1557 if (request && !benotify)
1559 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1564 Local Variables:
1565 mode: c
1566 c-file-style: "bsd"
1567 indent-tabs-mode: t
1568 tab-width: 8
1569 End: