/[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.6 - (show annotations)
Sun Mar 23 21:12:20 2003 UTC (21 years, 3 months ago) by root
Branch: MAIN
Changes since 1.5: +18 -6 lines
+ sub jerror and related modifications

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

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