/[cvs]/nfo/perl/libs/Mail/Audit/Dispatch.pm
ViewVC logotype

Diff of /nfo/perl/libs/Mail/Audit/Dispatch.pm

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

revision 1.1 by root, Wed Jan 22 07:45:20 2003 UTC revision 1.7 by root, Sun Mar 23 22:26:22 2003 UTC
# Line 9  Line 9 
9  #  #
10  # ============================================================  # ============================================================
11  #  $Log$  #  $Log$
12    #  Revision 1.7  2003/03/23 22:26:22  root
13    #  + header-field 'Message-ID' now included when tracing
14    #
15    #  Revision 1.6  2003/03/23 21:12:20  root
16    #  + sub jerror and related modifications
17    #
18    #  Revision 1.5  2003/01/30 23:20:21  root
19    #  + fixed and enhanced
20    #
21    #  Revision 1.4  2003/01/22 17:58:21  root
22    #  + fixed and enhanced many things
23    #  - refactored code to Data::Code
24    #
25    #  Revision 1.3  2003/01/22 07:55:43  joko
26    #  + replaced '$HOME' with '$self->{settings}->{HOME}'
27    #
28    #  Revision 1.2  2003/01/22 07:54:24  joko
29    #  + replaced global variables with class-variables
30    #
31  #  Revision 1.1  2003/01/22 07:45:20  root  #  Revision 1.1  2003/01/22 07:45:20  root
32  #  + initial check-in - refactored from 'dispatchmail'  #  + initial check-in - refactored from 'dispatchmail'
33  #  #
# Line 27  package Mail::Audit::Dispatch; Line 46  package Mail::Audit::Dispatch;
46  use strict;  use strict;
47  # don't use warnings;  # don't use warnings;
48    
49  use base qw( DesignPattern::Object );  use base qw(
50      DesignPattern::Object
51      DesignPattern::Bridge
52    );
53    #  DesignPattern::Object::Logger
54    
55    
56  use Mail::Audit;  use Mail::Audit;
57  use Data::Dumper;  use Data::Dumper;
58    
59    
60  use org::netfrag::shortcuts qw( now );  use Data::Code::Symbol qw( export_symbols );
61    use Data::Storage::Handler::File qw( a2f );
62    use org::netfrag::shortcuts qw( now get_chomped run_cmd );
 my $LOG     = 1;        # writes reports to logfile  
 my $VERBOSE = 1;        # writes reports to STDOUT  
 my $TRACE   = 1;        # writes contents of messages to logfile  
63    
64    
65  sub _init {  sub _init {
66    my $self = shift;    my $self = shift;
67    $self->_init_options();    $self->_init_options();
68      $self->_override_options();
69    $self->_init_settings();    $self->_init_settings();
70    $self->_override_settings();    $self->_override_settings();
71      #$self->_run();
72  }  }
73    
74  sub _init_options {  sub _init_options {
75    my $self = shift;    my $self = shift;
76    $self->{options}->{base}   = $self->{base};    foreach (qw( user base rules LOG VERBOSE TRACE mode newsgroup )) {
77        $self->{options}->{$_} = $self->{$_};
78      }
79    }
80    
81    sub _override_options {
82      my $self = shift;
83    $self->{options}->{base} ||= $ENV{HOME};    $self->{options}->{base} ||= $ENV{HOME};
84    $self->{options}->{base} ||= $ENV{PWD};    $self->{options}->{base} ||= $ENV{PWD};
   $self->{options}->{rules}   = $self->{rules};  
85  }  }
86    
87  # - - - - - - - - - - - - - - - - - - - -  sub _init_settings {
88  #    my $self = shift;
89  #                targets    $self->{settings}->{USER} = $self->{options}->{user} if $self->{options}->{user};
90  #    $self->{settings}->{USER} ||= '';
91  #  declare and initialize some variables    $self->{settings}->{USER} ||= $ENV{USER};
92  #      these are mostly base paths  
93  #      mail should be delivered to    $self->{settings}->{HOME} = '../var/spool/mail/' . $self->{settings}->{USER};
94  #    $self->{settings}->{HOME} = $self->{options}->{base} if $self->{options}->{base};
95  # - - - - - - - - - - - - - - - - - - - -  
96  sub _init_paths {    $self->_init_settings_paths();
97    }
98    
99    sub _init_settings_paths {
100    my $self = shift;    my $self = shift;
101    $self->{settings}->{USER}      = $ENV{USER};    $self->{settings}->{MAILDIR}   = "$self->{settings}->{HOME}/Mail";
102    $self->{settings}->{HOME}      = $self->{options}->{base};    #$self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc.pm";
103    $self->{settings}->{MAILDIR}   = "$HOME/Mail";    $self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc";
104    $self->{settings}->{RULESFILE} = "$HOME/.dispatchmailrc.pm";    #$self->{settings}->{LOCKFILE}  = "$self->{settings}->{HOME}/.procmail.lockfile";
105    #$self->{settings}->{LOCKFILE}  = "$HOME/.procmail.lockfile";    $self->{settings}->{LOCKFILE}  = "$self->{settings}->{HOME}/.dispatchmail.lockfile";
106    $self->{settings}->{LOCKFILE}  = "$HOME/.dispatchmail.lockfile";    $self->{settings}->{LOGFILE}   = "$self->{settings}->{MAILDIR}/.mail-delivery.log";
107    $self->{settings}->{LOGFILE}   = "$MAILDIR/.dispatchmail.log";    $self->{settings}->{ERRLOG}    = ".mail-delivery_errors.log";
108    $self->{settings}->{DEFAULT}   = "$MAILDIR/Inbox";    $self->{settings}->{DEFAULT}   = "$self->{settings}->{MAILDIR}/Inbox";
109  }  }
110    
111  sub _override_settings {  sub _override_settings {
112    my $self = shift;    my $self = shift;
113    # override $RULESFILE if given as option on the command line    # override $self->{settings}->{RULESFILE} if given as option on the command line
114      $self->{settings}->{RULESFILE} =      $self->{settings}->{RULESFILE} =
115        $self->{options}->{rules} if $self->{options}->{rules};        $self->{options}->{rules} if $self->{options}->{rules};
116    # change logfile    # change logfile
117      $self->{settings}->{LOGFILE} =      $self->{settings}->{LOGFILE} =
118        "recievemail-emerg.log" if (! -e $self->{settings}->{MAILDIR});        $self->{settings}->{ERRLOG} if (! -e $self->{settings}->{MAILDIR});
   
     $self->{settings}->{USER} ||= '';  
119  }  }
120    
121    
# Line 96  sub _override_settings { Line 124  sub _override_settings {
124  #                 main  #                 main
125  # - - - - - - - - - - - - - - - - - - - -  # - - - - - - - - - - - - - - - - - - - -
126    
127    sub run {
128      my $self = shift;
129      
130      $self->{options}->{mode} ||= 'recieve';
131      my $call = '_' . $self->{options}->{mode};
132    
133    # "jump" into processing of new incoming mail and get a "handler" to this mail    # "jump" into processing of new incoming mail and get a "handler" to this mail
134    my $incoming = Mail::Audit->new;    $self->{incoming} = Mail::Audit->new();
135    
136    sub traceEntry {    my $result = $self->$call(@_);
137      s2f('-' x 40 . '  TRACE  ' . '-' x 10);    return $result;
138      s2f("From:    " . gchomp($incoming->from));  
139      s2f("To:      " . gchomp($incoming->to));  }
     s2f("Subject: " . gchomp($incoming->subject));  
     s2f('-' x 40 . '  TRACE  ' . '-' x 10);  
   }  
140    
141    sub _recieve {
142    
143      my $self = shift;
144      
145    # 0.a. pre flight tracing    # 0.a. pre flight tracing
146      my $now = now();      my $now = now();
147      report("$0 running at $now for user '$USER'.");      $self->report("$now - $0 running for user '$self->{settings}->{USER}'.");
148      traceEntry() if $TRACE;      $self->traceEntry() if $self->{options}->{TRACE};
149    
150    # 0.b. pre flight checks    # 0.b. pre flight checks
151    
152       # TODO: check if $HOME is empty       # TODO: check if $self->{settings}->{HOME} is empty
153    
154       # check if $HOME exists       # check if $self->{settings}->{HOME} exists
155       if (! -e $MAILDIR) {       if (! -e $self->{settings}->{MAILDIR}) {
156         my $msg = "delivery failed, base directory $MAILDIR does not exist";         my $msg = "ERROR: Delivery failed, base directory '$self->{settings}->{MAILDIR}' does not exist.";
157         report($msg);         $self->jerror($msg);
        forward_delivery();  
158       }       }
159    
160    # 1. include rules or fallback    # 1. include rules or fallback
161      # check if $RULESFILE exists      # check if $self->{settings}->{RULESFILE} exists
162      if (-f $RULESFILE) {      if (-f $self->{settings}->{RULESFILE}) {
163        report("Loading rules from \"$RULESFILE\".");        $self->report("RULES:  Loading from \"$self->{settings}->{RULESFILE}\".");
164        require $RULESFILE;        my $evalstr = "require '$self->{settings}->{RULESFILE}';";
165          eval($evalstr);
166          if ($@) {
167            my $msg = "ERROR:  Delivery failed, '$self->{settings}->{RULESFILE}' had syntax errors:\n$@";
168            $self->jerror($msg);
169          }
170      } else {      } else {
171        #die("$RULESFILE doesn't exist");        #die("$self->{settings}->{RULESFILE} doesn't exist");
172        report("Configured rulesfile \"$RULESFILE\" doesn't exist.");        $self->jerror("Configured rulesfile \"$self->{settings}->{RULESFILE}\" doesn't exist.");
       forward_delivery();  
173      }      }
174    
175    # 2. export required stuff to rules namespace    # 2. export required stuff to rules namespace
176      export_symbols();      my @symbols = qw( jaccept report compareTarget accept copy ignore );
177        export_symbols(\@symbols, 'rules');
178    
179    # 3. run dispatcher    # 3. run dispatcher
180      report("Running \"rules::dispatch\".");      $self->report("RULES:  Running Perl sub \"rules::dispatch\".");
181      rules::dispatch();      rules::dispatch($self);
182    
183    # 4. dispatcher didn't do anything    # 4. dispatcher didn't do anything
184      report("dispatcher could not apply any filter, using default delivery");      $self->report("dispatcher could not apply any filter, using default delivery");
185    
186      # the default-handler: simply accept all mails and route them to "/var/spool/mail"      # the default-handler: simply accept all mails and route them to "/var/spool/mail"
187      # $incoming->accept();      # $self->{incoming}->accept();
188    
189      # if you want to reject all mails coming through to here, do a ...      # if you want to reject all mails coming through to here, do a ...
190      # $incoming->reject;      # $self->{incoming}->reject;
191    
192      # catch all mails and route them to a "DEFAULT"-inbox      # catch all mails and route them to a "DEFAULT"-inbox
193      jaccept($DEFAULT);      $self->jaccept($self->{settings}->{DEFAULT});
194    
195    }
196    
197    sub jerror {
198      my $self = shift;
199      my $msg = shift;
200      $self->report("ERROR: $msg");
201      $self->forward_delivery();
202    }
203    
204    sub _mail2news {
205      my $self = shift;
206      $self->report("MAIL2NEWS: $self->{options}->{newsgroup}");
207      my $plugin = 'Newsgate';
208      $self->load($plugin);
209      $self->$plugin($self->{options}->{newsgroup});
210    }
211    
212    
213    
214    # - - - - - - - - - - - - - - - - - - - -    # - - - - - - - - - - - - - - - - - - - -
215    #          tracing & reporting    #          tracing & reporting
216    # - - - - - - - - - - - - - - - - - - - -    # - - - - - - - - - - - - - - - - - - - -
217    sub s2f {    sub traceEntry {
218      my $str = shift;      my $self = shift;
219      open(FH, '>>' . $LOGFILE);      $self->appendLog('-' x 40 . '  TRACE  ' . '-' x 10);
220      print FH $str, "\n";      $self->appendLog("From:       " . get_chomped($self->{incoming}->from));
221      close(FH);      $self->appendLog("To:         " . get_chomped($self->{incoming}->to));
222        $self->appendLog("Subject:    " . get_chomped($self->{incoming}->subject));
223        $self->appendLog("Message-ID: " . get_chomped($self->{incoming}->get('Message-ID')));
224        $self->appendLog('-' x 40 . '  TRACE  ' . '-' x 10);
225      }
226    
227      sub appendLog {
228        my $self = shift;
229        my $msg = shift;
230        a2f($self->{settings}->{LOGFILE}, $msg);
231    }    }
232    
233    sub report {    sub report {
234        my $self = shift;
235      my $msg = shift;          my $msg = shift;    
236      # TODO: tracing, debugging      # TODO: tracing, debugging
237    
238      print $msg, "\n" if $VERBOSE;      #print STDERR $msg, "\n" if $self->{options}->{VERBOSE};
239      if ($LOG) {      if ($self->{options}->{LOG}) {
240        s2f($msg);        $self->appendLog($msg);
241      }      }
242    
243    }    }
# Line 184  sub _override_settings { Line 247  sub _override_settings {
247    # - - - - - - - - - - - - - - - - - - - -    # - - - - - - - - - - - - - - - - - - - -
248    
249    sub compareTarget {    sub compareTarget {
250        my $self = shift;
251      my $pattern = shift;      my $pattern = shift;
252      my $ok = 0;      my $ok = 0;
253      $ok = 1 if ($incoming->to =~ m/$pattern/);      $ok = 1 if ($self->{incoming}->to =~ m/$pattern/);
254      $ok = 1 if ($incoming->cc =~ m/$pattern/);      $ok = 1 if ($self->{incoming}->cc =~ m/$pattern/);
255      $ok = 1 if ($incoming->bcc =~ m/$pattern/);      $ok = 1 if ($self->{incoming}->bcc =~ m/$pattern/);
256      return $ok;      return $ok;
257    }    }
258    
259      sub accept_spool {
260        my $self = shift;
261        my $path = "/var/spool/mail/$self->{settings}->{USER}";
262        $self->report("defaulting to spool delivery ($path)");
263        $self->{incoming}->accept($path);
264      }
265    
266      sub forward_delivery {
267        my $self = shift;
268        $self->report("Forwarding delivery to next handler in queue (probably /var/spool/mail).");
269        return $self->{incoming}->accept;
270      }
271    
272    
273    sub jaccept {    sub jaccept {
274        my $self = shift;
275      my $deliver_to = shift;      my $deliver_to = shift;
276            
277      report("ACCEPT: $deliver_to");      $self->report("ACCEPT: $deliver_to");
278    
279      # check deliver_to path      # check deliver_to path
280      if (! -e $deliver_to) {      if (! -e $deliver_to) {
281        report("deliver_to path \"$deliver_to\" doesn't exist");        my $good = 0;
282        forward_delivery();        $self->report("ERROR:  TARGET Path/File doesn't exist.");
283        return;        if ($self->{settings}->{AUTOCREATE_FOLDERS}) {
284            my $cmd = "touch $deliver_to";
285            $self->report("TARGET: AUTOCREATE_FOLDERS is enabled: touching TARGET.");
286            #if (mkdir $deliver_to) {
287            run_cmd($cmd);
288            # re-test TARGET - for existance now
289            if (-e $deliver_to) {
290              $good = 1;
291            } else {
292              $self->report("ERROR:  TARGET creation failed (command was: '$cmd').");
293            }
294          }
295          if (!$good) {
296            $self->forward_delivery();
297            return;
298          }
299      }      }
300    
301      $incoming->accept($deliver_to);      return $self->{incoming}->accept($deliver_to);
302    }    }
303    
   sub accept_spool {  
     my $path = "/var/spool/mail/$USER";  
     report("defaulting to spool delivery ($path)");  
     $incoming->accept($path);  
   }  
304    
305    sub forward_delivery {    sub accept {
306      report("Forwarding delivery to next handler in queue (probably /var/spool/mail).");      my $self = shift;
307      $incoming->accept;      return $self->jaccept(@_);
308    }    }
309    
310      sub copy {
311        my $self = shift;
312        my $plugin = shift;
313        my $deliver_to = shift;
314        
315        $self->report("COPY: $plugin: $deliver_to");
316    
317        $self->load($plugin);
318        return $self->$plugin($deliver_to);
319      }
320    
321  # - - - - - - - - - - - - - - - - - - - -    sub ignore {
322  #             helper functions      my $self = shift;
323  # - - - - - - - - - - - - - - - - - - - -      $self->report("IGNORE");
324        return $self->{incoming}->ignore;
325      }
326    
327  sub get_coderef {  1;
   my $codepack = shift;  
   my $method = shift;  
   $codepack || return '[error]';  
   $method ||= '';  
   $method && ($codepack .= '::');  
   return eval '\&' . $codepack . $method . ';';  
 }  
   
 sub export_symbols {  
   #  my $callpack = 'rules';  
   #  my @EXPORT = qw( incoming subject MAILDIR jaccept );  
   #  foreach my $sym (@EXPORT) {  
       no strict 'refs';  
   #    *{"${callpack}::$sym"} = get_coderef('main', $sym);  
   #  }  
   {  
     no strict 'refs';  
     *{"rules::jaccept"}       = get_coderef('main', 'jaccept');  
     *{"rules::report"}        = get_coderef('main', 'report');  
     *{"rules::compareTarget"} = get_coderef('main', 'compareTarget');  
   }  
   $rules::MAILDIR  = $MAILDIR;  
   $rules::incoming = $incoming;  
 }  
   
 sub gchomp {  
   my $str = shift;  
   chomp($str);  
   return $str;  
 }  

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.7

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed