1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/*
** Copyright (C) 2023 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"
#include "mu-mime-object.hh"
#include "utils/mu-utils.hh"
#include "utils/mu-utils-file.hh"
#include <string>
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;
const MimeObject&
MessagePart::mime_object() const noexcept
{
return *mime_obj;
}
static std::string
cook(const std::string& fname, const std::vector<char>& forbidden)
{
std::string clean;
clean.reserve(fname.length());
for (auto& c: basename(fname))
if (seq_some(forbidden,[&](char fc){return ::iscntrl(c) || c == fc;}))
clean += '-';
else
clean += c;
if (clean[0] == '.' && (clean == "." || clean == ".."))
return "-";
else
return clean;
}
static std::string
cook_minimal(const std::string& fname)
{
return cook(fname, { '/' });
}
static std::string
cook_full(const std::string& fname)
{
auto cooked = cook(fname, { '/', ' ', '\\', ':' });
if (cooked.size() > 1 && cooked[0] == '-')
cooked.erase(0, 1);
return cooked;
}
Option<std::string>
MessagePart::cooked_filename(bool minimal) const noexcept
{
auto&& cooker{minimal ? cook_minimal : cook_full};
// a MimePart... use the name if there is one.
if (mime_object().is_part())
return MimePart{mime_object()}.filename().map(cooker);
// MimeMessagepart. Construct a name based on subject.
if (mime_object().is_message_part()) {
auto msg{MimeMessagePart{mime_object()}.get_message()};
if (!msg)
return Nothing;
else
return msg->subject()
.map(cooker)
.value_or("no-subject") + ".eml";
}
return Nothing;
}
Option<std::string>
MessagePart::raw_filename() const noexcept
{
if (!mime_object().is_part())
return Nothing;
else
return MimePart{mime_object()}.filename();
}
Option<std::string>
MessagePart::mime_type() const noexcept
{
if (const auto ctype{mime_object().content_type()}; ctype)
return ctype->media_type() + "/" + ctype->media_subtype();
else
return Nothing;
}
Option<std::string>
MessagePart::content_description() const noexcept
{
if (!mime_object().is_part())
return Nothing;
else
return MimePart{mime_object()}.content_description();
}
size_t
MessagePart::size() const noexcept
{
if (!mime_object().is_part())
return 0;
else
return MimePart{mime_object()}.size();
}
bool
MessagePart::is_attachment() const noexcept
{
if (!mime_object().is_part())
return false;
else
return MimePart{mime_object()}.is_attachment();
}
Option<std::string>
MessagePart::to_string() const noexcept
{
if (mime_object().is_part())
return MimePart{mime_object()}.to_string();
else
return mime_object().to_string_opt();
}
Result<size_t>
MessagePart::to_file(const std::string& path, bool overwrite) const noexcept
{
if (mime_object().is_part())
return MimePart{mime_object()}.to_file(path, overwrite);
else if (mime_object().is_message_part()) {
if (auto&& msg{MimeMessagePart{mime_object()}.get_message()}; !msg)
return Err(Error::Code::Message, "failed to get message-part");
else
return msg->to_file(path, overwrite);
} else
return mime_object().to_file(path, overwrite);
}
bool
MessagePart::is_signed() const noexcept
{
return mime_object().is_multipart_signed();
}
bool
MessagePart::is_encrypted() const noexcept
{
return mime_object().is_multipart_encrypted();
}
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"},
{"application", "ics"}}))
return false;
if (is_attachment()) // i.e., as per content-disposition
return true;
// we also consider "inline" parts as attachment, if the
// content-disposition has a filename property.
if (const auto cdisp{mime_object().content_disposition()}; !!cdisp) {
if (const auto& fname{cdisp->parameter("filename")}; !!fname)
return true;
}
// otherwise, rely on the disposition
return is_attachment();
}
#ifdef BUILD_TESTS
#include "utils/mu-test-utils.hh"
static void
test_cooked_full()
{
std::array<std::pair<std::string, std::string>, 4> cases = {{
{ "/hello/world/foo", "foo" },
{ "foo:/\n/bar", "bar"},
{ "Aap Noot Mies", "Aap-Noot-Mies"},
{ "..", "-"}
}};
for (auto&& test: cases)
assert_equal(cook_full(test.first), test.second);
}
static void
test_cooked_minimal()
{
std::array<std::pair<std::string, std::string>, 4> cases = {{
{ "/hello/world/foo", "foo" },
{ "foo:/\n/bar", "bar"},
{ "Aap Noot Mies.doc", "Aap Noot Mies.doc"},
{ "..", "-"}
}};
for (auto&& test: cases)
assert_equal(cook_minimal(test.first), test.second);
}
int
main(int argc, char* argv[])
{
mu_test_init(&argc, &argv);
g_test_add_func("/message/message-part/cooked-full", test_cooked_full);
g_test_add_func("/message/message-part/cooked-minimal", test_cooked_minimal);
return g_test_run();
}
#endif /*BUILD_TESTS*/
|