#!/usr/local/bin/perl #$Header: /cygdrive/h/CVSROOT/www2/246/toys/mail/mh2rmail.pl,v 1.1 2001/01/22 17:51:23 miura Exp $ # # MHのフォルダをRMAILファイルに変換する # # 今後の課題:summary-line(これは自分のアドレスを知らなければならない) # # # # usage: jperl mh2rmail.pl [+]folder > RMAIL-FILE # # minimum customization $homedir=$ENV{'HOME'}; # ホームディレクトリ $profile='.mh_profile'; # プロファイルのファイル名 # option switches $debug=0; # debug message # global variables $maildir="Mail"; # メールを格納するトップディレクトリ $folder="inbox"; # メッセージを格納するフォルダ @head=( "BABYL OPTIONS:\n", "Version: 5\n", "Labels:\n", "Note: This is the header of an rmail file.\n", "Note: If you are seeing it in rmail,\n", "Note: it means the file has no messages in it.\n\037"); @toprune=("^via:", "^mail-from:", "^origin:", "^status:", "^received:","^message-id:","^summary-line:"); require 'getopts.pl'; &Getopts('hdpsm:'); if (defined($opt_h)) { print < RMAIL-FILE options: -h : this help -d : debug mode -p : prune header -s : build summary-lines -m DIR : mh mail directory which contains folders END_HELP exit(0); } if (defined($opt_d)) { $debug=1; } $op_prune=0; $op_summary=0; if (defined($opt_p)) { $op_prune=1; ($debug) && print "option: header pruning\n"; } if (defined($opt_s)) { $op_summary=1; ($debug) && print "option: build summary-line\n"; } if (defined($opt_m)) { $maildir=$opt_m; ($debug) && print "option: mail dir=$maildir\n"; } else { # プロファイルよりMHディレクトリを取り出す。 ($debug) && print "reading profile...\n"; open(PROF, "$homedir/$profile") || die("profile '$homedir/$profile' not found\n"); while () { ($debug) && print; chop; if (/^path:\s*(\S+)/io) { $maildir=$1; if ($maildir !~ /^\//o) { $maildir="$homedir/$maildir"; } ($debug) && print "mail dir=$maildir\n"; } } } close(PROF); if ($#ARGV == -1) { die("($#ARGV)\nusage: mh2rmail [+]folder\n"); } $dirname=$ARGV[0]; if ($dirname =~ /^\+/) { $dirname=substr($dirname,1); } if (!defined($dirname)) { die("folder not specified.\n"); } ($debug) && print "scanning: $dirname\n"; opendir(DIR,"$maildir/$dirname") || die("can't open folder $dirname.\n"); @dir=readdir(DIR); closedir(DIR); @folder=sort numcomp @dir; print @head; while (@folder) { $article="$folder[0]"; shift(@folder); if ($article =~ /[0-9]+/o) { ($debug) && print "."; open(FROM, "$maildir/$dirname/$article") || die("can't open '$article'\n"); if ($op_prune) { print "\014\n1,,\n"; # ヘッダを読む。 @header=(''); # dummy element @myhead=(); while (($b=) !~ /^$/) { if ($b =~ /^[A-Z]\S+:/io) { push(header, $b); } else { # 前のヘッダ行の続き $header[$#header] .= $b; } } shift(@header); # cut off dummy at the top # summary-line を @header に unshift でつける while (@header) { $head=@header[0]; shift(@header); print $head; $prune=0; foreach $field (@toprune) { # ここは @toprune を展開すれば高速化できる if ($head =~ /$field/i) { $prune=1; last; } } if (!$prune) { push(myhead, $head); } } print "\n"; print "*** EOOH ***\n"; # prune したヘッダを挿入 print @myhead; print "\n"; } else { print "\014\n0, unseen,,\n"; print "*** EOOH ***\n"; } while ($b=) { print $b; } close(FROM); printf "\037"; } } # ファイル名数字ソートのための比較ルーチン # DOS版perlが sort BLOCK LIST というシンタックスを認めてくれなかったため sub numcomp { $a <=> $b; }; 0;