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.5 |
# $Id: Dispatch.pm,v 1.4 2003/01/22 17:58:21 root Exp $ |
9 |
root |
1.1 |
# |
10 |
|
|
# ============================================================ |
11 |
joko |
1.2 |
# $Log: Dispatch.pm,v $ |
12 |
root |
1.5 |
# Revision 1.4 2003/01/22 17:58:21 root |
13 |
|
|
# + fixed and enhanced many things |
14 |
|
|
# - refactored code to Data::Code |
15 |
|
|
# |
16 |
root |
1.4 |
# Revision 1.3 2003/01/22 07:55:43 joko |
17 |
|
|
# + replaced '$HOME' with '$self->{settings}->{HOME}' |
18 |
|
|
# |
19 |
joko |
1.3 |
# Revision 1.2 2003/01/22 07:54:24 joko |
20 |
|
|
# + replaced global variables with class-variables |
21 |
|
|
# |
22 |
joko |
1.2 |
# Revision 1.1 2003/01/22 07:45:20 root |
23 |
|
|
# + initial check-in - refactored from 'dispatchmail' |
24 |
|
|
# |
25 |
root |
1.1 |
# 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 |
root |
1.4 |
use base qw( |
41 |
|
|
DesignPattern::Object |
42 |
root |
1.5 |
DesignPattern::Bridge |
43 |
root |
1.4 |
); |
44 |
|
|
# DesignPattern::Object::Logger |
45 |
root |
1.1 |
|
46 |
|
|
|
47 |
|
|
use Mail::Audit; |
48 |
|
|
use Data::Dumper; |
49 |
|
|
|
50 |
|
|
|
51 |
root |
1.4 |
use Data::Code::Symbol qw( export_symbols ); |
52 |
|
|
use Data::Storage::Handler::File qw( a2f ); |
53 |
root |
1.5 |
use org::netfrag::shortcuts qw( now get_chomped run_cmd ); |
54 |
root |
1.1 |
|
55 |
|
|
|
56 |
|
|
sub _init { |
57 |
|
|
my $self = shift; |
58 |
|
|
$self->_init_options(); |
59 |
joko |
1.2 |
$self->_override_options(); |
60 |
root |
1.1 |
$self->_init_settings(); |
61 |
|
|
$self->_override_settings(); |
62 |
root |
1.4 |
#$self->_run(); |
63 |
root |
1.1 |
} |
64 |
|
|
|
65 |
|
|
sub _init_options { |
66 |
|
|
my $self = shift; |
67 |
root |
1.5 |
foreach (qw( user base rules LOG VERBOSE TRACE mode newsgroup )) { |
68 |
joko |
1.2 |
$self->{options}->{$_} = $self->{$_}; |
69 |
|
|
} |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
sub _override_options { |
73 |
|
|
my $self = shift; |
74 |
root |
1.1 |
$self->{options}->{base} ||= $ENV{HOME}; |
75 |
|
|
$self->{options}->{base} ||= $ENV{PWD}; |
76 |
|
|
} |
77 |
|
|
|
78 |
joko |
1.2 |
sub _init_settings { |
79 |
root |
1.1 |
my $self = shift; |
80 |
root |
1.4 |
$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 |
joko |
1.3 |
$self->{settings}->{MAILDIR} = "$self->{settings}->{HOME}/Mail"; |
93 |
root |
1.5 |
#$self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc.pm"; |
94 |
|
|
$self->{settings}->{RULESFILE} = "$self->{settings}->{HOME}/.dispatchmailrc"; |
95 |
joko |
1.3 |
#$self->{settings}->{LOCKFILE} = "$self->{settings}->{HOME}/.procmail.lockfile"; |
96 |
|
|
$self->{settings}->{LOCKFILE} = "$self->{settings}->{HOME}/.dispatchmail.lockfile"; |
97 |
joko |
1.2 |
$self->{settings}->{LOGFILE} = "$self->{settings}->{MAILDIR}/.dispatchmail.log"; |
98 |
root |
1.4 |
$self->{settings}->{ERRLOG} = ".dispatchmail-errors.log"; |
99 |
joko |
1.2 |
$self->{settings}->{DEFAULT} = "$self->{settings}->{MAILDIR}/Inbox"; |
100 |
root |
1.1 |
} |
101 |
|
|
|
102 |
|
|
sub _override_settings { |
103 |
|
|
my $self = shift; |
104 |
joko |
1.2 |
# override $self->{settings}->{RULESFILE} if given as option on the command line |
105 |
root |
1.1 |
$self->{settings}->{RULESFILE} = |
106 |
|
|
$self->{options}->{rules} if $self->{options}->{rules}; |
107 |
|
|
# change logfile |
108 |
|
|
$self->{settings}->{LOGFILE} = |
109 |
root |
1.4 |
$self->{settings}->{ERRLOG} if (! -e $self->{settings}->{MAILDIR}); |
110 |
root |
1.1 |
} |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
|
114 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
115 |
|
|
# main |
116 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
117 |
|
|
|
118 |
root |
1.5 |
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 |
root |
1.4 |
|
130 |
root |
1.5 |
} |
131 |
root |
1.4 |
|
132 |
root |
1.5 |
sub _recieve { |
133 |
root |
1.4 |
|
134 |
joko |
1.2 |
my $self = shift; |
135 |
|
|
|
136 |
root |
1.1 |
# 0.a. pre flight tracing |
137 |
|
|
my $now = now(); |
138 |
root |
1.4 |
$self->report("$now - $0 running for user '$self->{settings}->{USER}'."); |
139 |
|
|
$self->traceEntry() if $self->{options}->{TRACE}; |
140 |
root |
1.1 |
|
141 |
|
|
# 0.b. pre flight checks |
142 |
|
|
|
143 |
joko |
1.3 |
# TODO: check if $self->{settings}->{HOME} is empty |
144 |
root |
1.1 |
|
145 |
joko |
1.3 |
# check if $self->{settings}->{HOME} exists |
146 |
joko |
1.2 |
if (! -e $self->{settings}->{MAILDIR}) { |
147 |
root |
1.5 |
my $msg = "ERROR: Delivery failed, base directory '$self->{settings}->{MAILDIR}' does not exist."; |
148 |
root |
1.4 |
$self->report($msg); |
149 |
|
|
$self->forward_delivery(); |
150 |
root |
1.1 |
} |
151 |
|
|
|
152 |
|
|
# 1. include rules or fallback |
153 |
joko |
1.2 |
# check if $self->{settings}->{RULESFILE} exists |
154 |
|
|
if (-f $self->{settings}->{RULESFILE}) { |
155 |
root |
1.5 |
$self->report("RULES: Loading from \"$self->{settings}->{RULESFILE}\"."); |
156 |
joko |
1.2 |
require $self->{settings}->{RULESFILE}; |
157 |
root |
1.1 |
} else { |
158 |
joko |
1.2 |
#die("$self->{settings}->{RULESFILE} doesn't exist"); |
159 |
root |
1.4 |
$self->report("Configured rulesfile \"$self->{settings}->{RULESFILE}\" doesn't exist."); |
160 |
|
|
$self->forward_delivery(); |
161 |
root |
1.1 |
} |
162 |
|
|
|
163 |
|
|
# 2. export required stuff to rules namespace |
164 |
root |
1.5 |
my @symbols = qw( jaccept report compareTarget accept copy ignore ); |
165 |
root |
1.4 |
export_symbols(\@symbols, 'rules'); |
166 |
root |
1.1 |
|
167 |
|
|
# 3. run dispatcher |
168 |
root |
1.5 |
$self->report("RULES: Running Perl sub \"rules::dispatch\"."); |
169 |
root |
1.4 |
rules::dispatch($self); |
170 |
root |
1.1 |
|
171 |
|
|
# 4. dispatcher didn't do anything |
172 |
root |
1.4 |
$self->report("dispatcher could not apply any filter, using default delivery"); |
173 |
root |
1.1 |
|
174 |
|
|
# the default-handler: simply accept all mails and route them to "/var/spool/mail" |
175 |
joko |
1.2 |
# $self->{incoming}->accept(); |
176 |
root |
1.1 |
|
177 |
|
|
# if you want to reject all mails coming through to here, do a ... |
178 |
joko |
1.2 |
# $self->{incoming}->reject; |
179 |
root |
1.1 |
|
180 |
|
|
# catch all mails and route them to a "DEFAULT"-inbox |
181 |
root |
1.4 |
$self->jaccept($self->{settings}->{DEFAULT}); |
182 |
root |
1.1 |
|
183 |
root |
1.4 |
} |
184 |
root |
1.1 |
|
185 |
root |
1.5 |
|
186 |
|
|
sub _mail2news { |
187 |
root |
1.4 |
my $self = shift; |
188 |
root |
1.5 |
$self->report("MAIL2NEWS: $self->{options}->{newsgroup}"); |
189 |
|
|
my $plugin = 'Newsgate'; |
190 |
|
|
$self->load($plugin); |
191 |
|
|
$self->$plugin($self->{options}->{newsgroup}); |
192 |
root |
1.4 |
} |
193 |
root |
1.1 |
|
194 |
|
|
|
195 |
root |
1.5 |
|
196 |
root |
1.1 |
# - - - - - - - - - - - - - - - - - - - - |
197 |
|
|
# tracing & reporting |
198 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
199 |
root |
1.5 |
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 |
root |
1.4 |
sub appendLog { |
209 |
|
|
my $self = shift; |
210 |
|
|
my $msg = shift; |
211 |
|
|
a2f($self->{settings}->{LOGFILE}, $msg); |
212 |
root |
1.1 |
} |
213 |
|
|
|
214 |
|
|
sub report { |
215 |
root |
1.4 |
my $self = shift; |
216 |
root |
1.1 |
my $msg = shift; |
217 |
|
|
# TODO: tracing, debugging |
218 |
|
|
|
219 |
root |
1.4 |
#print STDERR $msg, "\n" if $self->{options}->{VERBOSE}; |
220 |
|
|
if ($self->{options}->{LOG}) { |
221 |
|
|
$self->appendLog($msg); |
222 |
root |
1.1 |
} |
223 |
|
|
|
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
227 |
|
|
# processing mail |
228 |
|
|
# - - - - - - - - - - - - - - - - - - - - |
229 |
|
|
|
230 |
|
|
sub compareTarget { |
231 |
root |
1.4 |
my $self = shift; |
232 |
root |
1.1 |
my $pattern = shift; |
233 |
|
|
my $ok = 0; |
234 |
joko |
1.2 |
$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 |
root |
1.1 |
return $ok; |
238 |
|
|
} |
239 |
|
|
|
240 |
root |
1.5 |
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 |
root |
1.1 |
sub jaccept { |
255 |
root |
1.4 |
my $self = shift; |
256 |
root |
1.1 |
my $deliver_to = shift; |
257 |
|
|
|
258 |
root |
1.4 |
$self->report("ACCEPT: $deliver_to"); |
259 |
root |
1.1 |
|
260 |
|
|
# check deliver_to path |
261 |
|
|
if (! -e $deliver_to) { |
262 |
root |
1.5 |
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 |
root |
1.1 |
} |
281 |
|
|
|
282 |
root |
1.5 |
return $self->{incoming}->accept($deliver_to); |
283 |
root |
1.1 |
} |
284 |
|
|
|
285 |
root |
1.5 |
|
286 |
|
|
sub accept { |
287 |
root |
1.4 |
my $self = shift; |
288 |
root |
1.5 |
return $self->jaccept(@_); |
289 |
root |
1.1 |
} |
290 |
|
|
|
291 |
root |
1.5 |
sub copy { |
292 |
root |
1.4 |
my $self = shift; |
293 |
root |
1.5 |
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 |
root |
1.1 |
} |
301 |
|
|
|
302 |
root |
1.5 |
sub ignore { |
303 |
|
|
my $self = shift; |
304 |
|
|
$self->report("IGNORE"); |
305 |
|
|
return $self->{incoming}->ignore; |
306 |
|
|
} |
307 |
joko |
1.2 |
|
308 |
|
|
1; |