| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 1999-2011 Novell, Inc. (www.novell.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 |
#include <glib/gstdio.h> |
| 10 |
|
| 11 |
#include "common/e-ews-request.h" |
| 12 |
#include "common/e-ews-item-change.h" |
| 13 |
|
| 14 |
#include "e-ews-camel-common.h" |
| 15 |
|
| 16 |
struct _create_mime_msg_data { |
| 17 |
EEwsConnection *cnc; |
| 18 |
CamelMimeMessage *message; |
| 19 |
CamelMessageInfo *info; |
| 20 |
CamelAddress *from; |
| 21 |
CamelAddress *recipients; |
| 22 |
gboolean is_send; |
| 23 |
}; |
| 24 |
|
| 25 |
static void |
| 26 |
filter_recipients (CamelMimeMessage *message, |
| 27 |
CamelAddress *recipients, |
| 28 |
GHashTable *recip_to, |
| 29 |
GHashTable *recip_cc, |
| 30 |
GHashTable *recip_bcc) |
| 31 |
{ |
| 32 |
CamelInternetAddress *addresses, *mime_cc, *mime_bcc; |
| 33 |
gint ii, len; |
| 34 |
|
| 35 |
g_return_if_fail (message != NULL); |
| 36 |
g_return_if_fail (recipients != NULL); |
| 37 |
g_return_if_fail (CAMEL_IS_INTERNET_ADDRESS (recipients)); |
| 38 |
g_return_if_fail (recip_to != NULL); |
| 39 |
g_return_if_fail (recip_cc != NULL); |
| 40 |
g_return_if_fail (recip_bcc != NULL); |
| 41 |
|
| 42 |
mime_cc = camel_mime_message_get_recipients (message, CAMEL_RECIPIENT_TYPE_CC); |
| 43 |
mime_bcc = camel_mime_message_get_recipients (message, CAMEL_RECIPIENT_TYPE_BCC); |
| 44 |
|
| 45 |
addresses = CAMEL_INTERNET_ADDRESS (recipients); |
| 46 |
len = camel_address_length (recipients); |
| 47 |
for (ii = 0; ii < len; ii++) { |
| 48 |
const gchar *name = NULL, *email = NULL; |
| 49 |
|
| 50 |
if (!camel_internet_address_get (addresses, ii, &name, &email) || |
| 51 |
!email) |
| 52 |
continue; |
| 53 |
|
| 54 |
if (mime_bcc && camel_internet_address_find_address (mime_bcc, email, NULL) != -1) { |
| 55 |
g_hash_table_insert (recip_bcc, (gpointer) email, GINT_TO_POINTER (1)); |
| 56 |
} else if (mime_cc && camel_internet_address_find_address (mime_cc, email, NULL) != -1) { |
| 57 |
g_hash_table_insert (recip_cc, (gpointer) email, GINT_TO_POINTER (1)); |
| 58 |
} else { |
| 59 |
g_hash_table_insert (recip_to, (gpointer) email, GINT_TO_POINTER (1)); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
static void |
| 65 |
write_recipients (ESoapRequest *request, |
| 66 |
const gchar *elem_name, |
| 67 |
GHashTable *recips, |
| 68 |
gboolean is_resend) |
| 69 |
{ |
| 70 |
GHashTableIter iter; |
| 71 |
gpointer key, value; |
| 72 |
|
| 73 |
g_return_if_fail (request != NULL); |
| 74 |
g_return_if_fail (elem_name != NULL); |
| 75 |
g_return_if_fail (recips != NULL); |
| 76 |
|
| 77 |
if (!is_resend && !g_hash_table_size (recips)) |
| 78 |
return; |
| 79 |
|
| 80 |
e_soap_request_start_element (request, elem_name, NULL, NULL); |
| 81 |
|
| 82 |
g_hash_table_iter_init (&iter, recips); |
| 83 |
while (g_hash_table_iter_next (&iter, &key, &value)) { |
| 84 |
e_soap_request_start_element (request, "Mailbox", NULL, NULL); |
| 85 |
e_ews_request_write_string_parameter_with_attribute (request, "EmailAddress", NULL, key, NULL, NULL); |
| 86 |
e_soap_request_end_element (request); /* Mailbox */ |
| 87 |
} |
| 88 |
|
| 89 |
e_soap_request_end_element (request); /* elem_name */ |
| 90 |
} |
| 91 |
|
| 92 |
static gboolean |
| 93 |
is_any_address_filled (CamelInternetAddress *addrs) |
| 94 |
{ |
| 95 |
return addrs && camel_address_length (CAMEL_ADDRESS (addrs)) > 0; |
| 96 |
} |
| 97 |
|
| 98 |
/* MAPI flags gleaned from windows header files */ |
| 99 |
#define MAPI_MSGFLAG_READ 0x01 |
| 100 |
#define MAPI_MSGFLAG_UNSENT 0x08 |
| 101 |
|
| 102 |
static gboolean |
| 103 |
create_mime_message_cb (ESoapRequest *request, |
| 104 |
gpointer user_data, |
| 105 |
GError **error) |
| 106 |
{ |
| 107 |
struct _create_mime_msg_data *create_data = user_data; |
| 108 |
CamelStream *mem, *filtered; |
| 109 |
CamelMimeFilter *filter; |
| 110 |
CamelContentType *content_type; |
| 111 |
GByteArray *bytes; |
| 112 |
gchar *base64; |
| 113 |
gint msgflag; |
| 114 |
guint32 message_camel_flags = 0; |
| 115 |
|
| 116 |
if (create_data->info) |
| 117 |
message_camel_flags = camel_message_info_get_flags (create_data->info); |
| 118 |
|
| 119 |
if (create_data->is_send && !(message_camel_flags & CAMEL_MESSAGE_FLAGGED)) { |
| 120 |
const gchar *value; |
| 121 |
|
| 122 |
value = camel_medium_get_header (CAMEL_MEDIUM (create_data->message), "X-Priority"); |
| 123 |
|
| 124 |
if (g_strcmp0 (value, "1") == 0) { |
| 125 |
message_camel_flags |= CAMEL_MESSAGE_FLAGGED; |
| 126 |
} else { |
| 127 |
value = camel_medium_get_header (CAMEL_MEDIUM (create_data->message), "Importance"); |
| 128 |
|
| 129 |
if (value && g_ascii_strcasecmp (value, "High") == 0) |
| 130 |
message_camel_flags |= CAMEL_MESSAGE_FLAGGED; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
e_soap_request_start_element (request, "Message", NULL, NULL); |
| 135 |
e_soap_request_start_element (request, "MimeContent", NULL, NULL); |
| 136 |
|
| 137 |
/* This is horrid. We really need to extend ESoapRequest to allow us |
| 138 |
* to stream this directly rather than storing it in RAM. Which right |
| 139 |
* now we are doing about four times: the GByteArray in the mem stream, |
| 140 |
* then the base64 version, then the xmlDoc, then the soup request. */ |
| 141 |
|
| 142 |
mem = camel_stream_mem_new (); |
| 143 |
filtered = camel_stream_filter_new (mem); |
| 144 |
|
| 145 |
filter = camel_mime_filter_crlf_new ( |
| 146 |
CAMEL_MIME_FILTER_CRLF_ENCODE, |
| 147 |
CAMEL_MIME_FILTER_CRLF_MODE_CRLF_ONLY); |
| 148 |
camel_stream_filter_add (CAMEL_STREAM_FILTER (filtered), filter); |
| 149 |
g_object_unref (filter); |
| 150 |
|
| 151 |
camel_data_wrapper_write_to_stream_sync ( |
| 152 |
CAMEL_DATA_WRAPPER (create_data->message), |
| 153 |
filtered, NULL, NULL); |
| 154 |
camel_stream_flush (filtered, NULL, NULL); |
| 155 |
camel_stream_flush (mem, NULL, NULL); |
| 156 |
bytes = camel_stream_mem_get_byte_array (CAMEL_STREAM_MEM (mem)); |
| 157 |
|
| 158 |
base64 = g_base64_encode (bytes->data, bytes->len); |
| 159 |
g_object_unref (mem); |
| 160 |
g_object_unref (filtered); |
| 161 |
|
| 162 |
e_soap_request_write_string (request, base64); |
| 163 |
g_free (base64); |
| 164 |
|
| 165 |
e_soap_request_end_element (request); /* MimeContent */ |
| 166 |
|
| 167 |
content_type = camel_mime_part_get_content_type (CAMEL_MIME_PART (create_data->message)); |
| 168 |
if (content_type && camel_content_type_is (content_type, "multipart", "report") && |
| 169 |
camel_content_type_param (content_type, "report-type") && |
| 170 |
g_ascii_strcasecmp (camel_content_type_param (content_type, "report-type"), "disposition-notification") == 0) { |
| 171 |
/* it's a disposition notification reply, set ItemClass too */ |
| 172 |
e_soap_request_start_element (request, "ItemClass", NULL, NULL); |
| 173 |
e_soap_request_write_string (request, "REPORT.IPM.NOTE.IPNRN"); |
| 174 |
e_soap_request_end_element (request); /* ItemClass */ |
| 175 |
} |
| 176 |
|
| 177 |
e_ews_request_write_string_parameter_with_attribute (request, |
| 178 |
"Importance", |
| 179 |
NULL, |
| 180 |
(message_camel_flags & CAMEL_MESSAGE_FLAGGED) != 0 ? "High" : "Normal", |
| 181 |
NULL, |
| 182 |
NULL); |
| 183 |
|
| 184 |
/* more MAPI crap. You can't just set the IsDraft property |
| 185 |
* here you have to use the MAPI MSGFLAG_UNSENT extended |
| 186 |
* property Further crap is that Exchange 2007 assumes when it |
| 187 |
* sees this property that you're setting the value to 0 |
| 188 |
* ... it never checks */ |
| 189 |
msgflag = MAPI_MSGFLAG_READ; /* draft or sent is always read */ |
| 190 |
if ((message_camel_flags & CAMEL_MESSAGE_DRAFT) != 0) |
| 191 |
msgflag |= MAPI_MSGFLAG_UNSENT; |
| 192 |
|
| 193 |
e_ews_request_add_extended_property_tag_int (request, 0x0e07, msgflag); |
| 194 |
|
| 195 |
if ((message_camel_flags & (CAMEL_MESSAGE_FORWARDED | CAMEL_MESSAGE_ANSWERED)) != 0) { |
| 196 |
gint icon; |
| 197 |
|
| 198 |
icon = (message_camel_flags & CAMEL_MESSAGE_ANSWERED) != 0 ? 0x105 : 0x106; |
| 199 |
|
| 200 |
e_ews_request_add_extended_property_tag_int (request, 0x1080, icon); |
| 201 |
} |
| 202 |
|
| 203 |
if (create_data->info) { |
| 204 |
const gchar *followup, *completed, *dueby; |
| 205 |
time_t completed_tt = (time_t) 0 , dueby_tt = (time_t) 0; |
| 206 |
|
| 207 |
/* follow-up flags */ |
| 208 |
followup = camel_message_info_get_user_tag (create_data->info, "follow-up"); |
| 209 |
completed = camel_message_info_get_user_tag (create_data->info, "completed-on"); |
| 210 |
dueby = camel_message_info_get_user_tag (create_data->info, "due-by"); |
| 211 |
|
| 212 |
if (followup && !*followup) |
| 213 |
followup = NULL; |
| 214 |
|
| 215 |
if (completed && *completed) |
| 216 |
completed_tt = camel_header_decode_date (completed, NULL); |
| 217 |
|
| 218 |
if (dueby && *dueby) |
| 219 |
dueby_tt = camel_header_decode_date (dueby, NULL); |
| 220 |
|
| 221 |
/* PidTagFlagStatus */ |
| 222 |
e_ews_request_add_extended_property_tag_int (request, 0x1090, |
| 223 |
followup ? (completed_tt != (time_t) 0 ? 0x01 /* followupComplete */: 0x02 /* followupFlagged */) : 0x0); |
| 224 |
|
| 225 |
if (followup) { |
| 226 |
/* PidLidFlagRequest */ |
| 227 |
e_ews_request_add_extended_property_distinguished_tag_string (request, "Common", 0x8530, followup); |
| 228 |
|
| 229 |
/* PidTagToDoItemFlags */ |
| 230 |
e_ews_request_add_extended_property_tag_int (request, 0x0e2b, 1); |
| 231 |
} |
| 232 |
|
| 233 |
if (followup && completed_tt != (time_t) 0) { |
| 234 |
/* minute precision */ |
| 235 |
completed_tt = completed_tt - (completed_tt % 60); |
| 236 |
|
| 237 |
/* PidTagFlagCompleteTime */ |
| 238 |
e_ews_request_add_extended_property_tag_time (request, 0x1091, completed_tt); |
| 239 |
|
| 240 |
/* PidLidTaskDateCompleted */ |
| 241 |
e_ews_request_add_extended_property_distinguished_tag_time (request, "Task", 0x810f, completed_tt); |
| 242 |
|
| 243 |
/* PidLidTaskStatus */ |
| 244 |
e_ews_request_add_extended_property_distinguished_tag_int (request, "Task", 0x8101, 2); |
| 245 |
|
| 246 |
/* PidLidPercentComplete */ |
| 247 |
e_ews_request_add_extended_property_distinguished_tag_double (request, "Task", 0x8102, 1.0); |
| 248 |
|
| 249 |
/* PidLidTaskComplete */ |
| 250 |
e_ews_request_add_extended_property_distinguished_tag_boolean (request, "Task", 0x811c, TRUE); |
| 251 |
} |
| 252 |
|
| 253 |
if (followup && dueby_tt != (time_t) 0 && completed_tt == (time_t) 0) { |
| 254 |
/* PidLidTaskStatus */ |
| 255 |
e_ews_request_add_extended_property_distinguished_tag_int (request, "Task", 0x8101, 0); |
| 256 |
|
| 257 |
/* PidLidPercentComplete */ |
| 258 |
e_ews_request_add_extended_property_distinguished_tag_double (request, "Task", 0x8102, 0.0); |
| 259 |
|
| 260 |
/* PidLidTaskDueDate */ |
| 261 |
e_ews_request_add_extended_property_distinguished_tag_time (request, "Task", 0x8105, dueby_tt); |
| 262 |
|
| 263 |
/* PidLidTaskComplete */ |
| 264 |
e_ews_request_add_extended_property_distinguished_tag_boolean (request, "Task", 0x811c, FALSE); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if (create_data->cnc && create_data->is_send) { |
| 269 |
CamelEwsSettings *settings; |
| 270 |
|
| 271 |
settings = e_ews_connection_ref_settings (create_data->cnc); |
| 272 |
if (settings) { |
| 273 |
e_soap_request_start_element (request, "Sender", NULL, NULL); |
| 274 |
|
| 275 |
e_soap_request_start_element (request, "Mailbox", NULL, NULL); |
| 276 |
e_ews_request_write_string_parameter_with_attribute (request, "EmailAddress", NULL, camel_ews_settings_get_email (settings), NULL, NULL); |
| 277 |
e_soap_request_end_element (request); /* Mailbox */ |
| 278 |
|
| 279 |
e_soap_request_end_element (request); /* Sender */ |
| 280 |
} |
| 281 |
g_clear_object (&settings); |
| 282 |
} |
| 283 |
|
| 284 |
if (create_data->recipients) { |
| 285 |
GHashTable *recip_to, *recip_cc, *recip_bcc; |
| 286 |
gboolean is_resend; |
| 287 |
|
| 288 |
is_resend = is_any_address_filled (camel_mime_message_get_recipients (create_data->message, CAMEL_RECIPIENT_TYPE_RESENT_TO)) || |
| 289 |
is_any_address_filled (camel_mime_message_get_recipients (create_data->message, CAMEL_RECIPIENT_TYPE_RESENT_CC)) || |
| 290 |
is_any_address_filled (camel_mime_message_get_recipients (create_data->message, CAMEL_RECIPIENT_TYPE_RESENT_BCC)); |
| 291 |
|
| 292 |
recip_to = g_hash_table_new (camel_strcase_hash, camel_strcase_equal); |
| 293 |
recip_cc = g_hash_table_new (camel_strcase_hash, camel_strcase_equal); |
| 294 |
recip_bcc = g_hash_table_new (camel_strcase_hash, camel_strcase_equal); |
| 295 |
|
| 296 |
filter_recipients (create_data->message, create_data->recipients, recip_to, recip_cc, recip_bcc); |
| 297 |
|
| 298 |
write_recipients (request, "ToRecipients", recip_to, is_resend); |
| 299 |
write_recipients (request, "CcRecipients", recip_cc, is_resend); |
| 300 |
write_recipients (request, "BccRecipients", recip_bcc, is_resend); |
| 301 |
|
| 302 |
g_hash_table_destroy (recip_to); |
| 303 |
g_hash_table_destroy (recip_cc); |
| 304 |
g_hash_table_destroy (recip_bcc); |
| 305 |
} |
| 306 |
|
| 307 |
if (create_data->is_send && create_data->from && CAMEL_IS_INTERNET_ADDRESS (create_data->from)) { |
| 308 |
const gchar *from_name = NULL, *from_email = NULL; |
| 309 |
|
| 310 |
if (camel_internet_address_get (CAMEL_INTERNET_ADDRESS (create_data->from), 0, &from_name, &from_email) && from_email) { |
| 311 |
e_soap_request_start_element (request, "From", NULL, NULL); |
| 312 |
|
| 313 |
e_soap_request_start_element (request, "Mailbox", NULL, NULL); |
| 314 |
if (from_name && *from_name) |
| 315 |
e_ews_request_write_string_parameter_with_attribute (request, "Name", NULL, from_name, NULL, NULL); |
| 316 |
e_ews_request_write_string_parameter_with_attribute (request, "EmailAddress", NULL, from_email, NULL, NULL); |
| 317 |
e_soap_request_end_element (request); /* Mailbox */ |
| 318 |
|
| 319 |
e_soap_request_end_element (request); /* From */ |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
e_ews_request_write_string_parameter_with_attribute ( |
| 324 |
request, |
| 325 |
"IsRead", |
| 326 |
NULL, |
| 327 |
(message_camel_flags & CAMEL_MESSAGE_SEEN) != 0 ? "true" : "false", |
| 328 |
NULL, |
| 329 |
NULL); |
| 330 |
|
| 331 |
e_soap_request_end_element (request); /* Message */ |
| 332 |
|
| 333 |
g_free (create_data); |
| 334 |
|
| 335 |
return TRUE; |
| 336 |
} |
| 337 |
|
| 338 |
gboolean |
| 339 |
camel_ews_utils_create_mime_message (EEwsConnection *cnc, |
| 340 |
const gchar *disposition, |
| 341 |
const EwsFolderId *fid, |
| 342 |
CamelMimeMessage *message, |
| 343 |
CamelMessageInfo *info, |
| 344 |
CamelAddress *from, |
| 345 |
CamelAddress *recipients, |
| 346 |
gchar **itemid, |
| 347 |
gchar **changekey, |
| 348 |
GCancellable *cancellable, |
| 349 |
GError **error) |
| 350 |
{ |
| 351 |
struct _create_mime_msg_data *create_data; |
| 352 |
GSList *ids; |
| 353 |
EEwsItem *item; |
| 354 |
const EwsId *ewsid; |
| 355 |
gboolean res; |
| 356 |
|
| 357 |
create_data = g_new0 (struct _create_mime_msg_data, 1); |
| 358 |
|
| 359 |
create_data->cnc = cnc; |
| 360 |
create_data->message = message; |
| 361 |
create_data->info = info; |
| 362 |
create_data->from = from; |
| 363 |
create_data->recipients = recipients; |
| 364 |
create_data->is_send = g_strcmp0 (disposition, "SendOnly") == 0 || g_strcmp0 (disposition, "SendAndSaveCopy") == 0; |
| 365 |
|
| 366 |
if (create_data->is_send && !create_data->from) { |
| 367 |
CamelInternetAddress *address = camel_mime_message_get_from (message); |
| 368 |
|
| 369 |
if (address) |
| 370 |
create_data->from = CAMEL_ADDRESS (address); |
| 371 |
} |
| 372 |
|
| 373 |
res = e_ews_connection_create_items_sync ( |
| 374 |
cnc, EWS_PRIORITY_MEDIUM, |
| 375 |
disposition, NULL, fid, |
| 376 |
create_mime_message_cb, create_data, |
| 377 |
&ids, cancellable, error); |
| 378 |
|
| 379 |
if (!res || (!itemid && !changekey)) |
| 380 |
return res; |
| 381 |
|
| 382 |
item = (EEwsItem *) ids->data; |
| 383 |
if (!item || !(ewsid = e_ews_item_get_id (item))) { |
| 384 |
g_set_error ( |
| 385 |
error, CAMEL_ERROR, CAMEL_ERROR_GENERIC, |
| 386 |
_("CreateItem call failed to return ID for new message")); |
| 387 |
return FALSE; |
| 388 |
} |
| 389 |
|
| 390 |
if (itemid) |
| 391 |
*itemid = g_strdup (ewsid->id); |
| 392 |
if (changekey) |
| 393 |
*changekey = g_strdup (ewsid->change_key); |
| 394 |
|
| 395 |
g_object_unref (item); |
| 396 |
g_slist_free (ids); |
| 397 |
return TRUE; |
| 398 |
} |