Release 1.25.0 -- Buddy Idle Time, RTF
[siplcs.git] / src / core / sipe-buddy.c
blob8364bcfc6fb20717563a5d72d0ed50e2e2ab0a52
1 /**
2 * @file sipe-buddy.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2019 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
22 * GetUserPhoto operation
23 * <http://msdn.microsoft.com/en-us/library/office/jj900502.aspx>
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
34 #include <glib.h>
36 #include "sipe-common.h"
37 #include "sipmsg.h"
38 #include "sip-csta.h"
39 #include "sip-soap.h"
40 #include "sip-transport.h"
41 #include "sipe-backend.h"
42 #include "sipe-buddy.h"
43 #include "sipe-cal.h"
44 #include "sipe-chat.h"
45 #include "sipe-conf.h"
46 #include "sipe-core.h"
47 #include "sipe-core-private.h"
48 #include "sipe-appshare.h"
49 #include "sipe-digest.h"
50 #include "sipe-group.h"
51 #include "sipe-http.h"
52 #include "sipe-im.h"
53 #include "sipe-media.h"
54 #include "sipe-nls.h"
55 #include "sipe-ocs2005.h"
56 #include "sipe-ocs2007.h"
57 #include "sipe-schedule.h"
58 #include "sipe-session.h"
59 #include "sipe-status.h"
60 #include "sipe-subscriptions.h"
61 #include "sipe-svc.h"
62 #include "sipe-ucs.h"
63 #include "sipe-utils.h"
64 #include "sipe-webticket.h"
65 #include "sipe-xml.h"
67 struct sipe_buddies {
68 GHashTable *uri;
69 GHashTable *exchange_key;
71 /* Pending photo download HTTP requests */
72 GSList *pending_photo_requests;
75 struct buddy_group_data {
76 const struct sipe_group *group;
77 gboolean is_obsolete;
80 struct photo_response_data {
81 gchar *who;
82 gchar *photo_hash;
83 struct sipe_http_request *request;
86 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
87 const gchar *uri);
88 static void photo_response_data_free(struct photo_response_data *data);
90 void sipe_buddy_add_keys(struct sipe_core_private *sipe_private,
91 struct sipe_buddy *buddy,
92 const gchar *exchange_key,
93 const gchar *change_key)
95 if (exchange_key) {
96 buddy->exchange_key = g_strdup(exchange_key);
97 g_hash_table_insert(sipe_private->buddies->exchange_key,
98 buddy->exchange_key,
99 buddy);
101 if (change_key)
102 buddy->change_key = g_strdup(change_key);
105 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
106 const gchar *uri,
107 const gchar *exchange_key,
108 const gchar *change_key)
110 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
111 gchar *normalized_uri = g_ascii_strdown(uri, -1);
112 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
113 normalized_uri);
115 if (!buddy) {
116 buddy = g_new0(struct sipe_buddy, 1);
117 buddy->name = normalized_uri;
118 g_hash_table_insert(sipe_private->buddies->uri,
119 buddy->name,
120 buddy);
122 sipe_buddy_add_keys(sipe_private,
123 buddy,
124 exchange_key,
125 change_key);
127 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri);
129 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
130 buddy->just_added = TRUE;
131 sipe_subscribe_presence_single_cb(sipe_private,
132 buddy->name);
135 buddy_fetch_photo(sipe_private, normalized_uri);
137 normalized_uri = NULL; /* buddy takes ownership */
138 } else {
139 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri);
140 buddy->is_obsolete = FALSE;
142 g_free(normalized_uri);
144 return(buddy);
147 static gboolean is_buddy_in_group(struct sipe_buddy *buddy,
148 const gchar *name)
150 if (buddy) {
151 GSList *entry = buddy->groups;
153 while (entry) {
154 struct buddy_group_data *bgd = entry->data;
155 if (sipe_strequal(bgd->group->name, name)) {
156 bgd->is_obsolete = FALSE;
157 return(TRUE);
159 entry = entry->next;
163 return(FALSE);
166 void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private,
167 struct sipe_buddy *buddy,
168 struct sipe_group *group,
169 const gchar *alias)
171 const gchar *uri = buddy->name;
172 const gchar *group_name = group->name;
173 sipe_backend_buddy bb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
174 uri,
175 group_name);
177 if (!bb) {
178 bb = sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
179 uri,
180 alias,
181 group_name);
182 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
183 uri, alias ? alias : "<NONE>");
187 if (!is_empty(alias)) {
188 gchar *old_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
189 bb);
191 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri),
192 old_alias)) {
193 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
195 alias);
196 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
197 uri, old_alias, alias);
199 g_free(old_alias);
202 if (!is_buddy_in_group(buddy, group_name)) {
203 sipe_buddy_insert_group(buddy, group);
204 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
205 uri, group_name);
209 static gint buddy_group_compare(gconstpointer a, gconstpointer b)
211 return(((const struct buddy_group_data *)a)->group->id -
212 ((const struct buddy_group_data *)b)->group->id);
215 void sipe_buddy_insert_group(struct sipe_buddy *buddy,
216 struct sipe_group *group)
218 struct buddy_group_data *bgd = g_new0(struct buddy_group_data, 1);
220 bgd->group = group;
222 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
223 bgd,
224 buddy_group_compare,
225 NULL);
228 static void buddy_group_free(gpointer data)
230 g_free(data);
233 static void buddy_group_remove(struct sipe_buddy *buddy,
234 struct buddy_group_data *bgd)
236 buddy->groups = g_slist_remove(buddy->groups, bgd);
237 buddy_group_free(bgd);
240 static void sipe_buddy_remove_group(struct sipe_buddy *buddy,
241 const struct sipe_group *group)
243 GSList *entry = buddy->groups;
244 struct buddy_group_data *bgd = NULL;
246 while (entry) {
247 bgd = entry->data;
248 if (bgd->group == group)
249 break;
250 entry = entry->next;
253 buddy_group_remove(buddy, bgd);
256 void sipe_buddy_update_groups(struct sipe_core_private *sipe_private,
257 struct sipe_buddy *buddy,
258 GSList *new_groups)
260 const gchar *uri = buddy->name;
261 GSList *entry = buddy->groups;
263 while (entry) {
264 struct buddy_group_data *bgd = entry->data;
265 const struct sipe_group *group = bgd->group;
267 /* next buddy group */
268 entry = entry->next;
270 /* old group NOT found in new list? */
271 if (g_slist_find(new_groups, group) == NULL) {
272 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
273 uri,
274 group->name);
275 SIPE_DEBUG_INFO("sipe_buddy_update_groups: removing buddy %s from group '%s'",
276 uri, group->name);
277 /* this should never be NULL */
278 if (oldb)
279 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
280 oldb);
281 buddy_group_remove(buddy, bgd);
286 gchar *sipe_buddy_groups_string(struct sipe_buddy *buddy)
288 guint i = 0;
289 gchar *string;
290 /* creating array from GList, converting guint to gchar * */
291 gchar **ids_arr = g_new(gchar *, g_slist_length(buddy->groups) + 1);
292 GSList *entry = buddy->groups;
294 if (!ids_arr)
295 return(NULL);
297 while (entry) {
298 const struct sipe_group *group = ((struct buddy_group_data *) entry->data)->group;
299 ids_arr[i] = g_strdup_printf("%u", group->id);
300 entry = entry->next;
301 i++;
303 ids_arr[i] = NULL;
305 string = g_strjoinv(" ", ids_arr);
306 g_strfreev(ids_arr);
308 return(string);
311 void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private)
313 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
314 NULL,
315 NULL);
316 GSList *entry = buddies;
318 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
319 g_slist_length(buddies));
320 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
321 sipe_buddy_count(sipe_private));
322 while (entry) {
323 sipe_backend_buddy bb = entry->data;
324 gchar *bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC,
325 bb);
326 gchar *gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC,
327 bb);
328 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
329 bname);
331 if (!is_buddy_in_group(buddy, gname)) {
332 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",
333 bname, gname);
334 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, bb);
337 g_free(gname);
338 g_free(bname);
340 entry = entry->next;
343 g_slist_free(buddies);
346 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
347 const gchar *uri)
349 if (!uri) return(NULL);
350 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
353 struct sipe_buddy *sipe_buddy_find_by_exchange_key(struct sipe_core_private *sipe_private,
354 const gchar *exchange_key)
356 return(g_hash_table_lookup(sipe_private->buddies->exchange_key,
357 exchange_key));
360 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
361 GHFunc callback,
362 gpointer callback_data)
364 g_hash_table_foreach(sipe_private->buddies->uri,
365 callback,
366 callback_data);
369 static void buddy_free(struct sipe_buddy *buddy)
371 #ifndef _WIN32
373 * We are calling g_hash_table_foreach_steal(). That means that no
374 * key/value deallocation functions are called. Therefore the glib
375 * hash code does not touch the key (buddy->name) or value (buddy)
376 * of the to-be-deleted hash node at all. It follows that we
378 * - MUST free the memory for the key ourselves and
379 * - ARE allowed to do it in this function
381 * Conclusion: glib must be broken on the Windows platform if sipe
382 * crashes with SIGTRAP when closing. You'll have to live
383 * with the memory leak until this is fixed.
385 g_free(buddy->name);
386 #endif
387 g_free(buddy->exchange_key);
388 g_free(buddy->change_key);
389 g_free(buddy->activity);
390 g_free(buddy->meeting_subject);
391 g_free(buddy->meeting_location);
392 g_free(buddy->note);
394 g_free(buddy->cal_start_time);
395 g_free(buddy->cal_free_busy_base64);
396 g_free(buddy->cal_free_busy);
397 g_free(buddy->last_non_cal_activity);
399 sipe_cal_free_working_hours(buddy->cal_working_hours);
401 g_free(buddy->device_name);
402 sipe_utils_slist_free_full(buddy->groups, buddy_group_free);
403 g_free(buddy);
406 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
407 gpointer buddy,
408 SIPE_UNUSED_PARAMETER gpointer user_data)
410 buddy_free(buddy);
411 /* We must return TRUE as the key/value have already been deleted */
412 return(TRUE);
415 void sipe_buddy_free(struct sipe_core_private *sipe_private)
417 struct sipe_buddies *buddies = sipe_private->buddies;
419 g_hash_table_foreach_steal(buddies->uri,
420 buddy_free_cb,
421 NULL);
423 /* core is being deallocated, remove all its pending photo requests */
424 while (buddies->pending_photo_requests) {
425 struct photo_response_data *data =
426 buddies->pending_photo_requests->data;
427 buddies->pending_photo_requests =
428 g_slist_remove(buddies->pending_photo_requests, data);
429 photo_response_data_free(data);
432 g_hash_table_destroy(buddies->uri);
433 g_hash_table_destroy(buddies->exchange_key);
434 g_free(buddies);
435 sipe_private->buddies = NULL;
438 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
439 gpointer value,
440 SIPE_UNUSED_PARAMETER gpointer user_data)
442 struct sipe_buddy *buddy = value;
443 GSList *entry = buddy->groups;
445 buddy->is_obsolete = TRUE;
446 while (entry) {
447 ((struct buddy_group_data *) entry->data)->is_obsolete = TRUE;
448 entry = entry->next;
452 void sipe_buddy_update_start(struct sipe_core_private *sipe_private)
454 g_hash_table_foreach(sipe_private->buddies->uri,
455 buddy_set_obsolete_flag,
456 NULL);
459 static gboolean buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
460 gpointer value,
461 gpointer user_data)
463 struct sipe_core_private *sipe_private = user_data;
464 struct sipe_buddy *buddy = value;
465 const gchar *uri = buddy->name;
467 if (buddy->is_obsolete) {
468 /* all backend buddies in different groups */
469 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
470 uri,
471 NULL);
472 GSList *entry = buddies;
474 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
475 g_slist_length(buddies),
476 uri);
478 while (entry) {
479 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
480 entry->data);
481 entry = entry->next;
483 g_slist_free(buddies);
485 buddy_free(buddy);
486 /* return TRUE as the key/value have already been deleted */
487 return(TRUE);
489 } else {
490 GSList *entry = buddy->groups;
492 while (entry) {
493 struct buddy_group_data *bgd = entry->data;
495 /* next buddy group */
496 entry = entry->next;
498 if (bgd->is_obsolete) {
499 const struct sipe_group *group = bgd->group;
500 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
501 uri,
502 group->name);
503 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: removing buddy '%s' from group '%s'",
504 uri, group->name);
505 /* this should never be NULL */
506 if (oldb)
507 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
508 oldb);
509 buddy_group_remove(buddy, bgd);
512 return(FALSE);
516 void sipe_buddy_update_finish(struct sipe_core_private *sipe_private)
518 g_hash_table_foreach_remove(sipe_private->buddies->uri,
519 buddy_check_obsolete_flag,
520 sipe_private);
523 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
524 const gchar *uri,
525 guint activity,
526 const gchar *status_text)
528 struct sipe_buddy *sbuddy;
529 GString *status;
531 if (!sipe_public) return NULL; /* happens on pidgin exit */
533 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
534 if (!sbuddy) return NULL;
536 status = g_string_new(sbuddy->activity ? sbuddy->activity :
537 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
538 status_text : NULL);
540 if (sbuddy->is_mobile) {
541 if (status->len)
542 g_string_append(status, " - ");
543 g_string_append(status, _("Mobile"));
546 if (sbuddy->note) {
547 if (status->len)
548 g_string_append(status, " - ");
549 g_string_append(status, sbuddy->note);
552 /* return NULL instead of empty status text */
553 return(g_string_free(status, status->len ? FALSE : TRUE));
556 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
557 const gchar *with)
559 sipe_backend_buddy pbuddy;
560 gchar *alias = NULL;
561 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
562 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
564 return alias;
567 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
568 const gchar *who,
569 const gchar *old_group_name,
570 const gchar *new_group_name)
572 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
573 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
574 who);
575 struct sipe_group *old_group = NULL;
576 struct sipe_group *new_group;
577 struct sipe_ucs_transaction *ucs_trans = NULL;
579 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' old group '%s' new group '%s'",
580 who ? who : "",
581 old_group_name ? old_group_name : "<UNDEFINED>",
582 new_group_name ? new_group_name : "<UNDEFINED>");
584 if (!buddy)
585 /* buddy not in roaming list */
586 return;
588 old_group = sipe_group_find_by_name(sipe_private, old_group_name);
589 if (old_group) {
590 sipe_buddy_remove_group(buddy, old_group);
591 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' removed from old group '%s'",
592 who, old_group_name);
595 new_group = sipe_group_find_by_name(sipe_private, new_group_name);
596 if (new_group) {
597 sipe_buddy_insert_group(buddy, new_group);
598 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' added to new group '%s'",
599 who, new_group_name);
602 if (sipe_ucs_is_migrated(sipe_private)) {
604 /* UCS handling */
605 ucs_trans = sipe_ucs_transaction(sipe_private);
607 if (new_group) {
609 * 1. new buddy added to existing group
610 * 2. existing buddy moved from old to existing group
612 sipe_ucs_group_add_buddy(sipe_private,
613 ucs_trans,
614 new_group,
615 buddy,
616 buddy->name);
617 if (old_group)
618 sipe_ucs_group_remove_buddy(sipe_private,
619 ucs_trans,
620 old_group,
621 buddy);
623 } else if (old_group) {
625 * 3. existing buddy removed from one of its groups
626 * 4. existing buddy removed from last group
628 sipe_ucs_group_remove_buddy(sipe_private,
629 ucs_trans,
630 old_group,
631 buddy);
632 if (g_slist_length(buddy->groups) < 1)
633 sipe_buddy_remove(sipe_private,
634 buddy);
635 /* buddy no longer valid */
638 /* non-UCS handling */
639 } else if (new_group)
640 sipe_group_update_buddy(sipe_private, buddy);
642 /* 5. buddy added to new group */
643 if (!new_group)
644 sipe_group_create(sipe_private,
645 ucs_trans,
646 new_group_name,
647 who);
650 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
651 const gchar *uri,
652 const gchar *group_name)
654 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
656 if (!sipe_buddy_find_by_uri(sipe_private, uri))
657 sipe_buddy_add(sipe_private,
658 uri,
659 NULL,
660 NULL);
661 else
662 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
663 uri);
665 sipe_core_buddy_group(sipe_public,
666 uri,
667 NULL,
668 group_name);
671 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
672 struct sipe_buddy *buddy)
674 struct sipe_buddies *buddies = sipe_private->buddies;
675 const gchar *uri = buddy->name;
676 GSList *entry = buddy->groups;
677 gchar *action_name = sipe_utils_presence_key(uri);
679 sipe_schedule_cancel(sipe_private, action_name);
680 g_free(action_name);
682 /* If the buddy still has groups, we need to delete backend buddies */
683 while (entry) {
684 const struct sipe_group *group = ((struct buddy_group_data *) entry->data)->group;
685 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
686 uri,
687 group->name);
688 /* this should never be NULL */
689 if (oldb)
690 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, oldb);
692 entry = entry->next;
695 g_hash_table_remove(buddies->uri, uri);
696 if (buddy->exchange_key)
697 g_hash_table_remove(buddies->exchange_key,
698 buddy->exchange_key);
700 buddy_free(buddy);
704 * Unassociates buddy from group first.
705 * Then see if no groups left, removes buddy completely.
706 * Otherwise updates buddy groups on server.
708 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
709 const gchar *uri,
710 const gchar *group_name)
712 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
713 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
714 uri);
715 struct sipe_group *group = NULL;
717 if (!buddy) return;
719 if (group_name) {
720 group = sipe_group_find_by_name(sipe_private, group_name);
721 if (group) {
722 sipe_buddy_remove_group(buddy, group);
723 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy '%s' removed from group '%s'",
724 uri, group->name);
728 if (g_slist_length(buddy->groups) < 1) {
730 if (sipe_ucs_is_migrated(sipe_private)) {
731 sipe_ucs_group_remove_buddy(sipe_private,
732 NULL,
733 group,
734 buddy);
735 } else {
736 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
737 buddy->name);
738 sip_soap_request(sipe_private,
739 "deleteContact",
740 request);
741 g_free(request);
744 sipe_buddy_remove(sipe_private, buddy);
745 } else {
746 if (sipe_ucs_is_migrated(sipe_private)) {
747 sipe_ucs_group_remove_buddy(sipe_private,
748 NULL,
749 group,
750 buddy);
751 } else
752 /* updates groups on server */
753 sipe_group_update_buddy(sipe_private, buddy);
757 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
758 const gchar *uri,
759 guint activity,
760 time_t last_active)
762 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
763 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
764 uri);
766 if (!sbuddy) return;
768 /* Check if on 2005 system contact's calendar,
769 * then set/preserve it.
771 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
772 sipe_backend_buddy_set_status(sipe_public,
773 uri,
774 activity,
775 last_active);
776 } else {
777 sipe_ocs2005_apply_calendar_status(sipe_private,
778 sbuddy,
779 sipe_status_activity_to_token(activity));
783 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
784 const gchar *uri,
785 const gchar *status_name,
786 gboolean is_online,
787 struct sipe_backend_buddy_tooltip *tooltip)
789 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
790 gchar *note = NULL;
791 gboolean is_oof_note = FALSE;
792 const gchar *activity = NULL;
793 gchar *calendar = NULL;
794 const gchar *meeting_subject = NULL;
795 const gchar *meeting_location = NULL;
796 gchar *access_text = NULL;
798 #define SIPE_ADD_BUDDY_INFO(l, t) \
800 gchar *tmp = g_markup_escape_text((t), -1); \
801 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
802 g_free(tmp); \
804 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
805 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
807 if (sipe_public) { /* happens on pidgin exit */
808 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
809 uri);
810 if (sbuddy) {
811 note = sbuddy->note;
812 is_oof_note = sbuddy->is_oof_note;
813 activity = sbuddy->activity;
814 calendar = sipe_cal_get_description(sbuddy);
815 meeting_subject = sbuddy->meeting_subject;
816 meeting_location = sbuddy->meeting_location;
818 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
819 gboolean is_group_access = FALSE;
820 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
821 "user",
822 sipe_get_no_sip_uri(uri),
823 &is_group_access);
824 const char *access_level = sipe_ocs2007_access_level_name(container_id);
825 access_text = is_group_access ?
826 g_strdup(access_level) :
827 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
828 access_level);
832 if (is_online) {
833 const gchar *status_str = activity ? activity : status_name;
835 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
837 if (is_online && !is_empty(calendar)) {
838 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
840 g_free(calendar);
841 if (!is_empty(meeting_location)) {
842 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
843 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
845 if (!is_empty(meeting_subject)) {
846 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
847 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
849 if (note) {
850 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
851 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
852 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
853 note_italics);
854 g_free(note_italics);
856 if (access_text) {
857 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
858 g_free(access_text);
862 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
863 const char *uri,
864 sipe_buddy_info_fields propkey,
865 char *property_value)
867 GSList *buddies, *entry;
869 if (property_value)
870 property_value = g_strstrip(property_value);
872 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
873 while (entry) {
874 gchar *prop_str;
875 sipe_backend_buddy p_buddy = entry->data;
877 /* for Display Name */
878 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
879 gchar *alias;
880 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
881 if (property_value && sipe_is_bad_alias(uri, alias)) {
882 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
883 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
885 g_free(alias);
887 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
888 if (!is_empty(property_value) &&
889 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
891 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
892 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
894 g_free(alias);
896 /* for other properties */
897 else {
898 if (!is_empty(property_value)) {
899 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
900 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
901 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
903 g_free(prop_str);
907 entry = entry->next;
909 g_slist_free(buddies);
913 struct ms_dlx_data;
914 struct ms_dlx_data {
915 GSList *search_rows;
916 gchar *other;
917 guint max_returns;
918 sipe_svc_callback *callback;
919 struct sipe_svc_session *session;
920 gchar *wsse_security;
921 struct sipe_backend_search_token *token;
922 /* must call ms_dlx_free() */
923 void (*failed_callback)(struct sipe_core_private *sipe_private,
924 struct ms_dlx_data *mdd);
927 static void free_search_rows(GSList *search_rows)
929 sipe_utils_slist_free_full(search_rows, g_free);
932 static void ms_dlx_free(struct ms_dlx_data *mdd)
934 free_search_rows(mdd->search_rows);
935 sipe_svc_session_close(mdd->session);
936 g_free(mdd->other);
937 g_free(mdd->wsse_security);
938 g_free(mdd);
941 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
942 #define DLX_SEARCH_ITEM \
943 "<AbEntryRequest.ChangeSearchQuery>" \
944 " <SearchOn>%s</SearchOn>" \
945 " <Value>%s</Value>" \
946 "</AbEntryRequest.ChangeSearchQuery>"
948 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
949 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
950 guint i = 0;
951 gchar *query = NULL;
953 while (query_rows) {
954 const gchar *attr;
955 gchar *value;
956 gchar *tmp = NULL;
958 attr = query_rows->data;
959 query_rows = g_slist_next(query_rows);
960 value = query_rows->data;
961 query_rows = g_slist_next(query_rows);
963 if (!value)
964 break;
967 * Special value for SIP ID
969 * Active Directory seems only to be able to search for
970 * SIP URIs. Make sure search string starts with "sip:".
972 if (!attr) {
973 attr = "msRTCSIP-PrimaryUserAddress";
974 if (!use_dlx)
975 value = tmp = sip_uri(value);
978 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
979 attr, value);
980 g_free(tmp);
982 attrs[i] = NULL;
984 if (i) {
985 query = g_strjoinv(NULL, attrs);
986 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
987 query ? query : "");
990 g_strfreev(attrs);
992 return query;
995 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
996 const gchar *base_uri,
997 const gchar *auth_uri,
998 const gchar *wsse_security,
999 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
1000 gpointer callback_data)
1002 struct ms_dlx_data *mdd = callback_data;
1004 if (wsse_security) {
1005 guint length = g_slist_length(mdd->search_rows);
1006 gchar *search;
1008 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
1009 base_uri);
1011 if (length > 0) {
1012 /* complex search */
1013 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
1014 search = g_strdup_printf("<ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
1015 " %s"
1016 "</ChangeSearch>",
1017 length / 2,
1018 query);
1019 g_free(query);
1020 } else {
1021 /* simple search */
1022 search = g_strdup_printf("<BasicSearch>"
1023 " <SearchList>c,company,displayName,givenName,mail,mailNickname,msRTCSIP-PrimaryUserAddress,sn</SearchList>"
1024 " <Value>%s</Value>"
1025 " <Verb>BeginsWith</Verb>"
1026 "</BasicSearch>",
1027 mdd->other);
1030 if (sipe_svc_ab_entry_request(sipe_private,
1031 mdd->session,
1032 auth_uri,
1033 wsse_security,
1034 search,
1035 mdd->max_returns,
1036 mdd->callback,
1037 mdd)) {
1039 /* keep webticket security token for potential further use */
1040 g_free(mdd->wsse_security);
1041 mdd->wsse_security = g_strdup(wsse_security);
1043 /* callback data passed down the line */
1044 mdd = NULL;
1046 g_free(search);
1048 } else {
1049 /* no ticket: this will show the minmum information */
1050 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
1051 base_uri);
1054 if (mdd)
1055 mdd->failed_callback(sipe_private, mdd);
1058 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
1059 struct ms_dlx_data *mdd)
1061 if (!sipe_webticket_request_with_port(sipe_private,
1062 mdd->session,
1063 sipe_private->dlx_uri,
1064 "AddressBookWebTicketBearer",
1065 ms_dlx_webticket,
1066 mdd)) {
1067 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
1068 sipe_private->dlx_uri);
1069 mdd->failed_callback(sipe_private, mdd);
1073 void sipe_buddy_search_contacts_finalize(struct sipe_core_private *sipe_private,
1074 struct sipe_backend_search_results *results,
1075 guint match_count,
1076 gboolean more)
1078 gchar *secondary = g_strdup_printf(
1079 dngettext(PACKAGE_NAME,
1080 "Found %d contact%s:",
1081 "Found %d contacts%s:", match_count),
1082 match_count, more ? _(" (more matched your query)") : "");
1084 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
1085 results,
1086 secondary,
1087 more);
1088 g_free(secondary);
1091 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
1092 const gchar *uri,
1093 SIPE_UNUSED_PARAMETER const gchar *raw,
1094 sipe_xml *soap_body,
1095 gpointer callback_data)
1097 struct ms_dlx_data *mdd = callback_data;
1099 if (soap_body) {
1100 const sipe_xml *node;
1101 struct sipe_backend_search_results *results;
1102 GHashTable *found;
1104 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
1105 uri);
1107 /* any matches? */
1108 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
1109 if (!node) {
1110 /* try again with simple search, if possible */
1111 if (mdd->other && mdd->search_rows) {
1112 SIPE_DEBUG_INFO_NOFORMAT("search_ab_entry_response: no matches, retrying with simple search");
1114 /* throw away original search query */
1115 free_search_rows(mdd->search_rows);
1116 mdd->search_rows = NULL;
1118 ms_dlx_webticket_request(sipe_private, mdd);
1120 /* callback data passed down the line */
1121 return;
1123 } else {
1124 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
1126 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1127 mdd->token,
1128 _("No contacts found"));
1129 ms_dlx_free(mdd);
1130 return;
1134 /* OK, we found something - show the results to the user */
1135 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
1136 mdd->token);
1137 if (!results) {
1138 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
1139 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1140 mdd->token,
1141 _("Unable to display the search results"));
1142 ms_dlx_free(mdd);
1143 return;
1146 /* SearchAbEntryResult can contain duplicates */
1147 found = g_hash_table_new_full(g_str_hash, g_str_equal,
1148 g_free, NULL);
1150 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
1151 const sipe_xml *attrs;
1152 gchar *sip_uri = NULL;
1153 gchar *displayname = NULL;
1154 gchar *company = NULL;
1155 gchar *country = NULL;
1156 gchar *email = NULL;
1158 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
1159 attrs;
1160 attrs = sipe_xml_twin(attrs)) {
1161 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
1162 "Name"));
1163 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
1164 "Value"));
1166 if (!is_empty(value)) {
1167 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
1168 g_free(sip_uri);
1169 sip_uri = value;
1170 value = NULL;
1171 } else if (sipe_strcase_equal(name, "displayname")) {
1172 g_free(displayname);
1173 displayname = value;
1174 value = NULL;
1175 } else if (sipe_strcase_equal(name, "mail")) {
1176 g_free(email);
1177 email = value;
1178 value = NULL;
1179 } else if (sipe_strcase_equal(name, "company")) {
1180 g_free(company);
1181 company = value;
1182 value = NULL;
1183 } else if (sipe_strcase_equal(name, "country")) {
1184 g_free(country);
1185 country = value;
1186 value = NULL;
1190 g_free(value);
1191 g_free(name);
1194 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
1195 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
1196 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
1197 results,
1198 uri_parts[1],
1199 displayname,
1200 company,
1201 country,
1202 email);
1203 g_strfreev(uri_parts);
1205 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
1206 sip_uri = NULL;
1209 g_free(email);
1210 g_free(country);
1211 g_free(company);
1212 g_free(displayname);
1213 g_free(sip_uri);
1216 sipe_buddy_search_contacts_finalize(sipe_private, results,
1217 g_hash_table_size(found),
1218 FALSE);
1219 g_hash_table_destroy(found);
1220 ms_dlx_free(mdd);
1222 } else {
1223 mdd->failed_callback(sipe_private, mdd);
1227 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
1228 struct sipmsg *msg,
1229 struct transaction *trans)
1231 struct sipe_backend_search_token *token = trans->payload->data;
1232 struct sipe_backend_search_results *results;
1233 sipe_xml *searchResults;
1234 const sipe_xml *mrow;
1235 guint match_count = 0;
1236 gboolean more = FALSE;
1238 /* valid response? */
1239 if (msg->response != 200) {
1240 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1241 msg->response);
1242 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1243 token,
1244 _("Contact search failed"));
1245 return(FALSE);
1248 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
1250 /* valid XML? */
1251 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1252 if (!searchResults) {
1253 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1254 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1255 token,
1256 _("Contact search failed"));
1257 return(FALSE);
1260 /* any matches? */
1261 mrow = sipe_xml_child(searchResults, "Body/Array/row");
1262 if (!mrow) {
1263 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1264 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1265 token,
1266 _("No contacts found"));
1268 sipe_xml_free(searchResults);
1269 return(FALSE);
1272 /* OK, we found something - show the results to the user */
1273 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
1274 trans->payload->data);
1275 if (!results) {
1276 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1277 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1278 token,
1279 _("Unable to display the search results"));
1281 sipe_xml_free(searchResults);
1282 return FALSE;
1285 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
1286 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
1287 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
1288 results,
1289 uri_parts[1],
1290 sipe_xml_attribute(mrow, "displayName"),
1291 sipe_xml_attribute(mrow, "company"),
1292 sipe_xml_attribute(mrow, "country"),
1293 sipe_xml_attribute(mrow, "email"));
1294 g_strfreev(uri_parts);
1295 match_count++;
1298 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
1299 char *data = sipe_xml_data(mrow);
1300 more = (g_ascii_strcasecmp(data, "true") == 0);
1301 g_free(data);
1304 sipe_buddy_search_contacts_finalize(sipe_private, results, match_count, more);
1305 sipe_xml_free(searchResults);
1307 return(TRUE);
1310 static void search_soap_request(struct sipe_core_private *sipe_private,
1311 GDestroyNotify destroy,
1312 void *data,
1313 guint max,
1314 SoapTransCallback callback,
1315 GSList *search_rows)
1317 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
1318 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1320 payload->destroy = destroy;
1321 payload->data = data;
1323 sip_soap_directory_search(sipe_private,
1324 max,
1325 query,
1326 callback,
1327 payload);
1328 g_free(query);
1331 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
1332 struct ms_dlx_data *mdd)
1334 /* error using [MS-DLX] server, retry using Active Directory */
1335 if (mdd->search_rows)
1336 search_soap_request(sipe_private,
1337 NULL,
1338 mdd->token,
1339 100,
1340 process_search_contact_response,
1341 mdd->search_rows);
1342 ms_dlx_free(mdd);
1345 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
1346 struct sipe_backend_search_token *token,
1347 const gchar *given_name,
1348 const gchar *surname,
1349 const gchar *email,
1350 const gchar *sipid,
1351 const gchar *company,
1352 const gchar *country)
1354 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1356 /* Lync 2013 or newer: use UCS if contacts are migrated */
1357 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) &&
1358 sipe_ucs_is_migrated(sipe_private)) {
1360 sipe_ucs_search(sipe_private,
1361 token,
1362 given_name,
1363 surname,
1364 email,
1365 sipid,
1366 company,
1367 country);
1369 } else {
1370 GSList *query_rows = NULL;
1371 guint count = 0;
1372 const gchar *simple = NULL;
1374 #define ADD_QUERY_ROW(attr, val) \
1375 if (val) { \
1376 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1377 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1378 simple = val; \
1379 count++; \
1382 ADD_QUERY_ROW("givenName", given_name);
1383 ADD_QUERY_ROW("sn", surname);
1384 ADD_QUERY_ROW("mail", email);
1385 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1386 ADD_QUERY_ROW(NULL, sipid);
1387 ADD_QUERY_ROW("company", company);
1388 ADD_QUERY_ROW("c", country);
1390 if (query_rows) {
1391 if (sipe_private->dlx_uri != NULL) {
1392 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1394 mdd->search_rows = query_rows;
1395 /* user entered only one search string, remember that one */
1396 if (count == 1)
1397 mdd->other = g_strdup(simple);
1398 mdd->max_returns = 100;
1399 mdd->callback = search_ab_entry_response;
1400 mdd->failed_callback = search_ab_entry_failed;
1401 mdd->session = sipe_svc_session_start();
1402 mdd->token = token;
1404 ms_dlx_webticket_request(sipe_private, mdd);
1406 } else {
1407 /* no [MS-DLX] server, use Active Directory search instead */
1408 search_soap_request(sipe_private,
1409 NULL,
1410 token,
1411 100,
1412 process_search_contact_response,
1413 query_rows);
1414 free_search_rows(query_rows);
1416 } else
1417 sipe_backend_search_failed(sipe_public,
1418 token,
1419 _("Invalid contact search query"));
1423 static void get_info_finalize(struct sipe_core_private *sipe_private,
1424 struct sipe_backend_buddy_info *info,
1425 const gchar *uri,
1426 const gchar *server_alias,
1427 const gchar *email)
1429 sipe_backend_buddy bbuddy;
1430 struct sipe_buddy *sbuddy;
1431 gchar *alias;
1432 gchar *value;
1434 if (!info) {
1435 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1436 } else {
1437 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
1439 if (!info)
1440 return;
1442 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
1444 if (is_empty(server_alias)) {
1445 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
1446 bbuddy);
1447 if (value) {
1448 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1449 info,
1450 SIPE_BUDDY_INFO_DISPLAY_NAME,
1451 value);
1453 } else {
1454 value = g_strdup(server_alias);
1457 /* present alias if it differs from server alias */
1458 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
1459 if (alias && !sipe_strequal(alias, value))
1461 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1462 info,
1463 SIPE_BUDDY_INFO_ALIAS,
1464 alias);
1466 g_free(alias);
1467 g_free(value);
1469 if (is_empty(email)) {
1470 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1471 bbuddy,
1472 SIPE_BUDDY_INFO_EMAIL);
1473 if (value) {
1474 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1475 info,
1476 SIPE_BUDDY_INFO_EMAIL,
1477 value);
1478 g_free(value);
1482 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1483 bbuddy,
1484 SIPE_BUDDY_INFO_SITE);
1485 if (value) {
1486 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1487 info,
1488 SIPE_BUDDY_INFO_SITE,
1489 value);
1490 g_free(value);
1493 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
1494 if (sbuddy && sbuddy->device_name) {
1495 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1496 info,
1497 SIPE_BUDDY_INFO_DEVICE,
1498 sbuddy->device_name);
1501 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
1505 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
1506 const gchar *uri,
1507 SIPE_UNUSED_PARAMETER const gchar *raw,
1508 sipe_xml *soap_body,
1509 gpointer callback_data)
1511 struct ms_dlx_data *mdd = callback_data;
1512 struct sipe_backend_buddy_info *info = NULL;
1513 gchar *server_alias = NULL;
1514 gchar *email = NULL;
1516 if (soap_body) {
1517 const sipe_xml *node;
1519 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1520 uri);
1522 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1524 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1525 node;
1526 node = sipe_xml_twin(node)) {
1527 gchar *name = sipe_xml_data(sipe_xml_child(node,
1528 "Name"));
1529 gchar *value = sipe_xml_data(sipe_xml_child(node,
1530 "Value"));
1531 const sipe_xml *values = sipe_xml_child(node,
1532 "Values");
1534 /* Single value entries */
1535 if (!is_empty(value)) {
1537 if (sipe_strcase_equal(name, "displayname")) {
1538 g_free(server_alias);
1539 server_alias = value;
1540 value = NULL;
1541 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1542 info,
1543 SIPE_BUDDY_INFO_DISPLAY_NAME,
1544 server_alias);
1545 } else if (sipe_strcase_equal(name, "mail")) {
1546 g_free(email);
1547 email = value;
1548 value = NULL;
1549 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1550 info,
1551 SIPE_BUDDY_INFO_EMAIL,
1552 email);
1553 } else if (sipe_strcase_equal(name, "title")) {
1554 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1555 info,
1556 SIPE_BUDDY_INFO_JOB_TITLE,
1557 value);
1558 } else if (sipe_strcase_equal(name, "company")) {
1559 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1560 info,
1561 SIPE_BUDDY_INFO_COMPANY,
1562 value);
1563 } else if (sipe_strcase_equal(name, "country")) {
1564 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1565 info,
1566 SIPE_BUDDY_INFO_COUNTRY,
1567 value);
1570 } else if (values) {
1571 gchar *first = sipe_xml_data(sipe_xml_child(values,
1572 "string"));
1574 if (sipe_strcase_equal(name, "telephonenumber")) {
1575 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1576 info,
1577 SIPE_BUDDY_INFO_WORK_PHONE,
1578 first);
1581 g_free(first);
1584 g_free(value);
1585 g_free(name);
1589 /* this will show the minmum information */
1590 get_info_finalize(sipe_private,
1591 info,
1592 mdd->other,
1593 server_alias,
1594 email);
1596 g_free(email);
1597 g_free(server_alias);
1598 ms_dlx_free(mdd);
1601 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1602 struct sipmsg *msg,
1603 struct transaction *trans)
1605 const gchar *uri = trans->payload->data;
1606 struct sipe_backend_buddy_info *info = NULL;
1607 gchar *server_alias = NULL;
1608 gchar *email = NULL;
1610 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1611 uri, sipe_private->username);
1613 if (msg->response != 200) {
1614 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1615 } else {
1616 sipe_xml *searchResults;
1617 const sipe_xml *mrow;
1619 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1620 msg->body ? msg->body : "");
1622 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1623 if (!searchResults) {
1625 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1627 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1628 const gchar *value;
1629 gchar *phone_number;
1631 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1633 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1634 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1635 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1638 * For 2007 system we will take this from ContactCard -
1639 * it has cleaner tel: URIs at least
1641 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1642 char *tel_uri = sip_to_tel_uri(phone_number);
1643 /* trims its parameters, so call first */
1644 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1645 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1646 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1647 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1648 g_free(tel_uri);
1650 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1651 uri);
1654 if (!is_empty(server_alias)) {
1655 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1656 info,
1657 SIPE_BUDDY_INFO_DISPLAY_NAME,
1658 server_alias);
1660 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1661 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1662 info,
1663 SIPE_BUDDY_INFO_JOB_TITLE,
1664 value);
1666 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1667 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1668 info,
1669 SIPE_BUDDY_INFO_OFFICE,
1670 value);
1672 if (!is_empty(phone_number)) {
1673 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1674 info,
1675 SIPE_BUDDY_INFO_WORK_PHONE,
1676 phone_number);
1678 g_free(phone_number);
1679 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1680 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1681 info,
1682 SIPE_BUDDY_INFO_COMPANY,
1683 value);
1685 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1686 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1687 info,
1688 SIPE_BUDDY_INFO_CITY,
1689 value);
1691 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1692 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1693 info,
1694 SIPE_BUDDY_INFO_STATE,
1695 value);
1697 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1698 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1699 info,
1700 SIPE_BUDDY_INFO_COUNTRY,
1701 value);
1703 if (!is_empty(email)) {
1704 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1705 info,
1706 SIPE_BUDDY_INFO_EMAIL,
1707 email);
1710 sipe_xml_free(searchResults);
1713 /* this will show the minmum information */
1714 get_info_finalize(sipe_private,
1715 info,
1716 uri,
1717 server_alias,
1718 email);
1720 g_free(server_alias);
1721 g_free(email);
1723 return TRUE;
1726 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1727 struct ms_dlx_data *mdd)
1729 /* error using [MS-DLX] server, retry using Active Directory */
1730 search_soap_request(sipe_private,
1731 g_free,
1732 mdd->other,
1734 process_get_info_response,
1735 mdd->search_rows);
1736 mdd->other = NULL;
1737 ms_dlx_free(mdd);
1740 static GSList *search_rows_for_uri(const gchar *uri)
1742 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1743 GSList *l = g_slist_append(NULL, NULL);
1744 return(g_slist_append(l, g_strdup(uri)));
1747 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1748 const gchar *who)
1750 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1751 GSList *search_rows = search_rows_for_uri(who);
1753 if (sipe_private->dlx_uri) {
1754 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1756 mdd->search_rows = search_rows;
1757 mdd->other = g_strdup(who);
1758 mdd->max_returns = 1;
1759 mdd->callback = get_info_ab_entry_response;
1760 mdd->failed_callback = get_info_ab_entry_failed;
1761 mdd->session = sipe_svc_session_start();
1763 ms_dlx_webticket_request(sipe_private, mdd);
1765 } else {
1766 /* no [MS-DLX] server, use Active Directory search instead */
1767 search_soap_request(sipe_private,
1768 g_free,
1769 g_strdup(who),
1771 process_get_info_response,
1772 search_rows);
1773 free_search_rows(search_rows);
1777 static void photo_response_data_free(struct photo_response_data *data)
1779 g_free(data->who);
1780 g_free(data->photo_hash);
1781 if (data->request) {
1782 sipe_http_request_cancel(data->request);
1784 g_free(data);
1787 static void photo_response_data_remove(struct sipe_core_private *sipe_private,
1788 struct photo_response_data *data)
1790 data->request = NULL;
1791 sipe_private->buddies->pending_photo_requests =
1792 g_slist_remove(sipe_private->buddies->pending_photo_requests, data);
1793 photo_response_data_free(data);
1796 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1797 guint status,
1798 GSList *headers,
1799 const char *body,
1800 gpointer data)
1802 struct photo_response_data *rdata = (struct photo_response_data *) data;
1804 if (status == SIPE_HTTP_STATUS_OK) {
1805 const gchar *len_str = sipe_utils_nameval_find(headers,
1806 "Content-Length");
1807 if (len_str) {
1808 gsize photo_size = atoi(len_str);
1809 gpointer photo = g_new(char, photo_size);
1811 if (photo) {
1812 memcpy(photo, body, photo_size);
1814 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1815 rdata->who,
1816 photo,
1817 photo_size,
1818 rdata->photo_hash);
1823 photo_response_data_remove(sipe_private, rdata);
1826 static void process_get_user_photo_response(struct sipe_core_private *sipe_private,
1827 guint status,
1828 SIPE_UNUSED_PARAMETER GSList *headers,
1829 const gchar *body,
1830 gpointer data)
1832 struct photo_response_data *rdata = (struct photo_response_data *) data;
1834 if ((status == SIPE_HTTP_STATUS_OK) && body) {
1835 sipe_xml *xml = sipe_xml_parse(body, strlen(body));
1836 const sipe_xml *node = sipe_xml_child(xml,
1837 "Body/GetUserPhotoResponse/PictureData");
1839 if (node) {
1840 gchar *base64;
1841 gsize photo_size;
1842 guchar *photo;
1844 /* decode photo data */
1845 base64 = sipe_xml_data(node);
1846 photo = g_base64_decode(base64, &photo_size);
1847 g_free(base64);
1849 /* EWS doesn't provide a hash -> calculate SHA-1 digest */
1850 if (!rdata->photo_hash) {
1851 guchar digest[SIPE_DIGEST_SHA1_LENGTH];
1852 sipe_digest_sha1(photo, photo_size, digest);
1854 /* rdata takes ownership of digest string */
1855 rdata->photo_hash = buff_to_hex_str(digest,
1856 SIPE_DIGEST_SHA1_LENGTH);
1859 /* backend frees "photo" */
1860 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1861 rdata->who,
1862 photo,
1863 photo_size,
1864 rdata->photo_hash);
1867 sipe_xml_free(xml);
1870 photo_response_data_remove(sipe_private, rdata);
1873 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1875 gchar *assertion = sipe_xml_extract_raw(wsse_security, "Assertion", TRUE);
1876 gchar *wsse_security_base64;
1877 gchar *x_ms_webticket_header;
1879 if (!assertion) {
1880 return NULL;
1883 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1884 strlen(assertion));
1885 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1886 wsse_security_base64);
1888 g_free(assertion);
1889 g_free(wsse_security_base64);
1891 return x_ms_webticket_header;
1894 /* see also sipe_ucs_http_request() */
1895 static struct sipe_http_request *get_user_photo_request(struct sipe_core_private *sipe_private,
1896 struct photo_response_data *data,
1897 const gchar *ews_url,
1898 const gchar *email)
1900 gchar *soap = g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
1901 "<soap:Envelope"
1902 " xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\""
1903 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
1904 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\""
1905 " >"
1906 " <soap:Header>"
1907 " <t:RequestServerVersion Version=\"Exchange2013\" />"
1908 " </soap:Header>"
1909 " <soap:Body>"
1910 " <m:GetUserPhoto>"
1911 " <m:Email>%s</m:Email>"
1912 " <m:SizeRequested>HR48x48</m:SizeRequested>"
1913 " </m:GetUserPhoto>"
1914 " </soap:Body>"
1915 "</soap:Envelope>",
1916 email);
1917 struct sipe_http_request *request = sipe_http_request_post(sipe_private,
1918 ews_url,
1919 NULL,
1920 soap,
1921 "text/xml; charset=UTF-8",
1922 process_get_user_photo_response,
1923 data);
1924 g_free(soap);
1926 if (request) {
1927 sipe_core_email_authentication(sipe_private,
1928 request);
1929 sipe_http_request_allow_redirect(request);
1930 } else {
1931 SIPE_DEBUG_ERROR_NOFORMAT("get_user_photo_request: failed to create HTTP connection");
1934 return(request);
1937 static void photo_response_data_finalize(struct sipe_core_private *sipe_private,
1938 struct photo_response_data *data,
1939 const gchar *uri,
1940 const gchar *photo_hash)
1942 if (data->request) {
1943 data->who = g_strdup(uri);
1944 data->photo_hash = g_strdup(photo_hash);
1946 sipe_private->buddies->pending_photo_requests =
1947 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1948 sipe_http_request_ready(data->request);
1949 } else {
1950 photo_response_data_free(data);
1954 void sipe_buddy_update_photo(struct sipe_core_private *sipe_private,
1955 const gchar *uri,
1956 const gchar *photo_hash,
1957 const gchar *photo_url,
1958 const gchar *headers)
1960 const gchar *photo_hash_old =
1961 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, uri);
1963 if (!sipe_strequal(photo_hash, photo_hash_old)) {
1964 struct photo_response_data *data = g_new0(struct photo_response_data, 1);
1966 SIPE_DEBUG_INFO("sipe_buddy_update_photo: who '%s' url '%s' hash '%s'",
1967 uri, photo_url, photo_hash);
1969 /* Photo URL is embedded XML? */
1970 if (g_str_has_prefix(photo_url, "<") &&
1971 g_str_has_suffix(photo_url, ">")) {
1972 /* add dummy root to embedded XML string */
1973 gchar *tmp = g_strdup_printf("<r>%s</r>", photo_url);
1974 sipe_xml *xml = sipe_xml_parse(tmp, strlen(tmp));
1975 g_free(tmp);
1977 if (xml) {
1978 gchar *ews_url = sipe_xml_data(sipe_xml_child(xml, "ewsUrl"));
1979 gchar *email = sipe_xml_data(sipe_xml_child(xml, "primarySMTP"));
1981 if (!is_empty(ews_url) && !is_empty(email)) {
1983 * Workaround for missing Office 365 buddy icons
1985 * (All?) Office 365 contact cards have the following
1986 * XML embedded as the photo URI XML node text:
1988 * <ewsUrl>https://outlook.office365.com/EWS/Exchange.asmx/WSSecurity</ewsUrl>
1989 * <primarySMTP>user@company.com</primarySMTP>
1991 * The simple HTTP request by get_user_photo_request()
1992 * is rejected with 401. But the response contains
1994 * WWW-Authenticate: Basic Realm=""
1996 * to which the HTTP transport answers with a retry
1997 * using Basic authentication. That in turn is rejected
1998 * with 500 and thus the buddy icon retrieval fails.
2000 * As a quick workaround strip the trailing "/WSSecurity"
2001 * from the URL. The HTTP request for the buddy icon
2002 * retrieval will work with this stripped URL.
2004 * @TODO: this is probably not the correct approach.
2005 * get_user_photo_request() should be updated
2006 * to support also a webticket request.
2008 gchar *tmp = g_strrstr(ews_url, "/WSSecurity");
2009 if (tmp)
2010 *tmp = '\0';
2012 data->request = get_user_photo_request(sipe_private,
2013 data,
2014 ews_url,
2015 email);
2018 g_free(email);
2019 g_free(ews_url);
2020 sipe_xml_free(xml);
2022 } else {
2023 data->request = sipe_http_request_get(sipe_private,
2024 photo_url,
2025 headers,
2026 process_buddy_photo_response,
2027 data);
2030 photo_response_data_finalize(sipe_private,
2031 data,
2032 uri,
2033 photo_hash);
2037 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
2038 const gchar *uri,
2039 SIPE_UNUSED_PARAMETER const gchar *raw,
2040 sipe_xml *soap_body,
2041 gpointer callback_data)
2043 struct ms_dlx_data *mdd = callback_data;
2044 gchar *photo_rel_path = NULL;
2045 gchar *photo_hash = NULL;
2047 if (soap_body) {
2048 const sipe_xml *node;
2050 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
2051 uri);
2053 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
2054 node;
2055 node = sipe_xml_twin(node)) {
2056 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
2057 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
2059 if (!is_empty(value)) {
2060 if (sipe_strcase_equal(name, "PhotoRelPath")) {
2061 g_free(photo_rel_path);
2062 photo_rel_path = value;
2063 value = NULL;
2064 } else if (sipe_strcase_equal(name, "PhotoHash")) {
2065 g_free(photo_hash);
2066 photo_hash = value;
2067 value = NULL;
2071 g_free(value);
2072 g_free(name);
2076 if (sipe_private->addressbook_uri && photo_rel_path && photo_hash) {
2077 gchar *photo_url = g_strdup_printf("%s/%s",
2078 sipe_private->addressbook_uri, photo_rel_path);
2079 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
2081 sipe_buddy_update_photo(sipe_private,
2082 mdd->other,
2083 photo_hash,
2084 photo_url,
2085 x_ms_webticket_header);
2087 g_free(x_ms_webticket_header);
2088 g_free(photo_url);
2091 g_free(photo_rel_path);
2092 g_free(photo_hash);
2093 ms_dlx_free(mdd);
2096 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
2097 struct ms_dlx_data *mdd)
2099 ms_dlx_free(mdd);
2102 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
2103 const gchar *uri)
2105 if (sipe_backend_uses_photo()) {
2107 /* Lync 2013 or newer: use UCS if contacts are migrated */
2108 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) &&
2109 sipe_ucs_is_migrated(sipe_private)) {
2110 struct photo_response_data *data = g_new0(struct photo_response_data, 1);
2112 data->request = get_user_photo_request(sipe_private,
2113 data,
2114 sipe_ucs_ews_url(sipe_private),
2115 sipe_get_no_sip_uri(uri));
2116 photo_response_data_finalize(sipe_private,
2117 data,
2118 uri,
2119 /* there is no hash */
2120 NULL);
2122 /* Lync 2010: use [MS-DLX] */
2123 } else if (sipe_private->dlx_uri &&
2124 sipe_private->addressbook_uri) {
2125 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
2127 mdd->search_rows = search_rows_for_uri(uri);
2128 mdd->other = g_strdup(uri);
2129 mdd->max_returns = 1;
2130 mdd->callback = get_photo_ab_entry_response;
2131 mdd->failed_callback = get_photo_ab_entry_failed;
2132 mdd->session = sipe_svc_session_start();
2134 ms_dlx_webticket_request(sipe_private, mdd);
2139 static void buddy_refresh_photos_cb(gpointer uri,
2140 SIPE_UNUSED_PARAMETER gpointer value,
2141 gpointer sipe_private)
2143 buddy_fetch_photo(sipe_private, uri);
2146 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
2148 g_hash_table_foreach(sipe_private->buddies->uri,
2149 buddy_refresh_photos_cb,
2150 sipe_private);
2153 /* Buddy menu callbacks*/
2155 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
2156 const gchar *who)
2158 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
2160 /* 2007+ conference */
2161 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
2162 sipe_conf_add(sipe_private, who);
2164 /* 2005- multiparty chat */
2165 } else {
2166 gchar *self = sip_uri_self(sipe_private);
2167 struct sip_session *session;
2169 session = sipe_session_add_chat(sipe_private,
2170 NULL,
2171 TRUE,
2172 self);
2173 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
2174 session->chat_session,
2175 session->chat_session->title,
2176 self);
2177 g_free(self);
2179 sipe_im_invite(sipe_private, session, who,
2180 NULL, NULL, NULL, FALSE);
2184 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
2185 const gchar *who)
2187 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
2188 who,
2189 NULL);
2190 gchar *email = sipe_backend_buddy_get_string(sipe_public,
2191 buddy,
2192 SIPE_BUDDY_INFO_EMAIL);
2194 if (email) {
2195 gchar *command_line = g_strdup_printf(
2196 #ifdef _WIN32
2197 "cmd /c start"
2198 #else
2199 "xdg-email"
2200 #endif
2201 " mailto:%s", email);
2202 g_free(email);
2204 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
2205 command_line);
2206 g_spawn_command_line_async(command_line, NULL);
2207 g_free(command_line);
2209 } else {
2210 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
2211 who);
2215 /* Buddy menu */
2217 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
2218 struct sipe_backend_buddy_menu *menu,
2219 sipe_backend_buddy buddy,
2220 sipe_buddy_info_fields id_phone,
2221 sipe_buddy_info_fields id_display,
2222 const gchar *type)
2224 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
2225 buddy,
2226 id_phone);
2227 if (phone) {
2228 gchar *display = sipe_backend_buddy_get_string(sipe_public,
2229 buddy,
2230 id_display);
2231 gchar *tmp = NULL;
2232 gchar *label = g_strdup_printf("%s %s",
2233 type,
2234 display ? display :
2235 (tmp = sip_tel_uri_denormalize(phone)));
2236 menu = sipe_backend_buddy_menu_add(sipe_public,
2237 menu,
2238 label,
2239 SIPE_BUDDY_MENU_MAKE_CALL,
2240 phone);
2241 g_free(tmp);
2242 g_free(label);
2243 g_free(display);
2244 g_free(phone);
2247 return(menu);
2250 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
2251 const gchar *buddy_name,
2252 struct sipe_backend_buddy_menu *menu)
2254 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
2255 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
2256 buddy_name,
2257 NULL);
2258 gchar *self = sip_uri_self(sipe_private);
2260 SIPE_SESSION_FOREACH {
2261 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
2263 struct sipe_chat_session *chat_session = session->chat_session;
2264 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
2266 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
2268 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
2270 if (is_conf &&
2271 /* Not conf OP */
2272 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
2273 /* We are a conf OP */
2274 conf_op) {
2275 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
2276 chat_session->title);
2277 menu = sipe_backend_buddy_menu_add(sipe_public,
2278 menu,
2279 label,
2280 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
2281 chat_session);
2282 g_free(label);
2285 if (is_conf &&
2286 /* We are a conf OP */
2287 conf_op) {
2288 gchar *label = g_strdup_printf(_("Remove from '%s'"),
2289 chat_session->title);
2290 menu = sipe_backend_buddy_menu_add(sipe_public,
2291 menu,
2292 label,
2293 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
2294 chat_session);
2295 g_free(label);
2298 else
2300 if (!is_conf ||
2301 (is_conf && !session->locked)) {
2302 gchar *label = g_strdup_printf(_("Invite to '%s'"),
2303 chat_session->title);
2304 menu = sipe_backend_buddy_menu_add(sipe_public,
2305 menu,
2306 label,
2307 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
2308 chat_session);
2309 g_free(label);
2313 } SIPE_SESSION_FOREACH_END;
2314 g_free(self);
2316 menu = sipe_backend_buddy_menu_add(sipe_public,
2317 menu,
2318 _("New chat"),
2319 SIPE_BUDDY_MENU_NEW_CHAT,
2320 NULL);
2322 /* add buddy's phone numbers if we have call control */
2323 if (sip_csta_is_idle(sipe_private)) {
2325 /* work phone */
2326 menu = buddy_menu_phone(sipe_public,
2327 menu,
2328 buddy,
2329 SIPE_BUDDY_INFO_WORK_PHONE,
2330 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
2331 _("Work"));
2332 /* mobile phone */
2333 menu = buddy_menu_phone(sipe_public,
2334 menu,
2335 buddy,
2336 SIPE_BUDDY_INFO_MOBILE_PHONE,
2337 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
2338 _("Mobile"));
2340 /* home phone */
2341 menu = buddy_menu_phone(sipe_public,
2342 menu,
2343 buddy,
2344 SIPE_BUDDY_INFO_HOME_PHONE,
2345 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
2346 _("Home"));
2348 /* other phone */
2349 menu = buddy_menu_phone(sipe_public,
2350 menu,
2351 buddy,
2352 SIPE_BUDDY_INFO_OTHER_PHONE,
2353 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
2354 _("Other"));
2356 /* custom1 phone */
2357 menu = buddy_menu_phone(sipe_public,
2358 menu,
2359 buddy,
2360 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
2361 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
2362 _("Custom1"));
2366 gchar *email = sipe_backend_buddy_get_string(sipe_public,
2367 buddy,
2368 SIPE_BUDDY_INFO_EMAIL);
2369 if (email) {
2370 menu = sipe_backend_buddy_menu_add(sipe_public,
2371 menu,
2372 _("Send email..."),
2373 SIPE_BUDDY_MENU_SEND_EMAIL,
2374 NULL);
2375 g_free(email);
2379 #ifdef HAVE_APPSHARE_SERVER
2381 struct sipe_media_call *call = sipe_media_call_find(SIPE_CORE_PRIVATE,
2382 buddy_name);
2384 if (call && sipe_appshare_get_role(call) == SIPE_APPSHARE_ROLE_PRESENTER) {
2385 /* We're already presenting to this buddy. */
2387 if (sipe_core_appshare_get_remote_control(call)) {
2388 menu = sipe_backend_buddy_menu_add(sipe_public, menu,
2389 _("Take desktop control"),
2390 SIPE_BUDDY_MENU_TAKE_DESKTOP_CONTROL,
2391 call);
2392 } else {
2393 menu = sipe_backend_buddy_menu_add(sipe_public, menu,
2394 _("Give desktop control"),
2395 SIPE_BUDDY_MENU_GIVE_DESKTOP_CONTROL,
2396 call);
2398 } else {
2399 menu = sipe_backend_buddy_menu_add(sipe_public, menu,
2400 _("Share my desktop"),
2401 SIPE_BUDDY_MENU_SHARE_DESKTOP,
2402 NULL);
2405 #endif // HAVE_APPSHARE_SERVER
2407 /* access level control */
2408 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
2409 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
2410 menu,
2411 _("Access level"),
2412 sipe_ocs2007_access_control_menu(sipe_private,
2413 buddy_name));
2415 return(menu);
2418 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
2420 return(g_hash_table_size(sipe_private->buddies->uri));
2423 static guint sipe_ht_hash_nick(const char *nick)
2425 char *lc = g_utf8_strdown(nick, -1);
2426 guint bucket = g_str_hash(lc);
2427 g_free(lc);
2429 return bucket;
2432 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
2434 char *nick1_norm = NULL;
2435 char *nick2_norm = NULL;
2436 gboolean equal;
2438 if (nick1 == NULL && nick2 == NULL) return TRUE;
2439 if (nick1 == NULL || nick2 == NULL ||
2440 !g_utf8_validate(nick1, -1, NULL) ||
2441 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
2443 nick1_norm = g_utf8_casefold(nick1, -1);
2444 nick2_norm = g_utf8_casefold(nick2, -1);
2445 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
2446 g_free(nick2_norm);
2447 g_free(nick1_norm);
2449 return equal;
2452 void sipe_buddy_init(struct sipe_core_private *sipe_private)
2454 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
2455 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
2456 (GEqualFunc) sipe_ht_equals_nick);
2457 buddies->exchange_key = g_hash_table_new(g_str_hash,
2458 g_str_equal);
2459 sipe_private->buddies = buddies;
2463 Local Variables:
2464 mode: c
2465 c-file-style: "bsd"
2466 indent-tabs-mode: t
2467 tab-width: 8
2468 End: