]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/API2/Quarantine.pm
add optional list parameter to quarusers api call
[pmg-api.git] / PMG / API2 / Quarantine.pm
index 68c09d280dd6fa44733b10a9baa2149f89fbf1ee..158247b14dc7303eab38b3738188189599675554 100644 (file)
@@ -39,15 +39,17 @@ my $extract_pmail = sub {
 };
 
 my $verify_optional_pmail = sub {
-    my ($authuser, $role, $pmail) = @_;
+    my ($authuser, $role, $pmail_param) = @_;
 
+    my $pmail;
     if ($role eq 'quser') {
-       raise_param_exc({ pmail => "parameter not allwed with role '$role'"})
-           if defined($pmail) && ($pmail ne $authuser);
        $pmail = $extract_pmail->($authuser, $role);
+       raise_param_exc({ pmail => "parameter not allwed with role '$role'"})
+           if defined($pmail_param) && ($pmail ne $pmail_param);
     } else {
        raise_param_exc({ pmail => "parameter required with role '$role'"})
-           if !defined($pmail);
+           if !defined($pmail_param);
+       $pmail = $pmail_param;
     }
     return $pmail;
 };
@@ -133,7 +135,7 @@ my $parse_header_info = sub {
 
     $res->{envelope_sender} = $ref->{sender};
     $res->{receiver} = $ref->{receiver} // $ref->{pmail};
-    $res->{id} = 'C' . $ref->{cid} . 'R' . $ref->{rid};
+    $res->{id} = 'C' . $ref->{cid} . 'R' . $ref->{rid} . 'T' . $ref->{ticketid};
     $res->{time} = $ref->{time};
     $res->{bytes} = $ref->{bytes};
 
@@ -251,7 +253,7 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            pmail => $pmail_param_type,
-           address => get_standard_option('pmg-email-address', {
+           address => get_standard_option('pmg-whiteblacklist-entry-list', {
                description => "The address you want to add.",
            }),
        },
@@ -260,7 +262,8 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       $read_or_modify_user_bw_list->('WL', $param, [ $param->{address} ]);
+       my $addresses = [split(',', $param->{address})];
+       $read_or_modify_user_bw_list->('WL', $param, $addresses);
 
        return undef;
     }});
@@ -276,7 +279,7 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            pmail => $pmail_param_type,
-           address => get_standard_option('pmg-email-address', {
+           address => get_standard_option('pmg-whiteblacklist-entry-list', {
                description => "The address you want to remove.",
            }),
        },
@@ -285,7 +288,8 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       $read_or_modify_user_bw_list->('WL', $param, [ $param->{address} ], 1);
+       my $addresses = [split(',', $param->{address})];
+       $read_or_modify_user_bw_list->('WL', $param, $addresses, 1);
 
        return undef;
     }});
@@ -330,7 +334,7 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            pmail => $pmail_param_type,
-           address => get_standard_option('pmg-email-address', {
+           address => get_standard_option('pmg-whiteblacklist-entry-list', {
                description => "The address you want to add.",
            }),
        },
@@ -339,7 +343,8 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       $read_or_modify_user_bw_list->('BL', $param, [ $param->{address} ]);
+       my $addresses = [split(',', $param->{address})];
+       $read_or_modify_user_bw_list->('BL', $param, $addresses);
 
        return undef;
     }});
@@ -355,7 +360,7 @@ __PACKAGE__->register_method ({
        additionalProperties => 0,
        properties => {
            pmail => $pmail_param_type,
-           address => get_standard_option('pmg-email-address', {
+           address => get_standard_option('pmg-whiteblacklist-entry-list', {
                description => "The address you want to remove.",
            }),
        },
@@ -364,7 +369,8 @@ __PACKAGE__->register_method ({
     code => sub {
        my ($param) = @_;
 
-       $read_or_modify_user_bw_list->('BL', $param, [ $param->{address} ], 1);
+       my $addresses = [split(',', $param->{address})];
+       $read_or_modify_user_bw_list->('BL', $param, $addresses, 1);
 
        return undef;
     }});
@@ -471,7 +477,14 @@ __PACKAGE__->register_method ({
     description => "Get a list of users with whitelist/blacklist setttings.",
     parameters => {
        additionalProperties => 0,
-       properties => {},
+       properties => {
+           list => {
+               type => 'string',
+               description => 'If set, limits the result to the given list.',
+               enum => ['BL', 'WL'],
+               optional => 1,
+           },
+       },
     },
     returns => {
        type => 'array',
@@ -495,10 +508,14 @@ __PACKAGE__->register_method ({
 
        my $dbh = PMG::DBTools::open_ruledb();
 
-       my $sth = $dbh->prepare(
-           "SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail");
-
-       $sth->execute();
+       my $sth;
+       if ($param->{list}) {
+           $sth = $dbh->prepare("SELECT DISTINCT pmail FROM UserPrefs WHERE name = ? ORDER BY pmail");
+           $sth->execute($param->{list});
+       } else {
+           $sth = $dbh->prepare("SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail");
+           $sth->execute();
+       }
 
        while (my $ref = $sth->fetchrow_hashref()) {
            push @$res, { mail => $ref->{pmail} };
@@ -737,11 +754,11 @@ __PACKAGE__->register_method ({
            id => {
                description => 'Unique ID',
                type => 'string',
-               pattern => 'C\d+R\d+',
-               maxLength => 40,
+               pattern => 'C\d+R\d+T\d+',
+               maxLength => 60,
            },
            raw => {
-               description => "Display 'raw' eml data. This is only used with the 'htmlmail' formatter.",
+               description => "Display 'raw' eml data. Deactivates size limit.",
                type => 'boolean',
                optional => 1,
                default => 0,
@@ -810,25 +827,25 @@ __PACKAGE__->register_method ({
        my $role = $rpcenv->get_role();
        my $format = $rpcenv->get_format();
 
-       my ($cid, $rid) = $param->{id} =~ m/^C(\d+)R(\d+)$/;
+       my $raw = $param->{raw} // 0;
+
+       my ($cid, $rid, $tid) = $param->{id} =~ m/^C(\d+)R(\d+)T(\d+)$/;
        $cid = int($cid);
        $rid = int($rid);
-
-       my $pmail = $role eq 'quser' ? $extract_pmail->($authuser, $role) : undef;
+       $tid = int($tid);
 
        my $dbh = PMG::DBTools::open_ruledb();
 
-       my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $pmail);
+       my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid);
 
        if ($role eq 'quser') {
            my $quar_username = $ref->{pmail} . '@quarantine';
-           raise_perm_exc("mail does not belong to user '$authuser' ($ref->{pmail}, $pmail)")
+           raise_perm_exc("mail does not belong to user '$authuser' ($ref->{pmail})")
                if $authuser ne $quar_username;
        }
 
        my $res = $parse_header_info->($ref);
 
-
        my $filename = $ref->{file};
        my $spooldir = $PMG::MailQueue::spooldir;
 
@@ -840,7 +857,7 @@ __PACKAGE__->register_method ({
            my $viewimages = $cfg->get('spamquar', 'viewimages');
            my $allowhref = $cfg->get('spamquar', 'allowhrefs');
 
-           $res->{content} = PMG::HTMLMail::email_to_html($path, $param->{raw}, $viewimages, $allowhref);
+           $res->{content} = PMG::HTMLMail::email_to_html($path, $raw, $viewimages, $allowhref);
 
            # to make result verification happy
            $res->{file} = '';
@@ -850,7 +867,10 @@ __PACKAGE__->register_method ({
        } else {
            # include additional details
 
-           my ($header, $content) = PMG::HTMLMail::read_raw_email($path, 4096);
+           # we want to get the whole email in raw mode
+           my $maxbytes = (!$raw)? 4096 : undef;
+
+           my ($header, $content) = PMG::HTMLMail::read_raw_email($path, $maxbytes);
 
            $res->{file} = $ref->{file};
            $res->{spaminfo} = decode_spaminfo($ref->{info});
@@ -891,12 +911,10 @@ __PACKAGE__->register_method ({
     parameters => {
        additionalProperties => 0,
        properties => {
-           pmail => $pmail_param_type,
            id => {
-               description => 'Unique ID',
+               description => 'Unique IDs, seperate with ;',
                type => 'string',
-               pattern => 'C\d+R\d+',
-               maxLength => 40,
+               pattern => 'C\d+R\d+T\d+(;C\d+R\d+T\d+)*',
            },
            action => {
                description => 'Action - specify what you want to do with the mail.',
@@ -913,36 +931,37 @@ __PACKAGE__->register_method ({
        my $authuser = $rpcenv->get_user();
        my $role = $rpcenv->get_role();
        my $action = $param->{action};
-
-       my ($cid, $rid) = $param->{id} =~ m/^C(\d+)R(\d+)$/;
-       $cid = int($cid);
-       $rid = int($rid);
-
-       my $pmail = $verify_optional_pmail->($authuser, $role, $param->{pmail});
+       my @idlist = split(';', $param->{id});
 
        my $dbh = PMG::DBTools::open_ruledb();
 
-       my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $pmail);
+       for my $id (@idlist) {
+           my ($cid, $rid, $tid) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/;
+           $cid = int($cid);
+           $rid = int($rid);
+           $tid = int($tid);
 
-       if ($role eq 'quser') {
-           my $quar_username = $ref->{pmail} . '@quarantine';
-           raise_perm_exc("mail does not belong to user '$authuser' ($ref->{pmail}, $pmail)")
-               if $authuser ne $quar_username;
-       }
+           my $ref = PMG::DBTools::load_mail_data($dbh, $cid, $rid, $tid);
 
-       my $sender = $get_real_sender->($ref);
-       my $username = $ref->{pmail};
-
-       if ($action eq 'whitelist') {
-           PMG::Quarantine::add_to_blackwhite($dbh, $username, 'WL', [ $sender ]);
-       } elsif ($action eq 'blacklist') {
-           PMG::Quarantine::add_to_blackwhite($dbh, $username, 'BL', [ $sender ]);
-       } elsif ($action eq 'deliver') {
-           PMG::Quarantine::deliver_quarantined_mail($dbh, $ref, $pmail);
-       } elsif ($action eq 'delete') {
-           PMG::Quarantine::delete_quarantined_mail($dbh, $ref, $pmail);
-       } else {
-           die "internal error"; # should not be reached
+           if ($role eq 'quser') {
+               my $quar_username = $ref->{pmail} . '@quarantine';
+               raise_perm_exc("mail does not belong to user '$authuser' ($ref->{pmail})")
+               if $authuser ne $quar_username;
+           }
+
+           my $sender = $get_real_sender->($ref);
+
+           if ($action eq 'whitelist') {
+               PMG::Quarantine::add_to_blackwhite($dbh, $ref->{pmail}, 'WL', [ $sender ]);
+           } elsif ($action eq 'blacklist') {
+               PMG::Quarantine::add_to_blackwhite($dbh, $ref->{pmail}, 'BL', [ $sender ]);
+           } elsif ($action eq 'deliver') {
+               PMG::Quarantine::deliver_quarantined_mail($dbh, $ref, $ref->{receiver} // $ref->{pmail});
+           } elsif ($action eq 'delete') {
+               PMG::Quarantine::delete_quarantined_mail($dbh, $ref);
+           } else {
+               die "internal error"; # should not be reached
+           }
        }
 
        return undef;