[Mimedefang] File::MMagic

Jason Englander jason at englanders.cc
Wed Jun 19 16:32:10 EDT 2002


I haven't really done anything useful with it yet, but after I saw mention
of the File::MMagic perl module on the list (File::MMagic is like the
'file' command, sort of...), I wrote up a simple little blurb for it in my
filter to log the magic type to syslog for each entity that has a
suggested file name.

Just throwing it out there for ideas/discussion/whatever.

Pop this in there somewhere:

sub get_filetype_magic ($) {
  my $file2chk = $_[0];
  use File::MMagic;
  # use this next line instead for the system magic file instead of the
  # built in File::MMagic one
  #my $mm = new File::Magic::new('/etc/magic');
  my $mm = new File::MMagic;
  my $ftype = $mm->checktype_filename($file2chk);
  return($ftype);
}

Then add this somewhere in filter:

  if ($fname) {
    my($body) = $entity->bodyhandle;
    if (defined($body)) {
      my($path) = $body->path;
      if (defined($path)) {
        my $magic = get_filetype_magic($path);
        syslog('info', "${fname}'s magic type is $magic");
      }
    }
  }

The value of $magic will be like a mime-type:

audio/x-wav
text/html
application/octet-stream
...

  Jason

-- 
Jason Englander
jason at englanders.cc





More information about the MIMEDefang mailing list