buddy: refactor URI normalization code
[siplcs.git] / src / core / sipe-buddy.c
blobddd912c1edb7e88997778d6bcc48787400e5990d
1 /**
2 * @file sipe-buddy.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
31 #include <glib.h>
33 #include "sipe-common.h"
34 #include "sipmsg.h"
35 #include "sip-csta.h"
36 #include "sip-soap.h"
37 #include "sip-transport.h"
38 #include "sipe-backend.h"
39 #include "sipe-buddy.h"
40 #include "sipe-cal.h"
41 #include "sipe-chat.h"
42 #include "sipe-conf.h"
43 #include "sipe-core.h"
44 #include "sipe-core-private.h"
45 #include "sipe-group.h"
46 #include "sipe-http.h"
47 #include "sipe-im.h"
48 #include "sipe-nls.h"
49 #include "sipe-ocs2005.h"
50 #include "sipe-ocs2007.h"
51 #include "sipe-schedule.h"
52 #include "sipe-session.h"
53 #include "sipe-status.h"
54 #include "sipe-subscriptions.h"
55 #include "sipe-svc.h"
56 #include "sipe-ucs.h"
57 #include "sipe-utils.h"
58 #include "sipe-webticket.h"
59 #include "sipe-xml.h"
61 struct sipe_buddies {
62 GHashTable *uri;
63 GHashTable *exchange_key;
65 /* Pending photo download HTTP requests */
66 GSList *pending_photo_requests;
69 struct photo_response_data {
70 gchar *who;
71 gchar *photo_hash;
72 struct sipe_http_request *request;
75 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
76 const gchar *uri);
77 static void photo_response_data_free(struct photo_response_data *data);
79 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
80 const gchar *uri,
81 const gchar *exchange_key)
83 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
84 gchar *normalized_uri = g_ascii_strdown(uri, -1);
85 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
86 normalized_uri);
88 if (!buddy) {
89 struct sipe_buddies *buddies = sipe_private->buddies;
91 buddy = g_new0(struct sipe_buddy, 1);
92 buddy->name = normalized_uri;
93 g_hash_table_insert(buddies->uri,
94 buddy->name,
95 buddy);
97 if (exchange_key) {
98 buddy->exchange_key = g_strdup(exchange_key);
99 g_hash_table_insert(buddies->exchange_key,
100 buddy->exchange_key,
101 buddy);
105 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri);
107 buddy_fetch_photo(sipe_private, normalized_uri);
109 normalized_uri = NULL; /* buddy takes ownership */
110 } else {
111 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri);
113 g_free(normalized_uri);
115 return(buddy);
118 void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private,
119 struct sipe_buddy *buddy,
120 struct sipe_group *group,
121 const gchar *alias)
123 const gchar *uri = buddy->name;
124 const gchar *group_name = group->name;
125 sipe_backend_buddy bb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
126 uri,
127 group_name);
129 if (!bb) {
130 bb = sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
131 uri,
132 alias,
133 group_name);
134 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
135 uri, alias ? alias : "<NONE>");
139 if (!is_empty(alias)) {
140 gchar *old_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
141 bb);
143 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri),
144 old_alias)) {
145 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
147 alias);
148 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
149 uri, old_alias, alias);
151 g_free(old_alias);
154 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
155 group,
156 (GCompareFunc) sipe_group_compare,
157 NULL);
158 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
159 uri, group_name);
162 void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private)
164 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
165 NULL,
166 NULL);
167 GSList *entry = buddies;
169 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
170 g_slist_length(buddies));
171 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
172 sipe_buddy_count(sipe_private));
173 while (entry) {
174 sipe_backend_buddy bb = entry->data;
175 gchar *bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC,
176 bb);
177 gchar *gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC,
178 bb);
179 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
180 bname);
181 gboolean in_sipe_groups = FALSE;
183 if (buddy) {
184 GSList *entry2 = buddy->groups;
186 while (entry2) {
187 struct sipe_group *group = entry2->data;
188 if (sipe_strequal(group->name, gname)) {
189 in_sipe_groups = TRUE;
190 break;
192 entry2 = entry2->next;
196 if (!in_sipe_groups) {
197 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: REMOVING '%s' from local group '%s', as buddy is not in that group on remote contact list",
198 bname, gname);
199 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, bb);
202 g_free(gname);
203 g_free(bname);
205 entry = entry->next;
208 g_slist_free(buddies);
211 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
212 const gchar *uri)
214 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
217 struct sipe_buddy *sipe_buddy_find_by_exchange_key(struct sipe_core_private *sipe_private,
218 const gchar *exchange_key)
220 return(g_hash_table_lookup(sipe_private->buddies->exchange_key,
221 exchange_key));
224 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
225 GHFunc callback,
226 gpointer callback_data)
228 g_hash_table_foreach(sipe_private->buddies->uri,
229 callback,
230 callback_data);
233 static void buddy_free(struct sipe_buddy *buddy)
235 #ifndef _WIN32
237 * We are calling g_hash_table_foreach_steal(). That means that no
238 * key/value deallocation functions are called. Therefore the glib
239 * hash code does not touch the key (buddy->name) or value (buddy)
240 * of the to-be-deleted hash node at all. It follows that we
242 * - MUST free the memory for the key ourselves and
243 * - ARE allowed to do it in this function
245 * Conclusion: glib must be broken on the Windows platform if sipe
246 * crashes with SIGTRAP when closing. You'll have to live
247 * with the memory leak until this is fixed.
249 g_free(buddy->name);
250 #endif
251 g_free(buddy->exchange_key);
252 g_free(buddy->activity);
253 g_free(buddy->meeting_subject);
254 g_free(buddy->meeting_location);
255 g_free(buddy->note);
257 g_free(buddy->cal_start_time);
258 g_free(buddy->cal_free_busy_base64);
259 g_free(buddy->cal_free_busy);
260 g_free(buddy->last_non_cal_activity);
262 sipe_cal_free_working_hours(buddy->cal_working_hours);
264 g_free(buddy->device_name);
265 g_slist_free(buddy->groups);
266 g_free(buddy);
269 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
270 gpointer buddy,
271 SIPE_UNUSED_PARAMETER gpointer user_data)
273 buddy_free(buddy);
274 /* We must return TRUE as the key/value have already been deleted */
275 return(TRUE);
278 void sipe_buddy_free(struct sipe_core_private *sipe_private)
280 struct sipe_buddies *buddies = sipe_private->buddies;
282 g_hash_table_foreach_steal(buddies->uri,
283 buddy_free_cb,
284 NULL);
286 /* core is being deallocated, remove all its pending photo requests */
287 while (buddies->pending_photo_requests) {
288 struct photo_response_data *data =
289 buddies->pending_photo_requests->data;
290 buddies->pending_photo_requests =
291 g_slist_remove(buddies->pending_photo_requests, data);
292 photo_response_data_free(data);
295 g_hash_table_destroy(buddies->uri);
296 g_hash_table_destroy(buddies->exchange_key);
297 g_free(buddies);
298 sipe_private->buddies = NULL;
301 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
302 const gchar *uri,
303 guint activity,
304 const gchar *status_text)
306 struct sipe_buddy *sbuddy;
307 const char *activity_str;
309 if (!sipe_public) return NULL; /* happens on pidgin exit */
311 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
312 if (!sbuddy) return NULL;
314 activity_str = sbuddy->activity ? sbuddy->activity :
315 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
316 status_text : NULL;
318 if (activity_str && sbuddy->note) {
319 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
320 } else if (activity_str) {
321 return g_strdup(activity_str);
322 } else if (sbuddy->note) {
323 return g_strdup_printf("<i>%s</i>", sbuddy->note);
324 } else {
325 return NULL;
329 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
330 const gchar *with)
332 sipe_backend_buddy pbuddy;
333 gchar *alias = NULL;
334 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
335 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
337 return alias;
340 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
341 const gchar *who,
342 const gchar *old_group_name,
343 const gchar *new_group_name)
345 struct sipe_buddy * buddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE,
346 who);
347 struct sipe_group * old_group = NULL;
348 struct sipe_group * new_group;
350 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
351 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
353 if(!buddy) { // buddy not in roaming list
354 return;
357 if (old_group_name) {
358 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
360 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
362 if (old_group) {
363 buddy->groups = g_slist_remove(buddy->groups, old_group);
364 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
367 if (!new_group) {
368 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
369 } else {
370 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
371 new_group,
372 (GCompareFunc)sipe_group_compare,
373 NULL);
374 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
378 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
379 const gchar *uri,
380 const gchar *group_name)
382 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
384 if (!sipe_buddy_find_by_uri(sipe_private, uri)) {
385 struct sipe_buddy *b = sipe_buddy_add(sipe_private, uri, NULL);
386 b->just_added = TRUE;
388 sipe_subscribe_presence_single_cb(sipe_private, b->name);
390 } else {
391 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
392 uri);
395 sipe_core_buddy_group(sipe_public,
396 uri,
397 NULL,
398 group_name);
401 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
402 struct sipe_buddy *buddy)
404 struct sipe_buddies *buddies = sipe_private->buddies;
405 gchar *action_name = sipe_utils_presence_key(buddy->name);
406 sipe_schedule_cancel(sipe_private, action_name);
407 g_free(action_name);
409 g_hash_table_remove(buddies->uri, buddy->name);
410 if (buddy->exchange_key)
411 g_hash_table_remove(buddies->exchange_key,
412 buddy->exchange_key);
414 buddy_free(buddy);
418 * Unassociates buddy from group first.
419 * Then see if no groups left, removes buddy completely.
420 * Otherwise updates buddy groups on server.
422 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
423 const gchar *uri,
424 const gchar *group_name)
426 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
427 struct sipe_buddy *b = sipe_buddy_find_by_uri(sipe_private,
428 uri);
430 if (!b) return;
432 if (group_name) {
433 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
434 group_name);
435 if (g) {
436 b->groups = g_slist_remove(b->groups, g);
437 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
438 uri, g->name);
442 if (g_slist_length(b->groups) < 1) {
443 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
444 b->name);
445 sip_soap_request(sipe_private,
446 "deleteContact",
447 request);
448 g_free(request);
449 sipe_buddy_remove(sipe_private, b);
450 } else {
451 /* updates groups on server */
452 sipe_group_update_buddy(sipe_private, b);
457 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
458 const gchar *uri,
459 guint activity)
461 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
462 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
463 uri);
465 if (!sbuddy) return;
467 /* Check if on 2005 system contact's calendar,
468 * then set/preserve it.
470 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
471 sipe_backend_buddy_set_status(sipe_public, uri, activity);
472 } else {
473 sipe_ocs2005_apply_calendar_status(sipe_private,
474 sbuddy,
475 sipe_status_activity_to_token(activity));
479 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
480 const gchar *uri,
481 const gchar *status_name,
482 gboolean is_online,
483 struct sipe_backend_buddy_tooltip *tooltip)
485 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
486 gchar *note = NULL;
487 gboolean is_oof_note = FALSE;
488 const gchar *activity = NULL;
489 gchar *calendar = NULL;
490 const gchar *meeting_subject = NULL;
491 const gchar *meeting_location = NULL;
492 gchar *access_text = NULL;
494 #define SIPE_ADD_BUDDY_INFO(l, t) \
496 gchar *tmp = g_markup_escape_text((t), -1); \
497 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
498 g_free(tmp); \
500 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
501 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
503 if (sipe_public) { /* happens on pidgin exit */
504 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
505 uri);
506 if (sbuddy) {
507 note = sbuddy->note;
508 is_oof_note = sbuddy->is_oof_note;
509 activity = sbuddy->activity;
510 calendar = sipe_cal_get_description(sbuddy);
511 meeting_subject = sbuddy->meeting_subject;
512 meeting_location = sbuddy->meeting_location;
514 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
515 gboolean is_group_access = FALSE;
516 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
517 "user",
518 sipe_get_no_sip_uri(uri),
519 &is_group_access);
520 const char *access_level = sipe_ocs2007_access_level_name(container_id);
521 access_text = is_group_access ?
522 g_strdup(access_level) :
523 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
524 access_level);
528 if (is_online) {
529 const gchar *status_str = activity ? activity : status_name;
531 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
533 if (is_online && !is_empty(calendar)) {
534 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
536 g_free(calendar);
537 if (!is_empty(meeting_location)) {
538 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
539 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
541 if (!is_empty(meeting_subject)) {
542 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
543 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
545 if (note) {
546 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
547 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
548 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
549 note_italics);
550 g_free(note_italics);
552 if (access_text) {
553 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
554 g_free(access_text);
558 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
559 const char *uri,
560 sipe_buddy_info_fields propkey,
561 char *property_value)
563 GSList *buddies, *entry;
565 if (property_value)
566 property_value = g_strstrip(property_value);
568 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
569 while (entry) {
570 gchar *prop_str;
571 sipe_backend_buddy p_buddy = entry->data;
573 /* for Display Name */
574 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
575 gchar *alias;
576 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
577 if (property_value && sipe_is_bad_alias(uri, alias)) {
578 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
579 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
581 g_free(alias);
583 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
584 if (!is_empty(property_value) &&
585 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
587 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
588 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
590 g_free(alias);
592 /* for other properties */
593 else {
594 if (!is_empty(property_value)) {
595 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
596 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
597 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
599 g_free(prop_str);
603 entry = entry->next;
605 g_slist_free(buddies);
609 struct ms_dlx_data;
610 struct ms_dlx_data {
611 GSList *search_rows;
612 gchar *other;
613 guint max_returns;
614 sipe_svc_callback *callback;
615 struct sipe_svc_session *session;
616 gchar *wsse_security;
617 struct sipe_backend_search_token *token;
618 /* must call ms_dlx_free() */
619 void (*failed_callback)(struct sipe_core_private *sipe_private,
620 struct ms_dlx_data *mdd);
623 static void ms_dlx_free(struct ms_dlx_data *mdd)
625 sipe_utils_slist_free_full(mdd->search_rows, g_free);
626 sipe_svc_session_close(mdd->session);
627 g_free(mdd->other);
628 g_free(mdd->wsse_security);
629 g_free(mdd);
632 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
633 #define DLX_SEARCH_ITEM \
634 "<AbEntryRequest.ChangeSearchQuery>" \
635 " <SearchOn>%s</SearchOn>" \
636 " <Value>%s</Value>" \
637 "</AbEntryRequest.ChangeSearchQuery>"
639 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
640 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
641 guint i = 0;
642 gchar *query = NULL;
644 while (query_rows) {
645 gchar *attr;
646 gchar *value;
648 attr = query_rows->data;
649 query_rows = g_slist_next(query_rows);
650 value = query_rows->data;
651 query_rows = g_slist_next(query_rows);
653 if (!attr || !value)
654 break;
656 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
657 attr, value);
659 attrs[i] = NULL;
661 if (i) {
662 query = g_strjoinv(NULL, attrs);
663 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
664 query ? query : "");
667 g_strfreev(attrs);
669 return query;
672 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
673 const gchar *base_uri,
674 const gchar *auth_uri,
675 const gchar *wsse_security,
676 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
677 gpointer callback_data)
679 struct ms_dlx_data *mdd = callback_data;
681 if (wsse_security) {
682 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
684 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
685 base_uri);
687 if (sipe_svc_ab_entry_request(sipe_private,
688 mdd->session,
689 auth_uri,
690 wsse_security,
691 query,
692 g_slist_length(mdd->search_rows) / 2,
693 mdd->max_returns,
694 mdd->callback,
695 mdd)) {
697 /* keep webticket security token for potential further use */
698 mdd->wsse_security = g_strdup(wsse_security);
700 /* callback data passed down the line */
701 mdd = NULL;
703 g_free(query);
705 } else {
706 /* no ticket: this will show the minmum information */
707 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
708 base_uri);
711 if (mdd)
712 mdd->failed_callback(sipe_private, mdd);
715 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
716 struct ms_dlx_data *mdd)
718 if (!sipe_webticket_request(sipe_private,
719 mdd->session,
720 sipe_private->dlx_uri,
721 "AddressBookWebTicketBearer",
722 ms_dlx_webticket,
723 mdd)) {
724 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
725 sipe_private->dlx_uri);
726 mdd->failed_callback(sipe_private, mdd);
730 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
731 struct sipe_backend_search_results *results,
732 guint match_count,
733 gboolean more)
735 gchar *secondary = g_strdup_printf(
736 dngettext(PACKAGE_NAME,
737 "Found %d contact%s:",
738 "Found %d contacts%s:", match_count),
739 match_count, more ? _(" (more matched your query)") : "");
741 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
742 results,
743 secondary,
744 more);
745 g_free(secondary);
748 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
749 const gchar *uri,
750 SIPE_UNUSED_PARAMETER const gchar *raw,
751 sipe_xml *soap_body,
752 gpointer callback_data)
754 struct ms_dlx_data *mdd = callback_data;
756 if (soap_body) {
757 const sipe_xml *node;
758 struct sipe_backend_search_results *results;
759 GHashTable *found;
761 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
762 uri);
764 /* any matches? */
765 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
766 if (!node) {
767 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
768 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
769 mdd->token,
770 _("No contacts found"));
771 ms_dlx_free(mdd);
772 return;
775 /* OK, we found something - show the results to the user */
776 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
777 mdd->token);
778 if (!results) {
779 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
780 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
781 mdd->token,
782 _("Unable to display the search results"));
783 ms_dlx_free(mdd);
784 return;
787 /* SearchAbEntryResult can contain duplicates */
788 found = g_hash_table_new_full(g_str_hash, g_str_equal,
789 g_free, NULL);
791 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
792 const sipe_xml *attrs;
793 gchar *sip_uri = NULL;
794 gchar *displayname = NULL;
795 gchar *company = NULL;
796 gchar *country = NULL;
797 gchar *email = NULL;
799 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
800 attrs;
801 attrs = sipe_xml_twin(attrs)) {
802 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
803 "Name"));
804 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
805 "Value"));
807 if (!is_empty(value)) {
808 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
809 g_free(sip_uri);
810 sip_uri = value;
811 value = NULL;
812 } else if (sipe_strcase_equal(name, "displayname")) {
813 g_free(displayname);
814 displayname = value;
815 value = NULL;
816 } else if (sipe_strcase_equal(name, "mail")) {
817 g_free(email);
818 email = value;
819 value = NULL;
820 } else if (sipe_strcase_equal(name, "company")) {
821 g_free(company);
822 company = value;
823 value = NULL;
824 } else if (sipe_strcase_equal(name, "country")) {
825 g_free(country);
826 country = value;
827 value = NULL;
831 g_free(value);
832 g_free(name);
835 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
836 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
837 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
838 results,
839 uri_parts[1],
840 displayname,
841 company,
842 country,
843 email);
844 g_strfreev(uri_parts);
846 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
847 sip_uri = NULL;
850 g_free(email);
851 g_free(country);
852 g_free(company);
853 g_free(displayname);
854 g_free(sip_uri);
857 search_contacts_finalize(sipe_private, results,
858 g_hash_table_size(found),
859 FALSE);
860 g_hash_table_destroy(found);
861 ms_dlx_free(mdd);
863 } else {
864 mdd->failed_callback(sipe_private, mdd);
868 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
869 struct sipmsg *msg,
870 struct transaction *trans)
872 struct sipe_backend_search_token *token = trans->payload->data;
873 struct sipe_backend_search_results *results;
874 sipe_xml *searchResults;
875 const sipe_xml *mrow;
876 guint match_count = 0;
877 gboolean more = FALSE;
879 /* valid response? */
880 if (msg->response != 200) {
881 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
882 msg->response);
883 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
884 token,
885 _("Contact search failed"));
886 return(FALSE);
889 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
891 /* valid XML? */
892 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
893 if (!searchResults) {
894 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
895 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
896 token,
897 _("Contact search failed"));
898 return(FALSE);
901 /* any matches? */
902 mrow = sipe_xml_child(searchResults, "Body/Array/row");
903 if (!mrow) {
904 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
905 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
906 token,
907 _("No contacts found"));
909 sipe_xml_free(searchResults);
910 return(FALSE);
913 /* OK, we found something - show the results to the user */
914 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
915 trans->payload->data);
916 if (!results) {
917 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
918 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
919 token,
920 _("Unable to display the search results"));
922 sipe_xml_free(searchResults);
923 return FALSE;
926 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
927 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
928 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
929 results,
930 uri_parts[1],
931 sipe_xml_attribute(mrow, "displayName"),
932 sipe_xml_attribute(mrow, "company"),
933 sipe_xml_attribute(mrow, "country"),
934 sipe_xml_attribute(mrow, "email"));
935 g_strfreev(uri_parts);
936 match_count++;
939 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
940 char *data = sipe_xml_data(mrow);
941 more = (g_ascii_strcasecmp(data, "true") == 0);
942 g_free(data);
945 search_contacts_finalize(sipe_private, results, match_count, more);
946 sipe_xml_free(searchResults);
948 return(TRUE);
951 static void search_soap_request(struct sipe_core_private *sipe_private,
952 struct sipe_backend_search_token *token,
953 GSList *search_rows)
955 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
956 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
958 payload->data = token;
960 sip_soap_directory_search(sipe_private,
961 100,
962 query,
963 process_search_contact_response,
964 payload);
965 g_free(query);
968 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
969 struct ms_dlx_data *mdd)
971 /* error using [MS-DLX] server, retry using Active Directory */
972 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
973 ms_dlx_free(mdd);
976 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
977 struct sipe_backend_search_token *token,
978 const gchar *given_name,
979 const gchar *surname,
980 const gchar *email,
981 const gchar *company,
982 const gchar *country)
984 GSList *query_rows = NULL;
986 #define ADD_QUERY_ROW(attr, val) \
987 if (val) { \
988 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
989 query_rows = g_slist_append(query_rows, g_strdup(val)); \
992 ADD_QUERY_ROW("givenName", given_name);
993 ADD_QUERY_ROW("sn", surname);
994 ADD_QUERY_ROW("mail", email);
995 ADD_QUERY_ROW("company", company);
996 ADD_QUERY_ROW("c", country);
998 if (query_rows) {
999 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
1000 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1002 mdd->search_rows = query_rows;
1003 mdd->max_returns = 100;
1004 mdd->callback = search_ab_entry_response;
1005 mdd->failed_callback = search_ab_entry_failed;
1006 mdd->session = sipe_svc_session_start();
1007 mdd->token = token;
1009 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
1011 } else {
1012 /* no [MS-DLX] server, use Active Directory search instead */
1013 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
1014 sipe_utils_slist_free_full(query_rows, g_free);
1016 } else
1017 sipe_backend_search_failed(sipe_public,
1018 token,
1019 _("Invalid contact search query"));
1022 static void get_info_finalize(struct sipe_core_private *sipe_private,
1023 struct sipe_backend_buddy_info *info,
1024 const gchar *uri,
1025 const gchar *server_alias,
1026 const gchar *email)
1028 sipe_backend_buddy bbuddy;
1029 struct sipe_buddy *sbuddy;
1030 gchar *alias;
1031 gchar *value;
1033 if (!info) {
1034 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1035 } else {
1036 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
1038 if (!info)
1039 return;
1041 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
1043 if (is_empty(server_alias)) {
1044 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
1045 bbuddy);
1046 if (value) {
1047 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1048 info,
1049 SIPE_BUDDY_INFO_DISPLAY_NAME,
1050 value);
1052 } else {
1053 value = g_strdup(server_alias);
1056 /* present alias if it differs from server alias */
1057 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
1058 if (alias && !sipe_strequal(alias, value))
1060 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1061 info,
1062 SIPE_BUDDY_INFO_ALIAS,
1063 alias);
1065 g_free(alias);
1066 g_free(value);
1068 if (is_empty(email)) {
1069 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1070 bbuddy,
1071 SIPE_BUDDY_INFO_EMAIL);
1072 if (value) {
1073 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1074 info,
1075 SIPE_BUDDY_INFO_EMAIL,
1076 value);
1077 g_free(value);
1081 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1082 bbuddy,
1083 SIPE_BUDDY_INFO_SITE);
1084 if (value) {
1085 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1086 info,
1087 SIPE_BUDDY_INFO_SITE,
1088 value);
1089 g_free(value);
1092 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
1093 if (sbuddy && sbuddy->device_name) {
1094 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1095 info,
1096 SIPE_BUDDY_INFO_DEVICE,
1097 sbuddy->device_name);
1100 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
1104 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
1105 const gchar *uri,
1106 SIPE_UNUSED_PARAMETER const gchar *raw,
1107 sipe_xml *soap_body,
1108 gpointer callback_data)
1110 struct ms_dlx_data *mdd = callback_data;
1111 struct sipe_backend_buddy_info *info = NULL;
1112 gchar *server_alias = NULL;
1113 gchar *email = NULL;
1115 if (soap_body) {
1116 const sipe_xml *node;
1118 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1119 uri);
1121 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1123 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1124 node;
1125 node = sipe_xml_twin(node)) {
1126 gchar *name = sipe_xml_data(sipe_xml_child(node,
1127 "Name"));
1128 gchar *value = sipe_xml_data(sipe_xml_child(node,
1129 "Value"));
1130 const sipe_xml *values = sipe_xml_child(node,
1131 "Values");
1133 /* Single value entries */
1134 if (!is_empty(value)) {
1136 if (sipe_strcase_equal(name, "displayname")) {
1137 g_free(server_alias);
1138 server_alias = value;
1139 value = NULL;
1140 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1141 info,
1142 SIPE_BUDDY_INFO_DISPLAY_NAME,
1143 server_alias);
1144 } else if (sipe_strcase_equal(name, "mail")) {
1145 g_free(email);
1146 email = value;
1147 value = NULL;
1148 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1149 info,
1150 SIPE_BUDDY_INFO_EMAIL,
1151 email);
1152 } else if (sipe_strcase_equal(name, "title")) {
1153 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1154 info,
1155 SIPE_BUDDY_INFO_JOB_TITLE,
1156 value);
1157 } else if (sipe_strcase_equal(name, "company")) {
1158 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1159 info,
1160 SIPE_BUDDY_INFO_COMPANY,
1161 value);
1162 } else if (sipe_strcase_equal(name, "country")) {
1163 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1164 info,
1165 SIPE_BUDDY_INFO_COUNTRY,
1166 value);
1169 } else if (values) {
1170 gchar *first = sipe_xml_data(sipe_xml_child(values,
1171 "string"));
1173 if (sipe_strcase_equal(name, "telephonenumber")) {
1174 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1175 info,
1176 SIPE_BUDDY_INFO_WORK_PHONE,
1177 first);
1180 g_free(first);
1183 g_free(value);
1184 g_free(name);
1188 /* this will show the minmum information */
1189 get_info_finalize(sipe_private,
1190 info,
1191 mdd->other,
1192 server_alias,
1193 email);
1195 g_free(email);
1196 g_free(server_alias);
1197 ms_dlx_free(mdd);
1200 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1201 struct sipmsg *msg,
1202 struct transaction *trans)
1204 const gchar *uri = trans->payload->data;
1205 struct sipe_backend_buddy_info *info = NULL;
1206 gchar *server_alias = NULL;
1207 gchar *email = NULL;
1209 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1210 uri, sipe_private->username);
1212 if (msg->response != 200) {
1213 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1214 } else {
1215 sipe_xml *searchResults;
1216 const sipe_xml *mrow;
1218 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1219 msg->body ? msg->body : "");
1221 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1222 if (!searchResults) {
1224 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1226 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1227 const gchar *value;
1228 gchar *phone_number;
1230 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1232 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1233 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1234 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1237 * For 2007 system we will take this from ContactCard -
1238 * it has cleaner tel: URIs at least
1240 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1241 char *tel_uri = sip_to_tel_uri(phone_number);
1242 /* trims its parameters, so call first */
1243 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1244 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1245 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1246 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1247 g_free(tel_uri);
1249 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1250 uri);
1253 if (!is_empty(server_alias)) {
1254 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1255 info,
1256 SIPE_BUDDY_INFO_DISPLAY_NAME,
1257 server_alias);
1259 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1260 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1261 info,
1262 SIPE_BUDDY_INFO_JOB_TITLE,
1263 value);
1265 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1266 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1267 info,
1268 SIPE_BUDDY_INFO_OFFICE,
1269 value);
1271 if (!is_empty(phone_number)) {
1272 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1273 info,
1274 SIPE_BUDDY_INFO_WORK_PHONE,
1275 phone_number);
1277 g_free(phone_number);
1278 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1279 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1280 info,
1281 SIPE_BUDDY_INFO_COMPANY,
1282 value);
1284 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1285 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1286 info,
1287 SIPE_BUDDY_INFO_CITY,
1288 value);
1290 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1291 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1292 info,
1293 SIPE_BUDDY_INFO_STATE,
1294 value);
1296 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1297 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1298 info,
1299 SIPE_BUDDY_INFO_COUNTRY,
1300 value);
1302 if (!is_empty(email)) {
1303 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1304 info,
1305 SIPE_BUDDY_INFO_EMAIL,
1306 email);
1309 sipe_xml_free(searchResults);
1312 /* this will show the minmum information */
1313 get_info_finalize(sipe_private,
1314 info,
1315 uri,
1316 server_alias,
1317 email);
1319 g_free(server_alias);
1320 g_free(email);
1322 return TRUE;
1325 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1326 struct ms_dlx_data *mdd)
1328 /* error using [MS-DLX] server, retry using Active Directory */
1329 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1330 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1332 payload->destroy = g_free;
1333 payload->data = mdd->other;
1334 mdd->other = NULL;
1336 sip_soap_directory_search(sipe_private,
1338 query,
1339 process_get_info_response,
1340 payload);
1342 ms_dlx_free(mdd);
1343 g_free(query);
1346 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1347 const gchar *who)
1349 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1351 if (sipe_private->dlx_uri) {
1352 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1354 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1355 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1357 mdd->other = g_strdup(who);
1358 mdd->max_returns = 1;
1359 mdd->callback = get_info_ab_entry_response;
1360 mdd->failed_callback = get_info_ab_entry_failed;
1361 mdd->session = sipe_svc_session_start();
1363 ms_dlx_webticket_request(sipe_private, mdd);
1365 } else {
1366 /* no [MS-DLX] server, use Active Directory search instead */
1367 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1368 "msRTCSIP-PrimaryUserAddress",
1369 who);
1370 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1372 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1373 row ? row : "");
1375 payload->destroy = g_free;
1376 payload->data = g_strdup(who);
1378 sip_soap_directory_search(sipe_private,
1380 row,
1381 process_get_info_response,
1382 payload);
1383 g_free(row);
1387 static void photo_response_data_free(struct photo_response_data *data)
1389 g_free(data->who);
1390 g_free(data->photo_hash);
1391 if (data->request) {
1392 sipe_http_request_cancel(data->request);
1394 g_free(data);
1397 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1398 guint status,
1399 GSList *headers,
1400 const char *body,
1401 gpointer data)
1403 struct photo_response_data *rdata = (struct photo_response_data *) data;
1405 rdata->request = NULL;
1407 if (status == SIPE_HTTP_STATUS_OK) {
1408 const gchar *len_str = sipe_utils_nameval_find(headers,
1409 "Content-Length");
1410 if (len_str) {
1411 gsize photo_size = atoi(len_str);
1412 gpointer photo = g_new(char, photo_size);
1414 if (photo) {
1415 memcpy(photo, body, photo_size);
1417 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1418 rdata->who,
1419 photo,
1420 photo_size,
1421 rdata->photo_hash);
1426 sipe_private->buddies->pending_photo_requests =
1427 g_slist_remove(sipe_private->buddies->pending_photo_requests, rdata);
1429 photo_response_data_free(rdata);
1432 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1434 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1435 gchar *wsse_security_base64;
1436 gchar *x_ms_webticket_header;
1438 if (!assertion) {
1439 return NULL;
1442 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1443 strlen(assertion));
1444 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1445 wsse_security_base64);
1447 g_free(assertion);
1448 g_free(wsse_security_base64);
1450 return x_ms_webticket_header;
1453 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1454 const gchar *uri,
1455 SIPE_UNUSED_PARAMETER const gchar *raw,
1456 sipe_xml *soap_body,
1457 gpointer callback_data)
1459 struct ms_dlx_data *mdd = callback_data;
1460 gchar *photo_rel_path = NULL;
1461 gchar *photo_hash = NULL;
1462 const gchar *photo_hash_old =
1463 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1465 if (soap_body) {
1466 const sipe_xml *node;
1468 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1469 uri);
1471 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1472 node;
1473 node = sipe_xml_twin(node)) {
1474 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1475 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1477 if (!is_empty(value)) {
1478 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1479 g_free(photo_rel_path);
1480 photo_rel_path = value;
1481 value = NULL;
1482 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1483 g_free(photo_hash);
1484 photo_hash = value;
1485 value = NULL;
1489 g_free(value);
1490 g_free(name);
1494 if (sipe_private->addressbook_uri && photo_rel_path &&
1495 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1496 gchar *photo_url = g_strdup_printf("%s/%s",
1497 sipe_private->addressbook_uri, photo_rel_path);
1498 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1500 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1501 data->who = g_strdup(mdd->other);
1502 data->photo_hash = photo_hash;
1503 photo_hash = NULL;
1505 data->request = sipe_http_request_get(sipe_private,
1506 photo_url,
1507 x_ms_webticket_header,
1508 process_buddy_photo_response,
1509 data);
1511 if (data->request) {
1512 sipe_private->buddies->pending_photo_requests =
1513 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1514 sipe_http_request_ready(data->request);
1515 } else {
1516 photo_response_data_free(data);
1519 g_free(x_ms_webticket_header);
1520 g_free(photo_url);
1523 g_free(photo_rel_path);
1524 g_free(photo_hash);
1525 ms_dlx_free(mdd);
1528 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1529 struct ms_dlx_data *mdd)
1531 ms_dlx_free(mdd);
1534 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1535 const gchar *uri)
1537 if (sipe_backend_uses_photo()) {
1539 /* Lync 2013 or newer: use UCS */
1540 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013)) {
1542 sipe_ucs_get_photo(sipe_private, uri);
1544 /* Lync 2010: use [MS-DLX] */
1545 } else if (sipe_private->dlx_uri &&
1546 sipe_private->addressbook_uri) {
1547 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1549 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1550 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1552 mdd->other = g_strdup(uri);
1553 mdd->max_returns = 1;
1554 mdd->callback = get_photo_ab_entry_response;
1555 mdd->failed_callback = get_photo_ab_entry_failed;
1556 mdd->session = sipe_svc_session_start();
1558 ms_dlx_webticket_request(sipe_private, mdd);
1563 static void buddy_refresh_photos_cb(gpointer uri,
1564 SIPE_UNUSED_PARAMETER gpointer value,
1565 gpointer sipe_private)
1567 buddy_fetch_photo(sipe_private, uri);
1570 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1572 g_hash_table_foreach(sipe_private->buddies->uri,
1573 buddy_refresh_photos_cb,
1574 sipe_private);
1577 /* Buddy menu callbacks*/
1579 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1580 const gchar *who)
1582 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1584 /* 2007+ conference */
1585 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1586 sipe_conf_add(sipe_private, who);
1588 /* 2005- multiparty chat */
1589 } else {
1590 gchar *self = sip_uri_self(sipe_private);
1591 struct sip_session *session;
1593 session = sipe_session_add_chat(sipe_private,
1594 NULL,
1595 TRUE,
1596 self);
1597 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1598 session->chat_session,
1599 session->chat_session->title,
1600 self);
1601 g_free(self);
1603 sipe_im_invite(sipe_private, session, who,
1604 NULL, NULL, NULL, FALSE);
1608 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1609 const gchar *who)
1611 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1612 who,
1613 NULL);
1614 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1615 buddy,
1616 SIPE_BUDDY_INFO_EMAIL);
1618 if (email) {
1619 gchar *command_line = g_strdup_printf(
1620 #ifdef _WIN32
1621 "cmd /c start"
1622 #else
1623 "xdg-email"
1624 #endif
1625 " mailto:%s", email);
1626 g_free(email);
1628 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1629 command_line);
1630 g_spawn_command_line_async(command_line, NULL);
1631 g_free(command_line);
1633 } else {
1634 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1635 who);
1639 /* Buddy menu */
1641 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1642 struct sipe_backend_buddy_menu *menu,
1643 sipe_backend_buddy buddy,
1644 sipe_buddy_info_fields id_phone,
1645 sipe_buddy_info_fields id_display,
1646 const gchar *type)
1648 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1649 buddy,
1650 id_phone);
1651 if (phone) {
1652 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1653 buddy,
1654 id_display);
1655 gchar *tmp = NULL;
1656 gchar *label = g_strdup_printf("%s %s",
1657 type,
1658 display ? display :
1659 (tmp = sip_tel_uri_denormalize(phone)));
1660 menu = sipe_backend_buddy_menu_add(sipe_public,
1661 menu,
1662 label,
1663 SIPE_BUDDY_MENU_MAKE_CALL,
1664 phone);
1665 g_free(tmp);
1666 g_free(label);
1667 g_free(display);
1668 g_free(phone);
1671 return(menu);
1674 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1675 const gchar *buddy_name,
1676 struct sipe_backend_buddy_menu *menu)
1678 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1679 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1680 buddy_name,
1681 NULL);
1682 gchar *self = sip_uri_self(sipe_private);
1684 SIPE_SESSION_FOREACH {
1685 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1687 struct sipe_chat_session *chat_session = session->chat_session;
1688 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1690 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1692 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1694 if (is_conf &&
1695 /* Not conf OP */
1696 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1697 /* We are a conf OP */
1698 conf_op) {
1699 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1700 chat_session->title);
1701 menu = sipe_backend_buddy_menu_add(sipe_public,
1702 menu,
1703 label,
1704 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1705 chat_session);
1706 g_free(label);
1709 if (is_conf &&
1710 /* We are a conf OP */
1711 conf_op) {
1712 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1713 chat_session->title);
1714 menu = sipe_backend_buddy_menu_add(sipe_public,
1715 menu,
1716 label,
1717 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1718 chat_session);
1719 g_free(label);
1722 else
1724 if (!is_conf ||
1725 (is_conf && !session->locked)) {
1726 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1727 chat_session->title);
1728 menu = sipe_backend_buddy_menu_add(sipe_public,
1729 menu,
1730 label,
1731 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1732 chat_session);
1733 g_free(label);
1737 } SIPE_SESSION_FOREACH_END;
1738 g_free(self);
1740 menu = sipe_backend_buddy_menu_add(sipe_public,
1741 menu,
1742 _("New chat"),
1743 SIPE_BUDDY_MENU_NEW_CHAT,
1744 NULL);
1746 /* add buddy's phone numbers if we have call control */
1747 if (sip_csta_is_idle(sipe_private)) {
1749 /* work phone */
1750 menu = buddy_menu_phone(sipe_public,
1751 menu,
1752 buddy,
1753 SIPE_BUDDY_INFO_WORK_PHONE,
1754 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1755 _("Work"));
1756 /* mobile phone */
1757 menu = buddy_menu_phone(sipe_public,
1758 menu,
1759 buddy,
1760 SIPE_BUDDY_INFO_MOBILE_PHONE,
1761 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1762 _("Mobile"));
1764 /* home phone */
1765 menu = buddy_menu_phone(sipe_public,
1766 menu,
1767 buddy,
1768 SIPE_BUDDY_INFO_HOME_PHONE,
1769 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1770 _("Home"));
1772 /* other phone */
1773 menu = buddy_menu_phone(sipe_public,
1774 menu,
1775 buddy,
1776 SIPE_BUDDY_INFO_OTHER_PHONE,
1777 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1778 _("Other"));
1780 /* custom1 phone */
1781 menu = buddy_menu_phone(sipe_public,
1782 menu,
1783 buddy,
1784 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1785 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1786 _("Custom1"));
1790 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1791 buddy,
1792 SIPE_BUDDY_INFO_EMAIL);
1793 if (email) {
1794 menu = sipe_backend_buddy_menu_add(sipe_public,
1795 menu,
1796 _("Send email..."),
1797 SIPE_BUDDY_MENU_SEND_EMAIL,
1798 NULL);
1799 g_free(email);
1803 /* access level control */
1804 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1805 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1806 menu,
1807 _("Access level"),
1808 sipe_ocs2007_access_control_menu(sipe_private,
1809 buddy_name));
1811 return(menu);
1814 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
1816 return(g_hash_table_size(sipe_private->buddies->uri));
1819 static guint sipe_ht_hash_nick(const char *nick)
1821 char *lc = g_utf8_strdown(nick, -1);
1822 guint bucket = g_str_hash(lc);
1823 g_free(lc);
1825 return bucket;
1828 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
1830 char *nick1_norm = NULL;
1831 char *nick2_norm = NULL;
1832 gboolean equal;
1834 if (nick1 == NULL && nick2 == NULL) return TRUE;
1835 if (nick1 == NULL || nick2 == NULL ||
1836 !g_utf8_validate(nick1, -1, NULL) ||
1837 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
1839 nick1_norm = g_utf8_casefold(nick1, -1);
1840 nick2_norm = g_utf8_casefold(nick2, -1);
1841 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
1842 g_free(nick2_norm);
1843 g_free(nick1_norm);
1845 return equal;
1848 void sipe_buddy_init(struct sipe_core_private *sipe_private)
1850 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
1851 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
1852 (GEqualFunc) sipe_ht_equals_nick);
1853 buddies->exchange_key = g_hash_table_new(g_str_hash,
1854 g_str_equal);
1855 sipe_private->buddies = buddies;
1859 Local Variables:
1860 mode: c
1861 c-file-style: "bsd"
1862 indent-tabs-mode: t
1863 tab-width: 8
1864 End: