10 |
use Config::IniFiles; |
use Config::IniFiles; |
11 |
use POSIX qw(strftime); |
use POSIX qw(strftime); |
12 |
use Text::Autoformat; |
use Text::Autoformat; |
13 |
|
use Text::Iconv; |
14 |
|
use Text::Unaccent; |
15 |
|
|
16 |
#use MWS_plucene; |
#use MWS_plucene; |
17 |
use MWS_swish; |
use MWS_swish; |
48 |
$self->{folder} = {}; |
$self->{folder} = {}; |
49 |
|
|
50 |
$self->{wrap_margin} = $self->{config}->val('global', 'wrap_margin'); |
$self->{wrap_margin} = $self->{config}->val('global', 'wrap_margin'); |
51 |
|
$self->{max_results} = $self->{config}->val('global', 'max_results') || 100; |
52 |
|
$self->reset_counters; |
53 |
|
|
54 |
return $self; |
return $self; |
55 |
} |
} |
56 |
|
|
57 |
|
sub normalize_string { |
58 |
|
my $self = shift; |
59 |
|
|
60 |
|
my $v = shift || return; |
61 |
|
|
62 |
|
$v = unac_string('ISO-8859-2', $v); |
63 |
|
$v = join('',sort split(/\s+/,$v)); |
64 |
|
$v =~ s/\W+//g; |
65 |
|
|
66 |
|
return $v; |
67 |
|
} |
68 |
|
|
69 |
|
# reset tables for search results |
70 |
|
sub reset_counters { |
71 |
|
my $self = shift; |
72 |
|
|
73 |
|
$self->{counter} = {}; |
74 |
|
|
75 |
|
# foreach my $c (qw(thread from to cc bcc lists links att)) { |
76 |
|
# $self->{counter}->{$c} = {}; |
77 |
|
# } |
78 |
|
|
79 |
|
} |
80 |
|
|
81 |
|
sub add_counter($$) { |
82 |
|
my $self = shift; |
83 |
|
|
84 |
|
my ($c,$v) = @_; |
85 |
|
my $k = $self->normalize_string($v); |
86 |
|
|
87 |
|
$self->{counter}->{$c}->{$k}->{name} = $v; |
88 |
|
return $self->{counter}->{$c}->{$k}->{usage}++; |
89 |
|
} |
90 |
|
|
91 |
|
sub counter { |
92 |
|
my $self = shift; |
93 |
|
|
94 |
|
my $c = shift || return; |
95 |
|
|
96 |
|
return if (! $self->{counter}->{$c}); |
97 |
|
|
98 |
|
return $self->{counter}->{$c}; |
99 |
|
} |
100 |
|
|
101 |
sub mbox_name2path { |
sub mbox_name2path { |
102 |
my $self = shift; |
my $self = shift; |
103 |
|
|
121 |
print STDERR "open_folder($mbox) ok\n" if ($debug); |
print STDERR "open_folder($mbox) ok\n" if ($debug); |
122 |
} |
} |
123 |
|
|
124 |
|
$self->{fetch_count} = 0; |
125 |
|
|
126 |
return $self->{folder}->{$mbox}; |
return $self->{folder}->{$mbox}; |
127 |
|
|
128 |
} |
} |
132 |
|
|
133 |
my $mbox = shift || croak "open_folder needs mbox name"; |
my $mbox = shift || croak "open_folder needs mbox name"; |
134 |
|
|
135 |
return $self->{folder}->{$mbox}->close(write => 'NEVER'); |
$self->{folder}->{$mbox}->close(write => 'NEVER') || croak "can't close folder $mbox"; |
136 |
|
|
137 |
|
# XXX this is rather agressive!!! |
138 |
|
$self->{folder} = {}; |
139 |
|
return |
140 |
} |
} |
141 |
|
|
142 |
sub fetch_message { |
sub fetch_message { |
147 |
|
|
148 |
# return message with ID |
# return message with ID |
149 |
print STDERR "fetch $id from $mbox\n" if ($debug); |
print STDERR "fetch $id from $mbox\n" if ($debug); |
150 |
return $self->open_folder($mbox)->find($id) || |
|
151 |
|
if ($self->{fetch_count}++ > 100) { |
152 |
|
$self->close_folder($mbox); |
153 |
|
print STDERR "close_folder($mbox) forced on ",$self->{fetch_count},"iteration\n"; |
154 |
|
} |
155 |
|
|
156 |
|
my $msg = $self->open_folder($mbox)->find($id); |
157 |
|
if ($msg) { |
158 |
|
return $msg; |
159 |
|
} else { |
160 |
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"; |
161 |
|
return; |
162 |
|
} |
163 |
} |
} |
164 |
|
|
165 |
|
|
168 |
|
|
169 |
my $s = shift || carp "search called without argument!"; |
my $s = shift || carp "search called without argument!"; |
170 |
|
|
171 |
|
$self->reset_counters; |
172 |
|
|
173 |
print STDERR "search_index($s)\n" if ($debug == 2); |
print STDERR "search_index($s)\n" if ($debug == 2); |
174 |
my @index_ids = $self->search_index($s); |
my @index_ids = $self->search_index($s); |
175 |
|
|
176 |
$self->{'index_ids'} = \@index_ids; |
$self->{'index_ids'} = \@index_ids; |
177 |
|
|
178 |
my $results = $#index_ids + 1; |
#my $results = $#index_ids + 1; |
179 |
$self->{'results'} = $results; |
#$self->{'results'} = $results; |
180 |
|
|
181 |
|
my $results = $self->{'total_hits'} || ($#index_ids + 1); |
182 |
|
|
183 |
$self->{'curr_result'} = 0; |
$self->{'curr_result'} = 0; |
184 |
|
|
187 |
return $results || 'error'; |
return $results || 'error'; |
188 |
} |
} |
189 |
|
|
190 |
|
sub decode_qp($) { |
191 |
|
my $self = shift; |
192 |
|
|
193 |
|
my $tmp = shift || return; |
194 |
|
|
195 |
|
sub decode($$) { |
196 |
|
my ($cp,$qp) = @_; |
197 |
|
my $iconv = Text::Iconv->new($cp,'ISO-8859-2'); |
198 |
|
print STDERR "decode($cp,$qp) -> " if ($debug == 2); |
199 |
|
$qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; |
200 |
|
$qp =~ s/_/ /g; |
201 |
|
print STDERR "$qp\n" if ($debug == 2); |
202 |
|
return $iconv->convert($qp) || $qp; |
203 |
|
} |
204 |
|
|
205 |
|
$tmp =~ s/=\?([^\?]+)\?Q\?(.+?)\?=/decode($1,$2)/ex; |
206 |
|
$tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g; |
207 |
|
return $tmp; |
208 |
|
} |
209 |
|
|
210 |
sub unroll($$$) { |
sub unroll($$$) { |
211 |
my $self = shift; |
my $self = shift; |
212 |
|
|
214 |
|
|
215 |
my @arr; |
my @arr; |
216 |
|
|
217 |
|
return if (! $message->$part); |
218 |
|
|
219 |
foreach my $from ($message->$part) { |
foreach my $from ($message->$part) { |
220 |
my $tmp = $from->$sub; |
my $tmp = $from->$sub || next; |
221 |
if ($tmp) { |
|
222 |
$tmp =~ s/^\s*["'](.*)["']\s*$/$1/; |
$tmp = $self->decode_qp($tmp); |
223 |
push @arr, $tmp; |
push @arr, $tmp; |
|
} |
|
224 |
} |
} |
225 |
|
|
226 |
return @arr; |
return @arr; |
239 |
push @arr, $self->fetch_result_by_id($id); |
push @arr, $self->fetch_result_by_id($id); |
240 |
} |
} |
241 |
|
|
242 |
|
|
243 |
return @arr; |
return @arr; |
244 |
} |
} |
245 |
|
|
279 |
# reformat with Text::Autoformat |
# reformat with Text::Autoformat |
280 |
my $wrap = $self->{wrap_margin}; |
my $wrap = $self->{wrap_margin}; |
281 |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
282 |
$body = autoformat $body; |
$body =~ s/[\r\n]/\n/gs; |
283 |
|
$body = autoformat($body, {right=>$wrap}); |
284 |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
285 |
} |
} |
286 |
|
|
299 |
|
|
300 |
print STDERR "fetch_result_by_id($id) not in cache, hitting disk\n" if ($debug == 2); |
print STDERR "fetch_result_by_id($id) not in cache, hitting disk\n" if ($debug == 2); |
301 |
|
|
302 |
my $message = $self->fetch_message($id) || print STDERR "can't fetch message '$id'"; |
my $message = $self->fetch_message($id) || return; |
303 |
|
|
304 |
$row->{'id'} = $id; |
$row->{'id'} = $id; |
305 |
@{$row->{'from'}} = $self->unroll($message,'from','phrase'); |
|
306 |
@{$row->{'to'}} = $self->unroll($message,'to','phrase'); |
foreach my $p (qw(from to cc bcc)) { |
307 |
@{$row->{'cc'}} = $self->unroll($message,'cc','phrase'); |
foreach my $v ($self->unroll($message,'from','phrase')) { |
308 |
$row->{'subject'} = $message->subject; |
push @{$row->{$p}},$v; |
309 |
|
$self->add_counter($p,$v); |
310 |
|
} |
311 |
|
} |
312 |
|
$row->{'subject'} = $self->decode_qp($message->subject); |
313 |
$row->{'body'} = $self->plain_text_body($message); |
$row->{'body'} = $self->plain_text_body($message); |
314 |
$row->{'date'} = $message->date; |
$row->{'date'} = $message->date; |
315 |
|
|