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
IMAP_CONFIGURATION =

29 minutes in in seconds

[:name, :email, :password, :host, :port].freeze
MICROSOFT_GRAPH_CONFIGURATION =
[:name, :email].freeze
MICROSOFT_GRAPH_INBOX_OPTIONS =
[:tenant_id, :client_id, :client_secret].freeze
DEFAULTS =

Default attributes for the mailbox configuration

{
  search_command: 'UNSEEN',
  delivery_method: 'postback',
  host: 'imap.gmail.com',
  port: 993,
  ssl: true,
  start_tls: false,
  limit_max_unread: 0,
  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


72
73
74
75
76
# File 'lib/mail_room/mailbox.rb', line 72

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

  validate!
end

Instance Method Details

#arbitration_klassObject


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

def arbitration_klass
  Arbitration[arbitration_method]
end

#arbitratorObject


101
102
103
# File 'lib/mail_room/mailbox.rb', line 101

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

#contextObject


126
127
128
# File 'lib/mail_room/mailbox.rb', line 126

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

#deliver(message) ⇒ Object

deliver the email message

Parameters:


113
114
115
116
117
118
119
# File 'lib/mail_room/mailbox.rb', line 113

def deliver(message)
  body = message.body
  return true unless body

  logger.info({context: context, uid: message.uid, action: "sending to deliverer", deliverer: delivery.class.name, byte_size: body.bytesize})
  delivery.deliver(body)
end

#deliver?(uid) ⇒ Boolean

Returns:

  • (Boolean)

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

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

  arbitrator.deliver?(uid)
end

#deliveryObject


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

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

#delivery_klassObject


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

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

#imap?Boolean

Returns:

  • (Boolean)

130
131
132
# File 'lib/mail_room/mailbox.rb', line 130

def imap?
  !microsoft_graph?
end

#loggerObject


78
79
80
81
82
83
84
85
86
87
# File 'lib/mail_room/mailbox.rb', line 78

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

#microsoft_graph?Boolean

Returns:

  • (Boolean)

134
135
136
# File 'lib/mail_room/mailbox.rb', line 134

def microsoft_graph?
  self[:inbox_method].to_s == 'microsoft_graph'
end

#ssl_optionsObject

true, false, or ssl options hash


122
123
124
# File 'lib/mail_room/mailbox.rb', line 122

def ssl_options
  replace_verify_mode(ssl)
end

#validate!Object


138
139
140
141
142
143
144
# File 'lib/mail_room/mailbox.rb', line 138

def validate!
  if microsoft_graph?
    validate_microsoft_graph!
  else
    validate_imap!
  end
end