Class: IMAPProcessor::Tidy

Inherits:
IMAPProcessor show all
Defined in:
lib/imap_processor/tidy.rb

Overview

Whereas Archive moves all mail before this month into a dated mailbox, and whereas Cleanse deletes all read unflagged mail over N days old, Tidy moves all read unflagged mail into a dated mailbox.

It’s somewhere in-between Archive and Cleanse in that it is used for mail you want to keep, but also keep out of the way of your active inbox.

Constant Summary

Constants inherited from IMAPProcessor

VERSION

Instance Attribute Summary collapse

Attributes inherited from IMAPProcessor

#imap, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from IMAPProcessor

add_move, #capability, #connect, #create_mailbox, #delete_messages, #each_message, #each_part, #log, #mime_parts, #move_messages, #noop?, run, #show_messages, #verbose?

Constructor Details

#initialize(options) ⇒ Tidy

:nodoc:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/imap_processor/tidy.rb', line 40

def initialize options # :nodoc:
  super

  log "Tidy: #{options[:Host]}"

  self.move = options[:move]

  connection = connect

  @imap = connection.imap
end

Instance Attribute Details

#moveObject

Whether to move the mail or just display. default:false



18
19
20
# File 'lib/imap_processor/tidy.rb', line 18

def move
  @move
end

Class Method Details

.process_args(args) ⇒ Object

:nodoc:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/imap_processor/tidy.rb', line 20

def self.process_args args # :nodoc:
  required_options = {
    :move => false,
  }

  super __FILE__, args, required_options do |opts, options|
    opts.banner << <<~EOF
      imap_tidy moves older messages from your mailboxen into dated mailboxen.
    EOF

    opts.on "--days=N", Integer, "Override age to move messages" do |n|
      options[:age] = n
    end

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

Instance Method Details

#runObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/imap_processor/tidy.rb', line 76

def run
  @boxes.each do |mailbox, days_old|
    select mailbox

    before_date = Time.now - 86_400 * (options[:age] || days_old)
    uids        = search %W[SEEN UNFLAGGED BEFORE #{before_date.imapdate}]

    next if uids.empty?

    log "FOUND %p" % [uids]

    uids_to_dates(uids)                 # id => "YYYY-MM"
      .multi_invert                     # "YYYY-MM" => [id,...]
      .sort
      .each do |date, uids|
        destination = "%s-%s" % [mailbox, date]
        show_messages uids
        move_messages uids, destination, false if move
      end

    log "EXPUNGE"
    imap.expunge if move unless noop?
  end
end

#search(args) ⇒ Object

Search a selected mailbox with args TODO: push up



65
66
67
68
# File 'lib/imap_processor/tidy.rb', line 65

def search args
  log "SEARCH #{args.join " "}"
  imap.search args
end

#select(mailbox) ⇒ Object

Select a mailbox TODO: push up



56
57
58
59
# File 'lib/imap_processor/tidy.rb', line 56

def select mailbox
  log "SELECT #{mailbox}"
  imap.select mailbox
end

#uids_to_dates(uids) ⇒ Object



70
71
72
73
74
# File 'lib/imap_processor/tidy.rb', line 70

def uids_to_dates uids
  imap
    .fetch(uids, "INTERNALDATE")
    .to_h { |fd| [fd.seqno, Time.imapdate(fd.attr["INTERNALDATE"]).yyyy_mm] }
end