]> git.proxmox.com Git - pmg-api.git/blame - PMG/RuleDB/BCC.pm
remove sub oicon() - no longer needed
[pmg-api.git] / PMG / RuleDB / BCC.pm
CommitLineData
f4fe6fc4
DM
1package PMG::RuleDB::BCC;
2
3use strict;
4use warnings;
f4fe6fc4
DM
5use DBI;
6
f4fe6fc4
DM
7use PVE::SafeSyslog;
8
9use PMG::Utils;
10use PMG::ModGroup;
11use PMG::RuleDB::Object;
12
13use base qw(PMG::RuleDB::Object);
14
15sub otype {
16 return 4005;
17}
18
19sub oclass {
20 return 'action';
21}
22
23sub otype_text {
24 return 'BCC';
25}
26
f4fe6fc4
DM
27sub oisedit {
28 return 1;
29}
30
31sub final {
32 return 0;
33}
34
35sub priority {
36 return 80;
37}
38
39sub new {
40 my ($type, $target, $original, $ogroup) = @_;
41
42 my $class = ref($type) || $type;
43
7a2cf7e6 44 my $self = $class->SUPER::new($class->otype(), $ogroup);
f4fe6fc4
DM
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
55sub 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
74sub save {
75 my ($self, $ruledb) = @_;
76
9ef3f143
DM
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";
f4fe6fc4
DM
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
111sub 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 $bcc_to = PMG::Utils::subst_values($self->{target}, $vars);
118
119 if ($bcc_to =~ m/^\s*$/) {
120 # this happens if a notification is triggered by bounce mails
121 # which notifies the sender <> - we just log and then ignore it
122 syslog('info', "%s: bcc to <> (ignored)", $queue->{logid});
123 return;
124 }
125
126 my @bcc_targets = split (/\s*,\s*/, $bcc_to);
127
128 if ($self->{original}) {
129 $subgroups = [[\@bcc_targets, $mod_group->{entity}]];
130 }
131
132 foreach my $ta (@$subgroups) {
133 my ($tg, $entity) = (@$ta[0], @$ta[1]);
134
135 $entity = $entity->dup();
3b05bbe6 136 PMG::Utils::remove_marks($entity);
f4fe6fc4
DM
137
138 if ($msginfo->{testmode}) {
139 my $fh = $msginfo->{test_fh};
140 print $fh "bcc from: $msginfo->{sender}\n";
141 printf $fh "bcc to: %s\n", join (',', @$tg);
142 print $fh "bcc content:\n";
143 $entity->print ($fh);
144 print $fh "bcc end\n";
145 } else {
146 my $qid = PMG::Utils::reinject_mail(
147 $entity, $msginfo->{sender}, \@bcc_targets,
148 $msginfo->{xforward}, $msginfo->{fqdn}, 1);
149 foreach (@bcc_targets) {
150 if ($qid) {
151 syslog('info', "%s: bcc to <%s> (%s)", $queue->{logid}, $_, $qid);
152 } else {
153 syslog('err', "%s: bcc to <%s> failed", $queue->{logid}, $_);
154 }
155 }
156 }
157 }
158
159 # warn if no subgroups
160}
161
162sub short_desc {
163 my $self = shift;
164
11a86e67
DM
165 my $descr = "send bcc to: $self->{target}";
166
167 $descr .= " (original)" if $self->{original};
168
169 return $descr;
170}
171
172sub properties {
173 my ($class) = @_;
174
175 return {
176 target => {
177 description => "Send a Blind Carbon Copy to this email address.",
178 type => 'string', format => 'email',
179 },
180 original =>{
181 description => "Send the original, unmodified mail.",
182 type => 'boolean',
59434740 183 optional => 1,
11a86e67
DM
184 default => 1,
185 },
186 };
187}
188
189sub get {
190 my ($self) = @_;
191
192 return {
193 target => $self->{target},
194 original => $self->{original},
195 };
196}
197
198sub update {
199 my ($self, $param) = @_;
200
201 $self->{target} = $param->{target};
202 $self->{original} = $param->{original} ? 1 : 0;
f4fe6fc4
DM
203}
204
2051;
206
207__END__
208
209=head1 PMG::RuleDB::BCC
210
211Send BCC.