--- trunk/MWS.pm 2004/05/06 12:40:11 12 +++ trunk/MWS.pm 2004/05/06 19:46:58 14 @@ -8,6 +8,9 @@ use Mail::Box::Manager; use Config::IniFiles; +use POSIX qw(strftime); +use Text::Autoformat; + #use MWS_plucene; use MWS_swish; @@ -21,10 +24,9 @@ our $VERSION = '1.00'; - my $folder; # placeholder for folders -my $debug = 1; +my $debug = 2; sub new { my $class = shift; @@ -43,24 +45,46 @@ # 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); } return $self->{folder}->{$mbox}; } +sub close_folder { + my $self = shift; + + my $mbox = shift || croak "open_folder needs mbox name"; + + return $self->{folder}->{$mbox}->close(write => 'NEVER'); +} + sub fetch_message { my $self = shift; @@ -68,6 +92,7 @@ my ($mbox,$id) = split(/ /,$mbox_id); # return message with ID + print STDERR "fetch $id from $mbox\n" if ($debug); return $self->open_folder($mbox)->find($id) || print STDERR "can't find message $id in $mbox. Time to re-index?\n"; } @@ -78,6 +103,7 @@ my $s = shift || carp "search called without argument!"; + print STDERR "search_index($s)\n" if ($debug == 2); my @index_ids = $self->search_index($s); $self->{'index_ids'} = \@index_ids; @@ -87,20 +113,27 @@ $self->{'curr_result'} = 0; + print STDERR "$results results\n" if ($debug == 2); + return $results || 'error'; } sub unroll($$$) { + my $self = shift; + my ($message,$part,$sub) = @_; my @arr; foreach my $from ($message->$part) { my $tmp = $from->$sub; - $tmp =~ s/^\s*["'](.*)["']\s*$/$1/; - push @arr, $tmp; + if ($tmp) { + $tmp =~ s/^\s*["'](.*)["']\s*$/$1/; + push @arr, $tmp; + } } - return \@arr; + + return @arr; } sub fetch_all_results { @@ -108,6 +141,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 +162,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 +172,27 @@ 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 = autoformat $body; + $body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2); + } + + return $body; } @@ -152,14 +201,28 @@ my $id = shift || return; - my $message = $self->fetch_message($id); + my $row = $self->{cache}->{$id}; + + if (! $row) { - my $row; + print STDERR "fetch_result_by_id($id) not in cache, hitting disk\n" if ($debug == 2); - $row->{'id'} = $id; - $row->{'from'} = unroll($message,'from','phrase'); - $row->{'subject'} = $message->subject; - $row->{'body'} = $self->plain_text_body($message); + my $message = $self->fetch_message($id) || print STDERR "can't fetch message '$id'"; + + $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'} = $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;