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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Wed Jan 22 07:55:43 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.2: +10 -7 lines
+ replaced '$HOME' with '$self->{settings}->{HOME}'

1 # ============================================================
2 #
3 # Mail::Audit::Dispatch v0.05
4 # A simple mail filter done in perl
5 # with the Perl-module "Mail::Audit"
6 # available from CPAN.
7 #
8 # $Id: Dispatch.pm,v 1.2 2003/01/22 07:54:24 joko Exp $
9 #
10 # ============================================================
11 # $Log: Dispatch.pm,v $
12 # Revision 1.2 2003/01/22 07:54:24 joko
13 # + replaced global variables with class-variables
14 #
15 # Revision 1.1 2003/01/22 07:45:20 root
16 # + initial check-in - refactored from 'dispatchmail'
17 #
18 # Revision 1.3 2003/01/22 07:16:51 root
19 # + sub traceEntry
20 # + further refactorings
21 #
22 # Revision 1.2 2003/01/22 05:38:44 collector
23 # + prepared refactoring to 'dispatchmail'
24 #
25 # ============================================================
26
27
28 package Mail::Audit::Dispatch;
29
30 use strict;
31 # don't use warnings;
32
33 use base qw( DesignPattern::Object );
34
35
36 use Mail::Audit;
37 use Data::Dumper;
38
39
40 use org::netfrag::shortcuts qw( now );
41
42
43 my $LOG = 1; # writes reports to logfile
44 my $self->{options}->{VERBOSE} = 1; # writes reports to STDOUT
45 my $self->{options}->{TRACE} = 1; # writes contents of messages to logfile
46
47
48 sub _init {
49 my $self = shift;
50 $self->_init_options();
51 $self->_override_options();
52 $self->_init_settings();
53 $self->_override_settings();
54 $self->_run();
55 }
56
57 sub _init_options {
58 my $self = shift;
59 foreach (qw( base rules LOG VERBOSE TRACE )) {
60 $self->{options}->{$_} = $self->{$_};
61 }
62 }
63
64 sub _override_options {
65 my $self = shift;
66 $self->{options}->{base} ||= $ENV{HOME};
67 $self->{options}->{base} ||= $ENV{PWD};
68 }
69
70 # - - - - - - - - - - - - - - - - - - - -
71 #
72 # targets
73 #
74 # declare and initialize some variables
75 # these are mostly base paths
76 # mail should be delivered to
77 #
78 # - - - - - - - - - - - - - - - - - - - -
79 sub _init_settings {
80 my $self = shift;
81 $self->{settings}->{USER} = $ENV{USER};
82 $self->{settings}->{HOME} = $self->{options}->{base};
83 $self->{settings}->{MAILDIR} = "$self->{settings}->{HOME}/Mail";
84 $self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc.pm";
85 #$self->{settings}->{LOCKFILE} = "$self->{settings}->{HOME}/.procmail.lockfile";
86 $self->{settings}->{LOCKFILE} = "$self->{settings}->{HOME}/.dispatchmail.lockfile";
87 $self->{settings}->{LOGFILE} = "$self->{settings}->{MAILDIR}/.dispatchmail.log";
88 $self->{settings}->{DEFAULT} = "$self->{settings}->{MAILDIR}/Inbox";
89 }
90
91 sub _override_settings {
92 my $self = shift;
93 # override $self->{settings}->{RULESFILE} if given as option on the command line
94 $self->{settings}->{RULESFILE} =
95 $self->{options}->{rules} if $self->{options}->{rules};
96 # change logfile
97 $self->{settings}->{LOGFILE} =
98 "recievemail-emerg.log" if (! -e $self->{settings}->{MAILDIR});
99
100 $self->{settings}->{USER} ||= '';
101 }
102
103
104
105 # - - - - - - - - - - - - - - - - - - - -
106 # main
107 # - - - - - - - - - - - - - - - - - - - -
108
109 sub _run {
110 my $self = shift;
111
112 # "jump" into processing of new incoming mail and get a "handler" to this mail
113 $self->{incoming} = Mail::Audit->new();
114
115 }
116
117 sub traceEntry {
118 my $self = shift;
119 s2f('-' x 40 . ' TRACE ' . '-' x 10);
120 s2f("From: " . gchomp($self->{incoming}->from));
121 s2f("To: " . gchomp($self->{incoming}->to));
122 s2f("Subject: " . gchomp($self->{incoming}->subject));
123 s2f('-' x 40 . ' TRACE ' . '-' x 10);
124 }
125
126
127 # 0.a. pre flight tracing
128 my $now = now();
129 report("$0 running at $now for user '$USER'.");
130 traceEntry() if $self->{options}->{TRACE};
131
132 # 0.b. pre flight checks
133
134 # TODO: check if $self->{settings}->{HOME} is empty
135
136 # check if $self->{settings}->{HOME} exists
137 if (! -e $self->{settings}->{MAILDIR}) {
138 my $msg = "delivery failed, base directory $self->{settings}->{MAILDIR} does not exist";
139 report($msg);
140 forward_delivery();
141 }
142
143 # 1. include rules or fallback
144 # check if $self->{settings}->{RULESFILE} exists
145 if (-f $self->{settings}->{RULESFILE}) {
146 report("Loading rules from \"$self->{settings}->{RULESFILE}\".");
147 require $self->{settings}->{RULESFILE};
148 } else {
149 #die("$self->{settings}->{RULESFILE} doesn't exist");
150 report("Configured rulesfile \"$self->{settings}->{RULESFILE}\" doesn't exist.");
151 forward_delivery();
152 }
153
154 # 2. export required stuff to rules namespace
155 export_symbols();
156
157 # 3. run dispatcher
158 report("Running \"rules::dispatch\".");
159 rules::dispatch();
160
161 # 4. dispatcher didn't do anything
162 report("dispatcher could not apply any filter, using default delivery");
163
164 # the default-handler: simply accept all mails and route them to "/var/spool/mail"
165 # $self->{incoming}->accept();
166
167 # if you want to reject all mails coming through to here, do a ...
168 # $self->{incoming}->reject;
169
170 # catch all mails and route them to a "DEFAULT"-inbox
171 jaccept($self->{settings}->{DEFAULT});
172
173
174
175
176
177 # - - - - - - - - - - - - - - - - - - - -
178 # tracing & reporting
179 # - - - - - - - - - - - - - - - - - - - -
180 sub s2f {
181 my $str = shift;
182 open(FH, '>>' . $self->{settings}->{LOGFILE});
183 print FH $str, "\n";
184 close(FH);
185 }
186
187 sub report {
188 my $msg = shift;
189 # TODO: tracing, debugging
190
191 print $msg, "\n" if $self->{options}->{VERBOSE};
192 if ($LOG) {
193 s2f($msg);
194 }
195
196 }
197
198 # - - - - - - - - - - - - - - - - - - - -
199 # processing mail
200 # - - - - - - - - - - - - - - - - - - - -
201
202 sub compareTarget {
203 my $pattern = shift;
204 my $ok = 0;
205 $ok = 1 if ($self->{incoming}->to =~ m/$pattern/);
206 $ok = 1 if ($self->{incoming}->cc =~ m/$pattern/);
207 $ok = 1 if ($self->{incoming}->bcc =~ m/$pattern/);
208 return $ok;
209 }
210
211 sub jaccept {
212 my $deliver_to = shift;
213
214 report("ACCEPT: $deliver_to");
215
216 # check deliver_to path
217 if (! -e $deliver_to) {
218 report("deliver_to path \"$deliver_to\" doesn't exist");
219 forward_delivery();
220 return;
221 }
222
223 $self->{incoming}->accept($deliver_to);
224 }
225
226 sub accept_spool {
227 my $path = "/var/spool/mail/$USER";
228 report("defaulting to spool delivery ($path)");
229 $self->{incoming}->accept($path);
230 }
231
232 sub forward_delivery {
233 report("Forwarding delivery to next handler in queue (probably /var/spool/mail).");
234 $self->{incoming}->accept;
235 }
236
237
238 # - - - - - - - - - - - - - - - - - - - -
239 # helper functions
240 # - - - - - - - - - - - - - - - - - - - -
241
242 sub get_coderef {
243 my $codepack = shift;
244 my $method = shift;
245 $codepack || return '[error]';
246 $method ||= '';
247 $method && ($codepack .= '::');
248 return eval '\&' . $codepack . $method . ';';
249 }
250
251 sub export_symbols {
252 # my $callpack = 'rules';
253 # my @EXPORT = qw( incoming subject MAILDIR jaccept );
254 # foreach my $sym (@EXPORT) {
255 no strict 'refs';
256 # *{"${callpack}::$sym"} = get_coderef('main', $sym);
257 # }
258 {
259 no strict 'refs';
260 *{"rules::jaccept"} = get_coderef('main', 'jaccept');
261 *{"rules::report"} = get_coderef('main', 'report');
262 *{"rules::compareTarget"} = get_coderef('main', 'compareTarget');
263 }
264 $rules::MAILDIR = $self->{settings}->{MAILDIR};
265 $rules::incoming = $self->{incoming};
266 }
267
268 sub gchomp {
269 my $str = shift;
270 chomp($str);
271 return $str;
272 }
273
274 1;

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