]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/CGI/Bugreport.pm
add libravatar support to the bts
[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 Digest::MD5 qw(md5_hex);
36 use Debbugs::Mail qw(get_addresses);
37 use Debbugs::MIME qw(convert_to_utf8 decode_rfc1522 create_mime_message);
38 use Debbugs::CGI qw(:url :html :util);
39 use Debbugs::Common qw(globify_scalar english_join);
40 use Debbugs::Config qw(:config);
41 use POSIX qw(strftime);
42 use Encode qw(decode_utf8);
43
44 BEGIN{
45      ($VERSION) = q$Revision: 494 $ =~ /^Revision:\s+([^\s+])/;
46      $DEBUG = 0 unless defined $DEBUG;
47
48      @EXPORT = ();
49      %EXPORT_TAGS = ();
50      @EXPORT_OK = (qw(display_entity handle_record handle_email_message));
51      Exporter::export_ok_tags(keys %EXPORT_TAGS);
52      $EXPORT_TAGS{all} = [@EXPORT_OK];
53 }
54
55
56
57 =head2 display_entity
58
59      display_entity(entity      => $entity,
60                     bug_num     => $ref,
61                     outer       => 1,
62                     msg_num     => $msg_num,
63                     attachments => \@attachments,
64                     output      => \$output);
65
66
67 =over
68
69 =item entity -- MIME::Parser entity
70
71 =item bug_num -- Bug number
72
73 =item outer -- Whether this is the outer entity; defaults to 1
74
75 =item msg_num -- message number in the log
76
77 =item attachments -- arrayref of attachments
78
79 =item output -- scalar reference for output
80
81 =back
82
83 =cut
84
85 sub display_entity {
86     my %param = validate_with(params => \@_,
87                               spec   => {entity      => {type => OBJECT,
88                                                         },
89                                          bug_num     => {type => SCALAR,
90                                                          regex => qr/^\d+$/,
91                                                         },
92                                          outer       => {type => BOOLEAN,
93                                                          default => 1,
94                                                         },
95                                          msg_num     => {type => SCALAR,
96                                                         },
97                                          attachments => {type => ARRAYREF,
98                                                          default => [],
99                                                         },
100                                          output      => {type => SCALARREF|HANDLE,
101                                                          default => \*STDOUT,
102                                                         },
103                                          terse       => {type => BOOLEAN,
104                                                          default => 0,
105                                                         },
106                                          msg         => {type => SCALAR,
107                                                          optional => 1,
108                                                         },
109                                          att         => {type => SCALAR,
110                                                          optional => 1,
111                                                         },
112                                          trim_headers => {type => BOOLEAN,
113                                                           default => 1,
114                                                          },
115                                         }
116                              );
117
118     $param{output} = globify_scalar($param{output});
119     my $entity = $param{entity};
120     my $ref = $param{bug_num};
121     my $top = $param{outer};
122     my $xmessage = $param{msg_num};
123     my $attachments = $param{attachments};
124
125     my $head = $entity->head;
126     my $disposition = $head->mime_attr('content-disposition');
127     $disposition = 'inline' if not defined $disposition or $disposition eq '';
128     my $type = $entity->effective_type;
129     my $filename = $entity->head->recommended_filename;
130     $filename = '' unless defined $filename;
131     $filename = decode_rfc1522($filename);
132
133     if ($param{outer} and
134         not $param{terse} and
135         not exists $param{att}) {
136          my $header = $entity->head;
137          print {$param{output}} "<div class=\"headers\">\n";
138          if ($param{trim_headers}) {
139               my @headers;
140               foreach (qw(From To Cc Subject Date)) {
141                    my $head_field = $head->get($_);
142                    next unless defined $head_field and $head_field ne '';
143                    if ($_ eq 'From') {
144                        push @headers,q(<img src=").__libravatar_url(decode_rfc1522($head_field)).q(">);
145                    }
146                    push @headers, qq(<p><span class="header">$_:</span> ) . html_escape(decode_rfc1522($head_field))."</p>";
147               }
148               print {$param{output}} join(qq(), @headers);
149          } else {
150               print {$param{output}} "<pre>".html_escape(decode_rfc1522($entity->head->stringify))."</pre>\n";
151          }
152          print {$param{output}} "</div>\n";
153     }
154
155     if (not (($param{outer} and $type =~ m{^text(?:/plain)?(?:;|$)})
156              or $type =~ m{^multipart/}
157             )) {
158         push @$attachments, $param{entity};
159         # output this attachment
160         if (exists $param{att} and
161             $param{att} == $#$attachments) {
162             my $head = $entity->head;
163             chomp(my $type = $entity->effective_type);
164             my $body = $entity->stringify_body;
165             # this attachment has its own content type, so we must not
166             # try to convert it to UTF-8 or do anything funky.
167             my @layers = PerlIO::get_layers($param{output});
168             binmode($param{output},':raw');
169             print {$param{output}} "Content-Type: $type";
170             my ($charset) = $head->get('Content-Type:') =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
171             print {$param{output}} qq(; charset="$charset") if defined $charset;
172             print {$param{output}} "\n";
173             if ($filename ne '') {
174                 my $qf = $filename;
175                 $qf =~ s/"/\\"/g;
176                 $qf =~ s[.*/][];
177                 print {$param{output}} qq{Content-Disposition: inline; filename="$qf"\n};
178             }
179             print {$param{output}} "\n";
180             my $decoder = MIME::Decoder->new($head->mime_encoding);
181             $decoder->decode(IO::Scalar->new(\$body), $param{output});
182             if (grep {/utf8/} @layers) {
183                 binmode($param{output},':utf8');
184             }
185             return;
186         }
187         elsif (not exists $param{att}) {
188              my @dlargs = (msg=>$xmessage, att=>$#$attachments);
189              push @dlargs, (filename=>$filename) if $filename ne '';
190              my $printname = $filename;
191              $printname = 'Message part ' . ($#$attachments + 1) if $filename eq '';
192              print {$param{output}} '<pre class="mime">[<a href="' .
193                   html_escape(bug_links(bug => $ref,
194                                         links_only => 1,
195                                         options => {@dlargs})
196                              ) . qq{">$printname</a> } .
197                                   "($type, $disposition)]</pre>\n";
198         }
199     }
200
201     return if not $param{outer} and $disposition eq 'attachment' and not exists $param{att};
202     return unless ($type =~ m[^text/?] and
203                    $type !~ m[^text/(?:html|enriched)(?:;|$)]) or
204                   $type =~ m[^application/pgp(?:;|$)] or
205                   $entity->parts;
206
207     if ($entity->is_multipart) {
208         my @parts = $entity->parts;
209         foreach my $part (@parts) {
210             display_entity(entity => $part,
211                            bug_num => $ref,
212                            outer => 0,
213                            msg_num => $xmessage,
214                            output => $param{output},
215                            attachments => $attachments,
216                            terse => $param{terse},
217                            exists $param{msg}?(msg=>$param{msg}):(),
218                            exists $param{att}?(att=>$param{att}):(),
219                           );
220             # print {$param{output}} "\n";
221         }
222     } elsif ($entity->parts) {
223         # We must be dealing with a nested message.
224          if (not exists $param{att}) {
225               print {$param{output}} "<blockquote>\n";
226          }
227         my @parts = $entity->parts;
228         foreach my $part (@parts) {
229             display_entity(entity => $part,
230                            bug_num => $ref,
231                            outer => 1,
232                            msg_num => $xmessage,
233                            output => $param{output},
234                            attachments => $attachments,
235                            terse => $param{terse},
236                            exists $param{msg}?(msg=>$param{msg}):(),
237                            exists $param{att}?(att=>$param{att}):(),
238                           );
239             # print {$param{output}} "\n";
240         }
241          if (not exists $param{att}) {
242               print {$param{output}} "</blockquote>\n";
243          }
244     } elsif (not $param{terse}) {
245          my $content_type = $entity->head->get('Content-Type:') || "text/html";
246          my ($charset) = $content_type =~ m/charset\s*=\s*\"?([\w-]+)\"?/i;
247          my $body = $entity->bodyhandle->as_string;
248          $body = convert_to_utf8($body,$charset//'utf8');
249          $body = html_escape($body);
250          # Attempt to deal with format=flowed
251          if ($content_type =~ m/format\s*=\s*\"?flowed\"?/i) {
252               $body =~ s{^\ }{}mgo;
253               # we ignore the other things that you can do with
254               # flowed e-mails cause they don't really matter.
255          }
256          # Add links to URLs
257          # We don't html escape here because we escape above;
258          # wierd terminators are because of that
259          $body =~ s{((?:ftp|http|https|svn|ftps|rsync)://[\S~-]+?/?) # Url
260                     ((?:\&gt\;)?[)]?(?:'|\&\#39\;)?[:.\,]?(?:\s|$)) # terminators
261               }{<a href=\"$1\">$1</a>$2}gox;
262          # Add links to bug closures
263          $body =~ s[(closes:\s*(?:bug)?\#?\s?\d+(?:,?\s*(?:bug)?\#?\s?\d+)*)]
264                    [my $temp = $1;
265                     $temp =~ s{(\d+)}
266                               {bug_links(bug=>$1)}ge;
267                     $temp;]gxie;
268          if (defined $config{cve_tracker} and
269              length $config{cve_tracker}
270             ) {
271              # Add links to CVE vulnerabilities (closes #568464)
272              $body =~ s{(^|\s)(CVE-\d{4}-\d{4,})(\s|[,.-\[\]]|$)}
273                        {$1<a href="http://$config{cve_tracker}$2">$2</a>$3}gxm;
274          }
275          if (not exists $param{att}) {
276               print {$param{output}} qq(<pre class="message">$body</pre>\n);
277          }
278     }
279 }
280
281
282 =head2 handle_email_message
283
284      handle_email_message($record->{text},
285                           ref        => $bug_number,
286                           msg_num => $msg_number,
287                          );
288
289 Returns a decoded e-mail message and displays entities/attachments as
290 appropriate.
291
292
293 =cut
294
295 sub handle_email_message{
296      my ($email,%param) = @_;
297
298      # output needs to have the is_utf8 flag on to avoid double
299      # encoding
300      my $output = decode_utf8('');
301      my $parser = MIME::Parser->new();
302      # Because we are using memory, not tempfiles, there's no need to
303      # clean up here like in Debbugs::MIME
304      $parser->tmp_to_core(1);
305      $parser->output_to_core(1);
306      my $entity = $parser->parse_data( $email);
307      my @attachments = ();
308      display_entity(entity  => $entity,
309                     bug_num => $param{ref},
310                     outer   => 1,
311                     msg_num => $param{msg_num},
312                     output => \$output,
313                     attachments => \@attachments,
314                     terse       => $param{terse},
315                     exists $param{msg}?(msg=>$param{msg}):(),
316                     exists $param{att}?(att=>$param{att}):(),
317                     exists $param{trim_headers}?(trim_headers=>$param{trim_headers}):(),
318                    );
319      return $output;
320
321 }
322
323 =head2 handle_record
324
325      push @log, handle_record($record,$ref,$msg_num);
326
327 Deals with a record in a bug log as returned by
328 L<Debbugs::Log::read_log_records>; returns the log information that
329 should be output to the browser.
330
331 =cut
332
333 sub handle_record{
334      my ($record,$bug_number,$msg_number,$seen_msg_ids) = @_;
335
336      # output needs to have the is_utf8 flag on to avoid double
337      # encoding
338      my $output = decode_utf8('');
339      local $_ = $record->{type};
340      if (/html/) {
341          # $record->{text} is not in perl's internal encoding; convert it
342          my $text = decode_utf8(decode_rfc1522($record->{text}));
343           my ($time) = $text =~ /<!--\s+time:(\d+)\s+-->/;
344           my $class = $text =~ /^<strong>(?:Acknowledgement|Reply|Information|Report|Notification)/m ? 'infmessage':'msgreceived';
345           $output .= $text;
346           # Link to forwarded http:// urls in the midst of the report
347           # (even though these links already exist at the top)
348           $output =~ s,((?:ftp|http|https)://[\S~-]+?/?)((?:[\)\'\:\.\,]|\&\#39;)?(?:\s|\.<|$)),<a href=\"$1\">$1</a>$2,go;
349           # Add links to the cloned bugs
350           $output =~ s{(Bug )(\d+)( cloned as bugs? )(\d+)(?:\-(\d+)|)}{$1.bug_links(bug=>$2).$3.bug_links(bug=>(defined $5)?[$4..$5]:$4)}eo;
351           # Add links to merged bugs
352           $output =~ s{(?<=Merged )([\d\s]+)(?=\.)}{join(' ',map {bug_links(bug=>$_)} (split /\s+/, $1))}eo;
353           # Add links to blocked bugs
354           $output =~ s{(?<=Blocking bugs)(?:( of )(\d+))?( (?:added|set to|removed):\s+)([\d\s\,]+)}
355                       {(defined $2?$1.bug_links(bug=>$2):'').$3.
356                            english_join([map {bug_links(bug=>$_)} (split /\,?\s+/, $4)])}eo;
357           $output =~ s{((?:[Aa]dded|[Rr]emoved)\ blocking\ bug(?:\(s\))?)(?:(\ of\ )(\d+))?(:?\s+)
358                        (\d+(?:,\s+\d+)*(?:\,?\s+and\s+\d+)?)}
359                       {$1.(defined $3?$2.bug_links(bug=>$3):'').$4.
360                            english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}xeo;
361           $output =~ s{([Aa]dded|[Rr]emoved)( indication that bug )(\d+)( blocks )([\d\s\,]+)}
362                       {$1.$2.(bug_links(bug=>$3)).$4.
363                            english_join([map {bug_links(bug=>$_)} (split /\,?\s+(?:and\s+)?/, $5)])}eo;
364           # Add links to reassigned packages
365           $output =~ s{(Bug reassigned from package \`)([^']+?)((?:'|\&\#39;) to \`)([^']+?)((?:'|\&\#39;))}
366           {$1.q(<a href=").html_escape(package_links(package=>$2)).qq(">$2</a>).$3.q(<a href=").html_escape(package_links(package=>$4)).qq(">$4</a>).$5}eo;
367           if (defined $time) {
368                $output .= ' ('.strftime('%a, %d %b %Y %T GMT',gmtime($time)).') ';
369           }
370           $output .= '<a href="' .
371                html_escape(bug_links(bug => $bug_number,
372                                      options => {msg => ($msg_number+1)},
373                                      links_only => 1,
374                                     )
375                           ) . '">Full text</a> and <a href="' .
376                                html_escape(bug_links(bug => $bug_number,
377                                                      options => {msg => ($msg_number+1),
378                                                                  mbox => 'yes'},
379                                                      links_only => 1)
380                                           ) . '">rfc822 format</a> available.';
381
382           $output = qq(<div class="$class"><hr>\n<a name="$msg_number"></a>\n) . $output . "</div>\n";
383      }
384      elsif (/recips/) {
385           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
386           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
387                return ();
388           }
389           elsif (defined $msg_id) {
390                $$seen_msg_ids{$msg_id} = 1;
391           }
392           $output .= qq(<hr><p class="msgreceived"><a name="$msg_number"></a>\n);
393           $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>';
394           $output .= handle_email_message($record->{text},
395                                           ref     => $bug_number,
396                                           msg_num => $msg_number,
397                                          );
398      }
399      elsif (/autocheck/) {
400           # Do nothing
401      }
402      elsif (/incoming-recv/) {
403           my ($msg_id) = $record->{text} =~ /^Message-Id:\s+<(.+)>/im;
404           if (defined $msg_id and exists $$seen_msg_ids{$msg_id}) {
405                return ();
406           }
407           elsif (defined $msg_id) {
408                $$seen_msg_ids{$msg_id} = 1;
409           }
410           # Incomming Mail Message
411           my ($received,$hostname) = $record->{text} =~ m/Received: \(at (\S+)\) by (\S+)\;/;
412           $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 |.
413                html_escape("$received\@$hostname") .
414                     q| (<a href="| . html_escape(bug_links(bug => $bug_number, links_only => 1, options => {msg=>$msg_number})) . '">full text</a>'.
415                          q|, <a href="| . html_escape(bug_links(bug => $bug_number,
416                                                                 links_only => 1,
417                                                                 options => {msg=>$msg_number,
418                                                                             mbox=>'yes'}
419                                                                )
420                                                      ) .'">mbox</a>)'.":</p>\n";
421           $output .= handle_email_message($record->{text},
422                                           ref     => $bug_number,
423                                           msg_num => $msg_number,
424                                          );
425      }
426      else {
427           die "Unknown record type $_";
428      }
429      return $output;
430 }
431
432
433 sub __libravatar_url {
434     my ($email) = @_;
435     ($email) = get_addresses($email);
436     return "http://cdn.libravatar.org/avatar/".md5_hex(lc($email))."?d=retro";
437 }
438
439
440 1;
441
442
443 __END__
444
445
446
447
448
449