* 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 static char*
* UTF8... we double check, and ensure our output is always correct cleanup_maybe (const char *str, gboolean *do_free)
* utf8 */
gchar *
maybe_cleanup (const char* str, const char *path, gboolean *do_free)
{ {
if (!str || G_LIKELY(g_utf8_validate(str, -1, NULL))) char *cur, *s;
return (char*)str;
g_debug ("invalid utf8 in %s", path); if (!str)
return NULL;
if (*do_free) if (!g_utf8_validate(str, -1, NULL)) {
return mu_str_asciify_in_place ((char*)str); if (*do_free)
else { s = mu_str_asciify_in_place ((char*)str);
gchar *ascii; else {
ascii = mu_str_asciify_in_place(g_strdup (str)); *do_free = TRUE;
*do_free = TRUE; s = mu_str_asciify_in_place(g_strdup (str));
return ascii; }
} } else
s = (char*)str;
/* strip control chars */
for (cur = s; *cur; ++cur)
if (iscntrl(*cur))
*cur = ' ';
return s;
} }
G_GNUC_CONST static GMimeRecipientType G_GNUC_CONST static GMimeRecipientType
recipient_type (MuMsgFieldId mfid) 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)); return get_recipient (self, recipient_type(mfid));
case MU_MSG_FIELD_ID_FROM: case MU_MSG_FIELD_ID_FROM:
return (char*)maybe_cleanup return (char*)cleanup_maybe
(g_mime_message_get_sender (self->_mime_msg), (g_mime_message_get_sender (self->_mime_msg), do_free);
self->_path, do_free);
case MU_MSG_FIELD_ID_PATH: return self->_path; 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); return (char*)get_mailing_list (self);
case MU_MSG_FIELD_ID_SUBJECT: case MU_MSG_FIELD_ID_SUBJECT:
return (char*)maybe_cleanup return (char*)cleanup_maybe
(g_mime_message_get_subject (self->_mime_msg), (g_mime_message_get_subject (self->_mime_msg), do_free);
self->_path, do_free);
case MU_MSG_FIELD_ID_MSGID: case MU_MSG_FIELD_ID_MSGID:
return get_msgid (self, do_free); return get_msgid (self, do_free);

View File

@ -17,6 +17,7 @@
** **
*/ */
#include <string.h> #include <string.h>
#include <ctype.h>
#include "mu-str.h" #include "mu-str.h"
#include "mu-msg.h" #include "mu-msg.h"
@ -49,12 +50,18 @@ append_sexp_attr_list (GString *gstr, const char* elm, const GSList *lst)
static void static void
append_sexp_attr (GString *gstr, const char* elm, const char *str) append_sexp_attr (GString *gstr, const char* elm, const char *str)
{ {
gchar *esc; gchar *esc, *utf8, *cur;
if (!str || strlen(str) == 0) if (!str || strlen(str) == 0)
return; /* empty: don't include */ return; /* empty: don't include */
esc = mu_str_escape_c_literal (str, TRUE); utf8 = mu_str_utf8ify (str);
for (cur = utf8; *cur; ++cur)
if (iscntrl(*cur))
*cur = ' ';
esc = mu_str_escape_c_literal (utf8, TRUE);
g_free (utf8);
g_string_append_printf (gstr, "\t:%s %s\n", elm, esc); g_string_append_printf (gstr, "\t:%s %s\n", elm, esc);
g_free (esc); g_free (esc);