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 POSIX qw(strftime);
41 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
42 $DEBUG = 0 unless defined $DEBUG;
46 @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
47 Exporter::export_ok_tags(keys %EXPORT_TAGS);
48 $EXPORT_TAGS{all} = [@EXPORT_OK];
55 display_entity(entity => $entity,
59 attachments => \@attachments,
65 =item entity -- MIME::Parser entity
67 =item bug_num -- Bug number
69 =item outer -- Whether this is the outer entity; defaults to 1
71 =item msg_num -- message number in the log
73 =item attachments -- arrayref of attachments
75 =item output -- scalar reference for output
82 my %param = validate_with(params => \@_,
83 spec => {entity => {type => OBJECT,
85 bug_num => {type => SCALAR,
88 outer => {type => BOOLEAN,
91 msg_num => {type => SCALAR,
93 attachments => {type => ARRAYREF,
96 output => {type => SCALARREF|HANDLE,
99 terse => {type => BOOLEAN,
102 msg => {type => SCALAR,
105 att => {type => SCALAR,
108 trim_headers => {type => BOOLEAN,
114 $param{output} = globify_scalar($param{output});
115 my $entity = $param{entity};
116 my $ref = $param{bug_num};
117 my $top = $param{outer};
118 my $xmessage = $param{msg_num};
119 my $attachments = $param{attachments};
121 my $head = $entity->head;
122 my $disposition = $head->mime_attr('content-disposition');
123 $disposition = 'inline' if not defined $disposition or $disposition eq '';
124 my $type = $entity->effective_type;
125 my $filename = $entity->head->recommended_filename;
126 $filename = '' unless defined $filename;
127 $filename = decode_rfc1522($filename);
129 if ($param{outer} and
130 not $param{terse} and
131 not exists $param{att}) {
132 my $header = $entity->head;
133 print {$param{output}} "<pre class=\"headers\">\n";
134 if ($param{trim_headers}) {
136 foreach (qw(From To Cc Subject Date)) {
137 my $head_field = $head->get($_);
138 next unless defined $head_field and $head_field ne '';
139 push @headers, qq(<b>$_:</b> ) . html_escape(decode_rfc1522($head_field));
141 print {$param{output}} join(qq(), @headers);
143 print {$param{output}} html_escape(decode_rfc1522($entity->head->stringify));
145 print {$param{output}} "</pre>\n";
148 if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
149 or $type =~ m{^multipart/}
151 push @$attachments, $param{entity};
152 # output this attachment
153 if (exists $param{att} and
154 $param{att} == $#$attachments) {
155 my $head = $entity->head;
156 chomp(my $type = $entity->effective_type);
157 my $body = $entity->stringify_body;
158 print {$param{output}} "Content-Type: $type";
159 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
160 print {$param{output}} qq(; charset="$charset") if defined $charset;
161 print {$param{output}}"\n";
162 if ($filename ne '') {
166 print {$param{output}} qq{Content-Disposition: inline; filename="$qf"\n};
168 print {$param{output}} "\n";
169 my $decoder = MIME::Decoder->new($head->mime_encoding);
170 $decoder->decode(IO::Scalar->new(\$body), $param{output});
173 elsif (not exists $param{att}) {
174 my @dlargs = (msg=>$xmessage, att=>$#$attachments);
175 push @dlargs, (filename=>$filename) if $filename ne '';
176 my $printname = $filename;
177 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
178 print {$param{output}} '<pre class="mime">[<a href="' .
179 html_escape(bug_links(bug => $ref,
181 options => {@dlargs})
182 ) . qq{">$printname</a> } .
183 "($type, $disposition)]</pre>\n";
187 return if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
188 return unless ($type =~ m[^text/?] and
189 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
190 $type =~ m[^application/pgp(?:;|$)] or
193 if ($entity->is_multipart) {
194 my @parts = $entity->parts;
195 foreach my $part (@parts) {
196 display_entity(entity => $part,
199 msg_num => $xmessage,
200 output => $param{output},
201 attachments => $attachments,
202 terse => $param{terse},
203 exists $param{msg}?(msg=>$param{msg}):(),
204 exists $param{att}?(att=>$param{att}):(),
206 # print {$param{output}} "\n";
208 } elsif ($entity->parts) {
209 # We must be dealing with a nested message.
210 if (not exists $param{att}) {
211 print {$param{output}} "<blockquote>\n";
213 my @parts = $entity->parts;
214 foreach my $part (@parts) {
215 display_entity(entity => $part,
218 msg_num => $xmessage,
219 output => $param{output},
220 attachments => $attachments,
221 terse => $param{terse},
222 exists $param{msg}?(msg=>$param{msg}):(),
223 exists $param{att}?(att=>$param{att}):(),
225 # print {$param{output}} "\n";
227 if (not exists $param{att}) {
228 print {$param{output}} "</blockquote>\n";
230 } elsif (not $param{terse}) {
231 my $content_type = $entity->head->get('Content-Type:') || "text/html";
232 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
233 my $body = $entity->bodyhandle->as_string;
234 $body = convert_to_utf8($body,$charset) if defined $charset;
235 $body = html_escape($body);
236 # Attempt to deal with format=flowed
237 if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
238 $body =~ s{^\ }{}mgo;
239 # we ignore the other things that you can do with
240 # flowed e-mails cause they don't really matter.
243 # We don't html escape here because we escape above;
244 # wierd terminators are because of that
245 $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
246 ((?:\>\;)?[)]?(?:'|\&\#39\;)?[:.\,]?(?:\s|$)) # terminators
247 }{<a href=\"$1\">$1</a>$2}gox;
248 # Add links to bug closures
249 $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)]
252 {bug_links(bug=>$1)}ge;
255 if (not exists $param{att}) {
256 print {$param{output}} qq(<pre class="message">$body</pre>\n);
262 =head2 handle_email_message
264 handle_email_message($record->{text},
266 msg_num => $msg_number,
269 Returns a decoded e-mail message and displays entities/attachments as
275 sub handle_email_message{
276 my ($email,%param) = @_;
279 my $parser = MIME::Parser->new();
280 # Because we are using memory, not tempfiles, there's no need to
281 # clean up here like in Debbugs::MIME
282 $parser->tmp_to_core(1);
283 $parser->output_to_core(1);
284 my $entity = $parser->parse_data( $email);
285 my @attachments = ();
286 display_entity(entity => $entity,
287 bug_num => $param{ref},
289 msg_num => $param{msg_num},
291 attachments => \@attachments,
292 terse => $param{terse},
293 exists $param{msg}?(msg=>$param{msg}):(),
294 exists $param{att}?(att=>$param{att}):(),
295 exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
303 push @log, handle_record($record,$ref,$msg_num);
305 Deals with a record in a bug log as returned by
306 L<Debbugs::Log::read_log_records>; returns the log information that
307 should be output to the browser.
312 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
315 local $_ = $record->{type};
317 my ($time) = $record->{text} =~ /<!--\s+time:(\d+)\s+-->/;
318 my $class = $record->{text} =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
319 $output .= decode_rfc1522($record->{text});
320 # Link to forwarded http:// urls in the midst of the report
321 # (even though these links already exist at the top)
322 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;)?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
323 # Add links to the cloned bugs
324 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
325 # Add links to merged bugs
326 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
327 # Add links to blocked bugs
328 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
329 {(defined $2?$1.bug_links(bug=>$2):'').$3.
330 english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
331 $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
332 (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
333 {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
334 english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
335 # Add links to reassigned packages
336 $output =~ s{(Bug reassigned from package \`)([^']+?)((?:'|\&\#39;) to \`)([^']+?)((?:'|\&\#39;))}
337 {$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;
339 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
341 $output .= '<a href="' .
342 html_escape(bug_links(bug => $bug_number,
343 options => {msg => ($msg_number+1)},
346 ) . '">Full text</a> and <a href="' .
347 html_escape(bug_links(bug => $bug_number,
348 options => {msg => ($msg_number+1),
351 ) . '">rfc822 format</a> available.';
353 $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
356 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
357 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
360 elsif (defined $msg_id) {
361 $$seen_msg_ids{$msg_id} = 1;
363 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
364 $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>';
365 $output .= handle_email_message($record->{text},
367 msg_num => $msg_number,
370 elsif (/autocheck/) {
373 elsif (/incoming-recv/) {
374 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
375 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
378 elsif (defined $msg_id) {
379 $$seen_msg_ids{$msg_id} = 1;
381 # Incomming Mail Message
382 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
383 $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 |.
384 html_escape("$received\@$hostname") .
385 q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
386 q|, <a href="| . html_escape(bug_links(bug => $bug_number,
388 options => {msg=>$msg_number,
391 ) .'">mbox</a>)'.":</p>\n";
392 $output .= handle_email_message($record->{text},
394 msg_num => $msg_number,
398 die "Unknown record type $_";