#! /usr/local/bin/perl # 与えられたファイルを展開してカレントディレクトリに残骸を撒き散らす (^_^; @endls = ( '*dummy*', ); $tmdecode = '/vacia/bin/sh/tmdecode'; $mnumb = `LANG=C /bin/date "+%y%m%d.$$"`; $mnumb =~ s/[ \t\r\n]*$//; foreach $target ( @ARGV ) { if(open(DI,"$target")) { $mnumc++; $mnum = "$mnumb.$mnumc"; local(@my_buf); undef @my_buf; while() { s/[ \t\r\n]*$//; if(/^$/) { $_ = ' '; } push(@my_buf,$_); } &parse_article(@my_buf); close(DI); } } exit; sub parse_article { # article 全体が与えられていることを仮定する local(@buf) = @_; local(%head) = &parse_article_header(*buf); if($head{'content-type'} =~ /^multipart\//) { &parse_article_subchapter(*buf); } else { &parse_body(*buf); } } sub parse_article_subchapter { local(*buf) = @_; local($line, @my_buf); local($flag) = undef; local($endl) = $head{'content-type'}; $endl =~ s/^.*;boundary\=(\")(.*)(\").*$/\2/; $endl = "--$endl"; local($endll) = length($endl); local($popl) = $endls[0]; local($popll) = length($popl); push(@endls, $endl); foreach $line ( @buf ) { if($flag < 1 && $line =~ /^ $/) { @my_buf = ( ' ', ); $flag = 1; next; } if($flag < 1) { next; } if(substr($line,0,$endll) eq $endl) { &parse_article(@my_buf); undef @my_buf; } elsif(substr($line,0,$popll) eq $popl) { &parse_article(@my_buf); undef @my_buf; last; } else { push(@my_buf,$line); } } if(0 <= $#my_buf) { &parse_article(@my_buf); } pop(@endls); return; } sub parse_body { local(*buf) = @_; local($flag, $mode); local($afo) = $head{'content-transfer-encoding'}; $afo =~ y/A-Z/a-z/; if($afo =~ /[Bb]ase64/) { $mode = 'base64'; } elsif($afo =~ /x\-uue/) { $mode = 'uu'; } elsif($afo =~ /quoted\-printable/) { $mode = 'asis'; } else { $mode = 'asis'; } undef $afo; local($i); foreach $i ( 'content-type', 'content-disposition' ) { $afo = $head{$i}; $afo =~ y/A-Z/a-z/; if($afo =~ /name\=/) { last; } } if($afo =~ /name\=/) { $afo = $'; $afo =~ s/;.*$//; $afo =~ s/\"(.*)\"/\1/; } if($afo !~ /[A-z]/) { $uniqnum++; $afo = "$uniqnum"; } local($ofn) = "mf$mnum-$afo"; local($ifn) = "/tmp/expanded-temp.$$-$uniqnum"; local($fn) = $ifn; if( $mode eq 'asis' ) { $fn = $ofn; } if(open(O,">$fn")) { foreach $line ( @buf ) { if($flag < 1 && $line =~ /^ $/) { $flag = 1; next; } if($flag < 1) { next; } print O "$line\n"; } close(O); if($mode eq 'base64' || $mode =~ /x\-uue/) { $com = "$tmdecode $mode $ifn $ofn"; print "# exec \% $com\n"; system("$com"); } } } sub parse_article_header { local(*buf) = @_; local($line, %head, $item, $val); foreach $line ( @buf ) { if($line =~ /^ $/) { last; } if($line =~ /^[A-z0-9\-]+: */) { ($item,$val) = ( $&, $' ); $item =~ s/: *$//; $item =~ y/A-Z/a-z/; $head{$item} = $val; } else { $line =~ /^[ \t]*/; $head{$item} .= $'; } } return %head; }