ucs: always initialize on Lync 2013
[siplcs.git] / src / core / sipe-buddy.c
bloba4045d291c35ea061f91dd12257844d4ea8e3445
1 /**
2 * @file sipe-buddy.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
31 #include <glib.h>
33 #include "sipe-common.h"
34 #include "sipmsg.h"
35 #include "sip-csta.h"
36 #include "sip-soap.h"
37 #include "sip-transport.h"
38 #include "sipe-backend.h"
39 #include "sipe-buddy.h"
40 #include "sipe-cal.h"
41 #include "sipe-chat.h"
42 #include "sipe-conf.h"
43 #include "sipe-core.h"
44 #include "sipe-core-private.h"
45 #include "sipe-group.h"
46 #include "sipe-http.h"
47 #include "sipe-im.h"
48 #include "sipe-nls.h"
49 #include "sipe-ocs2005.h"
50 #include "sipe-ocs2007.h"
51 #include "sipe-schedule.h"
52 #include "sipe-session.h"
53 #include "sipe-status.h"
54 #include "sipe-subscriptions.h"
55 #include "sipe-svc.h"
56 #include "sipe-ucs.h"
57 #include "sipe-utils.h"
58 #include "sipe-webticket.h"
59 #include "sipe-xml.h"
61 struct photo_response_data {
62 gchar *who;
63 gchar *photo_hash;
64 struct sipe_http_request *request;
67 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
68 const gchar *uri);
69 static void photo_response_data_free(struct photo_response_data *data);
71 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
72 const gchar *uri)
74 struct sipe_buddy *buddy = g_hash_table_lookup(sipe_private->buddies, uri);
75 if (!buddy) {
76 buddy = g_new0(struct sipe_buddy, 1);
77 buddy->name = g_strdup(uri);
78 g_hash_table_insert(sipe_private->buddies, buddy->name, buddy);
80 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", uri);
82 buddy_fetch_photo(sipe_private, uri);
83 } else {
84 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", uri);
87 return buddy;
90 static void buddy_free(struct sipe_buddy *buddy)
92 #ifndef _WIN32
94 * We are calling g_hash_table_foreach_steal(). That means that no
95 * key/value deallocation functions are called. Therefore the glib
96 * hash code does not touch the key (buddy->name) or value (buddy)
97 * of the to-be-deleted hash node at all. It follows that we
99 * - MUST free the memory for the key ourselves and
100 * - ARE allowed to do it in this function
102 * Conclusion: glib must be broken on the Windows platform if sipe
103 * crashes with SIGTRAP when closing. You'll have to live
104 * with the memory leak until this is fixed.
106 g_free(buddy->name);
107 #endif
108 g_free(buddy->activity);
109 g_free(buddy->meeting_subject);
110 g_free(buddy->meeting_location);
111 g_free(buddy->note);
113 g_free(buddy->cal_start_time);
114 g_free(buddy->cal_free_busy_base64);
115 g_free(buddy->cal_free_busy);
116 g_free(buddy->last_non_cal_activity);
118 sipe_cal_free_working_hours(buddy->cal_working_hours);
120 g_free(buddy->device_name);
121 g_slist_free(buddy->groups);
122 g_free(buddy);
125 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
126 gpointer buddy,
127 SIPE_UNUSED_PARAMETER gpointer user_data)
129 buddy_free(buddy);
130 /* We must return TRUE as the key/value have already been deleted */
131 return(TRUE);
134 void sipe_buddy_free_all(struct sipe_core_private *sipe_private)
136 g_hash_table_foreach_steal(sipe_private->buddies,
137 buddy_free_cb,
138 NULL);
140 /* core is being deallocated, remove all its pending photo requests */
141 while (sipe_private->pending_photo_requests) {
142 struct photo_response_data *data =
143 sipe_private->pending_photo_requests->data;
144 sipe_private->pending_photo_requests =
145 g_slist_remove(sipe_private->pending_photo_requests, data);
146 photo_response_data_free(data);
150 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
151 const gchar *uri,
152 guint activity,
153 const gchar *status_text)
155 struct sipe_buddy *sbuddy;
156 const char *activity_str;
158 if (!sipe_public) return NULL; /* happens on pidgin exit */
160 sbuddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, uri);
161 if (!sbuddy) return NULL;
163 activity_str = sbuddy->activity ? sbuddy->activity :
164 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
165 status_text : NULL;
167 if (activity_str && sbuddy->note) {
168 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
169 } else if (activity_str) {
170 return g_strdup(activity_str);
171 } else if (sbuddy->note) {
172 return g_strdup_printf("<i>%s</i>", sbuddy->note);
173 } else {
174 return NULL;
178 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
179 const gchar *with)
181 sipe_backend_buddy pbuddy;
182 gchar *alias = NULL;
183 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
184 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
186 return alias;
189 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
190 const gchar *who,
191 const gchar *old_group_name,
192 const gchar *new_group_name)
194 struct sipe_buddy * buddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, who);
195 struct sipe_group * old_group = NULL;
196 struct sipe_group * new_group;
198 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
199 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
201 if(!buddy) { // buddy not in roaming list
202 return;
205 if (old_group_name) {
206 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
208 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
210 if (old_group) {
211 buddy->groups = g_slist_remove(buddy->groups, old_group);
212 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
215 if (!new_group) {
216 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
217 } else {
218 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
219 new_group,
220 (GCompareFunc)sipe_group_compare,
221 NULL);
222 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
226 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
227 const gchar *uri,
228 const gchar *group_name)
230 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
232 if (!g_hash_table_lookup(sipe_private->buddies, uri)) {
233 struct sipe_buddy *b = sipe_buddy_add(sipe_private, uri);
234 b->just_added = TRUE;
236 sipe_subscribe_presence_single_cb(sipe_private, b->name);
238 } else {
239 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
240 uri);
243 sipe_core_buddy_group(sipe_public,
244 uri,
245 NULL,
246 group_name);
249 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
250 struct sipe_buddy *buddy)
252 gchar *action_name = sipe_utils_presence_key(buddy->name);
253 sipe_schedule_cancel(sipe_private, action_name);
254 g_free(action_name);
256 g_hash_table_remove(sipe_private->buddies, buddy->name);
258 buddy_free(buddy);
262 * Unassociates buddy from group first.
263 * Then see if no groups left, removes buddy completely.
264 * Otherwise updates buddy groups on server.
266 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
267 const gchar *uri,
268 const gchar *group_name)
270 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
271 struct sipe_buddy *b = g_hash_table_lookup(sipe_private->buddies,
272 uri);
274 if (!b) return;
276 if (group_name) {
277 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
278 group_name);
279 if (g) {
280 b->groups = g_slist_remove(b->groups, g);
281 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
282 uri, g->name);
286 if (g_slist_length(b->groups) < 1) {
287 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
288 b->name);
289 sip_soap_request(sipe_private,
290 "deleteContact",
291 request);
292 g_free(request);
293 sipe_buddy_remove(sipe_private, b);
294 } else {
295 /* updates groups on server */
296 sipe_group_update_buddy(sipe_private, b);
301 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
302 const gchar *uri,
303 guint activity)
305 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
306 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies,
307 uri);
309 if (!sbuddy) return;
311 /* Check if on 2005 system contact's calendar,
312 * then set/preserve it.
314 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
315 sipe_backend_buddy_set_status(sipe_public, uri, activity);
316 } else {
317 sipe_ocs2005_apply_calendar_status(sipe_private,
318 sbuddy,
319 sipe_status_activity_to_token(activity));
323 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
324 const gchar *uri,
325 const gchar *status_name,
326 gboolean is_online,
327 struct sipe_backend_buddy_tooltip *tooltip)
329 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
330 gchar *note = NULL;
331 gboolean is_oof_note = FALSE;
332 const gchar *activity = NULL;
333 gchar *calendar = NULL;
334 const gchar *meeting_subject = NULL;
335 const gchar *meeting_location = NULL;
336 gchar *access_text = NULL;
338 #define SIPE_ADD_BUDDY_INFO(l, t) \
340 gchar *tmp = g_markup_escape_text((t), -1); \
341 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
342 g_free(tmp); \
344 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
345 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
347 if (sipe_public) { /* happens on pidgin exit */
348 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
349 if (sbuddy) {
350 note = sbuddy->note;
351 is_oof_note = sbuddy->is_oof_note;
352 activity = sbuddy->activity;
353 calendar = sipe_cal_get_description(sbuddy);
354 meeting_subject = sbuddy->meeting_subject;
355 meeting_location = sbuddy->meeting_location;
357 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
358 gboolean is_group_access = FALSE;
359 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
360 "user",
361 sipe_get_no_sip_uri(uri),
362 &is_group_access);
363 const char *access_level = sipe_ocs2007_access_level_name(container_id);
364 access_text = is_group_access ?
365 g_strdup(access_level) :
366 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
367 access_level);
371 if (is_online) {
372 const gchar *status_str = activity ? activity : status_name;
374 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
376 if (is_online && !is_empty(calendar)) {
377 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
379 g_free(calendar);
380 if (!is_empty(meeting_location)) {
381 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
382 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
384 if (!is_empty(meeting_subject)) {
385 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
386 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
388 if (note) {
389 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
390 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
391 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
392 note_italics);
393 g_free(note_italics);
395 if (access_text) {
396 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
397 g_free(access_text);
401 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
402 const char *uri,
403 sipe_buddy_info_fields propkey,
404 char *property_value)
406 GSList *buddies, *entry;
408 if (property_value)
409 property_value = g_strstrip(property_value);
411 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
412 while (entry) {
413 gchar *prop_str;
414 sipe_backend_buddy p_buddy = entry->data;
416 /* for Display Name */
417 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
418 gchar *alias;
419 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
420 if (property_value && sipe_is_bad_alias(uri, alias)) {
421 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
422 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
424 g_free(alias);
426 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
427 if (!is_empty(property_value) &&
428 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
430 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
431 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
433 g_free(alias);
435 /* for other properties */
436 else {
437 if (!is_empty(property_value)) {
438 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
439 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
440 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
442 g_free(prop_str);
446 entry = entry->next;
448 g_slist_free(buddies);
452 struct ms_dlx_data;
453 struct ms_dlx_data {
454 GSList *search_rows;
455 gchar *other;
456 guint max_returns;
457 sipe_svc_callback *callback;
458 struct sipe_svc_session *session;
459 gchar *wsse_security;
460 struct sipe_backend_search_token *token;
461 /* must call ms_dlx_free() */
462 void (*failed_callback)(struct sipe_core_private *sipe_private,
463 struct ms_dlx_data *mdd);
466 static void ms_dlx_free(struct ms_dlx_data *mdd)
468 sipe_utils_slist_free_full(mdd->search_rows, g_free);
469 sipe_svc_session_close(mdd->session);
470 g_free(mdd->other);
471 g_free(mdd->wsse_security);
472 g_free(mdd);
475 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
476 #define DLX_SEARCH_ITEM \
477 "<AbEntryRequest.ChangeSearchQuery>" \
478 " <SearchOn>%s</SearchOn>" \
479 " <Value>%s</Value>" \
480 "</AbEntryRequest.ChangeSearchQuery>"
482 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
483 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
484 guint i = 0;
485 gchar *query = NULL;
487 while (query_rows) {
488 gchar *attr;
489 gchar *value;
491 attr = query_rows->data;
492 query_rows = g_slist_next(query_rows);
493 value = query_rows->data;
494 query_rows = g_slist_next(query_rows);
496 if (!attr || !value)
497 break;
499 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
500 attr, value);
502 attrs[i] = NULL;
504 if (i) {
505 query = g_strjoinv(NULL, attrs);
506 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
507 query ? query : "");
510 g_strfreev(attrs);
512 return query;
515 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
516 const gchar *base_uri,
517 const gchar *auth_uri,
518 const gchar *wsse_security,
519 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
520 gpointer callback_data)
522 struct ms_dlx_data *mdd = callback_data;
524 if (wsse_security) {
525 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
527 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
528 base_uri);
530 if (sipe_svc_ab_entry_request(sipe_private,
531 mdd->session,
532 auth_uri,
533 wsse_security,
534 query,
535 g_slist_length(mdd->search_rows) / 2,
536 mdd->max_returns,
537 mdd->callback,
538 mdd)) {
540 /* keep webticket security token for potential further use */
541 mdd->wsse_security = g_strdup(wsse_security);
543 /* callback data passed down the line */
544 mdd = NULL;
546 g_free(query);
548 } else {
549 /* no ticket: this will show the minmum information */
550 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
551 base_uri);
554 if (mdd)
555 mdd->failed_callback(sipe_private, mdd);
558 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
559 struct ms_dlx_data *mdd)
561 if (!sipe_webticket_request(sipe_private,
562 mdd->session,
563 sipe_private->dlx_uri,
564 "AddressBookWebTicketBearer",
565 ms_dlx_webticket,
566 mdd)) {
567 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
568 sipe_private->dlx_uri);
569 mdd->failed_callback(sipe_private, mdd);
573 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
574 struct sipe_backend_search_results *results,
575 guint match_count,
576 gboolean more)
578 gchar *secondary = g_strdup_printf(
579 dngettext(PACKAGE_NAME,
580 "Found %d contact%s:",
581 "Found %d contacts%s:", match_count),
582 match_count, more ? _(" (more matched your query)") : "");
584 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
585 results,
586 secondary,
587 more);
588 g_free(secondary);
591 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
592 const gchar *uri,
593 SIPE_UNUSED_PARAMETER const gchar *raw,
594 sipe_xml *soap_body,
595 gpointer callback_data)
597 struct ms_dlx_data *mdd = callback_data;
599 if (soap_body) {
600 const sipe_xml *node;
601 struct sipe_backend_search_results *results;
602 GHashTable *found;
604 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
605 uri);
607 /* any matches? */
608 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
609 if (!node) {
610 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
611 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
612 mdd->token,
613 _("No contacts found"));
614 ms_dlx_free(mdd);
615 return;
618 /* OK, we found something - show the results to the user */
619 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
620 mdd->token);
621 if (!results) {
622 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
623 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
624 mdd->token,
625 _("Unable to display the search results"));
626 ms_dlx_free(mdd);
627 return;
630 /* SearchAbEntryResult can contain duplicates */
631 found = g_hash_table_new_full(g_str_hash, g_str_equal,
632 g_free, NULL);
634 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
635 const sipe_xml *attrs;
636 gchar *sip_uri = NULL;
637 gchar *displayname = NULL;
638 gchar *company = NULL;
639 gchar *country = NULL;
640 gchar *email = NULL;
642 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
643 attrs;
644 attrs = sipe_xml_twin(attrs)) {
645 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
646 "Name"));
647 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
648 "Value"));
650 if (!is_empty(value)) {
651 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
652 g_free(sip_uri);
653 sip_uri = value;
654 value = NULL;
655 } else if (sipe_strcase_equal(name, "displayname")) {
656 g_free(displayname);
657 displayname = value;
658 value = NULL;
659 } else if (sipe_strcase_equal(name, "mail")) {
660 g_free(email);
661 email = value;
662 value = NULL;
663 } else if (sipe_strcase_equal(name, "company")) {
664 g_free(company);
665 company = value;
666 value = NULL;
667 } else if (sipe_strcase_equal(name, "country")) {
668 g_free(country);
669 country = value;
670 value = NULL;
674 g_free(value);
675 g_free(name);
678 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
679 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
680 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
681 results,
682 uri_parts[1],
683 displayname,
684 company,
685 country,
686 email);
687 g_strfreev(uri_parts);
689 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
690 sip_uri = NULL;
693 g_free(email);
694 g_free(country);
695 g_free(company);
696 g_free(displayname);
697 g_free(sip_uri);
700 search_contacts_finalize(sipe_private, results,
701 g_hash_table_size(found),
702 FALSE);
703 g_hash_table_destroy(found);
704 ms_dlx_free(mdd);
706 } else {
707 mdd->failed_callback(sipe_private, mdd);
711 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
712 struct sipmsg *msg,
713 struct transaction *trans)
715 struct sipe_backend_search_token *token = trans->payload->data;
716 struct sipe_backend_search_results *results;
717 sipe_xml *searchResults;
718 const sipe_xml *mrow;
719 guint match_count = 0;
720 gboolean more = FALSE;
722 /* valid response? */
723 if (msg->response != 200) {
724 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
725 msg->response);
726 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
727 token,
728 _("Contact search failed"));
729 return(FALSE);
732 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
734 /* valid XML? */
735 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
736 if (!searchResults) {
737 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
738 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
739 token,
740 _("Contact search failed"));
741 return(FALSE);
744 /* any matches? */
745 mrow = sipe_xml_child(searchResults, "Body/Array/row");
746 if (!mrow) {
747 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
748 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
749 token,
750 _("No contacts found"));
752 sipe_xml_free(searchResults);
753 return(FALSE);
756 /* OK, we found something - show the results to the user */
757 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
758 trans->payload->data);
759 if (!results) {
760 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
761 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
762 token,
763 _("Unable to display the search results"));
765 sipe_xml_free(searchResults);
766 return FALSE;
769 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
770 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
771 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
772 results,
773 uri_parts[1],
774 sipe_xml_attribute(mrow, "displayName"),
775 sipe_xml_attribute(mrow, "company"),
776 sipe_xml_attribute(mrow, "country"),
777 sipe_xml_attribute(mrow, "email"));
778 g_strfreev(uri_parts);
779 match_count++;
782 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
783 char *data = sipe_xml_data(mrow);
784 more = (g_ascii_strcasecmp(data, "true") == 0);
785 g_free(data);
788 search_contacts_finalize(sipe_private, results, match_count, more);
789 sipe_xml_free(searchResults);
791 return(TRUE);
794 static void search_soap_request(struct sipe_core_private *sipe_private,
795 struct sipe_backend_search_token *token,
796 GSList *search_rows)
798 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
799 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
801 payload->data = token;
803 sip_soap_directory_search(sipe_private,
804 100,
805 query,
806 process_search_contact_response,
807 payload);
808 g_free(query);
811 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
812 struct ms_dlx_data *mdd)
814 /* error using [MS-DLX] server, retry using Active Directory */
815 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
816 ms_dlx_free(mdd);
819 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
820 struct sipe_backend_search_token *token,
821 const gchar *given_name,
822 const gchar *surname,
823 const gchar *email,
824 const gchar *company,
825 const gchar *country)
827 GSList *query_rows = NULL;
829 #define ADD_QUERY_ROW(attr, val) \
830 if (val) { \
831 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
832 query_rows = g_slist_append(query_rows, g_strdup(val)); \
835 ADD_QUERY_ROW("givenName", given_name);
836 ADD_QUERY_ROW("sn", surname);
837 ADD_QUERY_ROW("mail", email);
838 ADD_QUERY_ROW("company", company);
839 ADD_QUERY_ROW("c", country);
841 if (query_rows) {
842 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
843 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
845 mdd->search_rows = query_rows;
846 mdd->max_returns = 100;
847 mdd->callback = search_ab_entry_response;
848 mdd->failed_callback = search_ab_entry_failed;
849 mdd->session = sipe_svc_session_start();
850 mdd->token = token;
852 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
854 } else {
855 /* no [MS-DLX] server, use Active Directory search instead */
856 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
857 sipe_utils_slist_free_full(query_rows, g_free);
859 } else
860 sipe_backend_search_failed(sipe_public,
861 token,
862 _("Invalid contact search query"));
865 static void get_info_finalize(struct sipe_core_private *sipe_private,
866 struct sipe_backend_buddy_info *info,
867 const gchar *uri,
868 const gchar *server_alias,
869 const gchar *email)
871 sipe_backend_buddy bbuddy;
872 struct sipe_buddy *sbuddy;
873 gchar *alias;
874 gchar *value;
876 if (!info) {
877 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
878 } else {
879 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
881 if (!info)
882 return;
884 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
886 if (is_empty(server_alias)) {
887 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
888 bbuddy);
889 if (value) {
890 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
891 info,
892 SIPE_BUDDY_INFO_DISPLAY_NAME,
893 value);
895 } else {
896 value = g_strdup(server_alias);
899 /* present alias if it differs from server alias */
900 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
901 if (alias && !sipe_strequal(alias, value))
903 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
904 info,
905 SIPE_BUDDY_INFO_ALIAS,
906 alias);
908 g_free(alias);
909 g_free(value);
911 if (is_empty(email)) {
912 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
913 bbuddy,
914 SIPE_BUDDY_INFO_EMAIL);
915 if (value) {
916 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
917 info,
918 SIPE_BUDDY_INFO_EMAIL,
919 value);
920 g_free(value);
924 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
925 bbuddy,
926 SIPE_BUDDY_INFO_SITE);
927 if (value) {
928 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
929 info,
930 SIPE_BUDDY_INFO_SITE,
931 value);
932 g_free(value);
935 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
936 if (sbuddy && sbuddy->device_name) {
937 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
938 info,
939 SIPE_BUDDY_INFO_DEVICE,
940 sbuddy->device_name);
943 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
947 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
948 const gchar *uri,
949 SIPE_UNUSED_PARAMETER const gchar *raw,
950 sipe_xml *soap_body,
951 gpointer callback_data)
953 struct ms_dlx_data *mdd = callback_data;
954 struct sipe_backend_buddy_info *info = NULL;
955 gchar *server_alias = NULL;
956 gchar *email = NULL;
958 if (soap_body) {
959 const sipe_xml *node;
961 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
962 uri);
964 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
966 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
967 node;
968 node = sipe_xml_twin(node)) {
969 gchar *name = sipe_xml_data(sipe_xml_child(node,
970 "Name"));
971 gchar *value = sipe_xml_data(sipe_xml_child(node,
972 "Value"));
973 const sipe_xml *values = sipe_xml_child(node,
974 "Values");
976 /* Single value entries */
977 if (!is_empty(value)) {
979 if (sipe_strcase_equal(name, "displayname")) {
980 g_free(server_alias);
981 server_alias = value;
982 value = NULL;
983 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
984 info,
985 SIPE_BUDDY_INFO_DISPLAY_NAME,
986 server_alias);
987 } else if (sipe_strcase_equal(name, "mail")) {
988 g_free(email);
989 email = value;
990 value = NULL;
991 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
992 info,
993 SIPE_BUDDY_INFO_EMAIL,
994 email);
995 } else if (sipe_strcase_equal(name, "title")) {
996 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
997 info,
998 SIPE_BUDDY_INFO_JOB_TITLE,
999 value);
1000 } else if (sipe_strcase_equal(name, "company")) {
1001 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1002 info,
1003 SIPE_BUDDY_INFO_COMPANY,
1004 value);
1005 } else if (sipe_strcase_equal(name, "country")) {
1006 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1007 info,
1008 SIPE_BUDDY_INFO_COUNTRY,
1009 value);
1012 } else if (values) {
1013 gchar *first = sipe_xml_data(sipe_xml_child(values,
1014 "string"));
1016 if (sipe_strcase_equal(name, "telephonenumber")) {
1017 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1018 info,
1019 SIPE_BUDDY_INFO_WORK_PHONE,
1020 first);
1023 g_free(first);
1026 g_free(value);
1027 g_free(name);
1031 /* this will show the minmum information */
1032 get_info_finalize(sipe_private,
1033 info,
1034 mdd->other,
1035 server_alias,
1036 email);
1038 g_free(email);
1039 g_free(server_alias);
1040 ms_dlx_free(mdd);
1043 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1044 struct sipmsg *msg,
1045 struct transaction *trans)
1047 const gchar *uri = trans->payload->data;
1048 struct sipe_backend_buddy_info *info = NULL;
1049 gchar *server_alias = NULL;
1050 gchar *email = NULL;
1052 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1053 uri, sipe_private->username);
1055 if (msg->response != 200) {
1056 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1057 } else {
1058 sipe_xml *searchResults;
1059 const sipe_xml *mrow;
1061 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1062 msg->body ? msg->body : "");
1064 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1065 if (!searchResults) {
1067 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1069 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1070 const gchar *value;
1071 gchar *phone_number;
1073 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1075 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1076 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1077 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1080 * For 2007 system we will take this from ContactCard -
1081 * it has cleaner tel: URIs at least
1083 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1084 char *tel_uri = sip_to_tel_uri(phone_number);
1085 /* trims its parameters, so call first */
1086 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1087 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1088 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1089 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1090 g_free(tel_uri);
1092 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1093 uri);
1096 if (!is_empty(server_alias)) {
1097 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1098 info,
1099 SIPE_BUDDY_INFO_DISPLAY_NAME,
1100 server_alias);
1102 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1103 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1104 info,
1105 SIPE_BUDDY_INFO_JOB_TITLE,
1106 value);
1108 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1109 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1110 info,
1111 SIPE_BUDDY_INFO_OFFICE,
1112 value);
1114 if (!is_empty(phone_number)) {
1115 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1116 info,
1117 SIPE_BUDDY_INFO_WORK_PHONE,
1118 phone_number);
1120 g_free(phone_number);
1121 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1122 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1123 info,
1124 SIPE_BUDDY_INFO_COMPANY,
1125 value);
1127 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1128 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1129 info,
1130 SIPE_BUDDY_INFO_CITY,
1131 value);
1133 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1134 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1135 info,
1136 SIPE_BUDDY_INFO_STATE,
1137 value);
1139 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1140 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1141 info,
1142 SIPE_BUDDY_INFO_COUNTRY,
1143 value);
1145 if (!is_empty(email)) {
1146 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1147 info,
1148 SIPE_BUDDY_INFO_EMAIL,
1149 email);
1152 sipe_xml_free(searchResults);
1155 /* this will show the minmum information */
1156 get_info_finalize(sipe_private,
1157 info,
1158 uri,
1159 server_alias,
1160 email);
1162 g_free(server_alias);
1163 g_free(email);
1165 return TRUE;
1168 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1169 struct ms_dlx_data *mdd)
1171 /* error using [MS-DLX] server, retry using Active Directory */
1172 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1173 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1175 payload->destroy = g_free;
1176 payload->data = mdd->other;
1177 mdd->other = NULL;
1179 sip_soap_directory_search(sipe_private,
1181 query,
1182 process_get_info_response,
1183 payload);
1185 ms_dlx_free(mdd);
1186 g_free(query);
1189 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1190 const gchar *who)
1192 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1194 if (sipe_private->dlx_uri) {
1195 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1197 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1198 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1200 mdd->other = g_strdup(who);
1201 mdd->max_returns = 1;
1202 mdd->callback = get_info_ab_entry_response;
1203 mdd->failed_callback = get_info_ab_entry_failed;
1204 mdd->session = sipe_svc_session_start();
1206 ms_dlx_webticket_request(sipe_private, mdd);
1208 } else {
1209 /* no [MS-DLX] server, use Active Directory search instead */
1210 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1211 "msRTCSIP-PrimaryUserAddress",
1212 who);
1213 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1215 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1216 row ? row : "");
1218 payload->destroy = g_free;
1219 payload->data = g_strdup(who);
1221 sip_soap_directory_search(sipe_private,
1223 row,
1224 process_get_info_response,
1225 payload);
1226 g_free(row);
1230 static void photo_response_data_free(struct photo_response_data *data)
1232 g_free(data->who);
1233 g_free(data->photo_hash);
1234 if (data->request) {
1235 sipe_http_request_cancel(data->request);
1237 g_free(data);
1240 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1241 guint status,
1242 GSList *headers,
1243 const char *body,
1244 gpointer data)
1246 struct photo_response_data *rdata = (struct photo_response_data *) data;
1248 rdata->request = NULL;
1250 if (status == SIPE_HTTP_STATUS_OK) {
1251 const gchar *len_str = sipe_utils_nameval_find(headers,
1252 "Content-Length");
1253 if (len_str) {
1254 gsize photo_size = atoi(len_str);
1255 gpointer photo = g_new(char, photo_size);
1257 if (photo) {
1258 memcpy(photo, body, photo_size);
1260 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1261 rdata->who,
1262 photo,
1263 photo_size,
1264 rdata->photo_hash);
1269 sipe_private->pending_photo_requests =
1270 g_slist_remove(sipe_private->pending_photo_requests, rdata);
1272 photo_response_data_free(rdata);
1275 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1277 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1278 gchar *wsse_security_base64;
1279 gchar *x_ms_webticket_header;
1281 if (!assertion) {
1282 return NULL;
1285 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1286 strlen(assertion));
1287 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1288 wsse_security_base64);
1290 g_free(assertion);
1291 g_free(wsse_security_base64);
1293 return x_ms_webticket_header;
1296 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1297 const gchar *uri,
1298 SIPE_UNUSED_PARAMETER const gchar *raw,
1299 sipe_xml *soap_body,
1300 gpointer callback_data)
1302 struct ms_dlx_data *mdd = callback_data;
1303 gchar *photo_rel_path = NULL;
1304 gchar *photo_hash = NULL;
1305 const gchar *photo_hash_old =
1306 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1308 if (soap_body) {
1309 const sipe_xml *node;
1311 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1312 uri);
1314 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1315 node;
1316 node = sipe_xml_twin(node)) {
1317 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1318 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1320 if (!is_empty(value)) {
1321 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1322 g_free(photo_rel_path);
1323 photo_rel_path = value;
1324 value = NULL;
1325 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1326 g_free(photo_hash);
1327 photo_hash = value;
1328 value = NULL;
1332 g_free(value);
1333 g_free(name);
1337 if (sipe_private->addressbook_uri && photo_rel_path &&
1338 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1339 gchar *photo_url = g_strdup_printf("%s/%s",
1340 sipe_private->addressbook_uri, photo_rel_path);
1341 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1343 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1344 data->who = g_strdup(mdd->other);
1345 data->photo_hash = photo_hash;
1346 photo_hash = NULL;
1348 data->request = sipe_http_request_get(sipe_private,
1349 photo_url,
1350 x_ms_webticket_header,
1351 process_buddy_photo_response,
1352 data);
1354 if (data->request) {
1355 sipe_private->pending_photo_requests =
1356 g_slist_append(sipe_private->pending_photo_requests, data);
1357 sipe_http_request_ready(data->request);
1358 } else {
1359 photo_response_data_free(data);
1362 g_free(x_ms_webticket_header);
1363 g_free(photo_url);
1366 g_free(photo_rel_path);
1367 g_free(photo_hash);
1368 ms_dlx_free(mdd);
1371 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1372 struct ms_dlx_data *mdd)
1374 ms_dlx_free(mdd);
1377 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1378 const gchar *uri)
1380 if (sipe_backend_uses_photo()) {
1382 /* Lync 2013 or newer: use UCS */
1383 if (SIPE_CORE_PRIVATE_FLAG_IS(LYNC2013)) {
1385 sipe_ucs_get_photo(sipe_private, uri);
1387 /* Lync 2010: use [MS-DLX] */
1388 } else if (sipe_private->dlx_uri &&
1389 sipe_private->addressbook_uri) {
1390 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1392 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1393 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1395 mdd->other = g_strdup(uri);
1396 mdd->max_returns = 1;
1397 mdd->callback = get_photo_ab_entry_response;
1398 mdd->failed_callback = get_photo_ab_entry_failed;
1399 mdd->session = sipe_svc_session_start();
1401 ms_dlx_webticket_request(sipe_private, mdd);
1406 static void buddy_refresh_photos_cb(gpointer uri,
1407 SIPE_UNUSED_PARAMETER gpointer value,
1408 gpointer sipe_private)
1410 buddy_fetch_photo(sipe_private, uri);
1413 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1415 g_hash_table_foreach(sipe_private->buddies,
1416 buddy_refresh_photos_cb,
1417 sipe_private);
1420 /* Buddy menu callbacks*/
1422 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1423 const gchar *who)
1425 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1427 /* 2007+ conference */
1428 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1429 sipe_conf_add(sipe_private, who);
1431 /* 2005- multiparty chat */
1432 } else {
1433 gchar *self = sip_uri_self(sipe_private);
1434 struct sip_session *session;
1436 session = sipe_session_add_chat(sipe_private,
1437 NULL,
1438 TRUE,
1439 self);
1440 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1441 session->chat_session,
1442 session->chat_session->title,
1443 self);
1444 g_free(self);
1446 sipe_im_invite(sipe_private, session, who,
1447 NULL, NULL, NULL, FALSE);
1451 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1452 const gchar *who)
1454 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1455 who,
1456 NULL);
1457 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1458 buddy,
1459 SIPE_BUDDY_INFO_EMAIL);
1461 if (email) {
1462 gchar *command_line = g_strdup_printf(
1463 #ifdef _WIN32
1464 "cmd /c start"
1465 #else
1466 "xdg-email"
1467 #endif
1468 " mailto:%s", email);
1469 g_free(email);
1471 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1472 command_line);
1473 g_spawn_command_line_async(command_line, NULL);
1474 g_free(command_line);
1476 } else {
1477 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1478 who);
1482 /* Buddy menu */
1484 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1485 struct sipe_backend_buddy_menu *menu,
1486 sipe_backend_buddy buddy,
1487 sipe_buddy_info_fields id_phone,
1488 sipe_buddy_info_fields id_display,
1489 const gchar *type)
1491 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1492 buddy,
1493 id_phone);
1494 if (phone) {
1495 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1496 buddy,
1497 id_display);
1498 gchar *tmp = NULL;
1499 gchar *label = g_strdup_printf("%s %s",
1500 type,
1501 display ? display :
1502 (tmp = sip_tel_uri_denormalize(phone)));
1503 menu = sipe_backend_buddy_menu_add(sipe_public,
1504 menu,
1505 label,
1506 SIPE_BUDDY_MENU_MAKE_CALL,
1507 phone);
1508 g_free(tmp);
1509 g_free(label);
1510 g_free(display);
1511 g_free(phone);
1514 return(menu);
1517 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1518 const gchar *buddy_name,
1519 struct sipe_backend_buddy_menu *menu)
1521 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1522 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1523 buddy_name,
1524 NULL);
1525 gchar *self = sip_uri_self(sipe_private);
1527 SIPE_SESSION_FOREACH {
1528 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1530 struct sipe_chat_session *chat_session = session->chat_session;
1531 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1533 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1535 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1537 if (is_conf &&
1538 /* Not conf OP */
1539 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1540 /* We are a conf OP */
1541 conf_op) {
1542 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1543 chat_session->title);
1544 menu = sipe_backend_buddy_menu_add(sipe_public,
1545 menu,
1546 label,
1547 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1548 chat_session);
1549 g_free(label);
1552 if (is_conf &&
1553 /* We are a conf OP */
1554 conf_op) {
1555 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1556 chat_session->title);
1557 menu = sipe_backend_buddy_menu_add(sipe_public,
1558 menu,
1559 label,
1560 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1561 chat_session);
1562 g_free(label);
1565 else
1567 if (!is_conf ||
1568 (is_conf && !session->locked)) {
1569 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1570 chat_session->title);
1571 menu = sipe_backend_buddy_menu_add(sipe_public,
1572 menu,
1573 label,
1574 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1575 chat_session);
1576 g_free(label);
1580 } SIPE_SESSION_FOREACH_END;
1581 g_free(self);
1583 menu = sipe_backend_buddy_menu_add(sipe_public,
1584 menu,
1585 _("New chat"),
1586 SIPE_BUDDY_MENU_NEW_CHAT,
1587 NULL);
1589 /* add buddy's phone numbers if we have call control */
1590 if (sip_csta_is_idle(sipe_private)) {
1592 /* work phone */
1593 menu = buddy_menu_phone(sipe_public,
1594 menu,
1595 buddy,
1596 SIPE_BUDDY_INFO_WORK_PHONE,
1597 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1598 _("Work"));
1599 /* mobile phone */
1600 menu = buddy_menu_phone(sipe_public,
1601 menu,
1602 buddy,
1603 SIPE_BUDDY_INFO_MOBILE_PHONE,
1604 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1605 _("Mobile"));
1607 /* home phone */
1608 menu = buddy_menu_phone(sipe_public,
1609 menu,
1610 buddy,
1611 SIPE_BUDDY_INFO_HOME_PHONE,
1612 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1613 _("Home"));
1615 /* other phone */
1616 menu = buddy_menu_phone(sipe_public,
1617 menu,
1618 buddy,
1619 SIPE_BUDDY_INFO_OTHER_PHONE,
1620 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1621 _("Other"));
1623 /* custom1 phone */
1624 menu = buddy_menu_phone(sipe_public,
1625 menu,
1626 buddy,
1627 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1628 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1629 _("Custom1"));
1633 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1634 buddy,
1635 SIPE_BUDDY_INFO_EMAIL);
1636 if (email) {
1637 menu = sipe_backend_buddy_menu_add(sipe_public,
1638 menu,
1639 _("Send email..."),
1640 SIPE_BUDDY_MENU_SEND_EMAIL,
1641 NULL);
1642 g_free(email);
1646 /* access level control */
1647 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1648 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1649 menu,
1650 _("Access level"),
1651 sipe_ocs2007_access_control_menu(sipe_private,
1652 buddy_name));
1654 return(menu);
1658 Local Variables:
1659 mode: c
1660 c-file-style: "bsd"
1661 indent-tabs-mode: t
1662 tab-width: 8
1663 End: