]> git.proxmox.com Git - pmg-api.git/blame - PMG/API2/Quarantine.pm
PMG::Utils::decode_rfc1522 - new helper
[pmg-api.git] / PMG / API2 / Quarantine.pm
CommitLineData
b66faa68
DM
1package PMG::API2::Quarantine;
2
3use strict;
4use warnings;
5use Time::Local;
6use Time::Zone;
7use Data::Dumper;
8
9use PVE::SafeSyslog;
10use PVE::Exception qw(raise_param_exc);
11use PVE::Tools qw(extract_param);
12use PVE::JSONSchema qw(get_standard_option);
13use PVE::RESTHandler;
14use PVE::INotify;
15
16use PMG::AccessControl;
17use PMG::DBTools;
18
19use base qw(PVE::RESTHandler);
20
21
22__PACKAGE__->register_method ({
23 name => 'index',
24 path => '',
25 method => 'GET',
26 permissions => { user => 'all' },
27 description => "Directory index.",
28 parameters => {
29 additionalProperties => 0,
30 properties => {},
31 },
32 returns => {
33 type => 'array',
34 items => {
35 type => "object",
36 properties => {},
37 },
38 links => [ { rel => 'child', href => "{name}" } ],
39 },
40 code => sub {
41 my ($param) = @_;
42
43 my $result = [
44 { name => 'deliver' },
45 { name => 'spam' },
46 { name => 'virus' },
47 ];
48
49 return $result;
50 }});
51
52__PACKAGE__->register_method ({
53 name => 'spam',
54 path => 'spam',
55 method => 'GET',
56 permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] },
57 description => "Show spam mails distribution (per day).",
58 parameters => {
59 additionalProperties => 0,
60 properties => {
61 starttime => {
62 description => "Only consider entries newer than 'startime' (unix epoch).",
63 type => 'integer',
64 minimum => 0,
65 optional => 1,
66 },
67 endtime => {
68 description => "Only consider entries older than 'endtime' (unix epoch).",
69 type => 'integer',
70 minimum => 1,
71 optional => 1,
72 },
73 pmail => {
ded33c7c 74 description => "List entries for the user with this primary email address. Quarantine users cannot speficy this parameter, but it is required for all other roles.",
b66faa68
DM
75 type => 'string', format => 'email',
76 optional => 1,
77 },
78 },
79 },
80 returns => {
81 type => 'array',
82 items => {
83 type => "object",
84 properties => {
85 day => {
86 description => "Day (as unix epoch).",
87 type => 'integer',
88 },
bc1ebe25 89 count => {
b66faa68
DM
90 description => "Number of quarantine entries.",
91 type => 'integer',
92 },
93 spamavg => {
94 description => "Average spam level.",
95 type => 'number',
bc1ebe25 96 },
b66faa68
DM
97 },
98 },
ded33c7c 99 links => [ { rel => 'child', href => "{day}" } ],
b66faa68
DM
100 },
101 code => sub {
102 my ($param) = @_;
103
104 my $rpcenv = PMG::RESTEnvironment->get();
105 my $authuser = $rpcenv->get_user();
106 my $role = $rpcenv->get_role();
107
108 my $pmail = $param->{pmail};
109
110 if ($role eq 'quser') {
111 raise_param_exc({ pmail => "paramater not allwed with role '$role'"})
112 if defined($pmail);
113 $pmail = $authuser;
ded33c7c
DM
114 } else {
115 raise_param_exc({ pmail => "paramater required with role '$role'"})
116 if !defined($pmail);
b66faa68
DM
117 }
118
119 my $res = [];
bc1ebe25 120
b66faa68
DM
121 my $dbh = PMG::DBTools::open_ruledb();
122
ec7035c2 123 my $start = $param->{starttime};
b66faa68
DM
124 my $end = $param->{endtime};
125
126 my $timezone = tz_local_offset();
127
128 my $sth = $dbh->prepare(
129 "SELECT " .
130 "((time + $timezone) / 86400) * 86400 - $timezone as day, " .
131 "count (ID) as count, avg (Spamlevel) as spamavg " .
132 "FROM CMailStore, CMSReceivers WHERE " .
ec7035c2
DM
133 (defined($start) ? "time >= $start AND " : '') .
134 (defined($end) ? "time < $end AND " : '') .
ded33c7c 135 "pmail = ? AND " .
b66faa68
DM
136 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
137 "AND Status = 'N' " .
138 "GROUP BY day " .
139 "ORDER BY day DESC");
140
ded33c7c
DM
141 $sth->execute($pmail);
142
143 while (my $ref = $sth->fetchrow_hashref()) {
144 push @$res, $ref;
145 }
146
147 return $res;
148 }});
149
150__PACKAGE__->register_method ({
151 name => 'spamlist',
152 path => 'spam/{starttime}',
153 method => 'GET',
154 permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] },
155 description => "Show spam mails distribution (per day).",
156 parameters => {
157 additionalProperties => 0,
158 properties => {
159 starttime => {
160 description => "Only consider entries newer than 'starttime' (unix epoch).",
161 type => 'integer',
162 minimum => 0,
163 },
164 endtime => {
165 description => "Only consider entries older than 'endtime' (unix epoch). This is set to '<start> + 1day' by default.",
166 type => 'integer',
167 minimum => 1,
168 optional => 1,
169 },
170 pmail => {
171 description => "List entries for the user with this primary email address. Quarantine users cannot speficy this parameter, but it is required for all other roles.",
172 type => 'string', format => 'email',
173 optional => 1,
174 },
175 },
176 },
177 returns => {
178 type => 'array',
179 items => {
180 type => "object",
181 properties => {},
182 },
183 },
184 code => sub {
185 my ($param) = @_;
186
187 my $rpcenv = PMG::RESTEnvironment->get();
188 my $authuser = $rpcenv->get_user();
189 my $role = $rpcenv->get_role();
190
191 my $pmail = $param->{pmail};
192
193 if ($role eq 'quser') {
194 raise_param_exc({ pmail => "paramater not allwed with role '$role'"})
195 if defined($pmail);
196 $pmail = $authuser;
b66faa68 197 } else {
ded33c7c
DM
198 raise_param_exc({ pmail => "paramater required with role '$role'"})
199 if !defined($pmail);
b66faa68
DM
200 }
201
ded33c7c
DM
202 my $res = [];
203
204 my $dbh = PMG::DBTools::open_ruledb();
205
206 my $start = $param->{starttime};
207 my $end = $param->{endtime} // ($start + 86400);
208
209 my $sth = $dbh->prepare(
210 "SELECT * " .
211 "FROM CMailStore, CMSReceivers WHERE " .
212 "pmail = ? AND time >= $start AND time < $end AND " .
213 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
214 "AND Status = 'N' ORDER BY pmail, time, receiver");
215
216 $sth->execute($pmail);
217
b66faa68
DM
218 while (my $ref = $sth->fetchrow_hashref()) {
219 push @$res, $ref;
220 }
221
222 return $res;
223 }});
224
2251;