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
[:host, :port, :username, :password]
- OPTIONAL_SETTINGS =
List of optional settings
[:read_only, :verbose]
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.
27 28 29 |
# File 'lib/imap_guard/guard.rb', line 27 def initialize settings self.settings = settings end |
Instance Attribute Details
#debug ⇒ Proc?
Returns Matched emails are passed to this debug lambda if present.
18 19 20 |
# File 'lib/imap_guard/guard.rb', line 18 def debug @debug end |
#mailbox ⇒ String? (readonly)
Returns Currently selected mailbox.
25 26 27 |
# File 'lib/imap_guard/guard.rb', line 25 def mailbox @mailbox end |
#settings ⇒ OpenStruct
The settings are frozen
Returns ImapGuard settings.
22 23 24 |
# File 'lib/imap_guard/guard.rb', line 22 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.
116 117 118 |
# File 'lib/imap_guard/guard.rb', line 116 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
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/imap_guard/guard.rb', line 72 def delete query, &filter operation = lambda { || unless @settings.read_only @imap.store(, "+FLAGS", [Net::IMAP::DELETED]) end 'deleted'.red } process query, operation, &filter end |
#disconnect ⇒ void
This method returns an undefined value.
Disconnects from the server.
122 123 124 |
# File 'lib/imap_guard/guard.rb', line 122 def disconnect @imap.disconnect end |
#each(query) ⇒ void
This method returns an undefined value.
Runs operation on messages matching the query
87 88 89 90 |
# File 'lib/imap_guard/guard.rb', line 87 def each query operation = lambda { || 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.
108 109 110 |
# File 'lib/imap_guard/guard.rb', line 108 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
95 96 97 98 |
# File 'lib/imap_guard/guard.rb', line 95 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.
101 102 103 |
# File 'lib/imap_guard/guard.rb', line 101 def list @imap.list("", "*").map(&:name).sort end |
#login ⇒ void
This method returns an undefined value.
Authenticates to the given IMAP server
34 35 36 37 38 |
# File 'lib/imap_guard/guard.rb', line 34 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
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/imap_guard/guard.rb', line 56 def move query, mailbox, &filter operation = lambda { || unless @settings.read_only @imap.copy(, mailbox) @imap.store(, "+FLAGS", [Net::IMAP::DELETED]) end "moved to #{mailbox}".yellow } process query, operation, &filter end |
#select(mailbox) ⇒ void
This method returns an undefined value.
Selects a mailbox (folder)
42 43 44 45 46 47 48 49 |
# File 'lib/imap_guard/guard.rb', line 42 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 |