--- trunk/MWS.pm 2004/05/05 15:38:35 7 +++ trunk/MWS.pm 2004/05/06 23:06:08 16 @@ -6,9 +6,14 @@ use warnings; use Carp; -use Plucene::Simple; use Mail::Box::Manager; use Config::IniFiles; +use POSIX qw(strftime); +use Text::Autoformat; +use Text::Iconv; + +#use MWS_plucene; +use MWS_swish; require Exporter; @@ -20,10 +25,9 @@ our $VERSION = '1.00'; - my $folder; # placeholder for folders -my $debug = 1; +my $debug = 2; sub new { my $class = shift; @@ -36,31 +40,58 @@ my $index_file = $self->{config}->val('global', 'index') || croak "can't find [index] section in config file with path of index"; - $self->{index} = Plucene::Simple->open($index_file) || croak "can't open index '$index_file': $!"; - - $self->{mgr} = Mail::Box::Manager->new; + $self->{mgr} = Mail::Box::Manager->new(access => 'r'); + $self->{index_file} = $index_file; # placeholder for opened folders $self->{folder} = {}; + $self->{wrap_margin} = $self->{config}->val('global', 'wrap_margin'); + return $self; } +sub mbox_name2path { + my $self = shift; + + my $mbox = shift || croak "folder_name2path needs mbox name"; + + return $self->{config}->val('folders', $mbox) || croak "comeone removed folder $mbox from config?"; +} + sub open_folder { my $self = shift; my $mbox = shift || croak "open_folder needs mbox name"; if (! $self->{folder}->{$mbox}) { - my $mbox_path = $self->{config}->val('folders', $mbox) || croak "comeone removed folder $mbox from config?"; + my $mbox_path = $self->mbox_name2path($mbox); + + print STDERR "about to open_folder($mbox)\n" if ($debug == 2); + $self->{folder}->{$mbox} = $self->{mgr}->open($mbox_path) || croak "can't open folder $mbox at '$mbox_path': $!"; - print STDERR "## open($mbox)\n" if ($debug); + + print STDERR "open_folder($mbox) ok\n" if ($debug); } + $self->{fetch_count} = 0; + return $self->{folder}->{$mbox}; } +sub close_folder { + my $self = shift; + + my $mbox = shift || croak "open_folder needs mbox name"; + + $self->{folder}->{$mbox}->close(write => 'NEVER') || croak "can't close folder $mbox"; + + # XXX this is rather agressive!!! + $self->{folder} = {}; + return +} + sub fetch_message { my $self = shift; @@ -68,6 +99,13 @@ my ($mbox,$id) = split(/ /,$mbox_id); # return message with ID + print STDERR "fetch $id from $mbox\n" if ($debug); + + if ($self->{fetch_count}++ > 100) { + $self->close_folder($mbox); + print STDERR "close_folder($mbox) forced on ",$self->{fetch_count},"iteration\n"; + } + return $self->open_folder($mbox)->find($id) || print STDERR "can't find message $id in $mbox. Time to re-index?\n"; } @@ -78,7 +116,8 @@ my $s = shift || carp "search called without argument!"; - my @index_ids = $self->{index}->search($s); + print STDERR "search_index($s)\n" if ($debug == 2); + my @index_ids = $self->search_index($s); $self->{'index_ids'} = \@index_ids; @@ -87,20 +126,46 @@ $self->{'curr_result'} = 0; + print STDERR "$results results\n" if ($debug == 2); + return $results || 'error'; } +sub decode_qp($) { + my $self = shift; + + my $tmp = shift || return; + + sub decode($$) { + my ($cp,$qp) = @_; + my $iconv = Text::Iconv->new($cp,'ISO-8859-2'); + print STDERR "decode($cp,$qp) -> " if ($debug == 2); + $qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg; + $qp =~ s/_/ /g; + print STDERR "$qp\n" if ($debug == 2); + return $iconv->convert($qp); + } + + $tmp =~ s/=\?([^\?]+)\?Q\?(.+)\?=/decode($1,$2)/ex; + return $tmp; +} + sub unroll($$$) { + my $self = shift; + my ($message,$part,$sub) = @_; my @arr; foreach my $from ($message->$part) { - my $tmp = $from->$sub; + my $tmp = $from->$sub || next; + + $tmp = $self->decode_qp($tmp); $tmp =~ s/^\s*["'](.*)["']\s*$/$1/; push @arr, $tmp; } - return \@arr; + + return @arr; } sub fetch_all_results { @@ -108,6 +173,8 @@ croak "results called before search!" if (! $self->{'index_ids'}); + print STDERR "fetch_all_results [",scalar @{$self->{'index_ids'}},"]\n" if ($debug == 2); + my @arr; foreach my $id (@{$self->{'index_ids'}}) { @@ -127,6 +194,8 @@ my $curr = $self->{'curr_result'}++; my $id = $self->{'index_ids'}->[$curr]; + + print STDERR "fetch_result: $curr = $id\n" if ($debug == 2); return $self->fetch_result_by_id($id); } @@ -135,15 +204,28 @@ my $self = shift; my $message = shift || croak "plain_text_body needs message!"; + my $body; + if (! $message->isMultipart) { - return $message->decoded->string; + $body = $message->decoded->string; } else { foreach my $part ($message->parts) { if ($part->body->mimeType eq 'text/plain') { - return $part->decoded->string; + $body = $part->decoded->string; + last; } } } + + # reformat with Text::Autoformat + my $wrap = $self->{wrap_margin}; + if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) { + $body =~ s/[\r\n]/\n/gs; + $body = autoformat($body, {right=>$wrap}); + $body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); + } + + return $body; } @@ -152,14 +234,28 @@ my $id = shift || return; - my $message = $self->fetch_message($id); + my $row = $self->{cache}->{$id}; + + if (! $row) { + + print STDERR "fetch_result_by_id($id) not in cache, hitting disk\n" if ($debug == 2); - my $row; + my $message = $self->fetch_message($id) || print STDERR "can't fetch message '$id'"; - $row->{'id'} = $id; - $row->{'from'} = unroll($message,'from','phrase'); - $row->{'subject'} = $message->subject; - $row->{'body'} = $self->plain_text_body($message); + $row->{'id'} = $id; + @{$row->{'from'}} = $self->unroll($message,'from','phrase'); + @{$row->{'to'}} = $self->unroll($message,'to','phrase'); + @{$row->{'cc'}} = $self->unroll($message,'cc','phrase'); + $row->{'subject'} = $self->decode_qp($message->subject); + $row->{'body'} = $self->plain_text_body($message); + $row->{'date'} = $message->date; + + # XXX store in cache? + $self->{cache}->{$id} = $row; + print STDERR "$id stored in cache\n" if ($debug == 2); + } else { + print STDERR "fetch_result_by_id($id) in cache\n" if ($debug == 2); + } return $row;