]> git.proxmox.com Git - pmg-api.git/blame - PMG/RuleDB/BCC.pm
TimeFrame.pm: use H:i format
[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
27sub oicon {
28 return 'bcc.gif';
29}
30
f4fe6fc4
DM
31sub oisedit {
32 return 1;
33}
34
35sub final {
36 return 0;
37}
38
39sub priority {
40 return 80;
41}
42
43sub 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
59sub 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
78sub 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
115sub 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
166sub short_desc {
167 my $self = shift;
168
169 return "send bcc to: $self->{target}";
170}
171
1721;
173
174__END__
175
176=head1 PMG::RuleDB::BCC
177
178Send BCC.