Class: IMAPProcessor::Learn

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

Overview

IMAPLearn flags messages per-folder based on what you’ve flagged before.

aka part three of my Plan for Total Email Domination.

Constant Summary collapse

LEARN_KEYWORD =

IMAP keyword for learned messages

'IMAPLEARN_FLAGGED'
TASTY_KEYWORD =

IMAP keyword for tasty messages

LEARN_KEYWORD + '_TASTY'
BLAND_KEYWORD =

IMAP keyword for bland messages

LEARN_KEYWORD + '_BLAND'

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

Creates a new IMAPLearn from options.

Options include:

+:Threshold+:: Tastiness threshold for flagging

and all options from IMAPClient



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/imap_processor/learn.rb', line 63

def initialize(options)
  super

  @db_root = File.join '~', '.imap_learn',
                    "#{options[:User]}@#{options[:Host]}:#{options[:Port]}"
  @db_root = File.expand_path @db_root

  @threshold = options[:Threshold]

  @classifiers = Hash.new do |h,k|
    filter_db = File.join @db_root, "#{k}.db"
    FileUtils.mkdir_p File.dirname(filter_db)
    h[k] = RBayes.new filter_db
  end

  @unlearned_flagged = []
  @tasty_unflagged = []
  @bland_flagged = []
  @tasty_unlearned = []
  @bland_unlearned = []

  @noop = false
end

Class Method Details

.process_args(args) ⇒ Object

Handles processing of args.



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/imap_processor/learn.rb', line 42

def self.process_args(args)
  @@options[:Threshold] = [0.85, 'Tastiness threshold not set']

  super __FILE__, args, {} do |opts, options|
    opts.on("-t", "--threshold THRESHOLD",
            "Flag messages more tasty than THRESHOLD",
            "Default: #{options[:Threshold].inspect}",
            "Options file name: Threshold", Float) do |threshold|
      options[:Threshold] = threshold
    end
  end
end

Instance Method Details

#runObject

Flags tasty messages from all selected mailboxes.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/imap_processor/learn.rb', line 90

def run
  log "Flagging tasty messages"

  message_count = 0
  mailboxes = find_mailboxes

  mailboxes.each do |mailbox|
    @mailbox = mailbox
    @imap.select @mailbox
    log "Selected #{@mailbox}"

    message_count += process_unlearned_flagged
    message_count += process_tasty_unflagged
    message_count += process_bland_flagged
    message_count += process_unlearned
  end

  log "Done. Found #{message_count} messages in #{mailboxes.length} mailboxes"
end