ms-dlx: generate query in sipe_core_buddy_search()
[siplcs.git] / src / core / sipe-buddy.c
blobafa0099ac1839b5068df2f4c1777e314f6594198
1 /**
2 * @file sipe-buddy.c
4 * pidgin-sipe
6 * Copyright (C) 2010-11 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 <string.h>
28 #include <time.h>
30 #include <glib.h>
32 #include "sipe-common.h"
33 #include "sipmsg.h"
34 #include "sip-csta.h"
35 #include "sip-soap.h"
36 #include "sip-transport.h"
37 #include "sipe-backend.h"
38 #include "sipe-buddy.h"
39 #include "sipe-cal.h"
40 #include "sipe-chat.h"
41 #include "sipe-conf.h"
42 #include "sipe-core.h"
43 #include "sipe-core-private.h"
44 #include "sipe-group.h"
45 #include "sipe-im.h"
46 #include "sipe-nls.h"
47 #include "sipe-ocs2005.h"
48 #include "sipe-ocs2007.h"
49 #include "sipe-schedule.h"
50 #include "sipe-session.h"
51 #include "sipe-subscriptions.h"
52 #include "sipe-svc.h"
53 #include "sipe-utils.h"
54 #include "sipe-webticket.h"
55 #include "sipe-xml.h"
57 static void buddy_free(struct sipe_buddy *buddy)
59 #ifndef _WIN32
61 * We are calling g_hash_table_foreach_steal(). That means that no
62 * key/value deallocation functions are called. Therefore the glib
63 * hash code does not touch the key (buddy->name) or value (buddy)
64 * of the to-be-deleted hash node at all. It follows that we
66 * - MUST free the memory for the key ourselves and
67 * - ARE allowed to do it in this function
69 * Conclusion: glib must be broken on the Windows platform if sipe
70 * crashes with SIGTRAP when closing. You'll have to live
71 * with the memory leak until this is fixed.
73 g_free(buddy->name);
74 #endif
75 g_free(buddy->activity);
76 g_free(buddy->meeting_subject);
77 g_free(buddy->meeting_location);
78 g_free(buddy->note);
80 g_free(buddy->cal_start_time);
81 g_free(buddy->cal_free_busy_base64);
82 g_free(buddy->cal_free_busy);
83 g_free(buddy->last_non_cal_activity);
85 sipe_cal_free_working_hours(buddy->cal_working_hours);
87 g_free(buddy->device_name);
88 g_slist_free(buddy->groups);
89 g_free(buddy);
92 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
93 gpointer buddy,
94 SIPE_UNUSED_PARAMETER gpointer user_data)
96 buddy_free(buddy);
97 /* We must return TRUE as the key/value have already been deleted */
98 return(TRUE);
101 void sipe_buddy_free_all(struct sipe_core_private *sipe_private)
103 g_hash_table_foreach_steal(sipe_private->buddies,
104 buddy_free_cb,
105 NULL);
108 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
109 const gchar *uri,
110 guint activity,
111 const gchar *status_text)
113 struct sipe_buddy *sbuddy;
114 const char *activity_str;
116 if (!sipe_public) return NULL; /* happens on pidgin exit */
118 sbuddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, uri);
119 if (!sbuddy) return NULL;
121 activity_str = sbuddy->activity ? sbuddy->activity :
122 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
123 status_text : NULL;
125 if (activity_str && sbuddy->note) {
126 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
127 } else if (activity_str) {
128 return g_strdup(activity_str);
129 } else if (sbuddy->note) {
130 return g_strdup_printf("<i>%s</i>", sbuddy->note);
131 } else {
132 return NULL;
136 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
137 const gchar *with)
139 sipe_backend_buddy pbuddy;
140 gchar *alias = NULL;
141 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
142 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
144 return alias;
147 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
148 const gchar *who,
149 const gchar *old_group_name,
150 const gchar *new_group_name)
152 struct sipe_buddy * buddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, who);
153 struct sipe_group * old_group = NULL;
154 struct sipe_group * new_group;
156 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
157 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
159 if(!buddy) { // buddy not in roaming list
160 return;
163 if (old_group_name) {
164 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
166 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
168 if (old_group) {
169 buddy->groups = g_slist_remove(buddy->groups, old_group);
170 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
173 if (!new_group) {
174 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
175 } else {
176 buddy->groups = slist_insert_unique_sorted(buddy->groups, new_group, (GCompareFunc)sipe_group_compare);
177 sipe_core_group_set_user(sipe_public, who);
181 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
182 const gchar *uri,
183 const gchar *group_name)
185 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
187 if (!g_hash_table_lookup(sipe_private->buddies, uri)) {
188 struct sipe_buddy *b = g_new0(struct sipe_buddy, 1);
190 SIPE_DEBUG_INFO("sipe_core_buddy_add: %s", uri);
192 b->name = g_strdup(uri);
193 b->just_added = TRUE;
194 g_hash_table_insert(sipe_private->buddies, b->name, b);
196 /* @TODO should go to callback */
197 sipe_subscribe_presence_single(sipe_private, b->name);
199 } else {
200 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
201 uri);
204 sipe_core_buddy_group(sipe_public,
205 uri,
206 NULL,
207 group_name);
211 * Unassociates buddy from group first.
212 * Then see if no groups left, removes buddy completely.
213 * Otherwise updates buddy groups on server.
215 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
216 const gchar *uri,
217 const gchar *group_name)
219 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
220 struct sipe_buddy *b = g_hash_table_lookup(sipe_private->buddies,
221 uri);
223 if (!b) return;
225 if (group_name) {
226 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
227 group_name);
228 if (g) {
229 b->groups = g_slist_remove(b->groups, g);
230 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
231 uri, g->name);
235 if (g_slist_length(b->groups) < 1) {
236 gchar *action_name = sipe_utils_presence_key(uri);
237 sipe_schedule_cancel(sipe_private, action_name);
238 g_free(action_name);
240 g_hash_table_remove(sipe_private->buddies, uri);
242 if (b->name) {
243 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
244 b->name);
245 sip_soap_request(sipe_private,
246 "deleteContact",
247 request);
248 g_free(request);
251 buddy_free(b);
252 } else {
253 /* updates groups on server */
254 sipe_core_group_set_user(sipe_public, b->name);
259 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
260 const gchar *uri,
261 const gchar *status_id)
263 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
264 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies,
265 uri);
267 if (!sbuddy) return;
269 /* Check if on 2005 system contact's calendar,
270 * then set/preserve it.
272 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
273 sipe_backend_buddy_set_status(sipe_public, uri, status_id);
274 } else {
275 sipe_ocs2005_apply_calendar_status(sipe_private,
276 sbuddy,
277 status_id);
281 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
282 const gchar *uri,
283 const gchar *status_name,
284 gboolean is_online,
285 struct sipe_backend_buddy_tooltip *tooltip)
287 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
288 gchar *note = NULL;
289 gboolean is_oof_note = FALSE;
290 const gchar *activity = NULL;
291 gchar *calendar = NULL;
292 const gchar *meeting_subject = NULL;
293 const gchar *meeting_location = NULL;
294 gchar *access_text = NULL;
296 #define SIPE_ADD_BUDDY_INFO(l, t) \
298 gchar *tmp = g_markup_escape_text((t), -1); \
299 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
300 g_free(tmp); \
302 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
303 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
305 if (sipe_public) { /* happens on pidgin exit */
306 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
307 if (sbuddy) {
308 note = sbuddy->note;
309 is_oof_note = sbuddy->is_oof_note;
310 activity = sbuddy->activity;
311 calendar = sipe_cal_get_description(sbuddy);
312 meeting_subject = sbuddy->meeting_subject;
313 meeting_location = sbuddy->meeting_location;
315 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
316 gboolean is_group_access = FALSE;
317 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
318 "user",
319 sipe_get_no_sip_uri(uri),
320 &is_group_access);
321 const char *access_level = sipe_ocs2007_access_level_name(container_id);
322 access_text = is_group_access ?
323 g_strdup(access_level) :
324 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
325 access_level);
329 if (is_online) {
330 const gchar *status_str = activity ? activity : status_name;
332 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
334 if (is_online && !is_empty(calendar)) {
335 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
337 g_free(calendar);
338 if (!is_empty(meeting_location)) {
339 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
340 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
342 if (!is_empty(meeting_subject)) {
343 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
344 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
346 if (note) {
347 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
348 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
349 g_strdup_printf("<i>%s</i>", note));
351 if (access_text) {
352 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
353 g_free(access_text);
357 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
358 const char *uri,
359 sipe_buddy_info_fields propkey,
360 char *property_value)
362 GSList *buddies, *entry;
364 if (property_value)
365 property_value = g_strstrip(property_value);
367 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
368 while (entry) {
369 gchar *prop_str;
370 gchar *server_alias;
371 gchar *alias;
372 sipe_backend_buddy p_buddy = entry->data;
374 /* for Display Name */
375 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
376 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
377 if (property_value && sipe_is_bad_alias(uri, alias)) {
378 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
379 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
381 g_free(alias);
383 server_alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
384 if (!is_empty(property_value) &&
385 (!sipe_strequal(property_value, server_alias) || is_empty(server_alias)) )
387 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
388 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
390 g_free(server_alias);
392 /* for other properties */
393 else {
394 if (!is_empty(property_value)) {
395 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
396 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
397 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
399 g_free(prop_str);
403 entry = entry->next;
405 g_slist_free(buddies);
408 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
409 struct sipmsg *msg,
410 SIPE_UNUSED_PARAMETER struct transaction *trans)
412 struct sipe_backend_search_results *results;
413 sipe_xml *searchResults;
414 const sipe_xml *mrow;
415 guint match_count = 0;
416 gboolean more = FALSE;
417 gchar *secondary;
419 /* valid response? */
420 if (msg->response != 200) {
421 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
422 msg->response);
423 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
424 _("Contact search failed"),
425 NULL);
426 return(FALSE);
429 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
431 /* valid XML? */
432 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
433 if (!searchResults) {
434 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
435 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
436 _("Contact search failed"),
437 NULL);
438 return(FALSE);
441 /* any matches? */
442 mrow = sipe_xml_child(searchResults, "Body/Array/row");
443 if (!mrow) {
444 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
445 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
446 _("No contacts found"),
447 NULL);
449 sipe_xml_free(searchResults);
450 return(FALSE);
453 /* OK, we found something - show the results to the user */
454 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC);
455 if (!results) {
456 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
457 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
458 _("Unable to display the search results"),
459 NULL);
461 sipe_xml_free(searchResults);
462 return FALSE;
465 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
466 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
467 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
468 results,
469 uri_parts[1],
470 sipe_xml_attribute(mrow, "displayName"),
471 sipe_xml_attribute(mrow, "company"),
472 sipe_xml_attribute(mrow, "country"),
473 sipe_xml_attribute(mrow, "email"));
474 g_strfreev(uri_parts);
475 match_count++;
478 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
479 char *data = sipe_xml_data(mrow);
480 more = (g_strcasecmp(data, "true") == 0);
481 g_free(data);
484 secondary = g_strdup_printf(
485 dngettext(PACKAGE_NAME,
486 "Found %d contact%s:",
487 "Found %d contacts%s:", match_count),
488 match_count, more ? _(" (more matched your query)") : "");
490 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
491 results,
492 secondary,
493 more);
494 g_free(secondary);
495 sipe_xml_free(searchResults);
497 return(TRUE);
500 struct ms_dlx_data;
501 struct ms_dlx_data {
502 gchar *search;
503 gchar *other;
504 guint entries;
505 guint max_returns;
506 sipe_svc_callback *callback;
507 /* must call ms_dlx_free() */
508 void (*failed_callback)(struct sipe_core_private *sipe_private,
509 struct ms_dlx_data *mdd);
512 static void ms_dlx_free(struct ms_dlx_data *mdd)
514 g_free(mdd->search);
515 g_free(mdd->other);
516 g_free(mdd);
519 static void ms_dlx_webticket(struct sipe_core_private *sipe_private,
520 const gchar *base_uri,
521 const gchar *auth_uri,
522 const gchar *wsse_security,
523 gpointer callback_data)
525 struct ms_dlx_data *mdd = callback_data;
527 if (wsse_security) {
528 SIPE_DEBUG_INFO("ms_dlx_webticket: got ticket for %s",
529 base_uri);
531 if (sipe_svc_ab_entry_request(sipe_private,
532 auth_uri,
533 wsse_security,
534 mdd->search,
535 mdd->entries,
536 mdd->max_returns,
537 mdd->callback,
538 mdd)) {
539 /* callback data passed down the line */
540 mdd = NULL;
543 } else {
544 /* no ticket: this will show the minmum information */
545 SIPE_DEBUG_ERROR("ms_dlx_webticket: no web ticket for %s",
546 base_uri);
549 if (mdd)
550 mdd->failed_callback(sipe_private, mdd);
553 static void ms_dlx_webticket_request(struct sipe_core_private *sipe_private,
554 struct ms_dlx_data *mdd)
556 if (!sipe_webticket_request(sipe_private,
557 sipe_private->dlx_uri,
558 "AddressBookWebTicketBearer",
559 ms_dlx_webticket,
560 mdd)) {
561 SIPE_DEBUG_ERROR("ms_dlx_webticket_request: couldn't request webticket for %s",
562 sipe_private->dlx_uri);
563 mdd->failed_callback(sipe_private, mdd);
567 static void search_ab_entry_response(struct sipe_core_private *sipe_private,
568 const gchar *uri,
569 SIPE_UNUSED_PARAMETER const gchar *raw,
570 sipe_xml *soap_body,
571 gpointer callback_data)
573 struct ms_dlx_data *mdd = callback_data;
575 if (soap_body) {
577 SIPE_DEBUG_INFO("search_ab_entry_response: received valid SOAP message from service %s",
578 uri);
580 ms_dlx_free(mdd);
582 } else {
583 mdd->failed_callback(sipe_private, mdd);
587 static void search_ab_entry_failed(struct sipe_core_private *sipe_private,
588 struct ms_dlx_data *mdd)
590 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
591 _("Contact search failed"),
592 NULL);
593 ms_dlx_free(mdd);
596 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
597 #define DLX_SEARCH_ITEM \
598 "<AbEntryRequest.ChangeSearchQuery>" \
599 " <SearchOn>%s</SearchOn>" \
600 " <Value>%s</Value>" \
601 "</AbEntryRequest.ChangeSearchQuery>"
603 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
604 const gchar *given_name,
605 const gchar *surname,
606 const gchar *company,
607 const gchar *country)
609 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
610 gchar **attrs = g_new(gchar *, 5);
611 guint i = 0;
612 gboolean dlx = (sipe_private->dlx_uri != NULL);
614 #define ADD_QUERY_ROW(a, v) \
615 if (v) attrs[i++] = g_markup_printf_escaped(dlx ? DLX_SEARCH_ITEM : SIPE_SOAP_SEARCH_ROW, \
616 a, \
619 ADD_QUERY_ROW("givenName", given_name);
620 ADD_QUERY_ROW("sn", surname);
621 ADD_QUERY_ROW("company", company);
622 ADD_QUERY_ROW("c", country);
623 attrs[i] = NULL;
625 if (i) {
626 gchar *query;
628 query = g_strjoinv(NULL, attrs);
629 SIPE_DEBUG_INFO("sipe_core_buddy_search: rows:\n%s",
630 query ? query : "");
632 if (dlx) {
633 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
635 mdd->search = query;
636 mdd->entries = i;
637 mdd->max_returns = 100;
638 mdd->callback = search_ab_entry_response;
639 mdd->failed_callback = search_ab_entry_failed;
641 ms_dlx_webticket_request(sipe_private, mdd);
643 } else {
644 /* no [MS-DLX] server, use Active Directory search instead */
645 sip_soap_directory_search(SIPE_CORE_PRIVATE,
646 100,
647 query,
648 process_search_contact_response,
649 NULL);
650 g_free(query);
654 g_strfreev(attrs);
657 static void get_info_finalize(struct sipe_core_private *sipe_private,
658 struct sipe_backend_buddy_info *info,
659 const gchar *uri,
660 const gchar *server_alias,
661 const gchar *email)
663 sipe_backend_buddy bbuddy;
664 struct sipe_buddy *sbuddy;
665 gchar *alias;
666 gchar *value;
668 if (!info) {
669 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
670 } else {
671 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
673 if (!info)
674 return;
676 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
678 if (is_empty(server_alias)) {
679 value = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
680 bbuddy);
681 if (value) {
682 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
683 info,
684 SIPE_BUDDY_INFO_DISPLAY_NAME,
685 value);
687 } else {
688 value = g_strdup(server_alias);
691 /* present alias if it differs from server alias */
692 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
693 if (alias && !sipe_strequal(alias, value))
695 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
696 info,
697 SIPE_BUDDY_INFO_ALIAS,
698 alias);
700 g_free(alias);
701 g_free(value);
703 if (is_empty(email)) {
704 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
705 bbuddy,
706 SIPE_BUDDY_INFO_EMAIL);
707 if (value) {
708 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
709 info,
710 SIPE_BUDDY_INFO_EMAIL,
711 value);
712 g_free(value);
716 value = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
717 bbuddy,
718 SIPE_BUDDY_INFO_SITE);
719 if (value) {
720 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
721 info,
722 SIPE_BUDDY_INFO_SITE,
723 value);
724 g_free(value);
727 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
728 if (sbuddy && sbuddy->device_name) {
729 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
730 info,
731 SIPE_BUDDY_INFO_DEVICE,
732 sbuddy->device_name);
735 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
739 static void get_info_ab_entry_response(struct sipe_core_private *sipe_private,
740 const gchar *uri,
741 SIPE_UNUSED_PARAMETER const gchar *raw,
742 sipe_xml *soap_body,
743 gpointer callback_data)
745 struct ms_dlx_data *mdd = callback_data;
746 struct sipe_backend_buddy_info *info = NULL;
747 gchar *server_alias = NULL;
748 gchar *email = NULL;
750 if (soap_body) {
751 const sipe_xml *node;
753 SIPE_DEBUG_INFO("get_info_ab_entry_response: received valid SOAP message from service %s",
754 uri);
756 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
758 for (node = sipe_xml_child(soap_body, "Body/SearchAbEntryResponse/SearchAbEntryResult/Items/AbEntry/Attributes/Attribute");
759 node;
760 node = sipe_xml_twin(node)) {
761 gchar *name = sipe_xml_data(sipe_xml_child(node,
762 "Name"));
763 gchar *value = sipe_xml_data(sipe_xml_child(node,
764 "Value"));
765 const sipe_xml *values = sipe_xml_child(node,
766 "Values");
768 /* Single value entries */
769 if (!is_empty(value)) {
771 if (sipe_strcase_equal(name, "displayname")) {
772 g_free(server_alias);
773 server_alias = value;
774 value = NULL;
775 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
776 info,
777 SIPE_BUDDY_INFO_DISPLAY_NAME,
778 server_alias);
779 } else if (sipe_strcase_equal(name, "mail")) {
780 g_free(email);
781 email = value;
782 value = NULL;
783 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
784 info,
785 SIPE_BUDDY_INFO_EMAIL,
786 email);
787 } else if (sipe_strcase_equal(name, "title")) {
788 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
789 info,
790 SIPE_BUDDY_INFO_JOB_TITLE,
791 value);
792 } else if (sipe_strcase_equal(name, "company")) {
793 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
794 info,
795 SIPE_BUDDY_INFO_COMPANY,
796 value);
797 } else if (sipe_strcase_equal(name, "country")) {
798 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
799 info,
800 SIPE_BUDDY_INFO_COUNTRY,
801 value);
804 } else if (values) {
805 gchar *first = sipe_xml_data(sipe_xml_child(values,
806 "string"));
808 if (sipe_strcase_equal(name, "telephonenumber")) {
809 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
810 info,
811 SIPE_BUDDY_INFO_WORK_PHONE,
812 first);
815 g_free(first);
818 g_free(value);
819 g_free(name);
823 /* this will show the minmum information */
824 get_info_finalize(sipe_private,
825 info,
826 mdd->other,
827 server_alias,
828 email);
830 g_free(email);
831 g_free(server_alias);
832 ms_dlx_free(mdd);
835 static void get_info_ab_entry_failed(struct sipe_core_private *sipe_private,
836 struct ms_dlx_data *mdd)
838 /* request failed: this will show the minmum information */
839 get_info_finalize(sipe_private,
840 NULL,
841 mdd->other,
842 NULL,
843 NULL);
844 ms_dlx_free(mdd);
847 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
848 struct sipmsg *msg,
849 struct transaction *trans)
851 const gchar *uri = trans->payload->data;
852 struct sipe_backend_buddy_info *info = NULL;
853 gchar *server_alias = NULL;
854 gchar *email = NULL;
856 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
857 uri, sipe_private->username);
859 if (msg->response != 200) {
860 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
861 } else {
862 sipe_xml *searchResults;
863 const sipe_xml *mrow;
865 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
866 msg->body ? msg->body : "");
868 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
869 if (!searchResults) {
871 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
873 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
874 const gchar *value;
875 gchar *phone_number;
877 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
879 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
880 email = g_strdup(sipe_xml_attribute(mrow, "email"));
881 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
884 * For 2007 system we will take this from ContactCard -
885 * it has cleaner tel: URIs at least
887 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
888 char *tel_uri = sip_to_tel_uri(phone_number);
889 /* trims its parameters, so call first */
890 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
891 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
892 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
893 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
894 g_free(tel_uri);
897 if (!is_empty(server_alias)) {
898 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
899 info,
900 SIPE_BUDDY_INFO_DISPLAY_NAME,
901 server_alias);
903 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
904 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
905 info,
906 SIPE_BUDDY_INFO_JOB_TITLE,
907 value);
909 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
910 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
911 info,
912 SIPE_BUDDY_INFO_OFFICE,
913 value);
915 if (!is_empty(phone_number)) {
916 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
917 info,
918 SIPE_BUDDY_INFO_WORK_PHONE,
919 phone_number);
921 g_free(phone_number);
922 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
923 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
924 info,
925 SIPE_BUDDY_INFO_COMPANY,
926 value);
928 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
929 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
930 info,
931 SIPE_BUDDY_INFO_CITY,
932 value);
934 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
935 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
936 info,
937 SIPE_BUDDY_INFO_STATE,
938 value);
940 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
941 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
942 info,
943 SIPE_BUDDY_INFO_COUNTRY,
944 value);
946 if (!is_empty(email)) {
947 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
948 info,
949 SIPE_BUDDY_INFO_EMAIL,
950 email);
953 sipe_xml_free(searchResults);
956 /* this will show the minmum information */
957 get_info_finalize(sipe_private,
958 info,
959 uri,
960 server_alias,
961 email);
963 g_free(server_alias);
964 g_free(email);
966 return TRUE;
969 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
970 const gchar *who)
972 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
974 if (sipe_private->dlx_uri) {
975 struct ms_dlx_data *mdd = g_new0(struct ms_dlx_data, 1);
977 mdd->search = g_markup_printf_escaped(DLX_SEARCH_ITEM,
978 "msRTCSIP-PrimaryUserAddress",
979 who);
980 mdd->other = g_strdup(who);
981 mdd->entries = 1;
982 mdd->max_returns = 1;
983 mdd->callback = get_info_ab_entry_response;
984 mdd->failed_callback = get_info_ab_entry_failed;
986 ms_dlx_webticket_request(sipe_private, mdd);
988 } else {
989 /* no [MS-DLX] server, use Active Directory search instead */
990 gchar *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
991 "msRTCSIP-PrimaryUserAddress",
992 who);
993 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
995 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s",
996 row ? row : "");
998 payload->destroy = g_free;
999 payload->data = g_strdup(who);
1001 sip_soap_directory_search(sipe_private,
1003 row,
1004 process_get_info_response,
1005 payload);
1006 g_free(row);
1010 /* Buddy menu callbacks*/
1012 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
1013 const gchar *who)
1015 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1017 /* 2007+ conference */
1018 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
1019 sipe_conf_add(sipe_private, who);
1021 /* 2005- multiparty chat */
1022 } else {
1023 gchar *self = sip_uri_self(sipe_private);
1024 struct sip_session *session;
1026 session = sipe_session_add_chat(sipe_private,
1027 NULL,
1028 TRUE,
1029 self);
1030 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
1031 session->chat_session,
1032 session->chat_session->title,
1033 self);
1034 g_free(self);
1036 sipe_im_invite(sipe_private, session, who,
1037 NULL, NULL, NULL, FALSE);
1041 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
1042 const gchar *who)
1044 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1045 who,
1046 NULL);
1047 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1048 buddy,
1049 SIPE_BUDDY_INFO_EMAIL);
1051 if (email) {
1052 gchar *command_line = g_strdup_printf(
1053 #ifdef _WIN32
1054 "cmd /c start"
1055 #else
1056 "xdg-email"
1057 #endif
1058 " mailto:%s", email);
1059 g_free(email);
1061 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
1062 command_line);
1063 g_spawn_command_line_async(command_line, NULL);
1064 g_free(command_line);
1066 } else {
1067 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
1068 who);
1072 /* Buddy menu */
1074 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
1075 struct sipe_backend_buddy_menu *menu,
1076 sipe_backend_buddy buddy,
1077 sipe_buddy_info_fields id_phone,
1078 sipe_buddy_info_fields id_display,
1079 const gchar *type)
1081 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
1082 buddy,
1083 id_phone);
1084 if (phone) {
1085 gchar *display = sipe_backend_buddy_get_string(sipe_public,
1086 buddy,
1087 id_display);
1088 gchar *tmp = NULL;
1089 gchar *label = g_strdup_printf("%s %s",
1090 type,
1091 display ? display :
1092 (tmp = sip_tel_uri_denormalize(phone)));
1093 menu = sipe_backend_buddy_menu_add(sipe_public,
1094 menu,
1095 label,
1096 SIPE_BUDDY_MENU_MAKE_CALL,
1097 phone);
1098 g_free(tmp);
1099 g_free(label);
1100 g_free(display);
1101 g_free(phone);
1104 return(menu);
1107 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
1108 const gchar *buddy_name,
1109 struct sipe_backend_buddy_menu *menu)
1111 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
1112 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
1113 buddy_name,
1114 NULL);
1115 gchar *self = sip_uri_self(sipe_private);
1117 SIPE_SESSION_FOREACH {
1118 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
1120 struct sipe_chat_session *chat_session = session->chat_session;
1121 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
1123 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
1125 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
1127 if (is_conf &&
1128 /* Not conf OP */
1129 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
1130 /* We are a conf OP */
1131 conf_op) {
1132 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
1133 chat_session->title);
1134 menu = sipe_backend_buddy_menu_add(sipe_public,
1135 menu,
1136 label,
1137 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
1138 chat_session);
1139 g_free(label);
1142 if (is_conf &&
1143 /* We are a conf OP */
1144 conf_op) {
1145 gchar *label = g_strdup_printf(_("Remove from '%s'"),
1146 chat_session->title);
1147 menu = sipe_backend_buddy_menu_add(sipe_public,
1148 menu,
1149 label,
1150 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
1151 chat_session);
1152 g_free(label);
1155 else
1157 if (!is_conf ||
1158 (is_conf && !session->locked)) {
1159 gchar *label = g_strdup_printf(_("Invite to '%s'"),
1160 chat_session->title);
1161 menu = sipe_backend_buddy_menu_add(sipe_public,
1162 menu,
1163 label,
1164 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
1165 chat_session);
1166 g_free(label);
1170 } SIPE_SESSION_FOREACH_END;
1171 g_free(self);
1173 menu = sipe_backend_buddy_menu_add(sipe_public,
1174 menu,
1175 _("New chat"),
1176 SIPE_BUDDY_MENU_NEW_CHAT,
1177 NULL);
1179 /* add buddy's phone numbers if we have call control */
1180 if (sip_csta_is_idle(sipe_private)) {
1182 /* work phone */
1183 menu = buddy_menu_phone(sipe_public,
1184 menu,
1185 buddy,
1186 SIPE_BUDDY_INFO_WORK_PHONE,
1187 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
1188 _("Work"));
1189 /* mobile phone */
1190 menu = buddy_menu_phone(sipe_public,
1191 menu,
1192 buddy,
1193 SIPE_BUDDY_INFO_MOBILE_PHONE,
1194 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
1195 _("Mobile"));
1197 /* home phone */
1198 menu = buddy_menu_phone(sipe_public,
1199 menu,
1200 buddy,
1201 SIPE_BUDDY_INFO_HOME_PHONE,
1202 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
1203 _("Home"));
1205 /* other phone */
1206 menu = buddy_menu_phone(sipe_public,
1207 menu,
1208 buddy,
1209 SIPE_BUDDY_INFO_OTHER_PHONE,
1210 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
1211 _("Other"));
1213 /* custom1 phone */
1214 menu = buddy_menu_phone(sipe_public,
1215 menu,
1216 buddy,
1217 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1218 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1219 _("Custom1"));
1223 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1224 buddy,
1225 SIPE_BUDDY_INFO_EMAIL);
1226 if (email) {
1227 menu = sipe_backend_buddy_menu_add(sipe_public,
1228 menu,
1229 _("Send email..."),
1230 SIPE_BUDDY_MENU_SEND_EMAIL,
1231 NULL);
1232 g_free(email);
1236 /* access level control */
1237 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007))
1238 menu = sipe_backend_buddy_sub_menu_add(sipe_public,
1239 menu,
1240 _("Access level"),
1241 sipe_ocs2007_access_control_menu(sipe_private,
1242 buddy_name));
1244 return(menu);
1248 Local Variables:
1249 mode: c
1250 c-file-style: "bsd"
1251 indent-tabs-mode: t
1252 tab-width: 8
1253 End: