core cleanup: move out allow_events field
[siplcs.git] / src / core / sipe-notify.c
blobcd0215b2124d9000e48c8f95dc66ad539da11f45
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 "http-conn.h" /* sipe-cal.h requires this */
39 #include "sipmsg.h"
40 #include "sip-csta.h"
41 #include "sip-soap.h"
42 #include "sip-transport.h"
43 #include "sipe-backend.h"
44 #include "sipe-buddy.h"
45 #include "sipe-cal.h"
46 #include "sipe-conf.h"
47 #include "sipe-core.h"
48 #include "sipe-core-private.h"
49 #include "sipe-group.h"
50 #include "sipe-media.h"
51 #include "sipe-mime.h"
52 #include "sipe-nls.h"
53 #include "sipe-notify.h"
54 #include "sipe-ocs2005.h"
55 #include "sipe-ocs2007.h"
56 #include "sipe-schedule.h"
57 #include "sipe-status.h"
58 #include "sipe-subscriptions.h"
59 #include "sipe-utils.h"
60 #include "sipe-xml.h"
62 /* OCS2005 */
63 static void sipe_process_provisioning(struct sipe_core_private *sipe_private,
64 struct sipmsg *msg)
66 sipe_xml *xn_provision;
67 const sipe_xml *node;
69 xn_provision = sipe_xml_parse(msg->body, msg->bodylen);
70 if ((node = sipe_xml_child(xn_provision, "user"))) {
71 SIPE_DEBUG_INFO("sipe_process_provisioning: uri=%s", sipe_xml_attribute(node, "uri"));
72 if ((node = sipe_xml_child(node, "line"))) {
73 const gchar *line_uri = sipe_xml_attribute(node, "uri");
74 const gchar *server = sipe_xml_attribute(node, "server");
75 SIPE_DEBUG_INFO("sipe_process_provisioning: line_uri=%s server=%s", line_uri, server);
76 sip_csta_open(sipe_private, line_uri, server);
79 sipe_xml_free(xn_provision);
82 /* OCS2007+ */
83 static void sipe_process_provisioning_v2(struct sipe_core_private *sipe_private,
84 struct sipmsg *msg)
86 sipe_xml *xn_provision_group_list;
87 const sipe_xml *node;
89 xn_provision_group_list = sipe_xml_parse(msg->body, msg->bodylen);
91 /* provisionGroup */
92 for (node = sipe_xml_child(xn_provision_group_list, "provisionGroup"); node; node = sipe_xml_twin(node)) {
93 if (sipe_strequal("ServerConfiguration", sipe_xml_attribute(node, "name"))) {
94 g_free(sipe_private->focus_factory_uri);
95 sipe_private->focus_factory_uri = sipe_xml_data(sipe_xml_child(node, "focusFactoryUri"));
96 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->focus_factory_uri=%s",
97 sipe_private->focus_factory_uri ? sipe_private->focus_factory_uri : "");
99 #ifdef HAVE_VV
100 g_free(sipe_private->mras_uri);
101 sipe_private->mras_uri = g_strstrip(sipe_xml_data(sipe_xml_child(node, "mrasUri")));
102 SIPE_DEBUG_INFO("sipe_process_provisioning_v2: sipe_private->mras_uri=%s",
103 sipe_private->mras_uri ? sipe_private->mras_uri : "");
105 if (sipe_private->mras_uri)
106 sipe_media_get_av_edge_credentials(sipe_private);
107 #endif
108 break;
111 sipe_xml_free(xn_provision_group_list);
114 static void process_incoming_notify_rlmi_resub(struct sipe_core_private *sipe_private,
115 const gchar *data, unsigned len)
117 sipe_xml *xn_list;
118 const sipe_xml *xn_resource;
119 GHashTable *servers = g_hash_table_new_full(g_str_hash, g_str_equal,
120 g_free, NULL);
121 GSList *server;
122 gchar *host;
124 xn_list = sipe_xml_parse(data, len);
126 for (xn_resource = sipe_xml_child(xn_list, "resource");
127 xn_resource;
128 xn_resource = sipe_xml_twin(xn_resource) )
130 const char *uri, *state;
131 const sipe_xml *xn_instance;
133 xn_instance = sipe_xml_child(xn_resource, "instance");
134 if (!xn_instance) continue;
136 uri = sipe_xml_attribute(xn_resource, "uri");
137 state = sipe_xml_attribute(xn_instance, "state");
138 SIPE_DEBUG_INFO("process_incoming_notify_rlmi_resub: uri(%s),state(%s)", uri, state);
140 if (strstr(state, "resubscribe")) {
141 const char *poolFqdn = sipe_xml_attribute(xn_instance, "poolFqdn");
143 if (poolFqdn) { //[MS-PRES] Section 3.4.5.1.3 Processing Details
144 gchar *user = g_strdup(uri);
145 host = g_strdup(poolFqdn);
146 server = g_hash_table_lookup(servers, host);
147 server = g_slist_append(server, user);
148 g_hash_table_insert(servers, host, server);
149 } else {
150 sipe_subscribe_presence_single(sipe_private,
151 (void *) uri);
156 /* Send out any deferred poolFqdn subscriptions */
157 g_hash_table_foreach(servers, (GHFunc) sipe_subscribe_poolfqdn_resource_uri, sipe_private);
158 g_hash_table_destroy(servers);
160 sipe_xml_free(xn_list);
164 * Update user phone
165 * Suitable for both 2005 and 2007 systems.
167 * @param uri buddy SIP URI with 'sip:' prefix whose info we want to change.
168 * @param phone_type
169 * @param phone may be modified to strip white space
170 * @param phone_display_string may be modified to strip white space
172 static void
173 sipe_update_user_phone(struct sipe_core_private *sipe_private,
174 const gchar *uri,
175 const gchar *phone_type,
176 gchar *phone,
177 gchar *phone_display_string)
179 sipe_buddy_info_fields phone_node = SIPE_BUDDY_INFO_WORK_PHONE; /* work phone by default */
180 sipe_buddy_info_fields phone_display_node = SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY; /* work phone by default */
182 if(!phone || strlen(phone) == 0) return;
184 if ((sipe_strequal(phone_type, "mobile") || sipe_strequal(phone_type, "cell"))) {
185 phone_node = SIPE_BUDDY_INFO_MOBILE_PHONE;
186 phone_display_node = SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY;
187 } else if (sipe_strequal(phone_type, "home")) {
188 phone_node = SIPE_BUDDY_INFO_HOME_PHONE;
189 phone_display_node = SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY;
190 } else if (sipe_strequal(phone_type, "other")) {
191 phone_node = SIPE_BUDDY_INFO_OTHER_PHONE;
192 phone_display_node = SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY;
193 } else if (sipe_strequal(phone_type, "custom1")) {
194 phone_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE;
195 phone_display_node = SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY;
198 sipe_buddy_update_property(sipe_private, uri, phone_node, phone);
199 if (phone_display_string) {
200 sipe_buddy_update_property(sipe_private, uri, phone_display_node, phone_display_string);
204 static void process_incoming_notify_msrtc(struct sipe_core_private *sipe_private,
205 const gchar *data,
206 unsigned len)
208 char *activity = NULL;
209 const char *epid;
210 const char *status_id = NULL;
211 const char *name;
212 char *uri;
213 char *self_uri = sip_uri_self(sipe_private);
214 int avl;
215 int act;
216 const char *device_name = NULL;
217 const char *cal_start_time = NULL;
218 const char *cal_granularity = NULL;
219 char *cal_free_busy_base64 = NULL;
220 struct sipe_buddy *sbuddy;
221 const sipe_xml *node;
222 sipe_xml *xn_presentity;
223 const sipe_xml *xn_availability;
224 const sipe_xml *xn_activity;
225 const sipe_xml *xn_display_name;
226 const sipe_xml *xn_email;
227 const sipe_xml *xn_phone_number;
228 const sipe_xml *xn_userinfo;
229 const sipe_xml *xn_note;
230 const sipe_xml *xn_oof;
231 const sipe_xml *xn_state;
232 const sipe_xml *xn_contact;
233 char *note;
234 int user_avail;
235 const char *user_avail_nil;
236 int res_avail;
237 time_t user_avail_since = 0;
238 time_t activity_since = 0;
240 /* fix for Reuters environment on Linux */
241 if (data && strstr(data, "encoding=\"utf-16\"")) {
242 char *tmp_data;
243 tmp_data = replace(data, "encoding=\"utf-16\"", "encoding=\"utf-8\"");
244 xn_presentity = sipe_xml_parse(tmp_data, strlen(tmp_data));
245 g_free(tmp_data);
246 } else {
247 xn_presentity = sipe_xml_parse(data, len);
250 xn_availability = sipe_xml_child(xn_presentity, "availability");
251 xn_activity = sipe_xml_child(xn_presentity, "activity");
252 xn_display_name = sipe_xml_child(xn_presentity, "displayName");
253 xn_email = sipe_xml_child(xn_presentity, "email");
254 xn_phone_number = sipe_xml_child(xn_presentity, "phoneNumber");
255 xn_userinfo = sipe_xml_child(xn_presentity, "userInfo");
256 xn_oof = xn_userinfo ? sipe_xml_child(xn_userinfo, "oof") : NULL;
257 xn_state = xn_userinfo ? sipe_xml_child(xn_userinfo, "states/state"): NULL;
258 user_avail = xn_state ? sipe_xml_int_attribute(xn_state, "avail", 0) : 0;
259 user_avail_since = xn_state ? sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since")) : 0;
260 user_avail_nil = xn_state ? sipe_xml_attribute(xn_state, "nil") : NULL;
261 xn_contact = xn_userinfo ? sipe_xml_child(xn_userinfo, "contact") : NULL;
262 xn_note = xn_userinfo ? sipe_xml_child(xn_userinfo, "note") : NULL;
263 note = xn_note ? sipe_xml_data(xn_note) : NULL;
265 if (sipe_strequal(user_avail_nil, "true")) { /* null-ed */
266 user_avail = 0;
267 user_avail_since = 0;
270 name = sipe_xml_attribute(xn_presentity, "uri"); /* without 'sip:' prefix */
271 uri = sip_uri_from_name(name);
272 avl = sipe_xml_int_attribute(xn_availability, "aggregate", 0);
273 epid = sipe_xml_attribute(xn_availability, "epid");
274 act = sipe_xml_int_attribute(xn_activity, "aggregate", 0);
276 status_id = sipe_ocs2005_status_from_activity_availability(act, avl);
277 activity = g_strdup(sipe_ocs2005_activity_description(act));
278 res_avail = sipe_ocs2007_availability_from_status(status_id, NULL);
279 if (user_avail > res_avail) {
280 res_avail = user_avail;
281 status_id = sipe_ocs2007_status_from_legacy_availability(user_avail);
284 if (xn_display_name) {
285 char *display_name = g_strdup(sipe_xml_attribute(xn_display_name, "displayName"));
286 char *email = xn_email ? g_strdup(sipe_xml_attribute(xn_email, "email")) : NULL;
287 char *phone_label = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "label")) : NULL;
288 char *phone_number = xn_phone_number ? g_strdup(sipe_xml_attribute(xn_phone_number, "number")) : NULL;
289 char *tel_uri = sip_to_tel_uri(phone_number);
291 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
292 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
293 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
294 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, !is_empty(phone_label) ? phone_label : phone_number);
296 g_free(tel_uri);
297 g_free(phone_label);
298 g_free(phone_number);
299 g_free(email);
300 g_free(display_name);
303 if (xn_contact) {
304 /* tel */
305 for (node = sipe_xml_child(xn_contact, "tel"); node; node = sipe_xml_twin(node))
307 /* Ex.: <tel type="work">tel:+3222220000</tel> */
308 const char *phone_type = sipe_xml_attribute(node, "type");
309 char* phone = sipe_xml_data(node);
311 sipe_update_user_phone(sipe_private, uri, phone_type, phone, NULL);
313 g_free(phone);
317 /* devicePresence */
318 for (node = sipe_xml_child(xn_presentity, "devices/devicePresence"); node; node = sipe_xml_twin(node)) {
319 const sipe_xml *xn_device_name;
320 const sipe_xml *xn_calendar_info;
321 const sipe_xml *xn_state;
322 char *state;
324 /* deviceName */
325 if (sipe_strequal(sipe_xml_attribute(node, "epid"), epid)) {
326 xn_device_name = sipe_xml_child(node, "deviceName");
327 device_name = xn_device_name ? sipe_xml_attribute(xn_device_name, "name") : NULL;
330 /* calendarInfo */
331 xn_calendar_info = sipe_xml_child(node, "calendarInfo");
332 if (xn_calendar_info) {
333 const char *cal_start_time_tmp = sipe_xml_attribute(xn_calendar_info, "startTime");
335 if (cal_start_time) {
336 time_t cal_start_time_t = sipe_utils_str_to_time(cal_start_time);
337 time_t cal_start_time_t_tmp = sipe_utils_str_to_time(cal_start_time_tmp);
339 if (cal_start_time_t_tmp > cal_start_time_t) {
340 cal_start_time = cal_start_time_tmp;
341 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
342 g_free(cal_free_busy_base64);
343 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
345 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);
347 } else {
348 cal_start_time = cal_start_time_tmp;
349 cal_granularity = sipe_xml_attribute(xn_calendar_info, "granularity");
350 g_free(cal_free_busy_base64);
351 cal_free_busy_base64 = sipe_xml_data(xn_calendar_info);
353 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);
357 /* state */
358 xn_state = sipe_xml_child(node, "states/state");
359 if (xn_state) {
360 int dev_avail = sipe_xml_int_attribute(xn_state, "avail", 0);
361 time_t dev_avail_since = sipe_utils_str_to_time(sipe_xml_attribute(xn_state, "since"));
363 state = sipe_xml_data(xn_state);
364 if (dev_avail_since > user_avail_since &&
365 dev_avail >= res_avail)
367 const gchar *new_desc;
368 res_avail = dev_avail;
369 if (!is_empty(state)) {
370 if (sipe_strequal(state, sipe_backend_activity_to_token(SIPE_ACTIVITY_ON_PHONE))) {
371 g_free(activity);
372 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_ON_PHONE));
373 } else if (sipe_strequal(state, "presenting")) {
374 g_free(activity);
375 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_IN_CONF));
376 } else {
377 activity = state;
378 state = NULL;
380 activity_since = dev_avail_since;
382 status_id = sipe_ocs2007_status_from_legacy_availability(res_avail);
383 new_desc = sipe_ocs2007_legacy_activity_description(res_avail);
384 if (new_desc) {
385 g_free(activity);
386 activity = g_strdup(new_desc);
389 g_free(state);
393 /* oof */
394 if (xn_oof && res_avail >= 15000) { /* 12000 in 2007 */
395 g_free(activity);
396 activity = g_strdup(sipe_core_activity_description(SIPE_ACTIVITY_OOF));
397 activity_since = 0;
400 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
401 if (sbuddy)
403 g_free(sbuddy->activity);
404 sbuddy->activity = activity;
405 activity = NULL;
407 sbuddy->activity_since = activity_since;
409 sbuddy->user_avail = user_avail;
410 sbuddy->user_avail_since = user_avail_since;
412 g_free(sbuddy->note);
413 sbuddy->note = NULL;
414 if (!is_empty(note)) { sbuddy->note = g_markup_escape_text(note, -1); }
416 sbuddy->is_oof_note = (xn_oof != NULL);
418 g_free(sbuddy->device_name);
419 sbuddy->device_name = NULL;
420 if (!is_empty(device_name)) { sbuddy->device_name = g_strdup(device_name); }
422 if (!is_empty(cal_free_busy_base64)) {
423 g_free(sbuddy->cal_start_time);
424 sbuddy->cal_start_time = g_strdup(cal_start_time);
426 sbuddy->cal_granularity = sipe_strcase_equal(cal_granularity, "PT15M") ? 15 : 0;
428 g_free(sbuddy->cal_free_busy_base64);
429 sbuddy->cal_free_busy_base64 = cal_free_busy_base64;
430 cal_free_busy_base64 = NULL;
432 g_free(sbuddy->cal_free_busy);
433 sbuddy->cal_free_busy = NULL;
436 sbuddy->last_non_cal_status_id = status_id;
437 g_free(sbuddy->last_non_cal_activity);
438 sbuddy->last_non_cal_activity = g_strdup(sbuddy->activity);
440 if (sipe_strcase_equal(sbuddy->name, self_uri)) {
441 if (!sipe_strequal(sbuddy->note, sipe_private->note)) /* not same */
443 if (sbuddy->is_oof_note)
444 SIPE_CORE_PRIVATE_FLAG_SET(OOF_NOTE);
445 else
446 SIPE_CORE_PRIVATE_FLAG_UNSET(OOF_NOTE);
448 g_free(sipe_private->note);
449 sipe_private->note = g_strdup(sbuddy->note);
451 sipe_private->note_since = time(NULL);
454 sipe_status_set_token(sipe_private,
455 sbuddy->last_non_cal_status_id);
458 g_free(cal_free_busy_base64);
459 g_free(activity);
461 SIPE_DEBUG_INFO("process_incoming_notify_msrtc: status(%s)", status_id);
462 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri, status_id);
464 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007) && sipe_strcase_equal(self_uri, uri)) {
465 sipe_ocs2005_user_info_has_updated(sipe_private, xn_userinfo);
468 g_free(note);
469 sipe_xml_free(xn_presentity);
470 g_free(uri);
471 g_free(self_uri);
474 static void process_incoming_notify_rlmi(struct sipe_core_private *sipe_private,
475 const gchar *data,
476 unsigned len)
478 const char *uri;
479 sipe_xml *xn_categories;
480 const sipe_xml *xn_category;
481 const char *status = NULL;
482 gboolean do_update_status = FALSE;
483 gboolean has_note_cleaned = FALSE;
484 gboolean has_free_busy_cleaned = FALSE;
486 xn_categories = sipe_xml_parse(data, len);
487 uri = sipe_xml_attribute(xn_categories, "uri"); /* with 'sip:' prefix */
489 for (xn_category = sipe_xml_child(xn_categories, "category");
490 xn_category ;
491 xn_category = sipe_xml_twin(xn_category) )
493 const sipe_xml *xn_node;
494 const char *tmp;
495 const char *attrVar = sipe_xml_attribute(xn_category, "name");
496 time_t publish_time = (tmp = sipe_xml_attribute(xn_category, "publishTime")) ?
497 sipe_utils_str_to_time(tmp) : 0;
499 /* contactCard */
500 if (sipe_strequal(attrVar, "contactCard"))
502 const sipe_xml *card = sipe_xml_child(xn_category, "contactCard");
504 if (card) {
505 const sipe_xml *node;
506 /* identity - Display Name and email */
507 node = sipe_xml_child(card, "identity");
508 if (node) {
509 char* display_name = sipe_xml_data(
510 sipe_xml_child(node, "name/displayName"));
511 char* email = sipe_xml_data(
512 sipe_xml_child(node, "email"));
514 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
515 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
517 g_free(display_name);
518 g_free(email);
520 /* company */
521 node = sipe_xml_child(card, "company");
522 if (node) {
523 char* company = sipe_xml_data(node);
524 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COMPANY, company);
525 g_free(company);
527 /* department */
528 node = sipe_xml_child(card, "department");
529 if (node) {
530 char* department = sipe_xml_data(node);
531 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DEPARTMENT, department);
532 g_free(department);
534 /* title */
535 node = sipe_xml_child(card, "title");
536 if (node) {
537 char* title = sipe_xml_data(node);
538 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_JOB_TITLE, title);
539 g_free(title);
541 /* office */
542 node = sipe_xml_child(card, "office");
543 if (node) {
544 char* office = sipe_xml_data(node);
545 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_OFFICE, office);
546 g_free(office);
548 /* site (url) */
549 node = sipe_xml_child(card, "url");
550 if (node) {
551 char* site = sipe_xml_data(node);
552 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_SITE, site);
553 g_free(site);
555 /* phone */
556 for (node = sipe_xml_child(card, "phone");
557 node;
558 node = sipe_xml_twin(node))
560 const char *phone_type = sipe_xml_attribute(node, "type");
561 char* phone = sipe_xml_data(sipe_xml_child(node, "uri"));
562 char* phone_display_string = sipe_xml_data(sipe_xml_child(node, "displayString"));
564 sipe_update_user_phone(sipe_private, uri, phone_type, phone, phone_display_string);
566 g_free(phone);
567 g_free(phone_display_string);
569 /* address */
570 for (node = sipe_xml_child(card, "address");
571 node;
572 node = sipe_xml_twin(node))
574 if (sipe_strequal(sipe_xml_attribute(node, "type"), "work")) {
575 char* street = sipe_xml_data(sipe_xml_child(node, "street"));
576 char* city = sipe_xml_data(sipe_xml_child(node, "city"));
577 char* state = sipe_xml_data(sipe_xml_child(node, "state"));
578 char* zipcode = sipe_xml_data(sipe_xml_child(node, "zipcode"));
579 char* country_code = sipe_xml_data(sipe_xml_child(node, "countryCode"));
581 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STREET, street);
582 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_CITY, city);
583 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_STATE, state);
584 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_ZIPCODE, zipcode);
585 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_COUNTRY, country_code);
587 g_free(street);
588 g_free(city);
589 g_free(state);
590 g_free(zipcode);
591 g_free(country_code);
593 break;
598 /* note */
599 else if (sipe_strequal(attrVar, "note"))
601 if (uri) {
602 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
604 if (!has_note_cleaned) {
605 has_note_cleaned = TRUE;
607 g_free(sbuddy->note);
608 sbuddy->note = NULL;
609 sbuddy->is_oof_note = FALSE;
610 sbuddy->note_since = publish_time;
612 do_update_status = TRUE;
614 if (sbuddy && (publish_time >= sbuddy->note_since)) {
615 /* clean up in case no 'note' element is supplied
616 * which indicate note removal in client
618 g_free(sbuddy->note);
619 sbuddy->note = NULL;
620 sbuddy->is_oof_note = FALSE;
621 sbuddy->note_since = publish_time;
623 xn_node = sipe_xml_child(xn_category, "note/body");
624 if (xn_node) {
625 char *tmp;
626 sbuddy->note = g_markup_escape_text((tmp = sipe_xml_data(xn_node)), -1);
627 g_free(tmp);
628 sbuddy->is_oof_note = sipe_strequal(sipe_xml_attribute(xn_node, "type"), "OOF");
629 sbuddy->note_since = publish_time;
631 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: uri(%s), note(%s)",
632 uri, sbuddy->note ? sbuddy->note : "");
634 /* to trigger UI refresh in case no status info is supplied in this update */
635 do_update_status = TRUE;
639 /* state */
640 else if(sipe_strequal(attrVar, "state"))
642 char *tmp;
643 int availability;
644 const sipe_xml *xn_availability;
645 const sipe_xml *xn_activity;
646 const sipe_xml *xn_meeting_subject;
647 const sipe_xml *xn_meeting_location;
648 struct sipe_buddy *sbuddy = uri ? g_hash_table_lookup(sipe_private->buddies, uri) : NULL;
650 xn_node = sipe_xml_child(xn_category, "state");
651 if (!xn_node) continue;
652 xn_availability = sipe_xml_child(xn_node, "availability");
653 if (!xn_availability) continue;
654 xn_activity = sipe_xml_child(xn_node, "activity");
655 xn_meeting_subject = sipe_xml_child(xn_node, "meetingSubject");
656 xn_meeting_location = sipe_xml_child(xn_node, "meetingLocation");
658 tmp = sipe_xml_data(xn_availability);
659 availability = atoi(tmp);
660 g_free(tmp);
662 /* activity, meeting_subject, meeting_location */
663 if (sbuddy) {
664 const gchar *tmp;
666 /* activity */
667 g_free(sbuddy->activity);
668 sbuddy->activity = NULL;
669 if (xn_activity) {
670 const char *token = sipe_xml_attribute(xn_activity, "token");
671 const sipe_xml *xn_custom = sipe_xml_child(xn_activity, "custom");
673 /* from token */
674 if (!is_empty(token)) {
675 sbuddy->activity = g_strdup(sipe_core_activity_description(sipe_backend_token_to_activity(token)));
677 /* from custom element */
678 if (xn_custom) {
679 char *custom = sipe_xml_data(xn_custom);
681 if (!is_empty(custom)) {
682 g_free(sbuddy->activity);
683 sbuddy->activity = custom;
684 custom = NULL;
686 g_free(custom);
689 /* meeting_subject */
690 g_free(sbuddy->meeting_subject);
691 sbuddy->meeting_subject = NULL;
692 if (xn_meeting_subject) {
693 char *meeting_subject = sipe_xml_data(xn_meeting_subject);
695 if (!is_empty(meeting_subject)) {
696 sbuddy->meeting_subject = meeting_subject;
697 meeting_subject = NULL;
699 g_free(meeting_subject);
701 /* meeting_location */
702 g_free(sbuddy->meeting_location);
703 sbuddy->meeting_location = NULL;
704 if (xn_meeting_location) {
705 char *meeting_location = sipe_xml_data(xn_meeting_location);
707 if (!is_empty(meeting_location)) {
708 sbuddy->meeting_location = meeting_location;
709 meeting_location = NULL;
711 g_free(meeting_location);
714 status = sipe_ocs2007_status_from_legacy_availability(availability);
715 tmp = sipe_ocs2007_legacy_activity_description(availability);
716 if (sbuddy->activity && tmp) {
717 gchar *tmp2 = sbuddy->activity;
719 sbuddy->activity = g_strdup_printf("%s, %s", sbuddy->activity, tmp);
720 g_free(tmp2);
721 } else if (tmp) {
722 sbuddy->activity = g_strdup(tmp);
726 do_update_status = TRUE;
728 /* calendarData */
729 else if(sipe_strequal(attrVar, "calendarData"))
731 struct sipe_buddy *sbuddy = uri ? g_hash_table_lookup(sipe_private->buddies, uri) : NULL;
732 const sipe_xml *xn_free_busy = sipe_xml_child(xn_category, "calendarData/freeBusy");
733 const sipe_xml *xn_working_hours = sipe_xml_child(xn_category, "calendarData/WorkingHours");
735 if (sbuddy && xn_free_busy) {
736 if (!has_free_busy_cleaned) {
737 has_free_busy_cleaned = TRUE;
739 g_free(sbuddy->cal_start_time);
740 sbuddy->cal_start_time = NULL;
742 g_free(sbuddy->cal_free_busy_base64);
743 sbuddy->cal_free_busy_base64 = NULL;
745 g_free(sbuddy->cal_free_busy);
746 sbuddy->cal_free_busy = NULL;
748 sbuddy->cal_free_busy_published = publish_time;
751 if (publish_time >= sbuddy->cal_free_busy_published) {
752 g_free(sbuddy->cal_start_time);
753 sbuddy->cal_start_time = g_strdup(sipe_xml_attribute(xn_free_busy, "startTime"));
755 sbuddy->cal_granularity = sipe_strcase_equal(sipe_xml_attribute(xn_free_busy, "granularity"), "PT15M") ?
756 15 : 0;
758 g_free(sbuddy->cal_free_busy_base64);
759 sbuddy->cal_free_busy_base64 = sipe_xml_data(xn_free_busy);
761 g_free(sbuddy->cal_free_busy);
762 sbuddy->cal_free_busy = NULL;
764 sbuddy->cal_free_busy_published = publish_time;
766 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);
770 if (sbuddy && xn_working_hours) {
771 sipe_cal_parse_working_hours(xn_working_hours, sbuddy);
776 if (do_update_status) {
777 if (!status) {
778 /* no status category in this update,
779 using contact's current status */
780 status = sipe_backend_buddy_get_status(SIPE_CORE_PUBLIC,
781 uri);
784 SIPE_DEBUG_INFO("process_incoming_notify_rlmi: %s", status);
785 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri, status);
788 sipe_xml_free(xn_categories);
791 static void sipe_buddy_status_from_activity(struct sipe_core_private *sipe_private,
792 const gchar *uri,
793 const gchar *activity,
794 gboolean is_online)
796 if (is_online) {
797 const gchar *status_id = NULL;
798 if (activity) {
799 if (sipe_strequal(activity,
800 sipe_backend_activity_to_token(SIPE_ACTIVITY_BUSY))) {
801 status_id = sipe_backend_activity_to_token(SIPE_ACTIVITY_BUSY);
802 } else if (sipe_strequal(activity,
803 sipe_backend_activity_to_token(SIPE_ACTIVITY_AWAY))) {
804 status_id = sipe_backend_activity_to_token(SIPE_ACTIVITY_AWAY);
808 if (!status_id) {
809 status_id = sipe_backend_activity_to_token(SIPE_ACTIVITY_AVAILABLE);
812 SIPE_DEBUG_INFO("sipe_buddy_status_from_activity: status_id(%s)", status_id);
813 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri, status_id);
814 } else {
815 sipe_core_buddy_got_status(SIPE_CORE_PUBLIC, uri,
816 sipe_backend_activity_to_token(SIPE_ACTIVITY_OFFLINE));
820 static void process_incoming_notify_pidf(struct sipe_core_private *sipe_private,
821 const gchar *data,
822 unsigned len)
824 gchar *uri;
825 gchar *getbasic;
826 gchar *activity = NULL;
827 sipe_xml *pidf;
828 const sipe_xml *basicstatus = NULL, *tuple, *status;
829 gboolean isonline = FALSE;
830 const sipe_xml *display_name_node;
832 pidf = sipe_xml_parse(data, len);
833 if (!pidf) {
834 SIPE_DEBUG_INFO("process_incoming_notify_pidf: no parseable pidf:%s", data);
835 return;
838 if ((tuple = sipe_xml_child(pidf, "tuple")))
840 if ((status = sipe_xml_child(tuple, "status"))) {
841 basicstatus = sipe_xml_child(status, "basic");
845 if (!basicstatus) {
846 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic found");
847 sipe_xml_free(pidf);
848 return;
851 getbasic = sipe_xml_data(basicstatus);
852 if (!getbasic) {
853 SIPE_DEBUG_INFO_NOFORMAT("process_incoming_notify_pidf: no basic data found");
854 sipe_xml_free(pidf);
855 return;
858 SIPE_DEBUG_INFO("process_incoming_notify_pidf: basic-status(%s)", getbasic);
859 if (strstr(getbasic, "open")) {
860 isonline = TRUE;
862 g_free(getbasic);
864 uri = sip_uri(sipe_xml_attribute(pidf, "entity")); /* with 'sip:' prefix */ /* AOL comes without the prefix */
866 display_name_node = sipe_xml_child(pidf, "display-name");
867 if (display_name_node) {
868 char * display_name = sipe_xml_data(display_name_node);
870 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, display_name);
871 g_free(display_name);
874 if ((tuple = sipe_xml_child(pidf, "tuple"))) {
875 if ((status = sipe_xml_child(tuple, "status"))) {
876 if ((basicstatus = sipe_xml_child(status, "activities"))) {
877 if ((basicstatus = sipe_xml_child(basicstatus, "activity"))) {
878 activity = sipe_xml_data(basicstatus);
879 SIPE_DEBUG_INFO("process_incoming_notify_pidf: activity(%s)", activity);
885 sipe_buddy_status_from_activity(sipe_private,
886 uri,
887 activity,
888 isonline);
890 g_free(activity);
891 g_free(uri);
892 sipe_xml_free(pidf);
895 static void sipe_presence_mime_cb(gpointer user_data, /* sipe_core_private */
896 const GSList *fields,
897 const gchar *body,
898 gsize length)
900 const gchar *type = sipe_utils_nameval_find(fields, "Content-Type");
902 if (strstr(type,"application/rlmi+xml")) {
903 process_incoming_notify_rlmi_resub(user_data, body, length);
904 } else if (strstr(type, "text/xml+msrtc.pidf")) {
905 process_incoming_notify_msrtc(user_data, body, length);
906 } else {
907 process_incoming_notify_rlmi(user_data, body, length);
911 static void sipe_process_presence(struct sipe_core_private *sipe_private,
912 struct sipmsg *msg)
914 const char *ctype = sipmsg_find_header(msg, "Content-Type");
916 SIPE_DEBUG_INFO("sipe_process_presence: Content-Type: %s", ctype ? ctype : "");
918 if (ctype &&
919 (strstr(ctype, "application/rlmi+xml") ||
920 strstr(ctype, "application/msrtc-event-categories+xml")))
922 if (strstr(ctype, "multipart"))
924 sipe_mime_parts_foreach(ctype, msg->body, sipe_presence_mime_cb, sipe_private);
926 else if(strstr(ctype, "application/msrtc-event-categories+xml") )
928 process_incoming_notify_rlmi(sipe_private, msg->body, msg->bodylen);
930 else if(strstr(ctype, "application/rlmi+xml"))
932 process_incoming_notify_rlmi_resub(sipe_private, msg->body, msg->bodylen);
935 else if(ctype && strstr(ctype, "text/xml+msrtc.pidf"))
937 process_incoming_notify_msrtc(sipe_private, msg->body, msg->bodylen);
939 else
941 process_incoming_notify_pidf(sipe_private, msg->body, msg->bodylen);
946 * Fires on deregistration event initiated by server.
947 * [MS-SIPREGE] SIP extension.
949 * OCS2007 Example
951 * Content-Type: text/registration-event
952 * subscription-state: terminated;expires=0
953 * ms-diagnostics-public: 4141;reason="User disabled"
955 * deregistered;event=rejected
957 static void sipe_process_registration_notify(struct sipe_core_private *sipe_private,
958 struct sipmsg *msg)
960 const gchar *contenttype = sipmsg_find_header(msg, "Content-Type");
961 gchar *event = NULL;
962 gchar *reason = NULL;
963 gchar *warning;
965 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: deregistration received.");
967 if (!g_ascii_strncasecmp(contenttype, "text/registration-event", 23)) {
968 event = sipmsg_find_part_of_header(msg->body, "event=", NULL, NULL);
969 //@TODO have proper parameter extraction _by_name_ func, case insesitive.
970 event = event ? event : sipmsg_find_part_of_header(msg->body, "event=", ";", NULL);
971 } else {
972 SIPE_DEBUG_INFO_NOFORMAT("sipe_process_registration_notify: unknown content type, exiting.");
973 return;
976 reason = sipmsg_get_ms_diagnostics_reason(msg);
977 reason = reason ? reason : sipmsg_get_ms_diagnostics_public_reason(msg);
978 if (!reason) { // for LCS2005
979 if (event && sipe_strcase_equal(event, "unregistered")) {
980 //reason = g_strdup(_("User logged out")); // [MS-OCER]
981 reason = g_strdup(_("you are already signed in at another location"));
982 } else if (event && sipe_strcase_equal(event, "rejected")) {
983 reason = g_strdup(_("user disabled")); // [MS-OCER]
984 } else if (event && sipe_strcase_equal(event, "deactivated")) {
985 reason = g_strdup(_("user moved")); // [MS-OCER]
988 g_free(event);
989 warning = g_strdup_printf(_("You have been rejected by the server: %s"), reason ? reason : _("no reason given"));
990 g_free(reason);
992 sipe_backend_connection_error(SIPE_CORE_PUBLIC,
993 SIPE_CONNECTION_ERROR_INVALID_USERNAME,
994 warning);
995 g_free(warning);
1000 * Removes entries from local buddy list
1001 * that does not correspond ones in the roaming contact list.
1003 static void sipe_cleanup_local_blist(struct sipe_core_private *sipe_private)
1005 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
1006 NULL, NULL);
1007 GSList *entry = buddies;
1008 struct sipe_buddy *buddy;
1009 sipe_backend_buddy b;
1010 gchar *bname;
1011 gchar *gname;
1013 SIPE_DEBUG_INFO("sipe_cleanup_local_blist: overall %d backend buddies (including clones)", g_slist_length(buddies));
1014 SIPE_DEBUG_INFO("sipe_cleanup_local_blist: %d sipe buddies (unique)", g_hash_table_size(sipe_private->buddies));
1015 while (entry) {
1016 b = entry->data;
1017 gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC, b);
1018 bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC, b);
1019 buddy = g_hash_table_lookup(sipe_private->buddies, bname);
1020 if(buddy) {
1021 gboolean in_sipe_groups = FALSE;
1022 GSList *entry2 = buddy->groups;
1023 while (entry2) {
1024 struct sipe_group *group = entry2->data;
1025 if (sipe_strequal(group->name, gname)) {
1026 in_sipe_groups = TRUE;
1027 break;
1029 entry2 = entry2->next;
1031 if(!in_sipe_groups) {
1032 SIPE_DEBUG_INFO("*** REMOVING %s from blist group: %s as not having this group in roaming list", bname, gname);
1033 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, b);
1035 } else {
1036 SIPE_DEBUG_INFO("*** REMOVING %s from blist group: %s as this buddy not in roaming list", bname, gname);
1037 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, b);
1039 g_free(bname);
1040 g_free(gname);
1041 entry = entry->next;
1043 g_slist_free(buddies);
1047 * A callback for g_hash_table_foreach
1049 static void sipe_buddy_subscribe_cb(char *buddy_name,
1050 SIPE_UNUSED_PARAMETER struct sipe_buddy *buddy,
1051 struct sipe_core_private *sipe_private)
1053 gchar *action_name = sipe_utils_presence_key(buddy_name);
1054 /* g_hash_table_size() can never return 0, otherwise this function wouldn't be called :-) */
1055 guint time_range = (g_hash_table_size(sipe_private->buddies) * 1000) / 25; /* time interval for 25 requests per sec. In msec. */
1056 guint timeout = ((guint) rand()) / (RAND_MAX / time_range) + 1; /* random period within the range but never 0! */
1058 sipe_schedule_mseconds(sipe_private,
1059 action_name,
1060 g_strdup(buddy_name),
1061 timeout,
1062 sipe_subscribe_presence_single,
1063 g_free);
1064 g_free(action_name);
1067 static gboolean sipe_process_roaming_contacts(struct sipe_core_private *sipe_private,
1068 struct sipmsg *msg)
1070 int len = msg->bodylen;
1072 const gchar *tmp = sipmsg_find_header(msg, "Event");
1073 const sipe_xml *item;
1074 sipe_xml *isc;
1075 guint delta;
1076 const sipe_xml *group_node;
1077 if (!g_str_has_prefix(tmp, "vnd-microsoft-roaming-contacts")) {
1078 return FALSE;
1081 /* Convert the contact from XML to backend Buddies */
1082 isc = sipe_xml_parse(msg->body, len);
1083 if (!isc) {
1084 return FALSE;
1087 /* [MS-SIP]: deltaNum MUST be non-zero */
1088 delta = sipe_xml_int_attribute(isc, "deltaNum", 0);
1089 if (delta) {
1090 sipe_private->deltanum_contacts = delta;
1093 if (sipe_strequal(sipe_xml_name(isc), "contactList")) {
1095 /* Parse groups */
1096 for (group_node = sipe_xml_child(isc, "group"); group_node; group_node = sipe_xml_twin(group_node)) {
1097 struct sipe_group * group = g_new0(struct sipe_group, 1);
1098 const char *name = sipe_xml_attribute(group_node, "name");
1100 if (g_str_has_prefix(name, "~")) {
1101 name = _("Other Contacts");
1103 group->name = g_strdup(name);
1104 group->id = (int)g_ascii_strtod(sipe_xml_attribute(group_node, "id"), NULL);
1106 sipe_group_add(sipe_private, group);
1109 // Make sure we have at least one group
1110 if (g_slist_length(sipe_private->groups) == 0) {
1111 sipe_group_create(sipe_private, _("Other Contacts"), NULL);
1114 /* Parse contacts */
1115 for (item = sipe_xml_child(isc, "contact"); item; item = sipe_xml_twin(item)) {
1116 const gchar *uri = sipe_xml_attribute(item, "uri");
1117 const gchar *name = sipe_xml_attribute(item, "name");
1118 gchar *buddy_name;
1119 struct sipe_buddy *buddy = NULL;
1120 gchar *tmp;
1121 gchar **item_groups;
1122 int i = 0;
1124 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
1125 tmp = sip_uri_from_name(uri);
1126 buddy_name = g_ascii_strdown(tmp, -1);
1127 g_free(tmp);
1129 /* assign to group Other Contacts if nothing else received */
1130 tmp = g_strdup(sipe_xml_attribute(item, "groups"));
1131 if(is_empty(tmp)) {
1132 struct sipe_group *group = sipe_group_find_by_name(sipe_private, _("Other Contacts"));
1133 g_free(tmp);
1134 tmp = group ? g_strdup_printf("%d", group->id) : g_strdup("1");
1136 item_groups = g_strsplit(tmp, " ", 0);
1137 g_free(tmp);
1139 while (item_groups[i]) {
1140 struct sipe_group *group = sipe_group_find_by_id(sipe_private, g_ascii_strtod(item_groups[i], NULL));
1142 // If couldn't find the right group for this contact, just put them in the first group we have
1143 if (group == NULL && g_slist_length(sipe_private->groups) > 0) {
1144 group = sipe_private->groups->data;
1147 if (group != NULL) {
1148 gchar *b_alias;
1149 sipe_backend_buddy b = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, buddy_name, group->name);
1150 if (!b){
1151 b = sipe_backend_buddy_add(SIPE_CORE_PUBLIC, buddy_name, uri, group->name);
1152 SIPE_DEBUG_INFO("Created new buddy %s with alias %s", buddy_name, uri);
1155 b_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, b);
1156 if (sipe_strcase_equal(uri, b_alias)) {
1157 if (name != NULL && strlen(name) != 0) {
1158 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, b, name);
1160 SIPE_DEBUG_INFO("Replaced buddy %s alias with %s", buddy_name, name);
1163 g_free(b_alias);
1165 if (!buddy) {
1166 buddy = g_new0(struct sipe_buddy, 1);
1167 buddy->name = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC, b);
1168 g_hash_table_insert(sipe_private->buddies, buddy->name, buddy);
1170 SIPE_DEBUG_INFO("Added SIPE buddy %s", buddy->name);
1173 buddy->groups = slist_insert_unique_sorted(buddy->groups, group, (GCompareFunc)sipe_group_compare);
1175 SIPE_DEBUG_INFO("Added buddy %s to group %s", buddy->name, group->name);
1176 } else {
1177 SIPE_DEBUG_INFO("No group found for contact %s! Unable to add to buddy list",
1178 name);
1181 i++;
1182 } // while, contact groups
1183 g_strfreev(item_groups);
1184 g_free(buddy_name);
1186 } // for, contacts
1188 sipe_cleanup_local_blist(sipe_private);
1190 /* Add self-contact if not there yet. 2005 systems. */
1191 /* This will resemble subscription to roaming_self in 2007 systems */
1192 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1193 gchar *self_uri = sip_uri_self(sipe_private);
1194 struct sipe_buddy *buddy = g_hash_table_lookup(sipe_private->buddies, self_uri);
1196 if (!buddy) {
1197 buddy = g_new0(struct sipe_buddy, 1);
1198 buddy->name = g_strdup(self_uri);
1199 g_hash_table_insert(sipe_private->buddies, buddy->name, buddy);
1201 g_free(self_uri);
1204 sipe_xml_free(isc);
1206 /* subscribe to buddies */
1207 if (!SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
1208 /* do it once, then count Expire field to schedule resubscribe */
1209 if (SIPE_CORE_PRIVATE_FLAG_IS(BATCHED_SUPPORT)) {
1210 sipe_subscribe_presence_batched(sipe_private);
1211 } else {
1212 g_hash_table_foreach(sipe_private->buddies,
1213 (GHFunc)sipe_buddy_subscribe_cb,
1214 sipe_private);
1216 SIPE_CORE_PRIVATE_FLAG_SET(SUBSCRIBED_BUDDIES);
1218 /* for 2005 systems schedule contacts' status update
1219 * based on their calendar information
1221 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1222 sipe_ocs2005_schedule_status_update(sipe_private, time(NULL));
1225 return 0;
1228 static void sipe_process_roaming_acl(struct sipe_core_private *sipe_private,
1229 struct sipmsg *msg)
1231 guint delta;
1232 sipe_xml *xml;
1234 xml = sipe_xml_parse(msg->body, msg->bodylen);
1235 if (!xml)
1236 return;
1238 /* [MS-SIP]: deltaNum MUST be non-zero */
1239 delta = sipe_xml_int_attribute(xml, "deltaNum", 0);
1240 if (delta) {
1241 sipe_private->deltanum_acl = delta;
1244 sipe_xml_free(xml);
1247 struct sipe_auth_job {
1248 gchar *who;
1249 struct sipe_core_private *sipe_private;
1252 void sipe_core_contact_allow_deny(struct sipe_core_public *sipe_public,
1253 const gchar* who,
1254 gboolean allow)
1256 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1258 if (allow) {
1259 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: authorizing contact %s", who);
1260 } else {
1261 SIPE_DEBUG_INFO("sipe_core_contact_allow_deny: blocking contact %s", who);
1264 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1265 sipe_ocs2007_change_access_level(sipe_private,
1266 (allow ? -1 : 32000),
1267 "user",
1268 sipe_get_no_sip_uri(who));
1269 } else {
1270 sip_soap_ocs2005_setacl(sipe_private, who, allow);
1275 static void sipe_auth_user_cb(gpointer data)
1277 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1278 if (!job) return;
1280 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1281 job->who,
1282 TRUE);
1283 g_free(job);
1286 static void sipe_deny_user_cb(gpointer data)
1288 struct sipe_auth_job *job = (struct sipe_auth_job *) data;
1289 if (!job) return;
1291 sipe_core_contact_allow_deny((struct sipe_core_public *)job->sipe_private,
1292 job->who,
1293 FALSE);
1294 g_free(job);
1297 /* OCS2005- */
1298 static void sipe_process_presence_wpending (struct sipe_core_private *sipe_private,
1299 struct sipmsg * msg)
1301 sipe_xml *watchers;
1302 const sipe_xml *watcher;
1303 // Ensure it's either not a response (eg it's a BENOTIFY) or that it's a 200 OK response
1304 if (msg->response != 0 && msg->response != 200) return;
1306 if (msg->bodylen == 0 || msg->body == NULL || sipe_strequal(sipmsg_find_header(msg, "Event"), "msrtc.wpending")) return;
1308 watchers = sipe_xml_parse(msg->body, msg->bodylen);
1309 if (!watchers) return;
1311 for (watcher = sipe_xml_child(watchers, "watcher"); watcher; watcher = sipe_xml_twin(watcher)) {
1312 gchar * remote_user = g_strdup(sipe_xml_attribute(watcher, "uri"));
1313 gchar * alias = g_strdup(sipe_xml_attribute(watcher, "displayName"));
1314 gboolean on_list = g_hash_table_lookup(sipe_private->buddies, remote_user) != NULL;
1316 // TODO pull out optional displayName to pass as alias
1317 if (remote_user) {
1318 struct sipe_auth_job * job = g_new0(struct sipe_auth_job, 1);
1319 job->who = remote_user;
1320 job->sipe_private = sipe_private;
1321 sipe_backend_buddy_request_authorization(SIPE_CORE_PUBLIC,
1322 remote_user,
1323 alias,
1324 on_list,
1325 sipe_auth_user_cb,
1326 sipe_deny_user_cb,
1327 (gpointer)job);
1332 sipe_xml_free(watchers);
1333 return;
1336 static void sipe_presence_timeout_mime_cb(gpointer user_data,
1337 SIPE_UNUSED_PARAMETER const GSList *fields,
1338 const gchar *body,
1339 gsize length)
1341 GSList **buddies = user_data;
1342 sipe_xml *xml = sipe_xml_parse(body, length);
1344 if (xml && !sipe_strequal(sipe_xml_name(xml), "list")) {
1345 const gchar *uri = sipe_xml_attribute(xml, "uri");
1346 const sipe_xml *xn_category;
1349 * automaton: presence is never expected to change
1351 * see: http://msdn.microsoft.com/en-us/library/ee354295(office.13).aspx
1353 for (xn_category = sipe_xml_child(xml, "category");
1354 xn_category;
1355 xn_category = sipe_xml_twin(xn_category)) {
1356 if (sipe_strequal(sipe_xml_attribute(xn_category, "name"),
1357 "contactCard")) {
1358 const sipe_xml *node = sipe_xml_child(xn_category, "contactCard/automaton");
1359 if (node) {
1360 char *boolean = sipe_xml_data(node);
1361 if (sipe_strequal(boolean, "true")) {
1362 SIPE_DEBUG_INFO("sipe_process_presence_timeout: %s is an automaton: - not subscribing to presence updates",
1363 uri);
1364 uri = NULL;
1366 g_free(boolean);
1368 break;
1372 if (uri) {
1373 *buddies = g_slist_append(*buddies, sip_uri(uri));
1377 sipe_xml_free(xml);
1380 static void sipe_process_presence_timeout(struct sipe_core_private *sipe_private,
1381 struct sipmsg *msg,
1382 const gchar *who,
1383 int timeout)
1385 const char *ctype = sipmsg_find_header(msg, "Content-Type");
1386 gchar *action_name = sipe_utils_presence_key(who);
1388 SIPE_DEBUG_INFO("sipe_process_presence_timeout: Content-Type: %s", ctype ? ctype : "");
1390 if (ctype &&
1391 strstr(ctype, "multipart") &&
1392 (strstr(ctype, "application/rlmi+xml") ||
1393 strstr(ctype, "application/msrtc-event-categories+xml"))) {
1394 GSList *buddies = NULL;
1396 sipe_mime_parts_foreach(ctype, msg->body, sipe_presence_timeout_mime_cb, &buddies);
1398 if (buddies)
1399 sipe_subscribe_presence_batched_schedule(sipe_private,
1400 action_name,
1401 who,
1402 buddies,
1403 timeout);
1405 } else {
1406 sipe_schedule_seconds(sipe_private,
1407 action_name,
1408 g_strdup(who),
1409 timeout,
1410 sipe_subscribe_presence_single,
1411 g_free);
1412 SIPE_DEBUG_INFO("Resubscription single contact with batched support(%s) in %d", who, timeout);
1414 g_free(action_name);
1418 * Dispatcher for all incoming subscription information
1419 * whether it comes from NOTIFY, BENOTIFY requests or
1420 * piggy-backed to subscription's OK responce.
1422 * @param request whether initiated from BE/NOTIFY request or OK-response message.
1423 * @param benotify whether initiated from NOTIFY or BENOTIFY request.
1425 void process_incoming_notify(struct sipe_core_private *sipe_private,
1426 struct sipmsg *msg,
1427 gboolean request, gboolean benotify)
1429 const gchar *content_type = sipmsg_find_header(msg, "Content-Type");
1430 const gchar *event = sipmsg_find_header(msg, "Event");
1431 const gchar *subscription_state = sipmsg_find_header(msg, "subscription-state");
1433 SIPE_DEBUG_INFO("process_incoming_notify: subscription_state: %s", subscription_state ? subscription_state : "");
1435 /* implicit subscriptions */
1436 if (content_type && g_str_has_prefix(content_type, "application/ms-imdn+xml")) {
1437 sipe_process_imdn(sipe_private, msg);
1440 if (event) {
1441 /* for one off subscriptions (send with Expire: 0) */
1442 if (sipe_strcase_equal(event, "vnd-microsoft-provisioning-v2"))
1444 sipe_process_provisioning_v2(sipe_private, msg);
1446 else if (sipe_strcase_equal(event, "vnd-microsoft-provisioning"))
1448 sipe_process_provisioning(sipe_private, msg);
1450 else if (sipe_strcase_equal(event, "presence"))
1452 sipe_process_presence(sipe_private, msg);
1454 else if (sipe_strcase_equal(event, "registration-notify"))
1456 sipe_process_registration_notify(sipe_private, msg);
1459 if (!subscription_state || strstr(subscription_state, "active"))
1461 if (sipe_strcase_equal(event, "vnd-microsoft-roaming-contacts"))
1463 sipe_process_roaming_contacts(sipe_private, msg);
1465 else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-self"))
1467 sipe_ocs2007_process_roaming_self(sipe_private, msg);
1469 else if (sipe_strcase_equal(event, "vnd-microsoft-roaming-ACL"))
1471 sipe_process_roaming_acl(sipe_private, msg);
1473 else if (sipe_strcase_equal(event, "presence.wpending"))
1475 sipe_process_presence_wpending(sipe_private, msg);
1477 else if (sipe_strcase_equal(event, "conference"))
1479 sipe_process_conference(sipe_private, msg);
1484 /* The server sends status 'terminated' */
1485 if (subscription_state && strstr(subscription_state, "terminated") ) {
1486 gchar *who = parse_from(sipmsg_find_header(msg, request ? "From" : "To"));
1487 gchar *key = sipe_utils_subscription_key(event, who);
1489 SIPE_DEBUG_INFO("process_incoming_notify: server says that subscription to %s was terminated.", who);
1490 g_free(who);
1492 sipe_subscriptions_remove(sipe_private, key);
1493 g_free(key);
1496 if (!request && event) {
1497 const gchar *expires_header = sipmsg_find_header(msg, "Expires");
1498 int timeout = expires_header ? strtol(expires_header, NULL, 10) : 0;
1499 SIPE_DEBUG_INFO("process_incoming_notify: subscription expires:%d", timeout);
1501 if (timeout) {
1502 /* 2 min ahead of expiration */
1503 timeout = (timeout - 120) > 120 ? (timeout - 120) : timeout;
1505 if (sipe_strcase_equal(event, "presence.wpending") &&
1506 g_slist_find_custom(sipe_private->allowed_events, "presence.wpending", (GCompareFunc)g_ascii_strcasecmp))
1508 gchar *action_name = g_strdup_printf("<%s>", "presence.wpending");
1509 sipe_schedule_seconds(sipe_private,
1510 action_name,
1511 NULL,
1512 timeout,
1513 sipe_subscribe_presence_wpending,
1514 NULL);
1515 g_free(action_name);
1517 else if (sipe_strcase_equal(event, "presence") &&
1518 g_slist_find_custom(sipe_private->allowed_events, "presence", (GCompareFunc)g_ascii_strcasecmp))
1520 gchar *who = parse_from(sipmsg_find_header(msg, "To"));
1521 gchar *action_name = sipe_utils_presence_key(who);
1523 if (SIPE_CORE_PRIVATE_FLAG_IS(BATCHED_SUPPORT)) {
1524 sipe_process_presence_timeout(sipe_private, msg, who, timeout);
1526 else {
1527 sipe_schedule_seconds(sipe_private,
1528 action_name,
1529 g_strdup(who),
1530 timeout,
1531 sipe_subscribe_presence_single,
1532 g_free);
1533 SIPE_DEBUG_INFO("Resubscription single contact (%s) in %d", who, timeout);
1535 g_free(action_name);
1536 g_free(who);
1541 /* The client responses on received a NOTIFY message */
1542 if (request && !benotify)
1544 sip_transport_response(sipe_private, msg, 200, "OK", NULL);
1549 Local Variables:
1550 mode: c
1551 c-file-style: "bsd"
1552 indent-tabs-mode: t
1553 tab-width: 8
1554 End: