* lib: refactor attachment checking

This commit is contained in:
djcb
2012-08-09 11:45:04 +03:00
parent ea86b71294
commit 51fe7fcae8
4 changed files with 31 additions and 187 deletions

View File

@ -207,43 +207,28 @@ get_recipient (MuMsgFile *self, GMimeRecipientType rtype)
static gboolean static gboolean
looks_like_attachment (GMimeObject *part) looks_like_attachment (GMimeObject *part)
{ {
const char *str;
GMimeContentDisposition *disp; 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)) if (!GMIME_IS_CONTENT_DISPOSITION(disp))
return FALSE; return FALSE;
str = g_mime_content_disposition_get_disposition (disp); dispstr = g_mime_content_disposition_get_disposition (disp);
if (!str)
return FALSE;
ct = g_mime_object_get_content_type (part); if (g_ascii_strcasecmp (dispstr, "attachment") == 0)
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)
return TRUE; return TRUE;
if (g_ascii_strcasecmp(str, GMIME_DISPOSITION_INLINE) == 0) { /* we also consider images, attachment or inline, to be
/* some inline parts are also considered attachments... */ * attachments... */
int i; ctype = g_mime_object_get_content_type (part);
const char* att_types[][2] = { if (g_mime_content_type_is_type (ctype, "image", "*"))
{"image", "*"}, return TRUE;
{"application", "*"},
{"message", "*"}};
for (i = 0; i != G_N_ELEMENTS (att_types); ++i) return FALSE;
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 */
} }
@ -262,7 +247,10 @@ msg_cflags_cb (GMimeObject *parent, GMimeObject *part, MuFlags *flags)
if (!GMIME_IS_PART(part)) if (!GMIME_IS_PART(part))
return; 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; *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 */ /* NOTE: buffer will be *freed* or returned unchanged */
static char* static char*
convert_to_utf8 (GMimePart *part, char *buffer) 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 static gboolean
contains (GSList *lst, const char *str) contains (GSList *lst, const char *str)
@ -644,7 +494,8 @@ get_references (MuMsgFile *self)
mime_refs = g_mime_references_decode (str); mime_refs = g_mime_references_decode (str);
g_free (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; const char* msgid;
msgid = g_mime_references_get_message_id (cur); msgid = g_mime_references_get_message_id (cur);
/* don't include duplicates */ /* don't include duplicates */

View File

@ -821,12 +821,20 @@ mu_msg_find_files (MuMsg *msg, MuMsgOptions opts, const GRegex *pattern)
gboolean 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); 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 FALSE;
/* 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 TRUE;
return FALSE;
} }

View File

@ -105,12 +105,10 @@ char* mu_msg_part_get_text (MuMsg *msg, MuMsgPart *part, MuMsgOptions opts,
* does this msg part look like an attachment? * does this msg part look like an attachment?
* *
* @param part a message part * @param part a message part
* @param include_inline consider 'inline' parts also as attachments
* *
* @return TRUE if it looks like an attachment, FALSE otherwise * @return TRUE if it looks like an attachment, FALSE otherwise
*/ */
gboolean mu_msg_part_looks_like_attachment (MuMsgPart *part, gboolean mu_msg_part_maybe_attachment (MuMsgPart *part);
gboolean include_inline);
/** /**

View File

@ -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 * Like g_mime_message_foreach, but will recurse into encrypted parts
* if @param decrypt is TRUE and mu was built with crypto support * if @param decrypt is TRUE and mu was built with crypto support