1 /*
2  * e-mail-parser-message-deliverystatus.c
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #include "evolution-config.h"
19 
20 #include <string.h>
21 
22 #include <e-util/e-util.h>
23 
24 #include "e-mail-part-attachment.h"
25 #include "e-mail-part-headers.h"
26 #include "e-mail-parser-extension.h"
27 
28 typedef EMailParserExtension EMailParserMessageDeliveryStatus;
29 typedef EMailParserExtensionClass EMailParserMessageDeliveryStatusClass;
30 
31 GType e_mail_parser_message_delivery_status_get_type (void);
32 
33 G_DEFINE_TYPE (
34 	EMailParserMessageDeliveryStatus,
35 	e_mail_parser_message_delivery_status,
36 	E_TYPE_MAIL_PARSER_EXTENSION)
37 
38 static const gchar *parser_mime_types[] = {
39 	"message/delivery-status",
40 	"message/feedback-report",
41 	"message/disposition-notification",
42 	"text/rfc822-headers",
43 	NULL
44 };
45 
46 static gboolean
empe_msg_deliverystatus_parse(EMailParserExtension * extension,EMailParser * parser,CamelMimePart * part,GString * part_id,GCancellable * cancellable,GQueue * out_mail_parts)47 empe_msg_deliverystatus_parse (EMailParserExtension *extension,
48                                EMailParser *parser,
49                                CamelMimePart *part,
50                                GString *part_id,
51                                GCancellable *cancellable,
52                                GQueue *out_mail_parts)
53 {
54 	GQueue work_queue = G_QUEUE_INIT;
55 	CamelContentType *ct;
56 	EMailPart *mail_part = NULL;
57 	gboolean show_inline;
58 	gsize len;
59 
60 	ct = camel_mime_part_get_content_type (part);
61 	show_inline = ct && camel_content_type_is (ct, "message", "feedback-report");
62 
63 	len = part_id->len;
64 	g_string_append (part_id, ".delivery-status");
65 
66 	if (ct && camel_content_type_is (ct, "text", "rfc822-headers")) {
67 		CamelMimePart *replace_part;
68 		CamelMimeParser *parser;
69 		CamelStream *stream;
70 		gboolean success;
71 
72 		show_inline = TRUE;
73 
74 		stream = camel_stream_mem_new ();
75 		parser = camel_mime_parser_new ();
76 		replace_part = camel_mime_part_new ();
77 
78 		success = camel_data_wrapper_decode_to_stream_sync (camel_medium_get_content (CAMEL_MEDIUM (part)), stream, cancellable, NULL);
79 		if (success) {
80 			g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, cancellable, NULL);
81 
82 			success = camel_mime_parser_init_with_stream (parser, stream, NULL) != -1;
83 		}
84 
85 		success = success && camel_mime_part_construct_from_parser_sync (replace_part, parser, cancellable, NULL);
86 
87 		if (success) {
88 			const CamelNameValueArray *headers;
89 
90 			headers = camel_medium_get_headers (CAMEL_MEDIUM (replace_part));
91 			success = camel_name_value_array_get_length (headers) > 0;
92 		}
93 
94 		if (success) {
95 			mail_part = e_mail_part_headers_new (replace_part, part_id->str);
96 			e_mail_part_set_mime_type (mail_part, "text/rfc822-headers");
97 		}
98 
99 		g_object_unref (replace_part);
100 		g_object_unref (parser);
101 		g_object_unref (stream);
102 	}
103 
104 	if (!mail_part) {
105 		mail_part = e_mail_part_new (part, part_id->str);
106 		e_mail_part_set_mime_type (mail_part, "text/plain");
107 	}
108 
109 	g_string_truncate (part_id, len);
110 
111 	g_queue_push_tail (&work_queue, mail_part);
112 
113 	/* The only reason for having a separate parser for
114 	 * message/delivery-status is to display the part as an attachment */
115 	e_mail_parser_wrap_as_attachment (parser, part, part_id, &work_queue);
116 
117 	if (!show_inline) {
118 		GSettings *settings;
119 
120 		settings = e_util_ref_settings ("org.gnome.evolution.mail");
121 		show_inline = g_settings_get_boolean (settings, "display-delivery-notification-inline");
122 		g_object_unref (settings);
123 	}
124 
125 	if (show_inline) {
126 		EMailPart *attachment_part;
127 
128 		attachment_part = g_queue_peek_head (&work_queue);
129 		if (attachment_part && E_IS_MAIL_PART_ATTACHMENT (attachment_part))
130 			attachment_part->force_inline = TRUE;
131 	}
132 
133 	e_queue_transfer (&work_queue, out_mail_parts);
134 
135 	return TRUE;
136 }
137 
138 static void
e_mail_parser_message_delivery_status_class_init(EMailParserExtensionClass * class)139 e_mail_parser_message_delivery_status_class_init (EMailParserExtensionClass *class)
140 {
141 	class->mime_types = parser_mime_types;
142 	class->priority = G_PRIORITY_LOW;
143 	class->parse = empe_msg_deliverystatus_parse;
144 }
145 
146 static void
e_mail_parser_message_delivery_status_init(EMailParserExtension * extension)147 e_mail_parser_message_delivery_status_init (EMailParserExtension *extension)
148 {
149 }
150