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