Class: ImapGuard::Guard

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#debugProc?

Returns Matched emails are passed to this debug lambda if present.

Returns:

  • (Proc, nil)

    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

#mailboxString? (readonly)

Returns Currently selected mailbox.

Returns:

  • (String, nil)

    Currently selected mailbox



30
31
32
# File 'lib/imap_guard/guard.rb', line 30

def mailbox
  @mailbox
end

#settingsStruct::Settings

Note:

The settings are frozen

Returns ImapGuard settings.

Returns:

  • (Struct::Settings)

    ImapGuard settings



27
28
29
# File 'lib/imap_guard/guard.rb', line 27

def settings
  @settings
end

Instance Method Details

#closevoid

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

Parameters:

  • query

    IMAP query

  • filter

    Optional 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 |message_id|
    @imap.store(message_id, "+FLAGS", [Net::IMAP::DELETED]) unless @settings.read_only

    "deleted".red
  end
  process query, operation, &filter
end

#disconnectvoid

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

Parameters:

  • query

    IMAP query

  • opration

    Lambda to call on each message



90
91
92
93
# File 'lib/imap_guard/guard.rb', line 90

def each(query)
  operation = ->(message_id) { yield message_id } # rubocop:disable Style/ExplicitBlockArgument
  process query, operation
end

#expungevoid

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

Note:

We use “BODY.PEEK[]” to avoid setting the Seen flag.

Fetches a message from its UID

Returns:

  • (Mail)


98
99
100
101
# File 'lib/imap_guard/guard.rb', line 98

def fetch_mail(message_id)
  msg = @imap.fetch(message_id, "BODY.PEEK[]").first.attr["BODY[]"]
  Mail.read_from_string msg
end

#listArray<String>

Returns Sorted list of all mailboxes.

Returns:

  • (Array<String>)

    Sorted list of all mailboxes



104
105
106
# File 'lib/imap_guard/guard.rb', line 104

def list
  @imap.list("", "*").map(&:name).sort
end

#loginvoid

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 
  @imap = Net::IMAP.new(@settings.host, port: @settings.port, ssl: true)
  @imap.(@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

Parameters:

  • query

    IMAP query

  • mailbox

    Destination mailbox

  • filter

    Optional 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 |message_id|
    unless @settings.read_only
      @imap.copy(message_id, mailbox)
      @imap.store(message_id, "+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