6 * Copyright (C) 2010-2014 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
33 #include "sipe-common.h"
37 #include "sip-transport.h"
38 #include "sipe-backend.h"
39 #include "sipe-buddy.h"
41 #include "sipe-chat.h"
42 #include "sipe-conf.h"
43 #include "sipe-core.h"
44 #include "sipe-core-private.h"
45 #include "sipe-group.h"
46 #include "sipe-http.h"
49 #include "sipe-ocs2005.h"
50 #include "sipe-ocs2007.h"
51 #include "sipe-schedule.h"
52 #include "sipe-session.h"
53 #include "sipe-status.h"
54 #include "sipe-subscriptions.h"
57 #include "sipe-utils.h"
58 #include "sipe-webticket.h"
63 GHashTable
*exchange_key
;
65 /* Pending photo download HTTP requests */
66 GSList
*pending_photo_requests
;
69 struct buddy_group_data
{
70 const struct sipe_group
*group
;
74 struct photo_response_data
{
77 struct sipe_http_request
*request
;
80 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
82 static void photo_response_data_free(struct photo_response_data
*data
);
84 void sipe_buddy_add_keys(struct sipe_core_private
*sipe_private
,
85 struct sipe_buddy
*buddy
,
86 const gchar
*exchange_key
,
87 const gchar
*change_key
)
90 buddy
->exchange_key
= g_strdup(exchange_key
);
91 g_hash_table_insert(sipe_private
->buddies
->exchange_key
,
96 buddy
->change_key
= g_strdup(change_key
);
99 struct sipe_buddy
*sipe_buddy_add(struct sipe_core_private
*sipe_private
,
101 const gchar
*exchange_key
,
102 const gchar
*change_key
)
104 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
105 gchar
*normalized_uri
= g_ascii_strdown(uri
, -1);
106 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
110 buddy
= g_new0(struct sipe_buddy
, 1);
111 buddy
->name
= normalized_uri
;
112 g_hash_table_insert(sipe_private
->buddies
->uri
,
116 sipe_buddy_add_keys(sipe_private
,
121 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri
);
123 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES
)) {
124 buddy
->just_added
= TRUE
;
125 sipe_subscribe_presence_single_cb(sipe_private
,
129 buddy_fetch_photo(sipe_private
, normalized_uri
);
131 normalized_uri
= NULL
; /* buddy takes ownership */
133 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri
);
134 buddy
->is_obsolete
= FALSE
;
136 g_free(normalized_uri
);
141 static gboolean
is_buddy_in_group(struct sipe_buddy
*buddy
,
145 GSList
*entry
= buddy
->groups
;
148 struct buddy_group_data
*bgd
= entry
->data
;
149 if (sipe_strequal(bgd
->group
->name
, name
)) {
150 bgd
->is_obsolete
= FALSE
;
160 void sipe_buddy_add_to_group(struct sipe_core_private
*sipe_private
,
161 struct sipe_buddy
*buddy
,
162 struct sipe_group
*group
,
165 const gchar
*uri
= buddy
->name
;
166 const gchar
*group_name
= group
->name
;
167 sipe_backend_buddy bb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
172 bb
= sipe_backend_buddy_add(SIPE_CORE_PUBLIC
,
176 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
177 uri
, alias
? alias
: "<NONE>");
181 if (!is_empty(alias
)) {
182 gchar
*old_alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
185 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri
),
187 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
,
190 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
191 uri
, old_alias
, alias
);
196 if (!is_buddy_in_group(buddy
, group_name
)) {
197 sipe_buddy_insert_group(buddy
, group
);
198 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
203 static gint
buddy_group_compare(gconstpointer a
, gconstpointer b
)
205 return(((const struct buddy_group_data
*)a
)->group
->id
-
206 ((const struct buddy_group_data
*)b
)->group
->id
);
209 void sipe_buddy_insert_group(struct sipe_buddy
*buddy
,
210 struct sipe_group
*group
)
212 struct buddy_group_data
*bgd
= g_new0(struct buddy_group_data
, 1);
216 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
222 static void buddy_group_free(gpointer data
)
227 static void buddy_group_remove(struct sipe_buddy
*buddy
,
228 struct buddy_group_data
*bgd
)
230 buddy
->groups
= g_slist_remove(buddy
->groups
, bgd
);
231 buddy_group_free(bgd
);
234 static void sipe_buddy_remove_group(struct sipe_buddy
*buddy
,
235 const struct sipe_group
*group
)
237 GSList
*entry
= buddy
->groups
;
238 struct buddy_group_data
*bgd
= NULL
;
242 if (bgd
->group
== group
)
247 buddy_group_remove(buddy
, bgd
);
250 void sipe_buddy_update_groups(struct sipe_core_private
*sipe_private
,
251 struct sipe_buddy
*buddy
,
254 const gchar
*uri
= buddy
->name
;
255 GSList
*entry
= buddy
->groups
;
258 struct buddy_group_data
*bgd
= entry
->data
;
259 const struct sipe_group
*group
= bgd
->group
;
261 /* next buddy group */
264 /* old group NOT found in new list? */
265 if (g_slist_find(new_groups
, group
) == NULL
) {
266 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
269 SIPE_DEBUG_INFO("sipe_buddy_update_groups: removing buddy %s from group '%s'",
271 /* this should never be NULL */
273 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
275 buddy_group_remove(buddy
, bgd
);
280 gchar
*sipe_buddy_groups_string(struct sipe_buddy
*buddy
)
284 /* creating array from GList, converting guint to gchar * */
285 gchar
**ids_arr
= g_new(gchar
*, g_slist_length(buddy
->groups
) + 1);
286 GSList
*entry
= buddy
->groups
;
292 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
293 ids_arr
[i
] = g_strdup_printf("%u", group
->id
);
299 string
= g_strjoinv(" ", ids_arr
);
305 void sipe_buddy_cleanup_local_list(struct sipe_core_private
*sipe_private
)
307 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
310 GSList
*entry
= buddies
;
312 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
313 g_slist_length(buddies
));
314 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
315 sipe_buddy_count(sipe_private
));
317 sipe_backend_buddy bb
= entry
->data
;
318 gchar
*bname
= sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC
,
320 gchar
*gname
= sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC
,
322 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
325 if (!is_buddy_in_group(buddy
, gname
)) {
326 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",
328 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, bb
);
337 g_slist_free(buddies
);
340 struct sipe_buddy
*sipe_buddy_find_by_uri(struct sipe_core_private
*sipe_private
,
343 return(g_hash_table_lookup(sipe_private
->buddies
->uri
, uri
));
346 struct sipe_buddy
*sipe_buddy_find_by_exchange_key(struct sipe_core_private
*sipe_private
,
347 const gchar
*exchange_key
)
349 return(g_hash_table_lookup(sipe_private
->buddies
->exchange_key
,
353 void sipe_buddy_foreach(struct sipe_core_private
*sipe_private
,
355 gpointer callback_data
)
357 g_hash_table_foreach(sipe_private
->buddies
->uri
,
362 static void buddy_free(struct sipe_buddy
*buddy
)
366 * We are calling g_hash_table_foreach_steal(). That means that no
367 * key/value deallocation functions are called. Therefore the glib
368 * hash code does not touch the key (buddy->name) or value (buddy)
369 * of the to-be-deleted hash node at all. It follows that we
371 * - MUST free the memory for the key ourselves and
372 * - ARE allowed to do it in this function
374 * Conclusion: glib must be broken on the Windows platform if sipe
375 * crashes with SIGTRAP when closing. You'll have to live
376 * with the memory leak until this is fixed.
380 g_free(buddy
->exchange_key
);
381 g_free(buddy
->change_key
);
382 g_free(buddy
->activity
);
383 g_free(buddy
->meeting_subject
);
384 g_free(buddy
->meeting_location
);
387 g_free(buddy
->cal_start_time
);
388 g_free(buddy
->cal_free_busy_base64
);
389 g_free(buddy
->cal_free_busy
);
390 g_free(buddy
->last_non_cal_activity
);
392 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
394 g_free(buddy
->device_name
);
395 sipe_utils_slist_free_full(buddy
->groups
, buddy_group_free
);
399 static gboolean
buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key
,
401 SIPE_UNUSED_PARAMETER gpointer user_data
)
404 /* We must return TRUE as the key/value have already been deleted */
408 void sipe_buddy_free(struct sipe_core_private
*sipe_private
)
410 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
412 g_hash_table_foreach_steal(buddies
->uri
,
416 /* core is being deallocated, remove all its pending photo requests */
417 while (buddies
->pending_photo_requests
) {
418 struct photo_response_data
*data
=
419 buddies
->pending_photo_requests
->data
;
420 buddies
->pending_photo_requests
=
421 g_slist_remove(buddies
->pending_photo_requests
, data
);
422 photo_response_data_free(data
);
425 g_hash_table_destroy(buddies
->uri
);
426 g_hash_table_destroy(buddies
->exchange_key
);
428 sipe_private
->buddies
= NULL
;
431 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
433 SIPE_UNUSED_PARAMETER gpointer user_data
)
435 struct sipe_buddy
*buddy
= value
;
436 GSList
*entry
= buddy
->groups
;
438 buddy
->is_obsolete
= TRUE
;
440 ((struct buddy_group_data
*) entry
->data
)->is_obsolete
= TRUE
;
445 void sipe_buddy_update_start(struct sipe_core_private
*sipe_private
)
447 g_hash_table_foreach(sipe_private
->buddies
->uri
,
448 buddy_set_obsolete_flag
,
452 static gboolean
buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
456 struct sipe_core_private
*sipe_private
= user_data
;
457 struct sipe_buddy
*buddy
= value
;
458 const gchar
*uri
= buddy
->name
;
460 if (buddy
->is_obsolete
) {
461 /* all backend buddies in different groups */
462 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
465 GSList
*entry
= buddies
;
467 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
468 g_slist_length(buddies
),
472 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
476 g_slist_free(buddies
);
479 /* return TRUE as the key/value have already been deleted */
483 GSList
*entry
= buddy
->groups
;
486 struct buddy_group_data
*bgd
= entry
->data
;
488 /* next buddy group */
491 if (bgd
->is_obsolete
) {
492 const struct sipe_group
*group
= bgd
->group
;
493 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
496 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: removing buddy '%s' from group '%s'",
498 /* this should never be NULL */
500 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
502 buddy_group_remove(buddy
, bgd
);
509 void sipe_buddy_update_finish(struct sipe_core_private
*sipe_private
)
511 g_hash_table_foreach_remove(sipe_private
->buddies
->uri
,
512 buddy_check_obsolete_flag
,
516 gchar
*sipe_core_buddy_status(struct sipe_core_public
*sipe_public
,
519 const gchar
*status_text
)
521 struct sipe_buddy
*sbuddy
;
524 if (!sipe_public
) return NULL
; /* happens on pidgin exit */
526 sbuddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
, uri
);
527 if (!sbuddy
) return NULL
;
529 status
= g_string_new(sbuddy
->activity
? sbuddy
->activity
:
530 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
533 if (sbuddy
->is_mobile
) {
535 g_string_append(status
, " - ");
536 g_string_append(status
, _("Mobile"));
541 g_string_append(status
, " - ");
542 g_string_append(status
, sbuddy
->note
);
545 return(g_string_free(status
, FALSE
));
548 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
551 sipe_backend_buddy pbuddy
;
553 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
554 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
559 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
561 const gchar
*old_group_name
,
562 const gchar
*new_group_name
)
564 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
565 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
567 struct sipe_group
*old_group
= NULL
;
568 struct sipe_group
*new_group
;
569 struct sipe_ucs_transaction
*ucs_trans
= NULL
;
571 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' old group '%s' new group '%s'",
573 old_group_name
? old_group_name
: "<UNDEFINED>",
574 new_group_name
? new_group_name
: "<UNDEFINED>");
577 /* buddy not in roaming list */
580 old_group
= sipe_group_find_by_name(sipe_private
, old_group_name
);
582 sipe_buddy_remove_group(buddy
, old_group
);
583 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' removed from old group '%s'",
584 who
, old_group_name
);
587 new_group
= sipe_group_find_by_name(sipe_private
, new_group_name
);
589 sipe_buddy_insert_group(buddy
, new_group
);
590 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' added to new group '%s'",
591 who
, new_group_name
);
594 if (sipe_ucs_is_migrated(sipe_private
)) {
597 ucs_trans
= sipe_ucs_transaction(sipe_private
);
601 * 1. new buddy added to existing group
602 * 2. existing buddy moved from old to existing group
604 sipe_ucs_group_add_buddy(sipe_private
,
610 sipe_ucs_group_remove_buddy(sipe_private
,
615 } else if (old_group
) {
617 * 3. existing buddy removed from one of its groups
618 * 4. existing buddy removed from last group
620 sipe_ucs_group_remove_buddy(sipe_private
,
624 if (g_slist_length(buddy
->groups
) < 1)
625 sipe_buddy_remove(sipe_private
,
627 /* buddy no longer valid */
630 /* non-UCS handling */
631 } else if (new_group
)
632 sipe_group_update_buddy(sipe_private
, buddy
);
634 /* 5. buddy added to new group */
636 sipe_group_create(sipe_private
,
642 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
644 const gchar
*group_name
)
646 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
648 if (!sipe_buddy_find_by_uri(sipe_private
, uri
))
649 sipe_buddy_add(sipe_private
,
654 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
657 sipe_core_buddy_group(sipe_public
,
663 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
664 struct sipe_buddy
*buddy
)
666 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
667 const gchar
*uri
= buddy
->name
;
668 GSList
*entry
= buddy
->groups
;
669 gchar
*action_name
= sipe_utils_presence_key(uri
);
671 sipe_schedule_cancel(sipe_private
, action_name
);
674 /* If the buddy still has groups, we need to delete backend buddies */
676 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
677 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
680 /* this should never be NULL */
682 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, oldb
);
687 g_hash_table_remove(buddies
->uri
, uri
);
688 if (buddy
->exchange_key
)
689 g_hash_table_remove(buddies
->exchange_key
,
690 buddy
->exchange_key
);
696 * Unassociates buddy from group first.
697 * Then see if no groups left, removes buddy completely.
698 * Otherwise updates buddy groups on server.
700 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
702 const gchar
*group_name
)
704 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
705 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
707 struct sipe_group
*group
= NULL
;
712 group
= sipe_group_find_by_name(sipe_private
, group_name
);
714 sipe_buddy_remove_group(buddy
, group
);
715 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy '%s' removed from group '%s'",
720 if (g_slist_length(buddy
->groups
) < 1) {
722 if (sipe_ucs_is_migrated(sipe_private
)) {
723 sipe_ucs_group_remove_buddy(sipe_private
,
728 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
730 sip_soap_request(sipe_private
,
736 sipe_buddy_remove(sipe_private
, buddy
);
738 if (sipe_ucs_is_migrated(sipe_private
)) {
739 sipe_ucs_group_remove_buddy(sipe_private
,
744 /* updates groups on server */
745 sipe_group_update_buddy(sipe_private
, buddy
);
749 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
753 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
754 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
759 /* Check if on 2005 system contact's calendar,
760 * then set/preserve it.
762 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
763 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
765 sipe_ocs2005_apply_calendar_status(sipe_private
,
767 sipe_status_activity_to_token(activity
));
771 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
773 const gchar
*status_name
,
775 struct sipe_backend_buddy_tooltip
*tooltip
)
777 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
779 gboolean is_oof_note
= FALSE
;
780 const gchar
*activity
= NULL
;
781 gchar
*calendar
= NULL
;
782 const gchar
*meeting_subject
= NULL
;
783 const gchar
*meeting_location
= NULL
;
784 gchar
*access_text
= NULL
;
786 #define SIPE_ADD_BUDDY_INFO(l, t) \
788 gchar *tmp = g_markup_escape_text((t), -1); \
789 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
792 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
793 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
795 if (sipe_public
) { /* happens on pidgin exit */
796 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
800 is_oof_note
= sbuddy
->is_oof_note
;
801 activity
= sbuddy
->activity
;
802 calendar
= sipe_cal_get_description(sbuddy
);
803 meeting_subject
= sbuddy
->meeting_subject
;
804 meeting_location
= sbuddy
->meeting_location
;
806 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
807 gboolean is_group_access
= FALSE
;
808 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
810 sipe_get_no_sip_uri(uri
),
812 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
813 access_text
= is_group_access
?
814 g_strdup(access_level
) :
815 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
821 const gchar
*status_str
= activity
? activity
: status_name
;
823 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
825 if (is_online
&& !is_empty(calendar
)) {
826 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
829 if (!is_empty(meeting_location
)) {
830 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
831 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
833 if (!is_empty(meeting_subject
)) {
834 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
835 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
838 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
839 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
840 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
842 g_free(note_italics
);
845 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
850 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
852 sipe_buddy_info_fields propkey
,
853 char *property_value
)
855 GSList
*buddies
, *entry
;
858 property_value
= g_strstrip(property_value
);
860 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
863 sipe_backend_buddy p_buddy
= entry
->data
;
865 /* for Display Name */
866 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
868 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
869 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
870 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
871 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
875 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
876 if (!is_empty(property_value
) &&
877 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
879 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
880 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
884 /* for other properties */
886 if (!is_empty(property_value
)) {
887 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
888 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
889 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
897 g_slist_free(buddies
);
906 sipe_svc_callback
*callback
;
907 struct sipe_svc_session
*session
;
908 gchar
*wsse_security
;
909 struct sipe_backend_search_token
*token
;
910 /* must call ms_dlx_free() */
911 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
912 struct ms_dlx_data
*mdd
);
915 static void free_search_rows(GSList
*search_rows
)
917 sipe_utils_slist_free_full(search_rows
, g_free
);
920 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
922 free_search_rows(mdd
->search_rows
);
923 sipe_svc_session_close(mdd
->session
);
925 g_free(mdd
->wsse_security
);
929 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
930 #define DLX_SEARCH_ITEM \
931 "<AbEntryRequest.ChangeSearchQuery>" \
932 " <SearchOn>%s</SearchOn>" \
933 " <Value>%s</Value>" \
934 "</AbEntryRequest.ChangeSearchQuery>"
936 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
937 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
946 attr
= query_rows
->data
;
947 query_rows
= g_slist_next(query_rows
);
948 value
= query_rows
->data
;
949 query_rows
= g_slist_next(query_rows
);
955 * Special value for SIP ID
957 * Active Directory seems only to be able to search for
958 * SIP URIs. Make sure search string starts with "sip:".
961 attr
= "msRTCSIP-PrimaryUserAddress";
963 value
= tmp
= sip_uri(value
);
966 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
973 query
= g_strjoinv(NULL
, attrs
);
974 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
983 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
984 const gchar
*base_uri
,
985 const gchar
*auth_uri
,
986 const gchar
*wsse_security
,
987 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
988 gpointer callback_data
)
990 struct ms_dlx_data
*mdd
= callback_data
;
993 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
996 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
999 search
= g_strdup_printf("<ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
1002 g_slist_length(mdd
->search_rows
) / 2,
1006 if (sipe_svc_ab_entry_request(sipe_private
,
1015 /* keep webticket security token for potential further use */
1016 mdd
->wsse_security
= g_strdup(wsse_security
);
1018 /* callback data passed down the line */
1024 /* no ticket: this will show the minmum information */
1025 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
1030 mdd
->failed_callback(sipe_private
, mdd
);
1033 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
1034 struct ms_dlx_data
*mdd
)
1036 if (!sipe_webticket_request(sipe_private
,
1038 sipe_private
->dlx_uri
,
1039 "AddressBookWebTicketBearer",
1042 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
1043 sipe_private
->dlx_uri
);
1044 mdd
->failed_callback(sipe_private
, mdd
);
1048 static void search_contacts_finalize(struct sipe_core_private
*sipe_private
,
1049 struct sipe_backend_search_results
*results
,
1053 gchar
*secondary
= g_strdup_printf(
1054 dngettext(PACKAGE_NAME
,
1055 "Found %d contact%s:",
1056 "Found %d contacts%s:", match_count
),
1057 match_count
, more
? _(" (more matched your query)") : "");
1059 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
1066 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
1068 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1069 sipe_xml
*soap_body
,
1070 gpointer callback_data
)
1072 struct ms_dlx_data
*mdd
= callback_data
;
1075 const sipe_xml
*node
;
1076 struct sipe_backend_search_results
*results
;
1079 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
1083 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
1085 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
1086 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1088 _("No contacts found"));
1093 /* OK, we found something - show the results to the user */
1094 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1097 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
1098 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1100 _("Unable to display the search results"));
1105 /* SearchAbEntryResult can contain duplicates */
1106 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1109 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
1110 const sipe_xml
*attrs
;
1111 gchar
*sip_uri
= NULL
;
1112 gchar
*displayname
= NULL
;
1113 gchar
*company
= NULL
;
1114 gchar
*country
= NULL
;
1115 gchar
*email
= NULL
;
1117 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
1119 attrs
= sipe_xml_twin(attrs
)) {
1120 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
1122 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
1125 if (!is_empty(value
)) {
1126 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
1130 } else if (sipe_strcase_equal(name
, "displayname")) {
1131 g_free(displayname
);
1132 displayname
= value
;
1134 } else if (sipe_strcase_equal(name
, "mail")) {
1138 } else if (sipe_strcase_equal(name
, "company")) {
1142 } else if (sipe_strcase_equal(name
, "country")) {
1153 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
1154 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
1155 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1162 g_strfreev(uri_parts
);
1164 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
1171 g_free(displayname
);
1175 search_contacts_finalize(sipe_private
, results
,
1176 g_hash_table_size(found
),
1178 g_hash_table_destroy(found
);
1182 mdd
->failed_callback(sipe_private
, mdd
);
1186 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
1188 struct transaction
*trans
)
1190 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
1191 struct sipe_backend_search_results
*results
;
1192 sipe_xml
*searchResults
;
1193 const sipe_xml
*mrow
;
1194 guint match_count
= 0;
1195 gboolean more
= FALSE
;
1197 /* valid response? */
1198 if (msg
->response
!= 200) {
1199 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1201 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1203 _("Contact search failed"));
1207 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
1210 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1211 if (!searchResults
) {
1212 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1213 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1215 _("Contact search failed"));
1220 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
1222 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1223 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1225 _("No contacts found"));
1227 sipe_xml_free(searchResults
);
1231 /* OK, we found something - show the results to the user */
1232 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1233 trans
->payload
->data
);
1235 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1236 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1238 _("Unable to display the search results"));
1240 sipe_xml_free(searchResults
);
1244 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
1245 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
1246 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1249 sipe_xml_attribute(mrow
, "displayName"),
1250 sipe_xml_attribute(mrow
, "company"),
1251 sipe_xml_attribute(mrow
, "country"),
1252 sipe_xml_attribute(mrow
, "email"));
1253 g_strfreev(uri_parts
);
1257 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
1258 char *data
= sipe_xml_data(mrow
);
1259 more
= (g_ascii_strcasecmp(data
, "true") == 0);
1263 search_contacts_finalize(sipe_private
, results
, match_count
, more
);
1264 sipe_xml_free(searchResults
);
1269 static void search_soap_request(struct sipe_core_private
*sipe_private
,
1270 GDestroyNotify destroy
,
1273 SoapTransCallback callback
,
1274 GSList
*search_rows
)
1276 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
1277 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1279 payload
->destroy
= destroy
;
1280 payload
->data
= data
;
1282 sip_soap_directory_search(sipe_private
,
1290 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1291 struct ms_dlx_data
*mdd
)
1293 /* error using [MS-DLX] server, retry using Active Directory */
1294 search_soap_request(sipe_private
,
1298 process_search_contact_response
,
1303 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
1304 struct sipe_backend_search_token
*token
,
1305 const gchar
*given_name
,
1306 const gchar
*surname
,
1309 const gchar
*company
,
1310 const gchar
*country
)
1312 GSList
*query_rows
= NULL
;
1314 #define ADD_QUERY_ROW(attr, val) \
1316 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1317 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1320 ADD_QUERY_ROW("givenName", given_name
);
1321 ADD_QUERY_ROW("sn", surname
);
1322 ADD_QUERY_ROW("mail", email
);
1323 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1324 ADD_QUERY_ROW(NULL
, sipid
);
1325 ADD_QUERY_ROW("company", company
);
1326 ADD_QUERY_ROW("c", country
);
1329 if (SIPE_CORE_PRIVATE
->dlx_uri
!= NULL
) {
1330 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1332 mdd
->search_rows
= query_rows
;
1333 mdd
->max_returns
= 100;
1334 mdd
->callback
= search_ab_entry_response
;
1335 mdd
->failed_callback
= search_ab_entry_failed
;
1336 mdd
->session
= sipe_svc_session_start();
1339 ms_dlx_webticket_request(SIPE_CORE_PRIVATE
, mdd
);
1342 /* no [MS-DLX] server, use Active Directory search instead */
1343 search_soap_request(SIPE_CORE_PRIVATE
,
1347 process_search_contact_response
,
1349 free_search_rows(query_rows
);
1352 sipe_backend_search_failed(sipe_public
,
1354 _("Invalid contact search query"));
1357 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1358 struct sipe_backend_buddy_info
*info
,
1360 const gchar
*server_alias
,
1363 sipe_backend_buddy bbuddy
;
1364 struct sipe_buddy
*sbuddy
;
1369 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1371 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1376 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1378 if (is_empty(server_alias
)) {
1379 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1382 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1384 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1388 value
= g_strdup(server_alias
);
1391 /* present alias if it differs from server alias */
1392 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1393 if (alias
&& !sipe_strequal(alias
, value
))
1395 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1397 SIPE_BUDDY_INFO_ALIAS
,
1403 if (is_empty(email
)) {
1404 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1406 SIPE_BUDDY_INFO_EMAIL
);
1408 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1410 SIPE_BUDDY_INFO_EMAIL
,
1416 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1418 SIPE_BUDDY_INFO_SITE
);
1420 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1422 SIPE_BUDDY_INFO_SITE
,
1427 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1428 if (sbuddy
&& sbuddy
->device_name
) {
1429 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1431 SIPE_BUDDY_INFO_DEVICE
,
1432 sbuddy
->device_name
);
1435 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1439 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1441 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1442 sipe_xml
*soap_body
,
1443 gpointer callback_data
)
1445 struct ms_dlx_data
*mdd
= callback_data
;
1446 struct sipe_backend_buddy_info
*info
= NULL
;
1447 gchar
*server_alias
= NULL
;
1448 gchar
*email
= NULL
;
1451 const sipe_xml
*node
;
1453 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1456 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1458 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1460 node
= sipe_xml_twin(node
)) {
1461 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1463 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1465 const sipe_xml
*values
= sipe_xml_child(node
,
1468 /* Single value entries */
1469 if (!is_empty(value
)) {
1471 if (sipe_strcase_equal(name
, "displayname")) {
1472 g_free(server_alias
);
1473 server_alias
= value
;
1475 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1477 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1479 } else if (sipe_strcase_equal(name
, "mail")) {
1483 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1485 SIPE_BUDDY_INFO_EMAIL
,
1487 } else if (sipe_strcase_equal(name
, "title")) {
1488 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1490 SIPE_BUDDY_INFO_JOB_TITLE
,
1492 } else if (sipe_strcase_equal(name
, "company")) {
1493 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1495 SIPE_BUDDY_INFO_COMPANY
,
1497 } else if (sipe_strcase_equal(name
, "country")) {
1498 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1500 SIPE_BUDDY_INFO_COUNTRY
,
1504 } else if (values
) {
1505 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1508 if (sipe_strcase_equal(name
, "telephonenumber")) {
1509 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1511 SIPE_BUDDY_INFO_WORK_PHONE
,
1523 /* this will show the minmum information */
1524 get_info_finalize(sipe_private
,
1531 g_free(server_alias
);
1535 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1537 struct transaction
*trans
)
1539 const gchar
*uri
= trans
->payload
->data
;
1540 struct sipe_backend_buddy_info
*info
= NULL
;
1541 gchar
*server_alias
= NULL
;
1542 gchar
*email
= NULL
;
1544 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1545 uri
, sipe_private
->username
);
1547 if (msg
->response
!= 200) {
1548 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1550 sipe_xml
*searchResults
;
1551 const sipe_xml
*mrow
;
1553 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1554 msg
->body
? msg
->body
: "");
1556 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1557 if (!searchResults
) {
1559 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1561 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1563 gchar
*phone_number
;
1565 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1567 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1568 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1569 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1572 * For 2007 system we will take this from ContactCard -
1573 * it has cleaner tel: URIs at least
1575 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1576 char *tel_uri
= sip_to_tel_uri(phone_number
);
1577 /* trims its parameters, so call first */
1578 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1579 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1580 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1581 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1584 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1588 if (!is_empty(server_alias
)) {
1589 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1591 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1594 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1595 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1597 SIPE_BUDDY_INFO_JOB_TITLE
,
1600 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1601 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1603 SIPE_BUDDY_INFO_OFFICE
,
1606 if (!is_empty(phone_number
)) {
1607 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1609 SIPE_BUDDY_INFO_WORK_PHONE
,
1612 g_free(phone_number
);
1613 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1614 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1616 SIPE_BUDDY_INFO_COMPANY
,
1619 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1620 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1622 SIPE_BUDDY_INFO_CITY
,
1625 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1626 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1628 SIPE_BUDDY_INFO_STATE
,
1631 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1632 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1634 SIPE_BUDDY_INFO_COUNTRY
,
1637 if (!is_empty(email
)) {
1638 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1640 SIPE_BUDDY_INFO_EMAIL
,
1644 sipe_xml_free(searchResults
);
1647 /* this will show the minmum information */
1648 get_info_finalize(sipe_private
,
1654 g_free(server_alias
);
1660 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1661 struct ms_dlx_data
*mdd
)
1663 /* error using [MS-DLX] server, retry using Active Directory */
1664 search_soap_request(sipe_private
,
1668 process_get_info_response
,
1674 static GSList
*search_rows_for_uri(const gchar
*uri
)
1676 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1677 GSList
*l
= g_slist_append(NULL
, NULL
);
1678 return(g_slist_append(l
, g_strdup(uri
)));
1681 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1684 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1685 GSList
*search_rows
= search_rows_for_uri(who
);
1687 if (sipe_private
->dlx_uri
) {
1688 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1690 mdd
->search_rows
= search_rows
;
1691 mdd
->other
= g_strdup(who
);
1692 mdd
->max_returns
= 1;
1693 mdd
->callback
= get_info_ab_entry_response
;
1694 mdd
->failed_callback
= get_info_ab_entry_failed
;
1695 mdd
->session
= sipe_svc_session_start();
1697 ms_dlx_webticket_request(sipe_private
, mdd
);
1700 /* no [MS-DLX] server, use Active Directory search instead */
1701 search_soap_request(sipe_private
,
1705 process_get_info_response
,
1707 free_search_rows(search_rows
);
1711 static void photo_response_data_free(struct photo_response_data
*data
)
1714 g_free(data
->photo_hash
);
1715 if (data
->request
) {
1716 sipe_http_request_cancel(data
->request
);
1721 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1727 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1729 rdata
->request
= NULL
;
1731 if (status
== SIPE_HTTP_STATUS_OK
) {
1732 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1735 gsize photo_size
= atoi(len_str
);
1736 gpointer photo
= g_new(char, photo_size
);
1739 memcpy(photo
, body
, photo_size
);
1741 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1750 sipe_private
->buddies
->pending_photo_requests
=
1751 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, rdata
);
1753 photo_response_data_free(rdata
);
1756 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1758 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "saml:Assertion", TRUE
);
1759 gchar
*wsse_security_base64
;
1760 gchar
*x_ms_webticket_header
;
1766 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1768 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1769 wsse_security_base64
);
1772 g_free(wsse_security_base64
);
1774 return x_ms_webticket_header
;
1777 void sipe_buddy_update_photo(struct sipe_core_private
*sipe_private
,
1779 const gchar
*photo_hash
,
1780 const gchar
*photo_url
,
1781 const gchar
*headers
)
1783 const gchar
*photo_hash_old
=
1784 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, uri
);
1786 if (!sipe_strequal(photo_hash
, photo_hash_old
)) {
1787 struct photo_response_data
*data
= g_new(struct photo_response_data
, 1);
1789 SIPE_DEBUG_INFO("sipe_buddy_update_photo: who '%s' url '%s' hash '%s'",
1790 uri
, photo_url
, photo_hash
);
1792 data
->who
= g_strdup(uri
);
1793 data
->photo_hash
= g_strdup(photo_hash
);
1795 data
->request
= sipe_http_request_get(sipe_private
,
1798 process_buddy_photo_response
,
1801 if (data
->request
) {
1802 sipe_private
->buddies
->pending_photo_requests
=
1803 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1804 sipe_http_request_ready(data
->request
);
1806 photo_response_data_free(data
);
1811 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
1813 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1814 sipe_xml
*soap_body
,
1815 gpointer callback_data
)
1817 struct ms_dlx_data
*mdd
= callback_data
;
1818 gchar
*photo_rel_path
= NULL
;
1819 gchar
*photo_hash
= NULL
;
1822 const sipe_xml
*node
;
1824 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1827 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1829 node
= sipe_xml_twin(node
)) {
1830 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
1831 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
1833 if (!is_empty(value
)) {
1834 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
1835 g_free(photo_rel_path
);
1836 photo_rel_path
= value
;
1838 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
1850 if (sipe_private
->addressbook_uri
&& photo_rel_path
&& photo_hash
) {
1851 gchar
*photo_url
= g_strdup_printf("%s/%s",
1852 sipe_private
->addressbook_uri
, photo_rel_path
);
1853 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
1855 sipe_buddy_update_photo(sipe_private
,
1859 x_ms_webticket_header
);
1861 g_free(x_ms_webticket_header
);
1865 g_free(photo_rel_path
);
1870 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
1871 struct ms_dlx_data
*mdd
)
1876 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
1879 if (sipe_backend_uses_photo()) {
1881 /* Lync 2013 or newer: use UCS if contacts are migrated */
1882 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
) &&
1883 sipe_ucs_is_migrated(sipe_private
)) {
1885 sipe_ucs_get_photo(sipe_private
, uri
);
1887 /* Lync 2010: use [MS-DLX] */
1888 } else if (sipe_private
->dlx_uri
&&
1889 sipe_private
->addressbook_uri
) {
1890 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1892 mdd
->search_rows
= search_rows_for_uri(uri
);
1893 mdd
->other
= g_strdup(uri
);
1894 mdd
->max_returns
= 1;
1895 mdd
->callback
= get_photo_ab_entry_response
;
1896 mdd
->failed_callback
= get_photo_ab_entry_failed
;
1897 mdd
->session
= sipe_svc_session_start();
1899 ms_dlx_webticket_request(sipe_private
, mdd
);
1904 static void buddy_refresh_photos_cb(gpointer uri
,
1905 SIPE_UNUSED_PARAMETER gpointer value
,
1906 gpointer sipe_private
)
1908 buddy_fetch_photo(sipe_private
, uri
);
1911 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
1913 g_hash_table_foreach(sipe_private
->buddies
->uri
,
1914 buddy_refresh_photos_cb
,
1918 /* Buddy menu callbacks*/
1920 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
1923 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1925 /* 2007+ conference */
1926 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1927 sipe_conf_add(sipe_private
, who
);
1929 /* 2005- multiparty chat */
1931 gchar
*self
= sip_uri_self(sipe_private
);
1932 struct sip_session
*session
;
1934 session
= sipe_session_add_chat(sipe_private
,
1938 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
1939 session
->chat_session
,
1940 session
->chat_session
->title
,
1944 sipe_im_invite(sipe_private
, session
, who
,
1945 NULL
, NULL
, NULL
, FALSE
);
1949 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
1952 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1955 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1957 SIPE_BUDDY_INFO_EMAIL
);
1960 gchar
*command_line
= g_strdup_printf(
1966 " mailto:%s", email
);
1969 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1971 g_spawn_command_line_async(command_line
, NULL
);
1972 g_free(command_line
);
1975 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1982 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
1983 struct sipe_backend_buddy_menu
*menu
,
1984 sipe_backend_buddy buddy
,
1985 sipe_buddy_info_fields id_phone
,
1986 sipe_buddy_info_fields id_display
,
1989 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
1993 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
1997 gchar
*label
= g_strdup_printf("%s %s",
2000 (tmp
= sip_tel_uri_denormalize(phone
)));
2001 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2004 SIPE_BUDDY_MENU_MAKE_CALL
,
2015 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
2016 const gchar
*buddy_name
,
2017 struct sipe_backend_buddy_menu
*menu
)
2019 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
2020 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
2023 gchar
*self
= sip_uri_self(sipe_private
);
2025 SIPE_SESSION_FOREACH
{
2026 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
2028 struct sipe_chat_session
*chat_session
= session
->chat_session
;
2029 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
2031 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
2033 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
2037 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
2038 /* We are a conf OP */
2040 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
2041 chat_session
->title
);
2042 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2045 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
2051 /* We are a conf OP */
2053 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
2054 chat_session
->title
);
2055 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2058 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
2066 (is_conf
&& !session
->locked
)) {
2067 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
2068 chat_session
->title
);
2069 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2072 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
2078 } SIPE_SESSION_FOREACH_END
;
2081 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2084 SIPE_BUDDY_MENU_NEW_CHAT
,
2087 /* add buddy's phone numbers if we have call control */
2088 if (sip_csta_is_idle(sipe_private
)) {
2091 menu
= buddy_menu_phone(sipe_public
,
2094 SIPE_BUDDY_INFO_WORK_PHONE
,
2095 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
2098 menu
= buddy_menu_phone(sipe_public
,
2101 SIPE_BUDDY_INFO_MOBILE_PHONE
,
2102 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
2106 menu
= buddy_menu_phone(sipe_public
,
2109 SIPE_BUDDY_INFO_HOME_PHONE
,
2110 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
2114 menu
= buddy_menu_phone(sipe_public
,
2117 SIPE_BUDDY_INFO_OTHER_PHONE
,
2118 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
2122 menu
= buddy_menu_phone(sipe_public
,
2125 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
2126 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
2131 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2133 SIPE_BUDDY_INFO_EMAIL
);
2135 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2138 SIPE_BUDDY_MENU_SEND_EMAIL
,
2144 /* access level control */
2145 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
2146 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
2149 sipe_ocs2007_access_control_menu(sipe_private
,
2155 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
2157 return(g_hash_table_size(sipe_private
->buddies
->uri
));
2160 static guint
sipe_ht_hash_nick(const char *nick
)
2162 char *lc
= g_utf8_strdown(nick
, -1);
2163 guint bucket
= g_str_hash(lc
);
2169 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
2171 char *nick1_norm
= NULL
;
2172 char *nick2_norm
= NULL
;
2175 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
2176 if (nick1
== NULL
|| nick2
== NULL
||
2177 !g_utf8_validate(nick1
, -1, NULL
) ||
2178 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
2180 nick1_norm
= g_utf8_casefold(nick1
, -1);
2181 nick2_norm
= g_utf8_casefold(nick2
, -1);
2182 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
2189 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
2191 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
2192 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
2193 (GEqualFunc
) sipe_ht_equals_nick
);
2194 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
2196 sipe_private
->buddies
= buddies
;