buddy: fix compilation error
[siplcs.git] / src / core / sipe-buddy.c
blobd122d2a103939eaefaae62e22cebded1879cc12a
1 /**
2 * @file sipe-buddy.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2014 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;
71 gboolean is_obsolete;
74 struct photo_response_data {
75 gchar *who;
76 gchar *photo_hash;
77 struct sipe_http_request *request;
80 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
81 const gchar *uri);
82 static void photo_response_data_free(struct photo_response_data *data);
84 void sipe_buddy_add_keys(struct sipe_core_private *sipe_private,
85 struct sipe_buddy *buddy,
86 const gchar *exchange_key,
87 const gchar *change_key)
89 if (exchange_key) {
90 buddy->exchange_key = g_strdup(exchange_key);
91 g_hash_table_insert(sipe_private->buddies->exchange_key,
92 buddy->exchange_key,
93 buddy);
95 if (change_key)
96 buddy->change_key = g_strdup(change_key);
99 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
100 const gchar *uri,
101 const gchar *exchange_key,
102 const gchar *change_key)
104 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
105 gchar *normalized_uri = g_ascii_strdown(uri, -1);
106 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
107 normalized_uri);
109 if (!buddy) {
110 buddy = g_new0(struct sipe_buddy, 1);
111 buddy->name = normalized_uri;
112 g_hash_table_insert(sipe_private->buddies->uri,
113 buddy->name,
114 buddy);
116 sipe_buddy_add_keys(sipe_private,
117 buddy,
118 exchange_key,
119 change_key);
121 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri);
123 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
124 buddy->just_added = TRUE;
125 sipe_subscribe_presence_single_cb(sipe_private,
126 buddy->name);
129 buddy_fetch_photo(sipe_private, normalized_uri);
131 normalized_uri = NULL; /* buddy takes ownership */
132 } else {
133 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri);
134 buddy->is_obsolete = FALSE;
136 g_free(normalized_uri);
138 return(buddy);
141 static gboolean is_buddy_in_group(struct sipe_buddy *buddy,
142 const gchar *name)
144 if (buddy) {
145 GSList *entry = buddy->groups;
147 while (entry) {
148 struct buddy_group_data *bgd = entry->data;
149 if (sipe_strequal(bgd->group->name, name)) {
150 bgd->is_obsolete = FALSE;
151 return(TRUE);
153 entry = entry->next;
157 return(FALSE);
160 void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private,
161 struct sipe_buddy *buddy,
162 struct sipe_group *group,
163 const gchar *alias)
165 const gchar *uri = buddy->name;
166 const gchar *group_name = group->name;
167 sipe_backend_buddy bb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
168 uri,
169 group_name);
171 if (!bb) {
172 bb = sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
173 uri,
174 alias,
175 group_name);
176 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
177 uri, alias ? alias : "<NONE>");
181 if (!is_empty(alias)) {
182 gchar *old_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
183 bb);
185 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri),
186 old_alias)) {
187 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
189 alias);
190 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
191 uri, old_alias, alias);
193 g_free(old_alias);
196 if (!is_buddy_in_group(buddy, group_name)) {
197 sipe_buddy_insert_group(buddy, group);
198 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
199 uri, group_name);
203 static gint buddy_group_compare(gconstpointer a, gconstpointer b)
205 return(((const struct buddy_group_data *)a)->group->id -
206 ((const struct buddy_group_data *)b)->group->id);
209 void sipe_buddy_insert_group(struct sipe_buddy *buddy,
210 struct sipe_group *group)
212 struct buddy_group_data *bgd = g_new0(struct buddy_group_data, 1);
214 bgd->group = group;
216 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
217 bgd,
218 buddy_group_compare,
219 NULL);
222 static void buddy_group_free(gpointer data)
224 g_free(data);
227 static void buddy_group_remove(struct sipe_buddy *buddy,
228 struct buddy_group_data *bgd)
230 buddy->groups = g_slist_remove(buddy->groups, bgd);
231 buddy_group_free(bgd);
234 static void sipe_buddy_remove_group(struct sipe_buddy *buddy,
235 const struct sipe_group *group)
237 GSList *entry = buddy->groups;
238 struct buddy_group_data *bgd = NULL;
240 while (entry) {
241 bgd = entry->data;
242 if (bgd->group == group)
243 break;
244 entry = entry->next;
247 buddy_group_remove(buddy, bgd);
250 void sipe_buddy_update_groups(struct sipe_core_private *sipe_private,
251 struct sipe_buddy *buddy,
252 GSList *new_groups)
254 const gchar *uri = buddy->name;
255 GSList *entry = buddy->groups;
257 while (entry) {
258 struct buddy_group_data *bgd = entry->data;
259 const struct sipe_group *group = bgd->group;
261 /* next buddy group */
262 entry = entry->next;
264 /* old group NOT found in new list? */
265 if (g_slist_find(new_groups, group) == NULL) {
266 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
267 uri,
268 group->name);
269 SIPE_DEBUG_INFO("sipe_buddy_update_groups: removing buddy %s from group '%s'",
270 uri, group->name);
271 /* this should never be NULL */
272 if (oldb)
273 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
274 oldb);
275 buddy_group_remove(buddy, bgd);
280 gchar *sipe_buddy_groups_string(struct sipe_buddy *buddy)
282 guint i = 0;
283 gchar *string;
284 /* creating array from GList, converting guint to gchar * */
285 gchar **ids_arr = g_new(gchar *, g_slist_length(buddy->groups) + 1);
286 GSList *entry = buddy->groups;
288 if (!ids_arr)
289 return(NULL);
291 while (entry) {
292 const struct sipe_group *group = ((struct buddy_group_data *) entry->data)->group;
293 ids_arr[i] = g_strdup_printf("%u", group->id);
294 entry = entry->next;
295 i++;
297 ids_arr[i] = NULL;
299 string = g_strjoinv(" ", ids_arr);
300 g_strfreev(ids_arr);
302 return(string);
305 void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private)
307 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
308 NULL,
309 NULL);
310 GSList *entry = buddies;
312 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
313 g_slist_length(buddies));
314 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
315 sipe_buddy_count(sipe_private));
316 while (entry) {
317 sipe_backend_buddy bb = entry->data;
318 gchar *bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC,
319 bb);
320 gchar *gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC,
321 bb);
322 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
323 bname);
325 if (!is_buddy_in_group(buddy, gname)) {
326 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",
327 bname, gname);
328 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, bb);
331 g_free(gname);
332 g_free(bname);
334 entry = entry->next;
337 g_slist_free(buddies);
340 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
341 const gchar *uri)
343 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
346 struct sipe_buddy *sipe_buddy_find_by_exchange_key(struct sipe_core_private *sipe_private,
347 const gchar *exchange_key)
349 return(g_hash_table_lookup(sipe_private->buddies->exchange_key,
350 exchange_key));
353 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
354 GHFunc callback,
355 gpointer callback_data)
357 g_hash_table_foreach(sipe_private->buddies->uri,
358 callback,
359 callback_data);
362 static void buddy_free(struct sipe_buddy *buddy)
364 #ifndef _WIN32
366 * We are calling g_hash_table_foreach_steal(). That means that no
367 * key/value deallocation functions are called. Therefore the glib
368 * hash code does not touch the key (buddy->name) or value (buddy)
369 * of the to-be-deleted hash node at all. It follows that we
371 * - MUST free the memory for the key ourselves and
372 * - ARE allowed to do it in this function
374 * Conclusion: glib must be broken on the Windows platform if sipe
375 * crashes with SIGTRAP when closing. You'll have to live
376 * with the memory leak until this is fixed.
378 g_free(buddy->name);
379 #endif
380 g_free(buddy->exchange_key);
381 g_free(buddy->change_key);
382 g_free(buddy->activity);
383 g_free(buddy->meeting_subject);
384 g_free(buddy->meeting_location);
385 g_free(buddy->note);
387 g_free(buddy->cal_start_time);
388 g_free(buddy->cal_free_busy_base64);
389 g_free(buddy->cal_free_busy);
390 g_free(buddy->last_non_cal_activity);
392 sipe_cal_free_working_hours(buddy->cal_working_hours);
394 g_free(buddy->device_name);
395 sipe_utils_slist_free_full(buddy->groups, buddy_group_free);
396 g_free(buddy);
399 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
400 gpointer buddy,
401 SIPE_UNUSED_PARAMETER gpointer user_data)
403 buddy_free(buddy);
404 /* We must return TRUE as the key/value have already been deleted */
405 return(TRUE);
408 void sipe_buddy_free(struct sipe_core_private *sipe_private)
410 struct sipe_buddies *buddies = sipe_private->buddies;
412 g_hash_table_foreach_steal(buddies->uri,
413 buddy_free_cb,
414 NULL);
416 /* core is being deallocated, remove all its pending photo requests */
417 while (buddies->pending_photo_requests) {
418 struct photo_response_data *data =
419 buddies->pending_photo_requests->data;
420 buddies->pending_photo_requests =
421 g_slist_remove(buddies->pending_photo_requests, data);
422 photo_response_data_free(data);
425 g_hash_table_destroy(buddies->uri);
426 g_hash_table_destroy(buddies->exchange_key);
427 g_free(buddies);
428 sipe_private->buddies = NULL;
431 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
432 gpointer value,
433 SIPE_UNUSED_PARAMETER gpointer user_data)
435 struct sipe_buddy *buddy = value;
436 GSList *entry = buddy->groups;
438 buddy->is_obsolete = TRUE;
439 while (entry) {
440 ((struct buddy_group_data *) entry->data)->is_obsolete = TRUE;
441 entry = entry->next;
445 void sipe_buddy_update_start(struct sipe_core_private *sipe_private)
447 g_hash_table_foreach(sipe_private->buddies->uri,
448 buddy_set_obsolete_flag,
449 NULL);
452 static gboolean buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
453 gpointer value,
454 gpointer user_data)
456 struct sipe_core_private *sipe_private = user_data;
457 struct sipe_buddy *buddy = value;
458 const gchar *uri = buddy->name;
460 if (buddy->is_obsolete) {
461 /* all backend buddies in different groups */
462 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
463 uri,
464 NULL);
465 GSList *entry = buddies;
467 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
468 g_slist_length(buddies),
469 uri);
471 while (entry) {
472 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
473 entry->data);
474 entry = entry->next;
476 g_slist_free(buddies);
478 buddy_free(buddy);
479 /* return TRUE as the key/value have already been deleted */
480 return(TRUE);
482 } else {
483 GSList *entry = buddy->groups;
485 while (entry) {
486 struct buddy_group_data *bgd = entry->data;
488 /* next buddy group */
489 entry = entry->next;
491 if (bgd->is_obsolete) {
492 const struct sipe_group *group = bgd->group;
493 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
494 uri,
495 group->name);
496 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: removing buddy '%s' from group '%s'",
497 uri, group->name);
498 /* this should never be NULL */
499 if (oldb)
500 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
501 oldb);
502 buddy_group_remove(buddy, bgd);
505 return(FALSE);
509 void sipe_buddy_update_finish(struct sipe_core_private *sipe_private)
511 g_hash_table_foreach_remove(sipe_private->buddies->uri,
512 buddy_check_obsolete_flag,
513 sipe_private);
516 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
517 const gchar *uri,
518 guint activity,
519 const gchar *status_text)
521 struct sipe_buddy *sbuddy;
522 GString *status;
524 if (!sipe_public) return NULL; /* happens on pidgin exit */
526 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
527 if (!sbuddy) return NULL;
529 status = g_string_new(sbuddy->activity ? sbuddy->activity :
530 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
531 status_text : NULL);
533 if (sbuddy->is_mobile) {
534 if (status->len)
535 g_string_append(status, " - ");
536 g_string_append(status, _("Mobile"));
539 if (sbuddy->note) {
540 if (status->len)
541 g_string_append(status, " - ");
542 g_string_append(status, sbuddy->note);
545 return(g_string_free(status, FALSE));
548 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
549 const gchar *with)
551 sipe_backend_buddy pbuddy;
552 gchar *alias = NULL;
553 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
554 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
556 return alias;
559 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
560 const gchar *who,
561 const gchar *old_group_name,
562 const gchar *new_group_name)
564 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
565 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
566 who);
567 struct sipe_group *old_group = NULL;
568 struct sipe_group *new_group;
569 struct sipe_ucs_transaction *ucs_trans = NULL;
571 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' old group '%s' new group '%s'",
572 who ? who : "",
573 old_group_name ? old_group_name : "<UNDEFINED>",
574 new_group_name ? new_group_name : "<UNDEFINED>");
576 if (!buddy)
577 /* buddy not in roaming list */
578 return;
580 old_group = sipe_group_find_by_name(sipe_private, old_group_name);
581 if (old_group) {
582 sipe_buddy_remove_group(buddy, old_group);
583 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' removed from old group '%s'",
584 who, old_group_name);
587 new_group = sipe_group_find_by_name(sipe_private, new_group_name);
588 if (new_group) {
589 sipe_buddy_insert_group(buddy, new_group);
590 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' added to new group '%s'",
591 who, new_group_name);
594 if (sipe_ucs_is_migrated(sipe_private)) {
596 /* UCS handling */
597 ucs_trans = sipe_ucs_transaction(sipe_private);
599 if (new_group) {
601 * 1. new buddy added to existing group
602 * 2. existing buddy moved from old to existing group
604 sipe_ucs_group_add_buddy(sipe_private,
605 ucs_trans,
606 new_group,
607 buddy,
608 buddy->name);
609 if (old_group)
610 sipe_ucs_group_remove_buddy(sipe_private,
611 ucs_trans,
612 old_group,
613 buddy);
615 } else if (old_group) {
617 * 3. existing buddy removed from one of its groups
618 * 4. existing buddy removed from last group
620 sipe_ucs_group_remove_buddy(sipe_private,
621 ucs_trans,
622 old_group,
623 buddy);
624 if (g_slist_length(buddy->groups) < 1)
625 sipe_buddy_remove(sipe_private,
626 buddy);
627 /* buddy no longer valid */
630 /* non-UCS handling */
631 } else if (new_group)
632 sipe_group_update_buddy(sipe_private, buddy);
634 /* 5. buddy added to new group */
635 if (!new_group)
636 sipe_group_create(sipe_private,
637 ucs_trans,
638 new_group_name,
639 who);
642 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
643 const gchar *uri,
644 const gchar *group_name)
646 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
648 if (!sipe_buddy_find_by_uri(sipe_private, uri))
649 sipe_buddy_add(sipe_private,
650 uri,
651 NULL,
652 NULL);
653 else
654 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
655 uri);
657 sipe_core_buddy_group(sipe_public,
658 uri,
659 NULL,
660 group_name);
663 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
664 struct sipe_buddy *buddy)
666 struct sipe_buddies *buddies = sipe_private->buddies;
667 const gchar *uri = buddy->name;
668 GSList *entry = buddy->groups;
669 gchar *action_name = sipe_utils_presence_key(uri);
671 sipe_schedule_cancel(sipe_private, action_name);
672 g_free(action_name);
674 /* If the buddy still has groups, we need to delete backend buddies */
675 while (entry) {
676 const struct sipe_group *group = ((struct buddy_group_data *) entry->data)->group;
677 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
678 uri,
679 group->name);
680 /* this should never be NULL */
681 if (oldb)
682 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, oldb);
684 entry = entry->next;
687 g_hash_table_remove(buddies->uri, uri);
688 if (buddy->exchange_key)
689 g_hash_table_remove(buddies->exchange_key,
690 buddy->exchange_key);
692 buddy_free(buddy);
696 * Unassociates buddy from group first.
697 * Then see if no groups left, removes buddy completely.
698 * Otherwise updates buddy groups on server.
700 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
701 const gchar *uri,
702 const gchar *group_name)
704 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
705 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
706 uri);
707 struct sipe_group *group = NULL;
709 if (!buddy) return;
711 if (group_name) {
712 group = sipe_group_find_by_name(sipe_private, group_name);
713 if (group) {
714 sipe_buddy_remove_group(buddy, group);
715 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy '%s' removed from group '%s'",
716 uri, group->name);
720 if (g_slist_length(buddy->groups) < 1) {
722 if (sipe_ucs_is_migrated(sipe_private)) {
723 sipe_ucs_group_remove_buddy(sipe_private,
724 NULL,
725 group,
726 buddy);
727 } else {
728 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
729 buddy->name);
730 sip_soap_request(sipe_private,
731 "deleteContact",
732 request);
733 g_free(request);
736 sipe_buddy_remove(sipe_private, buddy);
737 } else {
738 if (sipe_ucs_is_migrated(sipe_private)) {
739 sipe_ucs_group_remove_buddy(sipe_private,
740 NULL,
741 group,
742 buddy);
743 } else
744 /* updates groups on server */
745 sipe_group_update_buddy(sipe_private, buddy);
749 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
750 const gchar *uri,
751 guint activity)
753 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
754 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
755 uri);
757 if (!sbuddy) return;
759 /* Check if on 2005 system contact's calendar,
760 * then set/preserve it.
762 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
763 sipe_backend_buddy_set_status(sipe_public, uri, activity);
764 } else {
765 sipe_ocs2005_apply_calendar_status(sipe_private,
766 sbuddy,
767 sipe_status_activity_to_token(activity));
771 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
772 const gchar *uri,
773 const gchar *status_name,
774 gboolean is_online,
775 struct sipe_backend_buddy_tooltip *tooltip)
777 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
778 gchar *note = NULL;
779 gboolean is_oof_note = FALSE;
780 const gchar *activity = NULL;
781 gchar *calendar = NULL;
782 const gchar *meeting_subject = NULL;
783 const gchar *meeting_location = NULL;
784 gchar *access_text = NULL;
786 #define SIPE_ADD_BUDDY_INFO(l, t) \
788 gchar *tmp = g_markup_escape_text((t), -1); \
789 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
790 g_free(tmp); \
792 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
793 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
795 if (sipe_public) { /* happens on pidgin exit */
796 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
797 uri);
798 if (sbuddy) {
799 note = sbuddy->note;
800 is_oof_note = sbuddy->is_oof_note;
801 activity = sbuddy->activity;
802 calendar = sipe_cal_get_description(sbuddy);
803 meeting_subject = sbuddy->meeting_subject;
804 meeting_location = sbuddy->meeting_location;
806 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
807 gboolean is_group_access = FALSE;
808 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
809 "user",
810 sipe_get_no_sip_uri(uri),
811 &is_group_access);
812 const char *access_level = sipe_ocs2007_access_level_name(container_id);
813 access_text = is_group_access ?
814 g_strdup(access_level) :
815 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
816 access_level);
820 if (is_online) {
821 const gchar *status_str = activity ? activity : status_name;
823 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
825 if (is_online && !is_empty(calendar)) {
826 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
828 g_free(calendar);
829 if (!is_empty(meeting_location)) {
830 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
831 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
833 if (!is_empty(meeting_subject)) {
834 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
835 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
837 if (note) {
838 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
839 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
840 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
841 note_italics);
842 g_free(note_italics);
844 if (access_text) {
845 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
846 g_free(access_text);
850 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
851 const char *uri,
852 sipe_buddy_info_fields propkey,
853 char *property_value)
855 GSList *buddies, *entry;
857 if (property_value)
858 property_value = g_strstrip(property_value);
860 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
861 while (entry) {
862 gchar *prop_str;
863 sipe_backend_buddy p_buddy = entry->data;
865 /* for Display Name */
866 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
867 gchar *alias;
868 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
869 if (property_value && sipe_is_bad_alias(uri, alias)) {
870 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
871 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
873 g_free(alias);
875 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
876 if (!is_empty(property_value) &&
877 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
879 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
880 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
882 g_free(alias);
884 /* for other properties */
885 else {
886 if (!is_empty(property_value)) {
887 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
888 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
889 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
891 g_free(prop_str);
895 entry = entry->next;
897 g_slist_free(buddies);
901 struct ms_dlx_data;
902 struct ms_dlx_data {
903 GSList *search_rows;
904 gchar *other;
905 guint max_returns;
906 sipe_svc_callback *callback;
907 struct sipe_svc_session *session;
908 gchar *wsse_security;
909 struct sipe_backend_search_token *token;
910 /* must call ms_dlx_free() */
911 void (*failed_callback)(struct sipe_core_private *sipe_private,
912 struct ms_dlx_data *mdd);
915 static void free_search_rows(GSList *search_rows)
917 sipe_utils_slist_free_full(search_rows, g_free);
920 static void ms_dlx_free(struct ms_dlx_data *mdd)
922 free_search_rows(mdd->search_rows);
923 sipe_svc_session_close(mdd->session);
924 g_free(mdd->other);
925 g_free(mdd->wsse_security);
926 g_free(mdd);
929 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
930 #define DLX_SEARCH_ITEM \
931 "<AbEntryRequest.ChangeSearchQuery>" \
932 " <SearchOn>%s</SearchOn>" \
933 " <Value>%s</Value>" \
934 "</AbEntryRequest.ChangeSearchQuery>"
936 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
937 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
938 guint i = 0;
939 gchar *query = NULL;
941 while (query_rows) {
942 gchar *attr;
943 gchar *value;
944 gchar *tmp = NULL;
946 attr = query_rows->data;
947 query_rows = g_slist_next(query_rows);
948 value = query_rows->data;
949 query_rows = g_slist_next(query_rows);
951 if (!value)
952 break;
955 * Special value for SIP ID
957 * Active Directory seems only to be able to search for
958 * SIP URIs. Make sure search string starts with "sip:".
960 if (!attr) {
961 attr = "msRTCSIP-PrimaryUserAddress";
962 if (!use_dlx)
963 value = tmp = sip_uri(value);
966 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
967 attr, value);
968 g_free(tmp);
970 attrs[i] = NULL;
972 if (i) {
973 query = g_strjoinv(NULL, attrs);
974 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
975 query ? query : "");
978 g_strfreev(attrs);
980 return query;
983 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
984 const gchar *base_uri,
985 const gchar *auth_uri,
986 const gchar *wsse_security,
987 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
988 gpointer callback_data)
990 struct ms_dlx_data *mdd = callback_data;
992 if (wsse_security) {
993 guint length = g_slist_length(mdd->search_rows);
994 gchar *search;
996 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
997 base_uri);
999 if (length > 0) {
1000 /* complex search */
1001 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
1002 search = g_strdup_printf("<ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
1003 " %s"
1004 "</ChangeSearch>",
1005 length / 2,
1006 query);
1007 g_free(query);
1008 } else {
1009 /* simple search */
1010 search = g_strdup_printf("<BasicSearch>"
1011 " <SearchList>c,company,displayName,givenName,mail,mailNickname,msRTCSIP-PrimaryUserAddress,sn</SearchList>"
1012 " <Value>%s</Value>"
1013 " <Verb>BeginsWith</Verb>"
1014 "</BasicSearch>",
1015 mdd->other);
1018 if (sipe_svc_ab_entry_request(sipe_private,
1019 mdd->session,
1020 auth_uri,
1021 wsse_security,
1022 search,
1023 mdd->max_returns,
1024 mdd->callback,
1025 mdd)) {
1027 /* keep webticket security token for potential further use */
1028 g_free(mdd->wsse_security);
1029 mdd->wsse_security = g_strdup(wsse_security);
1031 /* callback data passed down the line */
1032 mdd = NULL;
1034 g_free(search);
1036 } else {
1037 /* no ticket: this will show the minmum information */
1038 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
1039 base_uri);
1042 if (mdd)
1043 mdd->failed_callback(sipe_private, mdd);
1046 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
1047 struct ms_dlx_data *mdd)
1049 if (!sipe_webticket_request(sipe_private,
1050 mdd->session,
1051 sipe_private->dlx_uri,
1052 "AddressBookWebTicketBearer",
1053 ms_dlx_webticket,
1054 mdd)) {
1055 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
1056 sipe_private->dlx_uri);
1057 mdd->failed_callback(sipe_private, mdd);
1061 void sipe_buddy_search_contacts_finalize(struct sipe_core_private *sipe_private,
1062 struct sipe_backend_search_results *results,
1063 guint match_count,
1064 gboolean more)
1066 gchar *secondary = g_strdup_printf(
1067 dngettext(PACKAGE_NAME,
1068 "Found %d contact%s:",
1069 "Found %d contacts%s:", match_count),
1070 match_count, more ? _(" (more matched your query)") : "");
1072 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
1073 results,
1074 secondary,
1075 more);
1076 g_free(secondary);
1079 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
1080 const gchar *uri,
1081 SIPE_UNUSED_PARAMETER const gchar *raw,
1082 sipe_xml *soap_body,
1083 gpointer callback_data)
1085 struct ms_dlx_data *mdd = callback_data;
1087 if (soap_body) {
1088 const sipe_xml *node;
1089 struct sipe_backend_search_results *results;
1090 GHashTable *found;
1092 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
1093 uri);
1095 /* any matches? */
1096 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
1097 if (!node) {
1098 /* try again with simple search, if possible */
1099 if (mdd->other && mdd->search_rows) {
1100 SIPE_DEBUG_INFO_NOFORMAT("search_ab_entry_response: no matches, retrying with simple search");
1102 /* throw away original search query */
1103 free_search_rows(mdd->search_rows);
1104 mdd->search_rows = NULL;
1106 ms_dlx_webticket_request(sipe_private, mdd);
1108 /* callback data passed down the line */
1109 return;
1111 } else {
1112 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
1114 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1115 mdd->token,
1116 _("No contacts found"));
1117 ms_dlx_free(mdd);
1118 return;
1122 /* OK, we found something - show the results to the user */
1123 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
1124 mdd->token);
1125 if (!results) {
1126 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
1127 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1128 mdd->token,
1129 _("Unable to display the search results"));
1130 ms_dlx_free(mdd);
1131 return;
1134 /* SearchAbEntryResult can contain duplicates */
1135 found = g_hash_table_new_full(g_str_hash, g_str_equal,
1136 g_free, NULL);
1138 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
1139 const sipe_xml *attrs;
1140 gchar *sip_uri = NULL;
1141 gchar *displayname = NULL;
1142 gchar *company = NULL;
1143 gchar *country = NULL;
1144 gchar *email = NULL;
1146 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
1147 attrs;
1148 attrs = sipe_xml_twin(attrs)) {
1149 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
1150 "Name"));
1151 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
1152 "Value"));
1154 if (!is_empty(value)) {
1155 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
1156 g_free(sip_uri);
1157 sip_uri = value;
1158 value = NULL;
1159 } else if (sipe_strcase_equal(name, "displayname")) {
1160 g_free(displayname);
1161 displayname = value;
1162 value = NULL;
1163 } else if (sipe_strcase_equal(name, "mail")) {
1164 g_free(email);
1165 email = value;
1166 value = NULL;
1167 } else if (sipe_strcase_equal(name, "company")) {
1168 g_free(company);
1169 company = value;
1170 value = NULL;
1171 } else if (sipe_strcase_equal(name, "country")) {
1172 g_free(country);
1173 country = value;
1174 value = NULL;
1178 g_free(value);
1179 g_free(name);
1182 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
1183 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
1184 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
1185 results,
1186 uri_parts[1],
1187 displayname,
1188 company,
1189 country,
1190 email);
1191 g_strfreev(uri_parts);
1193 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
1194 sip_uri = NULL;
1197 g_free(email);
1198 g_free(country);
1199 g_free(company);
1200 g_free(displayname);
1201 g_free(sip_uri);
1204 sipe_buddy_search_contacts_finalize(sipe_private, results,
1205 g_hash_table_size(found),
1206 FALSE);
1207 g_hash_table_destroy(found);
1208 ms_dlx_free(mdd);
1210 } else {
1211 mdd->failed_callback(sipe_private, mdd);
1215 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
1216 struct sipmsg *msg,
1217 struct transaction *trans)
1219 struct sipe_backend_search_token *token = trans->payload->data;
1220 struct sipe_backend_search_results *results;
1221 sipe_xml *searchResults;
1222 const sipe_xml *mrow;
1223 guint match_count = 0;
1224 gboolean more = FALSE;
1226 /* valid response? */
1227 if (msg->response != 200) {
1228 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1229 msg->response);
1230 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1231 token,
1232 _("Contact search failed"));
1233 return(FALSE);
1236 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
1238 /* valid XML? */
1239 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1240 if (!searchResults) {
1241 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1242 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1243 token,
1244 _("Contact search failed"));
1245 return(FALSE);
1248 /* any matches? */
1249 mrow = sipe_xml_child(searchResults, "Body/Array/row");
1250 if (!mrow) {
1251 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1252 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1253 token,
1254 _("No contacts found"));
1256 sipe_xml_free(searchResults);
1257 return(FALSE);
1260 /* OK, we found something - show the results to the user */
1261 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
1262 trans->payload->data);
1263 if (!results) {
1264 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1265 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1266 token,
1267 _("Unable to display the search results"));
1269 sipe_xml_free(searchResults);
1270 return FALSE;
1273 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
1274 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
1275 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
1276 results,
1277 uri_parts[1],
1278 sipe_xml_attribute(mrow, "displayName"),
1279 sipe_xml_attribute(mrow, "company"),
1280 sipe_xml_attribute(mrow, "country"),
1281 sipe_xml_attribute(mrow, "email"));
1282 g_strfreev(uri_parts);
1283 match_count++;
1286 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
1287 char *data = sipe_xml_data(mrow);
1288 more = (g_ascii_strcasecmp(data, "true") == 0);
1289 g_free(data);
1292 sipe_buddy_search_contacts_finalize(sipe_private, results, match_count, more);
1293 sipe_xml_free(searchResults);
1295 return(TRUE);
1298 static void search_soap_request(struct sipe_core_private *sipe_private,
1299 GDestroyNotify destroy,
1300 void *data,
1301 guint max,
1302 SoapTransCallback callback,
1303 GSList *search_rows)
1305 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
1306 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1308 payload->destroy = destroy;
1309 payload->data = data;
1311 sip_soap_directory_search(sipe_private,
1312 max,
1313 query,
1314 callback,
1315 payload);
1316 g_free(query);
1319 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
1320 struct ms_dlx_data *mdd)
1322 /* error using [MS-DLX] server, retry using Active Directory */
1323 if (mdd->search_rows)
1324 search_soap_request(sipe_private,
1325 NULL,
1326 mdd->token,
1327 100,
1328 process_search_contact_response,
1329 mdd->search_rows);
1330 ms_dlx_free(mdd);
1333 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
1334 struct sipe_backend_search_token *token,
1335 const gchar *given_name,
1336 const gchar *surname,
1337 const gchar *email,
1338 const gchar *sipid,
1339 const gchar *company,
1340 const gchar *country)
1342 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1344 /* Lync 2013 or newer: use UCS if contacts are migrated */
1345 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) &&
1346 sipe_ucs_is_migrated(sipe_private)) {
1348 sipe_ucs_search(sipe_private,
1349 token,
1350 given_name,
1351 surname,
1352 email,
1353 sipid,
1354 company,
1355 country);
1357 } else {
1358 GSList *query_rows = NULL;
1359 guint count = 0;
1360 const gchar *simple = NULL;
1362 #define ADD_QUERY_ROW(attr, val) \
1363 if (val) { \
1364 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1365 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1366 simple = val; \
1367 count++; \
1370 ADD_QUERY_ROW("givenName", given_name);
1371 ADD_QUERY_ROW("sn", surname);
1372 ADD_QUERY_ROW("mail", email);
1373 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1374 ADD_QUERY_ROW(NULL, sipid);
1375 ADD_QUERY_ROW("company", company);
1376 ADD_QUERY_ROW("c", country);
1378 if (query_rows) {
1379 if (sipe_private->dlx_uri != NULL) {
1380 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1382 mdd->search_rows = query_rows;
1383 /* user entered only one search string, remember that one */
1384 if (count == 1)
1385 mdd->other = g_strdup(simple);
1386 mdd->max_returns = 100;
1387 mdd->callback = search_ab_entry_response;
1388 mdd->failed_callback = search_ab_entry_failed;
1389 mdd->session = sipe_svc_session_start();
1390 mdd->token = token;
1392 ms_dlx_webticket_request(sipe_private, mdd);
1394 } else {
1395 /* no [MS-DLX] server, use Active Directory search instead */
1396 search_soap_request(sipe_private,
1397 NULL,
1398 token,
1399 100,
1400 process_search_contact_response,
1401 query_rows);
1402 free_search_rows(query_rows);
1404 } else
1405 sipe_backend_search_failed(sipe_public,
1406 token,
1407 _("Invalid contact search query"));
1411 static void get_info_finalize(struct sipe_core_private *sipe_private,
1412 struct sipe_backend_buddy_info *info,
1413 const gchar *uri,
1414 const gchar *server_alias,
1415 const gchar *email)
1417 sipe_backend_buddy bbuddy;
1418 struct sipe_buddy *sbuddy;
1419 gchar *alias;
1420 gchar *value;
1422 if (!info) {
1423 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1424 } else {
1425 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
1427 if (!info)
1428 return;
1430 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
1432 if (is_empty(server_alias)) {
1433 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
1434 bbuddy);
1435 if (value) {
1436 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1437 info,
1438 SIPE_BUDDY_INFO_DISPLAY_NAME,
1439 value);
1441 } else {
1442 value = g_strdup(server_alias);
1445 /* present alias if it differs from server alias */
1446 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
1447 if (alias && !sipe_strequal(alias, value))
1449 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1450 info,
1451 SIPE_BUDDY_INFO_ALIAS,
1452 alias);
1454 g_free(alias);
1455 g_free(value);
1457 if (is_empty(email)) {
1458 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1459 bbuddy,
1460 SIPE_BUDDY_INFO_EMAIL);
1461 if (value) {
1462 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1463 info,
1464 SIPE_BUDDY_INFO_EMAIL,
1465 value);
1466 g_free(value);
1470 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1471 bbuddy,
1472 SIPE_BUDDY_INFO_SITE);
1473 if (value) {
1474 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1475 info,
1476 SIPE_BUDDY_INFO_SITE,
1477 value);
1478 g_free(value);
1481 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
1482 if (sbuddy && sbuddy->device_name) {
1483 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1484 info,
1485 SIPE_BUDDY_INFO_DEVICE,
1486 sbuddy->device_name);
1489 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
1493 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
1494 const gchar *uri,
1495 SIPE_UNUSED_PARAMETER const gchar *raw,
1496 sipe_xml *soap_body,
1497 gpointer callback_data)
1499 struct ms_dlx_data *mdd = callback_data;
1500 struct sipe_backend_buddy_info *info = NULL;
1501 gchar *server_alias = NULL;
1502 gchar *email = NULL;
1504 if (soap_body) {
1505 const sipe_xml *node;
1507 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1508 uri);
1510 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1512 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1513 node;
1514 node = sipe_xml_twin(node)) {
1515 gchar *name = sipe_xml_data(sipe_xml_child(node,
1516 "Name"));
1517 gchar *value = sipe_xml_data(sipe_xml_child(node,
1518 "Value"));
1519 const sipe_xml *values = sipe_xml_child(node,
1520 "Values");
1522 /* Single value entries */
1523 if (!is_empty(value)) {
1525 if (sipe_strcase_equal(name, "displayname")) {
1526 g_free(server_alias);
1527 server_alias = value;
1528 value = NULL;
1529 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1530 info,
1531 SIPE_BUDDY_INFO_DISPLAY_NAME,
1532 server_alias);
1533 } else if (sipe_strcase_equal(name, "mail")) {
1534 g_free(email);
1535 email = value;
1536 value = NULL;
1537 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1538 info,
1539 SIPE_BUDDY_INFO_EMAIL,
1540 email);
1541 } else if (sipe_strcase_equal(name, "title")) {
1542 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1543 info,
1544 SIPE_BUDDY_INFO_JOB_TITLE,
1545 value);
1546 } else if (sipe_strcase_equal(name, "company")) {
1547 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1548 info,
1549 SIPE_BUDDY_INFO_COMPANY,
1550 value);
1551 } else if (sipe_strcase_equal(name, "country")) {
1552 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1553 info,
1554 SIPE_BUDDY_INFO_COUNTRY,
1555 value);
1558 } else if (values) {
1559 gchar *first = sipe_xml_data(sipe_xml_child(values,
1560 "string"));
1562 if (sipe_strcase_equal(name, "telephonenumber")) {
1563 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1564 info,
1565 SIPE_BUDDY_INFO_WORK_PHONE,
1566 first);
1569 g_free(first);
1572 g_free(value);
1573 g_free(name);
1577 /* this will show the minmum information */
1578 get_info_finalize(sipe_private,
1579 info,
1580 mdd->other,
1581 server_alias,
1582 email);
1584 g_free(email);
1585 g_free(server_alias);
1586 ms_dlx_free(mdd);
1589 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1590 struct sipmsg *msg,
1591 struct transaction *trans)
1593 const gchar *uri = trans->payload->data;
1594 struct sipe_backend_buddy_info *info = NULL;
1595 gchar *server_alias = NULL;
1596 gchar *email = NULL;
1598 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1599 uri, sipe_private->username);
1601 if (msg->response != 200) {
1602 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1603 } else {
1604 sipe_xml *searchResults;
1605 const sipe_xml *mrow;
1607 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1608 msg->body ? msg->body : "");
1610 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1611 if (!searchResults) {
1613 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1615 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1616 const gchar *value;
1617 gchar *phone_number;
1619 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1621 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1622 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1623 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1626 * For 2007 system we will take this from ContactCard -
1627 * it has cleaner tel: URIs at least
1629 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1630 char *tel_uri = sip_to_tel_uri(phone_number);
1631 /* trims its parameters, so call first */
1632 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1633 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1634 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1635 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1636 g_free(tel_uri);
1638 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1639 uri);
1642 if (!is_empty(server_alias)) {
1643 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1644 info,
1645 SIPE_BUDDY_INFO_DISPLAY_NAME,
1646 server_alias);
1648 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1649 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1650 info,
1651 SIPE_BUDDY_INFO_JOB_TITLE,
1652 value);
1654 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1655 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1656 info,
1657 SIPE_BUDDY_INFO_OFFICE,
1658 value);
1660 if (!is_empty(phone_number)) {
1661 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1662 info,
1663 SIPE_BUDDY_INFO_WORK_PHONE,
1664 phone_number);
1666 g_free(phone_number);
1667 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1668 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1669 info,
1670 SIPE_BUDDY_INFO_COMPANY,
1671 value);
1673 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1674 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1675 info,
1676 SIPE_BUDDY_INFO_CITY,
1677 value);
1679 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1680 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1681 info,
1682 SIPE_BUDDY_INFO_STATE,
1683 value);
1685 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1686 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1687 info,
1688 SIPE_BUDDY_INFO_COUNTRY,
1689 value);
1691 if (!is_empty(email)) {
1692 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1693 info,
1694 SIPE_BUDDY_INFO_EMAIL,
1695 email);
1698 sipe_xml_free(searchResults);
1701 /* this will show the minmum information */
1702 get_info_finalize(sipe_private,
1703 info,
1704 uri,
1705 server_alias,
1706 email);
1708 g_free(server_alias);
1709 g_free(email);
1711 return TRUE;
1714 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1715 struct ms_dlx_data *mdd)
1717 /* error using [MS-DLX] server, retry using Active Directory */
1718 search_soap_request(sipe_private,
1719 g_free,
1720 mdd->other,
1722 process_get_info_response,
1723 mdd->search_rows);
1724 mdd->other = NULL;
1725 ms_dlx_free(mdd);
1728 static GSList *search_rows_for_uri(const gchar *uri)
1730 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1731 GSList *l = g_slist_append(NULL, NULL);
1732 return(g_slist_append(l, g_strdup(uri)));
1735 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1736 const gchar *who)
1738 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1739 GSList *search_rows = search_rows_for_uri(who);
1741 if (sipe_private->dlx_uri) {
1742 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1744 mdd->search_rows = search_rows;
1745 mdd->other = g_strdup(who);
1746 mdd->max_returns = 1;
1747 mdd->callback = get_info_ab_entry_response;
1748 mdd->failed_callback = get_info_ab_entry_failed;
1749 mdd->session = sipe_svc_session_start();
1751 ms_dlx_webticket_request(sipe_private, mdd);
1753 } else {
1754 /* no [MS-DLX] server, use Active Directory search instead */
1755 search_soap_request(sipe_private,
1756 g_free,
1757 g_strdup(who),
1759 process_get_info_response,
1760 search_rows);
1761 free_search_rows(search_rows);
1765 static void photo_response_data_free(struct photo_response_data *data)
1767 g_free(data->who);
1768 g_free(data->photo_hash);
1769 if (data->request) {
1770 sipe_http_request_cancel(data->request);
1772 g_free(data);
1775 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1776 guint status,
1777 GSList *headers,
1778 const char *body,
1779 gpointer data)
1781 struct photo_response_data *rdata = (struct photo_response_data *) data;
1783 rdata->request = NULL;
1785 if (status == SIPE_HTTP_STATUS_OK) {
1786 const gchar *len_str = sipe_utils_nameval_find(headers,
1787 "Content-Length");
1788 if (len_str) {
1789 gsize photo_size = atoi(len_str);
1790 gpointer photo = g_new(char, photo_size);
1792 if (photo) {
1793 memcpy(photo, body, photo_size);
1795 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1796 rdata->who,
1797 photo,
1798 photo_size,
1799 rdata->photo_hash);
1804 sipe_private->buddies->pending_photo_requests =
1805 g_slist_remove(sipe_private->buddies->pending_photo_requests, rdata);
1807 photo_response_data_free(rdata);
1810 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1812 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1813 gchar *wsse_security_base64;
1814 gchar *x_ms_webticket_header;
1816 if (!assertion) {
1817 return NULL;
1820 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1821 strlen(assertion));
1822 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1823 wsse_security_base64);
1825 g_free(assertion);
1826 g_free(wsse_security_base64);
1828 return x_ms_webticket_header;
1831 void sipe_buddy_update_photo(struct sipe_core_private *sipe_private,
1832 const gchar *uri,
1833 const gchar *photo_hash,
1834 const gchar *photo_url,
1835 const gchar *headers)
1837 const gchar *photo_hash_old =
1838 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, uri);
1840 if (!sipe_strequal(photo_hash, photo_hash_old)) {
1841 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1843 SIPE_DEBUG_INFO("sipe_buddy_update_photo: who '%s' url '%s' hash '%s'",
1844 uri, photo_url, photo_hash);
1846 data->who = g_strdup(uri);
1847 data->photo_hash = g_strdup(photo_hash);
1849 data->request = sipe_http_request_get(sipe_private,
1850 photo_url,
1851 headers,
1852 process_buddy_photo_response,
1853 data);
1855 if (data->request) {
1856 sipe_private->buddies->pending_photo_requests =
1857 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1858 sipe_http_request_ready(data->request);
1859 } else {
1860 photo_response_data_free(data);
1865 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1866 const gchar *uri,
1867 SIPE_UNUSED_PARAMETER const gchar *raw,
1868 sipe_xml *soap_body,
1869 gpointer callback_data)
1871 struct ms_dlx_data *mdd = callback_data;
1872 gchar *photo_rel_path = NULL;
1873 gchar *photo_hash = NULL;
1875 if (soap_body) {
1876 const sipe_xml *node;
1878 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1879 uri);
1881 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1882 node;
1883 node = sipe_xml_twin(node)) {
1884 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1885 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1887 if (!is_empty(value)) {
1888 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1889 g_free(photo_rel_path);
1890 photo_rel_path = value;
1891 value = NULL;
1892 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1893 g_free(photo_hash);
1894 photo_hash = value;
1895 value = NULL;
1899 g_free(value);
1900 g_free(name);
1904 if (sipe_private->addressbook_uri && photo_rel_path && photo_hash) {
1905 gchar *photo_url = g_strdup_printf("%s/%s",
1906 sipe_private->addressbook_uri, photo_rel_path);
1907 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1909 sipe_buddy_update_photo(sipe_private,
1910 mdd->other,
1911 photo_hash,
1912 photo_url,
1913 x_ms_webticket_header);
1915 g_free(x_ms_webticket_header);
1916 g_free(photo_url);
1919 g_free(photo_rel_path);
1920 g_free(photo_hash);
1921 ms_dlx_free(mdd);
1924 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1925 struct ms_dlx_data *mdd)
1927 ms_dlx_free(mdd);
1930 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1931 const gchar *uri)
1933 if (sipe_backend_uses_photo()) {
1935 /* Lync 2013 or newer: use UCS if contacts are migrated */
1936 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) &&
1937 sipe_ucs_is_migrated(sipe_private)) {
1939 sipe_ucs_get_photo(sipe_private, uri);
1941 /* Lync 2010: use [MS-DLX] */
1942 } else if (sipe_private->dlx_uri &&
1943 sipe_private->addressbook_uri) {
1944 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1946 mdd->search_rows = search_rows_for_uri(uri);
1947 mdd->other = g_strdup(uri);
1948 mdd->max_returns = 1;
1949 mdd->callback = get_photo_ab_entry_response;
1950 mdd->failed_callback = get_photo_ab_entry_failed;
1951 mdd->session = sipe_svc_session_start();
1953 ms_dlx_webticket_request(sipe_private, mdd);
1958 static void buddy_refresh_photos_cb(gpointer uri,
1959 SIPE_UNUSED_PARAMETER gpointer value,
1960 gpointer sipe_private)
1962 buddy_fetch_photo(sipe_private, uri);
1965 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1967 g_hash_table_foreach(sipe_private->buddies->uri,
1968 buddy_refresh_photos_cb,
1969 sipe_private);
1972 /* Buddy menu callbacks*/
1974 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1975 const gchar *who)
1977 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1979 /* 2007+ conference */
1980 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1981 sipe_conf_add(sipe_private, who);
1983 /* 2005- multiparty chat */
1984 } else {
1985 gchar *self = sip_uri_self(sipe_private);
1986 struct sip_session *session;
1988 session = sipe_session_add_chat(sipe_private,
1989 NULL,
1990 TRUE,
1991 self);
1992 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1993 session->chat_session,
1994 session->chat_session->title,
1995 self);
1996 g_free(self);
1998 sipe_im_invite(sipe_private, session, who,
1999 NULL, NULL, NULL, FALSE);
2003 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
2004 const gchar *who)
2006 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
2007 who,
2008 NULL);
2009 gchar *email = sipe_backend_buddy_get_string(sipe_public,
2010 buddy,
2011 SIPE_BUDDY_INFO_EMAIL);
2013 if (email) {
2014 gchar *command_line = g_strdup_printf(
2015 #ifdef _WIN32
2016 "cmd /c start"
2017 #else
2018 "xdg-email"
2019 #endif
2020 " mailto:%s", email);
2021 g_free(email);
2023 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
2024 command_line);
2025 g_spawn_command_line_async(command_line, NULL);
2026 g_free(command_line);
2028 } else {
2029 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
2030 who);
2034 /* Buddy menu */
2036 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
2037 struct sipe_backend_buddy_menu *menu,
2038 sipe_backend_buddy buddy,
2039 sipe_buddy_info_fields id_phone,
2040 sipe_buddy_info_fields id_display,
2041 const gchar *type)
2043 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
2044 buddy,
2045 id_phone);
2046 if (phone) {
2047 gchar *display = sipe_backend_buddy_get_string(sipe_public,
2048 buddy,
2049 id_display);
2050 gchar *tmp = NULL;
2051 gchar *label = g_strdup_printf("%s %s",
2052 type,
2053 display ? display :
2054 (tmp = sip_tel_uri_denormalize(phone)));
2055 menu = sipe_backend_buddy_menu_add(sipe_public,
2056 menu,
2057 label,
2058 SIPE_BUDDY_MENU_MAKE_CALL,
2059 phone);
2060 g_free(tmp);
2061 g_free(label);
2062 g_free(display);
2063 g_free(phone);
2066 return(menu);
2069 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
2070 const gchar *buddy_name,
2071 struct sipe_backend_buddy_menu *menu)
2073 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
2074 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
2075 buddy_name,
2076 NULL);
2077 gchar *self = sip_uri_self(sipe_private);
2079 SIPE_SESSION_FOREACH {
2080 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
2082 struct sipe_chat_session *chat_session = session->chat_session;
2083 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
2085 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
2087 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
2089 if (is_conf &&
2090 /* Not conf OP */
2091 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
2092 /* We are a conf OP */
2093 conf_op) {
2094 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
2095 chat_session->title);
2096 menu = sipe_backend_buddy_menu_add(sipe_public,
2097 menu,
2098 label,
2099 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
2100 chat_session);
2101 g_free(label);
2104 if (is_conf &&
2105 /* We are a conf OP */
2106 conf_op) {
2107 gchar *label = g_strdup_printf(_("Remove from '%s'"),
2108 chat_session->title);
2109 menu = sipe_backend_buddy_menu_add(sipe_public,
2110 menu,
2111 label,
2112 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
2113 chat_session);
2114 g_free(label);
2117 else
2119 if (!is_conf ||
2120 (is_conf && !session->locked)) {
2121 gchar *label = g_strdup_printf(_("Invite to '%s'"),
2122 chat_session->title);
2123 menu = sipe_backend_buddy_menu_add(sipe_public,
2124 menu,
2125 label,
2126 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
2127 chat_session);
2128 g_free(label);
2132 } SIPE_SESSION_FOREACH_END;
2133 g_free(self);
2135 menu = sipe_backend_buddy_menu_add(sipe_public,
2136 menu,
2137 _("New chat"),
2138 SIPE_BUDDY_MENU_NEW_CHAT,
2139 NULL);
2141 /* add buddy's phone numbers if we have call control */
2142 if (sip_csta_is_idle(sipe_private)) {
2144 /* work phone */
2145 menu = buddy_menu_phone(sipe_public,
2146 menu,
2147 buddy,
2148 SIPE_BUDDY_INFO_WORK_PHONE,
2149 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
2150 _("Work"));
2151 /* mobile phone */
2152 menu = buddy_menu_phone(sipe_public,
2153 menu,
2154 buddy,
2155 SIPE_BUDDY_INFO_MOBILE_PHONE,
2156 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
2157 _("Mobile"));
2159 /* home phone */
2160 menu = buddy_menu_phone(sipe_public,
2161 menu,
2162 buddy,
2163 SIPE_BUDDY_INFO_HOME_PHONE,
2164 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
2165 _("Home"));
2167 /* other phone */
2168 menu = buddy_menu_phone(sipe_public,
2169 menu,
2170 buddy,
2171 SIPE_BUDDY_INFO_OTHER_PHONE,
2172 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
2173 _("Other"));
2175 /* custom1 phone */
2176 menu = buddy_menu_phone(sipe_public,
2177 menu,
2178 buddy,
2179 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
2180 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
2181 _("Custom1"));
2185 gchar *email = sipe_backend_buddy_get_string(sipe_public,
2186 buddy,
2187 SIPE_BUDDY_INFO_EMAIL);
2188 if (email) {
2189 menu = sipe_backend_buddy_menu_add(sipe_public,
2190 menu,
2191 _("Send email..."),
2192 SIPE_BUDDY_MENU_SEND_EMAIL,
2193 NULL);
2194 g_free(email);
2198 /* access level control */
2199 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
2200 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
2201 menu,
2202 _("Access level"),
2203 sipe_ocs2007_access_control_menu(sipe_private,
2204 buddy_name));
2206 return(menu);
2209 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
2211 return(g_hash_table_size(sipe_private->buddies->uri));
2214 static guint sipe_ht_hash_nick(const char *nick)
2216 char *lc = g_utf8_strdown(nick, -1);
2217 guint bucket = g_str_hash(lc);
2218 g_free(lc);
2220 return bucket;
2223 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
2225 char *nick1_norm = NULL;
2226 char *nick2_norm = NULL;
2227 gboolean equal;
2229 if (nick1 == NULL && nick2 == NULL) return TRUE;
2230 if (nick1 == NULL || nick2 == NULL ||
2231 !g_utf8_validate(nick1, -1, NULL) ||
2232 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
2234 nick1_norm = g_utf8_casefold(nick1, -1);
2235 nick2_norm = g_utf8_casefold(nick2, -1);
2236 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
2237 g_free(nick2_norm);
2238 g_free(nick1_norm);
2240 return equal;
2243 void sipe_buddy_init(struct sipe_core_private *sipe_private)
2245 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
2246 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
2247 (GEqualFunc) sipe_ht_equals_nick);
2248 buddies->exchange_key = g_hash_table_new(g_str_hash,
2249 g_str_equal);
2250 sipe_private->buddies = buddies;
2254 Local Variables:
2255 mode: c
2256 c-file-style: "bsd"
2257 indent-tabs-mode: t
2258 tab-width: 8
2259 End: