--- mimedefang-filter.default	2005-03-02 22:41:15.000000000 -0900
+++ mimedefang-filter.tnef	2005-03-15 06:54:17.000000000 -0900
@@ -208,6 +208,18 @@
 	}
     }
 
+    # if this is a application/ms-tnef message then extract and delete it
+    if (lc($type) eq "application/ms-tnef") {
+      @tnef_files = extract_tnef( $entity );
+      my $tnef_file_qty = @tnef_files;
+      if( $tnef_file_qty > 0 ){
+        md_syslog( 'info', "ytnef extracted some files so we are now dropping the winmail.dat mime part" );
+      } else {
+        md_syslog( 'info', "ytnef didn't find any attachments, but we are dropping the winmail.dat mime part anyway" );
+      }
+      return action_drop();
+    }
+
     return action_accept();
 }
 
@@ -290,6 +302,18 @@
     # No sense doing any extra work
     return if message_rejected();
 
+    # add the tnef files to the message
+    foreach my $fname ( @tnef_files ){
+      local($/) = undef;
+      open(FILE, "$fname");
+      my $file = <FILE>;
+      close(FILE);
+      $file =~ s:^.*[\\/]::;
+      action_add_part($entity, "application/octet-stream", "base64", $file, $fname, "attachment");
+      md_syslog('info', "Added file $fname to message because it was extracted from the winmail.dat");
+    }
+
+
     # Spam checks if SpamAssassin is installed
     if ($Features{"SpamAssassin"}) {
 	if (-s "./INPUTMSG" < 100*1024) {
@@ -346,6 +370,44 @@
     # action_rebuild();
 }
 
+#***********************************************************************
+# %PROCEDURE: extract_tnef
+# %ARGUMENTS:
+#  entity -- a Mime::Entity object (see MIME-tools documentation for details)
+# %RETURNS:
+#  A array of filenames extracted from the tnef part
+# %DESCRIPTION:
+#  This function extracts the attachments from a tnef file (winmail.dat)
+#  then provides a array of the files so that they can be attached later.
+#***********************************************************************
+sub extract_tnef( $ ) {
+
+  my ( $entity ) = @_;
+  my ( $body ) = $entity->bodyhandle;
+  my @tnef_files = ();
+
+  if( ! defined( $body ) ){
+    return @tnef_files;
+  }
+
+  # Get filename
+  my ( $path ) = $body->path;
+  if( ! defined( $path ) ){
+    return @tnef_files;
+  }
+
+  @new_files = `/usr/local/bin/ytnef -f Work $path`;
+
+  foreach my $file ( @new_files ){
+    chomp( $file );
+    push( @tnef_files, $file );
+    md_syslog( 'info', "Found file $file in winmail.dat attachment" );
+  }
+
+  return @tnef_files;
+
+}
+
 # DO NOT delete the next line, or Perl will complain.
 1;
 
