/[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.5 - (show annotations)
Thu Jan 30 23:20:21 2003 UTC (21 years, 5 months ago) by root
Branch: MAIN
Changes since 1.4: +92 -33 lines
+ fixed and enhanced

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

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