nsmcp::SmtpLogTool method parse (protected)

 <instance of nsmcp::SmtpLogTool[i]> parse kind line

Defined in /usr/local/ns/tcl/nsmcp/smtp-tools.tcl

Parameters:
kind (required)
line (required)

Testcases:
No testcase defined.
Source code:
if {![regexp {^\[([^]]+)\] (\S+) (.*)$} $line -> timestamp thread rest]} {error "Invalid SMTP timestamp"}
set at [clock scan $timestamp -format {%d/%b/%Y:%H:%M:%S %z}]
set record [dict create timestamp $timestamp timestampSeconds $at thread $thread]
if {$kind eq "send"} {
    set source unknown
    set separator [string last "\t" $rest]
    if {$separator >= 0} {
        set attribute [string range $rest $separator+1 end]
        if {![regexp {^source=(send|relay)$} $attribute -> source]} {
            error "Invalid SMTP send source"
        }
        set rest [string range $rest 0 $separator-1]
    }
    if {![regexp {^(\S+) (\S+) \[([^]]+)\]:([0-9]+) ([0-9]+(?:\.[0-9]+)?) ([0-9]+) (.*?) RCPT: (.*)$} $rest  -> status result host port elapsed bytes sender recipients]} {error "Invalid SMTP send record"}
    return [dict merge $record [dict create status $status result $result destinationHost $host  destinationPort $port elapsedSeconds $elapsed sentBytes $bytes sender $sender recipients $recipients source $source]]
}
set fields [split $rest " "]
lassign $fields code event peer session transaction server sender recipient action reason targets
if {[llength $fields] < 11 || ![regexp {^(-|[1-5][0-9]{2})$} $code]
    || ![regexp {^[a-z0-9_-]+$} $event] || ![string is integer -strict $transaction]
    || [string index $peer 0] ne "\[" || [string index $peer end] ne "\]"} {error "Invalid SMTP event record"}
foreach field {code sender recipient action reason targets} {
    if {[set $field] eq "-"} {set $field ""}
}
foreach field {server sender recipient action reason session} {
    set $field [:decode [set $field]]
}
set metadata {}
set omitted 0
foreach field [lrange $fields 11 end] {
    if {[regexp {^([a-z0-9-]+)=(.*)$} $field -> key value]} {
        # Expose diagnostic fields, not arbitrary custom event payloads.
        if {$key in {last-command message-bytes relay-accepted relay-reply recipients findings policy-rule
            greylist-early-retries greylist-elapsed-seconds spf-fail dkim-no-key
            link-status link-limit link-host-mismatch link-embedded-redirect link-destination-hosts link-destination-limit
            dkim-obsolete-algorithm spf-result spf-peer spf-helo spf-errorcode dkim-signatures dkim-status dkim-limit}
            || [regexp {^dkim-[1-8]-(domain|selector|key|verification|algorithm|algorithm-status)$} $key]} {
            dict set metadata $key [:decode $value]
        } else {
            incr omitted
        }
    }
}
return [dict merge $record [dict create status $code event $event peerAddress [string range $peer 1 end-1]  session $session transaction $transaction recordedServer $server sender $sender recipient $recipient  action $action reason $reason targets [expr {$targets eq "" ? {} : [split $targets ,]}] metadata $metadata metadataFieldsOmitted $omitted]]
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: