module Roda::RodaPlugins::MailProcessor::RequestMethods

def _match_address(field, val, addresses)

(normal regexp match).
strings (case insensitive match of any string), or regexp
can be a string (case insensitive match of the string), array of
Match if any of the given addresses match the given val, which
def _match_address(field, val, addresses)
  case val
  when String
    addresses.any?{|a| address_match?(a, val)}
  when Array
    overlap = []
    addresses.each do |a|
      val.each do |v|
        if address_match?(a, v)
          overlap << a 
        end
      end
    end
    unless overlap.empty?
      @captures.concat(overlap)
    end
  when Regexp
    matched = false
    addresses.each do |v|
      if md = val.match(v)
        matched = true
        @captures.concat(md.captures)
      end
    end
    matched
  else
    unsupported_matcher(:field=>val)
  end
end

def _match_content(field, val, content)

(normal regexp match).
strings (case sensitive substring match of any string), or regexp
can be a string (case sensitive substring match), array of
Match if the content matches the given val, which
def _match_content(field, val, content)
  case val
  when String
    content.include?(val)
  when Array
    val.each do |v|
      if content.include?(v)
        return @captures << v
      end
    end
    false
  when Regexp
    if md = val.match(content)
      @captures.concat(md.captures)
    end
  else
    unsupported_matcher(field=>val)
  end
end

def address_match?(a1, a2)

Whether the addresses are the same (case insensitive match).
def address_match?(a1, a2)
  a1.casecmp?(a2)
end

def address_match?(a1, a2)

:nocov:
def address_match?(a1, a2)
  a1.downcase == a2.downcase
end

def block_result_body(_)

mark it as unhandled.
If the routing did not explicitly mark the mail as handled
def block_result_body(_)
  unless env['roda.mail_handled']
    scope.unhandled_mail('mail was not handled during mail_processor routing')
  end
end

def handle(&block)

unhandled_mail implicitly.
Mark the mail as having been handled, so routing will not call
def handle(&block)
  env['roda.mail_handled'] = true
  always(&block)
end

def handle_header(key, value=nil)

Same as +header+, but also mark the message as being handled.
def handle_header(key, value=nil)
  header(key, value) do |*args|
    handle do
      yield(*args)
    end
  end
end

def header(key, value=nil, &block)

Match based on a mail header value.
def header(key, value=nil, &block)
  on(:header=>[key, value], &block)
end

def mail

The mail instance being processed.
def mail
  env['roda.mail']
end

def match_body(val)

Match the value against the full mail body.
def match_body(val)
  _match_content(:body, val, mail.body.decoded)
end

def match_header((key, value))

value (which may be nil).
Match against a header specified by key with the given
def match_header((key, value))
  return unless content = mail.header[key]
  if value.nil?
    @captures << content.decoded
  else
    _match_content(:header, value, content.decoded)
  end
end

def match_rcpt(address)

Match the given address against all recipients in the mail.
def match_rcpt(address)
  _match_address(:rcpt, address, scope.mail_recipients)
end

def match_subject(val)

Match the value against the mail subject.
def match_subject(val)
  _match_content(:subject, val, mail.subject)
end

def match_text(val)

Match the value against the extracted mail text.
def match_text(val)
  _match_content(:text, val, scope.mail_text)
end