Class: ImapGuard::Guard

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

Instance Method Summary collapse

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

#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



18
19
20
# File 'lib/imap_guard/guard.rb', line 18

def debug
  @debug
end

#mailboxString? (readonly)

Returns Currently selected mailbox.

Returns:

  • (String, nil)

    Currently selected mailbox



25
26
27
# File 'lib/imap_guard/guard.rb', line 25

def mailbox
  @mailbox
end

#settingsOpenStruct

Note:

The settings are frozen

Returns ImapGuard settings.

Returns:

  • (OpenStruct)

    ImapGuard settings



22
23
24
# File 'lib/imap_guard/guard.rb', line 22

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.



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

Parameters:

  • query

    IMAP query

  • filter

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

    'deleted'.red
  }
  process query, operation, &filter
end

#disconnectvoid

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

Parameters:

  • query

    IMAP query

  • opration

    Lambda to call on each message



87
88
89
90
# File 'lib/imap_guard/guard.rb', line 87

def each query
  operation = lambda { |message_id| yield message_id }
  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.



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

Note:

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

Fetches a message from its UID

Returns:

  • (Mail)


95
96
97
98
# File 'lib/imap_guard/guard.rb', line 95

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



101
102
103
# File 'lib/imap_guard/guard.rb', line 101

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

#loginvoid

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



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