buddy: refactor add buddy to group code
[siplcs.git] / src / core / sipe-buddy.c
bloba5b3b9c80948be9e85e1cb07df604754cbb959bb
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)
83 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private, uri);
84 if (!buddy) {
85 struct sipe_buddies *buddies = sipe_private->buddies;
87 buddy = g_new0(struct sipe_buddy, 1);
88 buddy->name = g_strdup(uri);
89 g_hash_table_insert(buddies->uri,
90 buddy->name,
91 buddy);
93 if (exchange_key) {
94 buddy->exchange_key = g_strdup(exchange_key);
95 g_hash_table_insert(buddies->exchange_key,
96 buddy->exchange_key,
97 buddy);
101 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", uri);
103 buddy_fetch_photo(sipe_private, uri);
104 } else {
105 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", uri);
108 return buddy;
111 void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private,
112 struct sipe_buddy *buddy,
113 struct sipe_group *group,
114 const gchar *alias)
116 const gchar *uri = buddy->name;
117 const gchar *group_name = group->name;
118 sipe_backend_buddy bb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
119 uri,
120 group_name);
122 if (!bb) {
123 bb = sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
124 uri,
125 alias,
126 group_name);
127 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
128 uri, alias ? alias : "<NONE>");
132 if (!is_empty(alias)) {
133 gchar *old_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
134 bb);
136 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri),
137 old_alias)) {
138 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
140 alias);
141 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
142 uri, old_alias, alias);
144 g_free(old_alias);
147 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
148 group,
149 (GCompareFunc) sipe_group_compare,
150 NULL);
151 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
152 uri, group_name);
155 void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private)
157 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
158 NULL,
159 NULL);
160 GSList *entry = buddies;
162 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
163 g_slist_length(buddies));
164 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
165 sipe_buddy_count(sipe_private));
166 while (entry) {
167 sipe_backend_buddy bb = entry->data;
168 gchar *bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC,
169 bb);
170 gchar *gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC,
171 bb);
172 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
173 bname);
174 gboolean in_sipe_groups = FALSE;
176 if (buddy) {
177 GSList *entry2 = buddy->groups;
179 while (entry2) {
180 struct sipe_group *group = entry2->data;
181 if (sipe_strequal(group->name, gname)) {
182 in_sipe_groups = TRUE;
183 break;
185 entry2 = entry2->next;
189 if (!in_sipe_groups) {
190 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: REMOVING '%s' from local group '%s', as buddy is not in that group on remote contact list",
191 bname, gname);
192 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, bb);
195 g_free(gname);
196 g_free(bname);
198 entry = entry->next;
201 g_slist_free(buddies);
204 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
205 const gchar *uri)
207 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
210 struct sipe_buddy *sipe_buddy_find_by_exchange_key(struct sipe_core_private *sipe_private,
211 const gchar *exchange_key)
213 return(g_hash_table_lookup(sipe_private->buddies->exchange_key,
214 exchange_key));
217 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
218 GHFunc callback,
219 gpointer callback_data)
221 g_hash_table_foreach(sipe_private->buddies->uri,
222 callback,
223 callback_data);
226 static void buddy_free(struct sipe_buddy *buddy)
228 #ifndef _WIN32
230 * We are calling g_hash_table_foreach_steal(). That means that no
231 * key/value deallocation functions are called. Therefore the glib
232 * hash code does not touch the key (buddy->name) or value (buddy)
233 * of the to-be-deleted hash node at all. It follows that we
235 * - MUST free the memory for the key ourselves and
236 * - ARE allowed to do it in this function
238 * Conclusion: glib must be broken on the Windows platform if sipe
239 * crashes with SIGTRAP when closing. You'll have to live
240 * with the memory leak until this is fixed.
242 g_free(buddy->name);
243 #endif
244 g_free(buddy->exchange_key);
245 g_free(buddy->activity);
246 g_free(buddy->meeting_subject);
247 g_free(buddy->meeting_location);
248 g_free(buddy->note);
250 g_free(buddy->cal_start_time);
251 g_free(buddy->cal_free_busy_base64);
252 g_free(buddy->cal_free_busy);
253 g_free(buddy->last_non_cal_activity);
255 sipe_cal_free_working_hours(buddy->cal_working_hours);
257 g_free(buddy->device_name);
258 g_slist_free(buddy->groups);
259 g_free(buddy);
262 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
263 gpointer buddy,
264 SIPE_UNUSED_PARAMETER gpointer user_data)
266 buddy_free(buddy);
267 /* We must return TRUE as the key/value have already been deleted */
268 return(TRUE);
271 void sipe_buddy_free(struct sipe_core_private *sipe_private)
273 struct sipe_buddies *buddies = sipe_private->buddies;
275 g_hash_table_foreach_steal(buddies->uri,
276 buddy_free_cb,
277 NULL);
279 /* core is being deallocated, remove all its pending photo requests */
280 while (buddies->pending_photo_requests) {
281 struct photo_response_data *data =
282 buddies->pending_photo_requests->data;
283 buddies->pending_photo_requests =
284 g_slist_remove(buddies->pending_photo_requests, data);
285 photo_response_data_free(data);
288 g_hash_table_destroy(buddies->uri);
289 g_hash_table_destroy(buddies->exchange_key);
290 g_free(buddies);
291 sipe_private->buddies = NULL;
294 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
295 const gchar *uri,
296 guint activity,
297 const gchar *status_text)
299 struct sipe_buddy *sbuddy;
300 const char *activity_str;
302 if (!sipe_public) return NULL; /* happens on pidgin exit */
304 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
305 if (!sbuddy) return NULL;
307 activity_str = sbuddy->activity ? sbuddy->activity :
308 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
309 status_text : NULL;
311 if (activity_str && sbuddy->note) {
312 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
313 } else if (activity_str) {
314 return g_strdup(activity_str);
315 } else if (sbuddy->note) {
316 return g_strdup_printf("<i>%s</i>", sbuddy->note);
317 } else {
318 return NULL;
322 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
323 const gchar *with)
325 sipe_backend_buddy pbuddy;
326 gchar *alias = NULL;
327 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
328 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
330 return alias;
333 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
334 const gchar *who,
335 const gchar *old_group_name,
336 const gchar *new_group_name)
338 struct sipe_buddy * buddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE,
339 who);
340 struct sipe_group * old_group = NULL;
341 struct sipe_group * new_group;
343 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
344 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
346 if(!buddy) { // buddy not in roaming list
347 return;
350 if (old_group_name) {
351 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
353 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
355 if (old_group) {
356 buddy->groups = g_slist_remove(buddy->groups, old_group);
357 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
360 if (!new_group) {
361 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
362 } else {
363 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
364 new_group,
365 (GCompareFunc)sipe_group_compare,
366 NULL);
367 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
371 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
372 const gchar *uri,
373 const gchar *group_name)
375 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
377 if (!sipe_buddy_find_by_uri(sipe_private, uri)) {
378 struct sipe_buddy *b = sipe_buddy_add(sipe_private, uri, NULL);
379 b->just_added = TRUE;
381 sipe_subscribe_presence_single_cb(sipe_private, b->name);
383 } else {
384 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
385 uri);
388 sipe_core_buddy_group(sipe_public,
389 uri,
390 NULL,
391 group_name);
394 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
395 struct sipe_buddy *buddy)
397 struct sipe_buddies *buddies = sipe_private->buddies;
398 gchar *action_name = sipe_utils_presence_key(buddy->name);
399 sipe_schedule_cancel(sipe_private, action_name);
400 g_free(action_name);
402 g_hash_table_remove(buddies->uri, buddy->name);
403 if (buddy->exchange_key)
404 g_hash_table_remove(buddies->exchange_key,
405 buddy->exchange_key);
407 buddy_free(buddy);
411 * Unassociates buddy from group first.
412 * Then see if no groups left, removes buddy completely.
413 * Otherwise updates buddy groups on server.
415 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
416 const gchar *uri,
417 const gchar *group_name)
419 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
420 struct sipe_buddy *b = sipe_buddy_find_by_uri(sipe_private,
421 uri);
423 if (!b) return;
425 if (group_name) {
426 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
427 group_name);
428 if (g) {
429 b->groups = g_slist_remove(b->groups, g);
430 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
431 uri, g->name);
435 if (g_slist_length(b->groups) < 1) {
436 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
437 b->name);
438 sip_soap_request(sipe_private,
439 "deleteContact",
440 request);
441 g_free(request);
442 sipe_buddy_remove(sipe_private, b);
443 } else {
444 /* updates groups on server */
445 sipe_group_update_buddy(sipe_private, b);
450 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
451 const gchar *uri,
452 guint activity)
454 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
455 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
456 uri);
458 if (!sbuddy) return;
460 /* Check if on 2005 system contact's calendar,
461 * then set/preserve it.
463 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
464 sipe_backend_buddy_set_status(sipe_public, uri, activity);
465 } else {
466 sipe_ocs2005_apply_calendar_status(sipe_private,
467 sbuddy,
468 sipe_status_activity_to_token(activity));
472 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
473 const gchar *uri,
474 const gchar *status_name,
475 gboolean is_online,
476 struct sipe_backend_buddy_tooltip *tooltip)
478 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
479 gchar *note = NULL;
480 gboolean is_oof_note = FALSE;
481 const gchar *activity = NULL;
482 gchar *calendar = NULL;
483 const gchar *meeting_subject = NULL;
484 const gchar *meeting_location = NULL;
485 gchar *access_text = NULL;
487 #define SIPE_ADD_BUDDY_INFO(l, t) \
489 gchar *tmp = g_markup_escape_text((t), -1); \
490 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
491 g_free(tmp); \
493 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
494 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
496 if (sipe_public) { /* happens on pidgin exit */
497 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
498 uri);
499 if (sbuddy) {
500 note = sbuddy->note;
501 is_oof_note = sbuddy->is_oof_note;
502 activity = sbuddy->activity;
503 calendar = sipe_cal_get_description(sbuddy);
504 meeting_subject = sbuddy->meeting_subject;
505 meeting_location = sbuddy->meeting_location;
507 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
508 gboolean is_group_access = FALSE;
509 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
510 "user",
511 sipe_get_no_sip_uri(uri),
512 &is_group_access);
513 const char *access_level = sipe_ocs2007_access_level_name(container_id);
514 access_text = is_group_access ?
515 g_strdup(access_level) :
516 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
517 access_level);
521 if (is_online) {
522 const gchar *status_str = activity ? activity : status_name;
524 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
526 if (is_online && !is_empty(calendar)) {
527 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
529 g_free(calendar);
530 if (!is_empty(meeting_location)) {
531 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
532 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
534 if (!is_empty(meeting_subject)) {
535 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
536 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
538 if (note) {
539 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
540 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
541 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
542 note_italics);
543 g_free(note_italics);
545 if (access_text) {
546 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
547 g_free(access_text);
551 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
552 const char *uri,
553 sipe_buddy_info_fields propkey,
554 char *property_value)
556 GSList *buddies, *entry;
558 if (property_value)
559 property_value = g_strstrip(property_value);
561 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
562 while (entry) {
563 gchar *prop_str;
564 sipe_backend_buddy p_buddy = entry->data;
566 /* for Display Name */
567 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
568 gchar *alias;
569 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
570 if (property_value && sipe_is_bad_alias(uri, alias)) {
571 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
572 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
574 g_free(alias);
576 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
577 if (!is_empty(property_value) &&
578 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
580 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
581 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
583 g_free(alias);
585 /* for other properties */
586 else {
587 if (!is_empty(property_value)) {
588 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
589 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
590 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
592 g_free(prop_str);
596 entry = entry->next;
598 g_slist_free(buddies);
602 struct ms_dlx_data;
603 struct ms_dlx_data {
604 GSList *search_rows;
605 gchar *other;
606 guint max_returns;
607 sipe_svc_callback *callback;
608 struct sipe_svc_session *session;
609 gchar *wsse_security;
610 struct sipe_backend_search_token *token;
611 /* must call ms_dlx_free() */
612 void (*failed_callback)(struct sipe_core_private *sipe_private,
613 struct ms_dlx_data *mdd);
616 static void ms_dlx_free(struct ms_dlx_data *mdd)
618 sipe_utils_slist_free_full(mdd->search_rows, g_free);
619 sipe_svc_session_close(mdd->session);
620 g_free(mdd->other);
621 g_free(mdd->wsse_security);
622 g_free(mdd);
625 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
626 #define DLX_SEARCH_ITEM \
627 "<AbEntryRequest.ChangeSearchQuery>" \
628 " <SearchOn>%s</SearchOn>" \
629 " <Value>%s</Value>" \
630 "</AbEntryRequest.ChangeSearchQuery>"
632 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
633 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
634 guint i = 0;
635 gchar *query = NULL;
637 while (query_rows) {
638 gchar *attr;
639 gchar *value;
641 attr = query_rows->data;
642 query_rows = g_slist_next(query_rows);
643 value = query_rows->data;
644 query_rows = g_slist_next(query_rows);
646 if (!attr || !value)
647 break;
649 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
650 attr, value);
652 attrs[i] = NULL;
654 if (i) {
655 query = g_strjoinv(NULL, attrs);
656 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
657 query ? query : "");
660 g_strfreev(attrs);
662 return query;
665 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
666 const gchar *base_uri,
667 const gchar *auth_uri,
668 const gchar *wsse_security,
669 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
670 gpointer callback_data)
672 struct ms_dlx_data *mdd = callback_data;
674 if (wsse_security) {
675 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
677 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
678 base_uri);
680 if (sipe_svc_ab_entry_request(sipe_private,
681 mdd->session,
682 auth_uri,
683 wsse_security,
684 query,
685 g_slist_length(mdd->search_rows) / 2,
686 mdd->max_returns,
687 mdd->callback,
688 mdd)) {
690 /* keep webticket security token for potential further use */
691 mdd->wsse_security = g_strdup(wsse_security);
693 /* callback data passed down the line */
694 mdd = NULL;
696 g_free(query);
698 } else {
699 /* no ticket: this will show the minmum information */
700 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
701 base_uri);
704 if (mdd)
705 mdd->failed_callback(sipe_private, mdd);
708 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
709 struct ms_dlx_data *mdd)
711 if (!sipe_webticket_request(sipe_private,
712 mdd->session,
713 sipe_private->dlx_uri,
714 "AddressBookWebTicketBearer",
715 ms_dlx_webticket,
716 mdd)) {
717 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
718 sipe_private->dlx_uri);
719 mdd->failed_callback(sipe_private, mdd);
723 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
724 struct sipe_backend_search_results *results,
725 guint match_count,
726 gboolean more)
728 gchar *secondary = g_strdup_printf(
729 dngettext(PACKAGE_NAME,
730 "Found %d contact%s:",
731 "Found %d contacts%s:", match_count),
732 match_count, more ? _(" (more matched your query)") : "");
734 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
735 results,
736 secondary,
737 more);
738 g_free(secondary);
741 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
742 const gchar *uri,
743 SIPE_UNUSED_PARAMETER const gchar *raw,
744 sipe_xml *soap_body,
745 gpointer callback_data)
747 struct ms_dlx_data *mdd = callback_data;
749 if (soap_body) {
750 const sipe_xml *node;
751 struct sipe_backend_search_results *results;
752 GHashTable *found;
754 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
755 uri);
757 /* any matches? */
758 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
759 if (!node) {
760 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
761 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
762 mdd->token,
763 _("No contacts found"));
764 ms_dlx_free(mdd);
765 return;
768 /* OK, we found something - show the results to the user */
769 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
770 mdd->token);
771 if (!results) {
772 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
773 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
774 mdd->token,
775 _("Unable to display the search results"));
776 ms_dlx_free(mdd);
777 return;
780 /* SearchAbEntryResult can contain duplicates */
781 found = g_hash_table_new_full(g_str_hash, g_str_equal,
782 g_free, NULL);
784 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
785 const sipe_xml *attrs;
786 gchar *sip_uri = NULL;
787 gchar *displayname = NULL;
788 gchar *company = NULL;
789 gchar *country = NULL;
790 gchar *email = NULL;
792 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
793 attrs;
794 attrs = sipe_xml_twin(attrs)) {
795 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
796 "Name"));
797 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
798 "Value"));
800 if (!is_empty(value)) {
801 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
802 g_free(sip_uri);
803 sip_uri = value;
804 value = NULL;
805 } else if (sipe_strcase_equal(name, "displayname")) {
806 g_free(displayname);
807 displayname = value;
808 value = NULL;
809 } else if (sipe_strcase_equal(name, "mail")) {
810 g_free(email);
811 email = value;
812 value = NULL;
813 } else if (sipe_strcase_equal(name, "company")) {
814 g_free(company);
815 company = value;
816 value = NULL;
817 } else if (sipe_strcase_equal(name, "country")) {
818 g_free(country);
819 country = value;
820 value = NULL;
824 g_free(value);
825 g_free(name);
828 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
829 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
830 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
831 results,
832 uri_parts[1],
833 displayname,
834 company,
835 country,
836 email);
837 g_strfreev(uri_parts);
839 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
840 sip_uri = NULL;
843 g_free(email);
844 g_free(country);
845 g_free(company);
846 g_free(displayname);
847 g_free(sip_uri);
850 search_contacts_finalize(sipe_private, results,
851 g_hash_table_size(found),
852 FALSE);
853 g_hash_table_destroy(found);
854 ms_dlx_free(mdd);
856 } else {
857 mdd->failed_callback(sipe_private, mdd);
861 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
862 struct sipmsg *msg,
863 struct transaction *trans)
865 struct sipe_backend_search_token *token = trans->payload->data;
866 struct sipe_backend_search_results *results;
867 sipe_xml *searchResults;
868 const sipe_xml *mrow;
869 guint match_count = 0;
870 gboolean more = FALSE;
872 /* valid response? */
873 if (msg->response != 200) {
874 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
875 msg->response);
876 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
877 token,
878 _("Contact search failed"));
879 return(FALSE);
882 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
884 /* valid XML? */
885 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
886 if (!searchResults) {
887 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
888 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
889 token,
890 _("Contact search failed"));
891 return(FALSE);
894 /* any matches? */
895 mrow = sipe_xml_child(searchResults, "Body/Array/row");
896 if (!mrow) {
897 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
898 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
899 token,
900 _("No contacts found"));
902 sipe_xml_free(searchResults);
903 return(FALSE);
906 /* OK, we found something - show the results to the user */
907 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
908 trans->payload->data);
909 if (!results) {
910 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
911 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
912 token,
913 _("Unable to display the search results"));
915 sipe_xml_free(searchResults);
916 return FALSE;
919 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
920 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
921 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
922 results,
923 uri_parts[1],
924 sipe_xml_attribute(mrow, "displayName"),
925 sipe_xml_attribute(mrow, "company"),
926 sipe_xml_attribute(mrow, "country"),
927 sipe_xml_attribute(mrow, "email"));
928 g_strfreev(uri_parts);
929 match_count++;
932 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
933 char *data = sipe_xml_data(mrow);
934 more = (g_ascii_strcasecmp(data, "true") == 0);
935 g_free(data);
938 search_contacts_finalize(sipe_private, results, match_count, more);
939 sipe_xml_free(searchResults);
941 return(TRUE);
944 static void search_soap_request(struct sipe_core_private *sipe_private,
945 struct sipe_backend_search_token *token,
946 GSList *search_rows)
948 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
949 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
951 payload->data = token;
953 sip_soap_directory_search(sipe_private,
954 100,
955 query,
956 process_search_contact_response,
957 payload);
958 g_free(query);
961 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
962 struct ms_dlx_data *mdd)
964 /* error using [MS-DLX] server, retry using Active Directory */
965 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
966 ms_dlx_free(mdd);
969 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
970 struct sipe_backend_search_token *token,
971 const gchar *given_name,
972 const gchar *surname,
973 const gchar *email,
974 const gchar *company,
975 const gchar *country)
977 GSList *query_rows = NULL;
979 #define ADD_QUERY_ROW(attr, val) \
980 if (val) { \
981 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
982 query_rows = g_slist_append(query_rows, g_strdup(val)); \
985 ADD_QUERY_ROW("givenName", given_name);
986 ADD_QUERY_ROW("sn", surname);
987 ADD_QUERY_ROW("mail", email);
988 ADD_QUERY_ROW("company", company);
989 ADD_QUERY_ROW("c", country);
991 if (query_rows) {
992 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
993 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
995 mdd->search_rows = query_rows;
996 mdd->max_returns = 100;
997 mdd->callback = search_ab_entry_response;
998 mdd->failed_callback = search_ab_entry_failed;
999 mdd->session = sipe_svc_session_start();
1000 mdd->token = token;
1002 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
1004 } else {
1005 /* no [MS-DLX] server, use Active Directory search instead */
1006 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
1007 sipe_utils_slist_free_full(query_rows, g_free);
1009 } else
1010 sipe_backend_search_failed(sipe_public,
1011 token,
1012 _("Invalid contact search query"));
1015 static void get_info_finalize(struct sipe_core_private *sipe_private,
1016 struct sipe_backend_buddy_info *info,
1017 const gchar *uri,
1018 const gchar *server_alias,
1019 const gchar *email)
1021 sipe_backend_buddy bbuddy;
1022 struct sipe_buddy *sbuddy;
1023 gchar *alias;
1024 gchar *value;
1026 if (!info) {
1027 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1028 } else {
1029 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
1031 if (!info)
1032 return;
1034 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
1036 if (is_empty(server_alias)) {
1037 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
1038 bbuddy);
1039 if (value) {
1040 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1041 info,
1042 SIPE_BUDDY_INFO_DISPLAY_NAME,
1043 value);
1045 } else {
1046 value = g_strdup(server_alias);
1049 /* present alias if it differs from server alias */
1050 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
1051 if (alias && !sipe_strequal(alias, value))
1053 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1054 info,
1055 SIPE_BUDDY_INFO_ALIAS,
1056 alias);
1058 g_free(alias);
1059 g_free(value);
1061 if (is_empty(email)) {
1062 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1063 bbuddy,
1064 SIPE_BUDDY_INFO_EMAIL);
1065 if (value) {
1066 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1067 info,
1068 SIPE_BUDDY_INFO_EMAIL,
1069 value);
1070 g_free(value);
1074 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1075 bbuddy,
1076 SIPE_BUDDY_INFO_SITE);
1077 if (value) {
1078 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1079 info,
1080 SIPE_BUDDY_INFO_SITE,
1081 value);
1082 g_free(value);
1085 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
1086 if (sbuddy && sbuddy->device_name) {
1087 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1088 info,
1089 SIPE_BUDDY_INFO_DEVICE,
1090 sbuddy->device_name);
1093 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
1097 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
1098 const gchar *uri,
1099 SIPE_UNUSED_PARAMETER const gchar *raw,
1100 sipe_xml *soap_body,
1101 gpointer callback_data)
1103 struct ms_dlx_data *mdd = callback_data;
1104 struct sipe_backend_buddy_info *info = NULL;
1105 gchar *server_alias = NULL;
1106 gchar *email = NULL;
1108 if (soap_body) {
1109 const sipe_xml *node;
1111 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1112 uri);
1114 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1116 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1117 node;
1118 node = sipe_xml_twin(node)) {
1119 gchar *name = sipe_xml_data(sipe_xml_child(node,
1120 "Name"));
1121 gchar *value = sipe_xml_data(sipe_xml_child(node,
1122 "Value"));
1123 const sipe_xml *values = sipe_xml_child(node,
1124 "Values");
1126 /* Single value entries */
1127 if (!is_empty(value)) {
1129 if (sipe_strcase_equal(name, "displayname")) {
1130 g_free(server_alias);
1131 server_alias = value;
1132 value = NULL;
1133 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1134 info,
1135 SIPE_BUDDY_INFO_DISPLAY_NAME,
1136 server_alias);
1137 } else if (sipe_strcase_equal(name, "mail")) {
1138 g_free(email);
1139 email = value;
1140 value = NULL;
1141 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1142 info,
1143 SIPE_BUDDY_INFO_EMAIL,
1144 email);
1145 } else if (sipe_strcase_equal(name, "title")) {
1146 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1147 info,
1148 SIPE_BUDDY_INFO_JOB_TITLE,
1149 value);
1150 } else if (sipe_strcase_equal(name, "company")) {
1151 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1152 info,
1153 SIPE_BUDDY_INFO_COMPANY,
1154 value);
1155 } else if (sipe_strcase_equal(name, "country")) {
1156 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1157 info,
1158 SIPE_BUDDY_INFO_COUNTRY,
1159 value);
1162 } else if (values) {
1163 gchar *first = sipe_xml_data(sipe_xml_child(values,
1164 "string"));
1166 if (sipe_strcase_equal(name, "telephonenumber")) {
1167 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1168 info,
1169 SIPE_BUDDY_INFO_WORK_PHONE,
1170 first);
1173 g_free(first);
1176 g_free(value);
1177 g_free(name);
1181 /* this will show the minmum information */
1182 get_info_finalize(sipe_private,
1183 info,
1184 mdd->other,
1185 server_alias,
1186 email);
1188 g_free(email);
1189 g_free(server_alias);
1190 ms_dlx_free(mdd);
1193 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1194 struct sipmsg *msg,
1195 struct transaction *trans)
1197 const gchar *uri = trans->payload->data;
1198 struct sipe_backend_buddy_info *info = NULL;
1199 gchar *server_alias = NULL;
1200 gchar *email = NULL;
1202 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1203 uri, sipe_private->username);
1205 if (msg->response != 200) {
1206 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1207 } else {
1208 sipe_xml *searchResults;
1209 const sipe_xml *mrow;
1211 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1212 msg->body ? msg->body : "");
1214 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1215 if (!searchResults) {
1217 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1219 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1220 const gchar *value;
1221 gchar *phone_number;
1223 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1225 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1226 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1227 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1230 * For 2007 system we will take this from ContactCard -
1231 * it has cleaner tel: URIs at least
1233 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1234 char *tel_uri = sip_to_tel_uri(phone_number);
1235 /* trims its parameters, so call first */
1236 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1237 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1238 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1239 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1240 g_free(tel_uri);
1242 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1243 uri);
1246 if (!is_empty(server_alias)) {
1247 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1248 info,
1249 SIPE_BUDDY_INFO_DISPLAY_NAME,
1250 server_alias);
1252 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1253 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1254 info,
1255 SIPE_BUDDY_INFO_JOB_TITLE,
1256 value);
1258 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1259 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1260 info,
1261 SIPE_BUDDY_INFO_OFFICE,
1262 value);
1264 if (!is_empty(phone_number)) {
1265 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1266 info,
1267 SIPE_BUDDY_INFO_WORK_PHONE,
1268 phone_number);
1270 g_free(phone_number);
1271 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1272 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1273 info,
1274 SIPE_BUDDY_INFO_COMPANY,
1275 value);
1277 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1278 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1279 info,
1280 SIPE_BUDDY_INFO_CITY,
1281 value);
1283 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1284 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1285 info,
1286 SIPE_BUDDY_INFO_STATE,
1287 value);
1289 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1290 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1291 info,
1292 SIPE_BUDDY_INFO_COUNTRY,
1293 value);
1295 if (!is_empty(email)) {
1296 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1297 info,
1298 SIPE_BUDDY_INFO_EMAIL,
1299 email);
1302 sipe_xml_free(searchResults);
1305 /* this will show the minmum information */
1306 get_info_finalize(sipe_private,
1307 info,
1308 uri,
1309 server_alias,
1310 email);
1312 g_free(server_alias);
1313 g_free(email);
1315 return TRUE;
1318 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1319 struct ms_dlx_data *mdd)
1321 /* error using [MS-DLX] server, retry using Active Directory */
1322 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1323 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1325 payload->destroy = g_free;
1326 payload->data = mdd->other;
1327 mdd->other = NULL;
1329 sip_soap_directory_search(sipe_private,
1331 query,
1332 process_get_info_response,
1333 payload);
1335 ms_dlx_free(mdd);
1336 g_free(query);
1339 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1340 const gchar *who)
1342 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1344 if (sipe_private->dlx_uri) {
1345 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1347 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1348 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1350 mdd->other = g_strdup(who);
1351 mdd->max_returns = 1;
1352 mdd->callback = get_info_ab_entry_response;
1353 mdd->failed_callback = get_info_ab_entry_failed;
1354 mdd->session = sipe_svc_session_start();
1356 ms_dlx_webticket_request(sipe_private, mdd);
1358 } else {
1359 /* no [MS-DLX] server, use Active Directory search instead */
1360 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1361 "msRTCSIP-PrimaryUserAddress",
1362 who);
1363 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1365 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1366 row ? row : "");
1368 payload->destroy = g_free;
1369 payload->data = g_strdup(who);
1371 sip_soap_directory_search(sipe_private,
1373 row,
1374 process_get_info_response,
1375 payload);
1376 g_free(row);
1380 static void photo_response_data_free(struct photo_response_data *data)
1382 g_free(data->who);
1383 g_free(data->photo_hash);
1384 if (data->request) {
1385 sipe_http_request_cancel(data->request);
1387 g_free(data);
1390 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1391 guint status,
1392 GSList *headers,
1393 const char *body,
1394 gpointer data)
1396 struct photo_response_data *rdata = (struct photo_response_data *) data;
1398 rdata->request = NULL;
1400 if (status == SIPE_HTTP_STATUS_OK) {
1401 const gchar *len_str = sipe_utils_nameval_find(headers,
1402 "Content-Length");
1403 if (len_str) {
1404 gsize photo_size = atoi(len_str);
1405 gpointer photo = g_new(char, photo_size);
1407 if (photo) {
1408 memcpy(photo, body, photo_size);
1410 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1411 rdata->who,
1412 photo,
1413 photo_size,
1414 rdata->photo_hash);
1419 sipe_private->buddies->pending_photo_requests =
1420 g_slist_remove(sipe_private->buddies->pending_photo_requests, rdata);
1422 photo_response_data_free(rdata);
1425 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1427 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1428 gchar *wsse_security_base64;
1429 gchar *x_ms_webticket_header;
1431 if (!assertion) {
1432 return NULL;
1435 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1436 strlen(assertion));
1437 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1438 wsse_security_base64);
1440 g_free(assertion);
1441 g_free(wsse_security_base64);
1443 return x_ms_webticket_header;
1446 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1447 const gchar *uri,
1448 SIPE_UNUSED_PARAMETER const gchar *raw,
1449 sipe_xml *soap_body,
1450 gpointer callback_data)
1452 struct ms_dlx_data *mdd = callback_data;
1453 gchar *photo_rel_path = NULL;
1454 gchar *photo_hash = NULL;
1455 const gchar *photo_hash_old =
1456 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1458 if (soap_body) {
1459 const sipe_xml *node;
1461 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1462 uri);
1464 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1465 node;
1466 node = sipe_xml_twin(node)) {
1467 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1468 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1470 if (!is_empty(value)) {
1471 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1472 g_free(photo_rel_path);
1473 photo_rel_path = value;
1474 value = NULL;
1475 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1476 g_free(photo_hash);
1477 photo_hash = value;
1478 value = NULL;
1482 g_free(value);
1483 g_free(name);
1487 if (sipe_private->addressbook_uri && photo_rel_path &&
1488 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1489 gchar *photo_url = g_strdup_printf("%s/%s",
1490 sipe_private->addressbook_uri, photo_rel_path);
1491 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1493 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1494 data->who = g_strdup(mdd->other);
1495 data->photo_hash = photo_hash;
1496 photo_hash = NULL;
1498 data->request = sipe_http_request_get(sipe_private,
1499 photo_url,
1500 x_ms_webticket_header,
1501 process_buddy_photo_response,
1502 data);
1504 if (data->request) {
1505 sipe_private->buddies->pending_photo_requests =
1506 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1507 sipe_http_request_ready(data->request);
1508 } else {
1509 photo_response_data_free(data);
1512 g_free(x_ms_webticket_header);
1513 g_free(photo_url);
1516 g_free(photo_rel_path);
1517 g_free(photo_hash);
1518 ms_dlx_free(mdd);
1521 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1522 struct ms_dlx_data *mdd)
1524 ms_dlx_free(mdd);
1527 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1528 const gchar *uri)
1530 if (sipe_backend_uses_photo()) {
1532 /* Lync 2013 or newer: use UCS */
1533 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013)) {
1535 sipe_ucs_get_photo(sipe_private, uri);
1537 /* Lync 2010: use [MS-DLX] */
1538 } else if (sipe_private->dlx_uri &&
1539 sipe_private->addressbook_uri) {
1540 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1542 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1543 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1545 mdd->other = g_strdup(uri);
1546 mdd->max_returns = 1;
1547 mdd->callback = get_photo_ab_entry_response;
1548 mdd->failed_callback = get_photo_ab_entry_failed;
1549 mdd->session = sipe_svc_session_start();
1551 ms_dlx_webticket_request(sipe_private, mdd);
1556 static void buddy_refresh_photos_cb(gpointer uri,
1557 SIPE_UNUSED_PARAMETER gpointer value,
1558 gpointer sipe_private)
1560 buddy_fetch_photo(sipe_private, uri);
1563 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1565 g_hash_table_foreach(sipe_private->buddies->uri,
1566 buddy_refresh_photos_cb,
1567 sipe_private);
1570 /* Buddy menu callbacks*/
1572 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1573 const gchar *who)
1575 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1577 /* 2007+ conference */
1578 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1579 sipe_conf_add(sipe_private, who);
1581 /* 2005- multiparty chat */
1582 } else {
1583 gchar *self = sip_uri_self(sipe_private);
1584 struct sip_session *session;
1586 session = sipe_session_add_chat(sipe_private,
1587 NULL,
1588 TRUE,
1589 self);
1590 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1591 session->chat_session,
1592 session->chat_session->title,
1593 self);
1594 g_free(self);
1596 sipe_im_invite(sipe_private, session, who,
1597 NULL, NULL, NULL, FALSE);
1601 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1602 const gchar *who)
1604 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1605 who,
1606 NULL);
1607 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1608 buddy,
1609 SIPE_BUDDY_INFO_EMAIL);
1611 if (email) {
1612 gchar *command_line = g_strdup_printf(
1613 #ifdef _WIN32
1614 "cmd /c start"
1615 #else
1616 "xdg-email"
1617 #endif
1618 " mailto:%s", email);
1619 g_free(email);
1621 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1622 command_line);
1623 g_spawn_command_line_async(command_line, NULL);
1624 g_free(command_line);
1626 } else {
1627 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1628 who);
1632 /* Buddy menu */
1634 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1635 struct sipe_backend_buddy_menu *menu,
1636 sipe_backend_buddy buddy,
1637 sipe_buddy_info_fields id_phone,
1638 sipe_buddy_info_fields id_display,
1639 const gchar *type)
1641 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1642 buddy,
1643 id_phone);
1644 if (phone) {
1645 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1646 buddy,
1647 id_display);
1648 gchar *tmp = NULL;
1649 gchar *label = g_strdup_printf("%s %s",
1650 type,
1651 display ? display :
1652 (tmp = sip_tel_uri_denormalize(phone)));
1653 menu = sipe_backend_buddy_menu_add(sipe_public,
1654 menu,
1655 label,
1656 SIPE_BUDDY_MENU_MAKE_CALL,
1657 phone);
1658 g_free(tmp);
1659 g_free(label);
1660 g_free(display);
1661 g_free(phone);
1664 return(menu);
1667 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1668 const gchar *buddy_name,
1669 struct sipe_backend_buddy_menu *menu)
1671 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1672 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1673 buddy_name,
1674 NULL);
1675 gchar *self = sip_uri_self(sipe_private);
1677 SIPE_SESSION_FOREACH {
1678 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1680 struct sipe_chat_session *chat_session = session->chat_session;
1681 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1683 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1685 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1687 if (is_conf &&
1688 /* Not conf OP */
1689 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1690 /* We are a conf OP */
1691 conf_op) {
1692 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1693 chat_session->title);
1694 menu = sipe_backend_buddy_menu_add(sipe_public,
1695 menu,
1696 label,
1697 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1698 chat_session);
1699 g_free(label);
1702 if (is_conf &&
1703 /* We are a conf OP */
1704 conf_op) {
1705 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1706 chat_session->title);
1707 menu = sipe_backend_buddy_menu_add(sipe_public,
1708 menu,
1709 label,
1710 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1711 chat_session);
1712 g_free(label);
1715 else
1717 if (!is_conf ||
1718 (is_conf && !session->locked)) {
1719 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1720 chat_session->title);
1721 menu = sipe_backend_buddy_menu_add(sipe_public,
1722 menu,
1723 label,
1724 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1725 chat_session);
1726 g_free(label);
1730 } SIPE_SESSION_FOREACH_END;
1731 g_free(self);
1733 menu = sipe_backend_buddy_menu_add(sipe_public,
1734 menu,
1735 _("New chat"),
1736 SIPE_BUDDY_MENU_NEW_CHAT,
1737 NULL);
1739 /* add buddy's phone numbers if we have call control */
1740 if (sip_csta_is_idle(sipe_private)) {
1742 /* work phone */
1743 menu = buddy_menu_phone(sipe_public,
1744 menu,
1745 buddy,
1746 SIPE_BUDDY_INFO_WORK_PHONE,
1747 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1748 _("Work"));
1749 /* mobile phone */
1750 menu = buddy_menu_phone(sipe_public,
1751 menu,
1752 buddy,
1753 SIPE_BUDDY_INFO_MOBILE_PHONE,
1754 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1755 _("Mobile"));
1757 /* home phone */
1758 menu = buddy_menu_phone(sipe_public,
1759 menu,
1760 buddy,
1761 SIPE_BUDDY_INFO_HOME_PHONE,
1762 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1763 _("Home"));
1765 /* other phone */
1766 menu = buddy_menu_phone(sipe_public,
1767 menu,
1768 buddy,
1769 SIPE_BUDDY_INFO_OTHER_PHONE,
1770 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1771 _("Other"));
1773 /* custom1 phone */
1774 menu = buddy_menu_phone(sipe_public,
1775 menu,
1776 buddy,
1777 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1778 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1779 _("Custom1"));
1783 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1784 buddy,
1785 SIPE_BUDDY_INFO_EMAIL);
1786 if (email) {
1787 menu = sipe_backend_buddy_menu_add(sipe_public,
1788 menu,
1789 _("Send email..."),
1790 SIPE_BUDDY_MENU_SEND_EMAIL,
1791 NULL);
1792 g_free(email);
1796 /* access level control */
1797 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1798 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1799 menu,
1800 _("Access level"),
1801 sipe_ocs2007_access_control_menu(sipe_private,
1802 buddy_name));
1804 return(menu);
1807 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
1809 return(g_hash_table_size(sipe_private->buddies->uri));
1812 static guint sipe_ht_hash_nick(const char *nick)
1814 char *lc = g_utf8_strdown(nick, -1);
1815 guint bucket = g_str_hash(lc);
1816 g_free(lc);
1818 return bucket;
1821 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
1823 char *nick1_norm = NULL;
1824 char *nick2_norm = NULL;
1825 gboolean equal;
1827 if (nick1 == NULL && nick2 == NULL) return TRUE;
1828 if (nick1 == NULL || nick2 == NULL ||
1829 !g_utf8_validate(nick1, -1, NULL) ||
1830 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
1832 nick1_norm = g_utf8_casefold(nick1, -1);
1833 nick2_norm = g_utf8_casefold(nick2, -1);
1834 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
1835 g_free(nick2_norm);
1836 g_free(nick1_norm);
1838 return equal;
1841 void sipe_buddy_init(struct sipe_core_private *sipe_private)
1843 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
1844 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
1845 (GEqualFunc) sipe_ht_equals_nick);
1846 buddies->exchange_key = g_hash_table_new(g_str_hash,
1847 g_str_equal);
1848 sipe_private->buddies = buddies;
1852 Local Variables:
1853 mode: c
1854 c-file-style: "bsd"
1855 indent-tabs-mode: t
1856 tab-width: 8
1857 End: