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 buddy_fetch_photo(sipe_private
, normalized_uri
);
112 normalized_uri
= NULL
; /* buddy takes ownership */
114 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri
);
116 g_free(normalized_uri
);
121 void sipe_buddy_add_to_group(struct sipe_core_private
*sipe_private
,
122 struct sipe_buddy
*buddy
,
123 struct sipe_group
*group
,
126 const gchar
*uri
= buddy
->name
;
127 const gchar
*group_name
= group
->name
;
128 sipe_backend_buddy bb
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
,
133 bb
= sipe_backend_buddy_add(SIPE_CORE_PUBLIC
,
137 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
138 uri
, alias
? alias
: "<NONE>");
142 if (!is_empty(alias
)) {
143 gchar
*old_alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
,
146 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri
),
148 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
,
151 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
152 uri
, old_alias
, alias
);
157 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
159 (GCompareFunc
) sipe_group_compare
,
161 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
165 void sipe_buddy_cleanup_local_list(struct sipe_core_private
*sipe_private
)
167 GSList
*buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
,
170 GSList
*entry
= buddies
;
172 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
173 g_slist_length(buddies
));
174 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
175 sipe_buddy_count(sipe_private
));
177 sipe_backend_buddy bb
= entry
->data
;
178 gchar
*bname
= sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC
,
180 gchar
*gname
= sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC
,
182 struct sipe_buddy
*buddy
= sipe_buddy_find_by_uri(sipe_private
,
184 gboolean in_sipe_groups
= FALSE
;
187 GSList
*entry2
= buddy
->groups
;
190 struct sipe_group
*group
= entry2
->data
;
191 if (sipe_strequal(group
->name
, gname
)) {
192 in_sipe_groups
= TRUE
;
195 entry2
= entry2
->next
;
199 if (!in_sipe_groups
) {
200 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",
202 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC
, bb
);
211 g_slist_free(buddies
);
214 struct sipe_buddy
*sipe_buddy_find_by_uri(struct sipe_core_private
*sipe_private
,
217 return(g_hash_table_lookup(sipe_private
->buddies
->uri
, uri
));
220 struct sipe_buddy
*sipe_buddy_find_by_exchange_key(struct sipe_core_private
*sipe_private
,
221 const gchar
*exchange_key
)
223 return(g_hash_table_lookup(sipe_private
->buddies
->exchange_key
,
227 void sipe_buddy_foreach(struct sipe_core_private
*sipe_private
,
229 gpointer callback_data
)
231 g_hash_table_foreach(sipe_private
->buddies
->uri
,
236 static void buddy_free(struct sipe_buddy
*buddy
)
240 * We are calling g_hash_table_foreach_steal(). That means that no
241 * key/value deallocation functions are called. Therefore the glib
242 * hash code does not touch the key (buddy->name) or value (buddy)
243 * of the to-be-deleted hash node at all. It follows that we
245 * - MUST free the memory for the key ourselves and
246 * - ARE allowed to do it in this function
248 * Conclusion: glib must be broken on the Windows platform if sipe
249 * crashes with SIGTRAP when closing. You'll have to live
250 * with the memory leak until this is fixed.
254 g_free(buddy
->exchange_key
);
255 g_free(buddy
->change_key
);
256 g_free(buddy
->activity
);
257 g_free(buddy
->meeting_subject
);
258 g_free(buddy
->meeting_location
);
261 g_free(buddy
->cal_start_time
);
262 g_free(buddy
->cal_free_busy_base64
);
263 g_free(buddy
->cal_free_busy
);
264 g_free(buddy
->last_non_cal_activity
);
266 sipe_cal_free_working_hours(buddy
->cal_working_hours
);
268 g_free(buddy
->device_name
);
269 g_slist_free(buddy
->groups
);
273 static gboolean
buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key
,
275 SIPE_UNUSED_PARAMETER gpointer user_data
)
278 /* We must return TRUE as the key/value have already been deleted */
282 void sipe_buddy_free(struct sipe_core_private
*sipe_private
)
284 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
286 g_hash_table_foreach_steal(buddies
->uri
,
290 /* core is being deallocated, remove all its pending photo requests */
291 while (buddies
->pending_photo_requests
) {
292 struct photo_response_data
*data
=
293 buddies
->pending_photo_requests
->data
;
294 buddies
->pending_photo_requests
=
295 g_slist_remove(buddies
->pending_photo_requests
, data
);
296 photo_response_data_free(data
);
299 g_hash_table_destroy(buddies
->uri
);
300 g_hash_table_destroy(buddies
->exchange_key
);
302 sipe_private
->buddies
= NULL
;
305 gchar
*sipe_core_buddy_status(struct sipe_core_public
*sipe_public
,
308 const gchar
*status_text
)
310 struct sipe_buddy
*sbuddy
;
311 const char *activity_str
;
313 if (!sipe_public
) return NULL
; /* happens on pidgin exit */
315 sbuddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
, uri
);
316 if (!sbuddy
) return NULL
;
318 activity_str
= sbuddy
->activity
? sbuddy
->activity
:
319 (activity
== SIPE_ACTIVITY_BUSY
) || (activity
== SIPE_ACTIVITY_BRB
) ?
322 if (activity_str
&& sbuddy
->note
) {
323 return g_strdup_printf("%s - <i>%s</i>", activity_str
, sbuddy
->note
);
324 } else if (activity_str
) {
325 return g_strdup(activity_str
);
326 } else if (sbuddy
->note
) {
327 return g_strdup_printf("<i>%s</i>", sbuddy
->note
);
333 gchar
*sipe_buddy_get_alias(struct sipe_core_private
*sipe_private
,
336 sipe_backend_buddy pbuddy
;
338 if ((pbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, with
, NULL
))) {
339 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, pbuddy
);
344 void sipe_core_buddy_group(struct sipe_core_public
*sipe_public
,
346 const gchar
*old_group_name
,
347 const gchar
*new_group_name
)
349 struct sipe_buddy
* buddy
= sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE
,
351 struct sipe_group
* old_group
= NULL
;
352 struct sipe_group
* new_group
;
354 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
355 who
? who
: "", old_group_name
? old_group_name
: "", new_group_name
? new_group_name
: "");
357 if(!buddy
) { // buddy not in roaming list
361 if (old_group_name
) {
362 old_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, old_group_name
);
364 new_group
= sipe_group_find_by_name(SIPE_CORE_PRIVATE
, new_group_name
);
367 buddy
->groups
= g_slist_remove(buddy
->groups
, old_group
);
368 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who
, old_group_name
);
372 sipe_group_create(SIPE_CORE_PRIVATE
, new_group_name
, who
);
374 buddy
->groups
= sipe_utils_slist_insert_unique_sorted(buddy
->groups
,
376 (GCompareFunc
)sipe_group_compare
,
378 sipe_group_update_buddy(SIPE_CORE_PRIVATE
, buddy
);
382 void sipe_core_buddy_add(struct sipe_core_public
*sipe_public
,
384 const gchar
*group_name
)
386 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
388 if (!sipe_buddy_find_by_uri(sipe_private
, uri
)) {
389 struct sipe_buddy
*b
= sipe_buddy_add(sipe_private
,
393 b
->just_added
= TRUE
;
395 sipe_subscribe_presence_single_cb(sipe_private
, b
->name
);
398 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
402 sipe_core_buddy_group(sipe_public
,
408 void sipe_buddy_remove(struct sipe_core_private
*sipe_private
,
409 struct sipe_buddy
*buddy
)
411 struct sipe_buddies
*buddies
= sipe_private
->buddies
;
412 gchar
*action_name
= sipe_utils_presence_key(buddy
->name
);
413 sipe_schedule_cancel(sipe_private
, action_name
);
416 g_hash_table_remove(buddies
->uri
, buddy
->name
);
417 if (buddy
->exchange_key
)
418 g_hash_table_remove(buddies
->exchange_key
,
419 buddy
->exchange_key
);
425 * Unassociates buddy from group first.
426 * Then see if no groups left, removes buddy completely.
427 * Otherwise updates buddy groups on server.
429 void sipe_core_buddy_remove(struct sipe_core_public
*sipe_public
,
431 const gchar
*group_name
)
433 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
434 struct sipe_buddy
*b
= sipe_buddy_find_by_uri(sipe_private
,
440 struct sipe_group
*g
= sipe_group_find_by_name(sipe_private
,
443 b
->groups
= g_slist_remove(b
->groups
, g
);
444 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
449 if (g_slist_length(b
->groups
) < 1) {
450 gchar
*request
= g_strdup_printf("<m:URI>%s</m:URI>",
452 sip_soap_request(sipe_private
,
456 sipe_buddy_remove(sipe_private
, b
);
458 /* updates groups on server */
459 sipe_group_update_buddy(sipe_private
, b
);
464 void sipe_core_buddy_got_status(struct sipe_core_public
*sipe_public
,
468 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
469 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
474 /* Check if on 2005 system contact's calendar,
475 * then set/preserve it.
477 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
478 sipe_backend_buddy_set_status(sipe_public
, uri
, activity
);
480 sipe_ocs2005_apply_calendar_status(sipe_private
,
482 sipe_status_activity_to_token(activity
));
486 void sipe_core_buddy_tooltip_info(struct sipe_core_public
*sipe_public
,
488 const gchar
*status_name
,
490 struct sipe_backend_buddy_tooltip
*tooltip
)
492 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
494 gboolean is_oof_note
= FALSE
;
495 const gchar
*activity
= NULL
;
496 gchar
*calendar
= NULL
;
497 const gchar
*meeting_subject
= NULL
;
498 const gchar
*meeting_location
= NULL
;
499 gchar
*access_text
= NULL
;
501 #define SIPE_ADD_BUDDY_INFO(l, t) \
503 gchar *tmp = g_markup_escape_text((t), -1); \
504 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
507 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
508 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
510 if (sipe_public
) { /* happens on pidgin exit */
511 struct sipe_buddy
*sbuddy
= sipe_buddy_find_by_uri(sipe_private
,
515 is_oof_note
= sbuddy
->is_oof_note
;
516 activity
= sbuddy
->activity
;
517 calendar
= sipe_cal_get_description(sbuddy
);
518 meeting_subject
= sbuddy
->meeting_subject
;
519 meeting_location
= sbuddy
->meeting_location
;
521 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
522 gboolean is_group_access
= FALSE
;
523 const int container_id
= sipe_ocs2007_find_access_level(sipe_private
,
525 sipe_get_no_sip_uri(uri
),
527 const char *access_level
= sipe_ocs2007_access_level_name(container_id
);
528 access_text
= is_group_access
?
529 g_strdup(access_level
) :
530 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT
,
536 const gchar
*status_str
= activity
? activity
: status_name
;
538 SIPE_ADD_BUDDY_INFO(_("Status"), status_str
);
540 if (is_online
&& !is_empty(calendar
)) {
541 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar
);
544 if (!is_empty(meeting_location
)) {
545 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri
, meeting_location
);
546 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location
);
548 if (!is_empty(meeting_subject
)) {
549 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri
, meeting_subject
);
550 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject
);
553 gchar
*note_italics
= g_strdup_printf("<i>%s</i>", note
);
554 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri
, note
);
555 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note
? _("Out of office note") : _("Note"),
557 g_free(note_italics
);
560 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text
);
565 void sipe_buddy_update_property(struct sipe_core_private
*sipe_private
,
567 sipe_buddy_info_fields propkey
,
568 char *property_value
)
570 GSList
*buddies
, *entry
;
573 property_value
= g_strstrip(property_value
);
575 entry
= buddies
= sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC
, uri
, NULL
); /* all buddies in different groups */
578 sipe_backend_buddy p_buddy
= entry
->data
;
580 /* for Display Name */
581 if (propkey
== SIPE_BUDDY_INFO_DISPLAY_NAME
) {
583 alias
= sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC
, p_buddy
);
584 if (property_value
&& sipe_is_bad_alias(uri
, alias
)) {
585 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri
, property_value
);
586 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
590 alias
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
, p_buddy
);
591 if (!is_empty(property_value
) &&
592 (!sipe_strequal(property_value
, alias
) || is_empty(alias
)) )
594 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri
, property_value
);
595 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC
, p_buddy
, property_value
);
599 /* for other properties */
601 if (!is_empty(property_value
)) {
602 prop_str
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
);
603 if (!prop_str
|| !sipe_strcase_equal(prop_str
, property_value
)) {
604 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC
, p_buddy
, propkey
, property_value
);
612 g_slist_free(buddies
);
621 sipe_svc_callback
*callback
;
622 struct sipe_svc_session
*session
;
623 gchar
*wsse_security
;
624 struct sipe_backend_search_token
*token
;
625 /* must call ms_dlx_free() */
626 void (*failed_callback
)(struct sipe_core_private
*sipe_private
,
627 struct ms_dlx_data
*mdd
);
630 static void ms_dlx_free(struct ms_dlx_data
*mdd
)
632 sipe_utils_slist_free_full(mdd
->search_rows
, g_free
);
633 sipe_svc_session_close(mdd
->session
);
635 g_free(mdd
->wsse_security
);
639 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
640 #define DLX_SEARCH_ITEM \
641 "<AbEntryRequest.ChangeSearchQuery>" \
642 " <SearchOn>%s</SearchOn>" \
643 " <Value>%s</Value>" \
644 "</AbEntryRequest.ChangeSearchQuery>"
646 static gchar
* prepare_buddy_search_query(GSList
*query_rows
, gboolean use_dlx
) {
647 gchar
**attrs
= g_new(gchar
*, (g_slist_length(query_rows
) / 2) + 1);
655 attr
= query_rows
->data
;
656 query_rows
= g_slist_next(query_rows
);
657 value
= query_rows
->data
;
658 query_rows
= g_slist_next(query_rows
);
663 attrs
[i
++] = g_markup_printf_escaped(use_dlx
? DLX_SEARCH_ITEM
: SIPE_SOAP_SEARCH_ROW
,
669 query
= g_strjoinv(NULL
, attrs
);
670 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
679 static void ms_dlx_webticket(struct sipe_core_private
*sipe_private
,
680 const gchar
*base_uri
,
681 const gchar
*auth_uri
,
682 const gchar
*wsse_security
,
683 SIPE_UNUSED_PARAMETER
const gchar
*failure_msg
,
684 gpointer callback_data
)
686 struct ms_dlx_data
*mdd
= callback_data
;
689 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, TRUE
);
691 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
694 if (sipe_svc_ab_entry_request(sipe_private
,
699 g_slist_length(mdd
->search_rows
) / 2,
704 /* keep webticket security token for potential further use */
705 mdd
->wsse_security
= g_strdup(wsse_security
);
707 /* callback data passed down the line */
713 /* no ticket: this will show the minmum information */
714 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
719 mdd
->failed_callback(sipe_private
, mdd
);
722 static void ms_dlx_webticket_request(struct sipe_core_private
*sipe_private
,
723 struct ms_dlx_data
*mdd
)
725 if (!sipe_webticket_request(sipe_private
,
727 sipe_private
->dlx_uri
,
728 "AddressBookWebTicketBearer",
731 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
732 sipe_private
->dlx_uri
);
733 mdd
->failed_callback(sipe_private
, mdd
);
737 static void search_contacts_finalize(struct sipe_core_private
*sipe_private
,
738 struct sipe_backend_search_results
*results
,
742 gchar
*secondary
= g_strdup_printf(
743 dngettext(PACKAGE_NAME
,
744 "Found %d contact%s:",
745 "Found %d contacts%s:", match_count
),
746 match_count
, more
? _(" (more matched your query)") : "");
748 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC
,
755 static void search_ab_entry_response(struct sipe_core_private
*sipe_private
,
757 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
759 gpointer callback_data
)
761 struct ms_dlx_data
*mdd
= callback_data
;
764 const sipe_xml
*node
;
765 struct sipe_backend_search_results
*results
;
768 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
772 node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
774 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
775 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
777 _("No contacts found"));
782 /* OK, we found something - show the results to the user */
783 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
786 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
787 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
789 _("Unable to display the search results"));
794 /* SearchAbEntryResult can contain duplicates */
795 found
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
798 for (/* initialized above */ ; node
; node
= sipe_xml_twin(node
)) {
799 const sipe_xml
*attrs
;
800 gchar
*sip_uri
= NULL
;
801 gchar
*displayname
= NULL
;
802 gchar
*company
= NULL
;
803 gchar
*country
= NULL
;
806 for (attrs
= sipe_xml_child(node
, "Attributes/Attribute");
808 attrs
= sipe_xml_twin(attrs
)) {
809 gchar
*name
= sipe_xml_data(sipe_xml_child(attrs
,
811 gchar
*value
= sipe_xml_data(sipe_xml_child(attrs
,
814 if (!is_empty(value
)) {
815 if (sipe_strcase_equal(name
, "msrtcsip-primaryuseraddress")) {
819 } else if (sipe_strcase_equal(name
, "displayname")) {
823 } else if (sipe_strcase_equal(name
, "mail")) {
827 } else if (sipe_strcase_equal(name
, "company")) {
831 } else if (sipe_strcase_equal(name
, "country")) {
842 if (sip_uri
&& !g_hash_table_lookup(found
, sip_uri
)) {
843 gchar
**uri_parts
= g_strsplit(sip_uri
, ":", 2);
844 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
851 g_strfreev(uri_parts
);
853 g_hash_table_insert(found
, sip_uri
, (gpointer
) TRUE
);
864 search_contacts_finalize(sipe_private
, results
,
865 g_hash_table_size(found
),
867 g_hash_table_destroy(found
);
871 mdd
->failed_callback(sipe_private
, mdd
);
875 static gboolean
process_search_contact_response(struct sipe_core_private
*sipe_private
,
877 struct transaction
*trans
)
879 struct sipe_backend_search_token
*token
= trans
->payload
->data
;
880 struct sipe_backend_search_results
*results
;
881 sipe_xml
*searchResults
;
882 const sipe_xml
*mrow
;
883 guint match_count
= 0;
884 gboolean more
= FALSE
;
886 /* valid response? */
887 if (msg
->response
!= 200) {
888 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
890 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
892 _("Contact search failed"));
896 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg
->body
? msg
->body
: "");
899 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
900 if (!searchResults
) {
901 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
902 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
904 _("Contact search failed"));
909 mrow
= sipe_xml_child(searchResults
, "Body/Array/row");
911 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
912 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
914 _("No contacts found"));
916 sipe_xml_free(searchResults
);
920 /* OK, we found something - show the results to the user */
921 results
= sipe_backend_search_results_start(SIPE_CORE_PUBLIC
,
922 trans
->payload
->data
);
924 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
925 sipe_backend_search_failed(SIPE_CORE_PUBLIC
,
927 _("Unable to display the search results"));
929 sipe_xml_free(searchResults
);
933 for (/* initialized above */ ; mrow
; mrow
= sipe_xml_twin(mrow
)) {
934 gchar
**uri_parts
= g_strsplit(sipe_xml_attribute(mrow
, "uri"), ":", 2);
935 sipe_backend_search_results_add(SIPE_CORE_PUBLIC
,
938 sipe_xml_attribute(mrow
, "displayName"),
939 sipe_xml_attribute(mrow
, "company"),
940 sipe_xml_attribute(mrow
, "country"),
941 sipe_xml_attribute(mrow
, "email"));
942 g_strfreev(uri_parts
);
946 if ((mrow
= sipe_xml_child(searchResults
, "Body/directorySearch/moreAvailable")) != NULL
) {
947 char *data
= sipe_xml_data(mrow
);
948 more
= (g_ascii_strcasecmp(data
, "true") == 0);
952 search_contacts_finalize(sipe_private
, results
, match_count
, more
);
953 sipe_xml_free(searchResults
);
958 static void search_soap_request(struct sipe_core_private
*sipe_private
,
959 struct sipe_backend_search_token
*token
,
962 gchar
*query
= prepare_buddy_search_query(search_rows
, FALSE
);
963 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
965 payload
->data
= token
;
967 sip_soap_directory_search(sipe_private
,
970 process_search_contact_response
,
975 static void search_ab_entry_failed(struct sipe_core_private
*sipe_private
,
976 struct ms_dlx_data
*mdd
)
978 /* error using [MS-DLX] server, retry using Active Directory */
979 search_soap_request(sipe_private
, mdd
->token
, mdd
->search_rows
);
983 void sipe_core_buddy_search(struct sipe_core_public
*sipe_public
,
984 struct sipe_backend_search_token
*token
,
985 const gchar
*given_name
,
986 const gchar
*surname
,
988 const gchar
*company
,
989 const gchar
*country
)
991 GSList
*query_rows
= NULL
;
993 #define ADD_QUERY_ROW(attr, val) \
995 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
996 query_rows = g_slist_append(query_rows, g_strdup(val)); \
999 ADD_QUERY_ROW("givenName", given_name
);
1000 ADD_QUERY_ROW("sn", surname
);
1001 ADD_QUERY_ROW("mail", email
);
1002 ADD_QUERY_ROW("company", company
);
1003 ADD_QUERY_ROW("c", country
);
1006 if (SIPE_CORE_PRIVATE
->dlx_uri
!= NULL
) {
1007 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1009 mdd
->search_rows
= query_rows
;
1010 mdd
->max_returns
= 100;
1011 mdd
->callback
= search_ab_entry_response
;
1012 mdd
->failed_callback
= search_ab_entry_failed
;
1013 mdd
->session
= sipe_svc_session_start();
1016 ms_dlx_webticket_request(SIPE_CORE_PRIVATE
, mdd
);
1019 /* no [MS-DLX] server, use Active Directory search instead */
1020 search_soap_request(SIPE_CORE_PRIVATE
, token
, query_rows
);
1021 sipe_utils_slist_free_full(query_rows
, g_free
);
1024 sipe_backend_search_failed(sipe_public
,
1026 _("Invalid contact search query"));
1029 static void get_info_finalize(struct sipe_core_private
*sipe_private
,
1030 struct sipe_backend_buddy_info
*info
,
1032 const gchar
*server_alias
,
1035 sipe_backend_buddy bbuddy
;
1036 struct sipe_buddy
*sbuddy
;
1041 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1043 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC
, info
);
1048 bbuddy
= sipe_backend_buddy_find(SIPE_CORE_PUBLIC
, uri
, NULL
);
1050 if (is_empty(server_alias
)) {
1051 value
= sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC
,
1054 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1056 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1060 value
= g_strdup(server_alias
);
1063 /* present alias if it differs from server alias */
1064 alias
= sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC
, bbuddy
);
1065 if (alias
&& !sipe_strequal(alias
, value
))
1067 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1069 SIPE_BUDDY_INFO_ALIAS
,
1075 if (is_empty(email
)) {
1076 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1078 SIPE_BUDDY_INFO_EMAIL
);
1080 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1082 SIPE_BUDDY_INFO_EMAIL
,
1088 value
= sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC
,
1090 SIPE_BUDDY_INFO_SITE
);
1092 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1094 SIPE_BUDDY_INFO_SITE
,
1099 sbuddy
= sipe_buddy_find_by_uri(sipe_private
, uri
);
1100 if (sbuddy
&& sbuddy
->device_name
) {
1101 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1103 SIPE_BUDDY_INFO_DEVICE
,
1104 sbuddy
->device_name
);
1107 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC
, info
, uri
);
1111 static void get_info_ab_entry_response(struct sipe_core_private
*sipe_private
,
1113 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1114 sipe_xml
*soap_body
,
1115 gpointer callback_data
)
1117 struct ms_dlx_data
*mdd
= callback_data
;
1118 struct sipe_backend_buddy_info
*info
= NULL
;
1119 gchar
*server_alias
= NULL
;
1120 gchar
*email
= NULL
;
1123 const sipe_xml
*node
;
1125 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1128 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1130 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1132 node
= sipe_xml_twin(node
)) {
1133 gchar
*name
= sipe_xml_data(sipe_xml_child(node
,
1135 gchar
*value
= sipe_xml_data(sipe_xml_child(node
,
1137 const sipe_xml
*values
= sipe_xml_child(node
,
1140 /* Single value entries */
1141 if (!is_empty(value
)) {
1143 if (sipe_strcase_equal(name
, "displayname")) {
1144 g_free(server_alias
);
1145 server_alias
= value
;
1147 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1149 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1151 } else if (sipe_strcase_equal(name
, "mail")) {
1155 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1157 SIPE_BUDDY_INFO_EMAIL
,
1159 } else if (sipe_strcase_equal(name
, "title")) {
1160 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1162 SIPE_BUDDY_INFO_JOB_TITLE
,
1164 } else if (sipe_strcase_equal(name
, "company")) {
1165 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1167 SIPE_BUDDY_INFO_COMPANY
,
1169 } else if (sipe_strcase_equal(name
, "country")) {
1170 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1172 SIPE_BUDDY_INFO_COUNTRY
,
1176 } else if (values
) {
1177 gchar
*first
= sipe_xml_data(sipe_xml_child(values
,
1180 if (sipe_strcase_equal(name
, "telephonenumber")) {
1181 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1183 SIPE_BUDDY_INFO_WORK_PHONE
,
1195 /* this will show the minmum information */
1196 get_info_finalize(sipe_private
,
1203 g_free(server_alias
);
1207 static gboolean
process_get_info_response(struct sipe_core_private
*sipe_private
,
1209 struct transaction
*trans
)
1211 const gchar
*uri
= trans
->payload
->data
;
1212 struct sipe_backend_buddy_info
*info
= NULL
;
1213 gchar
*server_alias
= NULL
;
1214 gchar
*email
= NULL
;
1216 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1217 uri
, sipe_private
->username
);
1219 if (msg
->response
!= 200) {
1220 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg
->response
);
1222 sipe_xml
*searchResults
;
1223 const sipe_xml
*mrow
;
1225 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1226 msg
->body
? msg
->body
: "");
1228 searchResults
= sipe_xml_parse(msg
->body
, msg
->bodylen
);
1229 if (!searchResults
) {
1231 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1233 } else if ((mrow
= sipe_xml_child(searchResults
, "Body/Array/row"))) {
1235 gchar
*phone_number
;
1237 info
= sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC
);
1239 server_alias
= g_strdup(sipe_xml_attribute(mrow
, "displayName"));
1240 email
= g_strdup(sipe_xml_attribute(mrow
, "email"));
1241 phone_number
= g_strdup(sipe_xml_attribute(mrow
, "phone"));
1244 * For 2007 system we will take this from ContactCard -
1245 * it has cleaner tel: URIs at least
1247 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1248 char *tel_uri
= sip_to_tel_uri(phone_number
);
1249 /* trims its parameters, so call first */
1250 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_DISPLAY_NAME
, server_alias
);
1251 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_EMAIL
, email
);
1252 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE
, tel_uri
);
1253 sipe_buddy_update_property(sipe_private
, uri
, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
, phone_number
);
1256 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC
,
1260 if (!is_empty(server_alias
)) {
1261 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1263 SIPE_BUDDY_INFO_DISPLAY_NAME
,
1266 if ((value
= sipe_xml_attribute(mrow
, "title")) && strlen(value
) > 0) {
1267 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1269 SIPE_BUDDY_INFO_JOB_TITLE
,
1272 if ((value
= sipe_xml_attribute(mrow
, "office")) && strlen(value
) > 0) {
1273 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1275 SIPE_BUDDY_INFO_OFFICE
,
1278 if (!is_empty(phone_number
)) {
1279 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1281 SIPE_BUDDY_INFO_WORK_PHONE
,
1284 g_free(phone_number
);
1285 if ((value
= sipe_xml_attribute(mrow
, "company")) && strlen(value
) > 0) {
1286 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1288 SIPE_BUDDY_INFO_COMPANY
,
1291 if ((value
= sipe_xml_attribute(mrow
, "city")) && strlen(value
) > 0) {
1292 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1294 SIPE_BUDDY_INFO_CITY
,
1297 if ((value
= sipe_xml_attribute(mrow
, "state")) && strlen(value
) > 0) {
1298 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1300 SIPE_BUDDY_INFO_STATE
,
1303 if ((value
= sipe_xml_attribute(mrow
, "country")) && strlen(value
) > 0) {
1304 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1306 SIPE_BUDDY_INFO_COUNTRY
,
1309 if (!is_empty(email
)) {
1310 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC
,
1312 SIPE_BUDDY_INFO_EMAIL
,
1316 sipe_xml_free(searchResults
);
1319 /* this will show the minmum information */
1320 get_info_finalize(sipe_private
,
1326 g_free(server_alias
);
1332 static void get_info_ab_entry_failed(struct sipe_core_private
*sipe_private
,
1333 struct ms_dlx_data
*mdd
)
1335 /* error using [MS-DLX] server, retry using Active Directory */
1336 gchar
*query
= prepare_buddy_search_query(mdd
->search_rows
, FALSE
);
1337 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1339 payload
->destroy
= g_free
;
1340 payload
->data
= mdd
->other
;
1343 sip_soap_directory_search(sipe_private
,
1346 process_get_info_response
,
1353 void sipe_core_buddy_get_info(struct sipe_core_public
*sipe_public
,
1356 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1358 if (sipe_private
->dlx_uri
) {
1359 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1361 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1362 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(who
));
1364 mdd
->other
= g_strdup(who
);
1365 mdd
->max_returns
= 1;
1366 mdd
->callback
= get_info_ab_entry_response
;
1367 mdd
->failed_callback
= get_info_ab_entry_failed
;
1368 mdd
->session
= sipe_svc_session_start();
1370 ms_dlx_webticket_request(sipe_private
, mdd
);
1373 /* no [MS-DLX] server, use Active Directory search instead */
1374 gchar
*row
= g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW
,
1375 "msRTCSIP-PrimaryUserAddress",
1377 struct transaction_payload
*payload
= g_new0(struct transaction_payload
, 1);
1379 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1382 payload
->destroy
= g_free
;
1383 payload
->data
= g_strdup(who
);
1385 sip_soap_directory_search(sipe_private
,
1388 process_get_info_response
,
1394 static void photo_response_data_free(struct photo_response_data
*data
)
1397 g_free(data
->photo_hash
);
1398 if (data
->request
) {
1399 sipe_http_request_cancel(data
->request
);
1404 static void process_buddy_photo_response(struct sipe_core_private
*sipe_private
,
1410 struct photo_response_data
*rdata
= (struct photo_response_data
*) data
;
1412 rdata
->request
= NULL
;
1414 if (status
== SIPE_HTTP_STATUS_OK
) {
1415 const gchar
*len_str
= sipe_utils_nameval_find(headers
,
1418 gsize photo_size
= atoi(len_str
);
1419 gpointer photo
= g_new(char, photo_size
);
1422 memcpy(photo
, body
, photo_size
);
1424 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC
,
1433 sipe_private
->buddies
->pending_photo_requests
=
1434 g_slist_remove(sipe_private
->buddies
->pending_photo_requests
, rdata
);
1436 photo_response_data_free(rdata
);
1439 static gchar
*create_x_ms_webticket_header(const gchar
*wsse_security
)
1441 gchar
*assertion
= sipe_xml_extract_raw(wsse_security
, "saml:Assertion", TRUE
);
1442 gchar
*wsse_security_base64
;
1443 gchar
*x_ms_webticket_header
;
1449 wsse_security_base64
= g_base64_encode((const guchar
*)assertion
,
1451 x_ms_webticket_header
= g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1452 wsse_security_base64
);
1455 g_free(wsse_security_base64
);
1457 return x_ms_webticket_header
;
1460 static void get_photo_ab_entry_response(struct sipe_core_private
*sipe_private
,
1462 SIPE_UNUSED_PARAMETER
const gchar
*raw
,
1463 sipe_xml
*soap_body
,
1464 gpointer callback_data
)
1466 struct ms_dlx_data
*mdd
= callback_data
;
1467 gchar
*photo_rel_path
= NULL
;
1468 gchar
*photo_hash
= NULL
;
1469 const gchar
*photo_hash_old
=
1470 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC
, mdd
->other
);
1473 const sipe_xml
*node
;
1475 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1478 for (node
= sipe_xml_child(soap_body
, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1480 node
= sipe_xml_twin(node
)) {
1481 gchar
*name
= sipe_xml_data(sipe_xml_child(node
, "Name"));
1482 gchar
*value
= sipe_xml_data(sipe_xml_child(node
, "Value"));
1484 if (!is_empty(value
)) {
1485 if (sipe_strcase_equal(name
, "PhotoRelPath")) {
1486 g_free(photo_rel_path
);
1487 photo_rel_path
= value
;
1489 } else if (sipe_strcase_equal(name
, "PhotoHash")) {
1501 if (sipe_private
->addressbook_uri
&& photo_rel_path
&&
1502 photo_hash
&& !sipe_strequal(photo_hash
, photo_hash_old
)) {
1503 gchar
*photo_url
= g_strdup_printf("%s/%s",
1504 sipe_private
->addressbook_uri
, photo_rel_path
);
1505 gchar
*x_ms_webticket_header
= create_x_ms_webticket_header(mdd
->wsse_security
);
1507 struct photo_response_data
*data
= g_new(struct photo_response_data
, 1);
1508 data
->who
= g_strdup(mdd
->other
);
1509 data
->photo_hash
= photo_hash
;
1512 data
->request
= sipe_http_request_get(sipe_private
,
1514 x_ms_webticket_header
,
1515 process_buddy_photo_response
,
1518 if (data
->request
) {
1519 sipe_private
->buddies
->pending_photo_requests
=
1520 g_slist_append(sipe_private
->buddies
->pending_photo_requests
, data
);
1521 sipe_http_request_ready(data
->request
);
1523 photo_response_data_free(data
);
1526 g_free(x_ms_webticket_header
);
1530 g_free(photo_rel_path
);
1535 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER
struct sipe_core_private
*sipe_private
,
1536 struct ms_dlx_data
*mdd
)
1541 static void buddy_fetch_photo(struct sipe_core_private
*sipe_private
,
1544 if (sipe_backend_uses_photo()) {
1546 /* Lync 2013 or newer: use UCS */
1547 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013
)) {
1549 sipe_ucs_get_photo(sipe_private
, uri
);
1551 /* Lync 2010: use [MS-DLX] */
1552 } else if (sipe_private
->dlx_uri
&&
1553 sipe_private
->addressbook_uri
) {
1554 struct ms_dlx_data
*mdd
= g_new0(struct ms_dlx_data
, 1);
1556 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup("msRTCSIP-PrimaryUserAddress"));
1557 mdd
->search_rows
= g_slist_append(mdd
->search_rows
, g_strdup(uri
));
1559 mdd
->other
= g_strdup(uri
);
1560 mdd
->max_returns
= 1;
1561 mdd
->callback
= get_photo_ab_entry_response
;
1562 mdd
->failed_callback
= get_photo_ab_entry_failed
;
1563 mdd
->session
= sipe_svc_session_start();
1565 ms_dlx_webticket_request(sipe_private
, mdd
);
1570 static void buddy_refresh_photos_cb(gpointer uri
,
1571 SIPE_UNUSED_PARAMETER gpointer value
,
1572 gpointer sipe_private
)
1574 buddy_fetch_photo(sipe_private
, uri
);
1577 void sipe_buddy_refresh_photos(struct sipe_core_private
*sipe_private
)
1579 g_hash_table_foreach(sipe_private
->buddies
->uri
,
1580 buddy_refresh_photos_cb
,
1584 /* Buddy menu callbacks*/
1586 void sipe_core_buddy_new_chat(struct sipe_core_public
*sipe_public
,
1589 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1591 /* 2007+ conference */
1592 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
)) {
1593 sipe_conf_add(sipe_private
, who
);
1595 /* 2005- multiparty chat */
1597 gchar
*self
= sip_uri_self(sipe_private
);
1598 struct sip_session
*session
;
1600 session
= sipe_session_add_chat(sipe_private
,
1604 session
->chat_session
->backend
= sipe_backend_chat_create(SIPE_CORE_PUBLIC
,
1605 session
->chat_session
,
1606 session
->chat_session
->title
,
1610 sipe_im_invite(sipe_private
, session
, who
,
1611 NULL
, NULL
, NULL
, FALSE
);
1615 void sipe_core_buddy_send_email(struct sipe_core_public
*sipe_public
,
1618 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1621 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1623 SIPE_BUDDY_INFO_EMAIL
);
1626 gchar
*command_line
= g_strdup_printf(
1632 " mailto:%s", email
);
1635 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1637 g_spawn_command_line_async(command_line
, NULL
);
1638 g_free(command_line
);
1641 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1648 static struct sipe_backend_buddy_menu
*buddy_menu_phone(struct sipe_core_public
*sipe_public
,
1649 struct sipe_backend_buddy_menu
*menu
,
1650 sipe_backend_buddy buddy
,
1651 sipe_buddy_info_fields id_phone
,
1652 sipe_buddy_info_fields id_display
,
1655 gchar
*phone
= sipe_backend_buddy_get_string(sipe_public
,
1659 gchar
*display
= sipe_backend_buddy_get_string(sipe_public
,
1663 gchar
*label
= g_strdup_printf("%s %s",
1666 (tmp
= sip_tel_uri_denormalize(phone
)));
1667 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1670 SIPE_BUDDY_MENU_MAKE_CALL
,
1681 struct sipe_backend_buddy_menu
*sipe_core_buddy_create_menu(struct sipe_core_public
*sipe_public
,
1682 const gchar
*buddy_name
,
1683 struct sipe_backend_buddy_menu
*menu
)
1685 struct sipe_core_private
*sipe_private
= SIPE_CORE_PRIVATE
;
1686 sipe_backend_buddy buddy
= sipe_backend_buddy_find(sipe_public
,
1689 gchar
*self
= sip_uri_self(sipe_private
);
1691 SIPE_SESSION_FOREACH
{
1692 if (!sipe_strcase_equal(self
, buddy_name
) && session
->chat_session
)
1694 struct sipe_chat_session
*chat_session
= session
->chat_session
;
1695 gboolean is_conf
= (chat_session
->type
== SIPE_CHAT_TYPE_CONFERENCE
);
1697 if (sipe_backend_chat_find(chat_session
->backend
, buddy_name
))
1699 gboolean conf_op
= sipe_backend_chat_is_operator(chat_session
->backend
, self
);
1703 !sipe_backend_chat_is_operator(chat_session
->backend
, buddy_name
) &&
1704 /* We are a conf OP */
1706 gchar
*label
= g_strdup_printf(_("Make leader of '%s'"),
1707 chat_session
->title
);
1708 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1711 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER
,
1717 /* We are a conf OP */
1719 gchar
*label
= g_strdup_printf(_("Remove from '%s'"),
1720 chat_session
->title
);
1721 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1724 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT
,
1732 (is_conf
&& !session
->locked
)) {
1733 gchar
*label
= g_strdup_printf(_("Invite to '%s'"),
1734 chat_session
->title
);
1735 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1738 SIPE_BUDDY_MENU_INVITE_TO_CHAT
,
1744 } SIPE_SESSION_FOREACH_END
;
1747 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1750 SIPE_BUDDY_MENU_NEW_CHAT
,
1753 /* add buddy's phone numbers if we have call control */
1754 if (sip_csta_is_idle(sipe_private
)) {
1757 menu
= buddy_menu_phone(sipe_public
,
1760 SIPE_BUDDY_INFO_WORK_PHONE
,
1761 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY
,
1764 menu
= buddy_menu_phone(sipe_public
,
1767 SIPE_BUDDY_INFO_MOBILE_PHONE
,
1768 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY
,
1772 menu
= buddy_menu_phone(sipe_public
,
1775 SIPE_BUDDY_INFO_HOME_PHONE
,
1776 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY
,
1780 menu
= buddy_menu_phone(sipe_public
,
1783 SIPE_BUDDY_INFO_OTHER_PHONE
,
1784 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY
,
1788 menu
= buddy_menu_phone(sipe_public
,
1791 SIPE_BUDDY_INFO_CUSTOM1_PHONE
,
1792 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY
,
1797 gchar
*email
= sipe_backend_buddy_get_string(sipe_public
,
1799 SIPE_BUDDY_INFO_EMAIL
);
1801 menu
= sipe_backend_buddy_menu_add(sipe_public
,
1804 SIPE_BUDDY_MENU_SEND_EMAIL
,
1810 /* access level control */
1811 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007
))
1812 menu
= sipe_backend_buddy_sub_menu_add(sipe_public
,
1815 sipe_ocs2007_access_control_menu(sipe_private
,
1821 guint
sipe_buddy_count(struct sipe_core_private
*sipe_private
)
1823 return(g_hash_table_size(sipe_private
->buddies
->uri
));
1826 static guint
sipe_ht_hash_nick(const char *nick
)
1828 char *lc
= g_utf8_strdown(nick
, -1);
1829 guint bucket
= g_str_hash(lc
);
1835 static gboolean
sipe_ht_equals_nick(const char *nick1
, const char *nick2
)
1837 char *nick1_norm
= NULL
;
1838 char *nick2_norm
= NULL
;
1841 if (nick1
== NULL
&& nick2
== NULL
) return TRUE
;
1842 if (nick1
== NULL
|| nick2
== NULL
||
1843 !g_utf8_validate(nick1
, -1, NULL
) ||
1844 !g_utf8_validate(nick2
, -1, NULL
)) return FALSE
;
1846 nick1_norm
= g_utf8_casefold(nick1
, -1);
1847 nick2_norm
= g_utf8_casefold(nick2
, -1);
1848 equal
= g_utf8_collate(nick1_norm
, nick2_norm
) == 0;
1855 void sipe_buddy_init(struct sipe_core_private
*sipe_private
)
1857 struct sipe_buddies
*buddies
= g_new0(struct sipe_buddies
, 1);
1858 buddies
->uri
= g_hash_table_new((GHashFunc
) sipe_ht_hash_nick
,
1859 (GEqualFunc
) sipe_ht_equals_nick
);
1860 buddies
->exchange_key
= g_hash_table_new(g_str_hash
,
1862 sipe_private
->buddies
= buddies
;