From 0718a13ba8225c9d180b3207470876d048184310 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Sun, 12 Jun 2022 15:11:28 -0400 Subject: Allow flagging infinite sends by setting remaing count to -1. --- bin/spamcat | 4 +++- lib/SpamCat.pm | 20 +++++++++++--------- t/fixtures/always-allowed | 14 ++++++++++++++ t/fixtures/always-allowed.expected | 14 ++++++++++++++ t/lib.t | 23 ++++++++++++++--------- 5 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 t/fixtures/always-allowed create mode 100644 t/fixtures/always-allowed.expected diff --git a/bin/spamcat b/bin/spamcat index 25f04d2..60fdf46 100755 --- a/bin/spamcat +++ b/bin/spamcat @@ -30,7 +30,9 @@ Show database row for C. =item set C C -Set C's remaining message count to C. +Set C's remaining message count to C. If C is +less than 0, then mail from C will not be limited and always +be sent unmodified. =item dumpconfig diff --git a/lib/SpamCat.pm b/lib/SpamCat.pm index 0f3c8b8..7dff474 100644 --- a/lib/SpamCat.pm +++ b/lib/SpamCat.pm @@ -47,15 +47,17 @@ sub deliver { if (defined $count) { return if $count == 0; - my $count_str = '[' . $count . '/' . $self->{default_count} . ']'; - my $new_subject = $email->header('Subject'); - if ($new_subject) { - $new_subject .= ' - ' . $count_str; - } else { - $new_subject = $count_str; + if ($count > 0) { + my $count_str = '[' . $count . '/' . $self->{default_count} . ']'; + my $new_subject = $email->header('Subject'); + if ($new_subject) { + $new_subject .= ' - ' . $count_str; + } else { + $new_subject = $count_str; + } + $email->header_set('Subject' => $new_subject); + $email->header_set('X-SpamCat-Remaining' => $count); } - $email->header_set('Subject' => $new_subject); - $email->header_set('X-SpamCat-Remaining' => $count); } my $deliverfh = IO::File->new("| " . $self->{deliver}) || @@ -163,7 +165,7 @@ sub decrement_count_t { $count = $self->{default_count}; $q = 'INSERT INTO emails (count, sender) VALUES (?, ?)'; } else { - $count = $count <= 0 ? '0' : $count - 1; + $count = $count <= 0 ? $count : $count - 1; $q = "UPDATE emails SET count = ?, modified = CURRENT_TIMESTAMP WHERE sender = ?"; } diff --git a/t/fixtures/always-allowed b/t/fixtures/always-allowed new file mode 100644 index 0000000..b383d1a --- /dev/null +++ b/t/fixtures/always-allowed @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: always-allowed@spamcat.example.com +Subject: test +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/always-allowed.expected b/t/fixtures/always-allowed.expected new file mode 100644 index 0000000..b383d1a --- /dev/null +++ b/t/fixtures/always-allowed.expected @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: always-allowed@spamcat.example.com +Subject: test +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/lib.t b/t/lib.t index 395488d..b814c78 100644 --- a/t/lib.t +++ b/t/lib.t @@ -1,6 +1,6 @@ # -*- Mode: cperl -*- -use Test::More tests => 42; +use Test::More tests => 46; use strict; use warnings; @@ -66,18 +66,23 @@ test_file('wrongdomain', 1); test_file('nosubj', 1); test_file('bar', 0); +$sch->set_count('always-allowed', -1); +test_file('always-allowed', 1); + ok(SpamCat->can('get_table')); my @rows = @{$sch->get_table()}; -is($#rows, 3); +is($#rows, 4); @rows = sort { $a->{sender} cmp $b->{sender} } @rows; -is($rows[0]->{sender}, 'bar'); -is($rows[0]->{count}, 0); -is($rows[1]->{sender}, 'foo'); -is($rows[1]->{count}, 16); -is($rows[2]->{sender}, 'name1'); -is($rows[2]->{count}, 20); -is($rows[3]->{sender}, 'nosubj'); +is($rows[0]->{sender}, 'always-allowed'); +is($rows[0]->{count}, -1); +is($rows[1]->{sender}, 'bar'); +is($rows[1]->{count}, 0); +is($rows[2]->{sender}, 'foo'); +is($rows[2]->{count}, 16); +is($rows[3]->{sender}, 'name1'); is($rows[3]->{count}, 20); +is($rows[4]->{sender}, 'nosubj'); +is($rows[4]->{count}, 20); sub test_file { my ($filen, $should_exist) = @_; -- cgit v1.2.3