ucs: move GetUserPhoto operation to buddy
[siplcs.git] / src / core / sipe-buddy.c
blob4782dacc8f3b9047f27522f754a057c29472d1ec
1 /**
2 * @file sipe-buddy.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2016 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-digest.h"
49 #include "sipe-group.h"
50 #include "sipe-http.h"
51 #include "sipe-im.h"
52 #include "sipe-nls.h"
53 #include "sipe-ocs2005.h"
54 #include "sipe-ocs2007.h"
55 #include "sipe-schedule.h"
56 #include "sipe-session.h"
57 #include "sipe-status.h"
58 #include "sipe-subscriptions.h"
59 #include "sipe-svc.h"
60 #include "sipe-ucs.h"
61 #include "sipe-utils.h"
62 #include "sipe-webticket.h"
63 #include "sipe-xml.h"
65 struct sipe_buddies {
66 GHashTable *uri;
67 GHashTable *exchange_key;
69 /* Pending photo download HTTP requests */
70 GSList *pending_photo_requests;
73 struct buddy_group_data {
74 const struct sipe_group *group;
75 gboolean is_obsolete;
78 struct photo_response_data {
79 gchar *who;
80 gchar *photo_hash;
81 struct sipe_http_request *request;
84 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
85 const gchar *uri);
86 static void photo_response_data_free(struct photo_response_data *data);
88 void sipe_buddy_add_keys(struct sipe_core_private *sipe_private,
89 struct sipe_buddy *buddy,
90 const gchar *exchange_key,
91 const gchar *change_key)
93 if (exchange_key) {
94 buddy->exchange_key = g_strdup(exchange_key);
95 g_hash_table_insert(sipe_private->buddies->exchange_key,
96 buddy->exchange_key,
97 buddy);
99 if (change_key)
100 buddy->change_key = g_strdup(change_key);
103 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
104 const gchar *uri,
105 const gchar *exchange_key,
106 const gchar *change_key)
108 /* Buddy name must be lower case as we use purple_normalize_nocase() to compare */
109 gchar *normalized_uri = g_ascii_strdown(uri, -1);
110 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
111 normalized_uri);
113 if (!buddy) {
114 buddy = g_new0(struct sipe_buddy, 1);
115 buddy->name = normalized_uri;
116 g_hash_table_insert(sipe_private->buddies->uri,
117 buddy->name,
118 buddy);
120 sipe_buddy_add_keys(sipe_private,
121 buddy,
122 exchange_key,
123 change_key);
125 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri);
127 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
128 buddy->just_added = TRUE;
129 sipe_subscribe_presence_single_cb(sipe_private,
130 buddy->name);
133 buddy_fetch_photo(sipe_private, normalized_uri);
135 normalized_uri = NULL; /* buddy takes ownership */
136 } else {
137 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", normalized_uri);
138 buddy->is_obsolete = FALSE;
140 g_free(normalized_uri);
142 return(buddy);
145 static gboolean is_buddy_in_group(struct sipe_buddy *buddy,
146 const gchar *name)
148 if (buddy) {
149 GSList *entry = buddy->groups;
151 while (entry) {
152 struct buddy_group_data *bgd = entry->data;
153 if (sipe_strequal(bgd->group->name, name)) {
154 bgd->is_obsolete = FALSE;
155 return(TRUE);
157 entry = entry->next;
161 return(FALSE);
164 void sipe_buddy_add_to_group(struct sipe_core_private *sipe_private,
165 struct sipe_buddy *buddy,
166 struct sipe_group *group,
167 const gchar *alias)
169 const gchar *uri = buddy->name;
170 const gchar *group_name = group->name;
171 sipe_backend_buddy bb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
172 uri,
173 group_name);
175 if (!bb) {
176 bb = sipe_backend_buddy_add(SIPE_CORE_PUBLIC,
177 uri,
178 alias,
179 group_name);
180 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: created backend buddy '%s' with alias '%s'",
181 uri, alias ? alias : "<NONE>");
185 if (!is_empty(alias)) {
186 gchar *old_alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC,
187 bb);
189 if (sipe_strcase_equal(sipe_get_no_sip_uri(uri),
190 old_alias)) {
191 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC,
193 alias);
194 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: replaced alias for buddy '%s': old '%s' new '%s'",
195 uri, old_alias, alias);
197 g_free(old_alias);
200 if (!is_buddy_in_group(buddy, group_name)) {
201 sipe_buddy_insert_group(buddy, group);
202 SIPE_DEBUG_INFO("sipe_buddy_add_to_group: added buddy %s to group %s",
203 uri, group_name);
207 static gint buddy_group_compare(gconstpointer a, gconstpointer b)
209 return(((const struct buddy_group_data *)a)->group->id -
210 ((const struct buddy_group_data *)b)->group->id);
213 void sipe_buddy_insert_group(struct sipe_buddy *buddy,
214 struct sipe_group *group)
216 struct buddy_group_data *bgd = g_new0(struct buddy_group_data, 1);
218 bgd->group = group;
220 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
221 bgd,
222 buddy_group_compare,
223 NULL);
226 static void buddy_group_free(gpointer data)
228 g_free(data);
231 static void buddy_group_remove(struct sipe_buddy *buddy,
232 struct buddy_group_data *bgd)
234 buddy->groups = g_slist_remove(buddy->groups, bgd);
235 buddy_group_free(bgd);
238 static void sipe_buddy_remove_group(struct sipe_buddy *buddy,
239 const struct sipe_group *group)
241 GSList *entry = buddy->groups;
242 struct buddy_group_data *bgd = NULL;
244 while (entry) {
245 bgd = entry->data;
246 if (bgd->group == group)
247 break;
248 entry = entry->next;
251 buddy_group_remove(buddy, bgd);
254 void sipe_buddy_update_groups(struct sipe_core_private *sipe_private,
255 struct sipe_buddy *buddy,
256 GSList *new_groups)
258 const gchar *uri = buddy->name;
259 GSList *entry = buddy->groups;
261 while (entry) {
262 struct buddy_group_data *bgd = entry->data;
263 const struct sipe_group *group = bgd->group;
265 /* next buddy group */
266 entry = entry->next;
268 /* old group NOT found in new list? */
269 if (g_slist_find(new_groups, group) == NULL) {
270 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
271 uri,
272 group->name);
273 SIPE_DEBUG_INFO("sipe_buddy_update_groups: removing buddy %s from group '%s'",
274 uri, group->name);
275 /* this should never be NULL */
276 if (oldb)
277 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
278 oldb);
279 buddy_group_remove(buddy, bgd);
284 gchar *sipe_buddy_groups_string(struct sipe_buddy *buddy)
286 guint i = 0;
287 gchar *string;
288 /* creating array from GList, converting guint to gchar * */
289 gchar **ids_arr = g_new(gchar *, g_slist_length(buddy->groups) + 1);
290 GSList *entry = buddy->groups;
292 if (!ids_arr)
293 return(NULL);
295 while (entry) {
296 const struct sipe_group *group = ((struct buddy_group_data *) entry->data)->group;
297 ids_arr[i] = g_strdup_printf("%u", group->id);
298 entry = entry->next;
299 i++;
301 ids_arr[i] = NULL;
303 string = g_strjoinv(" ", ids_arr);
304 g_strfreev(ids_arr);
306 return(string);
309 void sipe_buddy_cleanup_local_list(struct sipe_core_private *sipe_private)
311 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
312 NULL,
313 NULL);
314 GSList *entry = buddies;
316 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: overall %d backend buddies (including clones)",
317 g_slist_length(buddies));
318 SIPE_DEBUG_INFO("sipe_buddy_cleanup_local_list: %d sipe buddies (unique)",
319 sipe_buddy_count(sipe_private));
320 while (entry) {
321 sipe_backend_buddy bb = entry->data;
322 gchar *bname = sipe_backend_buddy_get_name(SIPE_CORE_PUBLIC,
323 bb);
324 gchar *gname = sipe_backend_buddy_get_group_name(SIPE_CORE_PUBLIC,
325 bb);
326 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
327 bname);
329 if (!is_buddy_in_group(buddy, gname)) {
330 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",
331 bname, gname);
332 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, bb);
335 g_free(gname);
336 g_free(bname);
338 entry = entry->next;
341 g_slist_free(buddies);
344 struct sipe_buddy *sipe_buddy_find_by_uri(struct sipe_core_private *sipe_private,
345 const gchar *uri)
347 return(g_hash_table_lookup(sipe_private->buddies->uri, uri));
350 struct sipe_buddy *sipe_buddy_find_by_exchange_key(struct sipe_core_private *sipe_private,
351 const gchar *exchange_key)
353 return(g_hash_table_lookup(sipe_private->buddies->exchange_key,
354 exchange_key));
357 void sipe_buddy_foreach(struct sipe_core_private *sipe_private,
358 GHFunc callback,
359 gpointer callback_data)
361 g_hash_table_foreach(sipe_private->buddies->uri,
362 callback,
363 callback_data);
366 static void buddy_free(struct sipe_buddy *buddy)
368 #ifndef _WIN32
370 * We are calling g_hash_table_foreach_steal(). That means that no
371 * key/value deallocation functions are called. Therefore the glib
372 * hash code does not touch the key (buddy->name) or value (buddy)
373 * of the to-be-deleted hash node at all. It follows that we
375 * - MUST free the memory for the key ourselves and
376 * - ARE allowed to do it in this function
378 * Conclusion: glib must be broken on the Windows platform if sipe
379 * crashes with SIGTRAP when closing. You'll have to live
380 * with the memory leak until this is fixed.
382 g_free(buddy->name);
383 #endif
384 g_free(buddy->exchange_key);
385 g_free(buddy->change_key);
386 g_free(buddy->activity);
387 g_free(buddy->meeting_subject);
388 g_free(buddy->meeting_location);
389 g_free(buddy->note);
391 g_free(buddy->cal_start_time);
392 g_free(buddy->cal_free_busy_base64);
393 g_free(buddy->cal_free_busy);
394 g_free(buddy->last_non_cal_activity);
396 sipe_cal_free_working_hours(buddy->cal_working_hours);
398 g_free(buddy->device_name);
399 sipe_utils_slist_free_full(buddy->groups, buddy_group_free);
400 g_free(buddy);
403 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
404 gpointer buddy,
405 SIPE_UNUSED_PARAMETER gpointer user_data)
407 buddy_free(buddy);
408 /* We must return TRUE as the key/value have already been deleted */
409 return(TRUE);
412 void sipe_buddy_free(struct sipe_core_private *sipe_private)
414 struct sipe_buddies *buddies = sipe_private->buddies;
416 g_hash_table_foreach_steal(buddies->uri,
417 buddy_free_cb,
418 NULL);
420 /* core is being deallocated, remove all its pending photo requests */
421 while (buddies->pending_photo_requests) {
422 struct photo_response_data *data =
423 buddies->pending_photo_requests->data;
424 buddies->pending_photo_requests =
425 g_slist_remove(buddies->pending_photo_requests, data);
426 photo_response_data_free(data);
429 g_hash_table_destroy(buddies->uri);
430 g_hash_table_destroy(buddies->exchange_key);
431 g_free(buddies);
432 sipe_private->buddies = NULL;
435 static void buddy_set_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
436 gpointer value,
437 SIPE_UNUSED_PARAMETER gpointer user_data)
439 struct sipe_buddy *buddy = value;
440 GSList *entry = buddy->groups;
442 buddy->is_obsolete = TRUE;
443 while (entry) {
444 ((struct buddy_group_data *) entry->data)->is_obsolete = TRUE;
445 entry = entry->next;
449 void sipe_buddy_update_start(struct sipe_core_private *sipe_private)
451 g_hash_table_foreach(sipe_private->buddies->uri,
452 buddy_set_obsolete_flag,
453 NULL);
456 static gboolean buddy_check_obsolete_flag(SIPE_UNUSED_PARAMETER gpointer key,
457 gpointer value,
458 gpointer user_data)
460 struct sipe_core_private *sipe_private = user_data;
461 struct sipe_buddy *buddy = value;
462 const gchar *uri = buddy->name;
464 if (buddy->is_obsolete) {
465 /* all backend buddies in different groups */
466 GSList *buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC,
467 uri,
468 NULL);
469 GSList *entry = buddies;
471 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: REMOVING %d backend buddies for '%s'",
472 g_slist_length(buddies),
473 uri);
475 while (entry) {
476 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
477 entry->data);
478 entry = entry->next;
480 g_slist_free(buddies);
482 buddy_free(buddy);
483 /* return TRUE as the key/value have already been deleted */
484 return(TRUE);
486 } else {
487 GSList *entry = buddy->groups;
489 while (entry) {
490 struct buddy_group_data *bgd = entry->data;
492 /* next buddy group */
493 entry = entry->next;
495 if (bgd->is_obsolete) {
496 const struct sipe_group *group = bgd->group;
497 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
498 uri,
499 group->name);
500 SIPE_DEBUG_INFO("buddy_check_obsolete_flag: removing buddy '%s' from group '%s'",
501 uri, group->name);
502 /* this should never be NULL */
503 if (oldb)
504 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC,
505 oldb);
506 buddy_group_remove(buddy, bgd);
509 return(FALSE);
513 void sipe_buddy_update_finish(struct sipe_core_private *sipe_private)
515 g_hash_table_foreach_remove(sipe_private->buddies->uri,
516 buddy_check_obsolete_flag,
517 sipe_private);
520 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
521 const gchar *uri,
522 guint activity,
523 const gchar *status_text)
525 struct sipe_buddy *sbuddy;
526 GString *status;
528 if (!sipe_public) return NULL; /* happens on pidgin exit */
530 sbuddy = sipe_buddy_find_by_uri(SIPE_CORE_PRIVATE, uri);
531 if (!sbuddy) return NULL;
533 status = g_string_new(sbuddy->activity ? sbuddy->activity :
534 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
535 status_text : NULL);
537 if (sbuddy->is_mobile) {
538 if (status->len)
539 g_string_append(status, " - ");
540 g_string_append(status, _("Mobile"));
543 if (sbuddy->note) {
544 if (status->len)
545 g_string_append(status, " - ");
546 g_string_append(status, sbuddy->note);
549 /* return NULL instead of empty status text */
550 return(g_string_free(status, status->len ? FALSE : TRUE));
553 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
554 const gchar *with)
556 sipe_backend_buddy pbuddy;
557 gchar *alias = NULL;
558 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
559 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
561 return alias;
564 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
565 const gchar *who,
566 const gchar *old_group_name,
567 const gchar *new_group_name)
569 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
570 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
571 who);
572 struct sipe_group *old_group = NULL;
573 struct sipe_group *new_group;
574 struct sipe_ucs_transaction *ucs_trans = NULL;
576 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' old group '%s' new group '%s'",
577 who ? who : "",
578 old_group_name ? old_group_name : "<UNDEFINED>",
579 new_group_name ? new_group_name : "<UNDEFINED>");
581 if (!buddy)
582 /* buddy not in roaming list */
583 return;
585 old_group = sipe_group_find_by_name(sipe_private, old_group_name);
586 if (old_group) {
587 sipe_buddy_remove_group(buddy, old_group);
588 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' removed from old group '%s'",
589 who, old_group_name);
592 new_group = sipe_group_find_by_name(sipe_private, new_group_name);
593 if (new_group) {
594 sipe_buddy_insert_group(buddy, new_group);
595 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy '%s' added to new group '%s'",
596 who, new_group_name);
599 if (sipe_ucs_is_migrated(sipe_private)) {
601 /* UCS handling */
602 ucs_trans = sipe_ucs_transaction(sipe_private);
604 if (new_group) {
606 * 1. new buddy added to existing group
607 * 2. existing buddy moved from old to existing group
609 sipe_ucs_group_add_buddy(sipe_private,
610 ucs_trans,
611 new_group,
612 buddy,
613 buddy->name);
614 if (old_group)
615 sipe_ucs_group_remove_buddy(sipe_private,
616 ucs_trans,
617 old_group,
618 buddy);
620 } else if (old_group) {
622 * 3. existing buddy removed from one of its groups
623 * 4. existing buddy removed from last group
625 sipe_ucs_group_remove_buddy(sipe_private,
626 ucs_trans,
627 old_group,
628 buddy);
629 if (g_slist_length(buddy->groups) < 1)
630 sipe_buddy_remove(sipe_private,
631 buddy);
632 /* buddy no longer valid */
635 /* non-UCS handling */
636 } else if (new_group)
637 sipe_group_update_buddy(sipe_private, buddy);
639 /* 5. buddy added to new group */
640 if (!new_group)
641 sipe_group_create(sipe_private,
642 ucs_trans,
643 new_group_name,
644 who);
647 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
648 const gchar *uri,
649 const gchar *group_name)
651 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
653 if (!sipe_buddy_find_by_uri(sipe_private, uri))
654 sipe_buddy_add(sipe_private,
655 uri,
656 NULL,
657 NULL);
658 else
659 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
660 uri);
662 sipe_core_buddy_group(sipe_public,
663 uri,
664 NULL,
665 group_name);
668 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
669 struct sipe_buddy *buddy)
671 struct sipe_buddies *buddies = sipe_private->buddies;
672 const gchar *uri = buddy->name;
673 GSList *entry = buddy->groups;
674 gchar *action_name = sipe_utils_presence_key(uri);
676 sipe_schedule_cancel(sipe_private, action_name);
677 g_free(action_name);
679 /* If the buddy still has groups, we need to delete backend buddies */
680 while (entry) {
681 const struct sipe_group *group = ((struct buddy_group_data *) entry->data)->group;
682 sipe_backend_buddy oldb = sipe_backend_buddy_find(SIPE_CORE_PUBLIC,
683 uri,
684 group->name);
685 /* this should never be NULL */
686 if (oldb)
687 sipe_backend_buddy_remove(SIPE_CORE_PUBLIC, oldb);
689 entry = entry->next;
692 g_hash_table_remove(buddies->uri, uri);
693 if (buddy->exchange_key)
694 g_hash_table_remove(buddies->exchange_key,
695 buddy->exchange_key);
697 buddy_free(buddy);
701 * Unassociates buddy from group first.
702 * Then see if no groups left, removes buddy completely.
703 * Otherwise updates buddy groups on server.
705 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
706 const gchar *uri,
707 const gchar *group_name)
709 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
710 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
711 uri);
712 struct sipe_group *group = NULL;
714 if (!buddy) return;
716 if (group_name) {
717 group = sipe_group_find_by_name(sipe_private, group_name);
718 if (group) {
719 sipe_buddy_remove_group(buddy, group);
720 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy '%s' removed from group '%s'",
721 uri, group->name);
725 if (g_slist_length(buddy->groups) < 1) {
727 if (sipe_ucs_is_migrated(sipe_private)) {
728 sipe_ucs_group_remove_buddy(sipe_private,
729 NULL,
730 group,
731 buddy);
732 } else {
733 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
734 buddy->name);
735 sip_soap_request(sipe_private,
736 "deleteContact",
737 request);
738 g_free(request);
741 sipe_buddy_remove(sipe_private, buddy);
742 } else {
743 if (sipe_ucs_is_migrated(sipe_private)) {
744 sipe_ucs_group_remove_buddy(sipe_private,
745 NULL,
746 group,
747 buddy);
748 } else
749 /* updates groups on server */
750 sipe_group_update_buddy(sipe_private, buddy);
754 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
755 const gchar *uri,
756 guint activity)
758 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
759 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
760 uri);
762 if (!sbuddy) return;
764 /* Check if on 2005 system contact's calendar,
765 * then set/preserve it.
767 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
768 sipe_backend_buddy_set_status(sipe_public, uri, activity);
769 } else {
770 sipe_ocs2005_apply_calendar_status(sipe_private,
771 sbuddy,
772 sipe_status_activity_to_token(activity));
776 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
777 const gchar *uri,
778 const gchar *status_name,
779 gboolean is_online,
780 struct sipe_backend_buddy_tooltip *tooltip)
782 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
783 gchar *note = NULL;
784 gboolean is_oof_note = FALSE;
785 const gchar *activity = NULL;
786 gchar *calendar = NULL;
787 const gchar *meeting_subject = NULL;
788 const gchar *meeting_location = NULL;
789 gchar *access_text = NULL;
791 #define SIPE_ADD_BUDDY_INFO(l, t) \
793 gchar *tmp = g_markup_escape_text((t), -1); \
794 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
795 g_free(tmp); \
797 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
798 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
800 if (sipe_public) { /* happens on pidgin exit */
801 struct sipe_buddy *sbuddy = sipe_buddy_find_by_uri(sipe_private,
802 uri);
803 if (sbuddy) {
804 note = sbuddy->note;
805 is_oof_note = sbuddy->is_oof_note;
806 activity = sbuddy->activity;
807 calendar = sipe_cal_get_description(sbuddy);
808 meeting_subject = sbuddy->meeting_subject;
809 meeting_location = sbuddy->meeting_location;
811 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
812 gboolean is_group_access = FALSE;
813 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
814 "user",
815 sipe_get_no_sip_uri(uri),
816 &is_group_access);
817 const char *access_level = sipe_ocs2007_access_level_name(container_id);
818 access_text = is_group_access ?
819 g_strdup(access_level) :
820 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
821 access_level);
825 if (is_online) {
826 const gchar *status_str = activity ? activity : status_name;
828 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
830 if (is_online && !is_empty(calendar)) {
831 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
833 g_free(calendar);
834 if (!is_empty(meeting_location)) {
835 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
836 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
838 if (!is_empty(meeting_subject)) {
839 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
840 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
842 if (note) {
843 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
844 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
845 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
846 note_italics);
847 g_free(note_italics);
849 if (access_text) {
850 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
851 g_free(access_text);
855 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
856 const char *uri,
857 sipe_buddy_info_fields propkey,
858 char *property_value)
860 GSList *buddies, *entry;
862 if (property_value)
863 property_value = g_strstrip(property_value);
865 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
866 while (entry) {
867 gchar *prop_str;
868 sipe_backend_buddy p_buddy = entry->data;
870 /* for Display Name */
871 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
872 gchar *alias;
873 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
874 if (property_value && sipe_is_bad_alias(uri, alias)) {
875 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
876 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
878 g_free(alias);
880 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
881 if (!is_empty(property_value) &&
882 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
884 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
885 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
887 g_free(alias);
889 /* for other properties */
890 else {
891 if (!is_empty(property_value)) {
892 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
893 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
894 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
896 g_free(prop_str);
900 entry = entry->next;
902 g_slist_free(buddies);
906 struct ms_dlx_data;
907 struct ms_dlx_data {
908 GSList *search_rows;
909 gchar *other;
910 guint max_returns;
911 sipe_svc_callback *callback;
912 struct sipe_svc_session *session;
913 gchar *wsse_security;
914 struct sipe_backend_search_token *token;
915 /* must call ms_dlx_free() */
916 void (*failed_callback)(struct sipe_core_private *sipe_private,
917 struct ms_dlx_data *mdd);
920 static void free_search_rows(GSList *search_rows)
922 sipe_utils_slist_free_full(search_rows, g_free);
925 static void ms_dlx_free(struct ms_dlx_data *mdd)
927 free_search_rows(mdd->search_rows);
928 sipe_svc_session_close(mdd->session);
929 g_free(mdd->other);
930 g_free(mdd->wsse_security);
931 g_free(mdd);
934 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
935 #define DLX_SEARCH_ITEM \
936 "<AbEntryRequest.ChangeSearchQuery>" \
937 " <SearchOn>%s</SearchOn>" \
938 " <Value>%s</Value>" \
939 "</AbEntryRequest.ChangeSearchQuery>"
941 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
942 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
943 guint i = 0;
944 gchar *query = NULL;
946 while (query_rows) {
947 gchar *attr;
948 gchar *value;
949 gchar *tmp = NULL;
951 attr = query_rows->data;
952 query_rows = g_slist_next(query_rows);
953 value = query_rows->data;
954 query_rows = g_slist_next(query_rows);
956 if (!value)
957 break;
960 * Special value for SIP ID
962 * Active Directory seems only to be able to search for
963 * SIP URIs. Make sure search string starts with "sip:".
965 if (!attr) {
966 attr = "msRTCSIP-PrimaryUserAddress";
967 if (!use_dlx)
968 value = tmp = sip_uri(value);
971 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
972 attr, value);
973 g_free(tmp);
975 attrs[i] = NULL;
977 if (i) {
978 query = g_strjoinv(NULL, attrs);
979 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
980 query ? query : "");
983 g_strfreev(attrs);
985 return query;
988 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
989 const gchar *base_uri,
990 const gchar *auth_uri,
991 const gchar *wsse_security,
992 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
993 gpointer callback_data)
995 struct ms_dlx_data *mdd = callback_data;
997 if (wsse_security) {
998 guint length = g_slist_length(mdd->search_rows);
999 gchar *search;
1001 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
1002 base_uri);
1004 if (length > 0) {
1005 /* complex search */
1006 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
1007 search = g_strdup_printf("<ChangeSearch xmlns:q1=\"DistributionListExpander\" soapenc:arrayType=\"q1:AbEntryRequest.ChangeSearchQuery[%d]\">"
1008 " %s"
1009 "</ChangeSearch>",
1010 length / 2,
1011 query);
1012 g_free(query);
1013 } else {
1014 /* simple search */
1015 search = g_strdup_printf("<BasicSearch>"
1016 " <SearchList>c,company,displayName,givenName,mail,mailNickname,msRTCSIP-PrimaryUserAddress,sn</SearchList>"
1017 " <Value>%s</Value>"
1018 " <Verb>BeginsWith</Verb>"
1019 "</BasicSearch>",
1020 mdd->other);
1023 if (sipe_svc_ab_entry_request(sipe_private,
1024 mdd->session,
1025 auth_uri,
1026 wsse_security,
1027 search,
1028 mdd->max_returns,
1029 mdd->callback,
1030 mdd)) {
1032 /* keep webticket security token for potential further use */
1033 g_free(mdd->wsse_security);
1034 mdd->wsse_security = g_strdup(wsse_security);
1036 /* callback data passed down the line */
1037 mdd = NULL;
1039 g_free(search);
1041 } else {
1042 /* no ticket: this will show the minmum information */
1043 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
1044 base_uri);
1047 if (mdd)
1048 mdd->failed_callback(sipe_private, mdd);
1051 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
1052 struct ms_dlx_data *mdd)
1054 if (!sipe_webticket_request(sipe_private,
1055 mdd->session,
1056 sipe_private->dlx_uri,
1057 "AddressBookWebTicketBearer",
1058 ms_dlx_webticket,
1059 mdd)) {
1060 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
1061 sipe_private->dlx_uri);
1062 mdd->failed_callback(sipe_private, mdd);
1066 void sipe_buddy_search_contacts_finalize(struct sipe_core_private *sipe_private,
1067 struct sipe_backend_search_results *results,
1068 guint match_count,
1069 gboolean more)
1071 gchar *secondary = g_strdup_printf(
1072 dngettext(PACKAGE_NAME,
1073 "Found %d contact%s:",
1074 "Found %d contacts%s:", match_count),
1075 match_count, more ? _(" (more matched your query)") : "");
1077 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
1078 results,
1079 secondary,
1080 more);
1081 g_free(secondary);
1084 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
1085 const gchar *uri,
1086 SIPE_UNUSED_PARAMETER const gchar *raw,
1087 sipe_xml *soap_body,
1088 gpointer callback_data)
1090 struct ms_dlx_data *mdd = callback_data;
1092 if (soap_body) {
1093 const sipe_xml *node;
1094 struct sipe_backend_search_results *results;
1095 GHashTable *found;
1097 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
1098 uri);
1100 /* any matches? */
1101 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
1102 if (!node) {
1103 /* try again with simple search, if possible */
1104 if (mdd->other && mdd->search_rows) {
1105 SIPE_DEBUG_INFO_NOFORMAT("search_ab_entry_response: no matches, retrying with simple search");
1107 /* throw away original search query */
1108 free_search_rows(mdd->search_rows);
1109 mdd->search_rows = NULL;
1111 ms_dlx_webticket_request(sipe_private, mdd);
1113 /* callback data passed down the line */
1114 return;
1116 } else {
1117 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
1119 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1120 mdd->token,
1121 _("No contacts found"));
1122 ms_dlx_free(mdd);
1123 return;
1127 /* OK, we found something - show the results to the user */
1128 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
1129 mdd->token);
1130 if (!results) {
1131 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
1132 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1133 mdd->token,
1134 _("Unable to display the search results"));
1135 ms_dlx_free(mdd);
1136 return;
1139 /* SearchAbEntryResult can contain duplicates */
1140 found = g_hash_table_new_full(g_str_hash, g_str_equal,
1141 g_free, NULL);
1143 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
1144 const sipe_xml *attrs;
1145 gchar *sip_uri = NULL;
1146 gchar *displayname = NULL;
1147 gchar *company = NULL;
1148 gchar *country = NULL;
1149 gchar *email = NULL;
1151 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
1152 attrs;
1153 attrs = sipe_xml_twin(attrs)) {
1154 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
1155 "Name"));
1156 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
1157 "Value"));
1159 if (!is_empty(value)) {
1160 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
1161 g_free(sip_uri);
1162 sip_uri = value;
1163 value = NULL;
1164 } else if (sipe_strcase_equal(name, "displayname")) {
1165 g_free(displayname);
1166 displayname = value;
1167 value = NULL;
1168 } else if (sipe_strcase_equal(name, "mail")) {
1169 g_free(email);
1170 email = value;
1171 value = NULL;
1172 } else if (sipe_strcase_equal(name, "company")) {
1173 g_free(company);
1174 company = value;
1175 value = NULL;
1176 } else if (sipe_strcase_equal(name, "country")) {
1177 g_free(country);
1178 country = value;
1179 value = NULL;
1183 g_free(value);
1184 g_free(name);
1187 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
1188 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
1189 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
1190 results,
1191 uri_parts[1],
1192 displayname,
1193 company,
1194 country,
1195 email);
1196 g_strfreev(uri_parts);
1198 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
1199 sip_uri = NULL;
1202 g_free(email);
1203 g_free(country);
1204 g_free(company);
1205 g_free(displayname);
1206 g_free(sip_uri);
1209 sipe_buddy_search_contacts_finalize(sipe_private, results,
1210 g_hash_table_size(found),
1211 FALSE);
1212 g_hash_table_destroy(found);
1213 ms_dlx_free(mdd);
1215 } else {
1216 mdd->failed_callback(sipe_private, mdd);
1220 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
1221 struct sipmsg *msg,
1222 struct transaction *trans)
1224 struct sipe_backend_search_token *token = trans->payload->data;
1225 struct sipe_backend_search_results *results;
1226 sipe_xml *searchResults;
1227 const sipe_xml *mrow;
1228 guint match_count = 0;
1229 gboolean more = FALSE;
1231 /* valid response? */
1232 if (msg->response != 200) {
1233 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
1234 msg->response);
1235 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1236 token,
1237 _("Contact search failed"));
1238 return(FALSE);
1241 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
1243 /* valid XML? */
1244 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1245 if (!searchResults) {
1246 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
1247 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1248 token,
1249 _("Contact search failed"));
1250 return(FALSE);
1253 /* any matches? */
1254 mrow = sipe_xml_child(searchResults, "Body/Array/row");
1255 if (!mrow) {
1256 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
1257 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1258 token,
1259 _("No contacts found"));
1261 sipe_xml_free(searchResults);
1262 return(FALSE);
1265 /* OK, we found something - show the results to the user */
1266 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
1267 trans->payload->data);
1268 if (!results) {
1269 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
1270 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
1271 token,
1272 _("Unable to display the search results"));
1274 sipe_xml_free(searchResults);
1275 return FALSE;
1278 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
1279 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
1280 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
1281 results,
1282 uri_parts[1],
1283 sipe_xml_attribute(mrow, "displayName"),
1284 sipe_xml_attribute(mrow, "company"),
1285 sipe_xml_attribute(mrow, "country"),
1286 sipe_xml_attribute(mrow, "email"));
1287 g_strfreev(uri_parts);
1288 match_count++;
1291 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
1292 char *data = sipe_xml_data(mrow);
1293 more = (g_ascii_strcasecmp(data, "true") == 0);
1294 g_free(data);
1297 sipe_buddy_search_contacts_finalize(sipe_private, results, match_count, more);
1298 sipe_xml_free(searchResults);
1300 return(TRUE);
1303 static void search_soap_request(struct sipe_core_private *sipe_private,
1304 GDestroyNotify destroy,
1305 void *data,
1306 guint max,
1307 SoapTransCallback callback,
1308 GSList *search_rows)
1310 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
1311 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1313 payload->destroy = destroy;
1314 payload->data = data;
1316 sip_soap_directory_search(sipe_private,
1317 max,
1318 query,
1319 callback,
1320 payload);
1321 g_free(query);
1324 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
1325 struct ms_dlx_data *mdd)
1327 /* error using [MS-DLX] server, retry using Active Directory */
1328 if (mdd->search_rows)
1329 search_soap_request(sipe_private,
1330 NULL,
1331 mdd->token,
1332 100,
1333 process_search_contact_response,
1334 mdd->search_rows);
1335 ms_dlx_free(mdd);
1338 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
1339 struct sipe_backend_search_token *token,
1340 const gchar *given_name,
1341 const gchar *surname,
1342 const gchar *email,
1343 const gchar *sipid,
1344 const gchar *company,
1345 const gchar *country)
1347 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1349 /* Lync 2013 or newer: use UCS if contacts are migrated */
1350 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) &&
1351 sipe_ucs_is_migrated(sipe_private)) {
1353 sipe_ucs_search(sipe_private,
1354 token,
1355 given_name,
1356 surname,
1357 email,
1358 sipid,
1359 company,
1360 country);
1362 } else {
1363 GSList *query_rows = NULL;
1364 guint count = 0;
1365 const gchar *simple = NULL;
1367 #define ADD_QUERY_ROW(attr, val) \
1368 if (val) { \
1369 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
1370 query_rows = g_slist_append(query_rows, g_strdup(val)); \
1371 simple = val; \
1372 count++; \
1375 ADD_QUERY_ROW("givenName", given_name);
1376 ADD_QUERY_ROW("sn", surname);
1377 ADD_QUERY_ROW("mail", email);
1378 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1379 ADD_QUERY_ROW(NULL, sipid);
1380 ADD_QUERY_ROW("company", company);
1381 ADD_QUERY_ROW("c", country);
1383 if (query_rows) {
1384 if (sipe_private->dlx_uri != NULL) {
1385 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1387 mdd->search_rows = query_rows;
1388 /* user entered only one search string, remember that one */
1389 if (count == 1)
1390 mdd->other = g_strdup(simple);
1391 mdd->max_returns = 100;
1392 mdd->callback = search_ab_entry_response;
1393 mdd->failed_callback = search_ab_entry_failed;
1394 mdd->session = sipe_svc_session_start();
1395 mdd->token = token;
1397 ms_dlx_webticket_request(sipe_private, mdd);
1399 } else {
1400 /* no [MS-DLX] server, use Active Directory search instead */
1401 search_soap_request(sipe_private,
1402 NULL,
1403 token,
1404 100,
1405 process_search_contact_response,
1406 query_rows);
1407 free_search_rows(query_rows);
1409 } else
1410 sipe_backend_search_failed(sipe_public,
1411 token,
1412 _("Invalid contact search query"));
1416 static void get_info_finalize(struct sipe_core_private *sipe_private,
1417 struct sipe_backend_buddy_info *info,
1418 const gchar *uri,
1419 const gchar *server_alias,
1420 const gchar *email)
1422 sipe_backend_buddy bbuddy;
1423 struct sipe_buddy *sbuddy;
1424 gchar *alias;
1425 gchar *value;
1427 if (!info) {
1428 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1429 } else {
1430 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
1432 if (!info)
1433 return;
1435 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
1437 if (is_empty(server_alias)) {
1438 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
1439 bbuddy);
1440 if (value) {
1441 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1442 info,
1443 SIPE_BUDDY_INFO_DISPLAY_NAME,
1444 value);
1446 } else {
1447 value = g_strdup(server_alias);
1450 /* present alias if it differs from server alias */
1451 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
1452 if (alias && !sipe_strequal(alias, value))
1454 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1455 info,
1456 SIPE_BUDDY_INFO_ALIAS,
1457 alias);
1459 g_free(alias);
1460 g_free(value);
1462 if (is_empty(email)) {
1463 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1464 bbuddy,
1465 SIPE_BUDDY_INFO_EMAIL);
1466 if (value) {
1467 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1468 info,
1469 SIPE_BUDDY_INFO_EMAIL,
1470 value);
1471 g_free(value);
1475 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
1476 bbuddy,
1477 SIPE_BUDDY_INFO_SITE);
1478 if (value) {
1479 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1480 info,
1481 SIPE_BUDDY_INFO_SITE,
1482 value);
1483 g_free(value);
1486 sbuddy = sipe_buddy_find_by_uri(sipe_private, uri);
1487 if (sbuddy && sbuddy->device_name) {
1488 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1489 info,
1490 SIPE_BUDDY_INFO_DEVICE,
1491 sbuddy->device_name);
1494 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
1498 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
1499 const gchar *uri,
1500 SIPE_UNUSED_PARAMETER const gchar *raw,
1501 sipe_xml *soap_body,
1502 gpointer callback_data)
1504 struct ms_dlx_data *mdd = callback_data;
1505 struct sipe_backend_buddy_info *info = NULL;
1506 gchar *server_alias = NULL;
1507 gchar *email = NULL;
1509 if (soap_body) {
1510 const sipe_xml *node;
1512 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
1513 uri);
1515 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1517 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1518 node;
1519 node = sipe_xml_twin(node)) {
1520 gchar *name = sipe_xml_data(sipe_xml_child(node,
1521 "Name"));
1522 gchar *value = sipe_xml_data(sipe_xml_child(node,
1523 "Value"));
1524 const sipe_xml *values = sipe_xml_child(node,
1525 "Values");
1527 /* Single value entries */
1528 if (!is_empty(value)) {
1530 if (sipe_strcase_equal(name, "displayname")) {
1531 g_free(server_alias);
1532 server_alias = value;
1533 value = NULL;
1534 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1535 info,
1536 SIPE_BUDDY_INFO_DISPLAY_NAME,
1537 server_alias);
1538 } else if (sipe_strcase_equal(name, "mail")) {
1539 g_free(email);
1540 email = value;
1541 value = NULL;
1542 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1543 info,
1544 SIPE_BUDDY_INFO_EMAIL,
1545 email);
1546 } else if (sipe_strcase_equal(name, "title")) {
1547 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1548 info,
1549 SIPE_BUDDY_INFO_JOB_TITLE,
1550 value);
1551 } else if (sipe_strcase_equal(name, "company")) {
1552 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1553 info,
1554 SIPE_BUDDY_INFO_COMPANY,
1555 value);
1556 } else if (sipe_strcase_equal(name, "country")) {
1557 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1558 info,
1559 SIPE_BUDDY_INFO_COUNTRY,
1560 value);
1563 } else if (values) {
1564 gchar *first = sipe_xml_data(sipe_xml_child(values,
1565 "string"));
1567 if (sipe_strcase_equal(name, "telephonenumber")) {
1568 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1569 info,
1570 SIPE_BUDDY_INFO_WORK_PHONE,
1571 first);
1574 g_free(first);
1577 g_free(value);
1578 g_free(name);
1582 /* this will show the minmum information */
1583 get_info_finalize(sipe_private,
1584 info,
1585 mdd->other,
1586 server_alias,
1587 email);
1589 g_free(email);
1590 g_free(server_alias);
1591 ms_dlx_free(mdd);
1594 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1595 struct sipmsg *msg,
1596 struct transaction *trans)
1598 const gchar *uri = trans->payload->data;
1599 struct sipe_backend_buddy_info *info = NULL;
1600 gchar *server_alias = NULL;
1601 gchar *email = NULL;
1603 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1604 uri, sipe_private->username);
1606 if (msg->response != 200) {
1607 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1608 } else {
1609 sipe_xml *searchResults;
1610 const sipe_xml *mrow;
1612 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1613 msg->body ? msg->body : "");
1615 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1616 if (!searchResults) {
1618 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1620 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1621 const gchar *value;
1622 gchar *phone_number;
1624 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1626 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1627 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1628 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1631 * For 2007 system we will take this from ContactCard -
1632 * it has cleaner tel: URIs at least
1634 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1635 char *tel_uri = sip_to_tel_uri(phone_number);
1636 /* trims its parameters, so call first */
1637 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1638 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1639 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1640 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1641 g_free(tel_uri);
1643 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1644 uri);
1647 if (!is_empty(server_alias)) {
1648 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1649 info,
1650 SIPE_BUDDY_INFO_DISPLAY_NAME,
1651 server_alias);
1653 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1654 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1655 info,
1656 SIPE_BUDDY_INFO_JOB_TITLE,
1657 value);
1659 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1660 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1661 info,
1662 SIPE_BUDDY_INFO_OFFICE,
1663 value);
1665 if (!is_empty(phone_number)) {
1666 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1667 info,
1668 SIPE_BUDDY_INFO_WORK_PHONE,
1669 phone_number);
1671 g_free(phone_number);
1672 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1673 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1674 info,
1675 SIPE_BUDDY_INFO_COMPANY,
1676 value);
1678 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1679 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1680 info,
1681 SIPE_BUDDY_INFO_CITY,
1682 value);
1684 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1685 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1686 info,
1687 SIPE_BUDDY_INFO_STATE,
1688 value);
1690 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1691 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1692 info,
1693 SIPE_BUDDY_INFO_COUNTRY,
1694 value);
1696 if (!is_empty(email)) {
1697 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1698 info,
1699 SIPE_BUDDY_INFO_EMAIL,
1700 email);
1703 sipe_xml_free(searchResults);
1706 /* this will show the minmum information */
1707 get_info_finalize(sipe_private,
1708 info,
1709 uri,
1710 server_alias,
1711 email);
1713 g_free(server_alias);
1714 g_free(email);
1716 return TRUE;
1719 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1720 struct ms_dlx_data *mdd)
1722 /* error using [MS-DLX] server, retry using Active Directory */
1723 search_soap_request(sipe_private,
1724 g_free,
1725 mdd->other,
1727 process_get_info_response,
1728 mdd->search_rows);
1729 mdd->other = NULL;
1730 ms_dlx_free(mdd);
1733 static GSList *search_rows_for_uri(const gchar *uri)
1735 /* prepare_buddy_search_query() interprets NULL as SIP ID */
1736 GSList *l = g_slist_append(NULL, NULL);
1737 return(g_slist_append(l, g_strdup(uri)));
1740 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1741 const gchar *who)
1743 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1744 GSList *search_rows = search_rows_for_uri(who);
1746 if (sipe_private->dlx_uri) {
1747 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1749 mdd->search_rows = search_rows;
1750 mdd->other = g_strdup(who);
1751 mdd->max_returns = 1;
1752 mdd->callback = get_info_ab_entry_response;
1753 mdd->failed_callback = get_info_ab_entry_failed;
1754 mdd->session = sipe_svc_session_start();
1756 ms_dlx_webticket_request(sipe_private, mdd);
1758 } else {
1759 /* no [MS-DLX] server, use Active Directory search instead */
1760 search_soap_request(sipe_private,
1761 g_free,
1762 g_strdup(who),
1764 process_get_info_response,
1765 search_rows);
1766 free_search_rows(search_rows);
1770 static void photo_response_data_free(struct photo_response_data *data)
1772 g_free(data->who);
1773 g_free(data->photo_hash);
1774 if (data->request) {
1775 sipe_http_request_cancel(data->request);
1777 g_free(data);
1780 static void photo_response_data_remove(struct sipe_core_private *sipe_private,
1781 struct photo_response_data *data)
1783 data->request = NULL;
1784 sipe_private->buddies->pending_photo_requests =
1785 g_slist_remove(sipe_private->buddies->pending_photo_requests, data);
1786 photo_response_data_free(data);
1789 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1790 guint status,
1791 GSList *headers,
1792 const char *body,
1793 gpointer data)
1795 struct photo_response_data *rdata = (struct photo_response_data *) data;
1797 if (status == SIPE_HTTP_STATUS_OK) {
1798 const gchar *len_str = sipe_utils_nameval_find(headers,
1799 "Content-Length");
1800 if (len_str) {
1801 gsize photo_size = atoi(len_str);
1802 gpointer photo = g_new(char, photo_size);
1804 if (photo) {
1805 memcpy(photo, body, photo_size);
1807 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1808 rdata->who,
1809 photo,
1810 photo_size,
1811 rdata->photo_hash);
1816 photo_response_data_remove(sipe_private, rdata);
1819 static void process_get_user_photo_response(struct sipe_core_private *sipe_private,
1820 guint status,
1821 SIPE_UNUSED_PARAMETER GSList *headers,
1822 const gchar *body,
1823 gpointer data)
1825 struct photo_response_data *rdata = (struct photo_response_data *) data;
1827 if ((status == SIPE_HTTP_STATUS_OK) && body) {
1828 sipe_xml *xml = sipe_xml_parse(body, strlen(body));
1829 const sipe_xml *node = sipe_xml_child(xml,
1830 "Body/GetUserPhotoResponse/PictureData");
1832 if (node) {
1833 gchar *base64;
1834 gsize photo_size;
1835 guchar *photo;
1837 /* decode photo data */
1838 base64 = sipe_xml_data(node);
1839 photo = g_base64_decode(base64, &photo_size);
1840 g_free(base64);
1842 /* EWS doesn't provide a hash -> calculate SHA-1 digest */
1843 if (!rdata->photo_hash) {
1844 guchar digest[SIPE_DIGEST_SHA1_LENGTH];
1845 sipe_digest_sha1(photo, photo_size, digest);
1847 /* rdata takes ownership of digest string */
1848 rdata->photo_hash = buff_to_hex_str(digest,
1849 SIPE_DIGEST_SHA1_LENGTH);
1852 /* backend frees "photo" */
1853 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1854 rdata->who,
1855 photo,
1856 photo_size,
1857 rdata->photo_hash);
1860 sipe_xml_free(xml);
1863 photo_response_data_remove(sipe_private, rdata);
1866 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1868 gchar *assertion = sipe_xml_extract_raw(wsse_security, "Assertion", TRUE);
1869 gchar *wsse_security_base64;
1870 gchar *x_ms_webticket_header;
1872 if (!assertion) {
1873 return NULL;
1876 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1877 strlen(assertion));
1878 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1879 wsse_security_base64);
1881 g_free(assertion);
1882 g_free(wsse_security_base64);
1884 return x_ms_webticket_header;
1887 /* see also sipe_ucs_http_request() */
1888 static struct sipe_http_request *get_user_photo_request(struct sipe_core_private *sipe_private,
1889 struct photo_response_data *data,
1890 const gchar *ews_url,
1891 const gchar *email)
1893 gchar *soap = g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
1894 "<soap:Envelope"
1895 " xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\""
1896 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
1897 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\""
1898 " >"
1899 " <soap:Header>"
1900 " <t:RequestServerVersion Version=\"Exchange2013\" />"
1901 " </soap:Header>"
1902 " <soap:Body>"
1903 " <m:GetUserPhoto>"
1904 " <m:Email>%s</m:Email>"
1905 " <m:SizeRequested>HR48x48</m:SizeRequested>"
1906 " </m:GetUserPhoto>"
1907 " </soap:Body>"
1908 "</soap:Envelope>",
1909 email);
1910 struct sipe_http_request *request = sipe_http_request_post(sipe_private,
1911 ews_url,
1912 NULL,
1913 soap,
1914 "text/xml; charset=UTF-8",
1915 process_get_user_photo_response,
1916 data);
1917 g_free(soap);
1919 if (request) {
1920 sipe_core_email_authentication(sipe_private,
1921 request);
1922 sipe_http_request_allow_redirect(request);
1923 } else {
1924 SIPE_DEBUG_ERROR_NOFORMAT("get_user_photo_request: failed to create HTTP connection");
1927 return(request);
1930 static void photo_response_data_finalize(struct sipe_core_private *sipe_private,
1931 struct photo_response_data *data,
1932 const gchar *uri,
1933 const gchar *photo_hash)
1935 if (data->request) {
1936 data->who = g_strdup(uri);
1937 data->photo_hash = g_strdup(photo_hash);
1939 sipe_private->buddies->pending_photo_requests =
1940 g_slist_append(sipe_private->buddies->pending_photo_requests, data);
1941 sipe_http_request_ready(data->request);
1942 } else {
1943 photo_response_data_free(data);
1947 void sipe_buddy_update_photo(struct sipe_core_private *sipe_private,
1948 const gchar *uri,
1949 const gchar *photo_hash,
1950 const gchar *photo_url,
1951 const gchar *headers)
1953 const gchar *photo_hash_old =
1954 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, uri);
1956 if (!sipe_strequal(photo_hash, photo_hash_old)) {
1957 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1959 SIPE_DEBUG_INFO("sipe_buddy_update_photo: who '%s' url '%s' hash '%s'",
1960 uri, photo_url, photo_hash);
1962 /* Photo URL is embedded XML? */
1963 if (g_str_has_prefix(photo_url, "<") &&
1964 g_str_has_suffix(photo_url, ">")) {
1965 /* add dummy root to embedded XML string */
1966 gchar *tmp = g_strdup_printf("<r>%s</r>", photo_url);
1967 sipe_xml *xml = sipe_xml_parse(tmp, strlen(tmp));
1968 g_free(tmp);
1970 if (xml) {
1971 gchar *ews_url = sipe_xml_data(sipe_xml_child(xml, "ewsUrl"));
1972 gchar *email = sipe_xml_data(sipe_xml_child(xml, "primarySMTP"));
1974 if (!is_empty(ews_url) && !is_empty(email))
1975 data->request = get_user_photo_request(sipe_private,
1976 data,
1977 ews_url,
1978 email);
1980 g_free(email);
1981 g_free(ews_url);
1982 sipe_xml_free(xml);
1984 } else {
1985 data->request = sipe_http_request_get(sipe_private,
1986 photo_url,
1987 headers,
1988 process_buddy_photo_response,
1989 data);
1992 photo_response_data_finalize(sipe_private,
1993 data,
1994 uri,
1995 photo_hash);
1999 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
2000 const gchar *uri,
2001 SIPE_UNUSED_PARAMETER const gchar *raw,
2002 sipe_xml *soap_body,
2003 gpointer callback_data)
2005 struct ms_dlx_data *mdd = callback_data;
2006 gchar *photo_rel_path = NULL;
2007 gchar *photo_hash = NULL;
2009 if (soap_body) {
2010 const sipe_xml *node;
2012 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
2013 uri);
2015 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
2016 node;
2017 node = sipe_xml_twin(node)) {
2018 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
2019 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
2021 if (!is_empty(value)) {
2022 if (sipe_strcase_equal(name, "PhotoRelPath")) {
2023 g_free(photo_rel_path);
2024 photo_rel_path = value;
2025 value = NULL;
2026 } else if (sipe_strcase_equal(name, "PhotoHash")) {
2027 g_free(photo_hash);
2028 photo_hash = value;
2029 value = NULL;
2033 g_free(value);
2034 g_free(name);
2038 if (sipe_private->addressbook_uri && photo_rel_path && photo_hash) {
2039 gchar *photo_url = g_strdup_printf("%s/%s",
2040 sipe_private->addressbook_uri, photo_rel_path);
2041 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
2043 sipe_buddy_update_photo(sipe_private,
2044 mdd->other,
2045 photo_hash,
2046 photo_url,
2047 x_ms_webticket_header);
2049 g_free(x_ms_webticket_header);
2050 g_free(photo_url);
2053 g_free(photo_rel_path);
2054 g_free(photo_hash);
2055 ms_dlx_free(mdd);
2058 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
2059 struct ms_dlx_data *mdd)
2061 ms_dlx_free(mdd);
2064 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
2065 const gchar *uri)
2067 if (sipe_backend_uses_photo()) {
2069 /* Lync 2013 or newer: use UCS if contacts are migrated */
2070 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013) &&
2071 sipe_ucs_is_migrated(sipe_private)) {
2072 struct photo_response_data *data = g_new(struct photo_response_data, 1);
2074 data->request = get_user_photo_request(sipe_private,
2075 data,
2076 sipe_ucs_ews_url(sipe_private),
2077 sipe_get_no_sip_uri(uri));
2078 photo_response_data_finalize(sipe_private,
2079 data,
2080 uri,
2081 /* there is no hash */
2082 NULL);
2084 /* Lync 2010: use [MS-DLX] */
2085 } else if (sipe_private->dlx_uri &&
2086 sipe_private->addressbook_uri) {
2087 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
2089 mdd->search_rows = search_rows_for_uri(uri);
2090 mdd->other = g_strdup(uri);
2091 mdd->max_returns = 1;
2092 mdd->callback = get_photo_ab_entry_response;
2093 mdd->failed_callback = get_photo_ab_entry_failed;
2094 mdd->session = sipe_svc_session_start();
2096 ms_dlx_webticket_request(sipe_private, mdd);
2101 static void buddy_refresh_photos_cb(gpointer uri,
2102 SIPE_UNUSED_PARAMETER gpointer value,
2103 gpointer sipe_private)
2105 buddy_fetch_photo(sipe_private, uri);
2108 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
2110 g_hash_table_foreach(sipe_private->buddies->uri,
2111 buddy_refresh_photos_cb,
2112 sipe_private);
2115 /* Buddy menu callbacks*/
2117 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
2118 const gchar *who)
2120 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
2122 /* 2007+ conference */
2123 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
2124 sipe_conf_add(sipe_private, who);
2126 /* 2005- multiparty chat */
2127 } else {
2128 gchar *self = sip_uri_self(sipe_private);
2129 struct sip_session *session;
2131 session = sipe_session_add_chat(sipe_private,
2132 NULL,
2133 TRUE,
2134 self);
2135 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
2136 session->chat_session,
2137 session->chat_session->title,
2138 self);
2139 g_free(self);
2141 sipe_im_invite(sipe_private, session, who,
2142 NULL, NULL, NULL, FALSE);
2146 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
2147 const gchar *who)
2149 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
2150 who,
2151 NULL);
2152 gchar *email = sipe_backend_buddy_get_string(sipe_public,
2153 buddy,
2154 SIPE_BUDDY_INFO_EMAIL);
2156 if (email) {
2157 gchar *command_line = g_strdup_printf(
2158 #ifdef _WIN32
2159 "cmd /c start"
2160 #else
2161 "xdg-email"
2162 #endif
2163 " mailto:%s", email);
2164 g_free(email);
2166 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
2167 command_line);
2168 g_spawn_command_line_async(command_line, NULL);
2169 g_free(command_line);
2171 } else {
2172 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
2173 who);
2177 /* Buddy menu */
2179 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
2180 struct sipe_backend_buddy_menu *menu,
2181 sipe_backend_buddy buddy,
2182 sipe_buddy_info_fields id_phone,
2183 sipe_buddy_info_fields id_display,
2184 const gchar *type)
2186 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
2187 buddy,
2188 id_phone);
2189 if (phone) {
2190 gchar *display = sipe_backend_buddy_get_string(sipe_public,
2191 buddy,
2192 id_display);
2193 gchar *tmp = NULL;
2194 gchar *label = g_strdup_printf("%s %s",
2195 type,
2196 display ? display :
2197 (tmp = sip_tel_uri_denormalize(phone)));
2198 menu = sipe_backend_buddy_menu_add(sipe_public,
2199 menu,
2200 label,
2201 SIPE_BUDDY_MENU_MAKE_CALL,
2202 phone);
2203 g_free(tmp);
2204 g_free(label);
2205 g_free(display);
2206 g_free(phone);
2209 return(menu);
2212 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
2213 const gchar *buddy_name,
2214 struct sipe_backend_buddy_menu *menu)
2216 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
2217 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
2218 buddy_name,
2219 NULL);
2220 gchar *self = sip_uri_self(sipe_private);
2222 SIPE_SESSION_FOREACH {
2223 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
2225 struct sipe_chat_session *chat_session = session->chat_session;
2226 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
2228 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
2230 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
2232 if (is_conf &&
2233 /* Not conf OP */
2234 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
2235 /* We are a conf OP */
2236 conf_op) {
2237 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
2238 chat_session->title);
2239 menu = sipe_backend_buddy_menu_add(sipe_public,
2240 menu,
2241 label,
2242 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
2243 chat_session);
2244 g_free(label);
2247 if (is_conf &&
2248 /* We are a conf OP */
2249 conf_op) {
2250 gchar *label = g_strdup_printf(_("Remove from '%s'"),
2251 chat_session->title);
2252 menu = sipe_backend_buddy_menu_add(sipe_public,
2253 menu,
2254 label,
2255 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
2256 chat_session);
2257 g_free(label);
2260 else
2262 if (!is_conf ||
2263 (is_conf && !session->locked)) {
2264 gchar *label = g_strdup_printf(_("Invite to '%s'"),
2265 chat_session->title);
2266 menu = sipe_backend_buddy_menu_add(sipe_public,
2267 menu,
2268 label,
2269 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
2270 chat_session);
2271 g_free(label);
2275 } SIPE_SESSION_FOREACH_END;
2276 g_free(self);
2278 menu = sipe_backend_buddy_menu_add(sipe_public,
2279 menu,
2280 _("New chat"),
2281 SIPE_BUDDY_MENU_NEW_CHAT,
2282 NULL);
2284 /* add buddy's phone numbers if we have call control */
2285 if (sip_csta_is_idle(sipe_private)) {
2287 /* work phone */
2288 menu = buddy_menu_phone(sipe_public,
2289 menu,
2290 buddy,
2291 SIPE_BUDDY_INFO_WORK_PHONE,
2292 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
2293 _("Work"));
2294 /* mobile phone */
2295 menu = buddy_menu_phone(sipe_public,
2296 menu,
2297 buddy,
2298 SIPE_BUDDY_INFO_MOBILE_PHONE,
2299 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
2300 _("Mobile"));
2302 /* home phone */
2303 menu = buddy_menu_phone(sipe_public,
2304 menu,
2305 buddy,
2306 SIPE_BUDDY_INFO_HOME_PHONE,
2307 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
2308 _("Home"));
2310 /* other phone */
2311 menu = buddy_menu_phone(sipe_public,
2312 menu,
2313 buddy,
2314 SIPE_BUDDY_INFO_OTHER_PHONE,
2315 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
2316 _("Other"));
2318 /* custom1 phone */
2319 menu = buddy_menu_phone(sipe_public,
2320 menu,
2321 buddy,
2322 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
2323 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
2324 _("Custom1"));
2328 gchar *email = sipe_backend_buddy_get_string(sipe_public,
2329 buddy,
2330 SIPE_BUDDY_INFO_EMAIL);
2331 if (email) {
2332 menu = sipe_backend_buddy_menu_add(sipe_public,
2333 menu,
2334 _("Send email..."),
2335 SIPE_BUDDY_MENU_SEND_EMAIL,
2336 NULL);
2337 g_free(email);
2341 /* access level control */
2342 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
2343 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
2344 menu,
2345 _("Access level"),
2346 sipe_ocs2007_access_control_menu(sipe_private,
2347 buddy_name));
2349 return(menu);
2352 guint sipe_buddy_count(struct sipe_core_private *sipe_private)
2354 return(g_hash_table_size(sipe_private->buddies->uri));
2357 static guint sipe_ht_hash_nick(const char *nick)
2359 char *lc = g_utf8_strdown(nick, -1);
2360 guint bucket = g_str_hash(lc);
2361 g_free(lc);
2363 return bucket;
2366 static gboolean sipe_ht_equals_nick(const char *nick1, const char *nick2)
2368 char *nick1_norm = NULL;
2369 char *nick2_norm = NULL;
2370 gboolean equal;
2372 if (nick1 == NULL && nick2 == NULL) return TRUE;
2373 if (nick1 == NULL || nick2 == NULL ||
2374 !g_utf8_validate(nick1, -1, NULL) ||
2375 !g_utf8_validate(nick2, -1, NULL)) return FALSE;
2377 nick1_norm = g_utf8_casefold(nick1, -1);
2378 nick2_norm = g_utf8_casefold(nick2, -1);
2379 equal = g_utf8_collate(nick1_norm, nick2_norm) == 0;
2380 g_free(nick2_norm);
2381 g_free(nick1_norm);
2383 return equal;
2386 void sipe_buddy_init(struct sipe_core_private *sipe_private)
2388 struct sipe_buddies *buddies = g_new0(struct sipe_buddies, 1);
2389 buddies->uri = g_hash_table_new((GHashFunc) sipe_ht_hash_nick,
2390 (GEqualFunc) sipe_ht_equals_nick);
2391 buddies->exchange_key = g_hash_table_new(g_str_hash,
2392 g_str_equal);
2393 sipe_private->buddies = buddies;
2397 Local Variables:
2398 mode: c
2399 c-file-style: "bsd"
2400 indent-tabs-mode: t
2401 tab-width: 8
2402 End: