our $db;
-our $Version = '2.0';
+our $VERSION = '2.0';
# Globals
my $userid = $ENV{MAPS_USERNAME} ? $ENV{MAPS_USERNAME} : $ENV{USER};
CountEmail
CountList
CountLog
+ CountLogDistinct
Decrypt
DeleteEmail
DeleteList
sub _cleanTables($$;$) {
my ($table, $timestamp, $dryrun) = @_;
- my $count = 0;
- my $msg = 'Records deleted';
my $condition = "userid = '$userid' and timestamp < '$timestamp'";
if ($dryrun) {
return $db->count($table, $condition);
} else {
- return $db->delete($table, $condition);
+ my ($count, $msg) = $db->delete($table, $condition);
+
+ return $count;
} # if
} # _cleanTables
$db = MyDB->new($username, $password, $dbname, $dbserver);
croak "Unable to instantiate MyDB ($username\@$dbserver:$dbname)" unless $db;
+
+ return;
} # OpenDB
BEGIN {
# Add to white list
$params{sequence} = 0;
+
my ($err, $msg) = AddList(%params);
return -$err, $msg if $err;
while (my $rec = $db->getnext) {
last unless $rec->{userid};
- $status = Whitelist($rec->{sender}, $rec->data);
+ $status = Whitelist($rec->{sender}, $rec->{data});
last if $status;
$rec{hit_count} //= $db->count(
'email',
- "userid = '$rec{userid}' and sender like '$rec{sender}%'"
+ "userid = '$rec{userid}' and sender like '%$rec{sender}%'"
);
($rec{pattern}, $rec{domain}) = split /\@/, delete $rec{sender};
sub AddLog(%) {
my (%params) = @_;
- my $timestamp = UnixDatetime2SQLDatetime(scalar(localtime));
+ $params{timestamp} = UnixDatetime2SQLDatetime(scalar(localtime));
return $db->add('log', %params);
} # AddLog
} # for
return ($err, $msg) if $err;
+ return;
} # AddUserOptions
sub Blacklist(%) {
if ($username) {
if ($username =~ /(.*)\@(.*)/) {
return lc "$1\@$2";
+ } else {
+ return lc "$username\@";
} # if
} elsif ($domain) {
if ($domain =~ /(.*)\@(.*)/) {
? "$email_on_file\@"
: $email_on_file;
if ($sender and $sender =~ /$search_for/i) {
- $rule = "Matching rule: ($listtype:$rec->{sequence}) \"$email_on_file\"";
+ my $comment = $rec->{comment} ? " - $rec->{comment}" : '';
+
+ $rule = "Matching rule: ($listtype:$rec->{sequence}) \"$email_on_file$comment\"";
$rule .= " - $rec->{comment}" if $rec->{comment};
$status = 1;
my $table = 'list';
my $condition = "userid='$params{userid}' and type='$params{type}'";
my $count = 0;
+ my $msg;
# First let's go through the list to see if we have an domain level entry
# (e.g. @spammer.com) and also individual entries (baddude@spammer.com) then
} # for
} # while
- my $msg = 'Records deleted';
$condition = "userid='$params{userid}' and type='$params{type}' and retention is not null";
# First see if anything needs to be deleted
- return (0, $msg) unless $db->count($table, $condition);
+ ($count, $msg) = $db->count($table, $condition);
+
+ return 0 unless $count;
+
+ $count = 0;
my ($err, $errmsg) = $db->find($table, $condition);
. "$rec->{pattern}\@$rec->{domain} $dryrunstr");
$params{log}->dbug("last hit = $rec->{last_hit} < agedDate = $agedDate");
} # if
-
- $count++;
} # unless
+
+ $count++;
} else {
$params{log}->dbug("$rec->{userid}:$params{type}:$rec->{sequence}: nodelete $dryrunstr "
. "last hit = $rec->{last_hit} >= agedDate = $agedDate")
ResequenceList(
userid => $params{userid},
type => $params{type},
- ) if $count and !$params{dryrun};
+ ) if $count && !$params{dryrun};
- return wantarray ? ($count, $msg) : $count;
+ return $count;
} # CleanList
sub CountEmail(%) {
my $condition = "userid='$userid'";
$condition .= " and $additional_condition" if $additional_condition;
- return $db->count_distinct('log', 'sender', $condition);
+ return $db->count('log', $condition);
+} # CountLog
+
+sub CountLogDistinct(%) {
+ my (%params) = @_;
+
+ CheckParms(['userid', 'column'], \%params);
+
+ my ($additional_condition) = delete $params{additional} || '';
+
+ my $condition = "userid='$userid'";
+ $condition .= " and $additional_condition" if $additional_condition;
+
+ return $db->count_distinct('log', $params{column}, $condition);
} # CountLog
sub Decrypt ($$) {
sub GetUserInfo($) {
my ($userid) = @_;
- my $userinfo = $db->getone('user', "userid='$userid'", ['name', 'email']);
-
return %{$db->getone('user', "userid='$userid'", ['name', 'email'])};
-
- return %$userinfo;
} # GetUserInfo
sub GetUserOptions($) {
$db->lock('write', $table);
# Get all records for $userid and $type
- my $listrecs = $db->get($table, $condition);
+ my $listrecs = $db->get($table, $condition,'*', 'order by hit_count desc');
# Delete all of the list entries for this $userid and $type
my ($count, $msg) = $db->delete($table, $condition);
# Finally slice for the given range
my $end_at = $params{start_at} + $params{lines} - 1;
- $end_at = (@senders - 1)
- if $end_at > @senders;
+ $end_at = (@senders - 1) if $end_at >= @senders;
return (@senders) [$params{start_at} .. $end_at];
} # ReturnSenders
if (UserExists($to_user)) {
$userid = $to_user;
- return GetUserInfo $userid;
+ return GetUserOptions $userid;
} else {
return 0;
} # if
my $table = 'list';
my $condition = "userid = '$rec{userid}' and type = '$rec{type}' and sequence = $rec{sequence}";
- if ($rec{pattern} =~ /\@/ and !$rec{domain}) {
+ if ($rec{pattern} =~ /\@/ && !$rec{domain}) {
($rec{pattern}, $rec{domain}) = split /\@/, $rec{pattern};
- } elsif (!$rec{pattern} and $rec{domain} =~ /\@/) {
+ } elsif (!$rec{pattern} && $rec{domain} =~ /\@/) {
($rec{pattern}, $rec{domain}) = split /\@/, $rec{domain};
- } elsif (!$rec{pattern} and !$rec{domain}) {
+ } elsif (!$rec{pattern} && !$rec{domain}) {
return "Must specify either Username or Domain";
} # if