#!/bin/sh
#
#set -x
# bb-mailack.sh - E-MAIL ACKS TO BB NOTIFICATIONS
#
# Sean MacGuire
# Robert-Andre Croteau
# Version 1.9
# Mar 13th, 2002
#
# Modified by: Asif Iqbal
# Date: December 07, 2003
# Allow delay from Skytel Pager
#
# (c) Copyright Quest Software, Inc.  1997-2002  All rights reserved.
#
# HOW IT WORKS
#
# BB-generated e-mail messages all have the security code and problem
# in the subject line.  Since the user bb is running as shouldn't ever
# get mail (unless maybe it's bouncing), replies can be acked automatically
# by parsing the subject line and sending BB and ACK message.  This user
# can still receive mail, this script will disregard non-ack message
# and save the mail message in the folder
#
# To set up: (do read note #1 and #2 as setup may differ)
#
#   On the BBPAGER host, create a .forward file under the
#   user BB is running as:
#   |<BBHOME>/bin/bb-mailack.sh
#   Adjust BBHOME to your own BBHOME
#
#   Note #1, recent version of sendmail will only execute
#    programs that are located in /etc/smrsh (check your
#    sendmail config for the proper directory).  Save
#    The solution is to put a symlink in the smrsh home
#    to the bb-mailack.sh, then use an alias rather than
#    a .forward.  Thanks to user-1eeca733f232@xymon.invalid.
#
#   Note #2, words of wisdom from John Alexander <user-ffcd7574f4d9@xymon.invalid>
#   a) If you have later versions of SendMail (8.11) install Procmail.
#   b) Put a .procmailrc file in the bbuser home directory to redirect the mail
#   c) Ensure .procmailrc has permissions of 600 or it will gripe and fail.
#   d) Ensure the bbuser home directory has perms of 755 or Procmail
#      will gripe and fail
#   e) Procmail will fail with a "suspicious rcfile" error if the home
#      directory or .procmailrc is writable by anything more than the owner.
#   f) /var/log/syslog is your friend ;)
#
#   Set the following variables to the required values
#   BBUSER    User name
#   BBHOME    Location of the BB tree
#   MAILFILE  Location of the user's mailbox
#   REPLYPFX  Prefix returned with the mail message
#   DELAY     Default delay value
#   ACK	      Send confirmation message ?
#
# To use:
#   Just reply the e-mail notifications sent by your bbuser
#
#   You can set a different DELAY value than the default
#   Type in DELAY=XXX  right after the !BB - XXXXXYY!
#      !BB - XXXXXYY! DELAY=30
#
#   You can put a defaut


######################################################
# CONFIGURATION
#
# SET SOME VARIABLES WE NEED.  SEASON TO TASTE...
# Yes, we could have tried "who am i", whoami, id -u -n
# for BBUSER but instead to figure out what's portable
# it's easier to ask you for 30secs to set them up ;)
# Also applies to MAILFILE

# This script is modified for QWEST to allow
# delay through pager - Asif 01/06/03
DEBUG=0
BBUSER="hobbit"
BB=/export/home/hobbit/server/bin/bb
MAILFILE="/var/mail/$BBUSER"
DELAY=60
GREP="/usr/local/bin/grep"
AWK="/usr/xpg4/bin/awk"
FORMAIL="/usr/local/bin/formail"
TAIL="/bin/tail"
ECHO="/bin/echo"
SED="/bin/sed"
CUT="/bin/cut"
CAT="/bin/cat"
HEAD="/bin/head"
ADDR="user-00c3cf51326c@xymon.invalid"
FROMMAIL="" #<-- This is to verify if the user has right to delay
#######################################################
# START SCRIPT

# First store the incoming message...
FILE="/tmp/ACKME.$BBUSER.$$"
SUBJECT="/tmp/ACKME.SUBJ.$BBUSER.$$"
BODY="/tmp/ACKME.BODY.$BBUSER.$$"
STAFFS="/export/home/$BBUSER/.staffs"
TODAY="`date`"
set -f
rm -f $FILE
rm -f $FILEBODY
rm -f $FILEREPLYFROM

dd of=$FILE 2>/dev/null

SAVEMAIL="FALSE"


	# Keep only valid characters
	[ $DEBUG -eq 1 ] && $ECHO Running Formail to get the BODY of the MSG
	$FORMAIL -I "" < $FILE > $BODY
	[ $DEBUG -eq 1 ] && $CAT $BODY
	$CAT $BODY | $GREP -i "bbro@" | \
	$SED 's/\|/ /g' | $SED 's/\[//g' | $SED 's/\]//g' | $TAIL -1 > $SUBJECT 2>/dev/null
	# If it's a valid BB reply
	# We should have something in this format
	# bbrodomain.net BB  2335312 host.domain.net.cpu
	NUMBER=""
	[ $DEBUG -eq 1 ] && $CAT $SUBJECT
	if [ -s $SUBJECT ]
	then
		set `$CAT "$SUBJECT"` 2>&1 >/dev/null
		while [ "$#" -gt 1 ]
		do
			# echo $1
			if [ "$1" = "BB" ]
			then
				NUMBER=`echo "$2" | $SED "s/[^0-9]//g"`
				# echo $NUMBER
				if [ "$2" = "$NUMBER" ]
				then
					ACKHOSTSERVICE=`echo "$3"`
					break;
				fi
			fi
			shift
		done

		if test "$NUMBER"
		then
			set `$CAT $BODY | $GREP -iw DELAY | tr [A-Z] [a-z]`
			while [ "$#" -gt 1 ]
			do
				if [ "$1" = "delay" ]
				then
					DELAYVAL="$2"
					break
				fi
				shift
			done
			if [ "$DELAYVAL" != "" ]
			then
				DELAY="$DELAYVAL"
			fi
			
			### 
		LINENUMBER=`$CAT $BODY | $GREP -win "DELAY" | $GREP -wi "MSG" | $CUT -d: -f1`
		[ "$DEBUG" -eq 1 ] && $ECHO Line number is $LINENUMBER
		[ ! -z "$LINENUMBER" ] && LINENUMBER=`expr $LINENUMBER "+" 1`
		[ "$DEBUG" -eq 1 ] && $ECHO Now Line number is $LINENUMBER
		if test "$LINENUMBER"
		then
			MESSAGE=`$CAT $BODY | $HEAD -$LINENUMBER | $GREP -v "^$"`
			[ "$DEBUG" -eq 1 ] && $ECHO Message with the Delay is $MESSAGE
			MESSAGE=`$ECHO $MESSAGE | $SED 's/^.*[Mm][Ss][Gg].\(.*\)$/\1/'`
			[ "$DEBUG" -eq 1 ] && $ECHO Message followed by msg is $MESSAGE
			REPLY=`echo $MESSAGE`
		else
			MESSAGE=""
			REPLY=""
		fi
		# Get the mail Id of the sender

		FROM=`$FORMAIL -x from < $FILE | $HEAD -1 | $AWK '{print $1}'`
		[ $DEBUG -eq 1 ] && $ECHO $FROM
		# Converting pager number to Full Name - Asif
		FROMMAIL=`echo $FROM | $AWK -F@ '{print $1}'`
		[ -z "$FROMMAIL" ] && exit 97
		FROMMAIL=`$CAT $STAFFS | grep $FROMMAIL | $AWK -F: '{print $2}'`
			if [ "$MESSAGE" = "" ]
			then
				MESSAGE=`echo "$FROM" | $SED 's/[ 	]/+/g' 2>/dev/null`
			fi
			# Adding date for record
			MESSAGE=`echo $TODAY $MESSAGE` 

			#echo "Message Log Sample - $MESSAGE"
			# To delay it for all
			ACKCODE=$NUMBER
			# This is where the script sends the delay to the BBPAGER - Asif
			$BB 127.0.0.1 "ack ack_event ${ACKCODE} ${DELAY} ${MESSAGE}"
			if [ "$FROMMAIL" != "" ]
			then
			 ACKSUBJ="ACK BB [$ACKCODE] $ACKHOSTSERVICE Delayed $DELAY mins by $FROMMAIL"
			 TO="$ADDR"
			 echo "$REPLY" | /bin/mailx -r $FROM -s "$ACKSUBJ" "$TO"
			fi
		else
			SAVEMAIL="TRUE"
		fi
	else
	    SAVEMAIL="TRUE"
	fi

if [ -f "$MAILFILE" -a "$SAVEMAIL" = "TRUE" ]
then
	cat $FILE >> $MAILFILE
fi

rm -f $FILE
rm -f $SUBJECT
rm -f $BODY

exit 0
