Class: ImapGuard::Guard
- Inherits:
-
Object
- Object
- ImapGuard::Guard
- Defined in:
- lib/imap_guard/guard.rb
Overview
Guard allows you to process your mailboxes.
Defined Under Namespace
Classes: Settings
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 ⇒ Struct::Settings
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.
32 33 34 |
# File 'lib/imap_guard/guard.rb', line 32 def initialize(settings) self.settings = settings end |
Instance Attribute Details
#debug ⇒ Proc?
Returns Matched emails are passed to this debug lambda if present.
23 24 25 |
# File 'lib/imap_guard/guard.rb', line 23 def debug @debug end |
#mailbox ⇒ String? (readonly)
Returns Currently selected mailbox.
30 31 32 |
# File 'lib/imap_guard/guard.rb', line 30 def mailbox @mailbox end |
#settings ⇒ Struct::Settings
The settings are frozen
Returns ImapGuard settings.
27 28 29 |
# File 'lib/imap_guard/guard.rb', line 27 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.
119 120 121 |
# File 'lib/imap_guard/guard.rb', line 119 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
77 78 79 80 81 82 83 84 |
# File 'lib/imap_guard/guard.rb', line 77 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.
125 126 127 |
# File 'lib/imap_guard/guard.rb', line 125 def disconnect @imap.disconnect end |
#each(query) ⇒ void
This method returns an undefined value.
Runs operation on messages matching the query
90 91 92 93 |
# File 'lib/imap_guard/guard.rb', line 90 def each(query) operation = ->() { yield } # rubocop:disable Style/ExplicitBlockArgument 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.
111 112 113 |
# File 'lib/imap_guard/guard.rb', line 111 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
98 99 100 101 |
# File 'lib/imap_guard/guard.rb', line 98 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.
104 105 106 |
# File 'lib/imap_guard/guard.rb', line 104 def list @imap.list("", "*").map(&:name).sort end |
#login ⇒ void
This method returns an undefined value.
Authenticates to the given IMAP server
39 40 41 42 43 |
# File 'lib/imap_guard/guard.rb', line 39 def login @imap = Net::IMAP.new(@settings.host, port: @settings.port, ssl: true) @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
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/imap_guard/guard.rb', line 61 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)
47 48 49 50 51 52 53 54 |
# File 'lib/imap_guard/guard.rb', line 47 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 |