buddy: add interface to lookup buddy by URI
[siplcs.git] / src / core / sipe-buddy.c
blobc0dfe994bab7efc5ccf71570c4e9c7c0f1713626
1 /**
2 * @file sipe-buddy.c
4 * pidgin-sipe
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
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
31 #include <glib.h>
33 #include "sipe-common.h"
34 #include "sipmsg.h"
35 #include "sip-csta.h"
36 #include "sip-soap.h"
37 #include "sip-transport.h"
38 #include "sipe-backend.h"
39 #include "sipe-buddy.h"
40 #include "sipe-cal.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"
47 #include "sipe-im.h"
48 #include "sipe-nls.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"
55 #include "sipe-svc.h"
56 #include "sipe-ucs.h"
57 #include "sipe-utils.h"
58 #include "sipe-webticket.h"
59 #include "sipe-xml.h"
61 struct photo_response_data {
62 gchar *who;
63 gchar *photo_hash;
64 struct sipe_http_request *request;
67 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
68 const gchar *uri);
69 static void photo_response_data_free(struct photo_response_data *data);
71 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
72 const gchar *uri,
73 const gchar *exchange_key)
75 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private, uri);
76 if (!buddy) {
77 buddy = g_new0(struct sipe_buddy, 1);
78 buddy->name = g_strdup(uri);
79 buddy->exchange_key = g_strdup(exchange_key);
80 g_hash_table_insert(sipe_private->buddies, buddy->name, buddy);
82 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", uri);
84 buddy_fetch_photo(sipe_private, uri);
85 } else {
86 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", uri);
89 return buddy;
92 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
93 const gchar *uri)
95 return(g_hash_table_lookup(sipe_private->buddies, uri));
98 static void buddy_free(struct sipe_buddy *buddy)
100 #ifndef _WIN32
102 * We are calling g_hash_table_foreach_steal(). That means that no
103 * key/value deallocation functions are called. Therefore the glib
104 * hash code does not touch the key (buddy->name) or value (buddy)
105 * of the to-be-deleted hash node at all. It follows that we
107 * - MUST free the memory for the key ourselves and
108 * - ARE allowed to do it in this function
110 * Conclusion: glib must be broken on the Windows platform if sipe
111 * crashes with SIGTRAP when closing. You'll have to live
112 * with the memory leak until this is fixed.
114 g_free(buddy->name);
115 #endif
116 g_free(buddy->exchange_key);
117 g_free(buddy->activity);
118 g_free(buddy->meeting_subject);
119 g_free(buddy->meeting_location);
120 g_free(buddy->note);
122 g_free(buddy->cal_start_time);
123 g_free(buddy->cal_free_busy_base64);
124 g_free(buddy->cal_free_busy);
125 g_free(buddy->last_non_cal_activity);
127 sipe_cal_free_working_hours(buddy->cal_working_hours);
129 g_free(buddy->device_name);
130 g_slist_free(buddy->groups);
131 g_free(buddy);
134 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
135 gpointer buddy,
136 SIPE_UNUSED_PARAMETER gpointer user_data)
138 buddy_free(buddy);
139 /* We must return TRUE as the key/value have already been deleted */
140 return(TRUE);
143 void sipe_buddy_free(struct sipe_core_private *sipe_private)
145 g_hash_table_foreach_steal(sipe_private->buddies,
146 buddy_free_cb,
147 NULL);
149 /* core is being deallocated, remove all its pending photo requests */
150 while (sipe_private->pending_photo_requests) {
151 struct photo_response_data *data =
152 sipe_private->pending_photo_requests->data;
153 sipe_private->pending_photo_requests =
154 g_slist_remove(sipe_private->pending_photo_requests, data);
155 photo_response_data_free(data);
158 g_hash_table_destroy(sipe_private->buddies);
159 sipe_private->buddies = NULL;
162 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
163 const gchar *uri,
164 guint activity,
165 const gchar *status_text)
167 struct sipe_buddy *sbuddy;
168 const char *activity_str;
170 if (!sipe_public) return NULL; /* happens on pidgin exit */
172 sbuddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, uri);
173 if (!sbuddy) return NULL;
175 activity_str = sbuddy->activity ? sbuddy->activity :
176 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
177 status_text : NULL;
179 if (activity_str && sbuddy->note) {
180 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
181 } else if (activity_str) {
182 return g_strdup(activity_str);
183 } else if (sbuddy->note) {
184 return g_strdup_printf("<i>%s</i>", sbuddy->note);
185 } else {
186 return NULL;
190 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
191 const gchar *with)
193 sipe_backend_buddy pbuddy;
194 gchar *alias = NULL;
195 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
196 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
198 return alias;
201 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
202 const gchar *who,
203 const gchar *old_group_name,
204 const gchar *new_group_name)
206 struct sipe_buddy * buddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, who);
207 struct sipe_group * old_group = NULL;
208 struct sipe_group * new_group;
210 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
211 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
213 if(!buddy) { // buddy not in roaming list
214 return;
217 if (old_group_name) {
218 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
220 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
222 if (old_group) {
223 buddy->groups = g_slist_remove(buddy->groups, old_group);
224 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
227 if (!new_group) {
228 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
229 } else {
230 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
231 new_group,
232 (GCompareFunc)sipe_group_compare,
233 NULL);
234 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
238 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
239 const gchar *uri,
240 const gchar *group_name)
242 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
244 if (!sipe_buddy_find_by_uri(sipe_private, uri)) {
245 struct sipe_buddy *b = sipe_buddy_add(sipe_private, uri, NULL);
246 b->just_added = TRUE;
248 sipe_subscribe_presence_single_cb(sipe_private, b->name);
250 } else {
251 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
252 uri);
255 sipe_core_buddy_group(sipe_public,
256 uri,
257 NULL,
258 group_name);
261 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
262 struct sipe_buddy *buddy)
264 gchar *action_name = sipe_utils_presence_key(buddy->name);
265 sipe_schedule_cancel(sipe_private, action_name);
266 g_free(action_name);
268 g_hash_table_remove(sipe_private->buddies, buddy->name);
270 buddy_free(buddy);
274 * Unassociates buddy from group first.
275 * Then see if no groups left, removes buddy completely.
276 * Otherwise updates buddy groups on server.
278 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
279 const gchar *uri,
280 const gchar *group_name)
282 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
283 struct sipe_buddy *b = sipe_buddy_find_by_uri(sipe_private,
284 uri);
286 if (!b) return;
288 if (group_name) {
289 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
290 group_name);
291 if (g) {
292 b->groups = g_slist_remove(b->groups, g);
293 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
294 uri, g->name);
298 if (g_slist_length(b->groups) < 1) {
299 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
300 b->name);
301 sip_soap_request(sipe_private,
302 "deleteContact",
303 request);
304 g_free(request);
305 sipe_buddy_remove(sipe_private, b);
306 } else {
307 /* updates groups on server */
308 sipe_group_update_buddy(sipe_private, b);
313 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
314 const gchar *uri,
315 guint activity)
317 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
318 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
319 uri);
321 if (!sbuddy) return;
323 /* Check if on 2005 system contact's calendar,
324 * then set/preserve it.
326 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
327 sipe_backend_buddy_set_status(sipe_public, uri, activity);
328 } else {
329 sipe_ocs2005_apply_calendar_status(sipe_private,
330 sbuddy,
331 sipe_status_activity_to_token(activity));
335 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
336 const gchar *uri,
337 const gchar *status_name,
338 gboolean is_online,
339 struct sipe_backend_buddy_tooltip *tooltip)
341 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
342 gchar *note = NULL;
343 gboolean is_oof_note = FALSE;
344 const gchar *activity = NULL;
345 gchar *calendar = NULL;
346 const gchar *meeting_subject = NULL;
347 const gchar *meeting_location = NULL;
348 gchar *access_text = NULL;
350 #define SIPE_ADD_BUDDY_INFO(l, t) \
352 gchar *tmp = g_markup_escape_text((t), -1); \
353 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
354 g_free(tmp); \
356 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
357 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
359 if (sipe_public) { /* happens on pidgin exit */
360 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
361 uri);
362 if (sbuddy) {
363 note = sbuddy->note;
364 is_oof_note = sbuddy->is_oof_note;
365 activity = sbuddy->activity;
366 calendar = sipe_cal_get_description(sbuddy);
367 meeting_subject = sbuddy->meeting_subject;
368 meeting_location = sbuddy->meeting_location;
370 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
371 gboolean is_group_access = FALSE;
372 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
373 "user",
374 sipe_get_no_sip_uri(uri),
375 &is_group_access);
376 const char *access_level = sipe_ocs2007_access_level_name(container_id);
377 access_text = is_group_access ?
378 g_strdup(access_level) :
379 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
380 access_level);
384 if (is_online) {
385 const gchar *status_str = activity ? activity : status_name;
387 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
389 if (is_online && !is_empty(calendar)) {
390 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
392 g_free(calendar);
393 if (!is_empty(meeting_location)) {
394 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
395 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
397 if (!is_empty(meeting_subject)) {
398 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
399 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
401 if (note) {
402 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
403 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
404 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
405 note_italics);
406 g_free(note_italics);
408 if (access_text) {
409 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
410 g_free(access_text);
414 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
415 const char *uri,
416 sipe_buddy_info_fields propkey,
417 char *property_value)
419 GSList *buddies, *entry;
421 if (property_value)
422 property_value = g_strstrip(property_value);
424 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
425 while (entry) {
426 gchar *prop_str;
427 sipe_backend_buddy p_buddy = entry->data;
429 /* for Display Name */
430 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
431 gchar *alias;
432 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
433 if (property_value && sipe_is_bad_alias(uri, alias)) {
434 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
435 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
437 g_free(alias);
439 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
440 if (!is_empty(property_value) &&
441 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
443 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
444 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
446 g_free(alias);
448 /* for other properties */
449 else {
450 if (!is_empty(property_value)) {
451 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
452 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
453 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
455 g_free(prop_str);
459 entry = entry->next;
461 g_slist_free(buddies);
465 struct ms_dlx_data;
466 struct ms_dlx_data {
467 GSList *search_rows;
468 gchar *other;
469 guint max_returns;
470 sipe_svc_callback *callback;
471 struct sipe_svc_session *session;
472 gchar *wsse_security;
473 struct sipe_backend_search_token *token;
474 /* must call ms_dlx_free() */
475 void (*failed_callback)(struct sipe_core_private *sipe_private,
476 struct ms_dlx_data *mdd);
479 static void ms_dlx_free(struct ms_dlx_data *mdd)
481 sipe_utils_slist_free_full(mdd->search_rows, g_free);
482 sipe_svc_session_close(mdd->session);
483 g_free(mdd->other);
484 g_free(mdd->wsse_security);
485 g_free(mdd);
488 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
489 #define DLX_SEARCH_ITEM \
490 "<AbEntryRequest.ChangeSearchQuery>" \
491 " <SearchOn>%s</SearchOn>" \
492 " <Value>%s</Value>" \
493 "</AbEntryRequest.ChangeSearchQuery>"
495 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
496 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
497 guint i = 0;
498 gchar *query = NULL;
500 while (query_rows) {
501 gchar *attr;
502 gchar *value;
504 attr = query_rows->data;
505 query_rows = g_slist_next(query_rows);
506 value = query_rows->data;
507 query_rows = g_slist_next(query_rows);
509 if (!attr || !value)
510 break;
512 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
513 attr, value);
515 attrs[i] = NULL;
517 if (i) {
518 query = g_strjoinv(NULL, attrs);
519 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
520 query ? query : "");
523 g_strfreev(attrs);
525 return query;
528 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
529 const gchar *base_uri,
530 const gchar *auth_uri,
531 const gchar *wsse_security,
532 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
533 gpointer callback_data)
535 struct ms_dlx_data *mdd = callback_data;
537 if (wsse_security) {
538 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
540 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
541 base_uri);
543 if (sipe_svc_ab_entry_request(sipe_private,
544 mdd->session,
545 auth_uri,
546 wsse_security,
547 query,
548 g_slist_length(mdd->search_rows) / 2,
549 mdd->max_returns,
550 mdd->callback,
551 mdd)) {
553 /* keep webticket security token for potential further use */
554 mdd->wsse_security = g_strdup(wsse_security);
556 /* callback data passed down the line */
557 mdd = NULL;
559 g_free(query);
561 } else {
562 /* no ticket: this will show the minmum information */
563 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
564 base_uri);
567 if (mdd)
568 mdd->failed_callback(sipe_private, mdd);
571 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
572 struct ms_dlx_data *mdd)
574 if (!sipe_webticket_request(sipe_private,
575 mdd->session,
576 sipe_private->dlx_uri,
577 "AddressBookWebTicketBearer",
578 ms_dlx_webticket,
579 mdd)) {
580 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
581 sipe_private->dlx_uri);
582 mdd->failed_callback(sipe_private, mdd);
586 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
587 struct sipe_backend_search_results *results,
588 guint match_count,
589 gboolean more)
591 gchar *secondary = g_strdup_printf(
592 dngettext(PACKAGE_NAME,
593 "Found %d contact%s:",
594 "Found %d contacts%s:", match_count),
595 match_count, more ? _(" (more matched your query)") : "");
597 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
598 results,
599 secondary,
600 more);
601 g_free(secondary);
604 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
605 const gchar *uri,
606 SIPE_UNUSED_PARAMETER const gchar *raw,
607 sipe_xml *soap_body,
608 gpointer callback_data)
610 struct ms_dlx_data *mdd = callback_data;
612 if (soap_body) {
613 const sipe_xml *node;
614 struct sipe_backend_search_results *results;
615 GHashTable *found;
617 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
618 uri);
620 /* any matches? */
621 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
622 if (!node) {
623 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
624 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
625 mdd->token,
626 _("No contacts found"));
627 ms_dlx_free(mdd);
628 return;
631 /* OK, we found something - show the results to the user */
632 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
633 mdd->token);
634 if (!results) {
635 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
636 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
637 mdd->token,
638 _("Unable to display the search results"));
639 ms_dlx_free(mdd);
640 return;
643 /* SearchAbEntryResult can contain duplicates */
644 found = g_hash_table_new_full(g_str_hash, g_str_equal,
645 g_free, NULL);
647 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
648 const sipe_xml *attrs;
649 gchar *sip_uri = NULL;
650 gchar *displayname = NULL;
651 gchar *company = NULL;
652 gchar *country = NULL;
653 gchar *email = NULL;
655 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
656 attrs;
657 attrs = sipe_xml_twin(attrs)) {
658 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
659 "Name"));
660 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
661 "Value"));
663 if (!is_empty(value)) {
664 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
665 g_free(sip_uri);
666 sip_uri = value;
667 value = NULL;
668 } else if (sipe_strcase_equal(name, "displayname")) {
669 g_free(displayname);
670 displayname = value;
671 value = NULL;
672 } else if (sipe_strcase_equal(name, "mail")) {
673 g_free(email);
674 email = value;
675 value = NULL;
676 } else if (sipe_strcase_equal(name, "company")) {
677 g_free(company);
678 company = value;
679 value = NULL;
680 } else if (sipe_strcase_equal(name, "country")) {
681 g_free(country);
682 country = value;
683 value = NULL;
687 g_free(value);
688 g_free(name);
691 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
692 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
693 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
694 results,
695 uri_parts[1],
696 displayname,
697 company,
698 country,
699 email);
700 g_strfreev(uri_parts);
702 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
703 sip_uri = NULL;
706 g_free(email);
707 g_free(country);
708 g_free(company);
709 g_free(displayname);
710 g_free(sip_uri);
713 search_contacts_finalize(sipe_private, results,
714 g_hash_table_size(found),
715 FALSE);
716 g_hash_table_destroy(found);
717 ms_dlx_free(mdd);
719 } else {
720 mdd->failed_callback(sipe_private, mdd);
724 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
725 struct sipmsg *msg,
726 struct transaction *trans)
728 struct sipe_backend_search_token *token = trans->payload->data;
729 struct sipe_backend_search_results *results;
730 sipe_xml *searchResults;
731 const sipe_xml *mrow;
732 guint match_count = 0;
733 gboolean more = FALSE;
735 /* valid response? */
736 if (msg->response != 200) {
737 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
738 msg->response);
739 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
740 token,
741 _("Contact search failed"));
742 return(FALSE);
745 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
747 /* valid XML? */
748 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
749 if (!searchResults) {
750 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
751 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
752 token,
753 _("Contact search failed"));
754 return(FALSE);
757 /* any matches? */
758 mrow = sipe_xml_child(searchResults, "Body/Array/row");
759 if (!mrow) {
760 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
761 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
762 token,
763 _("No contacts found"));
765 sipe_xml_free(searchResults);
766 return(FALSE);
769 /* OK, we found something - show the results to the user */
770 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
771 trans->payload->data);
772 if (!results) {
773 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
774 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
775 token,
776 _("Unable to display the search results"));
778 sipe_xml_free(searchResults);
779 return FALSE;
782 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
783 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
784 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
785 results,
786 uri_parts[1],
787 sipe_xml_attribute(mrow, "displayName"),
788 sipe_xml_attribute(mrow, "company"),
789 sipe_xml_attribute(mrow, "country"),
790 sipe_xml_attribute(mrow, "email"));
791 g_strfreev(uri_parts);
792 match_count++;
795 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
796 char *data = sipe_xml_data(mrow);
797 more = (g_ascii_strcasecmp(data, "true") == 0);
798 g_free(data);
801 search_contacts_finalize(sipe_private, results, match_count, more);
802 sipe_xml_free(searchResults);
804 return(TRUE);
807 static void search_soap_request(struct sipe_core_private *sipe_private,
808 struct sipe_backend_search_token *token,
809 GSList *search_rows)
811 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
812 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
814 payload->data = token;
816 sip_soap_directory_search(sipe_private,
817 100,
818 query,
819 process_search_contact_response,
820 payload);
821 g_free(query);
824 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
825 struct ms_dlx_data *mdd)
827 /* error using [MS-DLX] server, retry using Active Directory */
828 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
829 ms_dlx_free(mdd);
832 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
833 struct sipe_backend_search_token *token,
834 const gchar *given_name,
835 const gchar *surname,
836 const gchar *email,
837 const gchar *company,
838 const gchar *country)
840 GSList *query_rows = NULL;
842 #define ADD_QUERY_ROW(attr, val) \
843 if (val) { \
844 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
845 query_rows = g_slist_append(query_rows, g_strdup(val)); \
848 ADD_QUERY_ROW("givenName", given_name);
849 ADD_QUERY_ROW("sn", surname);
850 ADD_QUERY_ROW("mail", email);
851 ADD_QUERY_ROW("company", company);
852 ADD_QUERY_ROW("c", country);
854 if (query_rows) {
855 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
856 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
858 mdd->search_rows = query_rows;
859 mdd->max_returns = 100;
860 mdd->callback = search_ab_entry_response;
861 mdd->failed_callback = search_ab_entry_failed;
862 mdd->session = sipe_svc_session_start();
863 mdd->token = token;
865 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
867 } else {
868 /* no [MS-DLX] server, use Active Directory search instead */
869 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
870 sipe_utils_slist_free_full(query_rows, g_free);
872 } else
873 sipe_backend_search_failed(sipe_public,
874 token,
875 _("Invalid contact search query"));
878 static void get_info_finalize(struct sipe_core_private *sipe_private,
879 struct sipe_backend_buddy_info *info,
880 const gchar *uri,
881 const gchar *server_alias,
882 const gchar *email)
884 sipe_backend_buddy bbuddy;
885 struct sipe_buddy *sbuddy;
886 gchar *alias;
887 gchar *value;
889 if (!info) {
890 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
891 } else {
892 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
894 if (!info)
895 return;
897 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
899 if (is_empty(server_alias)) {
900 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
901 bbuddy);
902 if (value) {
903 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
904 info,
905 SIPE_BUDDY_INFO_DISPLAY_NAME,
906 value);
908 } else {
909 value = g_strdup(server_alias);
912 /* present alias if it differs from server alias */
913 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
914 if (alias && !sipe_strequal(alias, value))
916 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
917 info,
918 SIPE_BUDDY_INFO_ALIAS,
919 alias);
921 g_free(alias);
922 g_free(value);
924 if (is_empty(email)) {
925 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
926 bbuddy,
927 SIPE_BUDDY_INFO_EMAIL);
928 if (value) {
929 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
930 info,
931 SIPE_BUDDY_INFO_EMAIL,
932 value);
933 g_free(value);
937 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
938 bbuddy,
939 SIPE_BUDDY_INFO_SITE);
940 if (value) {
941 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
942 info,
943 SIPE_BUDDY_INFO_SITE,
944 value);
945 g_free(value);
948 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
949 if (sbuddy && sbuddy->device_name) {
950 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
951 info,
952 SIPE_BUDDY_INFO_DEVICE,
953 sbuddy->device_name);
956 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
960 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
961 const gchar *uri,
962 SIPE_UNUSED_PARAMETER const gchar *raw,
963 sipe_xml *soap_body,
964 gpointer callback_data)
966 struct ms_dlx_data *mdd = callback_data;
967 struct sipe_backend_buddy_info *info = NULL;
968 gchar *server_alias = NULL;
969 gchar *email = NULL;
971 if (soap_body) {
972 const sipe_xml *node;
974 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
975 uri);
977 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
979 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
980 node;
981 node = sipe_xml_twin(node)) {
982 gchar *name = sipe_xml_data(sipe_xml_child(node,
983 "Name"));
984 gchar *value = sipe_xml_data(sipe_xml_child(node,
985 "Value"));
986 const sipe_xml *values = sipe_xml_child(node,
987 "Values");
989 /* Single value entries */
990 if (!is_empty(value)) {
992 if (sipe_strcase_equal(name, "displayname")) {
993 g_free(server_alias);
994 server_alias = value;
995 value = NULL;
996 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
997 info,
998 SIPE_BUDDY_INFO_DISPLAY_NAME,
999 server_alias);
1000 } else if (sipe_strcase_equal(name, "mail")) {
1001 g_free(email);
1002 email = value;
1003 value = NULL;
1004 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1005 info,
1006 SIPE_BUDDY_INFO_EMAIL,
1007 email);
1008 } else if (sipe_strcase_equal(name, "title")) {
1009 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1010 info,
1011 SIPE_BUDDY_INFO_JOB_TITLE,
1012 value);
1013 } else if (sipe_strcase_equal(name, "company")) {
1014 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1015 info,
1016 SIPE_BUDDY_INFO_COMPANY,
1017 value);
1018 } else if (sipe_strcase_equal(name, "country")) {
1019 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1020 info,
1021 SIPE_BUDDY_INFO_COUNTRY,
1022 value);
1025 } else if (values) {
1026 gchar *first = sipe_xml_data(sipe_xml_child(values,
1027 "string"));
1029 if (sipe_strcase_equal(name, "telephonenumber")) {
1030 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1031 info,
1032 SIPE_BUDDY_INFO_WORK_PHONE,
1033 first);
1036 g_free(first);
1039 g_free(value);
1040 g_free(name);
1044 /* this will show the minmum information */
1045 get_info_finalize(sipe_private,
1046 info,
1047 mdd->other,
1048 server_alias,
1049 email);
1051 g_free(email);
1052 g_free(server_alias);
1053 ms_dlx_free(mdd);
1056 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1057 struct sipmsg *msg,
1058 struct transaction *trans)
1060 const gchar *uri = trans->payload->data;
1061 struct sipe_backend_buddy_info *info = NULL;
1062 gchar *server_alias = NULL;
1063 gchar *email = NULL;
1065 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1066 uri, sipe_private->username);
1068 if (msg->response != 200) {
1069 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1070 } else {
1071 sipe_xml *searchResults;
1072 const sipe_xml *mrow;
1074 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1075 msg->body ? msg->body : "");
1077 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1078 if (!searchResults) {
1080 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1082 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1083 const gchar *value;
1084 gchar *phone_number;
1086 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1088 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1089 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1090 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1093 * For 2007 system we will take this from ContactCard -
1094 * it has cleaner tel: URIs at least
1096 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1097 char *tel_uri = sip_to_tel_uri(phone_number);
1098 /* trims its parameters, so call first */
1099 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1100 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1101 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1102 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1103 g_free(tel_uri);
1105 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1106 uri);
1109 if (!is_empty(server_alias)) {
1110 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1111 info,
1112 SIPE_BUDDY_INFO_DISPLAY_NAME,
1113 server_alias);
1115 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1116 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1117 info,
1118 SIPE_BUDDY_INFO_JOB_TITLE,
1119 value);
1121 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1122 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1123 info,
1124 SIPE_BUDDY_INFO_OFFICE,
1125 value);
1127 if (!is_empty(phone_number)) {
1128 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1129 info,
1130 SIPE_BUDDY_INFO_WORK_PHONE,
1131 phone_number);
1133 g_free(phone_number);
1134 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1135 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1136 info,
1137 SIPE_BUDDY_INFO_COMPANY,
1138 value);
1140 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1141 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1142 info,
1143 SIPE_BUDDY_INFO_CITY,
1144 value);
1146 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1147 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1148 info,
1149 SIPE_BUDDY_INFO_STATE,
1150 value);
1152 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1153 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1154 info,
1155 SIPE_BUDDY_INFO_COUNTRY,
1156 value);
1158 if (!is_empty(email)) {
1159 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1160 info,
1161 SIPE_BUDDY_INFO_EMAIL,
1162 email);
1165 sipe_xml_free(searchResults);
1168 /* this will show the minmum information */
1169 get_info_finalize(sipe_private,
1170 info,
1171 uri,
1172 server_alias,
1173 email);
1175 g_free(server_alias);
1176 g_free(email);
1178 return TRUE;
1181 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1182 struct ms_dlx_data *mdd)
1184 /* error using [MS-DLX] server, retry using Active Directory */
1185 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1186 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1188 payload->destroy = g_free;
1189 payload->data = mdd->other;
1190 mdd->other = NULL;
1192 sip_soap_directory_search(sipe_private,
1194 query,
1195 process_get_info_response,
1196 payload);
1198 ms_dlx_free(mdd);
1199 g_free(query);
1202 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1203 const gchar *who)
1205 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1207 if (sipe_private->dlx_uri) {
1208 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1210 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1211 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1213 mdd->other = g_strdup(who);
1214 mdd->max_returns = 1;
1215 mdd->callback = get_info_ab_entry_response;
1216 mdd->failed_callback = get_info_ab_entry_failed;
1217 mdd->session = sipe_svc_session_start();
1219 ms_dlx_webticket_request(sipe_private, mdd);
1221 } else {
1222 /* no [MS-DLX] server, use Active Directory search instead */
1223 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1224 "msRTCSIP-PrimaryUserAddress",
1225 who);
1226 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1228 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1229 row ? row : "");
1231 payload->destroy = g_free;
1232 payload->data = g_strdup(who);
1234 sip_soap_directory_search(sipe_private,
1236 row,
1237 process_get_info_response,
1238 payload);
1239 g_free(row);
1243 static void photo_response_data_free(struct photo_response_data *data)
1245 g_free(data->who);
1246 g_free(data->photo_hash);
1247 if (data->request) {
1248 sipe_http_request_cancel(data->request);
1250 g_free(data);
1253 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1254 guint status,
1255 GSList *headers,
1256 const char *body,
1257 gpointer data)
1259 struct photo_response_data *rdata = (struct photo_response_data *) data;
1261 rdata->request = NULL;
1263 if (status == SIPE_HTTP_STATUS_OK) {
1264 const gchar *len_str = sipe_utils_nameval_find(headers,
1265 "Content-Length");
1266 if (len_str) {
1267 gsize photo_size = atoi(len_str);
1268 gpointer photo = g_new(char, photo_size);
1270 if (photo) {
1271 memcpy(photo, body, photo_size);
1273 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1274 rdata->who,
1275 photo,
1276 photo_size,
1277 rdata->photo_hash);
1282 sipe_private->pending_photo_requests =
1283 g_slist_remove(sipe_private->pending_photo_requests, rdata);
1285 photo_response_data_free(rdata);
1288 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1290 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1291 gchar *wsse_security_base64;
1292 gchar *x_ms_webticket_header;
1294 if (!assertion) {
1295 return NULL;
1298 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1299 strlen(assertion));
1300 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1301 wsse_security_base64);
1303 g_free(assertion);
1304 g_free(wsse_security_base64);
1306 return x_ms_webticket_header;
1309 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1310 const gchar *uri,
1311 SIPE_UNUSED_PARAMETER const gchar *raw,
1312 sipe_xml *soap_body,
1313 gpointer callback_data)
1315 struct ms_dlx_data *mdd = callback_data;
1316 gchar *photo_rel_path = NULL;
1317 gchar *photo_hash = NULL;
1318 const gchar *photo_hash_old =
1319 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1321 if (soap_body) {
1322 const sipe_xml *node;
1324 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1325 uri);
1327 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1328 node;
1329 node = sipe_xml_twin(node)) {
1330 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1331 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1333 if (!is_empty(value)) {
1334 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1335 g_free(photo_rel_path);
1336 photo_rel_path = value;
1337 value = NULL;
1338 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1339 g_free(photo_hash);
1340 photo_hash = value;
1341 value = NULL;
1345 g_free(value);
1346 g_free(name);
1350 if (sipe_private->addressbook_uri && photo_rel_path &&
1351 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1352 gchar *photo_url = g_strdup_printf("%s/%s",
1353 sipe_private->addressbook_uri, photo_rel_path);
1354 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1356 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1357 data->who = g_strdup(mdd->other);
1358 data->photo_hash = photo_hash;
1359 photo_hash = NULL;
1361 data->request = sipe_http_request_get(sipe_private,
1362 photo_url,
1363 x_ms_webticket_header,
1364 process_buddy_photo_response,
1365 data);
1367 if (data->request) {
1368 sipe_private->pending_photo_requests =
1369 g_slist_append(sipe_private->pending_photo_requests, data);
1370 sipe_http_request_ready(data->request);
1371 } else {
1372 photo_response_data_free(data);
1375 g_free(x_ms_webticket_header);
1376 g_free(photo_url);
1379 g_free(photo_rel_path);
1380 g_free(photo_hash);
1381 ms_dlx_free(mdd);
1384 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1385 struct ms_dlx_data *mdd)
1387 ms_dlx_free(mdd);
1390 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1391 const gchar *uri)
1393 if (sipe_backend_uses_photo()) {
1395 /* Lync 2013 or newer: use UCS */
1396 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013)) {
1398 sipe_ucs_get_photo(sipe_private, uri);
1400 /* Lync 2010: use [MS-DLX] */
1401 } else if (sipe_private->dlx_uri &&
1402 sipe_private->addressbook_uri) {
1403 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1405 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1406 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1408 mdd->other = g_strdup(uri);
1409 mdd->max_returns = 1;
1410 mdd->callback = get_photo_ab_entry_response;
1411 mdd->failed_callback = get_photo_ab_entry_failed;
1412 mdd->session = sipe_svc_session_start();
1414 ms_dlx_webticket_request(sipe_private, mdd);
1419 static void buddy_refresh_photos_cb(gpointer uri,
1420 SIPE_UNUSED_PARAMETER gpointer value,
1421 gpointer sipe_private)
1423 buddy_fetch_photo(sipe_private, uri);
1426 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1428 g_hash_table_foreach(sipe_private->buddies,
1429 buddy_refresh_photos_cb,
1430 sipe_private);
1433 /* Buddy menu callbacks*/
1435 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1436 const gchar *who)
1438 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1440 /* 2007+ conference */
1441 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1442 sipe_conf_add(sipe_private, who);
1444 /* 2005- multiparty chat */
1445 } else {
1446 gchar *self = sip_uri_self(sipe_private);
1447 struct sip_session *session;
1449 session = sipe_session_add_chat(sipe_private,
1450 NULL,
1451 TRUE,
1452 self);
1453 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1454 session->chat_session,
1455 session->chat_session->title,
1456 self);
1457 g_free(self);
1459 sipe_im_invite(sipe_private, session, who,
1460 NULL, NULL, NULL, FALSE);
1464 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1465 const gchar *who)
1467 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1468 who,
1469 NULL);
1470 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1471 buddy,
1472 SIPE_BUDDY_INFO_EMAIL);
1474 if (email) {
1475 gchar *command_line = g_strdup_printf(
1476 #ifdef _WIN32
1477 "cmd /c start"
1478 #else
1479 "xdg-email"
1480 #endif
1481 " mailto:%s", email);
1482 g_free(email);
1484 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1485 command_line);
1486 g_spawn_command_line_async(command_line, NULL);
1487 g_free(command_line);
1489 } else {
1490 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1491 who);
1495 /* Buddy menu */
1497 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1498 struct sipe_backend_buddy_menu *menu,
1499 sipe_backend_buddy buddy,
1500 sipe_buddy_info_fields id_phone,
1501 sipe_buddy_info_fields id_display,
1502 const gchar *type)
1504 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1505 buddy,
1506 id_phone);
1507 if (phone) {
1508 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1509 buddy,
1510 id_display);
1511 gchar *tmp = NULL;
1512 gchar *label = g_strdup_printf("%s %s",
1513 type,
1514 display ? display :
1515 (tmp = sip_tel_uri_denormalize(phone)));
1516 menu = sipe_backend_buddy_menu_add(sipe_public,
1517 menu,
1518 label,
1519 SIPE_BUDDY_MENU_MAKE_CALL,
1520 phone);
1521 g_free(tmp);
1522 g_free(label);
1523 g_free(display);
1524 g_free(phone);
1527 return(menu);
1530 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1531 const gchar *buddy_name,
1532 struct sipe_backend_buddy_menu *menu)
1534 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1535 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1536 buddy_name,
1537 NULL);
1538 gchar *self = sip_uri_self(sipe_private);
1540 SIPE_SESSION_FOREACH {
1541 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1543 struct sipe_chat_session *chat_session = session->chat_session;
1544 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1546 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1548 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1550 if (is_conf &&
1551 /* Not conf OP */
1552 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1553 /* We are a conf OP */
1554 conf_op) {
1555 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1556 chat_session->title);
1557 menu = sipe_backend_buddy_menu_add(sipe_public,
1558 menu,
1559 label,
1560 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1561 chat_session);
1562 g_free(label);
1565 if (is_conf &&
1566 /* We are a conf OP */
1567 conf_op) {
1568 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1569 chat_session->title);
1570 menu = sipe_backend_buddy_menu_add(sipe_public,
1571 menu,
1572 label,
1573 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1574 chat_session);
1575 g_free(label);
1578 else
1580 if (!is_conf ||
1581 (is_conf && !session->locked)) {
1582 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1583 chat_session->title);
1584 menu = sipe_backend_buddy_menu_add(sipe_public,
1585 menu,
1586 label,
1587 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1588 chat_session);
1589 g_free(label);
1593 } SIPE_SESSION_FOREACH_END;
1594 g_free(self);
1596 menu = sipe_backend_buddy_menu_add(sipe_public,
1597 menu,
1598 _("New chat"),
1599 SIPE_BUDDY_MENU_NEW_CHAT,
1600 NULL);
1602 /* add buddy's phone numbers if we have call control */
1603 if (sip_csta_is_idle(sipe_private)) {
1605 /* work phone */
1606 menu = buddy_menu_phone(sipe_public,
1607 menu,
1608 buddy,
1609 SIPE_BUDDY_INFO_WORK_PHONE,
1610 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1611 _("Work"));
1612 /* mobile phone */
1613 menu = buddy_menu_phone(sipe_public,
1614 menu,
1615 buddy,
1616 SIPE_BUDDY_INFO_MOBILE_PHONE,
1617 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1618 _("Mobile"));
1620 /* home phone */
1621 menu = buddy_menu_phone(sipe_public,
1622 menu,
1623 buddy,
1624 SIPE_BUDDY_INFO_HOME_PHONE,
1625 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1626 _("Home"));
1628 /* other phone */
1629 menu = buddy_menu_phone(sipe_public,
1630 menu,
1631 buddy,
1632 SIPE_BUDDY_INFO_OTHER_PHONE,
1633 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1634 _("Other"));
1636 /* custom1 phone */
1637 menu = buddy_menu_phone(sipe_public,
1638 menu,
1639 buddy,
1640 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1641 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1642 _("Custom1"));
1646 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1647 buddy,
1648 SIPE_BUDDY_INFO_EMAIL);
1649 if (email) {
1650 menu = sipe_backend_buddy_menu_add(sipe_public,
1651 menu,
1652 _("Send email..."),
1653 SIPE_BUDDY_MENU_SEND_EMAIL,
1654 NULL);
1655 g_free(email);
1659 /* access level control */
1660 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1661 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1662 menu,
1663 _("Access level"),
1664 sipe_ocs2007_access_control_menu(sipe_private,
1665 buddy_name));
1667 return(menu);
1670 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
1672 return(g_hash_table_size(sipe_private->buddies));
1675 static guint sipe_ht_hash_nick(const char *nick)
1677 char *lc = g_utf8_strdown(nick, -1);
1678 guint bucket = g_str_hash(lc);
1679 g_free(lc);
1681 return bucket;
1684 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
1686 char *nick1_norm = NULL;
1687 char *nick2_norm = NULL;
1688 gboolean equal;
1690 if (nick1 == NULL && nick2 == NULL) return TRUE;
1691 if (nick1 == NULL || nick2 == NULL ||
1692 !g_utf8_validate(nick1, -1, NULL) ||
1693 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
1695 nick1_norm = g_utf8_casefold(nick1, -1);
1696 nick2_norm = g_utf8_casefold(nick2, -1);
1697 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
1698 g_free(nick2_norm);
1699 g_free(nick1_norm);
1701 return equal;
1704 void sipe_buddy_init(struct sipe_core_private *sipe_private)
1706 sipe_private->buddies = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
1707 (GEqualFunc) sipe_ht_equals_nick);
1713 Local Variables:
1714 mode: c
1715 c-file-style: "bsd"
1716 indent-tabs-mode: t
1717 tab-width: 8
1718 End: