1 # This module is part of debbugs, and is released
2 # under the terms of the GPL version 2, or any later version. See the
3 # file README and COPYING for more information.
5 # [Other people have contributed to this file; their copyrights should
7 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
10 package Debbugs::CGI::Bugreport;
14 Debbugs::CGI::Bugreport -- specific routines for the bugreport cgi script
31 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
32 use Exporter qw(import);
35 use Params::Validate qw(validate_with :types);
36 use Digest::MD5 qw(md5_hex);
37 use Debbugs::Mail qw(get_addresses :reply);
38 use Debbugs::MIME qw(decode_rfc1522 create_mime_message);
39 use Debbugs::CGI qw(:url :html :util);
40 use Debbugs::Common qw(globify_scalar english_join);
42 use Debbugs::Config qw(:config);
43 use Debbugs::Log qw(:read);
44 use POSIX qw(strftime);
45 use Encode qw(decode_utf8 encode_utf8);
46 use URI::Escape qw(uri_escape_utf8);
47 use Scalar::Util qw(blessed);
48 use List::AllUtils qw(sum);
52 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
53 $DEBUG = 0 unless defined $DEBUG;
57 @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
58 Exporter::export_ok_tags(keys %EXPORT_TAGS);
59 $EXPORT_TAGS{all} = [@EXPORT_OK];
66 display_entity(entity => $entity,
70 attachments => \@attachments,
76 =item entity -- MIME::Parser entity
78 =item bug_num -- Bug number
80 =item outer -- Whether this is the outer entity; defaults to 1
82 =item msg_num -- message number in the log
84 =item attachments -- arrayref of attachments
86 =item output -- scalar reference for output
93 my %param = validate_with(params => \@_,
94 spec => {entity => {type => OBJECT,
96 bug_num => {type => SCALAR,
99 outer => {type => BOOLEAN,
102 msg_num => {type => SCALAR,
104 attachments => {type => ARRAYREF,
107 output => {type => SCALARREF|HANDLE,
110 terse => {type => BOOLEAN,
113 msg => {type => SCALAR,
116 att => {type => SCALAR,
119 trim_headers => {type => BOOLEAN,
122 avatars => {type => BOOLEAN,
128 my $output = globify_scalar($param{output});
129 my $entity = $param{entity};
130 my $ref = $param{bug_num};
131 my $xmessage = $param{msg_num};
132 my $attachments = $param{attachments};
134 my $head = $entity->head;
135 my $disposition = $head->mime_attr('content-disposition');
136 $disposition = 'inline' if not defined $disposition or $disposition eq '';
137 my $type = $entity->effective_type;
138 my $filename = $entity->head->recommended_filename;
139 $filename = '' unless defined $filename;
140 $filename = decode_rfc1522($filename);
142 if ($param{outer} and
143 not $param{terse} and
144 not exists $param{att}) {
145 print {$output} "<div class=\"headers\">\n";
146 if ($param{trim_headers}) {
148 foreach (qw(From To Cc Subject Date)) {
149 my $head_field = $head->get($_);
150 next unless defined $head_field and $head_field ne '';
152 if ($_ eq 'From' and $param{avatars}) {
153 my $libravatar_url = __libravatar_url(decode_rfc1522($head_field));
154 if (defined $libravatar_url and length $libravatar_url) {
155 push @headers,q(<img src=").html_escape($libravatar_url).qq(" alt="">\n);
158 push @headers, qq(<div class="header"><span class="headerfield">$_:</span> ) . html_escape(decode_rfc1522($head_field))."</div>\n";
160 print {$output} join(qq(), @headers);
162 print {$output} "<pre>".html_escape(decode_rfc1522($entity->head->stringify))."</pre>\n";
164 print {$output} "</div>\n";
167 if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
168 or $type =~ m{^multipart/}
170 push @$attachments, $param{entity};
171 # output this attachment
172 if (exists $param{att} and
173 $param{att} == $#$attachments) {
174 my $head = $entity->head;
175 chomp(my $type = $entity->effective_type);
176 my $body = $entity->stringify_body;
177 # this attachment has its own content type, so we must not
178 # try to convert it to UTF-8 or do anything funky.
179 binmode($output,':raw');
180 print {$output} "Content-Type: $type";
181 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
182 print {$output} qq(; charset="$charset") if defined $charset;
183 print {$output} "\n";
184 if ($filename ne '') {
188 print {$output} qq{Content-Disposition: inline; filename="$qf"\n};
190 print {$output} "\n";
191 my $decoder = MIME::Decoder->new($head->mime_encoding);
192 $decoder->decode(IO::Scalar->new(\$body), $output);
193 # we don't reset the layers here, because it makes no
194 # sense to add anything to the output handle after this
198 elsif (not exists $param{att}) {
199 my @dlargs = (msg=>$xmessage, att=>$#$attachments);
200 push @dlargs, (filename=>$filename) if $filename ne '';
201 my $printname = $filename;
202 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
203 print {$output} '<pre class="mime">[<a href="' .
204 html_escape(bug_links(bug => $ref,
206 options => {@dlargs})
207 ) . qq{">$printname</a> } .
208 "($type, $disposition)]</pre>\n";
212 return 0 if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
213 return 0 unless (($type =~ m[^text/?] and
214 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
215 $type =~ m[^application/pgp(?:;|$)] or
218 if ($entity->is_multipart) {
219 my @parts = $entity->parts;
220 foreach my $part (@parts) {
222 display_entity(entity => $part,
225 msg_num => $xmessage,
227 attachments => $attachments,
228 terse => $param{terse},
229 exists $param{msg}?(msg=>$param{msg}):(),
230 exists $param{att}?(att=>$param{att}):(),
231 exists $param{avatars}?(avatars=>$param{avatars}):(),
236 # print {$output} "\n";
238 } elsif ($entity->parts) {
239 # We must be dealing with a nested message.
240 if (not exists $param{att}) {
241 print {$output} "<blockquote>\n";
243 my @parts = $entity->parts;
244 foreach my $part (@parts) {
245 display_entity(entity => $part,
248 msg_num => $xmessage,
250 attachments => $attachments,
251 terse => $param{terse},
252 exists $param{msg}?(msg=>$param{msg}):(),
253 exists $param{att}?(att=>$param{att}):(),
254 exists $param{avatars}?(avatars=>$param{avatars}):(),
256 # print {$output} "\n";
258 if (not exists $param{att}) {
259 print {$output} "</blockquote>\n";
261 } elsif (not $param{terse}) {
262 my $content_type = $entity->head->get('Content-Type:') || "text/html";
263 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
264 my $body = $entity->bodyhandle->as_string;
265 $body = convert_to_utf8($body,$charset//'utf8');
266 $body = html_escape($body);
267 my $css_class = "message";
268 # Attempt to deal with format=flowed
269 if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
270 $body =~ s{^\ }{}mgo;
271 # we ignore the other things that you can do with
272 # flowed e-mails cause they don't really matter.
273 $css_class .= " flowed";
276 # if the message is composed entirely of lines which are separated by
277 # newlines, wrap it. [Allow the signature to have special formatting.]
278 if ($body =~ /^([^\n]+\n\n)*[^\n]*\n?(-- \n.+)*$/s or
279 # if the first 20 lines in the message which have any non-space
280 # characters are larger than 100 characters more often than they
281 # are not, then use CSS to try to impose sensible wrapping
282 sum(0,map {length ($_) > 100?1:-1} grep {/\S/} split /\n/,$body,20) > 0
284 $css_class .= " wrapping";
287 # We don't html escape here because we escape above;
288 # wierd terminators are because of that
289 $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
290 ((?:\>\;)?[)]?(?:'|\&\#39\;|\"\;)?[:.\,]?(?:\s|$)) # terminators
291 }{<a href=\"$1\">$1</a>$2}gox;
292 # Add links to bug closures
293 $body =~ s[((?:closes|see):\s* # start of closed/referenced bugs
294 (?:bug)?\#?\s?\d+\s? # first bug
295 (?:,?\s*(?:bug)?\#?\s?\d+)* # additional bugs
296 (?:\s|\n|\)|\]|\}|\.|\,|$)) # ends with a space, newline, end of string, or ); fixes #747267
300 {bug_links(bug=>$1)}ge;
302 if (defined $config{cve_tracker} and
303 length $config{cve_tracker}
305 # Add links to CVE vulnerabilities (closes #568464)
306 $body =~ s{(^|\s|[\(\[])(CVE-\d{4}-\d{4,})(\s|[,.-\[\]\)]|$)}
307 {$1<a href="$config{cve_tracker}$2">$2</a>$3}gxm;
309 if (not exists $param{att}) {
310 print {$output} qq(<pre class="$css_class">$body</pre>\n);
317 =head2 handle_email_message
319 handle_email_message($record->{text},
321 msg_num => $msg_number,
324 Returns a decoded e-mail message and displays entities/attachments as
330 sub handle_email_message{
331 my ($record,%param) = @_;
334 my $output_fh = globify_scalar(\$output);
337 if (not blessed $record) {
338 my $parser = MIME::Parser->new();
339 # this will be cleaned up once it goes out of scope
340 $tempdir = File::Temp->newdir();
341 $parser->output_under($tempdir->dirname());
342 if ($record->{inner_file}) {
343 $entity = $parser->parse($record->{fh}) or
344 die "Unable to parse entity";
346 $entity = $parser->parse_data($record->{text}) or
347 die "Unable to parse entity";
352 my @attachments = ();
354 display_entity(entity => $entity,
355 bug_num => $param{ref},
357 msg_num => $param{msg_num},
358 output => $output_fh,
359 attachments => \@attachments,
360 terse => $param{terse},
361 exists $param{msg}?(msg=>$param{msg}):(),
362 exists $param{att}?(att=>$param{att}):(),
363 exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
364 exists $param{avatars}?(avatars=>$param{avatars}):(),
366 return $raw_output?$output:decode_utf8($output);
371 push @log, handle_record($record,$ref,$msg_num);
373 Deals with a record in a bug log as returned by
374 L<Debbugs::Log::read_log_records>; returns the log information that
375 should be output to the browser.
380 my ($record,$bug_number,$msg_number,$seen_msg_ids,%param) = @_;
382 # output needs to have the is_utf8 flag on to avoid double
384 my $output = decode_utf8('');
385 local $_ = $record->{type};
387 # $record->{text} is not in perl's internal encoding; convert it
388 my $text = decode_rfc1522(decode_utf8(record_text($record)));
389 my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
390 my $class = $text =~ /^<strong>(?:Acknowledgement|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
392 # Link to forwarded http:// urls in the midst of the report
393 # (even though these links already exist at the top)
394 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;|\"\;)?
395 (?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,gxo;
396 # Add links to the cloned bugs
397 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
398 # Add links to merged bugs
399 $output =~ s{(?<=Merged )([\d\s]+)(?=[\.<])}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
400 # Add links to blocked bugs
401 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
402 {(defined $2?$1.bug_links(bug=>$2):'').$3.
403 english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
404 $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
405 (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
406 {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
407 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
408 $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks ?)([\d\s\,]+)}
409 {$1.$2.(bug_links(bug=>$3)).$4.
410 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
411 # Add links to reassigned packages
412 $output =~ s{($config{bug}\sreassigned\sfrom\spackage\s(?:[\`']|\&\#39;))([^']+?)((?:'|\&\#39;|\"\;)
413 \sto\s(?:[\`']|\&\#39;|\"\;))([^']+?)((?:'|\&\#39;|\"\;))}
414 {$1.package_links(package=>$2).$3.
415 package_links(package=>$4).$5}exo;
417 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
419 $output .= qq{(<a href="} .
420 html_escape(bug_links(bug => $bug_number,
421 options => {msg => ($msg_number+1)},
424 ) . '">full text</a>, <a href="' .
425 html_escape(bug_links(bug => $bug_number,
426 options => {msg => ($msg_number+1),
430 qq{<a href="#$msg_number">link</a>).</p>};
432 $output = qq(<div class="$class"><hr><p>\n<a name="$msg_number"></a>\n) . $output . "</p></div>\n";
435 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/i);
436 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
439 elsif (defined $msg_id) {
440 $$seen_msg_ids{$msg_id} = 1;
442 return () if defined $param{spam} and $param{spam}->is_spam($msg_id);
443 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number" href="#$msg_number">🔗</a>\n);
444 $output .= 'View this message in <a href="' . html_escape(bug_links(bug=>$bug_number, links_only => 1, options=>{msg=>$msg_number, mbox=>'yes'})) . '">rfc822 format</a></p>';
445 $output .= handle_email_message($record,
447 msg_num => $msg_number,
451 elsif (/autocheck/) {
454 elsif (/incoming-recv/) {
455 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/i);
456 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
459 elsif (defined $msg_id) {
460 $$seen_msg_ids{$msg_id} = 1;
462 return () if defined $param{spam} and $param{spam}->is_spam($msg_id);
463 # Incomming Mail Message
464 my ($received,$hostname) = record_regex($record,qr/Received: \(at (\S+)\) by (\S+)\;/o);
465 $output .= qq|<hr><p class="msgreceived"><a name="$msg_number"></a><a name="msg$msg_number"></a><a href="#$msg_number">Message #$msg_number</a> received at |.
466 html_escape("$received\@$hostname") .
467 q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
468 q|, <a href="| . html_escape(bug_links(bug => $bug_number,
470 options => {msg=>$msg_number,
474 my $parser = MIME::Parser->new();
476 # this will be cleaned up once it goes out of scope
477 my $tempdir = File::Temp->newdir();
478 $parser->output_under($tempdir->dirname());
479 $parser->filer->ignore_filename(1);
481 if ($record->{inner_file}) {
482 $entity = $parser->parse($record->{fh});
484 $entity = $parser->parse_data($record->{text});
486 my $r_l = reply_headers($entity);
487 $output .= q(<a href=").
488 html_escape('mailto:'.$bug_number.'@'.$config{email_domain}.'?'.
489 join('&',map {defined $r_l->{$_}?$_.'='.uri_escape_utf8($r_l->{$_}):()} keys %{$r_l})).
492 $output .= ')'.":</p>\n";
493 $output .= handle_email_message($entity,
495 msg_num => $msg_number,
500 die "Unknown record type $_";
506 sub __libravatar_url {
508 if (not defined $config{libravatar_uri} or not length $config{libravatar_uri}) {
511 ($email) = grep {/\@/} get_addresses($email);
512 return $config{libravatar_uri}.uri_escape_utf8($email.($config{libravatar_uri_options}//''));
521 # cperl-indent-level: 4
522 # indent-tabs-mode: nil