11 |
use POSIX qw(strftime); |
use POSIX qw(strftime); |
12 |
use Text::Autoformat; |
use Text::Autoformat; |
13 |
use Text::Iconv; |
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; |
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; |
$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 |
|
|
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 |
|
|
233 |
|
|
234 |
sub decode($$) { |
sub decode($$) { |
235 |
my ($cp,$qp) = @_; |
my ($cp,$qp) = @_; |
|
my $iconv = Text::Iconv->new($cp,'ISO-8859-2'); |
|
236 |
print STDERR "decode($cp,$qp) -> " if ($debug == 2); |
print STDERR "decode($cp,$qp) -> " if ($debug == 2); |
237 |
$qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; |
$qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; |
238 |
$qp =~ s/_/ /g; |
$qp =~ s/_/ /g; |
239 |
print STDERR "$qp\n" if ($debug == 2); |
print STDERR "$qp -> " if ($debug == 2); |
240 |
return $iconv->convert($qp) || $qp; |
my $iconv = Text::Iconv->new($cp,'ISO-8859-2'); |
241 |
|
return $iconv->convert($qp) || ''; |
242 |
} |
} |
243 |
|
|
244 |
$tmp =~ s/=\?([^\?]+)\?Q\?(.+?)\?=/decode($1,$2)/ex; |
$tmp =~ s/=\?([^\?]+)\?Q\?(.+?)\?=/decode($1,$2)/ge; |
245 |
$tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g; |
$tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g; |
246 |
|
#print STDERR "$tmp\n" if ($debug == 2); |
247 |
return $tmp; |
return $tmp; |
248 |
} |
} |
249 |
|
|
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 |
|
|
342 |
my $message = $self->fetch_message($id) || return; |
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 |
|
push @{$row->{$p}},$v; |
349 |
|
$self->add_counter($p,$v); |
350 |
|
} |
351 |
|
} |
352 |
$row->{'subject'} = $self->decode_qp($message->subject); |
$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; |