Zionetrix::Git
Repositories
Help
Report an Issue
postfix_scripts
Code
Commits
Branches
Tags
Search
Tree:
774e0ce
Branches
Tags
master
postfix_scripts
scripts
mailid_to_username
Initial commit
Benjamin Renard
commited
774e0ce
at 2016-01-19 19:51:30
mailid_to_username
Blame
History
Raw
#!/bin/bash MAILID=$1 [ -z "$MAILID" ] && echo "Usage : $0 [mailid]" && exit 1 DEBUG=0 [ "$2" == "-d" ] && DEBUG=1 MAX_LOG=9 HOSTNAME=$( hostname -f ) function search_user () { MAILID=$1 MAILLOG=$2 [ ! -f "$MAILLOG" ] && MAILLOG=$MAILLOG.gz [ -f "$MAILLOG" ] && zgrep $MAILID $MAILLOG|grep 'sasl_username='|sed 's/^.*sasl_username=//' } function search_user_from_mailid() { MAILID=$1 [ $DEBUG -eq 1 ] && echo "Search username from Mail ID $MAILID ..." res=$( search_user $MAILID /var/log/mail.log ) if [ -n "$res" ] then echo "$res" exit 0 else [ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log. Try with archive (Max : $MAX_LOG)" for i in $( seq 1 $MAX_LOG ) do res=$( search_user $MAILID /var/log/mail.log.$i ) if [ -n "$res" ] then echo "$res" exit 0 else [ $DEBUG -eq 1 ] && echo "Not found from log file /var/log/mail.log.$i" fi done [ $DEBUG -eq 1 ] && echo "Not found in archive (Max : $MAX_LOG)" fi } if [ $( mailq|egrep -c ^$MAILID ) -ne 0 ] then [ $DEBUG -eq 1 ] && echo "Mail $MAILID still in mailq. Try to detect original mail ID ..." ORIGMAILID=$( postcat -q "$MAILID" |grep "by $HOSTNAME"|tail -n 1|sed 's/^.*by .* with.* id \([A-Z0-9]*\).*$/\1/' ) if [ -n "$ORIGMAILID" ] then if [ "$ORIGMAILID" != "$MAILID" ] then [ $DEBUG -eq 1 ] && echo "Original mail ID : $ORIGMAILID" search_user_from_mailid "$ORIGMAILID" [ $DEBUG -eq 1 ] && echo "User not found from original mail ID" exit 1 else [ $DEBUG -eq 1 ] && echo "Mail ID and original mail ID are equals" fi else [ $DEBUG -eq 1 ] && echo "Fail to detect original mail ID. Continue with mail ID in parameter" fi fi search_user_from_mailid "$MAILID" [ -n "$ORIGMAILID" ] && echo "User not found" && exit 1 [ $DEBUG -eq 1 ] && echo "User not found. Try to detect original mail ID with message ID in log" if [ $DEBUG -eq 1 ] then MSGID=$( mailid_to_messageid "$MAILID" -d ) else MSGID=$( mailid_to_messageid "$MAILID" ) fi if [ -z "$MSGID" ] then [ $DEBUG -eq 1 ] && echo "Message ID not found from log." exit 1 fi [ $DEBUG -eq 1 ] && echo "Message ID found : $MSGID. Try to detect original mail ID with this message ID" if [ $DEBUG -eq 1 ] then ORIGMAILID=$( messageid_to_original_mailid "$MAILID" -d ) else ORIGMAILID=$( messageid_to_original_mailid "$MAILID" ) fi if [ -z "$ORIGMAILID" ] then [ $DEBUG -eq 1 ] && echo "Original mail ID not found from message ID" exit 1 fi search_user_from_mailid "$ORIGMAILID" [ $DEBUG -eq 1 ] && echo "Username not found from original mail ID" exit 1