about summary refs log tree commit homepage
path: root/lib/PublicInbox/SearchMsg.pm
blob: 9dcc1e6dc5e54b8b9ddcebcf5260c63b6cfcb661 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# Copyright (C) 2015-2016 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# based on notmuch, but with no concept of folders, files or flags
#
# Wraps a document inside our Xapian search index.
package PublicInbox::SearchMsg;
use strict;
use warnings;
use Search::Xapian;
use POSIX qw//;
use Date::Parse qw/str2time/;
use PublicInbox::MID qw/mid_clean/;
use PublicInbox::Address;
our $PFX2TERM_RE = undef;
use constant EPOCH_822 => 'Thu, 01 Jan 1970 00:00:00 +0000';
use POSIX qw(strftime);

sub new {
	my ($class, $mime) = @_;
	my $doc = Search::Xapian::Document->new;
	$doc->add_term(PublicInbox::Search::xpfx('type') . 'mail');

	bless { type => 'mail', doc => $doc, mime => $mime }, $class;
}

sub wrap {
	my ($class, $doc, $mid) = @_;
	bless { doc => $doc, mime => undef, mid => $mid }, $class;
}

sub get_val ($$) {
	my ($doc, $col) = @_;
	Search::Xapian::sortable_unserialise($doc->get_value($col));
}

sub load_doc {
	my ($class, $doc) = @_;
	my $data = $doc->get_data or return;
	my $ts = get_val($doc, &PublicInbox::Search::TS);
	utf8::decode($data);
	my ($subj, $from, $refs, $to, $cc, $blob) = split(/\n/, $data);
	bless {
		doc => $doc,
		subject => $subj,
		ts => $ts,
		from => $from,
		references => $refs,
		to => $to,
		cc => $cc,
		blob => $blob,
	}, $class;
}

# :bytes and :lines metadata in RFC 3977
sub bytes ($) { get_val($_[0]->{doc}, &PublicInbox::Search::BYTES) }
sub lines ($) { get_val($_[0]->{doc}, &PublicInbox::Search::LINES) }
sub num ($) { get_val($_[0]->{doc}, &PublicInbox::Search::NUM) }

sub __hdr ($$) {
	my ($self, $field) = @_;
	my $val = $self->{$field};
	return $val if defined $val;

	my $mime = $self->{mime} or return;
	$val = $mime->header($field);
	$val = '' unless defined $val;
	$val =~ tr/\n/ /;
	$val =~ tr/\r//d;
	$self->{$field} = $val;
}

sub subject ($) { __hdr($_[0], 'subject') }
sub to ($) { __hdr($_[0], 'to') }
sub cc ($) { __hdr($_[0], 'cc') }

sub date ($) {
	my ($self) = @_;
	my $date = __hdr($self, 'date');
	return $date if defined $date;
	my $ts = $self->{ts};
	return unless defined $ts;
	$self->{date} = strftime('%a, %d %b %Y %T +0000', gmtime($ts));
}

sub from ($) {
	my ($self) = @_;
	my $from = __hdr($self, 'from');
	if (defined $from && !defined $self->{from_name}) {
		my @n = PublicInbox::Address::names($from);
		$self->{from_name} = join(', ', @n);
	}
	$from;
}

sub from_name {
	my ($self) = @_;
	my $from_name = $self->{from_name};
	return $from_name if defined $from_name;
	$self->from;
	$self->{from_name};
}

sub ts {
	my ($self) = @_;
	$self->{ts} ||= eval { str2time($self->mime->header('Date')) } || 0;
}

sub to_doc_data {
	my ($self, $blob) = @_;
	my @rows = ($self->subject, $self->from, $self->references,
			$self->to, $self->cc);
	push @rows, $blob if defined $blob;
	join("\n", @rows);
}

sub references {
	my ($self) = @_;
	my $x = $self->{references};
	defined $x ? $x : '';
}

sub ensure_metadata {
	my ($self) = @_;
	my $doc = $self->{doc};
	my $end = $doc->termlist_end;

	unless (defined $PFX2TERM_RE) {
		my $or = join('|', keys %PublicInbox::Search::PFX2TERM_RMAP);
		$PFX2TERM_RE = qr/\A($or)/;
	}

	while (my ($pfx, $field) = each %PublicInbox::Search::PFX2TERM_RMAP) {
		# ideally we'd move this out of the loop:
		my $i = $doc->termlist_begin;

		$i->skip_to($pfx);
		if ($i != $end) {
			my $val = $i->get_termname;

			if ($val =~ s/$PFX2TERM_RE//o) {
				$self->{$field} = $val;
			}
		}
	}
}

sub mid ($;$) {
	my ($self, $mid) = @_;

	if (defined $mid) {
		$self->{mid} = $mid;
	} elsif (my $rv = $self->{mid}) {
		$rv;
	} else {
		$self->ensure_metadata; # needed for ghosts
		$self->{mid} ||= $self->_extract_mid;
	}
}

sub _extract_mid { mid_clean(mid_mime($_[0]->mime)) }

sub blob {
	my ($self, $x40) = @_;
	if (defined $x40) {
		$self->{blob} = $x40;
	} else {
		$self->{blob};
	}
}

sub mime {
	my ($self, $mime) = @_;
	if (defined $mime) {
		$self->{mime} = $mime;
	} else {
		# TODO load from git
		$self->{mime};
	}
}

sub doc_id {
	my ($self, $doc_id) = @_;
	if (defined $doc_id) {
		$self->{doc_id} = $doc_id;
	} else {
		# TODO load from xapian
		$self->{doc_id};
	}
}

sub thread_id {
	my ($self) = @_;
	my $tid = $self->{thread};
	return $tid if defined $tid;
	$self->ensure_metadata;
	$self->{thread};
}

sub path {
	my ($self) = @_;
	my $path = $self->{path};
	return $path if defined $path;
	$self->ensure_metadata;
	$self->{path};
}

1;