vmail-stack/bin/vmail-autoresponders-mod.sh
2022-08-22 13:34:20 -07:00

90 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
#
# vmail-stack
# https://git.stack-source.com/msb/vmail-stack
# Copyright (c) 2022 Matthew Saunders Brown <matthewsaundersbrown@gmail.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# load include file
source $(dirname $0)/vmail.sh
help()
{
thisfilename=$(basename -- "$0")
echo "$thisfilename"
echo "Modify autoresponder"
echo ""
echo "usage: $thisfilename -e <email> [-u <subject>] [-b <body>] [-o <mode>] [-s <0|1>] [-h]"
echo ""
echo " -h Print this help."
echo " -e <email> Email address of the autoresponder."
echo " -u <subject> Subject of autresponder emails."
echo " -b <body> Body of autresponder emails."
echo " -o <mode> Mode - either Autoresponder or Vacation."
echo " -s <0|1> Status. 0 = disabled/off, 1 = enabled/on. Default is 1."
echo ""
echo " The body must be single quoted and escaped. All single quotes in the body must"
echo " have double back slash like this \\\' and all newlines should be encoded as \n."
echo " In Vacaion mode only the first email from a specific address is replied to,"
echo " in Autoresponder mode every incoming email, including repeats, get a response."
exit
}
vmail:getoptions "$@"
# check for email
if [[ -z $email ]]; then
echo "email is required"
exit 1
fi
# check for options
if [[ -z $subject && -z $body && -z $mode && -z $status ]]; then
echo "One or more options to update must be specified"
exit 1
fi
# build query
dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE"
dbcmdopts="-s -r -N -e"
# get mbox id, which also verfies that email address exists
dbquery="SELECT vm_mboxes.id FROM vm_mboxes, vm_domains WHERE vm_mboxes.mbox='$mbox' AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain';"
mbox_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [ -z $mbox_id ]; then
# mailbox does not exist
echo "ERROR: Email account $email does not exist."
exit 1
elif [ "$mbox_id" -gt '0' ]; then
# verified mbox, check for existing forward
dbquery="SELECT id FROM vm_autoresponders WHERE mbox_id='$mbox_id';"
vm_autoresponders_id=`eval $dbcmd $dbcmdopts \"$dbquery\"`
if [ -z $vm_autoresponders_id ]; then
# existing autoresponder does not exist, can't modify
echo "ERROR: Autoresponder for $email does not exists, can't edit. Add new autoresponder instead."
exit 1
else
# start update by re-setting mbox_id, doesn't change anything, just facilitates following code
dbquery="UPDATE vm_autoresponders SET mbox_id='$mbox_id'"
if [ ! -z "$subject" ]; then
dbquery="$dbquery, subject='$subject'"
fi
if [ ! -z "$body" ]; then
dbquery="$dbquery, body='$body'"
fi
if [ ! -z $mode ]; then
dbquery="$dbquery, mode='$mode'"
fi
if [ ! -z $status ]; then
dbquery="$dbquery, status='$status'"
fi
dbquery="$dbquery WHERE vm_autoresponders.id='$vm_autoresponders_id'"
eval $dbcmd $dbcmdopts \"$dbquery\;\"
fi
else
# db query error
echo "ERROR: System error querying database."
exit 1
fi