buddy: fix memory leak at sipe_core_buddy_tooltip_info()
[siplcs.git] / src / core / sipe-buddy.c
blobf456f4e143698e6d6f722140b94cb4d8e62d080d
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 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
388 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
389 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
390 note_italics);
391 g_free(note_italics);
393 if (access_text) {
394 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
395 g_free(access_text);
399 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
400 const char *uri,
401 sipe_buddy_info_fields propkey,
402 char *property_value)
404 GSList *buddies, *entry;
406 if (property_value)
407 property_value = g_strstrip(property_value);
409 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
410 while (entry) {
411 gchar *prop_str;
412 sipe_backend_buddy p_buddy = entry->data;
414 /* for Display Name */
415 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
416 gchar *alias;
417 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
418 if (property_value && sipe_is_bad_alias(uri, alias)) {
419 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
420 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
422 g_free(alias);
424 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
425 if (!is_empty(property_value) &&
426 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
428 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
429 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
431 g_free(alias);
433 /* for other properties */
434 else {
435 if (!is_empty(property_value)) {
436 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
437 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
438 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
440 g_free(prop_str);
444 entry = entry->next;
446 g_slist_free(buddies);
450 struct ms_dlx_data;
451 struct ms_dlx_data {
452 GSList *search_rows;
453 gchar *other;
454 guint max_returns;
455 sipe_svc_callback *callback;
456 struct sipe_svc_session *session;
457 gchar *wsse_security;
458 struct sipe_backend_search_token *token;
459 /* must call ms_dlx_free() */
460 void (*failed_callback)(struct sipe_core_private *sipe_private,
461 struct ms_dlx_data *mdd);
464 static void ms_dlx_free(struct ms_dlx_data *mdd)
466 GSList *entry = mdd->search_rows;
467 while (entry) {
468 g_free(entry->data);
469 entry = entry->next;
471 g_slist_free(mdd->search_rows);
472 sipe_svc_session_close(mdd->session);
473 g_free(mdd->other);
474 g_free(mdd->wsse_security);
475 g_free(mdd);
478 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
479 #define DLX_SEARCH_ITEM \
480 "<AbEntryRequest.ChangeSearchQuery>" \
481 " <SearchOn>%s</SearchOn>" \
482 " <Value>%s</Value>" \
483 "</AbEntryRequest.ChangeSearchQuery>"
485 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
486 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
487 guint i = 0;
488 gchar *query = NULL;
490 while (query_rows) {
491 gchar *attr;
492 gchar *value;
494 attr = query_rows->data;
495 query_rows = g_slist_next(query_rows);
496 value = query_rows->data;
497 query_rows = g_slist_next(query_rows);
499 if (!attr || !value)
500 break;
502 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
503 attr, value);
505 attrs[i] = NULL;
507 if (i) {
508 query = g_strjoinv(NULL, attrs);
509 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
510 query ? query : "");
513 g_strfreev(attrs);
515 return query;
518 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
519 const gchar *base_uri,
520 const gchar *auth_uri,
521 const gchar *wsse_security,
522 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
523 gpointer callback_data)
525 struct ms_dlx_data *mdd = callback_data;
527 if (wsse_security) {
528 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
530 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
531 base_uri);
533 if (sipe_svc_ab_entry_request(sipe_private,
534 mdd->session,
535 auth_uri,
536 wsse_security,
537 query,
538 g_slist_length(mdd->search_rows) / 2,
539 mdd->max_returns,
540 mdd->callback,
541 mdd)) {
543 /* keep webticket security token for potential further use */
544 mdd->wsse_security = g_strdup(wsse_security);
546 /* callback data passed down the line */
547 mdd = NULL;
549 g_free(query);
551 } else {
552 /* no ticket: this will show the minmum information */
553 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
554 base_uri);
557 if (mdd)
558 mdd->failed_callback(sipe_private, mdd);
561 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
562 struct ms_dlx_data *mdd)
564 if (!sipe_webticket_request(sipe_private,
565 mdd->session,
566 sipe_private->dlx_uri,
567 "AddressBookWebTicketBearer",
568 ms_dlx_webticket,
569 mdd)) {
570 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
571 sipe_private->dlx_uri);
572 mdd->failed_callback(sipe_private, mdd);
576 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
577 struct sipe_backend_search_results *results,
578 guint match_count,
579 gboolean more)
581 gchar *secondary = g_strdup_printf(
582 dngettext(PACKAGE_NAME,
583 "Found %d contact%s:",
584 "Found %d contacts%s:", match_count),
585 match_count, more ? _(" (more matched your query)") : "");
587 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
588 results,
589 secondary,
590 more);
591 g_free(secondary);
594 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
595 const gchar *uri,
596 SIPE_UNUSED_PARAMETER const gchar *raw,
597 sipe_xml *soap_body,
598 gpointer callback_data)
600 struct ms_dlx_data *mdd = callback_data;
602 if (soap_body) {
603 const sipe_xml *node;
604 struct sipe_backend_search_results *results;
605 GHashTable *found;
607 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
608 uri);
610 /* any matches? */
611 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
612 if (!node) {
613 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
614 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
615 mdd->token,
616 _("No contacts found"));
617 ms_dlx_free(mdd);
618 return;
621 /* OK, we found something - show the results to the user */
622 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
623 mdd->token);
624 if (!results) {
625 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
626 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
627 mdd->token,
628 _("Unable to display the search results"));
629 ms_dlx_free(mdd);
630 return;
633 /* SearchAbEntryResult can contain duplicates */
634 found = g_hash_table_new_full(g_str_hash, g_str_equal,
635 g_free, NULL);
637 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
638 const sipe_xml *attrs;
639 gchar *sip_uri = NULL;
640 gchar *displayname = NULL;
641 gchar *company = NULL;
642 gchar *country = NULL;
643 gchar *email = NULL;
645 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
646 attrs;
647 attrs = sipe_xml_twin(attrs)) {
648 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
649 "Name"));
650 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
651 "Value"));
653 if (!is_empty(value)) {
654 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
655 g_free(sip_uri);
656 sip_uri = value;
657 value = NULL;
658 } else if (sipe_strcase_equal(name, "displayname")) {
659 g_free(displayname);
660 displayname = value;
661 value = NULL;
662 } else if (sipe_strcase_equal(name, "mail")) {
663 g_free(email);
664 email = value;
665 value = NULL;
666 } else if (sipe_strcase_equal(name, "company")) {
667 g_free(company);
668 company = value;
669 value = NULL;
670 } else if (sipe_strcase_equal(name, "country")) {
671 g_free(country);
672 country = value;
673 value = NULL;
677 g_free(value);
678 g_free(name);
681 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
682 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
683 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
684 results,
685 uri_parts[1],
686 displayname,
687 company,
688 country,
689 email);
690 g_strfreev(uri_parts);
692 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
693 sip_uri = NULL;
696 g_free(email);
697 g_free(country);
698 g_free(company);
699 g_free(displayname);
700 g_free(sip_uri);
703 search_contacts_finalize(sipe_private, results,
704 g_hash_table_size(found),
705 FALSE);
706 g_hash_table_destroy(found);
707 ms_dlx_free(mdd);
709 } else {
710 mdd->failed_callback(sipe_private, mdd);
714 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
715 struct sipmsg *msg,
716 struct transaction *trans)
718 struct sipe_backend_search_token *token = trans->payload->data;
719 struct sipe_backend_search_results *results;
720 sipe_xml *searchResults;
721 const sipe_xml *mrow;
722 guint match_count = 0;
723 gboolean more = FALSE;
725 /* valid response? */
726 if (msg->response != 200) {
727 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
728 msg->response);
729 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
730 token,
731 _("Contact search failed"));
732 return(FALSE);
735 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
737 /* valid XML? */
738 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
739 if (!searchResults) {
740 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
741 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
742 token,
743 _("Contact search failed"));
744 return(FALSE);
747 /* any matches? */
748 mrow = sipe_xml_child(searchResults, "Body/Array/row");
749 if (!mrow) {
750 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
751 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
752 token,
753 _("No contacts found"));
755 sipe_xml_free(searchResults);
756 return(FALSE);
759 /* OK, we found something - show the results to the user */
760 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
761 trans->payload->data);
762 if (!results) {
763 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
764 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
765 token,
766 _("Unable to display the search results"));
768 sipe_xml_free(searchResults);
769 return FALSE;
772 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
773 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
774 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
775 results,
776 uri_parts[1],
777 sipe_xml_attribute(mrow, "displayName"),
778 sipe_xml_attribute(mrow, "company"),
779 sipe_xml_attribute(mrow, "country"),
780 sipe_xml_attribute(mrow, "email"));
781 g_strfreev(uri_parts);
782 match_count++;
785 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
786 char *data = sipe_xml_data(mrow);
787 more = (g_ascii_strcasecmp(data, "true") == 0);
788 g_free(data);
791 search_contacts_finalize(sipe_private, results, match_count, more);
792 sipe_xml_free(searchResults);
794 return(TRUE);
797 static void search_soap_request(struct sipe_core_private *sipe_private,
798 struct sipe_backend_search_token *token,
799 GSList *search_rows)
801 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
802 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
804 payload->data = token;
806 sip_soap_directory_search(sipe_private,
807 100,
808 query,
809 process_search_contact_response,
810 payload);
811 g_free(query);
814 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
815 struct ms_dlx_data *mdd)
817 /* error using [MS-DLX] server, retry using Active Directory */
818 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
819 ms_dlx_free(mdd);
822 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
823 struct sipe_backend_search_token *token,
824 const gchar *given_name,
825 const gchar *surname,
826 const gchar *email,
827 const gchar *company,
828 const gchar *country)
830 GSList *query_rows = NULL;
832 #define ADD_QUERY_ROW(attr, val) \
833 if (val) { \
834 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
835 query_rows = g_slist_append(query_rows, g_strdup(val)); \
838 ADD_QUERY_ROW("givenName", given_name);
839 ADD_QUERY_ROW("sn", surname);
840 ADD_QUERY_ROW("mail", email);
841 ADD_QUERY_ROW("company", company);
842 ADD_QUERY_ROW("c", country);
844 if (query_rows) {
845 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
846 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
848 mdd->search_rows = query_rows;
849 mdd->max_returns = 100;
850 mdd->callback = search_ab_entry_response;
851 mdd->failed_callback = search_ab_entry_failed;
852 mdd->session = sipe_svc_session_start();
853 mdd->token = token;
855 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
857 } else {
858 /* no [MS-DLX] server, use Active Directory search instead */
859 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
860 g_slist_free(query_rows);
862 } else
863 sipe_backend_search_failed(sipe_public,
864 token,
865 _("Invalid contact search query"));
868 static void get_info_finalize(struct sipe_core_private *sipe_private,
869 struct sipe_backend_buddy_info *info,
870 const gchar *uri,
871 const gchar *server_alias,
872 const gchar *email)
874 sipe_backend_buddy bbuddy;
875 struct sipe_buddy *sbuddy;
876 gchar *alias;
877 gchar *value;
879 if (!info) {
880 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
881 } else {
882 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
884 if (!info)
885 return;
887 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
889 if (is_empty(server_alias)) {
890 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
891 bbuddy);
892 if (value) {
893 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
894 info,
895 SIPE_BUDDY_INFO_DISPLAY_NAME,
896 value);
898 } else {
899 value = g_strdup(server_alias);
902 /* present alias if it differs from server alias */
903 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
904 if (alias && !sipe_strequal(alias, value))
906 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
907 info,
908 SIPE_BUDDY_INFO_ALIAS,
909 alias);
911 g_free(alias);
912 g_free(value);
914 if (is_empty(email)) {
915 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
916 bbuddy,
917 SIPE_BUDDY_INFO_EMAIL);
918 if (value) {
919 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
920 info,
921 SIPE_BUDDY_INFO_EMAIL,
922 value);
923 g_free(value);
927 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
928 bbuddy,
929 SIPE_BUDDY_INFO_SITE);
930 if (value) {
931 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
932 info,
933 SIPE_BUDDY_INFO_SITE,
934 value);
935 g_free(value);
938 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
939 if (sbuddy && sbuddy->device_name) {
940 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
941 info,
942 SIPE_BUDDY_INFO_DEVICE,
943 sbuddy->device_name);
946 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
950 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
951 const gchar *uri,
952 SIPE_UNUSED_PARAMETER const gchar *raw,
953 sipe_xml *soap_body,
954 gpointer callback_data)
956 struct ms_dlx_data *mdd = callback_data;
957 struct sipe_backend_buddy_info *info = NULL;
958 gchar *server_alias = NULL;
959 gchar *email = NULL;
961 if (soap_body) {
962 const sipe_xml *node;
964 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
965 uri);
967 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
969 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
970 node;
971 node = sipe_xml_twin(node)) {
972 gchar *name = sipe_xml_data(sipe_xml_child(node,
973 "Name"));
974 gchar *value = sipe_xml_data(sipe_xml_child(node,
975 "Value"));
976 const sipe_xml *values = sipe_xml_child(node,
977 "Values");
979 /* Single value entries */
980 if (!is_empty(value)) {
982 if (sipe_strcase_equal(name, "displayname")) {
983 g_free(server_alias);
984 server_alias = value;
985 value = NULL;
986 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
987 info,
988 SIPE_BUDDY_INFO_DISPLAY_NAME,
989 server_alias);
990 } else if (sipe_strcase_equal(name, "mail")) {
991 g_free(email);
992 email = value;
993 value = NULL;
994 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
995 info,
996 SIPE_BUDDY_INFO_EMAIL,
997 email);
998 } else if (sipe_strcase_equal(name, "title")) {
999 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1000 info,
1001 SIPE_BUDDY_INFO_JOB_TITLE,
1002 value);
1003 } else if (sipe_strcase_equal(name, "company")) {
1004 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1005 info,
1006 SIPE_BUDDY_INFO_COMPANY,
1007 value);
1008 } else if (sipe_strcase_equal(name, "country")) {
1009 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1010 info,
1011 SIPE_BUDDY_INFO_COUNTRY,
1012 value);
1015 } else if (values) {
1016 gchar *first = sipe_xml_data(sipe_xml_child(values,
1017 "string"));
1019 if (sipe_strcase_equal(name, "telephonenumber")) {
1020 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1021 info,
1022 SIPE_BUDDY_INFO_WORK_PHONE,
1023 first);
1026 g_free(first);
1029 g_free(value);
1030 g_free(name);
1034 /* this will show the minmum information */
1035 get_info_finalize(sipe_private,
1036 info,
1037 mdd->other,
1038 server_alias,
1039 email);
1041 g_free(email);
1042 g_free(server_alias);
1043 ms_dlx_free(mdd);
1046 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1047 struct sipmsg *msg,
1048 struct transaction *trans)
1050 const gchar *uri = trans->payload->data;
1051 struct sipe_backend_buddy_info *info = NULL;
1052 gchar *server_alias = NULL;
1053 gchar *email = NULL;
1055 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1056 uri, sipe_private->username);
1058 if (msg->response != 200) {
1059 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1060 } else {
1061 sipe_xml *searchResults;
1062 const sipe_xml *mrow;
1064 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1065 msg->body ? msg->body : "");
1067 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1068 if (!searchResults) {
1070 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1072 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1073 const gchar *value;
1074 gchar *phone_number;
1076 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1078 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1079 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1080 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1083 * For 2007 system we will take this from ContactCard -
1084 * it has cleaner tel: URIs at least
1086 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1087 char *tel_uri = sip_to_tel_uri(phone_number);
1088 /* trims its parameters, so call first */
1089 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1090 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1091 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1092 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1093 g_free(tel_uri);
1095 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1096 uri);
1099 if (!is_empty(server_alias)) {
1100 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1101 info,
1102 SIPE_BUDDY_INFO_DISPLAY_NAME,
1103 server_alias);
1105 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1106 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1107 info,
1108 SIPE_BUDDY_INFO_JOB_TITLE,
1109 value);
1111 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1112 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1113 info,
1114 SIPE_BUDDY_INFO_OFFICE,
1115 value);
1117 if (!is_empty(phone_number)) {
1118 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1119 info,
1120 SIPE_BUDDY_INFO_WORK_PHONE,
1121 phone_number);
1123 g_free(phone_number);
1124 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1125 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1126 info,
1127 SIPE_BUDDY_INFO_COMPANY,
1128 value);
1130 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1131 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1132 info,
1133 SIPE_BUDDY_INFO_CITY,
1134 value);
1136 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1137 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1138 info,
1139 SIPE_BUDDY_INFO_STATE,
1140 value);
1142 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1143 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1144 info,
1145 SIPE_BUDDY_INFO_COUNTRY,
1146 value);
1148 if (!is_empty(email)) {
1149 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1150 info,
1151 SIPE_BUDDY_INFO_EMAIL,
1152 email);
1155 sipe_xml_free(searchResults);
1158 /* this will show the minmum information */
1159 get_info_finalize(sipe_private,
1160 info,
1161 uri,
1162 server_alias,
1163 email);
1165 g_free(server_alias);
1166 g_free(email);
1168 return TRUE;
1171 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1172 struct ms_dlx_data *mdd)
1174 /* error using [MS-DLX] server, retry using Active Directory */
1175 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1176 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1178 payload->destroy = g_free;
1179 payload->data = mdd->other;
1180 mdd->other = NULL;
1182 sip_soap_directory_search(sipe_private,
1184 query,
1185 process_get_info_response,
1186 payload);
1188 ms_dlx_free(mdd);
1189 g_free(query);
1192 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1193 const gchar *who)
1195 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1197 if (sipe_private->dlx_uri) {
1198 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1200 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1201 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1203 mdd->other = g_strdup(who);
1204 mdd->max_returns = 1;
1205 mdd->callback = get_info_ab_entry_response;
1206 mdd->failed_callback = get_info_ab_entry_failed;
1207 mdd->session = sipe_svc_session_start();
1209 ms_dlx_webticket_request(sipe_private, mdd);
1211 } else {
1212 /* no [MS-DLX] server, use Active Directory search instead */
1213 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1214 "msRTCSIP-PrimaryUserAddress",
1215 who);
1216 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1218 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1219 row ? row : "");
1221 payload->destroy = g_free;
1222 payload->data = g_strdup(who);
1224 sip_soap_directory_search(sipe_private,
1226 row,
1227 process_get_info_response,
1228 payload);
1229 g_free(row);
1233 static void photo_response_data_free(struct photo_response_data *data)
1235 g_free(data->who);
1236 g_free(data->photo_hash);
1237 if (data->conn) {
1238 http_conn_free(data->conn);
1240 g_free(data);
1243 static void process_buddy_photo_response(int return_code, const char *body,
1244 GSList *headers, SIPE_UNUSED_PARAMETER HttpConn *conn, void *data)
1246 struct photo_response_data *rdata = (struct photo_response_data *)data;
1247 struct sipe_core_private *sipe_private = rdata->sipe_private;
1249 if (return_code == 200) {
1250 const gchar *len_str = sipe_utils_nameval_find(headers, "Content-Length");
1251 if (len_str) {
1252 gsize photo_size = atoi(len_str);
1253 gpointer photo = g_new(char, photo_size);
1255 if (photo) {
1256 memcpy(photo, body, photo_size);
1258 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1259 rdata->who,
1260 photo,
1261 photo_size,
1262 rdata->photo_hash);
1267 sipe_private->pending_photo_requests =
1268 g_slist_remove(sipe_private->pending_photo_requests, rdata);
1270 /* Mark connection for close and let it be freed at http_conn_input(). */
1271 http_conn_set_close(rdata->conn);
1272 rdata->conn = NULL;
1274 photo_response_data_free(rdata);
1277 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1279 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1280 gchar *wsse_security_base64;
1281 gchar *x_ms_webticket_header;
1283 if (!assertion) {
1284 return NULL;
1287 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1288 strlen(assertion));
1289 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1290 wsse_security_base64);
1292 g_free(assertion);
1293 g_free(wsse_security_base64);
1295 return x_ms_webticket_header;
1298 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1299 const gchar *uri,
1300 SIPE_UNUSED_PARAMETER const gchar *raw,
1301 sipe_xml *soap_body,
1302 gpointer callback_data)
1304 struct ms_dlx_data *mdd = callback_data;
1305 gchar *photo_rel_path = NULL;
1306 gchar *photo_hash = NULL;
1307 const gchar *photo_hash_old =
1308 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1310 if (soap_body) {
1311 const sipe_xml *node;
1313 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1314 uri);
1316 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1317 node;
1318 node = sipe_xml_twin(node)) {
1319 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1320 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1322 if (!is_empty(value)) {
1323 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1324 g_free(photo_rel_path);
1325 photo_rel_path = value;
1326 value = NULL;
1327 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1328 g_free(photo_hash);
1329 photo_hash = value;
1330 value = NULL;
1334 g_free(value);
1335 g_free(name);
1339 if (sipe_private->addressbook_uri && photo_rel_path &&
1340 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1341 gchar *photo_url = g_strdup_printf("%s/%s",
1342 sipe_private->addressbook_uri, photo_rel_path);
1343 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1345 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1346 data->sipe_private = sipe_private;
1347 data->who = g_strdup(mdd->other);
1348 data->photo_hash = photo_hash;
1349 photo_hash = NULL;
1351 data->conn = http_conn_create(
1352 SIPE_CORE_PUBLIC,
1353 NULL, /* HttpSession */
1354 HTTP_CONN_GET,
1355 HTTP_CONN_SSL,
1356 HTTP_CONN_NO_REDIRECT,
1357 photo_url,
1358 NULL, /* body */
1359 NULL, /* content-type */
1360 x_ms_webticket_header,
1361 NULL, /* auth */
1362 process_buddy_photo_response,
1363 data);
1365 if (data->conn) {
1366 sipe_private->pending_photo_requests =
1367 g_slist_append(sipe_private->pending_photo_requests, data);
1368 } else {
1369 photo_response_data_free(data);
1372 g_free(x_ms_webticket_header);
1373 g_free(photo_url);
1376 g_free(photo_rel_path);
1377 g_free(photo_hash);
1378 ms_dlx_free(mdd);
1381 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1382 struct ms_dlx_data *mdd)
1384 ms_dlx_free(mdd);
1387 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1388 const gchar *uri)
1390 if (sipe_backend_uses_photo() &&
1391 sipe_private->dlx_uri && sipe_private->addressbook_uri) {
1392 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1394 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1395 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1397 mdd->other = g_strdup(uri);
1398 mdd->max_returns = 1;
1399 mdd->callback = get_photo_ab_entry_response;
1400 mdd->failed_callback = get_photo_ab_entry_failed;
1401 mdd->session = sipe_svc_session_start();
1403 ms_dlx_webticket_request(sipe_private, mdd);
1407 static void buddy_refresh_photos_cb(gpointer uri,
1408 SIPE_UNUSED_PARAMETER gpointer value,
1409 gpointer sipe_private)
1411 buddy_fetch_photo(sipe_private, uri);
1414 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1416 g_hash_table_foreach(sipe_private->buddies,
1417 buddy_refresh_photos_cb,
1418 sipe_private);
1421 /* Buddy menu callbacks*/
1423 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1424 const gchar *who)
1426 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1428 /* 2007+ conference */
1429 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1430 sipe_conf_add(sipe_private, who);
1432 /* 2005- multiparty chat */
1433 } else {
1434 gchar *self = sip_uri_self(sipe_private);
1435 struct sip_session *session;
1437 session = sipe_session_add_chat(sipe_private,
1438 NULL,
1439 TRUE,
1440 self);
1441 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1442 session->chat_session,
1443 session->chat_session->title,
1444 self);
1445 g_free(self);
1447 sipe_im_invite(sipe_private, session, who,
1448 NULL, NULL, NULL, FALSE);
1452 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1453 const gchar *who)
1455 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1456 who,
1457 NULL);
1458 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1459 buddy,
1460 SIPE_BUDDY_INFO_EMAIL);
1462 if (email) {
1463 gchar *command_line = g_strdup_printf(
1464 #ifdef _WIN32
1465 "cmd /c start"
1466 #else
1467 "xdg-email"
1468 #endif
1469 " mailto:%s", email);
1470 g_free(email);
1472 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1473 command_line);
1474 g_spawn_command_line_async(command_line, NULL);
1475 g_free(command_line);
1477 } else {
1478 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1479 who);
1483 /* Buddy menu */
1485 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1486 struct sipe_backend_buddy_menu *menu,
1487 sipe_backend_buddy buddy,
1488 sipe_buddy_info_fields id_phone,
1489 sipe_buddy_info_fields id_display,
1490 const gchar *type)
1492 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1493 buddy,
1494 id_phone);
1495 if (phone) {
1496 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1497 buddy,
1498 id_display);
1499 gchar *tmp = NULL;
1500 gchar *label = g_strdup_printf("%s %s",
1501 type,
1502 display ? display :
1503 (tmp = sip_tel_uri_denormalize(phone)));
1504 menu = sipe_backend_buddy_menu_add(sipe_public,
1505 menu,
1506 label,
1507 SIPE_BUDDY_MENU_MAKE_CALL,
1508 phone);
1509 g_free(tmp);
1510 g_free(label);
1511 g_free(display);
1512 g_free(phone);
1515 return(menu);
1518 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1519 const gchar *buddy_name,
1520 struct sipe_backend_buddy_menu *menu)
1522 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1523 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1524 buddy_name,
1525 NULL);
1526 gchar *self = sip_uri_self(sipe_private);
1528 SIPE_SESSION_FOREACH {
1529 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1531 struct sipe_chat_session *chat_session = session->chat_session;
1532 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1534 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1536 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1538 if (is_conf &&
1539 /* Not conf OP */
1540 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1541 /* We are a conf OP */
1542 conf_op) {
1543 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1544 chat_session->title);
1545 menu = sipe_backend_buddy_menu_add(sipe_public,
1546 menu,
1547 label,
1548 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1549 chat_session);
1550 g_free(label);
1553 if (is_conf &&
1554 /* We are a conf OP */
1555 conf_op) {
1556 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1557 chat_session->title);
1558 menu = sipe_backend_buddy_menu_add(sipe_public,
1559 menu,
1560 label,
1561 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1562 chat_session);
1563 g_free(label);
1566 else
1568 if (!is_conf ||
1569 (is_conf && !session->locked)) {
1570 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1571 chat_session->title);
1572 menu = sipe_backend_buddy_menu_add(sipe_public,
1573 menu,
1574 label,
1575 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1576 chat_session);
1577 g_free(label);
1581 } SIPE_SESSION_FOREACH_END;
1582 g_free(self);
1584 menu = sipe_backend_buddy_menu_add(sipe_public,
1585 menu,
1586 _("New chat"),
1587 SIPE_BUDDY_MENU_NEW_CHAT,
1588 NULL);
1590 /* add buddy's phone numbers if we have call control */
1591 if (sip_csta_is_idle(sipe_private)) {
1593 /* work phone */
1594 menu = buddy_menu_phone(sipe_public,
1595 menu,
1596 buddy,
1597 SIPE_BUDDY_INFO_WORK_PHONE,
1598 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1599 _("Work"));
1600 /* mobile phone */
1601 menu = buddy_menu_phone(sipe_public,
1602 menu,
1603 buddy,
1604 SIPE_BUDDY_INFO_MOBILE_PHONE,
1605 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1606 _("Mobile"));
1608 /* home phone */
1609 menu = buddy_menu_phone(sipe_public,
1610 menu,
1611 buddy,
1612 SIPE_BUDDY_INFO_HOME_PHONE,
1613 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1614 _("Home"));
1616 /* other phone */
1617 menu = buddy_menu_phone(sipe_public,
1618 menu,
1619 buddy,
1620 SIPE_BUDDY_INFO_OTHER_PHONE,
1621 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1622 _("Other"));
1624 /* custom1 phone */
1625 menu = buddy_menu_phone(sipe_public,
1626 menu,
1627 buddy,
1628 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1629 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1630 _("Custom1"));
1634 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1635 buddy,
1636 SIPE_BUDDY_INFO_EMAIL);
1637 if (email) {
1638 menu = sipe_backend_buddy_menu_add(sipe_public,
1639 menu,
1640 _("Send email..."),
1641 SIPE_BUDDY_MENU_SEND_EMAIL,
1642 NULL);
1643 g_free(email);
1647 /* access level control */
1648 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1649 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1650 menu,
1651 _("Access level"),
1652 sipe_ocs2007_access_control_menu(sipe_private,
1653 buddy_name));
1655 return(menu);
1659 Local Variables:
1660 mode: c
1661 c-file-style: "bsd"
1662 indent-tabs-mode: t
1663 tab-width: 8
1664 End: