Mailing List CGatePro@mail.stalker.com Message #103024
From: Taylor, Jonn <jonnt@taylortelephone.com>
Subject: Re: Need help with filter
Date: Fri, 17 Feb 2012 14:29:19 -0600
To: CommuniGate Pro Discussions <CGatePro@mail.stalker.com>
For those that are interested here is the completed code for this helper. It is located here: http://www.taylortelephone.com/scripts/attachment-extractor.pl
 Run as helper as you like.

Usage: attachment-extractor.pl [options...]

--help      :  This message.
--filedest  :  Full path to the location to store files. Default /tmp/
--name      :  Name to insert between Date/Time and attachment file name. Default EXTRACTED

Jonn

On 02/17/2012 08:44 AM, Taylor, Jonn wrote:
Thanks, I forgot about that. I got it working!!!

Jonn

On 02/17/2012 02:03 AM, Nicolas Hatier wrote:
When you're calling it on the command line, you feed it with a message as saved by CGP.

As external filters, it gets a transient message file, which also contains the CGP envelope information. MIME::Tools isn't able to parse this file properly. You need to skip the first lines of the message, up to the first empty line, and have MIME::Tools parse the message starting there.

Nicolas

*Nicolas Hatier, ing.* <nicolas.hatier@niversoft.com <mailto:nicolas.hatier@niversoft.com>>
Niversoft idées logicielles - http://www.niversoft.com


On 2012-02-16 23:47, Taylor, Jonn wrote:
From the command line:

[root@mail CommuniGate]# ./attch_extrct_badge_times.pl
* Content-Filter is running
*
1 file test-extract.msg
* Attachment-Extractor-Filter starting
*
* * No Attchment 1 or Attchment name: Invoice 89989.Pdf *

* * Saved files to: /mnt/tts_fax/Incoming_FAX/Badge_Times_4014/2012-02-16_22-37-33-NTE-Invoice 89989.Pdf *
*
1 OK

As a filter called by CGP:

22:39:26.716 4 EXTFILTER(attch_extrct_badge_times) out(026): 44 FILE Queue/7191542.msg\n
22:39:26.716 4 EXTFILTER(attch_extrct_badge_times) inp(038): * Attachment-Extractor-Filter starting
22:39:26.716 4 EXTFILTER(attch_extrct_badge_times) inp(002): *
22:39:26.718 4 EXTFILTER(attch_extrct_badge_times) inp(005): 44 OK



On 02/16/2012 10:36 PM, Taylor, Jonn wrote:
I had that at the end and removed it and it did not make a difference. I am going to put it back. The symptoms are that it does nothing. It will execute and print OK at the end but never does anything. If I run it from a shell it works perfect.

Jonn

On 02/16/2012 06:37 PM, Nicolas Hatier wrote:

What are the symptoms?

Try to add

      open STDOUT, ">&STDOUT";

at the end of the while(<>) loop to make sure STDOUT is flushed.

Nicolas

*Nicolas Hatier, ing.* <nicolas.hatier@niversoft.com <mailto:nicolas.hatier@niversoft.com>>
Niversoft idées logicielles - http://www.niversoft.com


On 2012-02-16 18:28, Taylor, Jonn wrote:
All,

I am trying to write a filter to extract attachments from an email, rename the file and save it to a file share. The code below works at a bash shell but does not work as a filter. Any ideas????

#!/usr/bin/perl
use MIME::Tools;
use MIME::Parser;
use MIME::Decoder::QuotedPrint;
use MIME::Decoder::Base64;
use MIME::Decoder::Binary;
use MIME::Decoder::Gzip64;
use MIME::Decoder::NBit;
use MIME::Decoder::UU;
use MIME::Words qw(:all);
use MIME::Entity;
use MIME::Body;
use POSIX qw(strftime);
use strict;

sub Log
{
   print "* $_[0]\n";
}

$| = 1;

Log "Content-Filter is running";
Log "";

while (<>)
{
   my @line = split(/ /, $_);
        chomp($line[0]);

   Log "Attachment-Extractor-Filter starting";
   Log "";

   # Verifying the command send by the server. Only supports FILE for now
   if ($line[1] !~ /^file$/i)
   {
      print "$line[0] FAILURE\n";
   }
   else
   {
        $line[2] =~ s|\\|/|g; # Getting the filepath
        chomp($line[2]);
        my $file = "$line[2]";

         # extract attachment
        my (@body, $i, $subentity);
        my $parser = new MIME::Parser;
        #these are the types of attachments allowed
        my @attypes= qw(application/msword
                        application/vnd.ms-excel
                        application/pdf
                        application/octet-stream
        );

        my ($x, $newx, @attachment, $attachment, @attname, $bh, $nooatt);
        my $body;    #contains the message body

        $parser->ignore_errors(1);
        $parser->output_to_core(0);
        $parser->extract_nested_messages(1);
        $parser->use_inner_files(0);

    my $entity = $parser->parse_open("$file");
    my $error = ($@ || $parser->last_error);

    #get email body
    if ($entity->parts > 0){
        for ($i=0; $i<$entity->parts; $i++){
            $subentity = $entity->parts($i);
            if (($subentity->mime_type =~ m/text\/html/i) || ($subentity->mime_type =~ m/text\/plain/i)){
                $body = join "",  @{$subentity->body};
                next;
            }
            #this elsif is needed for Outlook's nasty multipart/alternative messages
            elsif ($subentity->mime_type =~ m/multipart\/alternative/i){
                $body = join "",  @{$subentity->body};
                #split html and text parts
                @body = split /------=_NextPart_\S*\n/, $body;
                #assign the first part of the message,
                #hopefully the text, part as the body
                $body = $body[1];
                #remove leading headers from body
                $body =~ s/^Content-Type.*Content-Transfer-Encoding.*?\n+//is;
                next;
            }
            #grab attachment name and contents
            foreach $x (@attypes) {
                if ($subentity->mime_type =~ m/$x/i){
                    $bh = $subentity->bodyhandle;
                    $attachment = $bh->as_string;
                    push @attachment, $attachment;
                    push @attname, $subentity->head->mime_attr('content-disposition.filename');
                }else{
                    #some clients send attachments as application/x-type.
                    #checks for that
                    $newx = $x;
                    $newx =~ s/application\/(.*)/application\/x-$1/i;
                    if ($subentity->mime_type =~ m/$newx/i){
                        $bh = $subentity->bodyhandle;
                        $attachment = $bh->as_string;
                        push @attachment, $attachment;
                        push @attname, $subentity->head->mime_attr('content-disposition.filename');
                    }
                }
            }
            $nooatt = $#attachment + 1;
        Log "* No Attchment $nooatt or Attchment name: $attname[$x] *\n";
        }
    }
    #Get current local time and format for file name output
    my $xdate;
    $xdate = strftime("%Y-%m-%d_%H-%M-%S", localtime());
    #write contents of each attachment to a file
    for ($x = 0; $x < $nooatt; $x++){
        open FH, ">/mnt/tts_fax/Incoming_FAX/Badge_Times_4014/$xdate-NTE-$attname[$x]";
        print FH "$attachment[$x]";
        close FH;
        Log "* Saved files to: /mnt/tts_fax/Incoming_FAX/Badge_Times_4014/$xdate-NTE-$attname[$x] *";         Log "";
    }
    # Addheader
        print "$line[0] OK\n";
   }
}



#############################################################
This message is sent to you because you are subscribed to
 the mailing list <CGatePro@mail.stalker.com>.
To unsubscribe, E-mail to: <CGatePro-off@mail.stalker.com>
To switch to the DIGEST mode, E-mail to <CGatePro-digest@mail.stalker.com>
To switch to the INDEX mode, E-mail to <CGatePro-index@mail.stalker.com>
Send administrative queries to <CGatePro-request@mail.stalker.com>
Subscribe (FEED) Subscribe (DIGEST) Subscribe (INDEX) Unsubscribe Mail to Listmaster