[Mew-dist 14517] incdir, incmbox

Yasunari Momoi momo at example.com
2000年 10月 12日 (木) 23:02:11 JST


Maildir から mail を取り込む incdir と,mbox から mail を取
り込む incmbox です.incdir は sh script,incmbox は perl で
書いてあります.

どちらのコマンドも -h で usage が出るので,それを参考にして
ください.デフォルト動作をさせれば,*BSD ならたいていいける
と思います (^^;).

incmbox は [Mew-dist 14199] で出ている 4 つの形式のうち,
mboxo と mboxrd だけに対応しています.>From の unquote はいっ
さいしていません.

間に合わせで作ったので,あまりちゃんとしていないところもあり
ますが,とりあえずたいていは動くと思われるのと,最初に出せば
誰かが改造してくれるかもという期待を込めて :-),流しておきま
す (_ _).

-- momo

-------------- next part --------------
#!/bin/sh

IDIR=.
MDIR=~/Maildir
MOVE="YES"

args=$(getopt bd:i: $*)
if [ $? != 0 ]; then
    echo "usage: incdir [-b] [-d maildir] [-i inboxdir]"
    echo "    -b            backup mail to maildir/cur directory"
    echo "    -d maildir    path to maildir"
    echo "    -i inboxdir   path to inboxdir"
    exit 1
fi
set -- $args
for i
do
    case "$i" in
    -b)
	MOVE="NO"; shift
	;;
    -d)
	MDIR="$2"; shift; shift
	;;
    -i)
	IDIR="$2"; shift; shift
	;;
    --)
	shift; break
	;;
    esac
done

if [ \( ! -d $MDIR/new \) -o \( $MOVE = "NO" -a ! -d $MDIR/cur \) ]; then
    echo "can't find $MDIR."
    exit 1
fi

if [ ! -d $IDIR ]; then
    echo "can't find $IDIR."
    exit 1
fi

SEQ=$(ls -f1 $IDIR | egrep '^[1-9][0-9]*$' | sort -n | tail -1)
if [ "$SEQ"X = ""X ]; then
    SEQ=1
else
    SEQ=$(($SEQ + 1))
fi

for f in $MDIR/new/*; do
    # sanity check
    [ ! -f $f ] && continue
    while [ -f $IDIR/$SEQ ]; do
	SEQ=$(($SEQ + 1))
    done

    if [ "$MOVE"X = "YES"X ]; then
	mv $f $IDIR/$SEQ
    else
	cp -p $f $IDIR/$SEQ
	mv $f $MDIR/cur/$(basename $f):2,S
    fi
    echo $SEQ
    SEQ=$(($SEQ + 1))
done

exit 0
-------------- next part --------------
#!/usr/local/bin/perl
require 5.004;
use strict;

use FileHandle;
use DirHandle;
use Getopt::Long;

my $Movemail = "movemail";
my $MBox = sprintf "/var/mail/%s", $ENV{USER};
my $TMPDir = $ENV{HOME};
my $InboxDir = '.';
my $Delete = undef;
my $Help = undef;

GetOptions(
	   'd=s',	\$MBox,
	   'm',		\$Delete,
	   'e=s',	\$Movemail,
	   't=s',	\$TMPDir,
	   'i=s',	\$InboxDir,
	   'h',		\$Help,
	  );

usage() if $Help;

my $tbox = sprintf "%s/mbox.%s.%d", $TMPDir, datefmt(time), $$;

system($Movemail, $MBox, $tbox) == 0 or
    die "system($Movemail, $MBox, $tbox) failed: $?\n";

# parse mail
my $fh = new FileHandle "< $tbox";
die "cannot open $tbox: $!\n" unless defined $fh;
my $seq = get_next_seq($InboxDir);
my $type = undef;
my $mes = '';
while (<$fh>) {
    unless (defined $type) {
	if (/^From /) { $type = 'from' }
	else	      { $type = 'cl' }
    }
    if ($type eq 'from') {
	if (/^From /) {
	    if ($mes ne '') {
		$seq = put_mail($seq, $mes);
		$mes = '';
	    }
	    next;
	}
    }
    else {
	# XXX type 'cl' not yet
    }
    $mes .= $_;
}
put_mail($seq, $mes) if $mes ne '';

unlink $tbox if $Delete or -z $tbox;

exit 0;


sub usage () {
    print STDERR <<"    EOM";
usage: incmbox [-m] [-h] [-d mbox] [-i inboxdir] [-e movemail] [-t tmpdir]
        -h              print this help
        -m              delete backup file
        -d mbox         path to mbox file
        -i inboxdir     path to inbox directory
        -e movemail     path to movemail command
        -t tmpdir       temporary directory
    EOM
    exit 0;
}

sub datefmt () {
    my($time) = @_;
    my($sec, $min, $hour, $mday, $mon, $year) = (localtime($time));
    sprintf("%04d%02d%02d-%02d%02d%02d",
	    $year+1900, $mon+1, $mday, $hour, $min, $sec);
}

sub get_next_seq () {
    my($dir) = @_;
    my $seq = 0;
    my $dh = new DirHandle $dir;
    die "cannot open $dir: $!\n" unless defined $dh;
    while (defined($_ = $dh->read)) {
	next if /[^\d]/;
	$seq = $_ if $seq <= $_;
    }
    undef $dh;
    ++$seq;
}

sub put_mail () {
    my($seq, $mes) = @_;
    # sanity check
    while (-f "$InboxDir/$seq") { ++$seq }
    my $fh = new FileHandle "> $InboxDir/$seq";
    die "cannot open $InboxDir/$seq: $!\n" unless defined $fh;
    print $fh $mes;
    undef $fh;
    print $seq, "\n";
    ++$seq;
}


Mew-dist メーリングリストの案内