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 parse_to_mime_entity);
39 use Debbugs::CGI qw(:url :html :util);
40 use Debbugs::Common qw(globify_scalar english_join hash_slice);
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 hash_slice(%param,qw(msg att avatars)),
234 # print {$output} "\n";
236 } elsif ($entity->parts) {
237 # We must be dealing with a nested message.
238 if (not exists $param{att}) {
239 print {$output} "<blockquote>\n";
241 my @parts = $entity->parts;
242 foreach my $part (@parts) {
243 display_entity(entity => $part,
246 msg_num => $xmessage,
248 attachments => $attachments,
249 terse => $param{terse},
250 hash_slice(%param,qw(msg att avatars)),
252 # print {$output} "\n";
254 if (not exists $param{att}) {
255 print {$output} "</blockquote>\n";
257 } elsif (not $param{terse}) {
258 my $content_type = $entity->head->get('Content-Type:') || "text/html";
259 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
260 my $body = $entity->bodyhandle->as_string;
261 $body = convert_to_utf8($body,$charset//'utf8');
262 $body = html_escape($body);
263 my $css_class = "message";
264 # Attempt to deal with format=flowed
265 if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
266 $body =~ s{^\ }{}mgo;
267 # we ignore the other things that you can do with
268 # flowed e-mails cause they don't really matter.
269 $css_class .= " flowed";
272 # if the message is composed entirely of lines which are separated by
273 # newlines, wrap it. [Allow the signature to have special formatting.]
274 if ($body =~ /^([^\n]+\n\n)*[^\n]*\n?(-- \n.+)*$/s or
275 # if the first 20 lines in the message which have any non-space
276 # characters are larger than 100 characters more often than they
277 # are not, then use CSS to try to impose sensible wrapping
278 sum(0,map {length ($_) > 100?1:-1} grep {/\S/} split /\n/,$body,20) > 0
280 $css_class .= " wrapping";
283 # We don't html escape here because we escape above;
284 # wierd terminators are because of that
285 $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
286 ((?:\>\;)?[)]?(?:'|\&\#39\;|\"\;)?[:.\,]?(?:\s|$)) # terminators
287 }{<a href=\"$1\">$1</a>$2}gox;
288 # Add links to bug closures
289 $body =~ s[((?:closes|see):\s* # start of closed/referenced bugs
290 (?:bug)?\#?\s?\d+\s? # first bug
291 (?:,?\s*(?:bug)?\#?\s?\d+)* # additional bugs
292 (?:\s|\n|\)|\]|\}|\.|\,|$)) # ends with a space, newline, end of string, or ); fixes #747267
296 {bug_links(bug=>$1)}ge;
298 if (defined $config{cve_tracker} and
299 length $config{cve_tracker}
301 # Add links to CVE vulnerabilities (closes #568464)
302 $body =~ s{(^|\s|[\(\[])(CVE-\d{4}-\d{4,})(\s|[,.-\[\]\)]|$)}
303 {$1<a href="$config{cve_tracker}$2">$2</a>$3}gxm;
305 if (not exists $param{att}) {
306 print {$output} qq(<pre class="$css_class">$body</pre>\n);
313 =head2 handle_email_message
315 handle_email_message($record->{text},
317 msg_num => $msg_number,
320 Returns a decoded e-mail message and displays entities/attachments as
326 sub handle_email_message{
327 my ($record,%param) = @_;
330 my $output_fh = globify_scalar(\$output);
333 if (not blessed $record) {
334 $entity = parse_to_mime_entity($record);
338 my @attachments = ();
340 display_entity(entity => $entity,
341 bug_num => $param{ref},
343 msg_num => $param{msg_num},
344 output => $output_fh,
345 attachments => \@attachments,
346 terse => $param{terse},
347 hash_slice(%param,qw(msg att trim_headers avatars),
350 return $raw_output?$output:decode_utf8($output);
355 push @log, handle_record($record,$ref,$msg_num);
357 Deals with a record in a bug log as returned by
358 L<Debbugs::Log::read_log_records>; returns the log information that
359 should be output to the browser.
364 my ($record,$bug_number,$msg_number,$seen_msg_ids,%param) = @_;
366 # output needs to have the is_utf8 flag on to avoid double
368 my $output = decode_utf8('');
369 local $_ = $record->{type};
371 # $record->{text} is not in perl's internal encoding; convert it
372 my $text = decode_rfc1522(decode_utf8(record_text($record)));
373 my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
374 my $class = $text =~ /^<strong>(?:Acknowledgement|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
376 # Link to forwarded http:// urls in the midst of the report
377 # (even though these links already exist at the top)
378 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;|\"\;)?
379 (?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,gxo;
380 # Add links to the cloned bugs
381 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
382 # Add links to merged bugs
383 $output =~ s{(?<=Merged )([\d\s]+)(?=[\.<])}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
384 # Add links to blocked bugs
385 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
386 {(defined $2?$1.bug_links(bug=>$2):'').$3.
387 english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
388 $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
389 (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
390 {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
391 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
392 $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks ?)([\d\s\,]+)}
393 {$1.$2.(bug_links(bug=>$3)).$4.
394 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
395 # Add links to reassigned packages
396 $output =~ s{($config{bug}\sreassigned\sfrom\spackage\s(?:[\`']|\&\#39;))([^']+?)((?:'|\&\#39;|\"\;)
397 \sto\s(?:[\`']|\&\#39;|\"\;))([^']+?)((?:'|\&\#39;|\"\;))}
398 {$1.package_links(package=>$2).$3.
399 package_links(package=>$4).$5}exo;
401 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
403 $output .= qq{(<a href="} .
404 html_escape(bug_links(bug => $bug_number,
405 options => {msg => ($msg_number+1)},
408 ) . '">full text</a>, <a href="' .
409 html_escape(bug_links(bug => $bug_number,
410 options => {msg => ($msg_number+1),
414 qq{<a href="#$msg_number">link</a>).</p>};
416 $output = qq(<div class="$class"><hr><p>\n<a name="$msg_number"></a>\n) . $output . "</p></div>\n";
419 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/i);
420 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
423 elsif (defined $msg_id) {
424 $$seen_msg_ids{$msg_id} = 1;
426 return () if defined $param{spam} and $param{spam}->is_spam($msg_id);
427 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number" href="#$msg_number">🔗</a>\n);
428 $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>';
429 $output .= handle_email_message($record,
431 msg_num => $msg_number,
435 elsif (/autocheck/) {
438 elsif (/incoming-recv/) {
439 my ($msg_id) = record_regex($record,qr/^Message-Id:\s+<(.+)>/i);
440 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
443 elsif (defined $msg_id) {
444 $$seen_msg_ids{$msg_id} = 1;
446 return () if defined $param{spam} and $param{spam}->is_spam($msg_id);
447 # Incomming Mail Message
448 my ($received,$hostname) = record_regex($record,qr/Received: \(at (\S+)\) by (\S+)\;/o);
449 $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 |.
450 html_escape("$received\@$hostname") .
451 q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
452 q|, <a href="| . html_escape(bug_links(bug => $bug_number,
454 options => {msg=>$msg_number,
458 my $parser = MIME::Parser->new();
460 # this will be cleaned up once it goes out of scope
461 my $tempdir = File::Temp->newdir();
462 $parser->output_under($tempdir->dirname());
463 $parser->filer->ignore_filename(1);
465 if ($record->{inner_file}) {
466 $entity = $parser->parse($record->{fh});
468 $entity = $parser->parse_data($record->{text});
470 my $r_l = reply_headers($entity);
471 $output .= q(<a href=").
472 html_escape('mailto:'.$bug_number.'@'.$config{email_domain}.'?'.
473 join('&',map {defined $r_l->{$_}?$_.'='.uri_escape_utf8($r_l->{$_}):()} keys %{$r_l})).
476 $output .= ')'.":</p>\n";
477 $output .= handle_email_message($entity,
479 msg_num => $msg_number,
484 die "Unknown record type $_";
490 sub __libravatar_url {
492 if (not defined $config{libravatar_uri} or not length $config{libravatar_uri}) {
495 ($email) = grep {/\@/} get_addresses($email);
496 return $config{libravatar_uri}.uri_escape_utf8($email.($config{libravatar_uri_options}//''));
505 # cperl-indent-level: 4
506 # indent-tabs-mode: nil