Class: MailRoom::Mailbox

Inherits:
Struct
  • Object
show all
Defined in:
lib/mail_room/mailbox.rb

Overview

Holds configuration for each of the email accounts we wish to monitor

and deliver email to when new emails arrive over imap

Constant Summary collapse

IMAP_IDLE_TIMEOUT =

Keep it to 29 minutes or less The IMAP serve will close the connection after 30 minutes of inactivity (which sending IDLE and then nothing technically is), so we re-idle every 29 minutes, as suggested by the spec: tools.ietf.org/html/rfc2177

29 * 60
REQUIRED_CONFIGURATION =

29 minutes in in seconds

[:name, :email, :password, :host, :port]
DEFAULTS =

Default attributes for the mailbox configuration

{
  :search_command => 'UNSEEN',
  :delivery_method => 'postback',
  :host => 'imap.gmail.com',
  :port => 993,
  :ssl => true,
  :start_tls => false,
  :idle_timeout => IMAP_IDLE_TIMEOUT,
  :delete_after_delivery => false,
  :expunge_deleted => false,
  :delivery_options => {},
  :arbitration_method => 'noop',
  :arbitration_options => {},
  :logger => {}
}

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Mailbox

Store the configuration and require the appropriate delivery method

Parameters:

  • attributes (Hash) (defaults to: {})

    configuration options


63
64
65
66
67
# File 'lib/mail_room/mailbox.rb', line 63

def initialize(attributes={})
  super(*DEFAULTS.merge(attributes).values_at(*members))

  validate!
end

Instance Method Details

#arbitration_klassObject


84
85
86
# File 'lib/mail_room/mailbox.rb', line 84

def arbitration_klass
  Arbitration[arbitration_method]
end

#arbitratorObject


92
93
94
# File 'lib/mail_room/mailbox.rb', line 92

def arbitrator
  @arbitrator ||= arbitration_klass.new(parsed_arbitration_options)
end

#contextObject


117
118
119
# File 'lib/mail_room/mailbox.rb', line 117

def context
  { email: self.email, name: self.name }
end

#deliver(message) ⇒ Object

deliver the imap email message

Parameters:

  • message (Net::IMAP::FetchData)

104
105
106
107
108
109
110
# File 'lib/mail_room/mailbox.rb', line 104

def deliver(message)
  body = message.attr['RFC822']
  return true unless body

  logger.info({context: context, uid: message.attr['UID'], action: "sending to deliverer", deliverer: delivery.class.name, byte_size: message.attr['RFC822.SIZE']})
  delivery.deliver(body)
end

#deliver?(uid) ⇒ Boolean

Returns:

  • (Boolean)

96
97
98
99
100
# File 'lib/mail_room/mailbox.rb', line 96

def deliver?(uid)
  logger.info({context: context, uid: uid, action: "asking arbiter to deliver", arbitrator: arbitrator.class.name})

  arbitrator.deliver?(uid)
end

#deliveryObject


88
89
90
# File 'lib/mail_room/mailbox.rb', line 88

def delivery
  @delivery ||= delivery_klass.new(parsed_delivery_options)
end

#delivery_klassObject


80
81
82
# File 'lib/mail_room/mailbox.rb', line 80

def delivery_klass
  self[:delivery_klass] ||= Delivery[delivery_method]
end

#loggerObject


69
70
71
72
73
74
75
76
77
78
# File 'lib/mail_room/mailbox.rb', line 69

def logger
  @logger ||=
    case self[:logger]
      when Logger
        self[:logger]
      else
        self[:logger] ||= {}
        MailRoom::Logger::Structured.new(self[:logger][:log_path])
    end
end

#ssl_optionsObject

true, false, or ssl options hash


113
114
115
# File 'lib/mail_room/mailbox.rb', line 113

def ssl_options
  replace_verify_mode(ssl)
end

#validate!Object


121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/mail_room/mailbox.rb', line 121

def validate!
  if self[:idle_timeout] > IMAP_IDLE_TIMEOUT
    raise IdleTimeoutTooLarge,
          "Please use an idle timeout smaller than #{29*60} to prevent " \
          "IMAP server disconnects"
  end

  REQUIRED_CONFIGURATION.each do |k|
    if self[k].nil?
      raise ConfigurationError,
            "Field :#{k} is required in Mailbox: #{inspect}"
    end
  end
end