]> git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Accept.pm
PMG/API2/MyNetworks.pm: fix links attribute
[pmg-api.git] / PMG / RuleDB / Accept.pm
1 package PMG::RuleDB::Accept;
2
3 use strict;
4 use warnings;
5 use DBI;
6
7 use PVE::SafeSyslog;
8 use Digest::SHA;
9
10 use PMG::Utils;
11 use PMG::ModGroup;
12 use PMG::RuleDB::Object;
13
14 use base qw(PMG::RuleDB::Object);
15
16 sub otype {
17 return 4000;
18 }
19
20 sub oclass {
21 return 'action';
22 }
23
24 sub otype_text {
25 return 'Accept';
26 }
27
28 sub oisedit {
29 return 0;
30 }
31
32 sub final {
33 return 1;
34 }
35
36 sub priority {
37 return 99;
38 }
39
40 sub new {
41 my ($type, $ogroup) = @_;
42
43 my $class = ref($type) || $type;
44
45 my $self = $class->SUPER::new($class->otype(), $ogroup);
46
47 return $self;
48 }
49
50 sub 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
63 sub 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
87 sub 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
133 sub short_desc {
134 my $self = shift;
135
136 return "accept message";
137 }
138
139 1;
140
141 __END__
142
143 =head1 PMG::RuleDB::Accept
144
145 Accept a message.