import rt 3.0.12
[freeside.git] / rt / lib / RT / Attachment_Overlay.pm
index d31aa75..481dbf0 100644 (file)
@@ -112,8 +112,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);
 
@@ -133,7 +133,9 @@ sub Create {
            =~ /^.*\bfilename="(.*)"$/ ? $1 : ''
     };
 
-    if ( $Attachment->parts ) {
+    # 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(
             TransactionId => $args{'TransactionId'},
             Parent        => 0,
@@ -241,6 +243,9 @@ Create an attachment exactly as specified in the named parameters.
 
 sub Import {
     my $self = shift;
+    my %args = ( ContentEncoding => 'none',
+
+                @_ );
     return($self->SUPER::Create(@_));
 }
 
@@ -309,11 +314,15 @@ sub OriginalContent {
   }
 
   # Encode::_utf8_on($content);
-  if (!$enc or $enc eq 'utf8' or $enc eq 'utf-8') {
+  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);
   }
-  Encode::from_to($content, 'utf8' => $enc);
+  
+  eval { Encode::from_to($content, 'utf8' => $enc);};
+  if ($@) {
+       $RT::Logger->error("Could not convert attachment from assumed utf8 to '$enc' :".$@);
+  }
   return $content;
 }
 
@@ -423,10 +432,13 @@ properly unfolded.
 =cut
 
 sub NiceHeaders {
-    my $self=shift;
-    my $hdrs="";
-    for (split(/\n/,$self->Headers)) {
-           $hdrs.="$_\n" if /^(To|From|RT-Send-Cc|Cc|Date|Subject): /i
+    my $self = shift;
+    my $hdrs = "";
+    my @hdrs = split(/\n/,$self->Headers);
+    while (my $str = shift @hdrs) {
+           next unless $str =~ /^(To|From|RT-Send-Cc|Cc|Date|Subject): /i;
+           $hdrs .= $str . "\n";
+           $hdrs .= shift( @hdrs ) . "\n" while ($hdrs[0] =~ /^[ \t]+/);
     }
     return $hdrs;
 }
@@ -439,7 +451,7 @@ sub NiceHeaders {
 Returns this object's headers as a string.  This method specifically
 removes the RT-Send-Bcc: header, so as to never reveal to whom RT sent a Bcc.
 We need to record the RT-Send-Cc and RT-Send-Bcc values so that we can actually send
-out mail. (The mailing rules are seperated from the ticket update code by
+out mail. (The mailing rules are separated from the ticket update code by
 an abstraction barrier that makes it impossible to pass this data directly
 
 =cut
@@ -447,8 +459,8 @@ an abstraction barrier that makes it impossible to pass this data directly
 sub Headers {
     my $self = shift;
     my $hdrs="";
-    for (split(/\n/,$self->SUPER::Headers)) {
-           $hdrs.="$_\n" unless /^(RT-Send-Bcc): /i
+    for ($self->_SplitHeaders) {
+           $hdrs.="$_\n" unless /^(RT-Send-Bcc):/i
     }
     return $hdrs;
 }
@@ -468,8 +480,8 @@ done in Headers() above.
 sub GetHeader {
     my $self = shift;
     my $tag = shift;
-    foreach my $line (split(/\n/,$self->SUPER::Headers)) {
-        if ($line =~ /^\Q$tag\E:\s+(.*)$/i) { #if we find the header, return its value
+    foreach my $line ($self->_SplitHeaders) {
+        if ($line =~ /^\Q$tag\E:\s+(.*)$/si) { #if we find the header, return its value
             return ($1);
         }
     }
@@ -492,7 +504,7 @@ sub SetHeader {
     my $tag = shift;
     my $newheader = '';
 
-    foreach my $line (split(/\n/,$self->SUPER::Headers)) {
+    foreach my $line ($self->_SplitHeaders) {
         if (defined $tag and $line =~ /^\Q$tag\E:\s+(.*)$/i) {
            $newheader .= "$tag: $_[0]\n";
            undef $tag;
@@ -545,6 +557,54 @@ sub _Value  {
 
 # }}}
 
+=head2 _SplitHeaders
+
+Returns an array of this attachment object's headers, with one header 
+per array entry. multiple lines are folded
+
+=begin testing
+
+my $test1 = "From: jesse";
+my @headers = RT::Attachment->_SplitHeaders($test1);
+is ($#headers, 0, $test1 );
+
+my $test2 = qq{From: jesse
+To: bobby
+Subject: foo
+};
+
+@headers = RT::Attachment->_SplitHeaders($test2);
+is ($#headers, 2, "testing a bunch of singline multiple headers" );
+
+
+my $test3 = qq{From: jesse
+To: bobby,
+ Suzie,
+    Sally,
+    Joey: bizzy,
+Subject: foo
+};
+
+@headers = RT::Attachment->_SplitHeaders($test3);
+is ($#headers, 2, "testing a bunch of singline multiple headers" );
+
+
+=end testing
+
+=cut
+
+sub _SplitHeaders {
+    my $self = shift;
+    my $headers = (shift || $self->SUPER::Headers());
+    my @headers;
+    for (split(/\n(?=\w|\z)/,$headers)) {
+        push @headers, $_;
+
+    }
+    return(@headers);
+}
+
+
 sub ContentLength {
     my $self = shift;
 
@@ -568,4 +628,13 @@ sub ContentLength {
 
 # }}}
 
+# Transactions don't change. by adding this cache congif directiove, we don't lose pathalogically on long tickets.
+sub _CacheConfig {
+    {
+        'cache_p'         => 1,
+          'fast_update_p' => 1,
+          'cache_for_sec' => 180,
+    }
+}
+
 1;