http: add API function to flag request as ready
[siplcs.git] / src / core / sipe-buddy.c
blobf62cfdec4c30d5a7d91517665e36d6c8c5ce0fd7
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 = slist_insert_unique_sorted(buddy->groups, new_group, (GCompareFunc)sipe_group_compare);
218 sipe_group_update_buddy(SIPE_CORE_PRIVATE, buddy);
222 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
223 const gchar *uri,
224 const gchar *group_name)
226 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
228 if (!g_hash_table_lookup(sipe_private->buddies, uri)) {
229 struct sipe_buddy *b = sipe_buddy_add(sipe_private, uri);
230 b->just_added = TRUE;
232 /* @TODO should go to callback */
233 sipe_subscribe_presence_single(sipe_private, b->name);
235 } else {
236 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
237 uri);
240 sipe_core_buddy_group(sipe_public,
241 uri,
242 NULL,
243 group_name);
246 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
247 struct sipe_buddy *buddy)
249 gchar *action_name = sipe_utils_presence_key(buddy->name);
250 sipe_schedule_cancel(sipe_private, action_name);
251 g_free(action_name);
253 g_hash_table_remove(sipe_private->buddies, buddy->name);
255 buddy_free(buddy);
259 * Unassociates buddy from group first.
260 * Then see if no groups left, removes buddy completely.
261 * Otherwise updates buddy groups on server.
263 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
264 const gchar *uri,
265 const gchar *group_name)
267 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
268 struct sipe_buddy *b = g_hash_table_lookup(sipe_private->buddies,
269 uri);
271 if (!b) return;
273 if (group_name) {
274 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
275 group_name);
276 if (g) {
277 b->groups = g_slist_remove(b->groups, g);
278 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
279 uri, g->name);
283 if (g_slist_length(b->groups) < 1) {
284 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
285 b->name);
286 sip_soap_request(sipe_private,
287 "deleteContact",
288 request);
289 g_free(request);
290 sipe_buddy_remove(sipe_private, b);
291 } else {
292 /* updates groups on server */
293 sipe_group_update_buddy(sipe_private, b);
298 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
299 const gchar *uri,
300 guint activity)
302 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
303 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies,
304 uri);
306 if (!sbuddy) return;
308 /* Check if on 2005 system contact's calendar,
309 * then set/preserve it.
311 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
312 sipe_backend_buddy_set_status(sipe_public, uri, activity);
313 } else {
314 sipe_ocs2005_apply_calendar_status(sipe_private,
315 sbuddy,
316 sipe_status_activity_to_token(activity));
320 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
321 const gchar *uri,
322 const gchar *status_name,
323 gboolean is_online,
324 struct sipe_backend_buddy_tooltip *tooltip)
326 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
327 gchar *note = NULL;
328 gboolean is_oof_note = FALSE;
329 const gchar *activity = NULL;
330 gchar *calendar = NULL;
331 const gchar *meeting_subject = NULL;
332 const gchar *meeting_location = NULL;
333 gchar *access_text = NULL;
335 #define SIPE_ADD_BUDDY_INFO(l, t) \
337 gchar *tmp = g_markup_escape_text((t), -1); \
338 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
339 g_free(tmp); \
341 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
342 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
344 if (sipe_public) { /* happens on pidgin exit */
345 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
346 if (sbuddy) {
347 note = sbuddy->note;
348 is_oof_note = sbuddy->is_oof_note;
349 activity = sbuddy->activity;
350 calendar = sipe_cal_get_description(sbuddy);
351 meeting_subject = sbuddy->meeting_subject;
352 meeting_location = sbuddy->meeting_location;
354 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
355 gboolean is_group_access = FALSE;
356 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
357 "user",
358 sipe_get_no_sip_uri(uri),
359 &is_group_access);
360 const char *access_level = sipe_ocs2007_access_level_name(container_id);
361 access_text = is_group_access ?
362 g_strdup(access_level) :
363 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
364 access_level);
368 if (is_online) {
369 const gchar *status_str = activity ? activity : status_name;
371 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
373 if (is_online && !is_empty(calendar)) {
374 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
376 g_free(calendar);
377 if (!is_empty(meeting_location)) {
378 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
379 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
381 if (!is_empty(meeting_subject)) {
382 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
383 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
385 if (note) {
386 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
387 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
388 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
389 note_italics);
390 g_free(note_italics);
392 if (access_text) {
393 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
394 g_free(access_text);
398 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
399 const char *uri,
400 sipe_buddy_info_fields propkey,
401 char *property_value)
403 GSList *buddies, *entry;
405 if (property_value)
406 property_value = g_strstrip(property_value);
408 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
409 while (entry) {
410 gchar *prop_str;
411 sipe_backend_buddy p_buddy = entry->data;
413 /* for Display Name */
414 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
415 gchar *alias;
416 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
417 if (property_value && sipe_is_bad_alias(uri, alias)) {
418 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
419 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
421 g_free(alias);
423 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
424 if (!is_empty(property_value) &&
425 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
427 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
428 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
430 g_free(alias);
432 /* for other properties */
433 else {
434 if (!is_empty(property_value)) {
435 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
436 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
437 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
439 g_free(prop_str);
443 entry = entry->next;
445 g_slist_free(buddies);
449 struct ms_dlx_data;
450 struct ms_dlx_data {
451 GSList *search_rows;
452 gchar *other;
453 guint max_returns;
454 sipe_svc_callback *callback;
455 struct sipe_svc_session *session;
456 gchar *wsse_security;
457 struct sipe_backend_search_token *token;
458 /* must call ms_dlx_free() */
459 void (*failed_callback)(struct sipe_core_private *sipe_private,
460 struct ms_dlx_data *mdd);
463 static void ms_dlx_free(struct ms_dlx_data *mdd)
465 GSList *entry = mdd->search_rows;
466 while (entry) {
467 g_free(entry->data);
468 entry = entry->next;
470 g_slist_free(mdd->search_rows);
471 sipe_svc_session_close(mdd->session);
472 g_free(mdd->other);
473 g_free(mdd->wsse_security);
474 g_free(mdd);
477 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
478 #define DLX_SEARCH_ITEM \
479 "<AbEntryRequest.ChangeSearchQuery>" \
480 " <SearchOn>%s</SearchOn>" \
481 " <Value>%s</Value>" \
482 "</AbEntryRequest.ChangeSearchQuery>"
484 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
485 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
486 guint i = 0;
487 gchar *query = NULL;
489 while (query_rows) {
490 gchar *attr;
491 gchar *value;
493 attr = query_rows->data;
494 query_rows = g_slist_next(query_rows);
495 value = query_rows->data;
496 query_rows = g_slist_next(query_rows);
498 if (!attr || !value)
499 break;
501 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
502 attr, value);
504 attrs[i] = NULL;
506 if (i) {
507 query = g_strjoinv(NULL, attrs);
508 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
509 query ? query : "");
512 g_strfreev(attrs);
514 return query;
517 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
518 const gchar *base_uri,
519 const gchar *auth_uri,
520 const gchar *wsse_security,
521 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
522 gpointer callback_data)
524 struct ms_dlx_data *mdd = callback_data;
526 if (wsse_security) {
527 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
529 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
530 base_uri);
532 if (sipe_svc_ab_entry_request(sipe_private,
533 mdd->session,
534 auth_uri,
535 wsse_security,
536 query,
537 g_slist_length(mdd->search_rows) / 2,
538 mdd->max_returns,
539 mdd->callback,
540 mdd)) {
542 /* keep webticket security token for potential further use */
543 mdd->wsse_security = g_strdup(wsse_security);
545 /* callback data passed down the line */
546 mdd = NULL;
548 g_free(query);
550 } else {
551 /* no ticket: this will show the minmum information */
552 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
553 base_uri);
556 if (mdd)
557 mdd->failed_callback(sipe_private, mdd);
560 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
561 struct ms_dlx_data *mdd)
563 if (!sipe_webticket_request(sipe_private,
564 mdd->session,
565 sipe_private->dlx_uri,
566 "AddressBookWebTicketBearer",
567 ms_dlx_webticket,
568 mdd)) {
569 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
570 sipe_private->dlx_uri);
571 mdd->failed_callback(sipe_private, mdd);
575 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
576 struct sipe_backend_search_results *results,
577 guint match_count,
578 gboolean more)
580 gchar *secondary = g_strdup_printf(
581 dngettext(PACKAGE_NAME,
582 "Found %d contact%s:",
583 "Found %d contacts%s:", match_count),
584 match_count, more ? _(" (more matched your query)") : "");
586 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
587 results,
588 secondary,
589 more);
590 g_free(secondary);
593 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
594 const gchar *uri,
595 SIPE_UNUSED_PARAMETER const gchar *raw,
596 sipe_xml *soap_body,
597 gpointer callback_data)
599 struct ms_dlx_data *mdd = callback_data;
601 if (soap_body) {
602 const sipe_xml *node;
603 struct sipe_backend_search_results *results;
604 GHashTable *found;
606 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
607 uri);
609 /* any matches? */
610 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
611 if (!node) {
612 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
613 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
614 mdd->token,
615 _("No contacts found"));
616 ms_dlx_free(mdd);
617 return;
620 /* OK, we found something - show the results to the user */
621 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
622 mdd->token);
623 if (!results) {
624 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
625 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
626 mdd->token,
627 _("Unable to display the search results"));
628 ms_dlx_free(mdd);
629 return;
632 /* SearchAbEntryResult can contain duplicates */
633 found = g_hash_table_new_full(g_str_hash, g_str_equal,
634 g_free, NULL);
636 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
637 const sipe_xml *attrs;
638 gchar *sip_uri = NULL;
639 gchar *displayname = NULL;
640 gchar *company = NULL;
641 gchar *country = NULL;
642 gchar *email = NULL;
644 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
645 attrs;
646 attrs = sipe_xml_twin(attrs)) {
647 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
648 "Name"));
649 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
650 "Value"));
652 if (!is_empty(value)) {
653 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
654 g_free(sip_uri);
655 sip_uri = value;
656 value = NULL;
657 } else if (sipe_strcase_equal(name, "displayname")) {
658 g_free(displayname);
659 displayname = value;
660 value = NULL;
661 } else if (sipe_strcase_equal(name, "mail")) {
662 g_free(email);
663 email = value;
664 value = NULL;
665 } else if (sipe_strcase_equal(name, "company")) {
666 g_free(company);
667 company = value;
668 value = NULL;
669 } else if (sipe_strcase_equal(name, "country")) {
670 g_free(country);
671 country = value;
672 value = NULL;
676 g_free(value);
677 g_free(name);
680 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
681 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
682 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
683 results,
684 uri_parts[1],
685 displayname,
686 company,
687 country,
688 email);
689 g_strfreev(uri_parts);
691 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
692 sip_uri = NULL;
695 g_free(email);
696 g_free(country);
697 g_free(company);
698 g_free(displayname);
699 g_free(sip_uri);
702 search_contacts_finalize(sipe_private, results,
703 g_hash_table_size(found),
704 FALSE);
705 g_hash_table_destroy(found);
706 ms_dlx_free(mdd);
708 } else {
709 mdd->failed_callback(sipe_private, mdd);
713 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
714 struct sipmsg *msg,
715 struct transaction *trans)
717 struct sipe_backend_search_token *token = trans->payload->data;
718 struct sipe_backend_search_results *results;
719 sipe_xml *searchResults;
720 const sipe_xml *mrow;
721 guint match_count = 0;
722 gboolean more = FALSE;
724 /* valid response? */
725 if (msg->response != 200) {
726 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
727 msg->response);
728 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
729 token,
730 _("Contact search failed"));
731 return(FALSE);
734 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
736 /* valid XML? */
737 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
738 if (!searchResults) {
739 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
740 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
741 token,
742 _("Contact search failed"));
743 return(FALSE);
746 /* any matches? */
747 mrow = sipe_xml_child(searchResults, "Body/Array/row");
748 if (!mrow) {
749 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
750 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
751 token,
752 _("No contacts found"));
754 sipe_xml_free(searchResults);
755 return(FALSE);
758 /* OK, we found something - show the results to the user */
759 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
760 trans->payload->data);
761 if (!results) {
762 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
763 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
764 token,
765 _("Unable to display the search results"));
767 sipe_xml_free(searchResults);
768 return FALSE;
771 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
772 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
773 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
774 results,
775 uri_parts[1],
776 sipe_xml_attribute(mrow, "displayName"),
777 sipe_xml_attribute(mrow, "company"),
778 sipe_xml_attribute(mrow, "country"),
779 sipe_xml_attribute(mrow, "email"));
780 g_strfreev(uri_parts);
781 match_count++;
784 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
785 char *data = sipe_xml_data(mrow);
786 more = (g_ascii_strcasecmp(data, "true") == 0);
787 g_free(data);
790 search_contacts_finalize(sipe_private, results, match_count, more);
791 sipe_xml_free(searchResults);
793 return(TRUE);
796 static void search_soap_request(struct sipe_core_private *sipe_private,
797 struct sipe_backend_search_token *token,
798 GSList *search_rows)
800 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
801 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
803 payload->data = token;
805 sip_soap_directory_search(sipe_private,
806 100,
807 query,
808 process_search_contact_response,
809 payload);
810 g_free(query);
813 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
814 struct ms_dlx_data *mdd)
816 /* error using [MS-DLX] server, retry using Active Directory */
817 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
818 ms_dlx_free(mdd);
821 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
822 struct sipe_backend_search_token *token,
823 const gchar *given_name,
824 const gchar *surname,
825 const gchar *email,
826 const gchar *company,
827 const gchar *country)
829 GSList *query_rows = NULL;
831 #define ADD_QUERY_ROW(attr, val) \
832 if (val) { \
833 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
834 query_rows = g_slist_append(query_rows, g_strdup(val)); \
837 ADD_QUERY_ROW("givenName", given_name);
838 ADD_QUERY_ROW("sn", surname);
839 ADD_QUERY_ROW("mail", email);
840 ADD_QUERY_ROW("company", company);
841 ADD_QUERY_ROW("c", country);
843 if (query_rows) {
844 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
845 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
847 mdd->search_rows = query_rows;
848 mdd->max_returns = 100;
849 mdd->callback = search_ab_entry_response;
850 mdd->failed_callback = search_ab_entry_failed;
851 mdd->session = sipe_svc_session_start();
852 mdd->token = token;
854 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
856 } else {
857 /* no [MS-DLX] server, use Active Directory search instead */
858 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
859 g_slist_free(query_rows);
861 } else
862 sipe_backend_search_failed(sipe_public,
863 token,
864 _("Invalid contact search query"));
867 static void get_info_finalize(struct sipe_core_private *sipe_private,
868 struct sipe_backend_buddy_info *info,
869 const gchar *uri,
870 const gchar *server_alias,
871 const gchar *email)
873 sipe_backend_buddy bbuddy;
874 struct sipe_buddy *sbuddy;
875 gchar *alias;
876 gchar *value;
878 if (!info) {
879 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
880 } else {
881 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
883 if (!info)
884 return;
886 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
888 if (is_empty(server_alias)) {
889 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
890 bbuddy);
891 if (value) {
892 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
893 info,
894 SIPE_BUDDY_INFO_DISPLAY_NAME,
895 value);
897 } else {
898 value = g_strdup(server_alias);
901 /* present alias if it differs from server alias */
902 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
903 if (alias && !sipe_strequal(alias, value))
905 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
906 info,
907 SIPE_BUDDY_INFO_ALIAS,
908 alias);
910 g_free(alias);
911 g_free(value);
913 if (is_empty(email)) {
914 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
915 bbuddy,
916 SIPE_BUDDY_INFO_EMAIL);
917 if (value) {
918 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
919 info,
920 SIPE_BUDDY_INFO_EMAIL,
921 value);
922 g_free(value);
926 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
927 bbuddy,
928 SIPE_BUDDY_INFO_SITE);
929 if (value) {
930 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
931 info,
932 SIPE_BUDDY_INFO_SITE,
933 value);
934 g_free(value);
937 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
938 if (sbuddy && sbuddy->device_name) {
939 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
940 info,
941 SIPE_BUDDY_INFO_DEVICE,
942 sbuddy->device_name);
945 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
949 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
950 const gchar *uri,
951 SIPE_UNUSED_PARAMETER const gchar *raw,
952 sipe_xml *soap_body,
953 gpointer callback_data)
955 struct ms_dlx_data *mdd = callback_data;
956 struct sipe_backend_buddy_info *info = NULL;
957 gchar *server_alias = NULL;
958 gchar *email = NULL;
960 if (soap_body) {
961 const sipe_xml *node;
963 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
964 uri);
966 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
968 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
969 node;
970 node = sipe_xml_twin(node)) {
971 gchar *name = sipe_xml_data(sipe_xml_child(node,
972 "Name"));
973 gchar *value = sipe_xml_data(sipe_xml_child(node,
974 "Value"));
975 const sipe_xml *values = sipe_xml_child(node,
976 "Values");
978 /* Single value entries */
979 if (!is_empty(value)) {
981 if (sipe_strcase_equal(name, "displayname")) {
982 g_free(server_alias);
983 server_alias = value;
984 value = NULL;
985 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
986 info,
987 SIPE_BUDDY_INFO_DISPLAY_NAME,
988 server_alias);
989 } else if (sipe_strcase_equal(name, "mail")) {
990 g_free(email);
991 email = value;
992 value = NULL;
993 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
994 info,
995 SIPE_BUDDY_INFO_EMAIL,
996 email);
997 } else if (sipe_strcase_equal(name, "title")) {
998 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
999 info,
1000 SIPE_BUDDY_INFO_JOB_TITLE,
1001 value);
1002 } else if (sipe_strcase_equal(name, "company")) {
1003 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1004 info,
1005 SIPE_BUDDY_INFO_COMPANY,
1006 value);
1007 } else if (sipe_strcase_equal(name, "country")) {
1008 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1009 info,
1010 SIPE_BUDDY_INFO_COUNTRY,
1011 value);
1014 } else if (values) {
1015 gchar *first = sipe_xml_data(sipe_xml_child(values,
1016 "string"));
1018 if (sipe_strcase_equal(name, "telephonenumber")) {
1019 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1020 info,
1021 SIPE_BUDDY_INFO_WORK_PHONE,
1022 first);
1025 g_free(first);
1028 g_free(value);
1029 g_free(name);
1033 /* this will show the minmum information */
1034 get_info_finalize(sipe_private,
1035 info,
1036 mdd->other,
1037 server_alias,
1038 email);
1040 g_free(email);
1041 g_free(server_alias);
1042 ms_dlx_free(mdd);
1045 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1046 struct sipmsg *msg,
1047 struct transaction *trans)
1049 const gchar *uri = trans->payload->data;
1050 struct sipe_backend_buddy_info *info = NULL;
1051 gchar *server_alias = NULL;
1052 gchar *email = NULL;
1054 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1055 uri, sipe_private->username);
1057 if (msg->response != 200) {
1058 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1059 } else {
1060 sipe_xml *searchResults;
1061 const sipe_xml *mrow;
1063 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1064 msg->body ? msg->body : "");
1066 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1067 if (!searchResults) {
1069 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1071 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1072 const gchar *value;
1073 gchar *phone_number;
1075 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1077 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1078 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1079 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1082 * For 2007 system we will take this from ContactCard -
1083 * it has cleaner tel: URIs at least
1085 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1086 char *tel_uri = sip_to_tel_uri(phone_number);
1087 /* trims its parameters, so call first */
1088 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1089 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1090 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1091 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1092 g_free(tel_uri);
1094 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1095 uri);
1098 if (!is_empty(server_alias)) {
1099 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1100 info,
1101 SIPE_BUDDY_INFO_DISPLAY_NAME,
1102 server_alias);
1104 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1105 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1106 info,
1107 SIPE_BUDDY_INFO_JOB_TITLE,
1108 value);
1110 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1111 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1112 info,
1113 SIPE_BUDDY_INFO_OFFICE,
1114 value);
1116 if (!is_empty(phone_number)) {
1117 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1118 info,
1119 SIPE_BUDDY_INFO_WORK_PHONE,
1120 phone_number);
1122 g_free(phone_number);
1123 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1124 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1125 info,
1126 SIPE_BUDDY_INFO_COMPANY,
1127 value);
1129 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1130 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1131 info,
1132 SIPE_BUDDY_INFO_CITY,
1133 value);
1135 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1136 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1137 info,
1138 SIPE_BUDDY_INFO_STATE,
1139 value);
1141 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1142 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1143 info,
1144 SIPE_BUDDY_INFO_COUNTRY,
1145 value);
1147 if (!is_empty(email)) {
1148 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1149 info,
1150 SIPE_BUDDY_INFO_EMAIL,
1151 email);
1154 sipe_xml_free(searchResults);
1157 /* this will show the minmum information */
1158 get_info_finalize(sipe_private,
1159 info,
1160 uri,
1161 server_alias,
1162 email);
1164 g_free(server_alias);
1165 g_free(email);
1167 return TRUE;
1170 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1171 struct ms_dlx_data *mdd)
1173 /* error using [MS-DLX] server, retry using Active Directory */
1174 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1175 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1177 payload->destroy = g_free;
1178 payload->data = mdd->other;
1179 mdd->other = NULL;
1181 sip_soap_directory_search(sipe_private,
1183 query,
1184 process_get_info_response,
1185 payload);
1187 ms_dlx_free(mdd);
1188 g_free(query);
1191 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1192 const gchar *who)
1194 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1196 if (sipe_private->dlx_uri) {
1197 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1199 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1200 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1202 mdd->other = g_strdup(who);
1203 mdd->max_returns = 1;
1204 mdd->callback = get_info_ab_entry_response;
1205 mdd->failed_callback = get_info_ab_entry_failed;
1206 mdd->session = sipe_svc_session_start();
1208 ms_dlx_webticket_request(sipe_private, mdd);
1210 } else {
1211 /* no [MS-DLX] server, use Active Directory search instead */
1212 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1213 "msRTCSIP-PrimaryUserAddress",
1214 who);
1215 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1217 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1218 row ? row : "");
1220 payload->destroy = g_free;
1221 payload->data = g_strdup(who);
1223 sip_soap_directory_search(sipe_private,
1225 row,
1226 process_get_info_response,
1227 payload);
1228 g_free(row);
1232 static void photo_response_data_free(struct photo_response_data *data)
1234 g_free(data->who);
1235 g_free(data->photo_hash);
1236 if (data->request) {
1237 sipe_http_request_cancel(data->request);
1239 g_free(data);
1242 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1243 guint status,
1244 GSList *headers,
1245 const char *body,
1246 gpointer data)
1248 struct photo_response_data *rdata = (struct photo_response_data *) data;
1250 rdata->request = NULL;
1252 if (status == SIPE_HTTP_STATUS_OK) {
1253 const gchar *len_str = sipe_utils_nameval_find(headers,
1254 "Content-Length");
1255 if (len_str) {
1256 gsize photo_size = atoi(len_str);
1257 gpointer photo = g_new(char, photo_size);
1259 if (photo) {
1260 memcpy(photo, body, photo_size);
1262 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1263 rdata->who,
1264 photo,
1265 photo_size,
1266 rdata->photo_hash);
1271 sipe_private->pending_photo_requests =
1272 g_slist_remove(sipe_private->pending_photo_requests, rdata);
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->who = g_strdup(mdd->other);
1347 data->photo_hash = photo_hash;
1348 photo_hash = NULL;
1350 data->request = sipe_http_request_get(sipe_private,
1351 photo_url,
1352 x_ms_webticket_header,
1353 process_buddy_photo_response,
1354 data);
1356 if (data->request) {
1357 sipe_private->pending_photo_requests =
1358 g_slist_append(sipe_private->pending_photo_requests, data);
1359 sipe_http_request_ready(data->request);
1360 } else {
1361 photo_response_data_free(data);
1364 g_free(x_ms_webticket_header);
1365 g_free(photo_url);
1368 g_free(photo_rel_path);
1369 g_free(photo_hash);
1370 ms_dlx_free(mdd);
1373 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1374 struct ms_dlx_data *mdd)
1376 ms_dlx_free(mdd);
1379 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1380 const gchar *uri)
1382 if (sipe_backend_uses_photo() &&
1383 sipe_private->dlx_uri && sipe_private->addressbook_uri) {
1384 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1386 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1387 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1389 mdd->other = g_strdup(uri);
1390 mdd->max_returns = 1;
1391 mdd->callback = get_photo_ab_entry_response;
1392 mdd->failed_callback = get_photo_ab_entry_failed;
1393 mdd->session = sipe_svc_session_start();
1395 ms_dlx_webticket_request(sipe_private, mdd);
1399 static void buddy_refresh_photos_cb(gpointer uri,
1400 SIPE_UNUSED_PARAMETER gpointer value,
1401 gpointer sipe_private)
1403 buddy_fetch_photo(sipe_private, uri);
1406 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1408 g_hash_table_foreach(sipe_private->buddies,
1409 buddy_refresh_photos_cb,
1410 sipe_private);
1413 /* Buddy menu callbacks*/
1415 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1416 const gchar *who)
1418 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1420 /* 2007+ conference */
1421 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1422 sipe_conf_add(sipe_private, who);
1424 /* 2005- multiparty chat */
1425 } else {
1426 gchar *self = sip_uri_self(sipe_private);
1427 struct sip_session *session;
1429 session = sipe_session_add_chat(sipe_private,
1430 NULL,
1431 TRUE,
1432 self);
1433 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1434 session->chat_session,
1435 session->chat_session->title,
1436 self);
1437 g_free(self);
1439 sipe_im_invite(sipe_private, session, who,
1440 NULL, NULL, NULL, FALSE);
1444 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1445 const gchar *who)
1447 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1448 who,
1449 NULL);
1450 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1451 buddy,
1452 SIPE_BUDDY_INFO_EMAIL);
1454 if (email) {
1455 gchar *command_line = g_strdup_printf(
1456 #ifdef _WIN32
1457 "cmd /c start"
1458 #else
1459 "xdg-email"
1460 #endif
1461 " mailto:%s", email);
1462 g_free(email);
1464 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1465 command_line);
1466 g_spawn_command_line_async(command_line, NULL);
1467 g_free(command_line);
1469 } else {
1470 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1471 who);
1475 /* Buddy menu */
1477 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1478 struct sipe_backend_buddy_menu *menu,
1479 sipe_backend_buddy buddy,
1480 sipe_buddy_info_fields id_phone,
1481 sipe_buddy_info_fields id_display,
1482 const gchar *type)
1484 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1485 buddy,
1486 id_phone);
1487 if (phone) {
1488 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1489 buddy,
1490 id_display);
1491 gchar *tmp = NULL;
1492 gchar *label = g_strdup_printf("%s %s",
1493 type,
1494 display ? display :
1495 (tmp = sip_tel_uri_denormalize(phone)));
1496 menu = sipe_backend_buddy_menu_add(sipe_public,
1497 menu,
1498 label,
1499 SIPE_BUDDY_MENU_MAKE_CALL,
1500 phone);
1501 g_free(tmp);
1502 g_free(label);
1503 g_free(display);
1504 g_free(phone);
1507 return(menu);
1510 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1511 const gchar *buddy_name,
1512 struct sipe_backend_buddy_menu *menu)
1514 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1515 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1516 buddy_name,
1517 NULL);
1518 gchar *self = sip_uri_self(sipe_private);
1520 SIPE_SESSION_FOREACH {
1521 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1523 struct sipe_chat_session *chat_session = session->chat_session;
1524 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1526 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1528 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1530 if (is_conf &&
1531 /* Not conf OP */
1532 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1533 /* We are a conf OP */
1534 conf_op) {
1535 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1536 chat_session->title);
1537 menu = sipe_backend_buddy_menu_add(sipe_public,
1538 menu,
1539 label,
1540 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1541 chat_session);
1542 g_free(label);
1545 if (is_conf &&
1546 /* We are a conf OP */
1547 conf_op) {
1548 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1549 chat_session->title);
1550 menu = sipe_backend_buddy_menu_add(sipe_public,
1551 menu,
1552 label,
1553 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1554 chat_session);
1555 g_free(label);
1558 else
1560 if (!is_conf ||
1561 (is_conf && !session->locked)) {
1562 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1563 chat_session->title);
1564 menu = sipe_backend_buddy_menu_add(sipe_public,
1565 menu,
1566 label,
1567 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1568 chat_session);
1569 g_free(label);
1573 } SIPE_SESSION_FOREACH_END;
1574 g_free(self);
1576 menu = sipe_backend_buddy_menu_add(sipe_public,
1577 menu,
1578 _("New chat"),
1579 SIPE_BUDDY_MENU_NEW_CHAT,
1580 NULL);
1582 /* add buddy's phone numbers if we have call control */
1583 if (sip_csta_is_idle(sipe_private)) {
1585 /* work phone */
1586 menu = buddy_menu_phone(sipe_public,
1587 menu,
1588 buddy,
1589 SIPE_BUDDY_INFO_WORK_PHONE,
1590 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1591 _("Work"));
1592 /* mobile phone */
1593 menu = buddy_menu_phone(sipe_public,
1594 menu,
1595 buddy,
1596 SIPE_BUDDY_INFO_MOBILE_PHONE,
1597 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1598 _("Mobile"));
1600 /* home phone */
1601 menu = buddy_menu_phone(sipe_public,
1602 menu,
1603 buddy,
1604 SIPE_BUDDY_INFO_HOME_PHONE,
1605 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1606 _("Home"));
1608 /* other phone */
1609 menu = buddy_menu_phone(sipe_public,
1610 menu,
1611 buddy,
1612 SIPE_BUDDY_INFO_OTHER_PHONE,
1613 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1614 _("Other"));
1616 /* custom1 phone */
1617 menu = buddy_menu_phone(sipe_public,
1618 menu,
1619 buddy,
1620 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1621 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1622 _("Custom1"));
1626 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1627 buddy,
1628 SIPE_BUDDY_INFO_EMAIL);
1629 if (email) {
1630 menu = sipe_backend_buddy_menu_add(sipe_public,
1631 menu,
1632 _("Send email..."),
1633 SIPE_BUDDY_MENU_SEND_EMAIL,
1634 NULL);
1635 g_free(email);
1639 /* access level control */
1640 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1641 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1642 menu,
1643 _("Access level"),
1644 sipe_ocs2007_access_control_menu(sipe_private,
1645 buddy_name));
1647 return(menu);
1651 Local Variables:
1652 mode: c
1653 c-file-style: "bsd"
1654 indent-tabs-mode: t
1655 tab-width: 8
1656 End: