Fix #193: Pidgin Status changes stop working (III)
[siplcs.git] / src / core / sipe-buddy.c
blob5753d9bdacf316a1e81735904afe35d5eb227d0b
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 sipe_subscribe_presence_single_cb(sipe_private, b->name);
237 } else {
238 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
239 uri);
242 sipe_core_buddy_group(sipe_public,
243 uri,
244 NULL,
245 group_name);
248 void sipe_buddy_remove(struct sipe_core_private *sipe_private,
249 struct sipe_buddy *buddy)
251 gchar *action_name = sipe_utils_presence_key(buddy->name);
252 sipe_schedule_cancel(sipe_private, action_name);
253 g_free(action_name);
255 g_hash_table_remove(sipe_private->buddies, buddy->name);
257 buddy_free(buddy);
261 * Unassociates buddy from group first.
262 * Then see if no groups left, removes buddy completely.
263 * Otherwise updates buddy groups on server.
265 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
266 const gchar *uri,
267 const gchar *group_name)
269 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
270 struct sipe_buddy *b = g_hash_table_lookup(sipe_private->buddies,
271 uri);
273 if (!b) return;
275 if (group_name) {
276 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
277 group_name);
278 if (g) {
279 b->groups = g_slist_remove(b->groups, g);
280 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
281 uri, g->name);
285 if (g_slist_length(b->groups) < 1) {
286 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
287 b->name);
288 sip_soap_request(sipe_private,
289 "deleteContact",
290 request);
291 g_free(request);
292 sipe_buddy_remove(sipe_private, b);
293 } else {
294 /* updates groups on server */
295 sipe_group_update_buddy(sipe_private, b);
300 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
301 const gchar *uri,
302 guint activity)
304 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
305 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies,
306 uri);
308 if (!sbuddy) return;
310 /* Check if on 2005 system contact's calendar,
311 * then set/preserve it.
313 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
314 sipe_backend_buddy_set_status(sipe_public, uri, activity);
315 } else {
316 sipe_ocs2005_apply_calendar_status(sipe_private,
317 sbuddy,
318 sipe_status_activity_to_token(activity));
322 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
323 const gchar *uri,
324 const gchar *status_name,
325 gboolean is_online,
326 struct sipe_backend_buddy_tooltip *tooltip)
328 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
329 gchar *note = NULL;
330 gboolean is_oof_note = FALSE;
331 const gchar *activity = NULL;
332 gchar *calendar = NULL;
333 const gchar *meeting_subject = NULL;
334 const gchar *meeting_location = NULL;
335 gchar *access_text = NULL;
337 #define SIPE_ADD_BUDDY_INFO(l, t) \
339 gchar *tmp = g_markup_escape_text((t), -1); \
340 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
341 g_free(tmp); \
343 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
344 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
346 if (sipe_public) { /* happens on pidgin exit */
347 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
348 if (sbuddy) {
349 note = sbuddy->note;
350 is_oof_note = sbuddy->is_oof_note;
351 activity = sbuddy->activity;
352 calendar = sipe_cal_get_description(sbuddy);
353 meeting_subject = sbuddy->meeting_subject;
354 meeting_location = sbuddy->meeting_location;
356 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
357 gboolean is_group_access = FALSE;
358 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
359 "user",
360 sipe_get_no_sip_uri(uri),
361 &is_group_access);
362 const char *access_level = sipe_ocs2007_access_level_name(container_id);
363 access_text = is_group_access ?
364 g_strdup(access_level) :
365 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
366 access_level);
370 if (is_online) {
371 const gchar *status_str = activity ? activity : status_name;
373 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
375 if (is_online && !is_empty(calendar)) {
376 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
378 g_free(calendar);
379 if (!is_empty(meeting_location)) {
380 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
381 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
383 if (!is_empty(meeting_subject)) {
384 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
385 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
387 if (note) {
388 gchar *note_italics = g_strdup_printf("<i>%s</i>", note);
389 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
390 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
391 note_italics);
392 g_free(note_italics);
394 if (access_text) {
395 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
396 g_free(access_text);
400 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
401 const char *uri,
402 sipe_buddy_info_fields propkey,
403 char *property_value)
405 GSList *buddies, *entry;
407 if (property_value)
408 property_value = g_strstrip(property_value);
410 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
411 while (entry) {
412 gchar *prop_str;
413 sipe_backend_buddy p_buddy = entry->data;
415 /* for Display Name */
416 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
417 gchar *alias;
418 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
419 if (property_value && sipe_is_bad_alias(uri, alias)) {
420 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
421 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
423 g_free(alias);
425 alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
426 if (!is_empty(property_value) &&
427 (!sipe_strequal(property_value, alias) || is_empty(alias)) )
429 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
430 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
432 g_free(alias);
434 /* for other properties */
435 else {
436 if (!is_empty(property_value)) {
437 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
438 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
439 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
441 g_free(prop_str);
445 entry = entry->next;
447 g_slist_free(buddies);
451 struct ms_dlx_data;
452 struct ms_dlx_data {
453 GSList *search_rows;
454 gchar *other;
455 guint max_returns;
456 sipe_svc_callback *callback;
457 struct sipe_svc_session *session;
458 gchar *wsse_security;
459 struct sipe_backend_search_token *token;
460 /* must call ms_dlx_free() */
461 void (*failed_callback)(struct sipe_core_private *sipe_private,
462 struct ms_dlx_data *mdd);
465 static void ms_dlx_free(struct ms_dlx_data *mdd)
467 sipe_utils_slist_free_full(mdd->search_rows, g_free);
468 sipe_svc_session_close(mdd->session);
469 g_free(mdd->other);
470 g_free(mdd->wsse_security);
471 g_free(mdd);
474 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
475 #define DLX_SEARCH_ITEM \
476 "<AbEntryRequest.ChangeSearchQuery>" \
477 " <SearchOn>%s</SearchOn>" \
478 " <Value>%s</Value>" \
479 "</AbEntryRequest.ChangeSearchQuery>"
481 static gchar * prepare_buddy_search_query(GSList *query_rows, gboolean use_dlx) {
482 gchar **attrs = g_new(gchar *, (g_slist_length(query_rows) / 2) + 1);
483 guint i = 0;
484 gchar *query = NULL;
486 while (query_rows) {
487 gchar *attr;
488 gchar *value;
490 attr = query_rows->data;
491 query_rows = g_slist_next(query_rows);
492 value = query_rows->data;
493 query_rows = g_slist_next(query_rows);
495 if (!attr || !value)
496 break;
498 attrs[i++] = g_markup_printf_escaped(use_dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW,
499 attr, value);
501 attrs[i] = NULL;
503 if (i) {
504 query = g_strjoinv(NULL, attrs);
505 SIPE_DEBUG_INFO("prepare_buddy_search_query: rows:\n%s",
506 query ? query : "");
509 g_strfreev(attrs);
511 return query;
514 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
515 const gchar *base_uri,
516 const gchar *auth_uri,
517 const gchar *wsse_security,
518 SIPE_UNUSED_PARAMETER const gchar *failure_msg,
519 gpointer callback_data)
521 struct ms_dlx_data *mdd = callback_data;
523 if (wsse_security) {
524 gchar *query = prepare_buddy_search_query(mdd->search_rows, TRUE);
526 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
527 base_uri);
529 if (sipe_svc_ab_entry_request(sipe_private,
530 mdd->session,
531 auth_uri,
532 wsse_security,
533 query,
534 g_slist_length(mdd->search_rows) / 2,
535 mdd->max_returns,
536 mdd->callback,
537 mdd)) {
539 /* keep webticket security token for potential further use */
540 mdd->wsse_security = g_strdup(wsse_security);
542 /* callback data passed down the line */
543 mdd = NULL;
545 g_free(query);
547 } else {
548 /* no ticket: this will show the minmum information */
549 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
550 base_uri);
553 if (mdd)
554 mdd->failed_callback(sipe_private, mdd);
557 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
558 struct ms_dlx_data *mdd)
560 if (!sipe_webticket_request(sipe_private,
561 mdd->session,
562 sipe_private->dlx_uri,
563 "AddressBookWebTicketBearer",
564 ms_dlx_webticket,
565 mdd)) {
566 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
567 sipe_private->dlx_uri);
568 mdd->failed_callback(sipe_private, mdd);
572 static void search_contacts_finalize(struct sipe_core_private *sipe_private,
573 struct sipe_backend_search_results *results,
574 guint match_count,
575 gboolean more)
577 gchar *secondary = g_strdup_printf(
578 dngettext(PACKAGE_NAME,
579 "Found %d contact%s:",
580 "Found %d contacts%s:", match_count),
581 match_count, more ? _(" (more matched your query)") : "");
583 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
584 results,
585 secondary,
586 more);
587 g_free(secondary);
590 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
591 const gchar *uri,
592 SIPE_UNUSED_PARAMETER const gchar *raw,
593 sipe_xml *soap_body,
594 gpointer callback_data)
596 struct ms_dlx_data *mdd = callback_data;
598 if (soap_body) {
599 const sipe_xml *node;
600 struct sipe_backend_search_results *results;
601 GHashTable *found;
603 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
604 uri);
606 /* any matches? */
607 node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry");
608 if (!node) {
609 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: no matches");
610 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
611 mdd->token,
612 _("No contacts found"));
613 ms_dlx_free(mdd);
614 return;
617 /* OK, we found something - show the results to the user */
618 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
619 mdd->token);
620 if (!results) {
621 SIPE_DEBUG_ERROR_NOFORMAT("search_ab_entry_response: Unable to display the search results.");
622 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
623 mdd->token,
624 _("Unable to display the search results"));
625 ms_dlx_free(mdd);
626 return;
629 /* SearchAbEntryResult can contain duplicates */
630 found = g_hash_table_new_full(g_str_hash, g_str_equal,
631 g_free, NULL);
633 for (/* initialized above */ ; node; node = sipe_xml_twin(node)) {
634 const sipe_xml *attrs;
635 gchar *sip_uri = NULL;
636 gchar *displayname = NULL;
637 gchar *company = NULL;
638 gchar *country = NULL;
639 gchar *email = NULL;
641 for (attrs = sipe_xml_child(node, "Attributes/Attribute");
642 attrs;
643 attrs = sipe_xml_twin(attrs)) {
644 gchar *name = sipe_xml_data(sipe_xml_child(attrs,
645 "Name"));
646 gchar *value = sipe_xml_data(sipe_xml_child(attrs,
647 "Value"));
649 if (!is_empty(value)) {
650 if (sipe_strcase_equal(name, "msrtcsip-primaryuseraddress")) {
651 g_free(sip_uri);
652 sip_uri = value;
653 value = NULL;
654 } else if (sipe_strcase_equal(name, "displayname")) {
655 g_free(displayname);
656 displayname = value;
657 value = NULL;
658 } else if (sipe_strcase_equal(name, "mail")) {
659 g_free(email);
660 email = value;
661 value = NULL;
662 } else if (sipe_strcase_equal(name, "company")) {
663 g_free(company);
664 company = value;
665 value = NULL;
666 } else if (sipe_strcase_equal(name, "country")) {
667 g_free(country);
668 country = value;
669 value = NULL;
673 g_free(value);
674 g_free(name);
677 if (sip_uri && !g_hash_table_lookup(found, sip_uri)) {
678 gchar **uri_parts = g_strsplit(sip_uri, ":", 2);
679 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
680 results,
681 uri_parts[1],
682 displayname,
683 company,
684 country,
685 email);
686 g_strfreev(uri_parts);
688 g_hash_table_insert(found, sip_uri, (gpointer) TRUE);
689 sip_uri = NULL;
692 g_free(email);
693 g_free(country);
694 g_free(company);
695 g_free(displayname);
696 g_free(sip_uri);
699 search_contacts_finalize(sipe_private, results,
700 g_hash_table_size(found),
701 FALSE);
702 g_hash_table_destroy(found);
703 ms_dlx_free(mdd);
705 } else {
706 mdd->failed_callback(sipe_private, mdd);
710 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
711 struct sipmsg *msg,
712 struct transaction *trans)
714 struct sipe_backend_search_token *token = trans->payload->data;
715 struct sipe_backend_search_results *results;
716 sipe_xml *searchResults;
717 const sipe_xml *mrow;
718 guint match_count = 0;
719 gboolean more = FALSE;
721 /* valid response? */
722 if (msg->response != 200) {
723 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
724 msg->response);
725 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
726 token,
727 _("Contact search failed"));
728 return(FALSE);
731 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
733 /* valid XML? */
734 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
735 if (!searchResults) {
736 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
737 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
738 token,
739 _("Contact search failed"));
740 return(FALSE);
743 /* any matches? */
744 mrow = sipe_xml_child(searchResults, "Body/Array/row");
745 if (!mrow) {
746 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
747 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
748 token,
749 _("No contacts found"));
751 sipe_xml_free(searchResults);
752 return(FALSE);
755 /* OK, we found something - show the results to the user */
756 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC,
757 trans->payload->data);
758 if (!results) {
759 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
760 sipe_backend_search_failed(SIPE_CORE_PUBLIC,
761 token,
762 _("Unable to display the search results"));
764 sipe_xml_free(searchResults);
765 return FALSE;
768 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
769 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
770 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
771 results,
772 uri_parts[1],
773 sipe_xml_attribute(mrow, "displayName"),
774 sipe_xml_attribute(mrow, "company"),
775 sipe_xml_attribute(mrow, "country"),
776 sipe_xml_attribute(mrow, "email"));
777 g_strfreev(uri_parts);
778 match_count++;
781 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
782 char *data = sipe_xml_data(mrow);
783 more = (g_ascii_strcasecmp(data, "true") == 0);
784 g_free(data);
787 search_contacts_finalize(sipe_private, results, match_count, more);
788 sipe_xml_free(searchResults);
790 return(TRUE);
793 static void search_soap_request(struct sipe_core_private *sipe_private,
794 struct sipe_backend_search_token *token,
795 GSList *search_rows)
797 gchar *query = prepare_buddy_search_query(search_rows, FALSE);
798 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
800 payload->data = token;
802 sip_soap_directory_search(sipe_private,
803 100,
804 query,
805 process_search_contact_response,
806 payload);
807 g_free(query);
810 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
811 struct ms_dlx_data *mdd)
813 /* error using [MS-DLX] server, retry using Active Directory */
814 search_soap_request(sipe_private, mdd->token, mdd->search_rows);
815 ms_dlx_free(mdd);
818 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
819 struct sipe_backend_search_token *token,
820 const gchar *given_name,
821 const gchar *surname,
822 const gchar *email,
823 const gchar *company,
824 const gchar *country)
826 GSList *query_rows = NULL;
828 #define ADD_QUERY_ROW(attr, val) \
829 if (val) { \
830 query_rows = g_slist_append(query_rows, g_strdup(attr)); \
831 query_rows = g_slist_append(query_rows, g_strdup(val)); \
834 ADD_QUERY_ROW("givenName", given_name);
835 ADD_QUERY_ROW("sn", surname);
836 ADD_QUERY_ROW("mail", email);
837 ADD_QUERY_ROW("company", company);
838 ADD_QUERY_ROW("c", country);
840 if (query_rows) {
841 if (SIPE_CORE_PRIVATE->dlx_uri != NULL) {
842 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
844 mdd->search_rows = query_rows;
845 mdd->max_returns = 100;
846 mdd->callback = search_ab_entry_response;
847 mdd->failed_callback = search_ab_entry_failed;
848 mdd->session = sipe_svc_session_start();
849 mdd->token = token;
851 ms_dlx_webticket_request(SIPE_CORE_PRIVATE, mdd);
853 } else {
854 /* no [MS-DLX] server, use Active Directory search instead */
855 search_soap_request(SIPE_CORE_PRIVATE, token, query_rows);
856 sipe_utils_slist_free_full(query_rows, g_free);
858 } else
859 sipe_backend_search_failed(sipe_public,
860 token,
861 _("Invalid contact search query"));
864 static void get_info_finalize(struct sipe_core_private *sipe_private,
865 struct sipe_backend_buddy_info *info,
866 const gchar *uri,
867 const gchar *server_alias,
868 const gchar *email)
870 sipe_backend_buddy bbuddy;
871 struct sipe_buddy *sbuddy;
872 gchar *alias;
873 gchar *value;
875 if (!info) {
876 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
877 } else {
878 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
880 if (!info)
881 return;
883 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
885 if (is_empty(server_alias)) {
886 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
887 bbuddy);
888 if (value) {
889 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
890 info,
891 SIPE_BUDDY_INFO_DISPLAY_NAME,
892 value);
894 } else {
895 value = g_strdup(server_alias);
898 /* present alias if it differs from server alias */
899 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
900 if (alias && !sipe_strequal(alias, value))
902 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
903 info,
904 SIPE_BUDDY_INFO_ALIAS,
905 alias);
907 g_free(alias);
908 g_free(value);
910 if (is_empty(email)) {
911 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
912 bbuddy,
913 SIPE_BUDDY_INFO_EMAIL);
914 if (value) {
915 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
916 info,
917 SIPE_BUDDY_INFO_EMAIL,
918 value);
919 g_free(value);
923 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
924 bbuddy,
925 SIPE_BUDDY_INFO_SITE);
926 if (value) {
927 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
928 info,
929 SIPE_BUDDY_INFO_SITE,
930 value);
931 g_free(value);
934 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
935 if (sbuddy && sbuddy->device_name) {
936 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
937 info,
938 SIPE_BUDDY_INFO_DEVICE,
939 sbuddy->device_name);
942 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
946 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
947 const gchar *uri,
948 SIPE_UNUSED_PARAMETER const gchar *raw,
949 sipe_xml *soap_body,
950 gpointer callback_data)
952 struct ms_dlx_data *mdd = callback_data;
953 struct sipe_backend_buddy_info *info = NULL;
954 gchar *server_alias = NULL;
955 gchar *email = NULL;
957 if (soap_body) {
958 const sipe_xml *node;
960 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
961 uri);
963 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
965 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
966 node;
967 node = sipe_xml_twin(node)) {
968 gchar *name = sipe_xml_data(sipe_xml_child(node,
969 "Name"));
970 gchar *value = sipe_xml_data(sipe_xml_child(node,
971 "Value"));
972 const sipe_xml *values = sipe_xml_child(node,
973 "Values");
975 /* Single value entries */
976 if (!is_empty(value)) {
978 if (sipe_strcase_equal(name, "displayname")) {
979 g_free(server_alias);
980 server_alias = value;
981 value = NULL;
982 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
983 info,
984 SIPE_BUDDY_INFO_DISPLAY_NAME,
985 server_alias);
986 } else if (sipe_strcase_equal(name, "mail")) {
987 g_free(email);
988 email = value;
989 value = NULL;
990 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
991 info,
992 SIPE_BUDDY_INFO_EMAIL,
993 email);
994 } else if (sipe_strcase_equal(name, "title")) {
995 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
996 info,
997 SIPE_BUDDY_INFO_JOB_TITLE,
998 value);
999 } else if (sipe_strcase_equal(name, "company")) {
1000 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1001 info,
1002 SIPE_BUDDY_INFO_COMPANY,
1003 value);
1004 } else if (sipe_strcase_equal(name, "country")) {
1005 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1006 info,
1007 SIPE_BUDDY_INFO_COUNTRY,
1008 value);
1011 } else if (values) {
1012 gchar *first = sipe_xml_data(sipe_xml_child(values,
1013 "string"));
1015 if (sipe_strcase_equal(name, "telephonenumber")) {
1016 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1017 info,
1018 SIPE_BUDDY_INFO_WORK_PHONE,
1019 first);
1022 g_free(first);
1025 g_free(value);
1026 g_free(name);
1030 /* this will show the minmum information */
1031 get_info_finalize(sipe_private,
1032 info,
1033 mdd->other,
1034 server_alias,
1035 email);
1037 g_free(email);
1038 g_free(server_alias);
1039 ms_dlx_free(mdd);
1042 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
1043 struct sipmsg *msg,
1044 struct transaction *trans)
1046 const gchar *uri = trans->payload->data;
1047 struct sipe_backend_buddy_info *info = NULL;
1048 gchar *server_alias = NULL;
1049 gchar *email = NULL;
1051 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
1052 uri, sipe_private->username);
1054 if (msg->response != 200) {
1055 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
1056 } else {
1057 sipe_xml *searchResults;
1058 const sipe_xml *mrow;
1060 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
1061 msg->body ? msg->body : "");
1063 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
1064 if (!searchResults) {
1066 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
1068 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
1069 const gchar *value;
1070 gchar *phone_number;
1072 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
1074 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
1075 email = g_strdup(sipe_xml_attribute(mrow, "email"));
1076 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
1079 * For 2007 system we will take this from ContactCard -
1080 * it has cleaner tel: URIs at least
1082 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1083 char *tel_uri = sip_to_tel_uri(phone_number);
1084 /* trims its parameters, so call first */
1085 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
1086 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
1087 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
1088 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
1089 g_free(tel_uri);
1091 sipe_backend_buddy_refresh_properties(SIPE_CORE_PUBLIC,
1092 uri);
1095 if (!is_empty(server_alias)) {
1096 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1097 info,
1098 SIPE_BUDDY_INFO_DISPLAY_NAME,
1099 server_alias);
1101 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
1102 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1103 info,
1104 SIPE_BUDDY_INFO_JOB_TITLE,
1105 value);
1107 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
1108 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1109 info,
1110 SIPE_BUDDY_INFO_OFFICE,
1111 value);
1113 if (!is_empty(phone_number)) {
1114 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1115 info,
1116 SIPE_BUDDY_INFO_WORK_PHONE,
1117 phone_number);
1119 g_free(phone_number);
1120 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
1121 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1122 info,
1123 SIPE_BUDDY_INFO_COMPANY,
1124 value);
1126 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
1127 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1128 info,
1129 SIPE_BUDDY_INFO_CITY,
1130 value);
1132 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
1133 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1134 info,
1135 SIPE_BUDDY_INFO_STATE,
1136 value);
1138 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
1139 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1140 info,
1141 SIPE_BUDDY_INFO_COUNTRY,
1142 value);
1144 if (!is_empty(email)) {
1145 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
1146 info,
1147 SIPE_BUDDY_INFO_EMAIL,
1148 email);
1151 sipe_xml_free(searchResults);
1154 /* this will show the minmum information */
1155 get_info_finalize(sipe_private,
1156 info,
1157 uri,
1158 server_alias,
1159 email);
1161 g_free(server_alias);
1162 g_free(email);
1164 return TRUE;
1167 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
1168 struct ms_dlx_data *mdd)
1170 /* error using [MS-DLX] server, retry using Active Directory */
1171 gchar *query = prepare_buddy_search_query(mdd->search_rows, FALSE);
1172 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1174 payload->destroy = g_free;
1175 payload->data = mdd->other;
1176 mdd->other = NULL;
1178 sip_soap_directory_search(sipe_private,
1180 query,
1181 process_get_info_response,
1182 payload);
1184 ms_dlx_free(mdd);
1185 g_free(query);
1188 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
1189 const gchar *who)
1191 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1193 if (sipe_private->dlx_uri) {
1194 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1196 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1197 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(who));
1199 mdd->other = g_strdup(who);
1200 mdd->max_returns = 1;
1201 mdd->callback = get_info_ab_entry_response;
1202 mdd->failed_callback = get_info_ab_entry_failed;
1203 mdd->session = sipe_svc_session_start();
1205 ms_dlx_webticket_request(sipe_private, mdd);
1207 } else {
1208 /* no [MS-DLX] server, use Active Directory search instead */
1209 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
1210 "msRTCSIP-PrimaryUserAddress",
1211 who);
1212 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
1214 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
1215 row ? row : "");
1217 payload->destroy = g_free;
1218 payload->data = g_strdup(who);
1220 sip_soap_directory_search(sipe_private,
1222 row,
1223 process_get_info_response,
1224 payload);
1225 g_free(row);
1229 static void photo_response_data_free(struct photo_response_data *data)
1231 g_free(data->who);
1232 g_free(data->photo_hash);
1233 if (data->request) {
1234 sipe_http_request_cancel(data->request);
1236 g_free(data);
1239 static void process_buddy_photo_response(struct sipe_core_private *sipe_private,
1240 guint status,
1241 GSList *headers,
1242 const char *body,
1243 gpointer data)
1245 struct photo_response_data *rdata = (struct photo_response_data *) data;
1247 rdata->request = NULL;
1249 if (status == SIPE_HTTP_STATUS_OK) {
1250 const gchar *len_str = sipe_utils_nameval_find(headers,
1251 "Content-Length");
1252 if (len_str) {
1253 gsize photo_size = atoi(len_str);
1254 gpointer photo = g_new(char, photo_size);
1256 if (photo) {
1257 memcpy(photo, body, photo_size);
1259 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
1260 rdata->who,
1261 photo,
1262 photo_size,
1263 rdata->photo_hash);
1268 sipe_private->pending_photo_requests =
1269 g_slist_remove(sipe_private->pending_photo_requests, rdata);
1271 photo_response_data_free(rdata);
1274 static gchar *create_x_ms_webticket_header(const gchar *wsse_security)
1276 gchar *assertion = sipe_xml_extract_raw(wsse_security, "saml:Assertion", TRUE);
1277 gchar *wsse_security_base64;
1278 gchar *x_ms_webticket_header;
1280 if (!assertion) {
1281 return NULL;
1284 wsse_security_base64 = g_base64_encode((const guchar *)assertion,
1285 strlen(assertion));
1286 x_ms_webticket_header = g_strdup_printf("X-MS-WebTicket: opaque=%s\r\n",
1287 wsse_security_base64);
1289 g_free(assertion);
1290 g_free(wsse_security_base64);
1292 return x_ms_webticket_header;
1295 static void get_photo_ab_entry_response(struct sipe_core_private *sipe_private,
1296 const gchar *uri,
1297 SIPE_UNUSED_PARAMETER const gchar *raw,
1298 sipe_xml *soap_body,
1299 gpointer callback_data)
1301 struct ms_dlx_data *mdd = callback_data;
1302 gchar *photo_rel_path = NULL;
1303 gchar *photo_hash = NULL;
1304 const gchar *photo_hash_old =
1305 sipe_backend_buddy_get_photo_hash(SIPE_CORE_PUBLIC, mdd->other);
1307 if (soap_body) {
1308 const sipe_xml *node;
1310 SIPE_DEBUG_INFO("get_photo_ab_entry_response: received valid SOAP message from service %s",
1311 uri);
1313 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
1314 node;
1315 node = sipe_xml_twin(node)) {
1316 gchar *name = sipe_xml_data(sipe_xml_child(node, "Name"));
1317 gchar *value = sipe_xml_data(sipe_xml_child(node, "Value"));
1319 if (!is_empty(value)) {
1320 if (sipe_strcase_equal(name, "PhotoRelPath")) {
1321 g_free(photo_rel_path);
1322 photo_rel_path = value;
1323 value = NULL;
1324 } else if (sipe_strcase_equal(name, "PhotoHash")) {
1325 g_free(photo_hash);
1326 photo_hash = value;
1327 value = NULL;
1331 g_free(value);
1332 g_free(name);
1336 if (sipe_private->addressbook_uri && photo_rel_path &&
1337 photo_hash && !sipe_strequal(photo_hash, photo_hash_old)) {
1338 gchar *photo_url = g_strdup_printf("%s/%s",
1339 sipe_private->addressbook_uri, photo_rel_path);
1340 gchar *x_ms_webticket_header = create_x_ms_webticket_header(mdd->wsse_security);
1342 struct photo_response_data *data = g_new(struct photo_response_data, 1);
1343 data->who = g_strdup(mdd->other);
1344 data->photo_hash = photo_hash;
1345 photo_hash = NULL;
1347 data->request = sipe_http_request_get(sipe_private,
1348 photo_url,
1349 x_ms_webticket_header,
1350 process_buddy_photo_response,
1351 data);
1353 if (data->request) {
1354 sipe_private->pending_photo_requests =
1355 g_slist_append(sipe_private->pending_photo_requests, data);
1356 sipe_http_request_ready(data->request);
1357 } else {
1358 photo_response_data_free(data);
1361 g_free(x_ms_webticket_header);
1362 g_free(photo_url);
1365 g_free(photo_rel_path);
1366 g_free(photo_hash);
1367 ms_dlx_free(mdd);
1370 static void get_photo_ab_entry_failed(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
1371 struct ms_dlx_data *mdd)
1373 ms_dlx_free(mdd);
1376 static void buddy_fetch_photo(struct sipe_core_private *sipe_private,
1377 const gchar *uri)
1379 if (sipe_backend_uses_photo() &&
1380 sipe_private->dlx_uri && sipe_private->addressbook_uri) {
1381 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
1383 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup("msRTCSIP-PrimaryUserAddress"));
1384 mdd->search_rows = g_slist_append(mdd->search_rows, g_strdup(uri));
1386 mdd->other = g_strdup(uri);
1387 mdd->max_returns = 1;
1388 mdd->callback = get_photo_ab_entry_response;
1389 mdd->failed_callback = get_photo_ab_entry_failed;
1390 mdd->session = sipe_svc_session_start();
1392 ms_dlx_webticket_request(sipe_private, mdd);
1396 static void buddy_refresh_photos_cb(gpointer uri,
1397 SIPE_UNUSED_PARAMETER gpointer value,
1398 gpointer sipe_private)
1400 buddy_fetch_photo(sipe_private, uri);
1403 void sipe_buddy_refresh_photos(struct sipe_core_private *sipe_private)
1405 g_hash_table_foreach(sipe_private->buddies,
1406 buddy_refresh_photos_cb,
1407 sipe_private);
1410 /* Buddy menu callbacks*/
1412 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1413 const gchar *who)
1415 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1417 /* 2007+ conference */
1418 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1419 sipe_conf_add(sipe_private, who);
1421 /* 2005- multiparty chat */
1422 } else {
1423 gchar *self = sip_uri_self(sipe_private);
1424 struct sip_session *session;
1426 session = sipe_session_add_chat(sipe_private,
1427 NULL,
1428 TRUE,
1429 self);
1430 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1431 session->chat_session,
1432 session->chat_session->title,
1433 self);
1434 g_free(self);
1436 sipe_im_invite(sipe_private, session, who,
1437 NULL, NULL, NULL, FALSE);
1441 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1442 const gchar *who)
1444 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1445 who,
1446 NULL);
1447 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1448 buddy,
1449 SIPE_BUDDY_INFO_EMAIL);
1451 if (email) {
1452 gchar *command_line = g_strdup_printf(
1453 #ifdef _WIN32
1454 "cmd /c start"
1455 #else
1456 "xdg-email"
1457 #endif
1458 " mailto:%s", email);
1459 g_free(email);
1461 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1462 command_line);
1463 g_spawn_command_line_async(command_line, NULL);
1464 g_free(command_line);
1466 } else {
1467 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1468 who);
1472 /* Buddy menu */
1474 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1475 struct sipe_backend_buddy_menu *menu,
1476 sipe_backend_buddy buddy,
1477 sipe_buddy_info_fields id_phone,
1478 sipe_buddy_info_fields id_display,
1479 const gchar *type)
1481 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1482 buddy,
1483 id_phone);
1484 if (phone) {
1485 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1486 buddy,
1487 id_display);
1488 gchar *tmp = NULL;
1489 gchar *label = g_strdup_printf("%s %s",
1490 type,
1491 display ? display :
1492 (tmp = sip_tel_uri_denormalize(phone)));
1493 menu = sipe_backend_buddy_menu_add(sipe_public,
1494 menu,
1495 label,
1496 SIPE_BUDDY_MENU_MAKE_CALL,
1497 phone);
1498 g_free(tmp);
1499 g_free(label);
1500 g_free(display);
1501 g_free(phone);
1504 return(menu);
1507 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1508 const gchar *buddy_name,
1509 struct sipe_backend_buddy_menu *menu)
1511 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1512 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1513 buddy_name,
1514 NULL);
1515 gchar *self = sip_uri_self(sipe_private);
1517 SIPE_SESSION_FOREACH {
1518 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1520 struct sipe_chat_session *chat_session = session->chat_session;
1521 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1523 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1525 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1527 if (is_conf &&
1528 /* Not conf OP */
1529 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1530 /* We are a conf OP */
1531 conf_op) {
1532 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1533 chat_session->title);
1534 menu = sipe_backend_buddy_menu_add(sipe_public,
1535 menu,
1536 label,
1537 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1538 chat_session);
1539 g_free(label);
1542 if (is_conf &&
1543 /* We are a conf OP */
1544 conf_op) {
1545 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1546 chat_session->title);
1547 menu = sipe_backend_buddy_menu_add(sipe_public,
1548 menu,
1549 label,
1550 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1551 chat_session);
1552 g_free(label);
1555 else
1557 if (!is_conf ||
1558 (is_conf && !session->locked)) {
1559 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1560 chat_session->title);
1561 menu = sipe_backend_buddy_menu_add(sipe_public,
1562 menu,
1563 label,
1564 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1565 chat_session);
1566 g_free(label);
1570 } SIPE_SESSION_FOREACH_END;
1571 g_free(self);
1573 menu = sipe_backend_buddy_menu_add(sipe_public,
1574 menu,
1575 _("New chat"),
1576 SIPE_BUDDY_MENU_NEW_CHAT,
1577 NULL);
1579 /* add buddy's phone numbers if we have call control */
1580 if (sip_csta_is_idle(sipe_private)) {
1582 /* work phone */
1583 menu = buddy_menu_phone(sipe_public,
1584 menu,
1585 buddy,
1586 SIPE_BUDDY_INFO_WORK_PHONE,
1587 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1588 _("Work"));
1589 /* mobile phone */
1590 menu = buddy_menu_phone(sipe_public,
1591 menu,
1592 buddy,
1593 SIPE_BUDDY_INFO_MOBILE_PHONE,
1594 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1595 _("Mobile"));
1597 /* home phone */
1598 menu = buddy_menu_phone(sipe_public,
1599 menu,
1600 buddy,
1601 SIPE_BUDDY_INFO_HOME_PHONE,
1602 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1603 _("Home"));
1605 /* other phone */
1606 menu = buddy_menu_phone(sipe_public,
1607 menu,
1608 buddy,
1609 SIPE_BUDDY_INFO_OTHER_PHONE,
1610 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1611 _("Other"));
1613 /* custom1 phone */
1614 menu = buddy_menu_phone(sipe_public,
1615 menu,
1616 buddy,
1617 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1618 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1619 _("Custom1"));
1623 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1624 buddy,
1625 SIPE_BUDDY_INFO_EMAIL);
1626 if (email) {
1627 menu = sipe_backend_buddy_menu_add(sipe_public,
1628 menu,
1629 _("Send email..."),
1630 SIPE_BUDDY_MENU_SEND_EMAIL,
1631 NULL);
1632 g_free(email);
1636 /* access level control */
1637 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1638 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1639 menu,
1640 _("Access level"),
1641 sipe_ocs2007_access_control_menu(sipe_private,
1642 buddy_name));
1644 return(menu);
1648 Local Variables:
1649 mode: c
1650 c-file-style: "bsd"
1651 indent-tabs-mode: t
1652 tab-width: 8
1653 End: