buddy: add interface to insert group
[siplcs.git] / src / core / sipe-buddy.c
blob4d13a3faf7391d0613e6e09c512cbdfc43b667bc
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,
82 const gchar *change_key)
84 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
85 gchar *normalized_uri = g_ascii_strdown(uri, -1);
86 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
87 normalized_uri);
89 if (!buddy) {
90 struct sipe_buddies *buddies = sipe_private->buddies;
92 buddy = g_new0(struct sipe_buddy, 1);
93 buddy->name = normalized_uri;
94 g_hash_table_insert(buddies->uri,
95 buddy->name,
96 buddy);
98 if (exchange_key) {
99 buddy->exchange_key = g_strdup(exchange_key);
100 g_hash_table_insert(buddies->exchange_key,
101 buddy->exchange_key,
102 buddy);
104 if (change_key)
105 buddy->change_key = g_strdup(change_key);
108 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri);
110 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
111 buddy->just_added = TRUE;
112 sipe_subscribe_presence_single_cb(sipe_private,
113 buddy->name);
116 buddy_fetch_photo(sipe_private, normalized_uri);
118 normalized_uri = NULL; /* buddy takes ownership */
119 } else {
120 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri);
121 buddy->is_obsolete = FALSE;
123 g_free(normalized_uri);
125 return(buddy);
128 static gboolean is_buddy_in_group(struct sipe_buddy *buddy,
129 const gchar *name)
131 gboolean in_group = FALSE;
133 if (buddy) {
134 GSList *entry2 = buddy->groups;
136 while (entry2) {
137 struct sipe_group *group = entry2->data;
138 if (sipe_strequal(group->name, name)) {
139 in_group = TRUE;
140 break;
142 entry2 = entry2->next;
146 return(in_group);
149 void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private,
150 struct sipe_buddy *buddy,
151 struct sipe_group *group,
152 const gchar *alias)
154 const gchar *uri = buddy->name;
155 const gchar *group_name = group->name;
156 sipe_backend_buddy bb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
157 uri,
158 group_name);
160 if (!bb) {
161 bb = sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
162 uri,
163 alias,
164 group_name);
165 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
166 uri, alias ? alias : "<NONE>");
170 if (!is_empty(alias)) {
171 gchar *old_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
172 bb);
174 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri),
175 old_alias)) {
176 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
178 alias);
179 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
180 uri, old_alias, alias);
182 g_free(old_alias);
185 if (!is_buddy_in_group(buddy, group_name)) {
186 sipe_buddy_insert_group(buddy, group);
187 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
188 uri, group_name);
192 static gint buddy_group_compare(gconstpointer a, gconstpointer b)
194 return(((const struct sipe_group *)a)->id -
195 ((const struct sipe_group *)b)->id);
198 void sipe_buddy_insert_group(struct sipe_buddy *buddy,
199 struct sipe_group *group)
201 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
202 group,
203 buddy_group_compare,
204 NULL);
207 void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private)
209 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
210 NULL,
211 NULL);
212 GSList *entry = buddies;
214 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
215 g_slist_length(buddies));
216 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
217 sipe_buddy_count(sipe_private));
218 while (entry) {
219 sipe_backend_buddy bb = entry->data;
220 gchar *bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC,
221 bb);
222 gchar *gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC,
223 bb);
224 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
225 bname);
227 if (!is_buddy_in_group(buddy, gname)) {
228 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",
229 bname, gname);
230 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, bb);
233 g_free(gname);
234 g_free(bname);
236 entry = entry->next;
239 g_slist_free(buddies);
242 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
243 const gchar *uri)
245 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
248 struct sipe_buddy *sipe_buddy_find_by_exchange_key(struct sipe_core_private *sipe_private,
249 const gchar *exchange_key)
251 return(g_hash_table_lookup(sipe_private->buddies->exchange_key,
252 exchange_key));
255 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
256 GHFunc callback,
257 gpointer callback_data)
259 g_hash_table_foreach(sipe_private->buddies->uri,
260 callback,
261 callback_data);
264 static void buddy_free(struct sipe_buddy *buddy)
266 #ifndef _WIN32
268 * We are calling g_hash_table_foreach_steal(). That means that no
269 * key/value deallocation functions are called. Therefore the glib
270 * hash code does not touch the key (buddy->name) or value (buddy)
271 * of the to-be-deleted hash node at all. It follows that we
273 * - MUST free the memory for the key ourselves and
274 * - ARE allowed to do it in this function
276 * Conclusion: glib must be broken on the Windows platform if sipe
277 * crashes with SIGTRAP when closing. You'll have to live
278 * with the memory leak until this is fixed.
280 g_free(buddy->name);
281 #endif
282 g_free(buddy->exchange_key);
283 g_free(buddy->change_key);
284 g_free(buddy->activity);
285 g_free(buddy->meeting_subject);
286 g_free(buddy->meeting_location);
287 g_free(buddy->note);
289 g_free(buddy->cal_start_time);
290 g_free(buddy->cal_free_busy_base64);
291 g_free(buddy->cal_free_busy);
292 g_free(buddy->last_non_cal_activity);
294 sipe_cal_free_working_hours(buddy->cal_working_hours);
296 g_free(buddy->device_name);
297 g_slist_free(buddy->groups);
298 g_free(buddy);
301 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
302 gpointer buddy,
303 SIPE_UNUSED_PARAMETER gpointer user_data)
305 buddy_free(buddy);
306 /* We must return TRUE as the key/value have already been deleted */
307 return(TRUE);
310 void sipe_buddy_free(struct sipe_core_private *sipe_private)
312 struct sipe_buddies *buddies = sipe_private->buddies;
314 g_hash_table_foreach_steal(buddies->uri,
315 buddy_free_cb,
316 NULL);
318 /* core is being deallocated, remove all its pending photo requests */
319 while (buddies->pending_photo_requests) {
320 struct photo_response_data *data =
321 buddies->pending_photo_requests->data;
322 buddies->pending_photo_requests =
323 g_slist_remove(buddies->pending_photo_requests, data);
324 photo_response_data_free(data);
327 g_hash_table_destroy(buddies->uri);
328 g_hash_table_destroy(buddies->exchange_key);
329 g_free(buddies);
330 sipe_private->buddies = NULL;
333 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
334 gpointer value,
335 SIPE_UNUSED_PARAMETER gpointer user_data)
337 ((struct sipe_buddy *) value)->is_obsolete = TRUE;
340 void sipe_buddy_update_start(struct sipe_core_private *sipe_private)
342 g_hash_table_foreach(sipe_private->buddies->uri,
343 buddy_set_obsolete_flag,
344 NULL);
347 static gboolean buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
348 gpointer value,
349 gpointer user_data)
351 struct sipe_core_private *sipe_private = user_data;
352 struct sipe_buddy *buddy = value;
354 if (buddy->is_obsolete) {
355 /* all backend buddies in different groups */
356 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
357 buddy->name,
358 NULL);
359 GSList *entry = buddies;
361 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
362 g_slist_length(buddies),
363 buddy->name);
365 while (entry) {
366 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
367 entry->data);
368 entry = entry->next;
370 g_slist_free(buddies);
372 buddy_free(buddy);
373 /* return TRUE as the key/value have already been deleted */
374 return(TRUE);
375 } else
376 return(FALSE);
379 void sipe_buddy_update_finish(struct sipe_core_private *sipe_private)
381 g_hash_table_foreach_remove(sipe_private->buddies->uri,
382 buddy_check_obsolete_flag,
383 sipe_private);
386 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
387 const gchar *uri,
388 guint activity,
389 const gchar *status_text)
391 struct sipe_buddy *sbuddy;
392 const char *activity_str;
394 if (!sipe_public) return NULL; /* happens on pidgin exit */
396 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
397 if (!sbuddy) return NULL;
399 activity_str = sbuddy->activity ? sbuddy->activity :
400 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
401 status_text : NULL;
403 if (activity_str && sbuddy->note) {
404 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
405 } else if (activity_str) {
406 return g_strdup(activity_str);
407 } else if (sbuddy->note) {
408 return g_strdup_printf("<i>%s</i>", sbuddy->note);
409 } else {
410 return NULL;
414 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
415 const gchar *with)
417 sipe_backend_buddy pbuddy;
418 gchar *alias = NULL;
419 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
420 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
422 return alias;
425 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
426 const gchar *who,
427 const gchar *old_group_name,
428 const gchar *new_group_name)
430 struct sipe_buddy * buddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE,
431 who);
432 struct sipe_group * old_group = NULL;
433 struct sipe_group * new_group;
435 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
436 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
438 if(!buddy) { // buddy not in roaming list
439 return;
442 if (old_group_name) {
443 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
445 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
447 if (old_group) {
448 buddy->groups = g_slist_remove(buddy->groups, old_group);
449 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
452 if (!new_group) {
453 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
454 } else {
455 sipe_buddy_insert_group(buddy, new_group);
456 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
460 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
461 const gchar *uri,
462 const gchar *group_name)
464 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
466 if (!sipe_buddy_find_by_uri(sipe_private, uri))
467 sipe_buddy_add(sipe_private,
468 uri,
469 NULL,
470 NULL);
471 else
472 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
473 uri);
475 sipe_core_buddy_group(sipe_public,
476 uri,
477 NULL,
478 group_name);
481 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
482 struct sipe_buddy *buddy)
484 struct sipe_buddies *buddies = sipe_private->buddies;
485 gchar *action_name = sipe_utils_presence_key(buddy->name);
486 sipe_schedule_cancel(sipe_private, action_name);
487 g_free(action_name);
489 g_hash_table_remove(buddies->uri, buddy->name);
490 if (buddy->exchange_key)
491 g_hash_table_remove(buddies->exchange_key,
492 buddy->exchange_key);
494 buddy_free(buddy);
498 * Unassociates buddy from group first.
499 * Then see if no groups left, removes buddy completely.
500 * Otherwise updates buddy groups on server.
502 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
503 const gchar *uri,
504 const gchar *group_name)
506 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
507 struct sipe_buddy *b = sipe_buddy_find_by_uri(sipe_private,
508 uri);
509 struct sipe_group *g = NULL;
511 if (!b) return;
513 if (group_name) {
514 g = sipe_group_find_by_name(sipe_private, group_name);
515 if (g) {
516 b->groups = g_slist_remove(b->groups, g);
517 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
518 uri, g->name);
522 if (g_slist_length(b->groups) < 1) {
524 if (sipe_ucs_is_migrated(sipe_private)) {
525 sipe_ucs_group_remove_buddy(sipe_private,
528 } else {
529 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
530 b->name);
531 sip_soap_request(sipe_private,
532 "deleteContact",
533 request);
534 g_free(request);
537 sipe_buddy_remove(sipe_private, b);
538 } else {
539 if (sipe_ucs_is_migrated(sipe_private)) {
540 sipe_ucs_group_remove_buddy(sipe_private,
543 } else
544 /* updates groups on server */
545 sipe_group_update_buddy(sipe_private, b);
549 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
550 const gchar *uri,
551 guint activity)
553 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
554 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
555 uri);
557 if (!sbuddy) return;
559 /* Check if on 2005 system contact's calendar,
560 * then set/preserve it.
562 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
563 sipe_backend_buddy_set_status(sipe_public, uri, activity);
564 } else {
565 sipe_ocs2005_apply_calendar_status(sipe_private,
566 sbuddy,
567 sipe_status_activity_to_token(activity));
571 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
572 const gchar *uri,
573 const gchar *status_name,
574 gboolean is_online,
575 struct sipe_backend_buddy_tooltip *tooltip)
577 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
578 gchar *note = NULL;
579 gboolean is_oof_note = FALSE;
580 const gchar *activity = NULL;
581 gchar *calendar = NULL;
582 const gchar *meeting_subject = NULL;
583 const gchar *meeting_location = NULL;
584 gchar *access_text = NULL;
586 #define SIPE_ADD_BUDDY_INFO(l, t) \
588 gchar *tmp = g_markup_escape_text((t), -1); \
589 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
590 g_free(tmp); \
592 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
593 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
595 if (sipe_public) { /* happens on pidgin exit */
596 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
597 uri);
598 if (sbuddy) {
599 note = sbuddy->note;
600 is_oof_note = sbuddy->is_oof_note;
601 activity = sbuddy->activity;
602 calendar = sipe_cal_get_description(sbuddy);
603 meeting_subject = sbuddy->meeting_subject;
604 meeting_location = sbuddy->meeting_location;
606 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
607 gboolean is_group_access = FALSE;
608 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
609 "user",
610 sipe_get_no_sip_uri(uri),
611 &is_group_access);
612 const char *access_level = sipe_ocs2007_access_level_name(container_id);
613 access_text = is_group_access ?
614 g_strdup(access_level) :
615 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
616 access_level);
620 if (is_online) {
621 const gchar *status_str = activity ? activity : status_name;
623 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
625 if (is_online && !is_empty(calendar)) {
626 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
628 g_free(calendar);
629 if (!is_empty(meeting_location)) {
630 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
631 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
633 if (!is_empty(meeting_subject)) {
634 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
635 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
637 if (note) {
638 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
639 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
640 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
641 note_italics);
642 g_free(note_italics);
644 if (access_text) {
645 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
646 g_free(access_text);
650 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
651 const char *uri,
652 sipe_buddy_info_fields propkey,
653 char *property_value)
655 GSList *buddies, *entry;
657 if (property_value)
658 property_value = g_strstrip(property_value);
660 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
661 while (entry) {
662 gchar *prop_str;
663 sipe_backend_buddy p_buddy = entry->data;
665 /* for Display Name */
666 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
667 gchar *alias;
668 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
669 if (property_value && sipe_is_bad_alias(uri, alias)) {
670 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
671 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
673 g_free(alias);
675 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
676 if (!is_empty(property_value) &&
677 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
679 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
680 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
682 g_free(alias);
684 /* for other properties */
685 else {
686 if (!is_empty(property_value)) {
687 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
688 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
689 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
691 g_free(prop_str);
695 entry = entry->next;
697 g_slist_free(buddies);
701 struct ms_dlx_data;
702 struct ms_dlx_data {
703 GSList *search_rows;
704 gchar *other;
705 guint max_returns;
706 sipe_svc_callback *callback;
707 struct sipe_svc_session *session;
708 gchar *wsse_security;
709 struct sipe_backend_search_token *token;
710 /* must call ms_dlx_free() */
711 void (*failed_callback)(struct sipe_core_private *sipe_private,
712 struct ms_dlx_data *mdd);
715 static void ms_dlx_free(struct ms_dlx_data *mdd)
717 sipe_utils_slist_free_full(mdd->search_rows, g_free);
718 sipe_svc_session_close(mdd->session);
719 g_free(mdd->other);
720 g_free(mdd->wsse_security);
721 g_free(mdd);
724 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
725 #define DLX_SEARCH_ITEM \
726 "<AbEntryRequest.ChangeSearchQuery>" \
727 " <SearchOn>%s</SearchOn>" \
728 " <Value>%s</Value>" \
729 "</AbEntryRequest.ChangeSearchQuery>"
731 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
732 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
733 guint i = 0;
734 gchar *query = NULL;
736 while (query_rows) {
737 gchar *attr;
738 gchar *value;
740 attr = query_rows->data;
741 query_rows = g_slist_next(query_rows);
742 value = query_rows->data;
743 query_rows = g_slist_next(query_rows);
745 if (!attr || !value)
746 break;
748 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
749 attr, value);
751 attrs[i] = NULL;
753 if (i) {
754 query = g_strjoinv(NULL, attrs);
755 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
756 query ? query : "");
759 g_strfreev(attrs);
761 return query;
764 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
765 const gchar *base_uri,
766 const gchar *auth_uri,
767 const gchar *wsse_security,
768 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
769 gpointer callback_data)
771 struct ms_dlx_data *mdd = callback_data;
773 if (wsse_security) {
774 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
776 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
777 base_uri);
779 if (sipe_svc_ab_entry_request(sipe_private,
780 mdd->session,
781 auth_uri,
782 wsse_security,
783 query,
784 g_slist_length(mdd->search_rows) / 2,
785 mdd->max_returns,
786 mdd->callback,
787 mdd)) {
789 /* keep webticket security token for potential further use */
790 mdd->wsse_security = g_strdup(wsse_security);
792 /* callback data passed down the line */
793 mdd = NULL;
795 g_free(query);
797 } else {
798 /* no ticket: this will show the minmum information */
799 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
800 base_uri);
803 if (mdd)
804 mdd->failed_callback(sipe_private, mdd);
807 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
808 struct ms_dlx_data *mdd)
810 if (!sipe_webticket_request(sipe_private,
811 mdd->session,
812 sipe_private->dlx_uri,
813 "AddressBookWebTicketBearer",
814 ms_dlx_webticket,
815 mdd)) {
816 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
817 sipe_private->dlx_uri);
818 mdd->failed_callback(sipe_private, mdd);
822 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
823 struct sipe_backend_search_results *results,
824 guint match_count,
825 gboolean more)
827 gchar *secondary = g_strdup_printf(
828 dngettext(PACKAGE_NAME,
829 "Found %d contact%s:",
830 "Found %d contacts%s:", match_count),
831 match_count, more ? _(" (more matched your query)") : "");
833 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
834 results,
835 secondary,
836 more);
837 g_free(secondary);
840 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
841 const gchar *uri,
842 SIPE_UNUSED_PARAMETER const gchar *raw,
843 sipe_xml *soap_body,
844 gpointer callback_data)
846 struct ms_dlx_data *mdd = callback_data;
848 if (soap_body) {
849 const sipe_xml *node;
850 struct sipe_backend_search_results *results;
851 GHashTable *found;
853 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
854 uri);
856 /* any matches? */
857 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
858 if (!node) {
859 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
860 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
861 mdd->token,
862 _("No contacts found"));
863 ms_dlx_free(mdd);
864 return;
867 /* OK, we found something - show the results to the user */
868 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
869 mdd->token);
870 if (!results) {
871 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
872 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
873 mdd->token,
874 _("Unable to display the search results"));
875 ms_dlx_free(mdd);
876 return;
879 /* SearchAbEntryResult can contain duplicates */
880 found = g_hash_table_new_full(g_str_hash, g_str_equal,
881 g_free, NULL);
883 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
884 const sipe_xml *attrs;
885 gchar *sip_uri = NULL;
886 gchar *displayname = NULL;
887 gchar *company = NULL;
888 gchar *country = NULL;
889 gchar *email = NULL;
891 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
892 attrs;
893 attrs = sipe_xml_twin(attrs)) {
894 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
895 "Name"));
896 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
897 "Value"));
899 if (!is_empty(value)) {
900 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
901 g_free(sip_uri);
902 sip_uri = value;
903 value = NULL;
904 } else if (sipe_strcase_equal(name, "displayname")) {
905 g_free(displayname);
906 displayname = value;
907 value = NULL;
908 } else if (sipe_strcase_equal(name, "mail")) {
909 g_free(email);
910 email = value;
911 value = NULL;
912 } else if (sipe_strcase_equal(name, "company")) {
913 g_free(company);
914 company = value;
915 value = NULL;
916 } else if (sipe_strcase_equal(name, "country")) {
917 g_free(country);
918 country = value;
919 value = NULL;
923 g_free(value);
924 g_free(name);
927 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
928 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
929 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
930 results,
931 uri_parts[1],
932 displayname,
933 company,
934 country,
935 email);
936 g_strfreev(uri_parts);
938 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
939 sip_uri = NULL;
942 g_free(email);
943 g_free(country);
944 g_free(company);
945 g_free(displayname);
946 g_free(sip_uri);
949 search_contacts_finalize(sipe_private, results,
950 g_hash_table_size(found),
951 FALSE);
952 g_hash_table_destroy(found);
953 ms_dlx_free(mdd);
955 } else {
956 mdd->failed_callback(sipe_private, mdd);
960 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
961 struct sipmsg *msg,
962 struct transaction *trans)
964 struct sipe_backend_search_token *token = trans->payload->data;
965 struct sipe_backend_search_results *results;
966 sipe_xml *searchResults;
967 const sipe_xml *mrow;
968 guint match_count = 0;
969 gboolean more = FALSE;
971 /* valid response? */
972 if (msg->response != 200) {
973 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
974 msg->response);
975 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
976 token,
977 _("Contact search failed"));
978 return(FALSE);
981 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
983 /* valid XML? */
984 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
985 if (!searchResults) {
986 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
987 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
988 token,
989 _("Contact search failed"));
990 return(FALSE);
993 /* any matches? */
994 mrow = sipe_xml_child(searchResults, "Body/Array/row");
995 if (!mrow) {
996 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
997 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
998 token,
999 _("No contacts found"));
1001 sipe_xml_free(searchResults);
1002 return(FALSE);
1005 /* OK, we found something - show the results to the user */
1006 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
1007 trans->payload->data);
1008 if (!results) {
1009 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1010 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1011 token,
1012 _("Unable to display the search results"));
1014 sipe_xml_free(searchResults);
1015 return FALSE;
1018 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
1019 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
1020 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
1021 results,
1022 uri_parts[1],
1023 sipe_xml_attribute(mrow, "displayName"),
1024 sipe_xml_attribute(mrow, "company"),
1025 sipe_xml_attribute(mrow, "country"),
1026 sipe_xml_attribute(mrow, "email"));
1027 g_strfreev(uri_parts);
1028 match_count++;
1031 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
1032 char *data = sipe_xml_data(mrow);
1033 more = (g_ascii_strcasecmp(data, "true") == 0);
1034 g_free(data);
1037 search_contacts_finalize(sipe_private, results, match_count, more);
1038 sipe_xml_free(searchResults);
1040 return(TRUE);
1043 static void search_soap_request(struct sipe_core_private *sipe_private,
1044 struct sipe_backend_search_token *token,
1045 GSList *search_rows)
1047 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
1048 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1050 payload->data = token;
1052 sip_soap_directory_search(sipe_private,
1053 100,
1054 query,
1055 process_search_contact_response,
1056 payload);
1057 g_free(query);
1060 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
1061 struct ms_dlx_data *mdd)
1063 /* error using [MS-DLX] server, retry using Active Directory */
1064 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
1065 ms_dlx_free(mdd);
1068 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
1069 struct sipe_backend_search_token *token,
1070 const gchar *given_name,
1071 const gchar *surname,
1072 const gchar *email,
1073 const gchar *company,
1074 const gchar *country)
1076 GSList *query_rows = NULL;
1078 #define ADD_QUERY_ROW(attr, val) \
1079 if (val) { \
1080 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1081 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1084 ADD_QUERY_ROW("givenName", given_name);
1085 ADD_QUERY_ROW("sn", surname);
1086 ADD_QUERY_ROW("mail", email);
1087 ADD_QUERY_ROW("company", company);
1088 ADD_QUERY_ROW("c", country);
1090 if (query_rows) {
1091 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
1092 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1094 mdd->search_rows = query_rows;
1095 mdd->max_returns = 100;
1096 mdd->callback = search_ab_entry_response;
1097 mdd->failed_callback = search_ab_entry_failed;
1098 mdd->session = sipe_svc_session_start();
1099 mdd->token = token;
1101 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
1103 } else {
1104 /* no [MS-DLX] server, use Active Directory search instead */
1105 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
1106 sipe_utils_slist_free_full(query_rows, g_free);
1108 } else
1109 sipe_backend_search_failed(sipe_public,
1110 token,
1111 _("Invalid contact search query"));
1114 static void get_info_finalize(struct sipe_core_private *sipe_private,
1115 struct sipe_backend_buddy_info *info,
1116 const gchar *uri,
1117 const gchar *server_alias,
1118 const gchar *email)
1120 sipe_backend_buddy bbuddy;
1121 struct sipe_buddy *sbuddy;
1122 gchar *alias;
1123 gchar *value;
1125 if (!info) {
1126 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1127 } else {
1128 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
1130 if (!info)
1131 return;
1133 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
1135 if (is_empty(server_alias)) {
1136 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
1137 bbuddy);
1138 if (value) {
1139 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1140 info,
1141 SIPE_BUDDY_INFO_DISPLAY_NAME,
1142 value);
1144 } else {
1145 value = g_strdup(server_alias);
1148 /* present alias if it differs from server alias */
1149 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
1150 if (alias && !sipe_strequal(alias, value))
1152 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1153 info,
1154 SIPE_BUDDY_INFO_ALIAS,
1155 alias);
1157 g_free(alias);
1158 g_free(value);
1160 if (is_empty(email)) {
1161 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1162 bbuddy,
1163 SIPE_BUDDY_INFO_EMAIL);
1164 if (value) {
1165 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1166 info,
1167 SIPE_BUDDY_INFO_EMAIL,
1168 value);
1169 g_free(value);
1173 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1174 bbuddy,
1175 SIPE_BUDDY_INFO_SITE);
1176 if (value) {
1177 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1178 info,
1179 SIPE_BUDDY_INFO_SITE,
1180 value);
1181 g_free(value);
1184 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
1185 if (sbuddy && sbuddy->device_name) {
1186 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1187 info,
1188 SIPE_BUDDY_INFO_DEVICE,
1189 sbuddy->device_name);
1192 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
1196 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
1197 const gchar *uri,
1198 SIPE_UNUSED_PARAMETER const gchar *raw,
1199 sipe_xml *soap_body,
1200 gpointer callback_data)
1202 struct ms_dlx_data *mdd = callback_data;
1203 struct sipe_backend_buddy_info *info = NULL;
1204 gchar *server_alias = NULL;
1205 gchar *email = NULL;
1207 if (soap_body) {
1208 const sipe_xml *node;
1210 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1211 uri);
1213 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1215 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1216 node;
1217 node = sipe_xml_twin(node)) {
1218 gchar *name = sipe_xml_data(sipe_xml_child(node,
1219 "Name"));
1220 gchar *value = sipe_xml_data(sipe_xml_child(node,
1221 "Value"));
1222 const sipe_xml *values = sipe_xml_child(node,
1223 "Values");
1225 /* Single value entries */
1226 if (!is_empty(value)) {
1228 if (sipe_strcase_equal(name, "displayname")) {
1229 g_free(server_alias);
1230 server_alias = value;
1231 value = NULL;
1232 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1233 info,
1234 SIPE_BUDDY_INFO_DISPLAY_NAME,
1235 server_alias);
1236 } else if (sipe_strcase_equal(name, "mail")) {
1237 g_free(email);
1238 email = value;
1239 value = NULL;
1240 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1241 info,
1242 SIPE_BUDDY_INFO_EMAIL,
1243 email);
1244 } else if (sipe_strcase_equal(name, "title")) {
1245 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1246 info,
1247 SIPE_BUDDY_INFO_JOB_TITLE,
1248 value);
1249 } else if (sipe_strcase_equal(name, "company")) {
1250 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1251 info,
1252 SIPE_BUDDY_INFO_COMPANY,
1253 value);
1254 } else if (sipe_strcase_equal(name, "country")) {
1255 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1256 info,
1257 SIPE_BUDDY_INFO_COUNTRY,
1258 value);
1261 } else if (values) {
1262 gchar *first = sipe_xml_data(sipe_xml_child(values,
1263 "string"));
1265 if (sipe_strcase_equal(name, "telephonenumber")) {
1266 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1267 info,
1268 SIPE_BUDDY_INFO_WORK_PHONE,
1269 first);
1272 g_free(first);
1275 g_free(value);
1276 g_free(name);
1280 /* this will show the minmum information */
1281 get_info_finalize(sipe_private,
1282 info,
1283 mdd->other,
1284 server_alias,
1285 email);
1287 g_free(email);
1288 g_free(server_alias);
1289 ms_dlx_free(mdd);
1292 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1293 struct sipmsg *msg,
1294 struct transaction *trans)
1296 const gchar *uri = trans->payload->data;
1297 struct sipe_backend_buddy_info *info = NULL;
1298 gchar *server_alias = NULL;
1299 gchar *email = NULL;
1301 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1302 uri, sipe_private->username);
1304 if (msg->response != 200) {
1305 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1306 } else {
1307 sipe_xml *searchResults;
1308 const sipe_xml *mrow;
1310 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1311 msg->body ? msg->body : "");
1313 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1314 if (!searchResults) {
1316 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1318 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1319 const gchar *value;
1320 gchar *phone_number;
1322 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1324 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1325 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1326 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1329 * For 2007 system we will take this from ContactCard -
1330 * it has cleaner tel: URIs at least
1332 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1333 char *tel_uri = sip_to_tel_uri(phone_number);
1334 /* trims its parameters, so call first */
1335 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1336 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1337 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1338 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1339 g_free(tel_uri);
1341 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1342 uri);
1345 if (!is_empty(server_alias)) {
1346 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1347 info,
1348 SIPE_BUDDY_INFO_DISPLAY_NAME,
1349 server_alias);
1351 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1352 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1353 info,
1354 SIPE_BUDDY_INFO_JOB_TITLE,
1355 value);
1357 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1358 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1359 info,
1360 SIPE_BUDDY_INFO_OFFICE,
1361 value);
1363 if (!is_empty(phone_number)) {
1364 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1365 info,
1366 SIPE_BUDDY_INFO_WORK_PHONE,
1367 phone_number);
1369 g_free(phone_number);
1370 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1371 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1372 info,
1373 SIPE_BUDDY_INFO_COMPANY,
1374 value);
1376 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1377 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1378 info,
1379 SIPE_BUDDY_INFO_CITY,
1380 value);
1382 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1383 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1384 info,
1385 SIPE_BUDDY_INFO_STATE,
1386 value);
1388 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1389 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1390 info,
1391 SIPE_BUDDY_INFO_COUNTRY,
1392 value);
1394 if (!is_empty(email)) {
1395 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1396 info,
1397 SIPE_BUDDY_INFO_EMAIL,
1398 email);
1401 sipe_xml_free(searchResults);
1404 /* this will show the minmum information */
1405 get_info_finalize(sipe_private,
1406 info,
1407 uri,
1408 server_alias,
1409 email);
1411 g_free(server_alias);
1412 g_free(email);
1414 return TRUE;
1417 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1418 struct ms_dlx_data *mdd)
1420 /* error using [MS-DLX] server, retry using Active Directory */
1421 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1422 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1424 payload->destroy = g_free;
1425 payload->data = mdd->other;
1426 mdd->other = NULL;
1428 sip_soap_directory_search(sipe_private,
1430 query,
1431 process_get_info_response,
1432 payload);
1434 ms_dlx_free(mdd);
1435 g_free(query);
1438 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1439 const gchar *who)
1441 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1443 if (sipe_private->dlx_uri) {
1444 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1446 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1447 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1449 mdd->other = g_strdup(who);
1450 mdd->max_returns = 1;
1451 mdd->callback = get_info_ab_entry_response;
1452 mdd->failed_callback = get_info_ab_entry_failed;
1453 mdd->session = sipe_svc_session_start();
1455 ms_dlx_webticket_request(sipe_private, mdd);
1457 } else {
1458 /* no [MS-DLX] server, use Active Directory search instead */
1459 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1460 "msRTCSIP-PrimaryUserAddress",
1461 who);
1462 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1464 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1465 row ? row : "");
1467 payload->destroy = g_free;
1468 payload->data = g_strdup(who);
1470 sip_soap_directory_search(sipe_private,
1472 row,
1473 process_get_info_response,
1474 payload);
1475 g_free(row);
1479 static void photo_response_data_free(struct photo_response_data *data)
1481 g_free(data->who);
1482 g_free(data->photo_hash);
1483 if (data->request) {
1484 sipe_http_request_cancel(data->request);
1486 g_free(data);
1489 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1490 guint status,
1491 GSList *headers,
1492 const char *body,
1493 gpointer data)
1495 struct photo_response_data *rdata = (struct photo_response_data *) data;
1497 rdata->request = NULL;
1499 if (status == SIPE_HTTP_STATUS_OK) {
1500 const gchar *len_str = sipe_utils_nameval_find(headers,
1501 "Content-Length");
1502 if (len_str) {
1503 gsize photo_size = atoi(len_str);
1504 gpointer photo = g_new(char, photo_size);
1506 if (photo) {
1507 memcpy(photo, body, photo_size);
1509 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1510 rdata->who,
1511 photo,
1512 photo_size,
1513 rdata->photo_hash);
1518 sipe_private->buddies->pending_photo_requests =
1519 g_slist_remove(sipe_private->buddies->pending_photo_requests, rdata);
1521 photo_response_data_free(rdata);
1524 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1526 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1527 gchar *wsse_security_base64;
1528 gchar *x_ms_webticket_header;
1530 if (!assertion) {
1531 return NULL;
1534 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1535 strlen(assertion));
1536 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1537 wsse_security_base64);
1539 g_free(assertion);
1540 g_free(wsse_security_base64);
1542 return x_ms_webticket_header;
1545 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1546 const gchar *uri,
1547 SIPE_UNUSED_PARAMETER const gchar *raw,
1548 sipe_xml *soap_body,
1549 gpointer callback_data)
1551 struct ms_dlx_data *mdd = callback_data;
1552 gchar *photo_rel_path = NULL;
1553 gchar *photo_hash = NULL;
1554 const gchar *photo_hash_old =
1555 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1557 if (soap_body) {
1558 const sipe_xml *node;
1560 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1561 uri);
1563 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1564 node;
1565 node = sipe_xml_twin(node)) {
1566 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1567 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1569 if (!is_empty(value)) {
1570 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1571 g_free(photo_rel_path);
1572 photo_rel_path = value;
1573 value = NULL;
1574 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1575 g_free(photo_hash);
1576 photo_hash = value;
1577 value = NULL;
1581 g_free(value);
1582 g_free(name);
1586 if (sipe_private->addressbook_uri && photo_rel_path &&
1587 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1588 gchar *photo_url = g_strdup_printf("%s/%s",
1589 sipe_private->addressbook_uri, photo_rel_path);
1590 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1592 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1593 data->who = g_strdup(mdd->other);
1594 data->photo_hash = photo_hash;
1595 photo_hash = NULL;
1597 data->request = sipe_http_request_get(sipe_private,
1598 photo_url,
1599 x_ms_webticket_header,
1600 process_buddy_photo_response,
1601 data);
1603 if (data->request) {
1604 sipe_private->buddies->pending_photo_requests =
1605 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1606 sipe_http_request_ready(data->request);
1607 } else {
1608 photo_response_data_free(data);
1611 g_free(x_ms_webticket_header);
1612 g_free(photo_url);
1615 g_free(photo_rel_path);
1616 g_free(photo_hash);
1617 ms_dlx_free(mdd);
1620 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1621 struct ms_dlx_data *mdd)
1623 ms_dlx_free(mdd);
1626 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1627 const gchar *uri)
1629 if (sipe_backend_uses_photo()) {
1631 /* Lync 2013 or newer: use UCS */
1632 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013)) {
1634 sipe_ucs_get_photo(sipe_private, uri);
1636 /* Lync 2010: use [MS-DLX] */
1637 } else if (sipe_private->dlx_uri &&
1638 sipe_private->addressbook_uri) {
1639 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1641 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1642 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1644 mdd->other = g_strdup(uri);
1645 mdd->max_returns = 1;
1646 mdd->callback = get_photo_ab_entry_response;
1647 mdd->failed_callback = get_photo_ab_entry_failed;
1648 mdd->session = sipe_svc_session_start();
1650 ms_dlx_webticket_request(sipe_private, mdd);
1655 static void buddy_refresh_photos_cb(gpointer uri,
1656 SIPE_UNUSED_PARAMETER gpointer value,
1657 gpointer sipe_private)
1659 buddy_fetch_photo(sipe_private, uri);
1662 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1664 g_hash_table_foreach(sipe_private->buddies->uri,
1665 buddy_refresh_photos_cb,
1666 sipe_private);
1669 /* Buddy menu callbacks*/
1671 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1672 const gchar *who)
1674 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1676 /* 2007+ conference */
1677 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1678 sipe_conf_add(sipe_private, who);
1680 /* 2005- multiparty chat */
1681 } else {
1682 gchar *self = sip_uri_self(sipe_private);
1683 struct sip_session *session;
1685 session = sipe_session_add_chat(sipe_private,
1686 NULL,
1687 TRUE,
1688 self);
1689 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1690 session->chat_session,
1691 session->chat_session->title,
1692 self);
1693 g_free(self);
1695 sipe_im_invite(sipe_private, session, who,
1696 NULL, NULL, NULL, FALSE);
1700 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1701 const gchar *who)
1703 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1704 who,
1705 NULL);
1706 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1707 buddy,
1708 SIPE_BUDDY_INFO_EMAIL);
1710 if (email) {
1711 gchar *command_line = g_strdup_printf(
1712 #ifdef _WIN32
1713 "cmd /c start"
1714 #else
1715 "xdg-email"
1716 #endif
1717 " mailto:%s", email);
1718 g_free(email);
1720 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1721 command_line);
1722 g_spawn_command_line_async(command_line, NULL);
1723 g_free(command_line);
1725 } else {
1726 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1727 who);
1731 /* Buddy menu */
1733 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1734 struct sipe_backend_buddy_menu *menu,
1735 sipe_backend_buddy buddy,
1736 sipe_buddy_info_fields id_phone,
1737 sipe_buddy_info_fields id_display,
1738 const gchar *type)
1740 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1741 buddy,
1742 id_phone);
1743 if (phone) {
1744 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1745 buddy,
1746 id_display);
1747 gchar *tmp = NULL;
1748 gchar *label = g_strdup_printf("%s %s",
1749 type,
1750 display ? display :
1751 (tmp = sip_tel_uri_denormalize(phone)));
1752 menu = sipe_backend_buddy_menu_add(sipe_public,
1753 menu,
1754 label,
1755 SIPE_BUDDY_MENU_MAKE_CALL,
1756 phone);
1757 g_free(tmp);
1758 g_free(label);
1759 g_free(display);
1760 g_free(phone);
1763 return(menu);
1766 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1767 const gchar *buddy_name,
1768 struct sipe_backend_buddy_menu *menu)
1770 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1771 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1772 buddy_name,
1773 NULL);
1774 gchar *self = sip_uri_self(sipe_private);
1776 SIPE_SESSION_FOREACH {
1777 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1779 struct sipe_chat_session *chat_session = session->chat_session;
1780 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1782 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1784 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1786 if (is_conf &&
1787 /* Not conf OP */
1788 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1789 /* We are a conf OP */
1790 conf_op) {
1791 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1792 chat_session->title);
1793 menu = sipe_backend_buddy_menu_add(sipe_public,
1794 menu,
1795 label,
1796 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1797 chat_session);
1798 g_free(label);
1801 if (is_conf &&
1802 /* We are a conf OP */
1803 conf_op) {
1804 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1805 chat_session->title);
1806 menu = sipe_backend_buddy_menu_add(sipe_public,
1807 menu,
1808 label,
1809 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1810 chat_session);
1811 g_free(label);
1814 else
1816 if (!is_conf ||
1817 (is_conf && !session->locked)) {
1818 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1819 chat_session->title);
1820 menu = sipe_backend_buddy_menu_add(sipe_public,
1821 menu,
1822 label,
1823 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1824 chat_session);
1825 g_free(label);
1829 } SIPE_SESSION_FOREACH_END;
1830 g_free(self);
1832 menu = sipe_backend_buddy_menu_add(sipe_public,
1833 menu,
1834 _("New chat"),
1835 SIPE_BUDDY_MENU_NEW_CHAT,
1836 NULL);
1838 /* add buddy's phone numbers if we have call control */
1839 if (sip_csta_is_idle(sipe_private)) {
1841 /* work phone */
1842 menu = buddy_menu_phone(sipe_public,
1843 menu,
1844 buddy,
1845 SIPE_BUDDY_INFO_WORK_PHONE,
1846 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1847 _("Work"));
1848 /* mobile phone */
1849 menu = buddy_menu_phone(sipe_public,
1850 menu,
1851 buddy,
1852 SIPE_BUDDY_INFO_MOBILE_PHONE,
1853 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1854 _("Mobile"));
1856 /* home phone */
1857 menu = buddy_menu_phone(sipe_public,
1858 menu,
1859 buddy,
1860 SIPE_BUDDY_INFO_HOME_PHONE,
1861 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1862 _("Home"));
1864 /* other phone */
1865 menu = buddy_menu_phone(sipe_public,
1866 menu,
1867 buddy,
1868 SIPE_BUDDY_INFO_OTHER_PHONE,
1869 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1870 _("Other"));
1872 /* custom1 phone */
1873 menu = buddy_menu_phone(sipe_public,
1874 menu,
1875 buddy,
1876 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1877 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1878 _("Custom1"));
1882 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1883 buddy,
1884 SIPE_BUDDY_INFO_EMAIL);
1885 if (email) {
1886 menu = sipe_backend_buddy_menu_add(sipe_public,
1887 menu,
1888 _("Send email..."),
1889 SIPE_BUDDY_MENU_SEND_EMAIL,
1890 NULL);
1891 g_free(email);
1895 /* access level control */
1896 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1897 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1898 menu,
1899 _("Access level"),
1900 sipe_ocs2007_access_control_menu(sipe_private,
1901 buddy_name));
1903 return(menu);
1906 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
1908 return(g_hash_table_size(sipe_private->buddies->uri));
1911 static guint sipe_ht_hash_nick(const char *nick)
1913 char *lc = g_utf8_strdown(nick, -1);
1914 guint bucket = g_str_hash(lc);
1915 g_free(lc);
1917 return bucket;
1920 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
1922 char *nick1_norm = NULL;
1923 char *nick2_norm = NULL;
1924 gboolean equal;
1926 if (nick1 == NULL && nick2 == NULL) return TRUE;
1927 if (nick1 == NULL || nick2 == NULL ||
1928 !g_utf8_validate(nick1, -1, NULL) ||
1929 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
1931 nick1_norm = g_utf8_casefold(nick1, -1);
1932 nick2_norm = g_utf8_casefold(nick2, -1);
1933 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
1934 g_free(nick2_norm);
1935 g_free(nick1_norm);
1937 return equal;
1940 void sipe_buddy_init(struct sipe_core_private *sipe_private)
1942 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
1943 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
1944 (GEqualFunc) sipe_ht_equals_nick);
1945 buddies->exchange_key = g_hash_table_new(g_str_hash,
1946 g_str_equal);
1947 sipe_private->buddies = buddies;
1951 Local Variables:
1952 mode: c
1953 c-file-style: "bsd"
1954 indent-tabs-mode: t
1955 tab-width: 8
1956 End: