6 * Copyright (C) 2010-2013 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 g_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
;
522 const char *activity_str
;
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 activity_str
= sbuddy
->activity
? sbuddy
->activity
:
530 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
533 if (activity_str
&& sbuddy
->note
) {
534 return g_strdup_printf("%s - <i>%s</i>", activity_str
, sbuddy
->note
);
535 } else if (activity_str
) {
536 return g_strdup(activity_str
);
537 } else if (sbuddy
->note
) {
538 return g_strdup_printf("<i>%s</i>", sbuddy
->note
);
544 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
547 sipe_backend_buddy pbuddy
;
549 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
550 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
555 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
557 const gchar
*old_group_name
,
558 const gchar
*new_group_name
)
560 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
,
562 struct sipe_group
*old_group
= NULL
;
563 struct sipe_group
*new_group
;
565 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' old group '%s' new group '%s'",
567 old_group_name
? old_group_name
: "<UNDEFINED>",
568 new_group_name
? new_group_name
: "<UNDEFINED>");
571 /* buddy not in roaming list */
574 old_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, old_group_name
);
576 sipe_buddy_remove_group(buddy
, old_group
);
577 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' removed from old group '%s'",
578 who
, old_group_name
);
581 new_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, new_group_name
);
583 sipe_buddy_insert_group(buddy
, new_group
);
584 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' added to new group '%s'",
585 who
, new_group_name
);
588 if (sipe_ucs_is_migrated(SIPE_CORE_PRIVATE
)) {
593 * 1. new buddy added to existing group
594 * 2. existing buddy moved from old to existing group
596 sipe_ucs_group_add_buddy(SIPE_CORE_PRIVATE
,
601 sipe_ucs_group_remove_buddy(SIPE_CORE_PRIVATE
,
605 } else if (old_group
) {
607 * 3. existing buddy removed from one of its groups
608 * 4. existing buddy removed from last group
610 sipe_ucs_group_remove_buddy(SIPE_CORE_PRIVATE
,
613 if (g_slist_length(buddy
->groups
) < 1)
614 sipe_buddy_remove(SIPE_CORE_PRIVATE
,
616 /* buddy no longer valid */
619 /* non-UCS handling */
620 } else if (new_group
)
621 sipe_group_update_buddy(SIPE_CORE_PRIVATE
, buddy
);
623 /* 5. buddy added to new group */
625 sipe_group_create(SIPE_CORE_PRIVATE
, new_group_name
, who
);
628 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
630 const gchar
*group_name
)
632 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
634 if (!sipe_buddy_find_by_uri(sipe_private
, uri
))
635 sipe_buddy_add(sipe_private
,
640 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
643 sipe_core_buddy_group(sipe_public
,
649 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
650 struct sipe_buddy
*buddy
)
652 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
653 const gchar
*uri
= buddy
->name
;
654 GSList
*entry
= buddy
->groups
;
655 gchar
*action_name
= sipe_utils_presence_key(uri
);
657 sipe_schedule_cancel(sipe_private
, action_name
);
660 /* If the buddy still has groups, we need to delete backend buddies */
662 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
663 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
666 /* this should never be NULL */
668 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, oldb
);
673 g_hash_table_remove(buddies
->uri
, uri
);
674 if (buddy
->exchange_key
)
675 g_hash_table_remove(buddies
->exchange_key
,
676 buddy
->exchange_key
);
682 * Unassociates buddy from group first.
683 * Then see if no groups left, removes buddy completely.
684 * Otherwise updates buddy groups on server.
686 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
688 const gchar
*group_name
)
690 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
691 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
693 struct sipe_group
*group
= NULL
;
698 group
= sipe_group_find_by_name(sipe_private
, group_name
);
700 sipe_buddy_remove_group(buddy
, group
);
701 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy '%s' removed from group '%s'",
706 if (g_slist_length(buddy
->groups
) < 1) {
708 if (sipe_ucs_is_migrated(sipe_private
)) {
709 sipe_ucs_group_remove_buddy(sipe_private
,
713 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
715 sip_soap_request(sipe_private
,
721 sipe_buddy_remove(sipe_private
, buddy
);
723 if (sipe_ucs_is_migrated(sipe_private
)) {
724 sipe_ucs_group_remove_buddy(sipe_private
,
728 /* updates groups on server */
729 sipe_group_update_buddy(sipe_private
, buddy
);
733 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
737 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
738 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
743 /* Check if on 2005 system contact's calendar,
744 * then set/preserve it.
746 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
747 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
749 sipe_ocs2005_apply_calendar_status(sipe_private
,
751 sipe_status_activity_to_token(activity
));
755 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
757 const gchar
*status_name
,
759 struct sipe_backend_buddy_tooltip
*tooltip
)
761 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
763 gboolean is_oof_note
= FALSE
;
764 const gchar
*activity
= NULL
;
765 gchar
*calendar
= NULL
;
766 const gchar
*meeting_subject
= NULL
;
767 const gchar
*meeting_location
= NULL
;
768 gchar
*access_text
= NULL
;
770 #define SIPE_ADD_BUDDY_INFO(l, t) \
772 gchar *tmp = g_markup_escape_text((t), -1); \
773 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
776 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
777 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
779 if (sipe_public
) { /* happens on pidgin exit */
780 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
784 is_oof_note
= sbuddy
->is_oof_note
;
785 activity
= sbuddy
->activity
;
786 calendar
= sipe_cal_get_description(sbuddy
);
787 meeting_subject
= sbuddy
->meeting_subject
;
788 meeting_location
= sbuddy
->meeting_location
;
790 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
791 gboolean is_group_access
= FALSE
;
792 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
794 sipe_get_no_sip_uri(uri
),
796 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
797 access_text
= is_group_access
?
798 g_strdup(access_level
) :
799 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
805 const gchar
*status_str
= activity
? activity
: status_name
;
807 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
809 if (is_online
&& !is_empty(calendar
)) {
810 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
813 if (!is_empty(meeting_location
)) {
814 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
815 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
817 if (!is_empty(meeting_subject
)) {
818 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
819 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
822 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
823 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
824 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
826 g_free(note_italics
);
829 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
834 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
836 sipe_buddy_info_fields propkey
,
837 char *property_value
)
839 GSList
*buddies
, *entry
;
842 property_value
= g_strstrip(property_value
);
844 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
847 sipe_backend_buddy p_buddy
= entry
->data
;
849 /* for Display Name */
850 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
852 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
853 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
854 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
855 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
859 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
860 if (!is_empty(property_value
) &&
861 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
863 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
864 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
868 /* for other properties */
870 if (!is_empty(property_value
)) {
871 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
872 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
873 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
881 g_slist_free(buddies
);
890 sipe_svc_callback
*callback
;
891 struct sipe_svc_session
*session
;
892 gchar
*wsse_security
;
893 struct sipe_backend_search_token
*token
;
894 /* must call ms_dlx_free() */
895 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
896 struct ms_dlx_data
*mdd
);
899 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
901 sipe_utils_slist_free_full(mdd
->search_rows
, g_free
);
902 sipe_svc_session_close(mdd
->session
);
904 g_free(mdd
->wsse_security
);
908 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
909 #define DLX_SEARCH_ITEM \
910 "<AbEntryRequest.ChangeSearchQuery>" \
911 " <SearchOn>%s</SearchOn>" \
912 " <Value>%s</Value>" \
913 "</AbEntryRequest.ChangeSearchQuery>"
915 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
916 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
924 attr
= query_rows
->data
;
925 query_rows
= g_slist_next(query_rows
);
926 value
= query_rows
->data
;
927 query_rows
= g_slist_next(query_rows
);
932 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
938 query
= g_strjoinv(NULL
, attrs
);
939 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
948 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
949 const gchar
*base_uri
,
950 const gchar
*auth_uri
,
951 const gchar
*wsse_security
,
952 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
953 gpointer callback_data
)
955 struct ms_dlx_data
*mdd
= callback_data
;
958 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
960 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
963 if (sipe_svc_ab_entry_request(sipe_private
,
968 g_slist_length(mdd
->search_rows
) / 2,
973 /* keep webticket security token for potential further use */
974 mdd
->wsse_security
= g_strdup(wsse_security
);
976 /* callback data passed down the line */
982 /* no ticket: this will show the minmum information */
983 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
988 mdd
->failed_callback(sipe_private
, mdd
);
991 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
992 struct ms_dlx_data
*mdd
)
994 if (!sipe_webticket_request(sipe_private
,
996 sipe_private
->dlx_uri
,
997 "AddressBookWebTicketBearer",
1000 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
1001 sipe_private
->dlx_uri
);
1002 mdd
->failed_callback(sipe_private
, mdd
);
1006 static void search_contacts_finalize(struct sipe_core_private
*sipe_private
,
1007 struct sipe_backend_search_results
*results
,
1011 gchar
*secondary
= g_strdup_printf(
1012 dngettext(PACKAGE_NAME
,
1013 "Found %d contact%s:",
1014 "Found %d contacts%s:", match_count
),
1015 match_count
, more
? _(" (more matched your query)") : "");
1017 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
1024 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
1026 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1027 sipe_xml
*soap_body
,
1028 gpointer callback_data
)
1030 struct ms_dlx_data
*mdd
= callback_data
;
1033 const sipe_xml
*node
;
1034 struct sipe_backend_search_results
*results
;
1037 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
1041 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
1043 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
1044 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1046 _("No contacts found"));
1051 /* OK, we found something - show the results to the user */
1052 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1055 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
1056 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1058 _("Unable to display the search results"));
1063 /* SearchAbEntryResult can contain duplicates */
1064 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1067 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
1068 const sipe_xml
*attrs
;
1069 gchar
*sip_uri
= NULL
;
1070 gchar
*displayname
= NULL
;
1071 gchar
*company
= NULL
;
1072 gchar
*country
= NULL
;
1073 gchar
*email
= NULL
;
1075 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
1077 attrs
= sipe_xml_twin(attrs
)) {
1078 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
1080 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
1083 if (!is_empty(value
)) {
1084 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
1088 } else if (sipe_strcase_equal(name
, "displayname")) {
1089 g_free(displayname
);
1090 displayname
= value
;
1092 } else if (sipe_strcase_equal(name
, "mail")) {
1096 } else if (sipe_strcase_equal(name
, "company")) {
1100 } else if (sipe_strcase_equal(name
, "country")) {
1111 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
1112 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
1113 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1120 g_strfreev(uri_parts
);
1122 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
1129 g_free(displayname
);
1133 search_contacts_finalize(sipe_private
, results
,
1134 g_hash_table_size(found
),
1136 g_hash_table_destroy(found
);
1140 mdd
->failed_callback(sipe_private
, mdd
);
1144 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
1146 struct transaction
*trans
)
1148 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
1149 struct sipe_backend_search_results
*results
;
1150 sipe_xml
*searchResults
;
1151 const sipe_xml
*mrow
;
1152 guint match_count
= 0;
1153 gboolean more
= FALSE
;
1155 /* valid response? */
1156 if (msg
->response
!= 200) {
1157 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1159 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1161 _("Contact search failed"));
1165 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
1168 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1169 if (!searchResults
) {
1170 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1171 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1173 _("Contact search failed"));
1178 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
1180 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1181 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1183 _("No contacts found"));
1185 sipe_xml_free(searchResults
);
1189 /* OK, we found something - show the results to the user */
1190 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1191 trans
->payload
->data
);
1193 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1194 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1196 _("Unable to display the search results"));
1198 sipe_xml_free(searchResults
);
1202 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
1203 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
1204 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1207 sipe_xml_attribute(mrow
, "displayName"),
1208 sipe_xml_attribute(mrow
, "company"),
1209 sipe_xml_attribute(mrow
, "country"),
1210 sipe_xml_attribute(mrow
, "email"));
1211 g_strfreev(uri_parts
);
1215 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
1216 char *data
= sipe_xml_data(mrow
);
1217 more
= (g_ascii_strcasecmp(data
, "true") == 0);
1221 search_contacts_finalize(sipe_private
, results
, match_count
, more
);
1222 sipe_xml_free(searchResults
);
1227 static void search_soap_request(struct sipe_core_private
*sipe_private
,
1228 struct sipe_backend_search_token
*token
,
1229 GSList
*search_rows
)
1231 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
1232 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1234 payload
->data
= token
;
1236 sip_soap_directory_search(sipe_private
,
1239 process_search_contact_response
,
1244 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1245 struct ms_dlx_data
*mdd
)
1247 /* error using [MS-DLX] server, retry using Active Directory */
1248 search_soap_request(sipe_private
, mdd
->token
, mdd
->search_rows
);
1252 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
1253 struct sipe_backend_search_token
*token
,
1254 const gchar
*given_name
,
1255 const gchar
*surname
,
1257 const gchar
*company
,
1258 const gchar
*country
)
1260 GSList
*query_rows
= NULL
;
1262 #define ADD_QUERY_ROW(attr, val) \
1264 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1265 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1268 ADD_QUERY_ROW("givenName", given_name
);
1269 ADD_QUERY_ROW("sn", surname
);
1270 ADD_QUERY_ROW("mail", email
);
1271 ADD_QUERY_ROW("company", company
);
1272 ADD_QUERY_ROW("c", country
);
1275 if (SIPE_CORE_PRIVATE
->dlx_uri
!= NULL
) {
1276 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1278 mdd
->search_rows
= query_rows
;
1279 mdd
->max_returns
= 100;
1280 mdd
->callback
= search_ab_entry_response
;
1281 mdd
->failed_callback
= search_ab_entry_failed
;
1282 mdd
->session
= sipe_svc_session_start();
1285 ms_dlx_webticket_request(SIPE_CORE_PRIVATE
, mdd
);
1288 /* no [MS-DLX] server, use Active Directory search instead */
1289 search_soap_request(SIPE_CORE_PRIVATE
, token
, query_rows
);
1290 sipe_utils_slist_free_full(query_rows
, g_free
);
1293 sipe_backend_search_failed(sipe_public
,
1295 _("Invalid contact search query"));
1298 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1299 struct sipe_backend_buddy_info
*info
,
1301 const gchar
*server_alias
,
1304 sipe_backend_buddy bbuddy
;
1305 struct sipe_buddy
*sbuddy
;
1310 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1312 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1317 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1319 if (is_empty(server_alias
)) {
1320 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1323 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1325 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1329 value
= g_strdup(server_alias
);
1332 /* present alias if it differs from server alias */
1333 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1334 if (alias
&& !sipe_strequal(alias
, value
))
1336 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1338 SIPE_BUDDY_INFO_ALIAS
,
1344 if (is_empty(email
)) {
1345 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1347 SIPE_BUDDY_INFO_EMAIL
);
1349 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1351 SIPE_BUDDY_INFO_EMAIL
,
1357 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1359 SIPE_BUDDY_INFO_SITE
);
1361 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1363 SIPE_BUDDY_INFO_SITE
,
1368 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1369 if (sbuddy
&& sbuddy
->device_name
) {
1370 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1372 SIPE_BUDDY_INFO_DEVICE
,
1373 sbuddy
->device_name
);
1376 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1380 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1382 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1383 sipe_xml
*soap_body
,
1384 gpointer callback_data
)
1386 struct ms_dlx_data
*mdd
= callback_data
;
1387 struct sipe_backend_buddy_info
*info
= NULL
;
1388 gchar
*server_alias
= NULL
;
1389 gchar
*email
= NULL
;
1392 const sipe_xml
*node
;
1394 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1397 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1399 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1401 node
= sipe_xml_twin(node
)) {
1402 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1404 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1406 const sipe_xml
*values
= sipe_xml_child(node
,
1409 /* Single value entries */
1410 if (!is_empty(value
)) {
1412 if (sipe_strcase_equal(name
, "displayname")) {
1413 g_free(server_alias
);
1414 server_alias
= value
;
1416 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1418 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1420 } else if (sipe_strcase_equal(name
, "mail")) {
1424 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1426 SIPE_BUDDY_INFO_EMAIL
,
1428 } else if (sipe_strcase_equal(name
, "title")) {
1429 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1431 SIPE_BUDDY_INFO_JOB_TITLE
,
1433 } else if (sipe_strcase_equal(name
, "company")) {
1434 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1436 SIPE_BUDDY_INFO_COMPANY
,
1438 } else if (sipe_strcase_equal(name
, "country")) {
1439 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1441 SIPE_BUDDY_INFO_COUNTRY
,
1445 } else if (values
) {
1446 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1449 if (sipe_strcase_equal(name
, "telephonenumber")) {
1450 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1452 SIPE_BUDDY_INFO_WORK_PHONE
,
1464 /* this will show the minmum information */
1465 get_info_finalize(sipe_private
,
1472 g_free(server_alias
);
1476 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1478 struct transaction
*trans
)
1480 const gchar
*uri
= trans
->payload
->data
;
1481 struct sipe_backend_buddy_info
*info
= NULL
;
1482 gchar
*server_alias
= NULL
;
1483 gchar
*email
= NULL
;
1485 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1486 uri
, sipe_private
->username
);
1488 if (msg
->response
!= 200) {
1489 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1491 sipe_xml
*searchResults
;
1492 const sipe_xml
*mrow
;
1494 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1495 msg
->body
? msg
->body
: "");
1497 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1498 if (!searchResults
) {
1500 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1502 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1504 gchar
*phone_number
;
1506 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1508 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1509 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1510 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1513 * For 2007 system we will take this from ContactCard -
1514 * it has cleaner tel: URIs at least
1516 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1517 char *tel_uri
= sip_to_tel_uri(phone_number
);
1518 /* trims its parameters, so call first */
1519 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1520 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1521 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1522 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1525 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1529 if (!is_empty(server_alias
)) {
1530 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1532 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1535 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1536 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1538 SIPE_BUDDY_INFO_JOB_TITLE
,
1541 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1542 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1544 SIPE_BUDDY_INFO_OFFICE
,
1547 if (!is_empty(phone_number
)) {
1548 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1550 SIPE_BUDDY_INFO_WORK_PHONE
,
1553 g_free(phone_number
);
1554 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1555 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1557 SIPE_BUDDY_INFO_COMPANY
,
1560 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1561 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1563 SIPE_BUDDY_INFO_CITY
,
1566 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1567 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1569 SIPE_BUDDY_INFO_STATE
,
1572 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1573 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1575 SIPE_BUDDY_INFO_COUNTRY
,
1578 if (!is_empty(email
)) {
1579 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1581 SIPE_BUDDY_INFO_EMAIL
,
1585 sipe_xml_free(searchResults
);
1588 /* this will show the minmum information */
1589 get_info_finalize(sipe_private
,
1595 g_free(server_alias
);
1601 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1602 struct ms_dlx_data
*mdd
)
1604 /* error using [MS-DLX] server, retry using Active Directory */
1605 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, FALSE
);
1606 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1608 payload
->destroy
= g_free
;
1609 payload
->data
= mdd
->other
;
1612 sip_soap_directory_search(sipe_private
,
1615 process_get_info_response
,
1622 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1625 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1627 if (sipe_private
->dlx_uri
) {
1628 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1630 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1631 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(who
));
1633 mdd
->other
= g_strdup(who
);
1634 mdd
->max_returns
= 1;
1635 mdd
->callback
= get_info_ab_entry_response
;
1636 mdd
->failed_callback
= get_info_ab_entry_failed
;
1637 mdd
->session
= sipe_svc_session_start();
1639 ms_dlx_webticket_request(sipe_private
, mdd
);
1642 /* no [MS-DLX] server, use Active Directory search instead */
1643 gchar
*row
= g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW
,
1644 "msRTCSIP-PrimaryUserAddress",
1646 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1648 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1651 payload
->destroy
= g_free
;
1652 payload
->data
= g_strdup(who
);
1654 sip_soap_directory_search(sipe_private
,
1657 process_get_info_response
,
1663 static void photo_response_data_free(struct photo_response_data
*data
)
1666 g_free(data
->photo_hash
);
1667 if (data
->request
) {
1668 sipe_http_request_cancel(data
->request
);
1673 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1679 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1681 rdata
->request
= NULL
;
1683 if (status
== SIPE_HTTP_STATUS_OK
) {
1684 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1687 gsize photo_size
= atoi(len_str
);
1688 gpointer photo
= g_new(char, photo_size
);
1691 memcpy(photo
, body
, photo_size
);
1693 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1702 sipe_private
->buddies
->pending_photo_requests
=
1703 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, rdata
);
1705 photo_response_data_free(rdata
);
1708 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1710 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "saml:Assertion", TRUE
);
1711 gchar
*wsse_security_base64
;
1712 gchar
*x_ms_webticket_header
;
1718 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1720 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1721 wsse_security_base64
);
1724 g_free(wsse_security_base64
);
1726 return x_ms_webticket_header
;
1729 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
1731 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1732 sipe_xml
*soap_body
,
1733 gpointer callback_data
)
1735 struct ms_dlx_data
*mdd
= callback_data
;
1736 gchar
*photo_rel_path
= NULL
;
1737 gchar
*photo_hash
= NULL
;
1738 const gchar
*photo_hash_old
=
1739 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, mdd
->other
);
1742 const sipe_xml
*node
;
1744 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1747 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1749 node
= sipe_xml_twin(node
)) {
1750 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
1751 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
1753 if (!is_empty(value
)) {
1754 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
1755 g_free(photo_rel_path
);
1756 photo_rel_path
= value
;
1758 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
1770 if (sipe_private
->addressbook_uri
&& photo_rel_path
&&
1771 photo_hash
&& !sipe_strequal(photo_hash
, photo_hash_old
)) {
1772 gchar
*photo_url
= g_strdup_printf("%s/%s",
1773 sipe_private
->addressbook_uri
, photo_rel_path
);
1774 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
1776 struct photo_response_data
*data
= g_new(struct photo_response_data
, 1);
1777 data
->who
= g_strdup(mdd
->other
);
1778 data
->photo_hash
= photo_hash
;
1781 data
->request
= sipe_http_request_get(sipe_private
,
1783 x_ms_webticket_header
,
1784 process_buddy_photo_response
,
1787 if (data
->request
) {
1788 sipe_private
->buddies
->pending_photo_requests
=
1789 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1790 sipe_http_request_ready(data
->request
);
1792 photo_response_data_free(data
);
1795 g_free(x_ms_webticket_header
);
1799 g_free(photo_rel_path
);
1804 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
1805 struct ms_dlx_data
*mdd
)
1810 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
1813 if (sipe_backend_uses_photo()) {
1815 /* Lync 2013 or newer: use UCS */
1816 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
)) {
1818 sipe_ucs_get_photo(sipe_private
, uri
);
1820 /* Lync 2010: use [MS-DLX] */
1821 } else if (sipe_private
->dlx_uri
&&
1822 sipe_private
->addressbook_uri
) {
1823 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1825 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1826 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(uri
));
1828 mdd
->other
= g_strdup(uri
);
1829 mdd
->max_returns
= 1;
1830 mdd
->callback
= get_photo_ab_entry_response
;
1831 mdd
->failed_callback
= get_photo_ab_entry_failed
;
1832 mdd
->session
= sipe_svc_session_start();
1834 ms_dlx_webticket_request(sipe_private
, mdd
);
1839 static void buddy_refresh_photos_cb(gpointer uri
,
1840 SIPE_UNUSED_PARAMETER gpointer value
,
1841 gpointer sipe_private
)
1843 buddy_fetch_photo(sipe_private
, uri
);
1846 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
1848 g_hash_table_foreach(sipe_private
->buddies
->uri
,
1849 buddy_refresh_photos_cb
,
1853 /* Buddy menu callbacks*/
1855 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
1858 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1860 /* 2007+ conference */
1861 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1862 sipe_conf_add(sipe_private
, who
);
1864 /* 2005- multiparty chat */
1866 gchar
*self
= sip_uri_self(sipe_private
);
1867 struct sip_session
*session
;
1869 session
= sipe_session_add_chat(sipe_private
,
1873 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
1874 session
->chat_session
,
1875 session
->chat_session
->title
,
1879 sipe_im_invite(sipe_private
, session
, who
,
1880 NULL
, NULL
, NULL
, FALSE
);
1884 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
1887 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1890 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1892 SIPE_BUDDY_INFO_EMAIL
);
1895 gchar
*command_line
= g_strdup_printf(
1901 " mailto:%s", email
);
1904 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1906 g_spawn_command_line_async(command_line
, NULL
);
1907 g_free(command_line
);
1910 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1917 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
1918 struct sipe_backend_buddy_menu
*menu
,
1919 sipe_backend_buddy buddy
,
1920 sipe_buddy_info_fields id_phone
,
1921 sipe_buddy_info_fields id_display
,
1924 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
1928 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
1932 gchar
*label
= g_strdup_printf("%s %s",
1935 (tmp
= sip_tel_uri_denormalize(phone
)));
1936 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1939 SIPE_BUDDY_MENU_MAKE_CALL
,
1950 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
1951 const gchar
*buddy_name
,
1952 struct sipe_backend_buddy_menu
*menu
)
1954 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1955 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1958 gchar
*self
= sip_uri_self(sipe_private
);
1960 SIPE_SESSION_FOREACH
{
1961 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
1963 struct sipe_chat_session
*chat_session
= session
->chat_session
;
1964 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
1966 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
1968 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
1972 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
1973 /* We are a conf OP */
1975 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
1976 chat_session
->title
);
1977 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1980 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
1986 /* We are a conf OP */
1988 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
1989 chat_session
->title
);
1990 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1993 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
2001 (is_conf
&& !session
->locked
)) {
2002 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
2003 chat_session
->title
);
2004 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2007 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
2013 } SIPE_SESSION_FOREACH_END
;
2016 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2019 SIPE_BUDDY_MENU_NEW_CHAT
,
2022 /* add buddy's phone numbers if we have call control */
2023 if (sip_csta_is_idle(sipe_private
)) {
2026 menu
= buddy_menu_phone(sipe_public
,
2029 SIPE_BUDDY_INFO_WORK_PHONE
,
2030 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
2033 menu
= buddy_menu_phone(sipe_public
,
2036 SIPE_BUDDY_INFO_MOBILE_PHONE
,
2037 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
2041 menu
= buddy_menu_phone(sipe_public
,
2044 SIPE_BUDDY_INFO_HOME_PHONE
,
2045 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
2049 menu
= buddy_menu_phone(sipe_public
,
2052 SIPE_BUDDY_INFO_OTHER_PHONE
,
2053 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
2057 menu
= buddy_menu_phone(sipe_public
,
2060 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
2061 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
2066 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
2068 SIPE_BUDDY_INFO_EMAIL
);
2070 menu
= sipe_backend_buddy_menu_add(sipe_public
,
2073 SIPE_BUDDY_MENU_SEND_EMAIL
,
2079 /* access level control */
2080 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
2081 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
2084 sipe_ocs2007_access_control_menu(sipe_private
,
2090 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
2092 return(g_hash_table_size(sipe_private
->buddies
->uri
));
2095 static guint
sipe_ht_hash_nick(const char *nick
)
2097 char *lc
= g_utf8_strdown(nick
, -1);
2098 guint bucket
= g_str_hash(lc
);
2104 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
2106 char *nick1_norm
= NULL
;
2107 char *nick2_norm
= NULL
;
2110 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
2111 if (nick1
== NULL
|| nick2
== NULL
||
2112 !g_utf8_validate(nick1
, -1, NULL
) ||
2113 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
2115 nick1_norm
= g_utf8_casefold(nick1
, -1);
2116 nick2_norm
= g_utf8_casefold(nick2
, -1);
2117 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
2124 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
2126 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
2127 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
2128 (GEqualFunc
) sipe_ht_equals_nick
);
2129 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
2131 sipe_private
->buddies
= buddies
;