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 return(g_hash_table_lookup(sipe_private
->buddies
->uri
, uri
));
350 struct sipe_buddy
*sipe_buddy_find_by_exchange_key(struct sipe_core_private
*sipe_private
,
351 const gchar
*exchange_key
)
353 return(g_hash_table_lookup(sipe_private
->buddies
->exchange_key
,
357 void sipe_buddy_foreach(struct sipe_core_private
*sipe_private
,
359 gpointer callback_data
)
361 g_hash_table_foreach(sipe_private
->buddies
->uri
,
366 static void buddy_free(struct sipe_buddy
*buddy
)
370 * We are calling g_hash_table_foreach_steal(). That means that no
371 * key/value deallocation functions are called. Therefore the glib
372 * hash code does not touch the key (buddy->name) or value (buddy)
373 * of the to-be-deleted hash node at all. It follows that we
375 * - MUST free the memory for the key ourselves and
376 * - ARE allowed to do it in this function
378 * Conclusion: glib must be broken on the Windows platform if sipe
379 * crashes with SIGTRAP when closing. You'll have to live
380 * with the memory leak until this is fixed.
384 g_free(buddy
->exchange_key
);
385 g_free(buddy
->change_key
);
386 g_free(buddy
->activity
);
387 g_free(buddy
->meeting_subject
);
388 g_free(buddy
->meeting_location
);
391 g_free(buddy
->cal_start_time
);
392 g_free(buddy
->cal_free_busy_base64
);
393 g_free(buddy
->cal_free_busy
);
394 g_free(buddy
->last_non_cal_activity
);
396 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
398 g_free(buddy
->device_name
);
399 sipe_utils_slist_free_full(buddy
->groups
, buddy_group_free
);
403 static gboolean
buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key
,
405 SIPE_UNUSED_PARAMETER gpointer user_data
)
408 /* We must return TRUE as the key/value have already been deleted */
412 void sipe_buddy_free(struct sipe_core_private
*sipe_private
)
414 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
416 g_hash_table_foreach_steal(buddies
->uri
,
420 /* core is being deallocated, remove all its pending photo requests */
421 while (buddies
->pending_photo_requests
) {
422 struct photo_response_data
*data
=
423 buddies
->pending_photo_requests
->data
;
424 buddies
->pending_photo_requests
=
425 g_slist_remove(buddies
->pending_photo_requests
, data
);
426 photo_response_data_free(data
);
429 g_hash_table_destroy(buddies
->uri
);
430 g_hash_table_destroy(buddies
->exchange_key
);
432 sipe_private
->buddies
= NULL
;
435 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
437 SIPE_UNUSED_PARAMETER gpointer user_data
)
439 struct sipe_buddy
*buddy
= value
;
440 GSList
*entry
= buddy
->groups
;
442 buddy
->is_obsolete
= TRUE
;
444 ((struct buddy_group_data
*) entry
->data
)->is_obsolete
= TRUE
;
449 void sipe_buddy_update_start(struct sipe_core_private
*sipe_private
)
451 g_hash_table_foreach(sipe_private
->buddies
->uri
,
452 buddy_set_obsolete_flag
,
456 static gboolean
buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
460 struct sipe_core_private
*sipe_private
= user_data
;
461 struct sipe_buddy
*buddy
= value
;
462 const gchar
*uri
= buddy
->name
;
464 if (buddy
->is_obsolete
) {
465 /* all backend buddies in different groups */
466 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
469 GSList
*entry
= buddies
;
471 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
472 g_slist_length(buddies
),
476 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
480 g_slist_free(buddies
);
483 /* return TRUE as the key/value have already been deleted */
487 GSList
*entry
= buddy
->groups
;
490 struct buddy_group_data
*bgd
= entry
->data
;
492 /* next buddy group */
495 if (bgd
->is_obsolete
) {
496 const struct sipe_group
*group
= bgd
->group
;
497 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
500 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: removing buddy '%s' from group '%s'",
502 /* this should never be NULL */
504 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
506 buddy_group_remove(buddy
, bgd
);
513 void sipe_buddy_update_finish(struct sipe_core_private
*sipe_private
)
515 g_hash_table_foreach_remove(sipe_private
->buddies
->uri
,
516 buddy_check_obsolete_flag
,
520 gchar
*sipe_core_buddy_status(struct sipe_core_public
*sipe_public
,
523 const gchar
*status_text
)
525 struct sipe_buddy
*sbuddy
;
528 if (!sipe_public
) return NULL
; /* happens on pidgin exit */
530 sbuddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
, uri
);
531 if (!sbuddy
) return NULL
;
533 status
= g_string_new(sbuddy
->activity
? sbuddy
->activity
:
534 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
537 if (sbuddy
->is_mobile
) {
539 g_string_append(status
, " - ");
540 g_string_append(status
, _("Mobile"));
545 g_string_append(status
, " - ");
546 g_string_append(status
, sbuddy
->note
);
549 /* return NULL instead of empty status text */
550 return(g_string_free(status
, status
->len
? FALSE
: TRUE
));
553 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
556 sipe_backend_buddy pbuddy
;
558 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
559 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
564 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
566 const gchar
*old_group_name
,
567 const gchar
*new_group_name
)
569 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
570 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
572 struct sipe_group
*old_group
= NULL
;
573 struct sipe_group
*new_group
;
574 struct sipe_ucs_transaction
*ucs_trans
= NULL
;
576 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' old group '%s' new group '%s'",
578 old_group_name
? old_group_name
: "<UNDEFINED>",
579 new_group_name
? new_group_name
: "<UNDEFINED>");
582 /* buddy not in roaming list */
585 old_group
= sipe_group_find_by_name(sipe_private
, old_group_name
);
587 sipe_buddy_remove_group(buddy
, old_group
);
588 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' removed from old group '%s'",
589 who
, old_group_name
);
592 new_group
= sipe_group_find_by_name(sipe_private
, new_group_name
);
594 sipe_buddy_insert_group(buddy
, new_group
);
595 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' added to new group '%s'",
596 who
, new_group_name
);
599 if (sipe_ucs_is_migrated(sipe_private
)) {
602 ucs_trans
= sipe_ucs_transaction(sipe_private
);
606 * 1. new buddy added to existing group
607 * 2. existing buddy moved from old to existing group
609 sipe_ucs_group_add_buddy(sipe_private
,
615 sipe_ucs_group_remove_buddy(sipe_private
,
620 } else if (old_group
) {
622 * 3. existing buddy removed from one of its groups
623 * 4. existing buddy removed from last group
625 sipe_ucs_group_remove_buddy(sipe_private
,
629 if (g_slist_length(buddy
->groups
) < 1)
630 sipe_buddy_remove(sipe_private
,
632 /* buddy no longer valid */
635 /* non-UCS handling */
636 } else if (new_group
)
637 sipe_group_update_buddy(sipe_private
, buddy
);
639 /* 5. buddy added to new group */
641 sipe_group_create(sipe_private
,
647 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
649 const gchar
*group_name
)
651 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
653 if (!sipe_buddy_find_by_uri(sipe_private
, uri
))
654 sipe_buddy_add(sipe_private
,
659 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
662 sipe_core_buddy_group(sipe_public
,
668 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
669 struct sipe_buddy
*buddy
)
671 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
672 const gchar
*uri
= buddy
->name
;
673 GSList
*entry
= buddy
->groups
;
674 gchar
*action_name
= sipe_utils_presence_key(uri
);
676 sipe_schedule_cancel(sipe_private
, action_name
);
679 /* If the buddy still has groups, we need to delete backend buddies */
681 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
682 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
685 /* this should never be NULL */
687 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, oldb
);
692 g_hash_table_remove(buddies
->uri
, uri
);
693 if (buddy
->exchange_key
)
694 g_hash_table_remove(buddies
->exchange_key
,
695 buddy
->exchange_key
);
701 * Unassociates buddy from group first.
702 * Then see if no groups left, removes buddy completely.
703 * Otherwise updates buddy groups on server.
705 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
707 const gchar
*group_name
)
709 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
710 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
712 struct sipe_group
*group
= NULL
;
717 group
= sipe_group_find_by_name(sipe_private
, group_name
);
719 sipe_buddy_remove_group(buddy
, group
);
720 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy '%s' removed from group '%s'",
725 if (g_slist_length(buddy
->groups
) < 1) {
727 if (sipe_ucs_is_migrated(sipe_private
)) {
728 sipe_ucs_group_remove_buddy(sipe_private
,
733 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
735 sip_soap_request(sipe_private
,
741 sipe_buddy_remove(sipe_private
, buddy
);
743 if (sipe_ucs_is_migrated(sipe_private
)) {
744 sipe_ucs_group_remove_buddy(sipe_private
,
749 /* updates groups on server */
750 sipe_group_update_buddy(sipe_private
, buddy
);
754 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
758 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
759 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
764 /* Check if on 2005 system contact's calendar,
765 * then set/preserve it.
767 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
768 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
770 sipe_ocs2005_apply_calendar_status(sipe_private
,
772 sipe_status_activity_to_token(activity
));
776 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
778 const gchar
*status_name
,
780 struct sipe_backend_buddy_tooltip
*tooltip
)
782 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
784 gboolean is_oof_note
= FALSE
;
785 const gchar
*activity
= NULL
;
786 gchar
*calendar
= NULL
;
787 const gchar
*meeting_subject
= NULL
;
788 const gchar
*meeting_location
= NULL
;
789 gchar
*access_text
= NULL
;
791 #define SIPE_ADD_BUDDY_INFO(l, t) \
793 gchar *tmp = g_markup_escape_text((t), -1); \
794 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
797 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
798 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
800 if (sipe_public
) { /* happens on pidgin exit */
801 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
805 is_oof_note
= sbuddy
->is_oof_note
;
806 activity
= sbuddy
->activity
;
807 calendar
= sipe_cal_get_description(sbuddy
);
808 meeting_subject
= sbuddy
->meeting_subject
;
809 meeting_location
= sbuddy
->meeting_location
;
811 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
812 gboolean is_group_access
= FALSE
;
813 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
815 sipe_get_no_sip_uri(uri
),
817 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
818 access_text
= is_group_access
?
819 g_strdup(access_level
) :
820 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
826 const gchar
*status_str
= activity
? activity
: status_name
;
828 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
830 if (is_online
&& !is_empty(calendar
)) {
831 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
834 if (!is_empty(meeting_location
)) {
835 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
836 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
838 if (!is_empty(meeting_subject
)) {
839 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
840 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
843 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
844 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
845 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
847 g_free(note_italics
);
850 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
855 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
857 sipe_buddy_info_fields propkey
,
858 char *property_value
)
860 GSList
*buddies
, *entry
;
863 property_value
= g_strstrip(property_value
);
865 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
868 sipe_backend_buddy p_buddy
= entry
->data
;
870 /* for Display Name */
871 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
873 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
874 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
875 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
876 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
880 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
881 if (!is_empty(property_value
) &&
882 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
884 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
885 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
889 /* for other properties */
891 if (!is_empty(property_value
)) {
892 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
893 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
894 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
902 g_slist_free(buddies
);
911 sipe_svc_callback
*callback
;
912 struct sipe_svc_session
*session
;
913 gchar
*wsse_security
;
914 struct sipe_backend_search_token
*token
;
915 /* must call ms_dlx_free() */
916 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
917 struct ms_dlx_data
*mdd
);
920 static void free_search_rows(GSList
*search_rows
)
922 sipe_utils_slist_free_full(search_rows
, g_free
);
925 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
927 free_search_rows(mdd
->search_rows
);
928 sipe_svc_session_close(mdd
->session
);
930 g_free(mdd
->wsse_security
);
934 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
935 #define DLX_SEARCH_ITEM \
936 "<AbEntryRequest.ChangeSearchQuery>" \
937 " <SearchOn>%s</SearchOn>" \
938 " <Value>%s</Value>" \
939 "</AbEntryRequest.ChangeSearchQuery>"
941 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
942 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
951 attr
= query_rows
->data
;
952 query_rows
= g_slist_next(query_rows
);
953 value
= query_rows
->data
;
954 query_rows
= g_slist_next(query_rows
);
960 * Special value for SIP ID
962 * Active Directory seems only to be able to search for
963 * SIP URIs. Make sure search string starts with "sip:".
966 attr
= "msRTCSIP-PrimaryUserAddress";
968 value
= tmp
= sip_uri(value
);
971 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
978 query
= g_strjoinv(NULL
, attrs
);
979 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
988 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
989 const gchar
*base_uri
,
990 const gchar
*auth_uri
,
991 const gchar
*wsse_security
,
992 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
993 gpointer callback_data
)
995 struct ms_dlx_data
*mdd
= callback_data
;
998 guint length
= g_slist_length(mdd
->search_rows
);
1001 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
1005 /* complex search */
1006 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
1007 search
= g_strdup_printf("<ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
1015 search
= g_strdup_printf("<BasicSearch>"
1016 " <SearchList>c,company,displayName,givenName,mail,mailNickname,msRTCSIP-PrimaryUserAddress,sn</SearchList>"
1017 " <Value>%s</Value>"
1018 " <Verb>BeginsWith</Verb>"
1023 if (sipe_svc_ab_entry_request(sipe_private
,
1032 /* keep webticket security token for potential further use */
1033 g_free(mdd
->wsse_security
);
1034 mdd
->wsse_security
= g_strdup(wsse_security
);
1036 /* callback data passed down the line */
1042 /* no ticket: this will show the minmum information */
1043 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
1048 mdd
->failed_callback(sipe_private
, mdd
);
1051 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
1052 struct ms_dlx_data
*mdd
)
1054 if (!sipe_webticket_request(sipe_private
,
1056 sipe_private
->dlx_uri
,
1057 "AddressBookWebTicketBearer",
1060 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
1061 sipe_private
->dlx_uri
);
1062 mdd
->failed_callback(sipe_private
, mdd
);
1066 void sipe_buddy_search_contacts_finalize(struct sipe_core_private
*sipe_private
,
1067 struct sipe_backend_search_results
*results
,
1071 gchar
*secondary
= g_strdup_printf(
1072 dngettext(PACKAGE_NAME
,
1073 "Found %d contact%s:",
1074 "Found %d contacts%s:", match_count
),
1075 match_count
, more
? _(" (more matched your query)") : "");
1077 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
1084 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
1086 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1087 sipe_xml
*soap_body
,
1088 gpointer callback_data
)
1090 struct ms_dlx_data
*mdd
= callback_data
;
1093 const sipe_xml
*node
;
1094 struct sipe_backend_search_results
*results
;
1097 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
1101 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
1103 /* try again with simple search, if possible */
1104 if (mdd
->other
&& mdd
->search_rows
) {
1105 SIPE_DEBUG_INFO_NOFORMAT("search_ab_entry_response: no matches, retrying with simple search");
1107 /* throw away original search query */
1108 free_search_rows(mdd
->search_rows
);
1109 mdd
->search_rows
= NULL
;
1111 ms_dlx_webticket_request(sipe_private
, mdd
);
1113 /* callback data passed down the line */
1117 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
1119 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1121 _("No contacts found"));
1127 /* OK, we found something - show the results to the user */
1128 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1131 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
1132 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1134 _("Unable to display the search results"));
1139 /* SearchAbEntryResult can contain duplicates */
1140 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1143 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
1144 const sipe_xml
*attrs
;
1145 gchar
*sip_uri
= NULL
;
1146 gchar
*displayname
= NULL
;
1147 gchar
*company
= NULL
;
1148 gchar
*country
= NULL
;
1149 gchar
*email
= NULL
;
1151 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
1153 attrs
= sipe_xml_twin(attrs
)) {
1154 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
1156 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
1159 if (!is_empty(value
)) {
1160 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
1164 } else if (sipe_strcase_equal(name
, "displayname")) {
1165 g_free(displayname
);
1166 displayname
= value
;
1168 } else if (sipe_strcase_equal(name
, "mail")) {
1172 } else if (sipe_strcase_equal(name
, "company")) {
1176 } else if (sipe_strcase_equal(name
, "country")) {
1187 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
1188 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
1189 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1196 g_strfreev(uri_parts
);
1198 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
1205 g_free(displayname
);
1209 sipe_buddy_search_contacts_finalize(sipe_private
, results
,
1210 g_hash_table_size(found
),
1212 g_hash_table_destroy(found
);
1216 mdd
->failed_callback(sipe_private
, mdd
);
1220 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
1222 struct transaction
*trans
)
1224 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
1225 struct sipe_backend_search_results
*results
;
1226 sipe_xml
*searchResults
;
1227 const sipe_xml
*mrow
;
1228 guint match_count
= 0;
1229 gboolean more
= FALSE
;
1231 /* valid response? */
1232 if (msg
->response
!= 200) {
1233 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1235 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1237 _("Contact search failed"));
1241 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
1244 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1245 if (!searchResults
) {
1246 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1247 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1249 _("Contact search failed"));
1254 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
1256 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1257 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1259 _("No contacts found"));
1261 sipe_xml_free(searchResults
);
1265 /* OK, we found something - show the results to the user */
1266 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1267 trans
->payload
->data
);
1269 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1270 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1272 _("Unable to display the search results"));
1274 sipe_xml_free(searchResults
);
1278 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
1279 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
1280 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1283 sipe_xml_attribute(mrow
, "displayName"),
1284 sipe_xml_attribute(mrow
, "company"),
1285 sipe_xml_attribute(mrow
, "country"),
1286 sipe_xml_attribute(mrow
, "email"));
1287 g_strfreev(uri_parts
);
1291 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
1292 char *data
= sipe_xml_data(mrow
);
1293 more
= (g_ascii_strcasecmp(data
, "true") == 0);
1297 sipe_buddy_search_contacts_finalize(sipe_private
, results
, match_count
, more
);
1298 sipe_xml_free(searchResults
);
1303 static void search_soap_request(struct sipe_core_private
*sipe_private
,
1304 GDestroyNotify destroy
,
1307 SoapTransCallback callback
,
1308 GSList
*search_rows
)
1310 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
1311 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1313 payload
->destroy
= destroy
;
1314 payload
->data
= data
;
1316 sip_soap_directory_search(sipe_private
,
1324 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1325 struct ms_dlx_data
*mdd
)
1327 /* error using [MS-DLX] server, retry using Active Directory */
1328 if (mdd
->search_rows
)
1329 search_soap_request(sipe_private
,
1333 process_search_contact_response
,
1338 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
1339 struct sipe_backend_search_token
*token
,
1340 const gchar
*given_name
,
1341 const gchar
*surname
,
1344 const gchar
*company
,
1345 const gchar
*country
)
1347 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1349 /* Lync 2013 or newer: use UCS if contacts are migrated */
1350 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) &&
1351 sipe_ucs_is_migrated(sipe_private
)) {
1353 sipe_ucs_search(sipe_private
,
1363 GSList
*query_rows
= NULL
;
1365 const gchar
*simple
= NULL
;
1367 #define ADD_QUERY_ROW(attr, val) \
1369 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1370 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1375 ADD_QUERY_ROW("givenName", given_name
);
1376 ADD_QUERY_ROW("sn", surname
);
1377 ADD_QUERY_ROW("mail", email
);
1378 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1379 ADD_QUERY_ROW(NULL
, sipid
);
1380 ADD_QUERY_ROW("company", company
);
1381 ADD_QUERY_ROW("c", country
);
1384 if (sipe_private
->dlx_uri
!= NULL
) {
1385 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1387 mdd
->search_rows
= query_rows
;
1388 /* user entered only one search string, remember that one */
1390 mdd
->other
= g_strdup(simple
);
1391 mdd
->max_returns
= 100;
1392 mdd
->callback
= search_ab_entry_response
;
1393 mdd
->failed_callback
= search_ab_entry_failed
;
1394 mdd
->session
= sipe_svc_session_start();
1397 ms_dlx_webticket_request(sipe_private
, mdd
);
1400 /* no [MS-DLX] server, use Active Directory search instead */
1401 search_soap_request(sipe_private
,
1405 process_search_contact_response
,
1407 free_search_rows(query_rows
);
1410 sipe_backend_search_failed(sipe_public
,
1412 _("Invalid contact search query"));
1416 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1417 struct sipe_backend_buddy_info
*info
,
1419 const gchar
*server_alias
,
1422 sipe_backend_buddy bbuddy
;
1423 struct sipe_buddy
*sbuddy
;
1428 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1430 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1435 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1437 if (is_empty(server_alias
)) {
1438 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1441 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1443 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1447 value
= g_strdup(server_alias
);
1450 /* present alias if it differs from server alias */
1451 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1452 if (alias
&& !sipe_strequal(alias
, value
))
1454 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1456 SIPE_BUDDY_INFO_ALIAS
,
1462 if (is_empty(email
)) {
1463 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1465 SIPE_BUDDY_INFO_EMAIL
);
1467 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1469 SIPE_BUDDY_INFO_EMAIL
,
1475 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1477 SIPE_BUDDY_INFO_SITE
);
1479 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1481 SIPE_BUDDY_INFO_SITE
,
1486 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1487 if (sbuddy
&& sbuddy
->device_name
) {
1488 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1490 SIPE_BUDDY_INFO_DEVICE
,
1491 sbuddy
->device_name
);
1494 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1498 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1500 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1501 sipe_xml
*soap_body
,
1502 gpointer callback_data
)
1504 struct ms_dlx_data
*mdd
= callback_data
;
1505 struct sipe_backend_buddy_info
*info
= NULL
;
1506 gchar
*server_alias
= NULL
;
1507 gchar
*email
= NULL
;
1510 const sipe_xml
*node
;
1512 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1515 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1517 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1519 node
= sipe_xml_twin(node
)) {
1520 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1522 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1524 const sipe_xml
*values
= sipe_xml_child(node
,
1527 /* Single value entries */
1528 if (!is_empty(value
)) {
1530 if (sipe_strcase_equal(name
, "displayname")) {
1531 g_free(server_alias
);
1532 server_alias
= value
;
1534 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1536 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1538 } else if (sipe_strcase_equal(name
, "mail")) {
1542 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1544 SIPE_BUDDY_INFO_EMAIL
,
1546 } else if (sipe_strcase_equal(name
, "title")) {
1547 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1549 SIPE_BUDDY_INFO_JOB_TITLE
,
1551 } else if (sipe_strcase_equal(name
, "company")) {
1552 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1554 SIPE_BUDDY_INFO_COMPANY
,
1556 } else if (sipe_strcase_equal(name
, "country")) {
1557 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1559 SIPE_BUDDY_INFO_COUNTRY
,
1563 } else if (values
) {
1564 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1567 if (sipe_strcase_equal(name
, "telephonenumber")) {
1568 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1570 SIPE_BUDDY_INFO_WORK_PHONE
,
1582 /* this will show the minmum information */
1583 get_info_finalize(sipe_private
,
1590 g_free(server_alias
);
1594 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1596 struct transaction
*trans
)
1598 const gchar
*uri
= trans
->payload
->data
;
1599 struct sipe_backend_buddy_info
*info
= NULL
;
1600 gchar
*server_alias
= NULL
;
1601 gchar
*email
= NULL
;
1603 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1604 uri
, sipe_private
->username
);
1606 if (msg
->response
!= 200) {
1607 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1609 sipe_xml
*searchResults
;
1610 const sipe_xml
*mrow
;
1612 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1613 msg
->body
? msg
->body
: "");
1615 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1616 if (!searchResults
) {
1618 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1620 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1622 gchar
*phone_number
;
1624 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1626 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1627 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1628 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1631 * For 2007 system we will take this from ContactCard -
1632 * it has cleaner tel: URIs at least
1634 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1635 char *tel_uri
= sip_to_tel_uri(phone_number
);
1636 /* trims its parameters, so call first */
1637 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1638 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1639 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1640 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1643 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1647 if (!is_empty(server_alias
)) {
1648 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1650 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1653 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1654 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1656 SIPE_BUDDY_INFO_JOB_TITLE
,
1659 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1660 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1662 SIPE_BUDDY_INFO_OFFICE
,
1665 if (!is_empty(phone_number
)) {
1666 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1668 SIPE_BUDDY_INFO_WORK_PHONE
,
1671 g_free(phone_number
);
1672 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1673 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1675 SIPE_BUDDY_INFO_COMPANY
,
1678 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1679 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1681 SIPE_BUDDY_INFO_CITY
,
1684 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1685 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1687 SIPE_BUDDY_INFO_STATE
,
1690 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1691 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1693 SIPE_BUDDY_INFO_COUNTRY
,
1696 if (!is_empty(email
)) {
1697 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1699 SIPE_BUDDY_INFO_EMAIL
,
1703 sipe_xml_free(searchResults
);
1706 /* this will show the minmum information */
1707 get_info_finalize(sipe_private
,
1713 g_free(server_alias
);
1719 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1720 struct ms_dlx_data
*mdd
)
1722 /* error using [MS-DLX] server, retry using Active Directory */
1723 search_soap_request(sipe_private
,
1727 process_get_info_response
,
1733 static GSList
*search_rows_for_uri(const gchar
*uri
)
1735 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1736 GSList
*l
= g_slist_append(NULL
, NULL
);
1737 return(g_slist_append(l
, g_strdup(uri
)));
1740 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1743 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1744 GSList
*search_rows
= search_rows_for_uri(who
);
1746 if (sipe_private
->dlx_uri
) {
1747 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1749 mdd
->search_rows
= search_rows
;
1750 mdd
->other
= g_strdup(who
);
1751 mdd
->max_returns
= 1;
1752 mdd
->callback
= get_info_ab_entry_response
;
1753 mdd
->failed_callback
= get_info_ab_entry_failed
;
1754 mdd
->session
= sipe_svc_session_start();
1756 ms_dlx_webticket_request(sipe_private
, mdd
);
1759 /* no [MS-DLX] server, use Active Directory search instead */
1760 search_soap_request(sipe_private
,
1764 process_get_info_response
,
1766 free_search_rows(search_rows
);
1770 static void photo_response_data_free(struct photo_response_data
*data
)
1773 g_free(data
->photo_hash
);
1774 if (data
->request
) {
1775 sipe_http_request_cancel(data
->request
);
1780 static void photo_response_data_remove(struct sipe_core_private
*sipe_private
,
1781 struct photo_response_data
*data
)
1783 data
->request
= NULL
;
1784 sipe_private
->buddies
->pending_photo_requests
=
1785 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, data
);
1786 photo_response_data_free(data
);
1789 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1795 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1797 if (status
== SIPE_HTTP_STATUS_OK
) {
1798 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1801 gsize photo_size
= atoi(len_str
);
1802 gpointer photo
= g_new(char, photo_size
);
1805 memcpy(photo
, body
, photo_size
);
1807 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1816 photo_response_data_remove(sipe_private
, rdata
);
1819 static void process_get_user_photo_response(struct sipe_core_private
*sipe_private
,
1821 SIPE_UNUSED_PARAMETER GSList
*headers
,
1825 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1827 if ((status
== SIPE_HTTP_STATUS_OK
) && body
) {
1828 sipe_xml
*xml
= sipe_xml_parse(body
, strlen(body
));
1829 const sipe_xml
*node
= sipe_xml_child(xml
,
1830 "Body/GetUserPhotoResponse/PictureData");
1837 /* decode photo data */
1838 base64
= sipe_xml_data(node
);
1839 photo
= g_base64_decode(base64
, &photo_size
);
1842 /* EWS doesn't provide a hash -> calculate SHA-1 digest */
1843 if (!rdata
->photo_hash
) {
1844 guchar digest
[SIPE_DIGEST_SHA1_LENGTH
];
1845 sipe_digest_sha1(photo
, photo_size
, digest
);
1847 /* rdata takes ownership of digest string */
1848 rdata
->photo_hash
= buff_to_hex_str(digest
,
1849 SIPE_DIGEST_SHA1_LENGTH
);
1852 /* backend frees "photo" */
1853 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1863 photo_response_data_remove(sipe_private
, rdata
);
1866 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1868 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "Assertion", TRUE
);
1869 gchar
*wsse_security_base64
;
1870 gchar
*x_ms_webticket_header
;
1876 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1878 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1879 wsse_security_base64
);
1882 g_free(wsse_security_base64
);
1884 return x_ms_webticket_header
;
1887 /* see also sipe_ucs_http_request() */
1888 static struct sipe_http_request
*get_user_photo_request(struct sipe_core_private
*sipe_private
,
1889 struct photo_response_data
*data
,
1890 const gchar
*ews_url
,
1893 gchar
*soap
= g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
1895 " xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\""
1896 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
1897 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\""
1900 " <t:RequestServerVersion Version=\"Exchange2013\" />"
1904 " <m:Email>%s</m:Email>"
1905 " <m:SizeRequested>HR48x48</m:SizeRequested>"
1906 " </m:GetUserPhoto>"
1910 struct sipe_http_request
*request
= sipe_http_request_post(sipe_private
,
1914 "text/xml; charset=UTF-8",
1915 process_get_user_photo_response
,
1920 sipe_core_email_authentication(sipe_private
,
1922 sipe_http_request_allow_redirect(request
);
1924 SIPE_DEBUG_ERROR_NOFORMAT("get_user_photo_request: failed to create HTTP connection");
1930 static void photo_response_data_finalize(struct sipe_core_private
*sipe_private
,
1931 struct photo_response_data
*data
,
1933 const gchar
*photo_hash
)
1935 if (data
->request
) {
1936 data
->who
= g_strdup(uri
);
1937 data
->photo_hash
= g_strdup(photo_hash
);
1939 sipe_private
->buddies
->pending_photo_requests
=
1940 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1941 sipe_http_request_ready(data
->request
);
1943 photo_response_data_free(data
);
1947 void sipe_buddy_update_photo(struct sipe_core_private
*sipe_private
,
1949 const gchar
*photo_hash
,
1950 const gchar
*photo_url
,
1951 const gchar
*headers
)
1953 const gchar
*photo_hash_old
=
1954 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, uri
);
1956 if (!sipe_strequal(photo_hash
, photo_hash_old
)) {
1957 struct photo_response_data
*data
= g_new(struct photo_response_data
, 1);
1959 SIPE_DEBUG_INFO("sipe_buddy_update_photo: who '%s' url '%s' hash '%s'",
1960 uri
, photo_url
, photo_hash
);
1962 /* Photo URL is embedded XML? */
1963 if (g_str_has_prefix(photo_url
, "<") &&
1964 g_str_has_suffix(photo_url
, ">")) {
1965 /* add dummy root to embedded XML string */
1966 gchar
*tmp
= g_strdup_printf("<r>%s</r>", photo_url
);
1967 sipe_xml
*xml
= sipe_xml_parse(tmp
, strlen(tmp
));
1971 gchar
*ews_url
= sipe_xml_data(sipe_xml_child(xml
, "ewsUrl"));
1972 gchar
*email
= sipe_xml_data(sipe_xml_child(xml
, "primarySMTP"));
1974 if (!is_empty(ews_url
) && !is_empty(email
))
1975 data
->request
= get_user_photo_request(sipe_private
,
1985 data
->request
= sipe_http_request_get(sipe_private
,
1988 process_buddy_photo_response
,
1992 photo_response_data_finalize(sipe_private
,
1999 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
2001 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
2002 sipe_xml
*soap_body
,
2003 gpointer callback_data
)
2005 struct ms_dlx_data
*mdd
= callback_data
;
2006 gchar
*photo_rel_path
= NULL
;
2007 gchar
*photo_hash
= NULL
;
2010 const sipe_xml
*node
;
2012 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
2015 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
2017 node
= sipe_xml_twin(node
)) {
2018 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
2019 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
2021 if (!is_empty(value
)) {
2022 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
2023 g_free(photo_rel_path
);
2024 photo_rel_path
= value
;
2026 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
2038 if (sipe_private
->addressbook_uri
&& photo_rel_path
&& photo_hash
) {
2039 gchar
*photo_url
= g_strdup_printf("%s/%s",
2040 sipe_private
->addressbook_uri
, photo_rel_path
);
2041 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
2043 sipe_buddy_update_photo(sipe_private
,
2047 x_ms_webticket_header
);
2049 g_free(x_ms_webticket_header
);
2053 g_free(photo_rel_path
);
2058 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
2059 struct ms_dlx_data
*mdd
)
2064 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
2067 if (sipe_backend_uses_photo()) {
2069 /* Lync 2013 or newer: use UCS if contacts are migrated */
2070 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) &&
2071 sipe_ucs_is_migrated(sipe_private
)) {
2072 struct photo_response_data
*data
= g_new(struct photo_response_data
, 1);
2074 data
->request
= get_user_photo_request(sipe_private
,
2076 sipe_ucs_ews_url(sipe_private
),
2077 sipe_get_no_sip_uri(uri
));
2078 photo_response_data_finalize(sipe_private
,
2081 /* there is no hash */
2084 /* Lync 2010: use [MS-DLX] */
2085 } else if (sipe_private
->dlx_uri
&&
2086 sipe_private
->addressbook_uri
) {
2087 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
2089 mdd
->search_rows
= search_rows_for_uri(uri
);
2090 mdd
->other
= g_strdup(uri
);
2091 mdd
->max_returns
= 1;
2092 mdd
->callback
= get_photo_ab_entry_response
;
2093 mdd
->failed_callback
= get_photo_ab_entry_failed
;
2094 mdd
->session
= sipe_svc_session_start();
2096 ms_dlx_webticket_request(sipe_private
, mdd
);
2101 static void buddy_refresh_photos_cb(gpointer uri
,
2102 SIPE_UNUSED_PARAMETER gpointer value
,
2103 gpointer sipe_private
)
2105 buddy_fetch_photo(sipe_private
, uri
);
2108 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
2110 g_hash_table_foreach(sipe_private
->buddies
->uri
,
2111 buddy_refresh_photos_cb
,
2115 /* Buddy menu callbacks*/
2117 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
2120 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
2122 /* 2007+ conference */
2123 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
2124 sipe_conf_add(sipe_private
, who
);
2126 /* 2005- multiparty chat */
2128 gchar
*self
= sip_uri_self(sipe_private
);
2129 struct sip_session
*session
;
2131 session
= sipe_session_add_chat(sipe_private
,
2135 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
2136 session
->chat_session
,
2137 session
->chat_session
->title
,
2141 sipe_im_invite(sipe_private
, session
, who
,
2142 NULL
, NULL
, NULL
, FALSE
);
2146 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
2149 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
2152 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2154 SIPE_BUDDY_INFO_EMAIL
);
2157 gchar
*command_line
= g_strdup_printf(
2163 " mailto:%s", email
);
2166 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
2168 g_spawn_command_line_async(command_line
, NULL
);
2169 g_free(command_line
);
2172 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
2179 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
2180 struct sipe_backend_buddy_menu
*menu
,
2181 sipe_backend_buddy buddy
,
2182 sipe_buddy_info_fields id_phone
,
2183 sipe_buddy_info_fields id_display
,
2186 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
2190 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
2194 gchar
*label
= g_strdup_printf("%s %s",
2197 (tmp
= sip_tel_uri_denormalize(phone
)));
2198 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2201 SIPE_BUDDY_MENU_MAKE_CALL
,
2212 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
2213 const gchar
*buddy_name
,
2214 struct sipe_backend_buddy_menu
*menu
)
2216 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
2217 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
2220 gchar
*self
= sip_uri_self(sipe_private
);
2222 SIPE_SESSION_FOREACH
{
2223 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
2225 struct sipe_chat_session
*chat_session
= session
->chat_session
;
2226 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
2228 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
2230 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
2234 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
2235 /* We are a conf OP */
2237 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
2238 chat_session
->title
);
2239 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2242 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
2248 /* We are a conf OP */
2250 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
2251 chat_session
->title
);
2252 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2255 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
2263 (is_conf
&& !session
->locked
)) {
2264 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
2265 chat_session
->title
);
2266 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2269 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
2275 } SIPE_SESSION_FOREACH_END
;
2278 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2281 SIPE_BUDDY_MENU_NEW_CHAT
,
2284 /* add buddy's phone numbers if we have call control */
2285 if (sip_csta_is_idle(sipe_private
)) {
2288 menu
= buddy_menu_phone(sipe_public
,
2291 SIPE_BUDDY_INFO_WORK_PHONE
,
2292 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
2295 menu
= buddy_menu_phone(sipe_public
,
2298 SIPE_BUDDY_INFO_MOBILE_PHONE
,
2299 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
2303 menu
= buddy_menu_phone(sipe_public
,
2306 SIPE_BUDDY_INFO_HOME_PHONE
,
2307 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
2311 menu
= buddy_menu_phone(sipe_public
,
2314 SIPE_BUDDY_INFO_OTHER_PHONE
,
2315 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
2319 menu
= buddy_menu_phone(sipe_public
,
2322 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
2323 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
2328 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2330 SIPE_BUDDY_INFO_EMAIL
);
2332 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2335 SIPE_BUDDY_MENU_SEND_EMAIL
,
2341 /* access level control */
2342 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
2343 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
2346 sipe_ocs2007_access_control_menu(sipe_private
,
2352 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
2354 return(g_hash_table_size(sipe_private
->buddies
->uri
));
2357 static guint
sipe_ht_hash_nick(const char *nick
)
2359 char *lc
= g_utf8_strdown(nick
, -1);
2360 guint bucket
= g_str_hash(lc
);
2366 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
2368 char *nick1_norm
= NULL
;
2369 char *nick2_norm
= NULL
;
2372 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
2373 if (nick1
== NULL
|| nick2
== NULL
||
2374 !g_utf8_validate(nick1
, -1, NULL
) ||
2375 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
2377 nick1_norm
= g_utf8_casefold(nick1
, -1);
2378 nick2_norm
= g_utf8_casefold(nick2
, -1);
2379 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
2386 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
2388 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
2389 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
2390 (GEqualFunc
) sipe_ht_equals_nick
);
2391 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
2393 sipe_private
->buddies
= buddies
;