Created
March 16, 2015 00:23
-
-
Save maki-tetsu/61de5222c879f284301b to your computer and use it in GitHub Desktop.
mhdir2mbox は mew の Maildir 形式で格納されているメールを mbox 形式に変換し thunderbird に移行するためのスクリプトです。Ruby 2.1.x での動作を確認しています。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/ruby | |
| # Copyright (C) 2008 F.Narisawa <http://fn23.seesaa.net> | |
| # Distributed under GPL | |
| # Version 0.1.2-1 | |
| # Author maki-tetsu <tim.makino@gmail.com> | |
| # * Ruby 2.1.1 ready | |
| require "find" | |
| require "fileutils" | |
| require "stringio" | |
| CRLF = "\r\n" | |
| def mkdirp(dnm) | |
| if File.directory?(dnm) then | |
| return | |
| else | |
| parentdir = dnm.gsub(%r{/[^/]*$},"") | |
| print "parentdir = #{parentdir}\n" | |
| if parentdir !~ %r{^[/.]+$} && parentdir.length > 0 then | |
| mkdirp(parentdir) | |
| end | |
| print "Make Directory: #{dnm}\n" | |
| Dir.mkdir(dnm) | |
| fn = dnm.sub(%r{.sbd$},'') | |
| print "Make Emptyfile: #{fn}\n" | |
| open(fn,"w").close | |
| end | |
| end | |
| curdir = Dir.pwd | |
| if ARGV.length < 1 then | |
| $stderr.print "mhdir2mbox.rb -d <tgtdir> <srcdir 1> [<sredir 2>...]" | |
| exit; | |
| end | |
| if ARGV[0] == "-d" then | |
| ARGV.shift | |
| tgtdir = ARGV.shift | |
| else | |
| tgtdir = curdir | |
| end | |
| if ! File.directory?(tgtdir) then | |
| $stderr.print "ERROR: Could not find directory: #{tgtdir}.\n" | |
| exit -1 | |
| end | |
| ARGV.each do |srcdir| | |
| if ! File.directory?(srcdir) then | |
| p "Can not find #{srcdir}." | |
| next | |
| end | |
| #srcdir = "./inbox" | |
| # tgtname = tgtdir + "/" + srcdir.gsub('/', '_').gsub('.','_') + ".mbox" | |
| srcname = srcdir.sub(%r{^\./}, "") | |
| srcname = srcname.sub(%r{/$}, "") | |
| if srcname =~ %r{\.+$} then | |
| p "Can not get directory name "#{srcname}". Skip." | |
| next | |
| end | |
| tgtname = tgtdir.sub(%r{/$},"") + "/" + srcname.gsub('/', '.sbd/') | |
| $stderr.print "Source = #{srcname}\n" | |
| $stderr.print "Target = #{tgtname}\n" | |
| $stderr.flush | |
| # ディレクトリ作成 | |
| dirnm = File::dirname(tgtname) | |
| if ! File.directory?(dirnm) then | |
| mkdirp(dirnm) | |
| end | |
| # ファイル作成 | |
| tgtfil = open(tgtname, "w") | |
| # Find.find(srcdir){|fpath| | |
| # 取り込み元ディレクトリからファイル読み出し | |
| Dir::foreach(srcdir)do |f| | |
| fpath = srcdir + "/" + f | |
| if File.file?(fpath) and fpath =~ %r{/[0-9]+$} then | |
| fromfil = open(fpath) | |
| begin | |
| buf = StringIO.new | |
| buf.print CRLF,"From - ",CRLF # 区切挿入 | |
| while text = fromfil.gets do | |
| text.chomp! | |
| text.scrub!('?') # UTF-8 として不正なバイトシーケンスを ? に置換する | |
| text.sub!(/\A(>*)From /,'\1>From ') | |
| buf.print text,CRLF | |
| end | |
| tgtfil.print buf.string | |
| $stderr.print "." | |
| rescue => e | |
| $stderr.print "E" | |
| end | |
| $stderr.flush | |
| end | |
| end | |
| tgtfil.print CRLF | |
| print "\n" | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ruby 2.1.1 で特定のバイトシーケンスを含む場合に正しく動作しなかったので修正しました。
Ruby 2.1 で追加された String#scrub を利用しています。