]> git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Accept.pm
1387e2428453650d4a1d505ce77581ac3ae3d7a6
[pmg-api.git] / PMG / RuleDB / Accept.pm
1 package PMG::RuleDB::Accept;
2
3 use strict;
4 use warnings;
5 use DBI;
6 use Encode;
7
8 use PVE::SafeSyslog;
9 use Digest::SHA;
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 4000;
19 }
20
21 sub oclass {
22 return 'action';
23 }
24
25 sub otype_text {
26 return 'Accept';
27 }
28
29 sub oisedit {
30 return 0;
31 }
32
33 sub final {
34 return 1;
35 }
36
37 sub priority {
38 return 99;
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 $subgroups = $mod_group->subgroups($targets, 1);
93
94 my $rulename = $vars->{RULE};
95
96 foreach my $ta (@$subgroups) {
97 my ($tg, $entity) = (@$ta[0], @$ta[1]);
98
99 PMG::Utils::remove_marks($entity);
100
101 if ($msginfo->{testmode}) {
102 my $fh = $msginfo->{test_fh};
103 print $fh "accept from: $msginfo->{sender}\n";
104 printf $fh "accept to: %s\n", join (',', @$tg);
105 print $fh "accept content:\n";
106
107 $entity->print($fh);
108 print $fh "accept end\n";
109 $queue->set_status($tg, 'delivered');
110 } else {
111 my ($qid, $code, $mess) = PMG::Utils::reinject_mail(
112 $entity, $msginfo->{sender}, $tg,
113 $msginfo->{xforward}, $msginfo->{fqdn});
114 if ($qid) {
115 foreach (@$tg) {
116 syslog('info', "%s: accept mail to <%s> (rule: %s, %s)", $queue->{logid}, encode('UTF-8', $_), $rulename, $qid);
117 }
118 $queue->set_status ($tg, 'delivered', $qid);
119 } else {
120 foreach (@$tg) {
121 syslog('err', "%s: reinject mail to <%s> (rule: %s) failed", $queue->{logid}, encode('UTF-8', $_), $rulename);
122 }
123 if ($code) {
124 my $resp = substr($code, 0, 1);
125 if ($resp eq '4' || $resp eq '5') {
126 $queue->set_status($tg, 'error', $code, $mess);
127 }
128 }
129 }
130 }
131 }
132
133 # warn if no subgroups
134 }
135
136 sub short_desc {
137 my $self = shift;
138
139 return "accept message";
140 }
141
142 1;
143
144 __END__
145
146 =head1 PMG::RuleDB::Accept
147
148 Accept a message.