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 |
|
|
15 |
#use MWS_plucene; |
#use MWS_plucene; |
16 |
use MWS_swish; |
use MWS_swish; |
74 |
print STDERR "open_folder($mbox) ok\n" if ($debug); |
print STDERR "open_folder($mbox) ok\n" if ($debug); |
75 |
} |
} |
76 |
|
|
77 |
|
$self->{fetch_count} = 0; |
78 |
|
|
79 |
return $self->{folder}->{$mbox}; |
return $self->{folder}->{$mbox}; |
80 |
|
|
81 |
} |
} |
85 |
|
|
86 |
my $mbox = shift || croak "open_folder needs mbox name"; |
my $mbox = shift || croak "open_folder needs mbox name"; |
87 |
|
|
88 |
return $self->{folder}->{$mbox}->close(write => 'NEVER'); |
$self->{folder}->{$mbox}->close(write => 'NEVER') || croak "can't close folder $mbox"; |
89 |
|
|
90 |
|
# XXX this is rather agressive!!! |
91 |
|
$self->{folder} = {}; |
92 |
|
return |
93 |
} |
} |
94 |
|
|
95 |
sub fetch_message { |
sub fetch_message { |
100 |
|
|
101 |
# return message with ID |
# return message with ID |
102 |
print STDERR "fetch $id from $mbox\n" if ($debug); |
print STDERR "fetch $id from $mbox\n" if ($debug); |
103 |
|
|
104 |
|
if ($self->{fetch_count}++ > 100) { |
105 |
|
$self->close_folder($mbox); |
106 |
|
print STDERR "close_folder($mbox) forced on ",$self->{fetch_count},"iteration\n"; |
107 |
|
} |
108 |
|
|
109 |
return $self->open_folder($mbox)->find($id) || |
return $self->open_folder($mbox)->find($id) || |
110 |
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"; |
111 |
} |
} |
131 |
return $results || 'error'; |
return $results || 'error'; |
132 |
} |
} |
133 |
|
|
134 |
|
sub decode_qp($) { |
135 |
|
my $self = shift; |
136 |
|
|
137 |
|
my $tmp = shift || return; |
138 |
|
|
139 |
|
sub decode($$) { |
140 |
|
my ($cp,$qp) = @_; |
141 |
|
my $iconv = Text::Iconv->new($cp,'ISO-8859-2'); |
142 |
|
print STDERR "decode($cp,$qp) -> " if ($debug == 2); |
143 |
|
$qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; |
144 |
|
$qp =~ s/_/ /g; |
145 |
|
print STDERR "$qp\n" if ($debug == 2); |
146 |
|
return $iconv->convert($qp); |
147 |
|
} |
148 |
|
|
149 |
|
$tmp =~ s/=\?([^\?]+)\?Q\?(.+)\?=/decode($1,$2)/ex; |
150 |
|
return $tmp; |
151 |
|
} |
152 |
|
|
153 |
sub unroll($$$) { |
sub unroll($$$) { |
154 |
my $self = shift; |
my $self = shift; |
155 |
|
|
158 |
my @arr; |
my @arr; |
159 |
|
|
160 |
foreach my $from ($message->$part) { |
foreach my $from ($message->$part) { |
161 |
my $tmp = $from->$sub; |
my $tmp = $from->$sub || next; |
162 |
if ($tmp) { |
|
163 |
$tmp =~ s/^\s*["'](.*)["']\s*$/$1/; |
$tmp = $self->decode_qp($tmp); |
164 |
push @arr, $tmp; |
$tmp =~ s/^\s*["'](.*)["']\s*$/$1/; |
165 |
} |
push @arr, $tmp; |
166 |
} |
} |
167 |
|
|
168 |
return @arr; |
return @arr; |
220 |
# reformat with Text::Autoformat |
# reformat with Text::Autoformat |
221 |
my $wrap = $self->{wrap_margin}; |
my $wrap = $self->{wrap_margin}; |
222 |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { |
223 |
$body = autoformat $body; |
$body =~ s/[\r\n]/\n/gs; |
224 |
|
$body = autoformat($body, {right=>$wrap}); |
225 |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
$body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); |
226 |
} |
} |
227 |
|
|
246 |
@{$row->{'from'}} = $self->unroll($message,'from','phrase'); |
@{$row->{'from'}} = $self->unroll($message,'from','phrase'); |
247 |
@{$row->{'to'}} = $self->unroll($message,'to','phrase'); |
@{$row->{'to'}} = $self->unroll($message,'to','phrase'); |
248 |
@{$row->{'cc'}} = $self->unroll($message,'cc','phrase'); |
@{$row->{'cc'}} = $self->unroll($message,'cc','phrase'); |
249 |
$row->{'subject'} = $message->subject; |
$row->{'subject'} = $self->decode_qp($message->subject); |
250 |
$row->{'body'} = $self->plain_text_body($message); |
$row->{'body'} = $self->plain_text_body($message); |
251 |
$row->{'date'} = $message->date; |
$row->{'date'} = $message->date; |
252 |
|
|