utils: fix memory leak in sorted GSList insert
[siplcs.git] / src / core / sipe-buddy.c
blob451177272358e5e50d81acd2c5ab420af76317de
1 /**
2 * @file sipe-buddy.c
4 * pidgin-sipe
6 * Copyright (C) 2010-2013 SIPE Project <http://sipe.sourceforge.net/>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
31 #include <glib.h>
33 #include "sipe-common.h"
34 #include "sipmsg.h"
35 #include "sip-csta.h"
36 #include "sip-soap.h"
37 #include "sip-transport.h"
38 #include "sipe-backend.h"
39 #include "sipe-buddy.h"
40 #include "sipe-cal.h"
41 #include "sipe-chat.h"
42 #include "sipe-conf.h"
43 #include "sipe-core.h"
44 #include "sipe-core-private.h"
45 #include "sipe-group.h"
46 #include "sipe-http.h"
47 #include "sipe-im.h"
48 #include "sipe-nls.h"
49 #include "sipe-ocs2005.h"
50 #include "sipe-ocs2007.h"
51 #include "sipe-schedule.h"
52 #include "sipe-session.h"
53 #include "sipe-status.h"
54 #include "sipe-subscriptions.h"
55 #include "sipe-svc.h"
56 #include "sipe-utils.h"
57 #include "sipe-webticket.h"
58 #include "sipe-xml.h"
60 struct photo_response_data {
61 gchar *who;
62 gchar *photo_hash;
63 struct sipe_http_request *request;
66 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
67 const gchar *uri);
68 static void photo_response_data_free(struct photo_response_data *data);
70 struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private,
71 const gchar *uri)
73 struct sipe_buddy *buddy = g_hash_table_lookup(sipe_private->buddies, uri);
74 if (!buddy) {
75 buddy = g_new0(struct sipe_buddy, 1);
76 buddy->name = g_strdup(uri);
77 g_hash_table_insert(sipe_private->buddies, buddy->name, buddy);
79 SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", uri);
81 buddy_fetch_photo(sipe_private, uri);
82 } else {
83 SIPE_DEBUG_INFO("sipe_buddy_add: Buddy %s already exists", uri);
86 return buddy;
89 static void buddy_free(struct sipe_buddy *buddy)
91 #ifndef _WIN32
93 * We are calling g_hash_table_foreach_steal(). That means that no
94 * key/value deallocation functions are called. Therefore the glib
95 * hash code does not touch the key (buddy->name) or value (buddy)
96 * of the to-be-deleted hash node at all. It follows that we
98 * - MUST free the memory for the key ourselves and
99 * - ARE allowed to do it in this function
101 * Conclusion: glib must be broken on the Windows platform if sipe
102 * crashes with SIGTRAP when closing. You'll have to live
103 * with the memory leak until this is fixed.
105 g_free(buddy->name);
106 #endif
107 g_free(buddy->activity);
108 g_free(buddy->meeting_subject);
109 g_free(buddy->meeting_location);
110 g_free(buddy->note);
112 g_free(buddy->cal_start_time);
113 g_free(buddy->cal_free_busy_base64);
114 g_free(buddy->cal_free_busy);
115 g_free(buddy->last_non_cal_activity);
117 sipe_cal_free_working_hours(buddy->cal_working_hours);
119 g_free(buddy->device_name);
120 g_slist_free(buddy->groups);
121 g_free(buddy);
124 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
125 gpointer buddy,
126 SIPE_UNUSED_PARAMETER gpointer user_data)
128 buddy_free(buddy);
129 /* We must return TRUE as the key/value have already been deleted */
130 return(TRUE);
133 void sipe_buddy_free_all(struct sipe_core_private *sipe_private)
135 g_hash_table_foreach_steal(sipe_private->buddies,
136 buddy_free_cb,
137 NULL);
139 /* core is being deallocated, remove all its pending photo requests */
140 while (sipe_private->pending_photo_requests) {
141 struct photo_response_data *data =
142 sipe_private->pending_photo_requests->data;
143 sipe_private->pending_photo_requests =
144 g_slist_remove(sipe_private->pending_photo_requests, data);
145 photo_response_data_free(data);
149 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
150 const gchar *uri,
151 guint activity,
152 const gchar *status_text)
154 struct sipe_buddy *sbuddy;
155 const char *activity_str;
157 if (!sipe_public) return NULL; /* happens on pidgin exit */
159 sbuddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, uri);
160 if (!sbuddy) return NULL;
162 activity_str = sbuddy->activity ? sbuddy->activity :
163 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
164 status_text : NULL;
166 if (activity_str && sbuddy->note) {
167 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
168 } else if (activity_str) {
169 return g_strdup(activity_str);
170 } else if (sbuddy->note) {
171 return g_strdup_printf("<i>%s</i>", sbuddy->note);
172 } else {
173 return NULL;
177 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
178 const gchar *with)
180 sipe_backend_buddy pbuddy;
181 gchar *alias = NULL;
182 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
183 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
185 return alias;
188 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
189 const gchar *who,
190 const gchar *old_group_name,
191 const gchar *new_group_name)
193 struct sipe_buddy * buddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, who);
194 struct sipe_group * old_group = NULL;
195 struct sipe_group * new_group;
197 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
198 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
200 if(!buddy) { // buddy not in roaming list
201 return;
204 if (old_group_name) {
205 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
207 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
209 if (old_group) {
210 buddy->groups = g_slist_remove(buddy->groups, old_group);
211 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
214 if (!new_group) {
215 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
216 } else {
217 buddy->groups = sipe_utils_slist_insert_unique_sorted(buddy->groups,
218 new_group,
219 (GCompareFunc)sipe_group_compare,
220 NULL);
221 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
225 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
226 const gchar *uri,
227 const gchar *group_name)
229 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
231 if (!g_hash_table_lookup(sipe_private->buddies, uri)) {
232 struct sipe_buddy *b = sipe_buddy_add(sipe_private, uri);
233 b->just_added = TRUE;
235 /* @TODO should go to callback */
236 sipe_subscribe_presence_single(sipe_private, b->name);
238 } else {
239 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
240 uri);
243 sipe_core_buddy_group(sipe_public,
244 uri,
245 NULL,
246 group_name);
249 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
250 struct sipe_buddy *buddy)
252 gchar *action_name = sipe_utils_presence_key(buddy->name);
253 sipe_schedule_cancel(sipe_private, action_name);
254 g_free(action_name);
256 g_hash_table_remove(sipe_private->buddies, buddy->name);
258 buddy_free(buddy);
262 * Unassociates buddy from group first.
263 * Then see if no groups left, removes buddy completely.
264 * Otherwise updates buddy groups on server.
266 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
267 const gchar *uri,
268 const gchar *group_name)
270 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
271 struct sipe_buddy *b = g_hash_table_lookup(sipe_private->buddies,
272 uri);
274 if (!b) return;
276 if (group_name) {
277 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
278 group_name);
279 if (g) {
280 b->groups = g_slist_remove(b->groups, g);
281 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
282 uri, g->name);
286 if (g_slist_length(b->groups) < 1) {
287 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
288 b->name);
289 sip_soap_request(sipe_private,
290 "deleteContact",
291 request);
292 g_free(request);
293 sipe_buddy_remove(sipe_private, b);
294 } else {
295 /* updates groups on server */
296 sipe_group_update_buddy(sipe_private, b);
301 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
302 const gchar *uri,
303 guint activity)
305 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
306 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies,
307 uri);
309 if (!sbuddy) return;
311 /* Check if on 2005 system contact's calendar,
312 * then set/preserve it.
314 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
315 sipe_backend_buddy_set_status(sipe_public, uri, activity);
316 } else {
317 sipe_ocs2005_apply_calendar_status(sipe_private,
318 sbuddy,
319 sipe_status_activity_to_token(activity));
323 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
324 const gchar *uri,
325 const gchar *status_name,
326 gboolean is_online,
327 struct sipe_backend_buddy_tooltip *tooltip)
329 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
330 gchar *note = NULL;
331 gboolean is_oof_note = FALSE;
332 const gchar *activity = NULL;
333 gchar *calendar = NULL;
334 const gchar *meeting_subject = NULL;
335 const gchar *meeting_location = NULL;
336 gchar *access_text = NULL;
338 #define SIPE_ADD_BUDDY_INFO(l, t) \
340 gchar *tmp = g_markup_escape_text((t), -1); \
341 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
342 g_free(tmp); \
344 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
345 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
347 if (sipe_public) { /* happens on pidgin exit */
348 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
349 if (sbuddy) {
350 note = sbuddy->note;
351 is_oof_note = sbuddy->is_oof_note;
352 activity = sbuddy->activity;
353 calendar = sipe_cal_get_description(sbuddy);
354 meeting_subject = sbuddy->meeting_subject;
355 meeting_location = sbuddy->meeting_location;
357 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
358 gboolean is_group_access = FALSE;
359 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
360 "user",
361 sipe_get_no_sip_uri(uri),
362 &is_group_access);
363 const char *access_level = sipe_ocs2007_access_level_name(container_id);
364 access_text = is_group_access ?
365 g_strdup(access_level) :
366 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
367 access_level);
371 if (is_online) {
372 const gchar *status_str = activity ? activity : status_name;
374 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
376 if (is_online && !is_empty(calendar)) {
377 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
379 g_free(calendar);
380 if (!is_empty(meeting_location)) {
381 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
382 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
384 if (!is_empty(meeting_subject)) {
385 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
386 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
388 if (note) {
389 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
390 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
391 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
392 note_italics);
393 g_free(note_italics);
395 if (access_text) {
396 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
397 g_free(access_text);
401 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
402 const char *uri,
403 sipe_buddy_info_fields propkey,
404 char *property_value)
406 GSList *buddies, *entry;
408 if (property_value)
409 property_value = g_strstrip(property_value);
411 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
412 while (entry) {
413 gchar *prop_str;
414 sipe_backend_buddy p_buddy = entry->data;
416 /* for Display Name */
417 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
418 gchar *alias;
419 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
420 if (property_value && sipe_is_bad_alias(uri, alias)) {
421 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
422 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
424 g_free(alias);
426 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
427 if (!is_empty(property_value) &&
428 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
430 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
431 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
433 g_free(alias);
435 /* for other properties */
436 else {
437 if (!is_empty(property_value)) {
438 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
439 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
440 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
442 g_free(prop_str);
446 entry = entry->next;
448 g_slist_free(buddies);
452 struct ms_dlx_data;
453 struct ms_dlx_data {
454 GSList *search_rows;
455 gchar *other;
456 guint max_returns;
457 sipe_svc_callback *callback;
458 struct sipe_svc_session *session;
459 gchar *wsse_security;
460 struct sipe_backend_search_token *token;
461 /* must call ms_dlx_free() */
462 void (*failed_callback)(struct sipe_core_private *sipe_private,
463 struct ms_dlx_data *mdd);
466 static void ms_dlx_free(struct ms_dlx_data *mdd)
468 GSList *entry = mdd->search_rows;
469 while (entry) {
470 g_free(entry->data);
471 entry = entry->next;
473 g_slist_free(mdd->search_rows);
474 sipe_svc_session_close(mdd->session);
475 g_free(mdd->other);
476 g_free(mdd->wsse_security);
477 g_free(mdd);
480 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
481 #define DLX_SEARCH_ITEM \
482 "<AbEntryRequest.ChangeSearchQuery>" \
483 " <SearchOn>%s</SearchOn>" \
484 " <Value>%s</Value>" \
485 "</AbEntryRequest.ChangeSearchQuery>"
487 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
488 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
489 guint i = 0;
490 gchar *query = NULL;
492 while (query_rows) {
493 gchar *attr;
494 gchar *value;
496 attr = query_rows->data;
497 query_rows = g_slist_next(query_rows);
498 value = query_rows->data;
499 query_rows = g_slist_next(query_rows);
501 if (!attr || !value)
502 break;
504 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
505 attr, value);
507 attrs[i] = NULL;
509 if (i) {
510 query = g_strjoinv(NULL, attrs);
511 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
512 query ? query : "");
515 g_strfreev(attrs);
517 return query;
520 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
521 const gchar *base_uri,
522 const gchar *auth_uri,
523 const gchar *wsse_security,
524 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
525 gpointer callback_data)
527 struct ms_dlx_data *mdd = callback_data;
529 if (wsse_security) {
530 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
532 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
533 base_uri);
535 if (sipe_svc_ab_entry_request(sipe_private,
536 mdd->session,
537 auth_uri,
538 wsse_security,
539 query,
540 g_slist_length(mdd->search_rows) / 2,
541 mdd->max_returns,
542 mdd->callback,
543 mdd)) {
545 /* keep webticket security token for potential further use */
546 mdd->wsse_security = g_strdup(wsse_security);
548 /* callback data passed down the line */
549 mdd = NULL;
551 g_free(query);
553 } else {
554 /* no ticket: this will show the minmum information */
555 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
556 base_uri);
559 if (mdd)
560 mdd->failed_callback(sipe_private, mdd);
563 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
564 struct ms_dlx_data *mdd)
566 if (!sipe_webticket_request(sipe_private,
567 mdd->session,
568 sipe_private->dlx_uri,
569 "AddressBookWebTicketBearer",
570 ms_dlx_webticket,
571 mdd)) {
572 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
573 sipe_private->dlx_uri);
574 mdd->failed_callback(sipe_private, mdd);
578 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
579 struct sipe_backend_search_results *results,
580 guint match_count,
581 gboolean more)
583 gchar *secondary = g_strdup_printf(
584 dngettext(PACKAGE_NAME,
585 "Found %d contact%s:",
586 "Found %d contacts%s:", match_count),
587 match_count, more ? _(" (more matched your query)") : "");
589 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
590 results,
591 secondary,
592 more);
593 g_free(secondary);
596 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
597 const gchar *uri,
598 SIPE_UNUSED_PARAMETER const gchar *raw,
599 sipe_xml *soap_body,
600 gpointer callback_data)
602 struct ms_dlx_data *mdd = callback_data;
604 if (soap_body) {
605 const sipe_xml *node;
606 struct sipe_backend_search_results *results;
607 GHashTable *found;
609 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
610 uri);
612 /* any matches? */
613 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
614 if (!node) {
615 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
616 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
617 mdd->token,
618 _("No contacts found"));
619 ms_dlx_free(mdd);
620 return;
623 /* OK, we found something - show the results to the user */
624 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
625 mdd->token);
626 if (!results) {
627 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
628 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
629 mdd->token,
630 _("Unable to display the search results"));
631 ms_dlx_free(mdd);
632 return;
635 /* SearchAbEntryResult can contain duplicates */
636 found = g_hash_table_new_full(g_str_hash, g_str_equal,
637 g_free, NULL);
639 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
640 const sipe_xml *attrs;
641 gchar *sip_uri = NULL;
642 gchar *displayname = NULL;
643 gchar *company = NULL;
644 gchar *country = NULL;
645 gchar *email = NULL;
647 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
648 attrs;
649 attrs = sipe_xml_twin(attrs)) {
650 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
651 "Name"));
652 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
653 "Value"));
655 if (!is_empty(value)) {
656 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
657 g_free(sip_uri);
658 sip_uri = value;
659 value = NULL;
660 } else if (sipe_strcase_equal(name, "displayname")) {
661 g_free(displayname);
662 displayname = value;
663 value = NULL;
664 } else if (sipe_strcase_equal(name, "mail")) {
665 g_free(email);
666 email = value;
667 value = NULL;
668 } else if (sipe_strcase_equal(name, "company")) {
669 g_free(company);
670 company = value;
671 value = NULL;
672 } else if (sipe_strcase_equal(name, "country")) {
673 g_free(country);
674 country = value;
675 value = NULL;
679 g_free(value);
680 g_free(name);
683 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
684 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
685 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
686 results,
687 uri_parts[1],
688 displayname,
689 company,
690 country,
691 email);
692 g_strfreev(uri_parts);
694 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
695 sip_uri = NULL;
698 g_free(email);
699 g_free(country);
700 g_free(company);
701 g_free(displayname);
702 g_free(sip_uri);
705 search_contacts_finalize(sipe_private, results,
706 g_hash_table_size(found),
707 FALSE);
708 g_hash_table_destroy(found);
709 ms_dlx_free(mdd);
711 } else {
712 mdd->failed_callback(sipe_private, mdd);
716 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
717 struct sipmsg *msg,
718 struct transaction *trans)
720 struct sipe_backend_search_token *token = trans->payload->data;
721 struct sipe_backend_search_results *results;
722 sipe_xml *searchResults;
723 const sipe_xml *mrow;
724 guint match_count = 0;
725 gboolean more = FALSE;
727 /* valid response? */
728 if (msg->response != 200) {
729 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
730 msg->response);
731 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
732 token,
733 _("Contact search failed"));
734 return(FALSE);
737 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
739 /* valid XML? */
740 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
741 if (!searchResults) {
742 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
743 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
744 token,
745 _("Contact search failed"));
746 return(FALSE);
749 /* any matches? */
750 mrow = sipe_xml_child(searchResults, "Body/Array/row");
751 if (!mrow) {
752 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
753 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
754 token,
755 _("No contacts found"));
757 sipe_xml_free(searchResults);
758 return(FALSE);
761 /* OK, we found something - show the results to the user */
762 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
763 trans->payload->data);
764 if (!results) {
765 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
766 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
767 token,
768 _("Unable to display the search results"));
770 sipe_xml_free(searchResults);
771 return FALSE;
774 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
775 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
776 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
777 results,
778 uri_parts[1],
779 sipe_xml_attribute(mrow, "displayName"),
780 sipe_xml_attribute(mrow, "company"),
781 sipe_xml_attribute(mrow, "country"),
782 sipe_xml_attribute(mrow, "email"));
783 g_strfreev(uri_parts);
784 match_count++;
787 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
788 char *data = sipe_xml_data(mrow);
789 more = (g_ascii_strcasecmp(data, "true") == 0);
790 g_free(data);
793 search_contacts_finalize(sipe_private, results, match_count, more);
794 sipe_xml_free(searchResults);
796 return(TRUE);
799 static void search_soap_request(struct sipe_core_private *sipe_private,
800 struct sipe_backend_search_token *token,
801 GSList *search_rows)
803 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
804 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
806 payload->data = token;
808 sip_soap_directory_search(sipe_private,
809 100,
810 query,
811 process_search_contact_response,
812 payload);
813 g_free(query);
816 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
817 struct ms_dlx_data *mdd)
819 /* error using [MS-DLX] server, retry using Active Directory */
820 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
821 ms_dlx_free(mdd);
824 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
825 struct sipe_backend_search_token *token,
826 const gchar *given_name,
827 const gchar *surname,
828 const gchar *email,
829 const gchar *company,
830 const gchar *country)
832 GSList *query_rows = NULL;
834 #define ADD_QUERY_ROW(attr, val) \
835 if (val) { \
836 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
837 query_rows = g_slist_append(query_rows, g_strdup(val)); \
840 ADD_QUERY_ROW("givenName", given_name);
841 ADD_QUERY_ROW("sn", surname);
842 ADD_QUERY_ROW("mail", email);
843 ADD_QUERY_ROW("company", company);
844 ADD_QUERY_ROW("c", country);
846 if (query_rows) {
847 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
848 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
850 mdd->search_rows = query_rows;
851 mdd->max_returns = 100;
852 mdd->callback = search_ab_entry_response;
853 mdd->failed_callback = search_ab_entry_failed;
854 mdd->session = sipe_svc_session_start();
855 mdd->token = token;
857 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
859 } else {
860 /* no [MS-DLX] server, use Active Directory search instead */
861 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
862 g_slist_free(query_rows);
864 } else
865 sipe_backend_search_failed(sipe_public,
866 token,
867 _("Invalid contact search query"));
870 static void get_info_finalize(struct sipe_core_private *sipe_private,
871 struct sipe_backend_buddy_info *info,
872 const gchar *uri,
873 const gchar *server_alias,
874 const gchar *email)
876 sipe_backend_buddy bbuddy;
877 struct sipe_buddy *sbuddy;
878 gchar *alias;
879 gchar *value;
881 if (!info) {
882 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
883 } else {
884 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
886 if (!info)
887 return;
889 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
891 if (is_empty(server_alias)) {
892 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
893 bbuddy);
894 if (value) {
895 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
896 info,
897 SIPE_BUDDY_INFO_DISPLAY_NAME,
898 value);
900 } else {
901 value = g_strdup(server_alias);
904 /* present alias if it differs from server alias */
905 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
906 if (alias && !sipe_strequal(alias, value))
908 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
909 info,
910 SIPE_BUDDY_INFO_ALIAS,
911 alias);
913 g_free(alias);
914 g_free(value);
916 if (is_empty(email)) {
917 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
918 bbuddy,
919 SIPE_BUDDY_INFO_EMAIL);
920 if (value) {
921 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
922 info,
923 SIPE_BUDDY_INFO_EMAIL,
924 value);
925 g_free(value);
929 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
930 bbuddy,
931 SIPE_BUDDY_INFO_SITE);
932 if (value) {
933 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
934 info,
935 SIPE_BUDDY_INFO_SITE,
936 value);
937 g_free(value);
940 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
941 if (sbuddy && sbuddy->device_name) {
942 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
943 info,
944 SIPE_BUDDY_INFO_DEVICE,
945 sbuddy->device_name);
948 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
952 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
953 const gchar *uri,
954 SIPE_UNUSED_PARAMETER const gchar *raw,
955 sipe_xml *soap_body,
956 gpointer callback_data)
958 struct ms_dlx_data *mdd = callback_data;
959 struct sipe_backend_buddy_info *info = NULL;
960 gchar *server_alias = NULL;
961 gchar *email = NULL;
963 if (soap_body) {
964 const sipe_xml *node;
966 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
967 uri);
969 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
971 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
972 node;
973 node = sipe_xml_twin(node)) {
974 gchar *name = sipe_xml_data(sipe_xml_child(node,
975 "Name"));
976 gchar *value = sipe_xml_data(sipe_xml_child(node,
977 "Value"));
978 const sipe_xml *values = sipe_xml_child(node,
979 "Values");
981 /* Single value entries */
982 if (!is_empty(value)) {
984 if (sipe_strcase_equal(name, "displayname")) {
985 g_free(server_alias);
986 server_alias = value;
987 value = NULL;
988 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
989 info,
990 SIPE_BUDDY_INFO_DISPLAY_NAME,
991 server_alias);
992 } else if (sipe_strcase_equal(name, "mail")) {
993 g_free(email);
994 email = value;
995 value = NULL;
996 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
997 info,
998 SIPE_BUDDY_INFO_EMAIL,
999 email);
1000 } else if (sipe_strcase_equal(name, "title")) {
1001 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1002 info,
1003 SIPE_BUDDY_INFO_JOB_TITLE,
1004 value);
1005 } else if (sipe_strcase_equal(name, "company")) {
1006 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1007 info,
1008 SIPE_BUDDY_INFO_COMPANY,
1009 value);
1010 } else if (sipe_strcase_equal(name, "country")) {
1011 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1012 info,
1013 SIPE_BUDDY_INFO_COUNTRY,
1014 value);
1017 } else if (values) {
1018 gchar *first = sipe_xml_data(sipe_xml_child(values,
1019 "string"));
1021 if (sipe_strcase_equal(name, "telephonenumber")) {
1022 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1023 info,
1024 SIPE_BUDDY_INFO_WORK_PHONE,
1025 first);
1028 g_free(first);
1031 g_free(value);
1032 g_free(name);
1036 /* this will show the minmum information */
1037 get_info_finalize(sipe_private,
1038 info,
1039 mdd->other,
1040 server_alias,
1041 email);
1043 g_free(email);
1044 g_free(server_alias);
1045 ms_dlx_free(mdd);
1048 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1049 struct sipmsg *msg,
1050 struct transaction *trans)
1052 const gchar *uri = trans->payload->data;
1053 struct sipe_backend_buddy_info *info = NULL;
1054 gchar *server_alias = NULL;
1055 gchar *email = NULL;
1057 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1058 uri, sipe_private->username);
1060 if (msg->response != 200) {
1061 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1062 } else {
1063 sipe_xml *searchResults;
1064 const sipe_xml *mrow;
1066 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1067 msg->body ? msg->body : "");
1069 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1070 if (!searchResults) {
1072 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1074 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1075 const gchar *value;
1076 gchar *phone_number;
1078 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1080 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1081 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1082 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1085 * For 2007 system we will take this from ContactCard -
1086 * it has cleaner tel: URIs at least
1088 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1089 char *tel_uri = sip_to_tel_uri(phone_number);
1090 /* trims its parameters, so call first */
1091 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1092 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1093 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1094 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1095 g_free(tel_uri);
1097 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1098 uri);
1101 if (!is_empty(server_alias)) {
1102 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1103 info,
1104 SIPE_BUDDY_INFO_DISPLAY_NAME,
1105 server_alias);
1107 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1108 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1109 info,
1110 SIPE_BUDDY_INFO_JOB_TITLE,
1111 value);
1113 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1114 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1115 info,
1116 SIPE_BUDDY_INFO_OFFICE,
1117 value);
1119 if (!is_empty(phone_number)) {
1120 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1121 info,
1122 SIPE_BUDDY_INFO_WORK_PHONE,
1123 phone_number);
1125 g_free(phone_number);
1126 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1127 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1128 info,
1129 SIPE_BUDDY_INFO_COMPANY,
1130 value);
1132 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1133 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1134 info,
1135 SIPE_BUDDY_INFO_CITY,
1136 value);
1138 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1139 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1140 info,
1141 SIPE_BUDDY_INFO_STATE,
1142 value);
1144 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1145 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1146 info,
1147 SIPE_BUDDY_INFO_COUNTRY,
1148 value);
1150 if (!is_empty(email)) {
1151 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1152 info,
1153 SIPE_BUDDY_INFO_EMAIL,
1154 email);
1157 sipe_xml_free(searchResults);
1160 /* this will show the minmum information */
1161 get_info_finalize(sipe_private,
1162 info,
1163 uri,
1164 server_alias,
1165 email);
1167 g_free(server_alias);
1168 g_free(email);
1170 return TRUE;
1173 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1174 struct ms_dlx_data *mdd)
1176 /* error using [MS-DLX] server, retry using Active Directory */
1177 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1178 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1180 payload->destroy = g_free;
1181 payload->data = mdd->other;
1182 mdd->other = NULL;
1184 sip_soap_directory_search(sipe_private,
1186 query,
1187 process_get_info_response,
1188 payload);
1190 ms_dlx_free(mdd);
1191 g_free(query);
1194 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1195 const gchar *who)
1197 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1199 if (sipe_private->dlx_uri) {
1200 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1202 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1203 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1205 mdd->other = g_strdup(who);
1206 mdd->max_returns = 1;
1207 mdd->callback = get_info_ab_entry_response;
1208 mdd->failed_callback = get_info_ab_entry_failed;
1209 mdd->session = sipe_svc_session_start();
1211 ms_dlx_webticket_request(sipe_private, mdd);
1213 } else {
1214 /* no [MS-DLX] server, use Active Directory search instead */
1215 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1216 "msRTCSIP-PrimaryUserAddress",
1217 who);
1218 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1220 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1221 row ? row : "");
1223 payload->destroy = g_free;
1224 payload->data = g_strdup(who);
1226 sip_soap_directory_search(sipe_private,
1228 row,
1229 process_get_info_response,
1230 payload);
1231 g_free(row);
1235 static void photo_response_data_free(struct photo_response_data *data)
1237 g_free(data->who);
1238 g_free(data->photo_hash);
1239 if (data->request) {
1240 sipe_http_request_cancel(data->request);
1242 g_free(data);
1245 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1246 guint status,
1247 GSList *headers,
1248 const char *body,
1249 gpointer data)
1251 struct photo_response_data *rdata = (struct photo_response_data *) data;
1253 rdata->request = NULL;
1255 if (status == SIPE_HTTP_STATUS_OK) {
1256 const gchar *len_str = sipe_utils_nameval_find(headers,
1257 "Content-Length");
1258 if (len_str) {
1259 gsize photo_size = atoi(len_str);
1260 gpointer photo = g_new(char, photo_size);
1262 if (photo) {
1263 memcpy(photo, body, photo_size);
1265 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1266 rdata->who,
1267 photo,
1268 photo_size,
1269 rdata->photo_hash);
1274 sipe_private->pending_photo_requests =
1275 g_slist_remove(sipe_private->pending_photo_requests, rdata);
1277 photo_response_data_free(rdata);
1280 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1282 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1283 gchar *wsse_security_base64;
1284 gchar *x_ms_webticket_header;
1286 if (!assertion) {
1287 return NULL;
1290 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1291 strlen(assertion));
1292 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1293 wsse_security_base64);
1295 g_free(assertion);
1296 g_free(wsse_security_base64);
1298 return x_ms_webticket_header;
1301 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1302 const gchar *uri,
1303 SIPE_UNUSED_PARAMETER const gchar *raw,
1304 sipe_xml *soap_body,
1305 gpointer callback_data)
1307 struct ms_dlx_data *mdd = callback_data;
1308 gchar *photo_rel_path = NULL;
1309 gchar *photo_hash = NULL;
1310 const gchar *photo_hash_old =
1311 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1313 if (soap_body) {
1314 const sipe_xml *node;
1316 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1317 uri);
1319 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1320 node;
1321 node = sipe_xml_twin(node)) {
1322 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1323 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1325 if (!is_empty(value)) {
1326 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1327 g_free(photo_rel_path);
1328 photo_rel_path = value;
1329 value = NULL;
1330 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1331 g_free(photo_hash);
1332 photo_hash = value;
1333 value = NULL;
1337 g_free(value);
1338 g_free(name);
1342 if (sipe_private->addressbook_uri && photo_rel_path &&
1343 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1344 gchar *photo_url = g_strdup_printf("%s/%s",
1345 sipe_private->addressbook_uri, photo_rel_path);
1346 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1348 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1349 data->who = g_strdup(mdd->other);
1350 data->photo_hash = photo_hash;
1351 photo_hash = NULL;
1353 data->request = sipe_http_request_get(sipe_private,
1354 photo_url,
1355 x_ms_webticket_header,
1356 process_buddy_photo_response,
1357 data);
1359 if (data->request) {
1360 sipe_private->pending_photo_requests =
1361 g_slist_append(sipe_private->pending_photo_requests, data);
1362 sipe_http_request_ready(data->request);
1363 } else {
1364 photo_response_data_free(data);
1367 g_free(x_ms_webticket_header);
1368 g_free(photo_url);
1371 g_free(photo_rel_path);
1372 g_free(photo_hash);
1373 ms_dlx_free(mdd);
1376 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1377 struct ms_dlx_data *mdd)
1379 ms_dlx_free(mdd);
1382 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1383 const gchar *uri)
1385 if (sipe_backend_uses_photo() &&
1386 sipe_private->dlx_uri && sipe_private->addressbook_uri) {
1387 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1389 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1390 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1392 mdd->other = g_strdup(uri);
1393 mdd->max_returns = 1;
1394 mdd->callback = get_photo_ab_entry_response;
1395 mdd->failed_callback = get_photo_ab_entry_failed;
1396 mdd->session = sipe_svc_session_start();
1398 ms_dlx_webticket_request(sipe_private, mdd);
1402 static void buddy_refresh_photos_cb(gpointer uri,
1403 SIPE_UNUSED_PARAMETER gpointer value,
1404 gpointer sipe_private)
1406 buddy_fetch_photo(sipe_private, uri);
1409 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1411 g_hash_table_foreach(sipe_private->buddies,
1412 buddy_refresh_photos_cb,
1413 sipe_private);
1416 /* Buddy menu callbacks*/
1418 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1419 const gchar *who)
1421 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1423 /* 2007+ conference */
1424 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1425 sipe_conf_add(sipe_private, who);
1427 /* 2005- multiparty chat */
1428 } else {
1429 gchar *self = sip_uri_self(sipe_private);
1430 struct sip_session *session;
1432 session = sipe_session_add_chat(sipe_private,
1433 NULL,
1434 TRUE,
1435 self);
1436 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1437 session->chat_session,
1438 session->chat_session->title,
1439 self);
1440 g_free(self);
1442 sipe_im_invite(sipe_private, session, who,
1443 NULL, NULL, NULL, FALSE);
1447 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1448 const gchar *who)
1450 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1451 who,
1452 NULL);
1453 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1454 buddy,
1455 SIPE_BUDDY_INFO_EMAIL);
1457 if (email) {
1458 gchar *command_line = g_strdup_printf(
1459 #ifdef _WIN32
1460 "cmd /c start"
1461 #else
1462 "xdg-email"
1463 #endif
1464 " mailto:%s", email);
1465 g_free(email);
1467 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1468 command_line);
1469 g_spawn_command_line_async(command_line, NULL);
1470 g_free(command_line);
1472 } else {
1473 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1474 who);
1478 /* Buddy menu */
1480 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1481 struct sipe_backend_buddy_menu *menu,
1482 sipe_backend_buddy buddy,
1483 sipe_buddy_info_fields id_phone,
1484 sipe_buddy_info_fields id_display,
1485 const gchar *type)
1487 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1488 buddy,
1489 id_phone);
1490 if (phone) {
1491 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1492 buddy,
1493 id_display);
1494 gchar *tmp = NULL;
1495 gchar *label = g_strdup_printf("%s %s",
1496 type,
1497 display ? display :
1498 (tmp = sip_tel_uri_denormalize(phone)));
1499 menu = sipe_backend_buddy_menu_add(sipe_public,
1500 menu,
1501 label,
1502 SIPE_BUDDY_MENU_MAKE_CALL,
1503 phone);
1504 g_free(tmp);
1505 g_free(label);
1506 g_free(display);
1507 g_free(phone);
1510 return(menu);
1513 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1514 const gchar *buddy_name,
1515 struct sipe_backend_buddy_menu *menu)
1517 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1518 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1519 buddy_name,
1520 NULL);
1521 gchar *self = sip_uri_self(sipe_private);
1523 SIPE_SESSION_FOREACH {
1524 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1526 struct sipe_chat_session *chat_session = session->chat_session;
1527 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1529 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1531 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1533 if (is_conf &&
1534 /* Not conf OP */
1535 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1536 /* We are a conf OP */
1537 conf_op) {
1538 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1539 chat_session->title);
1540 menu = sipe_backend_buddy_menu_add(sipe_public,
1541 menu,
1542 label,
1543 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1544 chat_session);
1545 g_free(label);
1548 if (is_conf &&
1549 /* We are a conf OP */
1550 conf_op) {
1551 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1552 chat_session->title);
1553 menu = sipe_backend_buddy_menu_add(sipe_public,
1554 menu,
1555 label,
1556 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1557 chat_session);
1558 g_free(label);
1561 else
1563 if (!is_conf ||
1564 (is_conf && !session->locked)) {
1565 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1566 chat_session->title);
1567 menu = sipe_backend_buddy_menu_add(sipe_public,
1568 menu,
1569 label,
1570 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1571 chat_session);
1572 g_free(label);
1576 } SIPE_SESSION_FOREACH_END;
1577 g_free(self);
1579 menu = sipe_backend_buddy_menu_add(sipe_public,
1580 menu,
1581 _("New chat"),
1582 SIPE_BUDDY_MENU_NEW_CHAT,
1583 NULL);
1585 /* add buddy's phone numbers if we have call control */
1586 if (sip_csta_is_idle(sipe_private)) {
1588 /* work phone */
1589 menu = buddy_menu_phone(sipe_public,
1590 menu,
1591 buddy,
1592 SIPE_BUDDY_INFO_WORK_PHONE,
1593 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1594 _("Work"));
1595 /* mobile phone */
1596 menu = buddy_menu_phone(sipe_public,
1597 menu,
1598 buddy,
1599 SIPE_BUDDY_INFO_MOBILE_PHONE,
1600 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1601 _("Mobile"));
1603 /* home phone */
1604 menu = buddy_menu_phone(sipe_public,
1605 menu,
1606 buddy,
1607 SIPE_BUDDY_INFO_HOME_PHONE,
1608 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1609 _("Home"));
1611 /* other phone */
1612 menu = buddy_menu_phone(sipe_public,
1613 menu,
1614 buddy,
1615 SIPE_BUDDY_INFO_OTHER_PHONE,
1616 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1617 _("Other"));
1619 /* custom1 phone */
1620 menu = buddy_menu_phone(sipe_public,
1621 menu,
1622 buddy,
1623 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1624 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1625 _("Custom1"));
1629 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1630 buddy,
1631 SIPE_BUDDY_INFO_EMAIL);
1632 if (email) {
1633 menu = sipe_backend_buddy_menu_add(sipe_public,
1634 menu,
1635 _("Send email..."),
1636 SIPE_BUDDY_MENU_SEND_EMAIL,
1637 NULL);
1638 g_free(email);
1642 /* access level control */
1643 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1644 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1645 menu,
1646 _("Access level"),
1647 sipe_ocs2007_access_control_menu(sipe_private,
1648 buddy_name));
1650 return(menu);
1654 Local Variables:
1655 mode: c
1656 c-file-style: "bsd"
1657 indent-tabs-mode: t
1658 tab-width: 8
1659 End: