#!/bin/sh showHelp() { cat < Allow IP to send email for the domain. Can be used several times. -i, --include Any domains that may deliver or relay mail for the domain. Can be used several times. -a Add any other server hostname that may deliver or relay mail for the domain. If domain is empty, it allow current IP address of the domain to send email for the domain. Can be used several times. -p, --policy Default value: @ -s, --short Only show the value EOF } MX=0 IPv4s= IPv6s= INCLUDEs= POLICY='?all' DOMAIN=@ SHORT=0 As= testData() { if [ -z "$1" ]; then ( echo "Invalid parameter ($2)\n" showHelp ) >&2 exit 1 fi } addIp() { if [ -n "$1" ]; then if [ $(echo "$1" | egrep "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+") ]; then IPv4s="$IPv4s ip4:$1" else IPv6s="$IPv6s ip6:$1" fi fi } addA() { if [ -z "$1" ]; then As="$As a" else As="$As a:$1" fi } addInclude() { if [ -n "$1" ]; then INCLUDEs="$INCLUDEs include:$1" fi } for i in $@; do case $1 in -h|--help) showHelp exit 0 ;; -mx) MX=1 shift ;; -s|--short) SHORT=1 shift ;; -a) shift addA $1 shift ;; -ip) shift addIp $1 shift ;; -i|--include) shift addInclude $1 shift ;; -p|--policy) shift case $1 in neutral) POLICY='?all';; soft) POLICY='~all';; fail) POLICY='-all';; esac shift ;; -d|--domain) shift DOMAIN=$1 shift ;; esac done testData "$DOMAIN" domain testData "$IPv4s$IPv6s" ip ( if [ $MX -eq 1 ]; then MX=mx fi if [ $SHORT -eq 0 ]; then echo "$DOMAIN IN TXT \"v=spf1 $MX $As $IPv4s $IPv6s $INCLUDEs $POLICY\"" else echo "v=spf1 $MX $As $IPv4s $IPv6s $INCLUDEs $POLICY" fi ) | sed 's/ */ /g'