6 * Copyright (C) 2010-2019 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
,
762 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
763 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
768 /* Check if on 2005 system contact's calendar,
769 * then set/preserve it.
771 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
772 sipe_backend_buddy_set_status(sipe_public
,
777 sipe_ocs2005_apply_calendar_status(sipe_private
,
779 sipe_status_activity_to_token(activity
));
783 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
785 const gchar
*status_name
,
787 struct sipe_backend_buddy_tooltip
*tooltip
)
789 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
791 gboolean is_oof_note
= FALSE
;
792 const gchar
*activity
= NULL
;
793 gchar
*calendar
= NULL
;
794 const gchar
*meeting_subject
= NULL
;
795 const gchar
*meeting_location
= NULL
;
796 gchar
*access_text
= NULL
;
798 #define SIPE_ADD_BUDDY_INFO(l, t) \
800 gchar *tmp = g_markup_escape_text((t), -1); \
801 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
804 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
805 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
807 if (sipe_public
) { /* happens on pidgin exit */
808 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
812 is_oof_note
= sbuddy
->is_oof_note
;
813 activity
= sbuddy
->activity
;
814 calendar
= sipe_cal_get_description(sbuddy
);
815 meeting_subject
= sbuddy
->meeting_subject
;
816 meeting_location
= sbuddy
->meeting_location
;
818 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
819 gboolean is_group_access
= FALSE
;
820 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
822 sipe_get_no_sip_uri(uri
),
824 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
825 access_text
= is_group_access
?
826 g_strdup(access_level
) :
827 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
833 const gchar
*status_str
= activity
? activity
: status_name
;
835 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
837 if (is_online
&& !is_empty(calendar
)) {
838 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
841 if (!is_empty(meeting_location
)) {
842 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
843 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
845 if (!is_empty(meeting_subject
)) {
846 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
847 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
850 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
851 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
852 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
854 g_free(note_italics
);
857 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
862 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
864 sipe_buddy_info_fields propkey
,
865 char *property_value
)
867 GSList
*buddies
, *entry
;
870 property_value
= g_strstrip(property_value
);
872 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
875 sipe_backend_buddy p_buddy
= entry
->data
;
877 /* for Display Name */
878 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
880 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
881 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
882 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
883 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
887 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
888 if (!is_empty(property_value
) &&
889 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
891 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
892 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
896 /* for other properties */
898 if (!is_empty(property_value
)) {
899 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
900 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
901 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
909 g_slist_free(buddies
);
918 sipe_svc_callback
*callback
;
919 struct sipe_svc_session
*session
;
920 gchar
*wsse_security
;
921 struct sipe_backend_search_token
*token
;
922 /* must call ms_dlx_free() */
923 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
924 struct ms_dlx_data
*mdd
);
927 static void free_search_rows(GSList
*search_rows
)
929 sipe_utils_slist_free_full(search_rows
, g_free
);
932 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
934 free_search_rows(mdd
->search_rows
);
935 sipe_svc_session_close(mdd
->session
);
937 g_free(mdd
->wsse_security
);
941 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
942 #define DLX_SEARCH_ITEM \
943 "<AbEntryRequest.ChangeSearchQuery>" \
944 " <SearchOn>%s</SearchOn>" \
945 " <Value>%s</Value>" \
946 "</AbEntryRequest.ChangeSearchQuery>"
948 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
949 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
958 attr
= query_rows
->data
;
959 query_rows
= g_slist_next(query_rows
);
960 value
= query_rows
->data
;
961 query_rows
= g_slist_next(query_rows
);
967 * Special value for SIP ID
969 * Active Directory seems only to be able to search for
970 * SIP URIs. Make sure search string starts with "sip:".
973 attr
= "msRTCSIP-PrimaryUserAddress";
975 value
= tmp
= sip_uri(value
);
978 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
985 query
= g_strjoinv(NULL
, attrs
);
986 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
995 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
996 const gchar
*base_uri
,
997 const gchar
*auth_uri
,
998 const gchar
*wsse_security
,
999 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
1000 gpointer callback_data
)
1002 struct ms_dlx_data
*mdd
= callback_data
;
1004 if (wsse_security
) {
1005 guint length
= g_slist_length(mdd
->search_rows
);
1008 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
1012 /* complex search */
1013 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
1014 search
= g_strdup_printf("<ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
1022 search
= g_strdup_printf("<BasicSearch>"
1023 " <SearchList>c,company,displayName,givenName,mail,mailNickname,msRTCSIP-PrimaryUserAddress,sn</SearchList>"
1024 " <Value>%s</Value>"
1025 " <Verb>BeginsWith</Verb>"
1030 if (sipe_svc_ab_entry_request(sipe_private
,
1039 /* keep webticket security token for potential further use */
1040 g_free(mdd
->wsse_security
);
1041 mdd
->wsse_security
= g_strdup(wsse_security
);
1043 /* callback data passed down the line */
1049 /* no ticket: this will show the minmum information */
1050 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
1055 mdd
->failed_callback(sipe_private
, mdd
);
1058 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
1059 struct ms_dlx_data
*mdd
)
1061 if (!sipe_webticket_request_with_port(sipe_private
,
1063 sipe_private
->dlx_uri
,
1064 "AddressBookWebTicketBearer",
1067 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
1068 sipe_private
->dlx_uri
);
1069 mdd
->failed_callback(sipe_private
, mdd
);
1073 void sipe_buddy_search_contacts_finalize(struct sipe_core_private
*sipe_private
,
1074 struct sipe_backend_search_results
*results
,
1078 gchar
*secondary
= g_strdup_printf(
1079 dngettext(PACKAGE_NAME
,
1080 "Found %d contact%s:",
1081 "Found %d contacts%s:", match_count
),
1082 match_count
, more
? _(" (more matched your query)") : "");
1084 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
1091 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
1093 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1094 sipe_xml
*soap_body
,
1095 gpointer callback_data
)
1097 struct ms_dlx_data
*mdd
= callback_data
;
1100 const sipe_xml
*node
;
1101 struct sipe_backend_search_results
*results
;
1104 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
1108 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
1110 /* try again with simple search, if possible */
1111 if (mdd
->other
&& mdd
->search_rows
) {
1112 SIPE_DEBUG_INFO_NOFORMAT("search_ab_entry_response: no matches, retrying with simple search");
1114 /* throw away original search query */
1115 free_search_rows(mdd
->search_rows
);
1116 mdd
->search_rows
= NULL
;
1118 ms_dlx_webticket_request(sipe_private
, mdd
);
1120 /* callback data passed down the line */
1124 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
1126 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1128 _("No contacts found"));
1134 /* OK, we found something - show the results to the user */
1135 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1138 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
1139 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1141 _("Unable to display the search results"));
1146 /* SearchAbEntryResult can contain duplicates */
1147 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1150 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
1151 const sipe_xml
*attrs
;
1152 gchar
*sip_uri
= NULL
;
1153 gchar
*displayname
= NULL
;
1154 gchar
*company
= NULL
;
1155 gchar
*country
= NULL
;
1156 gchar
*email
= NULL
;
1158 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
1160 attrs
= sipe_xml_twin(attrs
)) {
1161 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
1163 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
1166 if (!is_empty(value
)) {
1167 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
1171 } else if (sipe_strcase_equal(name
, "displayname")) {
1172 g_free(displayname
);
1173 displayname
= value
;
1175 } else if (sipe_strcase_equal(name
, "mail")) {
1179 } else if (sipe_strcase_equal(name
, "company")) {
1183 } else if (sipe_strcase_equal(name
, "country")) {
1194 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
1195 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
1196 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1203 g_strfreev(uri_parts
);
1205 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
1212 g_free(displayname
);
1216 sipe_buddy_search_contacts_finalize(sipe_private
, results
,
1217 g_hash_table_size(found
),
1219 g_hash_table_destroy(found
);
1223 mdd
->failed_callback(sipe_private
, mdd
);
1227 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
1229 struct transaction
*trans
)
1231 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
1232 struct sipe_backend_search_results
*results
;
1233 sipe_xml
*searchResults
;
1234 const sipe_xml
*mrow
;
1235 guint match_count
= 0;
1236 gboolean more
= FALSE
;
1238 /* valid response? */
1239 if (msg
->response
!= 200) {
1240 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1242 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1244 _("Contact search failed"));
1248 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
1251 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1252 if (!searchResults
) {
1253 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1254 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1256 _("Contact search failed"));
1261 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
1263 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1264 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1266 _("No contacts found"));
1268 sipe_xml_free(searchResults
);
1272 /* OK, we found something - show the results to the user */
1273 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1274 trans
->payload
->data
);
1276 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1277 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1279 _("Unable to display the search results"));
1281 sipe_xml_free(searchResults
);
1285 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
1286 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
1287 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1290 sipe_xml_attribute(mrow
, "displayName"),
1291 sipe_xml_attribute(mrow
, "company"),
1292 sipe_xml_attribute(mrow
, "country"),
1293 sipe_xml_attribute(mrow
, "email"));
1294 g_strfreev(uri_parts
);
1298 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
1299 char *data
= sipe_xml_data(mrow
);
1300 more
= (g_ascii_strcasecmp(data
, "true") == 0);
1304 sipe_buddy_search_contacts_finalize(sipe_private
, results
, match_count
, more
);
1305 sipe_xml_free(searchResults
);
1310 static void search_soap_request(struct sipe_core_private
*sipe_private
,
1311 GDestroyNotify destroy
,
1314 SoapTransCallback callback
,
1315 GSList
*search_rows
)
1317 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
1318 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1320 payload
->destroy
= destroy
;
1321 payload
->data
= data
;
1323 sip_soap_directory_search(sipe_private
,
1331 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1332 struct ms_dlx_data
*mdd
)
1334 /* error using [MS-DLX] server, retry using Active Directory */
1335 if (mdd
->search_rows
)
1336 search_soap_request(sipe_private
,
1340 process_search_contact_response
,
1345 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
1346 struct sipe_backend_search_token
*token
,
1347 const gchar
*given_name
,
1348 const gchar
*surname
,
1351 const gchar
*company
,
1352 const gchar
*country
)
1354 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1356 /* Lync 2013 or newer: use UCS if contacts are migrated */
1357 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) &&
1358 sipe_ucs_is_migrated(sipe_private
)) {
1360 sipe_ucs_search(sipe_private
,
1370 GSList
*query_rows
= NULL
;
1372 const gchar
*simple
= NULL
;
1374 #define ADD_QUERY_ROW(attr, val) \
1376 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1377 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1382 ADD_QUERY_ROW("givenName", given_name
);
1383 ADD_QUERY_ROW("sn", surname
);
1384 ADD_QUERY_ROW("mail", email
);
1385 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1386 ADD_QUERY_ROW(NULL
, sipid
);
1387 ADD_QUERY_ROW("company", company
);
1388 ADD_QUERY_ROW("c", country
);
1391 if (sipe_private
->dlx_uri
!= NULL
) {
1392 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1394 mdd
->search_rows
= query_rows
;
1395 /* user entered only one search string, remember that one */
1397 mdd
->other
= g_strdup(simple
);
1398 mdd
->max_returns
= 100;
1399 mdd
->callback
= search_ab_entry_response
;
1400 mdd
->failed_callback
= search_ab_entry_failed
;
1401 mdd
->session
= sipe_svc_session_start();
1404 ms_dlx_webticket_request(sipe_private
, mdd
);
1407 /* no [MS-DLX] server, use Active Directory search instead */
1408 search_soap_request(sipe_private
,
1412 process_search_contact_response
,
1414 free_search_rows(query_rows
);
1417 sipe_backend_search_failed(sipe_public
,
1419 _("Invalid contact search query"));
1423 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1424 struct sipe_backend_buddy_info
*info
,
1426 const gchar
*server_alias
,
1429 sipe_backend_buddy bbuddy
;
1430 struct sipe_buddy
*sbuddy
;
1435 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1437 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1442 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1444 if (is_empty(server_alias
)) {
1445 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1448 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1450 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1454 value
= g_strdup(server_alias
);
1457 /* present alias if it differs from server alias */
1458 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1459 if (alias
&& !sipe_strequal(alias
, value
))
1461 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1463 SIPE_BUDDY_INFO_ALIAS
,
1469 if (is_empty(email
)) {
1470 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1472 SIPE_BUDDY_INFO_EMAIL
);
1474 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1476 SIPE_BUDDY_INFO_EMAIL
,
1482 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1484 SIPE_BUDDY_INFO_SITE
);
1486 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1488 SIPE_BUDDY_INFO_SITE
,
1493 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1494 if (sbuddy
&& sbuddy
->device_name
) {
1495 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1497 SIPE_BUDDY_INFO_DEVICE
,
1498 sbuddy
->device_name
);
1501 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1505 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1507 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1508 sipe_xml
*soap_body
,
1509 gpointer callback_data
)
1511 struct ms_dlx_data
*mdd
= callback_data
;
1512 struct sipe_backend_buddy_info
*info
= NULL
;
1513 gchar
*server_alias
= NULL
;
1514 gchar
*email
= NULL
;
1517 const sipe_xml
*node
;
1519 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1522 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1524 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1526 node
= sipe_xml_twin(node
)) {
1527 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1529 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1531 const sipe_xml
*values
= sipe_xml_child(node
,
1534 /* Single value entries */
1535 if (!is_empty(value
)) {
1537 if (sipe_strcase_equal(name
, "displayname")) {
1538 g_free(server_alias
);
1539 server_alias
= value
;
1541 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1543 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1545 } else if (sipe_strcase_equal(name
, "mail")) {
1549 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1551 SIPE_BUDDY_INFO_EMAIL
,
1553 } else if (sipe_strcase_equal(name
, "title")) {
1554 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1556 SIPE_BUDDY_INFO_JOB_TITLE
,
1558 } else if (sipe_strcase_equal(name
, "company")) {
1559 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1561 SIPE_BUDDY_INFO_COMPANY
,
1563 } else if (sipe_strcase_equal(name
, "country")) {
1564 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1566 SIPE_BUDDY_INFO_COUNTRY
,
1570 } else if (values
) {
1571 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1574 if (sipe_strcase_equal(name
, "telephonenumber")) {
1575 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1577 SIPE_BUDDY_INFO_WORK_PHONE
,
1589 /* this will show the minmum information */
1590 get_info_finalize(sipe_private
,
1597 g_free(server_alias
);
1601 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1603 struct transaction
*trans
)
1605 const gchar
*uri
= trans
->payload
->data
;
1606 struct sipe_backend_buddy_info
*info
= NULL
;
1607 gchar
*server_alias
= NULL
;
1608 gchar
*email
= NULL
;
1610 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1611 uri
, sipe_private
->username
);
1613 if (msg
->response
!= 200) {
1614 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1616 sipe_xml
*searchResults
;
1617 const sipe_xml
*mrow
;
1619 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1620 msg
->body
? msg
->body
: "");
1622 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1623 if (!searchResults
) {
1625 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1627 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1629 gchar
*phone_number
;
1631 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1633 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1634 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1635 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1638 * For 2007 system we will take this from ContactCard -
1639 * it has cleaner tel: URIs at least
1641 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1642 char *tel_uri
= sip_to_tel_uri(phone_number
);
1643 /* trims its parameters, so call first */
1644 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1645 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1646 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1647 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1650 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1654 if (!is_empty(server_alias
)) {
1655 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1657 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1660 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1661 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1663 SIPE_BUDDY_INFO_JOB_TITLE
,
1666 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1667 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1669 SIPE_BUDDY_INFO_OFFICE
,
1672 if (!is_empty(phone_number
)) {
1673 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1675 SIPE_BUDDY_INFO_WORK_PHONE
,
1678 g_free(phone_number
);
1679 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1680 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1682 SIPE_BUDDY_INFO_COMPANY
,
1685 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1686 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1688 SIPE_BUDDY_INFO_CITY
,
1691 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1692 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1694 SIPE_BUDDY_INFO_STATE
,
1697 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1698 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1700 SIPE_BUDDY_INFO_COUNTRY
,
1703 if (!is_empty(email
)) {
1704 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1706 SIPE_BUDDY_INFO_EMAIL
,
1710 sipe_xml_free(searchResults
);
1713 /* this will show the minmum information */
1714 get_info_finalize(sipe_private
,
1720 g_free(server_alias
);
1726 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1727 struct ms_dlx_data
*mdd
)
1729 /* error using [MS-DLX] server, retry using Active Directory */
1730 search_soap_request(sipe_private
,
1734 process_get_info_response
,
1740 static GSList
*search_rows_for_uri(const gchar
*uri
)
1742 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1743 GSList
*l
= g_slist_append(NULL
, NULL
);
1744 return(g_slist_append(l
, g_strdup(uri
)));
1747 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1750 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1751 GSList
*search_rows
= search_rows_for_uri(who
);
1753 if (sipe_private
->dlx_uri
) {
1754 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1756 mdd
->search_rows
= search_rows
;
1757 mdd
->other
= g_strdup(who
);
1758 mdd
->max_returns
= 1;
1759 mdd
->callback
= get_info_ab_entry_response
;
1760 mdd
->failed_callback
= get_info_ab_entry_failed
;
1761 mdd
->session
= sipe_svc_session_start();
1763 ms_dlx_webticket_request(sipe_private
, mdd
);
1766 /* no [MS-DLX] server, use Active Directory search instead */
1767 search_soap_request(sipe_private
,
1771 process_get_info_response
,
1773 free_search_rows(search_rows
);
1777 static void photo_response_data_free(struct photo_response_data
*data
)
1780 g_free(data
->photo_hash
);
1781 if (data
->request
) {
1782 sipe_http_request_cancel(data
->request
);
1787 static void photo_response_data_remove(struct sipe_core_private
*sipe_private
,
1788 struct photo_response_data
*data
)
1790 data
->request
= NULL
;
1791 sipe_private
->buddies
->pending_photo_requests
=
1792 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, data
);
1793 photo_response_data_free(data
);
1796 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1802 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1804 if (status
== SIPE_HTTP_STATUS_OK
) {
1805 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1808 gsize photo_size
= atoi(len_str
);
1809 gpointer photo
= g_new(char, photo_size
);
1812 memcpy(photo
, body
, photo_size
);
1814 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1823 photo_response_data_remove(sipe_private
, rdata
);
1826 static void process_get_user_photo_response(struct sipe_core_private
*sipe_private
,
1828 SIPE_UNUSED_PARAMETER GSList
*headers
,
1832 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1834 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
1835 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
1836 const sipe_xml
*node
= sipe_xml_child(xml
,
1837 "Body/GetUserPhotoResponse/PictureData");
1844 /* decode photo data */
1845 base64
= sipe_xml_data(node
);
1846 photo
= g_base64_decode(base64
, &photo_size
);
1849 /* EWS doesn't provide a hash -> calculate SHA-1 digest */
1850 if (!rdata
->photo_hash
) {
1851 guchar digest
[SIPE_DIGEST_SHA1_LENGTH
];
1852 sipe_digest_sha1(photo
, photo_size
, digest
);
1854 /* rdata takes ownership of digest string */
1855 rdata
->photo_hash
= buff_to_hex_str(digest
,
1856 SIPE_DIGEST_SHA1_LENGTH
);
1859 /* backend frees "photo" */
1860 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1870 photo_response_data_remove(sipe_private
, rdata
);
1873 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1875 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "Assertion", TRUE
);
1876 gchar
*wsse_security_base64
;
1877 gchar
*x_ms_webticket_header
;
1883 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1885 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1886 wsse_security_base64
);
1889 g_free(wsse_security_base64
);
1891 return x_ms_webticket_header
;
1894 /* see also sipe_ucs_http_request() */
1895 static struct sipe_http_request
*get_user_photo_request(struct sipe_core_private
*sipe_private
,
1896 struct photo_response_data
*data
,
1897 const gchar
*ews_url
,
1900 gchar
*soap
= g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
1902 " xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\""
1903 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
1904 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\""
1907 " <t:RequestServerVersion Version=\"Exchange2013\" />"
1911 " <m:Email>%s</m:Email>"
1912 " <m:SizeRequested>HR48x48</m:SizeRequested>"
1913 " </m:GetUserPhoto>"
1917 struct sipe_http_request
*request
= sipe_http_request_post(sipe_private
,
1921 "text/xml; charset=UTF-8",
1922 process_get_user_photo_response
,
1927 sipe_core_email_authentication(sipe_private
,
1929 sipe_http_request_allow_redirect(request
);
1931 SIPE_DEBUG_ERROR_NOFORMAT("get_user_photo_request: failed to create HTTP connection");
1937 static void photo_response_data_finalize(struct sipe_core_private
*sipe_private
,
1938 struct photo_response_data
*data
,
1940 const gchar
*photo_hash
)
1942 if (data
->request
) {
1943 data
->who
= g_strdup(uri
);
1944 data
->photo_hash
= g_strdup(photo_hash
);
1946 sipe_private
->buddies
->pending_photo_requests
=
1947 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1948 sipe_http_request_ready(data
->request
);
1950 photo_response_data_free(data
);
1954 void sipe_buddy_update_photo(struct sipe_core_private
*sipe_private
,
1956 const gchar
*photo_hash
,
1957 const gchar
*photo_url
,
1958 const gchar
*headers
)
1960 const gchar
*photo_hash_old
=
1961 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, uri
);
1963 if (!sipe_strequal(photo_hash
, photo_hash_old
)) {
1964 struct photo_response_data
*data
= g_new0(struct photo_response_data
, 1);
1966 SIPE_DEBUG_INFO("sipe_buddy_update_photo: who '%s' url '%s' hash '%s'",
1967 uri
, photo_url
, photo_hash
);
1969 /* Photo URL is embedded XML? */
1970 if (g_str_has_prefix(photo_url
, "<") &&
1971 g_str_has_suffix(photo_url
, ">")) {
1972 /* add dummy root to embedded XML string */
1973 gchar
*tmp
= g_strdup_printf("<r>%s</r>", photo_url
);
1974 sipe_xml
*xml
= sipe_xml_parse(tmp
, strlen(tmp
));
1978 gchar
*ews_url
= sipe_xml_data(sipe_xml_child(xml
, "ewsUrl"));
1979 gchar
*email
= sipe_xml_data(sipe_xml_child(xml
, "primarySMTP"));
1981 if (!is_empty(ews_url
) && !is_empty(email
)) {
1983 * Workaround for missing Office 365 buddy icons
1985 * (All?) Office 365 contact cards have the following
1986 * XML embedded as the photo URI XML node text:
1988 * <ewsUrl>https://outlook.office365.com/EWS/Exchange.asmx/WSSecurity</ewsUrl>
1989 * <primarySMTP>user@company.com</primarySMTP>
1991 * The simple HTTP request by get_user_photo_request()
1992 * is rejected with 401. But the response contains
1994 * WWW-Authenticate: Basic Realm=""
1996 * to which the HTTP transport answers with a retry
1997 * using Basic authentication. That in turn is rejected
1998 * with 500 and thus the buddy icon retrieval fails.
2000 * As a quick workaround strip the trailing "/WSSecurity"
2001 * from the URL. The HTTP request for the buddy icon
2002 * retrieval will work with this stripped URL.
2004 * @TODO: this is probably not the correct approach.
2005 * get_user_photo_request() should be updated
2006 * to support also a webticket request.
2008 gchar
*tmp
= g_strrstr(ews_url
, "/WSSecurity");
2012 data
->request
= get_user_photo_request(sipe_private
,
2023 data
->request
= sipe_http_request_get(sipe_private
,
2026 process_buddy_photo_response
,
2030 photo_response_data_finalize(sipe_private
,
2037 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
2039 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
2040 sipe_xml
*soap_body
,
2041 gpointer callback_data
)
2043 struct ms_dlx_data
*mdd
= callback_data
;
2044 gchar
*photo_rel_path
= NULL
;
2045 gchar
*photo_hash
= NULL
;
2048 const sipe_xml
*node
;
2050 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
2053 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
2055 node
= sipe_xml_twin(node
)) {
2056 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
2057 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
2059 if (!is_empty(value
)) {
2060 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
2061 g_free(photo_rel_path
);
2062 photo_rel_path
= value
;
2064 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
2076 if (sipe_private
->addressbook_uri
&& photo_rel_path
&& photo_hash
) {
2077 gchar
*photo_url
= g_strdup_printf("%s/%s",
2078 sipe_private
->addressbook_uri
, photo_rel_path
);
2079 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
2081 sipe_buddy_update_photo(sipe_private
,
2085 x_ms_webticket_header
);
2087 g_free(x_ms_webticket_header
);
2091 g_free(photo_rel_path
);
2096 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
2097 struct ms_dlx_data
*mdd
)
2102 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
2105 if (sipe_backend_uses_photo()) {
2107 /* Lync 2013 or newer: use UCS if contacts are migrated */
2108 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) &&
2109 sipe_ucs_is_migrated(sipe_private
)) {
2110 struct photo_response_data
*data
= g_new0(struct photo_response_data
, 1);
2112 data
->request
= get_user_photo_request(sipe_private
,
2114 sipe_ucs_ews_url(sipe_private
),
2115 sipe_get_no_sip_uri(uri
));
2116 photo_response_data_finalize(sipe_private
,
2119 /* there is no hash */
2122 /* Lync 2010: use [MS-DLX] */
2123 } else if (sipe_private
->dlx_uri
&&
2124 sipe_private
->addressbook_uri
) {
2125 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
2127 mdd
->search_rows
= search_rows_for_uri(uri
);
2128 mdd
->other
= g_strdup(uri
);
2129 mdd
->max_returns
= 1;
2130 mdd
->callback
= get_photo_ab_entry_response
;
2131 mdd
->failed_callback
= get_photo_ab_entry_failed
;
2132 mdd
->session
= sipe_svc_session_start();
2134 ms_dlx_webticket_request(sipe_private
, mdd
);
2139 static void buddy_refresh_photos_cb(gpointer uri
,
2140 SIPE_UNUSED_PARAMETER gpointer value
,
2141 gpointer sipe_private
)
2143 buddy_fetch_photo(sipe_private
, uri
);
2146 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
2148 g_hash_table_foreach(sipe_private
->buddies
->uri
,
2149 buddy_refresh_photos_cb
,
2153 /* Buddy menu callbacks*/
2155 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
2158 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
2160 /* 2007+ conference */
2161 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
2162 sipe_conf_add(sipe_private
, who
);
2164 /* 2005- multiparty chat */
2166 gchar
*self
= sip_uri_self(sipe_private
);
2167 struct sip_session
*session
;
2169 session
= sipe_session_add_chat(sipe_private
,
2173 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
2174 session
->chat_session
,
2175 session
->chat_session
->title
,
2179 sipe_im_invite(sipe_private
, session
, who
,
2180 NULL
, NULL
, NULL
, FALSE
);
2184 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
2187 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
2190 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2192 SIPE_BUDDY_INFO_EMAIL
);
2195 gchar
*command_line
= g_strdup_printf(
2201 " mailto:%s", email
);
2204 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
2206 g_spawn_command_line_async(command_line
, NULL
);
2207 g_free(command_line
);
2210 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
2217 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
2218 struct sipe_backend_buddy_menu
*menu
,
2219 sipe_backend_buddy buddy
,
2220 sipe_buddy_info_fields id_phone
,
2221 sipe_buddy_info_fields id_display
,
2224 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
2228 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
2232 gchar
*label
= g_strdup_printf("%s %s",
2235 (tmp
= sip_tel_uri_denormalize(phone
)));
2236 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2239 SIPE_BUDDY_MENU_MAKE_CALL
,
2250 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
2251 const gchar
*buddy_name
,
2252 struct sipe_backend_buddy_menu
*menu
)
2254 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
2255 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
2258 gchar
*self
= sip_uri_self(sipe_private
);
2260 SIPE_SESSION_FOREACH
{
2261 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
2263 struct sipe_chat_session
*chat_session
= session
->chat_session
;
2264 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
2266 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
2268 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
2272 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
2273 /* We are a conf OP */
2275 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
2276 chat_session
->title
);
2277 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2280 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
2286 /* We are a conf OP */
2288 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
2289 chat_session
->title
);
2290 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2293 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
2301 (is_conf
&& !session
->locked
)) {
2302 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
2303 chat_session
->title
);
2304 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2307 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
2313 } SIPE_SESSION_FOREACH_END
;
2316 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2319 SIPE_BUDDY_MENU_NEW_CHAT
,
2322 /* add buddy's phone numbers if we have call control */
2323 if (sip_csta_is_idle(sipe_private
)) {
2326 menu
= buddy_menu_phone(sipe_public
,
2329 SIPE_BUDDY_INFO_WORK_PHONE
,
2330 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
2333 menu
= buddy_menu_phone(sipe_public
,
2336 SIPE_BUDDY_INFO_MOBILE_PHONE
,
2337 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
2341 menu
= buddy_menu_phone(sipe_public
,
2344 SIPE_BUDDY_INFO_HOME_PHONE
,
2345 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
2349 menu
= buddy_menu_phone(sipe_public
,
2352 SIPE_BUDDY_INFO_OTHER_PHONE
,
2353 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
2357 menu
= buddy_menu_phone(sipe_public
,
2360 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
2361 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
2366 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2368 SIPE_BUDDY_INFO_EMAIL
);
2370 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2373 SIPE_BUDDY_MENU_SEND_EMAIL
,
2379 #ifdef HAVE_APPSHARE_SERVER
2381 struct sipe_media_call
*call
= sipe_media_call_find(SIPE_CORE_PRIVATE
,
2384 if (call
&& sipe_appshare_get_role(call
) == SIPE_APPSHARE_ROLE_PRESENTER
) {
2385 /* We're already presenting to this buddy. */
2387 if (sipe_core_appshare_get_remote_control(call
)) {
2388 menu
= sipe_backend_buddy_menu_add(sipe_public
, menu
,
2389 _("Take desktop control"),
2390 SIPE_BUDDY_MENU_TAKE_DESKTOP_CONTROL
,
2393 menu
= sipe_backend_buddy_menu_add(sipe_public
, menu
,
2394 _("Give desktop control"),
2395 SIPE_BUDDY_MENU_GIVE_DESKTOP_CONTROL
,
2399 menu
= sipe_backend_buddy_menu_add(sipe_public
, menu
,
2400 _("Share my desktop"),
2401 SIPE_BUDDY_MENU_SHARE_DESKTOP
,
2405 #endif // HAVE_APPSHARE_SERVER
2407 /* access level control */
2408 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
2409 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
2412 sipe_ocs2007_access_control_menu(sipe_private
,
2418 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
2420 return(g_hash_table_size(sipe_private
->buddies
->uri
));
2423 static guint
sipe_ht_hash_nick(const char *nick
)
2425 char *lc
= g_utf8_strdown(nick
, -1);
2426 guint bucket
= g_str_hash(lc
);
2432 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
2434 char *nick1_norm
= NULL
;
2435 char *nick2_norm
= NULL
;
2438 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
2439 if (nick1
== NULL
|| nick2
== NULL
||
2440 !g_utf8_validate(nick1
, -1, NULL
) ||
2441 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
2443 nick1_norm
= g_utf8_casefold(nick1
, -1);
2444 nick2_norm
= g_utf8_casefold(nick2
, -1);
2445 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
2452 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
2454 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
2455 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
2456 (GEqualFunc
) sipe_ht_equals_nick
);
2457 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
2459 sipe_private
->buddies
= buddies
;