-# increments last_thread_id counter
-# returns a 64-bit integer represented as a decimal string
-sub next_thread_id {
- my ($self) = @_;
- my $db = $self->{xdb};
- my $last_thread_id = int($db->get_metadata('last_thread_id') || 0);
-
- $db->set_metadata('last_thread_id', ++$last_thread_id);
-
- $last_thread_id;
-}
-
-sub parse_references ($) {
- my ($smsg) = @_;
- my $doc = $smsg->{doc};
- my $mid = $smsg->mid;
- my $mime = $smsg->{mime};
- my $hdr = $mime->header_obj;
-
- # last References should be IRT, but some mail clients do things
- # out of order, so trust IRT over References iff IRT exists
- my @refs = (($hdr->header_raw('References') || '') =~ /<([^>]+)>/g);
- push(@refs, (($hdr->header_raw('In-Reply-To') || '') =~ /<([^>]+)>/g));
-
- if (@refs) {
- my %uniq = ($mid => 1);
- my @orig_refs = @refs;
- @refs = ();
-
- # prevent circular references via References: here:
- foreach my $ref (@orig_refs) {
- if (length($ref) > MAX_MID_SIZE) {
- warn "References: <$ref> too long, ignoring\n";
- }
- next if $uniq{$ref};
- $uniq{$ref} = 1;
- push @refs, $ref;
- }
- }
- $smsg->{references} = '<'.join('> <', @refs).'>' if @refs;
- \@refs
-}
-
-sub link_message {
- my ($self, $smsg, $refs, $old_tid) = @_;
- my $tid;
-
- if (@$refs) {
-
- # first ref *should* be the thread root,
- # but we can never trust clients to do the right thing
- my $ref = shift @$refs;
- $tid = $self->_resolve_mid_to_tid($ref);
- $self->merge_threads($tid, $old_tid) if defined $old_tid;
-
- # the rest of the refs should point to this tid:
- foreach $ref (@$refs) {
- my $ptid = $self->_resolve_mid_to_tid($ref);
- merge_threads($self, $tid, $ptid);
- }
- } else {
- $tid = defined $old_tid ? $old_tid : $self->next_thread_id;
- }
- $smsg->{doc}->add_term('G' . $tid);
-}
-