/[cvs]/nfo/perl/scripts/dispatchmail/bin/dispatchmail
ViewVC logotype

Annotation of /nfo/perl/scripts/dispatchmail/bin/dispatchmail

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Wed Jan 22 05:38:44 2003 UTC (21 years, 11 months ago) by collector
Branch: MAIN
Changes since 1.1: +13 -54 lines
+ prepared refactoring to 'dispatchmail'

1 collector 1.1 #!/usr/bin/perl
2    
3 collector 1.2 # ============================================================
4 collector 1.1 #
5 collector 1.2 # dispatchmail v0.05
6     # A simple mail filter done in perl
7     # with the Perl-module "Mail::Audit"
8     # available from CPAN.
9     #
10     # $Id$
11     #
12     # ============================================================
13     # $Log$
14     # ============================================================
15 collector 1.1
16     use strict;
17     # don't use warnings;
18    
19     use Mail::Audit;
20     use Data::Dumper;
21     use Getopt::Long;
22    
23    
24     # - - - - - - - - - - - - - - - - - - - -
25     # options
26     # - - - - - - - - - - - - - - - - - - - -
27     my $opt_base = '';
28     my $opt_rulesfile = '';
29     GetOptions(
30     'base=s' => \$opt_base,
31     'rules=s' => \$opt_rulesfile,
32     );
33     $opt_base ||= $ENV{HOME};
34     $opt_base ||= $ENV{PWD};
35    
36    
37     # - - - - - - - - - - - - - - - - - - - -
38     #
39     # targets
40     #
41     # declare and initialize some variables
42     # these are mostly base paths
43     # mail should be delivered to
44     #
45     # - - - - - - - - - - - - - - - - - - - -
46     #my $HOME = "/home/joko/virtual/joko_mail";
47     my $USER = $ENV{USER};
48     my $HOME = $opt_base;
49     my $MAILDIR = "$HOME/Mail";
50     my $LOGFILE = "$MAILDIR/recievemail.log";
51 collector 1.2 my $RULESFILE = "$HOME/.recievemailrc.pm";
52 collector 1.1 my $LOCKFILE = "$HOME/.procmail.lockfile";
53     my $DEFAULT = "$MAILDIR/Inbox";
54     my $DEBUG = 0;
55     my $TRACE = 1;
56     my $VERBOSE = 1;
57    
58     # override settings
59     # override $RULESFILE if given as option on the command line
60     $RULESFILE = $opt_rulesfile if ($opt_rulesfile);
61     # change logfile
62     $LOGFILE = "recievemail-emerg.log" if (! -e $MAILDIR);
63    
64    
65     # - - - - - - - - - - - - - - - - - - - -
66     # main
67     # - - - - - - - - - - - - - - - - - - - -
68    
69     # "jump" into processing of new incoming mail and get a "handler" to this mail
70     my $incoming = Mail::Audit->new;
71    
72     # 0.a. pre flight tracing
73     if ($TRACE) {
74 collector 1.2 s2f("-" x 80);
75 collector 1.1 s2f("Mail from " . gchomp($incoming->from) . " to " . gchomp($incoming->to));
76     s2f("Subject: " . gchomp($incoming->subject));
77     }
78    
79     # 0.b. pre flight checks
80    
81     # TODO: check if $HOME is empty
82    
83     # check if $HOME exists
84     if (! -e $MAILDIR) {
85     my $msg = "delivery failed, base directory $MAILDIR does not exist";
86     report($msg);
87     accept_spool();
88     }
89    
90     # 1. include rules or fallback
91     # check if $RULESFILE exists
92     if (-f $RULESFILE) {
93     report("loading rules from \"$RULESFILE\"");
94     require $RULESFILE;
95     } else {
96     #die("$RULESFILE doesn't exist");
97     report("configured rulesfile \"$RULESFILE\" doesn't exist");
98     accept_spool();
99     }
100    
101     # 2. export required stuff to rules namespace
102     export_symbols();
103    
104     # 3. run dispatcher
105     report("running \"rules::dispatch\"");
106     rules::dispatch();
107    
108     # 4. dispatcher didn't do anything
109     report("dispatcher could not apply any filter, using default delivery");
110    
111     # the default-handler: simply accept all mails and route them to "/var/spool/mail"
112     # $incoming->accept();
113    
114     # if you want to reject all mails coming through to here, do a ...
115     # $incoming->reject;
116    
117     # catch all mails and route them to a "DEFAULT"-inbox
118     jaccept($DEFAULT);
119    
120    
121    
122    
123    
124     # - - - - - - - - - - - - - - - - - - - -
125     # tracing & reporting
126     # - - - - - - - - - - - - - - - - - - - -
127     sub s2f {
128     my $str = shift;
129     open(FH, '>>' . $LOGFILE);
130     print FH $str, "\n";
131     close(FH);
132     }
133    
134     sub report {
135     my $msg = shift;
136     # TODO: tracing, debugging
137    
138     print $msg, "\n" if $VERBOSE;
139     if ($TRACE) {
140     s2f($msg);
141     }
142    
143     }
144    
145     # - - - - - - - - - - - - - - - - - - - -
146     # processing mail
147     # - - - - - - - - - - - - - - - - - - - -
148    
149     sub compareTarget {
150     my $pattern = shift;
151     my $ok = 0;
152     $ok = 1 if ($incoming->to =~ m/$pattern/);
153     $ok = 1 if ($incoming->cc =~ m/$pattern/);
154     $ok = 1 if ($incoming->bcc =~ m/$pattern/);
155     return $ok;
156     }
157    
158     sub jaccept {
159     my $deliver_to = shift;
160    
161     report("delivering to: $deliver_to");
162    
163     # check deliver_to path
164     if (! -e $deliver_to) {
165     report("deliver_to path \"$deliver_to\" doesn't exist");
166     accept_spool();
167     return;
168     }
169    
170     $incoming->accept($deliver_to);
171     }
172    
173     sub accept_spool {
174     my $path = "/var/spool/mail/$USER";
175     report("defaulting to spool delivery ($path)");
176     $incoming->accept($path);
177     }
178    
179    
180     # - - - - - - - - - - - - - - - - - - - -
181     # helper functions
182     # - - - - - - - - - - - - - - - - - - - -
183    
184     sub get_coderef {
185     my $codepack = shift;
186     my $method = shift;
187     $codepack || return '[error]';
188     $method ||= '';
189     $method && ($codepack .= '::');
190     return eval '\&' . $codepack . $method . ';';
191     }
192    
193     sub export_symbols {
194     # my $callpack = 'rules';
195     # my @EXPORT = qw( incoming subject MAILDIR jaccept );
196     # foreach my $sym (@EXPORT) {
197     no strict 'refs';
198     # *{"${callpack}::$sym"} = get_coderef('main', $sym);
199     # }
200     {
201     no strict 'refs';
202     *{"rules::jaccept"} = get_coderef('main', 'jaccept');
203     *{"rules::report"} = get_coderef('main', 'report');
204     *{"rules::compareTarget"} = get_coderef('main', 'compareTarget');
205     }
206     $rules::MAILDIR = $MAILDIR;
207     $rules::incoming = $incoming;
208     }
209    
210     sub gchomp {
211     my $str = shift;
212     chomp($str);
213     return $str;
214     }

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