class IMAPProcessor::Archive

Archives old mail on IMAP server by moving it to dated mailboxen.

Attributes

list[R]
move[R]
sep[R]
split[R]

Public Class Methods

new(options) click to toggle source
Calls superclass method IMAPProcessor::new
# File lib/imap_processor/archive.rb, line 43
def initialize(options)
  super

  puts "Archiving #{options[:Host]}"

  @list = options[:List]
  @move = options[:Move]
  @sep  = options[:Sep] || '.'
  @split = options[:Split]

  connection = connect

  @imap = connection.imap
end
process_args(args) click to toggle source
Calls superclass method IMAPProcessor::process_args
# File lib/imap_processor/archive.rb, line 10
  def self.process_args(args)
    required_options = {
      :List => true,
      :Move => false,
      :Split => false,
    }

    super __FILE__, args, required_options do |opts, options|
      opts.banner << <<-EOF
imap_archive archives old mail on IMAP server by moving it to dated mailboxen.
      EOF

      opts.on("--[no-]list", "Display messages (on by default)") do |list|
        options[:List] = list
      end

      opts.on("--[no-]move", "Move the messages (off by default)") do |move|
        options[:Move] = move
      end

      opts.on("--[no-]split", "Split mailbox into multiple months (off by default)") do |move|
        options[:Split] = move
      end

      opts.on("-s", "--sep SEPARATOR",
              "Mailbox date separator character",
              "Default: Read from ~/.#{@@opts_file_name}",
              "Options file name: :Sep") do |sep|
        options[:Sep] = sep
      end
    end
  end

Public Instance Methods

run() click to toggle source
# File lib/imap_processor/archive.rb, line 70
def run
  @boxes.each do |mailbox|
    log "SELECT #{mailbox}"
    imap.select mailbox

    uids_by_date = self.uids_by_date

    next if uids_by_date.empty?

    unless split then
      d = the_first - 1       # one second back into last month
      latest = [d.year, d.month]

      uids_by_date = {
        latest => uids_by_date.values.flatten(1)
      }
    end

    uids_by_date.sort.each do |date, uids|
      next if uids.empty?
      destination = "#{mailbox}#{sep}%4d-%02d" % date
      puts "#{destination}:"
      puts
      show_messages uids
      move_messages uids, destination, false if move
    end

    log "EXPUNGE"
    imap.expunge unless noop?
  end
end
the_first() click to toggle source
# File lib/imap_processor/archive.rb, line 58
def the_first
  t = Time.now
  Time.local(t.year, t.month, 1)
end
uids_by_date() click to toggle source
# File lib/imap_processor/archive.rb, line 102
def uids_by_date
  search = make_search
  log "SEARCH #{search.join ' '}"
  uids = imap.search search

  return {} if uids.empty?

  payload = imap.fetch(uids, 'BODY.PEEK[HEADER.FIELDS (DATE)]')

  mail = Hash[uids.zip(payload).map { |uid, m|
    date = m.attr["BODY[HEADER.FIELDS (DATE)]"].strip.split(/:\s*/, 2).last
    date = Time.parse(date) rescue Time.now
    [uid, date]
  }]

  mail.keys.group_by { |uid|
    date = mail[uid]
    [date.year, date.month]
  }
end