1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 eval { require PublicInbox::SearchIdx; };
7 plan skip_all => "Xapian missing for search" if $@;
8 use File::Temp qw/tempdir/;
10 my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
11 my $git_dir = "$tmpdir/a.git";
12 my ($root_id, $last_id);
14 is(0, system(qw(git init --shared -q --bare), $git_dir), "git init (main)");
15 eval { PublicInbox::Search->new($git_dir) };
16 ok($@, "exception raised on non-existent DB");
18 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
19 my $ibx = $rw->{-inbox};
20 $ibx->with_umask(sub {
25 my $ro = PublicInbox::Search->new($git_dir);
27 $rw->commit_txn_lazy if $rw;
28 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
33 # git repository perms
34 is($ibx->_git_config_perm(), &PublicInbox::InboxWritable::PERM_GROUP,
35 "undefined permission is group");
36 is(PublicInbox::InboxWritable::_umask_for(
37 PublicInbox::InboxWritable->_git_config_perm('0644')),
38 0022, "644 => umask(0022)");
39 is(PublicInbox::InboxWritable::_umask_for(
40 PublicInbox::InboxWritable->_git_config_perm('0600')),
41 0077, "600 => umask(0077)");
42 is(PublicInbox::InboxWritable::_umask_for(
43 PublicInbox::InboxWritable->_git_config_perm('0640')),
44 0027, "640 => umask(0027)");
45 is(PublicInbox::InboxWritable::_umask_for(
46 PublicInbox::InboxWritable->_git_config_perm('group')),
47 0007, 'group => umask(0007)');
48 is(PublicInbox::InboxWritable::_umask_for(
49 PublicInbox::InboxWritable->_git_config_perm('everybody')),
50 0002, 'everybody => umask(0002)');
51 is(PublicInbox::InboxWritable::_umask_for(
52 PublicInbox::InboxWritable->_git_config_perm('umask')),
53 umask, 'umask => existing umask');
56 $ibx->with_umask(sub {
57 my $root = Email::MIME->create(
59 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
60 Subject => 'Hello world',
61 'Message-ID' => '<root@s>',
62 From => 'John Smith <js@example.com>',
63 To => 'list@example.com',
66 my $last = Email::MIME->create(
68 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
69 Subject => 'Re: Hello world',
70 'In-Reply-To' => '<root@s>',
71 'Message-ID' => '<last@s>',
72 From => 'John Smith <js@example.com>',
73 To => 'list@example.com',
74 Cc => 'foo@example.com',
76 body => "goodbye forever :<\n");
80 $root_id = $rw->add_message($root);
81 is($root_id, int($root_id), "root_id is an integer: $root_id");
82 $last_id = $rw->add_message($last);
83 is($last_id, int($last_id), "last_id is an integer: $last_id");
88 sort(map { $_->mid } @$msgs);
94 my $found = $ro->query('m:root@s');
95 is(scalar(@$found), 1, "message found");
96 is($found->[0]->mid, 'root@s', 'mid set correctly') if scalar(@$found);
99 my @exp = sort qw(root@s last@s);
101 $res = $ro->query('s:(Hello world)');
102 @res = filter_mids($res);
103 is_deeply(\@res, \@exp, 'got expected results for s:() match');
105 $res = $ro->query('s:"Hello world"');
106 @res = filter_mids($res);
107 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
109 $res = $ro->query('s:"Hello world"', {limit => 1});
110 is(scalar @$res, 1, "limit works");
111 my $first = $res->[0];
113 $res = $ro->query('s:"Hello world"', {offset => 1});
114 is(scalar @$res, 1, "offset works");
115 my $second = $res->[0];
117 isnt($first, $second, "offset returned different result from limit");
121 $ibx->with_umask(sub {
123 my $rmid = '<ghost-message@s>';
124 my $reply_to_ghost = Email::MIME->create(
126 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
127 Subject => 'Re: ghosts',
128 'Message-ID' => '<ghost-reply@s>',
129 'In-Reply-To' => $rmid,
130 From => 'Time Traveler <tt@example.com>',
131 To => 'list@example.com',
136 my $reply_id = $rw->add_message($reply_to_ghost);
137 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
139 my $was_ghost = Email::MIME->create(
141 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
143 'Message-ID' => $rmid,
144 From => 'Laggy Sender <lag@example.com>',
145 To => 'list@example.com',
147 body => "are real\n");
149 my $ghost_id = $rw->add_message($was_ghost);
150 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
151 my $msgs = $rw->{over}->get_thread('ghost-message@s');
152 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
153 foreach (qw(sid tid)) {
154 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
156 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
157 ok($_->{num} > 0, 'positive art num') foreach @$msgs
160 # search thread on ghost
166 my $res = $ro->query('ghost');
167 my @exp = sort qw(ghost-message@s ghost-reply@s);
168 my @res = filter_mids($res);
169 is_deeply(\@res, \@exp, 'got expected results for Subject match');
172 $res = $ro->query('goodbye');
173 is(scalar(@$res), 1, "goodbye message found");
174 is($res->[0]->mid, 'last@s', 'got goodbye message body') if scalar(@$res);
177 $res = $ro->query('dt:20101002000001..20101002000001');
178 @res = filter_mids($res);
179 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
180 $res = $ro->query('dt:20101002000002..20101002000002');
181 is_deeply($res, [], 'exact Date: match down to the second');
185 $ibx->with_umask(sub {
188 my $long_mid = 'last' . ('x' x 60). '@s';
190 my $long = Email::MIME->create(
192 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
193 Subject => 'long message ID',
194 'References' => '<root@s> <last@s>',
195 'In-Reply-To' => '<last@s>',
196 'Message-ID' => "<$long_mid>",
197 From => '"Long I.D." <long-id@example.com>',
198 To => 'list@example.com',
201 my $long_id = $rw->add_message($long);
202 is($long_id, int($long_id), "long_id is an integer: $long_id");
209 my $long_reply_mid = 'reply-to-long@1';
210 my $long_reply = Email::MIME->create(
212 Subject => 'I break references',
213 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
214 'Message-ID' => "<$long_reply_mid>",
216 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
217 'In-Reply-To' => "<$long_mid>",
218 From => 'no1 <no1@example.com>',
219 To => 'list@example.com',
221 body => "no References\n");
222 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
226 my $t = $ro->get_thread('root@s');
227 is(scalar(@$t), 4, "got all 4 mesages in thread");
228 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
229 @res = filter_mids($t);
230 is_deeply(\@res, \@exp, "get_thread works");
233 # quote prioritization
234 $ibx->with_umask(sub {
236 $rw->add_message(Email::MIME->create(
238 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
240 'Message-ID' => '<quote@a>',
241 From => 'Quoter <quoter@example.com>',
242 To => 'list@example.com',
244 body => "> theatre illusions\nfade\n"));
246 $rw->add_message(Email::MIME->create(
248 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
250 'Message-ID' => '<nquote@a>',
251 From => 'Non-Quoter<non-quoter@example.com>',
252 To => 'list@example.com',
254 body => "theatre\nfade\n"));
255 my $res = $rw->query("theatre");
256 is(scalar(@$res), 2, "got both matches");
257 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher") if scalar(@$res);
258 is($res->[1]->mid, 'quote@a', "quoted result still returned") if scalar(@$res);
260 $res = $rw->query("illusions");
261 is(scalar(@$res), 1, "got a match for quoted text");
262 is($res->[0]->mid, 'quote@a',
263 "quoted result returned if nothing else") if scalar(@$res);
266 # circular references
267 $ibx->with_umask(sub {
268 my $s = 'foo://'. ('Circle' x 15).'/foo';
269 my $doc_id = $rw->add_message(Email::MIME->create(
270 header => [ Subject => $s ],
272 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
273 'Message-ID' => '<circle@a>',
274 'References' => '<circle@a>',
275 'In-Reply-To' => '<circle@a>',
276 From => 'Circle <circle@example.com>',
277 To => 'list@example.com',
280 ok($doc_id > 0, "doc_id defined with circular reference");
281 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
282 is(defined($smsg), 1, 'found m:circl@a');
283 is($smsg->references, '', "no references created") if defined($smsg);
284 is($smsg->subject, $s, 'long subject not rewritten') if defined($smsg);
287 $ibx->with_umask(sub {
289 my $mbox = 't/utf8.mbox';
290 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
294 $str =~ s/\AFrom [^\n]+\n//s;
295 my $mime = Email::MIME->new($str);
296 my $doc_id = $rw->add_message($mime);
297 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
298 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
299 is(defined($msg), 1, 'found testmessage@example.com');
300 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved') if defined($msg);
304 my $msgs = $ro->query('d:19931002..20101002');
305 ok(scalar(@$msgs) > 0, 'got results within range');
306 $msgs = $ro->query('d:20101003..');
307 is(scalar(@$msgs), 0, 'nothing after 20101003');
308 $msgs = $ro->query('d:..19931001');
309 is(scalar(@$msgs), 0, 'nothing before 19931001');
312 # names and addresses
314 my $mset = $ro->query('t:list@example.com', {mset => 1});
315 is($mset->size, 6, 'searched To: successfully');
316 foreach my $m ($mset->items) {
317 my $smsg = $ro->lookup_article($m->get_docid);
318 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
321 $mset = $ro->query('tc:list@example.com', {mset => 1});
322 is($mset->size, 6, 'searched To+Cc: successfully');
323 foreach my $m ($mset->items) {
324 my $smsg = $ro->lookup_article($m->get_docid);
325 my $tocc = join("\n", $smsg->to, $smsg->cc);
326 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
329 foreach my $pfx ('tcf:', 'c:') {
330 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
331 is($mset->items, 1, "searched $pfx successfully for Cc:");
332 foreach my $m ($mset->items) {
333 my $smsg = $ro->lookup_article($m->get_docid);
334 like($smsg->cc, qr/\bfoo\@example\.com\b/,
339 foreach my $pfx ('', 'tcf:', 'f:') {
340 my $res = $ro->query($pfx . 'Laggy');
342 "searched $pfx successfully for From:");
343 foreach my $smsg (@$res) {
344 like($smsg->from_name, qr/Laggy Sender/,
345 "From appears with $pfx");
353 my $res = $ro->query('b:hello');
354 is(scalar(@$res), 0, 'no match on body search only');
355 $res = $ro->query('bs:smith');
357 'no match on body+subject search for From');
359 $res = $ro->query('q:theatre');
360 is(scalar(@$res), 1, 'only one quoted body');
361 like($res->[0]->from_name, qr/\AQuoter/,
362 'got quoted body') if (scalar(@$res));
364 $res = $ro->query('nq:theatre');
365 is(scalar @$res, 1, 'only one non-quoted body');
366 like($res->[0]->from_name, qr/\ANon-Quoter/,
367 'got non-quoted body') if (scalar(@$res));
369 foreach my $pfx (qw(b: bs:)) {
370 $res = $ro->query($pfx . 'theatre');
371 is(scalar @$res, 2, "searched both bodies for $pfx");
372 like($res->[0]->from_name, qr/\ANon-Quoter/,
373 "non-quoter first for $pfx") if scalar(@$res);
377 $ibx->with_umask(sub {
378 my $part1 = Email::MIME->create(
380 content_type => 'text/plain',
381 disposition => 'attachment',
382 charset => 'US-ASCII',
383 encoding => 'quoted-printable',
384 filename => 'attached_fart.txt',
386 body_str => 'inside the attachment',
388 my $part2 = Email::MIME->create(
390 content_type => 'text/plain',
391 disposition => 'attachment',
392 charset => 'US-ASCII',
393 encoding => 'quoted-printable',
394 filename => 'part_deux.txt',
396 body_str => 'inside another',
398 my $amsg = Email::MIME->create(
400 Subject => 'see attachment',
401 'Message-ID' => '<file@attached>',
402 From => 'John Smith <js@example.com>',
403 To => 'list@example.com',
405 parts => [ $part1, $part2 ],
407 ok($rw->add_message($amsg), 'added attachment');
410 my $n = $ro->query('n:attached_fart.txt');
411 is(scalar @$n, 1, 'got result for n:');
412 my $res = $ro->query('part_deux.txt');
413 is(scalar @$res, 1, 'got result without n:');
414 is($n->[0]->mid, $res->[0]->mid,
415 'same result with and without') if scalar(@$res);
416 my $txt = $ro->query('"inside another"');
417 is(scalar @$txt, 1, 'found inside another');
418 is($txt->[0]->mid, $res->[0]->mid,
419 'search inside text attachments works') if scalar(@$txt);
422 if (scalar(@$n) >= 1) {
423 my $mid = $n->[0]->mid;
425 $art = $ro->next_by_mid($mid, \$id, \$prev);
426 ok($art, 'article exists in OVER DB');
428 $rw->unindex_blob($amsg);
429 $rw->commit_txn_lazy;
430 is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB') if defined($art);
433 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
434 "$git_dir/public-inbox",
435 glob("$git_dir/public-inbox/xapian*/"),
436 glob("$git_dir/public-inbox/xapian*/*")) {
438 my ($bn) = (split(m!/!, $f))[-1];
439 is($st[2] & 07777, -f _ ? 0660 : 02770,
440 "sharedRepository respected for $bn");
443 $ibx->with_umask(sub {
445 my $digits = '10010260936330';
446 my $ua = 'Pine.LNX.4.10';
447 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
448 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
450 my $pine = Email::MIME->create(
453 'Message-ID' => "<$mid>",
454 From => 'torvalds@transmeta',
455 To => 'list@example.com',
459 my $x = $rw->add_message($pine);
460 $rw->commit_txn_lazy;
461 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
462 'searching only digit yielded result');
467 is($ro->query("m:$wild*", { mset => 1})->size, 1,
468 "searching chopped($i) digit yielded result $wild ");
470 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);