* lib: cleanup header fields a bit more; fixes #209

This commit is contained in:
djcb
2013-05-15 00:08:38 +03:00
parent 24fa47dbef
commit f787f3b9ee
2 changed files with 35 additions and 24 deletions

View File

@ -592,28 +592,34 @@ get_tags (MuMsgFile *self)
}
/* wrongly encoded messages may cause GMime to return invalid
* UTF8... we double check, and ensure our output is always correct
* utf8 */
gchar *
maybe_cleanup (const char* str, const char *path, gboolean *do_free)
static char*
cleanup_maybe (const char *str, gboolean *do_free)
{
if (!str || G_LIKELY(g_utf8_validate(str, -1, NULL)))
return (char*)str;
char *cur, *s;
g_debug ("invalid utf8 in %s", path);
if (!str)
return NULL;
if (*do_free)
return mu_str_asciify_in_place ((char*)str);
else {
gchar *ascii;
ascii = mu_str_asciify_in_place(g_strdup (str));
*do_free = TRUE;
return ascii;
}
if (!g_utf8_validate(str, -1, NULL)) {
if (*do_free)
s = mu_str_asciify_in_place ((char*)str);
else {
*do_free = TRUE;
s = mu_str_asciify_in_place(g_strdup (str));
}
} else
s = (char*)str;
/* strip control chars */
for (cur = s; *cur; ++cur)
if (iscntrl(*cur))
*cur = ' ';
return s;
}
G_GNUC_CONST static GMimeRecipientType
recipient_type (MuMsgFieldId mfid)
{
@ -659,9 +665,8 @@ mu_msg_file_get_str_field (MuMsgFile *self, MuMsgFieldId mfid,
return get_recipient (self, recipient_type(mfid));
case MU_MSG_FIELD_ID_FROM:
return (char*)maybe_cleanup
(g_mime_message_get_sender (self->_mime_msg),
self->_path, do_free);
return (char*)cleanup_maybe
(g_mime_message_get_sender (self->_mime_msg), do_free);
case MU_MSG_FIELD_ID_PATH: return self->_path;
@ -670,9 +675,8 @@ mu_msg_file_get_str_field (MuMsgFile *self, MuMsgFieldId mfid,
return (char*)get_mailing_list (self);
case MU_MSG_FIELD_ID_SUBJECT:
return (char*)maybe_cleanup
(g_mime_message_get_subject (self->_mime_msg),
self->_path, do_free);
return (char*)cleanup_maybe
(g_mime_message_get_subject (self->_mime_msg), do_free);
case MU_MSG_FIELD_ID_MSGID:
return get_msgid (self, do_free);