From 51fe7fcae8a3e723b66419555998b47eaa2813c1 Mon Sep 17 00:00:00 2001 From: djcb Date: Thu, 9 Aug 2012 11:45:04 +0300 Subject: [PATCH] * lib: refactor attachment checking --- lib/mu-msg-file.c | 187 +++++----------------------------------------- lib/mu-msg-part.c | 14 +++- lib/mu-msg-part.h | 4 +- lib/mu-msg-priv.h | 13 ---- 4 files changed, 31 insertions(+), 187 deletions(-) diff --git a/lib/mu-msg-file.c b/lib/mu-msg-file.c index fbce60d9..4629dbf7 100644 --- a/lib/mu-msg-file.c +++ b/lib/mu-msg-file.c @@ -207,43 +207,28 @@ get_recipient (MuMsgFile *self, GMimeRecipientType rtype) static gboolean looks_like_attachment (GMimeObject *part) { - const char *str; GMimeContentDisposition *disp; - GMimeContentType *ct; + GMimeContentType *ctype; + + const char *dispstr; + + disp = g_mime_object_get_content_disposition (part); - disp = g_mime_object_get_content_disposition (GMIME_OBJECT(part)); if (!GMIME_IS_CONTENT_DISPOSITION(disp)) return FALSE; - str = g_mime_content_disposition_get_disposition (disp); - if (!str) - return FALSE; + dispstr = g_mime_content_disposition_get_disposition (disp); - ct = g_mime_object_get_content_type (part); - if (!ct) - return FALSE; /* ignore this part... */ - - /* note, some mailers use ATTACHMENT, INLINE instead of their - * more common lower-case counterparts */ - if (g_ascii_strcasecmp(str, GMIME_DISPOSITION_ATTACHMENT) == 0) + if (g_ascii_strcasecmp (dispstr, "attachment") == 0) return TRUE; - if (g_ascii_strcasecmp(str, GMIME_DISPOSITION_INLINE) == 0) { - /* some inline parts are also considered attachments... */ - int i; - const char* att_types[][2] = { - {"image", "*"}, - {"application", "*"}, - {"message", "*"}}; + /* we also consider images, attachment or inline, to be + * attachments... */ + ctype = g_mime_object_get_content_type (part); + if (g_mime_content_type_is_type (ctype, "image", "*")) + return TRUE; - for (i = 0; i != G_N_ELEMENTS (att_types); ++i) - if (g_mime_content_type_is_type (ct, - att_types[i][0], - att_types[i][1])) - return TRUE; /* looks like an attachment */ - } - - return FALSE; /* does not look like an attachment */ + return FALSE; } @@ -262,7 +247,10 @@ msg_cflags_cb (GMimeObject *parent, GMimeObject *part, MuFlags *flags) if (!GMIME_IS_PART(part)) return; - if (!(*flags & MU_FLAG_HAS_ATTACH) && looks_like_attachment(part)) + if (*flags & MU_FLAG_HAS_ATTACH) + return; + + if (looks_like_attachment (part)) *flags |= MU_FLAG_HAS_ATTACH; } @@ -361,38 +349,6 @@ get_prio (MuMsgFile *self) } -struct _GetBodyData { GMimeObject *_txt_part, *_html_part;}; -typedef struct _GetBodyData GetBodyData; - -static void -get_body_cb (GMimeObject *parent, GMimeObject *part, GetBodyData *data) -{ - GMimeContentType *ct; - - /* already found what we're looking for? */ - if (data->_html_part && data->_txt_part) - return; - - ct = g_mime_object_get_content_type (part); - if (!GMIME_IS_CONTENT_TYPE(ct)) { - g_warning ("not a content type!"); - return; - } - - if (looks_like_attachment (part)) - return; /* not the body */ - - /* is it right content type? */ - if (!data->_txt_part && - g_mime_content_type_is_type (ct, "text", "plain")) - data->_txt_part = part; - else if (!data->_html_part && - g_mime_content_type_is_type (ct, "text", "html")) - data->_html_part = part; -} - - - /* NOTE: buffer will be *freed* or returned unchanged */ static char* convert_to_utf8 (GMimePart *part, char *buffer) @@ -507,112 +463,6 @@ cleanup: } -GMimePart* -mu_msg_mime_get_body_part (GMimeMessage *msg, gboolean decrypt, - gboolean want_html) -{ - GetBodyData data; - - g_return_val_if_fail (GMIME_IS_MESSAGE(msg), NULL); - - memset (&data, 0, sizeof(GetBodyData)); - mu_mime_message_foreach (msg, decrypt, - (GMimeObjectForeachFunc)get_body_cb, - &data); - if (want_html) - return (GMimePart*)data._html_part; - else - return (GMimePart*)data._txt_part; -} - - - -/* static char* */ -/* get_body (MuMsgFile *self, gboolean decrypt, gboolean want_html) */ -/* { */ -/* GMimePart *part; */ -/* gboolean err; */ -/* gchar *str; */ - -/* g_return_val_if_fail (self, NULL); */ -/* g_return_val_if_fail (GMIME_IS_MESSAGE(self->_mime_msg), NULL); */ - -/* part = mu_msg_mime_get_body_part (self->_mime_msg, */ -/* decrypt, want_html); */ -/* if (!GMIME_IS_PART(part)) */ -/* return NULL; */ - -/* err = FALSE; */ -/* str = mu_msg_mime_part_to_string (part, &err); */ - -/* /\* note, str may be NULL (no body), but that's not necessarily */ -/* * an error; we only warn when an actual error occured *\/ */ -/* if (err) */ -/* g_warning ("error occured while retrieving %s body " */ -/* "for message %s", */ -/* want_html ? "html" : "text", self->_path); */ -/* return str; */ -/* } */ - - -/* static void */ -/* append_text (GMimeObject *parent, GMimeObject *part, gchar **txt) */ -/* { */ -/* GMimeContentType *ct; */ -/* GMimeContentDisposition *disp; */ -/* gchar *parttxt, *tmp; */ -/* gboolean err; */ - -/* if (!GMIME_IS_PART(part)) */ -/* return; */ - -/* ct = g_mime_object_get_content_type (part); */ -/* if (!GMIME_IS_CONTENT_TYPE(ct) || */ -/* !g_mime_content_type_is_type (ct, "text", "plain")) */ -/* return; /\* not a text-plain part *\/ */ - -/* disp = g_mime_object_get_content_disposition (part); */ -/* if (GMIME_IS_CONTENT_DISPOSITION(disp) && */ -/* g_strcmp0 (g_mime_content_disposition_get_disposition (disp), */ -/* GMIME_DISPOSITION_ATTACHMENT) == 0) */ -/* return; /\* it's an attachment, don't include *\/ */ - -/* parttxt = mu_msg_mime_part_to_string (GMIME_PART(part), &err); */ -/* if (err) { */ -/* /\* this happens for broken messages *\/ */ -/* g_debug ("%s: could not get text for part", __FUNCTION__); */ -/* return; */ -/* } */ - -/* /\* it's a text part -- append it! *\/ */ -/* tmp = *txt; */ -/* if (*txt) { */ -/* *txt = g_strconcat (*txt, parttxt, NULL); */ -/* g_free (parttxt); */ -/* } else */ -/* *txt = parttxt; */ - -/* g_free (tmp); */ -/* } */ - -/* instead of just the body, this function returns a concatenation of - * all text/plain parts with inline disposition - */ -/* static char* */ -/* get_concatenated_text (MuMsgFile *self, gboolean decrypt) */ -/* { */ -/* char *txt; */ - -/* g_return_val_if_fail (self, NULL); */ -/* g_return_val_if_fail (GMIME_IS_MESSAGE(self->_mime_msg), NULL); */ - -/* txt = NULL; */ -/* mu_mime_message_foreach (self->_mime_msg, decrypt, */ -/* (GMimeObjectForeachFunc)append_text, */ -/* &txt); */ -/* return txt; */ -/* } */ - static gboolean contains (GSList *lst, const char *str) @@ -644,7 +494,8 @@ get_references (MuMsgFile *self) mime_refs = g_mime_references_decode (str); g_free (str); - for (cur = mime_refs; cur; cur = g_mime_references_get_next(cur)) { + for (cur = mime_refs; cur; + cur = g_mime_references_get_next(cur)) { const char* msgid; msgid = g_mime_references_get_message_id (cur); /* don't include duplicates */ diff --git a/lib/mu-msg-part.c b/lib/mu-msg-part.c index a93b21ca..e40f5a5c 100644 --- a/lib/mu-msg-part.c +++ b/lib/mu-msg-part.c @@ -821,12 +821,20 @@ mu_msg_find_files (MuMsg *msg, MuMsgOptions opts, const GRegex *pattern) gboolean -mu_msg_part_looks_like_attachment (MuMsgPart *part, gboolean include_inline) +mu_msg_part_maybe_attachment (MuMsgPart *part) { g_return_val_if_fail (part, FALSE); - if (!include_inline && (part->part_type & MU_MSG_PART_TYPE_INLINE)) + /* attachments must be leaf parts */ + if (!part->part_type && MU_MSG_PART_TYPE_LEAF) return FALSE; - return TRUE; + /* non-textual inline parts are considered attachments as + * well */ + if (part->part_type & MU_MSG_PART_TYPE_INLINE && + !(part->part_type & MU_MSG_PART_TYPE_TEXT_PLAIN) && + !(part->part_type & MU_MSG_PART_TYPE_TEXT_HTML)) + return TRUE; + + return FALSE; } diff --git a/lib/mu-msg-part.h b/lib/mu-msg-part.h index 25532174..db8f8ae8 100644 --- a/lib/mu-msg-part.h +++ b/lib/mu-msg-part.h @@ -105,12 +105,10 @@ char* mu_msg_part_get_text (MuMsg *msg, MuMsgPart *part, MuMsgOptions opts, * does this msg part look like an attachment? * * @param part a message part - * @param include_inline consider 'inline' parts also as attachments * * @return TRUE if it looks like an attachment, FALSE otherwise */ -gboolean mu_msg_part_looks_like_attachment (MuMsgPart *part, - gboolean include_inline); +gboolean mu_msg_part_maybe_attachment (MuMsgPart *part); /** diff --git a/lib/mu-msg-priv.h b/lib/mu-msg-priv.h index 579844f1..59b19e45 100644 --- a/lib/mu-msg-priv.h +++ b/lib/mu-msg-priv.h @@ -92,19 +92,6 @@ gchar* mu_msg_mime_part_to_string (GMimePart *part, gboolean *err); -/** - * get the MIME part that's probably the body of the message (heuristic) - * - * @param self a MuMsg - * @param decrypt whether decryption should be attempted, if needed - * @param want_html whether it should be a html type of body - * - * @return the MIME part, or NULL in case of error. - */ -GMimePart* mu_msg_mime_get_body_part (GMimeMessage *msg, gboolean decrypt, - gboolean want_html); - - /** * Like g_mime_message_foreach, but will recurse into encrypted parts * if @param decrypt is TRUE and mu was built with crypto support