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