File sendsms of Package smstools3

#!/bin/bash
# This script send a text sms at the command line by creating
# a sms file in the outgoing queue.

# $1 is the destination phone number.
# $2 is the message text.
# If you leave $2 or both empty, the script will ask you.
# If you give more than 2 arguments, last is taken as a text and
# all other are taken as destination numbers.
# If a destination is asked, you can type multiple numbers
# delimited with spaces.

# Keys for example: "password" and "keke":
# KEYS="5f4dcc3b5aa765d61d8327deb882cf99 4a5ea11b030ec1cfbc8b9947fdf2c872 "

KEYS=""

# When creating keys, remember to use -n for echo:
# echo -n "key" | md5sum

smsd_group="sendsms"

function _log { D=$(date  +"%Y%m%dT%H:%M:%S") echo -e $D "$@"; }
function log_error { _log "\e[0;31mERROR\e[0m : $@" && exit 1; }
function log_warn  { _log "\e[0;33mWARN\e[0m  : $@"; }
function log_info  { _log "\e[1;32mINFO\e[0m  : $@"; }

if ! [ -z "$KEYS" ]; then
  read -p "Key: " KEY
  [ -z "$KEY" ] && log_info "key provided" || log_error "Key required, stopping."
  KEY=`echo -n "$KEY" | md5sum | awk '{print $1;}'`
  if ! echo "$KEYS" | grep "$KEY" >/dev/null; then
    log_error "Incorrect key, stopping."
  fi
fi

DEST=$1
TEXT=$2

if [ -z "$DEST" ]; then
  read -p "Destination(s): " DEST
  [ -z "$DEST" ] && log_error "No destination, stopping."
fi

if [ -z "$TEXT" ]; then
  read -p "Text: " TEXT
  [ -z "$TEXT" ] && log_error "No text, stopping."
fi

if [ $# -gt 2 ]; then
  n=$#
  while [ $n -gt 1 ]; do
    destinations="$destinations $1"
    shift
    n=`expr $n - 1`
  done
  TEXT=$1
else
  destinations=$DEST
fi

echo "-- "
echo "Text: $TEXT"

ALPHABET=""
if which iconv > /dev/null 2>&1; then
  if ! echo -n "$TEXT" | iconv -t ISO-8859-15 >/dev/null 2>&1; then
    ALPHABET="Alphabet: UCS"
  fi
fi

getent group $smsd_group > /dev/null 2>&1
[ $? != 0 ] && log_error "Group does not exist check configuration"

for destination in $destinations
do
  echo "To: $destination"

  TMPFILE=`mktemp /tmp/smsd_XXXXXX`

  echo "To: $destination" >> $TMPFILE
  [ -n "$ALPHABET" ] && echo "$ALPHABET" >> $TMPFILE
  echo "" >> $TMPFILE
  if [ -z "$ALPHABET" ]; then
    echo -n "$TEXT" >> $TMPFILE
  else
    echo -n "$TEXT" | iconv -t UNICODEBIG >> $TMPFILE
  fi

  setfacl -m g:${smsd_group}:rwx $TMPFILE

  # This bellow must be atomic operation
  ( FILE=`mktemp /var/spool/sms/outgoing/send_XXXXXX` && mv $TMPFILE $FILE ) && true || log_error "creating sms failed"
done
openSUSE Build Service is sponsored by