[pmg-devel] [PATCH pmg-api v3 05/10] API2/Quarantine: refactor quarantine listing api

Dominik Csapak d.csapak at proxmox.com
Thu Oct 10 11:21:56 CEST 2019


will be again used for the attachment quarantine

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/PMG/API2/Quarantine.pm | 104 +++++++++++++++++--------------------
 1 file changed, 49 insertions(+), 55 deletions(-)

diff --git a/src/PMG/API2/Quarantine.pm b/src/PMG/API2/Quarantine.pm
index 5b7170e..ec93e80 100644
--- a/src/PMG/API2/Quarantine.pm
+++ b/src/PMG/API2/Quarantine.pm
@@ -524,6 +524,53 @@ __PACKAGE__->register_method ({
 	return $res;
     }});
 
+my $quarantine_api = sub {
+    my ($param, $quartype, $check_pmail) = @_;
+
+    my $rpcenv = PMG::RESTEnvironment->get();
+    my $authuser = $rpcenv->get_user();
+
+    my $start = $param->{starttime} // (time - 86400);
+    my $end = $param->{endtime} // ($start + 86400);
+
+    my $select;
+    my $pmail;
+    if ($check_pmail) {
+	my $role = $rpcenv->get_role();
+	$pmail = $verify_optional_pmail->($authuser, $role, $param->{pmail});
+	$select = "SELECT * " .
+		  "FROM CMailStore, CMSReceivers WHERE " .
+		  "pmail = ? AND time >= $start AND time < $end AND " .
+		  "QType = '$quartype' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
+		  "AND Status = 'N' ORDER BY pmail, time, receiver";
+    } else {
+	$select = "SELECT * " .
+		  "FROM CMailStore, CMSReceivers WHERE " .
+		  "time >= $start AND time < $end AND " .
+		  "QType = '$quartype' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
+		  "AND Status = 'N' ORDER BY time, receiver";
+    }
+
+    my $res = [];
+
+    my $dbh = PMG::DBTools::open_ruledb();
+
+    my $sth = $dbh->prepare($select);
+
+    if ($check_pmail) {
+	$sth->execute($pmail);
+    } else {
+	$sth->execute();
+    }
+
+    while (my $ref = $sth->fetchrow_hashref()) {
+	my $data = $parse_header_info->($ref);
+	push @$res, $data;
+    }
+
+    return $res;
+};
+
 __PACKAGE__->register_method ({
     name => 'spam',
     path => 'spam',
@@ -585,35 +632,7 @@ __PACKAGE__->register_method ({
     },
     code => sub {
 	my ($param) = @_;
-
-	my $rpcenv = PMG::RESTEnvironment->get();
-	my $authuser = $rpcenv->get_user();
-	my $role = $rpcenv->get_role();
-
-	my $pmail = $verify_optional_pmail->($authuser, $role, $param->{pmail});
-
-	my $res = [];
-
-	my $dbh = PMG::DBTools::open_ruledb();
-
-	my $start = $param->{starttime} // (time - 86400);
-	my $end = $param->{endtime} // ($start + 86400);
-
-	my $sth = $dbh->prepare(
-	    "SELECT * " .
-	    "FROM CMailStore, CMSReceivers WHERE " .
-	    "pmail = ? AND time >= $start AND time < $end AND " .
-	    "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
-	    "AND Status = 'N' ORDER BY pmail, time, receiver");
-
-	$sth->execute($pmail);
-
-	while (my $ref = $sth->fetchrow_hashref()) {
-	    my $data = $parse_header_info->($ref);
-	    push @$res, $data;
-	}
-
-	return $res;
+	return $quarantine_api->($param, 'S', 1);
     }});
 
 __PACKAGE__->register_method ({
@@ -676,32 +695,7 @@ __PACKAGE__->register_method ({
     },
     code => sub {
 	my ($param) = @_;
-
-	my $rpcenv = PMG::RESTEnvironment->get();
-	my $authuser = $rpcenv->get_user();
-
-	my $res = [];
-
-	my $dbh = PMG::DBTools::open_ruledb();
-
-	my $start = $param->{starttime} // (time - 86400);
-	my $end = $param->{endtime} // ($start + 86400);
-
-	my $sth = $dbh->prepare(
-	    "SELECT * " .
-	    "FROM CMailStore, CMSReceivers WHERE " .
-	    "time >= $start AND time < $end AND " .
-	    "QType = 'V' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
-	    "AND Status = 'N' ORDER BY time, receiver");
-
-	$sth->execute();
-
-	while (my $ref = $sth->fetchrow_hashref()) {
-	    my $data = $parse_header_info->($ref);
-	    push @$res, $data;
-	}
-
-	return $res;
+	return $quarantine_api->($param, 'V');
     }});
 
 __PACKAGE__->register_method ({
-- 
2.20.1




More information about the pmg-devel mailing list