#! /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. SEPARATOR = /\A(\0*\d{2}-\w{3}-\d{4} \d{2}:\d{2}:\d{2} \+\d{4},)(\d+)(;\d+-\w+\r\n)\z/ MbxMailHeader = Struct.new(:head, :length, :tail) in_filename = ARGV.shift out_filename = ARGV.shift if out_filename.nil? STDERR.puts("usage: #{File.basename($0)} in_filename out_filename") exit(1) end open(in_filename) do |in_file| open(out_filename, "w") do |out_file| # output mbx header out_file.write(in_file.read(2048)) l = in_file.gets match_data = SEPARATOR.match(l) # processing mails count = 0 while match_data count += 1 header = MbxMailHeader.new(*match_data.captures) match_data = nil mail = "" while l = in_file.gets match_data = SEPARATOR.match(l) break if match_data mail << l end if header.length.to_i != mail.length puts("miss-matched size mail[#{count}]: header (#{header.length}) != mail size (#{mail.length})") header.length = mail.length end out_file.write(header.values.join) out_file.write(mail) puts("wrote #{count} mails.") if $DEBUG && (count % 100).zero? end puts("wrote #{count} mails.") end end