message: part: better handle internal mime-object

This commit is contained in:
Dirk-Jan C. Binnema 2022-04-09 19:15:30 +03:00
parent 8ede2a8354
commit 09e175e9b5
2 changed files with 50 additions and 19 deletions

View File

@ -35,6 +35,14 @@ MessagePart::MessagePart(const MessagePart& other):
MessagePart::~MessagePart() = default; MessagePart::~MessagePart() = default;
const MimeObject&
MessagePart::mime_object() const noexcept
{
return *mime_obj;
}
Option<std::string> Option<std::string>
MessagePart::cooked_filename() const noexcept MessagePart::cooked_filename() const noexcept
@ -55,12 +63,12 @@ MessagePart::cooked_filename() const noexcept
}; };
// a MimePart... use the name if there is one. // a MimePart... use the name if there is one.
if (mime_obj->is_part()) if (mime_object().is_part())
return MimePart(*mime_obj).filename().map(cleanup); return MimePart{mime_object()}.filename().map(cleanup);
// MimeMessagepart. Construct a name based on subject. // MimeMessagepart. Construct a name based on subject.
if (mime_obj->is_message_part()) { if (mime_object().is_message_part()) {
auto msg{MimeMessagePart(*mime_obj).get_message()}; auto msg{MimeMessagePart{mime_object()}.get_message()};
return msg.subject() return msg.subject()
.map(cleanup) .map(cleanup)
.value_or("no-subject") + ".eml"; .value_or("no-subject") + ".eml";
@ -73,10 +81,10 @@ return Nothing;
Option<std::string> Option<std::string>
MessagePart::raw_filename() const noexcept MessagePart::raw_filename() const noexcept
{ {
if (!mime_obj->is_part()) if (!mime_object().is_part())
return Nothing; return Nothing;
else else
return MimePart(*mime_obj).filename(); return MimePart{mime_object()}.filename();
} }
@ -84,7 +92,7 @@ MessagePart::raw_filename() const noexcept
Option<std::string> Option<std::string>
MessagePart::mime_type() const noexcept MessagePart::mime_type() const noexcept
{ {
if (const auto ctype{mime_obj->content_type()}; ctype) if (const auto ctype{mime_object().content_type()}; ctype)
return ctype->media_type() + "/" + ctype->media_subtype(); return ctype->media_type() + "/" + ctype->media_subtype();
else else
return Nothing; return Nothing;
@ -93,29 +101,29 @@ MessagePart::mime_type() const noexcept
size_t size_t
MessagePart::size() const noexcept MessagePart::size() const noexcept
{ {
if (!mime_obj->is_part()) if (!mime_object().is_part())
return 0; return 0;
else else
return MimePart(*mime_obj).size(); return MimePart{mime_object()}.size();
} }
bool bool
MessagePart::is_attachment() const noexcept MessagePart::is_attachment() const noexcept
{ {
if (!mime_obj->is_part()) if (!mime_object().is_part())
return false; return false;
else else
return MimePart(*mime_obj).is_attachment(); return MimePart{mime_object()}.is_attachment();
} }
Option<std::string> Option<std::string>
MessagePart::to_string() const noexcept MessagePart::to_string() const noexcept
{ {
if (mime_obj->is_part()) if (mime_object().is_part())
return MimePart(*mime_obj).to_string(); return MimePart{mime_object()}.to_string();
else else
return mime_obj->object_to_string(); return mime_object().object_to_string();
} }
@ -123,9 +131,15 @@ MessagePart::to_string() const noexcept
Result<size_t> Result<size_t>
MessagePart::to_file(const std::string& path, bool overwrite) const noexcept MessagePart::to_file(const std::string& path, bool overwrite) const noexcept
{ {
if (!mime_obj->is_part()) if (!mime_object().is_part())
return Err(Error::Code::InvalidArgument, return Err(Error::Code::InvalidArgument,
"not a part"); "not a part");
else else
return MimePart(*mime_obj).to_file(path, overwrite); return MimePart{mime_object()}.to_file(path, overwrite);
}
bool
MessagePart::is_signed() const noexcept
{
return mime_object().is_multipart_signed();
} }

View File

@ -28,7 +28,8 @@
namespace Mu { namespace Mu {
class MimeObject; // forward declaration class MimeObject; // forward declaration; don't want to include for build-time
// reasons.
class MessagePart { class MessagePart {
public: public:
@ -52,6 +53,15 @@ public:
*/ */
~MessagePart(); ~MessagePart();
/**
* Get the underlying MimeObject; you need to include mu-mime-object.hh
* to do anything useful with it.
*
* @return reference to the mime-object
*/
const MimeObject& mime_object() const noexcept;
/** /**
* Filename for the mime-part file. This is a "cooked" filename with * Filename for the mime-part file. This is a "cooked" filename with
* unallowed characters removed. If there's no filename specified, * unallowed characters removed. If there's no filename specified,
@ -70,7 +80,6 @@ public:
*/ */
Option<std::string> raw_filename() const noexcept; Option<std::string> raw_filename() const noexcept;
/** /**
* Mime-type for the mime-part (e.g. "text/plain") * Mime-type for the mime-part (e.g. "text/plain")
* *
@ -94,6 +103,14 @@ public:
*/ */
bool is_attachment() const noexcept; bool is_attachment() const noexcept;
/**
* Is this part signed?
*
* @return true or false
*/
bool is_signed() const noexcept;
/** /**
* Write (decoded) mime-part contents to string * Write (decoded) mime-part contents to string
* *
@ -113,7 +130,7 @@ public:
struct Private; struct Private;
private: private:
std::unique_ptr<MimeObject> mime_obj; const std::unique_ptr<MimeObject> mime_obj;
}; };
} // namespace Mu } // namespace Mu