[Mew-dist 14539] Re: incdir, incmbox

Yasunari Momoi momo at example.com
2000年 10月 14日 (土) 01:29:00 JST


From: Shun-ichi TAHARA (田原 俊一) <jado at example.com>
Subject: [Mew-dist 14538] Re: incdir, incmbox
Date: Sat, 14 Oct 2000 01:09:40 +0900

> > Maildir から mail を取り込む incdir と,mbox から mail を取
> > り込む incmbox です.incdir は sh script,incmbox は perl で
> > 書いてあります.
> 
> う、先を越された :-)。
> 
> こちらでは、movemailを使わない mewinc を作ってみました。

う,やられた.

こちらはドキュメントをつけたヴァージョンを kazu さんに送って
あったんですが,追加でさっき Content-Length: 対応をしてまし
た.詳しくはソースの最後のコメントと,コマンドのへルプを参照
してください.

こちらもせっかくなので流します (^^;).

# ということで,こちらが最新版です.> kazu さん

やっぱり C で書くべきなんだろうか.perl だと機種依存部分が手
抜きできて楽なんだけど...

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

VERSION='$Id: incdir,v 1.1.1.1 2000/10/13 04:45:43 momo Exp $'
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  (default: ~/Maildir)"
    echo "    -i inboxdir   path to inboxdir  (default: .)"
    echo
    echo "version: $VERSION"
    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

# incdir - incorporate new mail from qmail user's maildir
#     author: Yasunari Momoi <momo at example.com>
#     created: 2000/10/13
#
#
# Add the following to your emacs configuration file.
#
# (setq mew-mailbox-type 'mbox)   ; this applies also maildir
# (setq mew-mbox-command "incdir")
# (setq mew-mbox-command-arg "-d /path/to/your/maildir")
#
#
# Copyright (C) 2000 Yasunari Momoi.  All rights reserved.
# Copyright notice is same of Mew's.
-------------- next part --------------
#!/usr/local/bin/perl
require 5.004;
use strict;

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

my $Version = q$Id: incmbox,v 1.2 2000/10/13 16:16:16 momo Exp $;

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

GetOptions(
	   'd=s',	\$MBox,
	   'm',		\$Delete,
	   'c',		\$UseCL,
	   '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";

# split mbox
my $fh = new FileHandle "< $tbox";
die "cannot open $tbox: $!\n" unless defined $fh;
my $seq = get_next_seq($InboxDir);
my($header, $body, $len);
my $state = 0;				# 0: undef, 1: in header, 2: in body

while (<$fh>) {

    ### use Content-Length: field
    if ($UseCL) {
	## state: unknown
	if    ($state == 0) {
	    $state = 1 if /^From\s/;
	}
	## state: header
	elsif ($state == 1) {
	    $header .= $_;
	    if (/^\r?$/) { $state = 2;  next } # end of header
	    if (/^Content-Length:\s+(\d+)/i) { $len = $1 }
	}
	## state: body
	else {
	    if (defined $len) {
		$body .= $_;
		if (length $body >= $len) {
		    $seq = put_mail($seq, $header, $body);
		    undef $header;  undef $body;  undef $len;
		    $state = 0;  next;
		}
	    }
	    # if mail has no Content-Length field, search next unix-from line.
	    else {
		if (/^From\s/) {
		    $seq = put_mail($seq, $header, $body);
		    undef $header;  undef $body;  undef $len;
		    $state = 1;  next;
		}
		$body .= $_;
	    }
	}
    }

    ### mbox
    else {
	if (/^From\s/) {
	    if ($header ne '') {
		$seq = put_mail($seq, $header);
		$header = '';
	    }
	    next;
	}
	$header .= $_;
    }
}

put_mail($seq, $header, $body) if $header ne '';

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

exit 0;


sub usage () {
    print STDERR <<"    EOM";
usage: incmbox [-h] [-m] [-c] [-d mbox] [-i inboxdir] [-e movemail] [-t tmpdir]
        -h              print this help
        -m              delete backup file
	-c              use Content-Length: field
        -d mbox         path to mbox file  (default: /var/mail/$ENV{USER})
        -i inboxdir     path to inbox directory  (default: .)
        -e movemail     path to movemail command  (default: movemail)
        -t tmpdir       temporary directory  (default: ~)

version: $Version
    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;
    foreach (@mes) { print $fh $_ }
    undef $fh;
    print $seq, "\n";
    ++$seq;
}


# incmbox - incorporate new mail from unix mbox
#     author: Yasunari Momoi <momo at example.com>
#     created: 2000/10/13
#
#
# Add the following to your emacs configuration file.
#
# (setq mew-mailbox-type 'mbox)
# (setq mew-mbox-command "incmbox")
# (setq mew-mbox-command-arg "-d /path/to/your/mbox -e /path/to/movemail")
#
# If you want to use "Content-Length:" value when splitting mail,
# you must set option "-c".
#
#
# Copyright (C) 2000 Yasunari Momoi.  All rights reserved.
# Copyright notice is same of Mew's.


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