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