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
;
73 struct photo_response_data
{
76 struct sipe_http_request
*request
;
79 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
81 static void photo_response_data_free(struct photo_response_data
*data
);
83 struct sipe_buddy
*sipe_buddy_add(struct sipe_core_private
*sipe_private
,
85 const gchar
*exchange_key
,
86 const gchar
*change_key
)
88 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
89 gchar
*normalized_uri
= g_ascii_strdown(uri
, -1);
90 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
94 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
96 buddy
= g_new0(struct sipe_buddy
, 1);
97 buddy
->name
= normalized_uri
;
98 g_hash_table_insert(buddies
->uri
,
103 buddy
->exchange_key
= g_strdup(exchange_key
);
104 g_hash_table_insert(buddies
->exchange_key
,
109 buddy
->change_key
= g_strdup(change_key
);
112 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri
);
114 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES
)) {
115 buddy
->just_added
= TRUE
;
116 sipe_subscribe_presence_single_cb(sipe_private
,
120 buddy_fetch_photo(sipe_private
, normalized_uri
);
122 normalized_uri
= NULL
; /* buddy takes ownership */
124 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri
);
125 buddy
->is_obsolete
= FALSE
;
127 g_free(normalized_uri
);
132 static gboolean
is_buddy_in_group(struct sipe_buddy
*buddy
,
136 GSList
*entry
= buddy
->groups
;
139 struct buddy_group_data
*bgd
= entry
->data
;
140 if (sipe_strequal(bgd
->group
->name
, name
))
149 void sipe_buddy_add_to_group(struct sipe_core_private
*sipe_private
,
150 struct sipe_buddy
*buddy
,
151 struct sipe_group
*group
,
154 const gchar
*uri
= buddy
->name
;
155 const gchar
*group_name
= group
->name
;
156 sipe_backend_buddy bb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
161 bb
= sipe_backend_buddy_add(SIPE_CORE_PUBLIC
,
165 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
166 uri
, alias
? alias
: "<NONE>");
170 if (!is_empty(alias
)) {
171 gchar
*old_alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
174 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri
),
176 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
,
179 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
180 uri
, old_alias
, alias
);
185 if (!is_buddy_in_group(buddy
, group_name
)) {
186 sipe_buddy_insert_group(buddy
, group
);
187 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
192 static gint
buddy_group_compare(gconstpointer a
, gconstpointer b
)
194 return(((const struct buddy_group_data
*)a
)->group
->id
-
195 ((const struct buddy_group_data
*)b
)->group
->id
);
198 void sipe_buddy_insert_group(struct sipe_buddy
*buddy
,
199 struct sipe_group
*group
)
201 struct buddy_group_data
*bgd
= g_new0(struct buddy_group_data
, 1);
205 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
211 static void buddy_group_free(gpointer data
)
216 static void sipe_buddy_remove_group(struct sipe_buddy
*buddy
,
217 const struct sipe_group
*group
)
219 GSList
*entry
= buddy
->groups
;
220 struct buddy_group_data
*bgd
= NULL
;
224 if (bgd
->group
== group
)
229 buddy
->groups
= g_slist_remove(buddy
->groups
, bgd
);
230 buddy_group_free(bgd
);
233 void sipe_buddy_update_groups(struct sipe_core_private
*sipe_private
,
234 struct sipe_buddy
*buddy
,
237 const gchar
*uri
= buddy
->name
;
238 GSList
*entry
= buddy
->groups
;
241 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
243 /* next buddy group */
246 /* old group NOT found in new list? */
247 if (g_slist_find(new_groups
, group
) == NULL
) {
248 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
251 SIPE_DEBUG_INFO("sipe_buddy_update_groups: removing buddy %s from group '%s'",
253 /* this should never be NULL */
255 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
257 sipe_buddy_remove_group(buddy
, group
);
262 gchar
*sipe_buddy_groups_string(struct sipe_buddy
*buddy
)
266 /* creating array from GList, converting guint to gchar * */
267 gchar
**ids_arr
= g_new(gchar
*, g_slist_length(buddy
->groups
) + 1);
268 GSList
*entry
= buddy
->groups
;
274 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
275 ids_arr
[i
] = g_strdup_printf("%u", group
->id
);
281 string
= g_strjoinv(" ", ids_arr
);
287 void sipe_buddy_cleanup_local_list(struct sipe_core_private
*sipe_private
)
289 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
292 GSList
*entry
= buddies
;
294 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
295 g_slist_length(buddies
));
296 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
297 sipe_buddy_count(sipe_private
));
299 sipe_backend_buddy bb
= entry
->data
;
300 gchar
*bname
= sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC
,
302 gchar
*gname
= sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC
,
304 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
307 if (!is_buddy_in_group(buddy
, gname
)) {
308 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",
310 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, bb
);
319 g_slist_free(buddies
);
322 struct sipe_buddy
*sipe_buddy_find_by_uri(struct sipe_core_private
*sipe_private
,
325 return(g_hash_table_lookup(sipe_private
->buddies
->uri
, uri
));
328 struct sipe_buddy
*sipe_buddy_find_by_exchange_key(struct sipe_core_private
*sipe_private
,
329 const gchar
*exchange_key
)
331 return(g_hash_table_lookup(sipe_private
->buddies
->exchange_key
,
335 void sipe_buddy_foreach(struct sipe_core_private
*sipe_private
,
337 gpointer callback_data
)
339 g_hash_table_foreach(sipe_private
->buddies
->uri
,
344 static void buddy_free(struct sipe_buddy
*buddy
)
348 * We are calling g_hash_table_foreach_steal(). That means that no
349 * key/value deallocation functions are called. Therefore the glib
350 * hash code does not touch the key (buddy->name) or value (buddy)
351 * of the to-be-deleted hash node at all. It follows that we
353 * - MUST free the memory for the key ourselves and
354 * - ARE allowed to do it in this function
356 * Conclusion: glib must be broken on the Windows platform if sipe
357 * crashes with SIGTRAP when closing. You'll have to live
358 * with the memory leak until this is fixed.
362 g_free(buddy
->exchange_key
);
363 g_free(buddy
->change_key
);
364 g_free(buddy
->activity
);
365 g_free(buddy
->meeting_subject
);
366 g_free(buddy
->meeting_location
);
369 g_free(buddy
->cal_start_time
);
370 g_free(buddy
->cal_free_busy_base64
);
371 g_free(buddy
->cal_free_busy
);
372 g_free(buddy
->last_non_cal_activity
);
374 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
376 g_free(buddy
->device_name
);
377 g_slist_free_full(buddy
->groups
, buddy_group_free
);
381 static gboolean
buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key
,
383 SIPE_UNUSED_PARAMETER gpointer user_data
)
386 /* We must return TRUE as the key/value have already been deleted */
390 void sipe_buddy_free(struct sipe_core_private
*sipe_private
)
392 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
394 g_hash_table_foreach_steal(buddies
->uri
,
398 /* core is being deallocated, remove all its pending photo requests */
399 while (buddies
->pending_photo_requests
) {
400 struct photo_response_data
*data
=
401 buddies
->pending_photo_requests
->data
;
402 buddies
->pending_photo_requests
=
403 g_slist_remove(buddies
->pending_photo_requests
, data
);
404 photo_response_data_free(data
);
407 g_hash_table_destroy(buddies
->uri
);
408 g_hash_table_destroy(buddies
->exchange_key
);
410 sipe_private
->buddies
= NULL
;
413 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
415 SIPE_UNUSED_PARAMETER gpointer user_data
)
417 ((struct sipe_buddy
*) value
)->is_obsolete
= TRUE
;
420 void sipe_buddy_update_start(struct sipe_core_private
*sipe_private
)
422 g_hash_table_foreach(sipe_private
->buddies
->uri
,
423 buddy_set_obsolete_flag
,
427 static gboolean
buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
431 struct sipe_core_private
*sipe_private
= user_data
;
432 struct sipe_buddy
*buddy
= value
;
434 if (buddy
->is_obsolete
) {
435 /* all backend buddies in different groups */
436 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
439 GSList
*entry
= buddies
;
441 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
442 g_slist_length(buddies
),
446 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
450 g_slist_free(buddies
);
453 /* return TRUE as the key/value have already been deleted */
459 void sipe_buddy_update_finish(struct sipe_core_private
*sipe_private
)
461 g_hash_table_foreach_remove(sipe_private
->buddies
->uri
,
462 buddy_check_obsolete_flag
,
466 gchar
*sipe_core_buddy_status(struct sipe_core_public
*sipe_public
,
469 const gchar
*status_text
)
471 struct sipe_buddy
*sbuddy
;
472 const char *activity_str
;
474 if (!sipe_public
) return NULL
; /* happens on pidgin exit */
476 sbuddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
, uri
);
477 if (!sbuddy
) return NULL
;
479 activity_str
= sbuddy
->activity
? sbuddy
->activity
:
480 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
483 if (activity_str
&& sbuddy
->note
) {
484 return g_strdup_printf("%s - <i>%s</i>", activity_str
, sbuddy
->note
);
485 } else if (activity_str
) {
486 return g_strdup(activity_str
);
487 } else if (sbuddy
->note
) {
488 return g_strdup_printf("<i>%s</i>", sbuddy
->note
);
494 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
497 sipe_backend_buddy pbuddy
;
499 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
500 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
505 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
507 const gchar
*old_group_name
,
508 const gchar
*new_group_name
)
510 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
,
512 struct sipe_group
*old_group
= NULL
;
513 struct sipe_group
*new_group
;
515 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
516 who
? who
: "", old_group_name
? old_group_name
: "", new_group_name
? new_group_name
: "");
519 /* buddy not in roaming list */
522 if (old_group_name
) {
523 old_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, old_group_name
);
525 new_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, new_group_name
);
528 sipe_buddy_remove_group(buddy
, old_group
);
529 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who
, old_group_name
);
533 sipe_group_create(SIPE_CORE_PRIVATE
, new_group_name
, who
);
535 sipe_buddy_insert_group(buddy
, new_group
);
536 sipe_group_update_buddy(SIPE_CORE_PRIVATE
, buddy
);
540 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
542 const gchar
*group_name
)
544 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
546 if (!sipe_buddy_find_by_uri(sipe_private
, uri
))
547 sipe_buddy_add(sipe_private
,
552 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
555 sipe_core_buddy_group(sipe_public
,
561 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
562 struct sipe_buddy
*buddy
)
564 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
565 const gchar
*uri
= buddy
->name
;
566 GSList
*entry
= buddy
->groups
;
567 gchar
*action_name
= sipe_utils_presence_key(uri
);
569 sipe_schedule_cancel(sipe_private
, action_name
);
572 /* If the buddy still has groups, we need to delete backend buddies */
574 const struct sipe_group
*group
= ((struct buddy_group_data
*) entry
->data
)->group
;
575 sipe_backend_buddy oldb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
578 /* this should never be NULL */
580 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, oldb
);
585 g_hash_table_remove(buddies
->uri
, uri
);
586 if (buddy
->exchange_key
)
587 g_hash_table_remove(buddies
->exchange_key
,
588 buddy
->exchange_key
);
594 * Unassociates buddy from group first.
595 * Then see if no groups left, removes buddy completely.
596 * Otherwise updates buddy groups on server.
598 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
600 const gchar
*group_name
)
602 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
603 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
605 struct sipe_group
*group
= NULL
;
610 group
= sipe_group_find_by_name(sipe_private
, group_name
);
612 sipe_buddy_remove_group(buddy
, group
);
613 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
618 if (g_slist_length(buddy
->groups
) < 1) {
620 if (sipe_ucs_is_migrated(sipe_private
)) {
621 sipe_ucs_group_remove_buddy(sipe_private
,
625 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
627 sip_soap_request(sipe_private
,
633 sipe_buddy_remove(sipe_private
, buddy
);
635 if (sipe_ucs_is_migrated(sipe_private
)) {
636 sipe_ucs_group_remove_buddy(sipe_private
,
640 /* updates groups on server */
641 sipe_group_update_buddy(sipe_private
, buddy
);
645 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
649 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
650 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
655 /* Check if on 2005 system contact's calendar,
656 * then set/preserve it.
658 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
659 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
661 sipe_ocs2005_apply_calendar_status(sipe_private
,
663 sipe_status_activity_to_token(activity
));
667 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
669 const gchar
*status_name
,
671 struct sipe_backend_buddy_tooltip
*tooltip
)
673 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
675 gboolean is_oof_note
= FALSE
;
676 const gchar
*activity
= NULL
;
677 gchar
*calendar
= NULL
;
678 const gchar
*meeting_subject
= NULL
;
679 const gchar
*meeting_location
= NULL
;
680 gchar
*access_text
= NULL
;
682 #define SIPE_ADD_BUDDY_INFO(l, t) \
684 gchar *tmp = g_markup_escape_text((t), -1); \
685 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
688 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
689 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
691 if (sipe_public
) { /* happens on pidgin exit */
692 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
696 is_oof_note
= sbuddy
->is_oof_note
;
697 activity
= sbuddy
->activity
;
698 calendar
= sipe_cal_get_description(sbuddy
);
699 meeting_subject
= sbuddy
->meeting_subject
;
700 meeting_location
= sbuddy
->meeting_location
;
702 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
703 gboolean is_group_access
= FALSE
;
704 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
706 sipe_get_no_sip_uri(uri
),
708 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
709 access_text
= is_group_access
?
710 g_strdup(access_level
) :
711 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
717 const gchar
*status_str
= activity
? activity
: status_name
;
719 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
721 if (is_online
&& !is_empty(calendar
)) {
722 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
725 if (!is_empty(meeting_location
)) {
726 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
727 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
729 if (!is_empty(meeting_subject
)) {
730 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
731 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
734 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
735 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
736 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
738 g_free(note_italics
);
741 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
746 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
748 sipe_buddy_info_fields propkey
,
749 char *property_value
)
751 GSList
*buddies
, *entry
;
754 property_value
= g_strstrip(property_value
);
756 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
759 sipe_backend_buddy p_buddy
= entry
->data
;
761 /* for Display Name */
762 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
764 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
765 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
766 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
767 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
771 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
772 if (!is_empty(property_value
) &&
773 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
775 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
776 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
780 /* for other properties */
782 if (!is_empty(property_value
)) {
783 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
784 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
785 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
793 g_slist_free(buddies
);
802 sipe_svc_callback
*callback
;
803 struct sipe_svc_session
*session
;
804 gchar
*wsse_security
;
805 struct sipe_backend_search_token
*token
;
806 /* must call ms_dlx_free() */
807 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
808 struct ms_dlx_data
*mdd
);
811 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
813 sipe_utils_slist_free_full(mdd
->search_rows
, g_free
);
814 sipe_svc_session_close(mdd
->session
);
816 g_free(mdd
->wsse_security
);
820 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
821 #define DLX_SEARCH_ITEM \
822 "<AbEntryRequest.ChangeSearchQuery>" \
823 " <SearchOn>%s</SearchOn>" \
824 " <Value>%s</Value>" \
825 "</AbEntryRequest.ChangeSearchQuery>"
827 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
828 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
836 attr
= query_rows
->data
;
837 query_rows
= g_slist_next(query_rows
);
838 value
= query_rows
->data
;
839 query_rows
= g_slist_next(query_rows
);
844 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
850 query
= g_strjoinv(NULL
, attrs
);
851 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
860 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
861 const gchar
*base_uri
,
862 const gchar
*auth_uri
,
863 const gchar
*wsse_security
,
864 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
865 gpointer callback_data
)
867 struct ms_dlx_data
*mdd
= callback_data
;
870 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
872 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
875 if (sipe_svc_ab_entry_request(sipe_private
,
880 g_slist_length(mdd
->search_rows
) / 2,
885 /* keep webticket security token for potential further use */
886 mdd
->wsse_security
= g_strdup(wsse_security
);
888 /* callback data passed down the line */
894 /* no ticket: this will show the minmum information */
895 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
900 mdd
->failed_callback(sipe_private
, mdd
);
903 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
904 struct ms_dlx_data
*mdd
)
906 if (!sipe_webticket_request(sipe_private
,
908 sipe_private
->dlx_uri
,
909 "AddressBookWebTicketBearer",
912 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
913 sipe_private
->dlx_uri
);
914 mdd
->failed_callback(sipe_private
, mdd
);
918 static void search_contacts_finalize(struct sipe_core_private
*sipe_private
,
919 struct sipe_backend_search_results
*results
,
923 gchar
*secondary
= g_strdup_printf(
924 dngettext(PACKAGE_NAME
,
925 "Found %d contact%s:",
926 "Found %d contacts%s:", match_count
),
927 match_count
, more
? _(" (more matched your query)") : "");
929 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
936 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
938 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
940 gpointer callback_data
)
942 struct ms_dlx_data
*mdd
= callback_data
;
945 const sipe_xml
*node
;
946 struct sipe_backend_search_results
*results
;
949 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
953 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
955 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
956 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
958 _("No contacts found"));
963 /* OK, we found something - show the results to the user */
964 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
967 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
968 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
970 _("Unable to display the search results"));
975 /* SearchAbEntryResult can contain duplicates */
976 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
979 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
980 const sipe_xml
*attrs
;
981 gchar
*sip_uri
= NULL
;
982 gchar
*displayname
= NULL
;
983 gchar
*company
= NULL
;
984 gchar
*country
= NULL
;
987 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
989 attrs
= sipe_xml_twin(attrs
)) {
990 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
992 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
995 if (!is_empty(value
)) {
996 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
1000 } else if (sipe_strcase_equal(name
, "displayname")) {
1001 g_free(displayname
);
1002 displayname
= value
;
1004 } else if (sipe_strcase_equal(name
, "mail")) {
1008 } else if (sipe_strcase_equal(name
, "company")) {
1012 } else if (sipe_strcase_equal(name
, "country")) {
1023 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
1024 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
1025 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1032 g_strfreev(uri_parts
);
1034 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
1041 g_free(displayname
);
1045 search_contacts_finalize(sipe_private
, results
,
1046 g_hash_table_size(found
),
1048 g_hash_table_destroy(found
);
1052 mdd
->failed_callback(sipe_private
, mdd
);
1056 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
1058 struct transaction
*trans
)
1060 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
1061 struct sipe_backend_search_results
*results
;
1062 sipe_xml
*searchResults
;
1063 const sipe_xml
*mrow
;
1064 guint match_count
= 0;
1065 gboolean more
= FALSE
;
1067 /* valid response? */
1068 if (msg
->response
!= 200) {
1069 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1071 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1073 _("Contact search failed"));
1077 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
1080 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1081 if (!searchResults
) {
1082 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1083 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1085 _("Contact search failed"));
1090 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
1092 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1093 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1095 _("No contacts found"));
1097 sipe_xml_free(searchResults
);
1101 /* OK, we found something - show the results to the user */
1102 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1103 trans
->payload
->data
);
1105 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1106 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1108 _("Unable to display the search results"));
1110 sipe_xml_free(searchResults
);
1114 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
1115 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
1116 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1119 sipe_xml_attribute(mrow
, "displayName"),
1120 sipe_xml_attribute(mrow
, "company"),
1121 sipe_xml_attribute(mrow
, "country"),
1122 sipe_xml_attribute(mrow
, "email"));
1123 g_strfreev(uri_parts
);
1127 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
1128 char *data
= sipe_xml_data(mrow
);
1129 more
= (g_ascii_strcasecmp(data
, "true") == 0);
1133 search_contacts_finalize(sipe_private
, results
, match_count
, more
);
1134 sipe_xml_free(searchResults
);
1139 static void search_soap_request(struct sipe_core_private
*sipe_private
,
1140 struct sipe_backend_search_token
*token
,
1141 GSList
*search_rows
)
1143 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
1144 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1146 payload
->data
= token
;
1148 sip_soap_directory_search(sipe_private
,
1151 process_search_contact_response
,
1156 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1157 struct ms_dlx_data
*mdd
)
1159 /* error using [MS-DLX] server, retry using Active Directory */
1160 search_soap_request(sipe_private
, mdd
->token
, mdd
->search_rows
);
1164 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
1165 struct sipe_backend_search_token
*token
,
1166 const gchar
*given_name
,
1167 const gchar
*surname
,
1169 const gchar
*company
,
1170 const gchar
*country
)
1172 GSList
*query_rows
= NULL
;
1174 #define ADD_QUERY_ROW(attr, val) \
1176 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1177 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1180 ADD_QUERY_ROW("givenName", given_name
);
1181 ADD_QUERY_ROW("sn", surname
);
1182 ADD_QUERY_ROW("mail", email
);
1183 ADD_QUERY_ROW("company", company
);
1184 ADD_QUERY_ROW("c", country
);
1187 if (SIPE_CORE_PRIVATE
->dlx_uri
!= NULL
) {
1188 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1190 mdd
->search_rows
= query_rows
;
1191 mdd
->max_returns
= 100;
1192 mdd
->callback
= search_ab_entry_response
;
1193 mdd
->failed_callback
= search_ab_entry_failed
;
1194 mdd
->session
= sipe_svc_session_start();
1197 ms_dlx_webticket_request(SIPE_CORE_PRIVATE
, mdd
);
1200 /* no [MS-DLX] server, use Active Directory search instead */
1201 search_soap_request(SIPE_CORE_PRIVATE
, token
, query_rows
);
1202 sipe_utils_slist_free_full(query_rows
, g_free
);
1205 sipe_backend_search_failed(sipe_public
,
1207 _("Invalid contact search query"));
1210 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1211 struct sipe_backend_buddy_info
*info
,
1213 const gchar
*server_alias
,
1216 sipe_backend_buddy bbuddy
;
1217 struct sipe_buddy
*sbuddy
;
1222 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1224 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1229 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1231 if (is_empty(server_alias
)) {
1232 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1235 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1237 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1241 value
= g_strdup(server_alias
);
1244 /* present alias if it differs from server alias */
1245 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1246 if (alias
&& !sipe_strequal(alias
, value
))
1248 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1250 SIPE_BUDDY_INFO_ALIAS
,
1256 if (is_empty(email
)) {
1257 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1259 SIPE_BUDDY_INFO_EMAIL
);
1261 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1263 SIPE_BUDDY_INFO_EMAIL
,
1269 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1271 SIPE_BUDDY_INFO_SITE
);
1273 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1275 SIPE_BUDDY_INFO_SITE
,
1280 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1281 if (sbuddy
&& sbuddy
->device_name
) {
1282 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1284 SIPE_BUDDY_INFO_DEVICE
,
1285 sbuddy
->device_name
);
1288 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1292 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1294 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1295 sipe_xml
*soap_body
,
1296 gpointer callback_data
)
1298 struct ms_dlx_data
*mdd
= callback_data
;
1299 struct sipe_backend_buddy_info
*info
= NULL
;
1300 gchar
*server_alias
= NULL
;
1301 gchar
*email
= NULL
;
1304 const sipe_xml
*node
;
1306 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1309 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1311 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1313 node
= sipe_xml_twin(node
)) {
1314 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1316 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1318 const sipe_xml
*values
= sipe_xml_child(node
,
1321 /* Single value entries */
1322 if (!is_empty(value
)) {
1324 if (sipe_strcase_equal(name
, "displayname")) {
1325 g_free(server_alias
);
1326 server_alias
= value
;
1328 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1330 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1332 } else if (sipe_strcase_equal(name
, "mail")) {
1336 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1338 SIPE_BUDDY_INFO_EMAIL
,
1340 } else if (sipe_strcase_equal(name
, "title")) {
1341 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1343 SIPE_BUDDY_INFO_JOB_TITLE
,
1345 } else if (sipe_strcase_equal(name
, "company")) {
1346 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1348 SIPE_BUDDY_INFO_COMPANY
,
1350 } else if (sipe_strcase_equal(name
, "country")) {
1351 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1353 SIPE_BUDDY_INFO_COUNTRY
,
1357 } else if (values
) {
1358 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1361 if (sipe_strcase_equal(name
, "telephonenumber")) {
1362 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1364 SIPE_BUDDY_INFO_WORK_PHONE
,
1376 /* this will show the minmum information */
1377 get_info_finalize(sipe_private
,
1384 g_free(server_alias
);
1388 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1390 struct transaction
*trans
)
1392 const gchar
*uri
= trans
->payload
->data
;
1393 struct sipe_backend_buddy_info
*info
= NULL
;
1394 gchar
*server_alias
= NULL
;
1395 gchar
*email
= NULL
;
1397 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1398 uri
, sipe_private
->username
);
1400 if (msg
->response
!= 200) {
1401 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1403 sipe_xml
*searchResults
;
1404 const sipe_xml
*mrow
;
1406 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1407 msg
->body
? msg
->body
: "");
1409 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1410 if (!searchResults
) {
1412 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1414 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1416 gchar
*phone_number
;
1418 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1420 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1421 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1422 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1425 * For 2007 system we will take this from ContactCard -
1426 * it has cleaner tel: URIs at least
1428 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1429 char *tel_uri
= sip_to_tel_uri(phone_number
);
1430 /* trims its parameters, so call first */
1431 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1432 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1433 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1434 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1437 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1441 if (!is_empty(server_alias
)) {
1442 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1444 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1447 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1448 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1450 SIPE_BUDDY_INFO_JOB_TITLE
,
1453 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1454 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1456 SIPE_BUDDY_INFO_OFFICE
,
1459 if (!is_empty(phone_number
)) {
1460 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1462 SIPE_BUDDY_INFO_WORK_PHONE
,
1465 g_free(phone_number
);
1466 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1467 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1469 SIPE_BUDDY_INFO_COMPANY
,
1472 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1473 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1475 SIPE_BUDDY_INFO_CITY
,
1478 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1479 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1481 SIPE_BUDDY_INFO_STATE
,
1484 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1485 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1487 SIPE_BUDDY_INFO_COUNTRY
,
1490 if (!is_empty(email
)) {
1491 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1493 SIPE_BUDDY_INFO_EMAIL
,
1497 sipe_xml_free(searchResults
);
1500 /* this will show the minmum information */
1501 get_info_finalize(sipe_private
,
1507 g_free(server_alias
);
1513 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1514 struct ms_dlx_data
*mdd
)
1516 /* error using [MS-DLX] server, retry using Active Directory */
1517 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, FALSE
);
1518 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1520 payload
->destroy
= g_free
;
1521 payload
->data
= mdd
->other
;
1524 sip_soap_directory_search(sipe_private
,
1527 process_get_info_response
,
1534 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1537 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1539 if (sipe_private
->dlx_uri
) {
1540 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1542 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1543 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(who
));
1545 mdd
->other
= g_strdup(who
);
1546 mdd
->max_returns
= 1;
1547 mdd
->callback
= get_info_ab_entry_response
;
1548 mdd
->failed_callback
= get_info_ab_entry_failed
;
1549 mdd
->session
= sipe_svc_session_start();
1551 ms_dlx_webticket_request(sipe_private
, mdd
);
1554 /* no [MS-DLX] server, use Active Directory search instead */
1555 gchar
*row
= g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW
,
1556 "msRTCSIP-PrimaryUserAddress",
1558 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1560 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1563 payload
->destroy
= g_free
;
1564 payload
->data
= g_strdup(who
);
1566 sip_soap_directory_search(sipe_private
,
1569 process_get_info_response
,
1575 static void photo_response_data_free(struct photo_response_data
*data
)
1578 g_free(data
->photo_hash
);
1579 if (data
->request
) {
1580 sipe_http_request_cancel(data
->request
);
1585 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1591 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1593 rdata
->request
= NULL
;
1595 if (status
== SIPE_HTTP_STATUS_OK
) {
1596 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1599 gsize photo_size
= atoi(len_str
);
1600 gpointer photo
= g_new(char, photo_size
);
1603 memcpy(photo
, body
, photo_size
);
1605 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1614 sipe_private
->buddies
->pending_photo_requests
=
1615 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, rdata
);
1617 photo_response_data_free(rdata
);
1620 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1622 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "saml:Assertion", TRUE
);
1623 gchar
*wsse_security_base64
;
1624 gchar
*x_ms_webticket_header
;
1630 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1632 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1633 wsse_security_base64
);
1636 g_free(wsse_security_base64
);
1638 return x_ms_webticket_header
;
1641 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
1643 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1644 sipe_xml
*soap_body
,
1645 gpointer callback_data
)
1647 struct ms_dlx_data
*mdd
= callback_data
;
1648 gchar
*photo_rel_path
= NULL
;
1649 gchar
*photo_hash
= NULL
;
1650 const gchar
*photo_hash_old
=
1651 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, mdd
->other
);
1654 const sipe_xml
*node
;
1656 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1659 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1661 node
= sipe_xml_twin(node
)) {
1662 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
1663 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
1665 if (!is_empty(value
)) {
1666 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
1667 g_free(photo_rel_path
);
1668 photo_rel_path
= value
;
1670 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
1682 if (sipe_private
->addressbook_uri
&& photo_rel_path
&&
1683 photo_hash
&& !sipe_strequal(photo_hash
, photo_hash_old
)) {
1684 gchar
*photo_url
= g_strdup_printf("%s/%s",
1685 sipe_private
->addressbook_uri
, photo_rel_path
);
1686 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
1688 struct photo_response_data
*data
= g_new(struct photo_response_data
, 1);
1689 data
->who
= g_strdup(mdd
->other
);
1690 data
->photo_hash
= photo_hash
;
1693 data
->request
= sipe_http_request_get(sipe_private
,
1695 x_ms_webticket_header
,
1696 process_buddy_photo_response
,
1699 if (data
->request
) {
1700 sipe_private
->buddies
->pending_photo_requests
=
1701 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1702 sipe_http_request_ready(data
->request
);
1704 photo_response_data_free(data
);
1707 g_free(x_ms_webticket_header
);
1711 g_free(photo_rel_path
);
1716 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
1717 struct ms_dlx_data
*mdd
)
1722 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
1725 if (sipe_backend_uses_photo()) {
1727 /* Lync 2013 or newer: use UCS */
1728 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
)) {
1730 sipe_ucs_get_photo(sipe_private
, uri
);
1732 /* Lync 2010: use [MS-DLX] */
1733 } else if (sipe_private
->dlx_uri
&&
1734 sipe_private
->addressbook_uri
) {
1735 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1737 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1738 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(uri
));
1740 mdd
->other
= g_strdup(uri
);
1741 mdd
->max_returns
= 1;
1742 mdd
->callback
= get_photo_ab_entry_response
;
1743 mdd
->failed_callback
= get_photo_ab_entry_failed
;
1744 mdd
->session
= sipe_svc_session_start();
1746 ms_dlx_webticket_request(sipe_private
, mdd
);
1751 static void buddy_refresh_photos_cb(gpointer uri
,
1752 SIPE_UNUSED_PARAMETER gpointer value
,
1753 gpointer sipe_private
)
1755 buddy_fetch_photo(sipe_private
, uri
);
1758 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
1760 g_hash_table_foreach(sipe_private
->buddies
->uri
,
1761 buddy_refresh_photos_cb
,
1765 /* Buddy menu callbacks*/
1767 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
1770 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1772 /* 2007+ conference */
1773 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1774 sipe_conf_add(sipe_private
, who
);
1776 /* 2005- multiparty chat */
1778 gchar
*self
= sip_uri_self(sipe_private
);
1779 struct sip_session
*session
;
1781 session
= sipe_session_add_chat(sipe_private
,
1785 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
1786 session
->chat_session
,
1787 session
->chat_session
->title
,
1791 sipe_im_invite(sipe_private
, session
, who
,
1792 NULL
, NULL
, NULL
, FALSE
);
1796 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
1799 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1802 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1804 SIPE_BUDDY_INFO_EMAIL
);
1807 gchar
*command_line
= g_strdup_printf(
1813 " mailto:%s", email
);
1816 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1818 g_spawn_command_line_async(command_line
, NULL
);
1819 g_free(command_line
);
1822 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1829 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
1830 struct sipe_backend_buddy_menu
*menu
,
1831 sipe_backend_buddy buddy
,
1832 sipe_buddy_info_fields id_phone
,
1833 sipe_buddy_info_fields id_display
,
1836 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
1840 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
1844 gchar
*label
= g_strdup_printf("%s %s",
1847 (tmp
= sip_tel_uri_denormalize(phone
)));
1848 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1851 SIPE_BUDDY_MENU_MAKE_CALL
,
1862 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
1863 const gchar
*buddy_name
,
1864 struct sipe_backend_buddy_menu
*menu
)
1866 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1867 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1870 gchar
*self
= sip_uri_self(sipe_private
);
1872 SIPE_SESSION_FOREACH
{
1873 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
1875 struct sipe_chat_session
*chat_session
= session
->chat_session
;
1876 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
1878 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
1880 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
1884 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
1885 /* We are a conf OP */
1887 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
1888 chat_session
->title
);
1889 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1892 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
1898 /* We are a conf OP */
1900 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
1901 chat_session
->title
);
1902 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1905 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
1913 (is_conf
&& !session
->locked
)) {
1914 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
1915 chat_session
->title
);
1916 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1919 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
1925 } SIPE_SESSION_FOREACH_END
;
1928 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1931 SIPE_BUDDY_MENU_NEW_CHAT
,
1934 /* add buddy's phone numbers if we have call control */
1935 if (sip_csta_is_idle(sipe_private
)) {
1938 menu
= buddy_menu_phone(sipe_public
,
1941 SIPE_BUDDY_INFO_WORK_PHONE
,
1942 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
1945 menu
= buddy_menu_phone(sipe_public
,
1948 SIPE_BUDDY_INFO_MOBILE_PHONE
,
1949 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
1953 menu
= buddy_menu_phone(sipe_public
,
1956 SIPE_BUDDY_INFO_HOME_PHONE
,
1957 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
1961 menu
= buddy_menu_phone(sipe_public
,
1964 SIPE_BUDDY_INFO_OTHER_PHONE
,
1965 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
1969 menu
= buddy_menu_phone(sipe_public
,
1972 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
1973 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
1978 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1980 SIPE_BUDDY_INFO_EMAIL
);
1982 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1985 SIPE_BUDDY_MENU_SEND_EMAIL
,
1991 /* access level control */
1992 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
1993 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
1996 sipe_ocs2007_access_control_menu(sipe_private
,
2002 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
2004 return(g_hash_table_size(sipe_private
->buddies
->uri
));
2007 static guint
sipe_ht_hash_nick(const char *nick
)
2009 char *lc
= g_utf8_strdown(nick
, -1);
2010 guint bucket
= g_str_hash(lc
);
2016 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
2018 char *nick1_norm
= NULL
;
2019 char *nick2_norm
= NULL
;
2022 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
2023 if (nick1
== NULL
|| nick2
== NULL
||
2024 !g_utf8_validate(nick1
, -1, NULL
) ||
2025 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
2027 nick1_norm
= g_utf8_casefold(nick1
, -1);
2028 nick2_norm
= g_utf8_casefold(nick2
, -1);
2029 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
2036 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
2038 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
2039 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
2040 (GEqualFunc
) sipe_ht_equals_nick
);
2041 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
2043 sipe_private
->buddies
= buddies
;