Class: IMAPProcessor::Flag

Inherits:
Client show all
Defined in:
lib/imap_processor/flag.rb

Overview

Automatically flag your messages, yo!

aka part two of my Plan for Total Email Domination.

IMAPFlag flags messages you’ve responded to, messages you’ve written and messages in response to messages you’ve written.

If you unflag a message IMAPFlag is smart and doesn’t re-flag it.

I chose these settings because I find these messages interesting but don’t want to manually flag them. Why should I do all the clicking when the computer can do it for me?

Constant Summary collapse

AUTO_FLAG_KEYWORD =

IMAP keyword for automatically flagged messages

'IMAPFLAG_AUTO_FLAGGED'
MESSAGE_ID =

Message-Id query

'HEADER.FIELDS (MESSAGE-ID)'

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 Client

#connect, #find_mailboxes, #mark, #search

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) ⇒ Flag

Creates a new IMAPFlag from options.

Options include:

+:Email:: Email address used for sending email

and all options from IMAPClient



37
38
39
40
41
42
# File 'lib/imap_processor/flag.rb', line 37

def initialize(options)
  super

  @flag = options[:flag]
  @boxes = @flag.keys
end

Class Method Details

.process_args(args) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/imap_processor/flag.rb', line 44

def self.process_args(args)
  super __FILE__, args, {} do |opts, options|
    opts.banner << <<-EOF
imap_flag automatically flags your messages.
    EOF
  end
end

Instance Method Details

#all_emailObject



81
82
83
# File 'lib/imap_processor/flag.rb', line 81

def all_email
  @email.map { |e| "FROM #{e}" }.inject { |s,e| "OR #{s} #{e}" }
end

#answered_in_currObject

Answered messages in the selected mailbox.



73
74
75
76
77
78
79
# File 'lib/imap_processor/flag.rb', line 73

def answered_in_curr
  search [
    'ANSWERED',
    'NOT', 'FLAGGED',
    'NOT', 'KEYWORD', AUTO_FLAG_KEYWORD
  ], 'answered messages'
end

#find_messagesObject

Searches for messages I answered and messages I wrote.



62
63
64
65
66
67
68
# File 'lib/imap_processor/flag.rb', line 62

def find_messages
  @box = @boxes.find { |box| @mailbox =~ /#{box}/ } # TODO: needs more work
  raise unless @box
  @email = @flag[@box]
  raise unless @email
  return [answered_in_curr, wrote_in_curr, responses_in_curr].flatten
end

#responses_in_currObject

Messages in response to messages I wrote in the selected mailbox.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/imap_processor/flag.rb', line 96

def responses_in_curr
  log "  Scanning for responses to messages I wrote"
  my_mail = @imap.search self.all_email

  return [] if my_mail.empty?

  msg_ids = @imap.fetch my_mail, "BODY.PEEK[#{MESSAGE_ID}]"
  msg_ids.map! do |data|
    data.attr["BODY[#{MESSAGE_ID}]"].split(':', 2).last.strip
  end

  messages = msg_ids.map do |id|
    @imap.search([
      'HEADER', 'In-Reply-To', id,
      'NOT', 'FLAGGED',
      'NOT', 'KEYWORD', AUTO_FLAG_KEYWORD
    ])
  end

  messages.flatten!

  log "    Found #{messages.length} messages"

  return messages
end

#runObject

Removes read, unflagged messages from all selected mailboxes…



55
56
57
# File 'lib/imap_processor/flag.rb', line 55

def run
  super "Flagging messages", [:Flagged, AUTO_FLAG_KEYWORD]
end

#wrote_in_currObject

Messages I wrote in the selected mailbox.



88
89
90
91
# File 'lib/imap_processor/flag.rb', line 88

def wrote_in_curr
  search("#{self.all_email} NOT FLAGGED NOT KEYWORD AUTO_FLAG_KEYWORD",
         "messages by #{@email.join(", ")}")
end