ucs: retrieve IM list after update trigger
[siplcs.git] / src / core / sipe-buddy.c
blobc740c89f9c1024e6cd752cfa9ebaa824a1553ced
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;
63 GHashTable *exchange_key;
65 /* Pending photo download HTTP requests */
66 GSList *pending_photo_requests;
69 struct photo_response_data {
70 gchar *who;
71 gchar *photo_hash;
72 struct sipe_http_request *request;
75 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
76 const gchar *uri);
77 static void photo_response_data_free(struct photo_response_data *data);
79 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
80 const gchar *uri,
81 const gchar *exchange_key,
82 const gchar *change_key)
84 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
85 gchar *normalized_uri = g_ascii_strdown(uri, -1);
86 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
87 normalized_uri);
89 if (!buddy) {
90 struct sipe_buddies *buddies = sipe_private->buddies;
92 buddy = g_new0(struct sipe_buddy, 1);
93 buddy->name = normalized_uri;
94 g_hash_table_insert(buddies->uri,
95 buddy->name,
96 buddy);
98 if (exchange_key) {
99 buddy->exchange_key = g_strdup(exchange_key);
100 g_hash_table_insert(buddies->exchange_key,
101 buddy->exchange_key,
102 buddy);
104 if (change_key)
105 buddy->change_key = g_strdup(change_key);
108 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri);
110 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
111 buddy->just_added = TRUE;
112 sipe_subscribe_presence_single_cb(sipe_private,
113 buddy->name);
116 buddy_fetch_photo(sipe_private, normalized_uri);
118 normalized_uri = NULL; /* buddy takes ownership */
119 } else {
120 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri);
122 g_free(normalized_uri);
124 return(buddy);
127 static gboolean is_buddy_in_group(struct sipe_buddy *buddy,
128 const gchar *name)
130 gboolean in_group = FALSE;
132 if (buddy) {
133 GSList *entry2 = buddy->groups;
135 while (entry2) {
136 struct sipe_group *group = entry2->data;
137 if (sipe_strequal(group->name, name)) {
138 in_group = TRUE;
139 break;
141 entry2 = entry2->next;
145 return(in_group);
148 void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private,
149 struct sipe_buddy *buddy,
150 struct sipe_group *group,
151 const gchar *alias)
153 const gchar *uri = buddy->name;
154 const gchar *group_name = group->name;
155 sipe_backend_buddy bb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
156 uri,
157 group_name);
159 if (!bb) {
160 bb = sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
161 uri,
162 alias,
163 group_name);
164 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
165 uri, alias ? alias : "<NONE>");
169 if (!is_empty(alias)) {
170 gchar *old_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
171 bb);
173 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri),
174 old_alias)) {
175 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
177 alias);
178 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
179 uri, old_alias, alias);
181 g_free(old_alias);
184 if (!is_buddy_in_group(buddy, group_name)) {
185 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
186 group,
187 (GCompareFunc) sipe_group_compare,
188 NULL);
189 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
190 uri, group_name);
194 void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private)
196 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
197 NULL,
198 NULL);
199 GSList *entry = buddies;
201 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
202 g_slist_length(buddies));
203 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
204 sipe_buddy_count(sipe_private));
205 while (entry) {
206 sipe_backend_buddy bb = entry->data;
207 gchar *bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC,
208 bb);
209 gchar *gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC,
210 bb);
211 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
212 bname);
214 if (!is_buddy_in_group(buddy, gname)) {
215 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: REMOVING '%s' from local group '%s', as buddy is not in that group on remote contact list",
216 bname, gname);
217 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, bb);
220 g_free(gname);
221 g_free(bname);
223 entry = entry->next;
226 g_slist_free(buddies);
229 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
230 const gchar *uri)
232 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
235 struct sipe_buddy *sipe_buddy_find_by_exchange_key(struct sipe_core_private *sipe_private,
236 const gchar *exchange_key)
238 return(g_hash_table_lookup(sipe_private->buddies->exchange_key,
239 exchange_key));
242 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
243 GHFunc callback,
244 gpointer callback_data)
246 g_hash_table_foreach(sipe_private->buddies->uri,
247 callback,
248 callback_data);
251 static void buddy_free(struct sipe_buddy *buddy)
253 #ifndef _WIN32
255 * We are calling g_hash_table_foreach_steal(). That means that no
256 * key/value deallocation functions are called. Therefore the glib
257 * hash code does not touch the key (buddy->name) or value (buddy)
258 * of the to-be-deleted hash node at all. It follows that we
260 * - MUST free the memory for the key ourselves and
261 * - ARE allowed to do it in this function
263 * Conclusion: glib must be broken on the Windows platform if sipe
264 * crashes with SIGTRAP when closing. You'll have to live
265 * with the memory leak until this is fixed.
267 g_free(buddy->name);
268 #endif
269 g_free(buddy->exchange_key);
270 g_free(buddy->change_key);
271 g_free(buddy->activity);
272 g_free(buddy->meeting_subject);
273 g_free(buddy->meeting_location);
274 g_free(buddy->note);
276 g_free(buddy->cal_start_time);
277 g_free(buddy->cal_free_busy_base64);
278 g_free(buddy->cal_free_busy);
279 g_free(buddy->last_non_cal_activity);
281 sipe_cal_free_working_hours(buddy->cal_working_hours);
283 g_free(buddy->device_name);
284 g_slist_free(buddy->groups);
285 g_free(buddy);
288 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
289 gpointer buddy,
290 SIPE_UNUSED_PARAMETER gpointer user_data)
292 buddy_free(buddy);
293 /* We must return TRUE as the key/value have already been deleted */
294 return(TRUE);
297 void sipe_buddy_free(struct sipe_core_private *sipe_private)
299 struct sipe_buddies *buddies = sipe_private->buddies;
301 g_hash_table_foreach_steal(buddies->uri,
302 buddy_free_cb,
303 NULL);
305 /* core is being deallocated, remove all its pending photo requests */
306 while (buddies->pending_photo_requests) {
307 struct photo_response_data *data =
308 buddies->pending_photo_requests->data;
309 buddies->pending_photo_requests =
310 g_slist_remove(buddies->pending_photo_requests, data);
311 photo_response_data_free(data);
314 g_hash_table_destroy(buddies->uri);
315 g_hash_table_destroy(buddies->exchange_key);
316 g_free(buddies);
317 sipe_private->buddies = NULL;
320 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
321 const gchar *uri,
322 guint activity,
323 const gchar *status_text)
325 struct sipe_buddy *sbuddy;
326 const char *activity_str;
328 if (!sipe_public) return NULL; /* happens on pidgin exit */
330 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
331 if (!sbuddy) return NULL;
333 activity_str = sbuddy->activity ? sbuddy->activity :
334 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
335 status_text : NULL;
337 if (activity_str && sbuddy->note) {
338 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
339 } else if (activity_str) {
340 return g_strdup(activity_str);
341 } else if (sbuddy->note) {
342 return g_strdup_printf("<i>%s</i>", sbuddy->note);
343 } else {
344 return NULL;
348 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
349 const gchar *with)
351 sipe_backend_buddy pbuddy;
352 gchar *alias = NULL;
353 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
354 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
356 return alias;
359 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
360 const gchar *who,
361 const gchar *old_group_name,
362 const gchar *new_group_name)
364 struct sipe_buddy * buddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE,
365 who);
366 struct sipe_group * old_group = NULL;
367 struct sipe_group * new_group;
369 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
370 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
372 if(!buddy) { // buddy not in roaming list
373 return;
376 if (old_group_name) {
377 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
379 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
381 if (old_group) {
382 buddy->groups = g_slist_remove(buddy->groups, old_group);
383 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
386 if (!new_group) {
387 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
388 } else {
389 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
390 new_group,
391 (GCompareFunc)sipe_group_compare,
392 NULL);
393 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
397 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
398 const gchar *uri,
399 const gchar *group_name)
401 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
403 if (!sipe_buddy_find_by_uri(sipe_private, uri))
404 sipe_buddy_add(sipe_private,
405 uri,
406 NULL,
407 NULL);
408 else
409 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
410 uri);
412 sipe_core_buddy_group(sipe_public,
413 uri,
414 NULL,
415 group_name);
418 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
419 struct sipe_buddy *buddy)
421 struct sipe_buddies *buddies = sipe_private->buddies;
422 gchar *action_name = sipe_utils_presence_key(buddy->name);
423 sipe_schedule_cancel(sipe_private, action_name);
424 g_free(action_name);
426 g_hash_table_remove(buddies->uri, buddy->name);
427 if (buddy->exchange_key)
428 g_hash_table_remove(buddies->exchange_key,
429 buddy->exchange_key);
431 buddy_free(buddy);
435 * Unassociates buddy from group first.
436 * Then see if no groups left, removes buddy completely.
437 * Otherwise updates buddy groups on server.
439 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
440 const gchar *uri,
441 const gchar *group_name)
443 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
444 struct sipe_buddy *b = sipe_buddy_find_by_uri(sipe_private,
445 uri);
446 struct sipe_group *g = NULL;
448 if (!b) return;
450 if (group_name) {
451 g = sipe_group_find_by_name(sipe_private, group_name);
452 if (g) {
453 b->groups = g_slist_remove(b->groups, g);
454 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
455 uri, g->name);
459 if (g_slist_length(b->groups) < 1) {
461 if (sipe_ucs_is_migrated(sipe_private)) {
462 sipe_ucs_group_remove_buddy(sipe_private,
465 } else {
466 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
467 b->name);
468 sip_soap_request(sipe_private,
469 "deleteContact",
470 request);
471 g_free(request);
474 sipe_buddy_remove(sipe_private, b);
475 } else {
476 if (sipe_ucs_is_migrated(sipe_private)) {
477 sipe_ucs_group_remove_buddy(sipe_private,
480 } else
481 /* updates groups on server */
482 sipe_group_update_buddy(sipe_private, b);
486 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
487 const gchar *uri,
488 guint activity)
490 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
491 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
492 uri);
494 if (!sbuddy) return;
496 /* Check if on 2005 system contact's calendar,
497 * then set/preserve it.
499 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
500 sipe_backend_buddy_set_status(sipe_public, uri, activity);
501 } else {
502 sipe_ocs2005_apply_calendar_status(sipe_private,
503 sbuddy,
504 sipe_status_activity_to_token(activity));
508 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
509 const gchar *uri,
510 const gchar *status_name,
511 gboolean is_online,
512 struct sipe_backend_buddy_tooltip *tooltip)
514 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
515 gchar *note = NULL;
516 gboolean is_oof_note = FALSE;
517 const gchar *activity = NULL;
518 gchar *calendar = NULL;
519 const gchar *meeting_subject = NULL;
520 const gchar *meeting_location = NULL;
521 gchar *access_text = NULL;
523 #define SIPE_ADD_BUDDY_INFO(l, t) \
525 gchar *tmp = g_markup_escape_text((t), -1); \
526 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
527 g_free(tmp); \
529 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
530 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
532 if (sipe_public) { /* happens on pidgin exit */
533 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
534 uri);
535 if (sbuddy) {
536 note = sbuddy->note;
537 is_oof_note = sbuddy->is_oof_note;
538 activity = sbuddy->activity;
539 calendar = sipe_cal_get_description(sbuddy);
540 meeting_subject = sbuddy->meeting_subject;
541 meeting_location = sbuddy->meeting_location;
543 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
544 gboolean is_group_access = FALSE;
545 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
546 "user",
547 sipe_get_no_sip_uri(uri),
548 &is_group_access);
549 const char *access_level = sipe_ocs2007_access_level_name(container_id);
550 access_text = is_group_access ?
551 g_strdup(access_level) :
552 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
553 access_level);
557 if (is_online) {
558 const gchar *status_str = activity ? activity : status_name;
560 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
562 if (is_online && !is_empty(calendar)) {
563 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
565 g_free(calendar);
566 if (!is_empty(meeting_location)) {
567 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
568 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
570 if (!is_empty(meeting_subject)) {
571 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
572 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
574 if (note) {
575 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
576 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
577 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
578 note_italics);
579 g_free(note_italics);
581 if (access_text) {
582 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
583 g_free(access_text);
587 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
588 const char *uri,
589 sipe_buddy_info_fields propkey,
590 char *property_value)
592 GSList *buddies, *entry;
594 if (property_value)
595 property_value = g_strstrip(property_value);
597 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
598 while (entry) {
599 gchar *prop_str;
600 sipe_backend_buddy p_buddy = entry->data;
602 /* for Display Name */
603 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
604 gchar *alias;
605 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
606 if (property_value && sipe_is_bad_alias(uri, alias)) {
607 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
608 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
610 g_free(alias);
612 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
613 if (!is_empty(property_value) &&
614 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
616 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
617 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
619 g_free(alias);
621 /* for other properties */
622 else {
623 if (!is_empty(property_value)) {
624 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
625 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
626 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
628 g_free(prop_str);
632 entry = entry->next;
634 g_slist_free(buddies);
638 struct ms_dlx_data;
639 struct ms_dlx_data {
640 GSList *search_rows;
641 gchar *other;
642 guint max_returns;
643 sipe_svc_callback *callback;
644 struct sipe_svc_session *session;
645 gchar *wsse_security;
646 struct sipe_backend_search_token *token;
647 /* must call ms_dlx_free() */
648 void (*failed_callback)(struct sipe_core_private *sipe_private,
649 struct ms_dlx_data *mdd);
652 static void ms_dlx_free(struct ms_dlx_data *mdd)
654 sipe_utils_slist_free_full(mdd->search_rows, g_free);
655 sipe_svc_session_close(mdd->session);
656 g_free(mdd->other);
657 g_free(mdd->wsse_security);
658 g_free(mdd);
661 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
662 #define DLX_SEARCH_ITEM \
663 "<AbEntryRequest.ChangeSearchQuery>" \
664 " <SearchOn>%s</SearchOn>" \
665 " <Value>%s</Value>" \
666 "</AbEntryRequest.ChangeSearchQuery>"
668 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
669 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
670 guint i = 0;
671 gchar *query = NULL;
673 while (query_rows) {
674 gchar *attr;
675 gchar *value;
677 attr = query_rows->data;
678 query_rows = g_slist_next(query_rows);
679 value = query_rows->data;
680 query_rows = g_slist_next(query_rows);
682 if (!attr || !value)
683 break;
685 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
686 attr, value);
688 attrs[i] = NULL;
690 if (i) {
691 query = g_strjoinv(NULL, attrs);
692 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
693 query ? query : "");
696 g_strfreev(attrs);
698 return query;
701 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
702 const gchar *base_uri,
703 const gchar *auth_uri,
704 const gchar *wsse_security,
705 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
706 gpointer callback_data)
708 struct ms_dlx_data *mdd = callback_data;
710 if (wsse_security) {
711 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
713 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
714 base_uri);
716 if (sipe_svc_ab_entry_request(sipe_private,
717 mdd->session,
718 auth_uri,
719 wsse_security,
720 query,
721 g_slist_length(mdd->search_rows) / 2,
722 mdd->max_returns,
723 mdd->callback,
724 mdd)) {
726 /* keep webticket security token for potential further use */
727 mdd->wsse_security = g_strdup(wsse_security);
729 /* callback data passed down the line */
730 mdd = NULL;
732 g_free(query);
734 } else {
735 /* no ticket: this will show the minmum information */
736 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
737 base_uri);
740 if (mdd)
741 mdd->failed_callback(sipe_private, mdd);
744 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
745 struct ms_dlx_data *mdd)
747 if (!sipe_webticket_request(sipe_private,
748 mdd->session,
749 sipe_private->dlx_uri,
750 "AddressBookWebTicketBearer",
751 ms_dlx_webticket,
752 mdd)) {
753 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
754 sipe_private->dlx_uri);
755 mdd->failed_callback(sipe_private, mdd);
759 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
760 struct sipe_backend_search_results *results,
761 guint match_count,
762 gboolean more)
764 gchar *secondary = g_strdup_printf(
765 dngettext(PACKAGE_NAME,
766 "Found %d contact%s:",
767 "Found %d contacts%s:", match_count),
768 match_count, more ? _(" (more matched your query)") : "");
770 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
771 results,
772 secondary,
773 more);
774 g_free(secondary);
777 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
778 const gchar *uri,
779 SIPE_UNUSED_PARAMETER const gchar *raw,
780 sipe_xml *soap_body,
781 gpointer callback_data)
783 struct ms_dlx_data *mdd = callback_data;
785 if (soap_body) {
786 const sipe_xml *node;
787 struct sipe_backend_search_results *results;
788 GHashTable *found;
790 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
791 uri);
793 /* any matches? */
794 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
795 if (!node) {
796 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
797 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
798 mdd->token,
799 _("No contacts found"));
800 ms_dlx_free(mdd);
801 return;
804 /* OK, we found something - show the results to the user */
805 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
806 mdd->token);
807 if (!results) {
808 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
809 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
810 mdd->token,
811 _("Unable to display the search results"));
812 ms_dlx_free(mdd);
813 return;
816 /* SearchAbEntryResult can contain duplicates */
817 found = g_hash_table_new_full(g_str_hash, g_str_equal,
818 g_free, NULL);
820 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
821 const sipe_xml *attrs;
822 gchar *sip_uri = NULL;
823 gchar *displayname = NULL;
824 gchar *company = NULL;
825 gchar *country = NULL;
826 gchar *email = NULL;
828 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
829 attrs;
830 attrs = sipe_xml_twin(attrs)) {
831 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
832 "Name"));
833 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
834 "Value"));
836 if (!is_empty(value)) {
837 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
838 g_free(sip_uri);
839 sip_uri = value;
840 value = NULL;
841 } else if (sipe_strcase_equal(name, "displayname")) {
842 g_free(displayname);
843 displayname = value;
844 value = NULL;
845 } else if (sipe_strcase_equal(name, "mail")) {
846 g_free(email);
847 email = value;
848 value = NULL;
849 } else if (sipe_strcase_equal(name, "company")) {
850 g_free(company);
851 company = value;
852 value = NULL;
853 } else if (sipe_strcase_equal(name, "country")) {
854 g_free(country);
855 country = value;
856 value = NULL;
860 g_free(value);
861 g_free(name);
864 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
865 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
866 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
867 results,
868 uri_parts[1],
869 displayname,
870 company,
871 country,
872 email);
873 g_strfreev(uri_parts);
875 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
876 sip_uri = NULL;
879 g_free(email);
880 g_free(country);
881 g_free(company);
882 g_free(displayname);
883 g_free(sip_uri);
886 search_contacts_finalize(sipe_private, results,
887 g_hash_table_size(found),
888 FALSE);
889 g_hash_table_destroy(found);
890 ms_dlx_free(mdd);
892 } else {
893 mdd->failed_callback(sipe_private, mdd);
897 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
898 struct sipmsg *msg,
899 struct transaction *trans)
901 struct sipe_backend_search_token *token = trans->payload->data;
902 struct sipe_backend_search_results *results;
903 sipe_xml *searchResults;
904 const sipe_xml *mrow;
905 guint match_count = 0;
906 gboolean more = FALSE;
908 /* valid response? */
909 if (msg->response != 200) {
910 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
911 msg->response);
912 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
913 token,
914 _("Contact search failed"));
915 return(FALSE);
918 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
920 /* valid XML? */
921 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
922 if (!searchResults) {
923 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
924 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
925 token,
926 _("Contact search failed"));
927 return(FALSE);
930 /* any matches? */
931 mrow = sipe_xml_child(searchResults, "Body/Array/row");
932 if (!mrow) {
933 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
934 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
935 token,
936 _("No contacts found"));
938 sipe_xml_free(searchResults);
939 return(FALSE);
942 /* OK, we found something - show the results to the user */
943 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
944 trans->payload->data);
945 if (!results) {
946 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
947 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
948 token,
949 _("Unable to display the search results"));
951 sipe_xml_free(searchResults);
952 return FALSE;
955 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
956 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
957 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
958 results,
959 uri_parts[1],
960 sipe_xml_attribute(mrow, "displayName"),
961 sipe_xml_attribute(mrow, "company"),
962 sipe_xml_attribute(mrow, "country"),
963 sipe_xml_attribute(mrow, "email"));
964 g_strfreev(uri_parts);
965 match_count++;
968 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
969 char *data = sipe_xml_data(mrow);
970 more = (g_ascii_strcasecmp(data, "true") == 0);
971 g_free(data);
974 search_contacts_finalize(sipe_private, results, match_count, more);
975 sipe_xml_free(searchResults);
977 return(TRUE);
980 static void search_soap_request(struct sipe_core_private *sipe_private,
981 struct sipe_backend_search_token *token,
982 GSList *search_rows)
984 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
985 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
987 payload->data = token;
989 sip_soap_directory_search(sipe_private,
990 100,
991 query,
992 process_search_contact_response,
993 payload);
994 g_free(query);
997 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
998 struct ms_dlx_data *mdd)
1000 /* error using [MS-DLX] server, retry using Active Directory */
1001 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
1002 ms_dlx_free(mdd);
1005 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
1006 struct sipe_backend_search_token *token,
1007 const gchar *given_name,
1008 const gchar *surname,
1009 const gchar *email,
1010 const gchar *company,
1011 const gchar *country)
1013 GSList *query_rows = NULL;
1015 #define ADD_QUERY_ROW(attr, val) \
1016 if (val) { \
1017 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1018 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1021 ADD_QUERY_ROW("givenName", given_name);
1022 ADD_QUERY_ROW("sn", surname);
1023 ADD_QUERY_ROW("mail", email);
1024 ADD_QUERY_ROW("company", company);
1025 ADD_QUERY_ROW("c", country);
1027 if (query_rows) {
1028 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
1029 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1031 mdd->search_rows = query_rows;
1032 mdd->max_returns = 100;
1033 mdd->callback = search_ab_entry_response;
1034 mdd->failed_callback = search_ab_entry_failed;
1035 mdd->session = sipe_svc_session_start();
1036 mdd->token = token;
1038 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
1040 } else {
1041 /* no [MS-DLX] server, use Active Directory search instead */
1042 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
1043 sipe_utils_slist_free_full(query_rows, g_free);
1045 } else
1046 sipe_backend_search_failed(sipe_public,
1047 token,
1048 _("Invalid contact search query"));
1051 static void get_info_finalize(struct sipe_core_private *sipe_private,
1052 struct sipe_backend_buddy_info *info,
1053 const gchar *uri,
1054 const gchar *server_alias,
1055 const gchar *email)
1057 sipe_backend_buddy bbuddy;
1058 struct sipe_buddy *sbuddy;
1059 gchar *alias;
1060 gchar *value;
1062 if (!info) {
1063 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1064 } else {
1065 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
1067 if (!info)
1068 return;
1070 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
1072 if (is_empty(server_alias)) {
1073 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
1074 bbuddy);
1075 if (value) {
1076 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1077 info,
1078 SIPE_BUDDY_INFO_DISPLAY_NAME,
1079 value);
1081 } else {
1082 value = g_strdup(server_alias);
1085 /* present alias if it differs from server alias */
1086 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
1087 if (alias && !sipe_strequal(alias, value))
1089 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1090 info,
1091 SIPE_BUDDY_INFO_ALIAS,
1092 alias);
1094 g_free(alias);
1095 g_free(value);
1097 if (is_empty(email)) {
1098 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1099 bbuddy,
1100 SIPE_BUDDY_INFO_EMAIL);
1101 if (value) {
1102 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1103 info,
1104 SIPE_BUDDY_INFO_EMAIL,
1105 value);
1106 g_free(value);
1110 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1111 bbuddy,
1112 SIPE_BUDDY_INFO_SITE);
1113 if (value) {
1114 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1115 info,
1116 SIPE_BUDDY_INFO_SITE,
1117 value);
1118 g_free(value);
1121 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
1122 if (sbuddy && sbuddy->device_name) {
1123 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1124 info,
1125 SIPE_BUDDY_INFO_DEVICE,
1126 sbuddy->device_name);
1129 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
1133 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
1134 const gchar *uri,
1135 SIPE_UNUSED_PARAMETER const gchar *raw,
1136 sipe_xml *soap_body,
1137 gpointer callback_data)
1139 struct ms_dlx_data *mdd = callback_data;
1140 struct sipe_backend_buddy_info *info = NULL;
1141 gchar *server_alias = NULL;
1142 gchar *email = NULL;
1144 if (soap_body) {
1145 const sipe_xml *node;
1147 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1148 uri);
1150 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1152 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1153 node;
1154 node = sipe_xml_twin(node)) {
1155 gchar *name = sipe_xml_data(sipe_xml_child(node,
1156 "Name"));
1157 gchar *value = sipe_xml_data(sipe_xml_child(node,
1158 "Value"));
1159 const sipe_xml *values = sipe_xml_child(node,
1160 "Values");
1162 /* Single value entries */
1163 if (!is_empty(value)) {
1165 if (sipe_strcase_equal(name, "displayname")) {
1166 g_free(server_alias);
1167 server_alias = value;
1168 value = NULL;
1169 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1170 info,
1171 SIPE_BUDDY_INFO_DISPLAY_NAME,
1172 server_alias);
1173 } else if (sipe_strcase_equal(name, "mail")) {
1174 g_free(email);
1175 email = value;
1176 value = NULL;
1177 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1178 info,
1179 SIPE_BUDDY_INFO_EMAIL,
1180 email);
1181 } else if (sipe_strcase_equal(name, "title")) {
1182 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1183 info,
1184 SIPE_BUDDY_INFO_JOB_TITLE,
1185 value);
1186 } else if (sipe_strcase_equal(name, "company")) {
1187 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1188 info,
1189 SIPE_BUDDY_INFO_COMPANY,
1190 value);
1191 } else if (sipe_strcase_equal(name, "country")) {
1192 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1193 info,
1194 SIPE_BUDDY_INFO_COUNTRY,
1195 value);
1198 } else if (values) {
1199 gchar *first = sipe_xml_data(sipe_xml_child(values,
1200 "string"));
1202 if (sipe_strcase_equal(name, "telephonenumber")) {
1203 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1204 info,
1205 SIPE_BUDDY_INFO_WORK_PHONE,
1206 first);
1209 g_free(first);
1212 g_free(value);
1213 g_free(name);
1217 /* this will show the minmum information */
1218 get_info_finalize(sipe_private,
1219 info,
1220 mdd->other,
1221 server_alias,
1222 email);
1224 g_free(email);
1225 g_free(server_alias);
1226 ms_dlx_free(mdd);
1229 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1230 struct sipmsg *msg,
1231 struct transaction *trans)
1233 const gchar *uri = trans->payload->data;
1234 struct sipe_backend_buddy_info *info = NULL;
1235 gchar *server_alias = NULL;
1236 gchar *email = NULL;
1238 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1239 uri, sipe_private->username);
1241 if (msg->response != 200) {
1242 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1243 } else {
1244 sipe_xml *searchResults;
1245 const sipe_xml *mrow;
1247 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1248 msg->body ? msg->body : "");
1250 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1251 if (!searchResults) {
1253 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1255 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1256 const gchar *value;
1257 gchar *phone_number;
1259 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1261 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1262 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1263 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1266 * For 2007 system we will take this from ContactCard -
1267 * it has cleaner tel: URIs at least
1269 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1270 char *tel_uri = sip_to_tel_uri(phone_number);
1271 /* trims its parameters, so call first */
1272 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1273 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1274 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1275 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1276 g_free(tel_uri);
1278 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1279 uri);
1282 if (!is_empty(server_alias)) {
1283 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1284 info,
1285 SIPE_BUDDY_INFO_DISPLAY_NAME,
1286 server_alias);
1288 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1289 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1290 info,
1291 SIPE_BUDDY_INFO_JOB_TITLE,
1292 value);
1294 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1295 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1296 info,
1297 SIPE_BUDDY_INFO_OFFICE,
1298 value);
1300 if (!is_empty(phone_number)) {
1301 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1302 info,
1303 SIPE_BUDDY_INFO_WORK_PHONE,
1304 phone_number);
1306 g_free(phone_number);
1307 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1308 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1309 info,
1310 SIPE_BUDDY_INFO_COMPANY,
1311 value);
1313 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1314 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1315 info,
1316 SIPE_BUDDY_INFO_CITY,
1317 value);
1319 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1320 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1321 info,
1322 SIPE_BUDDY_INFO_STATE,
1323 value);
1325 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1326 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1327 info,
1328 SIPE_BUDDY_INFO_COUNTRY,
1329 value);
1331 if (!is_empty(email)) {
1332 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1333 info,
1334 SIPE_BUDDY_INFO_EMAIL,
1335 email);
1338 sipe_xml_free(searchResults);
1341 /* this will show the minmum information */
1342 get_info_finalize(sipe_private,
1343 info,
1344 uri,
1345 server_alias,
1346 email);
1348 g_free(server_alias);
1349 g_free(email);
1351 return TRUE;
1354 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1355 struct ms_dlx_data *mdd)
1357 /* error using [MS-DLX] server, retry using Active Directory */
1358 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1359 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1361 payload->destroy = g_free;
1362 payload->data = mdd->other;
1363 mdd->other = NULL;
1365 sip_soap_directory_search(sipe_private,
1367 query,
1368 process_get_info_response,
1369 payload);
1371 ms_dlx_free(mdd);
1372 g_free(query);
1375 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1376 const gchar *who)
1378 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1380 if (sipe_private->dlx_uri) {
1381 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1383 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1384 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1386 mdd->other = g_strdup(who);
1387 mdd->max_returns = 1;
1388 mdd->callback = get_info_ab_entry_response;
1389 mdd->failed_callback = get_info_ab_entry_failed;
1390 mdd->session = sipe_svc_session_start();
1392 ms_dlx_webticket_request(sipe_private, mdd);
1394 } else {
1395 /* no [MS-DLX] server, use Active Directory search instead */
1396 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1397 "msRTCSIP-PrimaryUserAddress",
1398 who);
1399 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1401 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1402 row ? row : "");
1404 payload->destroy = g_free;
1405 payload->data = g_strdup(who);
1407 sip_soap_directory_search(sipe_private,
1409 row,
1410 process_get_info_response,
1411 payload);
1412 g_free(row);
1416 static void photo_response_data_free(struct photo_response_data *data)
1418 g_free(data->who);
1419 g_free(data->photo_hash);
1420 if (data->request) {
1421 sipe_http_request_cancel(data->request);
1423 g_free(data);
1426 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1427 guint status,
1428 GSList *headers,
1429 const char *body,
1430 gpointer data)
1432 struct photo_response_data *rdata = (struct photo_response_data *) data;
1434 rdata->request = NULL;
1436 if (status == SIPE_HTTP_STATUS_OK) {
1437 const gchar *len_str = sipe_utils_nameval_find(headers,
1438 "Content-Length");
1439 if (len_str) {
1440 gsize photo_size = atoi(len_str);
1441 gpointer photo = g_new(char, photo_size);
1443 if (photo) {
1444 memcpy(photo, body, photo_size);
1446 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1447 rdata->who,
1448 photo,
1449 photo_size,
1450 rdata->photo_hash);
1455 sipe_private->buddies->pending_photo_requests =
1456 g_slist_remove(sipe_private->buddies->pending_photo_requests, rdata);
1458 photo_response_data_free(rdata);
1461 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1463 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1464 gchar *wsse_security_base64;
1465 gchar *x_ms_webticket_header;
1467 if (!assertion) {
1468 return NULL;
1471 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1472 strlen(assertion));
1473 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1474 wsse_security_base64);
1476 g_free(assertion);
1477 g_free(wsse_security_base64);
1479 return x_ms_webticket_header;
1482 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1483 const gchar *uri,
1484 SIPE_UNUSED_PARAMETER const gchar *raw,
1485 sipe_xml *soap_body,
1486 gpointer callback_data)
1488 struct ms_dlx_data *mdd = callback_data;
1489 gchar *photo_rel_path = NULL;
1490 gchar *photo_hash = NULL;
1491 const gchar *photo_hash_old =
1492 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1494 if (soap_body) {
1495 const sipe_xml *node;
1497 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1498 uri);
1500 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1501 node;
1502 node = sipe_xml_twin(node)) {
1503 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1504 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1506 if (!is_empty(value)) {
1507 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1508 g_free(photo_rel_path);
1509 photo_rel_path = value;
1510 value = NULL;
1511 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1512 g_free(photo_hash);
1513 photo_hash = value;
1514 value = NULL;
1518 g_free(value);
1519 g_free(name);
1523 if (sipe_private->addressbook_uri && photo_rel_path &&
1524 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1525 gchar *photo_url = g_strdup_printf("%s/%s",
1526 sipe_private->addressbook_uri, photo_rel_path);
1527 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1529 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1530 data->who = g_strdup(mdd->other);
1531 data->photo_hash = photo_hash;
1532 photo_hash = NULL;
1534 data->request = sipe_http_request_get(sipe_private,
1535 photo_url,
1536 x_ms_webticket_header,
1537 process_buddy_photo_response,
1538 data);
1540 if (data->request) {
1541 sipe_private->buddies->pending_photo_requests =
1542 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1543 sipe_http_request_ready(data->request);
1544 } else {
1545 photo_response_data_free(data);
1548 g_free(x_ms_webticket_header);
1549 g_free(photo_url);
1552 g_free(photo_rel_path);
1553 g_free(photo_hash);
1554 ms_dlx_free(mdd);
1557 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1558 struct ms_dlx_data *mdd)
1560 ms_dlx_free(mdd);
1563 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1564 const gchar *uri)
1566 if (sipe_backend_uses_photo()) {
1568 /* Lync 2013 or newer: use UCS */
1569 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013)) {
1571 sipe_ucs_get_photo(sipe_private, uri);
1573 /* Lync 2010: use [MS-DLX] */
1574 } else if (sipe_private->dlx_uri &&
1575 sipe_private->addressbook_uri) {
1576 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1578 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1579 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1581 mdd->other = g_strdup(uri);
1582 mdd->max_returns = 1;
1583 mdd->callback = get_photo_ab_entry_response;
1584 mdd->failed_callback = get_photo_ab_entry_failed;
1585 mdd->session = sipe_svc_session_start();
1587 ms_dlx_webticket_request(sipe_private, mdd);
1592 static void buddy_refresh_photos_cb(gpointer uri,
1593 SIPE_UNUSED_PARAMETER gpointer value,
1594 gpointer sipe_private)
1596 buddy_fetch_photo(sipe_private, uri);
1599 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1601 g_hash_table_foreach(sipe_private->buddies->uri,
1602 buddy_refresh_photos_cb,
1603 sipe_private);
1606 /* Buddy menu callbacks*/
1608 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1609 const gchar *who)
1611 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1613 /* 2007+ conference */
1614 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1615 sipe_conf_add(sipe_private, who);
1617 /* 2005- multiparty chat */
1618 } else {
1619 gchar *self = sip_uri_self(sipe_private);
1620 struct sip_session *session;
1622 session = sipe_session_add_chat(sipe_private,
1623 NULL,
1624 TRUE,
1625 self);
1626 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1627 session->chat_session,
1628 session->chat_session->title,
1629 self);
1630 g_free(self);
1632 sipe_im_invite(sipe_private, session, who,
1633 NULL, NULL, NULL, FALSE);
1637 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1638 const gchar *who)
1640 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1641 who,
1642 NULL);
1643 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1644 buddy,
1645 SIPE_BUDDY_INFO_EMAIL);
1647 if (email) {
1648 gchar *command_line = g_strdup_printf(
1649 #ifdef _WIN32
1650 "cmd /c start"
1651 #else
1652 "xdg-email"
1653 #endif
1654 " mailto:%s", email);
1655 g_free(email);
1657 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1658 command_line);
1659 g_spawn_command_line_async(command_line, NULL);
1660 g_free(command_line);
1662 } else {
1663 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1664 who);
1668 /* Buddy menu */
1670 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1671 struct sipe_backend_buddy_menu *menu,
1672 sipe_backend_buddy buddy,
1673 sipe_buddy_info_fields id_phone,
1674 sipe_buddy_info_fields id_display,
1675 const gchar *type)
1677 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1678 buddy,
1679 id_phone);
1680 if (phone) {
1681 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1682 buddy,
1683 id_display);
1684 gchar *tmp = NULL;
1685 gchar *label = g_strdup_printf("%s %s",
1686 type,
1687 display ? display :
1688 (tmp = sip_tel_uri_denormalize(phone)));
1689 menu = sipe_backend_buddy_menu_add(sipe_public,
1690 menu,
1691 label,
1692 SIPE_BUDDY_MENU_MAKE_CALL,
1693 phone);
1694 g_free(tmp);
1695 g_free(label);
1696 g_free(display);
1697 g_free(phone);
1700 return(menu);
1703 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1704 const gchar *buddy_name,
1705 struct sipe_backend_buddy_menu *menu)
1707 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1708 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1709 buddy_name,
1710 NULL);
1711 gchar *self = sip_uri_self(sipe_private);
1713 SIPE_SESSION_FOREACH {
1714 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1716 struct sipe_chat_session *chat_session = session->chat_session;
1717 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1719 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1721 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1723 if (is_conf &&
1724 /* Not conf OP */
1725 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1726 /* We are a conf OP */
1727 conf_op) {
1728 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1729 chat_session->title);
1730 menu = sipe_backend_buddy_menu_add(sipe_public,
1731 menu,
1732 label,
1733 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1734 chat_session);
1735 g_free(label);
1738 if (is_conf &&
1739 /* We are a conf OP */
1740 conf_op) {
1741 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1742 chat_session->title);
1743 menu = sipe_backend_buddy_menu_add(sipe_public,
1744 menu,
1745 label,
1746 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1747 chat_session);
1748 g_free(label);
1751 else
1753 if (!is_conf ||
1754 (is_conf && !session->locked)) {
1755 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1756 chat_session->title);
1757 menu = sipe_backend_buddy_menu_add(sipe_public,
1758 menu,
1759 label,
1760 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1761 chat_session);
1762 g_free(label);
1766 } SIPE_SESSION_FOREACH_END;
1767 g_free(self);
1769 menu = sipe_backend_buddy_menu_add(sipe_public,
1770 menu,
1771 _("New chat"),
1772 SIPE_BUDDY_MENU_NEW_CHAT,
1773 NULL);
1775 /* add buddy's phone numbers if we have call control */
1776 if (sip_csta_is_idle(sipe_private)) {
1778 /* work phone */
1779 menu = buddy_menu_phone(sipe_public,
1780 menu,
1781 buddy,
1782 SIPE_BUDDY_INFO_WORK_PHONE,
1783 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1784 _("Work"));
1785 /* mobile phone */
1786 menu = buddy_menu_phone(sipe_public,
1787 menu,
1788 buddy,
1789 SIPE_BUDDY_INFO_MOBILE_PHONE,
1790 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1791 _("Mobile"));
1793 /* home phone */
1794 menu = buddy_menu_phone(sipe_public,
1795 menu,
1796 buddy,
1797 SIPE_BUDDY_INFO_HOME_PHONE,
1798 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1799 _("Home"));
1801 /* other phone */
1802 menu = buddy_menu_phone(sipe_public,
1803 menu,
1804 buddy,
1805 SIPE_BUDDY_INFO_OTHER_PHONE,
1806 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1807 _("Other"));
1809 /* custom1 phone */
1810 menu = buddy_menu_phone(sipe_public,
1811 menu,
1812 buddy,
1813 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1814 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1815 _("Custom1"));
1819 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1820 buddy,
1821 SIPE_BUDDY_INFO_EMAIL);
1822 if (email) {
1823 menu = sipe_backend_buddy_menu_add(sipe_public,
1824 menu,
1825 _("Send email..."),
1826 SIPE_BUDDY_MENU_SEND_EMAIL,
1827 NULL);
1828 g_free(email);
1832 /* access level control */
1833 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1834 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1835 menu,
1836 _("Access level"),
1837 sipe_ocs2007_access_control_menu(sipe_private,
1838 buddy_name));
1840 return(menu);
1843 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
1845 return(g_hash_table_size(sipe_private->buddies->uri));
1848 static guint sipe_ht_hash_nick(const char *nick)
1850 char *lc = g_utf8_strdown(nick, -1);
1851 guint bucket = g_str_hash(lc);
1852 g_free(lc);
1854 return bucket;
1857 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
1859 char *nick1_norm = NULL;
1860 char *nick2_norm = NULL;
1861 gboolean equal;
1863 if (nick1 == NULL && nick2 == NULL) return TRUE;
1864 if (nick1 == NULL || nick2 == NULL ||
1865 !g_utf8_validate(nick1, -1, NULL) ||
1866 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
1868 nick1_norm = g_utf8_casefold(nick1, -1);
1869 nick2_norm = g_utf8_casefold(nick2, -1);
1870 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
1871 g_free(nick2_norm);
1872 g_free(nick1_norm);
1874 return equal;
1877 void sipe_buddy_init(struct sipe_core_private *sipe_private)
1879 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
1880 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
1881 (GEqualFunc) sipe_ht_equals_nick);
1882 buddies->exchange_key = g_hash_table_new(g_str_hash,
1883 g_str_equal);
1884 sipe_private->buddies = buddies;
1888 Local Variables:
1889 mode: c
1890 c-file-style: "bsd"
1891 indent-tabs-mode: t
1892 tab-width: 8
1893 End: