[Mimedefang] Re: proposing a patch for URL listing in boilerplate

Yizhar Hurwitz yizhar at mail.com
Mon Dec 24 15:53:19 EST 2007


HI.

Regarding the question about action_replace_with_url,
see below how I do it - creating html clickable links inside the text

All my modifications are in "mimedefang-filter" and not in mimedefang.pl.

But I also agree and suggest that a more end-user friendly version of 
action_replace_with_url will be available if possible,
as I assume that many users of mimedefang will benefit from it.

Note that I have copied action_replace_with_url from mimedefang.pl to 
mimedefang-filter,
to add my custom code inside the procedure itself, to avoid problems 
while upgrading etc...


Here goes:

################################################################

### Some config values for easy modification per system
### (I actualy store them in additional file which is "required" by 
mimedefang-filter):

# Detach files from messages.
$DetachBigEnable = 0;
$DetachBigSizeMB = 10;
$DetachMultimediaEnable = 0;
$DetachMultimediaExt = 'asf|avi|mov|mpeg|mpg|wmv';
$DetachBasePath = '/var/www/detach';
$DetachBaseURL = 'http://'. $HostName. '/detach';
$DetachText =
 "\n\n".
 "The following file was detached from the original message:\n".
 "_FILENAME_\n".
 "Size: _FILESIZE_\n".
 "You can download it here:\n\n".
 "_URL_\n\n".
 "Please note that the download process can take some time.\n" .
 "Please note that the files will later be deleted from the mail server.\n";
$DetachListTextTop =
 "\n".
 "The following files were detached:\n\n";
$DetachListTextBot =
 "\n\n".
 "Please note that the files will later be deleted from the mail server.\n";



#***********************************************************************
# %PROCEDURE: filter_initialize
#***********************************************************************
sub filter_initialize {
 $DetachSubdir = '/'. substr(time_str, 0, 7);
 $DetachPath = $DetachBasePath. $DetachSubdir;
 $DetachURL = $DetachBaseURL. $DetachSubdir;
 if ($DetachBigEnable or $DetachMultimediaEnable) {
  unless (-e $DetachPath) {
   mkdir ($DetachPath);
   chmod (0755, $DetachPath);
  }
 }

..........snip.............



#***********************************************************************
# %PROCEDURE: filter_begin
#***********************************************************************
sub filter_begin {
    my($entity) = @_;

    @DetachedFiles = ();

..........snip.............



#***********************************************************************
### This is a MODIFIED version of action_replace_with_url ###
### For use by New Ofek Anti Spam system. ####
# %PROCEDURE: custom_action_replace_with_url
# %ARGUMENTS:
#  entity -- part to replace
#  doc_root -- document root in which to place file
#  base_url -- base URL for retrieving document
#  msg -- message to replace document with.  The string "_URL_" is
#         replaced with the actual URL of the part.
#  cd_data -- optional Content-Disposition filename data to save
#  salt    -- optional salt to add to SHA1 hash.
# %RETURNS:
#  1 on success, 0 on failure
# %DESCRIPTION:
#  Places the part in doc_root/{sha1_of_part}.ext and replaces it with
#  a text/plain part giving the URL for pickup.
#***********************************************************************
sub custom_action_replace_with_url ($$$$;$$) {
    my($entity, $doc_root, $base_url, $msg, $cd_data, $salt) = @_;
    my($ctx);
    my($path);
    my($fname, $ext, $name, $url);
    my $extension = "";

    return 0 unless in_filter_context("action_replace_with_url");
    return 0 unless defined($entity->bodyhandle);
    $path = $entity->bodyhandle->path;
    return 0 unless defined($path);
    open(IN, "<$path") or return 0;

    $ctx = Digest::SHA1->new;
    $ctx->addfile(*IN);
    $ctx->add($salt) if defined($salt);
    close(IN);

    $fname = takeStabAtFilename($entity);
    $fname = "" unless defined($fname);
    $extension = $1 if ($fname =~ /(\.[^.]*)$/);

    # Use extension if it is .[alpha,digit,underscore]
    $extension = "" unless ($extension =~ /^\.[A-Za-z0-9_]*$/);

    # Filename to save
    $name = $ctx->hexdigest . $extension;
    $fname = $doc_root . "/" . $name;
    $url = $base_url . "/" . $name;

    if (-r $fname) {
        # If file exists, then this is either a duplicate or someone
        # has defeated SHA1.  Just update the mtime on the file.
        my($now);
        $now = time;
        utime($now, $now, $fname);
    } else {
        copy_or_link($path, $fname) or return 0;
        # In case umask is whacked...
        chmod 0644, $fname;
    }

    # save optional Content-Disposition data
    if (defined($cd_data) and ($cd_data ne "")) {
        if (open CDF, ">$doc_root/.$name") {
            print CDF $cd_data;
            close CDF;
            chmod 0644, "$doc_root/.$name";
        }
    }

    $msg =~ s/_URL_/$url/g;
    action_replace_with_warning($msg);

### Begin modified section: ###
    push (@DetachedFiles, $cd_data. "\n". $url. "\n");
### End modified section. ###

    return 1;
}




#***********************************************************************
# %PROCEDURE: filter
..........snip.............
#***********************************************************************
sub filter {

..........snip.............

    $FileSize = (stat($entity->bodyhandle->path))[7];
    if (($DetachBigEnable and ($FileSize >= $DetachBigSizeMB*1000000)) 
or ($DetachMultimediaEnable and ($ext =~ /^\.($DetachMu
ltimediaExt)$/i))) {
        md_graphdefang_log('detached', $fname, $FileSize);
        $detach_msg = $DetachText;
        $detach_msg =~ s/_FILENAME_/$fname/g;
        $detach_msg =~ s/_FILESIZE_/$FileSize/g;
        return custom_action_replace_with_url($entity, $DetachPath, 
$DetachURL, $detach_msg, $fname);
    }

..........snip.............



#***********************************************************************
# %PROCEDURE: list_detached_files
#***********************************************************************
sub list_detached_files ($) {
    my($entity) = @_;
    my $plain = $DetachListTextTop. join("\n", @DetachedFiles). 
$DetachListTextBot;
    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_end
#***********************************************************************
sub filter_end {
    my($entity) = @_;

    # No sense doing any extra work
    return if message_rejected();

    if (@DetachedFiles > 0) {
        &list_detached_files($entity);
    }

..........snip.............




################################################################

That's it.

Yizhar Hurwitz
http://yizhar.mvps.org




More information about the MIMEDefang mailing list