]> git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Block.pm
c7c640efe3253f3ee5e6e5c33f8fc21371d60583
[pmg-api.git] / PMG / RuleDB / Block.pm
1 package PMG::RuleDB::Block;
2
3 use strict;
4 use warnings;
5 use DBI;
6 use Digest::SHA;
7 use Encode;
8
9 use PVE::SafeSyslog;
10
11 use PMG::Utils;
12 use PMG::ModGroup;
13 use PMG::RuleDB::Object;
14
15 use base qw(PMG::RuleDB::Object);
16
17 sub otype {
18 return 4001;
19 }
20
21 sub oclass {
22 return 'action';
23 }
24
25 sub otype_text {
26 return 'Block';
27 }
28
29 sub oisedit {
30 return 0;
31 }
32
33 sub final {
34 return 1;
35 }
36
37 sub priority {
38 return 98;
39 }
40
41 sub new {
42 my ($type, $ogroup) = @_;
43
44 my $class = ref($type) || $type;
45
46 my $self = $class->SUPER::new($class->otype(), $ogroup);
47
48 return $self;
49 }
50
51 sub load_attr {
52 my ($type, $ruledb, $id, $ogroup, $value) = @_;
53
54 my $class = ref($type) || $type;
55
56 my $obj = $class->new ($ogroup);
57 $obj->{id} = $id;
58
59 $obj->{digest} = Digest::SHA::sha1_hex($id, $ogroup);
60
61 return $obj;
62 }
63
64 sub save {
65 my ($self, $ruledb) = @_;
66
67 defined($self->{ogroup}) || return undef;
68
69 if (defined ($self->{id})) {
70 # update
71
72 # nothing to update
73
74 } else {
75 # insert
76
77 my $sth = $ruledb->{dbh}->prepare(
78 "INSERT INTO Object (Objectgroup_ID, ObjectType) VALUES (?, ?);");
79
80 $sth->execute($self->ogroup, $self->otype);
81
82 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
83 }
84
85 return $self->{id};
86 }
87
88 sub execute {
89 my ($self, $queue, $ruledb, $mod_group, $targets,
90 $msginfo, $vars, $marks) = @_;
91
92 my $rulename = $vars->{RULE};
93
94 if ($msginfo->{testmode}) {
95 my $fh = $msginfo->{test_fh};
96 print $fh "block from: $msginfo->{sender}\n";
97 printf $fh "block to: %s\n", join (',', @$targets);
98 }
99
100 foreach my $to (@$targets) {
101 syslog('info', "%s: block mail to <%s> (rule: %s)", $queue->{logid}, encode('UTF-8', $to), $rulename);
102 }
103
104 $queue->set_status($targets, 'blocked');
105 }
106
107 sub short_desc {
108 my $self = shift;
109
110 return "block message";
111 }
112
113 1;
114
115 __END__
116
117 =head1 PMG::RuleDB::Block
118
119 Block a message.