Debian Codegraph @

src/common/e-ews-common-utils.c

raw
1 /*
2 * SPDX-FileCopyrightText: (C) 2024 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 <glib/gi18n-lib.h>
9
10 #include <camel/camel.h>
11 #include <libedataserver/libedataserver.h>
12 #include <libedata-cal/libedata-cal.h>
13
14 #include "e-ews-common-utils.h"
15
16 /* This is copy of e_mail_folder_uri_parse(), to not depend on the evolution code
17 in the library code (and to not bring gtk+ into random processes). */
18 gboolean
19 e_ews_common_utils_mail_folder_uri_parse (CamelSession *session,
20 const gchar *folder_uri,
21 CamelStore **out_store,
22 gchar **out_folder_name,
23 GError **error)
24 {
25 CamelURL *url;
26 CamelService *service = NULL;
27 gchar *folder_name = NULL;
28 gboolean success = FALSE;
29
30 g_return_val_if_fail (CAMEL_IS_SESSION (session), FALSE);
31 g_return_val_if_fail (folder_uri != NULL, FALSE);
32
33 url = camel_url_new (folder_uri, error);
34 if (url == NULL)
35 return FALSE;
36
37 /* Current URI Format: 'folder://' STORE_UID '/' FOLDER_PATH */
38 if (g_strcmp0 (url->protocol, "folder") == 0) {
39
40 if (url->host != NULL) {
41 gchar *uid;
42
43 if (url->user == NULL || *url->user == '\0')
44 uid = g_strdup (url->host);
45 else
46 uid = g_strconcat (
47 url->user, "@", url->host, NULL);
48
49 service = camel_session_ref_service (session, uid);
50 g_free (uid);
51 }
52
53 if (url->path != NULL && *url->path == '/')
54 folder_name = camel_url_decode_path (url->path + 1);
55
56 /* This style was used to reference accounts by UID before
57 * CamelServices themselves had UIDs. Some examples are:
58 *
59 * Special cases:
60 *
61 * 'email://local@local/' FOLDER_PATH
62 * 'email://vfolder@local/' FOLDER_PATH
63 *
64 * General case:
65 *
66 * 'email://' ACCOUNT_UID '/' FOLDER_PATH
67 *
68 * Note: ACCOUNT_UID is now equivalent to STORE_UID, and
69 * the STORE_UIDs for the special cases are 'local'
70 * and 'vfolder'.
71 */
72 } else if (g_strcmp0 (url->protocol, "email") == 0) {
73 gchar *uid = NULL;
74
75 /* Handle the special cases. */
76 if (g_strcmp0 (url->host, "local") == 0) {
77 if (g_strcmp0 (url->user, "local") == 0)
78 uid = g_strdup ("local");
79 if (g_strcmp0 (url->user, "vfolder") == 0)
80 uid = g_strdup ("vfolder");
81 }
82
83 /* Handle the general case. */
84 if (uid == NULL && url->host != NULL) {
85 if (url->user == NULL)
86 uid = g_strdup (url->host);
87 else
88 uid = g_strdup_printf (
89 "%s@%s", url->user, url->host);
90 }
91
92 if (uid != NULL) {
93 service = camel_session_ref_service (session, uid);
94 g_free (uid);
95 }
96
97 if (url->path != NULL && *url->path == '/')
98 folder_name = camel_url_decode_path (url->path + 1);
99
100 /* CamelFolderInfo URIs used to embed the store's URI, so the
101 * folder name is appended as either a path part or a fragment
102 * part, depending whether the store's URI used the path part.
103 * To determine which it is, you have to check the provider
104 * flags for CAMEL_URL_FRAGMENT_IS_PATH. */
105 } else {
106 gboolean local_mbox_folder;
107
108 /* In Evolution 2.x, the local mail store used mbox
109 * format. camel_session_ref_service_by_url() won't
110 * match "mbox:///.../mail/local" folder URIs, since
111 * the local mail store is now Maildir format. Test
112 * for this corner case and work around it.
113 *
114 * The folder path is kept in the fragment part of the
115 * URL which makes it easy to test the filesystem path.
116 * The suffix "evolution/mail/local" should match both
117 * the current XDG-compliant location and the old "dot
118 * folder" location (~/.evolution/mail/local). */
119 local_mbox_folder =
120 (g_strcmp0 (url->protocol, "mbox") == 0) &&
121 (url->path != NULL) &&
122 g_str_has_suffix (url->path, "evolution/mail/local");
123
124 if (local_mbox_folder) {
125 service = camel_session_ref_service (session, "local");
126 } else {
127 service = camel_session_ref_service_by_url (
128 session, url, CAMEL_PROVIDER_STORE);
129 }
130
131 if (CAMEL_IS_STORE (service)) {
132 CamelProvider *provider;
133
134 provider = camel_service_get_provider (service);
135
136 if (provider->url_flags & CAMEL_URL_FRAGMENT_IS_PATH)
137 folder_name = g_strdup (url->fragment);
138 else if (url->path != NULL && *url->path == '/')
139 folder_name = g_strdup (url->path + 1);
140 }
141 }
142
143 if (CAMEL_IS_STORE (service) && folder_name != NULL) {
144 if (out_store != NULL)
145 *out_store = CAMEL_STORE (g_object_ref (service));
146
147 if (out_folder_name != NULL) {
148 *out_folder_name = folder_name;
149 folder_name = NULL;
150 }
151
152 success = TRUE;
153 } else {
154 g_set_error (
155 error, CAMEL_FOLDER_ERROR,
156 CAMEL_FOLDER_ERROR_INVALID,
157 _("Invalid folder URI “%s”"),
158 folder_uri);
159 }
160
161 if (service != NULL)
162 g_object_unref (service);
163
164 g_free (folder_name);
165
166 camel_url_free (url);
167
168 return success;
169 }
170
171 /* This is copy of e_str_replace_string(), to not depend on the evolution code
172 in the library code (and to not bring gtk+ into random processes). */
173 GString *
174 e_ews_common_utils_str_replace_string (const gchar *text,
175 const gchar *before,
176 const gchar *after)
177 {
178 const gchar *p, *next;
179 GString *str;
180 gint find_len;
181
182 g_return_val_if_fail (text != NULL, NULL);
183 g_return_val_if_fail (before != NULL, NULL);
184 g_return_val_if_fail (*before, NULL);
185
186 find_len = strlen (before);
187 str = g_string_new ("");
188
189 p = text;
190 while (next = strstr (p, before), next) {
191 if (p < next)
192 g_string_append_len (str, p, next - p);
193
194 if (after && *after)
195 g_string_append (str, after);
196
197 p = next + find_len;
198 }
199
200 return g_string_append (str, p);
201 }
202
203 /* This corresponds to calendar_config_get_icaltimezone(), to not depend on the evolution code
204 in the library code (and to not bring gtk+ into random processes). */
205 ICalTimezone *
206 e_ews_common_utils_get_configured_icaltimezone (void)
207 {
208 ICalTimezone *zone = NULL;
209 gchar *location = NULL;
210
211 if (e_ews_common_utils_gsettings_schema_exists ("org.gnome.evolution.calendar")) {
212 GSettings *settings;
213
214 settings = g_settings_new ("org.gnome.evolution.calendar");
215
216 if (g_settings_get_boolean (settings, "use-system-timezone"))
217 location = e_cal_util_get_system_timezone_location ();
218 else
219 location = g_settings_get_string (settings, "timezone");
220
221 g_clear_object (&settings);
222 }
223
224 if (!location)
225 location = e_cal_util_get_system_timezone_location ();
226
227 if (location) {
228 zone = i_cal_timezone_get_builtin_timezone (location);
229
230 g_free (location);
231 }
232
233 return zone;
234 }
235
236 static ECalComponentAttendee *
237 get_attendee (GSList *attendees,
238 const gchar *address,
239 GHashTable *aliases)
240 {
241 GSList *l;
242
243 if (!address)
244 return NULL;
245
246 for (l = attendees; l; l = l->next) {
247 ECalComponentAttendee *attendee = l->data;
248 const gchar *nomailto;
249
250 nomailto = e_cal_util_get_attendee_email (attendee);
251
252 if (!nomailto || !*nomailto)
253 continue;
254
255 if ((address && e_cal_util_email_addresses_equal (nomailto, address)) ||
256 (aliases && g_hash_table_contains (aliases, nomailto))) {
257 return attendee;
258 }
259 }
260
261 return NULL;
262 }
263
264 static ECalComponentAttendee *
265 get_attendee_if_attendee_sentby_is_user (GSList *attendees,
266 const gchar *address,
267 GHashTable *aliases)
268 {
269 GSList *l;
270
271 for (l = attendees; l; l = l->next) {
272 ECalComponentAttendee *attendee = l->data;
273 const gchar *nomailto;
274
275 nomailto = e_cal_util_strip_mailto (e_cal_component_attendee_get_sentby (attendee));
276 if (!nomailto || !*nomailto)
277 continue;
278
279 if ((address && e_cal_util_email_addresses_equal (nomailto, address)) ||
280 (aliases && g_hash_table_contains (aliases, nomailto))) {
281 return attendee;
282 }
283 }
284
285 return NULL;
286 }
287
288 /* This is adapted copy of itip_get_comp_attendee(), to avoid dependency
289 on the evolution code in the backend. */
290 static gchar *
291 eecu_get_comp_attendee (ESourceRegistry *registry,
292 ECalComponent *comp,
293 ECalBackend *cal_backend)
294 {
295 ESource *source;
296 GSList *attendees;
297 ECalComponentAttendee *attendee = NULL;
298 GList *list, *link;
299 const gchar *extension_name;
300 gchar *address = NULL;
301
302 attendees = e_cal_component_get_attendees (comp);
303
304 if (cal_backend)
305 address = e_cal_backend_get_backend_property (cal_backend, E_CAL_BACKEND_PROPERTY_CAL_EMAIL_ADDRESS);
306
307 if (address != NULL && *address != '\0') {
308 attendee = get_attendee (attendees, address, NULL);
309
310 if (attendee) {
311 gchar *user_email;
312
313 user_email = g_strdup (e_cal_util_get_attendee_email (attendee));
314 g_slist_free_full (attendees, e_cal_component_attendee_free);
315 g_free (address);
316
317 return user_email;
318 }
319
320 attendee = get_attendee_if_attendee_sentby_is_user (attendees, address, NULL);
321
322 if (attendee != NULL) {
323 gchar *user_email;
324
325 user_email = g_strdup (e_cal_util_strip_mailto (e_cal_component_attendee_get_sentby (attendee)));
326 g_slist_free_full (attendees, e_cal_component_attendee_free);
327 g_free (address);
328
329 return user_email;
330 }
331 }
332
333 g_free (address);
334 address = NULL;
335
336 extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
337 list = e_source_registry_list_enabled (registry, extension_name);
338
339 for (link = list; link != NULL; link = g_list_next (link)) {
340 ESourceMailIdentity *extension;
341 GHashTable *aliases;
342
343 source = E_SOURCE (link->data);
344
345 extension_name = E_SOURCE_EXTENSION_MAIL_IDENTITY;
346 extension = e_source_get_extension (source, extension_name);
347 address = e_source_mail_identity_dup_address (extension);
348 aliases = e_source_mail_identity_get_aliases_as_hash_table (extension);
349
350 attendee = get_attendee (attendees, address, aliases);
351 if (attendee != NULL) {
352 gchar *user_email;
353
354 user_email = g_strdup (e_cal_util_get_attendee_email (attendee));
355 g_slist_free_full (attendees, e_cal_component_attendee_free);
356
357 if (aliases)
358 g_hash_table_destroy (aliases);
359 g_free (address);
360
361 g_list_free_full (list, g_object_unref);
362
363 return user_email;
364 }
365
366 /* If the account was not found in the attendees list, then
367 * let's check the 'sentby' fields of the attendees if we can
368 * find the account. */
369 attendee = get_attendee_if_attendee_sentby_is_user (attendees, address, aliases);
370 if (attendee) {
371 gchar *user_email;
372
373 user_email = g_strdup (e_cal_util_strip_mailto (e_cal_component_attendee_get_sentby (attendee)));
374 g_slist_free_full (attendees, e_cal_component_attendee_free);
375
376 if (aliases)
377 g_hash_table_destroy (aliases);
378 g_free (address);
379
380 g_list_free_full (list, g_object_unref);
381
382 return user_email;
383 }
384
385 if (aliases)
386 g_hash_table_destroy (aliases);
387 g_free (address);
388 }
389
390 g_list_free_full (list, g_object_unref);
391
392 /* We could not find the attendee in the component, so just give
393 * the default account address if the email address is not set in
394 * the backend. */
395 /* FIXME do we have a better way ? */
396 e_cal_util_get_default_name_and_address (registry, NULL, &address);
397
398 g_slist_free_full (attendees, e_cal_component_attendee_free);
399
400 return address;
401 }
402
403 static ICalProperty *
404 find_attendee (ICalComponent *icomp,
405 const gchar *address,
406 GHashTable *aliases)
407 {
408 ICalProperty *prop;
409
410 if (address == NULL)
411 return NULL;
412
413 for (prop = i_cal_component_get_first_property (icomp, I_CAL_ATTENDEE_PROPERTY);
414 prop != NULL;
415 g_object_unref (prop), prop = i_cal_component_get_next_property (icomp, I_CAL_ATTENDEE_PROPERTY)) {
416 gchar *attendee;
417 gchar *text;
418
419 attendee = i_cal_property_get_value_as_string (prop);
420
421 if (!attendee)
422 continue;
423
424 text = g_strdup (e_cal_util_strip_mailto (attendee));
425 text = g_strstrip (text);
426 if (text && ((!g_ascii_strcasecmp (address, text)) ||
427 (aliases && g_hash_table_contains (aliases, text)))) {
428 g_free (text);
429 g_free (attendee);
430 break;
431 }
432 g_free (text);
433 g_free (attendee);
434 }
435
436 return prop;
437 }
438
439 static ICalProperty *
440 find_attendee_if_sentby (ICalComponent *icomp,
441 const gchar *address,
442 GHashTable *aliases)
443 {
444 ICalProperty *prop;
445
446 if (address == NULL)
447 return NULL;
448
449 for (prop = i_cal_component_get_first_property (icomp, I_CAL_ATTENDEE_PROPERTY);
450 prop != NULL;
451 g_object_unref (prop), prop = i_cal_component_get_next_property (icomp, I_CAL_ATTENDEE_PROPERTY)) {
452 ICalParameter *param;
453 const gchar *attendee_sentby;
454 gchar *text;
455
456 param = i_cal_property_get_first_parameter (prop, I_CAL_SENTBY_PARAMETER);
457 if (!param)
458 continue;
459
460 attendee_sentby = i_cal_parameter_get_sentby (param);
461
462 if (!attendee_sentby) {
463 g_object_unref (param);
464 continue;
465 }
466
467 text = g_strdup (e_cal_util_strip_mailto (attendee_sentby));
468 text = g_strstrip (text);
469
470 g_object_unref (param);
471
472 if (text && ((!g_ascii_strcasecmp (address, text)) ||
473 (aliases && g_hash_table_contains (aliases, text)))) {
474 g_free (text);
475 break;
476 }
477 g_free (text);
478 }
479
480 return prop;
481 }
482
483 ICalProperty * /* (transfer full) */
484 e_ews_common_utils_find_attendee (ECalBackend *cal_backend,
485 ICalComponent *icomp,
486 GHashTable *aliases)
487 {
488 ECalComponent *comp;
489 ICalProperty *attendee = NULL;
490
491 g_return_val_if_fail (E_IS_CAL_BACKEND (cal_backend), NULL);
492 g_return_val_if_fail (I_CAL_IS_COMPONENT (icomp), NULL);
493
494 comp = e_cal_component_new_from_icalcomponent (i_cal_component_clone (icomp));
495 if (comp) {
496 ESourceRegistry *registry;
497 gchar *my_address;
498
499 registry = e_cal_backend_get_registry (cal_backend);
500 my_address = eecu_get_comp_attendee (registry, comp, cal_backend);
501
502 attendee = find_attendee (icomp, my_address ? my_address : "", aliases);
503 if (!attendee)
504 attendee = find_attendee_if_sentby (icomp, my_address ? my_address : "", aliases);
505
506 g_free (my_address);
507 g_object_unref (comp);
508 }
509
510 return attendee;
511 }
512
513 gboolean
514 e_ews_common_utils_gsettings_schema_exists (const gchar *schema_id)
515 {
516 GSettingsSchema *schema;
517
518 schema = g_settings_schema_source_lookup (g_settings_schema_source_get_default (), schema_id, TRUE);
519
520 if (!schema)
521 return FALSE;
522
523 g_settings_schema_unref (schema);
524
525 return TRUE;
526 }
527
528 GHashTable *
529 e_ews_common_utils_dup_mail_addresses (ESourceRegistry *registry,
530 ESource *child_source,
531 gchar **inout_user_email)
532 {
533 GHashTable *aliases = NULL;
534 GList *identities, *link;
535 const gchar *parent_uid;
536
537 g_return_val_if_fail (E_IS_SOURCE_REGISTRY (registry), NULL);
538 g_return_val_if_fail (E_IS_SOURCE (child_source), NULL);
539
540 parent_uid = e_source_get_parent (child_source);
541
542 if (!parent_uid || !*parent_uid)
543 return NULL;
544
545 identities = e_source_registry_list_enabled (registry, E_SOURCE_EXTENSION_MAIL_IDENTITY);
546
547 for (link = identities; link; link = g_list_next (link)) {
548 ESource *mail_identity = link->data;
549
550 if (g_strcmp0 (parent_uid, e_source_get_parent (mail_identity)) == 0) {
551 ESourceMailIdentity *extension;
552 gchar *address;
553
554 extension = e_source_get_extension (mail_identity, E_SOURCE_EXTENSION_MAIL_IDENTITY);
555 aliases = e_source_mail_identity_get_aliases_as_hash_table (extension);
556
557 address = e_source_mail_identity_dup_address (extension);
558 if (address && *address) {
559 if (inout_user_email && (!*inout_user_email || !**inout_user_email)) {
560 g_free (*inout_user_email);
561 *inout_user_email = g_strdup (address);
562 }
563
564 if (!aliases)
565 aliases = g_hash_table_new_full (camel_strcase_hash, camel_strcase_equal, g_free, g_free);
566 g_hash_table_insert (aliases, address, e_source_mail_identity_dup_name (extension));
567 } else {
568 g_free (address);
569 }
570 break;
571 }
572 }
573
574 g_list_free_full (identities, g_object_unref);
575
576 return aliases;
577 }