On Mon, 16 Mar 2015 10:51:59 -0400
"Bill Cole" <sausers-20150...@billmail.scconsult.com> wrote:

> Is the code for doing this shared anywhere or is it sharable? Please?

It's part of our commercial CanIt software.  But I can post a
chunk of Perl that's roughly what we do.

We parse the message into a MIME::Entity.  Then if we need to truncate
it, we call a function similar to the code shown below.  It generates a
version of the message with all non-text parts emptied out.  It's a really
evil hack.

Code is for your education.  Most likely needs considerable tweaking
for production; our real production code obviously is much more
sophisticated.

Regards,

David.

# Pass the function below a MIME entity.  It returns a file handle
# opened for reading on a truncated message body.

sub open_truncated_body
{
        my($mime_entity) = @_;

        # We are truncating non-text parts.  So
        # we override print_body... ugh.
        no warnings 'redefine'; ## no critic (NoWarnings)
        my $original_print_bodyhandle = *MIME::Entity::print_bodyhandle{'CODE'};
        local *MIME::Entity::print_bodyhandle = sub {
                my($self, $out) = @_;
                $out ||= select;
                if ($self->head->mime_type =~ m|^text/|) {
                        # Evil...
                        # TODO FIXME: per ticket 15530, need to cap
                        # the size of text/* attachments.
                        # Unfortunately, the only way to do this may
                        # require even more evil.
                        return &$original_print_bodyhandle($self, $out);
                }

                # Leave empty!!!
                return 1;
        };
        my $fh = IO::File->new('TRUNCATED-MSG', O_WRONLY|O_CREAT);
        if ($fh && $fh->opened) {
                $mime_entity->print($fh);
                $fh->close();
        } else {
                return undef;
        }
        return IO::File->new('TRUNCATED-MSG', O_RDONLY);
}

Reply via email to