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
|
/*
** Copyright (C) 2025 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-scm-types.hh"
#include "message/mu-message.hh"
#include "message/mu-mime-object.hh"
#include <mutex>
#include <cstdio>
using namespace Mu;
using namespace Mu::Scm;
namespace {
static SCM message_type;
static bool initialized;
std::mutex map_lock;
constexpr auto max_message_map_size{512};
struct MessageObject {
Message message;
SCM foreign_object{};
};
using MessageMap = std::unordered_map<std::string, MessageObject>;
static MessageMap message_map;
}
static const Message&
to_message(SCM scm, const char *func, int pos)
{
if (!SCM_IS_A_P(scm, message_type))
throw ScmError{ScmError::Id::WrongType, func, pos, scm, "mesagestore"};
return *reinterpret_cast<Message*>(scm_foreign_object_ref(scm, 0));
}
static void
finalize_message(SCM scm)
{
std::unique_lock lock{map_lock};
const auto msg = reinterpret_cast<const Message*>(scm_foreign_object_ref(scm, 0));
//mu_debug("finalizing message @ {}", msg->path());
if (const auto n = message_map.erase(msg->path()); n != 1)
mu_warning("huh?! deleted {}", n);
}
static SCM
subr_cc_message_make(SCM message_path_scm) try {
constexpr auto func{"cc-message-make"};
// message objects eat fds, tickle the gc... letting it handle it
// automatically is not soon enough.
if (message_map.size() >= 0.8 * max_message_map_size)
scm_gc();
std::unique_lock lock{map_lock};
// qttempt to give an good error message rather then getting something
// from GMime)
if (message_map.size() >= max_message_map_size)
throw ScmError{"cc-make-message", "too many open messages"};
// if we already have the message in our map, return it.
auto path{from_scm<std::string>(message_path_scm, func, 1)};
if (const auto it = message_map.find(path); it != message_map.end())
return it->second.foreign_object;
// don't have it yet; attempt to create one
if (auto res{Message::make_from_path(path)}; !res) {
mu_printerrln("{}", res.error().what());
throw ScmError{func, "failed to create message"};
} else {
// create a new object, store it in our map and return the foreign ptr.
std::pair<std::string, MessageObject> item {path,
MessageObject{std::move(*res), {}}};
auto it = message_map.emplace(std::move(item));
return it.first->second.foreign_object = scm_make_foreign_object_1(
message_type,
const_cast<Message*>(&it.first->second.message));
}
} catch (const ScmError& err) {
err.throw_scm();
}
static SCM
subr_cc_message_body(SCM message_scm, SCM html_scm) try {
constexpr auto func{"cc-message-make"};
const auto& message{to_message(message_scm, func, 1)};
const auto html{from_scm<bool>(html_scm, func, 2)};
if (const auto body{html ? message.body_html() : message.body_text()}; body)
return to_scm(*body);
else
return SCM_BOOL_F;
} catch (const ScmError& err) {
err.throw_scm();
}
static SCM
subr_cc_message_header(SCM message_scm, SCM field_scm) try {
constexpr auto func{"cc-message-header"};
const auto& message{to_message(message_scm, func, 1)};
const auto field{from_scm<std::string>(field_scm, func, 2)};
if (const auto val{message.header(field)}; val)
return to_scm(*val);
else
return SCM_BOOL_F;
} catch (const ScmError& err) {
err.throw_scm();
}
static SCM
subr_cc_message_plist(SCM message_scm) try {
constexpr auto func{"cc-message-plist"};
const auto& message{to_message(message_scm, func, 1)};
// return the serialized (mu4e) message
const auto plist{message.sexp().to_string()};
return to_scm(plist);
} catch (const ScmError& err) {
err.throw_scm();
}
/**
* Get a list of message's MIME-parts
*
* @param message_scm a Message (foreign-object)
*
* @return a list of MIME parts, each is a pair
* ( mime-obj . alist )
* where the mime-obj is the GMimeObject* as a foreign-object,
* and alist is an association list describing the part.
*/
static SCM
subr_cc_message_parts(SCM message_scm) try {
constexpr auto func{"cc-message-parts"};
const auto& message{to_message(message_scm, func, 1)};
const auto& parts{message.parts()};
SCM parts_scm{SCM_EOL};
for (size_t idx = 0; idx != parts.size(); ++idx) {
auto part{parts[idx]};
auto mime_part{GMIME_PART(part.mime_object().object())};
SCM mime_part_scm{to_scm(mime_part)};
SCM alist_scm{to_scm(idx, parts[idx])};
SCM item{scm_cons(mime_part_scm, alist_scm)};
parts_scm = scm_cons(item, parts_scm);
}
return scm_reverse_x(parts_scm, SCM_EOL);
} catch (const ScmError& err) {
err.throw_scm();
}
static void
init_subrs()
{
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-function-type"
scm_c_define_gsubr("cc-message-make", 1/*req*/, 0/*opt*/, 0/*rst*/,
reinterpret_cast<scm_t_subr>(subr_cc_message_make));
scm_c_define_gsubr("cc-message-body", 2/*req*/, 0/*opt*/, 0/*rst*/,
reinterpret_cast<scm_t_subr>(subr_cc_message_body));
scm_c_define_gsubr("cc-message-header",2/*req*/, 0/*opt*/, 0/*rst*/,
reinterpret_cast<scm_t_subr>(subr_cc_message_header));
scm_c_define_gsubr("cc-message-parts",1/*req*/, 0/*opt*/, 0/*rst*/,
reinterpret_cast<scm_t_subr>(subr_cc_message_parts));
scm_c_define_gsubr("cc-message-plist",1/*req*/, 0/*opt*/, 0/*rst*/,
reinterpret_cast<scm_t_subr>(subr_cc_message_plist));
#pragma GCC diagnostic pop
}
void
Mu::Scm::init_message()
{
if (initialized)
return;
message_type = scm_make_foreign_object_type(
make_symbol("message"),
scm_list_1(make_symbol("data")),
finalize_message);
init_subrs();
initialized = true;
}
|