Debian Codegraph @

src/Microsoft365/calendar/e-cal-backend-m365.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2020 Red Hat (www.redhat.com)
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "evolution-ews-config.h"
7
8 #include <string.h>
9
10 #include <glib/gstdio.h>
11 #include <glib/gi18n-lib.h>
12
13 #include <libedata-cal/libedata-cal.h>
14 #include <libecal/libecal.h>
15
16 #include "e-ews-common-utils.h"
17 #include "common/camel-m365-settings.h"
18 #include "common/e-m365-connection.h"
19 #include "common/e-m365-tz-utils.h"
20 #include "common/e-source-m365-folder.h"
21
22 #include "e-cal-backend-m365-utils.h"
23 #include "e-cal-backend-m365.h"
24
25 #define EC_ERROR(_code) e_client_error_create (_code, NULL)
26 #define EC_ERROR_EX(_code, _msg) e_client_error_create (_code, _msg)
27 #define ECC_ERROR(_code) e_cal_client_error_create (_code, NULL)
28 #define ECC_ERROR_EX(_code, _msg) e_cal_client_error_create (_code, _msg)
29
30 #define LOCK(_cb) g_rec_mutex_lock (&_cb->priv->property_lock)
31 #define UNLOCK(_cb) g_rec_mutex_unlock (&_cb->priv->property_lock)
32
33 struct _ECalBackendM365Private {
34 GRecMutex property_lock;
35 EM365Connection *cnc;
36 gchar *group_id;
37 gchar *folder_id;
38 gchar *attachments_dir;
39 };
40
41 G_DEFINE_TYPE_WITH_PRIVATE (ECalBackendM365, e_cal_backend_m365, E_TYPE_CAL_META_BACKEND)
42
43 static void
44 ecb_m365_convert_error_to_client_error (GError **perror)
45 {
46 GError *error = NULL;
47
48 if (!perror || !*perror ||
49 (*perror)->domain == E_CLIENT_ERROR ||
50 (*perror)->domain == E_CAL_CLIENT_ERROR)
51 return;
52
53 /*if ((*perror)->domain == M365_CONNECTION_ERROR) {
54 switch ((*perror)->code) {
55 case M365_CONNECTION_ERROR_AUTHENTICATION_FAILED:
56 error = EC_ERROR_EX (E_CLIENT_ERROR_AUTHENTICATION_FAILED, (*perror)->message);
57 break;
58 case M365_CONNECTION_ERROR_FOLDERNOTFOUND:
59 case M365_CONNECTION_ERROR_MANAGEDFOLDERNOTFOUND:
60 case M365_CONNECTION_ERROR_PARENTFOLDERNOTFOUND:
61 case M365_CONNECTION_ERROR_PUBLICFOLDERSERVERNOTFOUND:
62 error = ECC_ERROR_EX (E_CAL_CLIENT_ERROR_NO_SUCH_CALENDAR, (*perror)->message);
63 break;
64 case M365_CONNECTION_ERROR_EVENTNOTFOUND:
65 case M365_CONNECTION_ERROR_ITEMNOTFOUND:
66 error = ECC_ERROR_EX (E_CAL_CLIENT_ERROR_OBJECT_NOT_FOUND, (*perror)->message);
67 break;
68 case M365_CONNECTION_ERROR_UNAVAILABLE:
69 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_HOST_NOT_FOUND, (*perror)->message);
70 break;
71 }
72
73 if (!error)
74 error = EC_ERROR_EX (E_CLIENT_ERROR_OTHER_ERROR, (*perror)->message);
75 }*/
76
77 if (error) {
78 g_error_free (*perror);
79 *perror = error;
80 }
81 }
82
83 static const gchar *
84 ecb_m365_get_component_from_extra (const gchar *extra)
85 {
86 gchar *enter;
87
88 if (!extra)
89 return NULL;
90
91 enter = strchr (extra, '\n');
92
93 if (!enter)
94 return NULL;
95
96 return enter + 1;
97 }
98
99 /* Modifies inout_extra, cannot be called multiple times with the same arguments */
100 static void
101 ecb_m365_split_extra (gchar *inout_extra,
102 const gchar **out_change_key,
103 const gchar **out_ical_comp)
104 {
105 gchar *enter;
106
107 if (!inout_extra)
108 return;
109
110 enter = (gchar *) ecb_m365_get_component_from_extra (inout_extra);
111 g_return_if_fail (enter != NULL);
112
113 enter[-1] = '\0';
114
115 if (out_change_key)
116 *out_change_key = inout_extra;
117
118 if (out_ical_comp)
119 *out_ical_comp = enter;
120 }
121
122 static gchar *
123 ecb_m365_join_to_extra (const gchar *change_key,
124 const gchar *ical_comp)
125 {
126 if (!change_key && !ical_comp)
127 return NULL;
128
129 return g_strconcat (change_key ? change_key : "", "\n", ical_comp, NULL);
130 }
131
132 static ICalComponent *
133 ecb_m365_json_to_ical (ECalBackendM365 *cbm365,
134 JsonObject *m365_object,
135 GCancellable *cancellable,
136 GError **error)
137 {
138 g_return_val_if_fail (m365_object != NULL, NULL);
139
140 return e_cal_backend_m365_utils_json_to_ical (cbm365->priv->cnc,
141 cbm365->priv->group_id,
142 cbm365->priv->folder_id,
143 cbm365->priv->attachments_dir,
144 E_TIMEZONE_CACHE (cbm365),
145 e_cal_backend_get_kind (E_CAL_BACKEND (cbm365)),
146 m365_object,
147 cancellable,
148 error);
149 }
150
151 static ECalMetaBackendInfo *
152 ecb_m365_json_to_ical_nfo (ECalBackendM365 *cbm365,
153 EM365Event *m365_event,
154 GCancellable *cancellable,
155 GError **error)
156 {
157 ECalMetaBackendInfo *nfo;
158 ICalComponent *icomp;
159
160 icomp = ecb_m365_json_to_ical (cbm365, m365_event, cancellable, error);
161
162 if (!icomp)
163 return NULL;
164
165 nfo = e_cal_meta_backend_info_new (i_cal_component_get_uid (icomp),
166 e_m365_event_get_change_key (m365_event),
167 NULL, NULL);
168
169 if (nfo) {
170 nfo->object = i_cal_component_as_ical_string (icomp);
171 nfo->extra = ecb_m365_join_to_extra (e_m365_event_get_change_key (m365_event), nfo->object);
172 }
173
174 g_clear_object (&icomp);
175
176 return nfo;
177 }
178
179 static JsonBuilder *
180 ecb_m365_ical_to_json_locked (ECalBackendM365 *cbm365,
181 ICalComponent *new_comp,
182 ICalComponent *old_comp, /* nullable */
183 GCancellable *cancellable,
184 GError **error)
185 {
186 g_return_val_if_fail (new_comp != NULL, NULL);
187
188 return e_cal_backend_m365_utils_ical_to_json (cbm365->priv->cnc,
189 cbm365->priv->group_id,
190 cbm365->priv->folder_id,
191 E_TIMEZONE_CACHE (cbm365),
192 e_cal_backend_get_kind (E_CAL_BACKEND (cbm365)),
193 new_comp,
194 old_comp,
195 cancellable,
196 error);
197 }
198
199 static gboolean
200 ecb_m365_ical_to_json_2nd_go_locked (ECalBackendM365 *cbm365,
201 ICalComponent *new_comp,
202 ICalComponent *old_comp, /* nullable */
203 const gchar *m365_id,
204 GCancellable *cancellable,
205 GError **error)
206 {
207 g_return_val_if_fail (new_comp != NULL, FALSE);
208 g_return_val_if_fail (m365_id != NULL, FALSE);
209
210 return e_cal_backend_m365_utils_ical_to_json_2nd_go (cbm365->priv->cnc,
211 cbm365->priv->group_id,
212 cbm365->priv->folder_id,
213 E_TIMEZONE_CACHE (cbm365),
214 e_cal_backend_get_kind (E_CAL_BACKEND (cbm365)),
215 new_comp,
216 old_comp,
217 m365_id,
218 cancellable,
219 error);
220 }
221
222 static gboolean
223 ecb_m365_download_changes_locked (ECalBackendM365 *cbm365,
224 const GSList *ids,
225 GSList **out_info_objects,
226 GCancellable *cancellable,
227 GError **error)
228 {
229 GSList *items = NULL, *link;
230
231 if (!ids)
232 return TRUE;
233
234 switch (e_cal_backend_get_kind (E_CAL_BACKEND (cbm365))) {
235 case I_CAL_VEVENT_COMPONENT:
236 if (!e_m365_connection_get_events_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id, cbm365->priv->folder_id, ids, NULL, NULL, &items, cancellable, error))
237 return FALSE;
238 break;
239 case I_CAL_VTODO_COMPONENT:
240 if (!e_m365_connection_get_tasks_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id, cbm365->priv->folder_id, ids, NULL, NULL, &items, cancellable, error))
241 return FALSE;
242 break;
243 default:
244 g_warn_if_reached ();
245 return FALSE;
246 }
247
248 for (link = items; link; link = g_slist_next (link)) {
249 JsonObject *item = link->data;
250 ECalMetaBackendInfo *nfo;
251
252 if (!item)
253 continue;
254
255 nfo = ecb_m365_json_to_ical_nfo (cbm365, item, cancellable, error);
256
257 if (nfo)
258 *out_info_objects = g_slist_prepend (*out_info_objects, nfo);
259 }
260
261 g_slist_free_full (items, (GDestroyNotify) json_object_unref);
262
263 return TRUE;
264 }
265
266 static void
267 ecb_m365_maybe_disconnect_sync (ECalBackendM365 *cbm365,
268 GError **in_perror,
269 GCancellable *cancellable)
270 {
271 g_return_if_fail (E_IS_CAL_BACKEND_M365 (cbm365));
272
273 if (in_perror && g_error_matches (*in_perror, E_CLIENT_ERROR, E_CLIENT_ERROR_AUTHENTICATION_FAILED)) {
274 e_cal_meta_backend_disconnect_sync (E_CAL_META_BACKEND (cbm365), cancellable, NULL);
275 e_backend_schedule_credentials_required (E_BACKEND (cbm365), E_SOURCE_CREDENTIALS_REASON_REJECTED, NULL, 0, NULL, NULL, G_STRFUNC);
276 }
277 }
278
279 static gboolean
280 ecb_m365_unset_connection_sync (ECalBackendM365 *cbm365,
281 gboolean is_disconnect,
282 GCancellable *cancellable,
283 GError **error)
284 {
285 gboolean success = TRUE;
286
287 g_return_val_if_fail (E_IS_CAL_BACKEND_M365 (cbm365), FALSE);
288
289 LOCK (cbm365);
290
291 if (cbm365->priv->cnc) {
292 if (is_disconnect)
293 success = e_m365_connection_disconnect_sync (cbm365->priv->cnc, cancellable, error);
294 }
295
296 g_clear_object (&cbm365->priv->cnc);
297 g_clear_pointer (&cbm365->priv->group_id, g_free);
298 g_clear_pointer (&cbm365->priv->folder_id, g_free);
299
300 UNLOCK (cbm365);
301
302 return success;
303 }
304
305 static gboolean
306 ecb_m365_connect_sync (ECalMetaBackend *meta_backend,
307 const ENamedParameters *credentials,
308 ESourceAuthenticationResult *out_auth_result,
309 gchar **out_certificate_pem,
310 GTlsCertificateFlags *out_certificate_errors,
311 GCancellable *cancellable,
312 GError **error)
313 {
314 ECalBackendM365 *cbm365;
315 EM365FolderKind folder_kind;
316 gboolean success = FALSE;
317
318 g_return_val_if_fail (E_IS_CAL_BACKEND_M365 (meta_backend), FALSE);
319 g_return_val_if_fail (out_auth_result != NULL, FALSE);
320
321 switch (e_cal_backend_get_kind (E_CAL_BACKEND (meta_backend))) {
322 case I_CAL_VEVENT_COMPONENT:
323 folder_kind = E_M365_FOLDER_KIND_CALENDAR;
324 break;
325 case I_CAL_VTODO_COMPONENT:
326 folder_kind = E_M365_FOLDER_KIND_TASKS;
327 break;
328 default:
329 g_warn_if_reached ();
330 return FALSE;
331 }
332
333 cbm365 = E_CAL_BACKEND_M365 (meta_backend);
334
335 LOCK (cbm365);
336
337 if (cbm365->priv->cnc) {
338 UNLOCK (cbm365);
339
340 *out_auth_result = E_SOURCE_AUTHENTICATION_ACCEPTED;
341
342 return TRUE;
343 } else {
344 EBackend *backend;
345 ESourceRegistry *registry;
346 ESource *source;
347 EM365Connection *cnc = NULL;
348 ESourceM365Folder *m365_folder_extension;
349 CamelM365Settings *m365_settings;
350 gchar *group_id;
351 gchar *folder_id;
352
353 backend = E_BACKEND (cbm365);
354 source = e_backend_get_source (backend);
355 registry = e_cal_backend_get_registry (E_CAL_BACKEND (cbm365));
356 m365_settings = camel_m365_settings_get_from_backend (backend, registry);
357 g_warn_if_fail (m365_settings != NULL);
358
359 m365_folder_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_M365_FOLDER);
360 group_id = e_source_m365_folder_dup_group_id (m365_folder_extension);
361 folder_id = e_source_m365_folder_dup_id (m365_folder_extension);
362
363 if (folder_id) {
364 cnc = e_m365_connection_new_for_backend (backend, registry, source, m365_settings);
365
366 *out_auth_result = e_m365_connection_authenticate_sync (cnc, NULL, folder_kind, group_id, folder_id,
367 out_certificate_pem, out_certificate_errors, cancellable, error);
368
369 if (*out_auth_result == E_SOURCE_AUTHENTICATION_ACCEPTED) {
370 cbm365->priv->cnc = g_object_ref (cnc);
371
372 g_warn_if_fail (cbm365->priv->group_id == NULL);
373 g_warn_if_fail (cbm365->priv->folder_id == NULL);
374
375 g_free (cbm365->priv->group_id);
376 cbm365->priv->group_id = group_id;
377
378 g_free (cbm365->priv->folder_id);
379 cbm365->priv->folder_id = folder_id;
380
381 group_id = NULL;
382 folder_id = NULL;
383 success = TRUE;
384
385 e_cal_backend_set_writable (E_CAL_BACKEND (cbm365), TRUE);
386 }
387 } else {
388 *out_auth_result = E_SOURCE_AUTHENTICATION_ERROR;
389 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_OTHER_ERROR, _("Folder ID is not set")));
390 }
391
392 g_clear_object (&cnc);
393 g_free (group_id);
394 g_free (folder_id);
395 }
396
397 UNLOCK (cbm365);
398
399 ecb_m365_convert_error_to_client_error (error);
400
401 return success;
402 }
403
404 static gboolean
405 ecb_m365_disconnect_sync (ECalMetaBackend *meta_backend,
406 GCancellable *cancellable,
407 GError **error)
408 {
409 g_return_val_if_fail (E_IS_CAL_BACKEND_M365 (meta_backend), FALSE);
410
411 return ecb_m365_unset_connection_sync (E_CAL_BACKEND_M365 (meta_backend), TRUE, cancellable, error);
412 }
413
414 static gboolean
415 ecb_m365_gather_ids_cb (ECalCache *cal_cache,
416 const gchar *uid,
417 const gchar *rid,
418 const gchar *revision,
419 const gchar *object,
420 const gchar *extra,
421 guint32 custom_flags,
422 EOfflineState offline_state,
423 gpointer user_data)
424 {
425 GHashTable *left_known_uids = user_data;
426
427 if (uid && !g_hash_table_contains (left_known_uids, uid))
428 g_hash_table_insert (left_known_uids, g_strdup (uid), NULL);
429
430 return TRUE;
431 }
432
433 static gboolean
434 ecb_m365_get_changes_sync (ECalMetaBackend *meta_backend,
435 const gchar *last_sync_tag,
436 gboolean is_repeat,
437 gchar **out_new_sync_tag,
438 gboolean *out_repeat,
439 GSList **out_created_objects,
440 GSList **out_modified_objects,
441 GSList **out_removed_objects,
442 GCancellable *cancellable,
443 GError **error)
444 {
445 ECalBackendM365 *cbm365;
446 ECalCache *cal_cache;
447 GSList *items = NULL, *link;
448 GHashTable *left_known_ids;
449 GHashTableIter iter;
450 gpointer key;
451 gboolean full_read;
452 gboolean success = TRUE;
453 gboolean (* list_items_func) (EM365Connection *cnc,
454 const gchar *user_override,
455 const gchar *group_id,
456 const gchar *calendar_id,
457 const gchar *prefer_outlook_timezone,
458 const gchar *select,
459 const gchar *filter,
460 GSList **out_items,
461 GCancellable *cancellable,
462 GError **error);
463 const gchar *(* get_id_func) (JsonObject *item);
464 const gchar *(* get_change_key_func) (JsonObject *item);
465 const gchar *select_props = "id,changeKey";
466
467 g_return_val_if_fail (E_IS_CAL_BACKEND_M365 (meta_backend), FALSE);
468 g_return_val_if_fail (out_new_sync_tag != NULL, FALSE);
469 g_return_val_if_fail (out_repeat != NULL, FALSE);
470 g_return_val_if_fail (out_created_objects != NULL, FALSE);
471 g_return_val_if_fail (out_modified_objects != NULL, FALSE);
472 g_return_val_if_fail (out_removed_objects != NULL, FALSE);
473
474 switch (e_cal_backend_get_kind (E_CAL_BACKEND (meta_backend))) {
475 case I_CAL_VEVENT_COMPONENT:
476 list_items_func = e_m365_connection_list_events_sync;
477 get_id_func = e_m365_event_get_id;
478 get_change_key_func = e_m365_event_get_change_key;
479 break;
480 case I_CAL_VTODO_COMPONENT:
481 list_items_func = e_m365_connection_list_tasks_sync;
482 get_id_func = e_m365_task_get_id;
483 get_change_key_func = e_m365_task_get_last_modified_as_string;
484 select_props = NULL;
485 break;
486 default:
487 g_warn_if_reached ();
488 return FALSE;
489 }
490
491 *out_created_objects = NULL;
492 *out_modified_objects = NULL;
493 *out_removed_objects = NULL;
494
495 cbm365 = E_CAL_BACKEND_M365 (meta_backend);
496
497 cal_cache = e_cal_meta_backend_ref_cache (meta_backend);
498 g_return_val_if_fail (E_IS_CAL_CACHE (cal_cache), FALSE);
499
500 left_known_ids = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
501 e_cal_cache_search_with_callback (cal_cache, "#t", ecb_m365_gather_ids_cb, left_known_ids, cancellable, NULL);
502
503 LOCK (cbm365);
504
505 full_read = !select_props || !e_cache_get_count (E_CACHE (cal_cache), E_CACHE_INCLUDE_DELETED, cancellable, NULL);
506
507 success = list_items_func (cbm365->priv->cnc, NULL, cbm365->priv->group_id, cbm365->priv->folder_id, NULL,
508 full_read ? NULL : select_props, NULL, &items, cancellable, error);
509
510 if (success) {
511 GSList *new_ids = NULL; /* const gchar *, borrowed from 'items' objects */
512 GSList *changed_ids = NULL; /* const gchar *, borrowed from 'items' objects */
513
514 for (link = items; link && !g_cancellable_is_cancelled (cancellable); link = g_slist_next (link)) {
515 JsonObject *item = link->data;
516 const gchar *id, *change_key;
517 gchar *extra = NULL;
518
519 if (!item)
520 continue;
521
522 id = get_id_func (item);
523 change_key = get_change_key_func (item);
524
525 if (id)
526 g_hash_table_remove (left_known_ids, id);
527
528 if (e_cal_cache_get_component_extra (cal_cache, id, NULL, &extra, cancellable, NULL)) {
529 const gchar *saved_change_key = NULL;
530
531 ecb_m365_split_extra (extra, &saved_change_key, NULL);
532
533 if (g_strcmp0 (saved_change_key, change_key) == 0) {
534 g_free (extra);
535 continue;
536 } else if (full_read) {
537 ECalMetaBackendInfo *nfo;
538
539 nfo = ecb_m365_json_to_ical_nfo (cbm365, item, cancellable, NULL);
540
541 if (nfo)
542 *out_modified_objects = g_slist_prepend (*out_modified_objects, nfo);
543 } else {
544 changed_ids = g_slist_prepend (changed_ids, (gpointer) id);
545 }
546
547 g_free (extra);
548 } else if (full_read) {
549 ECalMetaBackendInfo *nfo;
550
551 nfo = ecb_m365_json_to_ical_nfo (cbm365, item, cancellable, NULL);
552
553 if (nfo)
554 *out_created_objects = g_slist_prepend (*out_created_objects, nfo);
555 } else {
556 new_ids = g_slist_prepend (new_ids, (gpointer) id);
557 }
558 }
559
560 if (new_ids) {
561 new_ids = g_slist_reverse (new_ids);
562 success = ecb_m365_download_changes_locked (cbm365, new_ids, out_created_objects, cancellable, error);
563 }
564
565 if (success && changed_ids) {
566 changed_ids = g_slist_reverse (changed_ids);
567 success = ecb_m365_download_changes_locked (cbm365, changed_ids, out_modified_objects, cancellable, error);
568 }
569
570 g_slist_free (new_ids);
571 g_slist_free (changed_ids);
572 }
573
574 g_slist_free_full (items, (GDestroyNotify) json_object_unref);
575
576 g_hash_table_iter_init (&iter, left_known_ids);
577 while (g_hash_table_iter_next (&iter, &key, NULL)) {
578 ECalMetaBackendInfo *nfo;
579 const gchar *uid = key;
580
581 nfo = e_cal_meta_backend_info_new (uid, NULL, NULL, NULL);
582 *out_removed_objects = g_slist_prepend (*out_removed_objects, nfo);
583 }
584
585 UNLOCK (cbm365);
586
587 ecb_m365_convert_error_to_client_error (error);
588 ecb_m365_maybe_disconnect_sync (cbm365, error, cancellable);
589
590 g_hash_table_destroy (left_known_ids);
591 g_clear_object (&cal_cache);
592
593 return success;
594 }
595
596 static gboolean
597 ecb_m365_load_component_sync (ECalMetaBackend *meta_backend,
598 const gchar *uid,
599 const gchar *extra,
600 ICalComponent **out_component,
601 gchar **out_extra,
602 GCancellable *cancellable,
603 GError **error)
604 {
605 ECalBackendM365 *cbm365;
606 ICalComponentKind kind;
607 JsonObject *item = NULL;
608 const gchar *(* get_change_key_func) (JsonObject *item);
609 guint pass;
610 gboolean success = FALSE;
611
612 g_return_val_if_fail (E_IS_CAL_BACKEND_M365 (meta_backend), FALSE);
613 g_return_val_if_fail (uid != NULL, FALSE);
614 g_return_val_if_fail (out_component != NULL, FALSE);
615 g_return_val_if_fail (out_extra != NULL, FALSE);
616
617 cbm365 = E_CAL_BACKEND_M365 (meta_backend);
618 kind = e_cal_backend_get_kind (E_CAL_BACKEND (cbm365));
619
620 LOCK (cbm365);
621
622 for (pass = 0; pass < 2; pass++) {
623 gchar *tmp_uid = NULL;
624 const gchar *use_uid = uid;
625
626 if (pass == 1) {
627 gchar *filter = g_strdup_printf ("iCalUid eq '%s'", uid);
628
629 if (kind == I_CAL_VEVENT_COMPONENT) {
630 GSList *events = NULL;
631
632 if (e_m365_connection_list_events_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
633 cbm365->priv->folder_id, NULL, "id", filter, &events, cancellable, NULL)) {
634 if (events && !events->next) {
635 EM365Event *event = events->data;
636 tmp_uid = g_strdup (e_m365_event_get_id (event));
637 }
638 g_slist_free_full (events, (GDestroyNotify) json_object_unref);
639 }
640 } else if (kind == I_CAL_VTODO_COMPONENT) {
641 GSList *events = NULL;
642
643 if (e_m365_connection_list_tasks_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
644 cbm365->priv->folder_id, NULL, "id", filter, &events, cancellable, NULL)) {
645 if (events && !events->next) {
646 EM365Event *event = events->data;
647 tmp_uid = g_strdup (e_m365_event_get_id (event));
648 }
649 g_slist_free_full (events, (GDestroyNotify) json_object_unref);
650 }
651 } else {
652 g_free (filter);
653 break;
654 }
655
656 g_free (filter);
657
658 if (tmp_uid)
659 use_uid = tmp_uid;
660 else
661 break;
662 }
663
664 switch (kind) {
665 case I_CAL_VEVENT_COMPONENT:
666 success = e_m365_connection_get_event_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
667 cbm365->priv->folder_id, use_uid, NULL, NULL, &item, cancellable, pass == 0 ? error : NULL);
668 get_change_key_func = e_m365_event_get_change_key;
669 break;
670 case I_CAL_VTODO_COMPONENT:
671 success = e_m365_connection_get_task_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
672 cbm365->priv->folder_id, use_uid, NULL, NULL, &item, cancellable, pass == 0 ? error : NULL);
673 get_change_key_func = e_m365_task_get_last_modified_as_string;
674 break;
675 default:
676 success = FALSE;
677 break;
678 }
679
680 g_free (tmp_uid);
681
682 if (success || g_cancellable_is_cancelled (cancellable))
683 break;
684 }
685
686 if (success) {
687 *out_component = ecb_m365_json_to_ical (cbm365, item, cancellable, error);
688
689 if (*out_component) {
690 gchar *ical_str;
691
692 ical_str = i_cal_component_as_ical_string (*out_component);
693
694 *out_extra = ecb_m365_join_to_extra (get_change_key_func (item), ical_str);
695
696 g_free (ical_str);
697 } else {
698 success = FALSE;
699 }
700 }
701
702 g_clear_pointer (&item, json_object_unref);
703
704 UNLOCK (cbm365);
705
706 ecb_m365_convert_error_to_client_error (error);
707 ecb_m365_maybe_disconnect_sync (cbm365, error, cancellable);
708
709 return success;
710 }
711
712 static ICalComponent *
713 ecb_m365_dup_main_comp (ICalComponent *icomp,
714 ICalComponentKind kind)
715 {
716 ICalComponent *main_comp = NULL;
717
718 if (i_cal_component_isa (icomp) == I_CAL_VCALENDAR_COMPONENT) {
719 ICalComponent *subcomp;
720
721 for (subcomp = i_cal_component_get_first_component (icomp, kind);
722 subcomp;
723 g_object_unref (subcomp), subcomp = i_cal_component_get_next_component (icomp, kind)) {
724 if (!e_cal_util_component_has_property (subcomp, I_CAL_RECURRENCEID_PROPERTY)) {
725 main_comp = g_object_ref (subcomp);
726 break;
727 }
728 }
729
730 g_clear_object (&subcomp);
731 } else {
732 main_comp = g_object_ref (icomp);
733 }
734
735 return main_comp;
736 }
737
738 static gboolean
739 ecb_m365_save_recurrence_changes_locked_sync (ECalBackendM365 *cbm365,
740 const GSList *instances, /* ECalComponent * */
741 const gchar *m365_uid,
742 ICalComponent *comp_in_extra,
743 GCancellable *cancellable,
744 GError **error)
745 {
746 gboolean (* get_instance_id_func) (EM365Connection *cnc,
747 const gchar *user_override,
748 const gchar *group_id,
749 const gchar *calendar_id,
750 const gchar *event_id,
751 ICalTime *instance_time,
752 gchar **out_instance_id,
753 GCancellable *cancellable,
754 GError **error);
755 gboolean (* get_item_func) (EM365Connection *cnc,
756 const gchar *user_override,
757 const gchar *group_id,
758 const gchar *calendar_id,
759 const gchar *event_id,
760 const gchar *prefer_outlook_timezone,
761 const gchar *select,
762 JsonObject **out_item,
763 GCancellable *cancellable,
764 GError **error);
765 gboolean (* update_item_func) (EM365Connection *cnc,
766 const gchar *user_override,
767 const gchar *group_id,
768 const gchar *folder_id,
769 const gchar *item_id,
770 JsonBuilder *item,
771 GCancellable *cancellable,
772 GError **error);
773 gboolean (* delete_item_func) (EM365Connection *cnc,
774 const gchar *user_override,
775 const gchar *group_id,
776 const gchar *calendar_id,
777 const gchar *event_id,
778 GCancellable *cancellable,
779 GError **error);
780 ICalComponentKind kind;
781 GHashTable *known_exdates = NULL;
782 GSList *link;
783 gboolean success = TRUE;
784
785 kind = e_cal_backend_get_kind (E_CAL_BACKEND (cbm365));
786
787 switch (kind) {
788 case I_CAL_VEVENT_COMPONENT:
789 get_instance_id_func = e_m365_connection_get_event_instance_id_sync;
790 get_item_func = e_m365_connection_get_event_sync;
791 update_item_func = e_m365_connection_update_event_sync;
792 delete_item_func = e_m365_connection_delete_event_sync;
793 break;
794 case I_CAL_VTODO_COMPONENT:
795 get_instance_id_func = NULL;
796 get_item_func = e_m365_connection_get_task_sync;
797 update_item_func = e_m365_connection_update_task_sync;
798 delete_item_func = e_m365_connection_delete_task_sync;
799 break;
800 default:
801 g_warn_if_reached ();
802 return FALSE;
803 }
804
805 if (comp_in_extra) {
806 ICalComponent *main_comp;
807 ICalProperty *prop;
808
809 main_comp = ecb_m365_dup_main_comp (comp_in_extra, kind);
810
811 for (prop = main_comp ? i_cal_component_get_first_property (main_comp, I_CAL_EXDATE_PROPERTY) : NULL;
812 prop;
813 g_object_unref (prop), prop = i_cal_component_get_next_property (main_comp, I_CAL_EXDATE_PROPERTY)) {
814 ICalTime *exdate = i_cal_property_get_exdate (prop);
815
816 if (!exdate)
817 continue;
818
819 if (!known_exdates)
820 known_exdates = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
821
822 g_hash_table_add (known_exdates, i_cal_time_as_ical_string (exdate));
823
824 g_clear_object (&exdate);
825 }
826
827 g_clear_object (&prop);
828 g_clear_object (&main_comp);
829 }
830
831 for (link = (GSList *) instances; link && success; link = g_slist_next (link)) {
832 ICalComponent *icomp = e_cal_component_get_icalcomponent (link->data);
833 ICalProperty *prop;
834
835 if (!icomp)
836 continue;
837
838 prop = i_cal_component_get_first_property (icomp, I_CAL_RECURRENCEID_PROPERTY);
839 if (prop) {
840 ICalTime *recurid;
841
842 recurid = i_cal_property_get_recurrenceid (prop);
843 if (recurid) {
844 gchar *instance_id = NULL;
845 GError *local_error = NULL;
846
847 if (get_instance_id_func) {
848 success = get_instance_id_func (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
849 cbm365->priv->folder_id, m365_uid, recurid, &instance_id, cancellable, &local_error);
850 } else {
851 g_propagate_error (&local_error, EC_ERROR_EX (E_CLIENT_ERROR_NOT_SUPPORTED,
852 _("Cannot store detached instances, because not supported in Microsoft 365")));
853 success = FALSE;
854 }
855
856 if (g_error_matches (local_error, E_SOUP_SESSION_ERROR, SOUP_STATUS_NOT_FOUND)) {
857 /* this should not happen, but ignore the error if it does */
858 g_clear_error (&local_error);
859 success = TRUE;
860 } else if (success && instance_id) {
861 JsonObject *item = NULL;
862
863 success = get_item_func (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
864 cbm365->priv->folder_id, instance_id, NULL, NULL, &item, cancellable, &local_error);
865
866 if (success && item) {
867 ICalComponent *old_comp;
868
869 old_comp = ecb_m365_json_to_ical (cbm365, item, cancellable, &local_error);
870 if (old_comp) {
871 JsonBuilder *builder;
872
873 builder = ecb_m365_ical_to_json_locked (cbm365, icomp, old_comp, cancellable, &local_error);
874
875 if (builder) {
876 success = update_item_func (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
877 cbm365->priv->folder_id, instance_id, builder, cancellable, &local_error);
878
879 if (success && !local_error) {
880 success = ecb_m365_ical_to_json_2nd_go_locked (cbm365, icomp, old_comp,
881 instance_id, cancellable, &local_error);
882 }
883
884 g_clear_object (&builder);
885 } else if (local_error) {
886 success = FALSE;
887 }
888
889 g_clear_object (&old_comp);
890 } else {
891 success = FALSE;
892 }
893 }
894
895 g_clear_pointer (&item, json_object_unref);
896 }
897
898 g_clear_object (&recurid);
899 g_free (instance_id);
900
901 if (local_error) {
902 g_propagate_error (error, local_error);
903 success = FALSE;
904 }
905 }
906
907 g_clear_object (&prop);
908 } else {
909 /* master object, handle EXDATE-s, which are for removed and detached instances */
910 for (prop = i_cal_component_get_first_property (icomp, I_CAL_EXDATE_PROPERTY);
911 prop && success;
912 g_object_unref (prop), prop = i_cal_component_get_next_property (icomp, I_CAL_EXDATE_PROPERTY)) {
913 ICalTime *exdate = i_cal_property_get_exdate (prop);
914 gchar *exdate_str;
915
916 if (!exdate)
917 continue;
918
919 exdate_str = i_cal_time_as_ical_string (exdate);
920 if (exdate_str && (!known_exdates || !g_hash_table_contains (known_exdates, exdate_str))) {
921 /* new EXDATE */
922 gboolean is_detached_instance = FALSE;
923 GSList *link2;
924
925 for (link2 = (GSList *) instances; link2 && !is_detached_instance; link2 = g_slist_next (link2)) {
926 ICalComponent *subcomp = e_cal_component_get_icalcomponent (link2->data);
927 ICalProperty *subprop;
928
929 if (!subcomp || subcomp == icomp)
930 continue;
931
932 subprop = i_cal_component_get_first_property (subcomp, I_CAL_RECURRENCEID_PROPERTY);
933 if (subprop) {
934 ICalTime *subrecurid = i_cal_property_get_recurrenceid (subprop);
935 is_detached_instance = subrecurid && i_cal_time_compare_date_only (exdate, subrecurid) == 0;
936 g_clear_object (&subrecurid);
937 }
938
939 g_clear_object (&subprop);
940 }
941
942 if (!is_detached_instance) {
943 gchar *instance_id = NULL;
944 GError *local_error = NULL;
945
946 if (get_instance_id_func) {
947 success = get_instance_id_func (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
948 cbm365->priv->folder_id, m365_uid, exdate, &instance_id, cancellable, &local_error);
949 } else {
950 g_propagate_error (&local_error, EC_ERROR_EX (E_CLIENT_ERROR_NOT_SUPPORTED,
951 _("Cannot store detached instances, because not supported in Microsoft 365")));
952 success = FALSE;
953 }
954
955 if (g_error_matches (local_error, E_SOUP_SESSION_ERROR, SOUP_STATUS_NOT_FOUND)) {
956 /* removing already removed component => success */
957 g_clear_error (&local_error);
958 success = TRUE;
959 } else if (success && instance_id) {
960 success = delete_item_func (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
961 cbm365->priv->folder_id, instance_id, cancellable, &local_error);
962
963 if (g_error_matches (local_error, E_SOUP_SESSION_ERROR, SOUP_STATUS_NOT_FOUND)) {
964 /* removing already removed component => success */
965 g_clear_error (&local_error);
966 success = TRUE;
967 }
968 }
969
970 g_free (instance_id);
971
972 if (local_error) {
973 g_propagate_error (error, local_error);
974 success = FALSE;
975 }
976 }
977 }
978
979 g_clear_object (&exdate);
980 g_free (exdate_str);
981 }
982
983 g_clear_object (&prop);
984 }
985 }
986
987 g_clear_pointer (&known_exdates, g_hash_table_destroy);
988
989 return success;
990 }
991
992 static gboolean
993 ecb_m365_organizer_is_user (ECalBackendM365 *cbm365,
994 ICalComponent *icomp)
995 {
996 ICalProperty *prop;
997 const gchar *email;
998 gboolean is_organizer = FALSE;
999
1000 g_return_val_if_fail (E_IS_CAL_BACKEND_M365 (cbm365), FALSE);
1001 g_return_val_if_fail (I_CAL_IS_COMPONENT (icomp), FALSE);
1002
1003 prop = i_cal_component_get_first_property (icomp, I_CAL_ORGANIZER_PROPERTY);
1004 if (!prop)
1005 return FALSE;
1006
1007 email = i_cal_property_get_organizer (prop);
1008 if (email && *email) {
1009 CamelM365Settings *m365_settings;
1010 gchar *user_email;
1011
1012 m365_settings = camel_m365_settings_get_from_backend (E_BACKEND (cbm365), e_cal_backend_get_registry (E_CAL_BACKEND (cbm365)));
1013
1014 user_email = camel_m365_settings_dup_email (m365_settings);
1015
1016 email = e_cal_util_strip_mailto (email);
1017
1018 is_organizer = user_email && g_ascii_strcasecmp (email, user_email) == 0;
1019
1020 g_free (user_email);
1021
1022 if (!is_organizer) {
1023 GHashTable *aliases;
1024
1025 aliases = e_ews_common_utils_dup_mail_addresses (e_cal_backend_get_registry (E_CAL_BACKEND (cbm365)),
1026 e_backend_get_source (E_BACKEND (cbm365)), NULL);
1027
1028 if (aliases) {
1029 is_organizer = g_hash_table_contains (aliases, email);
1030
1031 g_hash_table_unref (aliases);
1032 }
1033 }
1034 }
1035
1036 g_object_unref (prop);
1037
1038 return is_organizer;
1039 }
1040
1041 static gboolean
1042 ecb_m365_save_component_sync (ECalMetaBackend *meta_backend,
1043 gboolean overwrite_existing,
1044 EConflictResolution conflict_resolution,
1045 const GSList *instances,
1046 const gchar *extra,
1047 guint32 opflags,
1048 gchar **out_new_uid,
1049 gchar **out_new_extra,
1050 GCancellable *cancellable,
1051 GError **error)
1052 {
1053 ECalBackendM365 *cbm365;
1054 ICalComponent *new_comp, *old_comp = NULL, *comp_in_extra = NULL;
1055 JsonBuilder *builder;
1056 gboolean success = FALSE, bad_instances = FALSE;
1057 gboolean (* create_item_func) (EM365Connection *cnc,
1058 const gchar *user_override,
1059 const gchar *group_id,
1060 const gchar *folder_id,
1061 JsonBuilder *item,
1062 JsonObject **out_created_item,
1063 GCancellable *cancellable,
1064 GError **error);
1065 gboolean (* update_item_func) (EM365Connection *cnc,
1066 const gchar *user_override,
1067 const gchar *group_id,
1068 const gchar *folder_id,
1069 const gchar *item_id,
1070 JsonBuilder *item,
1071 GCancellable *cancellable,
1072 GError **error);
1073 const gchar *(* get_id_func) (JsonObject *item);
1074 const gchar *(* get_change_key_func) (JsonObject *item);
1075
1076 g_return_val_if_fail (E_IS_CAL_BACKEND_M365 (meta_backend), FALSE);
1077 g_return_val_if_fail (instances != NULL, FALSE);
1078
1079 new_comp = e_cal_component_get_icalcomponent (instances->data);
1080
1081 if (instances->next) {
1082 gboolean has_master = FALSE;
1083 const gchar *uid = NULL;
1084 GSList *link;
1085
1086 for (link = (GSList *) instances; link; link = g_slist_next (link)) {
1087 ECalComponent *comp = link->data;
1088 ICalComponent *icomp = e_cal_component_get_icalcomponent (comp);
1089
1090 if (!uid)
1091 uid = i_cal_component_get_uid (icomp);
1092 else
1093 bad_instances = g_strcmp0 (uid, i_cal_component_get_uid (icomp)) != 0;
1094
1095 if (bad_instances)
1096 break;
1097
1098 if (!e_cal_util_component_has_property (icomp, I_CAL_RECURRENCEID_PROPERTY)) {
1099 bad_instances = has_master;
1100 has_master = TRUE;
1101 new_comp = icomp;
1102 }
1103 }
1104 }
1105
1106 switch (e_cal_backend_get_kind (E_CAL_BACKEND (meta_backend))) {
1107 case I_CAL_VEVENT_COMPONENT:
1108 if (bad_instances) {
1109 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_NOT_SUPPORTED,
1110 _("Can store only simple events into Microsoft 365 calendar")));
1111
1112 return FALSE;
1113 }
1114
1115 create_item_func = e_m365_connection_create_event_sync;
1116 update_item_func = e_m365_connection_update_event_sync;
1117 get_id_func = e_m365_event_get_id;
1118 get_change_key_func = e_m365_event_get_change_key;
1119 break;
1120 case I_CAL_VTODO_COMPONENT:
1121 if (bad_instances) {
1122 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_NOT_SUPPORTED,
1123 _("Can store only simple tasks into Microsoft 365 task folder")));
1124
1125 return FALSE;
1126 }
1127
1128 create_item_func = e_m365_connection_create_task_sync;
1129 update_item_func = e_m365_connection_update_task_sync;
1130 get_id_func = e_m365_task_get_id;
1131 get_change_key_func = e_m365_task_get_last_modified_as_string;
1132 break;
1133 default:
1134 g_warn_if_reached ();
1135 return FALSE;
1136 }
1137
1138 cbm365 = E_CAL_BACKEND_M365 (meta_backend);
1139
1140 LOCK (cbm365);
1141
1142 if (extra && *extra) {
1143 const gchar *comp_str;
1144
1145 comp_str = ecb_m365_get_component_from_extra (extra);
1146
1147 if (comp_str)
1148 comp_in_extra = i_cal_component_new_from_string (comp_str);
1149
1150 if (comp_in_extra)
1151 old_comp = ecb_m365_dup_main_comp (comp_in_extra, e_cal_backend_get_kind (E_CAL_BACKEND (meta_backend)));
1152 }
1153
1154 builder = ecb_m365_ical_to_json_locked (cbm365, new_comp, old_comp, cancellable, error);
1155
1156 if (builder) {
1157 if (overwrite_existing) {
1158 const gchar *uid = i_cal_component_get_uid (new_comp);
1159
1160 success = update_item_func (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1161 cbm365->priv->folder_id, uid, builder, cancellable, error);
1162
1163 if (success)
1164 success = ecb_m365_ical_to_json_2nd_go_locked (cbm365, new_comp, old_comp, uid, cancellable, error);
1165
1166 if (success) {
1167 /* To re-read it from the server */
1168 *out_new_uid = g_strdup (uid);
1169 }
1170 } else if (e_cal_util_component_has_organizer (new_comp) &&
1171 e_cal_util_component_has_attendee (new_comp) &&
1172 !ecb_m365_organizer_is_user (cbm365, new_comp)) {
1173 success = FALSE;
1174 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_PERMISSION_DENIED, _("Cannot create meetings organized by other users in a Microsoft 365 calendar.")));
1175 } else {
1176 JsonObject *created_item = NULL;
1177
1178 success = create_item_func (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1179 cbm365->priv->folder_id, builder, &created_item, cancellable, error);
1180
1181 if (success && created_item) {
1182 const gchar *m365_id = get_id_func (created_item);
1183
1184 success = ecb_m365_ical_to_json_2nd_go_locked (cbm365, new_comp, old_comp, m365_id, cancellable, error);
1185 }
1186
1187 if (success && created_item) {
1188 ICalComponent *icomp;
1189
1190 *out_new_uid = g_strdup (get_id_func (created_item));
1191
1192 icomp = ecb_m365_json_to_ical (cbm365, created_item, cancellable, error);
1193
1194 if (icomp) {
1195 gchar *ical_str;
1196
1197 ical_str = i_cal_component_as_ical_string (icomp);
1198
1199 *out_new_extra = ecb_m365_join_to_extra (get_change_key_func (created_item), ical_str);
1200
1201 g_clear_object (&icomp);
1202 g_free (ical_str);
1203 } else {
1204 success = FALSE;
1205 }
1206 }
1207
1208 if (created_item)
1209 json_object_unref (created_item);
1210 }
1211
1212 g_clear_object (&builder);
1213 }
1214
1215 if (*out_new_uid && success && (
1216 e_cal_util_component_has_recurrences (new_comp) ||
1217 e_cal_util_component_is_instance (new_comp))) {
1218 success = ecb_m365_save_recurrence_changes_locked_sync (cbm365, instances, *out_new_uid, comp_in_extra, cancellable, error);
1219 }
1220
1221 UNLOCK (cbm365);
1222
1223 ecb_m365_convert_error_to_client_error (error);
1224 ecb_m365_maybe_disconnect_sync (cbm365, error, cancellable);
1225
1226 g_clear_object (&old_comp);
1227 g_clear_object (&comp_in_extra);
1228
1229 return success;
1230 }
1231
1232 static gboolean
1233 ecb_m365_remove_component_sync (ECalMetaBackend *meta_backend,
1234 EConflictResolution conflict_resolution,
1235 const gchar *uid,
1236 const gchar *extra,
1237 const gchar *object,
1238 guint32 opflags,
1239 GCancellable *cancellable,
1240 GError **error)
1241 {
1242 ECalBackendM365 *cbm365;
1243 GError *local_error = NULL;
1244 gboolean success;
1245
1246 g_return_val_if_fail (E_IS_CAL_BACKEND_M365 (meta_backend), FALSE);
1247 g_return_val_if_fail (object != NULL, FALSE);
1248
1249 cbm365 = E_CAL_BACKEND_M365 (meta_backend);
1250
1251 LOCK (cbm365);
1252
1253 switch (e_cal_backend_get_kind (E_CAL_BACKEND (cbm365))) {
1254 case I_CAL_VEVENT_COMPONENT:
1255 success = e_m365_connection_delete_event_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1256 cbm365->priv->folder_id, uid, cancellable, &local_error);
1257 break;
1258 case I_CAL_VTODO_COMPONENT:
1259 success = e_m365_connection_delete_task_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1260 cbm365->priv->folder_id, uid, cancellable, &local_error);
1261 break;
1262 default:
1263 g_warn_if_reached ();
1264 success = FALSE;
1265 break;
1266 }
1267
1268 UNLOCK (cbm365);
1269
1270 if (g_error_matches (local_error, E_M365_ERROR, E_M365_ERROR_ITEM_NOT_FOUND)) {
1271 g_clear_error (&local_error);
1272 success = TRUE;
1273 } else if (local_error) {
1274 g_propagate_error (error, local_error);
1275 }
1276
1277 ecb_m365_convert_error_to_client_error (error);
1278 ecb_m365_maybe_disconnect_sync (cbm365, error, cancellable);
1279
1280 return success;
1281 }
1282
1283 static void
1284 ecb_m365_discard_alarm_sync (ECalBackendSync *cal_backend_sync,
1285 EDataCal *cal,
1286 GCancellable *cancellable,
1287 const gchar *uid,
1288 const gchar *rid,
1289 const gchar *auid,
1290 guint32 opflags,
1291 GError **error)
1292 {
1293 ECalBackendM365 *cbm365;
1294
1295 g_return_if_fail (E_IS_CAL_BACKEND_M365 (cal_backend_sync));
1296 g_return_if_fail (uid != NULL);
1297
1298 if (e_cal_backend_get_kind (E_CAL_BACKEND (cal_backend_sync)) != I_CAL_VEVENT_COMPONENT) {
1299 g_propagate_error (error, EC_ERROR (E_CLIENT_ERROR_NOT_SUPPORTED));
1300 return;
1301 }
1302
1303 cbm365 = E_CAL_BACKEND_M365 (cal_backend_sync);
1304
1305 if (!e_cal_meta_backend_ensure_connected_sync (E_CAL_META_BACKEND (cbm365), cancellable, error))
1306 return;
1307
1308 LOCK (cbm365);
1309
1310 e_m365_connection_dismiss_reminder_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1311 cbm365->priv->folder_id, uid, cancellable, error);
1312
1313 UNLOCK (cbm365);
1314
1315 ecb_m365_convert_error_to_client_error (error);
1316 ecb_m365_maybe_disconnect_sync (cbm365, error, cancellable);
1317 }
1318
1319 static gboolean
1320 ecb_m365_get_rsvp (ICalProperty *attendee,
1321 EM365ResponseType *out_response,
1322 gboolean *out_rsvp_requested)
1323 {
1324 ICalParameter *param;
1325
1326 if (!attendee)
1327 return FALSE;
1328
1329 param = i_cal_property_get_first_parameter (attendee, I_CAL_PARTSTAT_PARAMETER);
1330 if (param) {
1331 switch (i_cal_parameter_get_partstat (param)) {
1332 case I_CAL_PARTSTAT_NEEDSACTION:
1333 *out_response = E_M365_RESPONSE_NOT_RESPONDED;
1334 break;
1335 case I_CAL_PARTSTAT_ACCEPTED:
1336 *out_response = E_M365_RESPONSE_ACCEPTED;
1337 break;
1338 case I_CAL_PARTSTAT_DECLINED:
1339 *out_response = E_M365_RESPONSE_DECLINED;
1340 break;
1341 case I_CAL_PARTSTAT_TENTATIVE:
1342 *out_response = E_M365_RESPONSE_TENTATIVELY_ACCEPTED;
1343 break;
1344 default:
1345 g_clear_object (&param);
1346 return FALSE;
1347 }
1348 g_clear_object (&param);
1349 }
1350
1351 param = i_cal_property_get_first_parameter (attendee, I_CAL_RSVP_PARAMETER);
1352 if (param) {
1353 *out_rsvp_requested = i_cal_parameter_get_rsvp (param) == I_CAL_RSVP_TRUE;
1354 g_clear_object (&param);
1355 }
1356
1357 return TRUE;
1358 }
1359
1360 static EM365ResponseType
1361 ecb_m365_get_current_user_meeting_reponse (ECalBackendM365 *cbm365,
1362 ICalComponent *icomp,
1363 const gchar *current_user_mail,
1364 GHashTable *aliases,
1365 gboolean *out_rsvp_requested)
1366 {
1367 ICalProperty *attendee;
1368 const gchar *attendee_str = NULL, *attendee_mail = NULL;
1369 gint attendees_count = 0;
1370 EM365ResponseType response = E_M365_RESPONSE_NOT_RESPONDED;
1371 gboolean found = FALSE;
1372
1373 if (out_rsvp_requested)
1374 *out_rsvp_requested = FALSE;
1375
1376 attendee = i_cal_component_get_first_property (icomp, I_CAL_ORGANIZER_PROPERTY);
1377 if (attendee) {
1378 attendee_str = i_cal_property_get_organizer (attendee);
1379
1380 if (attendee_str) {
1381 attendee_mail = e_cal_util_strip_mailto (attendee_str);
1382
1383 if (attendee_mail && ((current_user_mail && g_ascii_strcasecmp (attendee_mail, current_user_mail) == 0) ||
1384 (aliases && g_hash_table_contains (aliases, attendee_mail)))) {
1385 g_object_unref (attendee);
1386 return E_M365_RESPONSE_ORGANIZER;
1387 }
1388 }
1389
1390 g_object_unref (attendee);
1391 }
1392
1393 for (attendee = i_cal_component_get_first_property (icomp, I_CAL_ATTENDEE_PROPERTY);
1394 attendee;
1395 g_object_unref (attendee), attendee = i_cal_component_get_next_property (icomp, I_CAL_ATTENDEE_PROPERTY), attendees_count++) {
1396 attendee_str = i_cal_property_get_attendee (attendee);
1397
1398 if (attendee_str != NULL) {
1399 attendee_mail = e_cal_util_strip_mailto (attendee_str);
1400
1401 if (attendee_mail && ((current_user_mail && g_ascii_strcasecmp (attendee_mail, current_user_mail) == 0) ||
1402 (aliases && g_hash_table_contains (aliases, attendee_mail)))) {
1403 found = ecb_m365_get_rsvp (attendee, &response, out_rsvp_requested);
1404 if (found) {
1405 if (response == E_M365_RESPONSE_NOT_RESPONDED) {
1406 found = FALSE;
1407 } else {
1408 /* stop early when have a response, in case
1409 the user is in the attendees multiple times */
1410 g_clear_object (&attendee);
1411 break;
1412 }
1413 }
1414 }
1415 }
1416 }
1417
1418 /* this should not happen, but if the user's configured email does not match the one
1419 used in the invitation, like when the invitation comes to a mailing list... */
1420 if (!found && attendees_count == 1) {
1421 attendee = i_cal_component_get_first_property (icomp, I_CAL_ATTENDEE_PROPERTY);
1422 found = ecb_m365_get_rsvp (attendee, &response, out_rsvp_requested);
1423 } else if (!found) {
1424 attendee = e_ews_common_utils_find_attendee (E_CAL_BACKEND (cbm365), icomp, aliases);
1425
1426 if (attendee) {
1427 found = ecb_m365_get_rsvp (attendee, &response, out_rsvp_requested);
1428 g_clear_object (&attendee);
1429 }
1430 }
1431
1432 return response;
1433 }
1434
1435 static gboolean
1436 ecb_m365_comp_exists (ECalBackendM365 *cbm365,
1437 ICalComponent *subcomp,
1438 gchar **out_real_id,
1439 GCancellable *cancellable)
1440 {
1441 const gchar *uid = i_cal_component_get_uid (subcomp);
1442 gboolean exists = FALSE;
1443 ECalCache *cal_cache;
1444
1445 if (!uid)
1446 return FALSE;
1447
1448 cal_cache = e_cal_meta_backend_ref_cache (E_CAL_META_BACKEND (cbm365));
1449 if (cal_cache) {
1450 exists = e_cal_cache_contains (cal_cache, uid, NULL, E_CACHE_EXCLUDE_DELETED);
1451 g_clear_object (&cal_cache);
1452 }
1453
1454 if (!exists) {
1455 EM365Event *event = NULL;
1456
1457 LOCK (cbm365);
1458 exists = e_m365_connection_get_event_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1459 cbm365->priv->folder_id, uid, NULL, "id", &event, cancellable, NULL);
1460 UNLOCK (cbm365);
1461
1462 g_clear_pointer (&event, json_object_unref);
1463 }
1464
1465 if (!exists) {
1466 /* search for the event by the iCalUid property */
1467 gchar *filter = g_strdup_printf ("iCalUid eq '%s'", uid);
1468 GSList *events = NULL;
1469
1470 LOCK (cbm365);
1471
1472 if (e_m365_connection_list_events_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1473 cbm365->priv->folder_id, NULL, "id", filter, &events, cancellable, NULL)) {
1474 if (events && !events->next) {
1475 EM365Event *event = events->data;
1476 const gchar *tmp_id = e_m365_event_get_id (event);
1477
1478 if (tmp_id && *tmp_id) {
1479 exists = TRUE;
1480
1481 if (out_real_id)
1482 *out_real_id = g_strdup (tmp_id);
1483 }
1484 }
1485
1486 g_slist_free_full (events, (GDestroyNotify) json_object_unref);
1487 }
1488
1489 UNLOCK (cbm365);
1490
1491 g_free (filter);
1492 }
1493
1494 return exists;
1495 }
1496
1497 static void
1498 ecb_m365_receive_objects_sync (ECalBackendSync *sync_backend,
1499 EDataCal *cal,
1500 GCancellable *cancellable,
1501 const gchar *calobj,
1502 guint32 opflags,
1503 GError **error)
1504 {
1505 ECalBackendM365 *cbm365;
1506 ECalBackend *cal_backend;
1507 CamelM365Settings *m365_settings;
1508 ICalComponent *icomp, *subcomp;
1509 ICalComponentKind kind;
1510 GHashTable *aliases;
1511 gchar *user_email;
1512 gboolean success = TRUE, do_refresh = FALSE;
1513
1514 g_return_if_fail (E_IS_CAL_BACKEND_M365 (sync_backend));
1515
1516 cbm365 = E_CAL_BACKEND_M365 (sync_backend);
1517
1518 if (!e_cal_meta_backend_ensure_connected_sync (E_CAL_META_BACKEND (cbm365), cancellable, error))
1519 return;
1520
1521 icomp = calobj ? i_cal_parser_parse_string (calobj) : NULL;
1522
1523 if (!icomp) {
1524 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
1525 return;
1526 }
1527
1528 /* make sure ical data we parse is actually a vCalendar component */
1529 if (i_cal_component_isa (icomp) != I_CAL_VCALENDAR_COMPONENT) {
1530 g_object_unref (icomp);
1531 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
1532 return;
1533 }
1534
1535 cal_backend = E_CAL_BACKEND (cbm365);
1536 kind = e_cal_backend_get_kind (cal_backend);
1537
1538 m365_settings = camel_m365_settings_get_from_backend (E_BACKEND (cal_backend), e_cal_backend_get_registry (cal_backend));
1539 user_email = camel_m365_settings_dup_email (m365_settings);
1540 aliases = e_ews_common_utils_dup_mail_addresses (e_cal_backend_get_registry (E_CAL_BACKEND (cbm365)),
1541 e_backend_get_source (E_BACKEND (cbm365)), &user_email);
1542
1543 switch (i_cal_component_get_method (icomp)) {
1544 case I_CAL_METHOD_REQUEST:
1545 case I_CAL_METHOD_PUBLISH:
1546 case I_CAL_METHOD_REPLY: {
1547 GHashTable *split_by_uid = g_hash_table_new (g_str_hash, g_str_equal);
1548 GHashTableIter iter;
1549 gpointer value;
1550
1551 for (subcomp = i_cal_component_get_first_component (icomp, kind);
1552 subcomp;
1553 g_object_unref (subcomp), subcomp = i_cal_component_get_next_component (icomp, kind)) {
1554 const gchar *uid = i_cal_component_get_uid (subcomp);
1555 if (uid && *uid) {
1556 GSList *comps;
1557 comps = g_hash_table_lookup (split_by_uid, uid);
1558 comps = g_slist_prepend (comps, g_object_ref (subcomp));
1559
1560 g_hash_table_insert (split_by_uid, (gpointer) uid, comps);
1561 }
1562 }
1563
1564 g_hash_table_iter_init (&iter, split_by_uid);
1565 while (success && g_hash_table_iter_next (&iter, NULL, &value)) {
1566 GSList *comps = value;
1567 gchar *real_id = NULL;
1568
1569 subcomp = comps->data;
1570
1571 if (ecb_m365_comp_exists (cbm365, subcomp, &real_id, cancellable)) {
1572 EM365ResponseType response_type;
1573 gboolean rsvp_requested = FALSE;
1574
1575 /* getting a data for meeting request response */
1576 response_type = ecb_m365_get_current_user_meeting_reponse (cbm365, subcomp, user_email, aliases, &rsvp_requested);
1577 rsvp_requested = rsvp_requested && !(opflags & E_CAL_OPERATION_FLAG_DISABLE_ITIP_MESSAGE);
1578
1579 if (response_type == E_M365_RESPONSE_ACCEPTED ||
1580 response_type == E_M365_RESPONSE_DECLINED ||
1581 response_type == E_M365_RESPONSE_TENTATIVELY_ACCEPTED) {
1582 const gchar *comment = i_cal_component_get_comment (subcomp);
1583 const gchar *uid = i_cal_component_get_uid (subcomp);
1584 GError *local_error = NULL;
1585
1586 if (comment && !*comment)
1587 comment = NULL;
1588
1589 LOCK (cbm365);
1590
1591 success = e_m365_connection_response_event_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1592 cbm365->priv->folder_id, real_id ? real_id : uid, response_type, comment, rsvp_requested, cancellable, &local_error);
1593
1594 if (!success && g_error_matches (local_error, E_M365_ERROR, E_M365_ERROR_ID_MALFORMED)) {
1595 /* search for the event by the iCalUid property */
1596 gchar *filter = g_strdup_printf ("iCalUid eq '%s'", uid);
1597 GSList *events = NULL;
1598
1599 g_clear_error (&local_error);
1600
1601 if (e_m365_connection_list_events_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1602 cbm365->priv->folder_id, NULL, "id", filter, &events, cancellable, NULL)) {
1603 if (events && !events->next) {
1604 EM365Event *event = events->data;
1605 const gchar *tmp_id = e_m365_event_get_id (event);
1606 if (tmp_id && *tmp_id) {
1607 g_clear_error (&local_error);
1608 success = e_m365_connection_response_event_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1609 cbm365->priv->folder_id, tmp_id, response_type, comment, rsvp_requested, cancellable, &local_error);
1610 }
1611 }
1612 g_slist_free_full (events, (GDestroyNotify) json_object_unref);
1613 }
1614 g_free (filter);
1615
1616 if (!success && !local_error)
1617 local_error = ECC_ERROR (E_CAL_CLIENT_ERROR_OBJECT_NOT_FOUND);
1618 }
1619
1620 if (local_error)
1621 g_propagate_error (error, local_error);
1622
1623 UNLOCK (cbm365);
1624
1625 do_refresh = TRUE;
1626 } else if (e_cal_util_component_has_organizer (subcomp) &&
1627 e_cal_util_component_has_attendee (subcomp)) {
1628 g_set_error (error, E_CAL_CLIENT_ERROR, E_CAL_CLIENT_ERROR_UNKNOWN_USER, _("Cannot find user “%s” between attendees"), user_email ? user_email : "NULL");
1629 success = FALSE;
1630 }
1631
1632 g_free (real_id);
1633 } else {
1634 EDataCal *data_cal;
1635 GSList *calobjs = NULL, *link, *new_uids = NULL, *new_components = NULL;
1636 GError *local_error = NULL;
1637
1638 if (e_cal_util_component_has_organizer (subcomp) &&
1639 e_cal_util_component_has_attendee (subcomp) &&
1640 !ecb_m365_organizer_is_user (cbm365, subcomp)) {
1641 g_propagate_error (error, EC_ERROR_EX (E_CLIENT_ERROR_PERMISSION_DENIED, _("Cannot create meetings organized by other users in a Microsoft 365 calendar.")));
1642 success = FALSE;
1643 break;
1644 }
1645
1646 for (link = comps; link; link = g_slist_next (link)) {
1647 subcomp = link->data;
1648 calobjs = g_slist_prepend (calobjs, i_cal_component_as_ical_string (subcomp));
1649 }
1650
1651 data_cal = e_cal_backend_ref_data_cal (E_CAL_BACKEND (cbm365));
1652
1653 e_cal_backend_sync_create_objects (E_CAL_BACKEND_SYNC (cbm365), data_cal, cancellable, calobjs,
1654 opflags | E_CAL_OPERATION_FLAG_DISABLE_ITIP_MESSAGE, &new_uids, &new_components, &local_error);
1655
1656 success = !local_error;
1657
1658 if (local_error)
1659 g_propagate_error (error, local_error);
1660
1661 g_slist_free_full (calobjs, g_free);
1662 g_slist_free_full (new_uids, g_free);
1663 g_slist_free_full (new_components, g_object_unref);
1664 g_clear_object (&data_cal);
1665 }
1666 }
1667 g_hash_table_iter_init (&iter, split_by_uid);
1668 while (g_hash_table_iter_next (&iter, NULL, &value)) {
1669 GSList *comps = value;
1670 g_slist_free_full (comps, g_object_unref);
1671 }
1672 g_hash_table_destroy (split_by_uid);
1673 } break;
1674 case I_CAL_METHOD_COUNTER:
1675 /*
1676 * this is a new time proposal mail from one of the attendees
1677 * if we decline the proposal, nothing have to be done
1678 * if we accept it we will call to modify_object
1679 */
1680 success = FALSE;
1681 g_propagate_error (error, EC_ERROR (E_CLIENT_ERROR_NOT_SUPPORTED));
1682 break;
1683 case I_CAL_METHOD_CANCEL: {
1684 ECalObjModType mod_type = E_CAL_OBJ_MOD_ALL;
1685 GSList *ids = NULL;
1686
1687 for (subcomp = i_cal_component_get_first_component (icomp, kind);
1688 subcomp && success;
1689 g_object_unref (subcomp), subcomp = i_cal_component_get_next_component (icomp, kind)) {
1690 const gchar *uid;
1691 gchar *rid;
1692
1693 uid = i_cal_component_get_uid (subcomp);
1694 rid = e_cal_util_component_get_recurid_as_string (subcomp);
1695
1696 if (rid && *rid)
1697 mod_type = E_CAL_OBJ_MOD_THIS;
1698
1699 ids = g_slist_prepend (ids, e_cal_component_id_new (uid, rid));
1700
1701 g_free (rid);
1702 }
1703
1704 g_clear_object (&subcomp);
1705
1706 if (ids) {
1707 GSList *old_comps = NULL, *new_comps = NULL;
1708 GError *local_error = NULL;
1709
1710 e_cal_backend_sync_remove_objects (sync_backend, cal, cancellable, ids, mod_type, opflags, &old_comps, &new_comps, &local_error);
1711
1712 do_refresh = !local_error;
1713
1714 e_util_free_nullable_object_slist (old_comps);
1715 e_util_free_nullable_object_slist (new_comps);
1716 g_clear_error (&local_error);
1717 }
1718
1719 g_slist_free_full (ids, e_cal_component_id_free);
1720 } break;
1721 default:
1722 break;
1723 }
1724
1725 g_clear_pointer (&aliases, g_hash_table_unref);
1726 g_object_unref (icomp);
1727 g_free (user_email);
1728
1729 if (success && do_refresh)
1730 e_cal_meta_backend_schedule_refresh (E_CAL_META_BACKEND (cbm365));
1731
1732 ecb_m365_convert_error_to_client_error (error);
1733 ecb_m365_maybe_disconnect_sync (cbm365, error, cancellable);
1734 }
1735
1736 static void
1737 ecb_m365_send_objects_sync (ECalBackendSync *sync_backend,
1738 EDataCal *cal,
1739 GCancellable *cancellable,
1740 const gchar *calobj,
1741 guint32 opflags,
1742 GSList **users,
1743 gchar **modified_calobj,
1744 GError **error)
1745 {
1746 ECalBackendM365 *cbm365;
1747 ICalComponentKind kind;
1748 ICalComponent *icomp, *subcomp = NULL;
1749
1750 g_return_if_fail (E_IS_CAL_BACKEND_M365 (sync_backend));
1751
1752 cbm365 = E_CAL_BACKEND_M365 (sync_backend);
1753
1754 if (!e_cal_meta_backend_ensure_connected_sync (E_CAL_META_BACKEND (cbm365), cancellable, error))
1755 return;
1756
1757 icomp = calobj ? i_cal_parser_parse_string (calobj) : NULL;
1758
1759 /* make sure data was parsed properly */
1760 if (!icomp) {
1761 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
1762 return;
1763 }
1764
1765 /* make sure ical data we parse is actually a vcal component */
1766 if (i_cal_component_isa (icomp) != I_CAL_VCALENDAR_COMPONENT &&
1767 i_cal_component_isa (icomp) != I_CAL_VEVENT_COMPONENT) {
1768 g_object_unref (icomp);
1769 g_propagate_error (error, ECC_ERROR (E_CAL_CLIENT_ERROR_INVALID_OBJECT));
1770 return;
1771 }
1772
1773 if (i_cal_component_get_method (icomp) != I_CAL_METHOD_CANCEL) {
1774 g_object_unref (icomp);
1775 g_propagate_error (error, EC_ERROR (E_CLIENT_ERROR_NOT_SUPPORTED));
1776 return;
1777 }
1778
1779 kind = e_cal_backend_get_kind (E_CAL_BACKEND (cbm365));
1780
1781 if (i_cal_component_isa (icomp) == I_CAL_VCALENDAR_COMPONENT) {
1782 subcomp = i_cal_component_get_first_component (icomp, kind);
1783 } else if (i_cal_component_isa (icomp) == I_CAL_VEVENT_COMPONENT) {
1784 subcomp = g_object_ref (icomp);
1785 }
1786
1787 if (subcomp) {
1788 ICalProperty *prop;
1789 const gchar *uid = i_cal_component_get_uid (subcomp);
1790 const gchar *comment = i_cal_component_get_comment (subcomp);
1791 gchar *instance_id = NULL;
1792 gboolean success = TRUE;
1793
1794 LOCK (cbm365);
1795
1796 prop = i_cal_component_get_first_property (subcomp, I_CAL_RECURRENCEID_PROPERTY);
1797 if (prop) {
1798 ICalTime *recurid;
1799
1800 recurid = i_cal_property_get_recurrenceid (prop);
1801 if (recurid) {
1802 success = e_m365_connection_get_event_instance_id_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1803 cbm365->priv->folder_id, uid, recurid, &instance_id, cancellable, error);
1804
1805 if (success && instance_id)
1806 uid = instance_id;
1807
1808 g_clear_object (&recurid);
1809 }
1810 }
1811
1812 if (success) {
1813 e_m365_connection_cancel_event_sync (cbm365->priv->cnc, NULL, cbm365->priv->group_id,
1814 cbm365->priv->folder_id, uid, comment, cancellable, error);
1815 }
1816
1817 UNLOCK (cbm365);
1818
1819 g_clear_object (&prop);
1820 g_free (instance_id);
1821 }
1822
1823 g_clear_object (&subcomp);
1824 g_object_unref (icomp);
1825
1826 ecb_m365_convert_error_to_client_error (error);
1827 ecb_m365_maybe_disconnect_sync (cbm365, error, cancellable);
1828 }
1829
1830 static void
1831 ecb_m365_get_free_busy_sync (ECalBackendSync *cal_backend_sync,
1832 EDataCal *cal,
1833 GCancellable *cancellable,
1834 const GSList *users,
1835 time_t start,
1836 time_t end,
1837 GSList **out_freebusyobjs,
1838 GError **error)
1839 {
1840 ECalBackendM365 *cbm365;
1841 GSList *infos = NULL;
1842 gboolean success;
1843
1844 g_return_if_fail (E_IS_CAL_BACKEND_M365 (cal_backend_sync));
1845 g_return_if_fail (users != NULL);
1846 g_return_if_fail (out_freebusyobjs != NULL);
1847
1848 if (e_cal_backend_get_kind (E_CAL_BACKEND (cal_backend_sync)) != I_CAL_VEVENT_COMPONENT) {
1849 g_propagate_error (error, EC_ERROR (E_CLIENT_ERROR_NOT_SUPPORTED));
1850 return;
1851 }
1852
1853 cbm365 = E_CAL_BACKEND_M365 (cal_backend_sync);
1854
1855 if (!e_cal_meta_backend_ensure_connected_sync (E_CAL_META_BACKEND (cbm365), cancellable, error))
1856 return;
1857
1858 LOCK (cbm365);
1859
1860 success = e_m365_connection_get_schedule_sync (cbm365->priv->cnc, NULL, 30, start, end, users, &infos, cancellable, error);
1861
1862 UNLOCK (cbm365);
1863
1864 ecb_m365_convert_error_to_client_error (error);
1865 ecb_m365_maybe_disconnect_sync (cbm365, error, cancellable);
1866
1867 if (success) {
1868 ICalTimezone *utc_zone = i_cal_timezone_get_utc_timezone ();
1869 GSList *link;
1870
1871 *out_freebusyobjs = NULL;
1872
1873 for (link = infos; link; link = g_slist_next (link)) {
1874 EM365ScheduleInformation *schinfo = link->data;
1875 ICalComponent *vfb = NULL;
1876 JsonArray *array;
1877 guint ii, sz;
1878
1879 if (!schinfo || !e_m365_schedule_information_get_schedule_id (schinfo))
1880 continue;
1881
1882 array = e_m365_schedule_information_get_schedule_items (schinfo);
1883 sz = array ? json_array_get_length (array) : 0;
1884
1885 for (ii = 0; ii < sz; ii++) {
1886 EM365ScheduleItem *schitem = json_array_get_object_element (array, ii);
1887 EM365DateTimeWithZone *dt;
1888 ICalProperty *prop;
1889 ICalPeriod *ipt;
1890 ICalTime *itt;
1891 const gchar *tmp;
1892
1893 if (!schitem || !e_m365_schedule_item_get_start (schitem) || !e_m365_schedule_item_get_end (schitem))
1894 continue;
1895
1896 ipt = i_cal_period_new_null_period ();
1897
1898 dt = e_m365_schedule_item_get_start (schitem);
1899 itt = i_cal_time_new_from_timet_with_zone (e_m365_date_time_get_date_time (dt), 0, utc_zone);
1900 i_cal_period_set_start (ipt, itt);
1901 g_clear_object (&itt);
1902
1903 dt = e_m365_schedule_item_get_end (schitem);
1904 itt = i_cal_time_new_from_timet_with_zone (e_m365_date_time_get_date_time (dt), 0, utc_zone);
1905 i_cal_period_set_end (ipt, itt);
1906 g_clear_object (&itt);
1907
1908 prop = i_cal_property_new_freebusy (ipt);
1909 g_clear_object (&ipt);
1910
1911 switch (e_m365_schedule_item_get_status (schitem)) {
1912 case E_M365_FREE_BUSY_STATUS_FREE:
1913 i_cal_property_set_parameter_from_string (prop, "FBTYPE", "FREE");
1914 break;
1915 case E_M365_FREE_BUSY_STATUS_TENTATIVE:
1916 i_cal_property_set_parameter_from_string (prop, "FBTYPE", "BUSY-TENTATIVE");
1917 break;
1918 case E_M365_FREE_BUSY_STATUS_BUSY:
1919 i_cal_property_set_parameter_from_string (prop, "FBTYPE", "BUSY");
1920 break;
1921 case E_M365_FREE_BUSY_STATUS_OOF:
1922 case E_M365_FREE_BUSY_STATUS_WORKING_ELSEWHERE:
1923 i_cal_property_set_parameter_from_string (prop, "FBTYPE", "BUSY-UNAVAILABLE");
1924 break;
1925 default:
1926 break;
1927 }
1928
1929 tmp = e_m365_schedule_item_get_subject (schitem);
1930
1931 if (tmp && *tmp)
1932 i_cal_property_set_parameter_from_string (prop, "X-SUMMARY", tmp);
1933
1934 tmp = e_m365_schedule_item_get_location (schitem);
1935
1936 if (tmp && *tmp)
1937 i_cal_property_set_parameter_from_string (prop, "X-LOCATION", tmp);
1938
1939 if (!vfb)
1940 vfb = i_cal_component_new_vfreebusy ();
1941
1942 i_cal_component_take_property (vfb, prop);
1943 }
1944
1945 if (vfb) {
1946 gchar *mailto;
1947
1948 mailto = g_strconcat ("mailto:", e_m365_schedule_information_get_schedule_id (schinfo), NULL);
1949 i_cal_component_take_property (vfb, i_cal_property_new_attendee (mailto));
1950 g_free (mailto);
1951
1952 *out_freebusyobjs = g_slist_prepend (*out_freebusyobjs, i_cal_component_as_ical_string (vfb));
1953
1954 g_clear_object (&vfb);
1955 }
1956 }
1957
1958 *out_freebusyobjs = g_slist_reverse (*out_freebusyobjs);
1959 }
1960
1961 g_slist_free_full (infos, (GDestroyNotify) json_object_unref);
1962
1963 ecb_m365_convert_error_to_client_error (error);
1964 ecb_m365_maybe_disconnect_sync (cbm365, error, cancellable);
1965 }
1966
1967 static gchar *
1968 ecb_m365_get_backend_property (ECalBackend *cal_backend,
1969 const gchar *prop_name)
1970 {
1971 ECalBackendM365 *cbm365;
1972
1973 g_return_val_if_fail (E_IS_CAL_BACKEND_M365 (cal_backend), NULL);
1974 g_return_val_if_fail (prop_name != NULL, NULL);
1975
1976 cbm365 = E_CAL_BACKEND_M365 (cal_backend);
1977
1978 if (g_str_equal (prop_name, CLIENT_BACKEND_PROPERTY_CAPABILITIES)) {
1979 return g_strjoin (
1980 ",",
1981 E_CAL_STATIC_CAPABILITY_NO_EMAIL_ALARMS,
1982 E_CAL_STATIC_CAPABILITY_NO_AUDIO_ALARMS,
1983 E_CAL_STATIC_CAPABILITY_NO_PROCEDURE_ALARMS,
1984 E_CAL_STATIC_CAPABILITY_ONE_ALARM_ONLY,
1985 E_CAL_STATIC_CAPABILITY_REMOVE_ALARMS,
1986 E_CAL_STATIC_CAPABILITY_NO_THISANDPRIOR,
1987 E_CAL_STATIC_CAPABILITY_NO_THISANDFUTURE,
1988 E_CAL_STATIC_CAPABILITY_NO_CONV_TO_ASSIGN_TASK,
1989 E_CAL_STATIC_CAPABILITY_NO_TASK_ASSIGNMENT,
1990 E_CAL_STATIC_CAPABILITY_SAVE_SCHEDULES,
1991 E_CAL_STATIC_CAPABILITY_CREATE_MESSAGES,
1992 E_CAL_STATIC_CAPABILITY_NO_ALARM_AFTER_START,
1993 E_CAL_STATIC_CAPABILITY_NO_MEMO_START_DATE,
1994 E_CAL_STATIC_CAPABILITY_ALL_DAY_EVENT_AS_TIME,
1995 E_CAL_STATIC_CAPABILITY_TASK_DATE_ONLY,
1996 E_CAL_STATIC_CAPABILITY_TASK_NO_ALARM,
1997 E_CAL_STATIC_CAPABILITY_TASK_CAN_RECUR,
1998 E_CAL_STATIC_CAPABILITY_TASK_HANDLE_RECUR,
1999 E_CAL_STATIC_CAPABILITY_RETRACT_SUPPORTED,
2000 E_CAL_STATIC_CAPABILITY_USER_IS_ORGANIZER_ONLY,
2001 e_cal_meta_backend_get_capabilities (E_CAL_META_BACKEND (cbm365)),
2002 NULL);
2003 } else if (g_str_equal (prop_name, E_CAL_BACKEND_PROPERTY_CAL_EMAIL_ADDRESS)) {
2004 /* return email address of the person who opened the calendar */
2005 CamelM365Settings *m365_settings;
2006
2007 m365_settings = camel_m365_settings_get_from_backend (E_BACKEND (cal_backend), e_cal_backend_get_registry (cal_backend));
2008
2009 return camel_m365_settings_dup_email (m365_settings);
2010 } else if (g_str_equal (prop_name, E_CAL_BACKEND_PROPERTY_ALARM_EMAIL_ADDRESS)) {
2011 /* Microsoft 365 does not support email based alarms */
2012 return NULL;
2013 }
2014
2015 /* Chain up to parent's method. */
2016 return E_CAL_BACKEND_CLASS (e_cal_backend_m365_parent_class)->impl_get_backend_property (cal_backend, prop_name);
2017 }
2018
2019 static gboolean
2020 ecb_m365_get_destination_address (EBackend *backend,
2021 gchar **host,
2022 guint16 *port)
2023 {
2024 g_return_val_if_fail (port != NULL, FALSE);
2025 g_return_val_if_fail (host != NULL, FALSE);
2026
2027 /* Sanity checking */
2028 if (!e_cal_backend_get_registry (E_CAL_BACKEND (backend)) ||
2029 !e_backend_get_source (backend))
2030 return FALSE;
2031
2032 *host = g_strdup ("graph.microsoft.com");
2033 *port = 443;
2034
2035 return TRUE;
2036 }
2037
2038 static gchar *
2039 ecb_m365_dup_component_revision (ECalCache *cal_cache,
2040 ICalComponent *icomp,
2041 gpointer user_data)
2042 {
2043 g_return_val_if_fail (icomp != NULL, NULL);
2044
2045 return e_cal_util_component_dup_x_property (icomp, "X-EVOLUTION-CHANGEKEY");
2046 }
2047
2048 static void
2049 ecb_m365_constructed (GObject *object)
2050 {
2051 ECalBackendM365 *cbm365 = E_CAL_BACKEND_M365 (object);
2052 ECalCache *cal_cache;
2053 ESourceRegistry *registry;
2054 CamelM365Settings *m365_settings;
2055 gchar *user_email;
2056 gchar *cache_dirname;
2057
2058 /* Chain up to parent's method. */
2059 G_OBJECT_CLASS (e_cal_backend_m365_parent_class)->constructed (object);
2060
2061 /* Reset the connectable, it steals data from Authentication extension,
2062 where is written incorrect address */
2063 e_backend_set_connectable (E_BACKEND (object), NULL);
2064
2065 cal_cache = e_cal_meta_backend_ref_cache (E_CAL_META_BACKEND (cbm365));
2066 g_return_if_fail (cal_cache != NULL);
2067
2068 cache_dirname = g_path_get_dirname (e_cache_get_filename (E_CACHE (cal_cache)));
2069 g_signal_connect (cal_cache, "dup-component-revision", G_CALLBACK (ecb_m365_dup_component_revision), NULL);
2070
2071 g_clear_object (&cal_cache);
2072
2073 cbm365->priv->attachments_dir = g_build_filename (cache_dirname, "attachments", NULL);
2074 g_mkdir_with_parents (cbm365->priv->attachments_dir, 0777);
2075
2076 g_free (cache_dirname);
2077
2078 e_m365_tz_utils_ref_windows_zones ();
2079
2080 /* ensure the user email is set */
2081 registry = e_cal_backend_get_registry (E_CAL_BACKEND (cbm365));
2082 m365_settings = camel_m365_settings_get_from_backend (E_BACKEND (cbm365), registry);
2083 user_email = camel_m365_settings_dup_email (m365_settings);
2084
2085 if (!user_email || !*user_email) {
2086 GHashTable *addresses;
2087
2088 g_clear_pointer (&user_email, g_free);
2089
2090 addresses = e_ews_common_utils_dup_mail_addresses (registry,
2091 e_backend_get_source (E_BACKEND (cbm365)), &user_email);
2092 g_clear_pointer (&addresses, g_hash_table_unref);
2093
2094 if (user_email && *user_email)
2095 camel_m365_settings_set_email (m365_settings, user_email);
2096 }
2097
2098 g_free (user_email);
2099 }
2100
2101 static void
2102 ecb_m365_dispose (GObject *object)
2103 {
2104 ECalBackendM365 *cbm365 = E_CAL_BACKEND_M365 (object);
2105
2106 ecb_m365_unset_connection_sync (cbm365, FALSE, NULL, NULL);
2107
2108 /* Chain up to parent's method. */
2109 G_OBJECT_CLASS (e_cal_backend_m365_parent_class)->dispose (object);
2110 }
2111
2112 static void
2113 ecb_m365_finalize (GObject *object)
2114 {
2115 ECalBackendM365 *cbm365 = E_CAL_BACKEND_M365 (object);
2116
2117 g_free (cbm365->priv->attachments_dir);
2118
2119 g_rec_mutex_clear (&cbm365->priv->property_lock);
2120
2121 e_m365_tz_utils_unref_windows_zones ();
2122
2123 /* Chain up to parent's method. */
2124 G_OBJECT_CLASS (e_cal_backend_m365_parent_class)->finalize (object);
2125 }
2126
2127 static void
2128 e_cal_backend_m365_init (ECalBackendM365 *cbm365)
2129 {
2130 cbm365->priv = e_cal_backend_m365_get_instance_private (cbm365);
2131
2132 g_rec_mutex_init (&cbm365->priv->property_lock);
2133 }
2134
2135 static void
2136 e_cal_backend_m365_class_init (ECalBackendM365Class *klass)
2137 {
2138 GObjectClass *object_class;
2139 EBackendClass *backend_class;
2140 ECalBackendClass *cal_backend_class;
2141 ECalBackendSyncClass *cal_backend_sync_class;
2142 ECalMetaBackendClass *cal_meta_backend_class;
2143
2144 cal_meta_backend_class = E_CAL_META_BACKEND_CLASS (klass);
2145 cal_meta_backend_class->connect_sync = ecb_m365_connect_sync;
2146 cal_meta_backend_class->disconnect_sync = ecb_m365_disconnect_sync;
2147 cal_meta_backend_class->get_changes_sync = ecb_m365_get_changes_sync;
2148 cal_meta_backend_class->load_component_sync = ecb_m365_load_component_sync;
2149 cal_meta_backend_class->save_component_sync = ecb_m365_save_component_sync;
2150 cal_meta_backend_class->remove_component_sync = ecb_m365_remove_component_sync;
2151
2152 cal_backend_sync_class = E_CAL_BACKEND_SYNC_CLASS (klass);
2153 cal_backend_sync_class->discard_alarm_sync = ecb_m365_discard_alarm_sync;
2154 cal_backend_sync_class->receive_objects_sync = ecb_m365_receive_objects_sync;
2155 cal_backend_sync_class->send_objects_sync = ecb_m365_send_objects_sync;
2156 cal_backend_sync_class->get_free_busy_sync = ecb_m365_get_free_busy_sync;
2157
2158 cal_backend_class = E_CAL_BACKEND_CLASS (klass);
2159 cal_backend_class->impl_get_backend_property = ecb_m365_get_backend_property;
2160
2161 backend_class = E_BACKEND_CLASS (klass);
2162 backend_class->get_destination_address = ecb_m365_get_destination_address;
2163
2164 object_class = G_OBJECT_CLASS (klass);
2165 object_class->constructed = ecb_m365_constructed;
2166 object_class->dispose = ecb_m365_dispose;
2167 object_class->finalize = ecb_m365_finalize;
2168 }