Skip to content

Commit

Permalink
update directive on listener to include key
Browse files Browse the repository at this point in the history
  • Loading branch information
csucu committed Oct 11, 2023
1 parent df64270 commit d3f63e1
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type Listener interface {
CheckHost(ip net.IP, domain, sender string)
CheckHostResult(r Result, explanation string, extras *ResponseExtras, err error)
SPFRecord(s string)
Directive(unused bool, qualifier, mechanism, value, effectiveValue string)
Directive(unused bool, qualifier, mechanism, key, value, effectiveValue string)
NonMatch(qualifier, mechanism, value string, result Result, err error)
Match(qualifier, mechanism, value string, result Result, explanation string, extras *ResponseExtras, err error)
FirstMatch(r Result, err error)
Expand Down
4 changes: 2 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (p *parser) fireDirective(t *token, effectiveValue string) {
if p.listener == nil {
return
}
p.listener.Directive(false, t.qualifier.String(), t.mechanism.String(), t.value, effectiveValue)
p.listener.Directive(false, t.qualifier.String(), t.mechanism.String(), t.key, t.value, effectiveValue)
}

func (p *parser) fireMatchingIP(t *token, fqdn string, ipn net.IPNet, host string, ip net.IP) {
Expand All @@ -356,7 +356,7 @@ func (p *parser) fireUnusedDirective(t *token) {
if p.listener == nil || t == nil {
return
}
p.listener.Directive(true, t.qualifier.String(), t.mechanism.String(), t.value, "")
p.listener.Directive(true, t.qualifier.String(), t.mechanism.String(), t.key, t.value, "")
}

func (p *parser) fireNonMatch(t *token, r Result, e error) {
Expand Down
7 changes: 6 additions & 1 deletion printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (p *Printer) CheckHostResult(r spf.Result, explanation string, extras *spf.
fmt.Fprintf(p.w, "%s= %s, %v, %v, %v\n", strings.Repeat(" ", p.c), r, extras, explanation, err)
}

func (p *Printer) Directive(unused bool, qualifier, mechanism, value, effectiveValue string) {
func (p *Printer) Directive(unused bool, qualifier, mechanism, key, value, effectiveValue string) {
fmt.Fprintf(p.w, "%s", strings.Repeat(" ", p.c))
if qualifier == "+" {
qualifier = ""
Expand All @@ -61,6 +61,11 @@ func (p *Printer) Directive(unused bool, qualifier, mechanism, value, effectiveV
if mechanism == "v" {
delimiter = "="
}

if mechanism[0] == ':' {
fmt.Fprintf(p.w, " (%s)", key)
}

if value != "" {
fmt.Fprintf(p.w, "%s%s", delimiter, value)
}
Expand Down
11 changes: 9 additions & 2 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"strings"
)

// UnknownModifierMech constructed so we break policy if someone tries to create a policy out of mechanism string function instead of using actual key
const UnknownModifierMech = ":?"

type tokenType int

const (
Expand Down Expand Up @@ -83,9 +86,9 @@ func (tok tokenType) String() string {
case qTilde:
return "~"
case tUnknownModifier:
return "UNKNOWN_MODIFIER"
return UnknownModifierMech
default:
return strconv.Itoa(int(tok))
return ":" + strconv.Itoa(int(tok))
}
}

Expand Down Expand Up @@ -197,3 +200,7 @@ func (t *token) String() string {
}
return fmt.Sprintf("%s%s%s%s", q, k, d, t.value)
}

func IsKnownMechanism(s string) bool {
return tokenTypeFromString(s) != tErr
}

0 comments on commit d3f63e1

Please sign in to comment.