~anjan/public-inbox

sxmo-userscripts: fancy sms: sms2email, sms2irc, and wakeup hack v1 NEEDS REVISION

Peter John Hartman: 1
 fancy sms: sms2email, sms2irc, and wakeup hack

 10 files changed, 496 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~anjan/public-inbox/patches/27377/mbox | git am -3
Learn more about email & git

[PATCH sxmo-userscripts] fancy sms: sms2email, sms2irc, and wakeup hack Export this patch

---
 hooks/sms/README.md                  |  12 +++
 hooks/sms/fancy-sms                  | 108 +++++++++++++++++++++++++++
 hooks/sms/sxmo_msmtp-enqueue.sh      |  37 +++++++++
 hooks/sms/sxmo_msmtp-listqueue.sh    |   7 ++
 hooks/sms/sxmo_msmtp-runqueue.sh     |  74 ++++++++++++++++++
 hooks/sms/sxmo_msmtp-runqueue.sh.bak |  69 +++++++++++++++++
 hooks/sms/sxmo_sms-enqueue.sh        |  33 ++++++++
 hooks/sms/sxmo_sms-listqueue.sh      |   9 +++
 hooks/sms/sxmo_sms-runqueue.sh       |  75 +++++++++++++++++++
 hooks/sms/sxmo_sms-runqueue.sh.bak   |  72 ++++++++++++++++++
 10 files changed, 496 insertions(+)
 create mode 100644 hooks/sms/README.md
 create mode 100755 hooks/sms/fancy-sms
 create mode 100755 hooks/sms/sxmo_msmtp-enqueue.sh
 create mode 100755 hooks/sms/sxmo_msmtp-listqueue.sh
 create mode 100755 hooks/sms/sxmo_msmtp-runqueue.sh
 create mode 100755 hooks/sms/sxmo_msmtp-runqueue.sh.bak
 create mode 100755 hooks/sms/sxmo_sms-enqueue.sh
 create mode 100755 hooks/sms/sxmo_sms-listqueue.sh
 create mode 100755 hooks/sms/sxmo_sms-runqueue.sh
 create mode 100755 hooks/sms/sxmo_sms-runqueue.sh.bak

diff --git a/hooks/sms/README.md b/hooks/sms/README.md
new file mode 100644
index 0000000..4bf8d96
--- /dev/null
+++ b/hooks/sms/README.md
@@ -0,0 +1,12 @@
# inputhandler hooks

This directory contains example sms hooks.

# Contents

## android_buttons
- Author: Peter <peterjohnhartman@gmail.com>
- License: MIT
- Description: sends sms as email, detects a wakeup sms, and sends sms to weechat client


diff --git a/hooks/sms/fancy-sms b/hooks/sms/fancy-sms
new file mode 100755
index 0000000..e9f9754
--- /dev/null
+++ b/hooks/sms/fancy-sms
@@ -0,0 +1,108 @@
#!/bin/sh

# This sms hook does three things:
# 1. detects a specific message that will wake up the phone
# 2. sends sms as e-mail
# 3. sends sms to a remote weechat instance
# 
# (2) and (3) require the sxmo_msmtp-*queue.sh scripts
#
. "$(which sxmo_common.sh)"

EMAIL_ADDR="youraddr@you.com"
CONTACT="$1" # Contact or Number
NOSPACE_CONTACT="$(printf %s "$CONTACT" | sed 's/ /_/')"

TEXT="$2"
[ -n "$3" ] && MMS_ID="$3"
if [ -n "$4" ]; then
	GROUP_CONTACT="$4"
	NOSPACE_GROUP_CONTACT="$(printf %s "$GROUP_CONTACT" | sed 's/ /_/')"
fi

#
# wakeup hack
#

SMS_WAKEUP_CONTACT="tmobile email" # this should be in contacts

# For tmobile, the message is something like:
# myemailaddress@foo.com / subject message / body message
# sms-wakeup.sh script sends:
# printf "Subject: wake up 120 1234" | msmtp MYNUMBER@tmomail.net
# where: 120 is wakeup_time and 1234 is wakeup_pin
if [ "$CONTACT" = "$SMS_WAKEUP_CONTACT" ]; then
	printf "%s sms: Got an email from $SMS_WAKEUP_CONTACT.\n" "$(date)" >&2
	message="$(echo "$TEXT" | cut -d'/' -f2)"
	if printf %s "$message" | grep -q ' wake up '; then
		printf "%s sms: Contains 'wake up'.\n" "$(date)" >&2
		wakeup_time="$(echo "$message" | cut -d' ' -f4)"
		[ -z "wakeup_time" ] && wakeup_time="120"
		wakeup_pin="$(echo "$message" | cut -d' ' -f5)"
		if [ "$wakeup_pin" = "1234" ]; then
			printf "%s sms: Good pin! Waking up and waiting for ping or timeout of %s seconds.\n" "$(date)" "$wakeup_time" >&2
			RES="$(timeout "$wakeup_time" sh -c "while true; do if ping -c1 -i1 8.8.8.8 &>/dev/null; then echo "up"; break; fi; done")"
			if [ "$RES" = "up" ]; then
				printf "%s sms: Ok got network.\n" "$(date)" >&2
			else
				printf "%s sms: Did not get network!\n" "$(date)" >&2
			fi
			sleep 60 # give it another little bit so client can connect
			printf "%s sms: OK done. Going back to sleep...\n" "$(date)" >&2
			# dislodge notification
			SMS_WAKEUP_NUMBER="$(sxmo_contacts.sh --all | grep "^$SMS_WAKEUP_CONTACT:" | cut -d':' -f2 | sed 's/^ //')"
			cat "$LOGDIR/$SMS_WAKEUP_NUMBER/sms.txt"
			printf "%s sms_hook: dislodging notification: $LOGDIR/$SMS_WAKEUP_NUMBER/sms.txt\n" "$(date)" >&2
		else
			printf "%s sms: Bad pin: %s\n" "$(date)" "$wakeup_pin" >&2
		fi
		exit

	else
		printf "%s sms: Does not contain 'wake up'.\n" "$(date)" >&2
	fi

	exit # don't process these
fi

#
# sms2email hack
#
printf "%s sms: sending email...\n" "$(date)" >&2
ATTACHMENTS=
if [ -n "$MMS_ID" ]; then
	if [ -n "$GROUP_CONTACT" ]; then
		FROM="$GROUP_CONTACT"
		NUMBER="$(sxmo_contacts.sh --all | grep "^$GROUP_CONTACT:" | cut -d':' -f2 | sed 's/^ //')"
		[ -z "$NUMBER" ] && NUMBER="$GROUP_CONTACT"
	else
		FROM="$CONTACT"
		NUMBER="$(sxmo_contacts.sh --all | grep "^$CONTACT:" | cut -d':' -f2 | sed 's/^ //')"
		[ -z "$NUMBER" ] && NUMBER="$CONTACT"
	fi

	if ls "$LOGDIR"/"$NUMBER"/attachments/"$MMS_ID"*; then
		ATTACHMENTS="$(printf -- '-a %s ' "$LOGDIR"/"$NUMBER"/attachments/"$MMS_ID"*)"
	fi
fi

# Space in subject fails.
printf "SMS from %s <%s>: %s\n" "$CONTACT" "$GROUP_CONTACT" "$TEXT" | sxmo_msmtp-enqueue.sh -s "SMS:$NOSPACE_CONTACT" "$ATTACHMENTS" -- "$EMAIL_ADDR"

#
# sms2irc hack
#
printf "%s sms: sending irc stuff...\n" "$(date)" >&2
if [ -n "$GROUP_CONTACT" ]; then
	CHANNEL="$NOSPACE_GROUP_CONTACT"
else
	CHANNEL="$NOSPACE_CONTACT"
fi
MESSAGE="<< $(printf %s "$NOSPACE_CONTACT"): $(printf %s "$TEXT" | tr '\n' '|')"
printf "%s %s" "$CHANNEL" "$MESSAGE" | sxmo_sms-enqueue.sh

sxmo_msmtp-runqueue.sh || printf "%s sms: msmtp runqueue failed.\n" "$(date)" >&2
sxmo_sms-runqueue.sh || printf "%s sms: sms runqueue failed.\n" "$(date)" >&2

printf "%s sms: finished %s\n" "$(date)" "$(sxmo_hooks.sh statusbar)" >&2

diff --git a/hooks/sms/sxmo_msmtp-enqueue.sh b/hooks/sms/sxmo_msmtp-enqueue.sh
new file mode 100755
index 0000000..3fe74ed
--- /dev/null
+++ b/hooks/sms/sxmo_msmtp-enqueue.sh
@@ -0,0 +1,37 @@
#!/usr/bin/env sh

QUEUEDIR="$HOME/.msmtpqueue"

# Set secure permissions on created directories and files
umask 077

# Change to queue directory (create it if necessary)
if [ ! -d "$QUEUEDIR" ]; then
	mkdir -p "$QUEUEDIR" || exit 1
fi
cd "$QUEUEDIR" || exit 1

# Create new unique filenames of the form
# MAILFILE:  ccyy-mm-dd-hh.mm.ss[-x].mail
# MSMTPFILE: ccyy-mm-dd-hh.mm.ss[-x].msmtp
# where x is a consecutive number only appended if you send more than one
# mail per second.
BASE="$(date +%Y-%m-%d-%H.%M.%S)"
if [ -f "$BASE.mail" ] || [ -f "$BASE.msmtp" ]; then
	TMP="$BASE"
	i=1
	while [ -f "$TMP-$i.mail" ] || [ -f "$TMP-$i.msmtp" ]; do
		i=$((i + 1))
	done
	BASE="$BASE-$i"
fi
MAILFILE="$BASE.mail"
MSMTPFILE="$BASE.msmtp"

# Write command line to $MSMTPFILE
echo "$@" > "$MSMTPFILE" || exit 1

# Write the mail to $MAILFILE
cat > "$MAILFILE" || exit 1

exit 0
diff --git a/hooks/sms/sxmo_msmtp-listqueue.sh b/hooks/sms/sxmo_msmtp-listqueue.sh
new file mode 100755
index 0000000..149e3ef
--- /dev/null
+++ b/hooks/sms/sxmo_msmtp-listqueue.sh
@@ -0,0 +1,7 @@
#!/usr/bin/env sh
QUEUEDIR="$HOME/.msmtpqueue"

for i in "$QUEUEDIR"/*.mail; do
	grep -E -s -h '(^From:|^To:|^Subject:)' "$i" || echo "No mail in queue." >&2;
	echo " " 2>&1
done
diff --git a/hooks/sms/sxmo_msmtp-runqueue.sh b/hooks/sms/sxmo_msmtp-runqueue.sh
new file mode 100755
index 0000000..008e7f5
--- /dev/null
+++ b/hooks/sms/sxmo_msmtp-runqueue.sh
@@ -0,0 +1,74 @@
#!/usr/bin/env sh

QUEUEDIR="$HOME/.msmtpqueue"
LOCKFILE="$QUEUEDIR/.lock"
MAXWAIT=120

OPTIONS="$*"

stderr() {
	printf "%s sxmo_msmtp-runqueue.sh: %s.\n" "$(date)" "$*" >&2
}

if ! ping -q -c 1 -W 10 8.8.8.8 >/dev/null 2>/dev/null; then
	stderr "Could not ping. Bailing."
	exit 1
fi

# eat some options that would cause msmtp to return 0 without sendmail mail
case "$OPTIONS" in 
	*--help*)
		stderr "Send mails in $QUEUEDIR."
		exit 0
		;;
	*--version*)
		stderr "Version 1.0."
		exit 0
		;;
esac

# wait for a lock that another instance has set
WAIT=0
while [ -e "$LOCKFILE" ] && [ "$WAIT" -lt "$MAXWAIT" ]; do
	sleep 1
	WAIT="$((WAIT + 1))"
done
if [ -e "$LOCKFILE" ]; then
	stderr "$QUEUEDIR locked." 
	exit 1
fi

# change into $QUEUEDIR 
cd "$QUEUEDIR" || exit 1

# check for empty queuedir
if [ "$(echo ./*.mail)" = './*.mail' ]; then
	stderr "No mails in $QUEUEDIR." 
	exit 0
fi

# lock the $QUEUEDIR
touch "$LOCKFILE" || exit 1

# process all mails
for MAILFILE in *.mail; do
	MSMTPFILE="$(echo $MAILFILE | sed -e 's/mail/msmtp/')"
	stderr "Sending $MAILFILE to $(sed -e 's/^.*-- \(.*$\)/\1/' $MSMTPFILE) ..."
	if [ ! -f "$MSMTPFILE" ]; then
		stderr "No corresponding file $MSMTPFILE found."
		continue
	fi
	#msmtp $OPTIONS $(cat "$MSMTPFILE") < "$MAILFILE"
	mutt "$OPTIONS" $(cat "$MSMTPFILE") < "$MAILFILE"
	if [ $? -eq 0 ]; then
		rm "$MAILFILE" "$MSMTPFILE"
		stderr "$MAILFILE sent successfully"
	else
		stderr "$MAILFILE did not send successfully."
	fi
done

# remove the lock
rm -f "$LOCKFILE"

exit 0
diff --git a/hooks/sms/sxmo_msmtp-runqueue.sh.bak b/hooks/sms/sxmo_msmtp-runqueue.sh.bak
new file mode 100755
index 0000000..bd5d555
--- /dev/null
+++ b/hooks/sms/sxmo_msmtp-runqueue.sh.bak
@@ -0,0 +1,69 @@
#!/usr/bin/env sh

QUEUEDIR="$HOME/.msmtpqueue"
LOCKFILE="$QUEUEDIR/.lock"
MAXWAIT=120

OPTIONS="$*"
if ! ping -q -c 1 -W 10 www.google.com >/dev/null 2>/dev/null; then
	exit 1
fi

# eat some options that would cause msmtp to return 0 without sendmail mail
case "$OPTIONS" in 
	*--help*)
		echo "$0: send mails in $QUEUEDIR"
		echo "Options are passed to msmtp"
		exit 0
		;;
	*--version*)
		echo "$0: unknown version"
		exit 0
		;;
esac

# wait for a lock that another instance has set
WAIT=0
while [ -e "$LOCKFILE" ] && [ "$WAIT" -lt "$MAXWAIT" ]; do
	sleep 1
	WAIT="$((WAIT + 1))"
done
if [ -e "$LOCKFILE" ]; then
	echo "$QUEUEDIR locked." >&2
	exit 1
fi

# change into $QUEUEDIR 
cd "$QUEUEDIR" || exit 1

# check for empty queuedir
if [ "$(echo ./*.mail)" = './*.mail' ]; then
	echo "No mails in $QUEUEDIR" >&2
	exit 0
fi

# lock the $QUEUEDIR
touch "$LOCKFILE" || exit 1

# process all mails
for MAILFILE in *.mail; do
	MSMTPFILE="$(echo $MAILFILE | sed -e 's/mail/msmtp/')"
	echo "*** Sending $MAILFILE to $(sed -e 's/^.*-- \(.*$\)/\1/' $MSMTPFILE) ..." 2>&1
	if [ ! -f "$MSMTPFILE" ]; then
		echo "No corresponding file $MSMTPFILE found." 2>&1
		continue
	fi
	#msmtp $OPTIONS $(cat "$MSMTPFILE") < "$MAILFILE"
	mutt "$OPTIONS" $(cat "$MSMTPFILE") < "$MAILFILE"
	if [ $? -eq 0 ]; then
		rm "$MAILFILE" "$MSMTPFILE"
		echo "$MAILFILE sent successfully" 2>&1
	else
		echo "$MAILFILE did not send successfully." 2>&1
	fi
done

# remove the lock
rm -f "$LOCKFILE"

exit 0
diff --git a/hooks/sms/sxmo_sms-enqueue.sh b/hooks/sms/sxmo_sms-enqueue.sh
new file mode 100755
index 0000000..4d2e79c
--- /dev/null
+++ b/hooks/sms/sxmo_sms-enqueue.sh
@@ -0,0 +1,33 @@
#!/usr/bin/env sh

# TODO: move to sxmo_common.sh
QUEUEDIR="$HOME/.smsqueue"

# Set secure permissions on created directories and files
umask 077

# Change to queue directory (create it if necessary)
if [ ! -d "$QUEUEDIR" ]; then
	mkdir -p "$QUEUEDIR" || exit 1
fi
cd "$QUEUEDIR" || exit 1

# Create new unique filenames of the form
# SMSFILE:  ccyy-mm-dd-hh.mm.ss[-x].sms
# where x is a consecutive number only appended if you send more than one
# sms per second.
BASE="$(date +%Y-%m-%d-%H.%M.%S)"
if [ -f "$BASE.sms" ]; then
	TMP="$BASE"
	i=1
	while [ -f "$TMP-$i.sms" ]; do
		i=$((i + 1))
	done
	BASE="$BASE-$i"
fi
SMSFILE="$BASE.sms"

# Write the mail to $MAILFILE
cat > "$SMSFILE" || exit 1

exit 0
diff --git a/hooks/sms/sxmo_sms-listqueue.sh b/hooks/sms/sxmo_sms-listqueue.sh
new file mode 100755
index 0000000..d67bd93
--- /dev/null
+++ b/hooks/sms/sxmo_sms-listqueue.sh
@@ -0,0 +1,9 @@
#!/usr/bin/env sh

# TODO: move to sxmo_common.sh
QUEUEDIR="$HOME/.smsqueue"

for i in "$QUEUEDIR"/*.sms; do
	cat "$i" 2>/dev/null || echo "No sms in queue." >&2
	echo " " >&2
done
diff --git a/hooks/sms/sxmo_sms-runqueue.sh b/hooks/sms/sxmo_sms-runqueue.sh
new file mode 100755
index 0000000..95c7bba
--- /dev/null
+++ b/hooks/sms/sxmo_sms-runqueue.sh
@@ -0,0 +1,75 @@
#!/usr/bin/env sh

# TODO: move to sxmo_commons.sh
QUEUEDIR="$HOME/.smsqueue"
IRC_SERVER="trillap"
WEECHAT_FIFO="~/.weechat/weechat_fifo" # on the server

LOCKFILE="$QUEUEDIR/.lock"
MAXWAIT=120

stderr() {
	printf "%s sxmo_sms-runqueue.sh: %s.\n" "$(date)" "$*" >&2
}

OPTIONS="$*"

if ! ping -q -c 1 -W 10 8.8.8.8 >/dev/null 2>/dev/null; then
	exit 1
fi

case "$OPTIONS" in 
	*--help*)
		stderr "Send sms messages in $QUEUEDIR."
		exit 0
		;;
	*--version*)
		echo "Version 1.0."
		exit 0
		;;
esac

# wait for a lock that another instance has set
WAIT=0
while [ -e "$LOCKFILE" ] && [ "$WAIT" -lt "$MAXWAIT" ]; do
	sleep 1
	WAIT="$((WAIT + 1))"
done
if [ -e "$LOCKFILE" ]; then
	stderr "$QUEUEDIR locked."
	exit 1
fi

# change into $QUEUEDIR 
cd "$QUEUEDIR" || exit 1

# check for empty queuedir
if [ "$(echo ./*.sms)" = './*.sms' ]; then
	stderr "No sms messages in $QUEUEDIR."
	exit 0
fi

# lock the $QUEUEDIR
touch "$LOCKFILE" || exit 1

# process all mails
for SMSFILE in *.sms; do
	CHANNEL="$(cat "$SMSFILE" | cut -d' ' -f1)"
	MESSAGE="$(cat "$SMSFILE" | cut -d' ' -f2-)"
	stderr "Sending $SMSFILE to $CHANNEL."
	echo "irc.server.im */join #$CHANNEL" | ssh "$IRC_SERVER" -T "cat > $WEECHAT_FIFO"
	sleep 1
	echo "irc.im.#$CHANNEL *$MESSAGE" | ssh "$IRC_SERVER" -T "cat > $WEECHAT_FIFO"

	if [ $? -eq 0 ]; then
		rm "$SMSFILE"
		stderr "$SMSFILE sent successfully."
	else
		stderr "$SMSFILE failed to send!"
	fi
done

# remove the lock
rm -f "$LOCKFILE"

exit 0
diff --git a/hooks/sms/sxmo_sms-runqueue.sh.bak b/hooks/sms/sxmo_sms-runqueue.sh.bak
new file mode 100755
index 0000000..b6160e0
--- /dev/null
+++ b/hooks/sms/sxmo_sms-runqueue.sh.bak
@@ -0,0 +1,72 @@
#!/usr/bin/env sh

# TODO: move to sxmo_commons.sh
QUEUEDIR="$HOME/.smsqueue"
IRC_SERVER="trillap"
WEECHAT_FIFO="~/.weechat/weechat_fifo" # on the server

LOCKFILE="$QUEUEDIR/.lock"
MAXWAIT=120

OPTIONS="$*"

if ! ping -q -c 1 -W 10 www.google.com >/dev/null 2>/dev/null; then
	exit 1
fi

case "$OPTIONS" in 
	*--help*)
		echo "$0: send sms messages in $QUEUEDIR"
		echo "Options are passed to ssh."
		exit 0
		;;
	*--version*)
		echo "$0: unknown version"
		exit 0
		;;
esac

# wait for a lock that another instance has set
WAIT=0
while [ -e "$LOCKFILE" ] && [ "$WAIT" -lt "$MAXWAIT" ]; do
	sleep 1
	WAIT="$((WAIT + 1))"
done
if [ -e "$LOCKFILE" ]; then
	echo "$(date): $QUEUEDIR locked." >&2
	exit 1
fi

# change into $QUEUEDIR 
cd "$QUEUEDIR" || exit 1

# check for empty queuedir
if [ "$(echo ./*.sms)" = './*.sms' ]; then
	echo "No sms messages in $QUEUEDIR." >&2
	exit 0
fi

# lock the $QUEUEDIR
touch "$LOCKFILE" || exit 1

# process all mails
for SMSFILE in *.sms; do
	CHANNEL="$(cat "$SMSFILE" | cut -d' ' -f1)"
	MESSAGE="$(cat "$SMSFILE" | cut -d' ' -f2-)"
	echo "*** Sending $SMSFILE with $MESSAGE to $CHANNEL..." >&2
	echo "irc.server.im */join #$CHANNEL" | ssh "$IRC_SERVER" -T "cat > $WEECHAT_FIFO"
	sleep 1
	echo "irc.im.#$CHANNEL *$MESSAGE" | ssh "$IRC_SERVER" -T "cat > $WEECHAT_FIFO"

	if [ $? -eq 0 ]; then
		rm "$SMSFILE"
		echo "$SMSFILE sent successfully." >&2
	else
		echo "$SMSFILE failed to send!" >&2
	fi
done

# remove the lock
rm -f "$LOCKFILE"

exit 0
-- 
2.34.1
Hey,

Do you mind improving the readme for this patch? The scripts seem useful
but it's not organized well. Either create a new heading for each script
in the readme or make one folder and document it all in the readme.

Thanks,
Anjandev Momi
-- 
w:] www.momi.ca
pgp:] https://momi.ca/publickey.txt