]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/CGI/Bugreport.pm
move the function from bugreport.cgi out to a separate module
[debbugs.git] / Debbugs / CGI / Bugreport.pm
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.
4 #
5 # [Other people have contributed to this file; their copyrights should
6 # be listed here too.]
7 # Copyright 2008 by Don Armstrong <don@donarmstrong.com>.
8
9
10 package Debbugs::CGI::Bugreport;
11
12 =head1 NAME
13
14 Debbugs::CGI::Bugreport -- specific routines for the bugreport cgi script
15
16 =head1 SYNOPSIS
17
18
19 =head1 DESCRIPTION
20
21
22 =head1 BUGS
23
24 None known.
25
26 =cut
27
28 use warnings;
29 use strict;
30 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
31 use base qw(Exporter);
32
33 use IO::Scalar;
34
35 BEGIN{
36      ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
37      $DEBUG = 0 unless defined $DEBUG;
38
39      @EXPORT = ();
40      %EXPORT_TAGS = ();
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];
44 }
45
46
47
48 =head2 display_entity
49
50      display_entity(entity      => $entity,
51                     bug_num     => $ref,
52                     outer       => 1,
53                     msg_num     => $msg_num,
54                     attachments => \@attachments,
55                     output      => \$output);
56
57
58 =over
59
60 =item entity -- MIME::Parser entity
61
62 =item bug_num -- Bug number
63
64 =item outer -- Whether this is the outer entity; defaults to 1
65
66 =item msg_num -- message number in the log
67
68 =item attachments -- arrayref of attachments
69
70 =item output -- scalar reference for output
71
72 =back
73
74 =cut
75
76 sub display_entity {
77     my %param = valid_with(params => \@_,
78                            spec   => {entity      => {type => OBJECT,
79                                                      },
80                                       bug_num     => {type => SCALAR,
81                                                       regex => qr/^\d+$/,
82                                                      },
83                                       outer       => {type => BOOLEAN,
84                                                       default => 1,
85                                                       },
86                                       msg_num     => {type => SCALAR,
87                                                      },
88                                       attachments => {type => ARRAYREF,
89                                                       default => [],
90                                                      },
91                                       output      => {type => SCALARREF|HANDLE,
92                                                       default => \*STDOUT,
93                                                      },
94                                       terse       => {type => BOOLEAN,
95                                                       default => 0,
96                                                      },
97                                       msg         => {type => SCALAR,
98                                                       optional => 1,
99                                                      },
100                                       attachment => {type => SCALAR,
101                                                      optional => 1,
102                                                     },
103                                      }
104                           );
105
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});
114     }
115     my $attachments = $param{attachments};
116
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);
124
125     if ($top and not $param{terse}) {
126          my $header = $entity->head;
127          print {$param{output}} "<pre class=\"headers\">\n";
128          if ($trim_headers) {
129               my @headers;
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));
134               }
135               print {$param{output}} join(qq(), @headers);
136          } else {
137               print {$param{output}} html_escape(decode_rfc1522($entity->head->stringify));
138          }
139          print {$param{output}} "</pre>\n";
140     }
141
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";
151
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 '') {
162                 my $qf = $filename;
163                 $qf =~ s/"/\\"/g;
164                 $qf =~ s[.*/][];
165                 print {$param{output}} qq{Content-Disposition: inline; filename="$qf"\n};
166             }
167             print {$param{output}} "\n";
168             my $decoder = MIME::Decoder->new($head->mime_encoding);
169             $decoder->decode(new IO::Scalar(\$body), \*STDOUT);
170             exit(0);
171         }
172     }
173
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
178                   $entity->parts;
179
180     if ($entity->is_multipart) {
181         my @parts = $entity->parts;
182         foreach my $part (@parts) {
183             display_entity(entity => $part,
184                            bug_num => $ref,
185                            outer => 0,
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}):(),
192                           );
193             print {$param{output}} "\n";
194         }
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,
201                            bug_num => $ref,
202                            outer => 1,
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}):(),
209                           );
210             print {$param{output}} "\n";
211         }
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.
224          }
225          # Add links to URLs
226          # We don't html escape here because we escape above
227          $body =~ s{((ftp|http|https)://[\S~-]+?/?)((\&gt\;)?[)]?[']?[:.\,]?(\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+)*)]
231                    [my $temp = $1;
232                     $temp =~ s{(\d+)}
233                               {qq(<a href=").
234                                     html_escape(bug_url($1)).
235                                          qq(">$1</a>)
236                                     }ge;
237                     $temp;]gxie;
238          print {$param{output}} qq(<pre class="message">$body</pre>\n);
239     }
240 }
241
242
243 =head2 handle_email_message
244
245      handle_email_message($record->{text},
246                           ref        => $bug_number,
247                           msg_number => $msg_number,
248                          );
249
250 Returns a decoded e-mail message and displays entities/attachments as
251 appropriate.
252
253
254 =cut
255
256 sub handle_email_message{
257      my ($email,%options) = @_;
258
259      my $output = '';
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},
269                     outer   => 1,
270                     msg_number => $options{msg_number},
271                     ouput => $output,
272                     attachments => \@attachments,
273                     terse       => $params{terse},
274                     exists $param{msg}?(msg=>$param{msg}):(),
275                     exists $param{att}?(attachment=>$param{att}):(),
276                    );
277      return $output;
278
279 }
280
281 =head2 handle_record
282
283      push @log, handle_record($record,$ref,$msg_num);
284
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.
288
289 =cut
290
291 sub handle_record{
292      my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
293
294      my $output = '';
295      local $_ = $record->{type};
296      if (/html/) {
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;
314           if (defined $time) {
315                $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
316           }
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.';
319
320           $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
321      }
322      elsif (/recips/) {
323           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
324           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
325                return ();
326           }
327           elsif (defined $msg_id) {
328                $$seen_msg_ids{$msg_id} = 1;
329           }
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},
333                                     ref        => $bug_number,
334                                     msg_number => $msg_number,
335                                    );
336      }
337      elsif (/autocheck/) {
338           # Do nothing
339      }
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}) {
343                return ();
344           }
345           elsif (defined $msg_id) {
346                $$seen_msg_ids{$msg_id} = 1;
347           }
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},
355                                     ref        => $bug_number,
356                                     msg_number => $msg_number,
357                                    );
358      }
359      else {
360           die "Unknown record type $_";
361      }
362      return $output;
363 }
364
365
366
367 1;
368
369
370 __END__
371
372
373
374
375
376