No issues found
Tool | Failure ID | Location | Function | Message | Data |
---|---|---|---|---|---|
clang-analyzer | no-output-found | e-mail-request.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None | |
clang-analyzer | no-output-found | e-mail-request.c | Message(text='Unable to locate XML output from invoke-clang-analyzer') | None |
1 /*
2 * e-mail-request.c
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with the program; if not, see <http://www.gnu.org/licenses/>
16 *
17 */
18
19 #define LIBSOUP_USE_UNSTABLE_REQUEST_API
20
21 #include "e-mail-request.h"
22 #include "em-utils.h"
23
24 #include <libsoup/soup.h>
25 #include <libsoup/soup-requester.h>
26 #include <libsoup/soup-request-http.h>
27
28 #include <webkit/webkit.h>
29
30 #include <glib/gi18n.h>
31 #include <camel/camel.h>
32
33 #include <em-format/e-mail-formatter.h>
34 #include <em-format/e-mail-formatter-utils.h>
35 #include <em-format/e-mail-formatter-print.h>
36
37 #include <e-util/e-icon-factory.h>
38 #include <e-util/e-util.h>
39
40 #include <shell/e-shell.h>
41
42 #define d(x)
43 #define dd(x)
44
45 #define E_MAIL_REQUEST_GET_PRIVATE(obj) \
46 (G_TYPE_INSTANCE_GET_PRIVATE \
47 ((obj), E_TYPE_MAIL_REQUEST, EMailRequestPrivate))
48
49 struct _EMailRequestPrivate {
50 CamelStream *output_stream;
51 gchar *mime_type;
52
53 gint content_length;
54
55 GHashTable *uri_query;
56 gchar *uri_base;
57 gchar *full_uri;
58
59 gchar *ret_mime_type;
60 };
61
62 G_DEFINE_TYPE (EMailRequest, e_mail_request, SOUP_TYPE_REQUEST)
63
64 static void
65 handle_mail_request (GSimpleAsyncResult *res,
66 GObject *object,
67 GCancellable *cancellable)
68 {
69 EMailRequest *request = E_MAIL_REQUEST (object);
70 GInputStream *stream;
71 EMailFormatter *formatter;
72 EMailPartList *part_list;
73 CamelObjectBag *registry;
74 GByteArray *ba;
75 gchar *part_id;
76 gchar *val;
77 const gchar *default_charset, *charset;
78
79 EMailFormatterContext context = { 0 };
80
81 if (g_cancellable_is_cancelled (cancellable))
82 return;
83
84 if (request->priv->output_stream != NULL) {
85 g_object_unref (request->priv->output_stream);
86 }
87
88 registry = e_mail_part_list_get_registry ();
89 part_list = camel_object_bag_get (registry, request->priv->uri_base);
90 g_return_if_fail (part_list != NULL);
91
92 request->priv->output_stream = camel_stream_mem_new ();
93
94 val = g_hash_table_lookup (request->priv->uri_query, "headers_collapsed");
95 if (val && atoi (val) == 1)
96 context.flags |= E_MAIL_FORMATTER_HEADER_FLAG_COLLAPSED;
97
98 val = g_hash_table_lookup (request->priv->uri_query, "headers_collapsable");
99 if (val && atoi (val) == 1)
100 context.flags |= E_MAIL_FORMATTER_HEADER_FLAG_COLLAPSABLE;
101
102 val = g_hash_table_lookup (request->priv->uri_query, "mode");
103 if (val)
104 context.mode = atoi (val);
105
106 context.message = part_list->message;
107 context.message_uid = part_list->message_uid;
108 context.folder = part_list->folder;
109 context.parts = part_list->list;
110 context.uri = request->priv->full_uri;
111
112 default_charset = g_hash_table_lookup (request->priv->uri_query, "formatter_default_charset");
113 charset = g_hash_table_lookup (request->priv->uri_query, "formatter_charset");
114
115 if (context.mode == E_MAIL_FORMATTER_MODE_PRINTING)
116 formatter = e_mail_formatter_print_new ();
117 else
118 formatter = e_mail_formatter_new ();
119
120 if (default_charset && *default_charset)
121 e_mail_formatter_set_default_charset (formatter, default_charset);
122 if (charset && *charset)
123 e_mail_formatter_set_charset (formatter, charset);
124
125 part_id = g_hash_table_lookup (request->priv->uri_query, "part_id");
126 if (part_id) {
127 EMailPart *part;
128 const gchar *mime_type;
129 /* original part_id is owned by the GHashTable */
130 part_id = soup_uri_decode (part_id);
131 part = e_mail_part_list_find_part (part_list, part_id);
132
133 val = g_hash_table_lookup (request->priv->uri_query, "mime_type");
134 if (val) {
135 mime_type = val;
136 } else {
137 mime_type = NULL;
138 }
139
140 if (context.mode == E_MAIL_FORMATTER_MODE_SOURCE) {
141 mime_type = "application/vnd.evolution.source";
142 }
143
144 if (part) {
145 if (context.mode == E_MAIL_FORMATTER_MODE_CID) {
146 CamelDataWrapper *dw;
147 CamelStream *raw_content;
148 GByteArray *ba;
149
150 dw = camel_medium_get_content (CAMEL_MEDIUM (part->part));
151 g_return_if_fail (dw);
152
153 raw_content = camel_stream_mem_new ();
154 camel_data_wrapper_decode_to_stream_sync (dw, raw_content, cancellable, NULL);
155 ba = camel_stream_mem_get_byte_array (CAMEL_STREAM_MEM (raw_content));
156
157 camel_stream_write (request->priv->output_stream, (gchar *) ba->data, ba->len, cancellable, NULL);
158
159 g_object_unref (raw_content);
160 } else {
161 e_mail_formatter_format_as (
162 formatter, &context, part, request->priv->output_stream,
163 mime_type ? mime_type : part->mime_type, cancellable);
164 }
165 } else {
166 g_warning ("Failed to lookup requested part '%s' - this should not happen!", part_id);
167 }
168
169 } else {
170 e_mail_formatter_format_sync (
171 formatter, part_list, request->priv->output_stream,
172 context.flags, context.mode, cancellable);
173 }
174
175 /* Convert the GString to GInputStream and send it back to WebKit */
176 ba = camel_stream_mem_get_byte_array (CAMEL_STREAM_MEM (request->priv->output_stream));
177 if (!ba->data) {
178 gchar *data = g_strdup_printf (_("Failed to load part '%s'"), part_id);
179 dd (printf ("%s", data));
180 g_byte_array_append (ba, (guchar *) data, strlen (data));
181 g_free (data);
182 } else {
183 dd ({
184 gchar *d = g_strndup ((gchar *) ba->data, ba->len);
185 printf ("%s", d);
186 g_free (d);
187 });
188 }
189
190 g_free (part_id);
191 g_object_unref (part_list);
192 g_object_unref (formatter);
193
194 stream = g_memory_input_stream_new_from_data (
195 (gchar *) ba->data, ba->len, NULL);
196 g_simple_async_result_set_op_res_gpointer (res, stream, NULL);
197 }
198
199 static GInputStream *
200 get_empty_image_stream (gsize *len)
201 {
202 GdkPixbuf *p;
203 gchar *buff;
204 GInputStream *stream;
205
206 p = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, 1, 1);
207 gdk_pixbuf_fill (p, 0x00000000); /* transparent black */
208 gdk_pixbuf_save_to_buffer (p, &buff, len, "png", NULL, NULL);
209
210 stream = g_memory_input_stream_new_from_data (buff, *len, g_free);
211
212 g_object_unref (p);
213
214 return stream;
215 }
216
217 static void
218 handle_contact_photo_request (GSimpleAsyncResult *res,
219 GObject *object,
220 GCancellable *cancellable)
221 {
222 EMailRequest *request = E_MAIL_REQUEST (object);
223 const gchar *email;
224 gchar *photo_name;
225 gboolean only_local_photo;
226 CamelMimePart *photopart;
227 EShell *shell;
228 ESourceRegistry *registry;
229 CamelInternetAddress *cia;
230 CamelDataWrapper *dw;
231 GByteArray *ba;
232 GInputStream *stream = NULL;
233
234 shell = e_shell_get_default ();
235 registry = e_shell_get_registry (shell);
236
237 request->priv->mime_type = g_strdup ("image/*");
238
239 email = g_hash_table_lookup (
240 request->priv->uri_query, "mailaddr");
241 if (!email || !*email) {
242 gsize len;
243
244 stream = get_empty_image_stream (&len);
245 request->priv->content_length = len;
246
247 g_simple_async_result_set_op_res_gpointer (res, stream, NULL);
248 return;
249 }
250
251 photo_name = g_uri_unescape_string (email, NULL);
252 only_local_photo = g_hash_table_lookup_extended (
253 request->priv->uri_query, "only-local-photo",
254 NULL, NULL);
255
256 cia = camel_internet_address_new ();
257 camel_address_decode ((CamelAddress *) cia, (const gchar *) photo_name);
258 photopart = em_utils_contact_photo (
259 registry, cia, only_local_photo, cancellable);
260 g_object_unref (cia);
261 if (!photopart) {
262 gsize len;
263
264 stream = get_empty_image_stream (&len);
265 request->priv->content_length = len;
266
267 g_simple_async_result_set_op_res_gpointer (res, stream, NULL);
268 g_free (photo_name);
269 return;
270 }
271
272 ba = NULL;
273 dw = camel_medium_get_content (CAMEL_MEDIUM (photopart));
274 if (dw) {
275 ba = camel_data_wrapper_get_byte_array (dw);
276 }
277
278 if (!ba || ba->len == 0) {
279
280 const gchar *filename = camel_mime_part_get_filename (photopart);
281
282 if (filename && *filename &&
283 g_file_test (filename, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_REGULAR)) {
284 gchar *data;
285 gsize len;
286
287 if (!g_file_get_contents (filename, &data, &len, NULL)) {
288 stream = get_empty_image_stream (&len);
289 } else {
290 stream = g_memory_input_stream_new_from_data (
291 (gchar *) data, len, g_free);
292 }
293
294 request->priv->content_length = len;
295 }
296
297 } else {
298
299 stream = g_memory_input_stream_new_from_data (
300 (gchar *) ba->data, ba->len, NULL);
301
302 request->priv->content_length = ba->len;
303
304 }
305
306 g_free (photo_name);
307 g_simple_async_result_set_op_res_gpointer (res, stream, NULL);
308 }
309
310 static void
311 mail_request_finalize (GObject *object)
312 {
313 EMailRequest *request = E_MAIL_REQUEST (object);
314
315 g_clear_object (&request->priv->output_stream);
316
317 g_free (request->priv->mime_type);
318 request->priv->mime_type = NULL;
319
320 if (request->priv->uri_query) {
321 g_hash_table_destroy (request->priv->uri_query);
322 request->priv->uri_query = NULL;
323 }
324
325 g_free (request->priv->ret_mime_type);
326 request->priv->ret_mime_type = NULL;
327
328 g_free (request->priv->uri_base);
329 request->priv->uri_base = NULL;
330
331 g_free (request->priv->full_uri);
332 request->priv->full_uri = NULL;
333
334 G_OBJECT_CLASS (e_mail_request_parent_class)->finalize (object);
335 }
336
337 static gboolean
338 mail_request_check_uri (SoupRequest *request,
339 SoupURI *uri,
340 GError **error)
341 {
342 return (strcmp (uri->scheme, "mail") == 0);
343 }
344
345 static void
346 mail_request_send_async (SoupRequest *request,
347 GCancellable *cancellable,
348 GAsyncReadyCallback callback,
349 gpointer user_data)
350 {
351 EMailRequest *emr = E_MAIL_REQUEST (request);
352 GSimpleAsyncResult *simple;
353 SoupURI *uri;
354 gchar *uri_str;
355
356 uri = soup_request_get_uri (request);
357
358 d (printf ("received request for %s\n", soup_uri_to_string (uri, FALSE)));
359
360 if (uri->query) {
361 emr->priv->uri_query = soup_form_decode (uri->query);
362 } else {
363 emr->priv->uri_query = NULL;
364 }
365
366 emr->priv->full_uri = soup_uri_to_string (uri, FALSE);
367 uri_str = g_strdup_printf (
368 "%s://%s%s", uri->scheme, uri->host, uri->path);
369 emr->priv->uri_base = uri_str;
370
371 simple = g_simple_async_result_new (
372 G_OBJECT (request), callback,
373 user_data, mail_request_send_async);
374
375 g_simple_async_result_set_check_cancellable (simple, cancellable);
376
377 if (g_strcmp0 (uri->host, "contact-photo") == 0) {
378 g_simple_async_result_run_in_thread (
379 simple, handle_contact_photo_request,
380 G_PRIORITY_DEFAULT, cancellable);
381 } else {
382 g_simple_async_result_run_in_thread (
383 simple, handle_mail_request,
384 G_PRIORITY_DEFAULT, cancellable);
385 }
386
387 g_object_unref (simple);
388 }
389
390 static GInputStream *
391 mail_request_send_finish (SoupRequest *request,
392 GAsyncResult *result,
393 GError **error)
394 {
395 GInputStream *stream;
396
397 stream = g_simple_async_result_get_op_res_gpointer (
398 G_SIMPLE_ASYNC_RESULT (result));
399
400 /* Reset the stream before passing it back to webkit */
401 if (G_IS_INPUT_STREAM (stream) && G_IS_SEEKABLE (stream))
402 g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, NULL);
403
404 if (!stream) /* We must always return something */
405 stream = g_memory_input_stream_new ();
406
407 return stream;
408 }
409
410 static goffset
411 mail_request_get_content_length (SoupRequest *request)
412 {
413 EMailRequest *emr = E_MAIL_REQUEST (request);
414 GByteArray *ba;
415 gint content_length = 0;
416
417 if (emr->priv->content_length > 0)
418 content_length = emr->priv->content_length;
419 else if (emr->priv->output_stream) {
420 ba = camel_stream_mem_get_byte_array (CAMEL_STREAM_MEM (emr->priv->output_stream));
421 if (ba) {
422 content_length = ba->len;
423 }
424 }
425
426 d (printf ("Content-Length: %d bytes\n", content_length));
427 return content_length;
428 }
429
430 static const gchar *
431 mail_request_get_content_type (SoupRequest *request)
432 {
433 EMailRequest *emr = E_MAIL_REQUEST (request);
434 gchar *mime_type;
435
436 if (emr->priv->mime_type) {
437 mime_type = g_strdup (emr->priv->mime_type);
438 } else {
439 mime_type = g_strdup ("text/html");
440 }
441
442 if (g_strcmp0 (mime_type, "text/html") == 0) {
443 emr->priv->ret_mime_type = g_strconcat (mime_type, "; charset=\"UTF-8\"", NULL);
444 g_free (mime_type);
445 } else {
446 emr->priv->ret_mime_type = mime_type;
447 }
448
449 d (printf ("Content-Type: %s\n", emr->priv->ret_mime_type));
450
451 return emr->priv->ret_mime_type;
452 }
453
454 static const gchar *data_schemes[] = { "mail", NULL };
455
456 static void
457 e_mail_request_class_init (EMailRequestClass *class)
458 {
459 GObjectClass *object_class;
460 SoupRequestClass *request_class;
461
462 g_type_class_add_private (class, sizeof (EMailRequestPrivate));
463
464 object_class = G_OBJECT_CLASS (class);
465 object_class->finalize = mail_request_finalize;
466
467 request_class = SOUP_REQUEST_CLASS (class);
468 request_class->schemes = data_schemes;
469 request_class->send_async = mail_request_send_async;
470 request_class->send_finish = mail_request_send_finish;
471 request_class->get_content_type = mail_request_get_content_type;
472 request_class->get_content_length = mail_request_get_content_length;
473 request_class->check_uri = mail_request_check_uri;
474 }
475
476 static void
477 e_mail_request_init (EMailRequest *request)
478 {
479 request->priv = E_MAIL_REQUEST_GET_PRIVATE (request);
480 }