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
30 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
31 use base qw(Exporter);
34 use Params::Validate qw(validate_with :types);
35 use Digest::MD5 qw(md5_hex);
36 use Debbugs::Mail qw(get_addresses :reply);
37 use Debbugs::MIME qw(decode_rfc1522 create_mime_message);
38 use Debbugs::CGI qw(:url :html :util);
39 use Debbugs::Common qw(globify_scalar english_join);
41 use Debbugs::Config qw(:config);
42 use POSIX qw(strftime);
43 use Encode qw(decode_utf8 encode_utf8);
44 use URI::Escape qw(uri_escape_utf8);
45 use Scalar::Util qw(blessed);
48 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
49 $DEBUG = 0 unless defined $DEBUG;
53 @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
54 Exporter::export_ok_tags(keys %EXPORT_TAGS);
55 $EXPORT_TAGS{all} = [@EXPORT_OK];
62 display_entity(entity => $entity,
66 attachments => \@attachments,
72 =item entity -- MIME::Parser entity
74 =item bug_num -- Bug number
76 =item outer -- Whether this is the outer entity; defaults to 1
78 =item msg_num -- message number in the log
80 =item attachments -- arrayref of attachments
82 =item output -- scalar reference for output
89 my %param = validate_with(params => \@_,
90 spec => {entity => {type => OBJECT,
92 bug_num => {type => SCALAR,
95 outer => {type => BOOLEAN,
98 msg_num => {type => SCALAR,
100 attachments => {type => ARRAYREF,
103 output => {type => SCALARREF|HANDLE,
106 terse => {type => BOOLEAN,
109 msg => {type => SCALAR,
112 att => {type => SCALAR,
115 trim_headers => {type => BOOLEAN,
118 avatars => {type => BOOLEAN,
124 my $output = globify_scalar($param{output});
125 my $entity = $param{entity};
126 my $ref = $param{bug_num};
127 my $top = $param{outer};
128 my $xmessage = $param{msg_num};
129 my $attachments = $param{attachments};
131 my $head = $entity->head;
132 my $disposition = $head->mime_attr('content-disposition');
133 $disposition = 'inline' if not defined $disposition or $disposition eq '';
134 my $type = $entity->effective_type;
135 my $filename = $entity->head->recommended_filename;
136 $filename = '' unless defined $filename;
137 $filename = decode_rfc1522($filename);
139 if ($param{outer} and
140 not $param{terse} and
141 not exists $param{att}) {
142 print {$output} "<div class=\"headers\">\n";
143 if ($param{trim_headers}) {
145 foreach (qw(From To Cc Subject Date)) {
146 my $head_field = $head->get($_);
147 next unless defined $head_field and $head_field ne '';
149 if ($_ eq 'From' and $param{avatars}) {
150 my $libravatar_url = __libravatar_url(decode_rfc1522($head_field));
151 if (defined $libravatar_url and length $libravatar_url) {
152 push @headers,q(<img src=").html_escape($libravatar_url).qq(" alt="">\n);
155 push @headers, qq(<div class="header"><span class="headerfield">$_:</span> ) . html_escape(decode_rfc1522($head_field))."</div>\n";
157 print {$output} join(qq(), @headers);
159 print {$output} "<pre>".html_escape(decode_rfc1522($entity->head->stringify))."</pre>\n";
161 print {$output} "</div>\n";
164 if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
165 or $type =~ m{^multipart/}
167 push @$attachments, $param{entity};
168 # output this attachment
169 if (exists $param{att} and
170 $param{att} == $#$attachments) {
171 my $head = $entity->head;
172 chomp(my $type = $entity->effective_type);
173 my $body = $entity->stringify_body;
174 # this attachment has its own content type, so we must not
175 # try to convert it to UTF-8 or do anything funky.
176 binmode($output,':raw');
177 print {$output} "Content-Type: $type";
178 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
179 print {$output} qq(; charset="$charset") if defined $charset;
180 print {$output} "\n";
181 if ($filename ne '') {
185 print {$output} qq{Content-Disposition: inline; filename="$qf"\n};
187 print {$output} "\n";
188 my $decoder = MIME::Decoder->new($head->mime_encoding);
189 $decoder->decode(IO::Scalar->new(\$body), $output);
190 # we don't reset the layers here, because it makes no
191 # sense to add anything to the output handle after this
195 elsif (not exists $param{att}) {
196 my @dlargs = (msg=>$xmessage, att=>$#$attachments);
197 push @dlargs, (filename=>$filename) if $filename ne '';
198 my $printname = $filename;
199 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
200 print {$output} '<pre class="mime">[<a href="' .
201 html_escape(bug_links(bug => $ref,
203 options => {@dlargs})
204 ) . qq{">$printname</a> } .
205 "($type, $disposition)]</pre>\n";
209 return 0 if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
210 return 0 unless (($type =~ m[^text/?] and
211 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
212 $type =~ m[^application/pgp(?:;|$)] or
215 if ($entity->is_multipart) {
216 my @parts = $entity->parts;
217 foreach my $part (@parts) {
219 display_entity(entity => $part,
222 msg_num => $xmessage,
224 attachments => $attachments,
225 terse => $param{terse},
226 exists $param{msg}?(msg=>$param{msg}):(),
227 exists $param{att}?(att=>$param{att}):(),
228 exists $param{avatars}?(avatars=>$param{avatars}):(),
233 # print {$output} "\n";
235 } elsif ($entity->parts) {
236 # We must be dealing with a nested message.
237 if (not exists $param{att}) {
238 print {$output} "<blockquote>\n";
240 my @parts = $entity->parts;
241 foreach my $part (@parts) {
242 display_entity(entity => $part,
245 msg_num => $xmessage,
247 attachments => $attachments,
248 terse => $param{terse},
249 exists $param{msg}?(msg=>$param{msg}):(),
250 exists $param{att}?(att=>$param{att}):(),
251 exists $param{avatars}?(avatars=>$param{avatars}):(),
253 # print {$output} "\n";
255 if (not exists $param{att}) {
256 print {$output} "</blockquote>\n";
258 } elsif (not $param{terse}) {
259 my $content_type = $entity->head->get('Content-Type:') || "text/html";
260 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
261 my $body = $entity->bodyhandle->as_string;
262 $body = convert_to_utf8($body,$charset//'utf8');
263 $body = html_escape($body);
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.
271 # We don't html escape here because we escape above;
272 # wierd terminators are because of that
273 $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
274 ((?:\>\;)?[)]?(?:'|\&\#39\;|\"\;)?[:.\,]?(?:\s|$)) # terminators
275 }{<a href=\"$1\">$1</a>$2}gox;
276 # Add links to bug closures
277 $body =~ s[((?:closes|see):\s* # start of closed/referenced bugs
278 (?:bug)?\#?\s?\d+\s? # first bug
279 (?:,?\s*(?:bug)?\#?\s?\d+)* # additional bugs
280 (?:\s|\n|\)|\]|\}|\.|\,|$)) # ends with a space, newline, end of string, or ); fixes #747267
284 {bug_links(bug=>$1)}ge;
286 if (defined $config{cve_tracker} and
287 length $config{cve_tracker}
289 # Add links to CVE vulnerabilities (closes #568464)
290 $body =~ s{(^|\s|[\(\[])(CVE-\d{4}-\d{4,})(\s|[,.-\[\]\)]|$)}
291 {$1<a href="http://$config{cve_tracker}$2">$2</a>$3}gxm;
293 if (not exists $param{att}) {
294 print {$output} qq(<pre class="message">$body</pre>\n);
301 =head2 handle_email_message
303 handle_email_message($record->{text},
305 msg_num => $msg_number,
308 Returns a decoded e-mail message and displays entities/attachments as
314 sub handle_email_message{
315 my ($email,%param) = @_;
318 my $output_fh = globify_scalar(\$output);
320 if (not blessed $email) {
321 my $parser = MIME::Parser->new();
322 # Because we are using memory, not tempfiles, there's no need to
323 # clean up here like in Debbugs::MIME
324 $parser->tmp_to_core(1);
325 $parser->output_to_core(1);
326 $entity = $parser->parse_data( $email);
330 my @attachments = ();
332 display_entity(entity => $entity,
333 bug_num => $param{ref},
335 msg_num => $param{msg_num},
336 output => $output_fh,
337 attachments => \@attachments,
338 terse => $param{terse},
339 exists $param{msg}?(msg=>$param{msg}):(),
340 exists $param{att}?(att=>$param{att}):(),
341 exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
342 exists $param{avatars}?(avatars=>$param{avatars}):(),
344 return $raw_output?$output:decode_utf8($output);
349 push @log, handle_record($record,$ref,$msg_num);
351 Deals with a record in a bug log as returned by
352 L<Debbugs::Log::read_log_records>; returns the log information that
353 should be output to the browser.
358 my ($record,$bug_number,$msg_number,$seen_msg_ids,%param) = @_;
360 # output needs to have the is_utf8 flag on to avoid double
362 my $output = decode_utf8('');
363 local $_ = $record->{type};
365 # $record->{text} is not in perl's internal encoding; convert it
366 my $text = decode_rfc1522(decode_utf8($record->{text}));
367 my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
368 my $class = $text =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
370 # Link to forwarded http:// urls in the midst of the report
371 # (even though these links already exist at the top)
372 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;|\"\;)?
373 (?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,gxo;
374 # Add links to the cloned bugs
375 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
376 # Add links to merged bugs
377 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
378 # Add links to blocked bugs
379 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
380 {(defined $2?$1.bug_links(bug=>$2):'').$3.
381 english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
382 $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
383 (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
384 {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
385 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
386 $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks )([\d\s\,]+)}
387 {$1.$2.(bug_links(bug=>$3)).$4.
388 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
389 # Add links to reassigned packages
390 $output =~ s{(Bug\sreassigned\sfrom\spackage\s(?:[\`']|\&\#39;))([^']+?)((?:'|\&\#39;|\"\;)
391 \sto\s(?:[\`']|\&\#39;|\"\;))([^']+?)((?:'|\&\#39;|\"\;))}
392 {$1.q(<a href=").html_escape(package_links(package=>$2)).qq(">$2</a>).$3.
393 q(<a href=").html_escape(package_links(package=>$4)).qq(">$4</a>).$5}exo;
395 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
397 $output .= '<a href="' .
398 html_escape(bug_links(bug => $bug_number,
399 options => {msg => ($msg_number+1)},
402 ) . '">Full text</a> and <a href="' .
403 html_escape(bug_links(bug => $bug_number,
404 options => {msg => ($msg_number+1),
407 ) . '">rfc822 format</a> available.';
409 $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
412 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
413 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
416 elsif (defined $msg_id) {
417 $$seen_msg_ids{$msg_id} = 1;
419 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
420 $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>';
421 $output .= handle_email_message($record->{text},
423 msg_num => $msg_number,
427 elsif (/autocheck/) {
430 elsif (/incoming-recv/) {
431 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
432 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
435 elsif (defined $msg_id) {
436 $$seen_msg_ids{$msg_id} = 1;
438 # Incomming Mail Message
439 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
440 $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 |.
441 html_escape("$received\@$hostname") .
442 q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
443 q|, <a href="| . html_escape(bug_links(bug => $bug_number,
445 options => {msg=>$msg_number,
449 my $parser = MIME::Parser->new();
450 # Because we are using memory, not tempfiles, there's no need to
451 # clean up here like in Debbugs::MIME
452 $parser->tmp_to_core(1);
453 $parser->output_to_core(1);
454 my $entity = $parser->parse_data($record->{text});
455 my $r_l = reply_headers($entity);
456 $output .= q(<a href=").
457 html_escape('mailto:'.$bug_number.'@'.$config{email_domain}.'?'.
458 join('&',map {defined $r_l->{$_}?$_.'='.uri_escape_utf8($r_l->{$_}):()} keys %{$r_l})).
461 $output .= ')'.":</p>\n";
462 $output .= handle_email_message($entity,
464 msg_num => $msg_number,
469 die "Unknown record type $_";
475 sub __libravatar_url {
477 if (not defined $config{libravatar_uri} or not length $config{libravatar_uri}) {
480 ($email) = grep {/\@/} get_addresses($email);
481 return $config{libravatar_uri}.uri_escape_utf8($email.($config{libravatar_uri_options}//''));