import rt 3.6.4
[freeside.git] / rt / lib / RT / Attachment_Overlay.pm
index 8dcf56e..f1b9334 100644 (file)
@@ -1,8 +1,8 @@
-# {{{ BEGIN BPS TAGGED BLOCK
+# BEGIN BPS TAGGED BLOCK {{{
 # 
 # COPYRIGHT:
 #  
-# This software is Copyright (c) 1996-2004 Best Practical Solutions, LLC 
+# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC 
 #                                          <jesse@bestpractical.com>
 # 
 # (Except where explicitly superseded by other copyright notices)
@@ -22,7 +22,9 @@
 # 
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
-# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+# 02110-1301 or visit their web page on the internet at
+# http://www.gnu.org/copyleft/gpl.html.
 # 
 # 
 # CONTRIBUTION SUBMISSION POLICY:
@@ -42,7 +44,7 @@
 # works based on those contributions, and sublicense and distribute
 # those contributions and any derivatives thereof.
 # 
-# }}} END BPS TAGGED BLOCK
+# END BPS TAGGED BLOCK }}}
 =head1 SYNOPSIS
 
   use RT::Attachment;
@@ -66,6 +68,9 @@ ok (require RT::Attachment);
 
 =cut
 
+
+package RT::Attachment;
+
 use strict;
 no warnings qw(redefine);
 
@@ -121,14 +126,13 @@ sub TransactionObj {
 Create a new attachment. Takes a paramhash:
     
     'Attachment' Should be a single MIME body with optional subparts
-    'Parent' is an optional Parent RT::Attachment object
-    'TransactionId' is the mandatory id of the Transaction this attachment is associated with.;
+    'Parent' is an optional id of the parent attachment
+    'TransactionId' is the mandatory id of the transaction this attachment is associated with.;
 
 =cut
 
 sub Create {
     my $self = shift;
-    my ($id);
     my %args = ( id            => 0,
                  TransactionId => 0,
                  Parent        => 0,
@@ -138,8 +142,8 @@ sub Create {
     #For ease of reference
     my $Attachment = $args{'Attachment'};
 
-           #if we didn't specify a ticket, we need to bail
-           if ( $args{'TransactionId'} == 0 ) {
+    #if we didn't specify a ticket, we need to bail
+    if ( $args{'TransactionId'} == 0 ) {
         $RT::Logger->crit( "RT::Attachment->Create couldn't, as you didn't specify a transaction\n" );
         return (0);
 
@@ -153,32 +157,43 @@ sub Create {
     defined($Subject) or $Subject = '';
     chomp($Subject);
 
+    #Get the Message-ID
+    my $MessageId = $Attachment->head->get( 'Message-ID', 0 );
+    defined($MessageId) or $MessageId = '';
+    chomp ($MessageId);
+    $MessageId =~ s/^<(.*)>$/$1/go;
+
+
     #Get the filename
-    my $Filename = $Attachment->head->recommended_filename || eval {
-       ${ $Attachment->head->{mail_hdr_hash}{'Content-Disposition'}[0] }
-           =~ /^.*\bfilename="(.*)"$/ ? $1 : ''
-    };
+    my $Filename = $Attachment->head->recommended_filename;
 
     # If a message has no bodyhandle, that means that it has subparts (or appears to)
     # and we should act accordingly.  
     unless ( defined $Attachment->bodyhandle ) {
-        $id = $self->SUPER::Create(
+
+        my $id = $self->SUPER::Create(
             TransactionId => $args{'TransactionId'},
             Parent        => 0,
             ContentType   => $Attachment->mime_type,
-            Headers => $Attachment->head->as_string,
-            Subject => $Subject);
+            Headers       => $Attachment->head->as_string,
+            MessageId     => $MessageId,
+            Subject       => $Subject
+        );
+        
+        unless ($id) {
+            $RT::Logger->crit("Attachment insert failed - ".$RT::Handle->dbh->errstr);
+        }
 
         foreach my $part ( $Attachment->parts ) {
             my $SubAttachment = new RT::Attachment( $self->CurrentUser );
-            $SubAttachment->Create(
+            my ($id) = $SubAttachment->Create(
                 TransactionId => $args{'TransactionId'},
                 Parent        => $id,
                 Attachment    => $part,
-                ContentType   => $Attachment->mime_type,
-                Headers       => $Attachment->head->as_string(),
-
             );
+            unless ($id) {
+                $RT::Logger->crit("Attachment insert failed - ".$RT::Handle->dbh->errstr);
+            }
         }
         return ($id);
     }
@@ -186,21 +201,24 @@ sub Create {
     #If it's not multipart
     else {
 
+        my ($ContentEncoding, $Body) = $self->_EncodeLOB( $Attachment->bodyhandle->as_string,
+                                                          $Attachment->mime_type 
+                                                        );
+        my $id = $self->SUPER::Create(
+            TransactionId   => $args{'TransactionId'},
+            ContentType     => $Attachment->mime_type,
+            ContentEncoding => $ContentEncoding,
+            Parent          => $args{'Parent'},
+            Headers         => $Attachment->head->as_string,
+            Subject         => $Subject,
+            Content         => $Body,
+            Filename        => $Filename,
+            MessageId       => $MessageId,
+        );
+        unless ($id) {
+            $RT::Logger->crit("Attachment insert failed - ".$RT::Handle->dbh->errstr);
+        }
 
-        my $Body = $Attachment->bodyhandle->as_string;
-
-
-       my ($ContentEncoding, $Body) = $self->_EncodeLOB($Attachment->bodyhandle->as_string, $Attachment->mime_type);
-
-
-        my $id = $self->SUPER::Create( TransactionId => $args{'TransactionId'},
-                                       ContentType   => $Attachment->mime_type,
-                                       ContentEncoding => $ContentEncoding,
-                                       Parent          => $args{'Parent'},
-                                       Headers       =>  $Attachment->head->as_string, 
-                                       Subject       =>  $Subject,
-                                       Content         => $Body,
-                                       Filename => $Filename, );
         return ($id);
     }
 }
@@ -220,7 +238,11 @@ sub Import {
     my %args = ( ContentEncoding => 'none',
 
                 @_ );
-    return($self->SUPER::Create(@_));
+
+
+ ($args{'ContentEncoding'}, $args{'Content'}) = $self->_EncodeLOB($args{'Content'}, $args{'MimeType'});
+
+    return($self->SUPER::Create(%args));
 }
 
 # {{{ sub Content
@@ -234,26 +256,7 @@ before returning it.
 
 sub Content {
   my $self = shift;
-  my $decode_utf8 = (($self->ContentType =~ qr{^text/plain}i) ? 1 : 0);
-
-  if ( $self->ContentEncoding eq 'none' || ! $self->ContentEncoding ) {
-      return $self->_Value(
-         'Content',
-         decode_utf8 => $decode_utf8,
-      );
-  } elsif ( $self->ContentEncoding eq 'base64' ) {
-      return ( $decode_utf8
-        ? Encode::decode_utf8(MIME::Base64::decode_base64($self->_Value('Content')))
-        : MIME::Base64::decode_base64($self->_Value('Content'))
-      );
-  } elsif ( $self->ContentEncoding eq 'quoted-printable' ) {
-      return ( $decode_utf8
-        ? Encode::decode_utf8(MIME::QuotedPrint::decode($self->_Value('Content')))
-        : MIME::QuotedPrint::decode($self->_Value('Content'))
-      );
-  } else {
-      return( $self->loc("Unknown ContentEncoding [_1]", $self->ContentEncoding));
-  }
+   $self->_DecodeLOB($self->ContentType, $self->ContentEncoding, $self->_Value('Content', decode_utf8 => 0));
 }
 
 
@@ -265,7 +268,7 @@ sub Content {
 =head2 OriginalContent
 
 Returns the attachment's content as octets before RT's mangling.
-Currently, this just means restoring text/plain content back to its
+Currently, this just means restoring text content back to its
 original encoding.
 
 =cut
@@ -273,7 +276,8 @@ original encoding.
 sub OriginalContent {
   my $self = shift;
 
-  return $self->Content unless $self->ContentType eq 'text/plain';
+  return $self->Content unless (
+     $self->ContentType =~ qr{^(text/plain|message/rfc822)$}i) ;
   my $enc = $self->OriginalEncoding;
 
   my $content;
@@ -287,13 +291,13 @@ sub OriginalContent {
       return( $self->loc("Unknown ContentEncoding [_1]", $self->ContentEncoding));
   }
 
-  # Encode::_utf8_on($content);
+   Encode::_utf8_on($content);
   if (!$enc || $enc eq '' ||  $enc eq 'utf8' || $enc eq 'utf-8') {
     # If we somehow fail to do the decode, at least push out the raw bits
     eval {return( Encode::decode_utf8($content))} || return ($content);
   }
   
-  eval { Encode::from_to($content, 'utf8' => $enc);};
+  eval { Encode::from_to($content, 'utf8' => $enc) } if $enc;
   if ($@) {
        $RT::Logger->error("Could not convert attachment from assumed utf8 to '$enc' :".$@);
   }
@@ -398,19 +402,16 @@ sub Quote {
 
 =head2 NiceHeaders
 
-Returns the To, From, Cc, Date and Subject headers.
-
-It is a known issue that this breaks if any of these headers are not
-properly unfolded.
+Returns a multi-line string of the To, From, Cc, Date and Subject headers.
 
 =cut
 
 sub NiceHeaders {
     my $self = shift;
     my $hdrs = "";
-    my @hdrs = split(/\n/,$self->Headers);
+    my @hdrs = $self->_SplitHeaders;
     while (my $str = shift @hdrs) {
-           next unless $str =~ /^(To|From|RT-Send-Cc|Cc|Bcc:Date|Subject): /i;
+           next unless $str =~ /^(To|From|RT-Send-Cc|Cc|Bcc|Date|Subject):/i;
            $hdrs .= $str . "\n";
            $hdrs .= shift( @hdrs ) . "\n" while ($hdrs[0] =~ /^[ \t]+/);
     }
@@ -433,10 +434,9 @@ an abstraction barrier that makes it impossible to pass this data directly
 sub Headers {
     my $self = shift;
     my $hdrs="";
-    for ($self->_SplitHeaders) {
-           $hdrs.="$_\n" unless /^(RT-Send-Bcc):/i
-    }
-    return $hdrs;
+    my @headers =  grep { !/^RT-Send-Bcc/i } $self->_SplitHeaders;
+    return join("\n",@headers);
+
 }
 
 
@@ -535,7 +535,7 @@ sub _Value {
 =head2 _SplitHeaders
 
 Returns an array of this attachment object's headers, with one header 
-per array entry. multiple lines are folded
+per array entry. multiple lines are folded.
 
 =begin testing