use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
$AddWarningsInline = 1;
$Stupidity{"NoMultipleInlines"} = 1;

#***********************************************************************
# %PROCEDURE: custom_list_replacement_urls
# %ARGUMENTS:
#  entity
# %RETURNS:
#  nothing
# %DESCRIPTION:
#  Lists replacement URLs at end of body message for all removed attachments.
#  Must be called from filter_end.
#***********************************************************************
sub custom_list_replacement_urls ($;$$) {
    my($entity, $header, $footer) = @_;
    my $plain = $header.join("\n", @attachURLlist).$footer;
    my $html = $plain;
    $html =~ s|(http://\S*)|<a href="$1">$1</a>|g;
    $html =~ s/\n/<br>\n/g;
    append_text_boilerplate($entity, $plain, 0);
    append_html_boilerplate($entity, $html, 0);
}

#***********************************************************************
# %PROCEDURE: filter
#***********************************************************************
sub filter {
    my($entity, $fname, $ext, $type) = @_;

    return if message_rejected(); # Avoid unnecessary work

(...)

    # Some senders do not get their mail filtered
    if ( ($ext ne '') && ($Sender ne '<me@domain1.org>') && ($Sender ne '<me@domain2.org>') ) {

	# If it's not a compressed file then zip it
	my($comp_exts, $re);
	$comp_exts = '(zip|gz|tgz|bz2|Z|\{[^\}]+\})';
	$re = '\.' . $comp_exts . '\.*$';
	my $zipped;

	if (!re_match($entity, $re)) {
	    md_graphdefang_log('mail', 'CUSTOM_COMPRESS', $fname);
	    my $zip = Archive::Zip->new();
	    my $member;
	    $member = $zip->addFile($entity->bodyhandle->path, $fname);
	    # compress (DEFLATED) or not (STORED)
	    $member->desiredCompressionMethod( COMPRESSION_DEFLATED );
	    $member->desiredCompressionLevel( COMPRESSION_LEVEL_BEST_COMPRESSION );
	    $fname = "$fname.zip" unless $fname =~ s/\.[^.]*$/\.zip/;
	    $zip->writeToFileNamed("./Work/CUSTOM_$fname");
	    action_replace_with_file($entity, "./Work/CUSTOM_$fname", $fname);
	    $zipped = 1;
	}

	my($custom_size, $custom_bandwidth);
	$custom_bandwidth = (stat("./INPUTMSG"))[7]*scalar(@Recipients);
	md_graphdefang_log('mail', 'CUSTOM_BANDWIDTH', $custom_bandwidth);
	$custom_size = (stat($entity->bodyhandle->path))[7];
	md_graphdefang_log('mail', 'CUSTOM_MSG_PART_SIZE', $custom_size);
	if ( ($custom_size > 800*1024) || ($custom_bandwidth > 1300*1024) ) {
    	    action_notify_sender("You e-mail has been filtered and sent. No further action required.\n");
    	    return action_replace_with_url($entity,"/var/www/localhost/htdocs/dload","http://download.domain3.org/dload","You can download the attachment from:<br>\n<a href='_URL_'>_URL_</a><br>\nNOTICE: the attached file ($fname - $custom_size bytes) is available for about 1 week.\n","text/html","html","attachment",$fname);
	}

	if ($zipped) {
	    return 1;
	}

    }
    
    return action_accept();
}

(...)

sub filter_end {
    my($entity) = @_;

    return if message_rejected();

    if (@attachURLlist > 0) {
	&custom_list_replacement_urls($entity,"The following is a list links to download attached files.\n","\nThe data is available for about 1 week.")
    }

