]> git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/BCC.pm
cbe28105b030ace0cb9e016039129200a674b292
[pmg-api.git] / PMG / RuleDB / BCC.pm
1 package PMG::RuleDB::BCC;
2
3 use strict;
4 use warnings;
5 use DBI;
6
7 use PVE::SafeSyslog;
8
9 use PMG::Utils;
10 use PMG::ModGroup;
11 use PMG::RuleDB::Object;
12
13 use base qw(PMG::RuleDB::Object);
14
15 sub otype {
16 return 4005;
17 }
18
19 sub oclass {
20 return 'action';
21 }
22
23 sub otype_text {
24 return 'BCC';
25 }
26
27 sub oisedit {
28 return 1;
29 }
30
31 sub final {
32 return 0;
33 }
34
35 sub priority {
36 return 80;
37 }
38
39 sub new {
40 my ($type, $target, $original, $ogroup) = @_;
41
42 my $class = ref($type) || $type;
43
44 my $self = $class->SUPER::new($class->otype(), $ogroup);
45
46 $self->{target} = $target || 'receiver@domain.tld';
47
48 defined ($original) || ($original = 1);
49
50 $self->{original} = $original;
51
52 return $self;
53 }
54
55 sub load_attr {
56 my ($type, $ruledb, $id, $ogroup, $value) = @_;
57
58 my $class = ref($type) || $type;
59
60 defined($value) || return undef;
61
62 $value =~ m/^([01]):(.*)/ || return undef;
63
64 my ($target, $original) = ($2, $1);
65
66 my $obj = $class->new($target, $original, $ogroup);
67 $obj->{id} = $id;
68
69 $obj->{digest} = Digest::SHA::sha1_hex($id, $target, $original, $ogroup);
70
71 return $obj;
72 }
73
74 sub save {
75 my ($self, $ruledb) = @_;
76
77 defined($self->{ogroup}) || die "undefined object attribute: ERROR";
78 defined($self->{target}) || die "undefined object attribute: ERROR";
79 defined($self->{original}) || die "undefined object attribute: ERROR";
80
81 if ($self->{original}) {
82 $self->{original} = 1;
83 } else {
84 $self->{original} = 0;
85 }
86
87 my $value = "$self->{original}:$self->{target}";
88
89 if (defined($self->{id})) {
90 # update
91
92 $ruledb->{dbh}->do(
93 "UPDATE Object SET Value = ? WHERE ID = ?",
94 undef, $value, $self->{id});
95
96 } else {
97 # insert
98
99 my $sth = $ruledb->{dbh}->prepare(
100 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
101 "VALUES (?, ?, ?);");
102
103 $sth->execute($self->{ogroup}, $self->otype, $value);
104
105 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
106 }
107
108 return $self->{id};
109 }
110
111 sub execute {
112 my ($self, $queue, $ruledb, $mod_group, $targets,
113 $msginfo, $vars, $marks) = @_;
114
115 my $subgroups = $mod_group->subgroups($targets, 1);
116
117 my $rulename = $vars->{RULE};
118
119 my $bcc_to = PMG::Utils::subst_values($self->{target}, $vars);
120
121 if ($bcc_to =~ m/^\s*$/) {
122 # this happens if a notification is triggered by bounce mails
123 # which notifies the sender <> - we just log and then ignore it
124 syslog('info', "%s: bcc to <> (rule: %s, ignored)", $queue->{logid}, $rulename);
125 return;
126 }
127
128 my @bcc_targets = split (/\s*,\s*/, $bcc_to);
129
130 if ($self->{original}) {
131 $subgroups = [[\@bcc_targets, $mod_group->{entity}]];
132 }
133
134 foreach my $ta (@$subgroups) {
135 my ($tg, $entity) = (@$ta[0], @$ta[1]);
136
137 $entity = $entity->dup();
138 PMG::Utils::remove_marks($entity);
139
140 if ($msginfo->{testmode}) {
141 my $fh = $msginfo->{test_fh};
142 print $fh "bcc from: $msginfo->{sender}\n";
143 printf $fh "bcc to: %s\n", join (',', @$tg);
144 print $fh "bcc content:\n";
145 $entity->print ($fh);
146 print $fh "bcc end\n";
147 } else {
148 my $qid = PMG::Utils::reinject_mail(
149 $entity, $msginfo->{sender}, \@bcc_targets,
150 $msginfo->{xforward}, $msginfo->{fqdn}, 1);
151 foreach (@bcc_targets) {
152 if ($qid) {
153 syslog('info', "%s: bcc to <%s> (rule: %s, %s)", $queue->{logid}, $_, $rulename, $qid);
154 } else {
155 syslog('err', "%s: bcc to <%s> (rule: %s) failed", $queue->{logid}, $_, $rulename);
156 }
157 }
158 }
159 }
160
161 # warn if no subgroups
162 }
163
164 sub short_desc {
165 my $self = shift;
166
167 my $descr = "send bcc to: $self->{target}";
168
169 $descr .= " (original)" if $self->{original};
170
171 return $descr;
172 }
173
174 sub properties {
175 my ($class) = @_;
176
177 return {
178 target => {
179 description => "Send a Blind Carbon Copy to this email address.",
180 type => 'string', format => 'email',
181 },
182 original =>{
183 description => "Send the original, unmodified mail.",
184 type => 'boolean',
185 optional => 1,
186 default => 1,
187 },
188 };
189 }
190
191 sub get {
192 my ($self) = @_;
193
194 return {
195 target => $self->{target},
196 original => $self->{original},
197 };
198 }
199
200 sub update {
201 my ($self, $param) = @_;
202
203 $self->{target} = $param->{target};
204 $self->{original} = $param->{original} ? 1 : 0;
205 }
206
207 1;
208
209 __END__
210
211 =head1 PMG::RuleDB::BCC
212
213 Send BCC.