76 lines
1.9 KiB
Bash
76 lines
1.9 KiB
Bash
|
#!/bin/bash
|
||
|
# shellcheck disable=SC1091
|
||
|
[ -r /var/lib/from_repos/scripts/shared_functions.sh ] && . /var/lib/from_repos/scripts/shared_functions.sh
|
||
|
|
||
|
fullname=''
|
||
|
quota=104857600000
|
||
|
db_name='mail'
|
||
|
DEFAULT_DOMAIN='susurrando.com'
|
||
|
function usage() {
|
||
|
message "$(basename "${0}") --user|-u <USEREMAIL> [--password|-p <PASSWORD>] [--fullname|-n <FULL USER NAME>] [--quota|-q <QUOTA>] [--debug|-d]" p
|
||
|
}
|
||
|
while [ "${#}" -gt 0 ]
|
||
|
do
|
||
|
case "${1}" in
|
||
|
'--user'|'-u')
|
||
|
shift
|
||
|
username="${1}"
|
||
|
shift
|
||
|
;;
|
||
|
'--password'|'-p')
|
||
|
shift
|
||
|
password="${1}"
|
||
|
shift
|
||
|
;;
|
||
|
'--full-name'|'-n')
|
||
|
shift
|
||
|
fullname="${1}"
|
||
|
shift
|
||
|
;;
|
||
|
'--quota'|'-q')
|
||
|
shift
|
||
|
quota=$(echo "${1}" | grep -o '[0-9]*' | head -n 1)
|
||
|
shift
|
||
|
;;
|
||
|
'--debug'|'-d')
|
||
|
shift
|
||
|
export DEBUG=true
|
||
|
;;
|
||
|
*)
|
||
|
parameters="${parameters} ${1}"
|
||
|
message "Ignoring parameter '${1}'." p
|
||
|
shift
|
||
|
;;
|
||
|
esac
|
||
|
done
|
||
|
if [[ "${username}" == "" ]]; then
|
||
|
usage
|
||
|
exit 65
|
||
|
fi
|
||
|
if [[ "${password}" == "" ]]; then
|
||
|
password=$(/usr/bin/pwgen -c -n -B 9 1)
|
||
|
message "User password is ${password}" p
|
||
|
fi
|
||
|
if ! [[ "${username}" =~ .*@[a-zA-Z0-9]*\.[a-zA-Z0-9]{2,} ]]
|
||
|
then
|
||
|
username="${username}@${DEFAULT_DOMAIN}"
|
||
|
message "Username: '${username}'" p
|
||
|
fi
|
||
|
|
||
|
query="insert into users (email, password, fullname, quota) VALUES ('${username}', ENCRYPT('${password}'), '${fullname}', ${quota});"
|
||
|
message "Calling query: '${query}'"
|
||
|
echo "${query}" | mysql "${db_name}"
|
||
|
error_code="${?}"
|
||
|
if [[ "${error_code}" != "0" ]]; then
|
||
|
message "Error ${error_code} while adding user to the mail database" p
|
||
|
exit "${error_code}"
|
||
|
else
|
||
|
message "Added sucessfully to mail database" p
|
||
|
echo "Welcome to Susurrando.com mail service" | mail -s "Welcome to Susurrando mail service" "${username}"
|
||
|
error_code="${?}"
|
||
|
if [[ "${error_code}" != "0" ]]; then
|
||
|
message "Error ${error_code} sending welcome message" p
|
||
|
exit "${error_code}"
|
||
|
fi
|
||
|
fi
|