buddy: internalize pending photo requests list
[siplcs.git] / src / core / sipe-buddy.c
blob3746ae39ad77aa1f3e5f00c78bc81b7e66d57193
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 sipe_buddies {
62 GHashTable *uri;
64 /* Pending photo download HTTP requests */
65 GSList *pending_photo_requests;
68 struct photo_response_data {
69 gchar *who;
70 gchar *photo_hash;
71 struct sipe_http_request *request;
74 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
75 const gchar *uri);
76 static void photo_response_data_free(struct photo_response_data *data);
78 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
79 const gchar *uri,
80 const gchar *exchange_key)
82 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private, uri);
83 if (!buddy) {
84 buddy = g_new0(struct sipe_buddy, 1);
85 buddy->name = g_strdup(uri);
86 buddy->exchange_key = g_strdup(exchange_key);
87 g_hash_table_insert(sipe_private->buddies->uri,
88 buddy->name,
89 buddy);
91 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", uri);
93 buddy_fetch_photo(sipe_private, uri);
94 } else {
95 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", uri);
98 return buddy;
101 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
102 const gchar *uri)
104 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
107 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
108 GHFunc callback,
109 gpointer callback_data)
111 g_hash_table_foreach(sipe_private->buddies->uri,
112 callback,
113 callback_data);
116 static void buddy_free(struct sipe_buddy *buddy)
118 #ifndef _WIN32
120 * We are calling g_hash_table_foreach_steal(). That means that no
121 * key/value deallocation functions are called. Therefore the glib
122 * hash code does not touch the key (buddy->name) or value (buddy)
123 * of the to-be-deleted hash node at all. It follows that we
125 * - MUST free the memory for the key ourselves and
126 * - ARE allowed to do it in this function
128 * Conclusion: glib must be broken on the Windows platform if sipe
129 * crashes with SIGTRAP when closing. You'll have to live
130 * with the memory leak until this is fixed.
132 g_free(buddy->name);
133 #endif
134 g_free(buddy->exchange_key);
135 g_free(buddy->activity);
136 g_free(buddy->meeting_subject);
137 g_free(buddy->meeting_location);
138 g_free(buddy->note);
140 g_free(buddy->cal_start_time);
141 g_free(buddy->cal_free_busy_base64);
142 g_free(buddy->cal_free_busy);
143 g_free(buddy->last_non_cal_activity);
145 sipe_cal_free_working_hours(buddy->cal_working_hours);
147 g_free(buddy->device_name);
148 g_slist_free(buddy->groups);
149 g_free(buddy);
152 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
153 gpointer buddy,
154 SIPE_UNUSED_PARAMETER gpointer user_data)
156 buddy_free(buddy);
157 /* We must return TRUE as the key/value have already been deleted */
158 return(TRUE);
161 void sipe_buddy_free(struct sipe_core_private *sipe_private)
163 struct sipe_buddies *buddies = sipe_private->buddies;
165 g_hash_table_foreach_steal(buddies->uri,
166 buddy_free_cb,
167 NULL);
169 /* core is being deallocated, remove all its pending photo requests */
170 while (buddies->pending_photo_requests) {
171 struct photo_response_data *data =
172 buddies->pending_photo_requests->data;
173 buddies->pending_photo_requests =
174 g_slist_remove(buddies->pending_photo_requests, data);
175 photo_response_data_free(data);
178 g_hash_table_destroy(buddies->uri);
179 g_free(buddies);
180 sipe_private->buddies = NULL;
183 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
184 const gchar *uri,
185 guint activity,
186 const gchar *status_text)
188 struct sipe_buddy *sbuddy;
189 const char *activity_str;
191 if (!sipe_public) return NULL; /* happens on pidgin exit */
193 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
194 if (!sbuddy) return NULL;
196 activity_str = sbuddy->activity ? sbuddy->activity :
197 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
198 status_text : NULL;
200 if (activity_str && sbuddy->note) {
201 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
202 } else if (activity_str) {
203 return g_strdup(activity_str);
204 } else if (sbuddy->note) {
205 return g_strdup_printf("<i>%s</i>", sbuddy->note);
206 } else {
207 return NULL;
211 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
212 const gchar *with)
214 sipe_backend_buddy pbuddy;
215 gchar *alias = NULL;
216 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
217 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
219 return alias;
222 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
223 const gchar *who,
224 const gchar *old_group_name,
225 const gchar *new_group_name)
227 struct sipe_buddy * buddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE,
228 who);
229 struct sipe_group * old_group = NULL;
230 struct sipe_group * new_group;
232 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
233 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
235 if(!buddy) { // buddy not in roaming list
236 return;
239 if (old_group_name) {
240 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
242 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
244 if (old_group) {
245 buddy->groups = g_slist_remove(buddy->groups, old_group);
246 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
249 if (!new_group) {
250 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
251 } else {
252 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
253 new_group,
254 (GCompareFunc)sipe_group_compare,
255 NULL);
256 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
260 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
261 const gchar *uri,
262 const gchar *group_name)
264 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
266 if (!sipe_buddy_find_by_uri(sipe_private, uri)) {
267 struct sipe_buddy *b = sipe_buddy_add(sipe_private, uri, NULL);
268 b->just_added = TRUE;
270 sipe_subscribe_presence_single_cb(sipe_private, b->name);
272 } else {
273 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
274 uri);
277 sipe_core_buddy_group(sipe_public,
278 uri,
279 NULL,
280 group_name);
283 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
284 struct sipe_buddy *buddy)
286 gchar *action_name = sipe_utils_presence_key(buddy->name);
287 sipe_schedule_cancel(sipe_private, action_name);
288 g_free(action_name);
290 g_hash_table_remove(sipe_private->buddies->uri, buddy->name);
292 buddy_free(buddy);
296 * Unassociates buddy from group first.
297 * Then see if no groups left, removes buddy completely.
298 * Otherwise updates buddy groups on server.
300 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
301 const gchar *uri,
302 const gchar *group_name)
304 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
305 struct sipe_buddy *b = sipe_buddy_find_by_uri(sipe_private,
306 uri);
308 if (!b) return;
310 if (group_name) {
311 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
312 group_name);
313 if (g) {
314 b->groups = g_slist_remove(b->groups, g);
315 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
316 uri, g->name);
320 if (g_slist_length(b->groups) < 1) {
321 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
322 b->name);
323 sip_soap_request(sipe_private,
324 "deleteContact",
325 request);
326 g_free(request);
327 sipe_buddy_remove(sipe_private, b);
328 } else {
329 /* updates groups on server */
330 sipe_group_update_buddy(sipe_private, b);
335 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
336 const gchar *uri,
337 guint activity)
339 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
340 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
341 uri);
343 if (!sbuddy) return;
345 /* Check if on 2005 system contact's calendar,
346 * then set/preserve it.
348 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
349 sipe_backend_buddy_set_status(sipe_public, uri, activity);
350 } else {
351 sipe_ocs2005_apply_calendar_status(sipe_private,
352 sbuddy,
353 sipe_status_activity_to_token(activity));
357 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
358 const gchar *uri,
359 const gchar *status_name,
360 gboolean is_online,
361 struct sipe_backend_buddy_tooltip *tooltip)
363 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
364 gchar *note = NULL;
365 gboolean is_oof_note = FALSE;
366 const gchar *activity = NULL;
367 gchar *calendar = NULL;
368 const gchar *meeting_subject = NULL;
369 const gchar *meeting_location = NULL;
370 gchar *access_text = NULL;
372 #define SIPE_ADD_BUDDY_INFO(l, t) \
374 gchar *tmp = g_markup_escape_text((t), -1); \
375 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
376 g_free(tmp); \
378 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
379 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
381 if (sipe_public) { /* happens on pidgin exit */
382 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
383 uri);
384 if (sbuddy) {
385 note = sbuddy->note;
386 is_oof_note = sbuddy->is_oof_note;
387 activity = sbuddy->activity;
388 calendar = sipe_cal_get_description(sbuddy);
389 meeting_subject = sbuddy->meeting_subject;
390 meeting_location = sbuddy->meeting_location;
392 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
393 gboolean is_group_access = FALSE;
394 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
395 "user",
396 sipe_get_no_sip_uri(uri),
397 &is_group_access);
398 const char *access_level = sipe_ocs2007_access_level_name(container_id);
399 access_text = is_group_access ?
400 g_strdup(access_level) :
401 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
402 access_level);
406 if (is_online) {
407 const gchar *status_str = activity ? activity : status_name;
409 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
411 if (is_online && !is_empty(calendar)) {
412 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
414 g_free(calendar);
415 if (!is_empty(meeting_location)) {
416 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
417 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
419 if (!is_empty(meeting_subject)) {
420 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
421 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
423 if (note) {
424 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
425 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
426 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
427 note_italics);
428 g_free(note_italics);
430 if (access_text) {
431 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
432 g_free(access_text);
436 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
437 const char *uri,
438 sipe_buddy_info_fields propkey,
439 char *property_value)
441 GSList *buddies, *entry;
443 if (property_value)
444 property_value = g_strstrip(property_value);
446 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
447 while (entry) {
448 gchar *prop_str;
449 sipe_backend_buddy p_buddy = entry->data;
451 /* for Display Name */
452 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
453 gchar *alias;
454 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
455 if (property_value && sipe_is_bad_alias(uri, alias)) {
456 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
457 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
459 g_free(alias);
461 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
462 if (!is_empty(property_value) &&
463 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
465 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
466 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
468 g_free(alias);
470 /* for other properties */
471 else {
472 if (!is_empty(property_value)) {
473 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
474 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
475 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
477 g_free(prop_str);
481 entry = entry->next;
483 g_slist_free(buddies);
487 struct ms_dlx_data;
488 struct ms_dlx_data {
489 GSList *search_rows;
490 gchar *other;
491 guint max_returns;
492 sipe_svc_callback *callback;
493 struct sipe_svc_session *session;
494 gchar *wsse_security;
495 struct sipe_backend_search_token *token;
496 /* must call ms_dlx_free() */
497 void (*failed_callback)(struct sipe_core_private *sipe_private,
498 struct ms_dlx_data *mdd);
501 static void ms_dlx_free(struct ms_dlx_data *mdd)
503 sipe_utils_slist_free_full(mdd->search_rows, g_free);
504 sipe_svc_session_close(mdd->session);
505 g_free(mdd->other);
506 g_free(mdd->wsse_security);
507 g_free(mdd);
510 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
511 #define DLX_SEARCH_ITEM \
512 "<AbEntryRequest.ChangeSearchQuery>" \
513 " <SearchOn>%s</SearchOn>" \
514 " <Value>%s</Value>" \
515 "</AbEntryRequest.ChangeSearchQuery>"
517 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
518 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
519 guint i = 0;
520 gchar *query = NULL;
522 while (query_rows) {
523 gchar *attr;
524 gchar *value;
526 attr = query_rows->data;
527 query_rows = g_slist_next(query_rows);
528 value = query_rows->data;
529 query_rows = g_slist_next(query_rows);
531 if (!attr || !value)
532 break;
534 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
535 attr, value);
537 attrs[i] = NULL;
539 if (i) {
540 query = g_strjoinv(NULL, attrs);
541 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
542 query ? query : "");
545 g_strfreev(attrs);
547 return query;
550 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
551 const gchar *base_uri,
552 const gchar *auth_uri,
553 const gchar *wsse_security,
554 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
555 gpointer callback_data)
557 struct ms_dlx_data *mdd = callback_data;
559 if (wsse_security) {
560 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
562 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
563 base_uri);
565 if (sipe_svc_ab_entry_request(sipe_private,
566 mdd->session,
567 auth_uri,
568 wsse_security,
569 query,
570 g_slist_length(mdd->search_rows) / 2,
571 mdd->max_returns,
572 mdd->callback,
573 mdd)) {
575 /* keep webticket security token for potential further use */
576 mdd->wsse_security = g_strdup(wsse_security);
578 /* callback data passed down the line */
579 mdd = NULL;
581 g_free(query);
583 } else {
584 /* no ticket: this will show the minmum information */
585 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
586 base_uri);
589 if (mdd)
590 mdd->failed_callback(sipe_private, mdd);
593 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
594 struct ms_dlx_data *mdd)
596 if (!sipe_webticket_request(sipe_private,
597 mdd->session,
598 sipe_private->dlx_uri,
599 "AddressBookWebTicketBearer",
600 ms_dlx_webticket,
601 mdd)) {
602 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
603 sipe_private->dlx_uri);
604 mdd->failed_callback(sipe_private, mdd);
608 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
609 struct sipe_backend_search_results *results,
610 guint match_count,
611 gboolean more)
613 gchar *secondary = g_strdup_printf(
614 dngettext(PACKAGE_NAME,
615 "Found %d contact%s:",
616 "Found %d contacts%s:", match_count),
617 match_count, more ? _(" (more matched your query)") : "");
619 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
620 results,
621 secondary,
622 more);
623 g_free(secondary);
626 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
627 const gchar *uri,
628 SIPE_UNUSED_PARAMETER const gchar *raw,
629 sipe_xml *soap_body,
630 gpointer callback_data)
632 struct ms_dlx_data *mdd = callback_data;
634 if (soap_body) {
635 const sipe_xml *node;
636 struct sipe_backend_search_results *results;
637 GHashTable *found;
639 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
640 uri);
642 /* any matches? */
643 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
644 if (!node) {
645 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
646 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
647 mdd->token,
648 _("No contacts found"));
649 ms_dlx_free(mdd);
650 return;
653 /* OK, we found something - show the results to the user */
654 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
655 mdd->token);
656 if (!results) {
657 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
658 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
659 mdd->token,
660 _("Unable to display the search results"));
661 ms_dlx_free(mdd);
662 return;
665 /* SearchAbEntryResult can contain duplicates */
666 found = g_hash_table_new_full(g_str_hash, g_str_equal,
667 g_free, NULL);
669 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
670 const sipe_xml *attrs;
671 gchar *sip_uri = NULL;
672 gchar *displayname = NULL;
673 gchar *company = NULL;
674 gchar *country = NULL;
675 gchar *email = NULL;
677 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
678 attrs;
679 attrs = sipe_xml_twin(attrs)) {
680 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
681 "Name"));
682 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
683 "Value"));
685 if (!is_empty(value)) {
686 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
687 g_free(sip_uri);
688 sip_uri = value;
689 value = NULL;
690 } else if (sipe_strcase_equal(name, "displayname")) {
691 g_free(displayname);
692 displayname = value;
693 value = NULL;
694 } else if (sipe_strcase_equal(name, "mail")) {
695 g_free(email);
696 email = value;
697 value = NULL;
698 } else if (sipe_strcase_equal(name, "company")) {
699 g_free(company);
700 company = value;
701 value = NULL;
702 } else if (sipe_strcase_equal(name, "country")) {
703 g_free(country);
704 country = value;
705 value = NULL;
709 g_free(value);
710 g_free(name);
713 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
714 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
715 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
716 results,
717 uri_parts[1],
718 displayname,
719 company,
720 country,
721 email);
722 g_strfreev(uri_parts);
724 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
725 sip_uri = NULL;
728 g_free(email);
729 g_free(country);
730 g_free(company);
731 g_free(displayname);
732 g_free(sip_uri);
735 search_contacts_finalize(sipe_private, results,
736 g_hash_table_size(found),
737 FALSE);
738 g_hash_table_destroy(found);
739 ms_dlx_free(mdd);
741 } else {
742 mdd->failed_callback(sipe_private, mdd);
746 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
747 struct sipmsg *msg,
748 struct transaction *trans)
750 struct sipe_backend_search_token *token = trans->payload->data;
751 struct sipe_backend_search_results *results;
752 sipe_xml *searchResults;
753 const sipe_xml *mrow;
754 guint match_count = 0;
755 gboolean more = FALSE;
757 /* valid response? */
758 if (msg->response != 200) {
759 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
760 msg->response);
761 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
762 token,
763 _("Contact search failed"));
764 return(FALSE);
767 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
769 /* valid XML? */
770 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
771 if (!searchResults) {
772 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
773 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
774 token,
775 _("Contact search failed"));
776 return(FALSE);
779 /* any matches? */
780 mrow = sipe_xml_child(searchResults, "Body/Array/row");
781 if (!mrow) {
782 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
783 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
784 token,
785 _("No contacts found"));
787 sipe_xml_free(searchResults);
788 return(FALSE);
791 /* OK, we found something - show the results to the user */
792 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
793 trans->payload->data);
794 if (!results) {
795 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
796 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
797 token,
798 _("Unable to display the search results"));
800 sipe_xml_free(searchResults);
801 return FALSE;
804 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
805 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
806 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
807 results,
808 uri_parts[1],
809 sipe_xml_attribute(mrow, "displayName"),
810 sipe_xml_attribute(mrow, "company"),
811 sipe_xml_attribute(mrow, "country"),
812 sipe_xml_attribute(mrow, "email"));
813 g_strfreev(uri_parts);
814 match_count++;
817 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
818 char *data = sipe_xml_data(mrow);
819 more = (g_ascii_strcasecmp(data, "true") == 0);
820 g_free(data);
823 search_contacts_finalize(sipe_private, results, match_count, more);
824 sipe_xml_free(searchResults);
826 return(TRUE);
829 static void search_soap_request(struct sipe_core_private *sipe_private,
830 struct sipe_backend_search_token *token,
831 GSList *search_rows)
833 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
834 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
836 payload->data = token;
838 sip_soap_directory_search(sipe_private,
839 100,
840 query,
841 process_search_contact_response,
842 payload);
843 g_free(query);
846 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
847 struct ms_dlx_data *mdd)
849 /* error using [MS-DLX] server, retry using Active Directory */
850 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
851 ms_dlx_free(mdd);
854 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
855 struct sipe_backend_search_token *token,
856 const gchar *given_name,
857 const gchar *surname,
858 const gchar *email,
859 const gchar *company,
860 const gchar *country)
862 GSList *query_rows = NULL;
864 #define ADD_QUERY_ROW(attr, val) \
865 if (val) { \
866 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
867 query_rows = g_slist_append(query_rows, g_strdup(val)); \
870 ADD_QUERY_ROW("givenName", given_name);
871 ADD_QUERY_ROW("sn", surname);
872 ADD_QUERY_ROW("mail", email);
873 ADD_QUERY_ROW("company", company);
874 ADD_QUERY_ROW("c", country);
876 if (query_rows) {
877 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
878 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
880 mdd->search_rows = query_rows;
881 mdd->max_returns = 100;
882 mdd->callback = search_ab_entry_response;
883 mdd->failed_callback = search_ab_entry_failed;
884 mdd->session = sipe_svc_session_start();
885 mdd->token = token;
887 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
889 } else {
890 /* no [MS-DLX] server, use Active Directory search instead */
891 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
892 sipe_utils_slist_free_full(query_rows, g_free);
894 } else
895 sipe_backend_search_failed(sipe_public,
896 token,
897 _("Invalid contact search query"));
900 static void get_info_finalize(struct sipe_core_private *sipe_private,
901 struct sipe_backend_buddy_info *info,
902 const gchar *uri,
903 const gchar *server_alias,
904 const gchar *email)
906 sipe_backend_buddy bbuddy;
907 struct sipe_buddy *sbuddy;
908 gchar *alias;
909 gchar *value;
911 if (!info) {
912 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
913 } else {
914 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
916 if (!info)
917 return;
919 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
921 if (is_empty(server_alias)) {
922 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
923 bbuddy);
924 if (value) {
925 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
926 info,
927 SIPE_BUDDY_INFO_DISPLAY_NAME,
928 value);
930 } else {
931 value = g_strdup(server_alias);
934 /* present alias if it differs from server alias */
935 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
936 if (alias && !sipe_strequal(alias, value))
938 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
939 info,
940 SIPE_BUDDY_INFO_ALIAS,
941 alias);
943 g_free(alias);
944 g_free(value);
946 if (is_empty(email)) {
947 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
948 bbuddy,
949 SIPE_BUDDY_INFO_EMAIL);
950 if (value) {
951 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
952 info,
953 SIPE_BUDDY_INFO_EMAIL,
954 value);
955 g_free(value);
959 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
960 bbuddy,
961 SIPE_BUDDY_INFO_SITE);
962 if (value) {
963 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
964 info,
965 SIPE_BUDDY_INFO_SITE,
966 value);
967 g_free(value);
970 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
971 if (sbuddy && sbuddy->device_name) {
972 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
973 info,
974 SIPE_BUDDY_INFO_DEVICE,
975 sbuddy->device_name);
978 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
982 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
983 const gchar *uri,
984 SIPE_UNUSED_PARAMETER const gchar *raw,
985 sipe_xml *soap_body,
986 gpointer callback_data)
988 struct ms_dlx_data *mdd = callback_data;
989 struct sipe_backend_buddy_info *info = NULL;
990 gchar *server_alias = NULL;
991 gchar *email = NULL;
993 if (soap_body) {
994 const sipe_xml *node;
996 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
997 uri);
999 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1001 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1002 node;
1003 node = sipe_xml_twin(node)) {
1004 gchar *name = sipe_xml_data(sipe_xml_child(node,
1005 "Name"));
1006 gchar *value = sipe_xml_data(sipe_xml_child(node,
1007 "Value"));
1008 const sipe_xml *values = sipe_xml_child(node,
1009 "Values");
1011 /* Single value entries */
1012 if (!is_empty(value)) {
1014 if (sipe_strcase_equal(name, "displayname")) {
1015 g_free(server_alias);
1016 server_alias = value;
1017 value = NULL;
1018 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1019 info,
1020 SIPE_BUDDY_INFO_DISPLAY_NAME,
1021 server_alias);
1022 } else if (sipe_strcase_equal(name, "mail")) {
1023 g_free(email);
1024 email = value;
1025 value = NULL;
1026 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1027 info,
1028 SIPE_BUDDY_INFO_EMAIL,
1029 email);
1030 } else if (sipe_strcase_equal(name, "title")) {
1031 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1032 info,
1033 SIPE_BUDDY_INFO_JOB_TITLE,
1034 value);
1035 } else if (sipe_strcase_equal(name, "company")) {
1036 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1037 info,
1038 SIPE_BUDDY_INFO_COMPANY,
1039 value);
1040 } else if (sipe_strcase_equal(name, "country")) {
1041 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1042 info,
1043 SIPE_BUDDY_INFO_COUNTRY,
1044 value);
1047 } else if (values) {
1048 gchar *first = sipe_xml_data(sipe_xml_child(values,
1049 "string"));
1051 if (sipe_strcase_equal(name, "telephonenumber")) {
1052 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1053 info,
1054 SIPE_BUDDY_INFO_WORK_PHONE,
1055 first);
1058 g_free(first);
1061 g_free(value);
1062 g_free(name);
1066 /* this will show the minmum information */
1067 get_info_finalize(sipe_private,
1068 info,
1069 mdd->other,
1070 server_alias,
1071 email);
1073 g_free(email);
1074 g_free(server_alias);
1075 ms_dlx_free(mdd);
1078 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1079 struct sipmsg *msg,
1080 struct transaction *trans)
1082 const gchar *uri = trans->payload->data;
1083 struct sipe_backend_buddy_info *info = NULL;
1084 gchar *server_alias = NULL;
1085 gchar *email = NULL;
1087 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1088 uri, sipe_private->username);
1090 if (msg->response != 200) {
1091 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1092 } else {
1093 sipe_xml *searchResults;
1094 const sipe_xml *mrow;
1096 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1097 msg->body ? msg->body : "");
1099 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1100 if (!searchResults) {
1102 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1104 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1105 const gchar *value;
1106 gchar *phone_number;
1108 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1110 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1111 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1112 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1115 * For 2007 system we will take this from ContactCard -
1116 * it has cleaner tel: URIs at least
1118 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1119 char *tel_uri = sip_to_tel_uri(phone_number);
1120 /* trims its parameters, so call first */
1121 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1122 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1123 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1124 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1125 g_free(tel_uri);
1127 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1128 uri);
1131 if (!is_empty(server_alias)) {
1132 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1133 info,
1134 SIPE_BUDDY_INFO_DISPLAY_NAME,
1135 server_alias);
1137 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1138 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1139 info,
1140 SIPE_BUDDY_INFO_JOB_TITLE,
1141 value);
1143 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1144 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1145 info,
1146 SIPE_BUDDY_INFO_OFFICE,
1147 value);
1149 if (!is_empty(phone_number)) {
1150 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1151 info,
1152 SIPE_BUDDY_INFO_WORK_PHONE,
1153 phone_number);
1155 g_free(phone_number);
1156 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1157 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1158 info,
1159 SIPE_BUDDY_INFO_COMPANY,
1160 value);
1162 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1163 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1164 info,
1165 SIPE_BUDDY_INFO_CITY,
1166 value);
1168 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1169 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1170 info,
1171 SIPE_BUDDY_INFO_STATE,
1172 value);
1174 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1175 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1176 info,
1177 SIPE_BUDDY_INFO_COUNTRY,
1178 value);
1180 if (!is_empty(email)) {
1181 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1182 info,
1183 SIPE_BUDDY_INFO_EMAIL,
1184 email);
1187 sipe_xml_free(searchResults);
1190 /* this will show the minmum information */
1191 get_info_finalize(sipe_private,
1192 info,
1193 uri,
1194 server_alias,
1195 email);
1197 g_free(server_alias);
1198 g_free(email);
1200 return TRUE;
1203 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1204 struct ms_dlx_data *mdd)
1206 /* error using [MS-DLX] server, retry using Active Directory */
1207 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1208 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1210 payload->destroy = g_free;
1211 payload->data = mdd->other;
1212 mdd->other = NULL;
1214 sip_soap_directory_search(sipe_private,
1216 query,
1217 process_get_info_response,
1218 payload);
1220 ms_dlx_free(mdd);
1221 g_free(query);
1224 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1225 const gchar *who)
1227 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1229 if (sipe_private->dlx_uri) {
1230 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1232 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1233 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1235 mdd->other = g_strdup(who);
1236 mdd->max_returns = 1;
1237 mdd->callback = get_info_ab_entry_response;
1238 mdd->failed_callback = get_info_ab_entry_failed;
1239 mdd->session = sipe_svc_session_start();
1241 ms_dlx_webticket_request(sipe_private, mdd);
1243 } else {
1244 /* no [MS-DLX] server, use Active Directory search instead */
1245 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1246 "msRTCSIP-PrimaryUserAddress",
1247 who);
1248 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1250 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1251 row ? row : "");
1253 payload->destroy = g_free;
1254 payload->data = g_strdup(who);
1256 sip_soap_directory_search(sipe_private,
1258 row,
1259 process_get_info_response,
1260 payload);
1261 g_free(row);
1265 static void photo_response_data_free(struct photo_response_data *data)
1267 g_free(data->who);
1268 g_free(data->photo_hash);
1269 if (data->request) {
1270 sipe_http_request_cancel(data->request);
1272 g_free(data);
1275 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1276 guint status,
1277 GSList *headers,
1278 const char *body,
1279 gpointer data)
1281 struct photo_response_data *rdata = (struct photo_response_data *) data;
1283 rdata->request = NULL;
1285 if (status == SIPE_HTTP_STATUS_OK) {
1286 const gchar *len_str = sipe_utils_nameval_find(headers,
1287 "Content-Length");
1288 if (len_str) {
1289 gsize photo_size = atoi(len_str);
1290 gpointer photo = g_new(char, photo_size);
1292 if (photo) {
1293 memcpy(photo, body, photo_size);
1295 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1296 rdata->who,
1297 photo,
1298 photo_size,
1299 rdata->photo_hash);
1304 sipe_private->buddies->pending_photo_requests =
1305 g_slist_remove(sipe_private->buddies->pending_photo_requests, rdata);
1307 photo_response_data_free(rdata);
1310 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1312 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1313 gchar *wsse_security_base64;
1314 gchar *x_ms_webticket_header;
1316 if (!assertion) {
1317 return NULL;
1320 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1321 strlen(assertion));
1322 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1323 wsse_security_base64);
1325 g_free(assertion);
1326 g_free(wsse_security_base64);
1328 return x_ms_webticket_header;
1331 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1332 const gchar *uri,
1333 SIPE_UNUSED_PARAMETER const gchar *raw,
1334 sipe_xml *soap_body,
1335 gpointer callback_data)
1337 struct ms_dlx_data *mdd = callback_data;
1338 gchar *photo_rel_path = NULL;
1339 gchar *photo_hash = NULL;
1340 const gchar *photo_hash_old =
1341 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1343 if (soap_body) {
1344 const sipe_xml *node;
1346 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1347 uri);
1349 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1350 node;
1351 node = sipe_xml_twin(node)) {
1352 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1353 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1355 if (!is_empty(value)) {
1356 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1357 g_free(photo_rel_path);
1358 photo_rel_path = value;
1359 value = NULL;
1360 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1361 g_free(photo_hash);
1362 photo_hash = value;
1363 value = NULL;
1367 g_free(value);
1368 g_free(name);
1372 if (sipe_private->addressbook_uri && photo_rel_path &&
1373 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1374 gchar *photo_url = g_strdup_printf("%s/%s",
1375 sipe_private->addressbook_uri, photo_rel_path);
1376 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1378 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1379 data->who = g_strdup(mdd->other);
1380 data->photo_hash = photo_hash;
1381 photo_hash = NULL;
1383 data->request = sipe_http_request_get(sipe_private,
1384 photo_url,
1385 x_ms_webticket_header,
1386 process_buddy_photo_response,
1387 data);
1389 if (data->request) {
1390 sipe_private->buddies->pending_photo_requests =
1391 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1392 sipe_http_request_ready(data->request);
1393 } else {
1394 photo_response_data_free(data);
1397 g_free(x_ms_webticket_header);
1398 g_free(photo_url);
1401 g_free(photo_rel_path);
1402 g_free(photo_hash);
1403 ms_dlx_free(mdd);
1406 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1407 struct ms_dlx_data *mdd)
1409 ms_dlx_free(mdd);
1412 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1413 const gchar *uri)
1415 if (sipe_backend_uses_photo()) {
1417 /* Lync 2013 or newer: use UCS */
1418 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013)) {
1420 sipe_ucs_get_photo(sipe_private, uri);
1422 /* Lync 2010: use [MS-DLX] */
1423 } else if (sipe_private->dlx_uri &&
1424 sipe_private->addressbook_uri) {
1425 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1427 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1428 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1430 mdd->other = g_strdup(uri);
1431 mdd->max_returns = 1;
1432 mdd->callback = get_photo_ab_entry_response;
1433 mdd->failed_callback = get_photo_ab_entry_failed;
1434 mdd->session = sipe_svc_session_start();
1436 ms_dlx_webticket_request(sipe_private, mdd);
1441 static void buddy_refresh_photos_cb(gpointer uri,
1442 SIPE_UNUSED_PARAMETER gpointer value,
1443 gpointer sipe_private)
1445 buddy_fetch_photo(sipe_private, uri);
1448 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1450 g_hash_table_foreach(sipe_private->buddies->uri,
1451 buddy_refresh_photos_cb,
1452 sipe_private);
1455 /* Buddy menu callbacks*/
1457 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1458 const gchar *who)
1460 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1462 /* 2007+ conference */
1463 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1464 sipe_conf_add(sipe_private, who);
1466 /* 2005- multiparty chat */
1467 } else {
1468 gchar *self = sip_uri_self(sipe_private);
1469 struct sip_session *session;
1471 session = sipe_session_add_chat(sipe_private,
1472 NULL,
1473 TRUE,
1474 self);
1475 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1476 session->chat_session,
1477 session->chat_session->title,
1478 self);
1479 g_free(self);
1481 sipe_im_invite(sipe_private, session, who,
1482 NULL, NULL, NULL, FALSE);
1486 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1487 const gchar *who)
1489 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1490 who,
1491 NULL);
1492 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1493 buddy,
1494 SIPE_BUDDY_INFO_EMAIL);
1496 if (email) {
1497 gchar *command_line = g_strdup_printf(
1498 #ifdef _WIN32
1499 "cmd /c start"
1500 #else
1501 "xdg-email"
1502 #endif
1503 " mailto:%s", email);
1504 g_free(email);
1506 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1507 command_line);
1508 g_spawn_command_line_async(command_line, NULL);
1509 g_free(command_line);
1511 } else {
1512 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1513 who);
1517 /* Buddy menu */
1519 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1520 struct sipe_backend_buddy_menu *menu,
1521 sipe_backend_buddy buddy,
1522 sipe_buddy_info_fields id_phone,
1523 sipe_buddy_info_fields id_display,
1524 const gchar *type)
1526 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1527 buddy,
1528 id_phone);
1529 if (phone) {
1530 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1531 buddy,
1532 id_display);
1533 gchar *tmp = NULL;
1534 gchar *label = g_strdup_printf("%s %s",
1535 type,
1536 display ? display :
1537 (tmp = sip_tel_uri_denormalize(phone)));
1538 menu = sipe_backend_buddy_menu_add(sipe_public,
1539 menu,
1540 label,
1541 SIPE_BUDDY_MENU_MAKE_CALL,
1542 phone);
1543 g_free(tmp);
1544 g_free(label);
1545 g_free(display);
1546 g_free(phone);
1549 return(menu);
1552 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1553 const gchar *buddy_name,
1554 struct sipe_backend_buddy_menu *menu)
1556 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1557 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1558 buddy_name,
1559 NULL);
1560 gchar *self = sip_uri_self(sipe_private);
1562 SIPE_SESSION_FOREACH {
1563 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1565 struct sipe_chat_session *chat_session = session->chat_session;
1566 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1568 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1570 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1572 if (is_conf &&
1573 /* Not conf OP */
1574 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1575 /* We are a conf OP */
1576 conf_op) {
1577 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1578 chat_session->title);
1579 menu = sipe_backend_buddy_menu_add(sipe_public,
1580 menu,
1581 label,
1582 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1583 chat_session);
1584 g_free(label);
1587 if (is_conf &&
1588 /* We are a conf OP */
1589 conf_op) {
1590 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1591 chat_session->title);
1592 menu = sipe_backend_buddy_menu_add(sipe_public,
1593 menu,
1594 label,
1595 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1596 chat_session);
1597 g_free(label);
1600 else
1602 if (!is_conf ||
1603 (is_conf && !session->locked)) {
1604 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1605 chat_session->title);
1606 menu = sipe_backend_buddy_menu_add(sipe_public,
1607 menu,
1608 label,
1609 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1610 chat_session);
1611 g_free(label);
1615 } SIPE_SESSION_FOREACH_END;
1616 g_free(self);
1618 menu = sipe_backend_buddy_menu_add(sipe_public,
1619 menu,
1620 _("New chat"),
1621 SIPE_BUDDY_MENU_NEW_CHAT,
1622 NULL);
1624 /* add buddy's phone numbers if we have call control */
1625 if (sip_csta_is_idle(sipe_private)) {
1627 /* work phone */
1628 menu = buddy_menu_phone(sipe_public,
1629 menu,
1630 buddy,
1631 SIPE_BUDDY_INFO_WORK_PHONE,
1632 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1633 _("Work"));
1634 /* mobile phone */
1635 menu = buddy_menu_phone(sipe_public,
1636 menu,
1637 buddy,
1638 SIPE_BUDDY_INFO_MOBILE_PHONE,
1639 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1640 _("Mobile"));
1642 /* home phone */
1643 menu = buddy_menu_phone(sipe_public,
1644 menu,
1645 buddy,
1646 SIPE_BUDDY_INFO_HOME_PHONE,
1647 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1648 _("Home"));
1650 /* other phone */
1651 menu = buddy_menu_phone(sipe_public,
1652 menu,
1653 buddy,
1654 SIPE_BUDDY_INFO_OTHER_PHONE,
1655 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1656 _("Other"));
1658 /* custom1 phone */
1659 menu = buddy_menu_phone(sipe_public,
1660 menu,
1661 buddy,
1662 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1663 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1664 _("Custom1"));
1668 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1669 buddy,
1670 SIPE_BUDDY_INFO_EMAIL);
1671 if (email) {
1672 menu = sipe_backend_buddy_menu_add(sipe_public,
1673 menu,
1674 _("Send email..."),
1675 SIPE_BUDDY_MENU_SEND_EMAIL,
1676 NULL);
1677 g_free(email);
1681 /* access level control */
1682 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1683 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1684 menu,
1685 _("Access level"),
1686 sipe_ocs2007_access_control_menu(sipe_private,
1687 buddy_name));
1689 return(menu);
1692 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
1694 return(g_hash_table_size(sipe_private->buddies->uri));
1697 static guint sipe_ht_hash_nick(const char *nick)
1699 char *lc = g_utf8_strdown(nick, -1);
1700 guint bucket = g_str_hash(lc);
1701 g_free(lc);
1703 return bucket;
1706 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
1708 char *nick1_norm = NULL;
1709 char *nick2_norm = NULL;
1710 gboolean equal;
1712 if (nick1 == NULL && nick2 == NULL) return TRUE;
1713 if (nick1 == NULL || nick2 == NULL ||
1714 !g_utf8_validate(nick1, -1, NULL) ||
1715 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
1717 nick1_norm = g_utf8_casefold(nick1, -1);
1718 nick2_norm = g_utf8_casefold(nick2, -1);
1719 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
1720 g_free(nick2_norm);
1721 g_free(nick1_norm);
1723 return equal;
1726 void sipe_buddy_init(struct sipe_core_private *sipe_private)
1728 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
1729 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
1730 (GEqualFunc) sipe_ht_equals_nick);
1731 sipe_private->buddies = buddies;
1735 Local Variables:
1736 mode: c
1737 c-file-style: "bsd"
1738 indent-tabs-mode: t
1739 tab-width: 8
1740 End: