--- Actions.pm-orig 2009-07-06 16:18:21.000000000 +0200 +++ Actions.pm 2009-07-06 16:18:31.000000000 +0200 @@ -119,8 +119,23 @@ # # usage: &send_email(%options); # +# /ol: new options: +# RATELIMIT #seconds +# RATETAG tag to key ratelimit on, defaults to ADDRESSES +# RATEFLUSH ignore this message, only try to flush +# +# The difference to threshold is that multiple messages are buffered into one +# email message. No message is discarded. +# ################################################################ +my %ratelimit = (); +# +# key = RATETAG, +# value = hash with +# nextmail ctime for next mail +# message collected messages + sub send_email { my $login = (getpwuid($<))[0]; my %args = ( @@ -135,6 +150,25 @@ and $args{'THRESHOLDING'} eq 'on' and not &Swatch::Threshold::threshold(%args)); + if ($args{'RATELIMIT'}) { + my $tag = ($args{'RATETAG'}) ? $args{'RATETAG'} : $args{'ADDRESSES'}; + + $ratelimit{$tag} = { nextmail => 0, message => '' } unless ($ratelimit{$tag}); # initialize. + + return if ($args{'RATEFLUSH'} and ($ratelimit{$tag}->{'message'} eq "")); # nothing to flush. + + if (time < $ratelimit{$tag}->{'nextmail'}) { # ok, we just remember this message and don't send mail + $ratelimit{$tag}->{'message'} .= $args{'MESSAGE'} . "\n" unless ($args{'RATEFLUSH'}); + return; + } else { # include stored messages + $args{'MESSAGE'} = ($args{'RATEFLUSH'}) ? # just flushing? + $ratelimit{$tag}->{'message'} : + $args{'MESSAGE'} . "\n" . $ratelimit{$tag}->{'message'}; + $ratelimit{$tag}->{'nextmail'} = time + $args{'RATELIMIT'}; + $ratelimit{$tag}->{'message'} = ""; + } + } + if (! $args{'MAILER'} ) { foreach my $mailer (qw(/usr/lib/sendmail /usr/sbin/sendmail)) { $args{'MAILER'} = $mailer if ( -x $mailer );