~mil/sxmo-utils

sxmo-utils/scripts/modem/sxmo_modemsendsms.sh -rwxr-xr-x 1.3 KiB
9bd3f31bStacy Harper Cleanup sxmo_file.sh 8 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env sh

# include common definitions
# shellcheck source=scripts/core/sxmo_common.sh
. "$(dirname "$0")/sxmo_common.sh"

info() {
	echo "$1" >> /dev/stderr
}

err() {
	info "$1"
	exit 1
}

usage() {
	err "Usage: $(basename "$0") number [-|message]"
}

modem_n() {
	MODEMS="$(mmcli -L)"
	echo "$MODEMS" | grep -qoE 'Modem\/([0-9]+)' || err "Couldn't find modem - is your modem enabled?"
	echo "$MODEMS" | grep -oE 'Modem\/([0-9]+)' | cut -d'/' -f2
}

MODEM="$(modem_n)"
[ 0 -eq $# ] && usage
NUMBER="$1"

if [ "-" = "$2" ]; then
	TEXT="$(cat)"
else
	shift
	[ 0 -eq $# ] && usage

	TEXT="$*"
fi
TEXTSIZE="${#TEXT}"

#mmcli doesn't appear to be able to interpret a proper escape
#mechanism, so we'll substitute double quotes for two single quotes
SAFE_TEXT=$(echo "$TEXT" | sed "s/\"/''/g")

SMSNO="$(
	mmcli -m "$MODEM" --messaging-create-sms="text=\"$SAFE_TEXT\",number=$NUMBER" |
	grep -o "[0-9]*$"
)"
mmcli -s "${SMSNO}" --send || err "Couldn't send text message"
for i in $(mmcli -m "$MODEM" --messaging-list-sms | grep " (sent)" | cut -f5 -d' ') ; do
	mmcli -m "$MODEM" --messaging-delete-sms="$i"
done

TIME="$(date --iso-8601=seconds)"
mkdir -p "$LOGDIR/$NUMBER"
printf %b "Sent to $NUMBER at $TIME:\n$TEXT\n\n" >> "$LOGDIR/$NUMBER/sms.txt"
printf %b "$TIME\tsent_txt\t$NUMBER\t$TEXTSIZE chars\n" >> "$LOGDIR/modemlog.tsv"

info "Sent text message ok"