1 |
root |
1.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 |
root |
1.6 |
# $Id: Dispatch.pm,v 1.5 2003/01/30 23:20:21 root Exp $ |
9 |
root |
1.1 |
# |
10 |
|
|
# ============================================================ |
11 |
joko |
1.2 |
# $Log: Dispatch.pm,v $ |
12 |
root |
1.6 |
# Revision 1.5 2003/01/30 23:20:21 root |
13 |
|
|
# + fixed and enhanced |
14 |
|
|
# |
15 |
root |
1.5 |
# Revision 1.4 2003/01/22 17:58:21 root |
16 |
|
|
# + fixed and enhanced many things |
17 |
|
|
# - refactored code to Data::Code |
18 |
|
|
# |
19 |
root |
1.4 |
# Revision 1.3 2003/01/22 07:55:43 joko |
20 |
|
|
# + replaced '$HOME' with '$self->{settings}->{HOME}' |
21 |
|
|
# |
22 |
joko |
1.3 |
# Revision 1.2 2003/01/22 07:54:24 joko |
23 |
|
|
# + replaced global variables with class-variables |
24 |
|
|
# |
25 |
joko |
1.2 |
# Revision 1.1 2003/01/22 07:45:20 root |
26 |
|
|
# + initial check-in - refactored from 'dispatchmail' |
27 |
|
|
# |
28 |
root |
1.1 |
# 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 |
root |
1.4 |
use base qw( |
44 |
|
|
DesignPattern::Object |
45 |
root |
1.5 |
DesignPattern::Bridge |
46 |
root |
1.4 |
); |
47 |
|
|
# DesignPattern::Object::Logger |
48 |
root |
1.1 |
|
49 |
|
|
|
50 |
|
|
use Mail::Audit; |
51 |
|
|
use Data::Dumper; |
52 |
|
|
|
53 |
|
|
|
54 |
root |
1.4 |
use Data::Code::Symbol qw( export_symbols ); |
55 |
|
|
use Data::Storage::Handler::File qw( a2f ); |
56 |
root |
1.5 |
use org::netfrag::shortcuts qw( now get_chomped run_cmd ); |
57 |
root |
1.1 |
|
58 |
|
|
|
59 |
|
|
sub _init { |
60 |
|
|
my $self = shift; |
61 |
|
|
$self->_init_options(); |
62 |
joko |
1.2 |
$self->_override_options(); |
63 |
root |
1.1 |
$self->_init_settings(); |
64 |
|
|
$self->_override_settings(); |
65 |
root |
1.4 |
#$self->_run(); |
66 |
root |
1.1 |
} |
67 |
|
|
|
68 |
|
|
sub _init_options { |
69 |
|
|
my $self = shift; |
70 |
root |
1.5 |
foreach (qw( user base rules LOG VERBOSE TRACE mode newsgroup )) { |
71 |
joko |
1.2 |
$self->{options}->{$_} = $self->{$_}; |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
sub _override_options { |
76 |
|
|
my $self = shift; |
77 |
root |
1.1 |
$self->{options}->{base} ||= $ENV{HOME}; |
78 |
|
|
$self->{options}->{base} ||= $ENV{PWD}; |
79 |
|
|
} |
80 |
|
|
|
81 |
joko |
1.2 |
sub _init_settings { |
82 |
root |
1.1 |
my $self = shift; |
83 |
root |
1.4 |
$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 |
joko |
1.3 |
$self->{settings}->{MAILDIR} = "$self->{settings}->{HOME}/Mail"; |
96 |
root |
1.5 |
#$self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc.pm"; |
97 |
|
|
$self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc"; |
98 |
joko |
1.3 |
#$self->{settings}->{LOCKFILE} = "$self->{settings}->{HOME}/.procmail.lockfile"; |
99 |
|
|
$self->{settings}->{LOCKFILE} = "$self->{settings}->{HOME}/.dispatchmail.lockfile"; |
100 |
joko |
1.2 |
$self->{settings}->{LOGFILE} = "$self->{settings}->{MAILDIR}/.dispatchmail.log"; |
101 |
root |
1.4 |
$self->{settings}->{ERRLOG} = ".dispatchmail-errors.log"; |
102 |
joko |
1.2 |
$self->{settings}->{DEFAULT} = "$self->{settings}->{MAILDIR}/Inbox"; |
103 |
root |
1.1 |
} |
104 |
|
|
|
105 |
|
|
sub _override_settings { |
106 |
|
|
my $self = shift; |
107 |
joko |
1.2 |
# override $self->{settings}->{RULESFILE} if given as option on the command line |
108 |
root |
1.1 |
$self->{settings}->{RULESFILE} = |
109 |
|
|
$self->{options}->{rules} if $self->{options}->{rules}; |
110 |
|
|
# change logfile |
111 |
|
|
$self->{settings}->{LOGFILE} = |
112 |
root |
1.4 |
$self->{settings}->{ERRLOG} if (! -e $self->{settings}->{MAILDIR}); |
113 |
root |
1.1 |
} |
114 |
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
118 |
|
|
# main |
119 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
120 |
|
|
|
121 |
root |
1.5 |
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 |
root |
1.4 |
|
133 |
root |
1.5 |
} |
134 |
root |
1.4 |
|
135 |
root |
1.5 |
sub _recieve { |
136 |
root |
1.4 |
|
137 |
joko |
1.2 |
my $self = shift; |
138 |
|
|
|
139 |
root |
1.1 |
# 0.a. pre flight tracing |
140 |
|
|
my $now = now(); |
141 |
root |
1.4 |
$self->report("$now - $0 running for user '$self->{settings}->{USER}'."); |
142 |
|
|
$self->traceEntry() if $self->{options}->{TRACE}; |
143 |
root |
1.1 |
|
144 |
|
|
# 0.b. pre flight checks |
145 |
|
|
|
146 |
joko |
1.3 |
# TODO: check if $self->{settings}->{HOME} is empty |
147 |
root |
1.1 |
|
148 |
joko |
1.3 |
# check if $self->{settings}->{HOME} exists |
149 |
joko |
1.2 |
if (! -e $self->{settings}->{MAILDIR}) { |
150 |
root |
1.5 |
my $msg = "ERROR: Delivery failed, base directory '$self->{settings}->{MAILDIR}' does not exist."; |
151 |
root |
1.6 |
$self->jerror($msg); |
152 |
root |
1.1 |
} |
153 |
|
|
|
154 |
|
|
# 1. include rules or fallback |
155 |
joko |
1.2 |
# check if $self->{settings}->{RULESFILE} exists |
156 |
|
|
if (-f $self->{settings}->{RULESFILE}) { |
157 |
root |
1.5 |
$self->report("RULES: Loading from \"$self->{settings}->{RULESFILE}\"."); |
158 |
root |
1.6 |
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 |
root |
1.1 |
} else { |
165 |
joko |
1.2 |
#die("$self->{settings}->{RULESFILE} doesn't exist"); |
166 |
root |
1.6 |
$self->jerror("Configured rulesfile \"$self->{settings}->{RULESFILE}\" doesn't exist."); |
167 |
root |
1.1 |
} |
168 |
|
|
|
169 |
|
|
# 2. export required stuff to rules namespace |
170 |
root |
1.5 |
my @symbols = qw( jaccept report compareTarget accept copy ignore ); |
171 |
root |
1.4 |
export_symbols(\@symbols, 'rules'); |
172 |
root |
1.1 |
|
173 |
|
|
# 3. run dispatcher |
174 |
root |
1.5 |
$self->report("RULES: Running Perl sub \"rules::dispatch\"."); |
175 |
root |
1.4 |
rules::dispatch($self); |
176 |
root |
1.1 |
|
177 |
|
|
# 4. dispatcher didn't do anything |
178 |
root |
1.4 |
$self->report("dispatcher could not apply any filter, using default delivery"); |
179 |
root |
1.1 |
|
180 |
|
|
# the default-handler: simply accept all mails and route them to "/var/spool/mail" |
181 |
joko |
1.2 |
# $self->{incoming}->accept(); |
182 |
root |
1.1 |
|
183 |
|
|
# if you want to reject all mails coming through to here, do a ... |
184 |
joko |
1.2 |
# $self->{incoming}->reject; |
185 |
root |
1.1 |
|
186 |
|
|
# catch all mails and route them to a "DEFAULT"-inbox |
187 |
root |
1.4 |
$self->jaccept($self->{settings}->{DEFAULT}); |
188 |
root |
1.1 |
|
189 |
root |
1.4 |
} |
190 |
root |
1.1 |
|
191 |
root |
1.6 |
sub jerror { |
192 |
|
|
my $self = shift; |
193 |
|
|
my $msg = shift; |
194 |
|
|
$self->report("ERROR: $msg"); |
195 |
|
|
$self->forward_delivery(); |
196 |
|
|
} |
197 |
root |
1.5 |
|
198 |
|
|
sub _mail2news { |
199 |
root |
1.4 |
my $self = shift; |
200 |
root |
1.5 |
$self->report("MAIL2NEWS: $self->{options}->{newsgroup}"); |
201 |
|
|
my $plugin = 'Newsgate'; |
202 |
|
|
$self->load($plugin); |
203 |
|
|
$self->$plugin($self->{options}->{newsgroup}); |
204 |
root |
1.4 |
} |
205 |
root |
1.1 |
|
206 |
|
|
|
207 |
root |
1.5 |
|
208 |
root |
1.1 |
# - - - - - - - - - - - - - - - - - - - - |
209 |
|
|
# tracing & reporting |
210 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
211 |
root |
1.5 |
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 |
root |
1.4 |
sub appendLog { |
221 |
|
|
my $self = shift; |
222 |
|
|
my $msg = shift; |
223 |
|
|
a2f($self->{settings}->{LOGFILE}, $msg); |
224 |
root |
1.1 |
} |
225 |
|
|
|
226 |
|
|
sub report { |
227 |
root |
1.4 |
my $self = shift; |
228 |
root |
1.1 |
my $msg = shift; |
229 |
|
|
# TODO: tracing, debugging |
230 |
|
|
|
231 |
root |
1.4 |
#print STDERR $msg, "\n" if $self->{options}->{VERBOSE}; |
232 |
|
|
if ($self->{options}->{LOG}) { |
233 |
|
|
$self->appendLog($msg); |
234 |
root |
1.1 |
} |
235 |
|
|
|
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
239 |
|
|
# processing mail |
240 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
241 |
|
|
|
242 |
|
|
sub compareTarget { |
243 |
root |
1.4 |
my $self = shift; |
244 |
root |
1.1 |
my $pattern = shift; |
245 |
|
|
my $ok = 0; |
246 |
joko |
1.2 |
$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 |
root |
1.1 |
return $ok; |
250 |
|
|
} |
251 |
|
|
|
252 |
root |
1.5 |
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 |
root |
1.1 |
sub jaccept { |
267 |
root |
1.4 |
my $self = shift; |
268 |
root |
1.1 |
my $deliver_to = shift; |
269 |
|
|
|
270 |
root |
1.4 |
$self->report("ACCEPT: $deliver_to"); |
271 |
root |
1.1 |
|
272 |
|
|
# check deliver_to path |
273 |
|
|
if (! -e $deliver_to) { |
274 |
root |
1.5 |
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 |
root |
1.1 |
} |
293 |
|
|
|
294 |
root |
1.5 |
return $self->{incoming}->accept($deliver_to); |
295 |
root |
1.1 |
} |
296 |
|
|
|
297 |
root |
1.5 |
|
298 |
|
|
sub accept { |
299 |
root |
1.4 |
my $self = shift; |
300 |
root |
1.5 |
return $self->jaccept(@_); |
301 |
root |
1.1 |
} |
302 |
|
|
|
303 |
root |
1.5 |
sub copy { |
304 |
root |
1.4 |
my $self = shift; |
305 |
root |
1.5 |
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 |
root |
1.1 |
} |
313 |
|
|
|
314 |
root |
1.5 |
sub ignore { |
315 |
|
|
my $self = shift; |
316 |
|
|
$self->report("IGNORE"); |
317 |
|
|
return $self->{incoming}->ignore; |
318 |
|
|
} |
319 |
joko |
1.2 |
|
320 |
|
|
1; |