+sub index_xapian { # msg_iter callback
+ my ($part, $depth, @idx) = @{$_[0]};
+ my ($self, $doc) = @{$_[1]};
+ my $ct = $part->content_type || 'text/plain';
+ my $fn = $part->filename;
+ if (defined $fn && $fn ne '') {
+ index_text($self, $fn, 1, 'XFN');
+ }
+
+ my ($s, undef) = msg_part_text($part, $ct);
+ defined $s or return;
+
+ # split off quoted and unquoted blocks:
+ my @sections = split(/((?:^>[^\n]*\n)+)/sm, $s);
+ $part = $s = undef;
+ index_body($self, $_, /\A>/ ? 0 : $doc) for @sections;
+}
+
+sub add_xapian ($$$$) {
+ my ($self, $mime, $smsg, $mids) = @_;
+ $smsg->{mime} = $mime; # XXX dangerous
+ my $hdr = $mime->header_obj;
+ my $doc = $X->{Document}->new;
+ my $subj = $smsg->subject;
+ add_val($doc, PublicInbox::Search::TS(), $smsg->{ts});
+ my @ds = gmtime($smsg->{ds});
+ my $yyyymmdd = strftime('%Y%m%d', @ds);
+ add_val($doc, PublicInbox::Search::YYYYMMDD(), $yyyymmdd);
+ my $dt = strftime('%Y%m%d%H%M%S', @ds);
+ add_val($doc, PublicInbox::Search::DT(), $dt);
+
+ my $tg = term_generator($self);
+
+ $tg->set_document($doc);
+ index_text($self, $subj, 1, 'S') if $subj;
+ index_users($self, $smsg);
+
+ msg_iter($mime, \&index_xapian, [ $self, $doc ]);
+ foreach my $mid (@$mids) {
+ index_text($self, $mid, 1, 'XM');
+
+ # because too many Message-IDs are prefixed with
+ # "Pine.LNX."...
+ if ($mid =~ /\w{12,}/) {
+ my @long = ($mid =~ /(\w{3,}+)/g);
+ index_text($self, join(' ', @long), 1, 'XM');
+ }
+ }
+ $smsg->{to} = $smsg->{cc} = ''; # WWW doesn't need these, only NNTP
+ PublicInbox::OverIdx::parse_references($smsg, $hdr, $mids);
+ my $data = $smsg->to_doc_data;
+ $doc->set_data($data);
+ if (my $altid = $self->{-altid}) {
+ foreach my $alt (@$altid) {
+ my $pfx = $alt->{xprefix};
+ foreach my $mid (@$mids) {
+ my $id = $alt->mid2alt($mid);
+ next unless defined $id;
+ $doc->add_boolean_term($pfx . $id);
+ }
+ }
+ }
+ $doc->add_boolean_term('Q' . $_) foreach @$mids;
+ $self->{xdb}->replace_document($smsg->{num}, $doc);
+}
+
+sub _msgmap_init ($) {
+ my ($self) = @_;
+ die "BUG: _msgmap_init is only for v1\n" if $self->{ibx_ver} != 1;
+ $self->{mm} //= eval {
+ require PublicInbox::Msgmap;
+ PublicInbox::Msgmap->new($self->{inboxdir}, 1);
+ };
+}
+
+sub add_message {
+ # mime = Email::MIME object
+ my ($self, $mime, $smsg) = @_;
+ my $hdr = $mime->header_obj;
+ my $mids = mids_for_index($hdr);
+ $smsg //= bless { blob => '' }, 'PublicInbox::Smsg'; # test-only compat
+ $smsg->{mid} //= $mids->[0]; # v1 compatibility
+ $smsg->{num} //= do { # v1
+ _msgmap_init($self);
+ index_mm($self, $mime);
+ };
+
+ # v1 and tests only:
+ $smsg->{ds} //= msg_datestamp($hdr, $self->{autime});
+ $smsg->{ts} //= msg_timestamp($hdr, $self->{cotime});