1 /*
2  * e-mail-part.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 /**
19  * EMailPart:
20  *
21  * The #EMailPart is a wrapper around #CamelMimePart which holds additional
22  * information about the mime part, like it's ID, encryption type etc.
23  *
24  * Each #EMailPart must have a unique ID. The ID is a dot-separated
25  * hierarchical description of the location of the part within the email
26  * message.
27  */
28 
29 #include "evolution-config.h"
30 
31 #include "e-mail-part.h"
32 
33 #include <string.h>
34 
35 #include "e-mail-part-attachment.h"
36 #include "e-mail-part-list.h"
37 
38 #define E_MAIL_PART_GET_PRIVATE(obj) \
39 	(G_TYPE_INSTANCE_GET_PRIVATE \
40 	((obj), E_TYPE_MAIL_PART, EMailPartPrivate))
41 
42 struct _EMailPartPrivate {
43 	GWeakRef part_list;
44 	CamelMimePart *mime_part;
45 
46 	gchar *id;
47 	gchar *cid;
48 	gchar *mime_type;
49 
50 	gboolean is_attachment;
51 	gboolean is_printable;
52 	gboolean converted_to_utf8;
53 };
54 
55 enum {
56 	PROP_0,
57 	PROP_CID,
58 	PROP_CONVERTED_TO_UTF8,
59 	PROP_ID,
60 	PROP_IS_ATTACHMENT,
61 	PROP_IS_PRINTABLE,
62 	PROP_MIME_PART,
63 	PROP_MIME_TYPE,
64 	PROP_PART_LIST
65 };
66 
G_DEFINE_TYPE_WITH_CODE(EMailPart,e_mail_part,G_TYPE_OBJECT,G_IMPLEMENT_INTERFACE (E_TYPE_EXTENSIBLE,NULL))67 G_DEFINE_TYPE_WITH_CODE (
68 	EMailPart,
69 	e_mail_part,
70 	G_TYPE_OBJECT,
71 	G_IMPLEMENT_INTERFACE (
72 		E_TYPE_EXTENSIBLE, NULL))
73 
74 static void
75 mail_part_validity_pair_free (gpointer ptr)
76 {
77 	EMailPartValidityPair *pair = ptr;
78 
79 	if (!pair)
80 		return;
81 
82 	camel_cipher_validity_free (pair->validity);
83 	g_free (pair);
84 }
85 
86 static void
mail_part_set_id(EMailPart * part,const gchar * id)87 mail_part_set_id (EMailPart *part,
88                   const gchar *id)
89 {
90 	g_return_if_fail (part->priv->id == NULL);
91 
92 	part->priv->id = g_strdup (id);
93 }
94 
95 static void
mail_part_set_mime_part(EMailPart * part,CamelMimePart * mime_part)96 mail_part_set_mime_part (EMailPart *part,
97                          CamelMimePart *mime_part)
98 {
99 	g_return_if_fail (part->priv->mime_part == NULL);
100 
101 	/* The CamelMimePart is optional. */
102 	if (mime_part != NULL)
103 		part->priv->mime_part = g_object_ref (mime_part);
104 }
105 
106 static void
mail_part_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)107 mail_part_set_property (GObject *object,
108                         guint property_id,
109                         const GValue *value,
110                         GParamSpec *pspec)
111 {
112 	switch (property_id) {
113 		case PROP_CID:
114 			e_mail_part_set_cid (
115 				E_MAIL_PART (object),
116 				g_value_get_string (value));
117 			return;
118 
119 		case PROP_CONVERTED_TO_UTF8:
120 			e_mail_part_set_converted_to_utf8 (
121 				E_MAIL_PART (object),
122 				g_value_get_boolean (value));
123 			return;
124 
125 		case PROP_ID:
126 			mail_part_set_id (
127 				E_MAIL_PART (object),
128 				g_value_get_string (value));
129 			return;
130 
131 		case PROP_IS_ATTACHMENT:
132 			e_mail_part_set_is_attachment (
133 				E_MAIL_PART (object),
134 				g_value_get_boolean (value));
135 			return;
136 
137 		case PROP_IS_PRINTABLE:
138 			e_mail_part_set_is_printable (
139 				E_MAIL_PART (object),
140 				g_value_get_boolean (value));
141 			return;
142 
143 		case PROP_MIME_PART:
144 			mail_part_set_mime_part (
145 				E_MAIL_PART (object),
146 				g_value_get_object (value));
147 			return;
148 
149 		case PROP_MIME_TYPE:
150 			e_mail_part_set_mime_type (
151 				E_MAIL_PART (object),
152 				g_value_get_string (value));
153 			return;
154 
155 		case PROP_PART_LIST:
156 			e_mail_part_set_part_list (
157 				E_MAIL_PART (object),
158 				g_value_get_object (value));
159 			return;
160 	}
161 
162 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 }
164 
165 static void
mail_part_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)166 mail_part_get_property (GObject *object,
167                         guint property_id,
168                         GValue *value,
169                         GParamSpec *pspec)
170 {
171 	switch (property_id) {
172 		case PROP_CID:
173 			g_value_set_string (
174 				value,
175 				e_mail_part_get_cid (
176 				E_MAIL_PART (object)));
177 			return;
178 
179 		case PROP_CONVERTED_TO_UTF8:
180 			g_value_set_boolean (
181 				value,
182 				e_mail_part_get_converted_to_utf8 (
183 				E_MAIL_PART (object)));
184 			return;
185 
186 		case PROP_ID:
187 			g_value_set_string (
188 				value,
189 				e_mail_part_get_id (
190 				E_MAIL_PART (object)));
191 			return;
192 
193 		case PROP_IS_ATTACHMENT:
194 			g_value_set_boolean (
195 				value,
196 				e_mail_part_get_is_attachment (
197 				E_MAIL_PART (object)));
198 			return;
199 
200 		case PROP_IS_PRINTABLE:
201 			g_value_set_boolean (
202 				value,
203 				e_mail_part_get_is_printable (
204 				E_MAIL_PART (object)));
205 			return;
206 
207 		case PROP_MIME_PART:
208 			g_value_take_object (
209 				value,
210 				e_mail_part_ref_mime_part (
211 				E_MAIL_PART (object)));
212 			return;
213 
214 		case PROP_MIME_TYPE:
215 			g_value_set_string (
216 				value,
217 				e_mail_part_get_mime_type (
218 				E_MAIL_PART (object)));
219 			return;
220 
221 		case PROP_PART_LIST:
222 			g_value_take_object (
223 				value,
224 				e_mail_part_ref_part_list (
225 				E_MAIL_PART (object)));
226 			return;
227 	}
228 
229 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
230 }
231 
232 static void
mail_part_dispose(GObject * object)233 mail_part_dispose (GObject *object)
234 {
235 	EMailPartPrivate *priv;
236 
237 	priv = E_MAIL_PART_GET_PRIVATE (object);
238 
239 	g_weak_ref_set (&priv->part_list, NULL);
240 
241 	g_clear_object (&priv->mime_part);
242 
243 	/* Chain up to parent's dispose() method. */
244 	G_OBJECT_CLASS (e_mail_part_parent_class)->dispose (object);
245 }
246 
247 static void
mail_part_finalize(GObject * object)248 mail_part_finalize (GObject *object)
249 {
250 	EMailPart *part = E_MAIL_PART (object);
251 	EMailPartValidityPair *pair;
252 
253 	g_free (part->priv->id);
254 	g_free (part->priv->cid);
255 	g_free (part->priv->mime_type);
256 
257 	while ((pair = g_queue_pop_head (&part->validities)) != NULL)
258 		mail_part_validity_pair_free (pair);
259 
260 	/* Chain up to parent's finalize() method. */
261 	G_OBJECT_CLASS (e_mail_part_parent_class)->finalize (object);
262 }
263 
264 static void
mail_part_constructed(GObject * object)265 mail_part_constructed (GObject *object)
266 {
267 	/* Chain up to parent's constructed() method. */
268 	G_OBJECT_CLASS (e_mail_part_parent_class)->constructed (object);
269 
270 	e_extensible_load_extensions (E_EXTENSIBLE (object));
271 }
272 
273 static void
e_mail_part_class_init(EMailPartClass * class)274 e_mail_part_class_init (EMailPartClass *class)
275 {
276 	GObjectClass *object_class;
277 
278 	g_type_class_add_private (class, sizeof (EMailPartPrivate));
279 
280 	object_class = G_OBJECT_CLASS (class);
281 	object_class->set_property = mail_part_set_property;
282 	object_class->get_property = mail_part_get_property;
283 	object_class->dispose = mail_part_dispose;
284 	object_class->finalize = mail_part_finalize;
285 	object_class->constructed = mail_part_constructed;
286 
287 	g_object_class_install_property (
288 		object_class,
289 		PROP_CID,
290 		g_param_spec_string (
291 			"cid",
292 			"Content ID",
293 			"The MIME Content-ID",
294 			NULL,
295 			G_PARAM_READWRITE |
296 			G_PARAM_STATIC_STRINGS));
297 
298 	g_object_class_install_property (
299 		object_class,
300 		PROP_CONVERTED_TO_UTF8,
301 		g_param_spec_boolean (
302 			"converted-to-utf8",
303 			"Converted To UTF8",
304 			"Whether the part content was already converted to UTF-8",
305 			FALSE,
306 			G_PARAM_READWRITE |
307 			G_PARAM_STATIC_STRINGS));
308 
309 	g_object_class_install_property (
310 		object_class,
311 		PROP_ID,
312 		g_param_spec_string (
313 			"id",
314 			"Part ID",
315 			"The part ID",
316 			NULL,
317 			G_PARAM_READWRITE |
318 			G_PARAM_CONSTRUCT_ONLY |
319 			G_PARAM_STATIC_STRINGS));
320 
321 	g_object_class_install_property (
322 		object_class,
323 		PROP_IS_ATTACHMENT,
324 		g_param_spec_boolean (
325 			"is-attachment",
326 			"Is Attachment",
327 			"Format the part as an attachment",
328 			FALSE,
329 			G_PARAM_READWRITE |
330 			G_PARAM_CONSTRUCT |
331 			G_PARAM_STATIC_STRINGS));
332 
333 	g_object_class_install_property (
334 		object_class,
335 		PROP_IS_PRINTABLE,
336 		g_param_spec_boolean (
337 			"is-printable",
338 			"Is Printable",
339 			"Whether this part can be printed",
340 			TRUE,
341 			G_PARAM_READWRITE |
342 			G_PARAM_CONSTRUCT |
343 			G_PARAM_STATIC_STRINGS));
344 
345 	g_object_class_install_property (
346 		object_class,
347 		PROP_MIME_PART,
348 		g_param_spec_object (
349 			"mime-part",
350 			"MIME Part",
351 			"The MIME part",
352 			CAMEL_TYPE_MIME_PART,
353 			G_PARAM_READWRITE |
354 			G_PARAM_CONSTRUCT_ONLY |
355 			G_PARAM_STATIC_STRINGS));
356 
357 	g_object_class_install_property (
358 		object_class,
359 		PROP_MIME_TYPE,
360 		g_param_spec_string (
361 			"mime-type",
362 			"MIME Type",
363 			"The MIME type",
364 			NULL,
365 			G_PARAM_READWRITE |
366 			G_PARAM_STATIC_STRINGS));
367 
368 	g_object_class_install_property (
369 		object_class,
370 		PROP_PART_LIST,
371 		g_param_spec_object (
372 			"part-list",
373 			"Part List",
374 			"The part list that owns the part",
375 			E_TYPE_MAIL_PART_LIST,
376 			G_PARAM_READWRITE |
377 			G_PARAM_STATIC_STRINGS));
378 }
379 
380 static void
e_mail_part_init(EMailPart * part)381 e_mail_part_init (EMailPart *part)
382 {
383 	part->priv = E_MAIL_PART_GET_PRIVATE (part);
384 }
385 
386 /**
387  * e_mail_part_new:
388  * @mime_part: (allow-none) a #CamelMimePart or %NULL
389  * @id: part ID
390  *
391  * Creates a new #EMailPart for the given @mime_part.
392  *
393  * Return value: a new #EMailPart
394  */
395 EMailPart *
e_mail_part_new(CamelMimePart * mime_part,const gchar * id)396 e_mail_part_new (CamelMimePart *mime_part,
397                  const gchar *id)
398 {
399 	g_return_val_if_fail (id != NULL, NULL);
400 
401 	return g_object_new (
402 		E_TYPE_MAIL_PART,
403 		"id", id, "mime-part", mime_part, NULL);
404 }
405 
406 const gchar *
e_mail_part_get_id(EMailPart * part)407 e_mail_part_get_id (EMailPart *part)
408 {
409 	g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);
410 
411 	return part->priv->id;
412 }
413 
414 const gchar *
e_mail_part_get_cid(EMailPart * part)415 e_mail_part_get_cid (EMailPart *part)
416 {
417 	g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);
418 
419 	return part->priv->cid;
420 }
421 
422 void
e_mail_part_set_cid(EMailPart * part,const gchar * cid)423 e_mail_part_set_cid (EMailPart *part,
424                      const gchar *cid)
425 {
426 	g_return_if_fail (E_IS_MAIL_PART (part));
427 
428 	g_free (part->priv->cid);
429 	part->priv->cid = g_strdup (cid);
430 
431 	g_object_notify (G_OBJECT (part), "cid");
432 }
433 
434 gboolean
e_mail_part_id_has_prefix(EMailPart * part,const gchar * prefix)435 e_mail_part_id_has_prefix (EMailPart *part,
436                            const gchar *prefix)
437 {
438 	g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
439 	g_return_val_if_fail (prefix != NULL, FALSE);
440 
441 	return g_str_has_prefix (part->priv->id, prefix);
442 }
443 
444 gboolean
e_mail_part_id_has_suffix(EMailPart * part,const gchar * suffix)445 e_mail_part_id_has_suffix (EMailPart *part,
446                            const gchar *suffix)
447 {
448 	g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
449 	g_return_val_if_fail (suffix != NULL, FALSE);
450 
451 	return g_str_has_suffix (part->priv->id, suffix);
452 }
453 
454 gboolean
e_mail_part_id_has_substr(EMailPart * part,const gchar * substr)455 e_mail_part_id_has_substr (EMailPart *part,
456                            const gchar *substr)
457 {
458 	g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
459 	g_return_val_if_fail (substr != NULL, FALSE);
460 
461 	return (strstr (part->priv->id, substr) != NULL);
462 }
463 
464 CamelMimePart *
e_mail_part_ref_mime_part(EMailPart * part)465 e_mail_part_ref_mime_part (EMailPart *part)
466 {
467 	CamelMimePart *mime_part = NULL;
468 
469 	g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);
470 
471 	if (part->priv->mime_part != NULL)
472 		mime_part = g_object_ref (part->priv->mime_part);
473 
474 	return mime_part;
475 }
476 
477 const gchar *
e_mail_part_get_mime_type(EMailPart * part)478 e_mail_part_get_mime_type (EMailPart *part)
479 {
480 	g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);
481 
482 	return part->priv->mime_type;
483 }
484 
485 void
e_mail_part_set_mime_type(EMailPart * part,const gchar * mime_type)486 e_mail_part_set_mime_type (EMailPart *part,
487                            const gchar *mime_type)
488 {
489 	g_return_if_fail (E_IS_MAIL_PART (part));
490 
491 	if (g_strcmp0 (mime_type, part->priv->mime_type) == 0)
492 		return;
493 
494 	g_free (part->priv->mime_type);
495 	part->priv->mime_type = g_strdup (mime_type);
496 
497 	g_object_notify (G_OBJECT (part), "mime-type");
498 }
499 
500 gboolean
e_mail_part_get_converted_to_utf8(EMailPart * part)501 e_mail_part_get_converted_to_utf8 (EMailPart *part)
502 {
503 	g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
504 
505 	return part->priv->converted_to_utf8;
506 }
507 
508 void
e_mail_part_set_converted_to_utf8(EMailPart * part,gboolean converted_to_utf8)509 e_mail_part_set_converted_to_utf8 (EMailPart *part,
510 				   gboolean converted_to_utf8)
511 {
512 	g_return_if_fail (E_IS_MAIL_PART (part));
513 
514 	if (converted_to_utf8 == part->priv->converted_to_utf8)
515 		return;
516 
517 	part->priv->converted_to_utf8 = converted_to_utf8;
518 
519 	g_object_notify (G_OBJECT (part), "converted-to-utf8");
520 }
521 
522 gboolean
e_mail_part_should_show_inline(EMailPart * part)523 e_mail_part_should_show_inline (EMailPart *part)
524 {
525 	CamelMimePart *mime_part;
526 	const CamelContentDisposition *disposition;
527 	gboolean res = FALSE;
528 
529 	g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
530 
531 	/* Automatically expand attachments that have inline
532 	 * disposition or the EMailParts have specific
533 	 * force_inline flag set. */
534 
535 	if (part->force_collapse)
536 		return FALSE;
537 
538 	if (part->force_inline)
539 		return TRUE;
540 
541 	if (E_IS_MAIL_PART_ATTACHMENT (part)) {
542 		EMailPartAttachment *empa = E_MAIL_PART_ATTACHMENT (part);
543 
544 		if (g_strcmp0 (empa->snoop_mime_type, "message/rfc822") == 0)
545 			return TRUE;
546 	}
547 
548 	mime_part = e_mail_part_ref_mime_part (part);
549 	if (!mime_part)
550 		return FALSE;
551 
552 	disposition = camel_mime_part_get_content_disposition (mime_part);
553 	if (disposition && disposition->disposition &&
554 	    g_ascii_strncasecmp (disposition->disposition, "inline", 6) == 0) {
555 		GSettings *settings;
556 
557 		settings = e_util_ref_settings ("org.gnome.evolution.mail");
558 		res = g_settings_get_boolean (settings, "display-content-disposition-inline");
559 		g_clear_object (&settings);
560 	}
561 
562 	g_object_unref (mime_part);
563 
564 	return res;
565 }
566 
567 EMailPartList *
e_mail_part_ref_part_list(EMailPart * part)568 e_mail_part_ref_part_list (EMailPart *part)
569 {
570 	g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);
571 
572 	return g_weak_ref_get (&part->priv->part_list);
573 }
574 
575 void
e_mail_part_set_part_list(EMailPart * part,EMailPartList * part_list)576 e_mail_part_set_part_list (EMailPart *part,
577                            EMailPartList *part_list)
578 {
579 	g_return_if_fail (E_IS_MAIL_PART (part));
580 
581 	if (part_list != NULL)
582 		g_return_if_fail (E_IS_MAIL_PART_LIST (part_list));
583 
584 	g_weak_ref_set (&part->priv->part_list, part_list);
585 
586 	g_object_notify (G_OBJECT (part), "part-list");
587 }
588 
589 gboolean
e_mail_part_get_is_attachment(EMailPart * part)590 e_mail_part_get_is_attachment (EMailPart *part)
591 {
592 	g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
593 
594 	return part->priv->is_attachment;
595 }
596 
597 void
e_mail_part_set_is_attachment(EMailPart * part,gboolean is_attachment)598 e_mail_part_set_is_attachment (EMailPart *part,
599                                gboolean is_attachment)
600 {
601 	g_return_if_fail (E_IS_MAIL_PART (part));
602 
603 	if (is_attachment == part->priv->is_attachment)
604 		return;
605 
606 	part->priv->is_attachment = is_attachment;
607 
608 	g_object_notify (G_OBJECT (part), "is-attachment");
609 }
610 
611 gboolean
e_mail_part_get_is_printable(EMailPart * part)612 e_mail_part_get_is_printable (EMailPart *part)
613 {
614 	g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
615 
616 	return part->priv->is_printable;
617 }
618 
619 void
e_mail_part_set_is_printable(EMailPart * part,gboolean is_printable)620 e_mail_part_set_is_printable (EMailPart *part,
621 			      gboolean is_printable)
622 {
623 	g_return_if_fail (E_IS_MAIL_PART (part));
624 
625 	if ((is_printable ? 1 : 0) == (part->priv->is_printable ? 1 : 0))
626 		return;
627 
628 	part->priv->is_printable = is_printable;
629 
630 	g_object_notify (G_OBJECT (part), "is-printable");
631 }
632 
633 void
e_mail_part_content_loaded(EMailPart * part,EWebView * web_view,const gchar * iframe_id)634 e_mail_part_content_loaded (EMailPart *part,
635 			    EWebView *web_view,
636 			    const gchar *iframe_id)
637 {
638 	EMailPartClass *class;
639 
640 	g_return_if_fail (E_IS_MAIL_PART (part));
641 	g_return_if_fail (E_IS_WEB_VIEW (web_view));
642 
643 	class = E_MAIL_PART_GET_CLASS (part);
644 	g_return_if_fail (class != NULL);
645 
646 	if (class->content_loaded)
647 		class->content_loaded (part, web_view, iframe_id);
648 }
649 
650 static EMailPartValidityPair *
mail_part_find_validity_pair(EMailPart * part,EMailPartValidityFlags validity_type)651 mail_part_find_validity_pair (EMailPart *part,
652                               EMailPartValidityFlags validity_type)
653 {
654 	GList *head, *link;
655 
656 	head = g_queue_peek_head_link (&part->validities);
657 
658 	for (link = head; link != NULL; link = g_list_next (link)) {
659 		EMailPartValidityPair *pair = link->data;
660 
661 		if (pair == NULL)
662 			continue;
663 
664 		if ((pair->validity_type & validity_type) == validity_type)
665 			return pair;
666 	}
667 
668 	return NULL;
669 }
670 
671 /**
672  * e_mail_part_update_validity:
673  * @part: An #EMailPart
674  * @validity: a #CamelCipherValidity
675  * @validity_type: E_MAIL_PART_VALIDITY_* flags
676  *
677  * Updates validity of the @part. When the part already has some validity
678  * set, the new @validity and @validity_type are just appended, preserving
679  * the original validity. Validities of the same type (PGP or S/MIME) are
680  * merged together.
681  */
682 void
e_mail_part_update_validity(EMailPart * part,CamelCipherValidity * validity,EMailPartValidityFlags validity_type)683 e_mail_part_update_validity (EMailPart *part,
684                              CamelCipherValidity *validity,
685                              EMailPartValidityFlags validity_type)
686 {
687 	EMailPartValidityPair *pair;
688 	EMailPartValidityFlags mask;
689 
690 	g_return_if_fail (E_IS_MAIL_PART (part));
691 	g_return_if_fail (validity != NULL);
692 
693 	mask = E_MAIL_PART_VALIDITY_PGP | E_MAIL_PART_VALIDITY_SMIME;
694 
695 	/* Auto-add flags when the related part is present */
696 	if (!(validity_type & E_MAIL_PART_VALIDITY_SIGNED) &&
697 	    validity->sign.status != CAMEL_CIPHER_VALIDITY_SIGN_NONE)
698 		validity_type |= E_MAIL_PART_VALIDITY_SIGNED;
699 
700 	if (!(validity_type & E_MAIL_PART_VALIDITY_ENCRYPTED) &&
701 	    validity->encrypt.status != CAMEL_CIPHER_VALIDITY_ENCRYPT_NONE)
702 		validity_type |= E_MAIL_PART_VALIDITY_ENCRYPTED;
703 
704 	pair = mail_part_find_validity_pair (part, validity_type & mask);
705 	if (pair != NULL) {
706 		pair->validity_type |= validity_type;
707 		camel_cipher_validity_envelope (pair->validity, validity);
708 	} else {
709 		pair = g_new0 (EMailPartValidityPair, 1);
710 		pair->validity_type = validity_type;
711 		pair->validity = camel_cipher_validity_clone (validity);
712 
713 		g_queue_push_tail (&part->validities, pair);
714 	}
715 }
716 
717 /**
718  * e_mail_part_get_validity:
719  * @part: An #EMailPart
720  * @validity_type: E_MAIL_PART_VALIDITY_* flags
721  *
722  * Returns, validity of @part contains any validity with the same bits
723  * as @validity_type set. It should contain all bits of it.
724  *
725  * Returns: a #CamelCipherValidity of the given type, %NULL if not found
726  *
727  * Since: 3.8
728  */
729 CamelCipherValidity *
e_mail_part_get_validity(EMailPart * part,EMailPartValidityFlags validity_type)730 e_mail_part_get_validity (EMailPart *part,
731                           EMailPartValidityFlags validity_type)
732 {
733 	EMailPartValidityPair *pair;
734 
735 	g_return_val_if_fail (E_IS_MAIL_PART (part), NULL);
736 
737 	pair = mail_part_find_validity_pair (part, validity_type);
738 
739 	return (pair != NULL) ? pair->validity : NULL;
740 }
741 
742 gboolean
e_mail_part_has_validity(EMailPart * part)743 e_mail_part_has_validity (EMailPart *part)
744 {
745 	g_return_val_if_fail (E_IS_MAIL_PART (part), FALSE);
746 
747 	return !g_queue_is_empty (&part->validities);
748 }
749 
750 EMailPartValidityFlags
e_mail_part_get_validity_flags(EMailPart * part)751 e_mail_part_get_validity_flags (EMailPart *part)
752 {
753 	EMailPartValidityFlags flags = 0;
754 	GList *head, *link;
755 
756 	g_return_val_if_fail (E_IS_MAIL_PART (part), 0);
757 
758 	head = g_queue_peek_head_link (&part->validities);
759 
760 	for (link = head; link != NULL; link = g_list_next (link)) {
761 		EMailPartValidityPair *pair = link->data;
762 
763 		if (pair != NULL)
764 			flags |= pair->validity_type;
765 	}
766 
767 	return flags;
768 }
769 
770 static gboolean
from_matches_signers_alt_emails(CamelInternetAddress * from_address,CamelCipherCertInfo * cinfo)771 from_matches_signers_alt_emails (CamelInternetAddress *from_address,
772 				 CamelCipherCertInfo *cinfo)
773 {
774 	GSList *props_link;
775 	gboolean matches = FALSE;
776 
777 	for (props_link = cinfo->properties; props_link && !matches; props_link = g_slist_next (props_link)) {
778 		const CamelCipherCertInfoProperty *prop = props_link->data;
779 
780 		if (prop && g_strcmp0 (prop->name, CAMEL_CIPHER_CERT_INFO_PROPERTY_SIGNERS_ALT_EMAILS) == 0 && prop->value) {
781 			CamelInternetAddress *address;
782 			gint count, ii;
783 
784 			address = camel_internet_address_new ();
785 			count = camel_address_unformat (CAMEL_ADDRESS (address), prop->value);
786 			for (ii = 0; ii < count && !matches; ii++) {
787 				const gchar *email = NULL;
788 
789 				if (camel_internet_address_get (address, ii, NULL, &email) && email && *email) {
790 					matches = camel_internet_address_find_address (from_address, email, NULL) >= 0;
791 				}
792 			}
793 			g_object_unref (address);
794 			break;
795 		}
796 	}
797 
798 	return matches;
799 }
800 
801 void
e_mail_part_verify_validity_sender(EMailPart * part,CamelInternetAddress * from_address)802 e_mail_part_verify_validity_sender (EMailPart *part,
803 				    CamelInternetAddress *from_address)
804 {
805 	GList *link;
806 
807 	g_return_if_fail (E_IS_MAIL_PART (part));
808 
809 	if (!from_address)
810 		return;
811 
812 	for (link = g_queue_peek_head_link (&part->validities); link; link = g_list_next (link)) {
813 		EMailPartValidityPair *pair = link->data;
814 
815 		if (pair && pair->validity && !(pair->validity_type & E_MAIL_PART_VALIDITY_VERIFIED)) {
816 			pair->validity_type |= E_MAIL_PART_VALIDITY_VERIFIED;
817 
818 			if (pair->validity->sign.status != CAMEL_CIPHER_VALIDITY_SIGN_NONE) {
819 				GList *link2;
820 				gboolean from_matches_signer = FALSE;
821 
822 				for (link2 = g_queue_peek_head_link (&pair->validity->sign.signers); link2 && !from_matches_signer; link2 = g_list_next (link2)) {
823 					CamelCipherCertInfo *cinfo = link2->data;
824 
825 					if (cinfo->email && *cinfo->email) {
826 						from_matches_signer = from_matches_signer ||
827 							(from_address && camel_internet_address_find_address (from_address, cinfo->email, NULL) >= 0) ||
828 							(from_address && from_matches_signers_alt_emails (from_address, cinfo));
829 					}
830 				}
831 
832 				if (!from_matches_signer)
833 					pair->validity_type |= E_MAIL_PART_VALIDITY_SENDER_SIGNER_MISMATCH;
834 			}
835 		}
836 	}
837 }
838