]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/SACustom.pm
1 package PMG
::API2
::SACustom
;
7 use PVE
::JSONSchema
qw(get_standard_option);
10 use PVE
::Tools
qw(extract_param);
11 use PVE
::Exception
qw(raise_param_exc);
13 use PMG
::RESTEnvironment
;
17 use base
qw(PVE::RESTHandler);
19 my $score_properties = {
22 description
=> "The name of the rule.",
23 pattern
=> '[a-zA-Z\_\-\.0-9]+',
27 description
=> "The score the rule should be valued at.",
31 description
=> 'The Comment.',
36 sub json_config_properties
{
37 my ($props, $optional) = @_;
39 foreach my $opt (keys %$score_properties) {
40 # copy values and not the references
41 foreach my $prop (keys %{$score_properties->{$opt}}) {
42 $props->{$opt}->{$prop} = $score_properties->{$opt}->{$prop};
44 if ($optional->{$opt}) {
45 $props->{$opt}->{optional
} = 1;
52 __PACKAGE__-
>register_method({
53 name
=> 'list_scores',
56 description
=> "List custom scores.",
58 permissions
=> { check
=> [ 'admin', 'audit' ] },
61 additionalProperties
=> 0,
68 properties
=> json_config_properties
({
69 digest
=> get_standard_option
('pve-config-digest'),
72 # mark all properties optional, so that we can have
73 # one entry with only digest, and all others without digest
79 links
=> [ { rel
=> 'child', href
=> "{name}" } ],
84 my $restenv = PMG
::RESTEnvironment-
>get();
86 my $tmp = PVE
::INotify
::read_file
('pmg-scores.cf', 1);
88 my $changes = $tmp->{changes
};
89 $restenv->set_result_attrib('changes', $changes) if $changes;
93 for my $rule (sort keys %{$tmp->{data
}}) {
94 push @$res, $tmp->{data
}->{$rule};
97 my $digest = PMG
::SACustom
::calc_digest
($tmp->{data
});
106 __PACKAGE__-
>register_method({
107 name
=> 'apply_score_changes',
111 description
=> "Apply custom score changes.",
113 permissions
=> { check
=> [ 'admin' ] },
115 additionalProperties
=> 0,
117 'restart-daemon' => {
119 description
=> 'If set, also restarts pmg-smtp-filter. '.
120 'This is necessary for the changes to work.',
124 digest
=> get_standard_option
('pve-config-digest'),
127 returns
=> { type
=> "string" },
131 my $restenv = PMG
::RESTEnvironment-
>get();
133 my $user = $restenv->get_user();
135 my $config = PVE
::INotify
::read_file
('pmg-scores.cf');
137 my $digest = PMG
::SACustom
::calc_digest
($config);
138 PVE
::Tools
::assert_if_modified
($digest, $param->{digest
})
144 PMG
::SACustom
::apply_changes
();
145 if ($param->{'restart-daemon'}) {
146 syslog
('info', "re-starting service pmg-smtp-filter: $upid\n");
147 PMG
::Utils
::service_cmd
('pmg-smtp-filter', 'restart');
151 return $restenv->fork_worker('applycustomscores', undef, $user, $realcmd);
154 __PACKAGE__-
>register_method({
155 name
=> 'revert_score_changes',
159 description
=> "Revert custom score changes.",
161 permissions
=> { check
=> [ 'admin' ] },
163 additionalProperties
=> 0,
166 returns
=> { type
=> "null" },
170 unlink PMG
::SACustom
::get_shadow_path
();
176 __PACKAGE__-
>register_method({
177 name
=> 'create_score',
180 description
=> "Create custom SpamAssassin score",
184 additionalProperties
=> 0,
185 properties
=> json_config_properties
({
186 digest
=> get_standard_option
('pve-config-digest'),
189 returns
=> { type
=> 'null' },
193 my $name = extract_param
($param, 'name');
194 my $score = extract_param
($param, 'score');
195 my $comment = extract_param
($param, 'comment');
198 my $config = PVE
::INotify
::read_file
('pmg-scores.cf');
200 my $digest = PMG
::SACustom
::calc_digest
($config);
201 PVE
::Tools
::assert_if_modified
($digest, $param->{digest
})
210 PVE
::INotify
::write_file
('pmg-scores.cf', $config);
213 PVE
::Tools
::lock_file
("/var/lock/pmg-scores.cf.lck", 10, $code);
219 __PACKAGE__-
>register_method({
223 description
=> "Get custom SpamAssassin score",
227 additionalProperties
=> 0,
231 description
=> "The name of the rule.",
232 pattern
=> '[a-zA-Z\_\-\.0-9]+',
238 properties
=> json_config_properties
(),
243 my $name = extract_param
($param, 'name');
244 my $config = PVE
::INotify
::read_file
('pmg-scores.cf');
246 raise_param_exc
({ name
=> "$name not found" })
247 if !$config->{$name};
249 return $config->{$name};
252 __PACKAGE__-
>register_method({
253 name
=> 'edit_score',
256 description
=> "Edit custom SpamAssassin score",
260 additionalProperties
=> 0,
261 properties
=> json_config_properties
({
262 digest
=> get_standard_option
('pve-config-digest'),
265 returns
=> { type
=> 'null' },
269 my $name = extract_param
($param, 'name');
270 my $score = extract_param
($param, 'score');
271 my $comment = extract_param
($param, 'comment');
274 my $config = PVE
::INotify
::read_file
('pmg-scores.cf');
276 my $digest = PMG
::SACustom
::calc_digest
($config);
277 PVE
::Tools
::assert_if_modified
($digest, $param->{digest
})
286 PVE
::INotify
::write_file
('pmg-scores.cf', $config);
289 PVE
::Tools
::lock_file
("/var/lock/pmg-scores.cf.lck", 10, $code);
295 __PACKAGE__-
>register_method({
296 name
=> 'delete_score',
299 description
=> "Edit custom SpamAssassin score",
303 additionalProperties
=> 0,
307 description
=> "The name of the rule.",
308 pattern
=> '[a-zA-Z\_\-\.0-9]+',
310 digest
=> get_standard_option
('pve-config-digest'),
313 returns
=> { type
=> 'null' },
317 my $name = extract_param
($param, 'name');
320 my $config = PVE
::INotify
::read_file
('pmg-scores.cf');
322 my $digest = PMG
::SACustom
::calc_digest
($config);
323 PVE
::Tools
::assert_if_modified
($digest, $param->{digest
})
326 delete $config->{$name};
328 PVE
::INotify
::write_file
('pmg-scores.cf', $config);
331 PVE
::Tools
::lock_file
("/var/lock/pmg-scores.cf.lck", 10, $code);