webticket: cache ADFS token
[siplcs.git] / src / core / sipe-buddy.c
blobdcfcb827b51d1bb0fdfbf8858c327e75a59c26eb
1 /**
2 * @file sipe-buddy.c
4 * pidgin-sipe
6 * Copyright (C) 2010-12 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 "http-conn.h"
34 #include "sipe-common.h"
35 #include "sipmsg.h"
36 #include "sip-csta.h"
37 #include "sip-soap.h"
38 #include "sip-transport.h"
39 #include "sipe-backend.h"
40 #include "sipe-buddy.h"
41 #include "sipe-cal.h"
42 #include "sipe-chat.h"
43 #include "sipe-conf.h"
44 #include "sipe-core.h"
45 #include "sipe-core-private.h"
46 #include "sipe-group.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-utils.h"
57 #include "sipe-webticket.h"
58 #include "sipe-xml.h"
60 struct photo_response_data {
61 struct sipe_core_private *sipe_private;
62 gchar *who;
63 gchar *photo_hash;
64 HttpConn *conn;
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 = slist_insert_unique_sorted(buddy->groups, new_group, (GCompareFunc)sipe_group_compare);
219 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
223 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
224 const gchar *uri,
225 const gchar *group_name)
227 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
229 if (!g_hash_table_lookup(sipe_private->buddies, uri)) {
230 struct sipe_buddy *b = sipe_buddy_add(sipe_private, uri);
231 b->just_added = TRUE;
233 /* @TODO should go to callback */
234 sipe_subscribe_presence_single(sipe_private, b->name);
236 } else {
237 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
238 uri);
241 sipe_core_buddy_group(sipe_public,
242 uri,
243 NULL,
244 group_name);
247 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
248 struct sipe_buddy *buddy)
250 gchar *action_name = sipe_utils_presence_key(buddy->name);
251 sipe_schedule_cancel(sipe_private, action_name);
252 g_free(action_name);
254 g_hash_table_remove(sipe_private->buddies, buddy->name);
256 buddy_free(buddy);
260 * Unassociates buddy from group first.
261 * Then see if no groups left, removes buddy completely.
262 * Otherwise updates buddy groups on server.
264 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
265 const gchar *uri,
266 const gchar *group_name)
268 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
269 struct sipe_buddy *b = g_hash_table_lookup(sipe_private->buddies,
270 uri);
272 if (!b) return;
274 if (group_name) {
275 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
276 group_name);
277 if (g) {
278 b->groups = g_slist_remove(b->groups, g);
279 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
280 uri, g->name);
284 if (g_slist_length(b->groups) < 1) {
285 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
286 b->name);
287 sip_soap_request(sipe_private,
288 "deleteContact",
289 request);
290 g_free(request);
291 sipe_buddy_remove(sipe_private, b);
292 } else {
293 /* updates groups on server */
294 sipe_group_update_buddy(sipe_private, b);
299 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
300 const gchar *uri,
301 guint activity)
303 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
304 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies,
305 uri);
307 if (!sbuddy) return;
309 /* Check if on 2005 system contact's calendar,
310 * then set/preserve it.
312 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
313 sipe_backend_buddy_set_status(sipe_public, uri, activity);
314 } else {
315 sipe_ocs2005_apply_calendar_status(sipe_private,
316 sbuddy,
317 sipe_status_activity_to_token(activity));
321 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
322 const gchar *uri,
323 const gchar *status_name,
324 gboolean is_online,
325 struct sipe_backend_buddy_tooltip *tooltip)
327 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
328 gchar *note = NULL;
329 gboolean is_oof_note = FALSE;
330 const gchar *activity = NULL;
331 gchar *calendar = NULL;
332 const gchar *meeting_subject = NULL;
333 const gchar *meeting_location = NULL;
334 gchar *access_text = NULL;
336 #define SIPE_ADD_BUDDY_INFO(l, t) \
338 gchar *tmp = g_markup_escape_text((t), -1); \
339 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
340 g_free(tmp); \
342 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
343 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
345 if (sipe_public) { /* happens on pidgin exit */
346 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
347 if (sbuddy) {
348 note = sbuddy->note;
349 is_oof_note = sbuddy->is_oof_note;
350 activity = sbuddy->activity;
351 calendar = sipe_cal_get_description(sbuddy);
352 meeting_subject = sbuddy->meeting_subject;
353 meeting_location = sbuddy->meeting_location;
355 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
356 gboolean is_group_access = FALSE;
357 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
358 "user",
359 sipe_get_no_sip_uri(uri),
360 &is_group_access);
361 const char *access_level = sipe_ocs2007_access_level_name(container_id);
362 access_text = is_group_access ?
363 g_strdup(access_level) :
364 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
365 access_level);
369 if (is_online) {
370 const gchar *status_str = activity ? activity : status_name;
372 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
374 if (is_online && !is_empty(calendar)) {
375 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
377 g_free(calendar);
378 if (!is_empty(meeting_location)) {
379 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
380 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
382 if (!is_empty(meeting_subject)) {
383 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
384 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
386 if (note) {
387 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
388 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
389 g_strdup_printf("<i>%s</i>", note));
391 if (access_text) {
392 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
393 g_free(access_text);
397 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
398 const char *uri,
399 sipe_buddy_info_fields propkey,
400 char *property_value)
402 GSList *buddies, *entry;
404 if (property_value)
405 property_value = g_strstrip(property_value);
407 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
408 while (entry) {
409 gchar *prop_str;
410 sipe_backend_buddy p_buddy = entry->data;
412 /* for Display Name */
413 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
414 gchar *alias;
415 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
416 if (property_value && sipe_is_bad_alias(uri, alias)) {
417 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
418 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
420 g_free(alias);
422 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
423 if (!is_empty(property_value) &&
424 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
426 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
427 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
429 g_free(alias);
431 /* for other properties */
432 else {
433 if (!is_empty(property_value)) {
434 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
435 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
436 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
438 g_free(prop_str);
442 entry = entry->next;
444 g_slist_free(buddies);
448 struct ms_dlx_data;
449 struct ms_dlx_data {
450 GSList *search_rows;
451 gchar *other;
452 guint max_returns;
453 sipe_svc_callback *callback;
454 struct sipe_svc_session *session;
455 gchar *wsse_security;
456 struct sipe_backend_search_token *token;
457 /* must call ms_dlx_free() */
458 void (*failed_callback)(struct sipe_core_private *sipe_private,
459 struct ms_dlx_data *mdd);
462 static void ms_dlx_free(struct ms_dlx_data *mdd)
464 GSList *entry = mdd->search_rows;
465 while (entry) {
466 g_free(entry->data);
467 entry = entry->next;
469 g_slist_free(mdd->search_rows);
470 sipe_svc_session_close(mdd->session);
471 g_free(mdd->other);
472 g_free(mdd->wsse_security);
473 g_free(mdd);
476 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
477 #define DLX_SEARCH_ITEM \
478 "<AbEntryRequest.ChangeSearchQuery>" \
479 " <SearchOn>%s</SearchOn>" \
480 " <Value>%s</Value>" \
481 "</AbEntryRequest.ChangeSearchQuery>"
483 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
484 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
485 guint i = 0;
486 gchar *query = NULL;
488 while (query_rows) {
489 gchar *attr;
490 gchar *value;
492 attr = query_rows->data;
493 query_rows = g_slist_next(query_rows);
494 value = query_rows->data;
495 query_rows = g_slist_next(query_rows);
497 if (!attr || !value)
498 break;
500 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
501 attr, value);
503 attrs[i] = NULL;
505 if (i) {
506 query = g_strjoinv(NULL, attrs);
507 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
508 query ? query : "");
511 g_strfreev(attrs);
513 return query;
516 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
517 const gchar *base_uri,
518 const gchar *auth_uri,
519 const gchar *wsse_security,
520 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
521 gpointer callback_data)
523 struct ms_dlx_data *mdd = callback_data;
525 if (wsse_security) {
526 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
528 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
529 base_uri);
531 if (sipe_svc_ab_entry_request(sipe_private,
532 mdd->session,
533 auth_uri,
534 wsse_security,
535 query,
536 g_slist_length(mdd->search_rows) / 2,
537 mdd->max_returns,
538 mdd->callback,
539 mdd)) {
541 /* keep webticket security token for potential further use */
542 mdd->wsse_security = g_strdup(wsse_security);
544 /* callback data passed down the line */
545 mdd = NULL;
547 g_free(query);
549 } else {
550 /* no ticket: this will show the minmum information */
551 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
552 base_uri);
555 if (mdd)
556 mdd->failed_callback(sipe_private, mdd);
559 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
560 struct ms_dlx_data *mdd)
562 if (!sipe_webticket_request(sipe_private,
563 mdd->session,
564 sipe_private->dlx_uri,
565 "AddressBookWebTicketBearer",
566 ms_dlx_webticket,
567 mdd)) {
568 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
569 sipe_private->dlx_uri);
570 mdd->failed_callback(sipe_private, mdd);
574 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
575 struct sipe_backend_search_results *results,
576 guint match_count,
577 gboolean more)
579 gchar *secondary = g_strdup_printf(
580 dngettext(PACKAGE_NAME,
581 "Found %d contact%s:",
582 "Found %d contacts%s:", match_count),
583 match_count, more ? _(" (more matched your query)") : "");
585 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
586 results,
587 secondary,
588 more);
589 g_free(secondary);
592 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
593 const gchar *uri,
594 SIPE_UNUSED_PARAMETER const gchar *raw,
595 sipe_xml *soap_body,
596 gpointer callback_data)
598 struct ms_dlx_data *mdd = callback_data;
600 if (soap_body) {
601 const sipe_xml *node;
602 struct sipe_backend_search_results *results;
603 GHashTable *found;
605 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
606 uri);
608 /* any matches? */
609 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
610 if (!node) {
611 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
612 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
613 mdd->token,
614 _("No contacts found"));
615 ms_dlx_free(mdd);
616 return;
619 /* OK, we found something - show the results to the user */
620 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
621 mdd->token);
622 if (!results) {
623 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
624 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
625 mdd->token,
626 _("Unable to display the search results"));
627 ms_dlx_free(mdd);
628 return;
631 /* SearchAbEntryResult can contain duplicates */
632 found = g_hash_table_new_full(g_str_hash, g_str_equal,
633 g_free, NULL);
635 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
636 const sipe_xml *attrs;
637 gchar *sip_uri = NULL;
638 gchar *displayname = NULL;
639 gchar *company = NULL;
640 gchar *country = NULL;
641 gchar *email = NULL;
643 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
644 attrs;
645 attrs = sipe_xml_twin(attrs)) {
646 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
647 "Name"));
648 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
649 "Value"));
651 if (!is_empty(value)) {
652 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
653 g_free(sip_uri);
654 sip_uri = value;
655 value = NULL;
656 } else if (sipe_strcase_equal(name, "displayname")) {
657 g_free(displayname);
658 displayname = value;
659 value = NULL;
660 } else if (sipe_strcase_equal(name, "mail")) {
661 g_free(email);
662 email = value;
663 value = NULL;
664 } else if (sipe_strcase_equal(name, "company")) {
665 g_free(company);
666 company = value;
667 value = NULL;
668 } else if (sipe_strcase_equal(name, "country")) {
669 g_free(country);
670 country = value;
671 value = NULL;
675 g_free(value);
676 g_free(name);
679 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
680 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
681 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
682 results,
683 uri_parts[1],
684 displayname,
685 company,
686 country,
687 email);
688 g_strfreev(uri_parts);
690 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
691 sip_uri = NULL;
694 g_free(email);
695 g_free(country);
696 g_free(company);
697 g_free(displayname);
698 g_free(sip_uri);
701 search_contacts_finalize(sipe_private, results,
702 g_hash_table_size(found),
703 FALSE);
704 g_hash_table_destroy(found);
705 ms_dlx_free(mdd);
707 } else {
708 mdd->failed_callback(sipe_private, mdd);
712 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
713 struct sipmsg *msg,
714 struct transaction *trans)
716 struct sipe_backend_search_token *token = trans->payload->data;
717 struct sipe_backend_search_results *results;
718 sipe_xml *searchResults;
719 const sipe_xml *mrow;
720 guint match_count = 0;
721 gboolean more = FALSE;
723 /* valid response? */
724 if (msg->response != 200) {
725 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
726 msg->response);
727 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
728 token,
729 _("Contact search failed"));
730 return(FALSE);
733 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
735 /* valid XML? */
736 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
737 if (!searchResults) {
738 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
739 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
740 token,
741 _("Contact search failed"));
742 return(FALSE);
745 /* any matches? */
746 mrow = sipe_xml_child(searchResults, "Body/Array/row");
747 if (!mrow) {
748 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
749 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
750 token,
751 _("No contacts found"));
753 sipe_xml_free(searchResults);
754 return(FALSE);
757 /* OK, we found something - show the results to the user */
758 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
759 trans->payload->data);
760 if (!results) {
761 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
762 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
763 token,
764 _("Unable to display the search results"));
766 sipe_xml_free(searchResults);
767 return FALSE;
770 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
771 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
772 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
773 results,
774 uri_parts[1],
775 sipe_xml_attribute(mrow, "displayName"),
776 sipe_xml_attribute(mrow, "company"),
777 sipe_xml_attribute(mrow, "country"),
778 sipe_xml_attribute(mrow, "email"));
779 g_strfreev(uri_parts);
780 match_count++;
783 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
784 char *data = sipe_xml_data(mrow);
785 more = (g_ascii_strcasecmp(data, "true") == 0);
786 g_free(data);
789 search_contacts_finalize(sipe_private, results, match_count, more);
790 sipe_xml_free(searchResults);
792 return(TRUE);
795 static void search_soap_request(struct sipe_core_private *sipe_private,
796 struct sipe_backend_search_token *token,
797 GSList *search_rows)
799 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
800 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
802 payload->data = token;
804 sip_soap_directory_search(sipe_private,
805 100,
806 query,
807 process_search_contact_response,
808 payload);
809 g_free(query);
812 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
813 struct ms_dlx_data *mdd)
815 /* error using [MS-DLX] server, retry using Active Directory */
816 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
817 ms_dlx_free(mdd);
820 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
821 struct sipe_backend_search_token *token,
822 const gchar *given_name,
823 const gchar *surname,
824 const gchar *email,
825 const gchar *company,
826 const gchar *country)
828 GSList *query_rows = NULL;
830 #define ADD_QUERY_ROW(attr, val) \
831 if (val) { \
832 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
833 query_rows = g_slist_append(query_rows, g_strdup(val)); \
836 ADD_QUERY_ROW("givenName", given_name);
837 ADD_QUERY_ROW("sn", surname);
838 ADD_QUERY_ROW("mail", email);
839 ADD_QUERY_ROW("company", company);
840 ADD_QUERY_ROW("c", country);
842 if (query_rows) {
843 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
844 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
846 mdd->search_rows = query_rows;
847 mdd->max_returns = 100;
848 mdd->callback = search_ab_entry_response;
849 mdd->failed_callback = search_ab_entry_failed;
850 mdd->session = sipe_svc_session_start();
851 mdd->token = token;
853 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
855 } else {
856 /* no [MS-DLX] server, use Active Directory search instead */
857 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
858 g_slist_free(query_rows);
860 } else
861 sipe_backend_search_failed(sipe_public,
862 token,
863 _("Invalid contact search query"));
866 static void get_info_finalize(struct sipe_core_private *sipe_private,
867 struct sipe_backend_buddy_info *info,
868 const gchar *uri,
869 const gchar *server_alias,
870 const gchar *email)
872 sipe_backend_buddy bbuddy;
873 struct sipe_buddy *sbuddy;
874 gchar *alias;
875 gchar *value;
877 if (!info) {
878 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
879 } else {
880 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
882 if (!info)
883 return;
885 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
887 if (is_empty(server_alias)) {
888 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
889 bbuddy);
890 if (value) {
891 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
892 info,
893 SIPE_BUDDY_INFO_DISPLAY_NAME,
894 value);
896 } else {
897 value = g_strdup(server_alias);
900 /* present alias if it differs from server alias */
901 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
902 if (alias && !sipe_strequal(alias, value))
904 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
905 info,
906 SIPE_BUDDY_INFO_ALIAS,
907 alias);
909 g_free(alias);
910 g_free(value);
912 if (is_empty(email)) {
913 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
914 bbuddy,
915 SIPE_BUDDY_INFO_EMAIL);
916 if (value) {
917 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
918 info,
919 SIPE_BUDDY_INFO_EMAIL,
920 value);
921 g_free(value);
925 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
926 bbuddy,
927 SIPE_BUDDY_INFO_SITE);
928 if (value) {
929 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
930 info,
931 SIPE_BUDDY_INFO_SITE,
932 value);
933 g_free(value);
936 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
937 if (sbuddy && sbuddy->device_name) {
938 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
939 info,
940 SIPE_BUDDY_INFO_DEVICE,
941 sbuddy->device_name);
944 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
948 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
949 const gchar *uri,
950 SIPE_UNUSED_PARAMETER const gchar *raw,
951 sipe_xml *soap_body,
952 gpointer callback_data)
954 struct ms_dlx_data *mdd = callback_data;
955 struct sipe_backend_buddy_info *info = NULL;
956 gchar *server_alias = NULL;
957 gchar *email = NULL;
959 if (soap_body) {
960 const sipe_xml *node;
962 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
963 uri);
965 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
967 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
968 node;
969 node = sipe_xml_twin(node)) {
970 gchar *name = sipe_xml_data(sipe_xml_child(node,
971 "Name"));
972 gchar *value = sipe_xml_data(sipe_xml_child(node,
973 "Value"));
974 const sipe_xml *values = sipe_xml_child(node,
975 "Values");
977 /* Single value entries */
978 if (!is_empty(value)) {
980 if (sipe_strcase_equal(name, "displayname")) {
981 g_free(server_alias);
982 server_alias = value;
983 value = NULL;
984 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
985 info,
986 SIPE_BUDDY_INFO_DISPLAY_NAME,
987 server_alias);
988 } else if (sipe_strcase_equal(name, "mail")) {
989 g_free(email);
990 email = value;
991 value = NULL;
992 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
993 info,
994 SIPE_BUDDY_INFO_EMAIL,
995 email);
996 } else if (sipe_strcase_equal(name, "title")) {
997 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
998 info,
999 SIPE_BUDDY_INFO_JOB_TITLE,
1000 value);
1001 } else if (sipe_strcase_equal(name, "company")) {
1002 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1003 info,
1004 SIPE_BUDDY_INFO_COMPANY,
1005 value);
1006 } else if (sipe_strcase_equal(name, "country")) {
1007 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1008 info,
1009 SIPE_BUDDY_INFO_COUNTRY,
1010 value);
1013 } else if (values) {
1014 gchar *first = sipe_xml_data(sipe_xml_child(values,
1015 "string"));
1017 if (sipe_strcase_equal(name, "telephonenumber")) {
1018 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1019 info,
1020 SIPE_BUDDY_INFO_WORK_PHONE,
1021 first);
1024 g_free(first);
1027 g_free(value);
1028 g_free(name);
1032 /* this will show the minmum information */
1033 get_info_finalize(sipe_private,
1034 info,
1035 mdd->other,
1036 server_alias,
1037 email);
1039 g_free(email);
1040 g_free(server_alias);
1041 ms_dlx_free(mdd);
1044 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1045 struct sipmsg *msg,
1046 struct transaction *trans)
1048 const gchar *uri = trans->payload->data;
1049 struct sipe_backend_buddy_info *info = NULL;
1050 gchar *server_alias = NULL;
1051 gchar *email = NULL;
1053 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1054 uri, sipe_private->username);
1056 if (msg->response != 200) {
1057 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1058 } else {
1059 sipe_xml *searchResults;
1060 const sipe_xml *mrow;
1062 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1063 msg->body ? msg->body : "");
1065 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1066 if (!searchResults) {
1068 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1070 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1071 const gchar *value;
1072 gchar *phone_number;
1074 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1076 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1077 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1078 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1081 * For 2007 system we will take this from ContactCard -
1082 * it has cleaner tel: URIs at least
1084 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1085 char *tel_uri = sip_to_tel_uri(phone_number);
1086 /* trims its parameters, so call first */
1087 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1088 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1089 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1090 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1091 g_free(tel_uri);
1093 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1094 uri);
1097 if (!is_empty(server_alias)) {
1098 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1099 info,
1100 SIPE_BUDDY_INFO_DISPLAY_NAME,
1101 server_alias);
1103 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1104 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1105 info,
1106 SIPE_BUDDY_INFO_JOB_TITLE,
1107 value);
1109 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1110 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1111 info,
1112 SIPE_BUDDY_INFO_OFFICE,
1113 value);
1115 if (!is_empty(phone_number)) {
1116 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1117 info,
1118 SIPE_BUDDY_INFO_WORK_PHONE,
1119 phone_number);
1121 g_free(phone_number);
1122 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1123 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1124 info,
1125 SIPE_BUDDY_INFO_COMPANY,
1126 value);
1128 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1129 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1130 info,
1131 SIPE_BUDDY_INFO_CITY,
1132 value);
1134 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1135 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1136 info,
1137 SIPE_BUDDY_INFO_STATE,
1138 value);
1140 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1141 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1142 info,
1143 SIPE_BUDDY_INFO_COUNTRY,
1144 value);
1146 if (!is_empty(email)) {
1147 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1148 info,
1149 SIPE_BUDDY_INFO_EMAIL,
1150 email);
1153 sipe_xml_free(searchResults);
1156 /* this will show the minmum information */
1157 get_info_finalize(sipe_private,
1158 info,
1159 uri,
1160 server_alias,
1161 email);
1163 g_free(server_alias);
1164 g_free(email);
1166 return TRUE;
1169 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1170 struct ms_dlx_data *mdd)
1172 /* error using [MS-DLX] server, retry using Active Directory */
1173 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1174 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1176 payload->destroy = g_free;
1177 payload->data = mdd->other;
1178 mdd->other = NULL;
1180 sip_soap_directory_search(sipe_private,
1182 query,
1183 process_get_info_response,
1184 payload);
1186 ms_dlx_free(mdd);
1187 g_free(query);
1190 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1191 const gchar *who)
1193 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1195 if (sipe_private->dlx_uri) {
1196 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1198 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1199 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1201 mdd->other = g_strdup(who);
1202 mdd->max_returns = 1;
1203 mdd->callback = get_info_ab_entry_response;
1204 mdd->failed_callback = get_info_ab_entry_failed;
1205 mdd->session = sipe_svc_session_start();
1207 ms_dlx_webticket_request(sipe_private, mdd);
1209 } else {
1210 /* no [MS-DLX] server, use Active Directory search instead */
1211 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1212 "msRTCSIP-PrimaryUserAddress",
1213 who);
1214 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1216 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1217 row ? row : "");
1219 payload->destroy = g_free;
1220 payload->data = g_strdup(who);
1222 sip_soap_directory_search(sipe_private,
1224 row,
1225 process_get_info_response,
1226 payload);
1227 g_free(row);
1231 static void photo_response_data_free(struct photo_response_data *data)
1233 g_free(data->who);
1234 g_free(data->photo_hash);
1235 if (data->conn) {
1236 http_conn_free(data->conn);
1238 g_free(data);
1241 static void process_buddy_photo_response(int return_code, const char *body,
1242 GSList *headers, SIPE_UNUSED_PARAMETER HttpConn *conn, void *data)
1244 struct photo_response_data *rdata = (struct photo_response_data *)data;
1245 struct sipe_core_private *sipe_private = rdata->sipe_private;
1247 if (return_code == 200) {
1248 const gchar *len_str = sipe_utils_nameval_find(headers, "Content-Length");
1249 if (len_str) {
1250 gsize photo_size = atoi(len_str);
1251 gpointer photo = g_new(char, photo_size);
1253 if (photo) {
1254 memcpy(photo, body, photo_size);
1256 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1257 rdata->who,
1258 photo,
1259 photo_size,
1260 rdata->photo_hash);
1265 sipe_private->pending_photo_requests =
1266 g_slist_remove(sipe_private->pending_photo_requests, rdata);
1267 photo_response_data_free(rdata);
1270 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1272 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1273 gchar *wsse_security_base64;
1274 gchar *x_ms_webticket_header;
1276 if (!assertion) {
1277 return NULL;
1280 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1281 strlen(assertion));
1282 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1283 wsse_security_base64);
1285 g_free(assertion);
1286 g_free(wsse_security_base64);
1288 return x_ms_webticket_header;
1291 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1292 const gchar *uri,
1293 SIPE_UNUSED_PARAMETER const gchar *raw,
1294 sipe_xml *soap_body,
1295 gpointer callback_data)
1297 struct ms_dlx_data *mdd = callback_data;
1298 gchar *photo_rel_path = NULL;
1299 gchar *photo_hash = NULL;
1300 const gchar *photo_hash_old =
1301 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1303 if (soap_body) {
1304 const sipe_xml *node;
1306 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1307 uri);
1309 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1310 node;
1311 node = sipe_xml_twin(node)) {
1312 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1313 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1315 if (!is_empty(value)) {
1316 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1317 g_free(photo_rel_path);
1318 photo_rel_path = value;
1319 value = NULL;
1320 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1321 g_free(photo_hash);
1322 photo_hash = value;
1323 value = NULL;
1327 g_free(value);
1328 g_free(name);
1332 if (sipe_private->addressbook_uri && photo_rel_path &&
1333 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1334 gchar *photo_url = g_strdup_printf("%s/%s",
1335 sipe_private->addressbook_uri, photo_rel_path);
1336 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1338 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1339 data->sipe_private = sipe_private;
1340 data->who = g_strdup(mdd->other);
1341 data->photo_hash = photo_hash;
1342 photo_hash = NULL;
1344 data->conn = http_conn_create(
1345 SIPE_CORE_PUBLIC,
1346 NULL, /* HttpSession */
1347 HTTP_CONN_GET,
1348 HTTP_CONN_SSL,
1349 HTTP_CONN_NO_REDIRECT,
1350 photo_url,
1351 NULL, /* body */
1352 NULL, /* content-type */
1353 x_ms_webticket_header,
1354 NULL, /* auth */
1355 process_buddy_photo_response,
1356 data);
1358 if (data->conn) {
1359 sipe_private->pending_photo_requests =
1360 g_slist_append(sipe_private->pending_photo_requests, data);
1361 } else {
1362 photo_response_data_free(data);
1365 g_free(x_ms_webticket_header);
1366 g_free(photo_url);
1369 g_free(photo_rel_path);
1370 g_free(photo_hash);
1371 ms_dlx_free(mdd);
1374 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1375 struct ms_dlx_data *mdd)
1377 ms_dlx_free(mdd);
1380 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1381 const gchar *uri)
1383 if (sipe_backend_uses_photo() &&
1384 sipe_private->dlx_uri && sipe_private->addressbook_uri) {
1385 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1387 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1388 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1390 mdd->other = g_strdup(uri);
1391 mdd->max_returns = 1;
1392 mdd->callback = get_photo_ab_entry_response;
1393 mdd->failed_callback = get_photo_ab_entry_failed;
1394 mdd->session = sipe_svc_session_start();
1396 ms_dlx_webticket_request(sipe_private, mdd);
1400 static void buddy_refresh_photos_cb(gpointer uri,
1401 SIPE_UNUSED_PARAMETER gpointer value,
1402 gpointer sipe_private)
1404 buddy_fetch_photo(sipe_private, uri);
1407 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1409 g_hash_table_foreach(sipe_private->buddies,
1410 buddy_refresh_photos_cb,
1411 sipe_private);
1414 /* Buddy menu callbacks*/
1416 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1417 const gchar *who)
1419 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1421 /* 2007+ conference */
1422 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1423 sipe_conf_add(sipe_private, who);
1425 /* 2005- multiparty chat */
1426 } else {
1427 gchar *self = sip_uri_self(sipe_private);
1428 struct sip_session *session;
1430 session = sipe_session_add_chat(sipe_private,
1431 NULL,
1432 TRUE,
1433 self);
1434 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1435 session->chat_session,
1436 session->chat_session->title,
1437 self);
1438 g_free(self);
1440 sipe_im_invite(sipe_private, session, who,
1441 NULL, NULL, NULL, FALSE);
1445 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1446 const gchar *who)
1448 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1449 who,
1450 NULL);
1451 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1452 buddy,
1453 SIPE_BUDDY_INFO_EMAIL);
1455 if (email) {
1456 gchar *command_line = g_strdup_printf(
1457 #ifdef _WIN32
1458 "cmd /c start"
1459 #else
1460 "xdg-email"
1461 #endif
1462 " mailto:%s", email);
1463 g_free(email);
1465 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1466 command_line);
1467 g_spawn_command_line_async(command_line, NULL);
1468 g_free(command_line);
1470 } else {
1471 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1472 who);
1476 /* Buddy menu */
1478 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1479 struct sipe_backend_buddy_menu *menu,
1480 sipe_backend_buddy buddy,
1481 sipe_buddy_info_fields id_phone,
1482 sipe_buddy_info_fields id_display,
1483 const gchar *type)
1485 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1486 buddy,
1487 id_phone);
1488 if (phone) {
1489 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1490 buddy,
1491 id_display);
1492 gchar *tmp = NULL;
1493 gchar *label = g_strdup_printf("%s %s",
1494 type,
1495 display ? display :
1496 (tmp = sip_tel_uri_denormalize(phone)));
1497 menu = sipe_backend_buddy_menu_add(sipe_public,
1498 menu,
1499 label,
1500 SIPE_BUDDY_MENU_MAKE_CALL,
1501 phone);
1502 g_free(tmp);
1503 g_free(label);
1504 g_free(display);
1505 g_free(phone);
1508 return(menu);
1511 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1512 const gchar *buddy_name,
1513 struct sipe_backend_buddy_menu *menu)
1515 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1516 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1517 buddy_name,
1518 NULL);
1519 gchar *self = sip_uri_self(sipe_private);
1521 SIPE_SESSION_FOREACH {
1522 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1524 struct sipe_chat_session *chat_session = session->chat_session;
1525 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1527 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1529 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1531 if (is_conf &&
1532 /* Not conf OP */
1533 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1534 /* We are a conf OP */
1535 conf_op) {
1536 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1537 chat_session->title);
1538 menu = sipe_backend_buddy_menu_add(sipe_public,
1539 menu,
1540 label,
1541 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1542 chat_session);
1543 g_free(label);
1546 if (is_conf &&
1547 /* We are a conf OP */
1548 conf_op) {
1549 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1550 chat_session->title);
1551 menu = sipe_backend_buddy_menu_add(sipe_public,
1552 menu,
1553 label,
1554 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1555 chat_session);
1556 g_free(label);
1559 else
1561 if (!is_conf ||
1562 (is_conf && !session->locked)) {
1563 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1564 chat_session->title);
1565 menu = sipe_backend_buddy_menu_add(sipe_public,
1566 menu,
1567 label,
1568 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1569 chat_session);
1570 g_free(label);
1574 } SIPE_SESSION_FOREACH_END;
1575 g_free(self);
1577 menu = sipe_backend_buddy_menu_add(sipe_public,
1578 menu,
1579 _("New chat"),
1580 SIPE_BUDDY_MENU_NEW_CHAT,
1581 NULL);
1583 /* add buddy's phone numbers if we have call control */
1584 if (sip_csta_is_idle(sipe_private)) {
1586 /* work phone */
1587 menu = buddy_menu_phone(sipe_public,
1588 menu,
1589 buddy,
1590 SIPE_BUDDY_INFO_WORK_PHONE,
1591 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1592 _("Work"));
1593 /* mobile phone */
1594 menu = buddy_menu_phone(sipe_public,
1595 menu,
1596 buddy,
1597 SIPE_BUDDY_INFO_MOBILE_PHONE,
1598 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1599 _("Mobile"));
1601 /* home phone */
1602 menu = buddy_menu_phone(sipe_public,
1603 menu,
1604 buddy,
1605 SIPE_BUDDY_INFO_HOME_PHONE,
1606 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1607 _("Home"));
1609 /* other phone */
1610 menu = buddy_menu_phone(sipe_public,
1611 menu,
1612 buddy,
1613 SIPE_BUDDY_INFO_OTHER_PHONE,
1614 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1615 _("Other"));
1617 /* custom1 phone */
1618 menu = buddy_menu_phone(sipe_public,
1619 menu,
1620 buddy,
1621 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1622 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1623 _("Custom1"));
1627 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1628 buddy,
1629 SIPE_BUDDY_INFO_EMAIL);
1630 if (email) {
1631 menu = sipe_backend_buddy_menu_add(sipe_public,
1632 menu,
1633 _("Send email..."),
1634 SIPE_BUDDY_MENU_SEND_EMAIL,
1635 NULL);
1636 g_free(email);
1640 /* access level control */
1641 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1642 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1643 menu,
1644 _("Access level"),
1645 sipe_ocs2007_access_control_menu(sipe_private,
1646 buddy_name));
1648 return(menu);
1652 Local Variables:
1653 mode: c
1654 c-file-style: "bsd"
1655 indent-tabs-mode: t
1656 tab-width: 8
1657 End: