buddy: introduce data structure for group list
[siplcs.git] / src / core / sipe-buddy.c
blob979f0c3dd41d9a5d25543f74009f99973f7994e4
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 buddy_group_data {
70 const struct sipe_group *group;
73 struct photo_response_data {
74 gchar *who;
75 gchar *photo_hash;
76 struct sipe_http_request *request;
79 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
80 const gchar *uri);
81 static void photo_response_data_free(struct photo_response_data *data);
83 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
84 const gchar *uri,
85 const gchar *exchange_key,
86 const gchar *change_key)
88 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
89 gchar *normalized_uri = g_ascii_strdown(uri, -1);
90 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
91 normalized_uri);
93 if (!buddy) {
94 struct sipe_buddies *buddies = sipe_private->buddies;
96 buddy = g_new0(struct sipe_buddy, 1);
97 buddy->name = normalized_uri;
98 g_hash_table_insert(buddies->uri,
99 buddy->name,
100 buddy);
102 if (exchange_key) {
103 buddy->exchange_key = g_strdup(exchange_key);
104 g_hash_table_insert(buddies->exchange_key,
105 buddy->exchange_key,
106 buddy);
108 if (change_key)
109 buddy->change_key = g_strdup(change_key);
112 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri);
114 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
115 buddy->just_added = TRUE;
116 sipe_subscribe_presence_single_cb(sipe_private,
117 buddy->name);
120 buddy_fetch_photo(sipe_private, normalized_uri);
122 normalized_uri = NULL; /* buddy takes ownership */
123 } else {
124 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri);
125 buddy->is_obsolete = FALSE;
127 g_free(normalized_uri);
129 return(buddy);
132 static gboolean is_buddy_in_group(struct sipe_buddy *buddy,
133 const gchar *name)
135 if (buddy) {
136 GSList *entry = buddy->groups;
138 while (entry) {
139 struct buddy_group_data *bgd = entry->data;
140 if (sipe_strequal(bgd->group->name, name))
141 return(TRUE);
142 entry = entry->next;
146 return(FALSE);
149 void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private,
150 struct sipe_buddy *buddy,
151 struct sipe_group *group,
152 const gchar *alias)
154 const gchar *uri = buddy->name;
155 const gchar *group_name = group->name;
156 sipe_backend_buddy bb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
157 uri,
158 group_name);
160 if (!bb) {
161 bb = sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
162 uri,
163 alias,
164 group_name);
165 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
166 uri, alias ? alias : "<NONE>");
170 if (!is_empty(alias)) {
171 gchar *old_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
172 bb);
174 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri),
175 old_alias)) {
176 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
178 alias);
179 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
180 uri, old_alias, alias);
182 g_free(old_alias);
185 if (!is_buddy_in_group(buddy, group_name)) {
186 sipe_buddy_insert_group(buddy, group);
187 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
188 uri, group_name);
192 static gint buddy_group_compare(gconstpointer a, gconstpointer b)
194 return(((const struct buddy_group_data *)a)->group->id -
195 ((const struct buddy_group_data *)b)->group->id);
198 void sipe_buddy_insert_group(struct sipe_buddy *buddy,
199 struct sipe_group *group)
201 struct buddy_group_data *bgd = g_new0(struct buddy_group_data, 1);
203 bgd->group = group;
205 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
206 bgd,
207 buddy_group_compare,
208 NULL);
211 static void buddy_group_free(gpointer data)
213 g_free(data);
216 static void sipe_buddy_remove_group(struct sipe_buddy *buddy,
217 const struct sipe_group *group)
219 GSList *entry = buddy->groups;
220 struct buddy_group_data *bgd = NULL;
222 while (entry) {
223 bgd = entry->data;
224 if (bgd->group == group)
225 break;
226 entry = entry->next;
229 buddy->groups = g_slist_remove(buddy->groups, bgd);
230 buddy_group_free(bgd);
233 void sipe_buddy_update_groups(struct sipe_core_private *sipe_private,
234 struct sipe_buddy *buddy,
235 GSList *new_groups)
237 const gchar *uri = buddy->name;
238 GSList *entry = buddy->groups;
240 while (entry) {
241 const struct sipe_group *group = ((struct buddy_group_data *) entry->data)->group;
243 /* next buddy group */
244 entry = entry->next;
246 /* old group NOT found in new list? */
247 if (g_slist_find(new_groups, group) == NULL) {
248 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
249 uri,
250 group->name);
251 SIPE_DEBUG_INFO("sipe_buddy_update_groups: removing buddy %s from group '%s'",
252 uri, group->name);
253 /* this should never be NULL */
254 if (oldb)
255 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
256 oldb);
257 sipe_buddy_remove_group(buddy, group);
262 gchar *sipe_buddy_groups_string(struct sipe_buddy *buddy)
264 guint i = 0;
265 gchar *string;
266 /* creating array from GList, converting guint to gchar * */
267 gchar **ids_arr = g_new(gchar *, g_slist_length(buddy->groups) + 1);
268 GSList *entry = buddy->groups;
270 if (!ids_arr)
271 return(NULL);
273 while (entry) {
274 const struct sipe_group *group = ((struct buddy_group_data *) entry->data)->group;
275 ids_arr[i] = g_strdup_printf("%u", group->id);
276 entry = entry->next;
277 i++;
279 ids_arr[i] = NULL;
281 string = g_strjoinv(" ", ids_arr);
282 g_strfreev(ids_arr);
284 return(string);
287 void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private)
289 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
290 NULL,
291 NULL);
292 GSList *entry = buddies;
294 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
295 g_slist_length(buddies));
296 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
297 sipe_buddy_count(sipe_private));
298 while (entry) {
299 sipe_backend_buddy bb = entry->data;
300 gchar *bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC,
301 bb);
302 gchar *gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC,
303 bb);
304 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
305 bname);
307 if (!is_buddy_in_group(buddy, gname)) {
308 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",
309 bname, gname);
310 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, bb);
313 g_free(gname);
314 g_free(bname);
316 entry = entry->next;
319 g_slist_free(buddies);
322 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
323 const gchar *uri)
325 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
328 struct sipe_buddy *sipe_buddy_find_by_exchange_key(struct sipe_core_private *sipe_private,
329 const gchar *exchange_key)
331 return(g_hash_table_lookup(sipe_private->buddies->exchange_key,
332 exchange_key));
335 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
336 GHFunc callback,
337 gpointer callback_data)
339 g_hash_table_foreach(sipe_private->buddies->uri,
340 callback,
341 callback_data);
344 static void buddy_free(struct sipe_buddy *buddy)
346 #ifndef _WIN32
348 * We are calling g_hash_table_foreach_steal(). That means that no
349 * key/value deallocation functions are called. Therefore the glib
350 * hash code does not touch the key (buddy->name) or value (buddy)
351 * of the to-be-deleted hash node at all. It follows that we
353 * - MUST free the memory for the key ourselves and
354 * - ARE allowed to do it in this function
356 * Conclusion: glib must be broken on the Windows platform if sipe
357 * crashes with SIGTRAP when closing. You'll have to live
358 * with the memory leak until this is fixed.
360 g_free(buddy->name);
361 #endif
362 g_free(buddy->exchange_key);
363 g_free(buddy->change_key);
364 g_free(buddy->activity);
365 g_free(buddy->meeting_subject);
366 g_free(buddy->meeting_location);
367 g_free(buddy->note);
369 g_free(buddy->cal_start_time);
370 g_free(buddy->cal_free_busy_base64);
371 g_free(buddy->cal_free_busy);
372 g_free(buddy->last_non_cal_activity);
374 sipe_cal_free_working_hours(buddy->cal_working_hours);
376 g_free(buddy->device_name);
377 g_slist_free_full(buddy->groups, buddy_group_free);
378 g_free(buddy);
381 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
382 gpointer buddy,
383 SIPE_UNUSED_PARAMETER gpointer user_data)
385 buddy_free(buddy);
386 /* We must return TRUE as the key/value have already been deleted */
387 return(TRUE);
390 void sipe_buddy_free(struct sipe_core_private *sipe_private)
392 struct sipe_buddies *buddies = sipe_private->buddies;
394 g_hash_table_foreach_steal(buddies->uri,
395 buddy_free_cb,
396 NULL);
398 /* core is being deallocated, remove all its pending photo requests */
399 while (buddies->pending_photo_requests) {
400 struct photo_response_data *data =
401 buddies->pending_photo_requests->data;
402 buddies->pending_photo_requests =
403 g_slist_remove(buddies->pending_photo_requests, data);
404 photo_response_data_free(data);
407 g_hash_table_destroy(buddies->uri);
408 g_hash_table_destroy(buddies->exchange_key);
409 g_free(buddies);
410 sipe_private->buddies = NULL;
413 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
414 gpointer value,
415 SIPE_UNUSED_PARAMETER gpointer user_data)
417 ((struct sipe_buddy *) value)->is_obsolete = TRUE;
420 void sipe_buddy_update_start(struct sipe_core_private *sipe_private)
422 g_hash_table_foreach(sipe_private->buddies->uri,
423 buddy_set_obsolete_flag,
424 NULL);
427 static gboolean buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
428 gpointer value,
429 gpointer user_data)
431 struct sipe_core_private *sipe_private = user_data;
432 struct sipe_buddy *buddy = value;
434 if (buddy->is_obsolete) {
435 /* all backend buddies in different groups */
436 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
437 buddy->name,
438 NULL);
439 GSList *entry = buddies;
441 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
442 g_slist_length(buddies),
443 buddy->name);
445 while (entry) {
446 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
447 entry->data);
448 entry = entry->next;
450 g_slist_free(buddies);
452 buddy_free(buddy);
453 /* return TRUE as the key/value have already been deleted */
454 return(TRUE);
455 } else
456 return(FALSE);
459 void sipe_buddy_update_finish(struct sipe_core_private *sipe_private)
461 g_hash_table_foreach_remove(sipe_private->buddies->uri,
462 buddy_check_obsolete_flag,
463 sipe_private);
466 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
467 const gchar *uri,
468 guint activity,
469 const gchar *status_text)
471 struct sipe_buddy *sbuddy;
472 const char *activity_str;
474 if (!sipe_public) return NULL; /* happens on pidgin exit */
476 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
477 if (!sbuddy) return NULL;
479 activity_str = sbuddy->activity ? sbuddy->activity :
480 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
481 status_text : NULL;
483 if (activity_str && sbuddy->note) {
484 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
485 } else if (activity_str) {
486 return g_strdup(activity_str);
487 } else if (sbuddy->note) {
488 return g_strdup_printf("<i>%s</i>", sbuddy->note);
489 } else {
490 return NULL;
494 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
495 const gchar *with)
497 sipe_backend_buddy pbuddy;
498 gchar *alias = NULL;
499 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
500 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
502 return alias;
505 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
506 const gchar *who,
507 const gchar *old_group_name,
508 const gchar *new_group_name)
510 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE,
511 who);
512 struct sipe_group *old_group = NULL;
513 struct sipe_group *new_group;
515 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
516 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
518 if (!buddy)
519 /* buddy not in roaming list */
520 return;
522 if (old_group_name) {
523 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
525 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
527 if (old_group) {
528 sipe_buddy_remove_group(buddy, old_group);
529 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
532 if (!new_group) {
533 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
534 } else {
535 sipe_buddy_insert_group(buddy, new_group);
536 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
540 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
541 const gchar *uri,
542 const gchar *group_name)
544 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
546 if (!sipe_buddy_find_by_uri(sipe_private, uri))
547 sipe_buddy_add(sipe_private,
548 uri,
549 NULL,
550 NULL);
551 else
552 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
553 uri);
555 sipe_core_buddy_group(sipe_public,
556 uri,
557 NULL,
558 group_name);
561 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
562 struct sipe_buddy *buddy)
564 struct sipe_buddies *buddies = sipe_private->buddies;
565 const gchar *uri = buddy->name;
566 GSList *entry = buddy->groups;
567 gchar *action_name = sipe_utils_presence_key(uri);
569 sipe_schedule_cancel(sipe_private, action_name);
570 g_free(action_name);
572 /* If the buddy still has groups, we need to delete backend buddies */
573 while (entry) {
574 const struct sipe_group *group = ((struct buddy_group_data *) entry->data)->group;
575 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
576 uri,
577 group->name);
578 /* this should never be NULL */
579 if (oldb)
580 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, oldb);
582 entry = entry->next;
585 g_hash_table_remove(buddies->uri, uri);
586 if (buddy->exchange_key)
587 g_hash_table_remove(buddies->exchange_key,
588 buddy->exchange_key);
590 buddy_free(buddy);
594 * Unassociates buddy from group first.
595 * Then see if no groups left, removes buddy completely.
596 * Otherwise updates buddy groups on server.
598 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
599 const gchar *uri,
600 const gchar *group_name)
602 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
603 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
604 uri);
605 struct sipe_group *group = NULL;
607 if (!buddy) return;
609 if (group_name) {
610 group = sipe_group_find_by_name(sipe_private, group_name);
611 if (group) {
612 sipe_buddy_remove_group(buddy, group);
613 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
614 uri, group->name);
618 if (g_slist_length(buddy->groups) < 1) {
620 if (sipe_ucs_is_migrated(sipe_private)) {
621 sipe_ucs_group_remove_buddy(sipe_private,
622 group,
623 buddy);
624 } else {
625 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
626 buddy->name);
627 sip_soap_request(sipe_private,
628 "deleteContact",
629 request);
630 g_free(request);
633 sipe_buddy_remove(sipe_private, buddy);
634 } else {
635 if (sipe_ucs_is_migrated(sipe_private)) {
636 sipe_ucs_group_remove_buddy(sipe_private,
637 group,
638 buddy);
639 } else
640 /* updates groups on server */
641 sipe_group_update_buddy(sipe_private, buddy);
645 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
646 const gchar *uri,
647 guint activity)
649 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
650 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
651 uri);
653 if (!sbuddy) return;
655 /* Check if on 2005 system contact's calendar,
656 * then set/preserve it.
658 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
659 sipe_backend_buddy_set_status(sipe_public, uri, activity);
660 } else {
661 sipe_ocs2005_apply_calendar_status(sipe_private,
662 sbuddy,
663 sipe_status_activity_to_token(activity));
667 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
668 const gchar *uri,
669 const gchar *status_name,
670 gboolean is_online,
671 struct sipe_backend_buddy_tooltip *tooltip)
673 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
674 gchar *note = NULL;
675 gboolean is_oof_note = FALSE;
676 const gchar *activity = NULL;
677 gchar *calendar = NULL;
678 const gchar *meeting_subject = NULL;
679 const gchar *meeting_location = NULL;
680 gchar *access_text = NULL;
682 #define SIPE_ADD_BUDDY_INFO(l, t) \
684 gchar *tmp = g_markup_escape_text((t), -1); \
685 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
686 g_free(tmp); \
688 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
689 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
691 if (sipe_public) { /* happens on pidgin exit */
692 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
693 uri);
694 if (sbuddy) {
695 note = sbuddy->note;
696 is_oof_note = sbuddy->is_oof_note;
697 activity = sbuddy->activity;
698 calendar = sipe_cal_get_description(sbuddy);
699 meeting_subject = sbuddy->meeting_subject;
700 meeting_location = sbuddy->meeting_location;
702 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
703 gboolean is_group_access = FALSE;
704 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
705 "user",
706 sipe_get_no_sip_uri(uri),
707 &is_group_access);
708 const char *access_level = sipe_ocs2007_access_level_name(container_id);
709 access_text = is_group_access ?
710 g_strdup(access_level) :
711 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
712 access_level);
716 if (is_online) {
717 const gchar *status_str = activity ? activity : status_name;
719 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
721 if (is_online && !is_empty(calendar)) {
722 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
724 g_free(calendar);
725 if (!is_empty(meeting_location)) {
726 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
727 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
729 if (!is_empty(meeting_subject)) {
730 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
731 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
733 if (note) {
734 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
735 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
736 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
737 note_italics);
738 g_free(note_italics);
740 if (access_text) {
741 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
742 g_free(access_text);
746 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
747 const char *uri,
748 sipe_buddy_info_fields propkey,
749 char *property_value)
751 GSList *buddies, *entry;
753 if (property_value)
754 property_value = g_strstrip(property_value);
756 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
757 while (entry) {
758 gchar *prop_str;
759 sipe_backend_buddy p_buddy = entry->data;
761 /* for Display Name */
762 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
763 gchar *alias;
764 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
765 if (property_value && sipe_is_bad_alias(uri, alias)) {
766 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
767 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
769 g_free(alias);
771 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
772 if (!is_empty(property_value) &&
773 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
775 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
776 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
778 g_free(alias);
780 /* for other properties */
781 else {
782 if (!is_empty(property_value)) {
783 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
784 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
785 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
787 g_free(prop_str);
791 entry = entry->next;
793 g_slist_free(buddies);
797 struct ms_dlx_data;
798 struct ms_dlx_data {
799 GSList *search_rows;
800 gchar *other;
801 guint max_returns;
802 sipe_svc_callback *callback;
803 struct sipe_svc_session *session;
804 gchar *wsse_security;
805 struct sipe_backend_search_token *token;
806 /* must call ms_dlx_free() */
807 void (*failed_callback)(struct sipe_core_private *sipe_private,
808 struct ms_dlx_data *mdd);
811 static void ms_dlx_free(struct ms_dlx_data *mdd)
813 sipe_utils_slist_free_full(mdd->search_rows, g_free);
814 sipe_svc_session_close(mdd->session);
815 g_free(mdd->other);
816 g_free(mdd->wsse_security);
817 g_free(mdd);
820 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
821 #define DLX_SEARCH_ITEM \
822 "<AbEntryRequest.ChangeSearchQuery>" \
823 " <SearchOn>%s</SearchOn>" \
824 " <Value>%s</Value>" \
825 "</AbEntryRequest.ChangeSearchQuery>"
827 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
828 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
829 guint i = 0;
830 gchar *query = NULL;
832 while (query_rows) {
833 gchar *attr;
834 gchar *value;
836 attr = query_rows->data;
837 query_rows = g_slist_next(query_rows);
838 value = query_rows->data;
839 query_rows = g_slist_next(query_rows);
841 if (!attr || !value)
842 break;
844 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
845 attr, value);
847 attrs[i] = NULL;
849 if (i) {
850 query = g_strjoinv(NULL, attrs);
851 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
852 query ? query : "");
855 g_strfreev(attrs);
857 return query;
860 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
861 const gchar *base_uri,
862 const gchar *auth_uri,
863 const gchar *wsse_security,
864 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
865 gpointer callback_data)
867 struct ms_dlx_data *mdd = callback_data;
869 if (wsse_security) {
870 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
872 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
873 base_uri);
875 if (sipe_svc_ab_entry_request(sipe_private,
876 mdd->session,
877 auth_uri,
878 wsse_security,
879 query,
880 g_slist_length(mdd->search_rows) / 2,
881 mdd->max_returns,
882 mdd->callback,
883 mdd)) {
885 /* keep webticket security token for potential further use */
886 mdd->wsse_security = g_strdup(wsse_security);
888 /* callback data passed down the line */
889 mdd = NULL;
891 g_free(query);
893 } else {
894 /* no ticket: this will show the minmum information */
895 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
896 base_uri);
899 if (mdd)
900 mdd->failed_callback(sipe_private, mdd);
903 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
904 struct ms_dlx_data *mdd)
906 if (!sipe_webticket_request(sipe_private,
907 mdd->session,
908 sipe_private->dlx_uri,
909 "AddressBookWebTicketBearer",
910 ms_dlx_webticket,
911 mdd)) {
912 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
913 sipe_private->dlx_uri);
914 mdd->failed_callback(sipe_private, mdd);
918 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
919 struct sipe_backend_search_results *results,
920 guint match_count,
921 gboolean more)
923 gchar *secondary = g_strdup_printf(
924 dngettext(PACKAGE_NAME,
925 "Found %d contact%s:",
926 "Found %d contacts%s:", match_count),
927 match_count, more ? _(" (more matched your query)") : "");
929 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
930 results,
931 secondary,
932 more);
933 g_free(secondary);
936 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
937 const gchar *uri,
938 SIPE_UNUSED_PARAMETER const gchar *raw,
939 sipe_xml *soap_body,
940 gpointer callback_data)
942 struct ms_dlx_data *mdd = callback_data;
944 if (soap_body) {
945 const sipe_xml *node;
946 struct sipe_backend_search_results *results;
947 GHashTable *found;
949 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
950 uri);
952 /* any matches? */
953 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
954 if (!node) {
955 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
956 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
957 mdd->token,
958 _("No contacts found"));
959 ms_dlx_free(mdd);
960 return;
963 /* OK, we found something - show the results to the user */
964 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
965 mdd->token);
966 if (!results) {
967 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
968 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
969 mdd->token,
970 _("Unable to display the search results"));
971 ms_dlx_free(mdd);
972 return;
975 /* SearchAbEntryResult can contain duplicates */
976 found = g_hash_table_new_full(g_str_hash, g_str_equal,
977 g_free, NULL);
979 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
980 const sipe_xml *attrs;
981 gchar *sip_uri = NULL;
982 gchar *displayname = NULL;
983 gchar *company = NULL;
984 gchar *country = NULL;
985 gchar *email = NULL;
987 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
988 attrs;
989 attrs = sipe_xml_twin(attrs)) {
990 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
991 "Name"));
992 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
993 "Value"));
995 if (!is_empty(value)) {
996 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
997 g_free(sip_uri);
998 sip_uri = value;
999 value = NULL;
1000 } else if (sipe_strcase_equal(name, "displayname")) {
1001 g_free(displayname);
1002 displayname = value;
1003 value = NULL;
1004 } else if (sipe_strcase_equal(name, "mail")) {
1005 g_free(email);
1006 email = value;
1007 value = NULL;
1008 } else if (sipe_strcase_equal(name, "company")) {
1009 g_free(company);
1010 company = value;
1011 value = NULL;
1012 } else if (sipe_strcase_equal(name, "country")) {
1013 g_free(country);
1014 country = value;
1015 value = NULL;
1019 g_free(value);
1020 g_free(name);
1023 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
1024 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
1025 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
1026 results,
1027 uri_parts[1],
1028 displayname,
1029 company,
1030 country,
1031 email);
1032 g_strfreev(uri_parts);
1034 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
1035 sip_uri = NULL;
1038 g_free(email);
1039 g_free(country);
1040 g_free(company);
1041 g_free(displayname);
1042 g_free(sip_uri);
1045 search_contacts_finalize(sipe_private, results,
1046 g_hash_table_size(found),
1047 FALSE);
1048 g_hash_table_destroy(found);
1049 ms_dlx_free(mdd);
1051 } else {
1052 mdd->failed_callback(sipe_private, mdd);
1056 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
1057 struct sipmsg *msg,
1058 struct transaction *trans)
1060 struct sipe_backend_search_token *token = trans->payload->data;
1061 struct sipe_backend_search_results *results;
1062 sipe_xml *searchResults;
1063 const sipe_xml *mrow;
1064 guint match_count = 0;
1065 gboolean more = FALSE;
1067 /* valid response? */
1068 if (msg->response != 200) {
1069 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1070 msg->response);
1071 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1072 token,
1073 _("Contact search failed"));
1074 return(FALSE);
1077 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
1079 /* valid XML? */
1080 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1081 if (!searchResults) {
1082 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1083 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1084 token,
1085 _("Contact search failed"));
1086 return(FALSE);
1089 /* any matches? */
1090 mrow = sipe_xml_child(searchResults, "Body/Array/row");
1091 if (!mrow) {
1092 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1093 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1094 token,
1095 _("No contacts found"));
1097 sipe_xml_free(searchResults);
1098 return(FALSE);
1101 /* OK, we found something - show the results to the user */
1102 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
1103 trans->payload->data);
1104 if (!results) {
1105 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1106 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1107 token,
1108 _("Unable to display the search results"));
1110 sipe_xml_free(searchResults);
1111 return FALSE;
1114 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
1115 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
1116 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
1117 results,
1118 uri_parts[1],
1119 sipe_xml_attribute(mrow, "displayName"),
1120 sipe_xml_attribute(mrow, "company"),
1121 sipe_xml_attribute(mrow, "country"),
1122 sipe_xml_attribute(mrow, "email"));
1123 g_strfreev(uri_parts);
1124 match_count++;
1127 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
1128 char *data = sipe_xml_data(mrow);
1129 more = (g_ascii_strcasecmp(data, "true") == 0);
1130 g_free(data);
1133 search_contacts_finalize(sipe_private, results, match_count, more);
1134 sipe_xml_free(searchResults);
1136 return(TRUE);
1139 static void search_soap_request(struct sipe_core_private *sipe_private,
1140 struct sipe_backend_search_token *token,
1141 GSList *search_rows)
1143 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
1144 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1146 payload->data = token;
1148 sip_soap_directory_search(sipe_private,
1149 100,
1150 query,
1151 process_search_contact_response,
1152 payload);
1153 g_free(query);
1156 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
1157 struct ms_dlx_data *mdd)
1159 /* error using [MS-DLX] server, retry using Active Directory */
1160 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
1161 ms_dlx_free(mdd);
1164 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
1165 struct sipe_backend_search_token *token,
1166 const gchar *given_name,
1167 const gchar *surname,
1168 const gchar *email,
1169 const gchar *company,
1170 const gchar *country)
1172 GSList *query_rows = NULL;
1174 #define ADD_QUERY_ROW(attr, val) \
1175 if (val) { \
1176 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1177 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1180 ADD_QUERY_ROW("givenName", given_name);
1181 ADD_QUERY_ROW("sn", surname);
1182 ADD_QUERY_ROW("mail", email);
1183 ADD_QUERY_ROW("company", company);
1184 ADD_QUERY_ROW("c", country);
1186 if (query_rows) {
1187 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
1188 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1190 mdd->search_rows = query_rows;
1191 mdd->max_returns = 100;
1192 mdd->callback = search_ab_entry_response;
1193 mdd->failed_callback = search_ab_entry_failed;
1194 mdd->session = sipe_svc_session_start();
1195 mdd->token = token;
1197 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
1199 } else {
1200 /* no [MS-DLX] server, use Active Directory search instead */
1201 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
1202 sipe_utils_slist_free_full(query_rows, g_free);
1204 } else
1205 sipe_backend_search_failed(sipe_public,
1206 token,
1207 _("Invalid contact search query"));
1210 static void get_info_finalize(struct sipe_core_private *sipe_private,
1211 struct sipe_backend_buddy_info *info,
1212 const gchar *uri,
1213 const gchar *server_alias,
1214 const gchar *email)
1216 sipe_backend_buddy bbuddy;
1217 struct sipe_buddy *sbuddy;
1218 gchar *alias;
1219 gchar *value;
1221 if (!info) {
1222 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1223 } else {
1224 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
1226 if (!info)
1227 return;
1229 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
1231 if (is_empty(server_alias)) {
1232 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
1233 bbuddy);
1234 if (value) {
1235 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1236 info,
1237 SIPE_BUDDY_INFO_DISPLAY_NAME,
1238 value);
1240 } else {
1241 value = g_strdup(server_alias);
1244 /* present alias if it differs from server alias */
1245 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
1246 if (alias && !sipe_strequal(alias, value))
1248 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1249 info,
1250 SIPE_BUDDY_INFO_ALIAS,
1251 alias);
1253 g_free(alias);
1254 g_free(value);
1256 if (is_empty(email)) {
1257 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1258 bbuddy,
1259 SIPE_BUDDY_INFO_EMAIL);
1260 if (value) {
1261 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1262 info,
1263 SIPE_BUDDY_INFO_EMAIL,
1264 value);
1265 g_free(value);
1269 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1270 bbuddy,
1271 SIPE_BUDDY_INFO_SITE);
1272 if (value) {
1273 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1274 info,
1275 SIPE_BUDDY_INFO_SITE,
1276 value);
1277 g_free(value);
1280 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
1281 if (sbuddy && sbuddy->device_name) {
1282 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1283 info,
1284 SIPE_BUDDY_INFO_DEVICE,
1285 sbuddy->device_name);
1288 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
1292 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
1293 const gchar *uri,
1294 SIPE_UNUSED_PARAMETER const gchar *raw,
1295 sipe_xml *soap_body,
1296 gpointer callback_data)
1298 struct ms_dlx_data *mdd = callback_data;
1299 struct sipe_backend_buddy_info *info = NULL;
1300 gchar *server_alias = NULL;
1301 gchar *email = NULL;
1303 if (soap_body) {
1304 const sipe_xml *node;
1306 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1307 uri);
1309 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1311 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1312 node;
1313 node = sipe_xml_twin(node)) {
1314 gchar *name = sipe_xml_data(sipe_xml_child(node,
1315 "Name"));
1316 gchar *value = sipe_xml_data(sipe_xml_child(node,
1317 "Value"));
1318 const sipe_xml *values = sipe_xml_child(node,
1319 "Values");
1321 /* Single value entries */
1322 if (!is_empty(value)) {
1324 if (sipe_strcase_equal(name, "displayname")) {
1325 g_free(server_alias);
1326 server_alias = value;
1327 value = NULL;
1328 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1329 info,
1330 SIPE_BUDDY_INFO_DISPLAY_NAME,
1331 server_alias);
1332 } else if (sipe_strcase_equal(name, "mail")) {
1333 g_free(email);
1334 email = value;
1335 value = NULL;
1336 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1337 info,
1338 SIPE_BUDDY_INFO_EMAIL,
1339 email);
1340 } else if (sipe_strcase_equal(name, "title")) {
1341 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1342 info,
1343 SIPE_BUDDY_INFO_JOB_TITLE,
1344 value);
1345 } else if (sipe_strcase_equal(name, "company")) {
1346 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1347 info,
1348 SIPE_BUDDY_INFO_COMPANY,
1349 value);
1350 } else if (sipe_strcase_equal(name, "country")) {
1351 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1352 info,
1353 SIPE_BUDDY_INFO_COUNTRY,
1354 value);
1357 } else if (values) {
1358 gchar *first = sipe_xml_data(sipe_xml_child(values,
1359 "string"));
1361 if (sipe_strcase_equal(name, "telephonenumber")) {
1362 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1363 info,
1364 SIPE_BUDDY_INFO_WORK_PHONE,
1365 first);
1368 g_free(first);
1371 g_free(value);
1372 g_free(name);
1376 /* this will show the minmum information */
1377 get_info_finalize(sipe_private,
1378 info,
1379 mdd->other,
1380 server_alias,
1381 email);
1383 g_free(email);
1384 g_free(server_alias);
1385 ms_dlx_free(mdd);
1388 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1389 struct sipmsg *msg,
1390 struct transaction *trans)
1392 const gchar *uri = trans->payload->data;
1393 struct sipe_backend_buddy_info *info = NULL;
1394 gchar *server_alias = NULL;
1395 gchar *email = NULL;
1397 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1398 uri, sipe_private->username);
1400 if (msg->response != 200) {
1401 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1402 } else {
1403 sipe_xml *searchResults;
1404 const sipe_xml *mrow;
1406 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1407 msg->body ? msg->body : "");
1409 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1410 if (!searchResults) {
1412 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1414 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1415 const gchar *value;
1416 gchar *phone_number;
1418 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1420 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1421 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1422 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1425 * For 2007 system we will take this from ContactCard -
1426 * it has cleaner tel: URIs at least
1428 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1429 char *tel_uri = sip_to_tel_uri(phone_number);
1430 /* trims its parameters, so call first */
1431 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1432 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1433 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1434 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1435 g_free(tel_uri);
1437 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1438 uri);
1441 if (!is_empty(server_alias)) {
1442 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1443 info,
1444 SIPE_BUDDY_INFO_DISPLAY_NAME,
1445 server_alias);
1447 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1448 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1449 info,
1450 SIPE_BUDDY_INFO_JOB_TITLE,
1451 value);
1453 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1454 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1455 info,
1456 SIPE_BUDDY_INFO_OFFICE,
1457 value);
1459 if (!is_empty(phone_number)) {
1460 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1461 info,
1462 SIPE_BUDDY_INFO_WORK_PHONE,
1463 phone_number);
1465 g_free(phone_number);
1466 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1467 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1468 info,
1469 SIPE_BUDDY_INFO_COMPANY,
1470 value);
1472 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1473 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1474 info,
1475 SIPE_BUDDY_INFO_CITY,
1476 value);
1478 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1479 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1480 info,
1481 SIPE_BUDDY_INFO_STATE,
1482 value);
1484 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1485 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1486 info,
1487 SIPE_BUDDY_INFO_COUNTRY,
1488 value);
1490 if (!is_empty(email)) {
1491 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1492 info,
1493 SIPE_BUDDY_INFO_EMAIL,
1494 email);
1497 sipe_xml_free(searchResults);
1500 /* this will show the minmum information */
1501 get_info_finalize(sipe_private,
1502 info,
1503 uri,
1504 server_alias,
1505 email);
1507 g_free(server_alias);
1508 g_free(email);
1510 return TRUE;
1513 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1514 struct ms_dlx_data *mdd)
1516 /* error using [MS-DLX] server, retry using Active Directory */
1517 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1518 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1520 payload->destroy = g_free;
1521 payload->data = mdd->other;
1522 mdd->other = NULL;
1524 sip_soap_directory_search(sipe_private,
1526 query,
1527 process_get_info_response,
1528 payload);
1530 ms_dlx_free(mdd);
1531 g_free(query);
1534 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1535 const gchar *who)
1537 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1539 if (sipe_private->dlx_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(who));
1545 mdd->other = g_strdup(who);
1546 mdd->max_returns = 1;
1547 mdd->callback = get_info_ab_entry_response;
1548 mdd->failed_callback = get_info_ab_entry_failed;
1549 mdd->session = sipe_svc_session_start();
1551 ms_dlx_webticket_request(sipe_private, mdd);
1553 } else {
1554 /* no [MS-DLX] server, use Active Directory search instead */
1555 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1556 "msRTCSIP-PrimaryUserAddress",
1557 who);
1558 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1560 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1561 row ? row : "");
1563 payload->destroy = g_free;
1564 payload->data = g_strdup(who);
1566 sip_soap_directory_search(sipe_private,
1568 row,
1569 process_get_info_response,
1570 payload);
1571 g_free(row);
1575 static void photo_response_data_free(struct photo_response_data *data)
1577 g_free(data->who);
1578 g_free(data->photo_hash);
1579 if (data->request) {
1580 sipe_http_request_cancel(data->request);
1582 g_free(data);
1585 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1586 guint status,
1587 GSList *headers,
1588 const char *body,
1589 gpointer data)
1591 struct photo_response_data *rdata = (struct photo_response_data *) data;
1593 rdata->request = NULL;
1595 if (status == SIPE_HTTP_STATUS_OK) {
1596 const gchar *len_str = sipe_utils_nameval_find(headers,
1597 "Content-Length");
1598 if (len_str) {
1599 gsize photo_size = atoi(len_str);
1600 gpointer photo = g_new(char, photo_size);
1602 if (photo) {
1603 memcpy(photo, body, photo_size);
1605 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1606 rdata->who,
1607 photo,
1608 photo_size,
1609 rdata->photo_hash);
1614 sipe_private->buddies->pending_photo_requests =
1615 g_slist_remove(sipe_private->buddies->pending_photo_requests, rdata);
1617 photo_response_data_free(rdata);
1620 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1622 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1623 gchar *wsse_security_base64;
1624 gchar *x_ms_webticket_header;
1626 if (!assertion) {
1627 return NULL;
1630 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1631 strlen(assertion));
1632 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1633 wsse_security_base64);
1635 g_free(assertion);
1636 g_free(wsse_security_base64);
1638 return x_ms_webticket_header;
1641 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1642 const gchar *uri,
1643 SIPE_UNUSED_PARAMETER const gchar *raw,
1644 sipe_xml *soap_body,
1645 gpointer callback_data)
1647 struct ms_dlx_data *mdd = callback_data;
1648 gchar *photo_rel_path = NULL;
1649 gchar *photo_hash = NULL;
1650 const gchar *photo_hash_old =
1651 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1653 if (soap_body) {
1654 const sipe_xml *node;
1656 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1657 uri);
1659 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1660 node;
1661 node = sipe_xml_twin(node)) {
1662 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1663 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1665 if (!is_empty(value)) {
1666 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1667 g_free(photo_rel_path);
1668 photo_rel_path = value;
1669 value = NULL;
1670 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1671 g_free(photo_hash);
1672 photo_hash = value;
1673 value = NULL;
1677 g_free(value);
1678 g_free(name);
1682 if (sipe_private->addressbook_uri && photo_rel_path &&
1683 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1684 gchar *photo_url = g_strdup_printf("%s/%s",
1685 sipe_private->addressbook_uri, photo_rel_path);
1686 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1688 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1689 data->who = g_strdup(mdd->other);
1690 data->photo_hash = photo_hash;
1691 photo_hash = NULL;
1693 data->request = sipe_http_request_get(sipe_private,
1694 photo_url,
1695 x_ms_webticket_header,
1696 process_buddy_photo_response,
1697 data);
1699 if (data->request) {
1700 sipe_private->buddies->pending_photo_requests =
1701 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1702 sipe_http_request_ready(data->request);
1703 } else {
1704 photo_response_data_free(data);
1707 g_free(x_ms_webticket_header);
1708 g_free(photo_url);
1711 g_free(photo_rel_path);
1712 g_free(photo_hash);
1713 ms_dlx_free(mdd);
1716 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1717 struct ms_dlx_data *mdd)
1719 ms_dlx_free(mdd);
1722 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1723 const gchar *uri)
1725 if (sipe_backend_uses_photo()) {
1727 /* Lync 2013 or newer: use UCS */
1728 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013)) {
1730 sipe_ucs_get_photo(sipe_private, uri);
1732 /* Lync 2010: use [MS-DLX] */
1733 } else if (sipe_private->dlx_uri &&
1734 sipe_private->addressbook_uri) {
1735 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1737 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1738 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1740 mdd->other = g_strdup(uri);
1741 mdd->max_returns = 1;
1742 mdd->callback = get_photo_ab_entry_response;
1743 mdd->failed_callback = get_photo_ab_entry_failed;
1744 mdd->session = sipe_svc_session_start();
1746 ms_dlx_webticket_request(sipe_private, mdd);
1751 static void buddy_refresh_photos_cb(gpointer uri,
1752 SIPE_UNUSED_PARAMETER gpointer value,
1753 gpointer sipe_private)
1755 buddy_fetch_photo(sipe_private, uri);
1758 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1760 g_hash_table_foreach(sipe_private->buddies->uri,
1761 buddy_refresh_photos_cb,
1762 sipe_private);
1765 /* Buddy menu callbacks*/
1767 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1768 const gchar *who)
1770 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1772 /* 2007+ conference */
1773 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1774 sipe_conf_add(sipe_private, who);
1776 /* 2005- multiparty chat */
1777 } else {
1778 gchar *self = sip_uri_self(sipe_private);
1779 struct sip_session *session;
1781 session = sipe_session_add_chat(sipe_private,
1782 NULL,
1783 TRUE,
1784 self);
1785 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1786 session->chat_session,
1787 session->chat_session->title,
1788 self);
1789 g_free(self);
1791 sipe_im_invite(sipe_private, session, who,
1792 NULL, NULL, NULL, FALSE);
1796 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1797 const gchar *who)
1799 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1800 who,
1801 NULL);
1802 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1803 buddy,
1804 SIPE_BUDDY_INFO_EMAIL);
1806 if (email) {
1807 gchar *command_line = g_strdup_printf(
1808 #ifdef _WIN32
1809 "cmd /c start"
1810 #else
1811 "xdg-email"
1812 #endif
1813 " mailto:%s", email);
1814 g_free(email);
1816 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1817 command_line);
1818 g_spawn_command_line_async(command_line, NULL);
1819 g_free(command_line);
1821 } else {
1822 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1823 who);
1827 /* Buddy menu */
1829 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1830 struct sipe_backend_buddy_menu *menu,
1831 sipe_backend_buddy buddy,
1832 sipe_buddy_info_fields id_phone,
1833 sipe_buddy_info_fields id_display,
1834 const gchar *type)
1836 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1837 buddy,
1838 id_phone);
1839 if (phone) {
1840 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1841 buddy,
1842 id_display);
1843 gchar *tmp = NULL;
1844 gchar *label = g_strdup_printf("%s %s",
1845 type,
1846 display ? display :
1847 (tmp = sip_tel_uri_denormalize(phone)));
1848 menu = sipe_backend_buddy_menu_add(sipe_public,
1849 menu,
1850 label,
1851 SIPE_BUDDY_MENU_MAKE_CALL,
1852 phone);
1853 g_free(tmp);
1854 g_free(label);
1855 g_free(display);
1856 g_free(phone);
1859 return(menu);
1862 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1863 const gchar *buddy_name,
1864 struct sipe_backend_buddy_menu *menu)
1866 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1867 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1868 buddy_name,
1869 NULL);
1870 gchar *self = sip_uri_self(sipe_private);
1872 SIPE_SESSION_FOREACH {
1873 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1875 struct sipe_chat_session *chat_session = session->chat_session;
1876 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1878 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1880 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1882 if (is_conf &&
1883 /* Not conf OP */
1884 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1885 /* We are a conf OP */
1886 conf_op) {
1887 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1888 chat_session->title);
1889 menu = sipe_backend_buddy_menu_add(sipe_public,
1890 menu,
1891 label,
1892 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1893 chat_session);
1894 g_free(label);
1897 if (is_conf &&
1898 /* We are a conf OP */
1899 conf_op) {
1900 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1901 chat_session->title);
1902 menu = sipe_backend_buddy_menu_add(sipe_public,
1903 menu,
1904 label,
1905 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1906 chat_session);
1907 g_free(label);
1910 else
1912 if (!is_conf ||
1913 (is_conf && !session->locked)) {
1914 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1915 chat_session->title);
1916 menu = sipe_backend_buddy_menu_add(sipe_public,
1917 menu,
1918 label,
1919 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1920 chat_session);
1921 g_free(label);
1925 } SIPE_SESSION_FOREACH_END;
1926 g_free(self);
1928 menu = sipe_backend_buddy_menu_add(sipe_public,
1929 menu,
1930 _("New chat"),
1931 SIPE_BUDDY_MENU_NEW_CHAT,
1932 NULL);
1934 /* add buddy's phone numbers if we have call control */
1935 if (sip_csta_is_idle(sipe_private)) {
1937 /* work phone */
1938 menu = buddy_menu_phone(sipe_public,
1939 menu,
1940 buddy,
1941 SIPE_BUDDY_INFO_WORK_PHONE,
1942 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1943 _("Work"));
1944 /* mobile phone */
1945 menu = buddy_menu_phone(sipe_public,
1946 menu,
1947 buddy,
1948 SIPE_BUDDY_INFO_MOBILE_PHONE,
1949 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1950 _("Mobile"));
1952 /* home phone */
1953 menu = buddy_menu_phone(sipe_public,
1954 menu,
1955 buddy,
1956 SIPE_BUDDY_INFO_HOME_PHONE,
1957 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1958 _("Home"));
1960 /* other phone */
1961 menu = buddy_menu_phone(sipe_public,
1962 menu,
1963 buddy,
1964 SIPE_BUDDY_INFO_OTHER_PHONE,
1965 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1966 _("Other"));
1968 /* custom1 phone */
1969 menu = buddy_menu_phone(sipe_public,
1970 menu,
1971 buddy,
1972 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1973 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1974 _("Custom1"));
1978 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1979 buddy,
1980 SIPE_BUDDY_INFO_EMAIL);
1981 if (email) {
1982 menu = sipe_backend_buddy_menu_add(sipe_public,
1983 menu,
1984 _("Send email..."),
1985 SIPE_BUDDY_MENU_SEND_EMAIL,
1986 NULL);
1987 g_free(email);
1991 /* access level control */
1992 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1993 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1994 menu,
1995 _("Access level"),
1996 sipe_ocs2007_access_control_menu(sipe_private,
1997 buddy_name));
1999 return(menu);
2002 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
2004 return(g_hash_table_size(sipe_private->buddies->uri));
2007 static guint sipe_ht_hash_nick(const char *nick)
2009 char *lc = g_utf8_strdown(nick, -1);
2010 guint bucket = g_str_hash(lc);
2011 g_free(lc);
2013 return bucket;
2016 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
2018 char *nick1_norm = NULL;
2019 char *nick2_norm = NULL;
2020 gboolean equal;
2022 if (nick1 == NULL && nick2 == NULL) return TRUE;
2023 if (nick1 == NULL || nick2 == NULL ||
2024 !g_utf8_validate(nick1, -1, NULL) ||
2025 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
2027 nick1_norm = g_utf8_casefold(nick1, -1);
2028 nick2_norm = g_utf8_casefold(nick2, -1);
2029 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
2030 g_free(nick2_norm);
2031 g_free(nick1_norm);
2033 return equal;
2036 void sipe_buddy_init(struct sipe_core_private *sipe_private)
2038 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
2039 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
2040 (GEqualFunc) sipe_ht_equals_nick);
2041 buddies->exchange_key = g_hash_table_new(g_str_hash,
2042 g_str_equal);
2043 sipe_private->buddies = buddies;
2047 Local Variables:
2048 mode: c
2049 c-file-style: "bsd"
2050 indent-tabs-mode: t
2051 tab-width: 8
2052 End: