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 photo_response_data
{
72 struct sipe_http_request
*request
;
75 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
77 static void photo_response_data_free(struct photo_response_data
*data
);
79 struct sipe_buddy
*sipe_buddy_add(struct sipe_core_private
*sipe_private
,
81 const gchar
*exchange_key
,
82 const gchar
*change_key
)
84 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
85 gchar
*normalized_uri
= g_ascii_strdown(uri
, -1);
86 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
90 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
92 buddy
= g_new0(struct sipe_buddy
, 1);
93 buddy
->name
= normalized_uri
;
94 g_hash_table_insert(buddies
->uri
,
99 buddy
->exchange_key
= g_strdup(exchange_key
);
100 g_hash_table_insert(buddies
->exchange_key
,
105 buddy
->change_key
= g_strdup(change_key
);
108 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri
);
110 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES
)) {
111 buddy
->just_added
= TRUE
;
112 sipe_subscribe_presence_single_cb(sipe_private
,
116 buddy_fetch_photo(sipe_private
, normalized_uri
);
118 normalized_uri
= NULL
; /* buddy takes ownership */
120 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri
);
121 buddy
->is_obsolete
= FALSE
;
123 g_free(normalized_uri
);
128 static gboolean
is_buddy_in_group(struct sipe_buddy
*buddy
,
131 gboolean in_group
= FALSE
;
134 GSList
*entry2
= buddy
->groups
;
137 struct sipe_group
*group
= entry2
->data
;
138 if (sipe_strequal(group
->name
, name
)) {
142 entry2
= entry2
->next
;
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 sipe_group
*)a
)->id
-
195 ((const struct sipe_group
*)b
)->id
);
198 void sipe_buddy_insert_group(struct sipe_buddy
*buddy
,
199 struct sipe_group
*group
)
201 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
207 void sipe_buddy_cleanup_local_list(struct sipe_core_private
*sipe_private
)
209 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
212 GSList
*entry
= buddies
;
214 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
215 g_slist_length(buddies
));
216 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
217 sipe_buddy_count(sipe_private
));
219 sipe_backend_buddy bb
= entry
->data
;
220 gchar
*bname
= sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC
,
222 gchar
*gname
= sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC
,
224 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
227 if (!is_buddy_in_group(buddy
, gname
)) {
228 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",
230 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, bb
);
239 g_slist_free(buddies
);
242 struct sipe_buddy
*sipe_buddy_find_by_uri(struct sipe_core_private
*sipe_private
,
245 return(g_hash_table_lookup(sipe_private
->buddies
->uri
, uri
));
248 struct sipe_buddy
*sipe_buddy_find_by_exchange_key(struct sipe_core_private
*sipe_private
,
249 const gchar
*exchange_key
)
251 return(g_hash_table_lookup(sipe_private
->buddies
->exchange_key
,
255 void sipe_buddy_foreach(struct sipe_core_private
*sipe_private
,
257 gpointer callback_data
)
259 g_hash_table_foreach(sipe_private
->buddies
->uri
,
264 static void buddy_free(struct sipe_buddy
*buddy
)
268 * We are calling g_hash_table_foreach_steal(). That means that no
269 * key/value deallocation functions are called. Therefore the glib
270 * hash code does not touch the key (buddy->name) or value (buddy)
271 * of the to-be-deleted hash node at all. It follows that we
273 * - MUST free the memory for the key ourselves and
274 * - ARE allowed to do it in this function
276 * Conclusion: glib must be broken on the Windows platform if sipe
277 * crashes with SIGTRAP when closing. You'll have to live
278 * with the memory leak until this is fixed.
282 g_free(buddy
->exchange_key
);
283 g_free(buddy
->change_key
);
284 g_free(buddy
->activity
);
285 g_free(buddy
->meeting_subject
);
286 g_free(buddy
->meeting_location
);
289 g_free(buddy
->cal_start_time
);
290 g_free(buddy
->cal_free_busy_base64
);
291 g_free(buddy
->cal_free_busy
);
292 g_free(buddy
->last_non_cal_activity
);
294 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
296 g_free(buddy
->device_name
);
297 g_slist_free(buddy
->groups
);
301 static gboolean
buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key
,
303 SIPE_UNUSED_PARAMETER gpointer user_data
)
306 /* We must return TRUE as the key/value have already been deleted */
310 void sipe_buddy_free(struct sipe_core_private
*sipe_private
)
312 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
314 g_hash_table_foreach_steal(buddies
->uri
,
318 /* core is being deallocated, remove all its pending photo requests */
319 while (buddies
->pending_photo_requests
) {
320 struct photo_response_data
*data
=
321 buddies
->pending_photo_requests
->data
;
322 buddies
->pending_photo_requests
=
323 g_slist_remove(buddies
->pending_photo_requests
, data
);
324 photo_response_data_free(data
);
327 g_hash_table_destroy(buddies
->uri
);
328 g_hash_table_destroy(buddies
->exchange_key
);
330 sipe_private
->buddies
= NULL
;
333 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
335 SIPE_UNUSED_PARAMETER gpointer user_data
)
337 ((struct sipe_buddy
*) value
)->is_obsolete
= TRUE
;
340 void sipe_buddy_update_start(struct sipe_core_private
*sipe_private
)
342 g_hash_table_foreach(sipe_private
->buddies
->uri
,
343 buddy_set_obsolete_flag
,
347 static gboolean
buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key
,
351 struct sipe_core_private
*sipe_private
= user_data
;
352 struct sipe_buddy
*buddy
= value
;
354 if (buddy
->is_obsolete
) {
355 /* all backend buddies in different groups */
356 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
359 GSList
*entry
= buddies
;
361 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
362 g_slist_length(buddies
),
366 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
,
370 g_slist_free(buddies
);
373 /* return TRUE as the key/value have already been deleted */
379 void sipe_buddy_update_finish(struct sipe_core_private
*sipe_private
)
381 g_hash_table_foreach_remove(sipe_private
->buddies
->uri
,
382 buddy_check_obsolete_flag
,
386 gchar
*sipe_core_buddy_status(struct sipe_core_public
*sipe_public
,
389 const gchar
*status_text
)
391 struct sipe_buddy
*sbuddy
;
392 const char *activity_str
;
394 if (!sipe_public
) return NULL
; /* happens on pidgin exit */
396 sbuddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
, uri
);
397 if (!sbuddy
) return NULL
;
399 activity_str
= sbuddy
->activity
? sbuddy
->activity
:
400 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
403 if (activity_str
&& sbuddy
->note
) {
404 return g_strdup_printf("%s - <i>%s</i>", activity_str
, sbuddy
->note
);
405 } else if (activity_str
) {
406 return g_strdup(activity_str
);
407 } else if (sbuddy
->note
) {
408 return g_strdup_printf("<i>%s</i>", sbuddy
->note
);
414 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
417 sipe_backend_buddy pbuddy
;
419 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
420 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
425 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
427 const gchar
*old_group_name
,
428 const gchar
*new_group_name
)
430 struct sipe_buddy
* buddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
,
432 struct sipe_group
* old_group
= NULL
;
433 struct sipe_group
* new_group
;
435 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
436 who
? who
: "", old_group_name
? old_group_name
: "", new_group_name
? new_group_name
: "");
438 if(!buddy
) { // buddy not in roaming list
442 if (old_group_name
) {
443 old_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, old_group_name
);
445 new_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, new_group_name
);
448 buddy
->groups
= g_slist_remove(buddy
->groups
, old_group
);
449 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who
, old_group_name
);
453 sipe_group_create(SIPE_CORE_PRIVATE
, new_group_name
, who
);
455 sipe_buddy_insert_group(buddy
, new_group
);
456 sipe_group_update_buddy(SIPE_CORE_PRIVATE
, buddy
);
460 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
462 const gchar
*group_name
)
464 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
466 if (!sipe_buddy_find_by_uri(sipe_private
, uri
))
467 sipe_buddy_add(sipe_private
,
472 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
475 sipe_core_buddy_group(sipe_public
,
481 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
482 struct sipe_buddy
*buddy
)
484 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
485 gchar
*action_name
= sipe_utils_presence_key(buddy
->name
);
486 sipe_schedule_cancel(sipe_private
, action_name
);
489 g_hash_table_remove(buddies
->uri
, buddy
->name
);
490 if (buddy
->exchange_key
)
491 g_hash_table_remove(buddies
->exchange_key
,
492 buddy
->exchange_key
);
498 * Unassociates buddy from group first.
499 * Then see if no groups left, removes buddy completely.
500 * Otherwise updates buddy groups on server.
502 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
504 const gchar
*group_name
)
506 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
507 struct sipe_buddy
*b
= sipe_buddy_find_by_uri(sipe_private
,
509 struct sipe_group
*g
= NULL
;
514 g
= sipe_group_find_by_name(sipe_private
, group_name
);
516 b
->groups
= g_slist_remove(b
->groups
, g
);
517 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
522 if (g_slist_length(b
->groups
) < 1) {
524 if (sipe_ucs_is_migrated(sipe_private
)) {
525 sipe_ucs_group_remove_buddy(sipe_private
,
529 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
531 sip_soap_request(sipe_private
,
537 sipe_buddy_remove(sipe_private
, b
);
539 if (sipe_ucs_is_migrated(sipe_private
)) {
540 sipe_ucs_group_remove_buddy(sipe_private
,
544 /* updates groups on server */
545 sipe_group_update_buddy(sipe_private
, b
);
549 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
553 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
554 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
559 /* Check if on 2005 system contact's calendar,
560 * then set/preserve it.
562 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
563 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
565 sipe_ocs2005_apply_calendar_status(sipe_private
,
567 sipe_status_activity_to_token(activity
));
571 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
573 const gchar
*status_name
,
575 struct sipe_backend_buddy_tooltip
*tooltip
)
577 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
579 gboolean is_oof_note
= FALSE
;
580 const gchar
*activity
= NULL
;
581 gchar
*calendar
= NULL
;
582 const gchar
*meeting_subject
= NULL
;
583 const gchar
*meeting_location
= NULL
;
584 gchar
*access_text
= NULL
;
586 #define SIPE_ADD_BUDDY_INFO(l, t) \
588 gchar *tmp = g_markup_escape_text((t), -1); \
589 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
592 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
593 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
595 if (sipe_public
) { /* happens on pidgin exit */
596 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
600 is_oof_note
= sbuddy
->is_oof_note
;
601 activity
= sbuddy
->activity
;
602 calendar
= sipe_cal_get_description(sbuddy
);
603 meeting_subject
= sbuddy
->meeting_subject
;
604 meeting_location
= sbuddy
->meeting_location
;
606 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
607 gboolean is_group_access
= FALSE
;
608 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
610 sipe_get_no_sip_uri(uri
),
612 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
613 access_text
= is_group_access
?
614 g_strdup(access_level
) :
615 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
621 const gchar
*status_str
= activity
? activity
: status_name
;
623 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
625 if (is_online
&& !is_empty(calendar
)) {
626 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
629 if (!is_empty(meeting_location
)) {
630 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
631 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
633 if (!is_empty(meeting_subject
)) {
634 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
635 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
638 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
639 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
640 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
642 g_free(note_italics
);
645 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
650 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
652 sipe_buddy_info_fields propkey
,
653 char *property_value
)
655 GSList
*buddies
, *entry
;
658 property_value
= g_strstrip(property_value
);
660 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
663 sipe_backend_buddy p_buddy
= entry
->data
;
665 /* for Display Name */
666 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
668 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
669 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
670 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
671 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
675 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
676 if (!is_empty(property_value
) &&
677 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
679 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
680 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
684 /* for other properties */
686 if (!is_empty(property_value
)) {
687 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
688 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
689 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
697 g_slist_free(buddies
);
706 sipe_svc_callback
*callback
;
707 struct sipe_svc_session
*session
;
708 gchar
*wsse_security
;
709 struct sipe_backend_search_token
*token
;
710 /* must call ms_dlx_free() */
711 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
712 struct ms_dlx_data
*mdd
);
715 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
717 sipe_utils_slist_free_full(mdd
->search_rows
, g_free
);
718 sipe_svc_session_close(mdd
->session
);
720 g_free(mdd
->wsse_security
);
724 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
725 #define DLX_SEARCH_ITEM \
726 "<AbEntryRequest.ChangeSearchQuery>" \
727 " <SearchOn>%s</SearchOn>" \
728 " <Value>%s</Value>" \
729 "</AbEntryRequest.ChangeSearchQuery>"
731 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
732 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
740 attr
= query_rows
->data
;
741 query_rows
= g_slist_next(query_rows
);
742 value
= query_rows
->data
;
743 query_rows
= g_slist_next(query_rows
);
748 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
754 query
= g_strjoinv(NULL
, attrs
);
755 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
764 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
765 const gchar
*base_uri
,
766 const gchar
*auth_uri
,
767 const gchar
*wsse_security
,
768 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
769 gpointer callback_data
)
771 struct ms_dlx_data
*mdd
= callback_data
;
774 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
776 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
779 if (sipe_svc_ab_entry_request(sipe_private
,
784 g_slist_length(mdd
->search_rows
) / 2,
789 /* keep webticket security token for potential further use */
790 mdd
->wsse_security
= g_strdup(wsse_security
);
792 /* callback data passed down the line */
798 /* no ticket: this will show the minmum information */
799 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
804 mdd
->failed_callback(sipe_private
, mdd
);
807 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
808 struct ms_dlx_data
*mdd
)
810 if (!sipe_webticket_request(sipe_private
,
812 sipe_private
->dlx_uri
,
813 "AddressBookWebTicketBearer",
816 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
817 sipe_private
->dlx_uri
);
818 mdd
->failed_callback(sipe_private
, mdd
);
822 static void search_contacts_finalize(struct sipe_core_private
*sipe_private
,
823 struct sipe_backend_search_results
*results
,
827 gchar
*secondary
= g_strdup_printf(
828 dngettext(PACKAGE_NAME
,
829 "Found %d contact%s:",
830 "Found %d contacts%s:", match_count
),
831 match_count
, more
? _(" (more matched your query)") : "");
833 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
840 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
842 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
844 gpointer callback_data
)
846 struct ms_dlx_data
*mdd
= callback_data
;
849 const sipe_xml
*node
;
850 struct sipe_backend_search_results
*results
;
853 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
857 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
859 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
860 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
862 _("No contacts found"));
867 /* OK, we found something - show the results to the user */
868 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
871 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
872 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
874 _("Unable to display the search results"));
879 /* SearchAbEntryResult can contain duplicates */
880 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
883 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
884 const sipe_xml
*attrs
;
885 gchar
*sip_uri
= NULL
;
886 gchar
*displayname
= NULL
;
887 gchar
*company
= NULL
;
888 gchar
*country
= NULL
;
891 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
893 attrs
= sipe_xml_twin(attrs
)) {
894 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
896 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
899 if (!is_empty(value
)) {
900 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
904 } else if (sipe_strcase_equal(name
, "displayname")) {
908 } else if (sipe_strcase_equal(name
, "mail")) {
912 } else if (sipe_strcase_equal(name
, "company")) {
916 } else if (sipe_strcase_equal(name
, "country")) {
927 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
928 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
929 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
936 g_strfreev(uri_parts
);
938 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
949 search_contacts_finalize(sipe_private
, results
,
950 g_hash_table_size(found
),
952 g_hash_table_destroy(found
);
956 mdd
->failed_callback(sipe_private
, mdd
);
960 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
962 struct transaction
*trans
)
964 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
965 struct sipe_backend_search_results
*results
;
966 sipe_xml
*searchResults
;
967 const sipe_xml
*mrow
;
968 guint match_count
= 0;
969 gboolean more
= FALSE
;
971 /* valid response? */
972 if (msg
->response
!= 200) {
973 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
975 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
977 _("Contact search failed"));
981 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
984 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
985 if (!searchResults
) {
986 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
987 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
989 _("Contact search failed"));
994 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
996 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
997 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
999 _("No contacts found"));
1001 sipe_xml_free(searchResults
);
1005 /* OK, we found something - show the results to the user */
1006 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
1007 trans
->payload
->data
);
1009 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1010 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
1012 _("Unable to display the search results"));
1014 sipe_xml_free(searchResults
);
1018 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
1019 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
1020 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
1023 sipe_xml_attribute(mrow
, "displayName"),
1024 sipe_xml_attribute(mrow
, "company"),
1025 sipe_xml_attribute(mrow
, "country"),
1026 sipe_xml_attribute(mrow
, "email"));
1027 g_strfreev(uri_parts
);
1031 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
1032 char *data
= sipe_xml_data(mrow
);
1033 more
= (g_ascii_strcasecmp(data
, "true") == 0);
1037 search_contacts_finalize(sipe_private
, results
, match_count
, more
);
1038 sipe_xml_free(searchResults
);
1043 static void search_soap_request(struct sipe_core_private
*sipe_private
,
1044 struct sipe_backend_search_token
*token
,
1045 GSList
*search_rows
)
1047 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
1048 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1050 payload
->data
= token
;
1052 sip_soap_directory_search(sipe_private
,
1055 process_search_contact_response
,
1060 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1061 struct ms_dlx_data
*mdd
)
1063 /* error using [MS-DLX] server, retry using Active Directory */
1064 search_soap_request(sipe_private
, mdd
->token
, mdd
->search_rows
);
1068 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
1069 struct sipe_backend_search_token
*token
,
1070 const gchar
*given_name
,
1071 const gchar
*surname
,
1073 const gchar
*company
,
1074 const gchar
*country
)
1076 GSList
*query_rows
= NULL
;
1078 #define ADD_QUERY_ROW(attr, val) \
1080 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1081 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1084 ADD_QUERY_ROW("givenName", given_name
);
1085 ADD_QUERY_ROW("sn", surname
);
1086 ADD_QUERY_ROW("mail", email
);
1087 ADD_QUERY_ROW("company", company
);
1088 ADD_QUERY_ROW("c", country
);
1091 if (SIPE_CORE_PRIVATE
->dlx_uri
!= NULL
) {
1092 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1094 mdd
->search_rows
= query_rows
;
1095 mdd
->max_returns
= 100;
1096 mdd
->callback
= search_ab_entry_response
;
1097 mdd
->failed_callback
= search_ab_entry_failed
;
1098 mdd
->session
= sipe_svc_session_start();
1101 ms_dlx_webticket_request(SIPE_CORE_PRIVATE
, mdd
);
1104 /* no [MS-DLX] server, use Active Directory search instead */
1105 search_soap_request(SIPE_CORE_PRIVATE
, token
, query_rows
);
1106 sipe_utils_slist_free_full(query_rows
, g_free
);
1109 sipe_backend_search_failed(sipe_public
,
1111 _("Invalid contact search query"));
1114 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1115 struct sipe_backend_buddy_info
*info
,
1117 const gchar
*server_alias
,
1120 sipe_backend_buddy bbuddy
;
1121 struct sipe_buddy
*sbuddy
;
1126 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1128 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1133 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1135 if (is_empty(server_alias
)) {
1136 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1139 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1141 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1145 value
= g_strdup(server_alias
);
1148 /* present alias if it differs from server alias */
1149 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1150 if (alias
&& !sipe_strequal(alias
, value
))
1152 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1154 SIPE_BUDDY_INFO_ALIAS
,
1160 if (is_empty(email
)) {
1161 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1163 SIPE_BUDDY_INFO_EMAIL
);
1165 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1167 SIPE_BUDDY_INFO_EMAIL
,
1173 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1175 SIPE_BUDDY_INFO_SITE
);
1177 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1179 SIPE_BUDDY_INFO_SITE
,
1184 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1185 if (sbuddy
&& sbuddy
->device_name
) {
1186 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1188 SIPE_BUDDY_INFO_DEVICE
,
1189 sbuddy
->device_name
);
1192 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1196 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1198 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1199 sipe_xml
*soap_body
,
1200 gpointer callback_data
)
1202 struct ms_dlx_data
*mdd
= callback_data
;
1203 struct sipe_backend_buddy_info
*info
= NULL
;
1204 gchar
*server_alias
= NULL
;
1205 gchar
*email
= NULL
;
1208 const sipe_xml
*node
;
1210 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1213 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1215 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1217 node
= sipe_xml_twin(node
)) {
1218 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1220 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1222 const sipe_xml
*values
= sipe_xml_child(node
,
1225 /* Single value entries */
1226 if (!is_empty(value
)) {
1228 if (sipe_strcase_equal(name
, "displayname")) {
1229 g_free(server_alias
);
1230 server_alias
= value
;
1232 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1234 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1236 } else if (sipe_strcase_equal(name
, "mail")) {
1240 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1242 SIPE_BUDDY_INFO_EMAIL
,
1244 } else if (sipe_strcase_equal(name
, "title")) {
1245 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1247 SIPE_BUDDY_INFO_JOB_TITLE
,
1249 } else if (sipe_strcase_equal(name
, "company")) {
1250 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1252 SIPE_BUDDY_INFO_COMPANY
,
1254 } else if (sipe_strcase_equal(name
, "country")) {
1255 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1257 SIPE_BUDDY_INFO_COUNTRY
,
1261 } else if (values
) {
1262 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1265 if (sipe_strcase_equal(name
, "telephonenumber")) {
1266 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1268 SIPE_BUDDY_INFO_WORK_PHONE
,
1280 /* this will show the minmum information */
1281 get_info_finalize(sipe_private
,
1288 g_free(server_alias
);
1292 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1294 struct transaction
*trans
)
1296 const gchar
*uri
= trans
->payload
->data
;
1297 struct sipe_backend_buddy_info
*info
= NULL
;
1298 gchar
*server_alias
= NULL
;
1299 gchar
*email
= NULL
;
1301 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1302 uri
, sipe_private
->username
);
1304 if (msg
->response
!= 200) {
1305 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1307 sipe_xml
*searchResults
;
1308 const sipe_xml
*mrow
;
1310 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1311 msg
->body
? msg
->body
: "");
1313 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1314 if (!searchResults
) {
1316 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1318 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1320 gchar
*phone_number
;
1322 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1324 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1325 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1326 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1329 * For 2007 system we will take this from ContactCard -
1330 * it has cleaner tel: URIs at least
1332 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1333 char *tel_uri
= sip_to_tel_uri(phone_number
);
1334 /* trims its parameters, so call first */
1335 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1336 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1337 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1338 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1341 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1345 if (!is_empty(server_alias
)) {
1346 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1348 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1351 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1352 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1354 SIPE_BUDDY_INFO_JOB_TITLE
,
1357 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1358 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1360 SIPE_BUDDY_INFO_OFFICE
,
1363 if (!is_empty(phone_number
)) {
1364 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1366 SIPE_BUDDY_INFO_WORK_PHONE
,
1369 g_free(phone_number
);
1370 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1371 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1373 SIPE_BUDDY_INFO_COMPANY
,
1376 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1377 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1379 SIPE_BUDDY_INFO_CITY
,
1382 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1383 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1385 SIPE_BUDDY_INFO_STATE
,
1388 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1389 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1391 SIPE_BUDDY_INFO_COUNTRY
,
1394 if (!is_empty(email
)) {
1395 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1397 SIPE_BUDDY_INFO_EMAIL
,
1401 sipe_xml_free(searchResults
);
1404 /* this will show the minmum information */
1405 get_info_finalize(sipe_private
,
1411 g_free(server_alias
);
1417 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1418 struct ms_dlx_data
*mdd
)
1420 /* error using [MS-DLX] server, retry using Active Directory */
1421 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, FALSE
);
1422 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1424 payload
->destroy
= g_free
;
1425 payload
->data
= mdd
->other
;
1428 sip_soap_directory_search(sipe_private
,
1431 process_get_info_response
,
1438 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1441 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1443 if (sipe_private
->dlx_uri
) {
1444 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1446 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1447 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(who
));
1449 mdd
->other
= g_strdup(who
);
1450 mdd
->max_returns
= 1;
1451 mdd
->callback
= get_info_ab_entry_response
;
1452 mdd
->failed_callback
= get_info_ab_entry_failed
;
1453 mdd
->session
= sipe_svc_session_start();
1455 ms_dlx_webticket_request(sipe_private
, mdd
);
1458 /* no [MS-DLX] server, use Active Directory search instead */
1459 gchar
*row
= g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW
,
1460 "msRTCSIP-PrimaryUserAddress",
1462 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1464 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1467 payload
->destroy
= g_free
;
1468 payload
->data
= g_strdup(who
);
1470 sip_soap_directory_search(sipe_private
,
1473 process_get_info_response
,
1479 static void photo_response_data_free(struct photo_response_data
*data
)
1482 g_free(data
->photo_hash
);
1483 if (data
->request
) {
1484 sipe_http_request_cancel(data
->request
);
1489 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1495 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1497 rdata
->request
= NULL
;
1499 if (status
== SIPE_HTTP_STATUS_OK
) {
1500 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1503 gsize photo_size
= atoi(len_str
);
1504 gpointer photo
= g_new(char, photo_size
);
1507 memcpy(photo
, body
, photo_size
);
1509 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1518 sipe_private
->buddies
->pending_photo_requests
=
1519 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, rdata
);
1521 photo_response_data_free(rdata
);
1524 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1526 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "saml:Assertion", TRUE
);
1527 gchar
*wsse_security_base64
;
1528 gchar
*x_ms_webticket_header
;
1534 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1536 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1537 wsse_security_base64
);
1540 g_free(wsse_security_base64
);
1542 return x_ms_webticket_header
;
1545 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
1547 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1548 sipe_xml
*soap_body
,
1549 gpointer callback_data
)
1551 struct ms_dlx_data
*mdd
= callback_data
;
1552 gchar
*photo_rel_path
= NULL
;
1553 gchar
*photo_hash
= NULL
;
1554 const gchar
*photo_hash_old
=
1555 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, mdd
->other
);
1558 const sipe_xml
*node
;
1560 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1563 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1565 node
= sipe_xml_twin(node
)) {
1566 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
1567 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
1569 if (!is_empty(value
)) {
1570 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
1571 g_free(photo_rel_path
);
1572 photo_rel_path
= value
;
1574 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
1586 if (sipe_private
->addressbook_uri
&& photo_rel_path
&&
1587 photo_hash
&& !sipe_strequal(photo_hash
, photo_hash_old
)) {
1588 gchar
*photo_url
= g_strdup_printf("%s/%s",
1589 sipe_private
->addressbook_uri
, photo_rel_path
);
1590 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
1592 struct photo_response_data
*data
= g_new(struct photo_response_data
, 1);
1593 data
->who
= g_strdup(mdd
->other
);
1594 data
->photo_hash
= photo_hash
;
1597 data
->request
= sipe_http_request_get(sipe_private
,
1599 x_ms_webticket_header
,
1600 process_buddy_photo_response
,
1603 if (data
->request
) {
1604 sipe_private
->buddies
->pending_photo_requests
=
1605 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1606 sipe_http_request_ready(data
->request
);
1608 photo_response_data_free(data
);
1611 g_free(x_ms_webticket_header
);
1615 g_free(photo_rel_path
);
1620 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
1621 struct ms_dlx_data
*mdd
)
1626 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
1629 if (sipe_backend_uses_photo()) {
1631 /* Lync 2013 or newer: use UCS */
1632 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
)) {
1634 sipe_ucs_get_photo(sipe_private
, uri
);
1636 /* Lync 2010: use [MS-DLX] */
1637 } else if (sipe_private
->dlx_uri
&&
1638 sipe_private
->addressbook_uri
) {
1639 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1641 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1642 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(uri
));
1644 mdd
->other
= g_strdup(uri
);
1645 mdd
->max_returns
= 1;
1646 mdd
->callback
= get_photo_ab_entry_response
;
1647 mdd
->failed_callback
= get_photo_ab_entry_failed
;
1648 mdd
->session
= sipe_svc_session_start();
1650 ms_dlx_webticket_request(sipe_private
, mdd
);
1655 static void buddy_refresh_photos_cb(gpointer uri
,
1656 SIPE_UNUSED_PARAMETER gpointer value
,
1657 gpointer sipe_private
)
1659 buddy_fetch_photo(sipe_private
, uri
);
1662 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
1664 g_hash_table_foreach(sipe_private
->buddies
->uri
,
1665 buddy_refresh_photos_cb
,
1669 /* Buddy menu callbacks*/
1671 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
1674 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1676 /* 2007+ conference */
1677 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1678 sipe_conf_add(sipe_private
, who
);
1680 /* 2005- multiparty chat */
1682 gchar
*self
= sip_uri_self(sipe_private
);
1683 struct sip_session
*session
;
1685 session
= sipe_session_add_chat(sipe_private
,
1689 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
1690 session
->chat_session
,
1691 session
->chat_session
->title
,
1695 sipe_im_invite(sipe_private
, session
, who
,
1696 NULL
, NULL
, NULL
, FALSE
);
1700 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
1703 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1706 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1708 SIPE_BUDDY_INFO_EMAIL
);
1711 gchar
*command_line
= g_strdup_printf(
1717 " mailto:%s", email
);
1720 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1722 g_spawn_command_line_async(command_line
, NULL
);
1723 g_free(command_line
);
1726 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1733 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
1734 struct sipe_backend_buddy_menu
*menu
,
1735 sipe_backend_buddy buddy
,
1736 sipe_buddy_info_fields id_phone
,
1737 sipe_buddy_info_fields id_display
,
1740 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
1744 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
1748 gchar
*label
= g_strdup_printf("%s %s",
1751 (tmp
= sip_tel_uri_denormalize(phone
)));
1752 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1755 SIPE_BUDDY_MENU_MAKE_CALL
,
1766 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
1767 const gchar
*buddy_name
,
1768 struct sipe_backend_buddy_menu
*menu
)
1770 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1771 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1774 gchar
*self
= sip_uri_self(sipe_private
);
1776 SIPE_SESSION_FOREACH
{
1777 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
1779 struct sipe_chat_session
*chat_session
= session
->chat_session
;
1780 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
1782 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
1784 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
1788 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
1789 /* We are a conf OP */
1791 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
1792 chat_session
->title
);
1793 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1796 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
1802 /* We are a conf OP */
1804 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
1805 chat_session
->title
);
1806 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1809 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
1817 (is_conf
&& !session
->locked
)) {
1818 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
1819 chat_session
->title
);
1820 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1823 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
1829 } SIPE_SESSION_FOREACH_END
;
1832 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1835 SIPE_BUDDY_MENU_NEW_CHAT
,
1838 /* add buddy's phone numbers if we have call control */
1839 if (sip_csta_is_idle(sipe_private
)) {
1842 menu
= buddy_menu_phone(sipe_public
,
1845 SIPE_BUDDY_INFO_WORK_PHONE
,
1846 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
1849 menu
= buddy_menu_phone(sipe_public
,
1852 SIPE_BUDDY_INFO_MOBILE_PHONE
,
1853 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
1857 menu
= buddy_menu_phone(sipe_public
,
1860 SIPE_BUDDY_INFO_HOME_PHONE
,
1861 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
1865 menu
= buddy_menu_phone(sipe_public
,
1868 SIPE_BUDDY_INFO_OTHER_PHONE
,
1869 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
1873 menu
= buddy_menu_phone(sipe_public
,
1876 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
1877 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
1882 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1884 SIPE_BUDDY_INFO_EMAIL
);
1886 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1889 SIPE_BUDDY_MENU_SEND_EMAIL
,
1895 /* access level control */
1896 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
1897 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
1900 sipe_ocs2007_access_control_menu(sipe_private
,
1906 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
1908 return(g_hash_table_size(sipe_private
->buddies
->uri
));
1911 static guint
sipe_ht_hash_nick(const char *nick
)
1913 char *lc
= g_utf8_strdown(nick
, -1);
1914 guint bucket
= g_str_hash(lc
);
1920 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
1922 char *nick1_norm
= NULL
;
1923 char *nick2_norm
= NULL
;
1926 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
1927 if (nick1
== NULL
|| nick2
== NULL
||
1928 !g_utf8_validate(nick1
, -1, NULL
) ||
1929 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
1931 nick1_norm
= g_utf8_casefold(nick1
, -1);
1932 nick2_norm
= g_utf8_casefold(nick2
, -1);
1933 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
1940 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
1942 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
1943 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
1944 (GEqualFunc
) sipe_ht_equals_nick
);
1945 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
1947 sipe_private
->buddies
= buddies
;