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