Skip to content

Instantly share code, notes, and snippets.

@wereHamster
Created March 25, 2012 11:03
Show Gist options
  • Save wereHamster/2192838 to your computer and use it in GitHub Desktop.
Save wereHamster/2192838 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'pathname'
require 'mail'
class Archiver
attr_reader :maildir, :time
def initialize(maildir)
@maildir = maildir
@time = Time.at(Time.now.to_i - 60 * 60 * 24)
end
def run
Pathname.new("#{maildir}/cur").entries.each do |path|
file = Pathname.new("#{maildir}/cur/#{path.to_s}")
archive(file) if file.file?
end
end
def archive(file)
mail = Mail.read(file.realpath)
return if mail.date.to_time > time
puts "#{mail.date.strftime("%Y/%m/%d")}\t#{file.basename}"
file.rename("#{archive_path_for(mail)}/#{file.basename}")
rescue
puts $!.inspect
puts "Couldn't archive #{file}"
end
def archive_path_for(mail)
date = mail.date.strftime("%Y/%m/%d")
dir = Pathname.new("#{maildir}/archive/#{date}")
dir.mkpath
return dir
end
end
Archiver.new("#{ENV['HOME']}/.mail").run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment