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 Debbugs::MIME qw(decode_rfc1522 create_mime_message);
36 use Debbugs::CGI qw(:url :html :util);
37 use Debbugs::Common qw(globify_scalar english_join);
39 use Debbugs::Config qw(:config);
40 use POSIX qw(strftime);
41 use Encode qw(decode_utf8);
44 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
45 $DEBUG = 0 unless defined $DEBUG;
49 @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
50 Exporter::export_ok_tags(keys %EXPORT_TAGS);
51 $EXPORT_TAGS{all} = [@EXPORT_OK];
58 display_entity(entity => $entity,
62 attachments => \@attachments,
68 =item entity -- MIME::Parser entity
70 =item bug_num -- Bug number
72 =item outer -- Whether this is the outer entity; defaults to 1
74 =item msg_num -- message number in the log
76 =item attachments -- arrayref of attachments
78 =item output -- scalar reference for output
85 my %param = validate_with(params => \@_,
86 spec => {entity => {type => OBJECT,
88 bug_num => {type => SCALAR,
91 outer => {type => BOOLEAN,
94 msg_num => {type => SCALAR,
96 attachments => {type => ARRAYREF,
99 output => {type => SCALARREF|HANDLE,
102 terse => {type => BOOLEAN,
105 msg => {type => SCALAR,
108 att => {type => SCALAR,
111 trim_headers => {type => BOOLEAN,
117 $param{output} = globify_scalar($param{output});
118 my $entity = $param{entity};
119 my $ref = $param{bug_num};
120 my $top = $param{outer};
121 my $xmessage = $param{msg_num};
122 my $attachments = $param{attachments};
124 my $head = $entity->head;
125 my $disposition = $head->mime_attr('content-disposition');
126 $disposition = 'inline' if not defined $disposition or $disposition eq '';
127 my $type = $entity->effective_type;
128 my $filename = $entity->head->recommended_filename;
129 $filename = '' unless defined $filename;
130 $filename = decode_rfc1522($filename);
132 if ($param{outer} and
133 not $param{terse} and
134 not exists $param{att}) {
135 my $header = $entity->head;
136 print {$param{output}} "<pre class=\"headers\">\n";
137 if ($param{trim_headers}) {
139 foreach (qw(From To Cc Subject Date)) {
140 my $head_field = $head->get($_);
141 next unless defined $head_field and $head_field ne '';
142 push @headers, qq(<b>$_:</b> ) . html_escape(decode_rfc1522($head_field));
144 print {$param{output}} join(qq(), @headers);
146 print {$param{output}} html_escape(decode_rfc1522($entity->head->stringify));
148 print {$param{output}} "</pre>\n";
151 if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
152 or $type =~ m{^multipart/}
154 push @$attachments, $param{entity};
155 # output this attachment
156 if (exists $param{att} and
157 $param{att} == $#$attachments) {
158 my $head = $entity->head;
159 chomp(my $type = $entity->effective_type);
160 my $body = $entity->stringify_body;
161 # this attachment has its own content type, so we must not
162 # try to convert it to UTF-8 or do anything funky.
163 my @layers = PerlIO::get_layers($param{output});
164 binmode($param{output},':raw');
165 print {$param{output}} "Content-Type: $type";
166 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
167 print {$param{output}} qq(; charset="$charset") if defined $charset;
168 print {$param{output}} "\n";
169 if ($filename ne '') {
173 print {$param{output}} qq{Content-Disposition: inline; filename="$qf"\n};
175 print {$param{output}} "\n";
176 my $decoder = MIME::Decoder->new($head->mime_encoding);
177 $decoder->decode(IO::Scalar->new(\$body), $param{output});
178 if (grep {/utf8/} @layers) {
179 binmode($param{output},':utf8');
183 elsif (not exists $param{att}) {
184 my @dlargs = (msg=>$xmessage, att=>$#$attachments);
185 push @dlargs, (filename=>$filename) if $filename ne '';
186 my $printname = $filename;
187 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
188 print {$param{output}} '<pre class="mime">[<a href="' .
189 html_escape(bug_links(bug => $ref,
191 options => {@dlargs})
192 ) . qq{">$printname</a> } .
193 "($type, $disposition)]</pre>\n";
197 return if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
198 return unless ($type =~ m[^text/?] and
199 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
200 $type =~ m[^application/pgp(?:;|$)] or
203 if ($entity->is_multipart) {
204 my @parts = $entity->parts;
205 foreach my $part (@parts) {
206 display_entity(entity => $part,
209 msg_num => $xmessage,
210 output => $param{output},
211 attachments => $attachments,
212 terse => $param{terse},
213 exists $param{msg}?(msg=>$param{msg}):(),
214 exists $param{att}?(att=>$param{att}):(),
216 # print {$param{output}} "\n";
218 } elsif ($entity->parts) {
219 # We must be dealing with a nested message.
220 if (not exists $param{att}) {
221 print {$param{output}} "<blockquote>\n";
223 my @parts = $entity->parts;
224 foreach my $part (@parts) {
225 display_entity(entity => $part,
228 msg_num => $xmessage,
229 output => $param{output},
230 attachments => $attachments,
231 terse => $param{terse},
232 exists $param{msg}?(msg=>$param{msg}):(),
233 exists $param{att}?(att=>$param{att}):(),
235 # print {$param{output}} "\n";
237 if (not exists $param{att}) {
238 print {$param{output}} "</blockquote>\n";
240 } elsif (not $param{terse}) {
241 my $content_type = $entity->head->get('Content-Type:') || "text/html";
242 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
243 my $body = $entity->bodyhandle->as_string;
244 $body = convert_to_utf8($body,$charset//'utf8');
245 $body = html_escape($body);
246 # Attempt to deal with format=flowed
247 if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
248 $body =~ s{^\ }{}mgo;
249 # we ignore the other things that you can do with
250 # flowed e-mails cause they don't really matter.
253 # We don't html escape here because we escape above;
254 # wierd terminators are because of that
255 $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
256 ((?:\>\;)?[)]?(?:'|\&\#39\;)?[:.\,]?(?:\s|$)) # terminators
257 }{<a href=\"$1\">$1</a>$2}gox;
258 # Add links to bug closures
259 $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)]
262 {bug_links(bug=>$1)}ge;
264 if (defined $config{cve_tracker} and
265 length $config{cve_tracker}
267 # Add links to CVE vulnerabilities (closes #568464)
268 $body =~ s{(^|\s)(CVE-\d{4}-\d{4,})(\s|[,.-\[\]]|$)}
269 {$1<a href="http://$config{cve_tracker}$2">$2</a>$3}gxm;
271 if (not exists $param{att}) {
272 print {$param{output}} qq(<pre class="message">$body</pre>\n);
278 =head2 handle_email_message
280 handle_email_message($record->{text},
282 msg_num => $msg_number,
285 Returns a decoded e-mail message and displays entities/attachments as
291 sub handle_email_message{
292 my ($email,%param) = @_;
294 # output needs to have the is_utf8 flag on to avoid double
296 my $output = decode_utf8('');
297 my $parser = MIME::Parser->new();
298 # Because we are using memory, not tempfiles, there's no need to
299 # clean up here like in Debbugs::MIME
300 $parser->tmp_to_core(1);
301 $parser->output_to_core(1);
302 my $entity = $parser->parse_data( $email);
303 my @attachments = ();
304 display_entity(entity => $entity,
305 bug_num => $param{ref},
307 msg_num => $param{msg_num},
309 attachments => \@attachments,
310 terse => $param{terse},
311 exists $param{msg}?(msg=>$param{msg}):(),
312 exists $param{att}?(att=>$param{att}):(),
313 exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
321 push @log, handle_record($record,$ref,$msg_num);
323 Deals with a record in a bug log as returned by
324 L<Debbugs::Log::read_log_records>; returns the log information that
325 should be output to the browser.
330 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
332 # output needs to have the is_utf8 flag on to avoid double
334 my $output = decode_utf8('');
335 local $_ = $record->{type};
337 # $record->{text} is not in perl's internal encoding; convert it
338 my $text = decode_rfc1522(decode_utf8($record->{text}));
339 my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
340 my $class = $text =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
342 # Link to forwarded http:// urls in the midst of the report
343 # (even though these links already exist at the top)
344 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;)?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
345 # Add links to the cloned bugs
346 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
347 # Add links to merged bugs
348 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
349 # Add links to blocked bugs
350 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
351 {(defined $2?$1.bug_links(bug=>$2):'').$3.
352 english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
353 $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
354 (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
355 {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
356 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
357 $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks )([\d\s\,]+)}
358 {$1.$2.(bug_links(bug=>$3)).$4.
359 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
360 # Add links to reassigned packages
361 $output =~ s{(Bug reassigned from package \`)([^']+?)((?:'|\&\#39;) to \`)([^']+?)((?:'|\&\#39;))}
362 {$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;
364 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
366 $output .= '<a href="' .
367 html_escape(bug_links(bug => $bug_number,
368 options => {msg => ($msg_number+1)},
371 ) . '">Full text</a> and <a href="' .
372 html_escape(bug_links(bug => $bug_number,
373 options => {msg => ($msg_number+1),
376 ) . '">rfc822 format</a> available.';
378 $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
381 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
382 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
385 elsif (defined $msg_id) {
386 $$seen_msg_ids{$msg_id} = 1;
388 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
389 $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>';
390 $output .= handle_email_message($record->{text},
392 msg_num => $msg_number,
395 elsif (/autocheck/) {
398 elsif (/incoming-recv/) {
399 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
400 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
403 elsif (defined $msg_id) {
404 $$seen_msg_ids{$msg_id} = 1;
406 # Incomming Mail Message
407 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
408 $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 |.
409 html_escape("$received\@$hostname") .
410 q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
411 q|, <a href="| . html_escape(bug_links(bug => $bug_number,
413 options => {msg=>$msg_number,
416 ) .'">mbox</a>)'.":</p>\n";
417 $output .= handle_email_message($record->{text},
419 msg_num => $msg_number,
423 die "Unknown record type $_";