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 -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);
21 my $ibx = $rw->{-inbox};
23 my $ro = PublicInbox::Search->new($git_dir);
25 $rw->commit_txn_lazy if $rw;
26 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
31 # git repository perms
32 is($ibx->_git_config_perm(), &PublicInbox::InboxWritable::PERM_GROUP,
33 "undefined permission is group");
34 is(PublicInbox::InboxWritable::_umask_for(
35 PublicInbox::InboxWritable->_git_config_perm('0644')),
36 0022, "644 => umask(0022)");
37 is(PublicInbox::InboxWritable::_umask_for(
38 PublicInbox::InboxWritable->_git_config_perm('0600')),
39 0077, "600 => umask(0077)");
40 is(PublicInbox::InboxWritable::_umask_for(
41 PublicInbox::InboxWritable->_git_config_perm('0640')),
42 0027, "640 => umask(0027)");
43 is(PublicInbox::InboxWritable::_umask_for(
44 PublicInbox::InboxWritable->_git_config_perm('group')),
45 0007, 'group => umask(0007)');
46 is(PublicInbox::InboxWritable::_umask_for(
47 PublicInbox::InboxWritable->_git_config_perm('everybody')),
48 0002, 'everybody => umask(0002)');
49 is(PublicInbox::InboxWritable::_umask_for(
50 PublicInbox::InboxWritable->_git_config_perm('umask')),
51 umask, 'umask => existing umask');
55 my $root = Email::MIME->create(
57 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
58 Subject => 'Hello world',
59 'Message-ID' => '<root@s>',
60 From => 'John Smith <js@example.com>',
61 To => 'list@example.com',
64 my $last = Email::MIME->create(
66 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
67 Subject => 'Re: Hello world',
68 'In-Reply-To' => '<root@s>',
69 'Message-ID' => '<last@s>',
70 From => 'John Smith <js@example.com>',
71 To => 'list@example.com',
72 Cc => 'foo@example.com',
74 body => "goodbye forever :<\n");
78 $root_id = $rw->add_message($root);
79 is($root_id, int($root_id), "root_id is an integer: $root_id");
80 $last_id = $rw->add_message($last);
81 is($last_id, int($last_id), "last_id is an integer: $last_id");
86 sort(map { $_->mid } @{$res->{msgs}});
92 my $found = $ro->first_smsg_by_mid('root@s');
93 ok($found, "message found");
94 is($root_id, $found->{doc_id}, 'doc_id set correctly');
95 is($found->mid, 'root@s', 'mid set correctly');
98 my @exp = sort qw(root@s last@s);
100 $res = $ro->query('s:(Hello world)');
101 @res = filter_mids($res);
102 is_deeply(\@res, \@exp, 'got expected results for s:() match');
104 $res = $ro->query('s:"Hello world"');
105 @res = filter_mids($res);
106 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
108 $res = $ro->query('s:"Hello world"', {limit => 1});
109 is(scalar @{$res->{msgs}}, 1, "limit works");
110 my $first = $res->{msgs}->[0];
112 $res = $ro->query('s:"Hello world"', {offset => 1});
113 is(scalar @{$res->{msgs}}, 1, "offset works");
114 my $second = $res->{msgs}->[0];
116 isnt($first, $second, "offset returned different result from limit");
122 my $rmid = '<ghost-message@s>';
123 my $reply_to_ghost = Email::MIME->create(
125 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
126 Subject => 'Re: ghosts',
127 'Message-ID' => '<ghost-reply@s>',
128 'In-Reply-To' => $rmid,
129 From => 'Time Traveler <tt@example.com>',
130 To => 'list@example.com',
135 my $reply_id = $rw->add_message($reply_to_ghost);
136 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
138 my $was_ghost = Email::MIME->create(
140 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
142 'Message-ID' => $rmid,
143 From => 'Laggy Sender <lag@example.com>',
144 To => 'list@example.com',
146 body => "are real\n");
148 my $ghost_id = $rw->add_message($was_ghost);
149 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
150 my $msgs = $rw->{over}->get_thread('ghost-message@s')->{msgs};
151 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
152 foreach (qw(sid tid)) {
153 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
155 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
156 ok($_->{num} > 0, 'positive art num') foreach @$msgs
159 # search thread on ghost
165 my $res = $ro->query('ghost');
166 my @exp = sort qw(ghost-message@s ghost-reply@s);
167 my @res = filter_mids($res);
168 is_deeply(\@res, \@exp, 'got expected results for Subject match');
171 $res = $ro->query('goodbye');
172 is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
179 my $long_mid = 'last' . ('x' x 60). '@s';
181 my $long = Email::MIME->create(
183 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
184 Subject => 'long message ID',
185 'References' => '<root@s> <last@s>',
186 'In-Reply-To' => '<last@s>',
187 'Message-ID' => "<$long_mid>",
188 From => '"Long I.D." <long-id@example.com>',
189 To => 'list@example.com',
192 my $long_id = $rw->add_message($long);
193 is($long_id, int($long_id), "long_id is an integer: $long_id");
200 my $long_reply_mid = 'reply-to-long@1';
201 my $long_reply = Email::MIME->create(
203 Subject => 'I break references',
204 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
205 'Message-ID' => "<$long_reply_mid>",
207 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
208 'In-Reply-To' => "<$long_mid>",
209 From => '"no1 <no1@example.com>',
210 To => 'list@example.com',
212 body => "no References\n");
213 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
217 my $t = $ro->get_thread('root@s');
218 is($t->{total}, 4, "got all 4 mesages in thread");
219 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
220 @res = filter_mids($t);
221 is_deeply(\@res, \@exp, "get_thread works");
224 # quote prioritization
227 $rw->add_message(Email::MIME->create(
229 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
231 'Message-ID' => '<quote@a>',
232 From => 'Quoter <quoter@example.com>',
233 To => 'list@example.com',
235 body => "> theatre illusions\nfade\n"));
237 $rw->add_message(Email::MIME->create(
239 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
241 'Message-ID' => '<nquote@a>',
242 From => 'Non-Quoter<non-quoter@example.com>',
243 To => 'list@example.com',
245 body => "theatre\nfade\n"));
246 my $res = $rw->query("theatre");
247 is($res->{total}, 2, "got both matches");
248 is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
249 is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
251 $res = $rw->query("illusions");
252 is($res->{total}, 1, "got a match for quoted text");
253 is($res->{msgs}->[0]->mid, 'quote@a',
254 "quoted result returned if nothing else");
257 # circular references
259 my $s = 'foo://'. ('Circle' x 15).'/foo';
260 my $doc_id = $rw->add_message(Email::MIME->create(
261 header => [ Subject => $s ],
263 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
264 'Message-ID' => '<circle@a>',
265 'References' => '<circle@a>',
266 'In-Reply-To' => '<circle@a>',
267 From => 'Circle <circle@example.com>',
268 To => 'list@example.com',
271 ok($doc_id > 0, "doc_id defined with circular reference");
272 my $smsg = $rw->first_smsg_by_mid('circle@a');
273 is($smsg->references, '', "no references created");
274 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
275 is($s, $msg->subject, 'long subject not rewritten');
280 my $mbox = 't/utf8.mbox';
281 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
285 $str =~ s/\AFrom [^\n]+\n//s;
286 my $mime = Email::MIME->new($str);
287 my $doc_id = $rw->add_message($mime);
288 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
289 my $smsg = $rw->first_smsg_by_mid('testmessage@example.com');
290 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
292 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved');
296 my $res = $ro->query('d:19931002..20101002');
297 ok(scalar @{$res->{msgs}} > 0, 'got results within range');
298 $res = $ro->query('d:20101003..');
299 is(scalar @{$res->{msgs}}, 0, 'nothing after 20101003');
300 $res = $ro->query('d:..19931001');
301 is(scalar @{$res->{msgs}}, 0, 'nothing before 19931001');
304 # names and addresses
306 my $res = $ro->query('t:list@example.com');
307 is(scalar @{$res->{msgs}}, 6, 'searched To: successfully');
308 foreach my $smsg (@{$res->{msgs}}) {
309 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
312 $res = $ro->query('tc:list@example.com');
313 is(scalar @{$res->{msgs}}, 6, 'searched To+Cc: successfully');
314 foreach my $smsg (@{$res->{msgs}}) {
315 my $tocc = join("\n", $smsg->to, $smsg->cc);
316 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
319 foreach my $pfx ('tcf:', 'c:') {
320 $res = $ro->query($pfx . 'foo@example.com');
321 is(scalar @{$res->{msgs}}, 1,
322 "searched $pfx successfully for Cc:");
323 foreach my $smsg (@{$res->{msgs}}) {
324 like($smsg->cc, qr/\bfoo\@example\.com\b/,
329 foreach my $pfx ('', 'tcf:', 'f:') {
330 $res = $ro->query($pfx . 'Laggy');
331 is(scalar @{$res->{msgs}}, 1,
332 "searched $pfx successfully for From:");
333 foreach my $smsg (@{$res->{msgs}}) {
334 like($smsg->from, qr/Laggy Sender/,
335 "From appears with $pfx");
343 my $res = $ro->query('b:hello');
344 is(scalar @{$res->{msgs}}, 0, 'no match on body search only');
345 $res = $ro->query('bs:smith');
346 is(scalar @{$res->{msgs}}, 0,
347 'no match on body+subject search for From');
349 $res = $ro->query('q:theatre');
350 is(scalar @{$res->{msgs}}, 1, 'only one quoted body');
351 like($res->{msgs}->[0]->from, qr/\AQuoter/, 'got quoted body');
353 $res = $ro->query('nq:theatre');
354 is(scalar @{$res->{msgs}}, 1, 'only one non-quoted body');
355 like($res->{msgs}->[0]->from, qr/\ANon-Quoter/, 'got non-quoted body');
357 foreach my $pfx (qw(b: bs:)) {
358 $res = $ro->query($pfx . 'theatre');
359 is(scalar @{$res->{msgs}}, 2, "searched both bodies for $pfx");
360 like($res->{msgs}->[0]->from, qr/\ANon-Quoter/,
361 "non-quoter first for $pfx");
366 my $part1 = Email::MIME->create(
368 content_type => 'text/plain',
369 disposition => 'attachment',
370 charset => 'US-ASCII',
371 encoding => 'quoted-printable',
372 filename => 'attached_fart.txt',
374 body_str => 'inside the attachment',
376 my $part2 = Email::MIME->create(
378 content_type => 'text/plain',
379 disposition => 'attachment',
380 charset => 'US-ASCII',
381 encoding => 'quoted-printable',
382 filename => 'part_deux.txt',
384 body_str => 'inside another',
386 my $amsg = Email::MIME->create(
388 Subject => 'see attachment',
389 'Message-ID' => '<file@attached>',
390 From => 'John Smith <js@example.com>',
391 To => 'list@example.com',
393 parts => [ $part1, $part2 ],
395 ok($rw->add_message($amsg), 'added attachment');
398 my $n = $ro->query('n:attached_fart.txt');
399 is(scalar @{$n->{msgs}}, 1, 'got result for n:');
400 my $res = $ro->query('part_deux.txt');
401 is(scalar @{$res->{msgs}}, 1, 'got result without n:');
402 is($n->{msgs}->[0]->mid, $res->{msgs}->[0]->mid,
403 'same result with and without');
404 my $txt = $ro->query('"inside another"');
405 is($txt->{msgs}->[0]->mid, $res->{msgs}->[0]->mid,
406 'search inside text attachments works');
408 $rw->commit_txn_lazy;