| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 1999-2008 Novell, Inc. (www.novell.com) |
| 3 |
* SPDX-FileContributor: Sivaiah Nallagatla <[email protected]> |
| 4 |
* SPDX-FileContributor: Parthasarathi Susarla <[email protected]> |
| 5 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "evolution-ews-config.h" |
| 9 |
|
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include <glib/gi18n-lib.h> |
| 13 |
|
| 14 |
#include "e-ews-common-utils.h" |
| 15 |
#include "common/camel-ews-settings.h" |
| 16 |
#include "common/e-ews-camel-common.h" |
| 17 |
|
| 18 |
#include "camel-ews-store.h" |
| 19 |
#include "camel-ews-transport.h" |
| 20 |
#include "camel-ews-utils.h" |
| 21 |
|
| 22 |
#define REPLY_VIEW "default message attachments threading" |
| 23 |
|
| 24 |
struct _CamelEwsTransportPrivate |
| 25 |
{ |
| 26 |
GMutex connection_lock; |
| 27 |
EEwsConnection *connection; |
| 28 |
}; |
| 29 |
|
| 30 |
G_DEFINE_TYPE_WITH_PRIVATE (CamelEwsTransport, camel_ews_transport, CAMEL_TYPE_TRANSPORT) |
| 31 |
|
| 32 |
static gboolean |
| 33 |
ews_transport_can_server_side_sent_folder (CamelService *service, |
| 34 |
EwsFolderId **folder_id, |
| 35 |
GCancellable *cancellable) |
| 36 |
{ |
| 37 |
ESourceRegistry *registry; |
| 38 |
ESource *sibling, *source = NULL; |
| 39 |
gboolean is_server_side = FALSE; |
| 40 |
|
| 41 |
g_return_val_if_fail (CAMEL_IS_EWS_TRANSPORT (service), FALSE); |
| 42 |
g_return_val_if_fail (folder_id != NULL, FALSE); |
| 43 |
|
| 44 |
registry = e_source_registry_new_sync (cancellable, NULL); |
| 45 |
if (!registry) |
| 46 |
return FALSE; |
| 47 |
|
| 48 |
sibling = e_source_registry_ref_source (registry, camel_service_get_uid (service)); |
| 49 |
if (sibling) { |
| 50 |
CamelSession *session; |
| 51 |
GList *sources, *siter; |
| 52 |
|
| 53 |
session = camel_service_ref_session (service); |
| 54 |
sources = e_source_registry_list_sources (registry, E_SOURCE_EXTENSION_MAIL_SUBMISSION); |
| 55 |
for (siter = sources; siter; siter = siter->next) { |
| 56 |
source = siter->data; |
| 57 |
|
| 58 |
if (!source || g_strcmp0 (e_source_get_parent (source), e_source_get_parent (sibling)) != 0 || |
| 59 |
!e_source_has_extension (source, E_SOURCE_EXTENSION_MAIL_SUBMISSION) || |
| 60 |
!e_source_has_extension (source, E_SOURCE_EXTENSION_MAIL_COMPOSITION)) |
| 61 |
source = NULL; |
| 62 |
else |
| 63 |
break; |
| 64 |
} |
| 65 |
|
| 66 |
if (source && |
| 67 |
e_source_has_extension (source, E_SOURCE_EXTENSION_MAIL_SUBMISSION) && |
| 68 |
e_source_has_extension (source, E_SOURCE_EXTENSION_MAIL_COMPOSITION)) { |
| 69 |
ESourceMailSubmission *subm_extension; |
| 70 |
CamelStore *store = NULL; |
| 71 |
gchar *folder_name = NULL; |
| 72 |
|
| 73 |
subm_extension = e_source_get_extension (source, E_SOURCE_EXTENSION_MAIL_SUBMISSION); |
| 74 |
|
| 75 |
/* Copy messages on the server side only if the replies |
| 76 |
might not be saved to the original folder, which is handled |
| 77 |
by the evolution itself. */ |
| 78 |
if (!e_source_mail_submission_get_replies_to_origin_folder (subm_extension) && |
| 79 |
e_source_mail_submission_get_sent_folder (subm_extension) && |
| 80 |
e_ews_common_utils_mail_folder_uri_parse (session, |
| 81 |
e_source_mail_submission_get_sent_folder (subm_extension), |
| 82 |
&store, &folder_name, NULL) & CAMEL_IS_EWS_STORE (store)) { |
| 83 |
CamelEwsStore *ews_store = CAMEL_EWS_STORE (store); |
| 84 |
gchar *folder_id_str; |
| 85 |
|
| 86 |
folder_id_str = camel_ews_store_summary_get_folder_id_from_name ( |
| 87 |
ews_store->summary, folder_name); |
| 88 |
if (folder_id_str) { |
| 89 |
gchar *change_key; |
| 90 |
|
| 91 |
change_key = camel_ews_store_summary_get_change_key (ews_store->summary, folder_name, NULL); |
| 92 |
*folder_id = e_ews_folder_id_new (folder_id_str, change_key, FALSE); |
| 93 |
g_free (change_key); |
| 94 |
|
| 95 |
is_server_side = *folder_id != NULL; |
| 96 |
} |
| 97 |
|
| 98 |
g_free (folder_id_str); |
| 99 |
} |
| 100 |
|
| 101 |
g_clear_object (&store); |
| 102 |
g_free (folder_name); |
| 103 |
} |
| 104 |
|
| 105 |
g_list_free_full (sources, g_object_unref); |
| 106 |
g_object_unref (sibling); |
| 107 |
g_clear_object (&session); |
| 108 |
} |
| 109 |
|
| 110 |
g_object_unref (registry); |
| 111 |
|
| 112 |
return is_server_side; |
| 113 |
} |
| 114 |
|
| 115 |
static EEwsConnection * |
| 116 |
ews_transport_ref_connection (CamelEwsTransport *ews_transport) |
| 117 |
{ |
| 118 |
EEwsConnection *connection = NULL; |
| 119 |
|
| 120 |
g_return_val_if_fail (CAMEL_IS_EWS_TRANSPORT (ews_transport), NULL); |
| 121 |
|
| 122 |
g_mutex_lock (&ews_transport->priv->connection_lock); |
| 123 |
|
| 124 |
if (ews_transport->priv->connection) |
| 125 |
connection = g_object_ref (ews_transport->priv->connection); |
| 126 |
|
| 127 |
g_mutex_unlock (&ews_transport->priv->connection_lock); |
| 128 |
|
| 129 |
return connection; |
| 130 |
} |
| 131 |
|
| 132 |
static gboolean |
| 133 |
ews_transport_connect_sync (CamelService *service, |
| 134 |
GCancellable *cancellable, |
| 135 |
GError **error) |
| 136 |
{ |
| 137 |
EEwsConnection *connection; |
| 138 |
CamelSession *session; |
| 139 |
CamelSettings *settings; |
| 140 |
const gchar *auth_mech; |
| 141 |
gboolean success; |
| 142 |
|
| 143 |
/* Chain up to parent's method. */ |
| 144 |
if (!CAMEL_SERVICE_CLASS (camel_ews_transport_parent_class)->connect_sync (service, cancellable, error)) |
| 145 |
return FALSE; |
| 146 |
|
| 147 |
if (camel_service_get_connection_status (service) == CAMEL_SERVICE_DISCONNECTED) |
| 148 |
return FALSE; |
| 149 |
|
| 150 |
connection = ews_transport_ref_connection (CAMEL_EWS_TRANSPORT (service)); |
| 151 |
if (connection) { |
| 152 |
g_object_unref (connection); |
| 153 |
return TRUE; |
| 154 |
} |
| 155 |
|
| 156 |
session = camel_service_ref_session (service); |
| 157 |
settings = camel_service_ref_settings (service); |
| 158 |
|
| 159 |
/* Try running an operation that requires authentication |
| 160 |
* to make sure we have valid credentials available. */ |
| 161 |
auth_mech = camel_ews_settings_get_auth_mechanism_string (CAMEL_EWS_SETTINGS (settings)); |
| 162 |
|
| 163 |
success = camel_session_authenticate_sync (session, service, |
| 164 |
auth_mech ? auth_mech : "NTLM", cancellable, error); |
| 165 |
|
| 166 |
g_object_unref (session); |
| 167 |
g_object_unref (settings); |
| 168 |
|
| 169 |
return success; |
| 170 |
} |
| 171 |
|
| 172 |
static gboolean |
| 173 |
ews_transport_disconnect_sync (CamelService *service, |
| 174 |
gboolean clean, |
| 175 |
GCancellable *cancellable, |
| 176 |
GError **error) |
| 177 |
{ |
| 178 |
CamelEwsTransport *ews_transport = CAMEL_EWS_TRANSPORT (service); |
| 179 |
|
| 180 |
g_mutex_lock (&ews_transport->priv->connection_lock); |
| 181 |
g_clear_object (&ews_transport->priv->connection); |
| 182 |
g_mutex_unlock (&ews_transport->priv->connection_lock); |
| 183 |
|
| 184 |
return CAMEL_SERVICE_CLASS (camel_ews_transport_parent_class)->disconnect_sync (service, clean, cancellable, error); |
| 185 |
} |
| 186 |
|
| 187 |
static CamelAuthenticationResult |
| 188 |
ews_transport_authenticate_sync (CamelService *service, |
| 189 |
const gchar *mechanism, |
| 190 |
GCancellable *cancellable, |
| 191 |
GError **error) |
| 192 |
{ |
| 193 |
CamelAuthenticationResult result; |
| 194 |
CamelEwsTransport *ews_transport; |
| 195 |
CamelSettings *settings; |
| 196 |
CamelEwsSettings *ews_settings; |
| 197 |
EEwsConnection *connection; |
| 198 |
ESource *source; |
| 199 |
const gchar *password; |
| 200 |
gchar *hosturl, *new_sync_state = NULL; |
| 201 |
GSList *folders_created = NULL; |
| 202 |
GSList *folders_updated = NULL; |
| 203 |
GSList *folders_deleted = NULL; |
| 204 |
gboolean includes_last_folder = FALSE; |
| 205 |
GError *local_error = NULL; |
| 206 |
|
| 207 |
ews_transport = CAMEL_EWS_TRANSPORT (service); |
| 208 |
|
| 209 |
password = camel_service_get_password (service); |
| 210 |
|
| 211 |
settings = camel_service_ref_settings (service); |
| 212 |
|
| 213 |
ews_settings = CAMEL_EWS_SETTINGS (settings); |
| 214 |
hosturl = camel_ews_settings_dup_hosturl (ews_settings); |
| 215 |
source = camel_ews_utils_ref_corresponding_source (service, cancellable); |
| 216 |
|
| 217 |
connection = e_ews_connection_new (source, hosturl, ews_settings); |
| 218 |
e_ews_connection_set_password (connection, password); |
| 219 |
|
| 220 |
g_clear_object (&source); |
| 221 |
g_free (hosturl); |
| 222 |
|
| 223 |
g_object_unref (settings); |
| 224 |
|
| 225 |
e_binding_bind_property ( |
| 226 |
service, "proxy-resolver", |
| 227 |
connection, "proxy-resolver", |
| 228 |
G_BINDING_SYNC_CREATE); |
| 229 |
|
| 230 |
/* XXX We need to run some operation that requires authentication |
| 231 |
* but does not change any server-side state, so we can check |
| 232 |
* the error status and determine if our password is valid. |
| 233 |
* David suggested e_ews_connection_sync_folder_hierarchy(), |
| 234 |
* since we have to do that eventually anyway. */ |
| 235 |
|
| 236 |
e_ews_connection_sync_folder_hierarchy_sync (connection, EWS_PRIORITY_MEDIUM, NULL, |
| 237 |
&new_sync_state, &includes_last_folder, &folders_created, &folders_updated, &folders_deleted, |
| 238 |
cancellable, &local_error); |
| 239 |
|
| 240 |
g_slist_free_full (folders_created, g_object_unref); |
| 241 |
g_slist_free_full (folders_updated, g_object_unref); |
| 242 |
g_slist_free_full (folders_deleted, g_free); |
| 243 |
g_free (new_sync_state); |
| 244 |
|
| 245 |
if (g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_UNAVAILABLE)) { |
| 246 |
local_error->domain = CAMEL_SERVICE_ERROR; |
| 247 |
local_error->code = CAMEL_SERVICE_ERROR_UNAVAILABLE; |
| 248 |
} |
| 249 |
|
| 250 |
if (!local_error) { |
| 251 |
g_mutex_lock (&ews_transport->priv->connection_lock); |
| 252 |
g_clear_object (&ews_transport->priv->connection); |
| 253 |
ews_transport->priv->connection = g_object_ref (connection); |
| 254 |
g_mutex_unlock (&ews_transport->priv->connection_lock); |
| 255 |
} else { |
| 256 |
g_mutex_lock (&ews_transport->priv->connection_lock); |
| 257 |
g_clear_object (&ews_transport->priv->connection); |
| 258 |
g_mutex_unlock (&ews_transport->priv->connection_lock); |
| 259 |
} |
| 260 |
|
| 261 |
if (!local_error) { |
| 262 |
result = CAMEL_AUTHENTICATION_ACCEPTED; |
| 263 |
} else if (g_error_matches (local_error, EWS_CONNECTION_ERROR, EWS_CONNECTION_ERROR_AUTHENTICATION_FAILED)) { |
| 264 |
g_clear_error (&local_error); |
| 265 |
result = CAMEL_AUTHENTICATION_REJECTED; |
| 266 |
} else { |
| 267 |
g_propagate_error (error, local_error); |
| 268 |
result = CAMEL_AUTHENTICATION_ERROR; |
| 269 |
} |
| 270 |
|
| 271 |
g_object_unref (connection); |
| 272 |
|
| 273 |
return result; |
| 274 |
} |
| 275 |
|
| 276 |
static gchar * |
| 277 |
ews_transport_get_name (CamelService *service, |
| 278 |
gboolean brief) |
| 279 |
{ |
| 280 |
CamelSettings *settings; |
| 281 |
gchar *host; |
| 282 |
gchar *name; |
| 283 |
|
| 284 |
settings = camel_service_ref_settings (service); |
| 285 |
|
| 286 |
host = camel_ews_utils_get_host_name (settings); |
| 287 |
|
| 288 |
g_object_unref (settings); |
| 289 |
|
| 290 |
if (brief) |
| 291 |
name = g_strdup_printf ( |
| 292 |
_("Exchange server %s"), host); |
| 293 |
else |
| 294 |
name = g_strdup_printf ( |
| 295 |
_("Exchange mail delivery via %s"), host); |
| 296 |
|
| 297 |
g_free (host); |
| 298 |
|
| 299 |
return name; |
| 300 |
} |
| 301 |
|
| 302 |
static gboolean |
| 303 |
ews_send_to_sync (CamelTransport *transport, |
| 304 |
CamelMimeMessage *message, |
| 305 |
CamelAddress *from, |
| 306 |
CamelAddress *recipients, |
| 307 |
gboolean *out_sent_message_saved, |
| 308 |
GCancellable *cancellable, |
| 309 |
GError **error) |
| 310 |
{ |
| 311 |
CamelNetworkSettings *network_settings; |
| 312 |
CamelEwsSettings *ews_settings; |
| 313 |
CamelInternetAddress *used_from; |
| 314 |
CamelSettings *settings; |
| 315 |
CamelService *service; |
| 316 |
EEwsConnection *cnc; |
| 317 |
EwsFolderId *folder_id = NULL; |
| 318 |
gchar *ews_email; |
| 319 |
gchar *host_url; |
| 320 |
gchar *user; |
| 321 |
gboolean success = FALSE; |
| 322 |
|
| 323 |
service = CAMEL_SERVICE (transport); |
| 324 |
|
| 325 |
settings = camel_service_ref_settings (service); |
| 326 |
|
| 327 |
ews_settings = CAMEL_EWS_SETTINGS (settings); |
| 328 |
ews_email = camel_ews_settings_dup_email (ews_settings); |
| 329 |
host_url = camel_ews_settings_dup_hosturl (ews_settings); |
| 330 |
|
| 331 |
network_settings = CAMEL_NETWORK_SETTINGS (settings); |
| 332 |
user = camel_network_settings_dup_user (network_settings); |
| 333 |
|
| 334 |
g_object_unref (settings); |
| 335 |
|
| 336 |
if (CAMEL_IS_INTERNET_ADDRESS (from)) |
| 337 |
used_from = CAMEL_INTERNET_ADDRESS (from); |
| 338 |
else |
| 339 |
used_from = camel_mime_message_get_from (message); |
| 340 |
|
| 341 |
if (!used_from || camel_address_length (CAMEL_ADDRESS (used_from)) == 0) { |
| 342 |
g_set_error_literal ( |
| 343 |
error, CAMEL_ERROR, CAMEL_ERROR_GENERIC, |
| 344 |
_("Cannot send message with no From address")); |
| 345 |
goto exit; |
| 346 |
|
| 347 |
} else if (camel_address_length (CAMEL_ADDRESS (used_from)) > 1) { |
| 348 |
g_set_error_literal ( |
| 349 |
error, CAMEL_ERROR, CAMEL_ERROR_GENERIC, |
| 350 |
_("Exchange server cannot send message with " |
| 351 |
"multiple From addresses")); |
| 352 |
goto exit; |
| 353 |
|
| 354 |
} else { |
| 355 |
const gchar *used_email = NULL; |
| 356 |
|
| 357 |
if (!camel_internet_address_get (used_from, 0, NULL, &used_email)) { |
| 358 |
g_set_error_literal ( |
| 359 |
error, CAMEL_ERROR, CAMEL_ERROR_GENERIC, |
| 360 |
_("Failed to read From address")); |
| 361 |
goto exit; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
cnc = ews_transport_ref_connection (CAMEL_EWS_TRANSPORT (service)); |
| 366 |
if (!cnc) { |
| 367 |
g_set_error ( |
| 368 |
error, CAMEL_SERVICE_ERROR, |
| 369 |
CAMEL_SERVICE_ERROR_NOT_CONNECTED, |
| 370 |
_("Service not connected")); |
| 371 |
goto exit; |
| 372 |
} |
| 373 |
|
| 374 |
if (ews_transport_can_server_side_sent_folder (service, &folder_id, cancellable)) { |
| 375 |
if (out_sent_message_saved) |
| 376 |
*out_sent_message_saved = TRUE; |
| 377 |
} |
| 378 |
|
| 379 |
success = camel_ews_utils_create_mime_message ( |
| 380 |
cnc, folder_id ? "SendAndSaveCopy" : "SendOnly", folder_id, message, NULL, |
| 381 |
from, recipients, NULL, NULL, cancellable, error); |
| 382 |
|
| 383 |
g_object_unref (cnc); |
| 384 |
e_ews_folder_id_free (folder_id); |
| 385 |
|
| 386 |
exit: |
| 387 |
g_free (ews_email); |
| 388 |
g_free (host_url); |
| 389 |
g_free (user); |
| 390 |
|
| 391 |
return success; |
| 392 |
} |
| 393 |
|
| 394 |
static void |
| 395 |
ews_transport_dispose (GObject *object) |
| 396 |
{ |
| 397 |
CamelEwsTransport *ews_transport; |
| 398 |
|
| 399 |
ews_transport = CAMEL_EWS_TRANSPORT (object); |
| 400 |
|
| 401 |
g_mutex_lock (&ews_transport->priv->connection_lock); |
| 402 |
g_clear_object (&ews_transport->priv->connection); |
| 403 |
g_mutex_unlock (&ews_transport->priv->connection_lock); |
| 404 |
|
| 405 |
/* Chain up to parent's method. */ |
| 406 |
G_OBJECT_CLASS (camel_ews_transport_parent_class)->dispose (object); |
| 407 |
} |
| 408 |
|
| 409 |
static void |
| 410 |
ews_transport_finalize (GObject *object) |
| 411 |
{ |
| 412 |
CamelEwsTransport *ews_transport; |
| 413 |
|
| 414 |
ews_transport = CAMEL_EWS_TRANSPORT (object); |
| 415 |
|
| 416 |
g_mutex_clear (&ews_transport->priv->connection_lock); |
| 417 |
|
| 418 |
/* Chain up to parent's method. */ |
| 419 |
G_OBJECT_CLASS (camel_ews_transport_parent_class)->finalize (object); |
| 420 |
} |
| 421 |
|
| 422 |
static void |
| 423 |
camel_ews_transport_class_init (CamelEwsTransportClass *class) |
| 424 |
{ |
| 425 |
GObjectClass *object_class; |
| 426 |
CamelServiceClass *service_class; |
| 427 |
CamelTransportClass *transport_class; |
| 428 |
|
| 429 |
object_class = G_OBJECT_CLASS (class); |
| 430 |
object_class->dispose = ews_transport_dispose; |
| 431 |
object_class->finalize = ews_transport_finalize; |
| 432 |
|
| 433 |
service_class = CAMEL_SERVICE_CLASS (class); |
| 434 |
service_class->settings_type = CAMEL_TYPE_EWS_SETTINGS; |
| 435 |
service_class->get_name = ews_transport_get_name; |
| 436 |
service_class->connect_sync = ews_transport_connect_sync; |
| 437 |
service_class->disconnect_sync = ews_transport_disconnect_sync; |
| 438 |
service_class->authenticate_sync = ews_transport_authenticate_sync; |
| 439 |
|
| 440 |
transport_class = CAMEL_TRANSPORT_CLASS (class); |
| 441 |
transport_class->send_to_sync = ews_send_to_sync; |
| 442 |
} |
| 443 |
|
| 444 |
static void |
| 445 |
camel_ews_transport_init (CamelEwsTransport *ews_transport) |
| 446 |
{ |
| 447 |
ews_transport->priv = camel_ews_transport_get_instance_private (ews_transport); |
| 448 |
|
| 449 |
g_mutex_init (&ews_transport->priv->connection_lock); |
| 450 |
} |