#!/bin/bash
#Nifty: Find all trigger IP's for mails in mail queue for a specific domain

# Source niftylib
curl -s https://nifty.sh/lib/universal/niftylib.sh > /tmp/lib.nifty.sh
. /tmp/lib.nifty.sh
rm -f /tmp/lib.nifty.sh

# Requirements
require_root

if [ -z "$1" ]; then
  err "Usage: bash find_spammer_ips.sh <DOMAIN>"
  exit 1
fi

MAILQ_ID="$(mailq | awk -v domain="$1\$" "\$7 ~ domain {print \$1}" | tr -d '*')"

if [ -z "$MAILQ_ID" ]; then
  ce "No REMOTE_ADDR to extract!"
  exit
fi

IP_ADDRESSES=""
while read -r line; do
    IP_ADDRESSES="$IP_ADDRESSES$(postcat -q "$line" | awk '/REMOTE_ADDR/ {print $4}' | sort -u)\n"
done <<< "$MAILQ_ID"

echo -e "$IP_ADDRESSES" | sed -E '/$^/d' | sort -u > mailqip.txt

ce "Done! Check the file 'mailqip.txt' for the list of IP's"