]> git.proxmox.com Git - pmg-api.git/blob - PMG/RuleDB/Disclaimer.pm
fix otype attribute
[pmg-api.git] / PMG / RuleDB / Disclaimer.pm
1 package PMG::RuleDB::Disclaimer;
2
3 use strict;
4 use warnings;
5 use Carp;
6 use DBI;
7 use Digest::SHA;
8 use HTML::Parser;
9 use HTML::Entities;
10 use MIME::Body;
11 use IO::File;
12 use Encode;
13
14 use PMG::Utils;
15 use PMG::ModGroup;
16 use PMG::RuleDB::Object;;
17
18 use base qw(PMG::RuleDB::Object);
19
20 sub otype {
21 return 4009;
22 }
23
24 sub oclass {
25 return 'action';
26 }
27
28 sub otype_text {
29 return 'Disclaimer';
30 }
31
32 sub oicon {
33 return 'attach.gif';
34 }
35
36 sub oconfigsite {
37 return '';
38 }
39
40 sub oinfo {
41 return 'Add a Disclaimer';
42 }
43
44 sub oisedit {
45 return 1;
46 }
47
48 sub final {
49 return 0;
50 }
51
52 sub priority {
53 return 49;
54 }
55
56 my $std_discl = <<_EOD_;
57 This e-mail and any attached files are confidential and may be legally privileged. If you are not the addressee, any disclosure, reproduction, copying, distribution, or other dissemination or use of this communication is strictly prohibited. If you have received this transmission in error please notify the sender immediately and then delete this mail.<br>
58 E-mail transmission cannot be guaranteed to be secure or error free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses. The sender therefore does not accept liability for any errors or omissions in the contents of this message which arise as a result of e-mail transmission or changes to transmitted date not specifically approved by the sender.<br>
59 If this e-mail or attached files contain information which do not relate to our professional activity we do not accept liability for such information.
60 _EOD_
61
62 sub new {
63 my ($type, $value, $ogroup) = @_;
64
65 my $class = ref($type) || $type;
66
67 $value //= $std_discl;
68
69 my $self = $class->SUPER::new($class->otype(), $ogroup);
70
71 $self->{value} = $value;
72
73 return $self;
74 }
75
76 sub load_attr {
77 my ($type, $ruledb, $id, $ogroup, $value) = @_;
78
79 my $class = ref($type) || $type;
80
81 defined($value) || croak "undefined object attribute: ERROR";
82
83 my $obj = $class->new($value, $ogroup);
84
85 $obj->{id} = $id;
86
87 $obj->{digest} = Digest::SHA::sha1_hex($id, $value, $ogroup);
88
89 return $obj;
90 }
91
92 sub save {
93 my ($self, $ruledb) = @_;
94
95 defined($self->{ogroup}) || croak "undefined object attribute: ERROR";
96 defined($self->{value}) || croak "undefined object attribute: ERROR";
97
98 if (defined ($self->{id})) {
99 # update
100
101 $ruledb->{dbh}->do(
102 "UPDATE Object SET Value = ? WHERE ID = ?",
103 undef, $self->{value}, $self->{id});
104
105 } else {
106 # insert
107
108 my $sth = $ruledb->{dbh}->prepare(
109 "INSERT INTO Object (Objectgroup_ID, ObjectType, Value) " .
110 "VALUES (?, ?, ?);");
111
112 $sth->execute($self->ogroup, $self->otype, $self->{value});
113
114 $self->{id} = PMG::Utils::lastid($ruledb->{dbh}, 'object_id_seq');
115 }
116
117 return $self->{id};
118 }
119
120 sub add_data {
121 my ($self, $entity, $data) = @_;
122
123 $entity->bodyhandle || return undef;
124
125 my $fh;
126
127 # always use the decoded data
128 if (my $path = $entity->{PMX_decoded_path}) {
129 $fh = IO::File->new("<$path");
130 } else {
131 $fh = $entity->open("r");
132 }
133
134 return undef if !$fh;
135
136 # in memory (we cant modify the file, because
137 # a.) that would modify all entities (see ModGroup)
138 # b.) bad performance
139 my $body = new MIME::Body::InCore || return undef;
140
141 my $newfh = $body->open ("w") || return undef;
142
143 while (defined($_ = $fh->getline())) {
144 $newfh->print($_); # copy contents
145 }
146
147 $newfh->print("\n"); # add final \n
148
149 $newfh->print($data);
150
151 $newfh->close || return undef;
152
153 $entity->bodyhandle($body);
154
155 return 1;
156 }
157
158 sub sign {
159 my ($self, $entity, $html, $text) = @_;
160
161 my $found = 0;
162
163 if ($entity->head->mime_type =~ m{multipart/alternative}) {
164 foreach my $p ($entity->parts) {
165 $found = 1 if $self->sign ($p, $html, $text);
166 }
167 } elsif ($entity->head->mime_type =~ m{multipart/}) {
168 foreach my $p ($entity->parts) {
169 if ($self->sign ($p, $html, $text)) {
170 $found = 1;
171 last;
172 }
173 }
174 } elsif ($entity->head->mime_type =~ m{text/}) {
175 if ($entity->head->mime_type =~ m{text/html}) {
176 $self->add_data ($entity, $html);
177 $found = 1;
178 } elsif ($entity->head->mime_type =~ m{text/plain}) {
179 my $cs = $entity->head->mime_attr("content-type.charset");
180 eval {
181 my $enc_text = encode($cs, $text, Encode::FB_CROAK);
182 $self->add_data($entity, $enc_text);
183 };
184 # simply ignore if we can't represent the disclainer
185 # with that encoding
186 $found = 1;
187 } else {
188 # do nothing - unknown format
189 }
190 }
191
192 return $found;
193 }
194
195 sub execute {
196 my ($self, $queue, $ruledb, $mod_group, $targets,
197 $msginfo, $vars, $marks) = @_;
198
199 my $subgroups = $mod_group->subgroups($targets);
200
201 foreach my $ta (@$subgroups) {
202 my ($tg, $entity) = (@$ta[0], @$ta[1]);
203
204 my $html = "<br>--<br>" . PMG::Utils::subst_values ($self->{value}, $vars);
205
206 my $text = "";
207 my $parser = HTML::Parser->new(
208 api_version => 3, text_h => [ sub {$text .= shift;}, "dtext" ]);
209
210 my $tmp = $html;
211 $tmp =~ s/\r?\n//g;
212 $tmp =~ s/<br>/\n/g;
213
214 $parser->parse($tmp);
215 $parser->eof;
216
217 $self->sign($entity, "$html\n", "$text\n");
218
219 return;
220 }
221 }
222
223 sub short_desc {
224 my $self = shift;
225
226 return "disclaimer";
227 }
228
229
230 1;
231
232 __END__
233
234 =head1 PMG::RuleDB::Disclaimer
235
236 Add disclaimer.