4 |
|
|
5 |
use strict; |
use strict; |
6 |
use warnings; |
use warnings; |
|
use Carp; |
|
7 |
|
|
8 |
|
use lib '.'; |
9 |
|
|
10 |
|
use Carp; |
11 |
use Mail::Box::Manager; |
use Mail::Box::Manager; |
12 |
use Config::IniFiles; |
use Config::IniFiles; |
13 |
use POSIX qw(strftime); |
use POSIX qw(strftime); |
16 |
use Text::Unaccent; |
use Text::Unaccent; |
17 |
use Date::Parse; |
use Date::Parse; |
18 |
use POSIX qw(strftime); |
use POSIX qw(strftime); |
19 |
|
use MIME::Base64; |
|
#use MWS_plucene; |
|
|
use MWS_swish; |
|
|
|
|
|
require Exporter; |
|
|
|
|
|
our @ISA = qw(Exporter); |
|
|
|
|
|
our %EXPORT_TAGS = (); |
|
|
our @EXPORT_OK; |
|
|
our @EXPORT; |
|
20 |
|
|
21 |
our $VERSION = '1.00'; |
our $VERSION = '1.00'; |
22 |
|
|
23 |
my $folder; # placeholder for folders |
my $debug = 1; |
24 |
|
|
25 |
my $debug = 2; |
my @counters = qw(from to cc bcc folder); |
26 |
|
|
27 |
sub new { |
sub new { |
28 |
my $class = shift; |
my $class = shift; |
29 |
my $self = {}; |
my $self = {@_}; |
30 |
bless($self, $class); |
bless($self, $class); |
31 |
|
|
|
my $config_file = shift || die "need index file"; |
|
32 |
|
|
33 |
$self->{config} = new Config::IniFiles( -file => $config_file ); |
my $config_name = $self->{config_file} || croak "need config_file"; |
34 |
|
$config_name =~ s/\.conf.*$//; |
35 |
|
$self->{config_name} = $config_name; |
36 |
|
|
37 |
|
$self->{config} = new Config::IniFiles( -file => $self->{config_file} ); |
38 |
|
|
39 |
my $index_file = $self->{config}->val('global', 'index') || croak "can't find [index] section in config file with path of index"; |
my $index_dir = $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(access => 'r'); |
$self->{mgr} = Mail::Box::Manager->new(access => 'r'); |
42 |
$self->{index_file} = $index_file; |
$self->{index_dir} = $index_dir; |
43 |
|
|
44 |
# placeholder for opened folders |
# placeholder for opened folders |
45 |
$self->{folder} = {}; |
$self->{folder} = {}; |
60 |
$v = join('',sort split(/\s+/,$v)); |
$v = join('',sort split(/\s+/,$v)); |
61 |
$v =~ s/\W+//g; |
$v =~ s/\W+//g; |
62 |
|
|
63 |
return $v; |
return lc($v); |
64 |
} |
} |
65 |
|
|
66 |
# reset tables for search results |
# reset tables for search results |
81 |
my ($c,$v) = @_; |
my ($c,$v) = @_; |
82 |
my $k = $self->normalize_string($v); |
my $k = $self->normalize_string($v); |
83 |
|
|
84 |
$self->{counter}->{$c}->{$k}->{name} = $v; |
$self->{counter}->{$c}->{$k}->{name} = $v || return; |
85 |
return $self->{counter}->{$c}->{$k}->{usage}++; |
return $self->{counter}->{$c}->{$k}->{usage}++; |
86 |
} |
} |
87 |
|
|
106 |
push @out, sprintf($f, $v); |
push @out, sprintf($f, $v); |
107 |
} |
} |
108 |
|
|
109 |
print STDERR "fmtdate: ",join('|',@out),"\n"; |
print STDERR "fmtdate: ",join('|',@out),"\n" if ($debug == 2); |
110 |
|
|
111 |
return (wantarray ? @out : join("-",@out)); |
return (wantarray ? @out : join("-",@out)); |
112 |
} |
} |
114 |
sub add_counter_calendar($) { |
sub add_counter_calendar($) { |
115 |
my $self = shift; |
my $self = shift; |
116 |
|
|
117 |
my $t = shift || croak "add_counter_calendar without argument!"; |
my $t = shift || return; |
118 |
|
|
119 |
my ($yyyy,$mm,$dd) = $self->fmtdate($self->yyyymmdd($t)); |
my ($yyyy,$mm,$dd) = $self->fmtdate($self->yyyymmdd($t)); |
120 |
|
|
150 |
|
|
151 |
print STDERR "about to open_folder($mbox)\n" if ($debug == 2); |
print STDERR "about to open_folder($mbox)\n" if ($debug == 2); |
152 |
|
|
153 |
$self->{folder}->{$mbox} = $self->{mgr}->open($mbox_path) || croak "can't open folder $mbox at '$mbox_path': $!"; |
my $folder; |
154 |
|
|
155 |
|
# is this maildir (no locking anyway) |
156 |
|
if (-d $mbox_path) { |
157 |
|
$folder = $self->{mgr}->open($mbox_path); |
158 |
|
} else { |
159 |
|
$folder = $self->{mgr}->open($mbox_path, lock_type=>'none' ); |
160 |
|
} |
161 |
|
|
162 |
|
$self->{folder}->{$mbox} = $folder || croak "can't open folder $mbox at '$mbox_path': $!"; |
163 |
|
|
164 |
print STDERR "open_folder($mbox) ok\n" if ($debug); |
print STDERR "open_folder($mbox) ok\n" if ($debug); |
165 |
} |
} |
209 |
sub search { |
sub search { |
210 |
my $self = shift; |
my $self = shift; |
211 |
|
|
212 |
my $s = shift || carp "search called without argument!"; |
carp "search called without argument!" if (! @_); |
213 |
|
|
214 |
$self->reset_counters; |
$self->reset_counters; |
215 |
|
|
216 |
print STDERR "search_index($s)\n" if ($debug == 2); |
print STDERR "search(",join(" ",@_),")\n" if ($debug == 2); |
217 |
my @index_ids = $self->search_index($s); |
my @index_ids = $self->search_index(@_); |
218 |
|
|
219 |
$self->{'index_ids'} = \@index_ids; |
$self->{'index_ids'} = \@index_ids; |
220 |
|
|
225 |
|
|
226 |
$self->{'curr_result'} = 0; |
$self->{'curr_result'} = 0; |
227 |
|
|
228 |
|
$self->reset_counters; |
229 |
|
|
230 |
print STDERR "$results results\n" if ($debug == 2); |
print STDERR "$results results\n" if ($debug == 2); |
231 |
|
|
232 |
return $results || 'error'; |
return $results || 'error'; |
237 |
|
|
238 |
my $tmp = shift || return; |
my $tmp = shift || return; |
239 |
|
|
240 |
sub decode($$) { |
sub decode($$$) { |
241 |
my ($cp,$qp) = @_; |
my ($cp,$enc,$qp) = @_; |
242 |
print STDERR "decode($cp,$qp) -> " if ($debug == 2); |
|
243 |
$qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; |
print STDERR "decode($cp,$qp) -> " if ($debug == 2); |
244 |
$qp =~ s/_/ /g; |
|
245 |
print STDERR "$qp -> " if ($debug == 2); |
if (uc($enc) eq "Q") { |
246 |
my $iconv = Text::Iconv->new($cp,'ISO-8859-2'); |
$qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; |
247 |
return $iconv->convert($qp) || ''; |
$qp =~ s/_/ /g; |
248 |
|
} elsif (uc($enc) eq "B") { |
249 |
|
$qp = decode_base64($qp); |
250 |
|
} else { |
251 |
|
croak "unsupported encoding '$enc' in decode_qp\n"; |
252 |
|
return $qp; |
253 |
|
} |
254 |
|
|
255 |
|
print STDERR "$qp\n" if ($debug == 2); |
256 |
|
|
257 |
|
my $iconv; |
258 |
|
eval '$iconv = Text::Iconv->new($cp,"ISO-8859-2")'; |
259 |
|
if ($iconv) { |
260 |
|
return $iconv->convert($qp) || ''; |
261 |
|
} else { |
262 |
|
return $qp; |
263 |
|
} |
264 |
} |
} |
265 |
|
|
266 |
$tmp =~ s/=\?([^\?]+)\?Q\?(.+?)\?=/decode($1,$2)/ge; |
$tmp =~ s/=\?([^\?]+)\?([QB])\?(.+?)\?=/decode($1,$2,$3)/ige; |
267 |
$tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g; |
$tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g; |
268 |
#print STDERR "$tmp\n" if ($debug == 2); |
#print STDERR "$tmp\n" if ($debug == 2); |
269 |
return $tmp; |
return $tmp; |
338 |
} |
} |
339 |
} |
} |
340 |
|
|
341 |
|
if (! $body) { |
342 |
|
$body = "[plain/text body not found]" if ($debug == 2); |
343 |
|
print STDERR "plain/text body not found\n" if ($debug); |
344 |
|
return; |
345 |
|
} |
346 |
|
|
347 |
# reformat with Text::Autoformat |
# reformat with Text::Autoformat |
348 |
my $wrap = $self->{wrap_margin}; |
my $wrap = $self->{wrap_margin}; |
349 |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
350 |
$body =~ s/[\r\n]/\n/gs; |
eval '$body = autoformat($body, {right=>$wrap, all=>1});'; |
|
$body = autoformat($body, {right=>$wrap}); |
|
351 |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
352 |
} |
} |
353 |
|
|
370 |
|
|
371 |
$row->{'id'} = $id; |
$row->{'id'} = $id; |
372 |
|
|
373 |
foreach my $p (qw(from to cc bcc)) { |
foreach my $p (@counters) { |
374 |
foreach my $v ($self->unroll($message,'from','phrase')) { |
foreach my $v ($self->unroll($message,$p,'phrase')) { |
375 |
push @{$row->{$p}},$v; |
push @{$row->{$p}},$v; |
376 |
$self->add_counter($p,$v); |
$self->add_counter($p,$v); |
377 |
} |
} |
378 |
} |
} |
379 |
|
|
380 |
|
$self->add_counter('folder', $1) if ($id =~ m/^(\S+)\s/); |
381 |
|
|
382 |
$row->{'subject'} = $self->decode_qp($message->subject); |
$row->{'subject'} = $self->decode_qp($message->subject); |
383 |
$row->{'body'} = $self->plain_text_body($message); |
$row->{'body'} = $self->plain_text_body($message); |
384 |
my $utime = str2time($message->date); |
my $utime = str2time($message->date); |
393 |
print STDERR "$id stored in cache\n" if ($debug == 2); |
print STDERR "$id stored in cache\n" if ($debug == 2); |
394 |
} else { |
} else { |
395 |
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); |
396 |
|
foreach my $p (@counters) { |
397 |
|
foreach my $v (@{$row->{$p}}) { |
398 |
|
$self->add_counter($p,$v); |
399 |
|
} |
400 |
|
} |
401 |
|
|
402 |
|
$self->add_counter('folder', $1) if ($id =~ m/^(\S+)\s/); |
403 |
|
|
404 |
|
$self->add_counter_calendar($row->{date_utime}); |
405 |
} |
} |
406 |
|
|
407 |
return $row; |
return $row; |