Spf.Parser (Spfcheck v0.10.0) View Source

Functions to parse an SPF string or explain string for a given SPF context.

The parser also performs expansion during the semantic checks, so both functions take an SPF context as their only argument.

Link to this section Summary

Functions

Parse context's explain string and store the result under the :explanation key.

Parse context's SPF string and store the result under the :ast key.

Link to this section Types

Link to this section Functions

Specs

explain(Spf.Context.t()) :: Spf.Context.t()

Parse context's explain string and store the result under the :explanation key.

In case of any syntax errors, sets the explanation string to an empty string.

Example

iex> Spf.Context.new("example.com", ip: "1.2.3.4")
...> |> Map.put(:explain_string, "%{i} is bad")
...> |> Spf.Parser.explain()
...> |> Map.get(:explanation)
"1.2.3.4 is bad"

Specs

Parse context's SPF string and store the result under the :ast key.

The parser will parse the entire record so as to find as many problems as possible.

The parser will log notifications for:

  • ignoring an include'd explain modifier
  • for each DNS mechanism encountered (at :debug level)

The parser will log warnings for:

  • an SPF string length longer than 512 characters
  • when an exp modifier is present, but the SPF record cannot fail
  • SPF records with implicit endings
  • ignoring a redirect modifier because the all mechanism is present
  • each ignored term occurring after an all mechanism
  • the use of the ptr mechanism (which is not recommended)
  • the use of the p-macro (also not recommended)
  • repeated whitespace to separate terms
  • use of tab character(s) to sepearate terms
  • ignoring an unknown modifier
  • a redirect modifer, when no all is present, that is not the last term

The parser will log an error for:

  • repeated modifier terms
  • syntax errors in domain specifications
  • syntax errors in dual-cidr notations
  • invalid IP addresses (including invalid masks, if any)
  • unknown terms that are not unknown modifiers

The logging simply adds messages to the context.msg list but, when logging an error, the context.error and context.reason are also set.

Since the parser does not stop at the first error, the context.error and context.reason show the details of the last error seen. If given context also has a function reference stored in context.log, it is called with 4 arguments:

  • context
  • facility, an atom denoting which part sent the message
  • severity, an atom like :info, :warn, :error, :debug
  • msg, the message string

In the absence of an error, context.ast is fit for evaluation.

Example

iex> Spf.Context.new("example.com")
...> |> Map.put(:spf, "v=spf1 -all")
...> |> Spf.Parser.parse()
...> |> Map.get(:ast)
[{:all, '-', 7..10}]