From 297df938d615607d41f42068d39923cc64e9d292 Mon Sep 17 00:00:00 2001 From: Aaron LI Date: Mon, 23 May 2016 19:26:04 +0800 Subject: [PATCH] Use Unicode characters when cleaning up attachment filename MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improve the function ``cleanup_filename()`` of ``lib/mu-msg-part.c`` to use Unicode characters when replacing the control characters, slashes and colons with ``-``. Originally, this function just use plain C characters (i.e., assuming ASCII string) when checking each character is or not a control character, slash or colon. However, when the attachment filename contains non-ASCII (e.g., Chinese characters), all the non-ASCII characters are replaced with ``-``. For example: * Before: ``` > mu view test_chinese_attachment_filename.eml From: Tester To: Example Subject: Test email with attachment of Chinese filename Date: Mon 23 May 2016 05:22:09 PM CST Attachments: 'attachment-test.txt', '------------.txt', '-------test.txt' Hello, This is a simple test email with three attachments: 1. `attachment:test.txt`: filename is all English; 2. `测试附件.txt`: filename is all Chinese (exclude the extension); 3. `附件-test.txt`: filename mixes Chinese and English. ``` * After: ``` > ./build/mu/mu/mu view test_chinese_attachment_filename.eml From: Tester To: Example Subject: Test email with attachment of Chinese filename Date: Mon 23 May 2016 05:22:09 PM CST Attachments: 'attachment-test.txt', '测试附件.txt', '附件-test.txt' Hello, This is a simple test email with three attachments: 1. `attachment:test.txt`: filename is all English; 2. `测试附件.txt`: filename is all Chinese (exclude the extension); 3. `附件-test.txt`: filename mixes Chinese and English. ``` --- lib/mu-msg-part.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lib/mu-msg-part.c b/lib/mu-msg-part.c index b6bbffc3..f4242b32 100644 --- a/lib/mu-msg-part.c +++ b/lib/mu-msg-part.c @@ -239,15 +239,26 @@ get_part_size (GMimePart *part) } -static void +static char* cleanup_filename (char *fname) { + GString *gstr; gchar *cur; + gunichar uc; - /* replace control characters, slashes,colons by a '-' */ - for (cur = fname; *cur; ++cur) - if (*cur < ' ' || *cur == '/' || *cur == ':') - *cur = '-'; + gstr = g_string_sized_new (strlen (fname)); + + /* replace control characters, slashes, and colons by '-' */ + for (cur = fname; cur && *cur; cur = g_utf8_next_char (cur)) { + uc = g_utf8_get_char (cur); + if (g_unichar_iscntrl (uc) || uc == '/' || uc == ':') + g_string_append_unichar (gstr, '-'); + else + g_string_append_unichar (gstr, uc); + } + + g_free (fname); + return g_string_free (gstr, FALSE); } @@ -304,9 +315,9 @@ mime_part_get_filename (GMimeObject *mobj, unsigned index, if (!fname) fname = guess_file_name (mobj, index); - - /* remove slashes, spaces, colons... */ - cleanup_filename (fname); + + /* replace control characters, slashes, and colons */ + fname = cleanup_filename (fname); return fname; }