-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpmgdq
144 lines (114 loc) · 3.95 KB
/
pmgdq
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Std;
use Net::SMTP;
use JSON;
use PMG::RuleDB;
use PMG::Quarantine;
binmode(STDOUT, "encoding(UTF-8)");
my %options;
my $dbh;
sub myhelp{
print "pmdq -f <from> -r <rcpt> -u <subject> -a <action>\n";
print "\taction is mandatory : count/list/remove/deliver\n";
print "\tit needs at least one : from, rcpt or subject ; you can mix several\n";
print "\tIf '-r allemails' or '-f allemails' is used, the action will be done to all the quarantine queue !\n";
exit(2);
}
sub deliver_quarantined_mail {
my ($id, $receiver) = @_;
my ($cid, $rid, $tid) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/;
$cid = int($cid);
$rid = int($rid);
$tid = int($tid);
if (!$dbh) {
$dbh = PMG::DBTools::open_ruledb();
}
my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid);
PMG::Quarantine::deliver_quarantined_mail($dbh, $ref, $receiver);
}
sub delete_quarantined_mail {
my ($id) = @_;
my ($cid, $rid, $tid) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/;
$cid = int($cid);
$rid = int($rid);
$tid = int($tid);
if (!$dbh) {
$dbh = PMG::DBTools::open_ruledb();
}
my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid);
PMG::Quarantine::delete_quarantined_mail($dbh, $ref);
}
$options{f} = '';
$options{r} = '';
$options{s} = '';
$options{a} = '';
getopts('f:r:s:a:',\%options);
my $from = $options{f};
my $rcpt = $options{r};
my $subject = $options{s};
my $action = lc($options{a});
# List the spam rcpt since when ?
my $endtime = time(); # now
my $starttime = $endtime - (7 * 24 * 60 * 60) ; # 7 seven days before now !
if($action ne 'count' && $action ne 'list' && $action ne 'remove' && $action ne 'deliver'){
myhelp();
}
if($from eq '' && $rcpt eq '' && $subject eq ''){
myhelp();
}
#print("action = ".$action.", from = ".$from.", rcpt = ".$rcpt.", subject = ".$subject."\n");
my $users = qx(/usr/bin/pmgsh get /quarantine/spamusers --starttime $starttime --endtime $endtime 2>/dev/null);
my $data = decode_json($users);
#Global counter
my $ecount = 0;
foreach my $u (@$data){
my $result = 1;
if ($rcpt ne '') {
if($rcpt eq 'allemails'){
$result = 1;
} elsif($u->{'mail'} =~ /^$rcpt$/){
$result = 1;
} else {
$result = 0;
}
}
if($result == 1){
# print("Mail: ".$u->{'mail'}."\n");
my $jspam= qx(/usr/bin/pmgsh get /quarantine/spam --starttime $starttime --endtime $endtime -pmail $u->{'mail'} 2>/dev/null);
my $emails = decode_json($jspam);
foreach my $em (@$emails){
$result = 1;
if ($from ne '') {
if($from eq 'allemails'){
$result = 1;
} elsif($em->{'from'} =~ /.*$from.*/){
$result = 1;
} else {
$result = 0;
}
}
if ($result == 1 && $subject ne '') {
if($em->{'subject'} =~ /$subject/){
$result = 1;
} else {
$result = 0;
}
}
if ($result == 1){
$ecount++;
if($action eq 'list'){
print("ID: ".$em->{'id'}." - Rcpt: ".$u->{'mail'}." - From: ".$em->{'from'}." - Subject: ".$em->{'subject'}."\n");
} elsif($action eq 'remove'){
print("REMOVE => ID: ".$em->{'id'}." - Rcpt: ".$u->{'mail'}." - From: ".$em->{'from'}." - Subject: ".$em->{'subject'}."\n");
delete_quarantined_mail($em->{'id'});
} elsif($action eq 'deliver'){
print("DELIVER => ID: ".$em->{'id'}." - Rcpt: ".$u->{'mail'}." - From: ".$em->{'from'}." - Subject: ".$em->{'subject'}."\n");
deliver_quarantined_mail($em->{'id'},$u->{'mail'});
}
}
}
}
}
print("Total of $ecount quarantined mails\n");