Debian Codegraph @

src/EWS/common/e-ews-notification.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2013 Red Hat, Inc. (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 "e-ews-connection-utils.h"
11 #include "e-ews-debug.h"
12 #include "e-ews-notification.h"
13
14 struct _EEwsNotificationPrivate {
15 GMutex thread_lock;
16 GWeakRef connection_wk;
17 GCancellable *cancellable;
18 gchar *last_subscription_id; /* guarded by the caller, because it can be set only after construct */
19 };
20
21 G_DEFINE_TYPE_WITH_PRIVATE (EEwsNotification, e_ews_notification, G_TYPE_OBJECT)
22
23 enum {
24 PROP_0,
25 PROP_CONNECTION
26 };
27
28 enum {
29 SUBSCRIPTION_ID_CHANGED,
30 LAST_SIGNAL
31 };
32
33 static guint signals[LAST_SIGNAL];
34
35 static const gchar *default_events_names[] = {
36 "CopiedEvent",
37 "CreatedEvent",
38 "DeletedEvent",
39 "ModifiedEvent",
40 "MovedEvent",
41 "StatusEvent",
42 NULL
43 };
44
45 const gchar * const *
46 e_ews_notification_get_event_names (void)
47 {
48 return (const gchar * const *) default_events_names;
49 }
50
51 typedef struct _EEwsNotificationThreadData EEwsNotificationThreadData;
52
53 struct _EEwsNotificationThreadData {
54 EEwsNotification *notification;
55 GCancellable *cancellable;
56 GSList *folders;
57 };
58
59 static EEwsConnection *
60 e_ews_notification_ref_connection (const EEwsNotification *notification)
61 {
62 g_return_val_if_fail (E_IS_EWS_NOTIFICATION (notification), NULL);
63 g_return_val_if_fail (notification->priv != NULL, NULL);
64
65 return g_weak_ref_get (&notification->priv->connection_wk);
66 }
67
68 EEwsNotification *
69 e_ews_notification_new (EEwsConnection *connection,
70 gchar *last_subscription_id)
71 {
72 EEwsNotification *notif;
73
74 g_return_val_if_fail (E_IS_EWS_CONNECTION (connection), NULL);
75
76 notif = g_object_new (
77 E_TYPE_EWS_NOTIFICATION,
78 "connection", connection, NULL);
79
80 notif->priv->last_subscription_id = last_subscription_id;
81
82 return notif;
83 }
84
85 static void
86 e_ews_notification_set_connection (EEwsNotification *notification,
87 EEwsConnection *connection)
88 {
89 g_return_if_fail (E_IS_EWS_NOTIFICATION (notification));
90 g_return_if_fail (E_IS_EWS_CONNECTION (connection));
91
92 g_weak_ref_set (&notification->priv->connection_wk, connection);
93 }
94
95 static void
96 ews_notification_set_property (GObject *object,
97 guint property_id,
98 const GValue *value,
99 GParamSpec *pspec)
100 {
101 switch (property_id) {
102 case PROP_CONNECTION:
103 e_ews_notification_set_connection (
104 E_EWS_NOTIFICATION (object),
105 g_value_get_object (value));
106 return;
107 }
108
109 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
110 }
111
112 static void
113 ews_notification_get_property (GObject *object,
114 guint property_id,
115 GValue *value,
116 GParamSpec *pspec)
117 {
118 switch (property_id) {
119 case PROP_CONNECTION:
120 g_value_take_object (
121 value,
122 e_ews_notification_ref_connection (
123 E_EWS_NOTIFICATION (object)));
124 return;
125 }
126
127 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
128 }
129
130 static void
131 ews_notification_dispose (GObject *object)
132 {
133 EEwsNotification *notif = E_EWS_NOTIFICATION (object);
134
135 if (notif->priv->cancellable != NULL)
136 g_cancellable_cancel (notif->priv->cancellable);
137
138 g_clear_object (&notif->priv->cancellable);
139 g_weak_ref_set (&notif->priv->connection_wk, NULL);
140
141 /* Chain up to parent's dispose() method. */
142 G_OBJECT_CLASS (e_ews_notification_parent_class)->dispose (object);
143 }
144
145 static void
146 ews_notification_finalize (GObject *object)
147 {
148 EEwsNotification *notif;
149
150 notif = E_EWS_NOTIFICATION (object);
151
152 g_weak_ref_clear (&notif->priv->connection_wk);
153 g_free (notif->priv->last_subscription_id);
154 g_mutex_clear (&notif->priv->thread_lock);
155
156 /* Chain up to parent's method. */
157 G_OBJECT_CLASS (e_ews_notification_parent_class)->finalize (object);
158 }
159
160 static void
161 e_ews_notification_class_init (EEwsNotificationClass *class)
162 {
163 GObjectClass *object_class;
164
165 object_class = G_OBJECT_CLASS (class);
166 object_class->set_property = ews_notification_set_property;
167 object_class->get_property = ews_notification_get_property;
168 object_class->dispose = ews_notification_dispose;
169 object_class->finalize = ews_notification_finalize;
170
171 g_object_class_install_property (
172 object_class,
173 PROP_CONNECTION,
174 g_param_spec_object (
175 "connection",
176 "Connection",
177 "Connection",
178 E_TYPE_EWS_CONNECTION,
179 G_PARAM_READWRITE |
180 G_PARAM_CONSTRUCT_ONLY |
181 G_PARAM_STATIC_STRINGS));
182
183 signals[SUBSCRIPTION_ID_CHANGED] = g_signal_new (
184 "subscription-id-changed",
185 G_OBJECT_CLASS_TYPE (object_class),
186 G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
187 0, NULL, NULL,
188 g_cclosure_marshal_VOID__STRING,
189 G_TYPE_NONE, 1,
190 G_TYPE_STRING);
191 }
192
193 static void
194 e_ews_notification_init (EEwsNotification *notification)
195 {
196 notification->priv = e_ews_notification_get_instance_private (notification);
197
198 g_weak_ref_init (&notification->priv->connection_wk, NULL);
199 g_mutex_init (&notification->priv->thread_lock);
200 }
201
202 static gboolean
203 e_ews_notification_subscribe_folder_sync (EEwsNotification *notification,
204 GSList *folder_ids, /* gchar * */
205 gchar **out_subscription_id,
206 GCancellable *cancellable)
207 {
208 EEwsConnection *cnc;
209 gboolean success;
210
211 g_return_val_if_fail (notification != NULL, FALSE);
212 g_return_val_if_fail (notification->priv != NULL, FALSE);
213
214 if (g_cancellable_is_cancelled (cancellable))
215 return FALSE;
216
217 cnc = e_ews_notification_ref_connection (notification);
218
219 /* Can happen during process shutdown */
220 if (!cnc)
221 return FALSE;
222
223 success = e_ews_connection_subscribe_sync (cnc, G_PRIORITY_DEFAULT, folder_ids, out_subscription_id, cancellable, NULL);
224
225 g_signal_emit (notification, signals[SUBSCRIPTION_ID_CHANGED], 0, success ? *out_subscription_id : NULL, NULL);
226
227 g_object_unref (cnc);
228
229 return success;
230 }
231
232 static gboolean
233 e_ews_notification_unsubscribe_folder_sync (EEwsNotification *notification,
234 const gchar *subscription_id,
235 GCancellable *cancellable)
236 {
237 EEwsConnection *cnc;
238 gboolean success;
239
240 g_return_val_if_fail (notification != NULL, FALSE);
241 g_return_val_if_fail (notification->priv != NULL, FALSE);
242
243 cnc = e_ews_notification_ref_connection (notification);
244
245 /* Can happen during process shutdown */
246 if (!cnc)
247 return FALSE;
248
249 success = e_ews_connection_unsubscribe_sync (cnc, G_PRIORITY_DEFAULT, subscription_id, cancellable, NULL);
250
251 g_signal_emit (notification, signals[SUBSCRIPTION_ID_CHANGED], 0, NULL, NULL);
252
253 g_object_unref (cnc);
254
255 return success;
256 }
257
258 static EEwsNotificationEvent *
259 get_folder_event_info (ESoapParameter *param,
260 EEwsNotificationEventType event_type)
261 {
262 ESoapParameter *subparam;
263 EEwsNotificationEvent *event;
264
265 event = e_ews_notification_event_new ();
266
267 event->type = event_type;
268 event->is_item = FALSE;
269
270 subparam = e_soap_parameter_get_first_child_by_name (param, "FolderId");
271 event->folder_id = e_soap_parameter_get_property (subparam, "Id");
272
273 subparam = e_soap_parameter_get_first_child_by_name (param, "OldFolderId");
274 if (subparam != NULL) {
275 event->old_folder_id = e_soap_parameter_get_property (subparam, "Id");
276 }
277
278 return event;
279 }
280
281 static EEwsNotificationEvent *
282 get_item_event_info (ESoapParameter *param,
283 EEwsNotificationEventType event_type)
284 {
285 ESoapParameter *subparam;
286 EEwsNotificationEvent *event;
287
288 event = e_ews_notification_event_new ();
289
290 event->type = event_type;
291 event->is_item = TRUE;
292
293 subparam = e_soap_parameter_get_first_child_by_name (param, "ParentFolderId");
294 event->folder_id = e_soap_parameter_get_property (subparam, "Id");
295
296 subparam = e_soap_parameter_get_first_child_by_name (param, "OldParentFolderId");
297 if (subparam != NULL) {
298 event->old_folder_id = e_soap_parameter_get_property (subparam, "Id");
299 }
300
301 return event;
302 }
303
304 static EEwsNotificationEvent *
305 get_event_info (ESoapParameter *param,
306 EEwsNotificationEventType event_type)
307 {
308 ESoapParameter *subparam;
309
310 subparam = e_soap_parameter_get_first_child_by_name (param, "ItemId");
311 return (subparam != NULL) ? get_item_event_info (param, event_type) : get_folder_event_info (param, event_type);
312 }
313
314 static void
315 ews_notification_handle_events_param (ESoapParameter *node,
316 GSList **events)
317 {
318 ESoapParameter *param;
319 EEwsNotificationEvent *event;
320 guint event_type;
321
322 param = e_soap_parameter_get_first_child_by_name (node, "SubscriptionId");
323
324 for (param = e_soap_parameter_get_next_child (param); param != NULL; param = e_soap_parameter_get_next_child (param)) {
325 for (event_type = 0; default_events_names[event_type] != NULL; event_type++) {
326 if (g_strcmp0 ((const gchar *) param->name, default_events_names[event_type]) == 0)
327 break;
328
329 continue;
330 }
331
332 if (default_events_names[event_type] == NULL)
333 continue;
334
335 if (event_type != E_EWS_NOTIFICATION_EVENT_STATUS) {
336 event = get_event_info (param, event_type);
337 *events = g_slist_prepend (*events, event);
338 }
339 }
340
341 *events = g_slist_reverse (*events);
342 }
343
344 static gboolean
345 ews_notification_fire_events_from_response (EEwsNotification *notification,
346 ESoapResponse *response,
347 gboolean *out_success)
348 {
349 ESoapParameter *param, *subparam;
350 GSList *events = NULL;
351 GError *error = NULL;
352
353 param = e_soap_response_get_first_parameter_by_name (response, "ResponseMessages", &error);
354
355 /* Sanity check */
356 g_warn_if_fail ((param != NULL && error == NULL) || (param == NULL && error != NULL));
357
358 if (error != NULL) {
359 e_ews_debug_print (G_STRLOC ": %s\n", error->message);
360 g_error_free (error);
361 *out_success = FALSE;
362 return FALSE;
363 }
364
365 subparam = e_soap_parameter_get_first_child (param);
366
367 while (subparam != NULL) {
368 const gchar *name = (const gchar *) subparam->name;
369
370 if (!ews_get_response_status (subparam, &error)) {
371 e_ews_debug_print (G_STRLOC ": %s\n", error->message);
372 *out_success =
373 !g_error_matches (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_EXPIREDSUBSCRIPTION) &&
374 !g_error_matches (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_INVALIDPULLSUBSCRIPTIONID) &&
375 !g_error_matches (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_INVALIDSUBSCRIPTION) &&
376 !g_error_matches (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_SUBSCRIPTIONACCESSDENIED) &&
377 !g_error_matches (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_SUBSCRIPTIONDELEGATEACCESSNOTSUPPORTED) &&
378 !g_error_matches (error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_SUBSCRIPTIONNOTFOUND);
379 g_error_free (error);
380 g_slist_free_full (events, (GDestroyNotify) e_ews_notification_event_free);
381 return FALSE;
382 }
383
384 if (E_EWS_CONNECTION_UTILS_CHECK_ELEMENT (name, "GetStreamingEventsResponseMessage")) {
385 ESoapParameter *node, *node2;
386
387 node = e_soap_parameter_get_first_child_by_name (subparam, "Notifications");
388 if (node) {
389 node2 = e_soap_parameter_get_first_child_by_name (node, "Notification");
390 if (node2)
391 ews_notification_handle_events_param (node2, &events);
392 }
393 }
394
395 subparam = e_soap_parameter_get_next_child (subparam);
396 }
397
398 if (events != NULL) {
399 EEwsConnection *cnc;
400
401 cnc = e_ews_notification_ref_connection (notification);
402
403 if (cnc) {
404 g_signal_emit_by_name (cnc, "server-notification", events);
405 g_object_unref (cnc);
406 }
407
408 g_slist_free_full (events, (GDestroyNotify) e_ews_notification_event_free);
409 }
410
411 return TRUE;
412 }
413
414 static gboolean
415 ews_notification_process_chunk (EEwsNotification *notification,
416 GByteArray *chunk_data,
417 GCancellable *cancellable)
418 {
419 const gchar *chunk_str;
420 gsize chunk_len;
421 gboolean success = TRUE;
422
423 /*
424 * Here we receive, in chunks, "well-formed" messages that contain:
425 * <Envelope>...</Envelope><Envelope>...</Envelope><Envelope>....
426 *
427 * We need to treat chunks which don't end with </Envelope> (it can be
428 * also in the middle of the chunk, or even cut into two pieces by chunk
429 * division -- one part already read, the other just arriving)
430 *
431 * We are parsing those chunks in the following way:
432 * 1. Search for </Envelope> in chunk_data
433 * 2.1 </Envelope> is not found: Do nothing. Waiting for the next chunk
434 * 2.2 </Envelope> is found: Get the pair <Envelope>...</Envelope> and handle it
435 * 3. Update the chunk_data, removing the pair used in 2.2
436 * 4. Repeat from 1, until the 2.1 happens
437 */
438
439 chunk_str = (const gchar *) chunk_data->data;
440 chunk_len = chunk_data->len;
441
442 do {
443 ESoapResponse *response;
444 const gchar *end;
445 gsize len;
446
447 end = g_strstr_len (chunk_str, chunk_len, "</Envelope>");
448
449 if (end == NULL)
450 break;
451
452 len = end + strlen ("</Envelope>") - chunk_str;
453
454 response = e_soap_response_new_from_string (chunk_str, len);
455 if (response == NULL)
456 break;
457
458 if (!ews_notification_fire_events_from_response (notification, response, &success)) {
459 g_object_unref (response);
460 break;
461 }
462 g_object_unref (response);
463
464 g_byte_array_remove_range (chunk_data, 0, len);
465
466 chunk_str = (gchar *) chunk_data->data;
467 chunk_len = chunk_data->len;
468 } while (chunk_len > 0 && !g_cancellable_is_cancelled (cancellable));
469
470 return success;
471 }
472
473 static gboolean
474 e_ews_notification_get_events_sync (EEwsNotification *notification,
475 const gchar *subscription_id,
476 gboolean *out_fatal_error,
477 GCancellable *cancellable)
478 {
479 EEwsConnection *cnc;
480 ESoupSession *session = NULL;
481 SoupMessage *message = NULL;
482 GInputStream *input_stream = NULL;
483 GError *local_error = NULL;
484 gboolean success;
485
486 g_return_val_if_fail (out_fatal_error != NULL, FALSE);
487
488 *out_fatal_error = TRUE;
489
490 g_return_val_if_fail (notification != NULL, FALSE);
491 g_return_val_if_fail (notification->priv != NULL, FALSE);
492
493 if (g_cancellable_is_cancelled (cancellable))
494 return FALSE;
495
496 cnc = e_ews_notification_ref_connection (notification);
497
498 if (!cnc)
499 return FALSE;
500
501 input_stream = e_ews_connection_prepare_streaming_events_sync (cnc, G_PRIORITY_DEFAULT, subscription_id, &session, &message, cancellable, &local_error);
502
503 if (input_stream) {
504 GByteArray *chunk_data;
505 gpointer buffer;
506 gssize nread;
507 gboolean subscription_failed = FALSE;
508
509 buffer = g_malloc (EWS_BUFFER_SIZE);
510 chunk_data = g_byte_array_new ();
511
512 e_ews_debug_print ("%s: %p: started reading events\n", G_STRFUNC, notification);
513
514 /* Unref early, thus it can be freed and will cancel this thread. */
515 g_clear_object (&cnc);
516
517 while (nread = g_input_stream_read (input_stream, buffer, EWS_BUFFER_SIZE, cancellable, &local_error),
518 nread > 0 && !subscription_failed) {
519 g_byte_array_append (chunk_data, buffer, nread);
520 subscription_failed = !ews_notification_process_chunk (notification, chunk_data, cancellable);
521 }
522
523 e_ews_debug_print ("%s: %p: finished reading events; cancelled:%d err:%s is-partial-input:%d subscription-failed:%d\n", G_STRFUNC, notification,
524 g_cancellable_is_cancelled (cancellable),
525 local_error ? local_error->message : "no-err",
526 g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT),
527 subscription_failed);
528
529 /* It's okay when read failed on EOF */
530 *out_fatal_error = (local_error != NULL && !g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT)) ||
531 g_cancellable_is_cancelled (cancellable);
532 success = !local_error && !*out_fatal_error && !subscription_failed;
533
534 g_byte_array_unref (chunk_data);
535 g_free (buffer);
536 } else {
537 e_ews_debug_print ("%s: %p: failed to start reading events: %s\n", G_STRFUNC, notification, local_error ? local_error->message : "no-err");
538
539 *out_fatal_error = !local_error || local_error->domain == E_SOUP_SESSION_ERROR || local_error->domain == G_TLS_ERROR;
540 success = FALSE;
541 }
542
543 g_clear_object (&input_stream);
544 g_clear_object (&message);
545 g_clear_object (&session);
546 g_clear_object (&cnc);
547 g_clear_error (&local_error);
548
549 return success;
550 }
551
552 static gpointer
553 e_ews_notification_get_events_thread (gpointer user_data)
554 {
555 EEwsNotificationThreadData *td = user_data;
556 gchar *subscription_id = NULL;
557 gboolean ret, fatal_error = FALSE;
558
559 g_return_val_if_fail (td != NULL, NULL);
560 g_return_val_if_fail (td->notification != NULL, NULL);
561 g_return_val_if_fail (td->folders != NULL, NULL);
562
563 g_mutex_lock (&td->notification->priv->thread_lock);
564
565 if (td->notification->priv->last_subscription_id) {
566 e_ews_notification_unsubscribe_folder_sync (td->notification, td->notification->priv->last_subscription_id, td->cancellable);
567 g_clear_pointer (&td->notification->priv->last_subscription_id, g_free);
568 }
569
570 if (!e_ews_notification_subscribe_folder_sync (td->notification, td->folders, &subscription_id, td->cancellable))
571 goto exit;
572
573 do {
574 if (g_cancellable_is_cancelled (td->cancellable))
575 goto exit;
576
577 ret = e_ews_notification_get_events_sync (td->notification, subscription_id, &fatal_error, td->cancellable);
578
579 if (!ret && !g_cancellable_is_cancelled (td->cancellable)) {
580 e_ews_debug_print ("%s: Failed to get notification events (SubscriptionId: '%s')\n", G_STRFUNC, subscription_id);
581
582 e_ews_notification_unsubscribe_folder_sync (td->notification, subscription_id, td->cancellable);
583 g_free (subscription_id);
584 subscription_id = NULL;
585
586 if (!fatal_error) {
587 ret = e_ews_notification_subscribe_folder_sync (td->notification, td->folders, &subscription_id, td->cancellable);
588 if (ret) {
589 e_ews_debug_print ("%s: Re-subscribed to get notifications events (SubscriptionId: '%s')\n", G_STRFUNC, subscription_id);
590 } else {
591 e_ews_debug_print ("%s: Failed to re-subscribed to get notifications events\n", G_STRFUNC);
592 }
593 }
594 }
595 } while (ret);
596
597 exit:
598 if (subscription_id != NULL) {
599 e_ews_notification_unsubscribe_folder_sync (td->notification, subscription_id, td->cancellable);
600 g_free (subscription_id);
601 }
602
603 g_mutex_unlock (&td->notification->priv->thread_lock);
604 g_slist_free_full (td->folders, g_free);
605 g_object_unref (td->cancellable);
606 g_object_unref (td->notification);
607 g_slice_free (EEwsNotificationThreadData, td);
608
609 return NULL;
610 }
611
612 void
613 e_ews_notification_start_listening_sync (EEwsNotification *notification,
614 GSList *folders)
615 {
616 EEwsNotificationThreadData *td;
617 GSList *l;
618 GThread *thread;
619
620 g_return_if_fail (notification != NULL);
621 g_return_if_fail (notification->priv != NULL);
622 g_return_if_fail (folders != NULL);
623
624 if (notification->priv->cancellable != NULL)
625 e_ews_notification_stop_listening_sync (notification);
626
627 notification->priv->cancellable = g_cancellable_new ();
628
629 td = g_slice_new0 (EEwsNotificationThreadData);
630 td->notification = g_object_ref (notification);
631 td->cancellable = g_object_ref (notification->priv->cancellable);
632 for (l = folders; l != NULL; l = l->next)
633 td->folders = g_slist_prepend(td->folders, g_strdup (l->data));
634
635 thread = g_thread_new (NULL, e_ews_notification_get_events_thread, td);
636 g_thread_unref (thread);
637 }
638
639 void
640 e_ews_notification_stop_listening_sync (EEwsNotification *notification)
641 {
642 g_return_if_fail (notification != NULL);
643 g_return_if_fail (notification->priv != NULL);
644
645 g_cancellable_cancel (notification->priv->cancellable);
646 g_clear_object (&notification->priv->cancellable);
647 }