haraka/haraka-plugin-dkim

View on GitHub
bin/dkimverify

Summary

Maintainability
Test Coverage
#!/usr/bin/env node

// DKIM test tool

const nopt = require('nopt')
const path = require('path')
const base_path = path.join(__dirname, '..')
const dkim = require(`${base_path}/lib/dkim`)
const DKIMVerifyStream = dkim.DKIMVerifyStream

const parsed = nopt({ debug: Boolean, time_skew: Number, help: Boolean })

function print_usage() {
  console.log('Usage: dkimverify [--debug] [--time_skew=123] < message')
  process.exit(1)
}

if (parsed.help) print_usage()

if (!parsed.debug) {
  dkim.DKIMObject.prototype.debug = function (str) {}
  DKIMVerifyStream.prototype.debug = function (str) {}
}

const opts = {}
if (parsed.time_skew) opts.allowed_time_skew = parsed.time_skew

const verifier = new DKIMVerifyStream(opts, (err, result, results) => {
  if (err) console.log(err.message)
  if (Array.isArray(results)) {
    results.forEach(function (res) {
      console.log(
        `identity="${res.identity}" domain="${res.domain}" result=${res.result} ${res.error ? `(${res.error})` : ''}`,
      )
    })
  } else {
    console.log(`Result: ${result}`)
  }
})

process.stdin.pipe(verifier)