############################################################# # # pim_to_file.pl # # 2009.07.09 KNOnline.NET Suika # http://www.knonline.net # # # 携帯電話のPIMデータ(メールデータ)に含まれる添付ファイルを # 全て書き出します。 # # # 添付ファイルのファイル名は当てにならない(image.jpg)などが多く # また重複もしやすいので日付から生成する仕様になっています。 # *拡張子はファイル名から取得します。 # # # フォーマットを厳密に解釈していないので機種によっては正しく動作 # しないかもしれません。 # N905iμで確認しました。 # (メール→受信BOX→機能→全てmicroSDに書き出し) # # # ■使い方 # perl pim_to_file.pl 入力ファイル [出力ディレクトリ] # # SDカードに書き出した場合はF:\SD_PIM\PIM00002.VMG などの名前で # 保存されるようです。 # # use HTTP::Date; use MIME::Base64; use strict; my $infile = $ARGV[0]; my $outdir = $ARGV[1]; if($infile eq ''){ print "\n"; print "perl pim_to_file.pl 入力ファイル [出力ディレクトリ]\n"; print "\n"; print "\n"; exit(0); } if($outdir ne ''){ $outdir =~ s/\\$//; $outdir.= "\\"; } if(!open(DATA, $infile)){ print "入力ファイルが開けません。"; exit(0); } my $vmsg=0; my $vbody=0; my $text=0; my $base64=0; my $daycnt=0; my $date = ''; my $dateprev = ''; my @encoded; my $extention=''; my $line; foreach $line () { $line =~ s/\x0D?\x0A?$//; if($line eq 'BEGIN:VMSG'){ $vmsg = 1; } if($line eq 'END:VMSG'){ $vmsg = 0; } if($vmsg == 1){ if($line eq 'BEGIN:VBODY'){ $vbody = 1; } if($line eq 'END:VBODY'){ $vbody = 0; } } if($vbody == 1){ if ($line =~ /Content-Type: text\/plain(.*)/) { $text = 1; } if ($line =~ /--(.*)/) { $text = 0; $base64 =0; if(@encoded){ &base64_to_file($outdir,$date,$daycnt,$extention,@encoded); } $extention = ''; @encoded = (); } if($text == 0){ if ($line =~ /Date: (.+)/) { $date = $1; if($date ne $dateprev){ $daycnt = 0; } $dateprev = $date; } if ($line =~ /Content-Type: (.+)name=\"(.+)\.(.+)\"/) { #" $extention = $3; } if ($line eq 'Content-Transfer-Encoding: base64') { $base64 = 1; } } } if($base64 ==1){ if ($line ne 'Content-Transfer-Encoding: base64' && $line ne '') { push( @encoded, $line ); } } } close(DATA); exit(0); sub base64_to_file{ my($outdir, $htmldate, $daycnt, $extention , @data) = @_; my $time = str2time($htmldate); my ($sec,$min,$hor,$mday,$mon,$year,$dd0,$dd1,$dd2) = localtime($time); my $time_str = sprintf("%02d%02d%02d_%02d%02d%02d",($year+1900)%100,$mon+1,$mday,$hor,$min,$sec); my $filename; if($daycnt >0 ){ $time_str .= "_$daycnt"; } if($extention ne ''){ $filename = $time_str . '.' . $extention; }else{ $filename = $time_str; } if(!open(OUT, ">$outdir$filename") ){ print "Can't write the file \"$filename\" !!!\n"; return ; } binmode OUT; foreach (@data){ print OUT decode_base64($_); } close(OUT); print "\"$outdir$filename\"を生成しました。\n"; }