]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/SpamAssassin.pm
1 package PMG
::API2
::SpamAssassin
;
9 use PVE
::Exception
qw(raise_param_exc);
11 use PMG
::RESTEnvironment
;
12 use PVE
::JSONSchema
qw(get_standard_option);
16 use Mail
::SpamAssassin
;
18 use base
qw(PVE::RESTHandler);
20 my $SAUPDATE = '/usr/bin/sa-update';
22 __PACKAGE__-
>register_method ({
26 description
=> "Directory index.",
30 additionalProperties
=> 0,
32 node
=> get_standard_option
('pve-node'),
41 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
48 push @$res, { subdir
=> "rules" };
53 __PACKAGE__-
>register_method({
54 name
=> 'rules_status',
57 description
=> "SpamAssassin virus rules status.",
59 additionalProperties
=> 0,
61 node
=> get_standard_option
('pve-node'),
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},
80 my $saversion = $Mail::SpamAssassin
::VERSION
;
81 my $channelfile = "/var/lib/spamassassin/$saversion/updates_spamassassin_org.cf";
87 if (-f
$channelfile) {
88 # stat metadata cf file
89 $mtime = (stat($channelfile))[9]; # 9 is mtime
91 # parse version from metadata cf file
92 my $metadata = PVE
::Tools
::file_read_firstline
($channelfile);
93 if ($metadata =~ m/\s([0-9]+)$/) {
96 warn "invalid metadata in '$channelfile'\n";
99 # call sa-update to see if updates are available
101 my $cmd = "$SAUPDATE -v --checkonly";
102 PVE
::Tools
::run_command
($cmd, noerr
=> 1, logfunc
=> sub {
105 if ($line =~ m/Update available for channel \S+: -?[0-9]+ -> ([0-9]+)/) {
111 channel
=> 'updates.spamassassin.org',
114 $result->{version
} = $version if $version > -1;
115 $result->{update_version
} = $newversion if $newversion > -1;
116 $result->{last_updated
} = $mtime if $mtime > -1;
118 if ($newversion > $version) {
119 $result->{update_avail
} = 1;
121 $result->{update_avail
} = 0;
127 __PACKAGE__-
>register_method({
128 name
=> 'update_rules',
131 description
=> "Update SpamAssassin rules.",
134 additionalProperties
=> 0,
136 node
=> get_standard_option
('pve-node'),
139 returns
=> { type
=> 'string' },
143 my $rpcenv = PMG
::RESTEnvironment-
>get();
144 my $authuser = $rpcenv->get_user();
149 my $cmd = "$SAUPDATE -v";
151 PVE
::Tools
::run_command
($cmd, noerr
=> 1);
154 return $rpcenv->fork_worker('saupdate', undef, $authuser, $realcmd);