]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/CGI/Bugreport.pm
* Switch from bug_url to bug_links
[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 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);
38 use POSIX qw(strftime);
39
40 BEGIN{
41      ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
42      $DEBUG = 0 unless defined $DEBUG;
43
44      @EXPORT = ();
45      %EXPORT_TAGS = ();
46      @EXPORT_OK = (qw(display_entities handle_record handle_email_message));
47      Exporter::export_ok_tags(keys %EXPORT_TAGS);
48      $EXPORT_TAGS{all} = [@EXPORT_OK];
49 }
50
51
52
53 =head2 display_entity
54
55      display_entity(entity      => $entity,
56                     bug_num     => $ref,
57                     outer       => 1,
58                     msg_num     => $msg_num,
59                     attachments => \@attachments,
60                     output      => \$output);
61
62
63 =over
64
65 =item entity -- MIME::Parser entity
66
67 =item bug_num -- Bug number
68
69 =item outer -- Whether this is the outer entity; defaults to 1
70
71 =item msg_num -- message number in the log
72
73 =item attachments -- arrayref of attachments
74
75 =item output -- scalar reference for output
76
77 =back
78
79 =cut
80
81 sub display_entity {
82     my %param = validate_with(params => \@_,
83                               spec   => {entity      => {type => OBJECT,
84                                                         },
85                                          bug_num     => {type => SCALAR,
86                                                          regex => qr/^\d+$/,
87                                                         },
88                                          outer       => {type => BOOLEAN,
89                                                          default => 1,
90                                                         },
91                                          msg_num     => {type => SCALAR,
92                                                         },
93                                          attachments => {type => ARRAYREF,
94                                                          default => [],
95                                                         },
96                                          output      => {type => SCALARREF|HANDLE,
97                                                          default => \*STDOUT,
98                                                         },
99                                          terse       => {type => BOOLEAN,
100                                                          default => 0,
101                                                         },
102                                          msg         => {type => SCALAR,
103                                                          optional => 1,
104                                                         },
105                                          att         => {type => SCALAR,
106                                                          optional => 1,
107                                                         },
108                                          trim_headers => {type => BOOLEAN,
109                                                           default => 1,
110                                                          },
111                                         }
112                              );
113
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};
120
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);
128
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}) {
135               my @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));
140               }
141               print {$param{output}} join(qq(), @headers);
142          } else {
143               print {$param{output}} html_escape(decode_rfc1522($entity->head->stringify));
144          }
145          print {$param{output}} "</pre>\n";
146     }
147
148     if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
149              or $type =~ m{^multipart/}
150             )) {
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 '') {
163                 my $qf = $filename;
164                 $qf =~ s/"/\\"/g;
165                 $qf =~ s[.*/][];
166                 print {$param{output}} qq{Content-Disposition: inline; filename="$qf"\n};
167             }
168             print {$param{output}} "\n";
169             my $decoder = MIME::Decoder->new($head->mime_encoding);
170             $decoder->decode(IO::Scalar->new(\$body), $param{output});
171             return;
172         }
173         elsif (not exists $param{att}) {
174              my @dlargs = ($ref, 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="' . html_escape(bug_url(@dlargs)) . qq{">$printname</a> } .
179                   "($type, $disposition)]</pre>\n";
180         }
181     }
182
183     return if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
184     return unless ($type =~ m[^text/?] and
185                    $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
186                   $type =~ m[^application/pgp(?:;|$)] or
187                   $entity->parts;
188
189     if ($entity->is_multipart) {
190         my @parts = $entity->parts;
191         foreach my $part (@parts) {
192             display_entity(entity => $part,
193                            bug_num => $ref,
194                            outer => 0,
195                            msg_num => $xmessage,
196                            output => $param{output},
197                            attachments => $attachments,
198                            terse => $param{terse},
199                            exists $param{msg}?(msg=>$param{msg}):(),
200                            exists $param{att}?(att=>$param{att}):(),
201                           );
202             # print {$param{output}} "\n";
203         }
204     } elsif ($entity->parts) {
205         # We must be dealing with a nested message.
206          if (not exists $param{att}) {
207               print {$param{output}} "<blockquote>\n";
208          }
209         my @parts = $entity->parts;
210         foreach my $part (@parts) {
211             display_entity(entity => $part,
212                            bug_num => $ref,
213                            outer => 1,
214                            msg_num => $xmessage,
215                            ouput => $param{output},
216                            attachments => $attachments,
217                            terse => $param{terse},
218                            exists $param{msg}?(msg=>$param{msg}):(),
219                            exists $param{att}?(att=>$param{att}):(),
220                           );
221             # print {$param{output}} "\n";
222         }
223          if (not exists $param{att}) {
224               print {$param{output}} "</blockquote>\n";
225          }
226     } elsif (not $param{terse}) {
227          my $content_type = $entity->head->get('Content-Type:') || "text/html";
228          my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
229          my $body = $entity->bodyhandle->as_string;
230          $body = convert_to_utf8($body,$charset) if defined $charset;
231          $body = html_escape($body);
232          # Attempt to deal with format=flowed
233          if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
234               $body =~ s{^\ }{}mgo;
235               # we ignore the other things that you can do with
236               # flowed e-mails cause they don't really matter.
237          }
238          # Add links to URLs
239          # We don't html escape here because we escape above;
240          # wierd terminators are because of that
241          $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
242                     ((?:\&gt\;)?[)]?(?:'|\&\#39\;)?[:.\,]?(?:\s|$)) # terminators
243               }{<a href=\"$1\">$1</a>$2}gox;
244          # Add links to bug closures
245          $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)]
246                    [my $temp = $1;
247                     $temp =~ s{(\d+)}
248                               {bug_links(bug=>$1)}ge;
249                     $temp;]gxie;
250
251          if (not exists $param{att}) {
252               print {$param{output}} qq(<pre class="message">$body</pre>\n);
253          }
254     }
255 }
256
257
258 =head2 handle_email_message
259
260      handle_email_message($record->{text},
261                           ref        => $bug_number,
262                           msg_num => $msg_number,
263                          );
264
265 Returns a decoded e-mail message and displays entities/attachments as
266 appropriate.
267
268
269 =cut
270
271 sub handle_email_message{
272      my ($email,%param) = @_;
273
274      my $output = '';
275      my $parser = MIME::Parser->new();
276      # Because we are using memory, not tempfiles, there's no need to
277      # clean up here like in Debbugs::MIME
278      $parser->tmp_to_core(1);
279      $parser->output_to_core(1);
280      my $entity = $parser->parse_data( $email);
281      my @attachments = ();
282      display_entity(entity  => $entity,
283                     bug_num => $param{ref},
284                     outer   => 1,
285                     msg_num => $param{msg_num},
286                     output => \$output,
287                     attachments => \@attachments,
288                     terse       => $param{terse},
289                     exists $param{msg}?(msg=>$param{msg}):(),
290                     exists $param{att}?(att=>$param{att}):(),
291                    );
292      return $output;
293
294 }
295
296 =head2 handle_record
297
298      push @log, handle_record($record,$ref,$msg_num);
299
300 Deals with a record in a bug log as returned by
301 L<Debbugs::Log::read_log_records>; returns the log information that
302 should be output to the browser.
303
304 =cut
305
306 sub handle_record{
307      my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
308
309      my $output = '';
310      local $_ = $record->{type};
311      if (/html/) {
312           my ($time) = $record->{text} =~ /<!--\s+time:(\d+)\s+-->/;
313           my $class = $record->{text} =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/ ? 'infmessage':'msgreceived';
314           $output .= decode_rfc1522($record->{text});
315           # Link to forwarded http:// urls in the midst of the report
316           # (even though these links already exist at the top)
317           $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)([\)\'\:\.\,]?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
318           # Add links to the cloned bugs
319           $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>[$4..$5])}eo;
320           # Add links to merged bugs
321           $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
322           # Add links to blocked bugs
323           $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
324                       {(defined $2?$1.bug_links(bug=>$2):'').$3.
325                             join(' ',map {bug_links(bug=>$_)} (split /\,?\s+/, $4))}eo;
326           # Add links to reassigned packages
327           $output =~ s{(Bug reassigned from package \`)([^']+?)((?:'|\&\#39;) to \`)([^']+?)((?:'|\&\#39;))}
328           {$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;
329           if (defined $time) {
330                $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
331           }
332           $output .= '<a href="' . html_escape(bug_links($bug_number, options => {msg => ($msg_number+1)},links_only => 1)) . '">Full text</a> and <a href="' .
333                html_escape(html_escape(bug_links($bug_number, options => {msg => ($msg_number+1),mbox => 'yes'},links_only => 1)) . '">rfc822 format</a> available.';
334
335           $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
336      }
337      elsif (/recips/) {
338           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
339           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
340                return ();
341           }
342           elsif (defined $msg_id) {
343                $$seen_msg_ids{$msg_id} = 1;
344           }
345           $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
346           $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>';
347           $output .= handle_email_message($record->{text},
348                                           ref     => $bug_number,
349                                           msg_num => $msg_number,
350                                          );
351      }
352      elsif (/autocheck/) {
353           # Do nothing
354      }
355      elsif (/incoming-recv/) {
356           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
357           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
358                return ();
359           }
360           elsif (defined $msg_id) {
361                $$seen_msg_ids{$msg_id} = 1;
362           }
363           # Incomming Mail Message
364           my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
365           $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 |.
366                html_escape("$received\@$hostname") .
367                     q| (<a href="| . html_escape(bug_url($bug_number, msg=>$msg_number)) . '">full text</a>'.
368                          q|, <a href="| . html_escape(bug_url($bug_number, msg=>$msg_number,mbox=>'yes')) .'">mbox</a>)'.":</p>\n";
369           $output .= handle_email_message($record->{text},
370                                           ref     => $bug_number,
371                                           msg_num => $msg_number,
372                                          );
373      }
374      else {
375           die "Unknown record type $_";
376      }
377      return $output;
378 }
379
380
381
382 1;
383
384
385 __END__
386
387
388
389
390
391