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);
36 ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
37 $DEBUG = 0 unless defined $DEBUG;
41 @EXPORT_OK = (qw(display_entities handle_record handle_email_message));
42 Exporter::export_ok_tags(keys %EXPORT_TAGS);
43 $EXPORT_TAGS{all} = [@EXPORT_OK];
50 display_entity(entity => $entity,
54 attachments => \@attachments,
60 =item entity -- MIME::Parser entity
62 =item bug_num -- Bug number
64 =item outer -- Whether this is the outer entity; defaults to 1
66 =item msg_num -- message number in the log
68 =item attachments -- arrayref of attachments
70 =item output -- scalar reference for output
77 my %param = valid_with(params => \@_,
78 spec => {entity => {type => OBJECT,
80 bug_num => {type => SCALAR,
83 outer => {type => BOOLEAN,
86 msg_num => {type => SCALAR,
88 attachments => {type => ARRAYREF,
91 output => {type => SCALARREF|HANDLE,
94 terse => {type => BOOLEAN,
97 msg => {type => SCALAR,
100 attachment => {type => SCALAR,
106 my $entity = $param{entity};
107 my $ref = $param{bug_num};
108 my $top = $param{outer}
109 my $xmessage = $param{msg_num};
110 if (defined ref($options) and
111 ref($param{output}) eq 'SCALAR' and
112 not UNIVERSAL::isa($param{output},'GLOB')) {
113 $param{output} = IO::Scalar->new($param{output});
115 my $attachments = $param{attachments};
117 my $head = $entity->head;
118 my $disposition = $head->mime_attr('content-disposition');
119 $disposition = 'inline' if not defined $disposition or $disposition eq '';
120 my $type = $entity->effective_type;
121 my $filename = $entity->head->recommended_filename;
122 $filename = '' unless defined $filename;
123 $filename = decode_rfc1522($filename);
125 if ($top and not $param{terse}) {
126 my $header = $entity->head;
127 print {$param{output}} "<pre class=\"headers\">\n";
130 foreach (qw(From To Cc Subject Date)) {
131 my $head_field = $head->get($_);
132 next unless defined $head_field and $head_field ne '';
133 push @headers, qq(<b>$_:</b> ) . html_escape(decode_rfc1522($head_field));
135 print {$param{output}} join(qq(), @headers);
137 print {$param{output}} html_escape(decode_rfc1522($entity->head->stringify));
139 print {$param{output}} "</pre>\n";
142 unless (($top and $type =~ m[^text(?:/plain)?(?:;|$)]) or
143 ($type =~ m[^multipart/])) {
144 push @$attachments, $entity;
145 my @dlargs = ($ref, msg=>$xmessage, att=>$#$attachments);
146 push @dlargs, (filename=>$filename) if $filename ne '';
147 my $printname = $filename;
148 $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
149 print {$param{output}} '<pre class="mime">[<a href="' . html_escape(bug_url(@dlargs)) . qq{">$printname</a> } .
150 "($type, $disposition)]</pre>\n";
152 if (exists $param{msg} and exists $param{att} and
153 $att == $#$attachments) {
154 my $head = $entity->head;
155 chomp(my $type = $entity->effective_type);
156 my $body = $entity->stringify_body;
157 print {$param{output}} "Content-Type: $type";
158 my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
159 print {$param{output}} qq(; charset="$charset") if defined $charset;
160 print {$param{output}}"\n";
161 if ($filename ne '') {
165 print {$param{output}} qq{Content-Disposition: inline; filename="$qf"\n};
167 print {$param{output}} "\n";
168 my $decoder = MIME::Decoder->new($head->mime_encoding);
169 $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
174 return if not $top and $disposition eq 'attachment' and not defined($att);
175 return unless ($type =~ m[^text/?] and
176 $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
177 $type =~ m[^application/pgp(?:;|$)] or
180 if ($entity->is_multipart) {
181 my @parts = $entity->parts;
182 foreach my $part (@parts) {
183 display_entity(entity => $part,
186 msg_num => $xmessage,
187 output => $param{output},
188 attachments => $attachments,
189 terse => $param{terse},
190 exists $param{msg}?(msg=>$param{msg}):(),
191 exists $param{attachment}?(attachment=>$param{attachment}):(),
193 print {$param{output}} "\n";
195 } elsif ($entity->parts) {
196 # We must be dealing with a nested message.
197 print {$param{output}} "<blockquote>\n";
198 my @parts = $entity->parts;
199 foreach my $part (@parts) {
200 display_entity(entity => $part,
203 msg_num => $xmessage,
204 ouput => $param{output},
205 attachments => $attachments,
206 terse => $param{terse},
207 exists $param{msg}?(msg=>$param{msg}):(),
208 exists $param{attachment}?(attachment=>$param{attachment}):(),
210 print {$param{output}} "\n";
212 print {$param{output}} "</blockquote>\n";
213 } elsif (not $param{terse}) {
214 my $content_type = $entity->head->get('Content-Type:') || "text/html";
215 my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
216 my $body = $entity->bodyhandle->as_string;
217 $body = convert_to_utf8($body,$charset) if defined $charset;
218 $body = html_escape($body);
219 # Attempt to deal with format=flowed
220 if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
221 $body =~ s{^\ }{}mgo;
222 # we ignore the other things that you can do with
223 # flowed e-mails cause they don't really matter.
226 # We don't html escape here because we escape above
227 $body =~ s{((ftp|http|https)://[\S~-]+?/?)((\>\;)?[)]?[']?[:.\,]?(\s|$))}
228 {<a href=\"$1\">$1</a>$3}go;
229 # Add links to bug closures
230 $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)]
234 html_escape(bug_url($1)).
238 print {$param{output}} qq(<pre class="message">$body</pre>\n);
243 =head2 handle_email_message
245 handle_email_message($record->{text},
247 msg_number => $msg_number,
250 Returns a decoded e-mail message and displays entities/attachments as
256 sub handle_email_message{
257 my ($email,%options) = @_;
260 my $parser = new MIME::Parser;
261 # Because we are using memory, not tempfiles, there's no need to
262 # clean up here like in Debbugs::MIME
263 $parser->tmp_to_core(1);
264 $parser->output_to_core(1);
265 my $entity = $parser->parse_data( $email);
266 my @attachments = ();
267 display_entity(entity => $entity,
268 bug_num => $options{ref},
270 msg_number => $options{msg_number},
272 attachments => \@attachments,
273 terse => $params{terse},
274 exists $param{msg}?(msg=>$param{msg}):(),
275 exists $param{att}?(attachment=>$param{att}):(),
283 push @log, handle_record($record,$ref,$msg_num);
285 Deals with a record in a bug log as returned by
286 L<Debbugs::Log::read_log_records>; returns the log information that
287 should be output to the browser.
292 my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
295 local $_ = $record->{type};
297 my ($time) = $record->{text} =~ /<!--\s+time:(\d+)\s+-->/;
298 my $class = $record->{text} =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/ ? 'infmessage':'msgreceived';
299 $output .= decode_rfc1522($record->{text});
300 # Link to forwarded http:// urls in the midst of the report
301 # (even though these links already exist at the top)
302 $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
303 # Add links to the cloned bugs
304 $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links($2).$3.bug_links($4,$5)}eo;
305 # Add links to merged bugs
306 $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links($_)} (split /\s+/, $1))}eo;
307 # Add links to blocked bugs
308 $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
309 {(defined $2?$1.bug_links($2):'').$3.
310 join(' ',map {bug_links($_)} (split /\,?\s+/, $4))}eo;
311 # Add links to reassigned packages
312 $output =~ s{(Bug reassigned from package \`)([^']+?)((?:'|\&\#39;) to \`)([^']+?)((?:'|\&\#39;))}
313 {$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;
315 $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
317 $output .= '<a href="' . html_escape(bug_url($ref, msg => ($msg_number+1))) . '">Full text</a> and <a href="' .
318 html_escape(bug_url($ref, msg => ($msg_number+1), mbox => 'yes')) . '">rfc822 format</a> available.';
320 $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
323 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
324 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
327 elsif (defined $msg_id) {
328 $$seen_msg_ids{$msg_id} = 1;
330 $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
331 $output .= 'View this message in <a href="' . html_escape(bug_url($ref, msg=>$msg_number, mbox=>'yes')) . '">rfc822 format</a></p>';
332 $output .= handle_email_message($record->{text},
334 msg_number => $msg_number,
337 elsif (/autocheck/) {
340 elsif (/incoming-recv/) {
341 my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
342 if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
345 elsif (defined $msg_id) {
346 $$seen_msg_ids{$msg_id} = 1;
348 # Incomming Mail Message
349 my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
350 $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 |.
351 html_escape("$received\@$hostname") .
352 q| (<a href="| . html_escape(bug_url($ref, msg=>$msg_number)) . '">full text</a>'.
353 q|, <a href="| . html_escape(bug_url($ref, msg=>$msg_number,mbox=>'yes')) .'">mbox</a>)'.":</p>\n";
354 $output .= handle_email_message($record->{text},
356 msg_number => $msg_number,
360 die "Unknown record type $_";