]> xenbits.xensource.com Git - people/aperard/emesinae.git/commitdiff
Refactor some helpful utility functions into a new Emesinae::MIME package.
authorIan Campbell <ian.campbell@citrix.com>
Tue, 21 Jan 2014 13:56:34 +0000 (13:56 +0000)
committerIan Campbell <ian.campbell@citrix.com>
Tue, 21 Jan 2014 13:56:34 +0000 (13:56 +0000)
Emesinae/CGI/Message.pm
Emesinae/MIME.pm [new file with mode: 0644]
Makefile

index 26d7f0c75866ed99cd283759fe2796104a56a3f7..d848451af5424cf70512c8a9cacaac8d6b9cc29e 100644 (file)
@@ -14,6 +14,7 @@ use File::Temp qw();
 use Encode qw/encode decode/;
 
 use Emesinae::Message;
+use Emesinae::MIME;
 use Emesinae::Common;
 use Emesinae::CGI;
 
@@ -91,51 +92,6 @@ sub raw_link ($) {
     return msglink( $m, "Retrieve Raw Message", "/raw" );
 }
 
-sub getmailbody    # From Debbugs
-{
-    my $entity = shift;
-    my $type   = $entity->effective_type;
-
-    if (   $type eq 'text/plain'
-        or ( $type =~ m#text/?# and $type ne 'text/html' )
-        or $type eq 'application/pgp' )
-    {
-        return $entity;
-    }
-    elsif ( $type eq 'multipart/alternative' ) {
-
-        # RFC 2046 says we should use the last part we recognize.
-        for my $part ( reverse $entity->parts ) {
-            my $ret = getmailbody($part);
-            return $ret if $ret;
-        }
-    }
-    else {
-
-        # For other multipart types, we just pretend they're
-        # multipart/mixed and run through in order.
-        for my $part ( $entity->parts ) {
-            my $ret = getmailbody($part);
-            return $ret if $ret;
-        }
-    }
-    return undef;
-}
-
-sub getmailparts {
-    my $entity = shift;
-    my @ret;
-    if ( $entity->is_multipart ) {
-        for my $part ( $entity->parts ) {
-            push @ret, getmailparts($part);
-        }
-    }
-    else {
-        push @ret, $entity;
-    }
-    return @ret;
-}
-
 sub parthdr {
     my ( $entity, $m, $nr ) = @_;
     my $head = $entity->head;
@@ -214,10 +170,10 @@ sub format_html ($;%) {
         my $tempdir = File::Temp::tempdir();
         my $e       = $m->get_mime($tempdir);
         my $et      = $e->effective_type;
-        $bodyent = getmailbody($e);
+        $bodyent = Emesinae::MIME::getmailbody($e);
         push @body, $bodyent->bodyhandle->as_lines;
         rmtree $tempdir, 0, 1;
-        @parts = getmailparts($e);
+        @parts = Emesinae::MIME::getmailparts($e);
 
         # Strip leading and trailing blank lines
         shift @body while @body and $body[0] !~ /\S/;
@@ -265,7 +221,7 @@ sub format_part($$) {
     my $tempdir = File::Temp::tempdir();
     my $e       = $m->get_mime($tempdir);
 
-    my @parts  = getmailparts($e);
+    my @parts  = Emesinae::MIME::getmailparts($e);
     my $entity = $parts[ $part - 1 ];
     my $head   = $entity->head;
 
diff --git a/Emesinae/MIME.pm b/Emesinae/MIME.pm
new file mode 100644 (file)
index 0000000..6a76659
--- /dev/null
@@ -0,0 +1,52 @@
+package Emesinae::MIME;
+
+use warnings;
+use strict;
+
+sub getmailbody    # From Debbugs
+{
+    my $entity = shift;
+    my $type   = $entity->effective_type;
+
+    if (   $type eq 'text/plain'
+        or ( $type =~ m#text/?# and $type ne 'text/html' )
+        or $type eq 'application/pgp' )
+    {
+        return $entity;
+    }
+    elsif ( $type eq 'multipart/alternative' ) {
+
+        # RFC 2046 says we should use the last part we recognize.
+        for my $part ( reverse $entity->parts ) {
+            my $ret = getmailbody($part);
+            return $ret if $ret;
+        }
+    }
+    else {
+
+        # For other multipart types, we just pretend they're
+        # multipart/mixed and run through in order.
+        for my $part ( $entity->parts ) {
+            my $ret = getmailbody($part);
+            return $ret if $ret;
+        }
+    }
+    return undef;
+}
+
+sub getmailparts {
+    my $entity = shift;
+    my @ret;
+    if ( $entity->is_multipart ) {
+        for my $part ( $entity->parts ) {
+            push @ret, getmailparts($part);
+        }
+    }
+    else {
+        push @ret, $entity;
+    }
+    return @ret;
+}
+
+1;
+
index 3abc74842b12b006dcdfc3f02da87cba79a4aa0d..7c484f4e5ad0478296e06e52c0fce82ac2d5f180 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,6 +24,7 @@ SCRIPTS     := scripts/receive.pl     \
 MODULES     := Emesinae/Bug.pm          \
                Emesinae/Common.pm       \
                Emesinae/Message.pm      \
+               Emesinae/MIME.pm         \
                Emesinae/CGI.pm          \
                Emesinae/CGI/Message.pm