#! /usr/bin/ruby # Copyright (c) 2007 Yuya.Nishida. # # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files # (the "Software"), to deal in the Software without restriction, # including without limitation the rights to use, copy, modify, merge, # publish, distribute, sublicense, and/or sell copies of the Software, # and to permit persons to whom the Software is furnished to do so, # subject to the following conditions: # # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR # ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH # THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require "time" require "fileutils" SEPARATOR = /^\0*(\d{2}-\w{3}-\d{4} \d{2}:\d{2}:\d{2} \+\d{4}),(\d+);\d+-\w+\r$/ class NullWriter def write(s) # nop end end def convert_next_mail(mbx, output) match_data = nil loop do l = mbx.gets break if l.nil? match_data = SEPARATOR.match(l) break if match_data output.write(l) end return match_data ? [Time.parse(match_data[1]), match_data[2].to_i] : nil end def skip_mbx_header(mbx) return convert_next_mail(mbx, NullWriter.new) end mbx_filename = ARGV.shift mh_dirname = ARGV.shift if mh_dirname.nil? STDERR.puts("usage: #{File.basename($0)} mbx_filename mh_dirname") exit(1) end FileUtils.mkdir_p(mh_dirname) count = 0 open(mbx_filename) do |mbx| mail_time, length = *skip_mbx_header(mbx) while mail_time output_filename = File.join(mh_dirname, count.to_s) next_mail_time = nil open(output_filename, "w") do |mh| next_mail_time, length = *convert_next_mail(mbx, mh) end File.utime(Time.now, mail_time, output_filename) mail_time = next_mail_time count += 1 puts("wrote #{count} mails.") if (count % 100).zero? end puts("wrote #{count} mails.") end