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);
37 use Debbugs::MIME qw(convert_to_utf8 decode_rfc1522 create_mime_message);
38 use Debbugs::CGI qw(:url :html :util);
39 use Debbugs::Common qw(globify_scalar english_join);
40 use Debbugs::Config qw(:config);
41 use POSIX qw(strftime);
42 use Encode qw(decode_utf8);
45 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
46 $DEBUG = 0 unless defined $DEBUG;
50 @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
51 Exporter::export_ok_tags(keys %EXPORT_TAGS);
52 $EXPORT_TAGS{all} = [@EXPORT_OK];
59 display_entity(entity => $entity,
63 attachments => \@attachments,
69 =item entity -- MIME::Parser entity
71 =item bug_num -- Bug number
73 =item outer -- Whether this is the outer entity; defaults to 1
75 =item msg_num -- message number in the log
77 =item attachments -- arrayref of attachments
79 =item output -- scalar reference for output
86 my %param = validate_with(params => \@_,
87 spec => {entity => {type => OBJECT,
89 bug_num => {type => SCALAR,
92 outer => {type => BOOLEAN,
95 msg_num => {type => SCALAR,
97 attachments => {type => ARRAYREF,
100 output => {type => SCALARREF|HANDLE,
103 terse => {type => BOOLEAN,
106 msg => {type => SCALAR,
109 att => {type => SCALAR,
112 trim_headers => {type => BOOLEAN,
118 $param{output} = globify_scalar($param{output});
119 my $entity = $param{entity};
120 my $ref = $param{bug_num};
121 my $top = $param{outer};
122 my $xmessage = $param{msg_num};
123 my $attachments = $param{attachments};
125 my $head = $entity->head;
126 my $disposition = $head->mime_attr('content-disposition');
127 $disposition = 'inline' if not defined $disposition or $disposition eq '';
128 my $type = $entity->effective_type;
129 my $filename = $entity->head->recommended_filename;
130 $filename = '' unless defined $filename;
131 $filename = decode_rfc1522($filename);
133 if ($param{outer} and
134 not $param{terse} and
135 not exists $param{att}) {
136 my $header = $entity->head;
137 print {$param{output}} "<div class=\"headers\">\n";
138 if ($param{trim_headers}) {
140 foreach (qw(From To Cc Subject Date)) {
141 my $head_field = $head->get($_);
142 next unless defined $head_field and $head_field ne '';
144 my $libravatar_url = __libravatar_url(decode_rfc1522($head_field));
145 if (defined $libravatar_url and length $libravatar_url) {
146 push @headers,q(<img src=").$libravatar_url.q(">);
149 push @headers, qq(<p><span class="header">$_:</span> ) . html_escape(decode_rfc1522($head_field))."</p>";
151 print {$param{output}} join(qq(), @headers);
153 print {$param{output}} "<pre>".html_escape(decode_rfc1522($entity->head->stringify))."</pre>\n";
155 print {$param{output}} "</div>\n";
158 if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
159 or $type =~ m{^multipart/}
161 push @$attachments, $param{entity};
162 # output this attachment
163 if (exists $param{att} and
164 $param{att} == $#$attachments) {
165 my $head = $entity->head;
166 chomp(my $type = $entity->effective_type);
167 my $body = $entity->stringify_body;
168 # this attachment has its own content type, so we must not
169 # try to convert it to UTF-8 or do anything funky.
170 my @layers = PerlIO::get_layers($param{output});
171 binmode($param{output},':raw');
172 print {$param{output}} "Content-Type: $type";
173 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
174 print {$param{output}} qq(; charset="$charset") if defined $charset;
175 print {$param{output}} "\n";
176 if ($filename ne '') {
180 print {$param{output}} qq{Content-Disposition: inline; filename="$qf"\n};
182 print {$param{output}} "\n";
183 my $decoder = MIME::Decoder->new($head->mime_encoding);
184 $decoder->decode(IO::Scalar->new(\$body), $param{output});
185 if (grep {/utf8/} @layers) {
186 binmode($param{output},':utf8');
190 elsif (not exists $param{att}) {
191 my @dlargs = (msg=>$xmessage, att=>$#$attachments);
192 push @dlargs, (filename=>$filename) if $filename ne '';
193 my $printname = $filename;
194 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
195 print {$param{output}} '<pre class="mime">[<a href="' .
196 html_escape(bug_links(bug => $ref,
198 options => {@dlargs})
199 ) . qq{">$printname</a> } .
200 "($type, $disposition)]</pre>\n";
204 return if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
205 return unless ($type =~ m[^text/?] and
206 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
207 $type =~ m[^application/pgp(?:;|$)] or
210 if ($entity->is_multipart) {
211 my @parts = $entity->parts;
212 foreach my $part (@parts) {
213 display_entity(entity => $part,
216 msg_num => $xmessage,
217 output => $param{output},
218 attachments => $attachments,
219 terse => $param{terse},
220 exists $param{msg}?(msg=>$param{msg}):(),
221 exists $param{att}?(att=>$param{att}):(),
223 # print {$param{output}} "\n";
225 } elsif ($entity->parts) {
226 # We must be dealing with a nested message.
227 if (not exists $param{att}) {
228 print {$param{output}} "<blockquote>\n";
230 my @parts = $entity->parts;
231 foreach my $part (@parts) {
232 display_entity(entity => $part,
235 msg_num => $xmessage,
236 output => $param{output},
237 attachments => $attachments,
238 terse => $param{terse},
239 exists $param{msg}?(msg=>$param{msg}):(),
240 exists $param{att}?(att=>$param{att}):(),
242 # print {$param{output}} "\n";
244 if (not exists $param{att}) {
245 print {$param{output}} "</blockquote>\n";
247 } elsif (not $param{terse}) {
248 my $content_type = $entity->head->get('Content-Type:') || "text/html";
249 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
250 my $body = $entity->bodyhandle->as_string;
251 $body = convert_to_utf8($body,$charset//'utf8');
252 $body = html_escape($body);
253 # Attempt to deal with format=flowed
254 if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
255 $body =~ s{^\ }{}mgo;
256 # we ignore the other things that you can do with
257 # flowed e-mails cause they don't really matter.
260 # We don't html escape here because we escape above;
261 # wierd terminators are because of that
262 $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
263 ((?:\>\;)?[)]?(?:'|\&\#39\;)?[:.\,]?(?:\s|$)) # terminators
264 }{<a href=\"$1\">$1</a>$2}gox;
265 # Add links to bug closures
266 $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)]
269 {bug_links(bug=>$1)}ge;
271 if (defined $config{cve_tracker} and
272 length $config{cve_tracker}
274 # Add links to CVE vulnerabilities (closes #568464)
275 $body =~ s{(^|\s)(CVE-\d{4}-\d{4,})(\s|[,.-\[\]]|$)}
276 {$1<a href="http://$config{cve_tracker}$2">$2</a>$3}gxm;
278 if (not exists $param{att}) {
279 print {$param{output}} qq(<pre class="message">$body</pre>\n);
285 =head2 handle_email_message
287 handle_email_message($record->{text},
289 msg_num => $msg_number,
292 Returns a decoded e-mail message and displays entities/attachments as
298 sub handle_email_message{
299 my ($email,%param) = @_;
301 # output needs to have the is_utf8 flag on to avoid double
303 my $output = decode_utf8('');
304 my $parser = MIME::Parser->new();
305 # Because we are using memory, not tempfiles, there's no need to
306 # clean up here like in Debbugs::MIME
307 $parser->tmp_to_core(1);
308 $parser->output_to_core(1);
309 my $entity = $parser->parse_data( $email);
310 my @attachments = ();
311 display_entity(entity => $entity,
312 bug_num => $param{ref},
314 msg_num => $param{msg_num},
316 attachments => \@attachments,
317 terse => $param{terse},
318 exists $param{msg}?(msg=>$param{msg}):(),
319 exists $param{att}?(att=>$param{att}):(),
320 exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
328 push @log, handle_record($record,$ref,$msg_num);
330 Deals with a record in a bug log as returned by
331 L<Debbugs::Log::read_log_records>; returns the log information that
332 should be output to the browser.
337 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
339 # output needs to have the is_utf8 flag on to avoid double
341 my $output = decode_utf8('');
342 local $_ = $record->{type};
344 # $record->{text} is not in perl's internal encoding; convert it
345 my $text = decode_utf8(decode_rfc1522($record->{text}));
346 my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
347 my $class = $text =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
349 # Link to forwarded http:// urls in the midst of the report
350 # (even though these links already exist at the top)
351 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;)?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
352 # Add links to the cloned bugs
353 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
354 # Add links to merged bugs
355 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
356 # Add links to blocked bugs
357 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
358 {(defined $2?$1.bug_links(bug=>$2):'').$3.
359 english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
360 $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
361 (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
362 {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
363 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
364 $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks )([\d\s\,]+)}
365 {$1.$2.(bug_links(bug=>$3)).$4.
366 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
367 # Add links to reassigned packages
368 $output =~ s{(Bug reassigned from package \`)([^']+?)((?:'|\&\#39;) to \`)([^']+?)((?:'|\&\#39;))}
369 {$1.q(<a href=").html_escape(package_links(package=>$2)).qq(">$2</a>).$3.q(<a href=").html_escape(package_links(package=>$4)).qq(">$4</a>).$5}eo;
371 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
373 $output .= '<a href="' .
374 html_escape(bug_links(bug => $bug_number,
375 options => {msg => ($msg_number+1)},
378 ) . '">Full text</a> and <a href="' .
379 html_escape(bug_links(bug => $bug_number,
380 options => {msg => ($msg_number+1),
383 ) . '">rfc822 format</a> available.';
385 $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
388 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
389 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
392 elsif (defined $msg_id) {
393 $$seen_msg_ids{$msg_id} = 1;
395 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
396 $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>';
397 $output .= handle_email_message($record->{text},
399 msg_num => $msg_number,
402 elsif (/autocheck/) {
405 elsif (/incoming-recv/) {
406 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
407 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
410 elsif (defined $msg_id) {
411 $$seen_msg_ids{$msg_id} = 1;
413 # Incomming Mail Message
414 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
415 $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 |.
416 html_escape("$received\@$hostname") .
417 q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
418 q|, <a href="| . html_escape(bug_links(bug => $bug_number,
420 options => {msg=>$msg_number,
423 ) .'">mbox</a>)'.":</p>\n";
424 $output .= handle_email_message($record->{text},
426 msg_num => $msg_number,
430 die "Unknown record type $_";
436 sub __libravatar_url {
438 if (not defined $config{libravatar_uri} or not length $config{libravatar_uri}) {
441 ($email) = get_addresses($email);
442 return $config{libravatar_uri}.md5_hex(lc($email)).($config{libravatar_uri_options}//'');