1 /*
2  * e-mail-formatter-quote-message-rfc822.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 #include <glib/gi18n-lib.h>
22 
23 #include <camel/camel.h>
24 
25 #include <e-util/e-util.h>
26 
27 #include "e-mail-formatter-quote.h"
28 #include "e-mail-part-list.h"
29 #include "e-mail-part-utils.h"
30 
31 typedef EMailFormatterExtension EMailFormatterQuoteMessageRFC822;
32 typedef EMailFormatterExtensionClass EMailFormatterQuoteMessageRFC822Class;
33 
34 GType e_mail_formatter_quote_message_rfc822_get_type (void);
35 
36 G_DEFINE_TYPE (
37 	EMailFormatterQuoteMessageRFC822,
38 	e_mail_formatter_quote_message_rfc822,
39 	E_TYPE_MAIL_FORMATTER_QUOTE_EXTENSION)
40 
41 static const gchar *formatter_mime_types[] = {
42 	"message/rfc822",
43 	"application/vnd.evolution.rfc822.end",
44 	NULL
45 };
46 
47 static gboolean
emfqe_message_rfc822_format(EMailFormatterExtension * extension,EMailFormatter * formatter,EMailFormatterContext * context,EMailPart * part,GOutputStream * stream,GCancellable * cancellable)48 emfqe_message_rfc822_format (EMailFormatterExtension *extension,
49                              EMailFormatter *formatter,
50                              EMailFormatterContext *context,
51                              EMailPart *part,
52                              GOutputStream *stream,
53                              GCancellable *cancellable)
54 {
55 	GQueue queue = G_QUEUE_INIT;
56 	GList *head, *link;
57 	gchar *header, *end;
58 	EMailFormatterQuoteContext *qc = (EMailFormatterQuoteContext *) context;
59 	const gchar *part_id;
60 	const gchar *string;
61 
62 	part_id = e_mail_part_get_id (part);
63 
64 	if (g_cancellable_is_cancelled (cancellable))
65 		return FALSE;
66 
67 	header = e_mail_formatter_get_html_header (formatter);
68 	g_output_stream_write_all (
69 		stream, header, strlen (header), NULL, cancellable, NULL);
70 	g_free (header);
71 
72 	e_mail_part_list_queue_parts (context->part_list, part_id, &queue);
73 
74 	if (g_queue_is_empty (&queue))
75 		return FALSE;
76 
77 	/* Discard the first EMailPart. */
78 	g_object_unref (g_queue_pop_head (&queue));
79 
80 	head = g_queue_peek_head (&queue);
81 
82 	end = g_strconcat (part_id, ".end", NULL);
83 
84 	for (link = head; link != NULL; link = g_list_next (link)) {
85 		EMailPart *p = link->data;
86 		const gchar *p_id;
87 
88 		p_id = e_mail_part_get_id (p);
89 
90 		if (e_mail_part_id_has_suffix (p, ".headers.")) {
91 			if (qc->qf_flags & E_MAIL_FORMATTER_QUOTE_FLAG_HEADERS) {
92 				e_mail_formatter_format_as (
93 					formatter, context, part, stream,
94 					"application/vnd.evolution.headers",
95 					cancellable);
96 			}
97 
98 			continue;
99 		}
100 
101 		/* Check for nested rfc822 messages */
102 		if (e_mail_part_id_has_suffix (p, ".rfc822")) {
103 			gchar *sub_end = g_strconcat (p_id, ".end", NULL);
104 
105 			while (link != NULL) {
106 				p = link->data;
107 
108 				p_id = e_mail_part_get_id (p);
109 
110 				if (g_strcmp0 (p_id, sub_end) == 0)
111 					break;
112 
113 				link = g_list_next (link);
114 			}
115 			g_free (sub_end);
116 			continue;
117 		}
118 
119 		if ((g_strcmp0 (p_id, end) == 0))
120 			break;
121 
122 		if (p->is_hidden)
123 			continue;
124 
125 		e_mail_formatter_format_as (
126 			formatter, context, p,
127 			stream, NULL, cancellable);
128 	}
129 
130 	g_free (end);
131 
132 	while (!g_queue_is_empty (&queue))
133 		g_object_unref (g_queue_pop_head (&queue));
134 
135 	string = "</body></html>";
136 	g_output_stream_write_all (
137 		stream, string, strlen (string), NULL, cancellable, NULL);
138 
139 	return TRUE;
140 }
141 
142 static void
e_mail_formatter_quote_message_rfc822_class_init(EMailFormatterExtensionClass * class)143 e_mail_formatter_quote_message_rfc822_class_init (EMailFormatterExtensionClass *class)
144 {
145 	class->mime_types = formatter_mime_types;
146 	class->priority = G_PRIORITY_HIGH;
147 	class->format = emfqe_message_rfc822_format;
148 }
149 
150 static void
e_mail_formatter_quote_message_rfc822_init(EMailFormatterExtension * extension)151 e_mail_formatter_quote_message_rfc822_init (EMailFormatterExtension *extension)
152 {
153 }
154