/[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 42 - (show annotations)
Mon May 10 22:04:01 2004 UTC (19 years, 11 months ago) by dpavlin
File size: 8144 byte(s)
minor improvements and beginning of (non functional in this revision!) support
for folders

1 #!/usr/bin/perl -w
2
3 package MWS;
4
5 use strict;
6 use warnings;
7
8 use lib '.';
9
10 use Carp;
11 use Mail::Box::Manager;
12 use Config::IniFiles;
13 use POSIX qw(strftime);
14 use Text::Autoformat;
15 use Text::Iconv;
16 use Text::Unaccent;
17 use Date::Parse;
18 use POSIX qw(strftime);
19 use MIME::Base64;
20
21 our $VERSION = '1.00';
22
23 my $debug = 1;
24
25 my @counters = qw(from to cc bcc folder);
26
27 sub new {
28 my $class = shift;
29 my $self = {@_};
30 bless($self, $class);
31
32
33 my $config_name = $self->{config_file} || croak "need config_file";
34 $config_name =~ s/\.conf.*$//;
35 $self->{config_name} = $config_name;
36
37 $self->{config} = new Config::IniFiles( -file => $self->{config_file} );
38
39 my $index_dir = $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_dir} = $index_dir;
43
44 # placeholder for opened folders
45 $self->{folder} = {};
46
47 $self->{wrap_margin} = $self->{config}->val('global', 'wrap_margin');
48 $self->{max_results} = $self->{config}->val('global', 'max_results') || 100;
49 $self->reset_counters;
50
51 return $self;
52 }
53
54 sub normalize_string {
55 my $self = shift;
56
57 my $v = shift || return;
58
59 $v = unac_string('ISO-8859-2', $v);
60 $v = join('',sort split(/\s+/,$v));
61 $v =~ s/\W+//g;
62
63 return lc($v);
64 }
65
66 # reset tables for search results
67 sub reset_counters {
68 my $self = shift;
69
70 $self->{counter} = {};
71
72 # foreach my $c (qw(thread from to cc bcc lists links att)) {
73 # $self->{counter}->{$c} = {};
74 # }
75
76 }
77
78 sub add_counter($$) {
79 my $self = shift;
80
81 my ($c,$v) = @_;
82 my $k = $self->normalize_string($v);
83
84 $self->{counter}->{$c}->{$k}->{name} = $v || return;
85 return $self->{counter}->{$c}->{$k}->{usage}++;
86 }
87
88 sub yyyymmdd {
89 my $self = shift;
90
91 my $t = shift || time;
92
93 my (undef,undef,undef,$dd,$mm,$yyyy) = localtime($t);
94 $mm++;
95 $yyyy+=1900;
96 return ($yyyy,$mm,$dd);
97 }
98
99 sub fmtdate {
100 my $self = shift;
101
102 my @out;
103 my @formats = qw(%04d %02d %02d);
104 while (my $v = shift) {
105 my $f = shift @formats;
106 push @out, sprintf($f, $v);
107 }
108
109 print STDERR "fmtdate: ",join('|',@out),"\n" if ($debug == 2);
110
111 return (wantarray ? @out : join("-",@out));
112 }
113
114 sub add_counter_calendar($) {
115 my $self = shift;
116
117 my $t = shift || croak "add_counter_calendar without argument!";
118
119 my ($yyyy,$mm,$dd) = $self->fmtdate($self->yyyymmdd($t));
120
121 return $self->{counter}->{calendar}->{"$yyyy-$mm"}->{$dd}++;
122 }
123
124
125 sub counter {
126 my $self = shift;
127
128 my $c = shift || return;
129
130 return if (! $self->{counter}->{$c});
131
132 return $self->{counter}->{$c};
133 }
134
135 sub mbox_name2path {
136 my $self = shift;
137
138 my $mbox = shift || croak "folder_name2path needs mbox name";
139
140 return $self->{config}->val('folders', $mbox) || croak "comeone removed folder $mbox from config?";
141 }
142
143 sub open_folder {
144 my $self = shift;
145
146 my $mbox = shift || croak "open_folder needs mbox name";
147
148 if (! $self->{folder}->{$mbox}) {
149 my $mbox_path = $self->mbox_name2path($mbox);
150
151 print STDERR "about to open_folder($mbox)\n" if ($debug == 2);
152
153 $self->{folder}->{$mbox} = $self->{mgr}->open($mbox_path) || croak "can't open folder $mbox at '$mbox_path': $!";
154
155 print STDERR "open_folder($mbox) ok\n" if ($debug);
156 }
157
158 $self->{fetch_count} = 0;
159
160 return $self->{folder}->{$mbox};
161
162 }
163
164 sub close_folder {
165 my $self = shift;
166
167 my $mbox = shift || croak "open_folder needs mbox name";
168
169 $self->{folder}->{$mbox}->close(write => 'NEVER') || croak "can't close folder $mbox";
170
171 # XXX this is rather agressive!!!
172 $self->{folder} = {};
173 return
174 }
175
176 sub fetch_message {
177 my $self = shift;
178
179 my $mbox_id = shift || die "need mbox_id!";
180 my ($mbox,$id) = split(/ /,$mbox_id);
181
182 # return message with ID
183 print STDERR "fetch $id from $mbox\n" if ($debug);
184
185 if ($self->{fetch_count}++ > 100) {
186 $self->close_folder($mbox);
187 print STDERR "close_folder($mbox) forced on ",$self->{fetch_count},"iteration\n";
188 }
189
190 my $msg = $self->open_folder($mbox)->find($id);
191 if ($msg) {
192 return $msg;
193 } else {
194 print STDERR "can't find message $id in $mbox. Time to re-index?\n";
195 return;
196 }
197 }
198
199
200 sub search {
201 my $self = shift;
202
203 carp "search called without argument!" if (! @_);
204
205 $self->reset_counters;
206
207 print STDERR "search(",join(" ",@_),")\n" if ($debug == 2);
208 my @index_ids = $self->search_index(@_);
209
210 $self->{'index_ids'} = \@index_ids;
211
212 #my $results = $#index_ids + 1;
213 #$self->{'results'} = $results;
214
215 my $results = $self->{'total_hits'} || ($#index_ids + 1);
216
217 $self->{'curr_result'} = 0;
218
219 $self->reset_counters;
220
221 print STDERR "$results results\n" if ($debug == 2);
222
223 return $results || 'error';
224 }
225
226 sub decode_qp($) {
227 my $self = shift;
228
229 my $tmp = shift || return;
230
231 sub decode($$$) {
232 my ($cp,$enc,$qp) = @_;
233
234 print STDERR "decode($cp,$qp) -> " if ($debug == 2);
235
236 if (uc($enc) eq "Q") {
237 $qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg;
238 $qp =~ s/_/ /g;
239 } elsif (uc($enc) eq "B") {
240 $qp = decode_base64($qp);
241 } else {
242 croak "unsupported encoding '$enc' in decode_qp\n";
243 return $qp;
244 }
245
246 print STDERR "$qp\n" if ($debug == 2);
247
248 my $iconv = Text::Iconv->new($cp,'ISO-8859-2');
249 return $iconv->convert($qp) || '';
250 }
251
252 $tmp =~ s/=\?([^\?]+)\?([QB])\?(.+?)\?=/decode($1,$2,$3)/ige;
253 $tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g;
254 #print STDERR "$tmp\n" if ($debug == 2);
255 return $tmp;
256 }
257
258 sub unroll($$$) {
259 my $self = shift;
260
261 my ($message,$part,$sub) = @_;
262
263 my @arr;
264
265 return if (! $message->$part);
266
267 foreach my $from ($message->$part) {
268 my $tmp = $from->$sub || next;
269
270 $tmp = $self->decode_qp($tmp);
271 push @arr, $tmp;
272 }
273
274 return @arr;
275 }
276
277 sub fetch_all_results {
278 my $self = shift;
279
280 croak "results called before search!" if (! $self->{'index_ids'});
281
282 print STDERR "fetch_all_results [",scalar @{$self->{'index_ids'}},"]\n" if ($debug == 2);
283
284 my @arr;
285
286 foreach my $id (@{$self->{'index_ids'}}) {
287 push @arr, $self->fetch_result_by_id($id);
288 }
289
290
291 return @arr;
292 }
293
294 sub fetch_result {
295 my $self = shift;
296
297 my $args = {@_};
298
299 croak "results called before search!" if (! $self->{'index_ids'});
300
301 my $curr = $self->{'curr_result'}++;
302
303 my $id = $self->{'index_ids'}->[$curr];
304
305 print STDERR "fetch_result: $curr = $id\n" if ($debug == 2);
306
307 return $self->fetch_result_by_id($id);
308 }
309
310 sub plain_text_body {
311 my $self = shift;
312 my $message = shift || croak "plain_text_body needs message!";
313
314 my $body;
315
316 if (! $message->isMultipart) {
317 $body = $message->decoded->string;
318 } else {
319 foreach my $part ($message->parts) {
320 if ($part->body->mimeType eq 'text/plain') {
321 $body = $part->decoded->string;
322 last;
323 }
324 }
325 }
326
327 if (! $body) {
328 $body = "[plain/text body not found]" if ($debug == 2);
329 print STDERR "plain/text body not found\n" if ($debug);
330 return;
331 }
332
333 # reformat with Text::Autoformat
334 my $wrap = $self->{wrap_margin};
335 if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) {
336 $body = autoformat($body, {right=>$wrap, all=>1});
337 $body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2);
338 }
339
340 return $body;
341 }
342
343
344 sub fetch_result_by_id {
345 my $self = shift;
346
347 my $id = shift || return;
348
349 my $row = $self->{cache}->{$id};
350
351 if (! $row) {
352
353 print STDERR "fetch_result_by_id($id) not in cache, hitting disk\n" if ($debug == 2);
354
355 my $message = $self->fetch_message($id) || return;
356
357 $row->{'id'} = $id;
358
359 foreach my $p (@counters) {
360 foreach my $v ($self->unroll($message,$p,'phrase')) {
361 push @{$row->{$p}},$v;
362 $self->add_counter($p,$v);
363 }
364 }
365
366 $self->add_counter('folder', $1) if ($id =~ m/^(\S+)\s/);
367
368 $row->{'subject'} = $self->decode_qp($message->subject);
369 $row->{'body'} = $self->plain_text_body($message);
370 my $utime = str2time($message->date);
371
372 $row->{'date_utime'} = $utime;
373
374 $row->{'date'} = strftime("%Y-%m-%d %H:%M:%S", localtime($utime));
375 $self->add_counter_calendar($utime);
376
377 # XXX store in cache?
378 $self->{cache}->{$id} = $row;
379 print STDERR "$id stored in cache\n" if ($debug == 2);
380 } else {
381 print STDERR "fetch_result_by_id($id) in cache\n" if ($debug == 2);
382 foreach my $p (@counters) {
383 foreach my $v (@{$row->{$p}}) {
384 $self->add_counter($p,$v);
385 }
386 }
387
388 $self->add_counter('folder', $1) if ($id =~ m/^(\S+)\s/);
389
390 $self->add_counter_calendar($row->{date_utime});
391 }
392
393 return $row;
394
395 }

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26