HDDが壊れて二週間、iMacがほぼ復活。大変だったのはメール。Eudora Pro 6Jを使いながらも、メールボックスのデータ形式を古いままにしてたせいか、同じEudoraを再インストールしたのに開けない。
それで思い切ってMac OS XのMailにインポートしたら、文字化けして読めないのがひとつのメールボックスに何割か出てきた。Thunderbirdにインポートしても同じ位。
化けるメールのソースを見るとEudoraの行儀が悪くて変な形式のデータになってる。Eudoraはプログラムでちゃんと表示させてたんやろうけど、MailやThunderbirdはそうじゃないらしい。こんな感じ。
- 文字化けは、ファイルを添付したメールで、まともなContent-Transfer-EncodingヘッダとContent-Typeヘッダが消えてた。Content-Typeヘッダは添付があるとMultipart/Mixedになっていて、それは残ってた。
- 自分が送った昔のメールの送信日がインポートした日時に。これはDateヘッダが行ごと無くなってた。文字化けは1.と同じ理由。
- Mailでインポートする時、Attachment Converted: という文字列があると異常終了する。そうなる理由は解らへんけど、Attachment Converted はEudoraが、添付ファイルを切り出した代わりに付ける文字列。
そういう訳で、最初のうちはメールのソースをしこしこ手修正してたけど、数が多くて大変なのでこういうスクリプトを書いてやりました。
#!/usr/bin/perl
die "need a filename" if $#ARGV < 0;
$filename = $ARGV[0];
open INFILE, "tr '¥¥r' '¥¥n' <$filename |";
$reqDateHdr = 0;
$reqTypeHdr = 0;
$nowInTypeHdr = 0;
LINE: while (<INFILE>) {
chomp;
$line = $_;
if ( /^From ¥?¥?¥?@¥?¥?¥? / ) {
# START a new email message header
# From ???@??? Sun Aug 19 12:02:36 2007
($from, $addr, $day, $month, $date, $hms, $year) = split(/ /, $line);
# Date: Thu, 09 May 1996 10:36:12 +0900
$header = "Date: $day, $date $month $year $hms +0900";
$reqDateHdr = 1;
$reqTypeHdr = 1;
$nowInTypeHdr = 0;
}
elsif ( /^$/ ) {
# END the header
if ($reqDateHdr) {
print "$header¥n";
}
if ($reqTypeHdr) {
print "Content-Transfer-Encoding: 7bit¥n";
print "Content-Type: text/plain; charset=ISO-2022-JP¥n";
}
# START a new email body
$reqDateHdr = 0;
$reqTypeHdr = 0;
$nowInTypeHdr = 0;
}
elsif ( /^Date:/ ) {
# the another DATE header will not be generated.
$reqDateHdr = 0;
$nowInTypeHdr = 0;
}
elsif ( /^[Cc]ontent-[Tt]ransfer-[Ee]ncoding:/ ) {
$nowInTypeHdr = 0;
next LINE;
}
elsif ( /^[Cc]ontent-[Tt]ype:/ ) {
if ($reqTypeHdr) {
$nowInTypeHdr = 1;
next LINE;
}
}
elsif ( /^¥s/ ) {
# supress the Content-Type header and continuous lines.
next LINE if $nowInTypeHdr;
}
else {
$nowInTypeHdr = 0;
}
if ( /[Aa]ttachment¥s+[Cc]onverted:/ ) {
next LINE;
}
print "$line¥n";
}
久しぶりにプログラミングした気がする…
手直し:2007/9/3(月)…スクリプトを画像でなくテキストに。objectタグを使ってみた。
手直し:2007/9/5(水)…< と > を &シーケンスに置き換えて正しく表示。
コメントを残す