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
);
122 g_free(normalized_uri
);
127 static gboolean
is_buddy_in_group(struct sipe_buddy
*buddy
,
130 gboolean in_group
= FALSE
;
133 GSList
*entry2
= buddy
->groups
;
136 struct sipe_group
*group
= entry2
->data
;
137 if (sipe_strequal(group
->name
, name
)) {
141 entry2
= entry2
->next
;
148 void sipe_buddy_add_to_group(struct sipe_core_private
*sipe_private
,
149 struct sipe_buddy
*buddy
,
150 struct sipe_group
*group
,
153 const gchar
*uri
= buddy
->name
;
154 const gchar
*group_name
= group
->name
;
155 sipe_backend_buddy bb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
160 bb
= sipe_backend_buddy_add(SIPE_CORE_PUBLIC
,
164 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
165 uri
, alias
? alias
: "<NONE>");
169 if (!is_empty(alias
)) {
170 gchar
*old_alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
173 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri
),
175 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
,
178 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
179 uri
, old_alias
, alias
);
184 if (!is_buddy_in_group(buddy
, group_name
)) {
185 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
187 (GCompareFunc
) sipe_group_compare
,
189 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
194 void sipe_buddy_cleanup_local_list(struct sipe_core_private
*sipe_private
)
196 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
199 GSList
*entry
= buddies
;
201 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
202 g_slist_length(buddies
));
203 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
204 sipe_buddy_count(sipe_private
));
206 sipe_backend_buddy bb
= entry
->data
;
207 gchar
*bname
= sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC
,
209 gchar
*gname
= sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC
,
211 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
214 if (!is_buddy_in_group(buddy
, gname
)) {
215 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",
217 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, bb
);
226 g_slist_free(buddies
);
229 struct sipe_buddy
*sipe_buddy_find_by_uri(struct sipe_core_private
*sipe_private
,
232 return(g_hash_table_lookup(sipe_private
->buddies
->uri
, uri
));
235 struct sipe_buddy
*sipe_buddy_find_by_exchange_key(struct sipe_core_private
*sipe_private
,
236 const gchar
*exchange_key
)
238 return(g_hash_table_lookup(sipe_private
->buddies
->exchange_key
,
242 void sipe_buddy_foreach(struct sipe_core_private
*sipe_private
,
244 gpointer callback_data
)
246 g_hash_table_foreach(sipe_private
->buddies
->uri
,
251 static void buddy_free(struct sipe_buddy
*buddy
)
255 * We are calling g_hash_table_foreach_steal(). That means that no
256 * key/value deallocation functions are called. Therefore the glib
257 * hash code does not touch the key (buddy->name) or value (buddy)
258 * of the to-be-deleted hash node at all. It follows that we
260 * - MUST free the memory for the key ourselves and
261 * - ARE allowed to do it in this function
263 * Conclusion: glib must be broken on the Windows platform if sipe
264 * crashes with SIGTRAP when closing. You'll have to live
265 * with the memory leak until this is fixed.
269 g_free(buddy
->exchange_key
);
270 g_free(buddy
->change_key
);
271 g_free(buddy
->activity
);
272 g_free(buddy
->meeting_subject
);
273 g_free(buddy
->meeting_location
);
276 g_free(buddy
->cal_start_time
);
277 g_free(buddy
->cal_free_busy_base64
);
278 g_free(buddy
->cal_free_busy
);
279 g_free(buddy
->last_non_cal_activity
);
281 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
283 g_free(buddy
->device_name
);
284 g_slist_free(buddy
->groups
);
288 static gboolean
buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key
,
290 SIPE_UNUSED_PARAMETER gpointer user_data
)
293 /* We must return TRUE as the key/value have already been deleted */
297 void sipe_buddy_free(struct sipe_core_private
*sipe_private
)
299 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
301 g_hash_table_foreach_steal(buddies
->uri
,
305 /* core is being deallocated, remove all its pending photo requests */
306 while (buddies
->pending_photo_requests
) {
307 struct photo_response_data
*data
=
308 buddies
->pending_photo_requests
->data
;
309 buddies
->pending_photo_requests
=
310 g_slist_remove(buddies
->pending_photo_requests
, data
);
311 photo_response_data_free(data
);
314 g_hash_table_destroy(buddies
->uri
);
315 g_hash_table_destroy(buddies
->exchange_key
);
317 sipe_private
->buddies
= NULL
;
320 gchar
*sipe_core_buddy_status(struct sipe_core_public
*sipe_public
,
323 const gchar
*status_text
)
325 struct sipe_buddy
*sbuddy
;
326 const char *activity_str
;
328 if (!sipe_public
) return NULL
; /* happens on pidgin exit */
330 sbuddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
, uri
);
331 if (!sbuddy
) return NULL
;
333 activity_str
= sbuddy
->activity
? sbuddy
->activity
:
334 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
337 if (activity_str
&& sbuddy
->note
) {
338 return g_strdup_printf("%s - <i>%s</i>", activity_str
, sbuddy
->note
);
339 } else if (activity_str
) {
340 return g_strdup(activity_str
);
341 } else if (sbuddy
->note
) {
342 return g_strdup_printf("<i>%s</i>", sbuddy
->note
);
348 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
351 sipe_backend_buddy pbuddy
;
353 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
354 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
359 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
361 const gchar
*old_group_name
,
362 const gchar
*new_group_name
)
364 struct sipe_buddy
* buddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
,
366 struct sipe_group
* old_group
= NULL
;
367 struct sipe_group
* new_group
;
369 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
370 who
? who
: "", old_group_name
? old_group_name
: "", new_group_name
? new_group_name
: "");
372 if(!buddy
) { // buddy not in roaming list
376 if (old_group_name
) {
377 old_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, old_group_name
);
379 new_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, new_group_name
);
382 buddy
->groups
= g_slist_remove(buddy
->groups
, old_group
);
383 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who
, old_group_name
);
387 sipe_group_create(SIPE_CORE_PRIVATE
, new_group_name
, who
);
389 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
391 (GCompareFunc
)sipe_group_compare
,
393 sipe_group_update_buddy(SIPE_CORE_PRIVATE
, buddy
);
397 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
399 const gchar
*group_name
)
401 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
403 if (!sipe_buddy_find_by_uri(sipe_private
, uri
))
404 sipe_buddy_add(sipe_private
,
409 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
412 sipe_core_buddy_group(sipe_public
,
418 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
419 struct sipe_buddy
*buddy
)
421 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
422 gchar
*action_name
= sipe_utils_presence_key(buddy
->name
);
423 sipe_schedule_cancel(sipe_private
, action_name
);
426 g_hash_table_remove(buddies
->uri
, buddy
->name
);
427 if (buddy
->exchange_key
)
428 g_hash_table_remove(buddies
->exchange_key
,
429 buddy
->exchange_key
);
435 * Unassociates buddy from group first.
436 * Then see if no groups left, removes buddy completely.
437 * Otherwise updates buddy groups on server.
439 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
441 const gchar
*group_name
)
443 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
444 struct sipe_buddy
*b
= sipe_buddy_find_by_uri(sipe_private
,
446 struct sipe_group
*g
= NULL
;
451 g
= sipe_group_find_by_name(sipe_private
, group_name
);
453 b
->groups
= g_slist_remove(b
->groups
, g
);
454 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
459 if (g_slist_length(b
->groups
) < 1) {
461 if (sipe_ucs_is_migrated(sipe_private
)) {
462 sipe_ucs_group_remove_buddy(sipe_private
,
466 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
468 sip_soap_request(sipe_private
,
474 sipe_buddy_remove(sipe_private
, b
);
476 if (sipe_ucs_is_migrated(sipe_private
)) {
477 sipe_ucs_group_remove_buddy(sipe_private
,
481 /* updates groups on server */
482 sipe_group_update_buddy(sipe_private
, b
);
486 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
490 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
491 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
496 /* Check if on 2005 system contact's calendar,
497 * then set/preserve it.
499 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
500 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
502 sipe_ocs2005_apply_calendar_status(sipe_private
,
504 sipe_status_activity_to_token(activity
));
508 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
510 const gchar
*status_name
,
512 struct sipe_backend_buddy_tooltip
*tooltip
)
514 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
516 gboolean is_oof_note
= FALSE
;
517 const gchar
*activity
= NULL
;
518 gchar
*calendar
= NULL
;
519 const gchar
*meeting_subject
= NULL
;
520 const gchar
*meeting_location
= NULL
;
521 gchar
*access_text
= NULL
;
523 #define SIPE_ADD_BUDDY_INFO(l, t) \
525 gchar *tmp = g_markup_escape_text((t), -1); \
526 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
529 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
530 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
532 if (sipe_public
) { /* happens on pidgin exit */
533 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
537 is_oof_note
= sbuddy
->is_oof_note
;
538 activity
= sbuddy
->activity
;
539 calendar
= sipe_cal_get_description(sbuddy
);
540 meeting_subject
= sbuddy
->meeting_subject
;
541 meeting_location
= sbuddy
->meeting_location
;
543 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
544 gboolean is_group_access
= FALSE
;
545 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
547 sipe_get_no_sip_uri(uri
),
549 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
550 access_text
= is_group_access
?
551 g_strdup(access_level
) :
552 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
558 const gchar
*status_str
= activity
? activity
: status_name
;
560 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
562 if (is_online
&& !is_empty(calendar
)) {
563 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
566 if (!is_empty(meeting_location
)) {
567 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
568 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
570 if (!is_empty(meeting_subject
)) {
571 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
572 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
575 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
576 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
577 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
579 g_free(note_italics
);
582 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
587 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
589 sipe_buddy_info_fields propkey
,
590 char *property_value
)
592 GSList
*buddies
, *entry
;
595 property_value
= g_strstrip(property_value
);
597 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
600 sipe_backend_buddy p_buddy
= entry
->data
;
602 /* for Display Name */
603 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
605 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
606 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
607 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
608 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
612 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
613 if (!is_empty(property_value
) &&
614 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
616 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
617 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
621 /* for other properties */
623 if (!is_empty(property_value
)) {
624 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
625 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
626 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
634 g_slist_free(buddies
);
643 sipe_svc_callback
*callback
;
644 struct sipe_svc_session
*session
;
645 gchar
*wsse_security
;
646 struct sipe_backend_search_token
*token
;
647 /* must call ms_dlx_free() */
648 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
649 struct ms_dlx_data
*mdd
);
652 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
654 sipe_utils_slist_free_full(mdd
->search_rows
, g_free
);
655 sipe_svc_session_close(mdd
->session
);
657 g_free(mdd
->wsse_security
);
661 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
662 #define DLX_SEARCH_ITEM \
663 "<AbEntryRequest.ChangeSearchQuery>" \
664 " <SearchOn>%s</SearchOn>" \
665 " <Value>%s</Value>" \
666 "</AbEntryRequest.ChangeSearchQuery>"
668 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
669 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
677 attr
= query_rows
->data
;
678 query_rows
= g_slist_next(query_rows
);
679 value
= query_rows
->data
;
680 query_rows
= g_slist_next(query_rows
);
685 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
691 query
= g_strjoinv(NULL
, attrs
);
692 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
701 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
702 const gchar
*base_uri
,
703 const gchar
*auth_uri
,
704 const gchar
*wsse_security
,
705 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
706 gpointer callback_data
)
708 struct ms_dlx_data
*mdd
= callback_data
;
711 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
713 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
716 if (sipe_svc_ab_entry_request(sipe_private
,
721 g_slist_length(mdd
->search_rows
) / 2,
726 /* keep webticket security token for potential further use */
727 mdd
->wsse_security
= g_strdup(wsse_security
);
729 /* callback data passed down the line */
735 /* no ticket: this will show the minmum information */
736 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
741 mdd
->failed_callback(sipe_private
, mdd
);
744 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
745 struct ms_dlx_data
*mdd
)
747 if (!sipe_webticket_request(sipe_private
,
749 sipe_private
->dlx_uri
,
750 "AddressBookWebTicketBearer",
753 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
754 sipe_private
->dlx_uri
);
755 mdd
->failed_callback(sipe_private
, mdd
);
759 static void search_contacts_finalize(struct sipe_core_private
*sipe_private
,
760 struct sipe_backend_search_results
*results
,
764 gchar
*secondary
= g_strdup_printf(
765 dngettext(PACKAGE_NAME
,
766 "Found %d contact%s:",
767 "Found %d contacts%s:", match_count
),
768 match_count
, more
? _(" (more matched your query)") : "");
770 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
777 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
779 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
781 gpointer callback_data
)
783 struct ms_dlx_data
*mdd
= callback_data
;
786 const sipe_xml
*node
;
787 struct sipe_backend_search_results
*results
;
790 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
794 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
796 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
797 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
799 _("No contacts found"));
804 /* OK, we found something - show the results to the user */
805 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
808 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
809 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
811 _("Unable to display the search results"));
816 /* SearchAbEntryResult can contain duplicates */
817 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
820 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
821 const sipe_xml
*attrs
;
822 gchar
*sip_uri
= NULL
;
823 gchar
*displayname
= NULL
;
824 gchar
*company
= NULL
;
825 gchar
*country
= NULL
;
828 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
830 attrs
= sipe_xml_twin(attrs
)) {
831 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
833 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
836 if (!is_empty(value
)) {
837 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
841 } else if (sipe_strcase_equal(name
, "displayname")) {
845 } else if (sipe_strcase_equal(name
, "mail")) {
849 } else if (sipe_strcase_equal(name
, "company")) {
853 } else if (sipe_strcase_equal(name
, "country")) {
864 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
865 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
866 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
873 g_strfreev(uri_parts
);
875 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
886 search_contacts_finalize(sipe_private
, results
,
887 g_hash_table_size(found
),
889 g_hash_table_destroy(found
);
893 mdd
->failed_callback(sipe_private
, mdd
);
897 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
899 struct transaction
*trans
)
901 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
902 struct sipe_backend_search_results
*results
;
903 sipe_xml
*searchResults
;
904 const sipe_xml
*mrow
;
905 guint match_count
= 0;
906 gboolean more
= FALSE
;
908 /* valid response? */
909 if (msg
->response
!= 200) {
910 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
912 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
914 _("Contact search failed"));
918 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
921 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
922 if (!searchResults
) {
923 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
924 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
926 _("Contact search failed"));
931 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
933 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
934 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
936 _("No contacts found"));
938 sipe_xml_free(searchResults
);
942 /* OK, we found something - show the results to the user */
943 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
944 trans
->payload
->data
);
946 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
947 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
949 _("Unable to display the search results"));
951 sipe_xml_free(searchResults
);
955 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
956 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
957 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
960 sipe_xml_attribute(mrow
, "displayName"),
961 sipe_xml_attribute(mrow
, "company"),
962 sipe_xml_attribute(mrow
, "country"),
963 sipe_xml_attribute(mrow
, "email"));
964 g_strfreev(uri_parts
);
968 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
969 char *data
= sipe_xml_data(mrow
);
970 more
= (g_ascii_strcasecmp(data
, "true") == 0);
974 search_contacts_finalize(sipe_private
, results
, match_count
, more
);
975 sipe_xml_free(searchResults
);
980 static void search_soap_request(struct sipe_core_private
*sipe_private
,
981 struct sipe_backend_search_token
*token
,
984 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
985 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
987 payload
->data
= token
;
989 sip_soap_directory_search(sipe_private
,
992 process_search_contact_response
,
997 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
998 struct ms_dlx_data
*mdd
)
1000 /* error using [MS-DLX] server, retry using Active Directory */
1001 search_soap_request(sipe_private
, mdd
->token
, mdd
->search_rows
);
1005 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
1006 struct sipe_backend_search_token
*token
,
1007 const gchar
*given_name
,
1008 const gchar
*surname
,
1010 const gchar
*company
,
1011 const gchar
*country
)
1013 GSList
*query_rows
= NULL
;
1015 #define ADD_QUERY_ROW(attr, val) \
1017 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1018 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1021 ADD_QUERY_ROW("givenName", given_name
);
1022 ADD_QUERY_ROW("sn", surname
);
1023 ADD_QUERY_ROW("mail", email
);
1024 ADD_QUERY_ROW("company", company
);
1025 ADD_QUERY_ROW("c", country
);
1028 if (SIPE_CORE_PRIVATE
->dlx_uri
!= NULL
) {
1029 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1031 mdd
->search_rows
= query_rows
;
1032 mdd
->max_returns
= 100;
1033 mdd
->callback
= search_ab_entry_response
;
1034 mdd
->failed_callback
= search_ab_entry_failed
;
1035 mdd
->session
= sipe_svc_session_start();
1038 ms_dlx_webticket_request(SIPE_CORE_PRIVATE
, mdd
);
1041 /* no [MS-DLX] server, use Active Directory search instead */
1042 search_soap_request(SIPE_CORE_PRIVATE
, token
, query_rows
);
1043 sipe_utils_slist_free_full(query_rows
, g_free
);
1046 sipe_backend_search_failed(sipe_public
,
1048 _("Invalid contact search query"));
1051 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1052 struct sipe_backend_buddy_info
*info
,
1054 const gchar
*server_alias
,
1057 sipe_backend_buddy bbuddy
;
1058 struct sipe_buddy
*sbuddy
;
1063 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1065 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1070 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1072 if (is_empty(server_alias
)) {
1073 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1076 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1078 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1082 value
= g_strdup(server_alias
);
1085 /* present alias if it differs from server alias */
1086 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1087 if (alias
&& !sipe_strequal(alias
, value
))
1089 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1091 SIPE_BUDDY_INFO_ALIAS
,
1097 if (is_empty(email
)) {
1098 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1100 SIPE_BUDDY_INFO_EMAIL
);
1102 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1104 SIPE_BUDDY_INFO_EMAIL
,
1110 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1112 SIPE_BUDDY_INFO_SITE
);
1114 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1116 SIPE_BUDDY_INFO_SITE
,
1121 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1122 if (sbuddy
&& sbuddy
->device_name
) {
1123 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1125 SIPE_BUDDY_INFO_DEVICE
,
1126 sbuddy
->device_name
);
1129 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1133 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1135 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1136 sipe_xml
*soap_body
,
1137 gpointer callback_data
)
1139 struct ms_dlx_data
*mdd
= callback_data
;
1140 struct sipe_backend_buddy_info
*info
= NULL
;
1141 gchar
*server_alias
= NULL
;
1142 gchar
*email
= NULL
;
1145 const sipe_xml
*node
;
1147 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1150 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1152 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1154 node
= sipe_xml_twin(node
)) {
1155 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1157 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1159 const sipe_xml
*values
= sipe_xml_child(node
,
1162 /* Single value entries */
1163 if (!is_empty(value
)) {
1165 if (sipe_strcase_equal(name
, "displayname")) {
1166 g_free(server_alias
);
1167 server_alias
= value
;
1169 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1171 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1173 } else if (sipe_strcase_equal(name
, "mail")) {
1177 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1179 SIPE_BUDDY_INFO_EMAIL
,
1181 } else if (sipe_strcase_equal(name
, "title")) {
1182 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1184 SIPE_BUDDY_INFO_JOB_TITLE
,
1186 } else if (sipe_strcase_equal(name
, "company")) {
1187 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1189 SIPE_BUDDY_INFO_COMPANY
,
1191 } else if (sipe_strcase_equal(name
, "country")) {
1192 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1194 SIPE_BUDDY_INFO_COUNTRY
,
1198 } else if (values
) {
1199 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1202 if (sipe_strcase_equal(name
, "telephonenumber")) {
1203 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1205 SIPE_BUDDY_INFO_WORK_PHONE
,
1217 /* this will show the minmum information */
1218 get_info_finalize(sipe_private
,
1225 g_free(server_alias
);
1229 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1231 struct transaction
*trans
)
1233 const gchar
*uri
= trans
->payload
->data
;
1234 struct sipe_backend_buddy_info
*info
= NULL
;
1235 gchar
*server_alias
= NULL
;
1236 gchar
*email
= NULL
;
1238 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1239 uri
, sipe_private
->username
);
1241 if (msg
->response
!= 200) {
1242 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1244 sipe_xml
*searchResults
;
1245 const sipe_xml
*mrow
;
1247 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1248 msg
->body
? msg
->body
: "");
1250 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1251 if (!searchResults
) {
1253 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1255 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1257 gchar
*phone_number
;
1259 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1261 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1262 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1263 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1266 * For 2007 system we will take this from ContactCard -
1267 * it has cleaner tel: URIs at least
1269 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1270 char *tel_uri
= sip_to_tel_uri(phone_number
);
1271 /* trims its parameters, so call first */
1272 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1273 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1274 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1275 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1278 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1282 if (!is_empty(server_alias
)) {
1283 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1285 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1288 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1289 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1291 SIPE_BUDDY_INFO_JOB_TITLE
,
1294 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1295 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1297 SIPE_BUDDY_INFO_OFFICE
,
1300 if (!is_empty(phone_number
)) {
1301 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1303 SIPE_BUDDY_INFO_WORK_PHONE
,
1306 g_free(phone_number
);
1307 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1308 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1310 SIPE_BUDDY_INFO_COMPANY
,
1313 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1314 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1316 SIPE_BUDDY_INFO_CITY
,
1319 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1320 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1322 SIPE_BUDDY_INFO_STATE
,
1325 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1326 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1328 SIPE_BUDDY_INFO_COUNTRY
,
1331 if (!is_empty(email
)) {
1332 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1334 SIPE_BUDDY_INFO_EMAIL
,
1338 sipe_xml_free(searchResults
);
1341 /* this will show the minmum information */
1342 get_info_finalize(sipe_private
,
1348 g_free(server_alias
);
1354 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1355 struct ms_dlx_data
*mdd
)
1357 /* error using [MS-DLX] server, retry using Active Directory */
1358 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, FALSE
);
1359 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1361 payload
->destroy
= g_free
;
1362 payload
->data
= mdd
->other
;
1365 sip_soap_directory_search(sipe_private
,
1368 process_get_info_response
,
1375 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1378 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1380 if (sipe_private
->dlx_uri
) {
1381 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1383 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1384 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(who
));
1386 mdd
->other
= g_strdup(who
);
1387 mdd
->max_returns
= 1;
1388 mdd
->callback
= get_info_ab_entry_response
;
1389 mdd
->failed_callback
= get_info_ab_entry_failed
;
1390 mdd
->session
= sipe_svc_session_start();
1392 ms_dlx_webticket_request(sipe_private
, mdd
);
1395 /* no [MS-DLX] server, use Active Directory search instead */
1396 gchar
*row
= g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW
,
1397 "msRTCSIP-PrimaryUserAddress",
1399 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1401 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1404 payload
->destroy
= g_free
;
1405 payload
->data
= g_strdup(who
);
1407 sip_soap_directory_search(sipe_private
,
1410 process_get_info_response
,
1416 static void photo_response_data_free(struct photo_response_data
*data
)
1419 g_free(data
->photo_hash
);
1420 if (data
->request
) {
1421 sipe_http_request_cancel(data
->request
);
1426 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1432 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1434 rdata
->request
= NULL
;
1436 if (status
== SIPE_HTTP_STATUS_OK
) {
1437 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1440 gsize photo_size
= atoi(len_str
);
1441 gpointer photo
= g_new(char, photo_size
);
1444 memcpy(photo
, body
, photo_size
);
1446 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1455 sipe_private
->buddies
->pending_photo_requests
=
1456 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, rdata
);
1458 photo_response_data_free(rdata
);
1461 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1463 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "saml:Assertion", TRUE
);
1464 gchar
*wsse_security_base64
;
1465 gchar
*x_ms_webticket_header
;
1471 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1473 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1474 wsse_security_base64
);
1477 g_free(wsse_security_base64
);
1479 return x_ms_webticket_header
;
1482 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
1484 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1485 sipe_xml
*soap_body
,
1486 gpointer callback_data
)
1488 struct ms_dlx_data
*mdd
= callback_data
;
1489 gchar
*photo_rel_path
= NULL
;
1490 gchar
*photo_hash
= NULL
;
1491 const gchar
*photo_hash_old
=
1492 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, mdd
->other
);
1495 const sipe_xml
*node
;
1497 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1500 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1502 node
= sipe_xml_twin(node
)) {
1503 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
1504 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
1506 if (!is_empty(value
)) {
1507 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
1508 g_free(photo_rel_path
);
1509 photo_rel_path
= value
;
1511 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
1523 if (sipe_private
->addressbook_uri
&& photo_rel_path
&&
1524 photo_hash
&& !sipe_strequal(photo_hash
, photo_hash_old
)) {
1525 gchar
*photo_url
= g_strdup_printf("%s/%s",
1526 sipe_private
->addressbook_uri
, photo_rel_path
);
1527 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
1529 struct photo_response_data
*data
= g_new(struct photo_response_data
, 1);
1530 data
->who
= g_strdup(mdd
->other
);
1531 data
->photo_hash
= photo_hash
;
1534 data
->request
= sipe_http_request_get(sipe_private
,
1536 x_ms_webticket_header
,
1537 process_buddy_photo_response
,
1540 if (data
->request
) {
1541 sipe_private
->buddies
->pending_photo_requests
=
1542 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1543 sipe_http_request_ready(data
->request
);
1545 photo_response_data_free(data
);
1548 g_free(x_ms_webticket_header
);
1552 g_free(photo_rel_path
);
1557 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
1558 struct ms_dlx_data
*mdd
)
1563 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
1566 if (sipe_backend_uses_photo()) {
1568 /* Lync 2013 or newer: use UCS */
1569 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
)) {
1571 sipe_ucs_get_photo(sipe_private
, uri
);
1573 /* Lync 2010: use [MS-DLX] */
1574 } else if (sipe_private
->dlx_uri
&&
1575 sipe_private
->addressbook_uri
) {
1576 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1578 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1579 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(uri
));
1581 mdd
->other
= g_strdup(uri
);
1582 mdd
->max_returns
= 1;
1583 mdd
->callback
= get_photo_ab_entry_response
;
1584 mdd
->failed_callback
= get_photo_ab_entry_failed
;
1585 mdd
->session
= sipe_svc_session_start();
1587 ms_dlx_webticket_request(sipe_private
, mdd
);
1592 static void buddy_refresh_photos_cb(gpointer uri
,
1593 SIPE_UNUSED_PARAMETER gpointer value
,
1594 gpointer sipe_private
)
1596 buddy_fetch_photo(sipe_private
, uri
);
1599 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
1601 g_hash_table_foreach(sipe_private
->buddies
->uri
,
1602 buddy_refresh_photos_cb
,
1606 /* Buddy menu callbacks*/
1608 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
1611 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1613 /* 2007+ conference */
1614 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1615 sipe_conf_add(sipe_private
, who
);
1617 /* 2005- multiparty chat */
1619 gchar
*self
= sip_uri_self(sipe_private
);
1620 struct sip_session
*session
;
1622 session
= sipe_session_add_chat(sipe_private
,
1626 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
1627 session
->chat_session
,
1628 session
->chat_session
->title
,
1632 sipe_im_invite(sipe_private
, session
, who
,
1633 NULL
, NULL
, NULL
, FALSE
);
1637 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
1640 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1643 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1645 SIPE_BUDDY_INFO_EMAIL
);
1648 gchar
*command_line
= g_strdup_printf(
1654 " mailto:%s", email
);
1657 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1659 g_spawn_command_line_async(command_line
, NULL
);
1660 g_free(command_line
);
1663 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1670 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
1671 struct sipe_backend_buddy_menu
*menu
,
1672 sipe_backend_buddy buddy
,
1673 sipe_buddy_info_fields id_phone
,
1674 sipe_buddy_info_fields id_display
,
1677 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
1681 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
1685 gchar
*label
= g_strdup_printf("%s %s",
1688 (tmp
= sip_tel_uri_denormalize(phone
)));
1689 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1692 SIPE_BUDDY_MENU_MAKE_CALL
,
1703 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
1704 const gchar
*buddy_name
,
1705 struct sipe_backend_buddy_menu
*menu
)
1707 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1708 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1711 gchar
*self
= sip_uri_self(sipe_private
);
1713 SIPE_SESSION_FOREACH
{
1714 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
1716 struct sipe_chat_session
*chat_session
= session
->chat_session
;
1717 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
1719 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
1721 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
1725 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
1726 /* We are a conf OP */
1728 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
1729 chat_session
->title
);
1730 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1733 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
1739 /* We are a conf OP */
1741 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
1742 chat_session
->title
);
1743 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1746 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
1754 (is_conf
&& !session
->locked
)) {
1755 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
1756 chat_session
->title
);
1757 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1760 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
1766 } SIPE_SESSION_FOREACH_END
;
1769 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1772 SIPE_BUDDY_MENU_NEW_CHAT
,
1775 /* add buddy's phone numbers if we have call control */
1776 if (sip_csta_is_idle(sipe_private
)) {
1779 menu
= buddy_menu_phone(sipe_public
,
1782 SIPE_BUDDY_INFO_WORK_PHONE
,
1783 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
1786 menu
= buddy_menu_phone(sipe_public
,
1789 SIPE_BUDDY_INFO_MOBILE_PHONE
,
1790 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
1794 menu
= buddy_menu_phone(sipe_public
,
1797 SIPE_BUDDY_INFO_HOME_PHONE
,
1798 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
1802 menu
= buddy_menu_phone(sipe_public
,
1805 SIPE_BUDDY_INFO_OTHER_PHONE
,
1806 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
1810 menu
= buddy_menu_phone(sipe_public
,
1813 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
1814 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
1819 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1821 SIPE_BUDDY_INFO_EMAIL
);
1823 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1826 SIPE_BUDDY_MENU_SEND_EMAIL
,
1832 /* access level control */
1833 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
1834 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
1837 sipe_ocs2007_access_control_menu(sipe_private
,
1843 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
1845 return(g_hash_table_size(sipe_private
->buddies
->uri
));
1848 static guint
sipe_ht_hash_nick(const char *nick
)
1850 char *lc
= g_utf8_strdown(nick
, -1);
1851 guint bucket
= g_str_hash(lc
);
1857 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
1859 char *nick1_norm
= NULL
;
1860 char *nick2_norm
= NULL
;
1863 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
1864 if (nick1
== NULL
|| nick2
== NULL
||
1865 !g_utf8_validate(nick1
, -1, NULL
) ||
1866 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
1868 nick1_norm
= g_utf8_casefold(nick1
, -1);
1869 nick2_norm
= g_utf8_casefold(nick2
, -1);
1870 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
1877 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
1879 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
1880 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
1881 (GEqualFunc
) sipe_ht_equals_nick
);
1882 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
1884 sipe_private
->buddies
= buddies
;