module EmailAddress::Rewriter

Constants

PRVS_REGEX
SRS_FORMAT_REGEX

Public Instance Methods

batv_prvs(options = {}) click to toggle source
# File lib/email_address/rewriter.rb, line 86
def batv_prvs(options = {})
  k = options[:prvs_key_id] || "0"
  prvs_days = options[:prvs_days] || @config[:prvs_days] || 30
  ddd = prvs_day(prvs_days)
  ssssss = prvs_sign(k, ddd, to_s, options)
  ["prvs=", k, ddd, ssssss, "=", to_s].join("")
end
parse_prvs(email, options = {}) click to toggle source
# File lib/email_address/rewriter.rb, line 96
def parse_prvs(email, options = {})
  if email.match(PRVS_REGEX)
    @rewrite_scheme = :prvs
    k, ddd, ssssss, email = [$1, $2, $3, $4]

    unless ssssss == prvs_sign(k, ddd, email, options)
      @rewrite_error = "Invalid BATV Address: Signature unverified"
    end
    exp = ddd.to_i
    roll = 1000 - exp # rolling 1000 day window
    today = prvs_day(0)
    # I'm sure this is wrong
    if exp > today && exp < roll
      @rewrite_error = "Invalid SRS Email Address: Address expired"
    elsif exp < today && (today - exp) > 0
      @rewrite_error = "Invalid SRS Email Address: Address expired"
    end
    [local, domain].join("@")
  else
    email
  end
end
parse_rewritten(e) click to toggle source
# File lib/email_address/rewriter.rb, line 8
def parse_rewritten(e)
  @rewrite_scheme = nil
  @rewrite_error = nil
  parse_srs(e)
  # e = parse_batv(e)
end
parse_srs(email, options = {}, &block) click to toggle source
# File lib/email_address/rewriter.rb, line 37
def parse_srs(email, options = {}, &block)
  if email&.match(SRS_FORMAT_REGEX)
    @rewrite_scheme = :srs
    hhh, tt, domain, local, sending_domain = [$1, $2, $3, $4, $5]
    # hhh = tt = sending_domain if false && hhh # Hide warnings for now :-)
    a = [tt, domain, local].join("=") + "@" + sending_domain
    unless srs_hash(a, options, &block) === hhh
      @rewrite_error = "Invalid SRS Email Address: Possibly altered"
    end
    unless tt == srs_tt
      @rewrite_error = "Invalid SRS Email Address: Too old"
    end
    [local, domain].join("@")
  else
    email
  end
end
prvs_day(days) click to toggle source
# File lib/email_address/rewriter.rb, line 119
def prvs_day(days)
  ((Time.now.to_i + (days * 24 * 60 * 60)) / (24 * 60 * 60)).to_s[-3, 3]
end
prvs_sign(k, ddd, email, options = {}) click to toggle source
# File lib/email_address/rewriter.rb, line 123
def prvs_sign(k, ddd, email, options = {})
  str = [ddd, ssssss, "=", to_s].join("")
  key = options["key_#{k}".to_i] || @config["key_#{k}".to_i] || str.reverse
  Digest::SHA1.hexdigest([k, ddd, email, key].join(""))[0, 6]
end
srs(sending_domain, options = {}, &block) click to toggle source
# File lib/email_address/rewriter.rb, line 25
def srs(sending_domain, options = {}, &block)
  tt = srs_tt
  a = [tt, hostname, local.to_s].join("=") + "@" + sending_domain
  hhh = srs_hash(a, options, &block)

  ["SRS0", hhh, a].join("=")
end
srs?(email) click to toggle source
# File lib/email_address/rewriter.rb, line 33
def srs?(email)
  email.match(SRS_FORMAT_REGEX) ? true : false
end
srs_hash(email, options = {}, &block) click to toggle source
# File lib/email_address/rewriter.rb, line 63
def srs_hash(email, options = {}, &block)
  key = options[:key] || @config[:key] || email.reverse
  if block
    block.call(email)[0, 4]
  else
    Base64.encode64(Digest::SHA1.digest(email + key))[0, 4]
  end
end
srs_tt(t = Time.now.utc) click to toggle source

SRS Timeout Token Returns a 2-character code for the day. After a few days the code will roll. TT has a one-day resolution in order to make the address invalid after a few days. The cycle period is 3.5 years. Used to control late bounces and harvesting.

# File lib/email_address/rewriter.rb, line 59
def srs_tt(t = Time.now.utc)
  Base64.encode64((t.to_i / (60 * 60 * 24) % 210).to_s)[0, 2]
end
verp(recipient, split_char = "+") click to toggle source
# File lib/email_address/rewriter.rb, line 137
def verp(recipient, split_char = "+")
  local.to_s +
    split_char + recipient.tr("@", "=") +
    "@" + hostname
end