buddy: factor out local list cleanup code
[siplcs.git] / src / core / sipe-buddy.c
blobc074cea4d73e66d73fdc28bd31ade7fc07da78f8
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 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private, uri);
84 if (!buddy) {
85 struct sipe_buddies *buddies = sipe_private->buddies;
87 buddy = g_new0(struct sipe_buddy, 1);
88 buddy->name = g_strdup(uri);
89 g_hash_table_insert(buddies->uri,
90 buddy->name,
91 buddy);
93 if (exchange_key) {
94 buddy->exchange_key = g_strdup(exchange_key);
95 g_hash_table_insert(buddies->exchange_key,
96 buddy->exchange_key,
97 buddy);
101 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", uri);
103 buddy_fetch_photo(sipe_private, uri);
104 } else {
105 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", uri);
108 return buddy;
111 void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private)
113 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
114 NULL,
115 NULL);
116 GSList *entry = buddies;
118 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
119 g_slist_length(buddies));
120 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
121 sipe_buddy_count(sipe_private));
122 while (entry) {
123 sipe_backend_buddy bb = entry->data;
124 gchar *bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC,
125 bb);
126 gchar *gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC,
127 bb);
128 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
129 bname);
130 gboolean in_sipe_groups = FALSE;
132 if (buddy) {
133 GSList *entry2 = buddy->groups;
135 while (entry2) {
136 struct sipe_group *group = entry2->data;
137 if (sipe_strequal(group->name, gname)) {
138 in_sipe_groups = TRUE;
139 break;
141 entry2 = entry2->next;
145 if (!in_sipe_groups) {
146 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",
147 bname, gname);
148 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, bb);
151 g_free(gname);
152 g_free(bname);
154 entry = entry->next;
157 g_slist_free(buddies);
160 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
161 const gchar *uri)
163 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
166 struct sipe_buddy *sipe_buddy_find_by_exchange_key(struct sipe_core_private *sipe_private,
167 const gchar *exchange_key)
169 return(g_hash_table_lookup(sipe_private->buddies->exchange_key,
170 exchange_key));
173 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
174 GHFunc callback,
175 gpointer callback_data)
177 g_hash_table_foreach(sipe_private->buddies->uri,
178 callback,
179 callback_data);
182 static void buddy_free(struct sipe_buddy *buddy)
184 #ifndef _WIN32
186 * We are calling g_hash_table_foreach_steal(). That means that no
187 * key/value deallocation functions are called. Therefore the glib
188 * hash code does not touch the key (buddy->name) or value (buddy)
189 * of the to-be-deleted hash node at all. It follows that we
191 * - MUST free the memory for the key ourselves and
192 * - ARE allowed to do it in this function
194 * Conclusion: glib must be broken on the Windows platform if sipe
195 * crashes with SIGTRAP when closing. You'll have to live
196 * with the memory leak until this is fixed.
198 g_free(buddy->name);
199 #endif
200 g_free(buddy->exchange_key);
201 g_free(buddy->activity);
202 g_free(buddy->meeting_subject);
203 g_free(buddy->meeting_location);
204 g_free(buddy->note);
206 g_free(buddy->cal_start_time);
207 g_free(buddy->cal_free_busy_base64);
208 g_free(buddy->cal_free_busy);
209 g_free(buddy->last_non_cal_activity);
211 sipe_cal_free_working_hours(buddy->cal_working_hours);
213 g_free(buddy->device_name);
214 g_slist_free(buddy->groups);
215 g_free(buddy);
218 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
219 gpointer buddy,
220 SIPE_UNUSED_PARAMETER gpointer user_data)
222 buddy_free(buddy);
223 /* We must return TRUE as the key/value have already been deleted */
224 return(TRUE);
227 void sipe_buddy_free(struct sipe_core_private *sipe_private)
229 struct sipe_buddies *buddies = sipe_private->buddies;
231 g_hash_table_foreach_steal(buddies->uri,
232 buddy_free_cb,
233 NULL);
235 /* core is being deallocated, remove all its pending photo requests */
236 while (buddies->pending_photo_requests) {
237 struct photo_response_data *data =
238 buddies->pending_photo_requests->data;
239 buddies->pending_photo_requests =
240 g_slist_remove(buddies->pending_photo_requests, data);
241 photo_response_data_free(data);
244 g_hash_table_destroy(buddies->uri);
245 g_hash_table_destroy(buddies->exchange_key);
246 g_free(buddies);
247 sipe_private->buddies = NULL;
250 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
251 const gchar *uri,
252 guint activity,
253 const gchar *status_text)
255 struct sipe_buddy *sbuddy;
256 const char *activity_str;
258 if (!sipe_public) return NULL; /* happens on pidgin exit */
260 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
261 if (!sbuddy) return NULL;
263 activity_str = sbuddy->activity ? sbuddy->activity :
264 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
265 status_text : NULL;
267 if (activity_str && sbuddy->note) {
268 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
269 } else if (activity_str) {
270 return g_strdup(activity_str);
271 } else if (sbuddy->note) {
272 return g_strdup_printf("<i>%s</i>", sbuddy->note);
273 } else {
274 return NULL;
278 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
279 const gchar *with)
281 sipe_backend_buddy pbuddy;
282 gchar *alias = NULL;
283 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
284 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
286 return alias;
289 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
290 const gchar *who,
291 const gchar *old_group_name,
292 const gchar *new_group_name)
294 struct sipe_buddy * buddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE,
295 who);
296 struct sipe_group * old_group = NULL;
297 struct sipe_group * new_group;
299 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
300 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
302 if(!buddy) { // buddy not in roaming list
303 return;
306 if (old_group_name) {
307 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
309 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
311 if (old_group) {
312 buddy->groups = g_slist_remove(buddy->groups, old_group);
313 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
316 if (!new_group) {
317 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
318 } else {
319 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
320 new_group,
321 (GCompareFunc)sipe_group_compare,
322 NULL);
323 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
327 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
328 const gchar *uri,
329 const gchar *group_name)
331 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
333 if (!sipe_buddy_find_by_uri(sipe_private, uri)) {
334 struct sipe_buddy *b = sipe_buddy_add(sipe_private, uri, NULL);
335 b->just_added = TRUE;
337 sipe_subscribe_presence_single_cb(sipe_private, b->name);
339 } else {
340 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
341 uri);
344 sipe_core_buddy_group(sipe_public,
345 uri,
346 NULL,
347 group_name);
350 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
351 struct sipe_buddy *buddy)
353 struct sipe_buddies *buddies = sipe_private->buddies;
354 gchar *action_name = sipe_utils_presence_key(buddy->name);
355 sipe_schedule_cancel(sipe_private, action_name);
356 g_free(action_name);
358 g_hash_table_remove(buddies->uri, buddy->name);
359 if (buddy->exchange_key)
360 g_hash_table_remove(buddies->exchange_key,
361 buddy->exchange_key);
363 buddy_free(buddy);
367 * Unassociates buddy from group first.
368 * Then see if no groups left, removes buddy completely.
369 * Otherwise updates buddy groups on server.
371 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
372 const gchar *uri,
373 const gchar *group_name)
375 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
376 struct sipe_buddy *b = sipe_buddy_find_by_uri(sipe_private,
377 uri);
379 if (!b) return;
381 if (group_name) {
382 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
383 group_name);
384 if (g) {
385 b->groups = g_slist_remove(b->groups, g);
386 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
387 uri, g->name);
391 if (g_slist_length(b->groups) < 1) {
392 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
393 b->name);
394 sip_soap_request(sipe_private,
395 "deleteContact",
396 request);
397 g_free(request);
398 sipe_buddy_remove(sipe_private, b);
399 } else {
400 /* updates groups on server */
401 sipe_group_update_buddy(sipe_private, b);
406 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
407 const gchar *uri,
408 guint activity)
410 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
411 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
412 uri);
414 if (!sbuddy) return;
416 /* Check if on 2005 system contact's calendar,
417 * then set/preserve it.
419 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
420 sipe_backend_buddy_set_status(sipe_public, uri, activity);
421 } else {
422 sipe_ocs2005_apply_calendar_status(sipe_private,
423 sbuddy,
424 sipe_status_activity_to_token(activity));
428 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
429 const gchar *uri,
430 const gchar *status_name,
431 gboolean is_online,
432 struct sipe_backend_buddy_tooltip *tooltip)
434 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
435 gchar *note = NULL;
436 gboolean is_oof_note = FALSE;
437 const gchar *activity = NULL;
438 gchar *calendar = NULL;
439 const gchar *meeting_subject = NULL;
440 const gchar *meeting_location = NULL;
441 gchar *access_text = NULL;
443 #define SIPE_ADD_BUDDY_INFO(l, t) \
445 gchar *tmp = g_markup_escape_text((t), -1); \
446 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
447 g_free(tmp); \
449 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
450 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
452 if (sipe_public) { /* happens on pidgin exit */
453 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
454 uri);
455 if (sbuddy) {
456 note = sbuddy->note;
457 is_oof_note = sbuddy->is_oof_note;
458 activity = sbuddy->activity;
459 calendar = sipe_cal_get_description(sbuddy);
460 meeting_subject = sbuddy->meeting_subject;
461 meeting_location = sbuddy->meeting_location;
463 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
464 gboolean is_group_access = FALSE;
465 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
466 "user",
467 sipe_get_no_sip_uri(uri),
468 &is_group_access);
469 const char *access_level = sipe_ocs2007_access_level_name(container_id);
470 access_text = is_group_access ?
471 g_strdup(access_level) :
472 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
473 access_level);
477 if (is_online) {
478 const gchar *status_str = activity ? activity : status_name;
480 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
482 if (is_online && !is_empty(calendar)) {
483 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
485 g_free(calendar);
486 if (!is_empty(meeting_location)) {
487 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
488 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
490 if (!is_empty(meeting_subject)) {
491 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
492 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
494 if (note) {
495 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
496 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
497 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
498 note_italics);
499 g_free(note_italics);
501 if (access_text) {
502 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
503 g_free(access_text);
507 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
508 const char *uri,
509 sipe_buddy_info_fields propkey,
510 char *property_value)
512 GSList *buddies, *entry;
514 if (property_value)
515 property_value = g_strstrip(property_value);
517 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
518 while (entry) {
519 gchar *prop_str;
520 sipe_backend_buddy p_buddy = entry->data;
522 /* for Display Name */
523 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
524 gchar *alias;
525 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
526 if (property_value && sipe_is_bad_alias(uri, alias)) {
527 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
528 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
530 g_free(alias);
532 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
533 if (!is_empty(property_value) &&
534 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
536 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
537 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
539 g_free(alias);
541 /* for other properties */
542 else {
543 if (!is_empty(property_value)) {
544 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
545 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
546 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
548 g_free(prop_str);
552 entry = entry->next;
554 g_slist_free(buddies);
558 struct ms_dlx_data;
559 struct ms_dlx_data {
560 GSList *search_rows;
561 gchar *other;
562 guint max_returns;
563 sipe_svc_callback *callback;
564 struct sipe_svc_session *session;
565 gchar *wsse_security;
566 struct sipe_backend_search_token *token;
567 /* must call ms_dlx_free() */
568 void (*failed_callback)(struct sipe_core_private *sipe_private,
569 struct ms_dlx_data *mdd);
572 static void ms_dlx_free(struct ms_dlx_data *mdd)
574 sipe_utils_slist_free_full(mdd->search_rows, g_free);
575 sipe_svc_session_close(mdd->session);
576 g_free(mdd->other);
577 g_free(mdd->wsse_security);
578 g_free(mdd);
581 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
582 #define DLX_SEARCH_ITEM \
583 "<AbEntryRequest.ChangeSearchQuery>" \
584 " <SearchOn>%s</SearchOn>" \
585 " <Value>%s</Value>" \
586 "</AbEntryRequest.ChangeSearchQuery>"
588 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
589 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
590 guint i = 0;
591 gchar *query = NULL;
593 while (query_rows) {
594 gchar *attr;
595 gchar *value;
597 attr = query_rows->data;
598 query_rows = g_slist_next(query_rows);
599 value = query_rows->data;
600 query_rows = g_slist_next(query_rows);
602 if (!attr || !value)
603 break;
605 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
606 attr, value);
608 attrs[i] = NULL;
610 if (i) {
611 query = g_strjoinv(NULL, attrs);
612 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
613 query ? query : "");
616 g_strfreev(attrs);
618 return query;
621 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
622 const gchar *base_uri,
623 const gchar *auth_uri,
624 const gchar *wsse_security,
625 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
626 gpointer callback_data)
628 struct ms_dlx_data *mdd = callback_data;
630 if (wsse_security) {
631 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
633 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
634 base_uri);
636 if (sipe_svc_ab_entry_request(sipe_private,
637 mdd->session,
638 auth_uri,
639 wsse_security,
640 query,
641 g_slist_length(mdd->search_rows) / 2,
642 mdd->max_returns,
643 mdd->callback,
644 mdd)) {
646 /* keep webticket security token for potential further use */
647 mdd->wsse_security = g_strdup(wsse_security);
649 /* callback data passed down the line */
650 mdd = NULL;
652 g_free(query);
654 } else {
655 /* no ticket: this will show the minmum information */
656 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
657 base_uri);
660 if (mdd)
661 mdd->failed_callback(sipe_private, mdd);
664 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
665 struct ms_dlx_data *mdd)
667 if (!sipe_webticket_request(sipe_private,
668 mdd->session,
669 sipe_private->dlx_uri,
670 "AddressBookWebTicketBearer",
671 ms_dlx_webticket,
672 mdd)) {
673 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
674 sipe_private->dlx_uri);
675 mdd->failed_callback(sipe_private, mdd);
679 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
680 struct sipe_backend_search_results *results,
681 guint match_count,
682 gboolean more)
684 gchar *secondary = g_strdup_printf(
685 dngettext(PACKAGE_NAME,
686 "Found %d contact%s:",
687 "Found %d contacts%s:", match_count),
688 match_count, more ? _(" (more matched your query)") : "");
690 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
691 results,
692 secondary,
693 more);
694 g_free(secondary);
697 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
698 const gchar *uri,
699 SIPE_UNUSED_PARAMETER const gchar *raw,
700 sipe_xml *soap_body,
701 gpointer callback_data)
703 struct ms_dlx_data *mdd = callback_data;
705 if (soap_body) {
706 const sipe_xml *node;
707 struct sipe_backend_search_results *results;
708 GHashTable *found;
710 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
711 uri);
713 /* any matches? */
714 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
715 if (!node) {
716 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
717 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
718 mdd->token,
719 _("No contacts found"));
720 ms_dlx_free(mdd);
721 return;
724 /* OK, we found something - show the results to the user */
725 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
726 mdd->token);
727 if (!results) {
728 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
729 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
730 mdd->token,
731 _("Unable to display the search results"));
732 ms_dlx_free(mdd);
733 return;
736 /* SearchAbEntryResult can contain duplicates */
737 found = g_hash_table_new_full(g_str_hash, g_str_equal,
738 g_free, NULL);
740 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
741 const sipe_xml *attrs;
742 gchar *sip_uri = NULL;
743 gchar *displayname = NULL;
744 gchar *company = NULL;
745 gchar *country = NULL;
746 gchar *email = NULL;
748 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
749 attrs;
750 attrs = sipe_xml_twin(attrs)) {
751 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
752 "Name"));
753 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
754 "Value"));
756 if (!is_empty(value)) {
757 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
758 g_free(sip_uri);
759 sip_uri = value;
760 value = NULL;
761 } else if (sipe_strcase_equal(name, "displayname")) {
762 g_free(displayname);
763 displayname = value;
764 value = NULL;
765 } else if (sipe_strcase_equal(name, "mail")) {
766 g_free(email);
767 email = value;
768 value = NULL;
769 } else if (sipe_strcase_equal(name, "company")) {
770 g_free(company);
771 company = value;
772 value = NULL;
773 } else if (sipe_strcase_equal(name, "country")) {
774 g_free(country);
775 country = value;
776 value = NULL;
780 g_free(value);
781 g_free(name);
784 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
785 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
786 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
787 results,
788 uri_parts[1],
789 displayname,
790 company,
791 country,
792 email);
793 g_strfreev(uri_parts);
795 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
796 sip_uri = NULL;
799 g_free(email);
800 g_free(country);
801 g_free(company);
802 g_free(displayname);
803 g_free(sip_uri);
806 search_contacts_finalize(sipe_private, results,
807 g_hash_table_size(found),
808 FALSE);
809 g_hash_table_destroy(found);
810 ms_dlx_free(mdd);
812 } else {
813 mdd->failed_callback(sipe_private, mdd);
817 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
818 struct sipmsg *msg,
819 struct transaction *trans)
821 struct sipe_backend_search_token *token = trans->payload->data;
822 struct sipe_backend_search_results *results;
823 sipe_xml *searchResults;
824 const sipe_xml *mrow;
825 guint match_count = 0;
826 gboolean more = FALSE;
828 /* valid response? */
829 if (msg->response != 200) {
830 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
831 msg->response);
832 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
833 token,
834 _("Contact search failed"));
835 return(FALSE);
838 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
840 /* valid XML? */
841 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
842 if (!searchResults) {
843 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
844 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
845 token,
846 _("Contact search failed"));
847 return(FALSE);
850 /* any matches? */
851 mrow = sipe_xml_child(searchResults, "Body/Array/row");
852 if (!mrow) {
853 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
854 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
855 token,
856 _("No contacts found"));
858 sipe_xml_free(searchResults);
859 return(FALSE);
862 /* OK, we found something - show the results to the user */
863 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
864 trans->payload->data);
865 if (!results) {
866 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
867 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
868 token,
869 _("Unable to display the search results"));
871 sipe_xml_free(searchResults);
872 return FALSE;
875 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
876 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
877 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
878 results,
879 uri_parts[1],
880 sipe_xml_attribute(mrow, "displayName"),
881 sipe_xml_attribute(mrow, "company"),
882 sipe_xml_attribute(mrow, "country"),
883 sipe_xml_attribute(mrow, "email"));
884 g_strfreev(uri_parts);
885 match_count++;
888 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
889 char *data = sipe_xml_data(mrow);
890 more = (g_ascii_strcasecmp(data, "true") == 0);
891 g_free(data);
894 search_contacts_finalize(sipe_private, results, match_count, more);
895 sipe_xml_free(searchResults);
897 return(TRUE);
900 static void search_soap_request(struct sipe_core_private *sipe_private,
901 struct sipe_backend_search_token *token,
902 GSList *search_rows)
904 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
905 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
907 payload->data = token;
909 sip_soap_directory_search(sipe_private,
910 100,
911 query,
912 process_search_contact_response,
913 payload);
914 g_free(query);
917 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
918 struct ms_dlx_data *mdd)
920 /* error using [MS-DLX] server, retry using Active Directory */
921 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
922 ms_dlx_free(mdd);
925 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
926 struct sipe_backend_search_token *token,
927 const gchar *given_name,
928 const gchar *surname,
929 const gchar *email,
930 const gchar *company,
931 const gchar *country)
933 GSList *query_rows = NULL;
935 #define ADD_QUERY_ROW(attr, val) \
936 if (val) { \
937 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
938 query_rows = g_slist_append(query_rows, g_strdup(val)); \
941 ADD_QUERY_ROW("givenName", given_name);
942 ADD_QUERY_ROW("sn", surname);
943 ADD_QUERY_ROW("mail", email);
944 ADD_QUERY_ROW("company", company);
945 ADD_QUERY_ROW("c", country);
947 if (query_rows) {
948 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
949 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
951 mdd->search_rows = query_rows;
952 mdd->max_returns = 100;
953 mdd->callback = search_ab_entry_response;
954 mdd->failed_callback = search_ab_entry_failed;
955 mdd->session = sipe_svc_session_start();
956 mdd->token = token;
958 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
960 } else {
961 /* no [MS-DLX] server, use Active Directory search instead */
962 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
963 sipe_utils_slist_free_full(query_rows, g_free);
965 } else
966 sipe_backend_search_failed(sipe_public,
967 token,
968 _("Invalid contact search query"));
971 static void get_info_finalize(struct sipe_core_private *sipe_private,
972 struct sipe_backend_buddy_info *info,
973 const gchar *uri,
974 const gchar *server_alias,
975 const gchar *email)
977 sipe_backend_buddy bbuddy;
978 struct sipe_buddy *sbuddy;
979 gchar *alias;
980 gchar *value;
982 if (!info) {
983 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
984 } else {
985 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
987 if (!info)
988 return;
990 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
992 if (is_empty(server_alias)) {
993 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
994 bbuddy);
995 if (value) {
996 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
997 info,
998 SIPE_BUDDY_INFO_DISPLAY_NAME,
999 value);
1001 } else {
1002 value = g_strdup(server_alias);
1005 /* present alias if it differs from server alias */
1006 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
1007 if (alias && !sipe_strequal(alias, value))
1009 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1010 info,
1011 SIPE_BUDDY_INFO_ALIAS,
1012 alias);
1014 g_free(alias);
1015 g_free(value);
1017 if (is_empty(email)) {
1018 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1019 bbuddy,
1020 SIPE_BUDDY_INFO_EMAIL);
1021 if (value) {
1022 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1023 info,
1024 SIPE_BUDDY_INFO_EMAIL,
1025 value);
1026 g_free(value);
1030 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1031 bbuddy,
1032 SIPE_BUDDY_INFO_SITE);
1033 if (value) {
1034 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1035 info,
1036 SIPE_BUDDY_INFO_SITE,
1037 value);
1038 g_free(value);
1041 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
1042 if (sbuddy && sbuddy->device_name) {
1043 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1044 info,
1045 SIPE_BUDDY_INFO_DEVICE,
1046 sbuddy->device_name);
1049 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
1053 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
1054 const gchar *uri,
1055 SIPE_UNUSED_PARAMETER const gchar *raw,
1056 sipe_xml *soap_body,
1057 gpointer callback_data)
1059 struct ms_dlx_data *mdd = callback_data;
1060 struct sipe_backend_buddy_info *info = NULL;
1061 gchar *server_alias = NULL;
1062 gchar *email = NULL;
1064 if (soap_body) {
1065 const sipe_xml *node;
1067 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1068 uri);
1070 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1072 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1073 node;
1074 node = sipe_xml_twin(node)) {
1075 gchar *name = sipe_xml_data(sipe_xml_child(node,
1076 "Name"));
1077 gchar *value = sipe_xml_data(sipe_xml_child(node,
1078 "Value"));
1079 const sipe_xml *values = sipe_xml_child(node,
1080 "Values");
1082 /* Single value entries */
1083 if (!is_empty(value)) {
1085 if (sipe_strcase_equal(name, "displayname")) {
1086 g_free(server_alias);
1087 server_alias = value;
1088 value = NULL;
1089 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1090 info,
1091 SIPE_BUDDY_INFO_DISPLAY_NAME,
1092 server_alias);
1093 } else if (sipe_strcase_equal(name, "mail")) {
1094 g_free(email);
1095 email = value;
1096 value = NULL;
1097 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1098 info,
1099 SIPE_BUDDY_INFO_EMAIL,
1100 email);
1101 } else if (sipe_strcase_equal(name, "title")) {
1102 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1103 info,
1104 SIPE_BUDDY_INFO_JOB_TITLE,
1105 value);
1106 } else if (sipe_strcase_equal(name, "company")) {
1107 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1108 info,
1109 SIPE_BUDDY_INFO_COMPANY,
1110 value);
1111 } else if (sipe_strcase_equal(name, "country")) {
1112 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1113 info,
1114 SIPE_BUDDY_INFO_COUNTRY,
1115 value);
1118 } else if (values) {
1119 gchar *first = sipe_xml_data(sipe_xml_child(values,
1120 "string"));
1122 if (sipe_strcase_equal(name, "telephonenumber")) {
1123 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1124 info,
1125 SIPE_BUDDY_INFO_WORK_PHONE,
1126 first);
1129 g_free(first);
1132 g_free(value);
1133 g_free(name);
1137 /* this will show the minmum information */
1138 get_info_finalize(sipe_private,
1139 info,
1140 mdd->other,
1141 server_alias,
1142 email);
1144 g_free(email);
1145 g_free(server_alias);
1146 ms_dlx_free(mdd);
1149 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1150 struct sipmsg *msg,
1151 struct transaction *trans)
1153 const gchar *uri = trans->payload->data;
1154 struct sipe_backend_buddy_info *info = NULL;
1155 gchar *server_alias = NULL;
1156 gchar *email = NULL;
1158 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1159 uri, sipe_private->username);
1161 if (msg->response != 200) {
1162 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1163 } else {
1164 sipe_xml *searchResults;
1165 const sipe_xml *mrow;
1167 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1168 msg->body ? msg->body : "");
1170 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1171 if (!searchResults) {
1173 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1175 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1176 const gchar *value;
1177 gchar *phone_number;
1179 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1181 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1182 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1183 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1186 * For 2007 system we will take this from ContactCard -
1187 * it has cleaner tel: URIs at least
1189 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1190 char *tel_uri = sip_to_tel_uri(phone_number);
1191 /* trims its parameters, so call first */
1192 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1193 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1194 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1195 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1196 g_free(tel_uri);
1198 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1199 uri);
1202 if (!is_empty(server_alias)) {
1203 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1204 info,
1205 SIPE_BUDDY_INFO_DISPLAY_NAME,
1206 server_alias);
1208 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1209 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1210 info,
1211 SIPE_BUDDY_INFO_JOB_TITLE,
1212 value);
1214 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1215 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1216 info,
1217 SIPE_BUDDY_INFO_OFFICE,
1218 value);
1220 if (!is_empty(phone_number)) {
1221 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1222 info,
1223 SIPE_BUDDY_INFO_WORK_PHONE,
1224 phone_number);
1226 g_free(phone_number);
1227 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1228 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1229 info,
1230 SIPE_BUDDY_INFO_COMPANY,
1231 value);
1233 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1234 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1235 info,
1236 SIPE_BUDDY_INFO_CITY,
1237 value);
1239 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1240 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1241 info,
1242 SIPE_BUDDY_INFO_STATE,
1243 value);
1245 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1246 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1247 info,
1248 SIPE_BUDDY_INFO_COUNTRY,
1249 value);
1251 if (!is_empty(email)) {
1252 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1253 info,
1254 SIPE_BUDDY_INFO_EMAIL,
1255 email);
1258 sipe_xml_free(searchResults);
1261 /* this will show the minmum information */
1262 get_info_finalize(sipe_private,
1263 info,
1264 uri,
1265 server_alias,
1266 email);
1268 g_free(server_alias);
1269 g_free(email);
1271 return TRUE;
1274 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1275 struct ms_dlx_data *mdd)
1277 /* error using [MS-DLX] server, retry using Active Directory */
1278 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1279 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1281 payload->destroy = g_free;
1282 payload->data = mdd->other;
1283 mdd->other = NULL;
1285 sip_soap_directory_search(sipe_private,
1287 query,
1288 process_get_info_response,
1289 payload);
1291 ms_dlx_free(mdd);
1292 g_free(query);
1295 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1296 const gchar *who)
1298 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1300 if (sipe_private->dlx_uri) {
1301 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1303 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1304 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1306 mdd->other = g_strdup(who);
1307 mdd->max_returns = 1;
1308 mdd->callback = get_info_ab_entry_response;
1309 mdd->failed_callback = get_info_ab_entry_failed;
1310 mdd->session = sipe_svc_session_start();
1312 ms_dlx_webticket_request(sipe_private, mdd);
1314 } else {
1315 /* no [MS-DLX] server, use Active Directory search instead */
1316 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1317 "msRTCSIP-PrimaryUserAddress",
1318 who);
1319 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1321 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1322 row ? row : "");
1324 payload->destroy = g_free;
1325 payload->data = g_strdup(who);
1327 sip_soap_directory_search(sipe_private,
1329 row,
1330 process_get_info_response,
1331 payload);
1332 g_free(row);
1336 static void photo_response_data_free(struct photo_response_data *data)
1338 g_free(data->who);
1339 g_free(data->photo_hash);
1340 if (data->request) {
1341 sipe_http_request_cancel(data->request);
1343 g_free(data);
1346 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1347 guint status,
1348 GSList *headers,
1349 const char *body,
1350 gpointer data)
1352 struct photo_response_data *rdata = (struct photo_response_data *) data;
1354 rdata->request = NULL;
1356 if (status == SIPE_HTTP_STATUS_OK) {
1357 const gchar *len_str = sipe_utils_nameval_find(headers,
1358 "Content-Length");
1359 if (len_str) {
1360 gsize photo_size = atoi(len_str);
1361 gpointer photo = g_new(char, photo_size);
1363 if (photo) {
1364 memcpy(photo, body, photo_size);
1366 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1367 rdata->who,
1368 photo,
1369 photo_size,
1370 rdata->photo_hash);
1375 sipe_private->buddies->pending_photo_requests =
1376 g_slist_remove(sipe_private->buddies->pending_photo_requests, rdata);
1378 photo_response_data_free(rdata);
1381 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1383 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1384 gchar *wsse_security_base64;
1385 gchar *x_ms_webticket_header;
1387 if (!assertion) {
1388 return NULL;
1391 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1392 strlen(assertion));
1393 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1394 wsse_security_base64);
1396 g_free(assertion);
1397 g_free(wsse_security_base64);
1399 return x_ms_webticket_header;
1402 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1403 const gchar *uri,
1404 SIPE_UNUSED_PARAMETER const gchar *raw,
1405 sipe_xml *soap_body,
1406 gpointer callback_data)
1408 struct ms_dlx_data *mdd = callback_data;
1409 gchar *photo_rel_path = NULL;
1410 gchar *photo_hash = NULL;
1411 const gchar *photo_hash_old =
1412 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1414 if (soap_body) {
1415 const sipe_xml *node;
1417 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1418 uri);
1420 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1421 node;
1422 node = sipe_xml_twin(node)) {
1423 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1424 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1426 if (!is_empty(value)) {
1427 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1428 g_free(photo_rel_path);
1429 photo_rel_path = value;
1430 value = NULL;
1431 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1432 g_free(photo_hash);
1433 photo_hash = value;
1434 value = NULL;
1438 g_free(value);
1439 g_free(name);
1443 if (sipe_private->addressbook_uri && photo_rel_path &&
1444 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1445 gchar *photo_url = g_strdup_printf("%s/%s",
1446 sipe_private->addressbook_uri, photo_rel_path);
1447 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1449 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1450 data->who = g_strdup(mdd->other);
1451 data->photo_hash = photo_hash;
1452 photo_hash = NULL;
1454 data->request = sipe_http_request_get(sipe_private,
1455 photo_url,
1456 x_ms_webticket_header,
1457 process_buddy_photo_response,
1458 data);
1460 if (data->request) {
1461 sipe_private->buddies->pending_photo_requests =
1462 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1463 sipe_http_request_ready(data->request);
1464 } else {
1465 photo_response_data_free(data);
1468 g_free(x_ms_webticket_header);
1469 g_free(photo_url);
1472 g_free(photo_rel_path);
1473 g_free(photo_hash);
1474 ms_dlx_free(mdd);
1477 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1478 struct ms_dlx_data *mdd)
1480 ms_dlx_free(mdd);
1483 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1484 const gchar *uri)
1486 if (sipe_backend_uses_photo()) {
1488 /* Lync 2013 or newer: use UCS */
1489 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013)) {
1491 sipe_ucs_get_photo(sipe_private, uri);
1493 /* Lync 2010: use [MS-DLX] */
1494 } else if (sipe_private->dlx_uri &&
1495 sipe_private->addressbook_uri) {
1496 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1498 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1499 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1501 mdd->other = g_strdup(uri);
1502 mdd->max_returns = 1;
1503 mdd->callback = get_photo_ab_entry_response;
1504 mdd->failed_callback = get_photo_ab_entry_failed;
1505 mdd->session = sipe_svc_session_start();
1507 ms_dlx_webticket_request(sipe_private, mdd);
1512 static void buddy_refresh_photos_cb(gpointer uri,
1513 SIPE_UNUSED_PARAMETER gpointer value,
1514 gpointer sipe_private)
1516 buddy_fetch_photo(sipe_private, uri);
1519 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1521 g_hash_table_foreach(sipe_private->buddies->uri,
1522 buddy_refresh_photos_cb,
1523 sipe_private);
1526 /* Buddy menu callbacks*/
1528 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1529 const gchar *who)
1531 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1533 /* 2007+ conference */
1534 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1535 sipe_conf_add(sipe_private, who);
1537 /* 2005- multiparty chat */
1538 } else {
1539 gchar *self = sip_uri_self(sipe_private);
1540 struct sip_session *session;
1542 session = sipe_session_add_chat(sipe_private,
1543 NULL,
1544 TRUE,
1545 self);
1546 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1547 session->chat_session,
1548 session->chat_session->title,
1549 self);
1550 g_free(self);
1552 sipe_im_invite(sipe_private, session, who,
1553 NULL, NULL, NULL, FALSE);
1557 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1558 const gchar *who)
1560 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1561 who,
1562 NULL);
1563 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1564 buddy,
1565 SIPE_BUDDY_INFO_EMAIL);
1567 if (email) {
1568 gchar *command_line = g_strdup_printf(
1569 #ifdef _WIN32
1570 "cmd /c start"
1571 #else
1572 "xdg-email"
1573 #endif
1574 " mailto:%s", email);
1575 g_free(email);
1577 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1578 command_line);
1579 g_spawn_command_line_async(command_line, NULL);
1580 g_free(command_line);
1582 } else {
1583 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1584 who);
1588 /* Buddy menu */
1590 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1591 struct sipe_backend_buddy_menu *menu,
1592 sipe_backend_buddy buddy,
1593 sipe_buddy_info_fields id_phone,
1594 sipe_buddy_info_fields id_display,
1595 const gchar *type)
1597 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1598 buddy,
1599 id_phone);
1600 if (phone) {
1601 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1602 buddy,
1603 id_display);
1604 gchar *tmp = NULL;
1605 gchar *label = g_strdup_printf("%s %s",
1606 type,
1607 display ? display :
1608 (tmp = sip_tel_uri_denormalize(phone)));
1609 menu = sipe_backend_buddy_menu_add(sipe_public,
1610 menu,
1611 label,
1612 SIPE_BUDDY_MENU_MAKE_CALL,
1613 phone);
1614 g_free(tmp);
1615 g_free(label);
1616 g_free(display);
1617 g_free(phone);
1620 return(menu);
1623 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1624 const gchar *buddy_name,
1625 struct sipe_backend_buddy_menu *menu)
1627 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1628 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1629 buddy_name,
1630 NULL);
1631 gchar *self = sip_uri_self(sipe_private);
1633 SIPE_SESSION_FOREACH {
1634 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1636 struct sipe_chat_session *chat_session = session->chat_session;
1637 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1639 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1641 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1643 if (is_conf &&
1644 /* Not conf OP */
1645 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1646 /* We are a conf OP */
1647 conf_op) {
1648 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1649 chat_session->title);
1650 menu = sipe_backend_buddy_menu_add(sipe_public,
1651 menu,
1652 label,
1653 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1654 chat_session);
1655 g_free(label);
1658 if (is_conf &&
1659 /* We are a conf OP */
1660 conf_op) {
1661 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1662 chat_session->title);
1663 menu = sipe_backend_buddy_menu_add(sipe_public,
1664 menu,
1665 label,
1666 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1667 chat_session);
1668 g_free(label);
1671 else
1673 if (!is_conf ||
1674 (is_conf && !session->locked)) {
1675 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1676 chat_session->title);
1677 menu = sipe_backend_buddy_menu_add(sipe_public,
1678 menu,
1679 label,
1680 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1681 chat_session);
1682 g_free(label);
1686 } SIPE_SESSION_FOREACH_END;
1687 g_free(self);
1689 menu = sipe_backend_buddy_menu_add(sipe_public,
1690 menu,
1691 _("New chat"),
1692 SIPE_BUDDY_MENU_NEW_CHAT,
1693 NULL);
1695 /* add buddy's phone numbers if we have call control */
1696 if (sip_csta_is_idle(sipe_private)) {
1698 /* work phone */
1699 menu = buddy_menu_phone(sipe_public,
1700 menu,
1701 buddy,
1702 SIPE_BUDDY_INFO_WORK_PHONE,
1703 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1704 _("Work"));
1705 /* mobile phone */
1706 menu = buddy_menu_phone(sipe_public,
1707 menu,
1708 buddy,
1709 SIPE_BUDDY_INFO_MOBILE_PHONE,
1710 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1711 _("Mobile"));
1713 /* home phone */
1714 menu = buddy_menu_phone(sipe_public,
1715 menu,
1716 buddy,
1717 SIPE_BUDDY_INFO_HOME_PHONE,
1718 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1719 _("Home"));
1721 /* other phone */
1722 menu = buddy_menu_phone(sipe_public,
1723 menu,
1724 buddy,
1725 SIPE_BUDDY_INFO_OTHER_PHONE,
1726 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1727 _("Other"));
1729 /* custom1 phone */
1730 menu = buddy_menu_phone(sipe_public,
1731 menu,
1732 buddy,
1733 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1734 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1735 _("Custom1"));
1739 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1740 buddy,
1741 SIPE_BUDDY_INFO_EMAIL);
1742 if (email) {
1743 menu = sipe_backend_buddy_menu_add(sipe_public,
1744 menu,
1745 _("Send email..."),
1746 SIPE_BUDDY_MENU_SEND_EMAIL,
1747 NULL);
1748 g_free(email);
1752 /* access level control */
1753 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1754 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1755 menu,
1756 _("Access level"),
1757 sipe_ocs2007_access_control_menu(sipe_private,
1758 buddy_name));
1760 return(menu);
1763 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
1765 return(g_hash_table_size(sipe_private->buddies->uri));
1768 static guint sipe_ht_hash_nick(const char *nick)
1770 char *lc = g_utf8_strdown(nick, -1);
1771 guint bucket = g_str_hash(lc);
1772 g_free(lc);
1774 return bucket;
1777 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
1779 char *nick1_norm = NULL;
1780 char *nick2_norm = NULL;
1781 gboolean equal;
1783 if (nick1 == NULL && nick2 == NULL) return TRUE;
1784 if (nick1 == NULL || nick2 == NULL ||
1785 !g_utf8_validate(nick1, -1, NULL) ||
1786 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
1788 nick1_norm = g_utf8_casefold(nick1, -1);
1789 nick2_norm = g_utf8_casefold(nick2, -1);
1790 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
1791 g_free(nick2_norm);
1792 g_free(nick1_norm);
1794 return equal;
1797 void sipe_buddy_init(struct sipe_core_private *sipe_private)
1799 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
1800 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
1801 (GEqualFunc) sipe_ht_equals_nick);
1802 buddies->exchange_key = g_hash_table_new(g_str_hash,
1803 g_str_equal);
1804 sipe_private->buddies = buddies;
1808 Local Variables:
1809 mode: c
1810 c-file-style: "bsd"
1811 indent-tabs-mode: t
1812 tab-width: 8
1813 End: