/[mws]/trunk/MWS.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/MWS.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 13 - (show annotations)
Thu May 6 16:53:40 2004 UTC (20 years ago) by dpavlin
File size: 4026 byte(s)
partial implementation for dates, more verbose debugging, index should now
return all fields writen in it (this will break Plucene code, so it's
non-function from now on)

1 #!/usr/bin/perl -w
2
3 package MWS;
4
5 use strict;
6 use warnings;
7 use Carp;
8
9 use Mail::Box::Manager;
10 use Config::IniFiles;
11 use POSIX qw(strftime);
12 #use MWS_plucene;
13 use MWS_swish;
14
15 require Exporter;
16
17 our @ISA = qw(Exporter);
18
19 our %EXPORT_TAGS = ();
20 our @EXPORT_OK;
21 our @EXPORT;
22
23 our $VERSION = '1.00';
24
25
26 my $folder; # placeholder for folders
27
28 my $debug = 2;
29
30 sub new {
31 my $class = shift;
32 my $self = {};
33 bless($self, $class);
34
35 my $config_file = shift || die "need index file";
36
37 $self->{config} = new Config::IniFiles( -file => $config_file );
38
39 my $index_file = $self->{config}->val('global', 'index') || croak "can't find [index] section in config file with path of index";
40
41 $self->{mgr} = Mail::Box::Manager->new(access => 'r');
42 $self->{index_file} = $index_file;
43
44 # placeholder for opened folders
45 $self->{folder} = {};
46
47 return $self;
48 }
49
50 sub open_folder {
51 my $self = shift;
52
53 my $mbox = shift || croak "open_folder needs mbox name";
54
55 if (! $self->{folder}->{$mbox}) {
56 my $mbox_path = $self->{config}->val('folders', $mbox) || croak "comeone removed folder $mbox from config?";
57 print STDERR "about to open_folder($mbox)\n" if ($debug == 2);
58 $self->{folder}->{$mbox} = $self->{mgr}->open($mbox_path) || croak "can't open folder $mbox at '$mbox_path': $!";
59 print STDERR "open_folder($mbox)\n" if ($debug);
60 }
61
62 return $self->{folder}->{$mbox};
63
64 }
65
66 sub fetch_message {
67 my $self = shift;
68
69 my $mbox_id = shift || die "need mbox_id!";
70 my ($mbox,$id) = split(/ /,$mbox_id);
71
72 # return message with ID
73 print STDERR "fetch $id from $mbox\n" if ($debug);
74 return $self->open_folder($mbox)->find($id) ||
75 print STDERR "can't find message $id in $mbox. Time to re-index?\n";
76 }
77
78
79 sub search {
80 my $self = shift;
81
82 my $s = shift || carp "search called without argument!";
83
84 print STDERR "search_index($s)\n" if ($debug == 2);
85 my @index_ids = $self->search_index($s);
86
87 $self->{'index_ids'} = \@index_ids;
88
89 my $results = $#index_ids + 1;
90 $self->{'results'} = $results;
91
92 $self->{'curr_result'} = 0;
93
94 print STDERR "$results results\n" if ($debug == 2);
95
96 return $results || 'error';
97 }
98
99 sub unroll($$$) {
100 my $self = shift;
101
102 my ($message,$part,$sub) = @_;
103
104 my @arr;
105
106 foreach my $from ($message->$part) {
107 my $tmp = $from->$sub;
108 $tmp =~ s/^\s*["'](.*)["']\s*$/$1/;
109 push @arr, $tmp;
110 }
111 return \@arr;
112 }
113
114 sub fetch_all_results {
115 my $self = shift;
116
117 croak "results called before search!" if (! $self->{'index_ids'});
118
119 print STDERR "fetch_all_results_results\n" if ($debug == 2);
120
121 my @arr;
122
123 foreach my $id (@{$self->{'index_ids'}}) {
124 push @arr, $self->fetch_result_by_id($id);
125 }
126
127 return @arr;
128 }
129
130 sub fetch_result {
131 my $self = shift;
132
133 my $args = {@_};
134
135 croak "results called before search!" if (! $self->{'index_ids'});
136
137 my $curr = $self->{'curr_result'}++;
138
139 my $id = $self->{'index_ids'}->[$curr];
140
141 return $self->fetch_result_by_id($id);
142 }
143
144 sub plain_text_body {
145 my $self = shift;
146 my $message = shift || croak "plain_text_body needs message!";
147
148 if (! $message->isMultipart) {
149 return $message->decoded->string;
150 } else {
151 foreach my $part ($message->parts) {
152 if ($part->body->mimeType eq 'text/plain') {
153 return $part->decoded->string;
154 }
155 }
156 }
157 }
158
159
160 sub fetch_result_by_id {
161 my $self = shift;
162
163 my $id = shift || return;
164
165 my $row = $self->{cache}->{$id};
166
167 if (! $row) {
168
169 print STDERR "fetch_result_by_id($id) not in cache, hitting disk\n" if ($debug == 2);
170
171 my $message = $self->fetch_message($id) || print STDERR "can't fetch message '$id'";
172
173 $row->{'id'} = $id;
174 $row->{'from'} = $self->unroll($message,'from','phrase');
175 $row->{'to'} = $self->unroll($message,'to','phrase');
176 $row->{'cc'} = $self->unroll($message,'cc','phrase');
177 $row->{'subject'} = $message->subject;
178 $row->{'body'} = $self->plain_text_body($message);
179 $row->{'date'} = $message->date;
180
181 # XXX store in cache?
182 $self->{cache}->{$id} = $row;
183 print STDERR "$id stored in cache\n" if ($debug == 2);
184 } else {
185 print STDERR "fetch_result_by_id($id) in cache\n" if ($debug == 2);
186 }
187
188 return $row;
189
190 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26