/[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.7 - (show annotations)
Sun Mar 23 22:26:22 2003 UTC (21 years, 2 months ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +10 -6 lines
+ header-field 'Message-ID' now included when tracing

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

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