2022-03-26 15:19:08 +01:00
|
|
|
/*
|
|
|
|
** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
|
|
|
**
|
|
|
|
** This program is free software; you can redistribute it and/or modify it
|
|
|
|
** under the terms of the GNU General Public License as published by the
|
|
|
|
** Free Software Foundation; either version 3, or (at your option) any
|
|
|
|
** later version.
|
|
|
|
**
|
|
|
|
** This program is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with this program; if not, write to the Free Software Foundation,
|
|
|
|
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "mu-message-part.hh"
|
2022-03-28 21:22:36 +02:00
|
|
|
#include "glibconfig.h"
|
2022-03-26 15:19:08 +01:00
|
|
|
#include "mu-mime-object.hh"
|
2022-03-26 16:16:46 +01:00
|
|
|
#include "utils/mu-utils.hh"
|
2022-05-05 00:24:18 +02:00
|
|
|
#include <string>
|
2022-03-26 15:19:08 +01:00
|
|
|
|
|
|
|
using namespace Mu;
|
|
|
|
|
|
|
|
MessagePart::MessagePart(const Mu::MimeObject& obj):
|
|
|
|
mime_obj{std::make_unique<Mu::MimeObject>(obj)}
|
|
|
|
{}
|
|
|
|
|
|
|
|
MessagePart::MessagePart(const MessagePart& other):
|
|
|
|
MessagePart(*other.mime_obj)
|
|
|
|
{}
|
|
|
|
|
|
|
|
MessagePart::~MessagePart() = default;
|
|
|
|
|
2022-04-09 18:15:30 +02:00
|
|
|
const MimeObject&
|
|
|
|
MessagePart::mime_object() const noexcept
|
|
|
|
{
|
|
|
|
return *mime_obj;
|
|
|
|
}
|
|
|
|
|
2022-03-26 15:19:08 +01:00
|
|
|
Option<std::string>
|
2022-03-28 21:22:36 +02:00
|
|
|
MessagePart::cooked_filename() const noexcept
|
|
|
|
{
|
|
|
|
// make a bit more pallatble.
|
|
|
|
auto cleanup = [](const std::string& name)->std::string {
|
|
|
|
std::string clean;
|
|
|
|
clean.reserve(name.length());
|
|
|
|
for (auto& c: name) {
|
|
|
|
auto taboo{(::iscntrl(c) || c == G_DIR_SEPARATOR ||
|
|
|
|
c == ' ' || c == '\\' || c == ':')};
|
|
|
|
clean += (taboo ? '-' : c);
|
|
|
|
}
|
|
|
|
if (clean.size() > 1 && clean[0] == '-')
|
|
|
|
clean.erase(0, 1);
|
|
|
|
|
|
|
|
return clean;
|
|
|
|
};
|
|
|
|
|
|
|
|
// a MimePart... use the name if there is one.
|
2022-04-09 18:15:30 +02:00
|
|
|
if (mime_object().is_part())
|
|
|
|
return MimePart{mime_object()}.filename().map(cleanup);
|
2022-03-28 21:22:36 +02:00
|
|
|
|
|
|
|
// MimeMessagepart. Construct a name based on subject.
|
2022-04-09 18:15:30 +02:00
|
|
|
if (mime_object().is_message_part()) {
|
|
|
|
auto msg{MimeMessagePart{mime_object()}.get_message()};
|
2022-05-12 18:58:08 +02:00
|
|
|
if (!msg)
|
|
|
|
return Nothing;
|
|
|
|
else
|
|
|
|
return msg->subject()
|
|
|
|
.map(cleanup)
|
|
|
|
.value_or("no-subject") + ".eml";
|
2022-03-28 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-05 00:24:18 +02:00
|
|
|
return Nothing;
|
2022-03-28 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Option<std::string>
|
|
|
|
MessagePart::raw_filename() const noexcept
|
2022-03-26 15:19:08 +01:00
|
|
|
{
|
2022-04-09 18:15:30 +02:00
|
|
|
if (!mime_object().is_part())
|
2022-03-26 15:19:08 +01:00
|
|
|
return Nothing;
|
|
|
|
else
|
2022-04-09 18:15:30 +02:00
|
|
|
return MimePart{mime_object()}.filename();
|
2022-03-26 15:19:08 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 21:22:36 +02:00
|
|
|
|
|
|
|
|
2022-03-26 15:19:08 +01:00
|
|
|
Option<std::string>
|
|
|
|
MessagePart::mime_type() const noexcept
|
|
|
|
{
|
2022-04-09 18:15:30 +02:00
|
|
|
if (const auto ctype{mime_object().content_type()}; ctype)
|
2022-03-26 15:19:08 +01:00
|
|
|
return ctype->media_type() + "/" + ctype->media_subtype();
|
|
|
|
else
|
|
|
|
return Nothing;
|
|
|
|
}
|
|
|
|
|
2022-05-05 22:45:11 +02:00
|
|
|
Option<std::string>
|
|
|
|
MessagePart::content_description() const noexcept
|
|
|
|
{
|
|
|
|
if (!mime_object().is_part())
|
|
|
|
return Nothing;
|
|
|
|
else
|
|
|
|
return MimePart{mime_object()}.content_description();
|
|
|
|
}
|
|
|
|
|
2022-03-26 15:19:08 +01:00
|
|
|
size_t
|
|
|
|
MessagePart::size() const noexcept
|
|
|
|
{
|
2022-04-09 18:15:30 +02:00
|
|
|
if (!mime_object().is_part())
|
2022-03-26 15:19:08 +01:00
|
|
|
return 0;
|
|
|
|
else
|
2022-04-09 18:15:30 +02:00
|
|
|
return MimePart{mime_object()}.size();
|
2022-03-26 15:19:08 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 21:22:36 +02:00
|
|
|
bool
|
|
|
|
MessagePart::is_attachment() const noexcept
|
|
|
|
{
|
2022-04-09 18:15:30 +02:00
|
|
|
if (!mime_object().is_part())
|
2022-03-28 21:22:36 +02:00
|
|
|
return false;
|
|
|
|
else
|
2022-04-09 18:15:30 +02:00
|
|
|
return MimePart{mime_object()}.is_attachment();
|
2022-03-28 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
2022-03-26 15:19:08 +01:00
|
|
|
|
|
|
|
Option<std::string>
|
|
|
|
MessagePart::to_string() const noexcept
|
|
|
|
{
|
2022-04-09 18:15:30 +02:00
|
|
|
if (mime_object().is_part())
|
|
|
|
return MimePart{mime_object()}.to_string();
|
2022-03-26 15:19:08 +01:00
|
|
|
else
|
2022-04-16 12:16:29 +02:00
|
|
|
return mime_object().to_string_opt();
|
2022-03-26 15:19:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Result<size_t>
|
|
|
|
MessagePart::to_file(const std::string& path, bool overwrite) const noexcept
|
|
|
|
{
|
2022-04-09 18:15:30 +02:00
|
|
|
if (!mime_object().is_part())
|
2022-03-26 15:19:08 +01:00
|
|
|
return Err(Error::Code::InvalidArgument,
|
|
|
|
"not a part");
|
|
|
|
else
|
2022-04-09 18:15:30 +02:00
|
|
|
return MimePart{mime_object()}.to_file(path, overwrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
MessagePart::is_signed() const noexcept
|
|
|
|
{
|
|
|
|
return mime_object().is_multipart_signed();
|
2022-03-26 15:19:08 +01:00
|
|
|
}
|
2022-04-10 12:18:55 +02:00
|
|
|
|
|
|
|
bool
|
|
|
|
MessagePart::is_encrypted() const noexcept
|
|
|
|
{
|
|
|
|
return mime_object().is_multipart_encrypted();
|
|
|
|
}
|
2022-06-09 23:43:47 +02:00
|
|
|
|
|
|
|
bool /* heuristic */
|
|
|
|
MessagePart::looks_like_attachment() const noexcept
|
|
|
|
{
|
|
|
|
auto matches=[](const MimeContentType& ctype,
|
|
|
|
const std::initializer_list<std::pair<const char*, const char*>>& ctypes) {
|
|
|
|
return std::find_if(ctypes.begin(), ctypes.end(), [&](auto&& item){
|
|
|
|
return ctype.is_type(item.first, item.second); }) != ctypes.end();
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto ctype{mime_object().content_type()};
|
|
|
|
if (!ctype)
|
|
|
|
return false; // no content-type: not an attachment.
|
|
|
|
|
|
|
|
// we consider some parts _not_ to be attachments regardless of disposition
|
|
|
|
if (matches(*ctype,{{"application", "pgp-keys"}}))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// we consider some parts to be attachments regardless of disposition
|
|
|
|
if (matches(*ctype,{{"image", "*"},
|
|
|
|
{"audio", "*"},
|
|
|
|
{"application", "*"},
|
|
|
|
{"application", "x-patch"}}))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// otherwise, rely on the disposition
|
|
|
|
return is_attachment();
|
|
|
|
}
|