From 8a408f2f5b80d14e11bda3f427c1b4604491a225 Mon Sep 17 00:00:00 2001 From: Stefan Becker Date: Mon, 19 Aug 2013 22:56:24 +0300 Subject: [PATCH] ucs: add AddNewImContactToGroup operation Used to add a not-existing buddy to a group. --- src/core/sipe-buddy.c | 32 +++++++++++------- src/core/sipe-buddy.h | 13 +++++++ src/core/sipe-ucs.c | 94 ++++++++++++++++++++++++++++++++++++++++----------- 3 files changed, 107 insertions(+), 32 deletions(-) diff --git a/src/core/sipe-buddy.c b/src/core/sipe-buddy.c index 351475b2..5f4297d3 100644 --- a/src/core/sipe-buddy.c +++ b/src/core/sipe-buddy.c @@ -81,6 +81,21 @@ static void buddy_fetch_photo(struct sipe_core_private *sipe_private, const gchar *uri); static void photo_response_data_free(struct photo_response_data *data); +void sipe_buddy_add_keys(struct sipe_core_private *sipe_private, + struct sipe_buddy *buddy, + const gchar *exchange_key, + const gchar *change_key) +{ + if (exchange_key) { + buddy->exchange_key = g_strdup(exchange_key); + g_hash_table_insert(sipe_private->buddies->exchange_key, + buddy->exchange_key, + buddy); + } + if (change_key) + buddy->change_key = g_strdup(change_key); +} + struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private, const gchar *uri, const gchar *exchange_key, @@ -92,23 +107,16 @@ struct sipe_buddy *sipe_buddy_add(struct sipe_core_private *sipe_private, normalized_uri); if (!buddy) { - struct sipe_buddies *buddies = sipe_private->buddies; - buddy = g_new0(struct sipe_buddy, 1); buddy->name = normalized_uri; - g_hash_table_insert(buddies->uri, + g_hash_table_insert(sipe_private->buddies->uri, buddy->name, buddy); - if (exchange_key) { - buddy->exchange_key = g_strdup(exchange_key); - g_hash_table_insert(buddies->exchange_key, - buddy->exchange_key, - buddy); - } - if (change_key) - buddy->change_key = g_strdup(change_key); - + sipe_buddy_add_keys(sipe_private, + buddy, + exchange_key, + change_key); SIPE_DEBUG_INFO("sipe_buddy_add: Added buddy %s", normalized_uri); diff --git a/src/core/sipe-buddy.h b/src/core/sipe-buddy.h index d2a5723c..41cfc75b 100644 --- a/src/core/sipe-buddy.h +++ b/src/core/sipe-buddy.h @@ -63,6 +63,19 @@ struct sipe_buddy { }; /** + * Adds UCS Exchange/Change keys to a @c sipe_buddy structure + * + * @param sipe_private SIPE core data + * @param buddy sipe_buddy data structure + * @param exchange_key Exchange key (may be @c NULL) + * @param change_key Change key (may be @c NULL) + */ +void sipe_buddy_add_keys(struct sipe_core_private *sipe_private, + struct sipe_buddy *buddy, + const gchar *exchange_key, + const gchar *change_key); + +/** * Creates @c sipe_buddy structure for a new buddy and adds it into the buddy * list of given account. If buddy is already in the list, its existing * structure is returned. diff --git a/src/core/sipe-ucs.c b/src/core/sipe-ucs.c index 3f15adf3..0aa66fce 100644 --- a/src/core/sipe-ucs.c +++ b/src/core/sipe-ucs.c @@ -254,6 +254,65 @@ static void sipe_ucs_ignore_response(SIPE_UNUSED_PARAMETER struct sipe_core_priv SIPE_DEBUG_INFO_NOFORMAT("sipe_ucs_ignore_response: done"); } +static void ucs_extract_keys(const sipe_xml *persona_node, + const gchar **key, + const gchar **change) +{ + const sipe_xml *attr_node; + + /* extract Exchange key - not sure if this is correct */ + for (attr_node = sipe_xml_child(persona_node, + "Attributions/Attribution"); + attr_node; + attr_node = sipe_xml_twin(attr_node)) { + const sipe_xml *id_node = sipe_xml_child(attr_node, + "SourceId"); + gchar *type = sipe_xml_data(sipe_xml_child(attr_node, + "DisplayName")); + if (id_node && + sipe_strequal(type, "Lync Contacts")) { + *key = sipe_xml_attribute(id_node, "Id"); + *change = sipe_xml_attribute(id_node, "ChangeKey"); + g_free(type); + break; + } + g_free(type); + } +} + +static void sipe_ucs_add_new_im_contact_to_group_response(struct sipe_core_private *sipe_private, + const sipe_xml *body, + gpointer callback_data) +{ + gchar *who = callback_data; + struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private, who); + const sipe_xml *persona_node = sipe_xml_child(body, + "AddNewImContactToGroupResponse/Persona"); + + if (persona_node && + buddy && + is_empty(buddy->exchange_key) && + is_empty(buddy->change_key)) { + const gchar *key = NULL; + const gchar *change = NULL; + + ucs_extract_keys(persona_node, &key, &change); + + if (!is_empty(key) && !is_empty(change)) { + + sipe_buddy_add_keys(sipe_private, + buddy, + key, + change); + + SIPE_DEBUG_INFO("sipe_ucs_add_new_im_contact_to_group_response: persona URI '%s' key '%s' change '%s'", + buddy->name, key, change); + } + } + + g_free(who); +} + void sipe_ucs_group_add_buddy(struct sipe_core_private *sipe_private, struct sipe_group *group, struct sipe_buddy *buddy) @@ -275,6 +334,19 @@ void sipe_ucs_group_add_buddy(struct sipe_core_private *sipe_private, NULL); g_free(body); } else { + gchar *body = g_strdup_printf("" + " %s" + " " + "", + sipe_get_no_sip_uri(buddy->name), + group->exchange_key, + group->change_key); + + sipe_ucs_http_request(sipe_private, + body, + sipe_ucs_add_new_im_contact_to_group_response, + g_strdup(buddy->name)); + g_free(body); } } @@ -432,26 +504,8 @@ static void sipe_ucs_get_im_item_list_response(struct sipe_core_private *sipe_pr "ImAddress")); const gchar *key = NULL; const gchar *change = NULL; - const sipe_xml *attr_node; - - /* extract Exchange key - not sure if this is correct */ - for (attr_node = sipe_xml_child(persona_node, - "Attributions/Attribution"); - attr_node; - attr_node = sipe_xml_twin(attr_node)) { - const sipe_xml *id_node = sipe_xml_child(attr_node, - "SourceId"); - gchar *type = sipe_xml_data(sipe_xml_child(attr_node, - "DisplayName")); - if (id_node && - sipe_strequal(type, "Lync Contacts")) { - key = sipe_xml_attribute(id_node, "Id"); - change = sipe_xml_attribute(id_node, "ChangeKey"); - g_free(type); - break; - } - g_free(type); - } + + ucs_extract_keys(persona_node, &key, &change); if (!(is_empty(address) || is_empty(key) || is_empty(change))) { gchar *uri = sip_uri_from_name(address); -- 2.11.4.GIT