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
)
83 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
85 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
87 buddy
= g_new0(struct sipe_buddy
, 1);
88 buddy
->name
= g_strdup(uri
);
89 g_hash_table_insert(buddies
->uri
,
94 buddy
->exchange_key
= g_strdup(exchange_key
);
95 g_hash_table_insert(buddies
->exchange_key
,
101 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", uri
);
103 buddy_fetch_photo(sipe_private
, uri
);
105 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", uri
);
111 void sipe_buddy_add_to_group(struct sipe_core_private
*sipe_private
,
112 struct sipe_buddy
*buddy
,
113 struct sipe_group
*group
,
116 const gchar
*uri
= buddy
->name
;
117 const gchar
*group_name
= group
->name
;
118 sipe_backend_buddy bb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
123 bb
= sipe_backend_buddy_add(SIPE_CORE_PUBLIC
,
127 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
128 uri
, alias
? alias
: "<NONE>");
132 if (!is_empty(alias
)) {
133 gchar
*old_alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
136 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri
),
138 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
,
141 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
142 uri
, old_alias
, alias
);
147 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
149 (GCompareFunc
) sipe_group_compare
,
151 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
155 void sipe_buddy_cleanup_local_list(struct sipe_core_private
*sipe_private
)
157 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
160 GSList
*entry
= buddies
;
162 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
163 g_slist_length(buddies
));
164 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
165 sipe_buddy_count(sipe_private
));
167 sipe_backend_buddy bb
= entry
->data
;
168 gchar
*bname
= sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC
,
170 gchar
*gname
= sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC
,
172 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
174 gboolean in_sipe_groups
= FALSE
;
177 GSList
*entry2
= buddy
->groups
;
180 struct sipe_group
*group
= entry2
->data
;
181 if (sipe_strequal(group
->name
, gname
)) {
182 in_sipe_groups
= TRUE
;
185 entry2
= entry2
->next
;
189 if (!in_sipe_groups
) {
190 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",
192 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, bb
);
201 g_slist_free(buddies
);
204 struct sipe_buddy
*sipe_buddy_find_by_uri(struct sipe_core_private
*sipe_private
,
207 return(g_hash_table_lookup(sipe_private
->buddies
->uri
, uri
));
210 struct sipe_buddy
*sipe_buddy_find_by_exchange_key(struct sipe_core_private
*sipe_private
,
211 const gchar
*exchange_key
)
213 return(g_hash_table_lookup(sipe_private
->buddies
->exchange_key
,
217 void sipe_buddy_foreach(struct sipe_core_private
*sipe_private
,
219 gpointer callback_data
)
221 g_hash_table_foreach(sipe_private
->buddies
->uri
,
226 static void buddy_free(struct sipe_buddy
*buddy
)
230 * We are calling g_hash_table_foreach_steal(). That means that no
231 * key/value deallocation functions are called. Therefore the glib
232 * hash code does not touch the key (buddy->name) or value (buddy)
233 * of the to-be-deleted hash node at all. It follows that we
235 * - MUST free the memory for the key ourselves and
236 * - ARE allowed to do it in this function
238 * Conclusion: glib must be broken on the Windows platform if sipe
239 * crashes with SIGTRAP when closing. You'll have to live
240 * with the memory leak until this is fixed.
244 g_free(buddy
->exchange_key
);
245 g_free(buddy
->activity
);
246 g_free(buddy
->meeting_subject
);
247 g_free(buddy
->meeting_location
);
250 g_free(buddy
->cal_start_time
);
251 g_free(buddy
->cal_free_busy_base64
);
252 g_free(buddy
->cal_free_busy
);
253 g_free(buddy
->last_non_cal_activity
);
255 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
257 g_free(buddy
->device_name
);
258 g_slist_free(buddy
->groups
);
262 static gboolean
buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key
,
264 SIPE_UNUSED_PARAMETER gpointer user_data
)
267 /* We must return TRUE as the key/value have already been deleted */
271 void sipe_buddy_free(struct sipe_core_private
*sipe_private
)
273 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
275 g_hash_table_foreach_steal(buddies
->uri
,
279 /* core is being deallocated, remove all its pending photo requests */
280 while (buddies
->pending_photo_requests
) {
281 struct photo_response_data
*data
=
282 buddies
->pending_photo_requests
->data
;
283 buddies
->pending_photo_requests
=
284 g_slist_remove(buddies
->pending_photo_requests
, data
);
285 photo_response_data_free(data
);
288 g_hash_table_destroy(buddies
->uri
);
289 g_hash_table_destroy(buddies
->exchange_key
);
291 sipe_private
->buddies
= NULL
;
294 gchar
*sipe_core_buddy_status(struct sipe_core_public
*sipe_public
,
297 const gchar
*status_text
)
299 struct sipe_buddy
*sbuddy
;
300 const char *activity_str
;
302 if (!sipe_public
) return NULL
; /* happens on pidgin exit */
304 sbuddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
, uri
);
305 if (!sbuddy
) return NULL
;
307 activity_str
= sbuddy
->activity
? sbuddy
->activity
:
308 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
311 if (activity_str
&& sbuddy
->note
) {
312 return g_strdup_printf("%s - <i>%s</i>", activity_str
, sbuddy
->note
);
313 } else if (activity_str
) {
314 return g_strdup(activity_str
);
315 } else if (sbuddy
->note
) {
316 return g_strdup_printf("<i>%s</i>", sbuddy
->note
);
322 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
325 sipe_backend_buddy pbuddy
;
327 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
328 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
333 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
335 const gchar
*old_group_name
,
336 const gchar
*new_group_name
)
338 struct sipe_buddy
* buddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
,
340 struct sipe_group
* old_group
= NULL
;
341 struct sipe_group
* new_group
;
343 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
344 who
? who
: "", old_group_name
? old_group_name
: "", new_group_name
? new_group_name
: "");
346 if(!buddy
) { // buddy not in roaming list
350 if (old_group_name
) {
351 old_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, old_group_name
);
353 new_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, new_group_name
);
356 buddy
->groups
= g_slist_remove(buddy
->groups
, old_group
);
357 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who
, old_group_name
);
361 sipe_group_create(SIPE_CORE_PRIVATE
, new_group_name
, who
);
363 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
365 (GCompareFunc
)sipe_group_compare
,
367 sipe_group_update_buddy(SIPE_CORE_PRIVATE
, buddy
);
371 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
373 const gchar
*group_name
)
375 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
377 if (!sipe_buddy_find_by_uri(sipe_private
, uri
)) {
378 struct sipe_buddy
*b
= sipe_buddy_add(sipe_private
, uri
, NULL
);
379 b
->just_added
= TRUE
;
381 sipe_subscribe_presence_single_cb(sipe_private
, b
->name
);
384 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
388 sipe_core_buddy_group(sipe_public
,
394 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
395 struct sipe_buddy
*buddy
)
397 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
398 gchar
*action_name
= sipe_utils_presence_key(buddy
->name
);
399 sipe_schedule_cancel(sipe_private
, action_name
);
402 g_hash_table_remove(buddies
->uri
, buddy
->name
);
403 if (buddy
->exchange_key
)
404 g_hash_table_remove(buddies
->exchange_key
,
405 buddy
->exchange_key
);
411 * Unassociates buddy from group first.
412 * Then see if no groups left, removes buddy completely.
413 * Otherwise updates buddy groups on server.
415 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
417 const gchar
*group_name
)
419 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
420 struct sipe_buddy
*b
= sipe_buddy_find_by_uri(sipe_private
,
426 struct sipe_group
*g
= sipe_group_find_by_name(sipe_private
,
429 b
->groups
= g_slist_remove(b
->groups
, g
);
430 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
435 if (g_slist_length(b
->groups
) < 1) {
436 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
438 sip_soap_request(sipe_private
,
442 sipe_buddy_remove(sipe_private
, b
);
444 /* updates groups on server */
445 sipe_group_update_buddy(sipe_private
, b
);
450 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
454 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
455 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
460 /* Check if on 2005 system contact's calendar,
461 * then set/preserve it.
463 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
464 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
466 sipe_ocs2005_apply_calendar_status(sipe_private
,
468 sipe_status_activity_to_token(activity
));
472 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
474 const gchar
*status_name
,
476 struct sipe_backend_buddy_tooltip
*tooltip
)
478 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
480 gboolean is_oof_note
= FALSE
;
481 const gchar
*activity
= NULL
;
482 gchar
*calendar
= NULL
;
483 const gchar
*meeting_subject
= NULL
;
484 const gchar
*meeting_location
= NULL
;
485 gchar
*access_text
= NULL
;
487 #define SIPE_ADD_BUDDY_INFO(l, t) \
489 gchar *tmp = g_markup_escape_text((t), -1); \
490 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
493 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
494 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
496 if (sipe_public
) { /* happens on pidgin exit */
497 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
501 is_oof_note
= sbuddy
->is_oof_note
;
502 activity
= sbuddy
->activity
;
503 calendar
= sipe_cal_get_description(sbuddy
);
504 meeting_subject
= sbuddy
->meeting_subject
;
505 meeting_location
= sbuddy
->meeting_location
;
507 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
508 gboolean is_group_access
= FALSE
;
509 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
511 sipe_get_no_sip_uri(uri
),
513 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
514 access_text
= is_group_access
?
515 g_strdup(access_level
) :
516 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
522 const gchar
*status_str
= activity
? activity
: status_name
;
524 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
526 if (is_online
&& !is_empty(calendar
)) {
527 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
530 if (!is_empty(meeting_location
)) {
531 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
532 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
534 if (!is_empty(meeting_subject
)) {
535 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
536 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
539 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
540 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
541 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
543 g_free(note_italics
);
546 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
551 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
553 sipe_buddy_info_fields propkey
,
554 char *property_value
)
556 GSList
*buddies
, *entry
;
559 property_value
= g_strstrip(property_value
);
561 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
564 sipe_backend_buddy p_buddy
= entry
->data
;
566 /* for Display Name */
567 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
569 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
570 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
571 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
572 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
576 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
577 if (!is_empty(property_value
) &&
578 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
580 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
581 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
585 /* for other properties */
587 if (!is_empty(property_value
)) {
588 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
589 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
590 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
598 g_slist_free(buddies
);
607 sipe_svc_callback
*callback
;
608 struct sipe_svc_session
*session
;
609 gchar
*wsse_security
;
610 struct sipe_backend_search_token
*token
;
611 /* must call ms_dlx_free() */
612 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
613 struct ms_dlx_data
*mdd
);
616 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
618 sipe_utils_slist_free_full(mdd
->search_rows
, g_free
);
619 sipe_svc_session_close(mdd
->session
);
621 g_free(mdd
->wsse_security
);
625 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
626 #define DLX_SEARCH_ITEM \
627 "<AbEntryRequest.ChangeSearchQuery>" \
628 " <SearchOn>%s</SearchOn>" \
629 " <Value>%s</Value>" \
630 "</AbEntryRequest.ChangeSearchQuery>"
632 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
633 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
641 attr
= query_rows
->data
;
642 query_rows
= g_slist_next(query_rows
);
643 value
= query_rows
->data
;
644 query_rows
= g_slist_next(query_rows
);
649 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
655 query
= g_strjoinv(NULL
, attrs
);
656 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
665 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
666 const gchar
*base_uri
,
667 const gchar
*auth_uri
,
668 const gchar
*wsse_security
,
669 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
670 gpointer callback_data
)
672 struct ms_dlx_data
*mdd
= callback_data
;
675 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
677 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
680 if (sipe_svc_ab_entry_request(sipe_private
,
685 g_slist_length(mdd
->search_rows
) / 2,
690 /* keep webticket security token for potential further use */
691 mdd
->wsse_security
= g_strdup(wsse_security
);
693 /* callback data passed down the line */
699 /* no ticket: this will show the minmum information */
700 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
705 mdd
->failed_callback(sipe_private
, mdd
);
708 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
709 struct ms_dlx_data
*mdd
)
711 if (!sipe_webticket_request(sipe_private
,
713 sipe_private
->dlx_uri
,
714 "AddressBookWebTicketBearer",
717 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
718 sipe_private
->dlx_uri
);
719 mdd
->failed_callback(sipe_private
, mdd
);
723 static void search_contacts_finalize(struct sipe_core_private
*sipe_private
,
724 struct sipe_backend_search_results
*results
,
728 gchar
*secondary
= g_strdup_printf(
729 dngettext(PACKAGE_NAME
,
730 "Found %d contact%s:",
731 "Found %d contacts%s:", match_count
),
732 match_count
, more
? _(" (more matched your query)") : "");
734 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
741 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
743 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
745 gpointer callback_data
)
747 struct ms_dlx_data
*mdd
= callback_data
;
750 const sipe_xml
*node
;
751 struct sipe_backend_search_results
*results
;
754 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
758 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
760 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
761 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
763 _("No contacts found"));
768 /* OK, we found something - show the results to the user */
769 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
772 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
773 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
775 _("Unable to display the search results"));
780 /* SearchAbEntryResult can contain duplicates */
781 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
784 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
785 const sipe_xml
*attrs
;
786 gchar
*sip_uri
= NULL
;
787 gchar
*displayname
= NULL
;
788 gchar
*company
= NULL
;
789 gchar
*country
= NULL
;
792 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
794 attrs
= sipe_xml_twin(attrs
)) {
795 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
797 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
800 if (!is_empty(value
)) {
801 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
805 } else if (sipe_strcase_equal(name
, "displayname")) {
809 } else if (sipe_strcase_equal(name
, "mail")) {
813 } else if (sipe_strcase_equal(name
, "company")) {
817 } else if (sipe_strcase_equal(name
, "country")) {
828 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
829 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
830 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
837 g_strfreev(uri_parts
);
839 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
850 search_contacts_finalize(sipe_private
, results
,
851 g_hash_table_size(found
),
853 g_hash_table_destroy(found
);
857 mdd
->failed_callback(sipe_private
, mdd
);
861 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
863 struct transaction
*trans
)
865 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
866 struct sipe_backend_search_results
*results
;
867 sipe_xml
*searchResults
;
868 const sipe_xml
*mrow
;
869 guint match_count
= 0;
870 gboolean more
= FALSE
;
872 /* valid response? */
873 if (msg
->response
!= 200) {
874 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
876 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
878 _("Contact search failed"));
882 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
885 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
886 if (!searchResults
) {
887 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
888 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
890 _("Contact search failed"));
895 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
897 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
898 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
900 _("No contacts found"));
902 sipe_xml_free(searchResults
);
906 /* OK, we found something - show the results to the user */
907 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
908 trans
->payload
->data
);
910 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
911 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
913 _("Unable to display the search results"));
915 sipe_xml_free(searchResults
);
919 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
920 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
921 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
924 sipe_xml_attribute(mrow
, "displayName"),
925 sipe_xml_attribute(mrow
, "company"),
926 sipe_xml_attribute(mrow
, "country"),
927 sipe_xml_attribute(mrow
, "email"));
928 g_strfreev(uri_parts
);
932 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
933 char *data
= sipe_xml_data(mrow
);
934 more
= (g_ascii_strcasecmp(data
, "true") == 0);
938 search_contacts_finalize(sipe_private
, results
, match_count
, more
);
939 sipe_xml_free(searchResults
);
944 static void search_soap_request(struct sipe_core_private
*sipe_private
,
945 struct sipe_backend_search_token
*token
,
948 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
949 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
951 payload
->data
= token
;
953 sip_soap_directory_search(sipe_private
,
956 process_search_contact_response
,
961 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
962 struct ms_dlx_data
*mdd
)
964 /* error using [MS-DLX] server, retry using Active Directory */
965 search_soap_request(sipe_private
, mdd
->token
, mdd
->search_rows
);
969 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
970 struct sipe_backend_search_token
*token
,
971 const gchar
*given_name
,
972 const gchar
*surname
,
974 const gchar
*company
,
975 const gchar
*country
)
977 GSList
*query_rows
= NULL
;
979 #define ADD_QUERY_ROW(attr, val) \
981 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
982 query_rows = g_slist_append(query_rows, g_strdup(val)); \
985 ADD_QUERY_ROW("givenName", given_name
);
986 ADD_QUERY_ROW("sn", surname
);
987 ADD_QUERY_ROW("mail", email
);
988 ADD_QUERY_ROW("company", company
);
989 ADD_QUERY_ROW("c", country
);
992 if (SIPE_CORE_PRIVATE
->dlx_uri
!= NULL
) {
993 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
995 mdd
->search_rows
= query_rows
;
996 mdd
->max_returns
= 100;
997 mdd
->callback
= search_ab_entry_response
;
998 mdd
->failed_callback
= search_ab_entry_failed
;
999 mdd
->session
= sipe_svc_session_start();
1002 ms_dlx_webticket_request(SIPE_CORE_PRIVATE
, mdd
);
1005 /* no [MS-DLX] server, use Active Directory search instead */
1006 search_soap_request(SIPE_CORE_PRIVATE
, token
, query_rows
);
1007 sipe_utils_slist_free_full(query_rows
, g_free
);
1010 sipe_backend_search_failed(sipe_public
,
1012 _("Invalid contact search query"));
1015 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1016 struct sipe_backend_buddy_info
*info
,
1018 const gchar
*server_alias
,
1021 sipe_backend_buddy bbuddy
;
1022 struct sipe_buddy
*sbuddy
;
1027 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1029 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1034 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1036 if (is_empty(server_alias
)) {
1037 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1040 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1042 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1046 value
= g_strdup(server_alias
);
1049 /* present alias if it differs from server alias */
1050 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1051 if (alias
&& !sipe_strequal(alias
, value
))
1053 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1055 SIPE_BUDDY_INFO_ALIAS
,
1061 if (is_empty(email
)) {
1062 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1064 SIPE_BUDDY_INFO_EMAIL
);
1066 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1068 SIPE_BUDDY_INFO_EMAIL
,
1074 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1076 SIPE_BUDDY_INFO_SITE
);
1078 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1080 SIPE_BUDDY_INFO_SITE
,
1085 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1086 if (sbuddy
&& sbuddy
->device_name
) {
1087 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1089 SIPE_BUDDY_INFO_DEVICE
,
1090 sbuddy
->device_name
);
1093 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1097 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1099 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1100 sipe_xml
*soap_body
,
1101 gpointer callback_data
)
1103 struct ms_dlx_data
*mdd
= callback_data
;
1104 struct sipe_backend_buddy_info
*info
= NULL
;
1105 gchar
*server_alias
= NULL
;
1106 gchar
*email
= NULL
;
1109 const sipe_xml
*node
;
1111 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1114 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1116 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1118 node
= sipe_xml_twin(node
)) {
1119 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1121 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1123 const sipe_xml
*values
= sipe_xml_child(node
,
1126 /* Single value entries */
1127 if (!is_empty(value
)) {
1129 if (sipe_strcase_equal(name
, "displayname")) {
1130 g_free(server_alias
);
1131 server_alias
= value
;
1133 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1135 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1137 } else if (sipe_strcase_equal(name
, "mail")) {
1141 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1143 SIPE_BUDDY_INFO_EMAIL
,
1145 } else if (sipe_strcase_equal(name
, "title")) {
1146 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1148 SIPE_BUDDY_INFO_JOB_TITLE
,
1150 } else if (sipe_strcase_equal(name
, "company")) {
1151 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1153 SIPE_BUDDY_INFO_COMPANY
,
1155 } else if (sipe_strcase_equal(name
, "country")) {
1156 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1158 SIPE_BUDDY_INFO_COUNTRY
,
1162 } else if (values
) {
1163 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1166 if (sipe_strcase_equal(name
, "telephonenumber")) {
1167 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1169 SIPE_BUDDY_INFO_WORK_PHONE
,
1181 /* this will show the minmum information */
1182 get_info_finalize(sipe_private
,
1189 g_free(server_alias
);
1193 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1195 struct transaction
*trans
)
1197 const gchar
*uri
= trans
->payload
->data
;
1198 struct sipe_backend_buddy_info
*info
= NULL
;
1199 gchar
*server_alias
= NULL
;
1200 gchar
*email
= NULL
;
1202 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1203 uri
, sipe_private
->username
);
1205 if (msg
->response
!= 200) {
1206 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1208 sipe_xml
*searchResults
;
1209 const sipe_xml
*mrow
;
1211 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1212 msg
->body
? msg
->body
: "");
1214 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1215 if (!searchResults
) {
1217 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1219 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1221 gchar
*phone_number
;
1223 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1225 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1226 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1227 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1230 * For 2007 system we will take this from ContactCard -
1231 * it has cleaner tel: URIs at least
1233 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1234 char *tel_uri
= sip_to_tel_uri(phone_number
);
1235 /* trims its parameters, so call first */
1236 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1237 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1238 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1239 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1242 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1246 if (!is_empty(server_alias
)) {
1247 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1249 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1252 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1253 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1255 SIPE_BUDDY_INFO_JOB_TITLE
,
1258 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1259 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1261 SIPE_BUDDY_INFO_OFFICE
,
1264 if (!is_empty(phone_number
)) {
1265 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1267 SIPE_BUDDY_INFO_WORK_PHONE
,
1270 g_free(phone_number
);
1271 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1272 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1274 SIPE_BUDDY_INFO_COMPANY
,
1277 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1278 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1280 SIPE_BUDDY_INFO_CITY
,
1283 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1284 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1286 SIPE_BUDDY_INFO_STATE
,
1289 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1290 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1292 SIPE_BUDDY_INFO_COUNTRY
,
1295 if (!is_empty(email
)) {
1296 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1298 SIPE_BUDDY_INFO_EMAIL
,
1302 sipe_xml_free(searchResults
);
1305 /* this will show the minmum information */
1306 get_info_finalize(sipe_private
,
1312 g_free(server_alias
);
1318 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1319 struct ms_dlx_data
*mdd
)
1321 /* error using [MS-DLX] server, retry using Active Directory */
1322 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, FALSE
);
1323 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1325 payload
->destroy
= g_free
;
1326 payload
->data
= mdd
->other
;
1329 sip_soap_directory_search(sipe_private
,
1332 process_get_info_response
,
1339 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1342 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1344 if (sipe_private
->dlx_uri
) {
1345 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1347 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1348 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(who
));
1350 mdd
->other
= g_strdup(who
);
1351 mdd
->max_returns
= 1;
1352 mdd
->callback
= get_info_ab_entry_response
;
1353 mdd
->failed_callback
= get_info_ab_entry_failed
;
1354 mdd
->session
= sipe_svc_session_start();
1356 ms_dlx_webticket_request(sipe_private
, mdd
);
1359 /* no [MS-DLX] server, use Active Directory search instead */
1360 gchar
*row
= g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW
,
1361 "msRTCSIP-PrimaryUserAddress",
1363 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1365 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1368 payload
->destroy
= g_free
;
1369 payload
->data
= g_strdup(who
);
1371 sip_soap_directory_search(sipe_private
,
1374 process_get_info_response
,
1380 static void photo_response_data_free(struct photo_response_data
*data
)
1383 g_free(data
->photo_hash
);
1384 if (data
->request
) {
1385 sipe_http_request_cancel(data
->request
);
1390 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1396 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1398 rdata
->request
= NULL
;
1400 if (status
== SIPE_HTTP_STATUS_OK
) {
1401 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1404 gsize photo_size
= atoi(len_str
);
1405 gpointer photo
= g_new(char, photo_size
);
1408 memcpy(photo
, body
, photo_size
);
1410 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1419 sipe_private
->buddies
->pending_photo_requests
=
1420 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, rdata
);
1422 photo_response_data_free(rdata
);
1425 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1427 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "saml:Assertion", TRUE
);
1428 gchar
*wsse_security_base64
;
1429 gchar
*x_ms_webticket_header
;
1435 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1437 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1438 wsse_security_base64
);
1441 g_free(wsse_security_base64
);
1443 return x_ms_webticket_header
;
1446 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
1448 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1449 sipe_xml
*soap_body
,
1450 gpointer callback_data
)
1452 struct ms_dlx_data
*mdd
= callback_data
;
1453 gchar
*photo_rel_path
= NULL
;
1454 gchar
*photo_hash
= NULL
;
1455 const gchar
*photo_hash_old
=
1456 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, mdd
->other
);
1459 const sipe_xml
*node
;
1461 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1464 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1466 node
= sipe_xml_twin(node
)) {
1467 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
1468 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
1470 if (!is_empty(value
)) {
1471 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
1472 g_free(photo_rel_path
);
1473 photo_rel_path
= value
;
1475 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
1487 if (sipe_private
->addressbook_uri
&& photo_rel_path
&&
1488 photo_hash
&& !sipe_strequal(photo_hash
, photo_hash_old
)) {
1489 gchar
*photo_url
= g_strdup_printf("%s/%s",
1490 sipe_private
->addressbook_uri
, photo_rel_path
);
1491 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
1493 struct photo_response_data
*data
= g_new(struct photo_response_data
, 1);
1494 data
->who
= g_strdup(mdd
->other
);
1495 data
->photo_hash
= photo_hash
;
1498 data
->request
= sipe_http_request_get(sipe_private
,
1500 x_ms_webticket_header
,
1501 process_buddy_photo_response
,
1504 if (data
->request
) {
1505 sipe_private
->buddies
->pending_photo_requests
=
1506 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1507 sipe_http_request_ready(data
->request
);
1509 photo_response_data_free(data
);
1512 g_free(x_ms_webticket_header
);
1516 g_free(photo_rel_path
);
1521 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
1522 struct ms_dlx_data
*mdd
)
1527 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
1530 if (sipe_backend_uses_photo()) {
1532 /* Lync 2013 or newer: use UCS */
1533 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
)) {
1535 sipe_ucs_get_photo(sipe_private
, uri
);
1537 /* Lync 2010: use [MS-DLX] */
1538 } else if (sipe_private
->dlx_uri
&&
1539 sipe_private
->addressbook_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(uri
));
1545 mdd
->other
= g_strdup(uri
);
1546 mdd
->max_returns
= 1;
1547 mdd
->callback
= get_photo_ab_entry_response
;
1548 mdd
->failed_callback
= get_photo_ab_entry_failed
;
1549 mdd
->session
= sipe_svc_session_start();
1551 ms_dlx_webticket_request(sipe_private
, mdd
);
1556 static void buddy_refresh_photos_cb(gpointer uri
,
1557 SIPE_UNUSED_PARAMETER gpointer value
,
1558 gpointer sipe_private
)
1560 buddy_fetch_photo(sipe_private
, uri
);
1563 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
1565 g_hash_table_foreach(sipe_private
->buddies
->uri
,
1566 buddy_refresh_photos_cb
,
1570 /* Buddy menu callbacks*/
1572 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
1575 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1577 /* 2007+ conference */
1578 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1579 sipe_conf_add(sipe_private
, who
);
1581 /* 2005- multiparty chat */
1583 gchar
*self
= sip_uri_self(sipe_private
);
1584 struct sip_session
*session
;
1586 session
= sipe_session_add_chat(sipe_private
,
1590 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
1591 session
->chat_session
,
1592 session
->chat_session
->title
,
1596 sipe_im_invite(sipe_private
, session
, who
,
1597 NULL
, NULL
, NULL
, FALSE
);
1601 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
1604 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1607 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1609 SIPE_BUDDY_INFO_EMAIL
);
1612 gchar
*command_line
= g_strdup_printf(
1618 " mailto:%s", email
);
1621 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1623 g_spawn_command_line_async(command_line
, NULL
);
1624 g_free(command_line
);
1627 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1634 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
1635 struct sipe_backend_buddy_menu
*menu
,
1636 sipe_backend_buddy buddy
,
1637 sipe_buddy_info_fields id_phone
,
1638 sipe_buddy_info_fields id_display
,
1641 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
1645 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
1649 gchar
*label
= g_strdup_printf("%s %s",
1652 (tmp
= sip_tel_uri_denormalize(phone
)));
1653 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1656 SIPE_BUDDY_MENU_MAKE_CALL
,
1667 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
1668 const gchar
*buddy_name
,
1669 struct sipe_backend_buddy_menu
*menu
)
1671 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1672 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1675 gchar
*self
= sip_uri_self(sipe_private
);
1677 SIPE_SESSION_FOREACH
{
1678 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
1680 struct sipe_chat_session
*chat_session
= session
->chat_session
;
1681 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
1683 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
1685 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
1689 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
1690 /* We are a conf OP */
1692 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
1693 chat_session
->title
);
1694 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1697 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
1703 /* We are a conf OP */
1705 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
1706 chat_session
->title
);
1707 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1710 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
1718 (is_conf
&& !session
->locked
)) {
1719 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
1720 chat_session
->title
);
1721 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1724 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
1730 } SIPE_SESSION_FOREACH_END
;
1733 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1736 SIPE_BUDDY_MENU_NEW_CHAT
,
1739 /* add buddy's phone numbers if we have call control */
1740 if (sip_csta_is_idle(sipe_private
)) {
1743 menu
= buddy_menu_phone(sipe_public
,
1746 SIPE_BUDDY_INFO_WORK_PHONE
,
1747 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
1750 menu
= buddy_menu_phone(sipe_public
,
1753 SIPE_BUDDY_INFO_MOBILE_PHONE
,
1754 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
1758 menu
= buddy_menu_phone(sipe_public
,
1761 SIPE_BUDDY_INFO_HOME_PHONE
,
1762 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
1766 menu
= buddy_menu_phone(sipe_public
,
1769 SIPE_BUDDY_INFO_OTHER_PHONE
,
1770 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
1774 menu
= buddy_menu_phone(sipe_public
,
1777 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
1778 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
1783 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1785 SIPE_BUDDY_INFO_EMAIL
);
1787 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1790 SIPE_BUDDY_MENU_SEND_EMAIL
,
1796 /* access level control */
1797 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
1798 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
1801 sipe_ocs2007_access_control_menu(sipe_private
,
1807 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
1809 return(g_hash_table_size(sipe_private
->buddies
->uri
));
1812 static guint
sipe_ht_hash_nick(const char *nick
)
1814 char *lc
= g_utf8_strdown(nick
, -1);
1815 guint bucket
= g_str_hash(lc
);
1821 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
1823 char *nick1_norm
= NULL
;
1824 char *nick2_norm
= NULL
;
1827 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
1828 if (nick1
== NULL
|| nick2
== NULL
||
1829 !g_utf8_validate(nick1
, -1, NULL
) ||
1830 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
1832 nick1_norm
= g_utf8_casefold(nick1
, -1);
1833 nick2_norm
= g_utf8_casefold(nick2
, -1);
1834 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
1841 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
1843 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
1844 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
1845 (GEqualFunc
) sipe_ht_equals_nick
);
1846 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
1848 sipe_private
->buddies
= buddies
;