6 |
use warnings; |
use warnings; |
7 |
use Carp; |
use Carp; |
8 |
|
|
9 |
|
use Plucene::Simple; |
10 |
|
use Mail::Box::Manager; |
11 |
|
use Config::IniFiles; |
12 |
|
|
13 |
require Exporter; |
require Exporter; |
14 |
|
|
15 |
our @ISA = qw(Exporter); |
our @ISA = qw(Exporter); |
30 |
my $self = {}; |
my $self = {}; |
31 |
bless($self, $class); |
bless($self, $class); |
32 |
|
|
33 |
my $index_file = shift || die "need index file"; |
my $config_file = shift || die "need index file"; |
34 |
|
|
35 |
|
$self->{config} = new Config::IniFiles( -file => $config_file ); |
36 |
|
|
37 |
|
my $index_file = $self->{config}->val('global', 'index') || croak "can't find [index] section in config file with path of index"; |
38 |
|
|
39 |
$self->{index} = Plucene::Simple->open($index_file) || die "can't open index '$index_file': $!"; |
$self->{index} = Plucene::Simple->open($index_file) || croak "can't open index '$index_file': $!"; |
40 |
|
|
41 |
$self->{mgr} = Mail::Box::Manager->new; |
$self->{mgr} = Mail::Box::Manager->new; |
42 |
|
|
54 |
my ($mbox,$id) = split(/ /,$mbox_id); |
my ($mbox,$id) = split(/ /,$mbox_id); |
55 |
|
|
56 |
if (! $self->{folder}->{$mbox}) { |
if (! $self->{folder}->{$mbox}) { |
57 |
$self->{folder}->{$mbox} = $self->{mgr}->open($mbox); |
my $mbox_path = $self->{config}->val('folders', $mbox) || croak "comeone removed folder $mbox from config?"; |
58 |
|
$self->{folder}->{$mbox} = $self->{mgr}->open($mbox_path) || croak "can't open folder $mbox at '$mbox_path': $!"; |
59 |
print STDERR "## open($mbox)\n" if ($debug); |
print STDERR "## open($mbox)\n" if ($debug); |
60 |
} |
} |
61 |
|
|
93 |
} |
} |
94 |
return \@arr; |
return \@arr; |
95 |
} |
} |
96 |
|
|
97 |
|
sub fetch_all_results { |
98 |
|
my $self = shift; |
99 |
|
|
100 |
|
croak "results called before search!" if (! $self->{'index_ids'}); |
101 |
|
|
102 |
|
my @arr; |
103 |
|
|
104 |
|
foreach my $id (@{$self->{'index_ids'}}) { |
105 |
|
push @arr, $self->fetch_result_by_id($id); |
106 |
|
} |
107 |
|
|
108 |
|
return @arr; |
109 |
|
} |
110 |
|
|
111 |
sub fetch_result { |
sub fetch_result { |
112 |
my $self = shift; |
my $self = shift; |
113 |
|
|
114 |
|
my $args = {@_}; |
115 |
|
|
116 |
croak "results called before search!" if (! $self->{'index_ids'}); |
croak "results called before search!" if (! $self->{'index_ids'}); |
117 |
|
|
118 |
my $curr = $self->{'curr_result'}++; |
my $curr = $self->{'curr_result'}++; |
119 |
|
|
120 |
my $id = $self->{'index_ids'}->[$curr]; |
my $id = $self->{'index_ids'}->[$curr]; |
121 |
|
|
122 |
return if (! $id); |
return $self->fetch_result_by_id($id); |
123 |
|
} |
124 |
|
|
125 |
|
sub fetch_result_by_id { |
126 |
|
my $self = shift; |
127 |
|
|
128 |
|
my $id = shift || return; |
129 |
|
|
130 |
my $message = $self->fetch_message($id); |
my $message = $self->fetch_message($id); |
131 |
|
|
133 |
|
|
134 |
$row->{'from'} = unroll($message,'from','phrase'); |
$row->{'from'} = unroll($message,'from','phrase'); |
135 |
$row->{'subject'} = $message->get('Subject'); |
$row->{'subject'} = $message->get('Subject'); |
136 |
|
if (! $message->isMultipart) { |
137 |
|
$row->{'body'} = $message->decoded->string; |
138 |
|
} else { |
139 |
|
foreach my $part ($message->parts) { |
140 |
|
if ($part->body->mimeType eq 'text/plain') { |
141 |
|
$row->{'body'} = $part->decoded->string; |
142 |
|
last; |
143 |
|
} |
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
return $row; |
return $row; |
148 |
|
|