helper-scripts/sms/freesms

105 lines
2.6 KiB
Bash
Executable File

#!/bin/bash
function display_help() {
echo "${0} Usage :"
echo -e "\t-u --user\tSpecify the username"
echo -e "\t-p --password\tSpecify the password"
echo -e "\t-h or --help\tThis help"
echo ""
echo "Examples :"
echo -e "\tSend \"My message\" to your mobile phone :"
echo -e "\t\t${0} -u myuser -p mypassword My message"
echo -e "\tSend content from STDIN to your mobile phone :"
echo -e "\t\t${0} -u myuser -p mypassword -"
}
GET_OPT=`getopt -o hu:p: --long help,user:,password: -n "${0} parameters" -- "${@}"`
if [ ! "${?}" == "0" ] ; then
display_help
exit 1
fi
eval set -- "$GET_OPT"
while true ; do
case ${1} in
-u|--user)
NOM="${2}"
shift 2
;;
-p|--password)
PASS="${2}"
shift 2
;;
-h|--help)
display_help
shift
exit 0
;;
--)
MESSAGE="`echo ${@} | sed 's|^-- ||g'`"
shift
break
;;
*)
display_help >&2
exit 1
;;
esac
done
if [ -z "${NOM}" -o -z "${PASS}" ] ; then
echo "You need to specify Username and Password" >&2
display_help >&2
exit 1
fi
if [ "${MESSAGE}" == "-" ] ; then
if [[ ! -t 0 ]]; then
while read line ; do
MESSAGE="${MESSAGE}%0D${line}"
MESSAGE=`echo ${MESSAGE} | sed 's|^-%0D||g'`
done < <(cat -)
else
echo "STDIN is empty" >&2
exit 1
fi
fi
if [ -z "${MESSAGE}" ] ; then
echo "You need to specify the message to send" >&2
display_help >&2
exit 1
fi
if ! which which > /dev/null 2>&1 ; then
echo "which is not installed"
exit 1
elif which curl > /dev/null 2>&1 ; then
echo using curl
BIN="curl -i"
elif which wget > /dev/null 2<&1 ; then
echo using wget
BIN="wget --save-headers -qO -"
elif true ; then
echo "None of curl or wget installed" >&2
exit 1
fi
envoi=$(${BIN} "https://smsapi.free-mobile.fr/sendmsg?user=${NOM}&pass=${PASS}&msg=${MESSAGE}" 2>&1)
retour_HTTP=$(echo "${envoi}" | awk '/HTTP/ {print $2}')
case $retour_HTTP in
200)
echo "Le SMS a été envoyé sur votre mobile."
exit 0
;;
400)
echo "Un des paramètres obligatoires est manquant.";;
402)
echo "Trop de SMS ont été envoyés en trop peu de temps.";;
403)
echo "Le service n'est pas activé sur l'espace abonné, ou login / clé incorrect.";;
500)
echo "Erreur côté serveur. Veuillez réessayer ultérieurement.";;
esac
exit 1