class IMAPProcessor::Keywords

Lists keywords present on a server

Public Class Methods

new(options) click to toggle source
Calls superclass method IMAPProcessor::new
# 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
process_args(args) click to toggle source
Calls superclass method IMAPProcessor::process_args
# 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

Public Instance Methods

flags_to_literal(flags) click to toggle source

Turns flags into a format usable by the IMAP server

# 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
run() click to toggle source
# 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) click to toggle source

Displays messages in uids and their keywords

# 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