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(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);
46 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
47 $DEBUG = 0 unless defined $DEBUG;
51 @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
52 Exporter::export_ok_tags(keys %EXPORT_TAGS);
53 $EXPORT_TAGS{all} = [@EXPORT_OK];
60 display_entity(entity => $entity,
64 attachments => \@attachments,
70 =item entity -- MIME::Parser entity
72 =item bug_num -- Bug number
74 =item outer -- Whether this is the outer entity; defaults to 1
76 =item msg_num -- message number in the log
78 =item attachments -- arrayref of attachments
80 =item output -- scalar reference for output
87 my %param = validate_with(params => \@_,
88 spec => {entity => {type => OBJECT,
90 bug_num => {type => SCALAR,
93 outer => {type => BOOLEAN,
96 msg_num => {type => SCALAR,
98 attachments => {type => ARRAYREF,
101 output => {type => SCALARREF|HANDLE,
104 terse => {type => BOOLEAN,
107 msg => {type => SCALAR,
110 att => {type => SCALAR,
113 trim_headers => {type => BOOLEAN,
116 avatars => {type => BOOLEAN,
122 my $output = globify_scalar($param{output});
123 my $entity = $param{entity};
124 my $ref = $param{bug_num};
125 my $top = $param{outer};
126 my $xmessage = $param{msg_num};
127 my $attachments = $param{attachments};
129 my $head = $entity->head;
130 my $disposition = $head->mime_attr('content-disposition');
131 $disposition = 'inline' if not defined $disposition or $disposition eq '';
132 my $type = $entity->effective_type;
133 my $filename = $entity->head->recommended_filename;
134 $filename = '' unless defined $filename;
135 $filename = decode_rfc1522($filename);
137 if ($param{outer} and
138 not $param{terse} and
139 not exists $param{att}) {
140 my $header = $entity->head;
141 print {$output} "<div class=\"headers\">\n";
142 if ($param{trim_headers}) {
144 foreach (qw(From To Cc Subject Date)) {
145 my $head_field = $head->get($_);
146 next unless defined $head_field and $head_field ne '';
148 if ($_ eq 'From' and $param{avatars}) {
149 my $libravatar_url = __libravatar_url(decode_rfc1522($head_field));
150 if (defined $libravatar_url and length $libravatar_url) {
151 push @headers,q(<img src=").$libravatar_url.qq(">\n);
154 push @headers, qq(<p><span class="header">$_:</span> ) . html_escape(decode_rfc1522($head_field))."</p>\n";
156 print {$output} join(qq(), @headers);
158 print {$output} "<pre>".html_escape(decode_rfc1522($entity->head->stringify))."</pre>\n";
160 print {$output} "</div>\n";
163 if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
164 or $type =~ m{^multipart/}
166 push @$attachments, $param{entity};
167 # output this attachment
168 if (exists $param{att} and
169 $param{att} == $#$attachments) {
170 my $head = $entity->head;
171 chomp(my $type = $entity->effective_type);
172 my $body = $entity->stringify_body;
173 # this attachment has its own content type, so we must not
174 # try to convert it to UTF-8 or do anything funky.
175 my @layers = PerlIO::get_layers($output);
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 if (grep {/utf8/} @layers) {
191 binmode($output,':utf8');
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 if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
210 return 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) {
218 display_entity(entity => $part,
221 msg_num => $xmessage,
223 attachments => $attachments,
224 terse => $param{terse},
225 exists $param{msg}?(msg=>$param{msg}):(),
226 exists $param{att}?(att=>$param{att}):(),
227 exists $param{avatars}?(avatars=>$param{avatars}):(),
229 # print {$output} "\n";
231 } elsif ($entity->parts) {
232 # We must be dealing with a nested message.
233 if (not exists $param{att}) {
234 print {$output} "<blockquote>\n";
236 my @parts = $entity->parts;
237 foreach my $part (@parts) {
238 display_entity(entity => $part,
241 msg_num => $xmessage,
243 attachments => $attachments,
244 terse => $param{terse},
245 exists $param{msg}?(msg=>$param{msg}):(),
246 exists $param{att}?(att=>$param{att}):(),
247 exists $param{avatars}?(avatars=>$param{avatars}):(),
249 # print {$output} "\n";
251 if (not exists $param{att}) {
252 print {$output} "</blockquote>\n";
254 } elsif (not $param{terse}) {
255 my $content_type = $entity->head->get('Content-Type:') || "text/html";
256 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
257 my $body = $entity->bodyhandle->as_string;
258 $body = convert_to_utf8($body,$charset//'utf8');
259 $body = html_escape($body);
260 # Attempt to deal with format=flowed
261 if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
262 $body =~ s{^\ }{}mgo;
263 # we ignore the other things that you can do with
264 # flowed e-mails cause they don't really matter.
267 # We don't html escape here because we escape above;
268 # wierd terminators are because of that
269 $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
270 ((?:\>\;)?[)]?(?:'|\&\#39\;)?[:.\,]?(?:\s|$)) # terminators
271 }{<a href=\"$1\">$1</a>$2}gox;
272 # Add links to bug closures
273 $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)]
276 {bug_links(bug=>$1)}ge;
278 if (defined $config{cve_tracker} and
279 length $config{cve_tracker}
281 # Add links to CVE vulnerabilities (closes #568464)
282 $body =~ s{(^|\s)(CVE-\d{4}-\d{4,})(\s|[,.-\[\]]|$)}
283 {$1<a href="http://$config{cve_tracker}$2">$2</a>$3}gxm;
285 if (not exists $param{att}) {
286 print {$output} qq(<pre class="message">$body</pre>\n);
292 =head2 handle_email_message
294 handle_email_message($record->{text},
296 msg_num => $msg_number,
299 Returns a decoded e-mail message and displays entities/attachments as
305 sub handle_email_message{
306 my ($email,%param) = @_;
309 my $output_fh = globify_scalar(\$output);
310 my $parser = MIME::Parser->new();
311 # Because we are using memory, not tempfiles, there's no need to
312 # clean up here like in Debbugs::MIME
313 $parser->tmp_to_core(1);
314 $parser->output_to_core(1);
315 my $entity = $parser->parse_data( $email);
316 my @attachments = ();
317 display_entity(entity => $entity,
318 bug_num => $param{ref},
320 msg_num => $param{msg_num},
321 output => $output_fh,
322 attachments => \@attachments,
323 terse => $param{terse},
324 exists $param{msg}?(msg=>$param{msg}):(),
325 exists $param{att}?(att=>$param{att}):(),
326 exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
327 exists $param{avatars}?(avatars=>$param{avatars}):(),
329 return decode_utf8($output);
334 push @log, handle_record($record,$ref,$msg_num);
336 Deals with a record in a bug log as returned by
337 L<Debbugs::Log::read_log_records>; returns the log information that
338 should be output to the browser.
343 my ($record,$bug_number,$msg_number,$seen_msg_ids,%param) = @_;
345 # output needs to have the is_utf8 flag on to avoid double
347 my $output = decode_utf8('');
348 local $_ = $record->{type};
350 # $record->{text} is not in perl's internal encoding; convert it
351 my $text = decode_rfc1522(decode_utf8($record->{text}));
352 my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
353 my $class = $text =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
355 # Link to forwarded http:// urls in the midst of the report
356 # (even though these links already exist at the top)
357 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;)?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
358 # Add links to the cloned bugs
359 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
360 # Add links to merged bugs
361 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
362 # Add links to blocked bugs
363 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
364 {(defined $2?$1.bug_links(bug=>$2):'').$3.
365 english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
366 $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
367 (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
368 {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
369 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
370 $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks )([\d\s\,]+)}
371 {$1.$2.(bug_links(bug=>$3)).$4.
372 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
373 # Add links to reassigned packages
374 $output =~ s{(Bug reassigned from package \`)([^']+?)((?:'|\&\#39;) to \`)([^']+?)((?:'|\&\#39;))}
375 {$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;
377 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
379 $output .= '<a href="' .
380 html_escape(bug_links(bug => $bug_number,
381 options => {msg => ($msg_number+1)},
384 ) . '">Full text</a> and <a href="' .
385 html_escape(bug_links(bug => $bug_number,
386 options => {msg => ($msg_number+1),
389 ) . '">rfc822 format</a> available.';
391 $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
394 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
395 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
398 elsif (defined $msg_id) {
399 $$seen_msg_ids{$msg_id} = 1;
401 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
402 $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>';
403 $output .= handle_email_message($record->{text},
405 msg_num => $msg_number,
409 elsif (/autocheck/) {
412 elsif (/incoming-recv/) {
413 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
414 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
417 elsif (defined $msg_id) {
418 $$seen_msg_ids{$msg_id} = 1;
420 # Incomming Mail Message
421 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
422 $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 |.
423 html_escape("$received\@$hostname") .
424 q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
425 q|, <a href="| . html_escape(bug_links(bug => $bug_number,
427 options => {msg=>$msg_number,
430 ) .'">mbox</a>)'.":</p>\n";
431 $output .= handle_email_message($record->{text},
433 msg_num => $msg_number,
438 die "Unknown record type $_";
444 sub __libravatar_url {
446 if (not defined $config{libravatar_uri} or not length $config{libravatar_uri}) {
449 ($email) = get_addresses($email);
450 return $config{libravatar_uri}.md5_hex(lc($email)).($config{libravatar_uri_options}//'');