Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 8 Dec 1997 19:21:33 +0100
From:      Ollivier Robert <roberto@keltia.freenix.fr>
To:        freebsd-chat@FreeBSD.ORG
Subject:   Re: wanted: mail-filter capable of the maildir-format
Message-ID:  <19971208192133.41658@keltia.freenix.fr>
In-Reply-To: <Pine.BSF.3.96.971208123941.22686A-100000@blue.bad.bris.ac.uk>; from Aled Treharne on Mon, Dec 08, 1997 at 12:44:17PM %2B0000
References:  <19971206184553.58471@paert.tse-online.de> <Pine.BSF.3.96.971208123941.22686A-100000@blue.bad.bris.ac.uk>

next in thread | previous in thread | raw e-mail | index | archive | help

--fUYQa+Pmc3FrFX/N
Content-Type: text/plain; charset=us-ascii

According to Aled Treharne:
> procmail (v3.11pre7) supports the maildir format, so long as you have
> your mail in maildir formats in the first place.

Where did you get that idea ?

My copy if 3.11pre7 supports only mbox and MH formats...
 
> On this not, does anyone know of an easy way of changing normal (?)
> mailboxes into maildir format? At the mo' I have to save the mail to a

Here is a script of mine to do that. You'll need Perl5 and the Mailtools
module (Mail::Internet).

> Pine 3.95 supports maildir format too. :)

Mutt supports it of course :-)
-- 
Ollivier ROBERT -=- FreeBSD: The Power to Serve! -=- roberto@keltia.freenix.fr
FreeBSD keltia.freenix.fr 3.0-CURRENT #18: Tue Nov 25 22:32:12 CET 1997

--fUYQa+Pmc3FrFX/N
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Content-Description: Mbox -> Maildir mailbox converter
Content-Disposition: attachment; filename=mbox2maildir

#! /usr/local/bin/perl -w
#
# Takes a proper mbox file and turn it into a Maildir mailbox
# suitable for Mutt
#
# Copyright © 1997 Ollivier Robert <roberto@keltia.freenix.fr>
# Use freely as long as you don't pretend being the author. No warranty
# whatsoever.
#
# Usage: mbox2maildir mbox maildir
#
# 1.1    17/3/97    roberto
# 1.2     idem      distinguish between old and new messages, smarter
#                   count lines and output Lines: entry.
#
# $Id: mbox2maildir,v 1.3 1997/03/17 21:53:37 roberto Exp $

require 5.003;

use strict;
use vars qw($opt_h);

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## Standard packages
##
use FileHandle;
use Sys::Hostname;
use Getopt::Std qw(getopts);
use File::Basename qw(basename);

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## Found in Mail::Internet
##
use Mail::Internet;
use Mail::Util qw(read_mbox);

my $argv0 = basename($0);
my $version = sprintf("%d.%d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## Variables
##

my $hostname = hostname;
my $mess;

my $total_msgs = 0;
my %msg_count = (
                 new => 0,
                 cur => 0,
                );

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## Show usage
##
sub do_usage
{
    print <<"EOTEXT";
$argv0: Mbox to Maildir conversion program
Version $version by Ollivier Robert <roberto\@keltia.freenix.fr>

Usage: $argv0 [-h]

Options:  -h        displays this message

EOTEXT
    exit 0;
}

## -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
## Beginning of program
##

getopts ('h');

do_usage()
    if (defined $opt_h or $#ARGV != 1);

my $mbox = shift;
my $maildir = shift;

print <<"EOTEXT";
$argv0: mbox to Maildir conversion program
Version $version by Ollivier Robert <roberto\@keltia.freenix.fr>

EOTEXT

die "File $mbox doesn't exist ! : $!\n"
    if not -f $mbox;

die "Directory $maildir doesn't exist ! : $!\n"
    if not -d $maildir;

if (not (-d "$maildir/cur" and -d "$maildir/tmp" and "$maildir/new"))
{
    warn "Maildir folder without a proper structure, correcting...\n";
    mkdir $maildir. "/cur", 0700;
    mkdir $maildir. "/tmp", 0700;
    mkdir $maildir. "/new", 0700;
}

my @msgs = read_mbox ($mbox);

## Keep machine name
$hostname =~ s{(.*?)(\..*|)$}{$1};

die "Problem reading $mbox"
    if (not defined @msgs or $#msgs == -1);

foreach $mess (@msgs)
{
    my $status;

    ## Get the headers
    ##
    my $mail = new Mail::Internet $mess, MailFrom => 'IGNORE';

    my @body = @{$mail->body};
    my $lines = $#body;

    ## Get the Status: header.
    ##
    $status = $mail->get ("status");

    $mail->replace ("lines",
                    "Lines: $lines");
    $mail->delete ("content-length");

    ## Constructs the maildir filename
    ##
    my $when = time + $total_msgs;
    my $future_name = "$when.${$}_$total_msgs.$hostname";

    my $fh_out = new FileHandle ">$maildir/tmp/$future_name";
    die "Error writing $future_name: $!\n"
        if not defined $fh_out;

    ## Write the message
    ##
    eval { $mail->print ($fh_out) };
    die "$@"
        if $@;

    ## Nothing wrong, move to /new or /cur (if Status: exists)
    ##
    eval 
    { 
        rename "$maildir/tmp/$future_name", 
               "$maildir/" . ($status ? "cur" : "new") . "/$future_name"; 
    };
    die "$@"
        if $@;

    ## Cleanup
    ##
    $fh_out->close;

    ## Update the counters
    ##
    eval { $status ? $msg_count{'cur'}++ : $msg_count{'new'}++ };
    $total_msgs++;
}

print <<"EOTEXT";
There were
    $msg_count{'new'} new message(s)
    $msg_count{'cur'} message(s)
written in $maildir/new

EOTEXT


--fUYQa+Pmc3FrFX/N--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19971208192133.41658>