6 |
use warnings; |
use warnings; |
7 |
use Carp; |
use Carp; |
8 |
|
|
9 |
|
use Mail::Box::Manager; |
10 |
|
use Config::IniFiles; |
11 |
|
use POSIX qw(strftime); |
12 |
|
#use MWS_plucene; |
13 |
|
use MWS_swish; |
14 |
|
|
15 |
require Exporter; |
require Exporter; |
16 |
|
|
17 |
our @ISA = qw(Exporter); |
our @ISA = qw(Exporter); |
25 |
|
|
26 |
my $folder; # placeholder for folders |
my $folder; # placeholder for folders |
27 |
|
|
28 |
my $debug = 1; |
my $debug = 2; |
29 |
|
|
30 |
sub new { |
sub new { |
31 |
my $class = shift; |
my $class = shift; |
32 |
my $self = {}; |
my $self = {}; |
33 |
bless($self, $class); |
bless($self, $class); |
34 |
|
|
35 |
my $index_file = shift || die "need index file"; |
my $config_file = shift || die "need index file"; |
36 |
|
|
37 |
|
$self->{config} = new Config::IniFiles( -file => $config_file ); |
38 |
|
|
39 |
$self->{index} = Plucene::Simple->open($index_file) || die "can't open index '$index_file': $!"; |
my $index_file = $self->{config}->val('global', 'index') || croak "can't find [index] section in config file with path of index"; |
40 |
|
|
41 |
$self->{mgr} = Mail::Box::Manager->new; |
$self->{mgr} = Mail::Box::Manager->new(access => 'r'); |
42 |
|
$self->{index_file} = $index_file; |
43 |
|
|
44 |
# placeholder for opened folders |
# placeholder for opened folders |
45 |
$self->{folder} = {}; |
$self->{folder} = {}; |
47 |
return $self; |
return $self; |
48 |
} |
} |
49 |
|
|
50 |
|
sub open_folder { |
51 |
|
my $self = shift; |
52 |
|
|
53 |
|
my $mbox = shift || croak "open_folder needs mbox name"; |
54 |
|
|
55 |
|
if (! $self->{folder}->{$mbox}) { |
56 |
|
my $mbox_path = $self->{config}->val('folders', $mbox) || croak "comeone removed folder $mbox from config?"; |
57 |
|
print STDERR "about to open_folder($mbox)\n" if ($debug == 2); |
58 |
|
$self->{folder}->{$mbox} = $self->{mgr}->open($mbox_path) || croak "can't open folder $mbox at '$mbox_path': $!"; |
59 |
|
print STDERR "open_folder($mbox)\n" if ($debug); |
60 |
|
} |
61 |
|
|
62 |
|
return $self->{folder}->{$mbox}; |
63 |
|
|
64 |
|
} |
65 |
|
|
66 |
sub fetch_message { |
sub fetch_message { |
67 |
my $self = shift; |
my $self = shift; |
69 |
my $mbox_id = shift || die "need mbox_id!"; |
my $mbox_id = shift || die "need mbox_id!"; |
70 |
my ($mbox,$id) = split(/ /,$mbox_id); |
my ($mbox,$id) = split(/ /,$mbox_id); |
71 |
|
|
72 |
if (! $self->{folder}->{$mbox}) { |
# return message with ID |
73 |
$self->{folder}->{$mbox} = $self->{mgr}->open($mbox); |
print STDERR "fetch $id from $mbox\n" if ($debug); |
74 |
print STDERR "## open($mbox)\n" if ($debug); |
return $self->open_folder($mbox)->find($id) || |
|
} |
|
|
|
|
|
my $message = $self->{folder}->{$mbox}->find($id) || |
|
75 |
print STDERR "can't find message $id in $mbox. Time to re-index?\n"; |
print STDERR "can't find message $id in $mbox. Time to re-index?\n"; |
|
|
|
|
return $message; |
|
76 |
} |
} |
77 |
|
|
78 |
|
|
81 |
|
|
82 |
my $s = shift || carp "search called without argument!"; |
my $s = shift || carp "search called without argument!"; |
83 |
|
|
84 |
my @index_ids = $self->{index}->search($s); |
print STDERR "search_index($s)\n" if ($debug == 2); |
85 |
|
my @index_ids = $self->search_index($s); |
86 |
|
|
87 |
$self->{'index_ids'} = \@index_ids; |
$self->{'index_ids'} = \@index_ids; |
88 |
|
|
91 |
|
|
92 |
$self->{'curr_result'} = 0; |
$self->{'curr_result'} = 0; |
93 |
|
|
94 |
|
print STDERR "$results results\n" if ($debug == 2); |
95 |
|
|
96 |
return $results || 'error'; |
return $results || 'error'; |
97 |
} |
} |
98 |
|
|
99 |
sub unroll($$$) { |
sub unroll($$$) { |
100 |
|
my $self = shift; |
101 |
|
|
102 |
my ($message,$part,$sub) = @_; |
my ($message,$part,$sub) = @_; |
103 |
|
|
104 |
my @arr; |
my @arr; |
105 |
|
|
106 |
foreach my $from ($message->$part) { |
foreach my $from ($message->$part) { |
107 |
push @arr, $from->$sub; |
my $tmp = $from->$sub; |
108 |
|
$tmp =~ s/^\s*["'](.*)["']\s*$/$1/; |
109 |
|
push @arr, $tmp; |
110 |
} |
} |
111 |
return \@arr; |
return \@arr; |
112 |
} |
} |
113 |
|
|
114 |
|
sub fetch_all_results { |
115 |
|
my $self = shift; |
116 |
|
|
117 |
|
croak "results called before search!" if (! $self->{'index_ids'}); |
118 |
|
|
119 |
|
print STDERR "fetch_all_results_results\n" if ($debug == 2); |
120 |
|
|
121 |
|
my @arr; |
122 |
|
|
123 |
|
foreach my $id (@{$self->{'index_ids'}}) { |
124 |
|
push @arr, $self->fetch_result_by_id($id); |
125 |
|
} |
126 |
|
|
127 |
|
return @arr; |
128 |
|
} |
129 |
|
|
130 |
sub fetch_result { |
sub fetch_result { |
131 |
my $self = shift; |
my $self = shift; |
132 |
|
|
133 |
|
my $args = {@_}; |
134 |
|
|
135 |
croak "results called before search!" if (! $self->{'index_ids'}); |
croak "results called before search!" if (! $self->{'index_ids'}); |
136 |
|
|
137 |
my $curr = $self->{'curr_result'}++; |
my $curr = $self->{'curr_result'}++; |
138 |
|
|
139 |
my $id = $self->{'index_ids'}->[$curr]; |
my $id = $self->{'index_ids'}->[$curr]; |
140 |
|
|
141 |
return if (! $id); |
return $self->fetch_result_by_id($id); |
142 |
|
} |
143 |
|
|
144 |
|
sub plain_text_body { |
145 |
|
my $self = shift; |
146 |
|
my $message = shift || croak "plain_text_body needs message!"; |
147 |
|
|
148 |
|
if (! $message->isMultipart) { |
149 |
|
return $message->decoded->string; |
150 |
|
} else { |
151 |
|
foreach my $part ($message->parts) { |
152 |
|
if ($part->body->mimeType eq 'text/plain') { |
153 |
|
return $part->decoded->string; |
154 |
|
} |
155 |
|
} |
156 |
|
} |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
sub fetch_result_by_id { |
161 |
|
my $self = shift; |
162 |
|
|
163 |
my $message = $self->fetch_message($id); |
my $id = shift || return; |
164 |
|
|
165 |
my $row; |
my $row = $self->{cache}->{$id}; |
166 |
|
|
167 |
$row->{'from'} = unroll($message,'from','phrase'); |
if (! $row) { |
168 |
$row->{'subject'} = $message->get('Subject'); |
|
169 |
|
print STDERR "fetch_result_by_id($id) not in cache, hitting disk\n" if ($debug == 2); |
170 |
|
|
171 |
|
my $message = $self->fetch_message($id) || print STDERR "can't fetch message '$id'"; |
172 |
|
|
173 |
|
$row->{'id'} = $id; |
174 |
|
$row->{'from'} = $self->unroll($message,'from','phrase'); |
175 |
|
$row->{'to'} = $self->unroll($message,'to','phrase'); |
176 |
|
$row->{'cc'} = $self->unroll($message,'cc','phrase'); |
177 |
|
$row->{'subject'} = $message->subject; |
178 |
|
$row->{'body'} = $self->plain_text_body($message); |
179 |
|
$row->{'date'} = $message->date; |
180 |
|
|
181 |
|
# XXX store in cache? |
182 |
|
$self->{cache}->{$id} = $row; |
183 |
|
print STDERR "$id stored in cache\n" if ($debug == 2); |
184 |
|
} else { |
185 |
|
print STDERR "fetch_result_by_id($id) in cache\n" if ($debug == 2); |
186 |
|
} |
187 |
|
|
188 |
return $row; |
return $row; |
189 |
|
|