Class: IMAPProcessor::Archive

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

Overview

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

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, #connect, #create_mailbox, #delete_messages, #each_message, #each_part, #log, #mime_parts, #move_messages, run, #show_messages, #verbose?

Constructor Details

#initialize(options) ⇒ Archive

Returns a new instance of Archive.



30
31
32
33
34
35
36
37
38
39
# File 'lib/imap_processor/archive.rb', line 30

def initialize(options)
  super

  @list = options[:List]
  @move = options[:Move]

  connection = connect

  @imap = connection.imap
end

Instance Attribute Details

#listObject (readonly)

Returns the value of attribute list.



7
8
9
# File 'lib/imap_processor/archive.rb', line 7

def list
  @list
end

#moveObject (readonly)

Returns the value of attribute move.



7
8
9
# File 'lib/imap_processor/archive.rb', line 7

def move
  @move
end

Class Method Details

.process_args(args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/imap_processor/archive.rb', line 9

def self.process_args(args)
  required_options = {
    :List => true,
    :Move => 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
  end
end

Instance Method Details

#last_monthObject



46
47
48
49
# File 'lib/imap_processor/archive.rb', line 46

def last_month
  t = the_first - 1
  Time.local(t.year, t.month, 1).strftime("%Y-%m")
end

#make_searchObject

Makes a SEARCH argument set from keywords



54
55
56
# File 'lib/imap_processor/archive.rb', line 54

def make_search
  %W[SENTBEFORE #{the_first.imapdate}]
end

#runObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/imap_processor/archive.rb', line 58

def run
  @boxes.each do |mailbox|
    destination = "#{mailbox}.#{last_month}"

    log "SELECT #{mailbox}"
    response = imap.select mailbox

    search = make_search

    log "SEARCH #{search.join ' '}"
    uids = imap.search search

    next if uids.empty?

    puts "#{uids.length} messages in #{mailbox}#{list ? ':' : ''}"

    show_messages uids

    move_messages uids, destination if move
  end
end

#the_firstObject



41
42
43
44
# File 'lib/imap_processor/archive.rb', line 41

def the_first
  t = Time.now
  the_first = Time.local(t.year, t.month, 1)
end