diff --git a/bin/vmail-autoresponders-add.sh b/bin/vmail-autoresponders-add.sh new file mode 100755 index 0000000..e2c3f24 --- /dev/null +++ b/bin/vmail-autoresponders-add.sh @@ -0,0 +1,86 @@ +#!/bin/bash +# +# vmail-stack +# https://git.stack-source.com/msb/vmail-stack +# MIT License Copyright (c) 2021 Matthew Saunders Brown + +# load include file +source $(dirname $0)/vmail.sh + +help() +{ + thisfilename=$(basename -- "$0") + echo "$thisfilename" + echo "Add autoresponder" + echo "" + echo "usage: $thisfilename -e -u -b [-o ] [-s <0|1>] [-h]" + echo "" + echo " -h Print this help." + echo " -e Email address of the autoresponder." + echo " -u Subject of autresponder emails." + echo " -b Body of autresponder emails." + echo " -o Mode - either Autoresponder or Vacation. Default is 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 subject +if [[ -z $subject ]]; then + echo "subject is required" + exit 1 +fi + +# check for body +if [[ -z $body ]]; then + echo "body is required" + 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 + # no autoresponder, add new one now + dbquery="INSERT INTO vm_autoresponders SET mbox_id='$mbox_id', subject='$subject', body='$body'" + if [ ! -z $mode ]; then + dbquery="$dbquery, mode='$mode'" + fi + if [ ! -z $status ]; then + dbquery="$dbquery, status='$status'" + fi + eval $dbcmd $dbcmdopts \"$dbquery\;\" + else + echo "ERROR: Autoresponder for $email already exists, edit or delete and re-create instead." + exit 1 + fi +else + # db query error + echo "ERROR: System error querying database." + exit 1 +fi diff --git a/bin/vmail-autoresponders-del.sh b/bin/vmail-autoresponders-del.sh new file mode 100755 index 0000000..d4fd70d --- /dev/null +++ b/bin/vmail-autoresponders-del.sh @@ -0,0 +1,50 @@ +#!/bin/bash +# +# vmail-stack +# https://git.stack-source.com/msb/vmail-stack +# MIT License Copyright (c) 2021 Matthew Saunders Brown + +# load include file +source $(dirname $0)/vmail.sh + +help() +{ + thisfilename=$(basename -- "$0") + echo "$thisfilename" + echo "Delete autoresponder." + echo "" + echo "usage: $thisfilename -e email [-h]" + echo "" + echo " -h Print this help." + echo " -e Email address to remove autoresponder from." + exit +} + +vmail:getoptions "$@" + +# check for email +if [[ -z $email ]]; then + echo "email is required" + exit 1 +fi + +# build query +dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" +dbcmdopts="-s -r -N -e" + +# get autoresponders id which also works to confirm autoresponder exists +dbquery="SELECT vm_autoresponders.id FROM vm_autoresponders, vm_mboxes, vm_domains WHERE vm_autoresponders.mbox_id=vm_mboxes.id AND vm_mboxes.mbox='$mbox' AND vm_mboxes.domain_id=vm_domains.id AND vm_domains.domain='$domain';" +vm_autoresponders_id=`$dbcmd $dbcmdopts "$dbquery"` + +if [ -z "$vm_autoresponders_id" ]; then + echo "ERROR: Autoresponder for $email does not exist." + exit 1 +elif [ "$vm_autoresponders_id" -gt '0' ]; then + dbquery="DELETE FROM vm_autoresponders WHERE vm_autoresponders.id='$vm_autoresponders_id';" + eval $dbcmd $dbcmdopts \"$dbquery\" + echo "SUCCESS: Autoresponder for $email removed from system." + exit 0 +else + echo "ERROR: System error querying vmail database" + exit 1 +fi diff --git a/bin/vmail-autoresponders-get.sh b/bin/vmail-autoresponders-get.sh new file mode 100755 index 0000000..9e84a20 --- /dev/null +++ b/bin/vmail-autoresponders-get.sh @@ -0,0 +1,57 @@ +#!/bin/bash +# +# vmail-stack +# https://git.stack-source.com/msb/vmail-stack +# MIT License Copyright (c) 2021 Matthew Saunders Brown + +# load include file +source $(dirname $0)/vmail.sh + +help() +{ + thisfilename=$(basename -- "$0") + echo "$thisfilename" + echo "Get email autresonder data from vmail database." + echo "" + echo "usage: $thisfilename -e email|-d domain [-c] [-t] [-h]" + echo "" + echo " -h Print this help." + echo " -e Email address to get autresonder for." + echo " -d Domain to get all autoresponders for." + echo " -c Output in cvs format." + echo " -t Use tabs instead of tables for output, do not display column headers." + echo "" + echo " Search term is optional. If nothing specified all forwards for all email acccounts for all domains will be returned." + echo " Enter email address to get forward info for that email address." + echo " Enter forwarding address (in full email address format) with the -f option to get address(es) that forward to specified address." + echo " Enter domain name to get all forwards for all email addresses under that domain." + exit +} + + +vmail:getoptions "$@" + +# check for domain (which will aslo be set if email is specified) +if [[ -z $domain ]]; then + echo "Domian Name or Email is required." + exit 1 +fi + +# set initial db query data +dbcmd="mysql --defaults-extra-file=$MYSQL_CONNECTION_INFO_FILE" +dbcmdopts="-e" + +if [[ -n $tab ]]; then + dbcmdopts="-s -N $dbcmdopts" +fi + +dbquery="SELECT vm_mboxes.mbox, vm_domains.domain, vm_autoresponders.subject, QUOTE(REPLACE(vm_autoresponders.body,'\r\n','\\n')) AS body, vm_autoresponders.mode, vm_autoresponders.status FROM vm_autoresponders, vm_mboxes, vm_domains WHERE vm_autoresponders.mbox_id = vm_mboxes.id AND vm_mboxes.domain_id = vm_domains.id AND vm_domains.domain='$domain'" + +# build query +if [[ -n $email ]]; then + # search for specific autoresponder + dbquery="$dbquery AND vm_mboxes.mbox='$mbox' ORDER BY vm_mboxes.mbox" +fi + +# execute mysql query +eval $dbcmd $dbcmdopts "\"$dbquery\"" $cvs diff --git a/bin/vmail-autoresponders-mod.sh b/bin/vmail-autoresponders-mod.sh new file mode 100755 index 0000000..d188c9f --- /dev/null +++ b/bin/vmail-autoresponders-mod.sh @@ -0,0 +1,88 @@ +#!/bin/bash +# +# vmail-stack +# https://git.stack-source.com/msb/vmail-stack +# MIT License Copyright (c) 2021 Matthew Saunders Brown + +# load include file +source $(dirname $0)/vmail.sh + +help() +{ + thisfilename=$(basename -- "$0") + echo "$thisfilename" + echo "Modify autoresponder" + echo "" + echo "usage: $thisfilename -e [-u ] [-b ] [-o ] [-s <0|1>] [-h]" + echo "" + echo " -h Print this help." + echo " -e Email address of the autoresponder." + echo " -u Subject of autresponder emails." + echo " -b Body of autresponder emails." + echo " -o 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 diff --git a/bin/vmail.sh b/bin/vmail.sh index 017a004..7b9dc2d 100755 --- a/bin/vmail.sh +++ b/bin/vmail.sh @@ -13,15 +13,15 @@ readonly MYSQL_CONNECTION_INFO_FILE=$VMAIL_DIR/.my.cnf # switch to required user if [[ $(basename $0) == "vmail-dkim-"* ]]; then if [[ "$USER" != "Debian-exim" ]]; then - exec sudo -u Debian-exim -g ssl-cert $0 $@ + exec sudo -u Debian-exim -g ssl-cert $0 "$@" fi elif [[ $(basename $0) == "vmail-purge-spool.sh" ]]; then if [[ "$USER" != "Debian-exim" ]]; then - exec sudo -u Debian-exim $0 $@ + exec sudo -u Debian-exim $0 "$@" fi else if [[ "$USER" != "vmail" ]]; then - exec sudo -u vmail $0 $@ + exec sudo -u vmail $0 "$@" else # check that MYSQL_CONNECTION_INFO_FILE exists and is readable if [ ! -f "$MYSQL_CONNECTION_INFO_FILE" ]; then @@ -85,7 +85,7 @@ function vmail::yesno() { function vmail:getoptions () { local OPTIND - while getopts "ha:b:d:e:f:gj:cp:q:r:s:tk:gl:m:vx" opt ; do + while getopts "ha:b:d:e:f:gj:cp:q:r:s:tk:gl:m:o:u:vx" opt ; do case "${opt}" in h ) # display help and exit help @@ -103,8 +103,8 @@ function vmail:getoptions () { fi fi ;; - b ) # mbox - local part of email address - mbox=${OPTARG,,} + b ) # body - Body for Autoresponder emails + body="${OPTARG}" ;; c ) # cvs - output in cvs format cvs="| sed 's/\t/,/g'" @@ -178,6 +178,14 @@ function vmail:getoptions () { p ) # password password=${OPTARG} ;; + o ) # mode for autoresponder - must be Vacation or Autoresponder + mode=${OPTARG,,} # first force lower case + mode=${mode^} # then capitlize first letter + if [[ $mode != "Vacation" ]] && [[ $mode != "Autoresponder" ]]; then + echo "ERROR: Invalid mode setting: -o $mode. Must be either Vacation or Autoresponder" + exit 1 + fi + ;; q ) # quota quota=${OPTARG} ;; @@ -186,10 +194,17 @@ function vmail:getoptions () { ;; s ) # status - 0 or 1 status=${OPTARG} + if [[ $status != "0" ]] && [[ $status != "1" ]]; then + echo "ERROR: Invalid status setting: -s $status" + exit 1 + fi ;; t ) # tab - Use tabs instead of tables for output, do not display column headers tab=true ;; + u ) # subject - Subject for Autoresponder emails + subject="${OPTARG}" + ;; n ) # dry-run dryrun=true ;;