| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2022 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 <libedataserver/libedataserver.h> |
| 11 |
|
| 12 |
#include "common/camel-m365-settings.h" |
| 13 |
#include "common/e-m365-connection.h" |
| 14 |
#include "common/e-m365-json-utils.h" |
| 15 |
#include "common/e-m365-tz-utils.h" |
| 16 |
#include "calendar/e-cal-backend-m365-utils.h" |
| 17 |
|
| 18 |
#include "e-cal-backend-ews-m365.h" |
| 19 |
|
| 20 |
static gboolean |
| 21 |
ecb_ews_m365_authenticate (EM365Connection *m365_cnc, |
| 22 |
GCancellable *cancellable, |
| 23 |
GError **error) |
| 24 |
{ |
| 25 |
ESourceAuthenticationResult auth_result; |
| 26 |
ESourceCredentialsReason cred_reason; |
| 27 |
gchar *certificate_pem = NULL; |
| 28 |
GTlsCertificateFlags certificate_errors = 0; |
| 29 |
ESource *source; |
| 30 |
GError *local_error = NULL; |
| 31 |
|
| 32 |
auth_result = e_m365_connection_authenticate_sync (m365_cnc, NULL, E_M365_FOLDER_KIND_CALENDAR, |
| 33 |
NULL, NULL, &certificate_pem, &certificate_errors, cancellable, &local_error); |
| 34 |
|
| 35 |
if (auth_result == E_SOURCE_AUTHENTICATION_ACCEPTED) |
| 36 |
return TRUE; |
| 37 |
|
| 38 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_REFUSED, |
| 39 |
_("Cannot connect to the server, repeat the action once you login to the server.")); |
| 40 |
|
| 41 |
source = e_m365_connection_get_source (m365_cnc); |
| 42 |
|
| 43 |
switch (auth_result) { |
| 44 |
case E_SOURCE_AUTHENTICATION_ERROR: |
| 45 |
cred_reason = E_SOURCE_CREDENTIALS_REASON_ERROR; |
| 46 |
break; |
| 47 |
case E_SOURCE_AUTHENTICATION_ERROR_SSL_FAILED: |
| 48 |
cred_reason = E_SOURCE_CREDENTIALS_REASON_SSL_FAILED; |
| 49 |
break; |
| 50 |
case E_SOURCE_AUTHENTICATION_REJECTED: |
| 51 |
cred_reason = E_SOURCE_CREDENTIALS_REASON_REJECTED; |
| 52 |
break; |
| 53 |
default: |
| 54 |
cred_reason = E_SOURCE_CREDENTIALS_REASON_REQUIRED; |
| 55 |
break; |
| 56 |
} |
| 57 |
|
| 58 |
e_source_invoke_credentials_required (source, cred_reason, certificate_pem, certificate_errors, local_error, NULL, NULL, NULL); |
| 59 |
|
| 60 |
g_clear_error (&local_error); |
| 61 |
|
| 62 |
return FALSE; |
| 63 |
} |
| 64 |
|
| 65 |
static ESource * |
| 66 |
ecb_ews_m365_find_helper_source (ESourceRegistry *registry, |
| 67 |
ESource *calendar_source) |
| 68 |
{ |
| 69 |
ESource *result = NULL; |
| 70 |
ESource *collection_source; |
| 71 |
GList *sources, *link; |
| 72 |
const gchar *parent_uid; |
| 73 |
|
| 74 |
collection_source = e_source_registry_find_extension (registry, calendar_source, E_SOURCE_EXTENSION_COLLECTION); |
| 75 |
if (!collection_source) |
| 76 |
return NULL; |
| 77 |
|
| 78 |
parent_uid = e_source_get_uid (collection_source); |
| 79 |
sources = e_source_registry_list_sources (registry, E_SOURCE_EXTENSION_AUTHENTICATION); |
| 80 |
|
| 81 |
for (link = sources; link; link = g_list_next (link)) { |
| 82 |
ESource *adept = link->data; |
| 83 |
|
| 84 |
if (g_strcmp0 (parent_uid, e_source_get_parent (adept)) == 0) { |
| 85 |
ESourceAuthentication *auth_extension; |
| 86 |
|
| 87 |
auth_extension = e_source_get_extension (adept, E_SOURCE_EXTENSION_AUTHENTICATION); |
| 88 |
|
| 89 |
if (g_strcmp0 ("Microsoft365", e_source_authentication_get_method (auth_extension)) == 0) { |
| 90 |
result = g_object_ref (adept); |
| 91 |
break; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
g_list_free_full (sources, g_object_unref); |
| 97 |
|
| 98 |
return result; |
| 99 |
} |
| 100 |
|
| 101 |
gboolean |
| 102 |
ecb_ews_save_as_online_meeting_sync (ESourceRegistry *registry, |
| 103 |
EEwsConnection *ews_cnc, |
| 104 |
ETimezoneCache *timezone_cache, |
| 105 |
ECalComponent *comp, |
| 106 |
gchar **out_new_uid, |
| 107 |
GCancellable *cancellable, |
| 108 |
GError **error) |
| 109 |
{ |
| 110 |
CamelEwsSettings *ews_settings; |
| 111 |
CamelM365Settings *m365_settings; |
| 112 |
EM365Connection *m365_cnc; |
| 113 |
ESource *source; |
| 114 |
gboolean success = FALSE; |
| 115 |
|
| 116 |
ews_settings = e_ews_connection_ref_settings (ews_cnc); |
| 117 |
m365_settings = g_object_new (CAMEL_TYPE_M365_SETTINGS, NULL); |
| 118 |
|
| 119 |
camel_m365_settings_set_concurrent_connections (m365_settings, 1); |
| 120 |
|
| 121 |
e_binding_bind_property ( |
| 122 |
ews_settings, "user", |
| 123 |
m365_settings, "user", |
| 124 |
G_BINDING_SYNC_CREATE); |
| 125 |
|
| 126 |
e_binding_bind_property ( |
| 127 |
ews_settings, "timeout", |
| 128 |
m365_settings, "timeout", |
| 129 |
G_BINDING_SYNC_CREATE); |
| 130 |
|
| 131 |
e_binding_bind_property ( |
| 132 |
ews_settings, "use-impersonation", |
| 133 |
m365_settings, "use-impersonation", |
| 134 |
G_BINDING_SYNC_CREATE); |
| 135 |
|
| 136 |
e_binding_bind_property ( |
| 137 |
ews_settings, "impersonate-user", |
| 138 |
m365_settings, "impersonate-user", |
| 139 |
G_BINDING_SYNC_CREATE); |
| 140 |
|
| 141 |
/* Only the above properties are used in the m365_cnc */ |
| 142 |
|
| 143 |
source = ecb_ews_m365_find_helper_source (registry, e_ews_connection_get_source (ews_cnc)); |
| 144 |
|
| 145 |
if (!source) { |
| 146 |
/* Translators: The '%s' is replaced with a UID of the collection source */ |
| 147 |
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND, _("Cannot find Microsoft365 helper source for calendar “%s”"), |
| 148 |
e_source_get_uid (e_ews_connection_get_source (ews_cnc))); |
| 149 |
|
| 150 |
g_clear_object (&ews_settings); |
| 151 |
g_clear_object (&m365_settings); |
| 152 |
|
| 153 |
return FALSE; |
| 154 |
} |
| 155 |
|
| 156 |
m365_cnc = e_m365_connection_new_full (source, m365_settings, FALSE); |
| 157 |
g_clear_object (&source); |
| 158 |
|
| 159 |
e_binding_bind_property ( |
| 160 |
ews_cnc, "proxy-resolver", |
| 161 |
m365_cnc, "proxy-resolver", |
| 162 |
G_BINDING_SYNC_CREATE); |
| 163 |
|
| 164 |
success = ecb_ews_m365_authenticate (m365_cnc, cancellable, error); |
| 165 |
|
| 166 |
if (success) { |
| 167 |
ICalComponent *new_comp; |
| 168 |
JsonBuilder *builder; |
| 169 |
|
| 170 |
new_comp = e_cal_component_get_icalcomponent (comp); |
| 171 |
|
| 172 |
e_m365_tz_utils_ref_windows_zones (); |
| 173 |
|
| 174 |
builder = e_cal_backend_m365_utils_ical_to_json (m365_cnc, NULL, NULL, timezone_cache, I_CAL_VEVENT_COMPONENT, |
| 175 |
new_comp, NULL, cancellable, error); |
| 176 |
|
| 177 |
if (builder) { |
| 178 |
JsonObject *created_item = NULL; |
| 179 |
|
| 180 |
success = e_m365_connection_create_event_sync (m365_cnc, NULL, NULL, NULL, builder, &created_item, cancellable, error); |
| 181 |
|
| 182 |
if (success && created_item) { |
| 183 |
const gchar *m365_id = e_m365_event_get_id (created_item); |
| 184 |
|
| 185 |
success = e_cal_backend_m365_utils_ical_to_json_2nd_go (m365_cnc, NULL, NULL, timezone_cache, |
| 186 |
I_CAL_VEVENT_COMPONENT, new_comp, NULL, m365_id, cancellable, error); |
| 187 |
} |
| 188 |
|
| 189 |
if (success && created_item) { |
| 190 |
*out_new_uid = g_strdup (e_m365_event_get_id (created_item)); |
| 191 |
|
| 192 |
/* Convert from Microsoft Graph ID into EWS ID */ |
| 193 |
if (*out_new_uid && strchr (*out_new_uid, '-') != NULL) { |
| 194 |
gchar *ptr; |
| 195 |
|
| 196 |
for (ptr = *out_new_uid; *ptr; ptr++) { |
| 197 |
if (*ptr == '-') |
| 198 |
*ptr = '/'; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
g_clear_pointer (&created_item, json_object_unref); |
| 204 |
g_clear_object (&builder); |
| 205 |
} |
| 206 |
|
| 207 |
e_m365_tz_utils_unref_windows_zones (); |
| 208 |
} |
| 209 |
|
| 210 |
g_clear_object (&ews_settings); |
| 211 |
g_clear_object (&m365_settings); |
| 212 |
g_clear_object (&m365_cnc); |
| 213 |
|
| 214 |
return success; |
| 215 |
} |