group: refactor add buddy to group code
[siplcs.git] / src / core / sipe-ucs.c
bloba840c26dc6ee5427a2ca927965eedd197f16284b
1 /**
2 * @file sipe-ucs.c
4 * pidgin-sipe
6 * Copyright (C) 2013 SIPE Project <http://sipe.sourceforge.net/>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Implementation for Unified Contact Store [MS-OXWSCOS]
25 * <http://msdn.microsoft.com/en-us/library/jj194130.aspx>
28 #include <string.h>
30 #include <glib.h>
32 #include "sipe-backend.h"
33 #include "sipe-buddy.h"
34 #include "sipe-common.h"
35 #include "sipe-core.h"
36 #include "sipe-core-private.h"
37 #include "sipe-digest.h"
38 #include "sipe-ews-autodiscover.h"
39 #include "sipe-group.h"
40 #include "sipe-http.h"
41 #include "sipe-subscriptions.h"
42 #include "sipe-ucs.h"
43 #include "sipe-utils.h"
44 #include "sipe-xml.h"
46 typedef void (ucs_callback)(struct sipe_core_private *sipe_private,
47 const sipe_xml *body,
48 gpointer callback_data);
50 struct ucs_deferred {
51 ucs_callback *cb;
52 gpointer cb_data;
53 gchar *body;
56 struct ucs_request {
57 ucs_callback *cb;
58 gpointer cb_data;
59 struct sipe_http_request *request;
62 struct sipe_ucs {
63 gchar *ews_url;
64 GSList *deferred_requests;
65 GSList *pending_requests;
66 guint group_id;
67 gboolean migrated;
68 gboolean shutting_down;
71 static void sipe_ucs_deferred_free(struct sipe_core_private *sipe_private,
72 struct ucs_deferred *data)
74 if (data->cb)
75 /* Callback: aborted */
76 (*data->cb)(sipe_private, NULL, data->cb_data);
77 g_free(data->body);
78 g_free(data);
81 static void sipe_ucs_request_free(struct sipe_core_private *sipe_private,
82 struct ucs_request *data)
84 if (data->request)
85 sipe_http_request_cancel(data->request);
86 if (data->cb)
87 /* Callback: aborted */
88 (*data->cb)(sipe_private, NULL, data->cb_data);
89 g_free(data);
92 static void sipe_ucs_http_response(struct sipe_core_private *sipe_private,
93 guint status,
94 SIPE_UNUSED_PARAMETER GSList *headers,
95 const gchar *body,
96 gpointer callback_data)
98 struct ucs_request *data = callback_data;
99 struct sipe_ucs *ucs = sipe_private->ucs;
101 SIPE_DEBUG_INFO("sipe_ucs_http_response: code %d", status);
102 data->request = NULL;
104 if ((status == SIPE_HTTP_STATUS_OK) && body) {
105 sipe_xml *xml = sipe_xml_parse(body, strlen(body));
106 const sipe_xml *soap_body = sipe_xml_child(xml, "Body");
107 /* Callback: success */
108 (*data->cb)(sipe_private, soap_body, data->cb_data);
109 sipe_xml_free(xml);
110 } else {
111 /* Callback: failed */
112 (*data->cb)(sipe_private, NULL, data->cb_data);
115 /* already been called */
116 data->cb = NULL;
118 ucs->pending_requests = g_slist_remove(ucs->pending_requests,
119 data);
120 sipe_ucs_request_free(sipe_private, data);
123 static gboolean sipe_ucs_http_request(struct sipe_core_private *sipe_private,
124 const gchar *body,
125 ucs_callback *callback,
126 gpointer callback_data)
128 struct sipe_ucs *ucs = sipe_private->ucs;
129 gboolean success = FALSE;
131 if (ucs->shutting_down) {
132 SIPE_DEBUG_ERROR("sipe_ucs_http_request: new UCS request during shutdown: THIS SHOULD NOT HAPPEN! Debugging information:\n"
133 "Body: %s\n",
134 body ? body : "<EMPTY>");
136 } else if (ucs->ews_url) {
137 struct ucs_request *data = g_new0(struct ucs_request, 1);
138 gchar *soap = g_strdup_printf("<?xml version=\"1.0\"?>\r\n"
139 "<soap:Envelope"
140 " xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\""
141 " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""
142 " xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\""
143 " >"
144 " <soap:Header>"
145 " <t:RequestServerVersion Version=\"Exchange2013\" />"
146 " </soap:Header>"
147 " <soap:Body>"
148 " %s"
149 " </soap:Body>"
150 "</soap:Envelope>",
151 body);
152 struct sipe_http_request *request = sipe_http_request_post(sipe_private,
153 ucs->ews_url,
154 NULL,
155 soap,
156 "text/xml; charset=UTF-8",
157 sipe_ucs_http_response,
158 data);
159 g_free(soap);
161 if (request) {
162 data->cb = callback;
163 data->cb_data = callback_data;
164 data->request = request;
166 ucs->pending_requests = g_slist_prepend(ucs->pending_requests,
167 data);
169 sipe_core_email_authentication(sipe_private,
170 request);
171 sipe_http_request_allow_redirect(request);
172 sipe_http_request_ready(request);
174 success = TRUE;
175 } else {
176 SIPE_DEBUG_ERROR_NOFORMAT("sipe_ucs_http_request: failed to create HTTP connection");
177 g_free(data);
180 } else {
181 struct ucs_deferred *data = g_new0(struct ucs_deferred, 1);
182 data->cb = callback;
183 data->cb_data = callback_data;
184 data->body = g_strdup(body);
186 ucs->deferred_requests = g_slist_prepend(ucs->deferred_requests,
187 data);
188 success = TRUE;
191 return(success);
194 static void sipe_ucs_get_user_photo_response(struct sipe_core_private *sipe_private,
195 const sipe_xml *body,
196 gpointer callback_data)
198 gchar *uri = callback_data;
199 const sipe_xml *node = sipe_xml_child(body,
200 "GetUserPhotoResponse/PictureData");
202 if (node) {
203 gchar *base64;
204 gsize photo_size;
205 guchar *photo;
206 guchar digest[SIPE_DIGEST_SHA1_LENGTH];
207 gchar *digest_string;
209 /* decode photo data */
210 base64 = sipe_xml_data(node);
211 photo = g_base64_decode(base64, &photo_size);
212 g_free(base64);
214 /* EWS doesn't provide a hash -> calculate SHA-1 digest */
215 sipe_digest_sha1(photo, photo_size, digest);
216 digest_string = buff_to_hex_str(digest,
217 SIPE_DIGEST_SHA1_LENGTH);
219 /* backend frees "photo" */
220 sipe_backend_buddy_set_photo(SIPE_CORE_PUBLIC,
221 uri,
222 photo,
223 photo_size,
224 digest_string);
225 g_free(digest_string);
228 g_free(uri);
231 void sipe_ucs_get_photo(struct sipe_core_private *sipe_private,
232 const gchar *uri)
234 gchar *payload = g_strdup(uri);
235 gchar *body = g_strdup_printf("<m:GetUserPhoto>"
236 " <m:Email>%s</m:Email>"
237 " <m:SizeRequested>HR48x48</m:SizeRequested>"
238 "</m:GetUserPhoto>",
239 sipe_get_no_sip_uri(uri));
241 if (!sipe_ucs_http_request(sipe_private,
242 body,
243 sipe_ucs_get_user_photo_response,
244 payload))
245 g_free(payload);
247 g_free(body);
250 static void sipe_ucs_ignore_response(SIPE_UNUSED_PARAMETER struct sipe_core_private *sipe_private,
251 SIPE_UNUSED_PARAMETER const sipe_xml *body,
252 SIPE_UNUSED_PARAMETER gpointer callback_data)
254 SIPE_DEBUG_INFO_NOFORMAT("sipe_ucs_ignore_response: done");
257 void sipe_ucs_group_remove_buddy(struct sipe_core_private *sipe_private,
258 struct sipe_group *group,
259 struct sipe_buddy *buddy)
261 if (group) {
263 * If a contact is removed from last group, it will also be
264 * removed from contact list completely. The documentation has
265 * a RemoveContactFromImList operation, but that doesn't seem
266 * to work at all, i.e. it is always rejected by the server.
268 gchar *body = g_strdup_printf("<m:RemoveImContactFromGroup>"
269 " <m:ContactId Id=\"%s\" ChangeKey=\"%s\"/>"
270 " <m:GroupId Id=\"%s\" ChangeKey=\"%s\"/>"
271 "</m:RemoveImContactFromGroup>",
272 buddy->exchange_key,
273 buddy->change_key,
274 group->exchange_key,
275 group->change_key);
277 sipe_ucs_http_request(sipe_private,
278 body,
279 sipe_ucs_ignore_response,
280 NULL);
281 g_free(body);
285 static struct sipe_group *ucs_create_group(struct sipe_core_private *sipe_private,
286 const sipe_xml *group_node)
288 const sipe_xml *id_node = sipe_xml_child(group_node,
289 "ExchangeStoreId");
290 const gchar *key = sipe_xml_attribute(id_node, "Id");
291 const gchar *change = sipe_xml_attribute(id_node, "ChangeKey");
292 struct sipe_group *group = NULL;
294 if (!(is_empty(key) || is_empty(change))) {
295 gchar *name = sipe_xml_data(sipe_xml_child(group_node,
296 "DisplayName"));
297 group = sipe_group_add(sipe_private,
298 name,
299 key,
300 change,
301 /* sipe_group must have unique ID */
302 ++sipe_private->ucs->group_id);
303 g_free(name);
306 return(group);
309 static void sipe_ucs_add_im_group_response(struct sipe_core_private *sipe_private,
310 const sipe_xml *body,
311 gpointer callback_data)
313 gchar *who = callback_data;
314 const sipe_xml *group_node = sipe_xml_child(body,
315 "AddImGroupResponse/ImGroup");
316 struct sipe_group *group = ucs_create_group(sipe_private, group_node);
318 if (group) {
319 struct sipe_buddy *buddy = sipe_buddy_find_by_uri(sipe_private,
320 who);
322 if (buddy) {
323 sipe_buddy_insert_group(buddy, group);
324 /* @TODO: send operation to add buddy to group */
328 g_free(who);
331 void sipe_ucs_group_create(struct sipe_core_private *sipe_private,
332 const gchar *name,
333 const gchar *who)
335 /* new_name can contain restricted characters */
336 gchar *body = g_markup_printf_escaped("<m:AddImGroup>"
337 " <m:DisplayName>%s</m:DisplayName>"
338 "</m:AddImGroup>",
339 name);
341 sipe_ucs_http_request(sipe_private,
342 body,
343 sipe_ucs_add_im_group_response,
344 g_strdup(who));
345 g_free(body);
348 void sipe_ucs_group_rename(struct sipe_core_private *sipe_private,
349 struct sipe_group *group,
350 const gchar *new_name)
352 /* new_name can contain restricted characters */
353 gchar *body = g_markup_printf_escaped("<m:SetImGroup>"
354 " <m:GroupId Id=\"%s\" ChangeKey=\"%s\"/>"
355 " <m:NewDisplayName>%s</m:NewDisplayName>"
356 "</m:SetImGroup>",
357 group->exchange_key,
358 group->change_key,
359 new_name);
361 sipe_ucs_http_request(sipe_private,
362 body,
363 sipe_ucs_ignore_response,
364 NULL);
365 g_free(body);
368 void sipe_ucs_group_remove(struct sipe_core_private *sipe_private,
369 struct sipe_group *group)
371 gchar *body = g_strdup_printf("<m:RemoveImGroup>"
372 " <m:GroupId Id=\"%s\" ChangeKey=\"%s\"/>"
373 "</m:RemoveImGroup>",
374 group->exchange_key,
375 group->change_key);
377 sipe_ucs_http_request(sipe_private,
378 body,
379 sipe_ucs_ignore_response,
380 NULL);
381 g_free(body);
384 static void sipe_ucs_get_im_item_list_response(struct sipe_core_private *sipe_private,
385 const sipe_xml *body,
386 SIPE_UNUSED_PARAMETER gpointer callback_data)
388 const sipe_xml *node = sipe_xml_child(body,
389 "GetImItemListResponse/ImItemList");
391 if (node) {
392 const sipe_xml *persona_node;
393 const sipe_xml *group_node;
395 /* Start processing contact list */
396 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
397 sipe_group_update_start(sipe_private);
398 sipe_buddy_update_start(sipe_private);
399 } else
400 sipe_backend_buddy_list_processing_start(SIPE_CORE_PUBLIC);
402 for (persona_node = sipe_xml_child(node, "Personas/Persona");
403 persona_node;
404 persona_node = sipe_xml_twin(persona_node)) {
405 gchar *address = sipe_xml_data(sipe_xml_child(persona_node,
406 "ImAddress"));
407 const gchar *key = NULL;
408 const gchar *change = NULL;
409 const sipe_xml *attr_node;
411 /* extract Exchange key - not sure if this is correct */
412 for (attr_node = sipe_xml_child(persona_node,
413 "Attributions/Attribution");
414 attr_node;
415 attr_node = sipe_xml_twin(attr_node)) {
416 const sipe_xml *id_node = sipe_xml_child(attr_node,
417 "SourceId");
418 gchar *type = sipe_xml_data(sipe_xml_child(attr_node,
419 "DisplayName"));
420 if (id_node &&
421 sipe_strequal(type, "Lync Contacts")) {
422 key = sipe_xml_attribute(id_node, "Id");
423 change = sipe_xml_attribute(id_node, "ChangeKey");
424 g_free(type);
425 break;
427 g_free(type);
430 if (!(is_empty(address) || is_empty(key) || is_empty(change))) {
431 gchar *uri = sip_uri_from_name(address);
432 struct sipe_buddy *buddy = sipe_buddy_add(sipe_private,
433 uri,
434 key,
435 change);
436 g_free(uri);
438 SIPE_DEBUG_INFO("sipe_ucs_get_im_item_list_response: persona URI '%s' key '%s' change '%s'",
439 buddy->name, key, change);
441 g_free(address);
444 for (group_node = sipe_xml_child(node, "Groups/ImGroup");
445 group_node;
446 group_node = sipe_xml_twin(group_node)) {
447 struct sipe_group *group = ucs_create_group(sipe_private,
448 group_node);
450 if (group) {
451 const sipe_xml *member_node;
453 for (member_node = sipe_xml_child(group_node,
454 "MemberCorrelationKey/ItemId");
455 member_node;
456 member_node = sipe_xml_twin(member_node)) {
457 struct sipe_buddy *buddy = sipe_buddy_find_by_exchange_key(sipe_private,
458 sipe_xml_attribute(member_node,
459 "Id"));
460 if (buddy)
461 sipe_buddy_add_to_group(sipe_private,
462 buddy,
463 group,
464 /* alias will be set via buddy presence update */
465 NULL);
470 /* Finished processing contact list */
471 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES)) {
472 sipe_buddy_update_finish(sipe_private);
473 sipe_group_update_finish(sipe_private);
474 } else {
475 sipe_buddy_cleanup_local_list(sipe_private);
476 sipe_backend_buddy_list_processing_finish(SIPE_CORE_PUBLIC);
477 sipe_subscribe_presence_initial(sipe_private);
482 static void ucs_get_im_item_list(struct sipe_core_private *sipe_private)
484 if (sipe_private->ucs->migrated)
485 sipe_ucs_http_request(sipe_private,
486 "<m:GetImItemList/>",
487 sipe_ucs_get_im_item_list_response,
488 NULL);
491 static void ucs_ews_autodiscover_cb(struct sipe_core_private *sipe_private,
492 const struct sipe_ews_autodiscover_data *ews_data,
493 SIPE_UNUSED_PARAMETER gpointer callback_data)
495 struct sipe_ucs *ucs = sipe_private->ucs;
496 const gchar *ews_url;
498 if (!ucs || !ews_data)
499 return;
501 ews_url = ews_data->ews_url;
502 if (is_empty(ews_url)) {
503 SIPE_DEBUG_ERROR_NOFORMAT("ucs_ews_autodiscover_cb: can't detect EWS URL, contact list operations will not work!");
504 return;
507 SIPE_DEBUG_INFO("ucs_ews_autodiscover_cb: EWS URL '%s'", ews_url);
508 ucs->ews_url = g_strdup(ews_url);
510 ucs_get_im_item_list(sipe_private);
512 /* EWS URL is valid, send all deferred requests now */
513 if (ucs->deferred_requests) {
514 GSList *entry = ucs->deferred_requests;
515 while (entry) {
516 struct ucs_deferred *data = entry->data;
518 sipe_ucs_http_request(sipe_private,
519 data->body,
520 data->cb,
521 data->cb_data);
523 /* callback & data has been forwarded */
524 data->cb = NULL;
525 sipe_ucs_deferred_free(sipe_private, data);
527 entry = entry->next;
529 g_slist_free(ucs->deferred_requests);
530 ucs->deferred_requests = NULL;
534 gboolean sipe_ucs_is_migrated(struct sipe_core_private *sipe_private)
536 return(sipe_private->ucs ? sipe_private->ucs->migrated : FALSE);
539 void sipe_ucs_init(struct sipe_core_private *sipe_private,
540 gboolean migrated)
542 struct sipe_ucs *ucs;
544 if (sipe_private->ucs) {
545 /* contact list update trigger -> request list again */
546 if (SIPE_CORE_PRIVATE_FLAG_IS(SUBSCRIBED_BUDDIES))
547 ucs_get_im_item_list(sipe_private);
549 return;
552 sipe_private->ucs = ucs = g_new0(struct sipe_ucs, 1);
553 ucs->migrated = migrated;
555 sipe_ews_autodiscover_start(sipe_private,
556 ucs_ews_autodiscover_cb,
557 NULL);
560 void sipe_ucs_free(struct sipe_core_private *sipe_private)
562 struct sipe_ucs *ucs = sipe_private->ucs;
564 if (!ucs)
565 return;
567 /* UCS stack is shutting down: reject all new requests */
568 ucs->shutting_down = TRUE;
570 if (ucs->deferred_requests) {
571 GSList *entry = ucs->deferred_requests;
572 while (entry) {
573 sipe_ucs_deferred_free(sipe_private, entry->data);
574 entry = entry->next;
576 g_slist_free(ucs->deferred_requests);
579 if (ucs->pending_requests) {
580 GSList *entry = ucs->pending_requests;
581 while (entry) {
582 sipe_ucs_request_free(sipe_private, entry->data);
583 entry = entry->next;
585 g_slist_free(ucs->pending_requests);
588 g_free(ucs->ews_url);
589 g_free(ucs);
590 sipe_private->ucs = NULL;
594 Local Variables:
595 mode: c
596 c-file-style: "bsd"
597 indent-tabs-mode: t
598 tab-width: 8
599 End: