]> git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Quarantine.pm
implement spamlist api
[pmg-api.git] / PMG / API2 / Quarantine.pm
1 package PMG::API2::Quarantine;
2
3 use strict;
4 use warnings;
5 use Time::Local;
6 use Time::Zone;
7 use Data::Dumper;
8
9 use PVE::SafeSyslog;
10 use PVE::Exception qw(raise_param_exc);
11 use PVE::Tools qw(extract_param);
12 use PVE::JSONSchema qw(get_standard_option);
13 use PVE::RESTHandler;
14 use PVE::INotify;
15
16 use PMG::AccessControl;
17 use PMG::DBTools;
18
19 use 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 => {
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.",
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 },
89 count => {
90 description => "Number of quarantine entries.",
91 type => 'integer',
92 },
93 spamavg => {
94 description => "Average spam level.",
95 type => 'number',
96 },
97 },
98 },
99 links => [ { rel => 'child', href => "{day}" } ],
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;
114 } else {
115 raise_param_exc({ pmail => "paramater required with role '$role'"})
116 if !defined($pmail);
117 }
118
119 my $res = [];
120
121 my $dbh = PMG::DBTools::open_ruledb();
122
123 my $start = $param->{starttime};
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 " .
133 (defined($start) ? "time >= $start AND " : '') .
134 (defined($end) ? "time < $end AND " : '') .
135 "pmail = ? AND " .
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
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;
197 } else {
198 raise_param_exc({ pmail => "paramater required with role '$role'"})
199 if !defined($pmail);
200 }
201
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
218 while (my $ref = $sth->fetchrow_hashref()) {
219 push @$res, $ref;
220 }
221
222 return $res;
223 }});
224
225 1;