#!/usr/bin/perl -w
# vim: set sw=4 ts=4 si et:
# Copyright: GPL, Author: Guido Socher
# Do not load parser here but below on demand. This is faster
# if you do not need the parser (e.g for the help function in this
# module)
use MIME::Parser;
sub help(){

print  "mimeparsetst -- test installation of MIME::Parser

This is not a full test but it tests important parts needed for
lfwmail. To run the test execute: 
mimeparsetst test

This should produce a printout like this:
    Content-type: text/html
    Effective-type: text/html
    Body-file: /tmp/msg-1048023283-2040-0/msg-2040-1.html
    Subject: lfwmail is cool
    --
    Mail is from: \"Test Usr\" <test\@somewhere.com>

    part;0;text/html^unknown^/tmp/msg-1048023283-2040-0/msg-2040-1.html
";
 exit 0;
}
sub getmimeparts($$$);


sub getmimeparts($$$){
    my $entity=shift;
    my $result=shift;
    my $i=shift;
    my $path='';
    if ($entity->bodyhandle){
        $path = $entity->bodyhandle->path;
    }
    if ($path){
        # ignore empty parts with no file name
        $type=$entity->effective_type||'';
        $fname=$entity->head->recommended_filename||'unknown';
        $fname=~s/\^/./g;
        push(@{$result}, "part;$i;$type^$fname^$path\n");
    }
    foreach my $part ($entity->parts){
        $i++; # part number, note you can have in 
        #the result part 2, 3,5 (no 0, 1 no 4)
        getmimeparts($part,$result,$i);
    }
}
help() unless($ARGV[0] && $ARGV[0] eq "test");

require MIME::Parser;
my $parser = new MIME::Parser;
$parser->output_under("/tmp");
my $entity = $parser->parse(\*DATA);
$entity->dump_skeleton;

my $head = $entity->head;
my $from=$head->get('From');
if ($from){
    print "Mail is from: $from\n";
}
my @mimeparts;
getmimeparts($entity,\@mimeparts,0);
for  (@mimeparts){
    print;
}


__END__ 
From guido@linuxfocus.org  Thu Mar  6 12:21:29 2003
Return-Path: <guido@linuxfocus.org>
Received: by freespirit.linuxfocus.org (Postfix, from userid 501)
	id 7572E1123B; Thu,  6 Mar 2003 01:02:50 +0100 (CET)
Delivered-To: guido@linuxfocus.org
From: "Test Usr" <test@somewhere.com>
To: guido@linuxfocus.org
Subject: lfwmail is cool
Date: Thu, 06 Mar 2003 00:01:25 +0000
Mime-Version: 1.0
Content-Type: text/html
Sender: guido@linuxfocus.org
X-Spam-Status: No, hits=1.3 required=5.0
	tests=AWL,SPAM_PHRASE_08_13
	version=2.43
X-Spam-Level: *
Status: RO
Content-Length: 220
Lines: 7

<html><div style='background-color:'><DIV>
<P>aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
cccccccccccccccccccccccccccccccccccccccc
dddddddddddddddddddddddddddddddddddddddd
</P>
</html>
