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(convert_to_utf8 decode_rfc1522 create_mime_message);
36 use Debbugs::CGI qw(:url :html :util);
37 use Debbugs::Common qw(globify_scalar english_join);
38 use Debbugs::Config qw(:config);
39 use POSIX qw(strftime);
42 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
43 $DEBUG = 0 unless defined $DEBUG;
47 @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
48 Exporter::export_ok_tags(keys %EXPORT_TAGS);
49 $EXPORT_TAGS{all} = [@EXPORT_OK];
56 display_entity(entity => $entity,
60 attachments => \@attachments,
66 =item entity -- MIME::Parser entity
68 =item bug_num -- Bug number
70 =item outer -- Whether this is the outer entity; defaults to 1
72 =item msg_num -- message number in the log
74 =item attachments -- arrayref of attachments
76 =item output -- scalar reference for output
83 my %param = validate_with(params => \@_,
84 spec => {entity => {type => OBJECT,
86 bug_num => {type => SCALAR,
89 outer => {type => BOOLEAN,
92 msg_num => {type => SCALAR,
94 attachments => {type => ARRAYREF,
97 output => {type => SCALARREF|HANDLE,
100 terse => {type => BOOLEAN,
103 msg => {type => SCALAR,
106 att => {type => SCALAR,
109 trim_headers => {type => BOOLEAN,
115 $param{output} = globify_scalar($param{output});
116 my $entity = $param{entity};
117 my $ref = $param{bug_num};
118 my $top = $param{outer};
119 my $xmessage = $param{msg_num};
120 my $attachments = $param{attachments};
122 my $head = $entity->head;
123 my $disposition = $head->mime_attr('content-disposition');
124 $disposition = 'inline' if not defined $disposition or $disposition eq '';
125 my $type = $entity->effective_type;
126 my $filename = $entity->head->recommended_filename;
127 $filename = '' unless defined $filename;
128 $filename = decode_rfc1522($filename);
130 if ($param{outer} and
131 not $param{terse} and
132 not exists $param{att}) {
133 my $header = $entity->head;
134 print {$param{output}} "<pre class=\"headers\">\n";
135 if ($param{trim_headers}) {
137 foreach (qw(From To Cc Subject Date)) {
138 my $head_field = $head->get($_);
139 next unless defined $head_field and $head_field ne '';
140 push @headers, qq(<b>$_:</b> ) . html_escape(decode_rfc1522($head_field));
142 print {$param{output}} join(qq(), @headers);
144 print {$param{output}} html_escape(decode_rfc1522($entity->head->stringify));
146 print {$param{output}} "</pre>\n";
149 if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
150 or $type =~ m{^multipart/}
152 push @$attachments, $param{entity};
153 # output this attachment
154 if (exists $param{att} and
155 $param{att} == $#$attachments) {
156 my $head = $entity->head;
157 chomp(my $type = $entity->effective_type);
158 my $body = $entity->stringify_body;
159 print {$param{output}} "Content-Type: $type";
160 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
161 print {$param{output}} qq(; charset="$charset") if defined $charset;
162 print {$param{output}}"\n";
163 if ($filename ne '') {
167 print {$param{output}} qq{Content-Disposition: inline; filename="$qf"\n};
169 print {$param{output}} "\n";
170 my $decoder = MIME::Decoder->new($head->mime_encoding);
171 $decoder->decode(IO::Scalar->new(\$body), $param{output});
174 elsif (not exists $param{att}) {
175 my @dlargs = (msg=>$xmessage, att=>$#$attachments);
176 push @dlargs, (filename=>$filename) if $filename ne '';
177 my $printname = $filename;
178 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
179 print {$param{output}} '<pre class="mime">[<a href="' .
180 html_escape(bug_links(bug => $ref,
182 options => {@dlargs})
183 ) . qq{">$printname</a> } .
184 "($type, $disposition)]</pre>\n";
188 return if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
189 return unless ($type =~ m[^text/?] and
190 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
191 $type =~ m[^application/pgp(?:;|$)] or
194 if ($entity->is_multipart) {
195 my @parts = $entity->parts;
196 foreach my $part (@parts) {
197 display_entity(entity => $part,
200 msg_num => $xmessage,
201 output => $param{output},
202 attachments => $attachments,
203 terse => $param{terse},
204 exists $param{msg}?(msg=>$param{msg}):(),
205 exists $param{att}?(att=>$param{att}):(),
207 # print {$param{output}} "\n";
209 } elsif ($entity->parts) {
210 # We must be dealing with a nested message.
211 if (not exists $param{att}) {
212 print {$param{output}} "<blockquote>\n";
214 my @parts = $entity->parts;
215 foreach my $part (@parts) {
216 display_entity(entity => $part,
219 msg_num => $xmessage,
220 output => $param{output},
221 attachments => $attachments,
222 terse => $param{terse},
223 exists $param{msg}?(msg=>$param{msg}):(),
224 exists $param{att}?(att=>$param{att}):(),
226 # print {$param{output}} "\n";
228 if (not exists $param{att}) {
229 print {$param{output}} "</blockquote>\n";
231 } elsif (not $param{terse}) {
232 my $content_type = $entity->head->get('Content-Type:') || "text/html";
233 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
234 my $body = $entity->bodyhandle->as_string;
235 $body = convert_to_utf8($body,$charset) if defined $charset;
236 $body = html_escape($body);
237 # Attempt to deal with format=flowed
238 if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
239 $body =~ s{^\ }{}mgo;
240 # we ignore the other things that you can do with
241 # flowed e-mails cause they don't really matter.
244 # We don't html escape here because we escape above;
245 # wierd terminators are because of that
246 $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
247 ((?:\>\;)?[)]?(?:'|\&\#39\;)?[:.\,]?(?:\s|$)) # terminators
248 }{<a href=\"$1\">$1</a>$2}gox;
249 # Add links to bug closures
250 $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)]
253 {bug_links(bug=>$1)}ge;
255 if (defined $config{cve_tracker} and
256 length $config{cve_tracker}
258 # Add links to CVE vulnerabilities (closes #568464)
259 $body =~ s{(CVE-\d{4}-\d{4,})}
260 {<a href="http://$config{cve_tracker}$1">$1</a>}gx;
262 if (not exists $param{att}) {
263 print {$param{output}} qq(<pre class="message">$body</pre>\n);
269 =head2 handle_email_message
271 handle_email_message($record->{text},
273 msg_num => $msg_number,
276 Returns a decoded e-mail message and displays entities/attachments as
282 sub handle_email_message{
283 my ($email,%param) = @_;
286 my $parser = MIME::Parser->new();
287 # Because we are using memory, not tempfiles, there's no need to
288 # clean up here like in Debbugs::MIME
289 $parser->tmp_to_core(1);
290 $parser->output_to_core(1);
291 my $entity = $parser->parse_data( $email);
292 my @attachments = ();
293 display_entity(entity => $entity,
294 bug_num => $param{ref},
296 msg_num => $param{msg_num},
298 attachments => \@attachments,
299 terse => $param{terse},
300 exists $param{msg}?(msg=>$param{msg}):(),
301 exists $param{att}?(att=>$param{att}):(),
302 exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
310 push @log, handle_record($record,$ref,$msg_num);
312 Deals with a record in a bug log as returned by
313 L<Debbugs::Log::read_log_records>; returns the log information that
314 should be output to the browser.
319 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
322 local $_ = $record->{type};
324 my ($time) = $record->{text} =~ /<!--\s+time:(\d+)\s+-->/;
325 my $class = $record->{text} =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
326 $output .= decode_rfc1522($record->{text});
327 # Link to forwarded http:// urls in the midst of the report
328 # (even though these links already exist at the top)
329 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;)?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
330 # Add links to the cloned bugs
331 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
332 # Add links to merged bugs
333 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
334 # Add links to blocked bugs
335 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
336 {(defined $2?$1.bug_links(bug=>$2):'').$3.
337 english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
338 $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
339 (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
340 {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
341 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
342 # Add links to reassigned packages
343 $output =~ s{(Bug reassigned from package \`)([^']+?)((?:'|\&\#39;) to \`)([^']+?)((?:'|\&\#39;))}
344 {$1.q(<a href=").html_escape(pkg_url(pkg=>$2)).qq(">$2</a>).$3.q(<a href=").html_escape(pkg_url(pkg=>$4)).qq(">$4</a>).$5}eo;
346 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
348 $output .= '<a href="' .
349 html_escape(bug_links(bug => $bug_number,
350 options => {msg => ($msg_number+1)},
353 ) . '">Full text</a> and <a href="' .
354 html_escape(bug_links(bug => $bug_number,
355 options => {msg => ($msg_number+1),
358 ) . '">rfc822 format</a> available.';
360 $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
363 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
364 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
367 elsif (defined $msg_id) {
368 $$seen_msg_ids{$msg_id} = 1;
370 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
371 $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>';
372 $output .= handle_email_message($record->{text},
374 msg_num => $msg_number,
377 elsif (/autocheck/) {
380 elsif (/incoming-recv/) {
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 # Incomming Mail Message
389 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
390 $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 |.
391 html_escape("$received\@$hostname") .
392 q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
393 q|, <a href="| . html_escape(bug_links(bug => $bug_number,
395 options => {msg=>$msg_number,
398 ) .'">mbox</a>)'.":</p>\n";
399 $output .= handle_email_message($record->{text},
401 msg_num => $msg_number,
405 die "Unknown record type $_";