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);
17 use Mail
::SpamAssassin
;
19 use base
qw(PVE::RESTHandler);
21 __PACKAGE__-
>register_method ({
25 description
=> "Directory index.",
26 permissions
=> { check
=> [ 'admin', 'audit' ] },
28 additionalProperties
=> 0,
30 node
=> get_standard_option
('pve-node'),
39 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
46 push @$res, { subdir
=> "rules" };
51 __PACKAGE__-
>register_method({
52 name
=> 'rules_status',
55 description
=> "SpamAssassin rules status.",
56 permissions
=> { check
=> [ 'admin', 'audit' ] },
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 $sa_update_dir = "/var/lib/spamassassin/$saversion/";
83 my $check_channel = sub {
86 # see sa-update source:
87 my $channel_file_base = $channel;
88 $channel_file_base =~ s/[^A-Za-z0-9-]+/_/g;
89 my $channelfile = "${sa_update_dir}${channel_file_base}.cf";
95 if (-f
$channelfile) {
96 # stat metadata cf file
97 $mtime = (stat($channelfile))[9]; # 9 is mtime
99 # parse version from metadata cf file
100 my $metadata = PVE
::Tools
::file_read_firstline
($channelfile);
101 if ($metadata =~ m/\s([0-9]+)$/) {
104 warn "invalid metadata in '$channelfile'\n";
107 # call sa-update to see if updates are available
109 my $cmd = "sa-update -v --checkonly --channel $channel";
110 PVE
::Tools
::run_command
($cmd, noerr
=> 1, logfunc
=> sub {
113 if ($line =~ m/Update available for channel \S+: -?[0-9]+ -> ([0-9]+)/) {
122 $result->{version
} = $version if $version > -1;
123 $result->{update_version
} = $newversion if $newversion > -1;
124 $result->{last_updated
} = $mtime if $mtime > -1;
126 if ($newversion > $version) {
127 $result->{update_avail
} = 1;
129 $result->{update_avail
} = 0;
134 my @channels = ('updates.spamassassin.org');
136 my $localchannels = PMG
::Utils
::local_spamassassin_channels
();
137 push(@channels, map { $_->{channelurl
} } @$localchannels);
139 return [ map { $check_channel->($_) } @channels];
142 __PACKAGE__-
>register_method({
143 name
=> 'update_rules',
146 description
=> "Update SpamAssassin rules.",
148 permissions
=> { check
=> [ 'admin' ] },
151 additionalProperties
=> 0,
153 node
=> get_standard_option
('pve-node'),
156 returns
=> { type
=> 'string' },
160 my $rpcenv = PMG
::RESTEnvironment-
>get();
161 my $authuser = $rpcenv->get_user();
166 # setup proxy env (assume sa-update use http)
167 my $pmg_cfg = PMG
::Config-
>new();
168 if (my $http_proxy = $pmg_cfg->get('admin', 'http_proxy')) {
169 $ENV{http_proxy
} = $http_proxy;
172 my $cmd = "sa-update -v";
174 PVE
::Tools
::run_command
($cmd, noerr
=> 1);
176 PMG
::Utils
::update_local_spamassassin_channels
(1);
179 return $rpcenv->fork_worker('saupdate', undef, $authuser, $realcmd);