6 * Copyright (C) 2010-2016 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-digest.h"
49 #include "sipe-group.h"
50 #include "sipe-http.h"
53 #include "sipe-ocs2005.h"
54 #include "sipe-ocs2007.h"
55 #include "sipe-schedule.h"
56 #include "sipe-session.h"
57 #include "sipe-status.h"
58 #include "sipe-subscriptions.h"
61 #include "sipe-utils.h"
62 #include "sipe-webticket.h"
67 GHashTable
*exchange_key
;
69 /* Pending photo download HTTP requests */
70 GSList
*pending_photo_requests
;
73 struct buddy_group_data
{
74 const struct sipe_group
*group
;
78 struct photo_response_data
{
81 struct sipe_http_request
*request
;
84 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
86 static void photo_response_data_free(struct photo_response_data
*data
);
88 void sipe_buddy_add_keys(struct sipe_core_private
*sipe_private
,
89 struct sipe_buddy
*buddy
,
90 const gchar
*exchange_key
,
91 const gchar
*change_key
)
94 buddy
->exchange_key
= g_strdup(exchange_key
);
95 g_hash_table_insert(sipe_private
->buddies
->exchange_key
,
100 buddy
->change_key
= g_strdup(change_key
);
103 struct sipe_buddy
*sipe_buddy_add(struct sipe_core_private
*sipe_private
,
105 const gchar
*exchange_key
,
106 const gchar
*change_key
)
108 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
109 gchar
*normalized_uri
= g_ascii_strdown(uri
, -1);
110 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
114 buddy
= g_new0(struct sipe_buddy
, 1);
115 buddy
->name
= normalized_uri
;
116 g_hash_table_insert(sipe_private
->buddies
->uri
,
120 sipe_buddy_add_keys(sipe_private
,
125 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri
);
127 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES
)) {
128 buddy
->just_added
= TRUE
;
129 sipe_subscribe_presence_single_cb(sipe_private
,
133 buddy_fetch_photo(sipe_private
, normalized_uri
);
135 normalized_uri
= NULL
; /* buddy takes ownership */
137 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri
);
138 buddy
->is_obsolete
= FALSE
;
140 g_free(normalized_uri
);
145 static gboolean
is_buddy_in_group(struct sipe_buddy
*buddy
,
149 GSList
*entry
= buddy
->groups
;
152 struct buddy_group_data
*bgd
= entry
->data
;
153 if (sipe_strequal(bgd
->group
->name
, name
)) {
154 bgd
->is_obsolete
= FALSE
;
164 void sipe_buddy_add_to_group(struct sipe_core_private
*sipe_private
,
165 struct sipe_buddy
*buddy
,
166 struct sipe_group
*group
,
169 const gchar
*uri
= buddy
->name
;
170 const gchar
*group_name
= group
->name
;
171 sipe_backend_buddy bb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
176 bb
= sipe_backend_buddy_add(SIPE_CORE_PUBLIC
,
180 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
181 uri
, alias
? alias
: "<NONE>");
185 if (!is_empty(alias
)) {
186 gchar
*old_alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
189 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri
),
191 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
,
194 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
195 uri
, old_alias
, alias
);
200 if (!is_buddy_in_group(buddy
, group_name
)) {
201 sipe_buddy_insert_group(buddy
, group
);
202 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
207 static gint
buddy_group_compare(gconstpointer a
, gconstpointer b
)
209 return(((const struct buddy_group_data
*)a
)->group
->id
-
210 ((const struct buddy_group_data
*)b
)->group
->id
);
213 void sipe_buddy_insert_group(struct sipe_buddy
*buddy
,
214 struct sipe_group
*group
)
216 struct buddy_group_data
*bgd
= g_new0(struct buddy_group_data
, 1);
220 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
226 static void buddy_group_free(gpointer data
)
231 static void buddy_group_remove(struct sipe_buddy
*buddy
,
232 struct buddy_group_data
*bgd
)
234 buddy
->groups
= g_slist_remove(buddy
->groups
, bgd
);
235 buddy_group_free(bgd
);
238 static void sipe_buddy_remove_group(struct sipe_buddy
*buddy
,
239 const struct sipe_group
*group
)
241 GSList
*entry
= buddy
->groups
;
242 struct buddy_group_data
*bgd
= NULL
;
246 if (bgd
->group
== group
)
251 buddy_group_remove(buddy
, bgd
);
254 void sipe_buddy_update_groups(struct sipe_core_private
*sipe_private
,
255 struct sipe_buddy
*buddy
,
258 const gchar
*uri
= buddy
->name
;
259 GSList
*entry
= buddy
->groups
;
262 struct buddy_group_data
*bgd
= entry
->data
;
263 const struct sipe_group
*group
= bgd
->group
;
265 /* next buddy group */
268 /* old group NOT found in new list? */
269 if (g_slist_find(new_groups
, group
) == NULL
) {
270 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
273 SIPE_DEBUG_INFO("sipe_buddy_update_groups: removing buddy %s from group '%s'",
275 /* this should never be NULL */
277 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
279 buddy_group_remove(buddy
, bgd
);
284 gchar
*sipe_buddy_groups_string(struct sipe_buddy
*buddy
)
288 /* creating array from GList, converting guint to gchar * */
289 gchar
**ids_arr
= g_new(gchar
*, g_slist_length(buddy
->groups
) + 1);
290 GSList
*entry
= buddy
->groups
;
296 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
297 ids_arr
[i
] = g_strdup_printf("%u", group
->id
);
303 string
= g_strjoinv(" ", ids_arr
);
309 void sipe_buddy_cleanup_local_list(struct sipe_core_private
*sipe_private
)
311 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
314 GSList
*entry
= buddies
;
316 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
317 g_slist_length(buddies
));
318 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
319 sipe_buddy_count(sipe_private
));
321 sipe_backend_buddy bb
= entry
->data
;
322 gchar
*bname
= sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC
,
324 gchar
*gname
= sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC
,
326 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
329 if (!is_buddy_in_group(buddy
, gname
)) {
330 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",
332 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, bb
);
341 g_slist_free(buddies
);
344 struct sipe_buddy
*sipe_buddy_find_by_uri(struct sipe_core_private
*sipe_private
,
347 if (!uri
) return(NULL
);
348 return(g_hash_table_lookup(sipe_private
->buddies
->uri
, uri
));
351 struct sipe_buddy
*sipe_buddy_find_by_exchange_key(struct sipe_core_private
*sipe_private
,
352 const gchar
*exchange_key
)
354 return(g_hash_table_lookup(sipe_private
->buddies
->exchange_key
,
358 void sipe_buddy_foreach(struct sipe_core_private
*sipe_private
,
360 gpointer callback_data
)
362 g_hash_table_foreach(sipe_private
->buddies
->uri
,
367 static void buddy_free(struct sipe_buddy
*buddy
)
371 * We are calling g_hash_table_foreach_steal(). That means that no
372 * key/value deallocation functions are called. Therefore the glib
373 * hash code does not touch the key (buddy->name) or value (buddy)
374 * of the to-be-deleted hash node at all. It follows that we
376 * - MUST free the memory for the key ourselves and
377 * - ARE allowed to do it in this function
379 * Conclusion: glib must be broken on the Windows platform if sipe
380 * crashes with SIGTRAP when closing. You'll have to live
381 * with the memory leak until this is fixed.
385 g_free(buddy
->exchange_key
);
386 g_free(buddy
->change_key
);
387 g_free(buddy
->activity
);
388 g_free(buddy
->meeting_subject
);
389 g_free(buddy
->meeting_location
);
392 g_free(buddy
->cal_start_time
);
393 g_free(buddy
->cal_free_busy_base64
);
394 g_free(buddy
->cal_free_busy
);
395 g_free(buddy
->last_non_cal_activity
);
397 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
399 g_free(buddy
->device_name
);
400 sipe_utils_slist_free_full(buddy
->groups
, buddy_group_free
);
404 static gboolean
buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key
,
406 SIPE_UNUSED_PARAMETER gpointer user_data
)
409 /* We must return TRUE as the key/value have already been deleted */
413 void sipe_buddy_free(struct sipe_core_private
*sipe_private
)
415 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
417 g_hash_table_foreach_steal(buddies
->uri
,
421 /* core is being deallocated, remove all its pending photo requests */
422 while (buddies
->pending_photo_requests
) {
423 struct photo_response_data
*data
=
424 buddies
->pending_photo_requests
->data
;
425 buddies
->pending_photo_requests
=
426 g_slist_remove(buddies
->pending_photo_requests
, data
);
427 photo_response_data_free(data
);
430 g_hash_table_destroy(buddies
->uri
);
431 g_hash_table_destroy(buddies
->exchange_key
);
433 sipe_private
->buddies
= NULL
;
436 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
438 SIPE_UNUSED_PARAMETER gpointer user_data
)
440 struct sipe_buddy
*buddy
= value
;
441 GSList
*entry
= buddy
->groups
;
443 buddy
->is_obsolete
= TRUE
;
445 ((struct buddy_group_data
*) entry
->data
)->is_obsolete
= TRUE
;
450 void sipe_buddy_update_start(struct sipe_core_private
*sipe_private
)
452 g_hash_table_foreach(sipe_private
->buddies
->uri
,
453 buddy_set_obsolete_flag
,
457 static gboolean
buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
461 struct sipe_core_private
*sipe_private
= user_data
;
462 struct sipe_buddy
*buddy
= value
;
463 const gchar
*uri
= buddy
->name
;
465 if (buddy
->is_obsolete
) {
466 /* all backend buddies in different groups */
467 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
470 GSList
*entry
= buddies
;
472 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
473 g_slist_length(buddies
),
477 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
481 g_slist_free(buddies
);
484 /* return TRUE as the key/value have already been deleted */
488 GSList
*entry
= buddy
->groups
;
491 struct buddy_group_data
*bgd
= entry
->data
;
493 /* next buddy group */
496 if (bgd
->is_obsolete
) {
497 const struct sipe_group
*group
= bgd
->group
;
498 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
501 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: removing buddy '%s' from group '%s'",
503 /* this should never be NULL */
505 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
507 buddy_group_remove(buddy
, bgd
);
514 void sipe_buddy_update_finish(struct sipe_core_private
*sipe_private
)
516 g_hash_table_foreach_remove(sipe_private
->buddies
->uri
,
517 buddy_check_obsolete_flag
,
521 gchar
*sipe_core_buddy_status(struct sipe_core_public
*sipe_public
,
524 const gchar
*status_text
)
526 struct sipe_buddy
*sbuddy
;
529 if (!sipe_public
) return NULL
; /* happens on pidgin exit */
531 sbuddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
, uri
);
532 if (!sbuddy
) return NULL
;
534 status
= g_string_new(sbuddy
->activity
? sbuddy
->activity
:
535 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
538 if (sbuddy
->is_mobile
) {
540 g_string_append(status
, " - ");
541 g_string_append(status
, _("Mobile"));
546 g_string_append(status
, " - ");
547 g_string_append(status
, sbuddy
->note
);
550 /* return NULL instead of empty status text */
551 return(g_string_free(status
, status
->len
? FALSE
: TRUE
));
554 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
557 sipe_backend_buddy pbuddy
;
559 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
560 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
565 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
567 const gchar
*old_group_name
,
568 const gchar
*new_group_name
)
570 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
571 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
573 struct sipe_group
*old_group
= NULL
;
574 struct sipe_group
*new_group
;
575 struct sipe_ucs_transaction
*ucs_trans
= NULL
;
577 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' old group '%s' new group '%s'",
579 old_group_name
? old_group_name
: "<UNDEFINED>",
580 new_group_name
? new_group_name
: "<UNDEFINED>");
583 /* buddy not in roaming list */
586 old_group
= sipe_group_find_by_name(sipe_private
, old_group_name
);
588 sipe_buddy_remove_group(buddy
, old_group
);
589 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' removed from old group '%s'",
590 who
, old_group_name
);
593 new_group
= sipe_group_find_by_name(sipe_private
, new_group_name
);
595 sipe_buddy_insert_group(buddy
, new_group
);
596 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' added to new group '%s'",
597 who
, new_group_name
);
600 if (sipe_ucs_is_migrated(sipe_private
)) {
603 ucs_trans
= sipe_ucs_transaction(sipe_private
);
607 * 1. new buddy added to existing group
608 * 2. existing buddy moved from old to existing group
610 sipe_ucs_group_add_buddy(sipe_private
,
616 sipe_ucs_group_remove_buddy(sipe_private
,
621 } else if (old_group
) {
623 * 3. existing buddy removed from one of its groups
624 * 4. existing buddy removed from last group
626 sipe_ucs_group_remove_buddy(sipe_private
,
630 if (g_slist_length(buddy
->groups
) < 1)
631 sipe_buddy_remove(sipe_private
,
633 /* buddy no longer valid */
636 /* non-UCS handling */
637 } else if (new_group
)
638 sipe_group_update_buddy(sipe_private
, buddy
);
640 /* 5. buddy added to new group */
642 sipe_group_create(sipe_private
,
648 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
650 const gchar
*group_name
)
652 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
654 if (!sipe_buddy_find_by_uri(sipe_private
, uri
))
655 sipe_buddy_add(sipe_private
,
660 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
663 sipe_core_buddy_group(sipe_public
,
669 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
670 struct sipe_buddy
*buddy
)
672 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
673 const gchar
*uri
= buddy
->name
;
674 GSList
*entry
= buddy
->groups
;
675 gchar
*action_name
= sipe_utils_presence_key(uri
);
677 sipe_schedule_cancel(sipe_private
, action_name
);
680 /* If the buddy still has groups, we need to delete backend buddies */
682 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
683 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
686 /* this should never be NULL */
688 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, oldb
);
693 g_hash_table_remove(buddies
->uri
, uri
);
694 if (buddy
->exchange_key
)
695 g_hash_table_remove(buddies
->exchange_key
,
696 buddy
->exchange_key
);
702 * Unassociates buddy from group first.
703 * Then see if no groups left, removes buddy completely.
704 * Otherwise updates buddy groups on server.
706 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
708 const gchar
*group_name
)
710 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
711 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
713 struct sipe_group
*group
= NULL
;
718 group
= sipe_group_find_by_name(sipe_private
, group_name
);
720 sipe_buddy_remove_group(buddy
, group
);
721 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy '%s' removed from group '%s'",
726 if (g_slist_length(buddy
->groups
) < 1) {
728 if (sipe_ucs_is_migrated(sipe_private
)) {
729 sipe_ucs_group_remove_buddy(sipe_private
,
734 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
736 sip_soap_request(sipe_private
,
742 sipe_buddy_remove(sipe_private
, buddy
);
744 if (sipe_ucs_is_migrated(sipe_private
)) {
745 sipe_ucs_group_remove_buddy(sipe_private
,
750 /* updates groups on server */
751 sipe_group_update_buddy(sipe_private
, buddy
);
755 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
759 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
760 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
765 /* Check if on 2005 system contact's calendar,
766 * then set/preserve it.
768 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
769 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
771 sipe_ocs2005_apply_calendar_status(sipe_private
,
773 sipe_status_activity_to_token(activity
));
777 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
779 const gchar
*status_name
,
781 struct sipe_backend_buddy_tooltip
*tooltip
)
783 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
785 gboolean is_oof_note
= FALSE
;
786 const gchar
*activity
= NULL
;
787 gchar
*calendar
= NULL
;
788 const gchar
*meeting_subject
= NULL
;
789 const gchar
*meeting_location
= NULL
;
790 gchar
*access_text
= NULL
;
792 #define SIPE_ADD_BUDDY_INFO(l, t) \
794 gchar *tmp = g_markup_escape_text((t), -1); \
795 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
798 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
799 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
801 if (sipe_public
) { /* happens on pidgin exit */
802 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
806 is_oof_note
= sbuddy
->is_oof_note
;
807 activity
= sbuddy
->activity
;
808 calendar
= sipe_cal_get_description(sbuddy
);
809 meeting_subject
= sbuddy
->meeting_subject
;
810 meeting_location
= sbuddy
->meeting_location
;
812 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
813 gboolean is_group_access
= FALSE
;
814 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
816 sipe_get_no_sip_uri(uri
),
818 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
819 access_text
= is_group_access
?
820 g_strdup(access_level
) :
821 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
827 const gchar
*status_str
= activity
? activity
: status_name
;
829 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
831 if (is_online
&& !is_empty(calendar
)) {
832 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
835 if (!is_empty(meeting_location
)) {
836 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
837 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
839 if (!is_empty(meeting_subject
)) {
840 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
841 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
844 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
845 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
846 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
848 g_free(note_italics
);
851 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
856 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
858 sipe_buddy_info_fields propkey
,
859 char *property_value
)
861 GSList
*buddies
, *entry
;
864 property_value
= g_strstrip(property_value
);
866 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
869 sipe_backend_buddy p_buddy
= entry
->data
;
871 /* for Display Name */
872 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
874 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
875 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
876 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
877 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
881 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
882 if (!is_empty(property_value
) &&
883 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
885 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
886 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
890 /* for other properties */
892 if (!is_empty(property_value
)) {
893 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
894 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
895 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
903 g_slist_free(buddies
);
912 sipe_svc_callback
*callback
;
913 struct sipe_svc_session
*session
;
914 gchar
*wsse_security
;
915 struct sipe_backend_search_token
*token
;
916 /* must call ms_dlx_free() */
917 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
918 struct ms_dlx_data
*mdd
);
921 static void free_search_rows(GSList
*search_rows
)
923 sipe_utils_slist_free_full(search_rows
, g_free
);
926 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
928 free_search_rows(mdd
->search_rows
);
929 sipe_svc_session_close(mdd
->session
);
931 g_free(mdd
->wsse_security
);
935 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
936 #define DLX_SEARCH_ITEM \
937 "<AbEntryRequest.ChangeSearchQuery>" \
938 " <SearchOn>%s</SearchOn>" \
939 " <Value>%s</Value>" \
940 "</AbEntryRequest.ChangeSearchQuery>"
942 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
943 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
952 attr
= query_rows
->data
;
953 query_rows
= g_slist_next(query_rows
);
954 value
= query_rows
->data
;
955 query_rows
= g_slist_next(query_rows
);
961 * Special value for SIP ID
963 * Active Directory seems only to be able to search for
964 * SIP URIs. Make sure search string starts with "sip:".
967 attr
= "msRTCSIP-PrimaryUserAddress";
969 value
= tmp
= sip_uri(value
);
972 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
979 query
= g_strjoinv(NULL
, attrs
);
980 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
989 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
990 const gchar
*base_uri
,
991 const gchar
*auth_uri
,
992 const gchar
*wsse_security
,
993 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
994 gpointer callback_data
)
996 struct ms_dlx_data
*mdd
= callback_data
;
999 guint length
= g_slist_length(mdd
->search_rows
);
1002 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
1006 /* complex search */
1007 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
1008 search
= g_strdup_printf("<ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
1016 search
= g_strdup_printf("<BasicSearch>"
1017 " <SearchList>c,company,displayName,givenName,mail,mailNickname,msRTCSIP-PrimaryUserAddress,sn</SearchList>"
1018 " <Value>%s</Value>"
1019 " <Verb>BeginsWith</Verb>"
1024 if (sipe_svc_ab_entry_request(sipe_private
,
1033 /* keep webticket security token for potential further use */
1034 g_free(mdd
->wsse_security
);
1035 mdd
->wsse_security
= g_strdup(wsse_security
);
1037 /* callback data passed down the line */
1043 /* no ticket: this will show the minmum information */
1044 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
1049 mdd
->failed_callback(sipe_private
, mdd
);
1052 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
1053 struct ms_dlx_data
*mdd
)
1055 if (!sipe_webticket_request_with_port(sipe_private
,
1057 sipe_private
->dlx_uri
,
1058 "AddressBookWebTicketBearer",
1061 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
1062 sipe_private
->dlx_uri
);
1063 mdd
->failed_callback(sipe_private
, mdd
);
1067 void sipe_buddy_search_contacts_finalize(struct sipe_core_private
*sipe_private
,
1068 struct sipe_backend_search_results
*results
,
1072 gchar
*secondary
= g_strdup_printf(
1073 dngettext(PACKAGE_NAME
,
1074 "Found %d contact%s:",
1075 "Found %d contacts%s:", match_count
),
1076 match_count
, more
? _(" (more matched your query)") : "");
1078 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
1085 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
1087 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1088 sipe_xml
*soap_body
,
1089 gpointer callback_data
)
1091 struct ms_dlx_data
*mdd
= callback_data
;
1094 const sipe_xml
*node
;
1095 struct sipe_backend_search_results
*results
;
1098 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
1102 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
1104 /* try again with simple search, if possible */
1105 if (mdd
->other
&& mdd
->search_rows
) {
1106 SIPE_DEBUG_INFO_NOFORMAT("search_ab_entry_response: no matches, retrying with simple search");
1108 /* throw away original search query */
1109 free_search_rows(mdd
->search_rows
);
1110 mdd
->search_rows
= NULL
;
1112 ms_dlx_webticket_request(sipe_private
, mdd
);
1114 /* callback data passed down the line */
1118 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
1120 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1122 _("No contacts found"));
1128 /* OK, we found something - show the results to the user */
1129 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1132 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
1133 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1135 _("Unable to display the search results"));
1140 /* SearchAbEntryResult can contain duplicates */
1141 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1144 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
1145 const sipe_xml
*attrs
;
1146 gchar
*sip_uri
= NULL
;
1147 gchar
*displayname
= NULL
;
1148 gchar
*company
= NULL
;
1149 gchar
*country
= NULL
;
1150 gchar
*email
= NULL
;
1152 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
1154 attrs
= sipe_xml_twin(attrs
)) {
1155 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
1157 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
1160 if (!is_empty(value
)) {
1161 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
1165 } else if (sipe_strcase_equal(name
, "displayname")) {
1166 g_free(displayname
);
1167 displayname
= value
;
1169 } else if (sipe_strcase_equal(name
, "mail")) {
1173 } else if (sipe_strcase_equal(name
, "company")) {
1177 } else if (sipe_strcase_equal(name
, "country")) {
1188 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
1189 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
1190 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1197 g_strfreev(uri_parts
);
1199 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
1206 g_free(displayname
);
1210 sipe_buddy_search_contacts_finalize(sipe_private
, results
,
1211 g_hash_table_size(found
),
1213 g_hash_table_destroy(found
);
1217 mdd
->failed_callback(sipe_private
, mdd
);
1221 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
1223 struct transaction
*trans
)
1225 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
1226 struct sipe_backend_search_results
*results
;
1227 sipe_xml
*searchResults
;
1228 const sipe_xml
*mrow
;
1229 guint match_count
= 0;
1230 gboolean more
= FALSE
;
1232 /* valid response? */
1233 if (msg
->response
!= 200) {
1234 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1236 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1238 _("Contact search failed"));
1242 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
1245 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1246 if (!searchResults
) {
1247 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1248 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1250 _("Contact search failed"));
1255 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
1257 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1258 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1260 _("No contacts found"));
1262 sipe_xml_free(searchResults
);
1266 /* OK, we found something - show the results to the user */
1267 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1268 trans
->payload
->data
);
1270 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1271 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1273 _("Unable to display the search results"));
1275 sipe_xml_free(searchResults
);
1279 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
1280 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
1281 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1284 sipe_xml_attribute(mrow
, "displayName"),
1285 sipe_xml_attribute(mrow
, "company"),
1286 sipe_xml_attribute(mrow
, "country"),
1287 sipe_xml_attribute(mrow
, "email"));
1288 g_strfreev(uri_parts
);
1292 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
1293 char *data
= sipe_xml_data(mrow
);
1294 more
= (g_ascii_strcasecmp(data
, "true") == 0);
1298 sipe_buddy_search_contacts_finalize(sipe_private
, results
, match_count
, more
);
1299 sipe_xml_free(searchResults
);
1304 static void search_soap_request(struct sipe_core_private
*sipe_private
,
1305 GDestroyNotify destroy
,
1308 SoapTransCallback callback
,
1309 GSList
*search_rows
)
1311 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
1312 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1314 payload
->destroy
= destroy
;
1315 payload
->data
= data
;
1317 sip_soap_directory_search(sipe_private
,
1325 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1326 struct ms_dlx_data
*mdd
)
1328 /* error using [MS-DLX] server, retry using Active Directory */
1329 if (mdd
->search_rows
)
1330 search_soap_request(sipe_private
,
1334 process_search_contact_response
,
1339 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
1340 struct sipe_backend_search_token
*token
,
1341 const gchar
*given_name
,
1342 const gchar
*surname
,
1345 const gchar
*company
,
1346 const gchar
*country
)
1348 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1350 /* Lync 2013 or newer: use UCS if contacts are migrated */
1351 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) &&
1352 sipe_ucs_is_migrated(sipe_private
)) {
1354 sipe_ucs_search(sipe_private
,
1364 GSList
*query_rows
= NULL
;
1366 const gchar
*simple
= NULL
;
1368 #define ADD_QUERY_ROW(attr, val) \
1370 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1371 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1376 ADD_QUERY_ROW("givenName", given_name
);
1377 ADD_QUERY_ROW("sn", surname
);
1378 ADD_QUERY_ROW("mail", email
);
1379 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1380 ADD_QUERY_ROW(NULL
, sipid
);
1381 ADD_QUERY_ROW("company", company
);
1382 ADD_QUERY_ROW("c", country
);
1385 if (sipe_private
->dlx_uri
!= NULL
) {
1386 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1388 mdd
->search_rows
= query_rows
;
1389 /* user entered only one search string, remember that one */
1391 mdd
->other
= g_strdup(simple
);
1392 mdd
->max_returns
= 100;
1393 mdd
->callback
= search_ab_entry_response
;
1394 mdd
->failed_callback
= search_ab_entry_failed
;
1395 mdd
->session
= sipe_svc_session_start();
1398 ms_dlx_webticket_request(sipe_private
, mdd
);
1401 /* no [MS-DLX] server, use Active Directory search instead */
1402 search_soap_request(sipe_private
,
1406 process_search_contact_response
,
1408 free_search_rows(query_rows
);
1411 sipe_backend_search_failed(sipe_public
,
1413 _("Invalid contact search query"));
1417 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1418 struct sipe_backend_buddy_info
*info
,
1420 const gchar
*server_alias
,
1423 sipe_backend_buddy bbuddy
;
1424 struct sipe_buddy
*sbuddy
;
1429 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1431 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1436 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1438 if (is_empty(server_alias
)) {
1439 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1442 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1444 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1448 value
= g_strdup(server_alias
);
1451 /* present alias if it differs from server alias */
1452 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1453 if (alias
&& !sipe_strequal(alias
, value
))
1455 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1457 SIPE_BUDDY_INFO_ALIAS
,
1463 if (is_empty(email
)) {
1464 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1466 SIPE_BUDDY_INFO_EMAIL
);
1468 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1470 SIPE_BUDDY_INFO_EMAIL
,
1476 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1478 SIPE_BUDDY_INFO_SITE
);
1480 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1482 SIPE_BUDDY_INFO_SITE
,
1487 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1488 if (sbuddy
&& sbuddy
->device_name
) {
1489 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1491 SIPE_BUDDY_INFO_DEVICE
,
1492 sbuddy
->device_name
);
1495 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1499 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1501 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1502 sipe_xml
*soap_body
,
1503 gpointer callback_data
)
1505 struct ms_dlx_data
*mdd
= callback_data
;
1506 struct sipe_backend_buddy_info
*info
= NULL
;
1507 gchar
*server_alias
= NULL
;
1508 gchar
*email
= NULL
;
1511 const sipe_xml
*node
;
1513 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1516 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1518 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1520 node
= sipe_xml_twin(node
)) {
1521 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1523 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1525 const sipe_xml
*values
= sipe_xml_child(node
,
1528 /* Single value entries */
1529 if (!is_empty(value
)) {
1531 if (sipe_strcase_equal(name
, "displayname")) {
1532 g_free(server_alias
);
1533 server_alias
= value
;
1535 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1537 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1539 } else if (sipe_strcase_equal(name
, "mail")) {
1543 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1545 SIPE_BUDDY_INFO_EMAIL
,
1547 } else if (sipe_strcase_equal(name
, "title")) {
1548 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1550 SIPE_BUDDY_INFO_JOB_TITLE
,
1552 } else if (sipe_strcase_equal(name
, "company")) {
1553 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1555 SIPE_BUDDY_INFO_COMPANY
,
1557 } else if (sipe_strcase_equal(name
, "country")) {
1558 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1560 SIPE_BUDDY_INFO_COUNTRY
,
1564 } else if (values
) {
1565 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1568 if (sipe_strcase_equal(name
, "telephonenumber")) {
1569 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1571 SIPE_BUDDY_INFO_WORK_PHONE
,
1583 /* this will show the minmum information */
1584 get_info_finalize(sipe_private
,
1591 g_free(server_alias
);
1595 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1597 struct transaction
*trans
)
1599 const gchar
*uri
= trans
->payload
->data
;
1600 struct sipe_backend_buddy_info
*info
= NULL
;
1601 gchar
*server_alias
= NULL
;
1602 gchar
*email
= NULL
;
1604 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1605 uri
, sipe_private
->username
);
1607 if (msg
->response
!= 200) {
1608 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1610 sipe_xml
*searchResults
;
1611 const sipe_xml
*mrow
;
1613 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1614 msg
->body
? msg
->body
: "");
1616 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1617 if (!searchResults
) {
1619 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1621 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1623 gchar
*phone_number
;
1625 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1627 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1628 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1629 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1632 * For 2007 system we will take this from ContactCard -
1633 * it has cleaner tel: URIs at least
1635 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1636 char *tel_uri
= sip_to_tel_uri(phone_number
);
1637 /* trims its parameters, so call first */
1638 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1639 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1640 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1641 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1644 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1648 if (!is_empty(server_alias
)) {
1649 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1651 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1654 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1655 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1657 SIPE_BUDDY_INFO_JOB_TITLE
,
1660 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1661 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1663 SIPE_BUDDY_INFO_OFFICE
,
1666 if (!is_empty(phone_number
)) {
1667 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1669 SIPE_BUDDY_INFO_WORK_PHONE
,
1672 g_free(phone_number
);
1673 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1674 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1676 SIPE_BUDDY_INFO_COMPANY
,
1679 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1680 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1682 SIPE_BUDDY_INFO_CITY
,
1685 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1686 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1688 SIPE_BUDDY_INFO_STATE
,
1691 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1692 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1694 SIPE_BUDDY_INFO_COUNTRY
,
1697 if (!is_empty(email
)) {
1698 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1700 SIPE_BUDDY_INFO_EMAIL
,
1704 sipe_xml_free(searchResults
);
1707 /* this will show the minmum information */
1708 get_info_finalize(sipe_private
,
1714 g_free(server_alias
);
1720 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1721 struct ms_dlx_data
*mdd
)
1723 /* error using [MS-DLX] server, retry using Active Directory */
1724 search_soap_request(sipe_private
,
1728 process_get_info_response
,
1734 static GSList
*search_rows_for_uri(const gchar
*uri
)
1736 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1737 GSList
*l
= g_slist_append(NULL
, NULL
);
1738 return(g_slist_append(l
, g_strdup(uri
)));
1741 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1744 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1745 GSList
*search_rows
= search_rows_for_uri(who
);
1747 if (sipe_private
->dlx_uri
) {
1748 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1750 mdd
->search_rows
= search_rows
;
1751 mdd
->other
= g_strdup(who
);
1752 mdd
->max_returns
= 1;
1753 mdd
->callback
= get_info_ab_entry_response
;
1754 mdd
->failed_callback
= get_info_ab_entry_failed
;
1755 mdd
->session
= sipe_svc_session_start();
1757 ms_dlx_webticket_request(sipe_private
, mdd
);
1760 /* no [MS-DLX] server, use Active Directory search instead */
1761 search_soap_request(sipe_private
,
1765 process_get_info_response
,
1767 free_search_rows(search_rows
);
1771 static void photo_response_data_free(struct photo_response_data
*data
)
1774 g_free(data
->photo_hash
);
1775 if (data
->request
) {
1776 sipe_http_request_cancel(data
->request
);
1781 static void photo_response_data_remove(struct sipe_core_private
*sipe_private
,
1782 struct photo_response_data
*data
)
1784 data
->request
= NULL
;
1785 sipe_private
->buddies
->pending_photo_requests
=
1786 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, data
);
1787 photo_response_data_free(data
);
1790 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1796 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1798 if (status
== SIPE_HTTP_STATUS_OK
) {
1799 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1802 gsize photo_size
= atoi(len_str
);
1803 gpointer photo
= g_new(char, photo_size
);
1806 memcpy(photo
, body
, photo_size
);
1808 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1817 photo_response_data_remove(sipe_private
, rdata
);
1820 static void process_get_user_photo_response(struct sipe_core_private
*sipe_private
,
1822 SIPE_UNUSED_PARAMETER GSList
*headers
,
1826 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1828 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
1829 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
1830 const sipe_xml
*node
= sipe_xml_child(xml
,
1831 "Body/GetUserPhotoResponse/PictureData");
1838 /* decode photo data */
1839 base64
= sipe_xml_data(node
);
1840 photo
= g_base64_decode(base64
, &photo_size
);
1843 /* EWS doesn't provide a hash -> calculate SHA-1 digest */
1844 if (!rdata
->photo_hash
) {
1845 guchar digest
[SIPE_DIGEST_SHA1_LENGTH
];
1846 sipe_digest_sha1(photo
, photo_size
, digest
);
1848 /* rdata takes ownership of digest string */
1849 rdata
->photo_hash
= buff_to_hex_str(digest
,
1850 SIPE_DIGEST_SHA1_LENGTH
);
1853 /* backend frees "photo" */
1854 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1864 photo_response_data_remove(sipe_private
, rdata
);
1867 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1869 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "Assertion", TRUE
);
1870 gchar
*wsse_security_base64
;
1871 gchar
*x_ms_webticket_header
;
1877 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1879 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1880 wsse_security_base64
);
1883 g_free(wsse_security_base64
);
1885 return x_ms_webticket_header
;
1888 /* see also sipe_ucs_http_request() */
1889 static struct sipe_http_request
*get_user_photo_request(struct sipe_core_private
*sipe_private
,
1890 struct photo_response_data
*data
,
1891 const gchar
*ews_url
,
1894 gchar
*soap
= g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
1896 " xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\""
1897 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
1898 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\""
1901 " <t:RequestServerVersion Version=\"Exchange2013\" />"
1905 " <m:Email>%s</m:Email>"
1906 " <m:SizeRequested>HR48x48</m:SizeRequested>"
1907 " </m:GetUserPhoto>"
1911 struct sipe_http_request
*request
= sipe_http_request_post(sipe_private
,
1915 "text/xml; charset=UTF-8",
1916 process_get_user_photo_response
,
1921 sipe_core_email_authentication(sipe_private
,
1923 sipe_http_request_allow_redirect(request
);
1925 SIPE_DEBUG_ERROR_NOFORMAT("get_user_photo_request: failed to create HTTP connection");
1931 static void photo_response_data_finalize(struct sipe_core_private
*sipe_private
,
1932 struct photo_response_data
*data
,
1934 const gchar
*photo_hash
)
1936 if (data
->request
) {
1937 data
->who
= g_strdup(uri
);
1938 data
->photo_hash
= g_strdup(photo_hash
);
1940 sipe_private
->buddies
->pending_photo_requests
=
1941 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1942 sipe_http_request_ready(data
->request
);
1944 photo_response_data_free(data
);
1948 void sipe_buddy_update_photo(struct sipe_core_private
*sipe_private
,
1950 const gchar
*photo_hash
,
1951 const gchar
*photo_url
,
1952 const gchar
*headers
)
1954 const gchar
*photo_hash_old
=
1955 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, uri
);
1957 if (!sipe_strequal(photo_hash
, photo_hash_old
)) {
1958 struct photo_response_data
*data
= g_new0(struct photo_response_data
, 1);
1960 SIPE_DEBUG_INFO("sipe_buddy_update_photo: who '%s' url '%s' hash '%s'",
1961 uri
, photo_url
, photo_hash
);
1963 /* Photo URL is embedded XML? */
1964 if (g_str_has_prefix(photo_url
, "<") &&
1965 g_str_has_suffix(photo_url
, ">")) {
1966 /* add dummy root to embedded XML string */
1967 gchar
*tmp
= g_strdup_printf("<r>%s</r>", photo_url
);
1968 sipe_xml
*xml
= sipe_xml_parse(tmp
, strlen(tmp
));
1972 gchar
*ews_url
= sipe_xml_data(sipe_xml_child(xml
, "ewsUrl"));
1973 gchar
*email
= sipe_xml_data(sipe_xml_child(xml
, "primarySMTP"));
1975 if (!is_empty(ews_url
) && !is_empty(email
))
1976 data
->request
= get_user_photo_request(sipe_private
,
1986 data
->request
= sipe_http_request_get(sipe_private
,
1989 process_buddy_photo_response
,
1993 photo_response_data_finalize(sipe_private
,
2000 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
2002 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
2003 sipe_xml
*soap_body
,
2004 gpointer callback_data
)
2006 struct ms_dlx_data
*mdd
= callback_data
;
2007 gchar
*photo_rel_path
= NULL
;
2008 gchar
*photo_hash
= NULL
;
2011 const sipe_xml
*node
;
2013 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
2016 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
2018 node
= sipe_xml_twin(node
)) {
2019 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
2020 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
2022 if (!is_empty(value
)) {
2023 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
2024 g_free(photo_rel_path
);
2025 photo_rel_path
= value
;
2027 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
2039 if (sipe_private
->addressbook_uri
&& photo_rel_path
&& photo_hash
) {
2040 gchar
*photo_url
= g_strdup_printf("%s/%s",
2041 sipe_private
->addressbook_uri
, photo_rel_path
);
2042 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
2044 sipe_buddy_update_photo(sipe_private
,
2048 x_ms_webticket_header
);
2050 g_free(x_ms_webticket_header
);
2054 g_free(photo_rel_path
);
2059 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
2060 struct ms_dlx_data
*mdd
)
2065 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
2068 if (sipe_backend_uses_photo()) {
2070 /* Lync 2013 or newer: use UCS if contacts are migrated */
2071 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) &&
2072 sipe_ucs_is_migrated(sipe_private
)) {
2073 struct photo_response_data
*data
= g_new0(struct photo_response_data
, 1);
2075 data
->request
= get_user_photo_request(sipe_private
,
2077 sipe_ucs_ews_url(sipe_private
),
2078 sipe_get_no_sip_uri(uri
));
2079 photo_response_data_finalize(sipe_private
,
2082 /* there is no hash */
2085 /* Lync 2010: use [MS-DLX] */
2086 } else if (sipe_private
->dlx_uri
&&
2087 sipe_private
->addressbook_uri
) {
2088 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
2090 mdd
->search_rows
= search_rows_for_uri(uri
);
2091 mdd
->other
= g_strdup(uri
);
2092 mdd
->max_returns
= 1;
2093 mdd
->callback
= get_photo_ab_entry_response
;
2094 mdd
->failed_callback
= get_photo_ab_entry_failed
;
2095 mdd
->session
= sipe_svc_session_start();
2097 ms_dlx_webticket_request(sipe_private
, mdd
);
2102 static void buddy_refresh_photos_cb(gpointer uri
,
2103 SIPE_UNUSED_PARAMETER gpointer value
,
2104 gpointer sipe_private
)
2106 buddy_fetch_photo(sipe_private
, uri
);
2109 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
2111 g_hash_table_foreach(sipe_private
->buddies
->uri
,
2112 buddy_refresh_photos_cb
,
2116 /* Buddy menu callbacks*/
2118 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
2121 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
2123 /* 2007+ conference */
2124 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
2125 sipe_conf_add(sipe_private
, who
);
2127 /* 2005- multiparty chat */
2129 gchar
*self
= sip_uri_self(sipe_private
);
2130 struct sip_session
*session
;
2132 session
= sipe_session_add_chat(sipe_private
,
2136 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
2137 session
->chat_session
,
2138 session
->chat_session
->title
,
2142 sipe_im_invite(sipe_private
, session
, who
,
2143 NULL
, NULL
, NULL
, FALSE
);
2147 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
2150 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
2153 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2155 SIPE_BUDDY_INFO_EMAIL
);
2158 gchar
*command_line
= g_strdup_printf(
2164 " mailto:%s", email
);
2167 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
2169 g_spawn_command_line_async(command_line
, NULL
);
2170 g_free(command_line
);
2173 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
2180 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
2181 struct sipe_backend_buddy_menu
*menu
,
2182 sipe_backend_buddy buddy
,
2183 sipe_buddy_info_fields id_phone
,
2184 sipe_buddy_info_fields id_display
,
2187 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
2191 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
2195 gchar
*label
= g_strdup_printf("%s %s",
2198 (tmp
= sip_tel_uri_denormalize(phone
)));
2199 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2202 SIPE_BUDDY_MENU_MAKE_CALL
,
2213 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
2214 const gchar
*buddy_name
,
2215 struct sipe_backend_buddy_menu
*menu
)
2217 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
2218 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
2221 gchar
*self
= sip_uri_self(sipe_private
);
2223 SIPE_SESSION_FOREACH
{
2224 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
2226 struct sipe_chat_session
*chat_session
= session
->chat_session
;
2227 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
2229 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
2231 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
2235 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
2236 /* We are a conf OP */
2238 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
2239 chat_session
->title
);
2240 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2243 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
2249 /* We are a conf OP */
2251 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
2252 chat_session
->title
);
2253 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2256 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
2264 (is_conf
&& !session
->locked
)) {
2265 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
2266 chat_session
->title
);
2267 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2270 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
2276 } SIPE_SESSION_FOREACH_END
;
2279 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2282 SIPE_BUDDY_MENU_NEW_CHAT
,
2285 /* add buddy's phone numbers if we have call control */
2286 if (sip_csta_is_idle(sipe_private
)) {
2289 menu
= buddy_menu_phone(sipe_public
,
2292 SIPE_BUDDY_INFO_WORK_PHONE
,
2293 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
2296 menu
= buddy_menu_phone(sipe_public
,
2299 SIPE_BUDDY_INFO_MOBILE_PHONE
,
2300 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
2304 menu
= buddy_menu_phone(sipe_public
,
2307 SIPE_BUDDY_INFO_HOME_PHONE
,
2308 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
2312 menu
= buddy_menu_phone(sipe_public
,
2315 SIPE_BUDDY_INFO_OTHER_PHONE
,
2316 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
2320 menu
= buddy_menu_phone(sipe_public
,
2323 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
2324 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
2329 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2331 SIPE_BUDDY_INFO_EMAIL
);
2333 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2336 SIPE_BUDDY_MENU_SEND_EMAIL
,
2342 /* access level control */
2343 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
2344 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
2347 sipe_ocs2007_access_control_menu(sipe_private
,
2353 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
2355 return(g_hash_table_size(sipe_private
->buddies
->uri
));
2358 static guint
sipe_ht_hash_nick(const char *nick
)
2360 char *lc
= g_utf8_strdown(nick
, -1);
2361 guint bucket
= g_str_hash(lc
);
2367 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
2369 char *nick1_norm
= NULL
;
2370 char *nick2_norm
= NULL
;
2373 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
2374 if (nick1
== NULL
|| nick2
== NULL
||
2375 !g_utf8_validate(nick1
, -1, NULL
) ||
2376 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
2378 nick1_norm
= g_utf8_casefold(nick1
, -1);
2379 nick2_norm
= g_utf8_casefold(nick2
, -1);
2380 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
2387 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
2389 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
2390 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
2391 (GEqualFunc
) sipe_ht_equals_nick
);
2392 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
2394 sipe_private
->buddies
= buddies
;