Demo Discussion
Forum Config Examples Contributions Vulnerabilities
  Contributions to ELOG  Not logged in ELOG logo
Message ID: 4     Entry time: Fri Jun 13 17:10:48 2003
Author: Sridhar Anandakrishnan 
Author Email: sak@essc.psu.edu 
Category:  
Subject: submit emails to elog (along with MIME attachments) 
Status:  
Last Revision:  
Attached is a perl script to which you can pipe a message (or a
single-message file) to submit that message to `elog'.  The `elog'
distribution includes two programs `elogd', which is the main daemon that is
accessed via the browser, and `elog', which is a command-line interface to
elogd.

The attached perl script `mailelog', will split a multipart MIME message
into its components and submit each as an attachment to elog to create a new
entry in a specified logbook.  The attributes are the subject, from, and cc
of the message.

Usage: mailelog [-|file] [-l logbook]

(if there are no arguments, read from stdin)
(makes a command that looks like this:
elog -p 8080 -h localhost -l emails -a subject=<subject> -a from=<from> -a
cc=<cc> -f attachment-1 -f attachment-2 -f ...

attachment-1 is the body of the message and attachment-2... are the actual
MIME attachments.  Set the elogd configuration to display attachments, so
that the message body is immediately visible.

Defaults: -h localhost -p 8080 -l emails

If no `-l logbook' flag is specified, then the entry is sent to the `emails'
logbook, so make sure that logbook exists.  Save this in, e.g,
~/bin/mailelog, and make sure it is executable (`chmod +x mailelog') and on
your path (bash: `export PATH=$HOME/bin:$PATH' or csh/tcsh: `setenv PATH
$HOME/bin:$PATH')

Bugs: multi-message files don't work.  can't add other attributes.  if the
logbook doesn't have attributes subject, from, cc, they are quietly lost.
Attachment 1: mailelog  4 kB  | Hide | Hide all
#!/usr/bin/perl -w

=head1 NAME

doelog - save a mime message to elog

=head1 SYNOPSIS

    doelog [-l logbook] <mime-msg-file> <mime-msg-file> ...
    
    someprocess | doelog [-l logbook] -

=head1 DESCRIPTION

Takes one or more files from the command line that contain MIME
messages, and explodes their contents out into /tmp.  The parts
are sent to elog as attachments.

Modified mimeexplode of the MIME::Tools in perl, which see.

From mimeexplode:
"This was written as an example of the MIME:: modules in the
MIME-parser package I wrote.  It may prove useful as a quick-and-dirty
way of splitting a MIME message if you need to decode something, and
you don't have a MIME mail reader on hand."

=head1 COMMAND LINE OPTIONS

None yet.  

=head1 AUTHOR

sak@essc.psu.edu

=cut

BEGIN { unshift @INC, ".." }    # to test MIME:: stuff before installing it!

require 5.001;

use strict;
use vars qw($Msgno $cmd $default_logbook $tmpdir);

use MIME::Parser;
use Getopt::Std;
##
## $Id: doelog,v 1.4 2003/06/05 13:08:16 sak Exp sak $
## $Log: doelog,v $
## Revision 1.4  2003/06/05 13:08:16  sak
## Added a kludge to force elog to return if there is nothing piped to
## it.  Now you can use doelog either as a pipe or on a single-message file
##
## Revision 1.3  2003/06/05 12:37:49  sak
## Added "configuration section" to hold config variables like default
## logbook and tmpdir.
##
## Revision 1.2  2003/06/05 12:28:03  sak
## Allow up to 50 attachments
##
## Revision 1.1  2003/06/05 12:26:24  sak
## Initial revision
##

## CONFIGURATION SECTION
## base elog cmd
$cmd = "elog -h localhost -p 8080 ";
$default_logbook="emails";
$tmpdir="/tmp";
## END CONFIGURATION SECTION

#------------------------------------------------------------
# dump_entity - dump an entity's file info
#------------------------------------------------------------
sub dump_entity {
    my $ent = shift;
    my @parts = $ent->parts;
    my $file;
    
    die "too many attachments\n" if ($#parts>50);

    if (@parts) {        # multipart...
	map { dump_entity($_) } @parts;
    }
    else {               # single part...append to elog cmd
	$file = $ent->bodyhandle->path;
	$cmd .= "-f \"$file\" ";
	###print $cmd, "\n";
	###print "    Part: ", $ent->bodyhandle->path, 
	###      " (", scalar($ent->head->mime_type), ")\n";
    }
}

#------------------------------------------------------------
# main
#------------------------------------------------------------
sub main {
    my $file;
    my $entity;
    my $subject;
    my $from;
    my $cc;
    my $logbook;
    our($opt_l);

    # Sanity:
    ## (-w ".") or die "cwd not writable, you naughty boy...";

    ## check if user wants a particular logbook
    ## fix to add host and port?
    getopts('l:');
    if($opt_l) { $logbook=$opt_l;} else {$logbook=$default_logbook;}
    $cmd .= "-l $logbook ";
    
    # Go through messages:
    @ARGV or unshift @ARGV, "-";
    while (defined($file = shift @ARGV)) {


	# Create a new parser object:
	my $parser = new MIME::Parser;
    
	# Optional: set up parameters that will affect how it extracts 
	#   documents from the input stream:
	$parser->output_under($tmpdir);
    
	# Parse an input stream:
	open FILE, $file or die "couldn't open $file";
	$entity = $parser->read(\*FILE) or 
	    print STDERR "Couldn't parse MIME in $file; continuing...\n";
	close FILE;

	## get the subject, assumes all logbooks have a subject 
	## attribute - not necessarily true.  Mine do...
	if($subject = $entity->head->get('Subject', 0)) {
	    chomp($subject);
	    $cmd .= "-a subject=\'$subject\' ";
	}
	if($from = $entity->head->get('From', 0)) {
	    chomp($from);
	    $cmd .= "-a from=\'$from\' ";
	}
	if($cc = $entity->head->get('CC', 0)) {
	    chomp($cc);
	    $cmd .= "-a cc=\'$cc\' ";
	}

	##print $cmd, "\n";

	# Congratulations: you now have a (possibly multipart) MIME entity!
	dump_entity($entity) if $entity;
	### $entity->dump_skeleton if $entity;
	### print $cmd, "\n";
        ### kludge to force elog to return
	exec "$cmd<<EOF
EOF";
    }
    1;
}

exit (&main ? 0 : -1);
#------------------------------------------------------------
1;






ELOG V3.1.5-fe60aaf