Created
March 25, 2012 11:03
-
-
Save wereHamster/2192838 to your computer and use it in GitHub Desktop.
This file contains 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/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