| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: (C) 2020 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 |
#include <em-format/e-mail-formatter-extension.h> |
| 12 |
#include <em-format/e-mail-formatter.h> |
| 13 |
#include <em-format/e-mail-part-utils.h> |
| 14 |
|
| 15 |
#include "e-mail-part-ews-sharing-metadata.h" |
| 16 |
#include "e-mail-formatter-ews-sharing-metadata.h" |
| 17 |
|
| 18 |
typedef EMailFormatterExtension EMailFormatterEwsSharingMetadata; |
| 19 |
typedef EMailFormatterExtensionClass EMailFormatterEwsSharingMetadataClass; |
| 20 |
|
| 21 |
GType e_mail_formatter_ews_sharing_metadata_get_type (void); |
| 22 |
|
| 23 |
G_DEFINE_DYNAMIC_TYPE (EMailFormatterEwsSharingMetadata, e_mail_formatter_ews_sharing_metadata, E_TYPE_MAIL_FORMATTER_EXTENSION) |
| 24 |
|
| 25 |
static const gchar *formatter_mime_types[] = { |
| 26 |
"application/x-sharing-metadata-xml", |
| 27 |
NULL |
| 28 |
}; |
| 29 |
|
| 30 |
static gboolean |
| 31 |
emf_ews_sharing_metadata (const gchar *xml_str, |
| 32 |
gchar **out_datatype, |
| 33 |
gchar **out_initiator_name, |
| 34 |
gchar **out_initiator_email, |
| 35 |
gchar **out_folder_entry_id) |
| 36 |
{ |
| 37 |
xmlDoc *xml; |
| 38 |
xmlXPathContext *xpath; |
| 39 |
|
| 40 |
if (!xml_str || !*xml_str) |
| 41 |
return FALSE; |
| 42 |
|
| 43 |
xml = e_xml_parse_data (xml_str, strlen (xml_str)); |
| 44 |
|
| 45 |
if (!xml) |
| 46 |
return FALSE; |
| 47 |
|
| 48 |
xpath = e_xml_new_xpath_context_with_namespaces (xml, |
| 49 |
"s", "http://schemas.microsoft.com/sharing/2008", |
| 50 |
"e", "http://schemas.microsoft.com/exchange/sharing/2008", |
| 51 |
NULL); |
| 52 |
|
| 53 |
*out_datatype = e_xml_xpath_eval_as_string (xpath, "/s:SharingMessage/s:DataType"); |
| 54 |
*out_initiator_name = e_xml_xpath_eval_as_string (xpath, "/s:SharingMessage/s:Initiator/s:Name"); |
| 55 |
*out_initiator_email = e_xml_xpath_eval_as_string (xpath, "/s:SharingMessage/s:Initiator/s:SmtpAddress"); |
| 56 |
*out_folder_entry_id = e_xml_xpath_eval_as_string (xpath, "/s:SharingMessage/s:Invitation/s:Providers/s:Provider/e:FolderId"); |
| 57 |
|
| 58 |
xmlXPathFreeContext (xpath); |
| 59 |
xmlFreeDoc (xml); |
| 60 |
|
| 61 |
return *out_datatype && **out_datatype && |
| 62 |
*out_initiator_name && **out_initiator_name && |
| 63 |
*out_initiator_email && **out_initiator_email && |
| 64 |
*out_folder_entry_id && **out_folder_entry_id; |
| 65 |
} |
| 66 |
|
| 67 |
static gboolean |
| 68 |
emf_ews_sharing_metadata_format (EMailFormatterExtension *extension, |
| 69 |
EMailFormatter *formatter, |
| 70 |
EMailFormatterContext *context, |
| 71 |
EMailPart *part, |
| 72 |
GOutputStream *stream, |
| 73 |
GCancellable *cancellable) |
| 74 |
{ |
| 75 |
EMailPartEwsSharingMetadata *sharing_part; |
| 76 |
gboolean handled = TRUE; |
| 77 |
GString *buffer = NULL; |
| 78 |
|
| 79 |
if (!E_IS_MAIL_PART_EWS_SHARING_METADATA (part)) |
| 80 |
return FALSE; |
| 81 |
|
| 82 |
sharing_part = E_MAIL_PART_EWS_SHARING_METADATA (part); |
| 83 |
|
| 84 |
if (context->mode == E_MAIL_FORMATTER_MODE_PRINTING || |
| 85 |
context->mode == E_MAIL_FORMATTER_MODE_RAW) { |
| 86 |
gchar *datatype = NULL, *initiator_name = NULL, *initiator_email = NULL, *folder_entry_id = NULL; |
| 87 |
|
| 88 |
if (emf_ews_sharing_metadata (sharing_part->xml, &datatype, &initiator_name, &initiator_email, &folder_entry_id)) { |
| 89 |
ENamedParameters *params; |
| 90 |
gchar *params_str, *info; |
| 91 |
|
| 92 |
params = e_named_parameters_new (); |
| 93 |
|
| 94 |
e_named_parameters_set (params, "email", initiator_email); |
| 95 |
e_named_parameters_set (params, "folder_id", folder_entry_id); |
| 96 |
|
| 97 |
params_str = e_named_parameters_to_string (params); |
| 98 |
|
| 99 |
e_named_parameters_free (params); |
| 100 |
|
| 101 |
buffer = g_string_sized_new (2048); |
| 102 |
|
| 103 |
g_string_append (buffer, e_mail_formatter_get_sub_html_header (formatter)); |
| 104 |
/* No need for body margins within <iframe> */ |
| 105 |
g_string_append (buffer, "<style>body{ margin: 0; }</style>"); |
| 106 |
|
| 107 |
if (g_strcmp0 (datatype, "calendar") == 0) { |
| 108 |
/* Translators: the first %s is replaced with a user name, the second %s is replaced with an email address of that user */ |
| 109 |
info = g_strdup_printf (_("%s (%s) has invited you to view his or her Microsoft Exchange calendar."), initiator_name, initiator_email); |
| 110 |
} else { |
| 111 |
/* Translators: the first %s is replaced with a user name, the second %s is replaced with an email address of that user */ |
| 112 |
info = g_strdup_printf (_("%s (%s) has invited you to view his or her Microsoft Exchange folder."), initiator_name, initiator_email); |
| 113 |
} |
| 114 |
|
| 115 |
e_util_markup_append_escaped (buffer, |
| 116 |
"<div class=\"part-container -e-web-view-background-color -e-web-view-text-color\" style=\"border: none; padding: 8px; margin: 0;\">%s<br>" |
| 117 |
"<br>" |
| 118 |
"%s<br>" |
| 119 |
"<br>" |
| 120 |
"<button type=\"button\" class=\"ews-sharing-metadata-btn\" id=\"ews-sharing-metadata-btn\" value=\"%s\">%s</button></div></body></html>", |
| 121 |
info, |
| 122 |
_("Click the Subscribe button to add it to Evolution."), |
| 123 |
params_str, |
| 124 |
_("Subscribe")); |
| 125 |
|
| 126 |
g_free (params_str); |
| 127 |
g_free (info); |
| 128 |
} else { |
| 129 |
CamelMimePart *err_part; |
| 130 |
EMailPart *mail_part; |
| 131 |
const gchar *msg, *mime_type = "application/vnd.evolution.error"; |
| 132 |
|
| 133 |
msg = _("Failed to extract sharing information from provided data."); |
| 134 |
|
| 135 |
err_part = camel_mime_part_new (); |
| 136 |
camel_mime_part_set_content (err_part, msg, strlen (msg), mime_type); |
| 137 |
|
| 138 |
mail_part = e_mail_part_new (err_part, e_mail_part_get_id (part)); |
| 139 |
|
| 140 |
handled = e_mail_formatter_format_as (formatter, context, mail_part, stream, mime_type, cancellable); |
| 141 |
|
| 142 |
g_object_unref (mail_part); |
| 143 |
g_object_unref (err_part); |
| 144 |
} |
| 145 |
|
| 146 |
g_free (datatype); |
| 147 |
g_free (initiator_name); |
| 148 |
g_free (initiator_email); |
| 149 |
g_free (folder_entry_id); |
| 150 |
} else { |
| 151 |
const gchar *default_charset, *charset; |
| 152 |
gchar *uri; |
| 153 |
|
| 154 |
default_charset = e_mail_formatter_get_default_charset (formatter); |
| 155 |
charset = e_mail_formatter_get_charset (formatter); |
| 156 |
|
| 157 |
if (!default_charset) |
| 158 |
default_charset = ""; |
| 159 |
if (!charset) |
| 160 |
charset = ""; |
| 161 |
|
| 162 |
uri = e_mail_part_build_uri ( |
| 163 |
e_mail_part_list_get_folder (context->part_list), |
| 164 |
e_mail_part_list_get_message_uid (context->part_list), |
| 165 |
"part_id", G_TYPE_STRING, e_mail_part_get_id (part), |
| 166 |
"mode", G_TYPE_INT, E_MAIL_FORMATTER_MODE_RAW, |
| 167 |
"formatter_default_charset", G_TYPE_STRING, default_charset, |
| 168 |
"formatter_charset", G_TYPE_STRING, charset, |
| 169 |
NULL); |
| 170 |
|
| 171 |
buffer = g_string_sized_new (256); |
| 172 |
|
| 173 |
g_string_append_printf (buffer, |
| 174 |
"<div class=\"part-container-nostyle\" >" |
| 175 |
"<iframe width=\"100%%\" height=\"10\"" |
| 176 |
" id=\"%s\" name=\"%s\" " |
| 177 |
" frameborder=\"0\" src=\"%s\" " |
| 178 |
" class=\"-e-mail-formatter-frame-color %s" |
| 179 |
" -e-web-view-text-color\" >" |
| 180 |
"</iframe>" |
| 181 |
"</div>", |
| 182 |
e_mail_part_get_id (part), |
| 183 |
e_mail_part_get_id (part), |
| 184 |
uri, |
| 185 |
e_mail_part_get_frame_security_style (part)); |
| 186 |
|
| 187 |
g_free (uri); |
| 188 |
} |
| 189 |
|
| 190 |
if (buffer) { |
| 191 |
g_output_stream_write_all (stream, buffer->str, buffer->len, NULL, cancellable, NULL); |
| 192 |
|
| 193 |
g_string_free (buffer, TRUE); |
| 194 |
} |
| 195 |
|
| 196 |
return handled; |
| 197 |
} |
| 198 |
|
| 199 |
static void |
| 200 |
e_mail_formatter_ews_sharing_metadata_class_init (EMailFormatterExtensionClass *klass) |
| 201 |
{ |
| 202 |
klass->display_name = _("EWS Sharing Metadata"); |
| 203 |
klass->description = _("Display part as EWS sharing metadata"); |
| 204 |
klass->mime_types = formatter_mime_types; |
| 205 |
klass->format = emf_ews_sharing_metadata_format; |
| 206 |
} |
| 207 |
|
| 208 |
static void |
| 209 |
e_mail_formatter_ews_sharing_metadata_class_finalize (EMailFormatterExtensionClass *klass) |
| 210 |
{ |
| 211 |
} |
| 212 |
|
| 213 |
static void |
| 214 |
e_mail_formatter_ews_sharing_metadata_init (EMailFormatterExtension *extension) |
| 215 |
{ |
| 216 |
} |
| 217 |
|
| 218 |
void |
| 219 |
e_mail_formatter_ews_sharing_metadata_type_register (GTypeModule *type_module) |
| 220 |
{ |
| 221 |
e_mail_formatter_ews_sharing_metadata_register_type (type_module); |
| 222 |
} |