message: convert mime-parts to utf-8

Ensure that non-utf8 mime-parts are converted to utf8. This fixes a
problem with messages with such parts; added unit test.

Fixes #2333.
This commit is contained in:
Dirk-Jan C. Binnema
2022-09-19 18:27:03 +03:00
parent e8177c7f04
commit bb7c8d880a
2 changed files with 84 additions and 2 deletions

View File

@ -477,10 +477,22 @@ MimePart::size() const noexcept
return static_cast<size_t>(g_mime_stream_length(stream));
}
Option<std::string>
MimePart::to_string() const noexcept
{
/*
* easy case: text. this automatically handles conversion to utf-8.
*/
if (GMIME_IS_TEXT_PART(self())) {
if (char* txt{g_mime_text_part_get_text(GMIME_TEXT_PART(self()))}; !txt)
return Nothing;
else
return to_string_gchar(std::move(txt)/*consumes*/);
}
/*
* harder case: read from stream manually
*/
GMimeDataWrapper *wrapper{g_mime_part_get_content(self())};
if (!wrapper) { /* this happens with invalid mails */
g_debug("failed to create data wrapper");
@ -493,7 +505,6 @@ MimePart::to_string() const noexcept
return Nothing;
}
ssize_t buflen{g_mime_data_wrapper_write_to_stream(wrapper, stream)};
if (buflen <= 0) { /* empty buffer, not an error */
g_object_unref(stream);
@ -513,9 +524,11 @@ MimePart::to_string() const noexcept
buffer.resize(buflen);
return buffer;
}
Result<size_t>
MimePart::to_file(const std::string& path, bool overwrite) const noexcept
{