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);
22 my $ro = PublicInbox::Search->new($git_dir);
24 $rw->{xdb}->commit_transaction if $rw && $rw->{xdb};
25 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
26 $rw->_xdb_acquire->begin_transaction;
30 # git repository perms
31 is(PublicInbox::SearchIdx->_git_config_perm(undef),
32 &PublicInbox::SearchIdx::PERM_GROUP,
33 "undefined permission is group");
34 is(PublicInbox::SearchIdx::_umask_for(
35 PublicInbox::SearchIdx->_git_config_perm('0644')),
36 0022, "644 => umask(0022)");
37 is(PublicInbox::SearchIdx::_umask_for(
38 PublicInbox::SearchIdx->_git_config_perm('0600')),
39 0077, "600 => umask(0077)");
40 is(PublicInbox::SearchIdx::_umask_for(
41 PublicInbox::SearchIdx->_git_config_perm('0640')),
42 0027, "640 => umask(0027)");
43 is(PublicInbox::SearchIdx::_umask_for(
44 PublicInbox::SearchIdx->_git_config_perm('group')),
45 0007, 'group => umask(0007)');
46 is(PublicInbox::SearchIdx::_umask_for(
47 PublicInbox::SearchIdx->_git_config_perm('everybody')),
48 0002, 'everybody => umask(0002)');
49 is(PublicInbox::SearchIdx::_umask_for(
50 PublicInbox::SearchIdx->_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->lookup_message('<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');
96 ok(int($found->thread_id) > 0, 'thread_id is an integer');
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->{msgs}}, 1, "limit works");
111 my $first = $res->{msgs}->[0];
113 $res = $ro->query('s:"Hello world"', {offset => 1});
114 is(scalar @{$res->{msgs}}, 1, "offset works");
115 my $second = $res->{msgs}->[0];
117 isnt($first, $second, "offset returned different result from limit");
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 ok($ghost_id < $reply_id, "ghost vivified from earlier message");
154 # search thread on ghost
160 my $res = $ro->query('ghost');
161 my @exp = sort qw(ghost-message@s ghost-reply@s);
162 my @res = filter_mids($res);
163 is_deeply(\@res, \@exp, 'got expected results for Subject match');
166 $res = $ro->query('goodbye');
167 is($res->{msgs}->[0]->mid, 'last@s', 'got goodbye message body');
174 my $long_mid = 'last' . ('x' x 60). '@s';
176 my $long = Email::MIME->create(
178 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
179 Subject => 'long message ID',
180 'References' => '<root@s> <last@s>',
181 'In-Reply-To' => '<last@s>',
182 'Message-ID' => "<$long_mid>",
183 From => '"Long I.D." <long-id@example.com>',
184 To => 'list@example.com',
187 my $long_id = $rw->add_message($long);
188 is($long_id, int($long_id), "long_id is an integer: $long_id");
195 my $long_reply_mid = 'reply-to-long@1';
196 my $long_reply = Email::MIME->create(
198 Subject => 'I break references',
199 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
200 'Message-ID' => "<$long_reply_mid>",
202 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
203 'In-Reply-To' => "<$long_mid>",
204 From => '"no1 <no1@example.com>',
205 To => 'list@example.com',
207 body => "no References\n");
208 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
212 my $t = $ro->get_thread('root@s');
213 is($t->{total}, 4, "got all 4 mesages in thread");
214 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
215 @res = filter_mids($t);
216 is_deeply(\@res, \@exp, "get_thread works");
219 # quote prioritization
222 $rw->add_message(Email::MIME->create(
224 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
226 'Message-ID' => '<quote@a>',
227 From => 'Quoter <quoter@example.com>',
228 To => 'list@example.com',
230 body => "> theatre illusions\nfade\n"));
232 $rw->add_message(Email::MIME->create(
234 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
236 'Message-ID' => '<nquote@a>',
237 From => 'Non-Quoter<non-quoter@example.com>',
238 To => 'list@example.com',
240 body => "theatre\nfade\n"));
241 my $res = $rw->query("theatre");
242 is($res->{total}, 2, "got both matches");
243 is($res->{msgs}->[0]->mid, 'nquote@a', "non-quoted scores higher");
244 is($res->{msgs}->[1]->mid, 'quote@a', "quoted result still returned");
246 $res = $rw->query("illusions");
247 is($res->{total}, 1, "got a match for quoted text");
248 is($res->{msgs}->[0]->mid, 'quote@a',
249 "quoted result returned if nothing else");
252 # circular references
254 my $s = 'foo://'. ('Circle' x 15).'/foo';
255 my $doc_id = $rw->add_message(Email::MIME->create(
256 header => [ Subject => $s ],
258 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
259 'Message-ID' => '<circle@a>',
260 'References' => '<circle@a>',
261 'In-Reply-To' => '<circle@a>',
262 From => 'Circle <circle@example.com>',
263 To => 'list@example.com',
266 ok($doc_id > 0, "doc_id defined with circular reference");
267 my $smsg = $rw->lookup_message('circle@a');
268 is($smsg->references, '', "no references created");
269 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
270 is($s, $msg->subject, 'long subject not rewritten');
275 my $mbox = 't/utf8.mbox';
276 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
280 $str =~ s/\AFrom [^\n]+\n//s;
281 my $mime = Email::MIME->new($str);
282 my $doc_id = $rw->add_message($mime);
283 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
284 my $smsg = $rw->lookup_message('testmessage@example.com');
285 my $msg = PublicInbox::SearchMsg->load_doc($smsg->{doc});
287 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved');
291 my $res = $ro->query('d:19931002..20101002');
292 ok(scalar @{$res->{msgs}} > 0, 'got results within range');
293 $res = $ro->query('d:20101003..');
294 is(scalar @{$res->{msgs}}, 0, 'nothing after 20101003');
295 $res = $ro->query('d:..19931001');
296 is(scalar @{$res->{msgs}}, 0, 'nothing before 19931001');
299 # names and addresses
301 my $res = $ro->query('t:list@example.com');
302 is(scalar @{$res->{msgs}}, 6, 'searched To: successfully');
303 foreach my $smsg (@{$res->{msgs}}) {
304 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
307 $res = $ro->query('tc:list@example.com');
308 is(scalar @{$res->{msgs}}, 6, 'searched To+Cc: successfully');
309 foreach my $smsg (@{$res->{msgs}}) {
310 my $tocc = join("\n", $smsg->to, $smsg->cc);
311 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
314 foreach my $pfx ('tcf:', 'c:') {
315 $res = $ro->query($pfx . 'foo@example.com');
316 is(scalar @{$res->{msgs}}, 1,
317 "searched $pfx successfully for Cc:");
318 foreach my $smsg (@{$res->{msgs}}) {
319 like($smsg->cc, qr/\bfoo\@example\.com\b/,
324 foreach my $pfx ('', 'tcf:', 'f:') {
325 $res = $ro->query($pfx . 'Laggy');
326 is(scalar @{$res->{msgs}}, 1,
327 "searched $pfx successfully for From:");
328 foreach my $smsg (@{$res->{msgs}}) {
329 like($smsg->from, qr/Laggy Sender/,
330 "From appears with $pfx");
338 my $res = $ro->query('b:hello');
339 is(scalar @{$res->{msgs}}, 0, 'no match on body search only');
340 $res = $ro->query('bs:smith');
341 is(scalar @{$res->{msgs}}, 0,
342 'no match on body+subject search for From');
344 $res = $ro->query('q:theatre');
345 is(scalar @{$res->{msgs}}, 1, 'only one quoted body');
346 like($res->{msgs}->[0]->from, qr/\AQuoter/, 'got quoted body');
348 $res = $ro->query('nq:theatre');
349 is(scalar @{$res->{msgs}}, 1, 'only one non-quoted body');
350 like($res->{msgs}->[0]->from, qr/\ANon-Quoter/, 'got non-quoted body');
352 foreach my $pfx (qw(b: bs:)) {
353 $res = $ro->query($pfx . 'theatre');
354 is(scalar @{$res->{msgs}}, 2, "searched both bodies for $pfx");
355 like($res->{msgs}->[0]->from, qr/\ANon-Quoter/,
356 "non-quoter first for $pfx");
361 my $part1 = Email::MIME->create(
363 content_type => 'text/plain',
364 disposition => 'attachment',
365 charset => 'US-ASCII',
366 encoding => 'quoted-printable',
367 filename => 'attached_fart.txt',
369 body_str => 'inside the attachment',
371 my $part2 = Email::MIME->create(
373 content_type => 'text/plain',
374 disposition => 'attachment',
375 charset => 'US-ASCII',
376 encoding => 'quoted-printable',
377 filename => 'part_deux.txt',
379 body_str => 'inside another',
381 my $amsg = Email::MIME->create(
383 Subject => 'see attachment',
384 'Message-ID' => '<file@attached>',
385 From => 'John Smith <js@example.com>',
386 To => 'list@example.com',
388 parts => [ $part1, $part2 ],
390 ok($rw->add_message($amsg), 'added attachment');
393 my $n = $ro->query('n:attached_fart.txt');
394 is(scalar @{$n->{msgs}}, 1, 'got result for n:');
395 my $res = $ro->query('part_deux.txt');
396 is(scalar @{$res->{msgs}}, 1, 'got result without n:');
397 is($n->{msgs}->[0]->mid, $res->{msgs}->[0]->mid,
398 'same result with and without');
399 my $txt = $ro->query('"inside another"');
400 is($txt->{msgs}->[0]->mid, $res->{msgs}->[0]->mid,
401 'search inside text attachments works');