aboutsummaryrefslogtreecommitdiffstats
path: root/bin/spamcat
blob: 25f04d29f686844055c6ff1d09157738c1832938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env perl

=head1 NAME

spamcat - Filter spam by number of messages sent.

=head1 SYNOPSIS

spamcat [--help] [-c config-file] [dumpconfig|dump]

=head1 DESCRIPTION

B<spamcat> allows you to have disposable email addresses.

=head1 OPTIONS

=over 8

=item --help

Print a brief help message and exit.

=item -c C<file>

Load configuration from C<file>

=item get C<sender>

Show database row for C<sender>.

=item set C<sender> C<count>

Set C<sender>'s remaining message count to C<count>.

=item dumpconfig

Dump the current configuration.

=item dump

Dumps the spamcat database in tab-delimited format.

=back

=head1 AUTHOR

Brian Cully <bjc@kublai.com>

=cut

use SpamCat;
use SpamCat::Conf;

use Getopt::Long;
use Pod::Usage;

use Data::Dumper;

use strict;
use warnings;

my $DEFAULT_CONFIGFILE = '/usr/local/etc/spamcat.conf';

my ($help, $configfile);
GetOptions('help|h'     => \$help,
	   'c=s'        => \$configfile) || pod2usage(2);
pod2usage(1) if $help;

$configfile = $configfile || $DEFAULT_CONFIGFILE;
my %conf = SpamCat::Conf::read($configfile);

my $sch = SpamCat->new(%conf) ||
    die "Couldn't start spamcat: $!\n";
if ($#ARGV >= 0) {
    my $cmd = shift @ARGV;
    if ($cmd eq 'dump') {
	my @keys = qw(sender count created modified);
	print join("\t", @keys) . "\n";
	foreach my $row (@{$sch->get_table}) {
	    my @vals;
	    foreach my $k (@keys) {
		push @vals, $row->{$k};
	    }
	    print join("\t", @vals) . "\n";
	}
    } elsif ($cmd eq 'dumpconfig') {
	foreach my $k (keys %conf) {
	    my $v = $conf{$k};
	    if ($k eq 'domains') {
		$v = join ', ', @{$v};
	    }
	    print uc($k) . " = " . $v . "\n";
	}
    } elsif ($cmd eq 'get') {
	my $sender = shift @ARGV;
	pod2usage(1) unless $sender;
	my $count = $sch->get_count($sender) || $conf{default_count};
	print "$sender has $count messages remaining.\n";
    } elsif ($cmd eq 'set') {
	my ($sender, $count) = @ARGV;
	pod2usage(1) unless $count && $count =~ /\d+/;
	$sch->set_count($sender, $count);
	print "$sender has $count messages remaining.\n";
    } else {
	pod2usage(1);
    }
} else {
    $sch->deliver;
}