calendar: clean up API to remove dependency
[siplcs.git] / src / core / sipe-buddy.c
blobb879faa2d96a1b78f09b1959206ffdf4d3489e6b
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-utils.h"
53 #include "sipe-xml.h"
55 static void buddy_free(struct sipe_buddy *buddy)
57 #ifndef _WIN32
59 * We are calling g_hash_table_foreach_steal(). That means that no
60 * key/value deallocation functions are called. Therefore the glib
61 * hash code does not touch the key (buddy->name) or value (buddy)
62 * of the to-be-deleted hash node at all. It follows that we
64 * - MUST free the memory for the key ourselves and
65 * - ARE allowed to do it in this function
67 * Conclusion: glib must be broken on the Windows platform if sipe
68 * crashes with SIGTRAP when closing. You'll have to live
69 * with the memory leak until this is fixed.
71 g_free(buddy->name);
72 #endif
73 g_free(buddy->activity);
74 g_free(buddy->meeting_subject);
75 g_free(buddy->meeting_location);
76 g_free(buddy->note);
78 g_free(buddy->cal_start_time);
79 g_free(buddy->cal_free_busy_base64);
80 g_free(buddy->cal_free_busy);
81 g_free(buddy->last_non_cal_activity);
83 sipe_cal_free_working_hours(buddy->cal_working_hours);
85 g_free(buddy->device_name);
86 g_slist_free(buddy->groups);
87 g_free(buddy);
90 static gboolean buddy_free_cb(SIPE_UNUSED_PARAMETER gpointer key,
91 gpointer buddy,
92 SIPE_UNUSED_PARAMETER gpointer user_data)
94 buddy_free(buddy);
95 /* We must return TRUE as the key/value have already been deleted */
96 return(TRUE);
99 void sipe_buddy_free_all(struct sipe_core_private *sipe_private)
101 g_hash_table_foreach_steal(sipe_private->buddies,
102 buddy_free_cb,
103 NULL);
106 gchar *sipe_core_buddy_status(struct sipe_core_public *sipe_public,
107 const gchar *uri,
108 guint activity,
109 const gchar *status_text)
111 struct sipe_buddy *sbuddy;
112 const char *activity_str;
114 if (!sipe_public) return NULL; /* happens on pidgin exit */
116 sbuddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, uri);
117 if (!sbuddy) return NULL;
119 activity_str = sbuddy->activity ? sbuddy->activity :
120 (activity == SIPE_ACTIVITY_BUSY) || (activity == SIPE_ACTIVITY_BRB) ?
121 status_text : NULL;
123 if (activity_str && sbuddy->note) {
124 return g_strdup_printf("%s - <i>%s</i>", activity_str, sbuddy->note);
125 } else if (activity_str) {
126 return g_strdup(activity_str);
127 } else if (sbuddy->note) {
128 return g_strdup_printf("<i>%s</i>", sbuddy->note);
129 } else {
130 return NULL;
134 gchar *sipe_buddy_get_alias(struct sipe_core_private *sipe_private,
135 const gchar *with)
137 sipe_backend_buddy pbuddy;
138 gchar *alias = NULL;
139 if ((pbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, with, NULL))) {
140 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, pbuddy);
142 return alias;
145 void sipe_core_buddy_group(struct sipe_core_public *sipe_public,
146 const gchar *who,
147 const gchar *old_group_name,
148 const gchar *new_group_name)
150 struct sipe_buddy * buddy = g_hash_table_lookup(SIPE_CORE_PRIVATE->buddies, who);
151 struct sipe_group * old_group = NULL;
152 struct sipe_group * new_group;
154 SIPE_DEBUG_INFO("sipe_core_buddy_group: who:%s old_group_name:%s new_group_name:%s",
155 who ? who : "", old_group_name ? old_group_name : "", new_group_name ? new_group_name : "");
157 if(!buddy) { // buddy not in roaming list
158 return;
161 if (old_group_name) {
162 old_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, old_group_name);
164 new_group = sipe_group_find_by_name(SIPE_CORE_PRIVATE, new_group_name);
166 if (old_group) {
167 buddy->groups = g_slist_remove(buddy->groups, old_group);
168 SIPE_DEBUG_INFO("sipe_core_buddy_group: buddy %s removed from old group %s", who, old_group_name);
171 if (!new_group) {
172 sipe_group_create(SIPE_CORE_PRIVATE, new_group_name, who);
173 } else {
174 buddy->groups = slist_insert_unique_sorted(buddy->groups, new_group, (GCompareFunc)sipe_group_compare);
175 sipe_core_group_set_user(sipe_public, who);
179 void sipe_core_buddy_add(struct sipe_core_public *sipe_public,
180 const gchar *uri,
181 const gchar *group_name)
183 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
185 if (!g_hash_table_lookup(sipe_private->buddies, uri)) {
186 struct sipe_buddy *b = g_new0(struct sipe_buddy, 1);
188 SIPE_DEBUG_INFO("sipe_core_buddy_add: %s", uri);
190 b->name = g_strdup(uri);
191 b->just_added = TRUE;
192 g_hash_table_insert(sipe_private->buddies, b->name, b);
194 /* @TODO should go to callback */
195 sipe_subscribe_presence_single(sipe_private, b->name);
197 } else {
198 SIPE_DEBUG_INFO("sipe_core_buddy_add: buddy %s already in internal list",
199 uri);
202 sipe_core_buddy_group(sipe_public,
203 uri,
204 NULL,
205 group_name);
209 * Unassociates buddy from group first.
210 * Then see if no groups left, removes buddy completely.
211 * Otherwise updates buddy groups on server.
213 void sipe_core_buddy_remove(struct sipe_core_public *sipe_public,
214 const gchar *uri,
215 const gchar *group_name)
217 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
218 struct sipe_buddy *b = g_hash_table_lookup(sipe_private->buddies,
219 uri);
221 if (!b) return;
223 if (group_name) {
224 struct sipe_group *g = sipe_group_find_by_name(sipe_private,
225 group_name);
226 if (g) {
227 b->groups = g_slist_remove(b->groups, g);
228 SIPE_DEBUG_INFO("sipe_core_buddy_remove: buddy %s removed from group %s",
229 uri, g->name);
233 if (g_slist_length(b->groups) < 1) {
234 gchar *action_name = sipe_utils_presence_key(uri);
235 sipe_schedule_cancel(sipe_private, action_name);
236 g_free(action_name);
238 g_hash_table_remove(sipe_private->buddies, uri);
240 if (b->name) {
241 gchar *request = g_strdup_printf("<m:URI>%s</m:URI>",
242 b->name);
243 sip_soap_request(sipe_private,
244 "deleteContact",
245 request);
246 g_free(request);
249 buddy_free(b);
250 } else {
251 /* updates groups on server */
252 sipe_core_group_set_user(sipe_public, b->name);
257 void sipe_core_buddy_got_status(struct sipe_core_public *sipe_public,
258 const gchar *uri,
259 const gchar *status_id)
261 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
262 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies,
263 uri);
265 if (!sbuddy) return;
267 /* Check if on 2005 system contact's calendar,
268 * then set/preserve it.
270 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
271 sipe_backend_buddy_set_status(sipe_public, uri, status_id);
272 } else {
273 sipe_ocs2005_apply_calendar_status(sipe_private,
274 sbuddy,
275 status_id);
279 void sipe_core_buddy_tooltip_info(struct sipe_core_public *sipe_public,
280 const gchar *uri,
281 const gchar *status_name,
282 gboolean is_online,
283 struct sipe_backend_buddy_tooltip *tooltip)
285 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
286 gchar *note = NULL;
287 gboolean is_oof_note = FALSE;
288 const gchar *activity = NULL;
289 gchar *calendar = NULL;
290 const gchar *meeting_subject = NULL;
291 const gchar *meeting_location = NULL;
292 gchar *access_text = NULL;
294 #define SIPE_ADD_BUDDY_INFO(l, t) \
296 gchar *tmp = g_markup_escape_text((t), -1); \
297 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), tmp); \
298 g_free(tmp); \
300 #define SIPE_ADD_BUDDY_INFO_NOESCAPE(l, t) \
301 sipe_backend_buddy_tooltip_add(sipe_public, tooltip, (l), (t))
303 if (sipe_public) { /* happens on pidgin exit */
304 struct sipe_buddy *sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
305 if (sbuddy) {
306 note = sbuddy->note;
307 is_oof_note = sbuddy->is_oof_note;
308 activity = sbuddy->activity;
309 calendar = sipe_cal_get_description(sbuddy);
310 meeting_subject = sbuddy->meeting_subject;
311 meeting_location = sbuddy->meeting_location;
313 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
314 gboolean is_group_access = FALSE;
315 const int container_id = sipe_ocs2007_find_access_level(sipe_private,
316 "user",
317 sipe_get_no_sip_uri(uri),
318 &is_group_access);
319 const char *access_level = sipe_ocs2007_access_level_name(container_id);
320 access_text = is_group_access ?
321 g_strdup(access_level) :
322 g_strdup_printf(SIPE_OCS2007_INDENT_MARKED_FMT,
323 access_level);
327 if (is_online) {
328 const gchar *status_str = activity ? activity : status_name;
330 SIPE_ADD_BUDDY_INFO(_("Status"), status_str);
332 if (is_online && !is_empty(calendar)) {
333 SIPE_ADD_BUDDY_INFO(_("Calendar"), calendar);
335 g_free(calendar);
336 if (!is_empty(meeting_location)) {
337 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting location: '%s'", uri, meeting_location);
338 SIPE_ADD_BUDDY_INFO(_("Meeting in"), meeting_location);
340 if (!is_empty(meeting_subject)) {
341 SIPE_DEBUG_INFO("sipe_tooltip_text: %s meeting subject: '%s'", uri, meeting_subject);
342 SIPE_ADD_BUDDY_INFO(_("Meeting about"), meeting_subject);
344 if (note) {
345 SIPE_DEBUG_INFO("sipe_tooltip_text: %s note: '%s'", uri, note);
346 SIPE_ADD_BUDDY_INFO_NOESCAPE(is_oof_note ? _("Out of office note") : _("Note"),
347 g_strdup_printf("<i>%s</i>", note));
349 if (access_text) {
350 SIPE_ADD_BUDDY_INFO(_("Access level"), access_text);
351 g_free(access_text);
355 void sipe_buddy_update_property(struct sipe_core_private *sipe_private,
356 const char *uri,
357 sipe_buddy_info_fields propkey,
358 char *property_value)
360 GSList *buddies, *entry;
362 if (property_value)
363 property_value = g_strstrip(property_value);
365 entry = buddies = sipe_backend_buddy_find_all(SIPE_CORE_PUBLIC, uri, NULL); /* all buddies in different groups */
366 while (entry) {
367 gchar *prop_str;
368 gchar *server_alias;
369 gchar *alias;
370 sipe_backend_buddy p_buddy = entry->data;
372 /* for Display Name */
373 if (propkey == SIPE_BUDDY_INFO_DISPLAY_NAME) {
374 alias = sipe_backend_buddy_get_alias(SIPE_CORE_PUBLIC, p_buddy);
375 if (property_value && sipe_is_bad_alias(uri, alias)) {
376 SIPE_DEBUG_INFO("Replacing alias for %s with %s", uri, property_value);
377 sipe_backend_buddy_set_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
379 g_free(alias);
381 server_alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC, p_buddy);
382 if (!is_empty(property_value) &&
383 (!sipe_strequal(property_value, server_alias) || is_empty(server_alias)) )
385 SIPE_DEBUG_INFO("Replacing service alias for %s with %s", uri, property_value);
386 sipe_backend_buddy_set_server_alias(SIPE_CORE_PUBLIC, p_buddy, property_value);
388 g_free(server_alias);
390 /* for other properties */
391 else {
392 if (!is_empty(property_value)) {
393 prop_str = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC, p_buddy, propkey);
394 if (!prop_str || !sipe_strcase_equal(prop_str, property_value)) {
395 sipe_backend_buddy_set_string(SIPE_CORE_PUBLIC, p_buddy, propkey, property_value);
397 g_free(prop_str);
401 entry = entry->next;
403 g_slist_free(buddies);
406 static gboolean process_search_contact_response(struct sipe_core_private *sipe_private,
407 struct sipmsg *msg,
408 SIPE_UNUSED_PARAMETER struct transaction *trans)
410 struct sipe_backend_search_results *results;
411 sipe_xml *searchResults;
412 const sipe_xml *mrow;
413 guint match_count = 0;
414 gboolean more = FALSE;
415 gchar *secondary;
417 /* valid response? */
418 if (msg->response != 200) {
419 SIPE_DEBUG_ERROR("process_search_contact_response: request failed (%d)",
420 msg->response);
421 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
422 _("Contact search failed"),
423 NULL);
424 return(FALSE);
427 SIPE_DEBUG_INFO("process_search_contact_response: body:\n%s", msg->body ? msg->body : "");
429 /* valid XML? */
430 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
431 if (!searchResults) {
432 SIPE_DEBUG_INFO_NOFORMAT("process_search_contact_response: no parseable searchResults");
433 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
434 _("Contact search failed"),
435 NULL);
436 return(FALSE);
439 /* any matches? */
440 mrow = sipe_xml_child(searchResults, "Body/Array/row");
441 if (!mrow) {
442 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: no matches");
443 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
444 _("No contacts found"),
445 NULL);
447 sipe_xml_free(searchResults);
448 return(FALSE);
451 /* OK, we found something - show the results to the user */
452 results = sipe_backend_search_results_start(SIPE_CORE_PUBLIC);
453 if (!results) {
454 SIPE_DEBUG_ERROR_NOFORMAT("process_search_contact_response: Unable to display the search results.");
455 sipe_backend_notify_error(SIPE_CORE_PUBLIC,
456 _("Unable to display the search results"),
457 NULL);
459 sipe_xml_free(searchResults);
460 return FALSE;
463 for (/* initialized above */ ; mrow; mrow = sipe_xml_twin(mrow)) {
464 gchar **uri_parts = g_strsplit(sipe_xml_attribute(mrow, "uri"), ":", 2);
465 sipe_backend_search_results_add(SIPE_CORE_PUBLIC,
466 results,
467 uri_parts[1],
468 sipe_xml_attribute(mrow, "displayName"),
469 sipe_xml_attribute(mrow, "company"),
470 sipe_xml_attribute(mrow, "country"),
471 sipe_xml_attribute(mrow, "email"));
472 g_strfreev(uri_parts);
473 match_count++;
476 if ((mrow = sipe_xml_child(searchResults, "Body/directorySearch/moreAvailable")) != NULL) {
477 char *data = sipe_xml_data(mrow);
478 more = (g_strcasecmp(data, "true") == 0);
479 g_free(data);
482 secondary = g_strdup_printf(
483 dngettext(PACKAGE_NAME,
484 "Found %d contact%s:",
485 "Found %d contacts%s:", match_count),
486 match_count, more ? _(" (more matched your query)") : "");
488 sipe_backend_search_results_finalize(SIPE_CORE_PUBLIC,
489 results,
490 secondary,
491 more);
492 g_free(secondary);
493 sipe_xml_free(searchResults);
495 return(TRUE);
498 #define SIPE_SOAP_SEARCH_ROW "<m:row m:attrib=\"%s\" m:value=\"%s\"/>"
500 void sipe_core_buddy_search(struct sipe_core_public *sipe_public,
501 const gchar *given_name,
502 const gchar *surname,
503 const gchar *company,
504 const gchar *country)
506 gchar **attrs = g_new(gchar *, 5);
507 guint i = 0;
509 if (!attrs) return;
511 #define ADD_QUERY_ROW(a, v) \
512 if (v) attrs[i++] = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW, a, v)
514 ADD_QUERY_ROW("givenName", given_name);
515 ADD_QUERY_ROW("sn", surname);
516 ADD_QUERY_ROW("company", company);
517 ADD_QUERY_ROW("c", country);
519 if (i) {
520 gchar *query;
522 attrs[i] = NULL;
523 query = g_strjoinv(NULL, attrs);
524 SIPE_DEBUG_INFO("sipe_core_buddy_search: rows:\n%s",
525 query ? query : "");
526 sip_soap_directory_search(SIPE_CORE_PRIVATE,
527 100,
528 query,
529 process_search_contact_response,
530 NULL);
531 g_free(query);
534 g_strfreev(attrs);
537 static gboolean process_options_response(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
538 struct sipmsg *msg,
539 SIPE_UNUSED_PARAMETER struct transaction *trans)
541 if (msg->response != 200) {
542 SIPE_DEBUG_INFO("process_options_response: OPTIONS response is %d",
543 msg->response);
544 return(FALSE);
545 } else {
546 SIPE_DEBUG_INFO("process_options_response: body:\n%s",
547 msg->body ? msg->body : "");
548 return(TRUE);
552 /* Asks UA/proxy about its capabilities */
553 static void sipe_options_request(struct sipe_core_private *sipe_private,
554 const char *who)
556 gchar *to = sip_uri(who);
557 gchar *contact = get_contact(sipe_private);
558 gchar *request = g_strdup_printf("Accept: application/sdp\r\n"
559 "Contact: %s\r\n",
560 contact);
561 g_free(contact);
563 sip_transport_request(sipe_private,
564 "OPTIONS",
567 request,
568 NULL,
569 NULL,
570 process_options_response);
572 g_free(to);
573 g_free(request);
576 static gboolean process_get_info_response(struct sipe_core_private *sipe_private,
577 struct sipmsg *msg,
578 struct transaction *trans)
580 const gchar *uri = trans->payload->data;
581 sipe_backend_buddy bbuddy;
582 struct sipe_backend_buddy_info *info;
583 struct sipe_buddy *sbuddy;
584 gchar *alias = NULL;
585 gchar *device_name = NULL;
586 gchar *server_alias = NULL;
587 gchar *phone_number = NULL;
588 gchar *email = NULL;
589 gchar *site;
591 SIPE_DEBUG_INFO("Fetching %s's user info for %s",
592 uri, sipe_private->username);
594 info = sipe_backend_buddy_info_start(SIPE_CORE_PUBLIC);
595 if (!info) return(FALSE);
597 bbuddy = sipe_backend_buddy_find(SIPE_CORE_PUBLIC, uri, NULL);
598 alias = sipe_backend_buddy_get_local_alias(SIPE_CORE_PUBLIC, bbuddy);
600 /* will query buddy UA's capabilities and send answer to log */
601 if (sipe_backend_debug_enabled())
602 sipe_options_request(sipe_private, uri);
604 sbuddy = g_hash_table_lookup(sipe_private->buddies, uri);
605 if (sbuddy) {
606 device_name = sbuddy->device_name ? g_strdup(sbuddy->device_name) : NULL;
609 if (msg->response != 200) {
610 SIPE_DEBUG_INFO("process_get_info_response: SERVICE response is %d", msg->response);
611 } else {
612 sipe_xml *searchResults;
613 const sipe_xml *mrow;
615 SIPE_DEBUG_INFO("process_get_info_response: body:\n%s",
616 msg->body ? msg->body : "");
618 searchResults = sipe_xml_parse(msg->body, msg->bodylen);
619 if (!searchResults) {
621 SIPE_DEBUG_INFO_NOFORMAT("process_get_info_response: no parseable searchResults");
623 } else if ((mrow = sipe_xml_child(searchResults, "Body/Array/row"))) {
624 const gchar *value;
626 server_alias = g_strdup(sipe_xml_attribute(mrow, "displayName"));
627 email = g_strdup(sipe_xml_attribute(mrow, "email"));
628 phone_number = g_strdup(sipe_xml_attribute(mrow, "phone"));
631 * For 2007 system we will take this from ContactCard -
632 * it has cleaner tel: URIs at least
634 if (!SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
635 char *tel_uri = sip_to_tel_uri(phone_number);
636 /* trims its parameters, so call first */
637 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_DISPLAY_NAME, server_alias);
638 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_EMAIL, email);
639 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE, tel_uri);
640 sipe_buddy_update_property(sipe_private, uri, SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY, phone_number);
641 g_free(tel_uri);
644 if (server_alias && strlen(server_alias) > 0) {
645 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
646 info,
647 _("Display name"),
648 server_alias);
650 if ((value = sipe_xml_attribute(mrow, "title")) && strlen(value) > 0) {
651 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
652 info,
653 _("Job title"),
654 value);
656 if ((value = sipe_xml_attribute(mrow, "office")) && strlen(value) > 0) {
657 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
658 info,
659 _("Office"),
660 value);
662 if (phone_number && strlen(phone_number) > 0) {
663 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
664 info,
665 _("Business phone"),
666 phone_number);
668 if ((value = sipe_xml_attribute(mrow, "company")) && strlen(value) > 0) {
669 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
670 info,
671 _("Company"),
672 value);
674 if ((value = sipe_xml_attribute(mrow, "city")) && strlen(value) > 0) {
675 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
676 info,
677 _("City"),
678 value);
680 if ((value = sipe_xml_attribute(mrow, "state")) && strlen(value) > 0) {
681 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
682 info,
683 _("State"),
684 value);
686 if ((value = sipe_xml_attribute(mrow, "country")) && strlen(value) > 0) {
687 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
688 info,
689 _("Country"),
690 value);
692 if (email && strlen(email) > 0) {
693 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
694 info,
695 _("Email address"),
696 email);
700 sipe_xml_free(searchResults);
703 sipe_backend_buddy_info_break(SIPE_CORE_PUBLIC, info);
705 if (is_empty(server_alias)) {
706 g_free(server_alias);
707 server_alias = sipe_backend_buddy_get_server_alias(SIPE_CORE_PUBLIC,
708 bbuddy);
709 if (server_alias) {
710 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
711 info,
712 _("Display name"),
713 server_alias);
717 /* present alias if it differs from server alias */
718 if (alias && !sipe_strequal(alias, server_alias))
720 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
721 info,
722 _("Alias"),
723 alias);
726 if (is_empty(email)) {
727 g_free(email);
728 email = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
729 bbuddy,
730 SIPE_BUDDY_INFO_EMAIL);
731 if (email) {
732 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
733 info,
734 _("Email address"),
735 email);
739 site = sipe_backend_buddy_get_string(SIPE_CORE_PUBLIC,
740 bbuddy,
741 SIPE_BUDDY_INFO_SITE);
742 if (site) {
743 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
744 info,
745 _("Site"),
746 site);
747 g_free(site);
750 if (device_name) {
751 sipe_backend_buddy_info_add(SIPE_CORE_PUBLIC,
752 info,
753 _("Device"),
754 device_name);
757 sipe_backend_buddy_info_finalize(SIPE_CORE_PUBLIC, info, uri);
759 g_free(phone_number);
760 g_free(server_alias);
761 g_free(email);
762 g_free(device_name);
763 g_free(alias);
765 return TRUE;
769 * AD search first, LDAP based
771 void sipe_core_buddy_get_info(struct sipe_core_public *sipe_public,
772 const gchar *who)
774 char *row = g_markup_printf_escaped(SIPE_SOAP_SEARCH_ROW,
775 "msRTCSIP-PrimaryUserAddress",
776 who);
777 struct transaction_payload *payload = g_new0(struct transaction_payload, 1);
779 SIPE_DEBUG_INFO("sipe_core_buddy_get_info: row: %s", row ? row : "");
781 payload->destroy = g_free;
782 payload->data = g_strdup(who);
784 sip_soap_directory_search(SIPE_CORE_PRIVATE,
786 row,
787 process_get_info_response,
788 payload);
789 g_free(row);
792 /* Buddy menu callbacks*/
794 void sipe_core_buddy_new_chat(struct sipe_core_public *sipe_public,
795 const gchar *who)
797 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
799 /* 2007+ conference */
800 if (SIPE_CORE_PRIVATE_FLAG_IS(OCS2007)) {
801 sipe_conf_add(sipe_private, who);
803 /* 2005- multiparty chat */
804 } else {
805 gchar *self = sip_uri_self(sipe_private);
806 struct sip_session *session;
808 session = sipe_session_add_chat(sipe_private,
809 NULL,
810 TRUE,
811 self);
812 session->chat_session->backend = sipe_backend_chat_create(SIPE_CORE_PUBLIC,
813 session->chat_session,
814 session->chat_session->title,
815 self);
816 g_free(self);
818 sipe_im_invite(sipe_private, session, who,
819 NULL, NULL, NULL, FALSE);
823 void sipe_core_buddy_send_email(struct sipe_core_public *sipe_public,
824 const gchar *who)
826 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
827 who,
828 NULL);
829 gchar *email = sipe_backend_buddy_get_string(sipe_public,
830 buddy,
831 SIPE_BUDDY_INFO_EMAIL);
833 if (email) {
834 gchar *command_line = g_strdup_printf(
835 #ifdef _WIN32
836 "cmd /c start"
837 #else
838 "xdg-email"
839 #endif
840 " mailto:%s", email);
841 g_free(email);
843 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: going to call email client: %s",
844 command_line);
845 g_spawn_command_line_async(command_line, NULL);
846 g_free(command_line);
848 } else {
849 SIPE_DEBUG_INFO("sipe_core_buddy_send_email: no email address stored for buddy=%s",
850 who);
854 /* Buddy menu */
856 static struct sipe_backend_buddy_menu *buddy_menu_phone(struct sipe_core_public *sipe_public,
857 struct sipe_backend_buddy_menu *menu,
858 sipe_backend_buddy buddy,
859 sipe_buddy_info_fields id_phone,
860 sipe_buddy_info_fields id_display,
861 const gchar *type)
863 gchar *phone = sipe_backend_buddy_get_string(sipe_public,
864 buddy,
865 id_phone);
866 if (phone) {
867 gchar *display = sipe_backend_buddy_get_string(sipe_public,
868 buddy,
869 id_display);
870 gchar *tmp = NULL;
871 gchar *label = g_strdup_printf("%s %s",
872 type,
873 display ? display :
874 (tmp = sip_tel_uri_denormalize(phone)));
875 menu = sipe_backend_buddy_menu_add(sipe_public,
876 menu,
877 label,
878 SIPE_BUDDY_MENU_MAKE_CALL,
879 phone);
880 g_free(tmp);
881 g_free(label);
882 g_free(display);
883 g_free(phone);
886 return(menu);
889 struct sipe_backend_buddy_menu *sipe_core_buddy_create_menu(struct sipe_core_public *sipe_public,
890 const gchar *buddy_name,
891 struct sipe_backend_buddy_menu *menu)
893 struct sipe_core_private *sipe_private = SIPE_CORE_PRIVATE;
894 sipe_backend_buddy buddy = sipe_backend_buddy_find(sipe_public,
895 buddy_name,
896 NULL);
897 gchar *self = sip_uri_self(sipe_private);
899 SIPE_SESSION_FOREACH {
900 if (!sipe_strcase_equal(self, buddy_name) && session->chat_session)
902 struct sipe_chat_session *chat_session = session->chat_session;
903 gboolean is_conf = (chat_session->type == SIPE_CHAT_TYPE_CONFERENCE);
905 if (sipe_backend_chat_find(chat_session->backend, buddy_name))
907 gboolean conf_op = sipe_backend_chat_is_operator(chat_session->backend, self);
909 if (is_conf &&
910 /* Not conf OP */
911 !sipe_backend_chat_is_operator(chat_session->backend, buddy_name) &&
912 /* We are a conf OP */
913 conf_op) {
914 gchar *label = g_strdup_printf(_("Make leader of '%s'"),
915 chat_session->title);
916 menu = sipe_backend_buddy_menu_add(sipe_public,
917 menu,
918 label,
919 SIPE_BUDDY_MENU_MAKE_CHAT_LEADER,
920 chat_session);
921 g_free(label);
924 if (is_conf &&
925 /* We are a conf OP */
926 conf_op) {
927 gchar *label = g_strdup_printf(_("Remove from '%s'"),
928 chat_session->title);
929 menu = sipe_backend_buddy_menu_add(sipe_public,
930 menu,
931 label,
932 SIPE_BUDDY_MENU_REMOVE_FROM_CHAT,
933 chat_session);
934 g_free(label);
937 else
939 if (!is_conf ||
940 (is_conf && !session->locked)) {
941 gchar *label = g_strdup_printf(_("Invite to '%s'"),
942 chat_session->title);
943 menu = sipe_backend_buddy_menu_add(sipe_public,
944 menu,
945 label,
946 SIPE_BUDDY_MENU_INVITE_TO_CHAT,
947 chat_session);
948 g_free(label);
952 } SIPE_SESSION_FOREACH_END;
953 g_free(self);
955 menu = sipe_backend_buddy_menu_add(sipe_public,
956 menu,
957 _("New chat"),
958 SIPE_BUDDY_MENU_NEW_CHAT,
959 NULL);
961 /* add buddy's phone numbers if we have call control */
962 if (sip_csta_is_idle(sipe_private)) {
964 /* work phone */
965 menu = buddy_menu_phone(sipe_public,
966 menu,
967 buddy,
968 SIPE_BUDDY_INFO_WORK_PHONE,
969 SIPE_BUDDY_INFO_WORK_PHONE_DISPLAY,
970 _("Work"));
971 /* mobile phone */
972 menu = buddy_menu_phone(sipe_public,
973 menu,
974 buddy,
975 SIPE_BUDDY_INFO_MOBILE_PHONE,
976 SIPE_BUDDY_INFO_MOBILE_PHONE_DISPLAY,
977 _("Mobile"));
979 /* home phone */
980 menu = buddy_menu_phone(sipe_public,
981 menu,
982 buddy,
983 SIPE_BUDDY_INFO_HOME_PHONE,
984 SIPE_BUDDY_INFO_HOME_PHONE_DISPLAY,
985 _("Home"));
987 /* other phone */
988 menu = buddy_menu_phone(sipe_public,
989 menu,
990 buddy,
991 SIPE_BUDDY_INFO_OTHER_PHONE,
992 SIPE_BUDDY_INFO_OTHER_PHONE_DISPLAY,
993 _("Other"));
995 /* custom1 phone */
996 menu = buddy_menu_phone(sipe_public,
997 menu,
998 buddy,
999 SIPE_BUDDY_INFO_CUSTOM1_PHONE,
1000 SIPE_BUDDY_INFO_CUSTOM1_PHONE_DISPLAY,
1001 _("Custom1"));
1005 gchar *email = sipe_backend_buddy_get_string(sipe_public,
1006 buddy,
1007 SIPE_BUDDY_INFO_EMAIL);
1008 if (email) {
1009 menu = sipe_backend_buddy_menu_add(sipe_public,
1010 menu,
1011 _("Send email..."),
1012 SIPE_BUDDY_MENU_SEND_EMAIL,
1013 NULL);
1014 g_free(email);
1018 /*--------------------- START WIP ------------------------------*/
1020 return(menu);
1024 Local Variables:
1025 mode: c
1026 c-file-style: "bsd"
1027 indent-tabs-mode: t
1028 tab-width: 8
1029 End: