14 |
use Text::Unaccent; |
use Text::Unaccent; |
15 |
use Date::Parse; |
use Date::Parse; |
16 |
use POSIX qw(strftime); |
use POSIX qw(strftime); |
17 |
|
use MIME::Base64; |
18 |
|
|
19 |
#use MWS_plucene; |
#use MWS_plucene; |
20 |
use MWS_swish; |
use MWS_swish; |
31 |
|
|
32 |
my $folder; # placeholder for folders |
my $folder; # placeholder for folders |
33 |
|
|
34 |
my $debug = 2; |
my $debug = 1; |
35 |
|
|
36 |
sub new { |
sub new { |
37 |
my $class = shift; |
my $class = shift; |
42 |
|
|
43 |
$self->{config} = new Config::IniFiles( -file => $config_file ); |
$self->{config} = new Config::IniFiles( -file => $config_file ); |
44 |
|
|
45 |
my $index_file = $self->{config}->val('global', 'index') || croak "can't find [index] section in config file with path of index"; |
$self->{config_file} = $config_file; |
46 |
|
$config_file =~ s/\.conf.*$//; |
47 |
|
$self->{config_name} = $config_file; |
48 |
|
|
49 |
|
my $index_dir = $self->{config}->val('global', 'index') || croak "can't find [index] section in config file with path of index"; |
50 |
|
|
51 |
$self->{mgr} = Mail::Box::Manager->new(access => 'r'); |
$self->{mgr} = Mail::Box::Manager->new(access => 'r'); |
52 |
$self->{index_file} = $index_file; |
$self->{index_dir} = $index_dir; |
53 |
|
|
54 |
# placeholder for opened folders |
# placeholder for opened folders |
55 |
$self->{folder} = {}; |
$self->{folder} = {}; |
70 |
$v = join('',sort split(/\s+/,$v)); |
$v = join('',sort split(/\s+/,$v)); |
71 |
$v =~ s/\W+//g; |
$v =~ s/\W+//g; |
72 |
|
|
73 |
return $v; |
return lc($v); |
74 |
} |
} |
75 |
|
|
76 |
# reset tables for search results |
# reset tables for search results |
116 |
push @out, sprintf($f, $v); |
push @out, sprintf($f, $v); |
117 |
} |
} |
118 |
|
|
119 |
print STDERR "fmtdate: ",join('|',@out),"\n"; |
print STDERR "fmtdate: ",join('|',@out),"\n" if ($debug == 2); |
120 |
|
|
121 |
return (wantarray ? @out : join("-",@out)); |
return (wantarray ? @out : join("-",@out)); |
122 |
} |
} |
210 |
sub search { |
sub search { |
211 |
my $self = shift; |
my $self = shift; |
212 |
|
|
213 |
my $s = shift || carp "search called without argument!"; |
carp "search called without argument!" if (! @_); |
214 |
|
|
215 |
$self->reset_counters; |
$self->reset_counters; |
216 |
|
|
217 |
print STDERR "search_index($s)\n" if ($debug == 2); |
print STDERR "search(",join(" ",@_),")\n" if ($debug == 2); |
218 |
my @index_ids = $self->search_index($s); |
my @index_ids = $self->search_index(@_); |
219 |
|
|
220 |
$self->{'index_ids'} = \@index_ids; |
$self->{'index_ids'} = \@index_ids; |
221 |
|
|
226 |
|
|
227 |
$self->{'curr_result'} = 0; |
$self->{'curr_result'} = 0; |
228 |
|
|
229 |
|
$self->reset_counters; |
230 |
|
|
231 |
print STDERR "$results results\n" if ($debug == 2); |
print STDERR "$results results\n" if ($debug == 2); |
232 |
|
|
233 |
return $results || 'error'; |
return $results || 'error'; |
238 |
|
|
239 |
my $tmp = shift || return; |
my $tmp = shift || return; |
240 |
|
|
241 |
sub decode($$) { |
sub decode($$$) { |
242 |
my ($cp,$qp) = @_; |
my ($cp,$enc,$qp) = @_; |
243 |
print STDERR "decode($cp,$qp) -> " if ($debug == 2); |
|
244 |
$qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; |
print STDERR "decode($cp,$qp) -> " if ($debug == 2); |
245 |
$qp =~ s/_/ /g; |
|
246 |
print STDERR "$qp -> " if ($debug == 2); |
if (uc($enc) eq "Q") { |
247 |
|
$qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; |
248 |
|
$qp =~ s/_/ /g; |
249 |
|
} elsif (uc($enc) eq "B") { |
250 |
|
$qp = decode_base64($qp); |
251 |
|
} else { |
252 |
|
croak "unsupported encoding '$enc' in decode_qp\n"; |
253 |
|
return $qp; |
254 |
|
} |
255 |
|
|
256 |
|
print STDERR "$qp\n" if ($debug == 2); |
257 |
|
|
258 |
my $iconv = Text::Iconv->new($cp,'ISO-8859-2'); |
my $iconv = Text::Iconv->new($cp,'ISO-8859-2'); |
259 |
return $iconv->convert($qp) || ''; |
return $iconv->convert($qp) || ''; |
260 |
} |
} |
261 |
|
|
262 |
$tmp =~ s/=\?([^\?]+)\?Q\?(.+?)\?=/decode($1,$2)/ge; |
$tmp =~ s/=\?([^\?]+)\?([QB])\?(.+?)\?=/decode($1,$2,$3)/ige; |
263 |
$tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g; |
$tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g; |
264 |
#print STDERR "$tmp\n" if ($debug == 2); |
#print STDERR "$tmp\n" if ($debug == 2); |
265 |
return $tmp; |
return $tmp; |
334 |
} |
} |
335 |
} |
} |
336 |
|
|
337 |
|
if (! $body) { |
338 |
|
$body = "[plain/text body not found]" if ($debug == 2); |
339 |
|
print STDERR "plain/text body not found\n" if ($debug); |
340 |
|
return; |
341 |
|
} |
342 |
|
|
343 |
# reformat with Text::Autoformat |
# reformat with Text::Autoformat |
344 |
my $wrap = $self->{wrap_margin}; |
my $wrap = $self->{wrap_margin}; |
345 |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
346 |
$body =~ s/[\r\n]/\n/gs; |
$body = autoformat($body, {right=>$wrap, all=>1}); |
|
$body = autoformat($body, {right=>$wrap}); |
|
347 |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
348 |
} |
} |
349 |
|
|
367 |
$row->{'id'} = $id; |
$row->{'id'} = $id; |
368 |
|
|
369 |
foreach my $p (qw(from to cc bcc)) { |
foreach my $p (qw(from to cc bcc)) { |
370 |
foreach my $v ($self->unroll($message,'from','phrase')) { |
foreach my $v ($self->unroll($message,$p,'phrase')) { |
371 |
push @{$row->{$p}},$v; |
push @{$row->{$p}},$v; |
372 |
$self->add_counter($p,$v); |
$self->add_counter($p,$v); |
373 |
} |
} |
386 |
print STDERR "$id stored in cache\n" if ($debug == 2); |
print STDERR "$id stored in cache\n" if ($debug == 2); |
387 |
} else { |
} else { |
388 |
print STDERR "fetch_result_by_id($id) in cache\n" if ($debug == 2); |
print STDERR "fetch_result_by_id($id) in cache\n" if ($debug == 2); |
389 |
|
foreach my $p (qw(from to cc bcc)) { |
390 |
|
foreach my $v (@{$row->{$p}}) { |
391 |
|
$self->add_counter($p,$v); |
392 |
|
} |
393 |
|
} |
394 |
|
|
395 |
|
$self->add_counter_calendar($row->{date_utime}); |
396 |
} |
} |
397 |
|
|
398 |
return $row; |
return $row; |