]> git.proxmox.com Git - pmg-api.git/blame - PMG/RuleDB/Accept.pm
allow multiple ids for one action
[pmg-api.git] / PMG / RuleDB / Accept.pm
CommitLineData
758c7b6b
DM
1package PMG::RuleDB::Accept;
2
3use strict;
4use warnings;
758c7b6b
DM
5use DBI;
6
7use PVE::SafeSyslog;
8use Digest::SHA;
9
10use PMG::Utils;
11use PMG::ModGroup;
12use PMG::RuleDB::Object;
13
14use base qw(PMG::RuleDB::Object);
15
16sub otype {
17 return 4000;
18}
19
20sub oclass {
21 return 'action';
22}
23
24sub otype_text {
25 return 'Accept';
26}
27
758c7b6b
DM
28sub oisedit {
29 return 0;
30}
31
32sub final {
33 return 1;
34}
35
36sub priority {
37 return 99;
38}
39
40sub new {
41 my ($type, $ogroup) = @_;
42
43 my $class = ref($type) || $type;
44
7a2cf7e6 45 my $self = $class->SUPER::new($class->otype(), $ogroup);
758c7b6b
DM
46
47 return $self;
48}
49
50sub load_attr {
51 my ($type, $ruledb, $id, $ogroup, $value) = @_;
52
53 my $class = ref($type) || $type;
54
55 my $obj = $class->new($ogroup);
56 $obj->{id} = $id;
57
58 $obj->{digest} = Digest::SHA::sha1_hex($id, $ogroup);
59
60 return $obj;
61}
62
63sub save {
64 my ($self, $ruledb) = @_;
65
66 defined($self->{ogroup}) || return undef;
67
68 if (defined($self->{id})) {
69 # update
70
71 # nothing to update
72
73 } else {
74 # insert
75
76 my $sth = $ruledb->{dbh}->prepare(
77 "INSERT INTO Object (Objectgroup_ID, ObjectType) VALUES (?, ?);");
78
79 $sth->execute($self->ogroup, $self->otype);
80
81 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
82 }
83
84 return $self->{id};
85}
86
87sub execute {
88 my ($self, $queue, $ruledb, $mod_group, $targets,
89 $msginfo, $vars, $marks) = @_;
90
91 my $subgroups = $mod_group->subgroups($targets, 1);
92
93 foreach my $ta (@$subgroups) {
94 my ($tg, $entity) = (@$ta[0], @$ta[1]);
95
96 PMG::Utils::remove_marks($entity);
97
98 if ($msginfo->{testmode}) {
99 my $fh = $msginfo->{test_fh};
100 print $fh "accept from: $msginfo->{sender}\n";
101 printf $fh "accept to: %s\n", join (',', @$tg);
102 print $fh "accept content:\n";
103
104 $entity->print($fh);
105 print $fh "accept end\n";
106 $queue->set_status($tg, 'delivered');
107 } else {
108 my ($qid, $code, $mess) = PMG::Utils::reinject_mail(
109 $entity, $msginfo->{sender}, $tg,
110 $msginfo->{xforward}, $msginfo->{fqdn});
111 if ($qid) {
112 foreach (@$tg) {
113 syslog('info', "%s: accept mail to <%s> (%s)", $queue->{logid}, $_, $qid);
114 }
115 $queue->set_status ($tg, 'delivered', $qid);
116 } else {
117 foreach (@$tg) {
118 syslog('err', "%s: reinject mail to <%s> failed", $queue->{logid}, $_);
119 }
120 if ($code) {
121 my $resp = substr($code, 0, 1);
122 if ($resp eq '4' || $resp eq '5') {
123 $queue->set_status($tg, 'error', $code, $mess);
124 }
125 }
126 }
127 }
128 }
129
130 # warn if no subgroups
131}
132
133sub short_desc {
134 my $self = shift;
135
5e38fbef 136 return "accept message";
758c7b6b
DM
137}
138
1391;
5e38fbef 140
758c7b6b
DM
141__END__
142
143=head1 PMG::RuleDB::Accept
144
145Accept a message.