Class: IMAPProcessor::Keywords

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

Overview

Lists keywords present on a server

Constant Summary

Constants inherited from IMAPProcessor

VERSION

Instance Attribute Summary

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, run, #verbose?

Constructor Details

#initialize(options) ⇒ Keywords

Returns a new instance of Keywords.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/imap_processor/keywords.rb', line 47

def initialize(options)
  super

  @add      = options[:Add]
  @delete   = options[:Delete]
  @keywords = options[:Keywords]
  @not      = options[:Not] ? 'NOT' : nil
  @list     = options[:List]

  if @add and @delete then
    raise OptionParser::InvalidOption, "--add and --delete are exclusive"
  elsif @keywords.nil? and (@add or @delete) then
    raise OptionParser::InvalidOption,
          "--add and --delete require --keywords"
  end

  connection = connect

  @imap = connection.imap
end

Class Method Details

.process_args(args) ⇒ Object



8
9
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
42
43
44
45
# File 'lib/imap_processor/keywords.rb', line 8

def self.process_args(args)
  required_options = {
    :List => true
  }

  super __FILE__, args, required_options do |opts, options|
    opts.banner << <<-EOF
imap_keywords lists keywords on an IMAP server and allows you to delete
previously set keywords.
    EOF

#      opts.on(      "--add",
#              "Add keyword(s) to all messages") do |add|
#        options[:Add] = add
#      end

    opts.on(      "--delete",
            "Delete keyword(s) from all messages") do |delete|
      options[:Delete] = delete
    end

    opts.on(      "--keywords=KEYWORDS", Array,
            "Select messages with keyword(s),",
            "which will be ANDed") do |keywords|
      options[:Keywords] = keywords
    end

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

    opts.on(      "--not",
            "Select messages without --keywords") do
      options[:Not] = true
    end
  end
end

Instance Method Details

#flags_to_literal(flags) ⇒ Object

Turns flags into a format usable by the IMAP server



71
72
73
74
75
76
77
78
# File 'lib/imap_processor/keywords.rb', line 71

def flags_to_literal(flags)
  flags.flatten.map do |flag|
    case flag
    when Symbol then "\\#{flag}"
    else flag
    end
  end
end

#make_search(keywords) ⇒ Object

Makes a SEARCH argument set from keywords



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/imap_processor/keywords.rb', line 83

def make_search(keywords)
  if keywords then
    keywords.map do |kw|
      case kw
      when '\Answered', '\Deleted', '\Draft', '\Flagged',
           '\Recent', '\Seen' then
        [@not, kw[1..-1].upcase]
      else
        [@not, 'KEYWORD', kw]
      end
    end.flatten.compact
  else
    %w[ALL]
  end
end

#runObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/imap_processor/keywords.rb', line 99

def run
  @boxes.each do |mailbox|
    response = @imap.select mailbox
    log "Selected mailbox #{mailbox}"
    puts "Previously set flags:"
    puts flags_to_literal(@imap.responses['FLAGS']).join(' ')
    puts

    puts "Permanent flags:"
    puts flags_to_literal(@imap.responses['PERMANENTFLAGS']).join(' ')
    puts

    search = make_search @keywords

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

    if uids.empty? then
      puts "No messages"
      next
    else
      puts "#{uids.length} messages in #{mailbox}#{@list ? ':' : ''}"
    end

    @imap.store uids, '-FLAGS.SILENT', @keywords if @delete

    show_messages uids
  end
end

#show_messages(uids) ⇒ Object

Displays messages in uids and their keywords



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/imap_processor/keywords.rb', line 132

def show_messages(uids)
  return unless @list

  responses = @imap.fetch uids, [
    Net::IMAP::RawData.new('BODY.PEEK[HEADER.FIELDS (SUBJECT MESSAGE-ID)]'),
    'FLAGS'
  ]

  responses.each do |res|
    header = res.attr['BODY[HEADER.FIELDS (SUBJECT MESSAGE-ID)]']

    puts header.chomp

    flags = res.attr['FLAGS'].map { |flag| flag.inspect }.join ', '

    puts "Flags: #{flags}"
    puts
  end
end