/
Notmuch.pm6
386 lines (350 loc) · 10 KB
/
Notmuch.pm6
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use v6;
use nqp;
use NativeCall;
constant NOTMUCH_STATUS_SUCCESS = 0;
constant NOTMUCH_STATUS_OUT_OF_MEMORY = 1;
constant NOTMUCH_STATUS_READ_ONLY_DATABASE = 2;
constant NOTMUCH_STATUS_XAPIAN_EXCEPTION = 3;
constant NOTMUCH_STATUS_FILE_ERROR = 4;
constant NOTMUCH_STATUS_FILE_NOT_EMAIL = 5;
constant NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID = 6;
constant NOTMUCH_STATUS_NULL_POINTER = 7;
constant NOTMUCH_STATUS_TAG_TOO_LONG = 8;
constant NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW = 9;
constant NOTMUCH_STATUS_UNBALANCED_ATOMIC = 10;
constant NOTMUCH_STATUS_UNSUPPORTED_OPERATION = 11;
constant NOTMUCH_STATUS_UPGRADE_REQUIRED = 12;
constant NOTMUCH_STATUS_PATH_ERROR = 13;
class Tags is repr('CPointer') {
sub notmuch_tags_valid(Tags)
returns bool
is native('notmuch')
{*};
sub notmuch_tags_get(Tags)
returns Str
is native('notmuch')
{*};
sub notmuch_tags_destroy(Tags)
returns bool
is native('notmuch')
{*};
sub notmuch_tags_move_to_next(Tags)
is native('notmuch')
{*};
method all() {
gather {
while self.valid() {
take self.get();
self.move_to_next();
}
}
}
method destroy() {
notmuch_tags_destroy(self)
}
method get() {
return unless self.valid();
notmuch_tags_get(self)
}
method move_to_next() {
notmuch_tags_move_to_next(self)
}
method valid() {
notmuch_tags_valid(self)
}
}
class Message is repr('CPointer') {
sub notmuch_message_destroy(Message)
returns bool
is native('notmuch')
{*};
sub notmuch_message_get_filename(Message)
returns Str
is native('notmuch')
{*};
sub notmuch_message_get_header(Message, Str $header)
returns Str
is native('notmuch')
{*};
sub notmuch_message_get_tags(Message)
returns Tags
is native('notmuch')
{*};
sub notmuch_message_add_tag(Message, Str $tag)
returns int32
is native('notmuch')
{*};
sub notmuch_message_remove_tag(Message, Str $tag)
returns int32
is native('notmuch')
{*};
sub notmuch_message_get_message_id(Message)
returns Str
is native('notmuch')
{*};
sub notmuch_message_get_thread_id(Message)
returns Str
is native('notmuch')
{*};
method destroy() {
notmuch_message_destroy(self)
}
method get_filename() {
notmuch_message_get_filename(self)
}
method get_header(Str $header) {
notmuch_message_get_header(self, $header)
}
method get_tags() {
notmuch_message_get_tags(self)
}
method add_tag(str $tag) {
notmuch_message_add_tag(self, $tag)
}
method remove_tag(str $tag) {
notmuch_message_remove_tag(self, $tag)
}
method get_message_id() {
notmuch_message_get_message_id(self)
}
method get_thread_id() {
notmuch_message_get_thread_id(self)
}
}
class Thread is repr('CPointer') {
sub notmuch_thread_destroy(Thread)
returns bool
is native('notmuch')
{*};
sub notmuch_thread_get_tags(Thread)
returns Tags
is native('notmuch')
{*};
sub notmuch_thread_get_messages(Thread)
returns Tags
is native('notmuch')
{*};
sub notmuch_thread_get_thread_id(Thread)
returns Str
is native('notmuch')
{*};
method destroy() {
notmuch_thread_destroy(self)
}
method get_tags() {
notmuch_thread_get_tags(self)
}
method get_thread_id() {
notmuch_thread_get_thread_id(self)
}
method get_thread_get_messages() {
my $messages = notmuch_thread_get_messages(self);
}
}
class Database is repr('CPointer') {
sub notmuch_database_create_verbose(Str $path, CArray[long] $database, CArray[Str] $error_message)
returns int32
is native('notmuch', v4)
{*};
sub notmuch_database_create(Str $path, CArray[long] $database)
returns int32
is native('notmuch')
{*};
sub notmuch_database_open_verbose(Str $path, int32 $mode, CArray[long] $database, CArray[Str] $error_message)
returns int32
is native('notmuch', v4)
{*};
sub notmuch_database_open(Str $path, int32 $mode, CArray[long] $database)
returns int32
is native('notmuch')
{*};
sub notmuch_database_get_all_tags(Database)
returns Tags
is native('notmuch')
{*};
sub notmuch_database_add_message(Database, Str $filename, CArray[long] $message)
returns int32
is native('notmuch')
{*};
sub notmuch_database_find_message(Database, Str $id, CArray[long] $message)
returns int32
is native('notmuch')
{*};
sub notmuch_database_find_message_by_filename(Database, Str $filename, CArray[long] $message)
returns int32
is native('notmuch')
{*};
sub notmuch_database_get_version(Database)
returns int32
is native('notmuch')
{*};
sub notmuch_database_close(Database)
returns int32
is native('notmuch')
{*};
sub notmuch_database_destroy(Database)
returns bool
is native('notmuch')
{*};
method create(Str $path) {
my $buf = CArray[long].new;
my $err = CArray[Str].new;
$buf[0] = 0;
notmuch_database_create($path, $buf);
fail "create has failed" unless $buf[0];
# TODO(Gonéri): I guess there is better way to do that ^^
nqp::box_i(nqp::unbox_i(nqp::decont($buf[0])), Database)
}
method open(Str $path, Str $mode='r') {
my $buf = CArray[long].new;
$buf[0] = 0;
my $binmode = $mode eq 'w' ?? 1 !! 0;
notmuch_database_open($path, $binmode, $buf);
fail "open has failed" unless $buf[0];
# TODO(Gonéri): I guess there is better way to do that ^^
nqp::box_i(nqp::unbox_i(nqp::decont($buf[0])), Database)
}
method add_message(Str $filename) {
my $buf = CArray[long].new;
$buf[0] = 0;
notmuch_database_add_message(self, $filename, $buf);
fail "add_message has failed" unless $buf[0];
my $message = nqp::box_i(nqp::unbox_i(nqp::decont($buf[0])), Message);
return $message;
}
method find_message(Str $id) {
my $buf = CArray[long].new;
$buf[0] = 0;
notmuch_database_find_message(self, $id, $buf);
fail "find_message has failed" unless $buf[0];
my $message = nqp::box_i(nqp::unbox_i(nqp::decont($buf[0])), Message);
return $message;
}
method find_message_by_filename(Str $filename) {
my $buf = CArray[long].new;
$buf[0] = 0;
notmuch_database_find_message_by_filename(self, $filename, $buf);
fail "find_message_by_filename has failed" unless $buf[0];
my $message = nqp::box_i(nqp::unbox_i(nqp::decont($buf[0])), Message);
return $message;
}
method get_version() {
notmuch_database_get_version(self);
}
method close() {
if notmuch_database_destroy(self) != NOTMUCH_STATUS_SUCCESS {
die "Failed to close the DB";
}
}
}
class Messages is repr('CPointer') {
sub notmuch_messages_valid(Messages)
returns bool
is native('notmuch')
{*};
sub notmuch_messages_get(Messages)
returns Message
is native('notmuch')
{*};
sub notmuch_messages_destroy(Messages)
returns bool
is native('notmuch')
{*};
sub notmuch_messages_move_to_next(Messages)
is native('notmuch')
{*};
method all() {
gather {
while self.valid() {
take self.get();
self.move_to_next();
}
}
}
method destroy() {
notmuch_messages_destroy(self)
}
method get() {
return unless self.valid();
my $message = notmuch_messages_get(self);
return $message;
}
method move_to_next() {
notmuch_messages_move_to_next(self)
}
method valid() {
notmuch_messages_valid(self)
}
}
class Threads is repr('CPointer') {
sub notmuch_threads_valid(Threads)
returns bool
is native('notmuch')
{*};
sub notmuch_threads_get(Threads)
returns Thread
is native('notmuch')
{*};
sub notmuch_threads_destroy(Threads)
returns bool
is native('notmuch')
{*};
sub notmuch_threads_move_to_next(Threads)
is native('notmuch')
{*};
method all() {
gather {
while self.valid() {
take self.get();
self.move_to_next();
}
}
}
method destroy() {
notmuch_threads_destroy(self)
}
method get() {
return unless self.valid();
my $thread = notmuch_threads_get(self);
return $thread;
}
method move_to_next() {
notmuch_threads_move_to_next(self)
}
method valid() {
notmuch_threads_valid(self)
}
}
class Query is repr('CPointer') {
sub notmuch_query_create(Database $database, Str $query_string)
returns Query
is native('notmuch')
{*};
sub notmuch_query_destroy(Query)
returns bool
is native('notmuch')
{*};
sub notmuch_query_search_messages(Query)
returns Messages
is native('notmuch')
{*};
sub notmuch_query_search_threads(Query)
returns Threads
is native('notmuch')
{*};
method new(Database $database, Str $query_string) {
my $query = notmuch_query_create($database, $query_string);
return $query;
}
method destroy() {
notmuch_query_destroy(self);
}
method search_messages() {
my $messages = notmuch_query_search_messages(self);
return $messages;
}
method search_threads() {
my $threads = notmuch_query_search_threads(self);
return $threads;
}
}