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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
/*
** Copyright (C) 2024 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 "config.h"
#include <array>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include "message/mu-message.hh"
#include "mu-maildir.hh"
#include "mu-query-match-deciders.hh"
#include "mu-query.hh"
#include "mu-query-macros.hh"
#include "mu-query-parser.hh"
#include "message/mu-message.hh"
#include "utils/mu-option.hh"
#include "mu-cmd.hh"
#include "utils/mu-utils.hh"
using namespace Mu;
static Result<size_t>
count_query(const Store& store, const Options& opts)
{
if (opts.count.query.empty())
return Err(Error::Code::InvalidArgument,
"missing query");
auto&& query{join(opts.count.query, " ")};
return Ok(store.count_query(query));
}
static Result<std::string>
get_query(const Store& store, const Options& opts)
{
if (opts.find.bookmark.empty() && opts.find.query.empty())
return Err(Error::Code::InvalidArgument,
"neither bookmark nor query");
std::string bookmark;
if (!opts.find.bookmark.empty()) {
const auto res = resolve_bookmark(store, opts);
if (!res)
return Err(std::move(res.error()));
bookmark = res.value() + " ";
}
auto&& query{join(opts.find.query, " ")};
return Ok(bookmark + query);
}
static Result<void>
prepare_links(const Options& opts)
{
/* note, mu_maildir_mkdir simply ignores whatever part of the
* mail dir already exists */
if (auto&& res = maildir_mkdir(opts.find.linksdir, 0700, true); !res)
return Err(std::move(res.error()));
if (!opts.find.clearlinks)
return Ok();
if (auto&& res = maildir_clear_links(opts.find.linksdir); !res)
return Err(std::move(res.error()));
return Ok();
}
static Result<void>
output_link(const Option<Message>& msg, const OutputInfo& info, const Options& opts)
{
if (info.header)
return prepare_links(opts);
else if (info.footer)
return Ok();
/* during test, do not create "unique names" (i.e., names with path
* hashes), so we get a predictable result */
const auto unique_names{!g_getenv("MU_TEST")&&!g_test_initialized()};
if (auto&& res = maildir_link(msg->path(), opts.find.linksdir, unique_names); !res)
return Err(std::move(res.error()));
return Ok();
}
static void
ansi_color_maybe(Field::Id field_id, bool color)
{
const char* ansi;
if (!color)
return; /* nothing to do */
switch (field_id) {
case Field::Id::From: ansi = MU_COLOR_CYAN; break;
case Field::Id::To:
case Field::Id::Cc:
case Field::Id::Bcc: ansi = MU_COLOR_BLUE; break;
case Field::Id::Subject: ansi = MU_COLOR_GREEN; break;
case Field::Id::Date: ansi = MU_COLOR_MAGENTA; break;
default:
if (field_from_id(field_id).type != Field::Type::String)
ansi = MU_COLOR_YELLOW;
else
ansi = MU_COLOR_RED;
}
fputs(ansi, stdout);
}
static void
ansi_reset_maybe(Field::Id field_id, bool color)
{
if (!color)
return; /* nothing to do */
fputs(MU_COLOR_DEFAULT, stdout);
}
static std::string
display_field(const Message& msg, Field::Id field_id)
{
switch (field_from_id(field_id).type) {
case Field::Type::String:
return msg.document().string_value(field_id);
case Field::Type::Integer:
if (field_id == Field::Id::Priority) {
return to_string(msg.priority());
} else if (field_id == Field::Id::Flags) {
return to_string(msg.flags());
} else /* as string */
return msg.document().string_value(field_id);
case Field::Type::TimeT:
return mu_format("{:%c}",
mu_time(msg.document().integer_value(field_id)));
case Field::Type::ByteSize:
return to_string(msg.document().integer_value(field_id));
case Field::Type::StringList:
return join(msg.document().string_vec_value(field_id), ',');
case Field::Type::ContactList:
return to_string(msg.document().contacts_value(field_id));
default:
g_return_val_if_reached("");
return "";
}
}
static void
print_summary(const Message& msg, const Options& opts)
{
const auto body{msg.body_text()};
if (!body)
return;
const auto summ{summarize(body->c_str(), opts.find.summary_len.value_or(0))};
mu_print("Summary: ");
fputs_encoded(summ, stdout);
mu_println("");
}
static void
thread_indent(const QueryMatch& info, const Options& opts)
{
const auto is_root{any_of(info.flags & QueryMatch::Flags::Root)};
const auto first_child{any_of(info.flags & QueryMatch::Flags::First)};
const auto last_child{any_of(info.flags & QueryMatch::Flags::Last)};
const auto empty_parent{any_of(info.flags & QueryMatch::Flags::Orphan)};
const auto is_dup{any_of(info.flags & QueryMatch::Flags::Duplicate)};
// const auto is_related{any_of(info.flags & QueryMatch::Flags::Related)};
/* indent */
if (opts.debug) {
::fputs(info.thread_path.c_str(), stdout);
::fputs(" ", stdout);
} else
for (auto i = info.thread_level; i > 1; --i)
::fputs(" ", stdout);
if (!is_root) {
if (first_child)
::fputs("\\", stdout);
else if (last_child)
::fputs("/", stdout);
else
::fputs(" ", stdout);
::fputs(empty_parent ? "*> " : is_dup ? "=> "
: "-> ",
stdout);
}
}
static void
output_plain_fields(const Message& msg, const std::string& fields,
bool color, bool threads)
{
size_t nonempty{};
for (auto&& k: fields) {
const auto field_opt{field_from_shortcut(k)};
if (!field_opt || (!field_opt->is_value() && !field_opt->is_contact()))
nonempty += printf("%c", k);
else {
ansi_color_maybe(field_opt->id, color);
nonempty += fputs_encoded(
display_field(msg, field_opt->id), stdout);
ansi_reset_maybe(field_opt->id, color);
}
}
if (nonempty)
fputs("\n", stdout);
}
static Result<void>
output_plain(const Option<Message>& msg, const OutputInfo& info,
const Options& opts)
{
if (!msg)
return Ok();
/* we reuse the color (whatever that may be)
* for message-priority for threads, too */
ansi_color_maybe(Field::Id::Priority, !opts.nocolor);
if (opts.find.threads && info.match_info)
thread_indent(*info.match_info, opts);
output_plain_fields(*msg, opts.find.fields, !opts.nocolor, opts.find.threads);
if (opts.view.summary_len)
print_summary(*msg, opts);
return Ok();
}
static Result<void>
output_sexp(const Option<Message>& msg, const OutputInfo& info, const Options& opts)
{
if (msg) {
if (const auto sexp{msg->sexp()}; !sexp.empty())
fputs(sexp.to_string().c_str(), stdout);
else
fputs(msg->sexp().to_string().c_str(), stdout);
fputs("\n", stdout);
}
return Ok();
}
static Result<void>
output_json(const Option<Message>& msg, const OutputInfo& info, const Options& opts)
{
if (info.header) {
mu_println("[");
return Ok();
}
if (info.footer) {
mu_println("]");
return Ok();
}
if (!msg)
return Ok();
mu_println("{}{}", msg->sexp().to_json_string(), info.last ? "" : ",");
return Ok();
}
static void
print_attr_xml(const std::string& elm, const std::string& str)
{
if (str.empty())
return; /* empty: don't include */
auto&& esc{to_string_opt_gchar(g_markup_escape_text(str.c_str(), -1))};
mu_println("\t\t<{}>{}</{}>", elm, esc.value_or(""), elm);
}
static Result<void>
output_xml(const Option<Message>& msg, const OutputInfo& info, const Options& opts)
{
if (info.header) {
mu_println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
mu_println("<messages>");
return Ok();
}
if (info.footer) {
mu_println("</messages>");
return Ok();
}
mu_println("\t<message>");
print_attr_xml("from", to_string(msg->from()));
print_attr_xml("to", to_string(msg->to()));
print_attr_xml("cc", to_string(msg->cc()));
print_attr_xml("subject", msg->subject());
mu_println("\t\t<date>{}</date>", (unsigned)msg->date());
mu_println("\t\t<size>{}</size>", (unsigned)msg->size());
print_attr_xml("msgid", msg->message_id());
print_attr_xml("path", msg->path());
print_attr_xml("maildir", msg->maildir());
mu_println("\t</message>");
return Ok();
}
static OutputFunc
get_output_func(const Options& opts)
{
if (!opts.find.exec.empty())
return exec_cmd;
switch (opts.find.format) {
case Format::Links:
return output_link;
case Format::Plain:
return output_plain;
case Format::Xml:
return output_xml;
case Format::Sexp:
return output_sexp;
case Format::Json:
return output_json;
default:
throw Error(Error::Code::Internal,
"invalid format {}",
static_cast<size_t>(opts.find.format));
}
}
static Result<void>
output_query_results(const QueryResults& qres, const Options& opts)
{
GError* err{};
const auto output_func{get_output_func(opts)};
if (!output_func)
return Err(Error::Code::Query, &err, "failed to find output function");
if (auto&& res = output_func(Nothing, FirstOutput, opts); !res)
return Err(std::move(res.error()));
size_t n{0};
for (auto&& item : qres) {
n++;
auto msg{item.message()};
if (!msg)
continue;
if (msg->changed() < opts.find.after.value_or(0))
continue;
if (auto&& res = output_func(msg,
{item.doc_id(),
false,
false,
n == qres.size(), /* last? */
item.query_match()},
opts); !res)
return Err(std::move(res.error()));
}
if (auto&& res{output_func(Nothing, LastOutput, opts)}; !res)
return Err(std::move(res.error()));
else
return Ok();
}
static Result<void>
process_store_query(const Store& store, const std::string& expr, const Options& opts)
{
auto qres{run_query(store, expr, opts)};
if (!qres)
return Err(qres.error());
if (qres->empty())
return Err(Error::Code::NoMatches, "no matches for search expression");
return output_query_results(*qres, opts);
}
Result<void>
Mu::mu_cmd_find(const Store& store, const Options& opts)
{
auto expr{get_query(store, opts)};
if (!expr)
return Err(expr.error());
if (opts.find.analyze)
return analyze_query_expr(store, *expr, opts);
else
return process_store_query(store, *expr, opts);
}
#ifdef BUILD_TESTS
/*
* Tests.
*
*/
#include "utils/mu-test-utils.hh"
/* tests for the command line interface, uses testdir2 */
static std::string test_mu_home;
auto count_nl(const std::string& s)->size_t {
size_t n{};
for (auto&& c: s)
if (c == '\n')
++n;
return n;
}
static size_t
search_func(const std::string& expr, size_t expected)
{
auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home, expr});
assert_valid_result(res);
/* we expect zero lines of error output if there is a match; otherwise
* there should be one line 'No matches found' */
if (res->exit_code != 0) {
g_assert_cmpuint(res->exit_code, ==, 2); // no match
g_assert_true(res->standard_out.empty());
g_assert_cmpuint(count_nl(res->standard_err), ==, 1);
return 0;
}
return count_nl(res->standard_out);
}
#define search(Q,EXP) do { \
g_assert_cmpuint(search_func(Q, EXP), ==, EXP); \
} while(0)
static void
test_mu_find_empty_query(void)
{
search("\"\"", 14);
}
static void
test_mu_find_01(void)
{
search("f:john fruit", 1);
search("f:soc@example.com", 1);
search("t:alki@example.com", 1);
search("t:alcibiades", 1);
search("http emacs", 1);
search("f:soc@example.com OR f:john", 2);
search("f:soc@example.com OR f:john OR t:edmond", 3);
search("t:julius", 1);
search("s:dude", 1);
search("t:dantès", 1);
}
/* index testdir2, and make sure it adds two documents */
static void
test_mu_find_02(void)
{
search("bull", 1);
search("g:x", 0);
search("flag:encrypted", 0);
search("flag:attach", 1);
search("i:3BE9E6535E0D852173@emss35m06.us.lmco.com", 1);
}
static void
test_mu_find_file(void)
{
search("file:sittingbull.jpg", 1);
search("file:custer.jpg", 1);
search("file:custer.*", 1);
search("j:sit*", 1);
}
static void
test_mu_find_mime(void)
{
search("mime:image/jpeg", 1);
search("mime:text/plain", 14);
search("y:text*", 14);
search("y:image*", 1);
search("mime:message/rfc822", 2);
}
static void
test_mu_find_text_in_rfc822(void)
{
search("embed:dancing", 1);
search("e:curious", 1);
search("embed:with", 2);
search("e:karjala", 0);
search("embed:navigation", 1);
}
static void
test_mu_find_maildir_special(void)
{
search("\"maildir:/wOm_bàT\"", 3);
search("\"maildir:/wOm*\"", 3);
search("\"maildir:/wOm_*\"", 3);
search("\"maildir:wom_bat\"", 0);
search("\"maildir:/wombat\"", 0);
search("subject:atoms", 1);
search("\"maildir:/wom_bat\" subject:atoms", 1);
}
/* some more tests */
static void
test_mu_find_wrong_muhome()
{
auto res = run_command({MU_PROGRAM, "find", "--muhome",
join_paths("/foo", "bar", "nonexistent"), "f:socrates"});
assert_valid_result(res);
g_assert_cmpuint(res->exit_code,==,1); // general error
g_assert_cmpuint(count_nl(res->standard_err), >, 1);
}
static void
test_mu_find_links(void)
{
TempDir temp_dir;
{
auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home,
"--format", "links", "--linksdir", temp_dir.path(),
"mime:message/rfc822"});
assert_valid_result(res);
g_assert_cmpuint(res->exit_code,==,0);
g_assert_cmpuint(count_nl(res->standard_out),==,0);
g_assert_cmpuint(count_nl(res->standard_err),==,0);
}
/* furthermore, two symlinks should be there */
const auto f1{mu_format("{}/cur/rfc822.1", temp_dir)};
const auto f2{mu_format("{}/cur/rfc822.2", temp_dir)};
g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK);
g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK);
/* now we try again, we should get a line of error output,
* when we find the first target file already exists */
{
auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home,
"--format", "links", "--linksdir", temp_dir.path(),
"mime:message/rfc822"});
assert_valid_result(res);
g_assert_cmpuint(res->exit_code,==,1);
g_assert_cmpuint(count_nl(res->standard_out),==,0);
g_assert_cmpuint(count_nl(res->standard_err),==,1);
}
/* now we try again with --clearlinks, and the we should be
* back to 0 errors */
{
auto res = run_command({MU_PROGRAM, "find", "--muhome", test_mu_home,
"--format", "links", "--clearlinks", "--linksdir", temp_dir.path(),
"mime:message/rfc822"});
assert_valid_result(res);
g_assert_cmpuint(res->exit_code,==,0);
g_assert_cmpuint(count_nl(res->standard_out),==,0);
g_assert_cmpuint(count_nl(res->standard_err),==,0);
}
g_assert_cmpuint(determine_dtype(f1.c_str(), true), ==, DT_LNK);
g_assert_cmpuint(determine_dtype(f2.c_str(), true), ==, DT_LNK);
}
/* some more tests */
int
main(int argc, char* argv[])
{
mu_test_init(&argc, &argv);
if (!set_en_us_utf8_locale())
return 0; /* don't error out... */
TempDir temp_dir{};
{
test_mu_home = temp_dir.path();
auto res1 = run_command({MU_PROGRAM, "--quiet", "init",
"--muhome", test_mu_home, "--maildir" , MU_TESTMAILDIR2});
assert_valid_result(res1);
auto res2 = run_command({MU_PROGRAM, "--quiet", "index",
"--muhome", test_mu_home});
assert_valid_result(res2);
}
g_test_add_func("/cmd/find/empty-query", test_mu_find_empty_query);
g_test_add_func("/cmd/find/01", test_mu_find_01);
g_test_add_func("/cmd/find/02", test_mu_find_02);
g_test_add_func("/cmd/find/file", test_mu_find_file);
g_test_add_func("/cmd/find/mime", test_mu_find_mime);
g_test_add_func("/cmd/find/links", test_mu_find_links);
g_test_add_func("/cmd/find/text-in-rfc822", test_mu_find_text_in_rfc822);
g_test_add_func("/cmd/find/wrong-muhome", test_mu_find_wrong_muhome);
g_test_add_func("/cmd/find/maildir-special", test_mu_find_maildir_special);
return g_test_run();
}
#endif /*BUILD_TESTS*/
|