Class: IMAPProcessor::Archive
- Inherits:
-
IMAPProcessor
- Object
- IMAPProcessor
- IMAPProcessor::Archive
- 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
Instance Attribute Summary collapse
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#move ⇒ Object
readonly
Returns the value of attribute move.
-
#sep ⇒ Object
readonly
Returns the value of attribute sep.
-
#split ⇒ Object
readonly
Returns the value of attribute split.
Attributes inherited from IMAPProcessor
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options) ⇒ Archive
constructor
A new instance of Archive.
- #last_month ⇒ Object
-
#make_search ⇒ Object
Makes a SEARCH argument set from
keywords
. - #run ⇒ Object
- #the_first ⇒ Object
- #uids_by_date ⇒ Object
Methods inherited from IMAPProcessor
add_move, #capability, #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.
43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/imap_processor/archive.rb', line 43 def initialize() super @list = [:List] @move = [:Move] @sep = [:Sep] || '.' @split = [:Split] connection = connect @imap = connection.imap end |
Instance Attribute Details
#list ⇒ Object (readonly)
Returns the value of attribute list.
8 9 10 |
# File 'lib/imap_processor/archive.rb', line 8 def list @list end |
#move ⇒ Object (readonly)
Returns the value of attribute move.
8 9 10 |
# File 'lib/imap_processor/archive.rb', line 8 def move @move end |
#sep ⇒ Object (readonly)
Returns the value of attribute sep.
8 9 10 |
# File 'lib/imap_processor/archive.rb', line 8 def sep @sep end |
#split ⇒ Object (readonly)
Returns the value of attribute split.
8 9 10 |
# File 'lib/imap_processor/archive.rb', line 8 def split @split end |
Class Method Details
.process_args(args) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/imap_processor/archive.rb', line 10 def self.process_args(args) = { :List => true, :Move => false, :Split => false, } super __FILE__, args, do |opts, | opts. << "imap_archive archives old mail on IMAP server by moving it to dated mailboxen.\n EOF\n\n opts.on(\"--[no-]list\", \"Display messages (on by default)\") do |list|\n options[:List] = list\n end\n\n opts.on(\"--[no-]move\", \"Move the messages (off by default)\") do |move|\n options[:Move] = move\n end\n\n opts.on(\"--[no-]split\", \"Split mailbox into multiple months (off by default)\") do |move|\n options[:Split] = move\n end\n\n opts.on(\"-s\", \"--sep SEPARATOR\",\n \"Mailbox date separator character\",\n \"Default: Read from ~/.\#{@@opts_file_name}\",\n \"Options file name: :Sep\") do |sep|\n options[:Sep] = sep\n end\n end\nend\n" |
Instance Method Details
#last_month ⇒ Object
61 62 63 64 |
# File 'lib/imap_processor/archive.rb', line 61 def last_month t = the_first - 1 Time.local(t.year, t.month, 1).strftime("%Y-%m") end |
#make_search ⇒ Object
Makes a SEARCH argument set from keywords
69 70 71 |
# File 'lib/imap_processor/archive.rb', line 69 def make_search %W[SENTBEFORE #{the_first.imapdate}] end |
#run ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/imap_processor/archive.rb', line 73 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 today = Time.now d = today - 86400 * today.day 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 uids uids, destination, false if move end log "EXPUNGE" imap.expunge end end |
#the_first ⇒ Object
56 57 58 59 |
# File 'lib/imap_processor/archive.rb', line 56 def the_first t = Time.now Time.local(t.year, t.month, 1) end |
#uids_by_date ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/imap_processor/archive.rb', line 106 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 |