6 * Copyright (C) 2010-2018 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
22 * GetUserPhoto operation
23 * <http://msdn.microsoft.com/en-us/library/office/jj900502.aspx>
36 #include "sipe-common.h"
40 #include "sip-transport.h"
41 #include "sipe-backend.h"
42 #include "sipe-buddy.h"
44 #include "sipe-chat.h"
45 #include "sipe-conf.h"
46 #include "sipe-core.h"
47 #include "sipe-core-private.h"
48 #include "sipe-appshare.h"
49 #include "sipe-digest.h"
50 #include "sipe-group.h"
51 #include "sipe-http.h"
53 #include "sipe-media.h"
55 #include "sipe-ocs2005.h"
56 #include "sipe-ocs2007.h"
57 #include "sipe-schedule.h"
58 #include "sipe-session.h"
59 #include "sipe-status.h"
60 #include "sipe-subscriptions.h"
63 #include "sipe-utils.h"
64 #include "sipe-webticket.h"
69 GHashTable
*exchange_key
;
71 /* Pending photo download HTTP requests */
72 GSList
*pending_photo_requests
;
75 struct buddy_group_data
{
76 const struct sipe_group
*group
;
80 struct photo_response_data
{
83 struct sipe_http_request
*request
;
86 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
88 static void photo_response_data_free(struct photo_response_data
*data
);
90 void sipe_buddy_add_keys(struct sipe_core_private
*sipe_private
,
91 struct sipe_buddy
*buddy
,
92 const gchar
*exchange_key
,
93 const gchar
*change_key
)
96 buddy
->exchange_key
= g_strdup(exchange_key
);
97 g_hash_table_insert(sipe_private
->buddies
->exchange_key
,
102 buddy
->change_key
= g_strdup(change_key
);
105 struct sipe_buddy
*sipe_buddy_add(struct sipe_core_private
*sipe_private
,
107 const gchar
*exchange_key
,
108 const gchar
*change_key
)
110 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
111 gchar
*normalized_uri
= g_ascii_strdown(uri
, -1);
112 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
116 buddy
= g_new0(struct sipe_buddy
, 1);
117 buddy
->name
= normalized_uri
;
118 g_hash_table_insert(sipe_private
->buddies
->uri
,
122 sipe_buddy_add_keys(sipe_private
,
127 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri
);
129 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES
)) {
130 buddy
->just_added
= TRUE
;
131 sipe_subscribe_presence_single_cb(sipe_private
,
135 buddy_fetch_photo(sipe_private
, normalized_uri
);
137 normalized_uri
= NULL
; /* buddy takes ownership */
139 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri
);
140 buddy
->is_obsolete
= FALSE
;
142 g_free(normalized_uri
);
147 static gboolean
is_buddy_in_group(struct sipe_buddy
*buddy
,
151 GSList
*entry
= buddy
->groups
;
154 struct buddy_group_data
*bgd
= entry
->data
;
155 if (sipe_strequal(bgd
->group
->name
, name
)) {
156 bgd
->is_obsolete
= FALSE
;
166 void sipe_buddy_add_to_group(struct sipe_core_private
*sipe_private
,
167 struct sipe_buddy
*buddy
,
168 struct sipe_group
*group
,
171 const gchar
*uri
= buddy
->name
;
172 const gchar
*group_name
= group
->name
;
173 sipe_backend_buddy bb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
178 bb
= sipe_backend_buddy_add(SIPE_CORE_PUBLIC
,
182 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
183 uri
, alias
? alias
: "<NONE>");
187 if (!is_empty(alias
)) {
188 gchar
*old_alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
191 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri
),
193 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
,
196 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
197 uri
, old_alias
, alias
);
202 if (!is_buddy_in_group(buddy
, group_name
)) {
203 sipe_buddy_insert_group(buddy
, group
);
204 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
209 static gint
buddy_group_compare(gconstpointer a
, gconstpointer b
)
211 return(((const struct buddy_group_data
*)a
)->group
->id
-
212 ((const struct buddy_group_data
*)b
)->group
->id
);
215 void sipe_buddy_insert_group(struct sipe_buddy
*buddy
,
216 struct sipe_group
*group
)
218 struct buddy_group_data
*bgd
= g_new0(struct buddy_group_data
, 1);
222 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
228 static void buddy_group_free(gpointer data
)
233 static void buddy_group_remove(struct sipe_buddy
*buddy
,
234 struct buddy_group_data
*bgd
)
236 buddy
->groups
= g_slist_remove(buddy
->groups
, bgd
);
237 buddy_group_free(bgd
);
240 static void sipe_buddy_remove_group(struct sipe_buddy
*buddy
,
241 const struct sipe_group
*group
)
243 GSList
*entry
= buddy
->groups
;
244 struct buddy_group_data
*bgd
= NULL
;
248 if (bgd
->group
== group
)
253 buddy_group_remove(buddy
, bgd
);
256 void sipe_buddy_update_groups(struct sipe_core_private
*sipe_private
,
257 struct sipe_buddy
*buddy
,
260 const gchar
*uri
= buddy
->name
;
261 GSList
*entry
= buddy
->groups
;
264 struct buddy_group_data
*bgd
= entry
->data
;
265 const struct sipe_group
*group
= bgd
->group
;
267 /* next buddy group */
270 /* old group NOT found in new list? */
271 if (g_slist_find(new_groups
, group
) == NULL
) {
272 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
275 SIPE_DEBUG_INFO("sipe_buddy_update_groups: removing buddy %s from group '%s'",
277 /* this should never be NULL */
279 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
281 buddy_group_remove(buddy
, bgd
);
286 gchar
*sipe_buddy_groups_string(struct sipe_buddy
*buddy
)
290 /* creating array from GList, converting guint to gchar * */
291 gchar
**ids_arr
= g_new(gchar
*, g_slist_length(buddy
->groups
) + 1);
292 GSList
*entry
= buddy
->groups
;
298 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
299 ids_arr
[i
] = g_strdup_printf("%u", group
->id
);
305 string
= g_strjoinv(" ", ids_arr
);
311 void sipe_buddy_cleanup_local_list(struct sipe_core_private
*sipe_private
)
313 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
316 GSList
*entry
= buddies
;
318 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
319 g_slist_length(buddies
));
320 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
321 sipe_buddy_count(sipe_private
));
323 sipe_backend_buddy bb
= entry
->data
;
324 gchar
*bname
= sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC
,
326 gchar
*gname
= sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC
,
328 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
331 if (!is_buddy_in_group(buddy
, gname
)) {
332 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",
334 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, bb
);
343 g_slist_free(buddies
);
346 struct sipe_buddy
*sipe_buddy_find_by_uri(struct sipe_core_private
*sipe_private
,
349 if (!uri
) return(NULL
);
350 return(g_hash_table_lookup(sipe_private
->buddies
->uri
, uri
));
353 struct sipe_buddy
*sipe_buddy_find_by_exchange_key(struct sipe_core_private
*sipe_private
,
354 const gchar
*exchange_key
)
356 return(g_hash_table_lookup(sipe_private
->buddies
->exchange_key
,
360 void sipe_buddy_foreach(struct sipe_core_private
*sipe_private
,
362 gpointer callback_data
)
364 g_hash_table_foreach(sipe_private
->buddies
->uri
,
369 static void buddy_free(struct sipe_buddy
*buddy
)
373 * We are calling g_hash_table_foreach_steal(). That means that no
374 * key/value deallocation functions are called. Therefore the glib
375 * hash code does not touch the key (buddy->name) or value (buddy)
376 * of the to-be-deleted hash node at all. It follows that we
378 * - MUST free the memory for the key ourselves and
379 * - ARE allowed to do it in this function
381 * Conclusion: glib must be broken on the Windows platform if sipe
382 * crashes with SIGTRAP when closing. You'll have to live
383 * with the memory leak until this is fixed.
387 g_free(buddy
->exchange_key
);
388 g_free(buddy
->change_key
);
389 g_free(buddy
->activity
);
390 g_free(buddy
->meeting_subject
);
391 g_free(buddy
->meeting_location
);
394 g_free(buddy
->cal_start_time
);
395 g_free(buddy
->cal_free_busy_base64
);
396 g_free(buddy
->cal_free_busy
);
397 g_free(buddy
->last_non_cal_activity
);
399 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
401 g_free(buddy
->device_name
);
402 sipe_utils_slist_free_full(buddy
->groups
, buddy_group_free
);
406 static gboolean
buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key
,
408 SIPE_UNUSED_PARAMETER gpointer user_data
)
411 /* We must return TRUE as the key/value have already been deleted */
415 void sipe_buddy_free(struct sipe_core_private
*sipe_private
)
417 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
419 g_hash_table_foreach_steal(buddies
->uri
,
423 /* core is being deallocated, remove all its pending photo requests */
424 while (buddies
->pending_photo_requests
) {
425 struct photo_response_data
*data
=
426 buddies
->pending_photo_requests
->data
;
427 buddies
->pending_photo_requests
=
428 g_slist_remove(buddies
->pending_photo_requests
, data
);
429 photo_response_data_free(data
);
432 g_hash_table_destroy(buddies
->uri
);
433 g_hash_table_destroy(buddies
->exchange_key
);
435 sipe_private
->buddies
= NULL
;
438 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
440 SIPE_UNUSED_PARAMETER gpointer user_data
)
442 struct sipe_buddy
*buddy
= value
;
443 GSList
*entry
= buddy
->groups
;
445 buddy
->is_obsolete
= TRUE
;
447 ((struct buddy_group_data
*) entry
->data
)->is_obsolete
= TRUE
;
452 void sipe_buddy_update_start(struct sipe_core_private
*sipe_private
)
454 g_hash_table_foreach(sipe_private
->buddies
->uri
,
455 buddy_set_obsolete_flag
,
459 static gboolean
buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
463 struct sipe_core_private
*sipe_private
= user_data
;
464 struct sipe_buddy
*buddy
= value
;
465 const gchar
*uri
= buddy
->name
;
467 if (buddy
->is_obsolete
) {
468 /* all backend buddies in different groups */
469 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
472 GSList
*entry
= buddies
;
474 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
475 g_slist_length(buddies
),
479 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
483 g_slist_free(buddies
);
486 /* return TRUE as the key/value have already been deleted */
490 GSList
*entry
= buddy
->groups
;
493 struct buddy_group_data
*bgd
= entry
->data
;
495 /* next buddy group */
498 if (bgd
->is_obsolete
) {
499 const struct sipe_group
*group
= bgd
->group
;
500 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
503 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: removing buddy '%s' from group '%s'",
505 /* this should never be NULL */
507 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
509 buddy_group_remove(buddy
, bgd
);
516 void sipe_buddy_update_finish(struct sipe_core_private
*sipe_private
)
518 g_hash_table_foreach_remove(sipe_private
->buddies
->uri
,
519 buddy_check_obsolete_flag
,
523 gchar
*sipe_core_buddy_status(struct sipe_core_public
*sipe_public
,
526 const gchar
*status_text
)
528 struct sipe_buddy
*sbuddy
;
531 if (!sipe_public
) return NULL
; /* happens on pidgin exit */
533 sbuddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
, uri
);
534 if (!sbuddy
) return NULL
;
536 status
= g_string_new(sbuddy
->activity
? sbuddy
->activity
:
537 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
540 if (sbuddy
->is_mobile
) {
542 g_string_append(status
, " - ");
543 g_string_append(status
, _("Mobile"));
548 g_string_append(status
, " - ");
549 g_string_append(status
, sbuddy
->note
);
552 /* return NULL instead of empty status text */
553 return(g_string_free(status
, status
->len
? FALSE
: TRUE
));
556 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
559 sipe_backend_buddy pbuddy
;
561 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
562 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
567 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
569 const gchar
*old_group_name
,
570 const gchar
*new_group_name
)
572 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
573 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
575 struct sipe_group
*old_group
= NULL
;
576 struct sipe_group
*new_group
;
577 struct sipe_ucs_transaction
*ucs_trans
= NULL
;
579 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' old group '%s' new group '%s'",
581 old_group_name
? old_group_name
: "<UNDEFINED>",
582 new_group_name
? new_group_name
: "<UNDEFINED>");
585 /* buddy not in roaming list */
588 old_group
= sipe_group_find_by_name(sipe_private
, old_group_name
);
590 sipe_buddy_remove_group(buddy
, old_group
);
591 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' removed from old group '%s'",
592 who
, old_group_name
);
595 new_group
= sipe_group_find_by_name(sipe_private
, new_group_name
);
597 sipe_buddy_insert_group(buddy
, new_group
);
598 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' added to new group '%s'",
599 who
, new_group_name
);
602 if (sipe_ucs_is_migrated(sipe_private
)) {
605 ucs_trans
= sipe_ucs_transaction(sipe_private
);
609 * 1. new buddy added to existing group
610 * 2. existing buddy moved from old to existing group
612 sipe_ucs_group_add_buddy(sipe_private
,
618 sipe_ucs_group_remove_buddy(sipe_private
,
623 } else if (old_group
) {
625 * 3. existing buddy removed from one of its groups
626 * 4. existing buddy removed from last group
628 sipe_ucs_group_remove_buddy(sipe_private
,
632 if (g_slist_length(buddy
->groups
) < 1)
633 sipe_buddy_remove(sipe_private
,
635 /* buddy no longer valid */
638 /* non-UCS handling */
639 } else if (new_group
)
640 sipe_group_update_buddy(sipe_private
, buddy
);
642 /* 5. buddy added to new group */
644 sipe_group_create(sipe_private
,
650 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
652 const gchar
*group_name
)
654 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
656 if (!sipe_buddy_find_by_uri(sipe_private
, uri
))
657 sipe_buddy_add(sipe_private
,
662 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
665 sipe_core_buddy_group(sipe_public
,
671 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
672 struct sipe_buddy
*buddy
)
674 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
675 const gchar
*uri
= buddy
->name
;
676 GSList
*entry
= buddy
->groups
;
677 gchar
*action_name
= sipe_utils_presence_key(uri
);
679 sipe_schedule_cancel(sipe_private
, action_name
);
682 /* If the buddy still has groups, we need to delete backend buddies */
684 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
685 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
688 /* this should never be NULL */
690 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, oldb
);
695 g_hash_table_remove(buddies
->uri
, uri
);
696 if (buddy
->exchange_key
)
697 g_hash_table_remove(buddies
->exchange_key
,
698 buddy
->exchange_key
);
704 * Unassociates buddy from group first.
705 * Then see if no groups left, removes buddy completely.
706 * Otherwise updates buddy groups on server.
708 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
710 const gchar
*group_name
)
712 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
713 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
715 struct sipe_group
*group
= NULL
;
720 group
= sipe_group_find_by_name(sipe_private
, group_name
);
722 sipe_buddy_remove_group(buddy
, group
);
723 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy '%s' removed from group '%s'",
728 if (g_slist_length(buddy
->groups
) < 1) {
730 if (sipe_ucs_is_migrated(sipe_private
)) {
731 sipe_ucs_group_remove_buddy(sipe_private
,
736 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
738 sip_soap_request(sipe_private
,
744 sipe_buddy_remove(sipe_private
, buddy
);
746 if (sipe_ucs_is_migrated(sipe_private
)) {
747 sipe_ucs_group_remove_buddy(sipe_private
,
752 /* updates groups on server */
753 sipe_group_update_buddy(sipe_private
, buddy
);
757 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
761 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
762 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
767 /* Check if on 2005 system contact's calendar,
768 * then set/preserve it.
770 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
771 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
773 sipe_ocs2005_apply_calendar_status(sipe_private
,
775 sipe_status_activity_to_token(activity
));
779 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
781 const gchar
*status_name
,
783 struct sipe_backend_buddy_tooltip
*tooltip
)
785 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
787 gboolean is_oof_note
= FALSE
;
788 const gchar
*activity
= NULL
;
789 gchar
*calendar
= NULL
;
790 const gchar
*meeting_subject
= NULL
;
791 const gchar
*meeting_location
= NULL
;
792 gchar
*access_text
= NULL
;
794 #define SIPE_ADD_BUDDY_INFO(l, t) \
796 gchar *tmp = g_markup_escape_text((t), -1); \
797 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
800 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
801 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
803 if (sipe_public
) { /* happens on pidgin exit */
804 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
808 is_oof_note
= sbuddy
->is_oof_note
;
809 activity
= sbuddy
->activity
;
810 calendar
= sipe_cal_get_description(sbuddy
);
811 meeting_subject
= sbuddy
->meeting_subject
;
812 meeting_location
= sbuddy
->meeting_location
;
814 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
815 gboolean is_group_access
= FALSE
;
816 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
818 sipe_get_no_sip_uri(uri
),
820 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
821 access_text
= is_group_access
?
822 g_strdup(access_level
) :
823 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
829 const gchar
*status_str
= activity
? activity
: status_name
;
831 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
833 if (is_online
&& !is_empty(calendar
)) {
834 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
837 if (!is_empty(meeting_location
)) {
838 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
839 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
841 if (!is_empty(meeting_subject
)) {
842 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
843 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
846 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
847 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
848 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
850 g_free(note_italics
);
853 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
858 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
860 sipe_buddy_info_fields propkey
,
861 char *property_value
)
863 GSList
*buddies
, *entry
;
866 property_value
= g_strstrip(property_value
);
868 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
871 sipe_backend_buddy p_buddy
= entry
->data
;
873 /* for Display Name */
874 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
876 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
877 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
878 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
879 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
883 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
884 if (!is_empty(property_value
) &&
885 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
887 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
888 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
892 /* for other properties */
894 if (!is_empty(property_value
)) {
895 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
896 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
897 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
905 g_slist_free(buddies
);
914 sipe_svc_callback
*callback
;
915 struct sipe_svc_session
*session
;
916 gchar
*wsse_security
;
917 struct sipe_backend_search_token
*token
;
918 /* must call ms_dlx_free() */
919 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
920 struct ms_dlx_data
*mdd
);
923 static void free_search_rows(GSList
*search_rows
)
925 sipe_utils_slist_free_full(search_rows
, g_free
);
928 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
930 free_search_rows(mdd
->search_rows
);
931 sipe_svc_session_close(mdd
->session
);
933 g_free(mdd
->wsse_security
);
937 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
938 #define DLX_SEARCH_ITEM \
939 "<AbEntryRequest.ChangeSearchQuery>" \
940 " <SearchOn>%s</SearchOn>" \
941 " <Value>%s</Value>" \
942 "</AbEntryRequest.ChangeSearchQuery>"
944 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
945 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
954 attr
= query_rows
->data
;
955 query_rows
= g_slist_next(query_rows
);
956 value
= query_rows
->data
;
957 query_rows
= g_slist_next(query_rows
);
963 * Special value for SIP ID
965 * Active Directory seems only to be able to search for
966 * SIP URIs. Make sure search string starts with "sip:".
969 attr
= "msRTCSIP-PrimaryUserAddress";
971 value
= tmp
= sip_uri(value
);
974 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
981 query
= g_strjoinv(NULL
, attrs
);
982 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
991 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
992 const gchar
*base_uri
,
993 const gchar
*auth_uri
,
994 const gchar
*wsse_security
,
995 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
996 gpointer callback_data
)
998 struct ms_dlx_data
*mdd
= callback_data
;
1000 if (wsse_security
) {
1001 guint length
= g_slist_length(mdd
->search_rows
);
1004 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
1008 /* complex search */
1009 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
1010 search
= g_strdup_printf("<ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
1018 search
= g_strdup_printf("<BasicSearch>"
1019 " <SearchList>c,company,displayName,givenName,mail,mailNickname,msRTCSIP-PrimaryUserAddress,sn</SearchList>"
1020 " <Value>%s</Value>"
1021 " <Verb>BeginsWith</Verb>"
1026 if (sipe_svc_ab_entry_request(sipe_private
,
1035 /* keep webticket security token for potential further use */
1036 g_free(mdd
->wsse_security
);
1037 mdd
->wsse_security
= g_strdup(wsse_security
);
1039 /* callback data passed down the line */
1045 /* no ticket: this will show the minmum information */
1046 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
1051 mdd
->failed_callback(sipe_private
, mdd
);
1054 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
1055 struct ms_dlx_data
*mdd
)
1057 if (!sipe_webticket_request_with_port(sipe_private
,
1059 sipe_private
->dlx_uri
,
1060 "AddressBookWebTicketBearer",
1063 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
1064 sipe_private
->dlx_uri
);
1065 mdd
->failed_callback(sipe_private
, mdd
);
1069 void sipe_buddy_search_contacts_finalize(struct sipe_core_private
*sipe_private
,
1070 struct sipe_backend_search_results
*results
,
1074 gchar
*secondary
= g_strdup_printf(
1075 dngettext(PACKAGE_NAME
,
1076 "Found %d contact%s:",
1077 "Found %d contacts%s:", match_count
),
1078 match_count
, more
? _(" (more matched your query)") : "");
1080 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
1087 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
1089 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1090 sipe_xml
*soap_body
,
1091 gpointer callback_data
)
1093 struct ms_dlx_data
*mdd
= callback_data
;
1096 const sipe_xml
*node
;
1097 struct sipe_backend_search_results
*results
;
1100 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
1104 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
1106 /* try again with simple search, if possible */
1107 if (mdd
->other
&& mdd
->search_rows
) {
1108 SIPE_DEBUG_INFO_NOFORMAT("search_ab_entry_response: no matches, retrying with simple search");
1110 /* throw away original search query */
1111 free_search_rows(mdd
->search_rows
);
1112 mdd
->search_rows
= NULL
;
1114 ms_dlx_webticket_request(sipe_private
, mdd
);
1116 /* callback data passed down the line */
1120 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
1122 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1124 _("No contacts found"));
1130 /* OK, we found something - show the results to the user */
1131 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1134 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
1135 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1137 _("Unable to display the search results"));
1142 /* SearchAbEntryResult can contain duplicates */
1143 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1146 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
1147 const sipe_xml
*attrs
;
1148 gchar
*sip_uri
= NULL
;
1149 gchar
*displayname
= NULL
;
1150 gchar
*company
= NULL
;
1151 gchar
*country
= NULL
;
1152 gchar
*email
= NULL
;
1154 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
1156 attrs
= sipe_xml_twin(attrs
)) {
1157 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
1159 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
1162 if (!is_empty(value
)) {
1163 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
1167 } else if (sipe_strcase_equal(name
, "displayname")) {
1168 g_free(displayname
);
1169 displayname
= value
;
1171 } else if (sipe_strcase_equal(name
, "mail")) {
1175 } else if (sipe_strcase_equal(name
, "company")) {
1179 } else if (sipe_strcase_equal(name
, "country")) {
1190 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
1191 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
1192 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1199 g_strfreev(uri_parts
);
1201 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
1208 g_free(displayname
);
1212 sipe_buddy_search_contacts_finalize(sipe_private
, results
,
1213 g_hash_table_size(found
),
1215 g_hash_table_destroy(found
);
1219 mdd
->failed_callback(sipe_private
, mdd
);
1223 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
1225 struct transaction
*trans
)
1227 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
1228 struct sipe_backend_search_results
*results
;
1229 sipe_xml
*searchResults
;
1230 const sipe_xml
*mrow
;
1231 guint match_count
= 0;
1232 gboolean more
= FALSE
;
1234 /* valid response? */
1235 if (msg
->response
!= 200) {
1236 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1238 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1240 _("Contact search failed"));
1244 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
1247 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1248 if (!searchResults
) {
1249 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1250 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1252 _("Contact search failed"));
1257 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
1259 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1260 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1262 _("No contacts found"));
1264 sipe_xml_free(searchResults
);
1268 /* OK, we found something - show the results to the user */
1269 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1270 trans
->payload
->data
);
1272 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1273 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1275 _("Unable to display the search results"));
1277 sipe_xml_free(searchResults
);
1281 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
1282 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
1283 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1286 sipe_xml_attribute(mrow
, "displayName"),
1287 sipe_xml_attribute(mrow
, "company"),
1288 sipe_xml_attribute(mrow
, "country"),
1289 sipe_xml_attribute(mrow
, "email"));
1290 g_strfreev(uri_parts
);
1294 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
1295 char *data
= sipe_xml_data(mrow
);
1296 more
= (g_ascii_strcasecmp(data
, "true") == 0);
1300 sipe_buddy_search_contacts_finalize(sipe_private
, results
, match_count
, more
);
1301 sipe_xml_free(searchResults
);
1306 static void search_soap_request(struct sipe_core_private
*sipe_private
,
1307 GDestroyNotify destroy
,
1310 SoapTransCallback callback
,
1311 GSList
*search_rows
)
1313 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
1314 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1316 payload
->destroy
= destroy
;
1317 payload
->data
= data
;
1319 sip_soap_directory_search(sipe_private
,
1327 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1328 struct ms_dlx_data
*mdd
)
1330 /* error using [MS-DLX] server, retry using Active Directory */
1331 if (mdd
->search_rows
)
1332 search_soap_request(sipe_private
,
1336 process_search_contact_response
,
1341 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
1342 struct sipe_backend_search_token
*token
,
1343 const gchar
*given_name
,
1344 const gchar
*surname
,
1347 const gchar
*company
,
1348 const gchar
*country
)
1350 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1352 /* Lync 2013 or newer: use UCS if contacts are migrated */
1353 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) &&
1354 sipe_ucs_is_migrated(sipe_private
)) {
1356 sipe_ucs_search(sipe_private
,
1366 GSList
*query_rows
= NULL
;
1368 const gchar
*simple
= NULL
;
1370 #define ADD_QUERY_ROW(attr, val) \
1372 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1373 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1378 ADD_QUERY_ROW("givenName", given_name
);
1379 ADD_QUERY_ROW("sn", surname
);
1380 ADD_QUERY_ROW("mail", email
);
1381 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1382 ADD_QUERY_ROW(NULL
, sipid
);
1383 ADD_QUERY_ROW("company", company
);
1384 ADD_QUERY_ROW("c", country
);
1387 if (sipe_private
->dlx_uri
!= NULL
) {
1388 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1390 mdd
->search_rows
= query_rows
;
1391 /* user entered only one search string, remember that one */
1393 mdd
->other
= g_strdup(simple
);
1394 mdd
->max_returns
= 100;
1395 mdd
->callback
= search_ab_entry_response
;
1396 mdd
->failed_callback
= search_ab_entry_failed
;
1397 mdd
->session
= sipe_svc_session_start();
1400 ms_dlx_webticket_request(sipe_private
, mdd
);
1403 /* no [MS-DLX] server, use Active Directory search instead */
1404 search_soap_request(sipe_private
,
1408 process_search_contact_response
,
1410 free_search_rows(query_rows
);
1413 sipe_backend_search_failed(sipe_public
,
1415 _("Invalid contact search query"));
1419 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1420 struct sipe_backend_buddy_info
*info
,
1422 const gchar
*server_alias
,
1425 sipe_backend_buddy bbuddy
;
1426 struct sipe_buddy
*sbuddy
;
1431 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1433 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1438 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1440 if (is_empty(server_alias
)) {
1441 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1444 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1446 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1450 value
= g_strdup(server_alias
);
1453 /* present alias if it differs from server alias */
1454 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1455 if (alias
&& !sipe_strequal(alias
, value
))
1457 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1459 SIPE_BUDDY_INFO_ALIAS
,
1465 if (is_empty(email
)) {
1466 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1468 SIPE_BUDDY_INFO_EMAIL
);
1470 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1472 SIPE_BUDDY_INFO_EMAIL
,
1478 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1480 SIPE_BUDDY_INFO_SITE
);
1482 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1484 SIPE_BUDDY_INFO_SITE
,
1489 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1490 if (sbuddy
&& sbuddy
->device_name
) {
1491 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1493 SIPE_BUDDY_INFO_DEVICE
,
1494 sbuddy
->device_name
);
1497 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1501 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1503 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1504 sipe_xml
*soap_body
,
1505 gpointer callback_data
)
1507 struct ms_dlx_data
*mdd
= callback_data
;
1508 struct sipe_backend_buddy_info
*info
= NULL
;
1509 gchar
*server_alias
= NULL
;
1510 gchar
*email
= NULL
;
1513 const sipe_xml
*node
;
1515 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1518 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1520 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1522 node
= sipe_xml_twin(node
)) {
1523 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1525 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1527 const sipe_xml
*values
= sipe_xml_child(node
,
1530 /* Single value entries */
1531 if (!is_empty(value
)) {
1533 if (sipe_strcase_equal(name
, "displayname")) {
1534 g_free(server_alias
);
1535 server_alias
= value
;
1537 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1539 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1541 } else if (sipe_strcase_equal(name
, "mail")) {
1545 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1547 SIPE_BUDDY_INFO_EMAIL
,
1549 } else if (sipe_strcase_equal(name
, "title")) {
1550 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1552 SIPE_BUDDY_INFO_JOB_TITLE
,
1554 } else if (sipe_strcase_equal(name
, "company")) {
1555 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1557 SIPE_BUDDY_INFO_COMPANY
,
1559 } else if (sipe_strcase_equal(name
, "country")) {
1560 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1562 SIPE_BUDDY_INFO_COUNTRY
,
1566 } else if (values
) {
1567 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1570 if (sipe_strcase_equal(name
, "telephonenumber")) {
1571 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1573 SIPE_BUDDY_INFO_WORK_PHONE
,
1585 /* this will show the minmum information */
1586 get_info_finalize(sipe_private
,
1593 g_free(server_alias
);
1597 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1599 struct transaction
*trans
)
1601 const gchar
*uri
= trans
->payload
->data
;
1602 struct sipe_backend_buddy_info
*info
= NULL
;
1603 gchar
*server_alias
= NULL
;
1604 gchar
*email
= NULL
;
1606 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1607 uri
, sipe_private
->username
);
1609 if (msg
->response
!= 200) {
1610 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1612 sipe_xml
*searchResults
;
1613 const sipe_xml
*mrow
;
1615 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1616 msg
->body
? msg
->body
: "");
1618 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1619 if (!searchResults
) {
1621 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1623 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1625 gchar
*phone_number
;
1627 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1629 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1630 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1631 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1634 * For 2007 system we will take this from ContactCard -
1635 * it has cleaner tel: URIs at least
1637 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1638 char *tel_uri
= sip_to_tel_uri(phone_number
);
1639 /* trims its parameters, so call first */
1640 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1641 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1642 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1643 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1646 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1650 if (!is_empty(server_alias
)) {
1651 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1653 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1656 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1657 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1659 SIPE_BUDDY_INFO_JOB_TITLE
,
1662 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1663 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1665 SIPE_BUDDY_INFO_OFFICE
,
1668 if (!is_empty(phone_number
)) {
1669 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1671 SIPE_BUDDY_INFO_WORK_PHONE
,
1674 g_free(phone_number
);
1675 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1676 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1678 SIPE_BUDDY_INFO_COMPANY
,
1681 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1682 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1684 SIPE_BUDDY_INFO_CITY
,
1687 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1688 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1690 SIPE_BUDDY_INFO_STATE
,
1693 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1694 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1696 SIPE_BUDDY_INFO_COUNTRY
,
1699 if (!is_empty(email
)) {
1700 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1702 SIPE_BUDDY_INFO_EMAIL
,
1706 sipe_xml_free(searchResults
);
1709 /* this will show the minmum information */
1710 get_info_finalize(sipe_private
,
1716 g_free(server_alias
);
1722 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1723 struct ms_dlx_data
*mdd
)
1725 /* error using [MS-DLX] server, retry using Active Directory */
1726 search_soap_request(sipe_private
,
1730 process_get_info_response
,
1736 static GSList
*search_rows_for_uri(const gchar
*uri
)
1738 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1739 GSList
*l
= g_slist_append(NULL
, NULL
);
1740 return(g_slist_append(l
, g_strdup(uri
)));
1743 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1746 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1747 GSList
*search_rows
= search_rows_for_uri(who
);
1749 if (sipe_private
->dlx_uri
) {
1750 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1752 mdd
->search_rows
= search_rows
;
1753 mdd
->other
= g_strdup(who
);
1754 mdd
->max_returns
= 1;
1755 mdd
->callback
= get_info_ab_entry_response
;
1756 mdd
->failed_callback
= get_info_ab_entry_failed
;
1757 mdd
->session
= sipe_svc_session_start();
1759 ms_dlx_webticket_request(sipe_private
, mdd
);
1762 /* no [MS-DLX] server, use Active Directory search instead */
1763 search_soap_request(sipe_private
,
1767 process_get_info_response
,
1769 free_search_rows(search_rows
);
1773 static void photo_response_data_free(struct photo_response_data
*data
)
1776 g_free(data
->photo_hash
);
1777 if (data
->request
) {
1778 sipe_http_request_cancel(data
->request
);
1783 static void photo_response_data_remove(struct sipe_core_private
*sipe_private
,
1784 struct photo_response_data
*data
)
1786 data
->request
= NULL
;
1787 sipe_private
->buddies
->pending_photo_requests
=
1788 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, data
);
1789 photo_response_data_free(data
);
1792 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1798 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1800 if (status
== SIPE_HTTP_STATUS_OK
) {
1801 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1804 gsize photo_size
= atoi(len_str
);
1805 gpointer photo
= g_new(char, photo_size
);
1808 memcpy(photo
, body
, photo_size
);
1810 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1819 photo_response_data_remove(sipe_private
, rdata
);
1822 static void process_get_user_photo_response(struct sipe_core_private
*sipe_private
,
1824 SIPE_UNUSED_PARAMETER GSList
*headers
,
1828 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1830 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
1831 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
1832 const sipe_xml
*node
= sipe_xml_child(xml
,
1833 "Body/GetUserPhotoResponse/PictureData");
1840 /* decode photo data */
1841 base64
= sipe_xml_data(node
);
1842 photo
= g_base64_decode(base64
, &photo_size
);
1845 /* EWS doesn't provide a hash -> calculate SHA-1 digest */
1846 if (!rdata
->photo_hash
) {
1847 guchar digest
[SIPE_DIGEST_SHA1_LENGTH
];
1848 sipe_digest_sha1(photo
, photo_size
, digest
);
1850 /* rdata takes ownership of digest string */
1851 rdata
->photo_hash
= buff_to_hex_str(digest
,
1852 SIPE_DIGEST_SHA1_LENGTH
);
1855 /* backend frees "photo" */
1856 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1866 photo_response_data_remove(sipe_private
, rdata
);
1869 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1871 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "Assertion", TRUE
);
1872 gchar
*wsse_security_base64
;
1873 gchar
*x_ms_webticket_header
;
1879 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1881 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1882 wsse_security_base64
);
1885 g_free(wsse_security_base64
);
1887 return x_ms_webticket_header
;
1890 /* see also sipe_ucs_http_request() */
1891 static struct sipe_http_request
*get_user_photo_request(struct sipe_core_private
*sipe_private
,
1892 struct photo_response_data
*data
,
1893 const gchar
*ews_url
,
1896 gchar
*soap
= g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
1898 " xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\""
1899 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
1900 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\""
1903 " <t:RequestServerVersion Version=\"Exchange2013\" />"
1907 " <m:Email>%s</m:Email>"
1908 " <m:SizeRequested>HR48x48</m:SizeRequested>"
1909 " </m:GetUserPhoto>"
1913 struct sipe_http_request
*request
= sipe_http_request_post(sipe_private
,
1917 "text/xml; charset=UTF-8",
1918 process_get_user_photo_response
,
1923 sipe_core_email_authentication(sipe_private
,
1925 sipe_http_request_allow_redirect(request
);
1927 SIPE_DEBUG_ERROR_NOFORMAT("get_user_photo_request: failed to create HTTP connection");
1933 static void photo_response_data_finalize(struct sipe_core_private
*sipe_private
,
1934 struct photo_response_data
*data
,
1936 const gchar
*photo_hash
)
1938 if (data
->request
) {
1939 data
->who
= g_strdup(uri
);
1940 data
->photo_hash
= g_strdup(photo_hash
);
1942 sipe_private
->buddies
->pending_photo_requests
=
1943 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1944 sipe_http_request_ready(data
->request
);
1946 photo_response_data_free(data
);
1950 void sipe_buddy_update_photo(struct sipe_core_private
*sipe_private
,
1952 const gchar
*photo_hash
,
1953 const gchar
*photo_url
,
1954 const gchar
*headers
)
1956 const gchar
*photo_hash_old
=
1957 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, uri
);
1959 if (!sipe_strequal(photo_hash
, photo_hash_old
)) {
1960 struct photo_response_data
*data
= g_new0(struct photo_response_data
, 1);
1962 SIPE_DEBUG_INFO("sipe_buddy_update_photo: who '%s' url '%s' hash '%s'",
1963 uri
, photo_url
, photo_hash
);
1965 /* Photo URL is embedded XML? */
1966 if (g_str_has_prefix(photo_url
, "<") &&
1967 g_str_has_suffix(photo_url
, ">")) {
1968 /* add dummy root to embedded XML string */
1969 gchar
*tmp
= g_strdup_printf("<r>%s</r>", photo_url
);
1970 sipe_xml
*xml
= sipe_xml_parse(tmp
, strlen(tmp
));
1974 gchar
*ews_url
= sipe_xml_data(sipe_xml_child(xml
, "ewsUrl"));
1975 gchar
*email
= sipe_xml_data(sipe_xml_child(xml
, "primarySMTP"));
1977 if (!is_empty(ews_url
) && !is_empty(email
)) {
1979 * Workaround for missing Office 365 buddy icons
1981 * (All?) Office 365 contact cards have the following
1982 * XML embedded as the photo URI XML node text:
1984 * <ewsUrl>https://outlook.office365.com/EWS/Exchange.asmx/WSSecurity</ewsUrl>
1985 * <primarySMTP>user@company.com</primarySMTP>
1987 * The simple HTTP request by get_user_photo_request()
1988 * is rejected with 401. But the response contains
1990 * WWW-Authenticate: Basic Realm=""
1992 * to which the HTTP transport answers with a retry
1993 * using Basic authentication. That in turn is rejected
1994 * with 500 and thus the buddy icon retrieval fails.
1996 * As a quick workaround strip the trailing "/WSSecurity"
1997 * from the URL. The HTTP request for the buddy icon
1998 * retrieval will work with this stripped URL.
2000 * @TODO: this is probably not the correct approach.
2001 * get_user_photo_request() should be updated
2002 * to support also a webticket request.
2004 gchar
*tmp
= g_strrstr(ews_url
, "/WSSecurity");
2008 data
->request
= get_user_photo_request(sipe_private
,
2019 data
->request
= sipe_http_request_get(sipe_private
,
2022 process_buddy_photo_response
,
2026 photo_response_data_finalize(sipe_private
,
2033 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
2035 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
2036 sipe_xml
*soap_body
,
2037 gpointer callback_data
)
2039 struct ms_dlx_data
*mdd
= callback_data
;
2040 gchar
*photo_rel_path
= NULL
;
2041 gchar
*photo_hash
= NULL
;
2044 const sipe_xml
*node
;
2046 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
2049 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
2051 node
= sipe_xml_twin(node
)) {
2052 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
2053 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
2055 if (!is_empty(value
)) {
2056 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
2057 g_free(photo_rel_path
);
2058 photo_rel_path
= value
;
2060 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
2072 if (sipe_private
->addressbook_uri
&& photo_rel_path
&& photo_hash
) {
2073 gchar
*photo_url
= g_strdup_printf("%s/%s",
2074 sipe_private
->addressbook_uri
, photo_rel_path
);
2075 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
2077 sipe_buddy_update_photo(sipe_private
,
2081 x_ms_webticket_header
);
2083 g_free(x_ms_webticket_header
);
2087 g_free(photo_rel_path
);
2092 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
2093 struct ms_dlx_data
*mdd
)
2098 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
2101 if (sipe_backend_uses_photo()) {
2103 /* Lync 2013 or newer: use UCS if contacts are migrated */
2104 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) &&
2105 sipe_ucs_is_migrated(sipe_private
)) {
2106 struct photo_response_data
*data
= g_new0(struct photo_response_data
, 1);
2108 data
->request
= get_user_photo_request(sipe_private
,
2110 sipe_ucs_ews_url(sipe_private
),
2111 sipe_get_no_sip_uri(uri
));
2112 photo_response_data_finalize(sipe_private
,
2115 /* there is no hash */
2118 /* Lync 2010: use [MS-DLX] */
2119 } else if (sipe_private
->dlx_uri
&&
2120 sipe_private
->addressbook_uri
) {
2121 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
2123 mdd
->search_rows
= search_rows_for_uri(uri
);
2124 mdd
->other
= g_strdup(uri
);
2125 mdd
->max_returns
= 1;
2126 mdd
->callback
= get_photo_ab_entry_response
;
2127 mdd
->failed_callback
= get_photo_ab_entry_failed
;
2128 mdd
->session
= sipe_svc_session_start();
2130 ms_dlx_webticket_request(sipe_private
, mdd
);
2135 static void buddy_refresh_photos_cb(gpointer uri
,
2136 SIPE_UNUSED_PARAMETER gpointer value
,
2137 gpointer sipe_private
)
2139 buddy_fetch_photo(sipe_private
, uri
);
2142 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
2144 g_hash_table_foreach(sipe_private
->buddies
->uri
,
2145 buddy_refresh_photos_cb
,
2149 /* Buddy menu callbacks*/
2151 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
2154 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
2156 /* 2007+ conference */
2157 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
2158 sipe_conf_add(sipe_private
, who
);
2160 /* 2005- multiparty chat */
2162 gchar
*self
= sip_uri_self(sipe_private
);
2163 struct sip_session
*session
;
2165 session
= sipe_session_add_chat(sipe_private
,
2169 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
2170 session
->chat_session
,
2171 session
->chat_session
->title
,
2175 sipe_im_invite(sipe_private
, session
, who
,
2176 NULL
, NULL
, NULL
, FALSE
);
2180 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
2183 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
2186 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2188 SIPE_BUDDY_INFO_EMAIL
);
2191 gchar
*command_line
= g_strdup_printf(
2197 " mailto:%s", email
);
2200 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
2202 g_spawn_command_line_async(command_line
, NULL
);
2203 g_free(command_line
);
2206 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
2213 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
2214 struct sipe_backend_buddy_menu
*menu
,
2215 sipe_backend_buddy buddy
,
2216 sipe_buddy_info_fields id_phone
,
2217 sipe_buddy_info_fields id_display
,
2220 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
2224 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
2228 gchar
*label
= g_strdup_printf("%s %s",
2231 (tmp
= sip_tel_uri_denormalize(phone
)));
2232 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2235 SIPE_BUDDY_MENU_MAKE_CALL
,
2246 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
2247 const gchar
*buddy_name
,
2248 struct sipe_backend_buddy_menu
*menu
)
2250 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
2251 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
2254 gchar
*self
= sip_uri_self(sipe_private
);
2256 SIPE_SESSION_FOREACH
{
2257 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
2259 struct sipe_chat_session
*chat_session
= session
->chat_session
;
2260 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
2262 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
2264 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
2268 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
2269 /* We are a conf OP */
2271 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
2272 chat_session
->title
);
2273 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2276 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
2282 /* We are a conf OP */
2284 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
2285 chat_session
->title
);
2286 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2289 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
2297 (is_conf
&& !session
->locked
)) {
2298 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
2299 chat_session
->title
);
2300 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2303 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
2309 } SIPE_SESSION_FOREACH_END
;
2312 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2315 SIPE_BUDDY_MENU_NEW_CHAT
,
2318 /* add buddy's phone numbers if we have call control */
2319 if (sip_csta_is_idle(sipe_private
)) {
2322 menu
= buddy_menu_phone(sipe_public
,
2325 SIPE_BUDDY_INFO_WORK_PHONE
,
2326 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
2329 menu
= buddy_menu_phone(sipe_public
,
2332 SIPE_BUDDY_INFO_MOBILE_PHONE
,
2333 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
2337 menu
= buddy_menu_phone(sipe_public
,
2340 SIPE_BUDDY_INFO_HOME_PHONE
,
2341 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
2345 menu
= buddy_menu_phone(sipe_public
,
2348 SIPE_BUDDY_INFO_OTHER_PHONE
,
2349 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
2353 menu
= buddy_menu_phone(sipe_public
,
2356 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
2357 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
2362 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2364 SIPE_BUDDY_INFO_EMAIL
);
2366 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2369 SIPE_BUDDY_MENU_SEND_EMAIL
,
2375 #ifdef HAVE_APPSHARE_SERVER
2377 struct sipe_media_call
*call
= sipe_media_call_find(SIPE_CORE_PRIVATE
,
2380 if (call
&& sipe_appshare_get_role(call
) == SIPE_APPSHARE_ROLE_PRESENTER
) {
2381 /* We're already presenting to this buddy. */
2383 if (sipe_core_appshare_get_remote_control(call
)) {
2384 menu
= sipe_backend_buddy_menu_add(sipe_public
, menu
,
2385 _("Take desktop control"),
2386 SIPE_BUDDY_MENU_TAKE_DESKTOP_CONTROL
,
2389 menu
= sipe_backend_buddy_menu_add(sipe_public
, menu
,
2390 _("Give desktop control"),
2391 SIPE_BUDDY_MENU_GIVE_DESKTOP_CONTROL
,
2395 menu
= sipe_backend_buddy_menu_add(sipe_public
, menu
,
2396 _("Share my desktop"),
2397 SIPE_BUDDY_MENU_SHARE_DESKTOP
,
2401 #endif // HAVE_APPSHARE_SERVER
2403 /* access level control */
2404 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
2405 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
2408 sipe_ocs2007_access_control_menu(sipe_private
,
2414 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
2416 return(g_hash_table_size(sipe_private
->buddies
->uri
));
2419 static guint
sipe_ht_hash_nick(const char *nick
)
2421 char *lc
= g_utf8_strdown(nick
, -1);
2422 guint bucket
= g_str_hash(lc
);
2428 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
2430 char *nick1_norm
= NULL
;
2431 char *nick2_norm
= NULL
;
2434 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
2435 if (nick1
== NULL
|| nick2
== NULL
||
2436 !g_utf8_validate(nick1
, -1, NULL
) ||
2437 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
2439 nick1_norm
= g_utf8_casefold(nick1
, -1);
2440 nick2_norm
= g_utf8_casefold(nick2
, -1);
2441 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
2448 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
2450 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
2451 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
2452 (GEqualFunc
) sipe_ht_equals_nick
);
2453 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
2455 sipe_private
->buddies
= buddies
;