9 |
# |
# |
10 |
# ============================================================ |
# ============================================================ |
11 |
# $Log$ |
# $Log$ |
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 |
# Revision 1.2 2003/01/22 07:54:24 joko |
23 |
# + replaced global variables with class-variables |
# + replaced global variables with class-variables |
24 |
# |
# |
40 |
use strict; |
use strict; |
41 |
# don't use warnings; |
# don't use warnings; |
42 |
|
|
43 |
use base qw( DesignPattern::Object ); |
use base qw( |
44 |
|
DesignPattern::Object |
45 |
|
DesignPattern::Bridge |
46 |
|
); |
47 |
|
# DesignPattern::Object::Logger |
48 |
|
|
49 |
|
|
50 |
use Mail::Audit; |
use Mail::Audit; |
51 |
use Data::Dumper; |
use Data::Dumper; |
52 |
|
|
53 |
|
|
54 |
use org::netfrag::shortcuts qw( now ); |
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 ); |
|
my $LOG = 1; # writes reports to logfile |
|
|
my $self->{options}->{VERBOSE} = 1; # writes reports to STDOUT |
|
|
my $self->{options}->{TRACE} = 1; # writes contents of messages to logfile |
|
57 |
|
|
58 |
|
|
59 |
sub _init { |
sub _init { |
62 |
$self->_override_options(); |
$self->_override_options(); |
63 |
$self->_init_settings(); |
$self->_init_settings(); |
64 |
$self->_override_settings(); |
$self->_override_settings(); |
65 |
$self->_run(); |
#$self->_run(); |
66 |
} |
} |
67 |
|
|
68 |
sub _init_options { |
sub _init_options { |
69 |
my $self = shift; |
my $self = shift; |
70 |
foreach (qw( base rules LOG VERBOSE TRACE )) { |
foreach (qw( user base rules LOG VERBOSE TRACE mode newsgroup )) { |
71 |
$self->{options}->{$_} = $self->{$_}; |
$self->{options}->{$_} = $self->{$_}; |
72 |
} |
} |
73 |
} |
} |
78 |
$self->{options}->{base} ||= $ENV{PWD}; |
$self->{options}->{base} ||= $ENV{PWD}; |
79 |
} |
} |
80 |
|
|
|
# - - - - - - - - - - - - - - - - - - - - |
|
|
# |
|
|
# targets |
|
|
# |
|
|
# declare and initialize some variables |
|
|
# these are mostly base paths |
|
|
# mail should be delivered to |
|
|
# |
|
|
# - - - - - - - - - - - - - - - - - - - - |
|
81 |
sub _init_settings { |
sub _init_settings { |
82 |
my $self = shift; |
my $self = shift; |
83 |
$self->{settings}->{USER} = $ENV{USER}; |
$self->{settings}->{USER} = $self->{options}->{user} if $self->{options}->{user}; |
84 |
$self->{settings}->{HOME} = $self->{options}->{base}; |
$self->{settings}->{USER} ||= ''; |
85 |
$self->{settings}->{MAILDIR} = "$HOME/Mail"; |
$self->{settings}->{USER} ||= $ENV{USER}; |
86 |
$self->{settings}->{RULESFILE} = "$HOME/.dispatchmailrc.pm"; |
|
87 |
#$self->{settings}->{LOCKFILE} = "$HOME/.procmail.lockfile"; |
$self->{settings}->{HOME} = '../var/spool/mail/' . $self->{settings}->{USER}; |
88 |
$self->{settings}->{LOCKFILE} = "$HOME/.dispatchmail.lockfile"; |
$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"; |
$self->{settings}->{LOGFILE} = "$self->{settings}->{MAILDIR}/.dispatchmail.log"; |
101 |
|
$self->{settings}->{ERRLOG} = ".dispatchmail-errors.log"; |
102 |
$self->{settings}->{DEFAULT} = "$self->{settings}->{MAILDIR}/Inbox"; |
$self->{settings}->{DEFAULT} = "$self->{settings}->{MAILDIR}/Inbox"; |
103 |
} |
} |
104 |
|
|
109 |
$self->{options}->{rules} if $self->{options}->{rules}; |
$self->{options}->{rules} if $self->{options}->{rules}; |
110 |
# change logfile |
# change logfile |
111 |
$self->{settings}->{LOGFILE} = |
$self->{settings}->{LOGFILE} = |
112 |
"recievemail-emerg.log" if (! -e $self->{settings}->{MAILDIR}); |
$self->{settings}->{ERRLOG} if (! -e $self->{settings}->{MAILDIR}); |
|
|
|
|
$self->{settings}->{USER} ||= ''; |
|
113 |
} |
} |
114 |
|
|
115 |
|
|
118 |
# main |
# main |
119 |
# - - - - - - - - - - - - - - - - - - - - |
# - - - - - - - - - - - - - - - - - - - - |
120 |
|
|
121 |
sub _run { |
sub run { |
122 |
my $self = shift; |
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 |
# "jump" into processing of new incoming mail and get a "handler" to this mail |
128 |
$self->{incoming} = Mail::Audit->new(); |
$self->{incoming} = Mail::Audit->new(); |
129 |
|
|
130 |
} |
my $result = $self->$call(@_); |
131 |
|
return $result; |
132 |
|
|
133 |
sub traceEntry { |
} |
|
my $self = shift; |
|
|
s2f('-' x 40 . ' TRACE ' . '-' x 10); |
|
|
s2f("From: " . gchomp($self->{incoming}->from)); |
|
|
s2f("To: " . gchomp($self->{incoming}->to)); |
|
|
s2f("Subject: " . gchomp($self->{incoming}->subject)); |
|
|
s2f('-' x 40 . ' TRACE ' . '-' x 10); |
|
|
} |
|
134 |
|
|
135 |
|
sub _recieve { |
136 |
|
|
137 |
|
my $self = shift; |
138 |
|
|
139 |
# 0.a. pre flight tracing |
# 0.a. pre flight tracing |
140 |
my $now = now(); |
my $now = now(); |
141 |
report("$0 running at $now for user '$USER'."); |
$self->report("$now - $0 running for user '$self->{settings}->{USER}'."); |
142 |
traceEntry() if $self->{options}->{TRACE}; |
$self->traceEntry() if $self->{options}->{TRACE}; |
143 |
|
|
144 |
# 0.b. pre flight checks |
# 0.b. pre flight checks |
145 |
|
|
146 |
# TODO: check if $HOME is empty |
# TODO: check if $self->{settings}->{HOME} is empty |
147 |
|
|
148 |
# check if $HOME exists |
# check if $self->{settings}->{HOME} exists |
149 |
if (! -e $self->{settings}->{MAILDIR}) { |
if (! -e $self->{settings}->{MAILDIR}) { |
150 |
my $msg = "delivery failed, base directory $self->{settings}->{MAILDIR} does not exist"; |
my $msg = "ERROR: Delivery failed, base directory '$self->{settings}->{MAILDIR}' does not exist."; |
151 |
report($msg); |
$self->report($msg); |
152 |
forward_delivery(); |
$self->forward_delivery(); |
153 |
} |
} |
154 |
|
|
155 |
# 1. include rules or fallback |
# 1. include rules or fallback |
156 |
# check if $self->{settings}->{RULESFILE} exists |
# check if $self->{settings}->{RULESFILE} exists |
157 |
if (-f $self->{settings}->{RULESFILE}) { |
if (-f $self->{settings}->{RULESFILE}) { |
158 |
report("Loading rules from \"$self->{settings}->{RULESFILE}\"."); |
$self->report("RULES: Loading from \"$self->{settings}->{RULESFILE}\"."); |
159 |
require $self->{settings}->{RULESFILE}; |
require $self->{settings}->{RULESFILE}; |
160 |
} else { |
} else { |
161 |
#die("$self->{settings}->{RULESFILE} doesn't exist"); |
#die("$self->{settings}->{RULESFILE} doesn't exist"); |
162 |
report("Configured rulesfile \"$self->{settings}->{RULESFILE}\" doesn't exist."); |
$self->report("Configured rulesfile \"$self->{settings}->{RULESFILE}\" doesn't exist."); |
163 |
forward_delivery(); |
$self->forward_delivery(); |
164 |
} |
} |
165 |
|
|
166 |
# 2. export required stuff to rules namespace |
# 2. export required stuff to rules namespace |
167 |
export_symbols(); |
my @symbols = qw( jaccept report compareTarget accept copy ignore ); |
168 |
|
export_symbols(\@symbols, 'rules'); |
169 |
|
|
170 |
# 3. run dispatcher |
# 3. run dispatcher |
171 |
report("Running \"rules::dispatch\"."); |
$self->report("RULES: Running Perl sub \"rules::dispatch\"."); |
172 |
rules::dispatch(); |
rules::dispatch($self); |
173 |
|
|
174 |
# 4. dispatcher didn't do anything |
# 4. dispatcher didn't do anything |
175 |
report("dispatcher could not apply any filter, using default delivery"); |
$self->report("dispatcher could not apply any filter, using default delivery"); |
176 |
|
|
177 |
# the default-handler: simply accept all mails and route them to "/var/spool/mail" |
# the default-handler: simply accept all mails and route them to "/var/spool/mail" |
178 |
# $self->{incoming}->accept(); |
# $self->{incoming}->accept(); |
181 |
# $self->{incoming}->reject; |
# $self->{incoming}->reject; |
182 |
|
|
183 |
# catch all mails and route them to a "DEFAULT"-inbox |
# catch all mails and route them to a "DEFAULT"-inbox |
184 |
jaccept($self->{settings}->{DEFAULT}); |
$self->jaccept($self->{settings}->{DEFAULT}); |
185 |
|
|
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
|
sub _mail2news { |
190 |
|
my $self = shift; |
191 |
|
$self->report("MAIL2NEWS: $self->{options}->{newsgroup}"); |
192 |
|
my $plugin = 'Newsgate'; |
193 |
|
$self->load($plugin); |
194 |
|
$self->$plugin($self->{options}->{newsgroup}); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
|
199 |
# - - - - - - - - - - - - - - - - - - - - |
# - - - - - - - - - - - - - - - - - - - - |
200 |
# tracing & reporting |
# tracing & reporting |
201 |
# - - - - - - - - - - - - - - - - - - - - |
# - - - - - - - - - - - - - - - - - - - - |
202 |
sub s2f { |
sub traceEntry { |
203 |
my $str = shift; |
my $self = shift; |
204 |
open(FH, '>>' . $self->{settings}->{LOGFILE}); |
$self->appendLog('-' x 40 . ' TRACE ' . '-' x 10); |
205 |
print FH $str, "\n"; |
$self->appendLog("From: " . get_chomped($self->{incoming}->from)); |
206 |
close(FH); |
$self->appendLog("To: " . get_chomped($self->{incoming}->to)); |
207 |
|
$self->appendLog("Subject: " . get_chomped($self->{incoming}->subject)); |
208 |
|
$self->appendLog('-' x 40 . ' TRACE ' . '-' x 10); |
209 |
|
} |
210 |
|
|
211 |
|
sub appendLog { |
212 |
|
my $self = shift; |
213 |
|
my $msg = shift; |
214 |
|
a2f($self->{settings}->{LOGFILE}, $msg); |
215 |
} |
} |
216 |
|
|
217 |
sub report { |
sub report { |
218 |
|
my $self = shift; |
219 |
my $msg = shift; |
my $msg = shift; |
220 |
# TODO: tracing, debugging |
# TODO: tracing, debugging |
221 |
|
|
222 |
print $msg, "\n" if $self->{options}->{VERBOSE}; |
#print STDERR $msg, "\n" if $self->{options}->{VERBOSE}; |
223 |
if ($LOG) { |
if ($self->{options}->{LOG}) { |
224 |
s2f($msg); |
$self->appendLog($msg); |
225 |
} |
} |
226 |
|
|
227 |
} |
} |
231 |
# - - - - - - - - - - - - - - - - - - - - |
# - - - - - - - - - - - - - - - - - - - - |
232 |
|
|
233 |
sub compareTarget { |
sub compareTarget { |
234 |
|
my $self = shift; |
235 |
my $pattern = shift; |
my $pattern = shift; |
236 |
my $ok = 0; |
my $ok = 0; |
237 |
$ok = 1 if ($self->{incoming}->to =~ m/$pattern/); |
$ok = 1 if ($self->{incoming}->to =~ m/$pattern/); |
240 |
return $ok; |
return $ok; |
241 |
} |
} |
242 |
|
|
243 |
|
sub accept_spool { |
244 |
|
my $self = shift; |
245 |
|
my $path = "/var/spool/mail/$self->{settings}->{USER}"; |
246 |
|
$self->report("defaulting to spool delivery ($path)"); |
247 |
|
$self->{incoming}->accept($path); |
248 |
|
} |
249 |
|
|
250 |
|
sub forward_delivery { |
251 |
|
my $self = shift; |
252 |
|
$self->report("Forwarding delivery to next handler in queue (probably /var/spool/mail)."); |
253 |
|
return $self->{incoming}->accept; |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
sub jaccept { |
sub jaccept { |
258 |
|
my $self = shift; |
259 |
my $deliver_to = shift; |
my $deliver_to = shift; |
260 |
|
|
261 |
report("ACCEPT: $deliver_to"); |
$self->report("ACCEPT: $deliver_to"); |
262 |
|
|
263 |
# check deliver_to path |
# check deliver_to path |
264 |
if (! -e $deliver_to) { |
if (! -e $deliver_to) { |
265 |
report("deliver_to path \"$deliver_to\" doesn't exist"); |
my $good = 0; |
266 |
forward_delivery(); |
$self->report("ERROR: TARGET Path/File doesn't exist."); |
267 |
return; |
if ($self->{settings}->{AUTOCREATE_FOLDERS}) { |
268 |
|
my $cmd = "touch $deliver_to"; |
269 |
|
$self->report("TARGET: AUTOCREATE_FOLDERS is enabled: touching TARGET."); |
270 |
|
#if (mkdir $deliver_to) { |
271 |
|
run_cmd($cmd); |
272 |
|
# re-test TARGET - for existance now |
273 |
|
if (-e $deliver_to) { |
274 |
|
$good = 1; |
275 |
|
} else { |
276 |
|
$self->report("ERROR: TARGET creation failed (command was: '$cmd')."); |
277 |
|
} |
278 |
|
} |
279 |
|
if (!$good) { |
280 |
|
$self->forward_delivery(); |
281 |
|
return; |
282 |
|
} |
283 |
} |
} |
284 |
|
|
285 |
$self->{incoming}->accept($deliver_to); |
return $self->{incoming}->accept($deliver_to); |
286 |
} |
} |
287 |
|
|
|
sub accept_spool { |
|
|
my $path = "/var/spool/mail/$USER"; |
|
|
report("defaulting to spool delivery ($path)"); |
|
|
$self->{incoming}->accept($path); |
|
|
} |
|
288 |
|
|
289 |
sub forward_delivery { |
sub accept { |
290 |
report("Forwarding delivery to next handler in queue (probably /var/spool/mail)."); |
my $self = shift; |
291 |
$self->{incoming}->accept; |
return $self->jaccept(@_); |
292 |
} |
} |
293 |
|
|
294 |
|
sub copy { |
295 |
|
my $self = shift; |
296 |
|
my $plugin = shift; |
297 |
|
my $deliver_to = shift; |
298 |
|
|
299 |
|
$self->report("COPY: $plugin: $deliver_to"); |
300 |
|
|
301 |
# - - - - - - - - - - - - - - - - - - - - |
$self->load($plugin); |
302 |
# helper functions |
return $self->$plugin($deliver_to); |
|
# - - - - - - - - - - - - - - - - - - - - |
|
|
|
|
|
sub get_coderef { |
|
|
my $codepack = shift; |
|
|
my $method = shift; |
|
|
$codepack || return '[error]'; |
|
|
$method ||= ''; |
|
|
$method && ($codepack .= '::'); |
|
|
return eval '\&' . $codepack . $method . ';'; |
|
|
} |
|
|
|
|
|
sub export_symbols { |
|
|
# my $callpack = 'rules'; |
|
|
# my @EXPORT = qw( incoming subject MAILDIR jaccept ); |
|
|
# foreach my $sym (@EXPORT) { |
|
|
no strict 'refs'; |
|
|
# *{"${callpack}::$sym"} = get_coderef('main', $sym); |
|
|
# } |
|
|
{ |
|
|
no strict 'refs'; |
|
|
*{"rules::jaccept"} = get_coderef('main', 'jaccept'); |
|
|
*{"rules::report"} = get_coderef('main', 'report'); |
|
|
*{"rules::compareTarget"} = get_coderef('main', 'compareTarget'); |
|
303 |
} |
} |
|
$rules::MAILDIR = $self->{settings}->{MAILDIR}; |
|
|
$rules::incoming = $self->{incoming}; |
|
|
} |
|
304 |
|
|
305 |
sub gchomp { |
sub ignore { |
306 |
my $str = shift; |
my $self = shift; |
307 |
chomp($str); |
$self->report("IGNORE"); |
308 |
return $str; |
return $self->{incoming}->ignore; |
309 |
} |
} |
310 |
|
|
311 |
1; |
1; |