class SanitizeEmail::OverriddenAddresses

Tools for overriding addresses

Constants

REPLACE_ALLIGATOR
REPLACE_AT

Attributes

bad_list[RW]
good_list[RW]
overridden_bcc[RW]
overridden_cc[RW]
overridden_to[RW]
sanitized_bcc[RW]
sanitized_cc[RW]
sanitized_to[RW]

Public Class Methods

new(message, **args) click to toggle source
# File lib/sanitize_email/overridden_addresses.rb, line 24
def initialize(message, **args)
  # Not using extract_options! because non-rails compatibility is a goal
  args = SanitizeEmail::Config.to_init.merge(args)
  @sanitized_to = args[:sanitized_to]
  @sanitized_cc = args[:sanitized_cc]
  @sanitized_bcc = args[:sanitized_bcc]
  @good_list = args[:good_list] || []
  @bad_list = args[:bad_list] || []
  @overridden_to = to_override(message.to)
  @overridden_cc = cc_override(message.cc)
  @overridden_bcc = bcc_override(message.bcc)
end

Public Instance Methods

address_list_filter(list_type, address) click to toggle source
# File lib/sanitize_email/overridden_addresses.rb, line 94
def address_list_filter(list_type, address)
  # TODO: How does this handle email addresses with user names like "Foo Example <foo@example.org>"
  has_address = send(list_type).include?(address)
  case list_type
  when :good_list then
    has_address ? address : nil
  when :bad_list then
    has_address ? nil : address
  end
end
bcc_override(actual_addresses) click to toggle source
# File lib/sanitize_email/overridden_addresses.rb, line 54
def bcc_override(actual_addresses)
  override_email(:bcc, actual_addresses).join(',')
end
cc_override(actual_addresses) click to toggle source
# File lib/sanitize_email/overridden_addresses.rb, line 50
def cc_override(actual_addresses)
  override_email(:cc, actual_addresses).join(',')
end
clean_addresses(addresses, list_type) click to toggle source
# File lib/sanitize_email/overridden_addresses.rb, line 117
def clean_addresses(addresses, list_type)
  # Normalize addresses just in case it isn't an array yet
  addresses.map do |address|
    # If this address is on the good list then let it pass
    address_list_filter(list_type, address)
  end.compact.uniq
end
good_listize(real_addresses) click to toggle source

Allow good listed email addresses, and then remove the bad listed addresses

# File lib/sanitize_email/overridden_addresses.rb, line 38
def good_listize(real_addresses)
  good_listed = clean_addresses(real_addresses, :good_list)
  good_listed = clean_addresses(good_listed, :bad_list) unless good_listed.empty?
  good_listed
end
inject_user_names(real_addresses, sanitized_addresses) click to toggle source
# File lib/sanitize_email/overridden_addresses.rb, line 105
def inject_user_names(real_addresses, sanitized_addresses)
  real_addresses.each_with_object([]) do |real_recipient, result|
    new_recipient = if real_recipient.nil?
                      sanitized_addresses
                    else
                      # puts "SANITIZED: #{sanitized_addresses}"
                      sanitized_addresses.map { |sanitized| "#{real_recipient.gsub(REPLACE_AT[0], REPLACE_AT[1]).gsub(/[<>]/, '~')} <#{sanitized}>" }
                    end
    result << new_recipient
  end.flatten
end
override_email(type, actual_addresses) click to toggle source
# File lib/sanitize_email/overridden_addresses.rb, line 58
def override_email(type, actual_addresses)
  # Normalized to an arrays
  # puts "override_email 1: #{type} - #{actual_addresses}"
  real_addresses = Array(actual_addresses)

  # puts "override_email 2: #{type} - #{real_addresses}"
  # If there were no original recipients, then we DO NOT override the nil with the sanitized recipients
  return [] if real_addresses.empty?

  good_listed = good_listize(real_addresses)
  # puts "override_email 3: #{type} - #{good_listed}"
  # If there are good_list addresses to send to then use them as is, no mods needed
  return good_listed unless good_listed.empty?

  # TODO: Allow overriding if an addressed email is on the good list?
  # If there are no sanitized addresses we can't override!
  sanitized_addresses = sanitize_addresses(type)
  # puts "override_email 4: #{type} - #{sanitized_addresses}"
  return [] if sanitized_addresses.empty?

  # At this point it is assured that the address list will need to be sanitized
  # One more check to ensure none of the configured sanitized email addresses are on the bad_list
  sanitized_addresses = clean_addresses(sanitized_addresses, :bad_list)
  # puts "override_email 5: #{type} - #{sanitized_addresses}"

  # If we don't want to inject the "email" in the "user name" section of the sanitized recipients,
  # then just return the default sanitized recipients
  return sanitized_addresses unless SanitizeEmail.use_actual_email_as_sanitized_user_name

  with_user_names = inject_user_names(real_addresses, sanitized_addresses)
  # puts "real_addresses 2: #{real_addresses}"
  # puts "override_email 6: #{type} - #{with_user_names}"
  # Otherwise inject the email as the "user name"
  with_user_names
end
sanitize_addresses(type) click to toggle source
# File lib/sanitize_email/overridden_addresses.rb, line 125
def sanitize_addresses(type)
  case type
  when :to then
    Array(sanitized_to)
  when :cc then
    Array(sanitized_cc)
  when :bcc then
    Array(sanitized_bcc)
  else
    raise UnknownOverride, 'unknown email override'
  end
end
to_override(actual_addresses) click to toggle source
# File lib/sanitize_email/overridden_addresses.rb, line 44
def to_override(actual_addresses)
  to = override_email(:to, actual_addresses)
  raise MissingTo, "after overriding :to (#{actual_addresses}) there are no addresses to send in To: header." if to.empty?
  to.join(',')
end