]> git.donarmstrong.com Git - debbugs.git/blobdiff - Debbugs/MIME.pm
support export tags
[debbugs.git] / Debbugs / MIME.pm
index 2e1d611c94a8085524633d3aaa37d78cf194246d..f9b58c9093d9836d942f7a4bd79dc961e513588d 100644 (file)
@@ -31,12 +31,21 @@ use warnings;
 use strict;
 
 use base qw(Exporter);
-use vars qw($VERSION @EXPORT_OK);
+use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS @EXPORT);
 
 BEGIN {
     $VERSION = 1.00;
+    $DEBUG = 0 unless defined $DEBUG;
 
-    @EXPORT_OK = qw(parse decode_rfc1522 encode_rfc1522 convert_to_utf8 create_mime_message getmailbody);
+    @EXPORT = ();
+
+    %EXPORT_TAGS = (mime => qw(parse create_mime_message getmailbody),
+                   rfc1522 => qw(decode_rfc1522 encode_rfc522),
+                   utf8 => qw(convert_to_utf8),
+                  );
+    @EXPORT_OK=();
+    Exporter::export_ok_tags(keys %EXPORT_TAGS);
+    $EXPORT_TAGS{all} = [@EXPORT_OK];
 }
 
 use File::Path;
@@ -58,7 +67,7 @@ sub getmailbody
     my $entity = shift;
     my $type = $entity->effective_type;
     if ($type eq 'text/plain' or
-           ($type =~ m#text/# and $type ne 'text/html') or
+           ($type =~ m#text/?# and $type ne 'text/html') or
            $type eq 'application/pgp') {
        return $entity->bodyhandle;
     } elsif ($type eq 'multipart/alternative') {
@@ -120,7 +129,12 @@ sub parse
 
     # Strip off RFC2440-style PGP clearsigning.
     if (@bodylines and $bodylines[0] =~ /^-----BEGIN PGP SIGNED/) {
-       shift @bodylines while @bodylines and length $bodylines[0];
+       shift @bodylines while @bodylines and
+           length $bodylines[0] and
+               # we currently don't strip \r; handle this for the
+               # time being, though eventually it should be stripped
+               # too, I think. [See #565981]
+               $bodylines[0] ne "\r";
        shift @bodylines while @bodylines and $bodylines[0] !~ /\S/;
        for my $findsig (0 .. $#bodylines) {
            if ($bodylines[$findsig] =~ /^-----BEGIN PGP SIGNATURE/) {
@@ -224,9 +238,7 @@ sub convert_to_utf8 {
      return $data if $charset eq 'raw' or is_utf8($data,1);
      my $result;
      eval {
-         # this encode/decode madness is to make sure that the data
-         # really is valid utf8 and that the is_utf8 flag is off.
-         $result = encode("utf8",decode($charset,$data))
+        $result = decode($charset,$data);
      };
      if ($@) {
          warn "Unable to decode charset; '$charset' and '$data': $@";
@@ -281,10 +293,15 @@ sub encode_rfc1522 {
 
      # handle being passed undef properly
      return undef if not defined $rawstr;
+     if (is_utf8($rawstr)) {
+        $rawstr= encode_utf8($rawstr);
+     }
      # We process words in reverse so we can preserve spacing between
      # encoded words. This regex splits on word|nonword boundaries and
-     # nonword|nonword boundaries.
-     my @words = reverse split /(?:(?<=[\s\n])|(?=[\s\n]))/m, $rawstr;
+     # nonword|nonword boundaries. We also consider parenthesis and "
+     # to be nonwords to avoid escaping them in comments in violation
+     # of RFC1522
+     my @words = reverse split /(?:(?<=[\s\n\)\(\"])|(?=[\s\n\)\(\"]))/m, $rawstr;
 
      my $previous_word_encoded = 0;
      my $string = '';
@@ -306,7 +323,7 @@ sub encode_rfc1522 {
               if (length $encoded > 75) {
                    # Turn utf8 into the internal perl representation
                    # so . is a character, not a byte.
-                   my $tempstr = decode_utf8($word,Encode::FB_DEFAULT);
+                   my $tempstr = is_utf8($word)?$word:decode_utf8($word,Encode::FB_DEFAULT);
                    my @encoded;
                    # Strip it into 10 character long segments, and encode
                    # the segments