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 |
|
use Date::Parse; |
16 |
|
use POSIX qw(strftime); |
17 |
|
|
18 |
#use MWS_plucene; |
#use MWS_plucene; |
19 |
use MWS_swish; |
use MWS_swish; |
50 |
$self->{folder} = {}; |
$self->{folder} = {}; |
51 |
|
|
52 |
$self->{wrap_margin} = $self->{config}->val('global', 'wrap_margin'); |
$self->{wrap_margin} = $self->{config}->val('global', 'wrap_margin'); |
53 |
|
$self->{max_results} = $self->{config}->val('global', 'max_results') || 100; |
54 |
|
$self->reset_counters; |
55 |
|
|
56 |
return $self; |
return $self; |
57 |
} |
} |
58 |
|
|
59 |
|
sub normalize_string { |
60 |
|
my $self = shift; |
61 |
|
|
62 |
|
my $v = shift || return; |
63 |
|
|
64 |
|
$v = unac_string('ISO-8859-2', $v); |
65 |
|
$v = join('',sort split(/\s+/,$v)); |
66 |
|
$v =~ s/\W+//g; |
67 |
|
|
68 |
|
return $v; |
69 |
|
} |
70 |
|
|
71 |
|
# reset tables for search results |
72 |
|
sub reset_counters { |
73 |
|
my $self = shift; |
74 |
|
|
75 |
|
$self->{counter} = {}; |
76 |
|
|
77 |
|
# foreach my $c (qw(thread from to cc bcc lists links att)) { |
78 |
|
# $self->{counter}->{$c} = {}; |
79 |
|
# } |
80 |
|
|
81 |
|
} |
82 |
|
|
83 |
|
sub add_counter($$) { |
84 |
|
my $self = shift; |
85 |
|
|
86 |
|
my ($c,$v) = @_; |
87 |
|
my $k = $self->normalize_string($v); |
88 |
|
|
89 |
|
$self->{counter}->{$c}->{$k}->{name} = $v; |
90 |
|
return $self->{counter}->{$c}->{$k}->{usage}++; |
91 |
|
} |
92 |
|
|
93 |
|
sub yyyymmdd { |
94 |
|
my $self = shift; |
95 |
|
|
96 |
|
my $t = shift || time; |
97 |
|
|
98 |
|
my (undef,undef,undef,$dd,$mm,$yyyy) = localtime($t); |
99 |
|
$mm++; |
100 |
|
$yyyy+=1900; |
101 |
|
return ($yyyy,$mm,$dd); |
102 |
|
} |
103 |
|
|
104 |
|
sub fmtdate { |
105 |
|
my $self = shift; |
106 |
|
|
107 |
|
my @out; |
108 |
|
my @formats = qw(%04d %02d %02d); |
109 |
|
while (my $v = shift) { |
110 |
|
my $f = shift @formats; |
111 |
|
push @out, sprintf($f, $v); |
112 |
|
} |
113 |
|
|
114 |
|
print STDERR "fmtdate: ",join('|',@out),"\n"; |
115 |
|
|
116 |
|
return (wantarray ? @out : join("-",@out)); |
117 |
|
} |
118 |
|
|
119 |
|
sub add_counter_calendar($) { |
120 |
|
my $self = shift; |
121 |
|
|
122 |
|
my $t = shift || croak "add_counter_calendar without argument!"; |
123 |
|
|
124 |
|
my ($yyyy,$mm,$dd) = $self->fmtdate($self->yyyymmdd($t)); |
125 |
|
|
126 |
|
return $self->{counter}->{calendar}->{"$yyyy-$mm"}->{$dd}++; |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
sub counter { |
131 |
|
my $self = shift; |
132 |
|
|
133 |
|
my $c = shift || return; |
134 |
|
|
135 |
|
return if (! $self->{counter}->{$c}); |
136 |
|
|
137 |
|
return $self->{counter}->{$c}; |
138 |
|
} |
139 |
|
|
140 |
sub mbox_name2path { |
sub mbox_name2path { |
141 |
my $self = shift; |
my $self = shift; |
142 |
|
|
160 |
print STDERR "open_folder($mbox) ok\n" if ($debug); |
print STDERR "open_folder($mbox) ok\n" if ($debug); |
161 |
} |
} |
162 |
|
|
163 |
|
$self->{fetch_count} = 0; |
164 |
|
|
165 |
return $self->{folder}->{$mbox}; |
return $self->{folder}->{$mbox}; |
166 |
|
|
167 |
} |
} |
171 |
|
|
172 |
my $mbox = shift || croak "open_folder needs mbox name"; |
my $mbox = shift || croak "open_folder needs mbox name"; |
173 |
|
|
174 |
return $self->{folder}->{$mbox}->close(write => 'NEVER'); |
$self->{folder}->{$mbox}->close(write => 'NEVER') || croak "can't close folder $mbox"; |
175 |
|
|
176 |
|
# XXX this is rather agressive!!! |
177 |
|
$self->{folder} = {}; |
178 |
|
return |
179 |
} |
} |
180 |
|
|
181 |
sub fetch_message { |
sub fetch_message { |
186 |
|
|
187 |
# return message with ID |
# return message with ID |
188 |
print STDERR "fetch $id from $mbox\n" if ($debug); |
print STDERR "fetch $id from $mbox\n" if ($debug); |
189 |
return $self->open_folder($mbox)->find($id) || |
|
190 |
|
if ($self->{fetch_count}++ > 100) { |
191 |
|
$self->close_folder($mbox); |
192 |
|
print STDERR "close_folder($mbox) forced on ",$self->{fetch_count},"iteration\n"; |
193 |
|
} |
194 |
|
|
195 |
|
my $msg = $self->open_folder($mbox)->find($id); |
196 |
|
if ($msg) { |
197 |
|
return $msg; |
198 |
|
} else { |
199 |
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"; |
200 |
|
return; |
201 |
|
} |
202 |
} |
} |
203 |
|
|
204 |
|
|
207 |
|
|
208 |
my $s = shift || carp "search called without argument!"; |
my $s = shift || carp "search called without argument!"; |
209 |
|
|
210 |
|
$self->reset_counters; |
211 |
|
|
212 |
print STDERR "search_index($s)\n" if ($debug == 2); |
print STDERR "search_index($s)\n" if ($debug == 2); |
213 |
my @index_ids = $self->search_index($s); |
my @index_ids = $self->search_index($s); |
214 |
|
|
215 |
$self->{'index_ids'} = \@index_ids; |
$self->{'index_ids'} = \@index_ids; |
216 |
|
|
217 |
my $results = $#index_ids + 1; |
#my $results = $#index_ids + 1; |
218 |
$self->{'results'} = $results; |
#$self->{'results'} = $results; |
219 |
|
|
220 |
|
my $results = $self->{'total_hits'} || ($#index_ids + 1); |
221 |
|
|
222 |
$self->{'curr_result'} = 0; |
$self->{'curr_result'} = 0; |
223 |
|
|
226 |
return $results || 'error'; |
return $results || 'error'; |
227 |
} |
} |
228 |
|
|
229 |
|
sub decode_qp($) { |
230 |
|
my $self = shift; |
231 |
|
|
232 |
|
my $tmp = shift || return; |
233 |
|
|
234 |
|
sub decode($$) { |
235 |
|
my ($cp,$qp) = @_; |
236 |
|
print STDERR "decode($cp,$qp) -> " if ($debug == 2); |
237 |
|
$qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; |
238 |
|
$qp =~ s/_/ /g; |
239 |
|
print STDERR "$qp -> " if ($debug == 2); |
240 |
|
my $iconv = Text::Iconv->new($cp,'ISO-8859-2'); |
241 |
|
return $iconv->convert($qp) || ''; |
242 |
|
} |
243 |
|
|
244 |
|
$tmp =~ s/=\?([^\?]+)\?Q\?(.+?)\?=/decode($1,$2)/ge; |
245 |
|
$tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g; |
246 |
|
#print STDERR "$tmp\n" if ($debug == 2); |
247 |
|
return $tmp; |
248 |
|
} |
249 |
|
|
250 |
sub unroll($$$) { |
sub unroll($$$) { |
251 |
my $self = shift; |
my $self = shift; |
252 |
|
|
254 |
|
|
255 |
my @arr; |
my @arr; |
256 |
|
|
257 |
|
return if (! $message->$part); |
258 |
|
|
259 |
foreach my $from ($message->$part) { |
foreach my $from ($message->$part) { |
260 |
my $tmp = $from->$sub; |
my $tmp = $from->$sub || next; |
261 |
if ($tmp) { |
|
262 |
$tmp =~ s/^\s*["'](.*)["']\s*$/$1/; |
$tmp = $self->decode_qp($tmp); |
263 |
push @arr, $tmp; |
push @arr, $tmp; |
|
} |
|
264 |
} |
} |
265 |
|
|
266 |
return @arr; |
return @arr; |
279 |
push @arr, $self->fetch_result_by_id($id); |
push @arr, $self->fetch_result_by_id($id); |
280 |
} |
} |
281 |
|
|
282 |
|
|
283 |
return @arr; |
return @arr; |
284 |
} |
} |
285 |
|
|
319 |
# reformat with Text::Autoformat |
# reformat with Text::Autoformat |
320 |
my $wrap = $self->{wrap_margin}; |
my $wrap = $self->{wrap_margin}; |
321 |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
322 |
$body = autoformat $body; |
$body =~ s/[\r\n]/\n/gs; |
323 |
|
$body = autoformat($body, {right=>$wrap}); |
324 |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
325 |
} |
} |
326 |
|
|
339 |
|
|
340 |
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); |
341 |
|
|
342 |
my $message = $self->fetch_message($id) || print STDERR "can't fetch message '$id'"; |
my $message = $self->fetch_message($id) || return; |
343 |
|
|
344 |
$row->{'id'} = $id; |
$row->{'id'} = $id; |
345 |
@{$row->{'from'}} = $self->unroll($message,'from','phrase'); |
|
346 |
@{$row->{'to'}} = $self->unroll($message,'to','phrase'); |
foreach my $p (qw(from to cc bcc)) { |
347 |
@{$row->{'cc'}} = $self->unroll($message,'cc','phrase'); |
foreach my $v ($self->unroll($message,'from','phrase')) { |
348 |
$row->{'subject'} = $message->subject; |
push @{$row->{$p}},$v; |
349 |
|
$self->add_counter($p,$v); |
350 |
|
} |
351 |
|
} |
352 |
|
$row->{'subject'} = $self->decode_qp($message->subject); |
353 |
$row->{'body'} = $self->plain_text_body($message); |
$row->{'body'} = $self->plain_text_body($message); |
354 |
$row->{'date'} = $message->date; |
my $utime = str2time($message->date); |
355 |
|
|
356 |
|
$row->{'date_utime'} = $utime; |
357 |
|
|
358 |
|
$row->{'date'} = strftime("%Y-%m-%d %H:%M:%S", localtime($utime)); |
359 |
|
$self->add_counter_calendar($utime); |
360 |
|
|
361 |
# XXX store in cache? |
# XXX store in cache? |
362 |
$self->{cache}->{$id} = $row; |
$self->{cache}->{$id} = $row; |