]> git.proxmox.com Git - pmg-api.git/blame - PMG/API2/SpamAssassin.pm
delete/deliver_quarantined_mail: use receiver instead of pmail
[pmg-api.git] / PMG / API2 / SpamAssassin.pm
CommitLineData
b00d2d73
DC
1package PMG::API2::SpamAssassin;
2
3use strict;
4use warnings;
5
6use PVE::Tools;
7use PVE::SafeSyslog;
8use PVE::INotify;
9use PVE::Exception qw(raise_param_exc);
10use PVE::RESTHandler;
11use PMG::RESTEnvironment;
12use PVE::JSONSchema qw(get_standard_option);
13
14use PMG::Utils;
15
16use Mail::SpamAssassin;
17
18use base qw(PVE::RESTHandler);
19
20my $SAUPDATE = '/usr/bin/sa-update';
21
22__PACKAGE__->register_method ({
23 name => 'index',
24 path => '',
25 method => 'GET',
26 description => "Directory index.",
46607089 27 permissions => { check => [ 'admin', 'audit' ] },
b00d2d73
DC
28 parameters => {
29 additionalProperties => 0,
30 properties => {
31 node => get_standard_option('pve-node'),
32 },
33 },
34 returns => {
35 type => 'array',
36 items => {
37 type => "object",
38 properties => {},
39 },
40 links => [ { rel => 'child', href => "{subdir}" } ],
41 },
42 code => sub {
43 my ($param) = @_;
44
45 my $res = [];
46
47 push @$res, { subdir => "rules" };
48
49 return $res;
50 }});
51
52__PACKAGE__->register_method({
53 name => 'rules_status',
54 path => 'rules',
55 method => 'GET',
46607089
DM
56 description => "SpamAssassin rules status.",
57 permissions => { check => [ 'admin', 'audit' ] },
58 proxyto => 'node',
b00d2d73
DC
59 parameters => {
60 additionalProperties => 0,
61 properties => {
62 node => get_standard_option('pve-node'),
63 },
64 },
65 returns => {
66 type => 'array',
67 items => {
68 type => "object",
69 properties => {
70 channel => { type => 'string' },
71 update_avail => { type => 'boolean' },
72 version => { type => 'string', optional => 1 },
73 last_updated => { type => 'integer', optional => 1},
74 update_version => { type => 'string', optional => 1},
75 },
76 },
77 },
78 code => sub {
79 my ($param) = @_;
80
81 my $saversion = $Mail::SpamAssassin::VERSION;
82 my $channelfile = "/var/lib/spamassassin/$saversion/updates_spamassassin_org.cf";
83
84 my $mtime = -1;
85 my $version = -1;
86 my $newversion = -1;
87
88 if (-f $channelfile) {
89 # stat metadata cf file
90 $mtime = (stat($channelfile))[9]; # 9 is mtime
91
92 # parse version from metadata cf file
93 my $metadata = PVE::Tools::file_read_firstline($channelfile);
94 if ($metadata =~ m/\s([0-9]+)$/) {
95 $version = $1;
96 } else {
97 warn "invalid metadata in '$channelfile'\n";
98 }
99 }
100 # call sa-update to see if updates are available
101
102 my $cmd = "$SAUPDATE -v --checkonly";
103 PVE::Tools::run_command($cmd, noerr => 1, logfunc => sub {
104 my ($line) = @_;
105
106 if ($line =~ m/Update available for channel \S+: -?[0-9]+ -> ([0-9]+)/) {
107 $newversion = $1;
108 }
109 });
110
111 my $result = {
112 channel => 'updates.spamassassin.org',
113 };
114
115 $result->{version} = $version if $version > -1;
116 $result->{update_version} = $newversion if $newversion > -1;
117 $result->{last_updated} = $mtime if $mtime > -1;
118
119 if ($newversion > $version) {
120 $result->{update_avail} = 1;
121 } else {
122 $result->{update_avail} = 0;
123 }
124
125 return [$result];
126 }});
127
128__PACKAGE__->register_method({
129 name => 'update_rules',
130 path => 'rules',
131 method => 'POST',
132 description => "Update SpamAssassin rules.",
133 protected => 1,
46607089
DM
134 permissions => { check => [ 'admin' ] },
135 proxyto => 'node',
b00d2d73
DC
136 parameters => {
137 additionalProperties => 0,
138 properties => {
139 node => get_standard_option('pve-node'),
140 },
141 },
142 returns => { type => 'string' },
143 code => sub {
144 my ($param) = @_;
145
146 my $rpcenv = PMG::RESTEnvironment->get();
147 my $authuser = $rpcenv->get_user();
148
149 my $realcmd = sub {
150 my $upid = shift;
151
152 my $cmd = "$SAUPDATE -v";
153
154 PVE::Tools::run_command($cmd, noerr => 1);
155 };
156
157 return $rpcenv->fork_worker('saupdate', undef, $authuser, $realcmd);
158 }});
159
1601;