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);
49 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
50 $DEBUG = 0 unless defined $DEBUG;
54 @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
55 Exporter::export_ok_tags(keys %EXPORT_TAGS);
56 $EXPORT_TAGS{all} = [@EXPORT_OK];
63 display_entity(entity => $entity,
67 attachments => \@attachments,
73 =item entity -- MIME::Parser entity
75 =item bug_num -- Bug number
77 =item outer -- Whether this is the outer entity; defaults to 1
79 =item msg_num -- message number in the log
81 =item attachments -- arrayref of attachments
83 =item output -- scalar reference for output
90 my %param = validate_with(params => \@_,
91 spec => {entity => {type => OBJECT,
93 bug_num => {type => SCALAR,
96 outer => {type => BOOLEAN,
99 msg_num => {type => SCALAR,
101 attachments => {type => ARRAYREF,
104 output => {type => SCALARREF|HANDLE,
107 terse => {type => BOOLEAN,
110 msg => {type => SCALAR,
113 att => {type => SCALAR,
116 trim_headers => {type => BOOLEAN,
119 avatars => {type => BOOLEAN,
125 my $output = globify_scalar($param{output});
126 my $entity = $param{entity};
127 my $ref = $param{bug_num};
128 my $top = $param{outer};
129 my $xmessage = $param{msg_num};
130 my $attachments = $param{attachments};
132 my $head = $entity->head;
133 my $disposition = $head->mime_attr('content-disposition');
134 $disposition = 'inline' if not defined $disposition or $disposition eq '';
135 my $type = $entity->effective_type;
136 my $filename = $entity->head->recommended_filename;
137 $filename = '' unless defined $filename;
138 $filename = decode_rfc1522($filename);
140 if ($param{outer} and
141 not $param{terse} and
142 not exists $param{att}) {
143 print {$output} "<div class=\"headers\">\n";
144 if ($param{trim_headers}) {
146 foreach (qw(From To Cc Subject Date)) {
147 my $head_field = $head->get($_);
148 next unless defined $head_field and $head_field ne '';
150 if ($_ eq 'From' and $param{avatars}) {
151 my $libravatar_url = __libravatar_url(decode_rfc1522($head_field));
152 if (defined $libravatar_url and length $libravatar_url) {
153 push @headers,q(<img src=").html_escape($libravatar_url).qq(" alt="">\n);
156 push @headers, qq(<div class="header"><span class="headerfield">$_:</span> ) . html_escape(decode_rfc1522($head_field))."</div>\n";
158 print {$output} join(qq(), @headers);
160 print {$output} "<pre>".html_escape(decode_rfc1522($entity->head->stringify))."</pre>\n";
162 print {$output} "</div>\n";
165 if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
166 or $type =~ m{^multipart/}
168 push @$attachments, $param{entity};
169 # output this attachment
170 if (exists $param{att} and
171 $param{att} == $#$attachments) {
172 my $head = $entity->head;
173 chomp(my $type = $entity->effective_type);
174 my $body = $entity->stringify_body;
175 # this attachment has its own content type, so we must not
176 # try to convert it to UTF-8 or do anything funky.
177 binmode($output,':raw');
178 print {$output} "Content-Type: $type";
179 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
180 print {$output} qq(; charset="$charset") if defined $charset;
181 print {$output} "\n";
182 if ($filename ne '') {
186 print {$output} qq{Content-Disposition: inline; filename="$qf"\n};
188 print {$output} "\n";
189 my $decoder = MIME::Decoder->new($head->mime_encoding);
190 $decoder->decode(IO::Scalar->new(\$body), $output);
191 # we don't reset the layers here, because it makes no
192 # sense to add anything to the output handle after this
196 elsif (not exists $param{att}) {
197 my @dlargs = (msg=>$xmessage, att=>$#$attachments);
198 push @dlargs, (filename=>$filename) if $filename ne '';
199 my $printname = $filename;
200 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
201 print {$output} '<pre class="mime">[<a href="' .
202 html_escape(bug_links(bug => $ref,
204 options => {@dlargs})
205 ) . qq{">$printname</a> } .
206 "($type, $disposition)]</pre>\n";
210 return 0 if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
211 return 0 unless (($type =~ m[^text/?] and
212 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
213 $type =~ m[^application/pgp(?:;|$)] or
216 if ($entity->is_multipart) {
217 my @parts = $entity->parts;
218 foreach my $part (@parts) {
220 display_entity(entity => $part,
223 msg_num => $xmessage,
225 attachments => $attachments,
226 terse => $param{terse},
227 exists $param{msg}?(msg=>$param{msg}):(),
228 exists $param{att}?(att=>$param{att}):(),
229 exists $param{avatars}?(avatars=>$param{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 exists $param{msg}?(msg=>$param{msg}):(),
251 exists $param{att}?(att=>$param{att}):(),
252 exists $param{avatars}?(avatars=>$param{avatars}):(),
254 # print {$output} "\n";
256 if (not exists $param{att}) {
257 print {$output} "</blockquote>\n";
259 } elsif (not $param{terse}) {
260 my $content_type = $entity->head->get('Content-Type:') || "text/html";
261 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
262 my $body = $entity->bodyhandle->as_string;
263 $body = convert_to_utf8($body,$charset//'utf8');
264 $body = html_escape($body);
265 # Attempt to deal with format=flowed
266 if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
267 $body =~ s{^\ }{}mgo;
268 # we ignore the other things that you can do with
269 # flowed e-mails cause they don't really matter.
272 # We don't html escape here because we escape above;
273 # wierd terminators are because of that
274 $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
275 ((?:\>\;)?[)]?(?:'|\&\#39\;|\"\;)?[:.\,]?(?:\s|$)) # terminators
276 }{<a href=\"$1\">$1</a>$2}gox;
277 # Add links to bug closures
278 $body =~ s[((?:closes|see):\s* # start of closed/referenced bugs
279 (?:bug)?\#?\s?\d+\s? # first bug
280 (?:,?\s*(?:bug)?\#?\s?\d+)* # additional bugs
281 (?:\s|\n|\)|\]|\}|\.|\,|$)) # ends with a space, newline, end of string, or ); fixes #747267
285 {bug_links(bug=>$1)}ge;
287 if (defined $config{cve_tracker} and
288 length $config{cve_tracker}
290 # Add links to CVE vulnerabilities (closes #568464)
291 $body =~ s{(^|\s|[\(\[])(CVE-\d{4}-\d{4,})(\s|[,.-\[\]\)]|$)}
292 {$1<a href="http://$config{cve_tracker}$2">$2</a>$3}gxm;
294 if (not exists $param{att}) {
295 print {$output} qq(<pre class="message">$body</pre>\n);
302 =head2 handle_email_message
304 handle_email_message($record->{text},
306 msg_num => $msg_number,
309 Returns a decoded e-mail message and displays entities/attachments as
315 sub handle_email_message{
316 my ($email,%param) = @_;
319 my $output_fh = globify_scalar(\$output);
321 if (not blessed $email) {
322 my $parser = MIME::Parser->new();
323 # Because we are using memory, not tempfiles, there's no need to
324 # clean up here like in Debbugs::MIME
325 # this will be cleaned up once it goes out of scope
326 my $tempdir = File::Temp->newdir();
327 $parser->output_under($tempdir->dirname());
328 $entity = $parser->parse_data( $email);
332 my @attachments = ();
334 display_entity(entity => $entity,
335 bug_num => $param{ref},
337 msg_num => $param{msg_num},
338 output => $output_fh,
339 attachments => \@attachments,
340 terse => $param{terse},
341 exists $param{msg}?(msg=>$param{msg}):(),
342 exists $param{att}?(att=>$param{att}):(),
343 exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
344 exists $param{avatars}?(avatars=>$param{avatars}):(),
346 return $raw_output?$output:decode_utf8($output);
351 push @log, handle_record($record,$ref,$msg_num);
353 Deals with a record in a bug log as returned by
354 L<Debbugs::Log::read_log_records>; returns the log information that
355 should be output to the browser.
360 my ($record,$bug_number,$msg_number,$seen_msg_ids,%param) = @_;
362 # output needs to have the is_utf8 flag on to avoid double
364 my $output = decode_utf8('');
365 local $_ = $record->{type};
367 # $record->{text} is not in perl's internal encoding; convert it
368 my $text = decode_rfc1522(decode_utf8($record->{text}));
369 my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
370 my $class = $text =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
372 # Link to forwarded http:// urls in the midst of the report
373 # (even though these links already exist at the top)
374 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;|\"\;)?
375 (?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,gxo;
376 # Add links to the cloned bugs
377 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
378 # Add links to merged bugs
379 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
380 # Add links to blocked bugs
381 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
382 {(defined $2?$1.bug_links(bug=>$2):'').$3.
383 english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
384 $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
385 (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
386 {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
387 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
388 $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks )([\d\s\,]+)}
389 {$1.$2.(bug_links(bug=>$3)).$4.
390 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
391 # Add links to reassigned packages
392 $output =~ s{(Bug\sreassigned\sfrom\spackage\s(?:[\`']|\&\#39;))([^']+?)((?:'|\&\#39;|\"\;)
393 \sto\s(?:[\`']|\&\#39;|\"\;))([^']+?)((?:'|\&\#39;|\"\;))}
394 {$1.q(<a href=").html_escape(package_links(package=>$2)).qq(">$2</a>).$3.
395 q(<a href=").html_escape(package_links(package=>$4)).qq(">$4</a>).$5}exo;
397 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
399 $output .= '<a href="' .
400 html_escape(bug_links(bug => $bug_number,
401 options => {msg => ($msg_number+1)},
404 ) . '">Full text</a> and <a href="' .
405 html_escape(bug_links(bug => $bug_number,
406 options => {msg => ($msg_number+1),
409 ) . '">rfc822 format</a> available.';
411 $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
414 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
415 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
418 elsif (defined $msg_id) {
419 $$seen_msg_ids{$msg_id} = 1;
421 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
422 $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>';
423 $output .= handle_email_message($record->{text},
425 msg_num => $msg_number,
429 elsif (/autocheck/) {
432 elsif (/incoming-recv/) {
433 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
434 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
437 elsif (defined $msg_id) {
438 $$seen_msg_ids{$msg_id} = 1;
440 # Incomming Mail Message
441 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
442 $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 |.
443 html_escape("$received\@$hostname") .
444 q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
445 q|, <a href="| . html_escape(bug_links(bug => $bug_number,
447 options => {msg=>$msg_number,
451 my $parser = MIME::Parser->new();
453 # this will be cleaned up once it goes out of scope
454 my $tempdir = File::Temp->newdir();
455 $parser->output_under($tempdir->dirname());
456 my $entity = $parser->parse_data($record->{text});
457 my $r_l = reply_headers($entity);
458 $output .= q(<a href=").
459 html_escape('mailto:'.$bug_number.'@'.$config{email_domain}.'?'.
460 join('&',map {defined $r_l->{$_}?$_.'='.uri_escape_utf8($r_l->{$_}):()} keys %{$r_l})).
463 $output .= ')'.":</p>\n";
464 $output .= handle_email_message($entity,
466 msg_num => $msg_number,
471 die "Unknown record type $_";
477 sub __libravatar_url {
479 if (not defined $config{libravatar_uri} or not length $config{libravatar_uri}) {
482 ($email) = grep {/\@/} get_addresses($email);
483 return $config{libravatar_uri}.uri_escape_utf8($email.($config{libravatar_uri_options}//''));