1 |
#!/usr/bin/perl -w |
2 |
|
3 |
package MWS; |
4 |
|
5 |
use strict; |
6 |
use warnings; |
7 |
use Carp; |
8 |
|
9 |
require Exporter; |
10 |
|
11 |
our @ISA = qw(Exporter); |
12 |
|
13 |
our %EXPORT_TAGS = (); |
14 |
our @EXPORT_OK; |
15 |
our @EXPORT; |
16 |
|
17 |
our $VERSION = '1.00'; |
18 |
|
19 |
|
20 |
my $folder; # placeholder for folders |
21 |
|
22 |
my $debug = 1; |
23 |
|
24 |
sub new { |
25 |
my $class = shift; |
26 |
my $self = {}; |
27 |
bless($self, $class); |
28 |
|
29 |
my $index_file = shift || die "need index file"; |
30 |
|
31 |
$self->{index} = Plucene::Simple->open($index_file) || die "can't open index '$index_file': $!"; |
32 |
|
33 |
$self->{mgr} = Mail::Box::Manager->new; |
34 |
|
35 |
# placeholder for opened folders |
36 |
$self->{folder} = {}; |
37 |
|
38 |
return $self; |
39 |
} |
40 |
|
41 |
|
42 |
sub fetch_message { |
43 |
my $self = shift; |
44 |
|
45 |
my $mbox_id = shift || die "need mbox_id!"; |
46 |
my ($mbox,$id) = split(/ /,$mbox_id); |
47 |
|
48 |
if (! $self->{folder}->{$mbox}) { |
49 |
$self->{folder}->{$mbox} = $self->{mgr}->open($mbox); |
50 |
print STDERR "## open($mbox)\n" if ($debug); |
51 |
} |
52 |
|
53 |
my $message = $self->{folder}->{$mbox}->find($id) || |
54 |
print STDERR "can't find message $id in $mbox. Time to re-index?\n"; |
55 |
|
56 |
return $message; |
57 |
} |
58 |
|
59 |
|
60 |
sub search { |
61 |
my $self = shift; |
62 |
|
63 |
my $s = shift || carp "search called without argument!"; |
64 |
|
65 |
my @index_ids = $self->{index}->search($s); |
66 |
|
67 |
$self->{'index_ids'} = \@index_ids; |
68 |
|
69 |
my $results = $#index_ids + 1; |
70 |
$self->{'results'} = $results; |
71 |
|
72 |
$self->{'curr_result'} = 0; |
73 |
|
74 |
return $results || 'error'; |
75 |
} |
76 |
|
77 |
sub unroll($$$) { |
78 |
my ($message,$part,$sub) = @_; |
79 |
|
80 |
my @arr; |
81 |
|
82 |
foreach my $from ($message->$part) { |
83 |
push @arr, $from->$sub; |
84 |
} |
85 |
return \@arr; |
86 |
} |
87 |
|
88 |
sub fetch_result { |
89 |
my $self = shift; |
90 |
|
91 |
croak "results called before search!" if (! $self->{'index_ids'}); |
92 |
|
93 |
my $curr = $self->{'curr_result'}++; |
94 |
|
95 |
my $id = $self->{'index_ids'}->[$curr]; |
96 |
|
97 |
return if (! $id); |
98 |
|
99 |
my $message = $self->fetch_message($id); |
100 |
|
101 |
my $row; |
102 |
|
103 |
$row->{'from'} = unroll($message,'from','phrase'); |
104 |
$row->{'subject'} = $message->get('Subject'); |
105 |
|
106 |
return $row; |
107 |
|
108 |
} |