/[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

Diff of /trunk/MWS.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 19 by dpavlin, Fri May 7 20:52:34 2004 UTC revision 41 by dpavlin, Mon May 10 20:26:17 2004 UTC
# Line 4  package MWS; Line 4  package MWS;
4    
5  use strict;  use strict;
6  use warnings;  use warnings;
 use Carp;  
7    
8    use lib '.';
9    
10    use Carp;
11  use Mail::Box::Manager;  use Mail::Box::Manager;
12  use Config::IniFiles;  use Config::IniFiles;
13  use POSIX qw(strftime);  use POSIX qw(strftime);
14  use Text::Autoformat;  use Text::Autoformat;
15  use Text::Iconv;  use Text::Iconv;
16  use Text::Unaccent;  use Text::Unaccent;
17    use Date::Parse;
18  #use MWS_plucene;  use POSIX qw(strftime);
19  use MWS_swish;  use MIME::Base64;
   
 require Exporter;  
   
 our @ISA = qw(Exporter);  
   
 our %EXPORT_TAGS = ();  
 our @EXPORT_OK;  
 our @EXPORT;  
20    
21  our $VERSION = '1.00';  our $VERSION = '1.00';
22    
23  my $folder;     # placeholder for folders  my $folder;     # placeholder for folders
24    
25  my $debug = 2;  my $debug = 1;
26    
27  sub new {  sub new {
28          my $class = shift;          my $class = shift;
29          my $self = {};          my $self = {@_};
30          bless($self, $class);          bless($self, $class);
31    
         my $config_file = shift || die "need index file";  
32    
33          $self->{config} = new Config::IniFiles( -file => $config_file );          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_file = $self->{config}->val('global', 'index') || croak "can't find [index] section in config file with path of index";          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');          $self->{mgr} = Mail::Box::Manager->new(access => 'r');
42          $self->{index_file} = $index_file;          $self->{index_dir} = $index_dir;
43    
44          # placeholder for opened folders          # placeholder for opened folders
45          $self->{folder} = {};          $self->{folder} = {};
# Line 63  sub normalize_string { Line 60  sub normalize_string {
60          $v = join('',sort split(/\s+/,$v));          $v = join('',sort split(/\s+/,$v));
61          $v =~ s/\W+//g;          $v =~ s/\W+//g;
62    
63          return $v;          return lc($v);
64  }  }
65    
66  # reset tables for search results  # reset tables for search results
# Line 88  sub add_counter($$) { Line 85  sub add_counter($$) {
85          return $self->{counter}->{$c}->{$k}->{usage}++;          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 {  sub counter {
126          my $self = shift;          my $self = shift;
127    
# Line 166  sub fetch_message { Line 200  sub fetch_message {
200  sub search {  sub search {
201          my $self = shift;          my $self = shift;
202    
203          my $s = shift || carp "search called without argument!";          carp "search called without argument!" if (! @_);
204    
205          $self->reset_counters;          $self->reset_counters;
206    
207          print STDERR "search_index($s)\n" if ($debug == 2);          print STDERR "search(",join(" ",@_),")\n" if ($debug == 2);
208          my @index_ids = $self->search_index($s);          my @index_ids = $self->search_index(@_);
209    
210          $self->{'index_ids'} = \@index_ids;          $self->{'index_ids'} = \@index_ids;
211    
# Line 182  sub search { Line 216  sub search {
216    
217          $self->{'curr_result'} = 0;          $self->{'curr_result'} = 0;
218    
219            $self->reset_counters;
220    
221          print STDERR "$results results\n" if ($debug == 2);          print STDERR "$results results\n" if ($debug == 2);
222    
223          return $results || 'error';          return $results || 'error';
# Line 192  sub decode_qp($) { Line 228  sub decode_qp($) {
228    
229          my $tmp = shift || return;          my $tmp = shift || return;
230    
231          sub decode($$) {          sub decode($$$) {
232                  my ($cp,$qp) = @_;                  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');                  my $iconv = Text::Iconv->new($cp,'ISO-8859-2');
249          print STDERR "decode($cp,$qp) -> " if ($debug == 2);                  return $iconv->convert($qp) || '';
                 $qp =~ s/=([a-f0-9][a-f0-9])/chr(hex($1))/ieg;  
                 $qp =~ s/_/ /g;  
         print STDERR "$qp\n" if ($debug == 2);  
                 return $iconv->convert($qp) || $qp;  
250          }          }
251    
252          $tmp =~ s/=\?([^\?]+)\?Q\?(.+?)\?=/decode($1,$2)/ex;          $tmp =~ s/=\?([^\?]+)\?([QB])\?(.+?)\?=/decode($1,$2,$3)/ige;
253          $tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g;          $tmp =~ s/^\s*["']+(.*?)["']+\s*$/$1/g;
254            #print STDERR "$tmp\n" if ($debug == 2);
255          return $tmp;          return $tmp;
256  }  }
257    
# Line 276  sub plain_text_body { Line 324  sub plain_text_body {
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          # reformat with Text::Autoformat
334          my $wrap = $self->{wrap_margin};          my $wrap = $self->{wrap_margin};
335          if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) {          if ($wrap && $body && $body =~ m/^.{$wrap}..*$/m) {
336                  $body =~ s/[\r\n]/\n/gs;                  $body = autoformat($body, {right=>$wrap, all=>1});
                 $body = autoformat($body, {right=>$wrap});  
337                  $body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2);                  $body .="\n[reformated using autoformat, margin at $wrap]" if ($debug == 2);
338          }          }
339    
# Line 304  sub fetch_result_by_id { Line 357  sub fetch_result_by_id {
357                  $row->{'id'} = $id;                  $row->{'id'} = $id;
358    
359                  foreach my $p (qw(from to cc bcc)) {                  foreach my $p (qw(from to cc bcc)) {
360                          foreach my $v ($self->unroll($message,'from','phrase')) {                          foreach my $v ($self->unroll($message,$p,'phrase')) {
361                                  push @{$row->{$p}},$v;                                  push @{$row->{$p}},$v;
362                                  $self->add_counter($p,$v);                                  $self->add_counter($p,$v);
363                          }                          }
364                  }                  }
365                  $row->{'subject'} = $self->decode_qp($message->subject);                  $row->{'subject'} = $self->decode_qp($message->subject);
366                  $row->{'body'} = $self->plain_text_body($message);                  $row->{'body'} = $self->plain_text_body($message);
367                  $row->{'date'} = $message->date;                  my $utime = str2time($message->date);
368    
369                    $row->{'date_utime'} = $utime;
370    
371                    $row->{'date'} = strftime("%Y-%m-%d %H:%M:%S", localtime($utime));
372                    $self->add_counter_calendar($utime);
373    
374                  # XXX store in cache?                  # XXX store in cache?
375                  $self->{cache}->{$id} = $row;                  $self->{cache}->{$id} = $row;
376                  print STDERR "$id stored in cache\n" if ($debug == 2);                  print STDERR "$id stored in cache\n" if ($debug == 2);
377          } else {          } else {
378                  print STDERR "fetch_result_by_id($id) in cache\n" if ($debug == 2);                  print STDERR "fetch_result_by_id($id) in cache\n" if ($debug == 2);
379                    foreach my $p (qw(from to cc bcc)) {
380                            foreach my $v (@{$row->{$p}}) {
381                                    $self->add_counter($p,$v);
382                            }
383                    }
384    
385                    $self->add_counter_calendar($row->{date_utime});
386          }          }
387    
388          return $row;          return $row;

Legend:
Removed from v.19  
changed lines
  Added in v.41

  ViewVC Help
Powered by ViewVC 1.1.26