Class: ImapGuard::Guard
- Inherits:
-
Object
- Object
- ImapGuard::Guard
- Defined in:
- lib/imap_guard/guard.rb
Overview
Guard allows you to process your mailboxes.
Constant Summary collapse
- REQUIRED_SETTINGS =
List of required settings
%i[host port username password].freeze
- OPTIONAL_SETTINGS =
List of optional settings
%i[read_only verbose].freeze
Instance Attribute Summary collapse
-
#debug ⇒ Proc?
Matched emails are passed to this debug lambda if present.
-
#mailbox ⇒ String?
readonly
Currently selected mailbox.
-
#settings ⇒ OpenStruct
readonly
ImapGuard settings.
Instance Method Summary collapse
-
#close ⇒ void
Sends a CLOSE command to close the currently selected mailbox.
-
#delete(query, &filter) ⇒ void
Deletes messages matching the query and filter block.
-
#disconnect ⇒ void
Disconnects from the server.
-
#each(query) ⇒ void
Runs operation on messages matching the query.
-
#expunge ⇒ void
Sends a EXPUNGE command to permanently remove from the currently selected mailbox all messages that have the Deleted flag set.
-
#fetch_mail(message_id) ⇒ Mail
Fetches a message from its UID.
-
#initialize(settings) ⇒ Guard
constructor
A new instance of Guard.
-
#list ⇒ Array<String>
Sorted list of all mailboxes.
-
#login ⇒ void
Authenticates to the given IMAP server.
-
#move(query, mailbox, &filter) ⇒ void
Moves messages matching the query and filter block.
-
#select(mailbox) ⇒ void
Selects a mailbox (folder).
Constructor Details
#initialize(settings) ⇒ Guard
Returns a new instance of Guard.
30 31 32 |
# File 'lib/imap_guard/guard.rb', line 30 def initialize(settings) self.settings = settings end |
Instance Attribute Details
#debug ⇒ Proc?
Returns Matched emails are passed to this debug lambda if present.
21 22 23 |
# File 'lib/imap_guard/guard.rb', line 21 def debug @debug end |
#mailbox ⇒ String? (readonly)
Returns Currently selected mailbox.
28 29 30 |
# File 'lib/imap_guard/guard.rb', line 28 def mailbox @mailbox end |
#settings ⇒ OpenStruct
The settings are frozen
Returns ImapGuard settings.
25 26 27 |
# File 'lib/imap_guard/guard.rb', line 25 def settings @settings end |
Instance Method Details
#close ⇒ void
This method returns an undefined value.
Sends a CLOSE command to close the currently selected mailbox. The CLOSE command permanently removes from the mailbox all messages that have the Deleted flag set.
117 118 119 |
# File 'lib/imap_guard/guard.rb', line 117 def close @imap.close unless @settings.read_only end |
#delete(query, &filter) ⇒ void
This method returns an undefined value.
Deletes messages matching the query and filter block
75 76 77 78 79 80 81 82 |
# File 'lib/imap_guard/guard.rb', line 75 def delete(query, &filter) operation = lambda do || @imap.store(, "+FLAGS", [Net::IMAP::DELETED]) unless @settings.read_only "deleted".red end process query, operation, &filter end |
#disconnect ⇒ void
This method returns an undefined value.
Disconnects from the server.
123 124 125 |
# File 'lib/imap_guard/guard.rb', line 123 def disconnect @imap.disconnect end |
#each(query) ⇒ void
This method returns an undefined value.
Runs operation on messages matching the query
88 89 90 91 |
# File 'lib/imap_guard/guard.rb', line 88 def each(query) operation = ->() { yield } process query, operation end |
#expunge ⇒ void
This method returns an undefined value.
Sends a EXPUNGE command to permanently remove from the currently selected mailbox all messages that have the Deleted flag set.
109 110 111 |
# File 'lib/imap_guard/guard.rb', line 109 def expunge @imap.expunge unless @settings.read_only end |
#fetch_mail(message_id) ⇒ Mail
We use “BODY.PEEK[]” to avoid setting the Seen flag.
Fetches a message from its UID
96 97 98 99 |
# File 'lib/imap_guard/guard.rb', line 96 def fetch_mail() msg = @imap.fetch(, "BODY.PEEK[]").first.attr["BODY[]"] Mail.read_from_string msg end |
#list ⇒ Array<String>
Returns Sorted list of all mailboxes.
102 103 104 |
# File 'lib/imap_guard/guard.rb', line 102 def list @imap.list("", "*").map(&:name).sort end |
#login ⇒ void
This method returns an undefined value.
Authenticates to the given IMAP server
37 38 39 40 41 |
# File 'lib/imap_guard/guard.rb', line 37 def login @imap = Net::IMAP.new(@settings.host, @settings.port, true, nil, false) @imap.login(@settings.username, @settings.password) verbose.puts "Logged in successfully" end |
#move(query, mailbox, &filter) ⇒ void
This method returns an undefined value.
Moves messages matching the query and filter block
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/imap_guard/guard.rb', line 59 def move(query, mailbox, &filter) operation = lambda do || unless @settings.read_only @imap.copy(, mailbox) @imap.store(, "+FLAGS", [Net::IMAP::DELETED]) end "moved to #{mailbox}".yellow end process query, operation, &filter end |
#select(mailbox) ⇒ void
This method returns an undefined value.
Selects a mailbox (folder)
45 46 47 48 49 50 51 52 |
# File 'lib/imap_guard/guard.rb', line 45 def select(mailbox) if @settings.read_only @imap.examine(mailbox) # open in read-only else @imap.select(mailbox) # open in read-write end @mailbox = mailbox end |