From faf9bfc48db9a9b0c360e68f42de67003b35f1ea Mon Sep 17 00:00:00 2001 From: Miles Alan Date: Wed, 5 Aug 2020 22:11:58 -0500 Subject: [PATCH] Refactor notifications work to use 1 interface for writing notifs & use dates Summary of changes: 1. Always use the sxmo_notificationwrite.sh script to write notifications rather then sometimes using echo to the DIR 2. Reorder args for notiffile spec to be action as 1st line, watchfile 2nd, and rest as msg; this way msg can be multiple lines without propogating bugs 3. Write calls as a standard notification via sxmo_notificationwrite.sh 4. Use datestamps from notification files instead of filename, this way (3) works without extra logic 5. Various style fixes --- scripts/core/sxmo_appmenu.sh | 31 +++++----- scripts/core/sxmo_notificationmonitor.sh | 79 ++++++++++++++---------- scripts/core/sxmo_notifications.sh | 16 ----- scripts/core/sxmo_notificationsmenu.sh | 32 ++++++++++ scripts/core/sxmo_notificationwrite.sh | 32 ++++++---- scripts/core/sxmo_xinit.sh | 2 +- scripts/modem/sxmo_modemmonitor.sh | 23 ++++--- 7 files changed, 130 insertions(+), 85 deletions(-) delete mode 100755 scripts/core/sxmo_notifications.sh create mode 100755 scripts/core/sxmo_notificationsmenu.sh diff --git a/scripts/core/sxmo_appmenu.sh b/scripts/core/sxmo_appmenu.sh index 912fbdf..fd40ba8 100755 --- a/scripts/core/sxmo_appmenu.sh +++ b/scripts/core/sxmo_appmenu.sh @@ -340,26 +340,25 @@ getprogchoices() { # E.g. sets CHOICES var programchoicesinit "$@" - # Decorate menu at top w/ notifications if they exist - NOTIFICATIONS="$(find "$NOTIFDIR"/* -type f | grep -vc "sxmo_incomingcall" || echo 0)" - echo "$NOTIFICATIONS" | grep -v 0 && + # Decorate menu at top *always* w/ incoming call entry if present + if [ -e "$NOTIFDIR"/incomingcall ]; then + INCOMINGCALLMSG="$(tail -n+3 "$NOTIFDIR"/incomingcall)" + INCOMINGCALLPICKUPACTION="$(head -n1 "$NOTIFDIR"/incomingcall)" CHOICES=" - Notifications ($(echo "$NOTIFICATIONS" | cut -d " " -f1)) ^ 0 ^ sxmo_notifications.sh + $INCOMINGCALLMSG ^ sh -c "rm "$NOTIFDIR"/incomingcall; "$INCOMINGCALLPICKUPACTION"" $CHOICES " + fi - # Decorate menu at top w/ incoming call entry if present - INCOMINGCALL="$(cat "$NOTIFDIR"/sxmo_incomingcall || echo NOCALL)" - if echo "$INCOMINGCALL" | grep -v NOCALL; then - CALLID="$(echo "$INCOMINGCALL" | cut -d: -f1)" - CALLNUM="$(echo "$INCOMINGCALL" | cut -d: -f2)" - CALLCONTACT="$(sxmo_contacts.sh | grep -v "Unknown Number" | grep -m1 "$CALLNUM" | cut -d: -f2)" - CHOICES=" - Pickup: $( - [ -n "$CALLCONTACT" ] && echo "$CALLCONTACT" || echo "$CALLNUM" - ) ^ 0 ^ sxmo_modemcall.sh pickup $CALLID - $CHOICES - " + # For the Sys menu decorate at top with notifications if >1 notification + if [ "$WINNAME" = "Sys" ]; then + NNOTIFICATIONS="$(find "$NOTIFDIR" -type f | wc -l)" + if [ "$NNOTIFICATIONS" -gt 0 ]; then + CHOICES=" + Notifications ($NNOTIFICATIONS) ^ 0 ^ sxmo_notificationsmenu.sh + $CHOICES + " + fi fi # Decorate menu at bottom w/ system menu entry if not system menu diff --git a/scripts/core/sxmo_notificationmonitor.sh b/scripts/core/sxmo_notificationmonitor.sh index 175a87d..a876efe 100755 --- a/scripts/core/sxmo_notificationmonitor.sh +++ b/scripts/core/sxmo_notificationmonitor.sh @@ -1,48 +1,65 @@ #!/usr/bin/env sh - -# This script should be run to initialize the notification watchers. - NOTIFDIR="$XDG_CONFIG_HOME"/sxmo/notifications -handlecreation(){ - sxmo_setpineled green 1; - echo "$1" | grep "sxmo_incomingcall" || - { +notificationhook() { + if [ -x "$XDG_CONFIG_HOME"/sxmo/hooks/notification ]; then + "$XDG_CONFIG_HOME"/sxmo/hooks/notification + else sxmo_vibratepine 200; sleep 0.1; sxmo_vibratepine 200; sleep 0.1; - } & + fi +} - # Dunstify / start notification watcher if it matches the sxmo_notificationwrite format - grep -c . "$1" | grep 3 && - { - inotifywait "$(tail -1 "$1")" && rm -f "$1" & +handlenewnotiffile(){ + NOTIFFILE="$1" - DUNST_RETURN="$(dunstify --action="2,open" "$(head -1 "$1" | cut -c1-70)")"; - # shellcheck disable=SC2091 - echo "$DUNST_RETURN" | grep -v 2 || { $(head -2 "$1" | tail -1)& } - } + if [ "$(wc -l "$NOTIFFILE" | cut -d' ' -f1)" -lt 3 ]; then + echo "Invalid notification file $NOTIFFILE found (<3 lines -- see notif spec in sxmo_notifwrite.sh), deleting!" >&2 + rm "$NOTIFFILE" + else + sxmo_setpineled green 1; + notificationhook & + NOTIFACTION="$(awk NR==1 "$NOTIFFILE")" + NOTIFWATCHFILE="$(awk NR==2 "$NOTIFFILE")" + NOTIFMSG="$(tail -n+3 "$NOTIFFILE" | cut -c1-70)" + + if dunstify --action="2,open" "$NOTIFMSG" | grep 2; then + eval "$NOTIFACTION" + else + inotifywait "$NOTIFWATCHFILE" && rm -f "$NOTIFFILE" & + fi + fi } -sxmo_setpineled green 0 -for NOTIF in "$NOTIFDIR"/*; do - [ -f "$NOTIF" ] || continue - handlecreation "$NOTIF" -done - -while true; do - { - DIREVENT="$(inotifywait -e create,moved_to,delete,delete_self,moved_from "$NOTIFDIR"/)" - case "$(echo "$DIREVENT" | cut -d" " -f2)" in +recreateexistingnotifs() { + for NOTIF in "$NOTIFDIR"/*; do + [ -f "$NOTIF" ] || continue + handlenewnotiffile "$NOTIF" + done +} + +monitorforaddordelnotifs() { + while true; do + INOTIFYOUTPUT="$( + inotifywait -e create,moved_to,delete,delete_self,moved_from "$NOTIFDIR"/ + )" + INOTIFYEVENTTYPE="$(echo "$INOTIFYOUTPUT" | cut -d" " -f2)" + + case "$INOTIFYEVENTTYPE" in "CREATE"|"MOVED_TO") - NOTIFFILE="$NOTIFDIR/$(echo "$DIREVENT" | cut -d" " -f3)" - handlecreation "$NOTIFFILE" + NOTIFFILE="$NOTIFDIR/$(echo "$INOTIFYOUTPUT" | cut -d" " -f3)" + handlenewnotiffile "$NOTIFFILE" ;; - "DELETE"|"DELETE_SELF"|"MOVED_FROM") + # E.g. if no more notifications unset LED find "$NOTIFDIR"/ -type f -mindepth 1 | read -r || sxmo_setpineled green 0 ;; esac - } -done + done +} + +sxmo_setpineled green 0 +recreateexistingnotifs +monitorforaddordelnotifs diff --git a/scripts/core/sxmo_notifications.sh b/scripts/core/sxmo_notifications.sh deleted file mode 100755 index 7ce9eb3..0000000 --- a/scripts/core/sxmo_notifications.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env sh - -NOTIFDIR="$XDG_CONFIG_HOME"/sxmo/notifications - -FILES="$(find "$NOTIFDIR"/ -type f -not -name 'sxmo_incomingcall')" -for FILE in $FILES; do - CHOICES="$(printf %b "$FILE\t$(echo "$FILE" | cut -d: -f4-6) $(head -1 "$FILE")\n$CHOICES")" -done -PICKED="$(printf %b "$CHOICES\nClose Menu" | cut -f2 | dmenu -c -i -fn "Terminus-18" -p "Notifs" -l 10)" - -echo "$PICKED" | grep "Close Menu" && exit 0 -TIMESTAMP="$(echo "$PICKED" | cut -d" " -f1 | cut -d: -f4-6)" -FILE="$(printf %b "$CHOICES" | grep "$PICKED" | cut -f1 | grep "$TIMESTAMP")" - -# shellcheck disable=SC2091 -$(head -2 "$FILE" | tail -1) diff --git a/scripts/core/sxmo_notificationsmenu.sh b/scripts/core/sxmo_notificationsmenu.sh new file mode 100755 index 0000000..722030b --- /dev/null +++ b/scripts/core/sxmo_notificationsmenu.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env sh +NOTIFDIR="$XDG_CONFIG_HOME"/sxmo/notifications + +notificationmenu() { + CHOICES="Close Menu" + for NOTIFFILE in "$NOTIFDIR"/*; do + NOTIFMSG="$(tail -n+3 "$NOTIFFILE" | tr "\n^" " ")" + NOTIFHRANDMIN="$(stat --printf %y "$NOTIFFILE" | grep -oE '[0-9]{2}:[0-9]{2}')" + CHOICES=" + $CHOICES + $NOTIFHRANDMIN - $NOTIFMSG ^ $NOTIFFILE + " + done + + PICKEDCONTENT="$( + printf "%b" "$CHOICES" | + xargs -0 echo | + sed '/^[[:space:]]*$/d' | + awk '{$1=$1};1' | + cut -d^ -f1 | + dmenu -c -i -fn "Terminus-18" -p "Notifs" -l 10 + )" + + [ "$PICKEDCONTENT" = "Close Menu" ] && exit 1 + + PICKEDNOTIFFILE="$(echo "$CHOICES" | grep "$PICKEDCONTENT" | cut -d^ -f2 | tr -d ' ')" + NOTIFACTION="$(head -n1 "$PICKEDNOTIFFILE")" + eval "$NOTIFACTION" & + rm "$PICKEDNOTIFFILE" +} + +notificationmenu diff --git a/scripts/core/sxmo_notificationwrite.sh b/scripts/core/sxmo_notificationwrite.sh index f301f9f..ba6b8af 100755 --- a/scripts/core/sxmo_notificationwrite.sh +++ b/scripts/core/sxmo_notificationwrite.sh @@ -1,15 +1,25 @@ #!/usr/bin/env sh - -# This script takes 3 arguments, (1) a fuzzy description of the notification, (2) the action that the notification invokes upon selecting, and (3) the file to watch for deactivation. -# A notification file has 3 different fields, (1) a fuzzy description, (2) the selection action, and (3) the watch file. - NOTIFDIR="$XDG_CONFIG_HOME"/sxmo/notifications -mkdir -p "$NOTIFDIR" -echo "$3" | grep -v . && { echo "Not enough args."; exit 2; } +# Takes 4 args: +# (1) the filepath of the notification to write (or random to generate a random id) +# (2) action notification invokes upon selecting +# (3) the file to watch for deactivation. +# (4) description of notification +NOTIFFILEPATHTOWRITE="$1" +ACTION="$2" +WATCHFILE="$3" +NOTIFMSG="$4" + +writenotification() { + lsof | grep "$WATCHFILE" && exit 0 # Already viewing watchfile, nops + mkdir -p "$NOTIFDIR" + if [ "$NOTIFFILEPATHTOWRITE" = "random" ]; then + NOTIFFILEPATHTOWRITE="$NOTIFDIR/$(tr -dc 'a-zA-Z0-9' < /dev/urandom | head -c 10)" + fi + rm -f "$NOTIFFILEPATHTOWRITE" + printf %b "$ACTION\n$WATCHFILE\n$NOTIFMSG\n" > "$NOTIFFILEPATHTOWRITE" +} -# Don't send a notification if we're already looking at it! -lsof | grep "$3" && exit 0 - -OUTFILE=$NOTIFDIR/$(date "+%Y:%m:%d:%H:%M:%S:%N") -printf %b "$1\n$2\n$3\n" > "$OUTFILE" +[ "$#" -lt 4 ] && echo "Need >=4 args to create a notification" && exit 1 +writenotification \ No newline at end of file diff --git a/scripts/core/sxmo_xinit.sh b/scripts/core/sxmo_xinit.sh index 9becdb1..d232fc4 100755 --- a/scripts/core/sxmo_xinit.sh +++ b/scripts/core/sxmo_xinit.sh @@ -43,7 +43,7 @@ daemons() { daemonsneedingdbus() { dunst -conf /usr/share/sxmo/appcfg/dunst.conf & - sxmo_notificationmonitor.sh & + #sxmo_notificationmonitor.sh & sxmo_lisgdstart.sh & } diff --git a/scripts/modem/sxmo_modemmonitor.sh b/scripts/modem/sxmo_modemmonitor.sh index 0ca91c0..5bf602d 100755 --- a/scripts/modem/sxmo_modemmonitor.sh +++ b/scripts/modem/sxmo_modemmonitor.sh @@ -26,11 +26,7 @@ checkforincomingcalls() { grep -Eo '[0-9]+ incoming \(ringing-in\)' | grep -Eo '[0-9]+' )" - - if echo "$VOICECALLID" | grep -v .; then - rm -f "$NOTIFDIR/sxmo_incomingcall" - return - fi + echo "$VOICECALLID" | grep -v . && rm -f "$NOTIFDIR/incomingcall" && return if [ -x "$XDG_CONFIG_HOME/sxmo/hooks/ring" ]; then "$XDG_CONFIG_HOME/sxmo/hooks/ring" @@ -61,7 +57,13 @@ checkforincomingcalls() { TIME="$(date --iso-8601=seconds)" mkdir -p "$LOGDIR" printf %b "$TIME\tcall_ring\t$INCOMINGNUMBER\n" >> "$LOGDIR/modemlog.tsv" - printf %b "$VOICECALLID:$INCOMINGNUMBER\n" > "$NOTIFDIR/sxmo_incomingcall" + + sxmo_notificationwrite.sh \ + "$NOTIFDIR/incomingcall" \ + "sxmo_modemcall.sh pickup $VOICECALLID" \ + "$NOTIFDIR/incomingcall" \ + "Pickup $(sxmo_contacts.sh | grep -E "^$INCOMINGNUMBER")" & + echo "Number: $INCOMINGNUMBER (VOICECALLID: $VOICECALLID)" } @@ -75,7 +77,6 @@ checkfornewtexts() { # Loop each textid received and read out the data into appropriate logfile for TEXTID in $TEXTIDS; do - TEXTDATA="$(mmcli -m "$(modem_n)" -s "$TEXTID" -K)" TEXT="$(echo "$TEXTDATA" | grep sms.content.text | sed -E 's/^sms\.content\.text\s+:\s+//')" NUM="$( @@ -90,9 +91,11 @@ checkfornewtexts() { printf %b "$TIME\trecv_txt\t$NUM\t${#TEXT} chars\n" >> "$LOGDIR/modemlog.tsv" mmcli -m "$(modem_n)" --messaging-delete-sms="$TEXTID" - CONTACT="$(sxmo_contacts.sh | grep "$NUM" || echo "$NUM")" - echo "$CONTACT" | grep -c . | grep 1 || CONTACT="$NUM" - ( sxmo_notificationwrite.sh "Message from $CONTACT: $TEXT" "st -e tail -n9999 -f $LOGDIR/$NUM/sms.txt" "$LOGDIR/$NUM/sms.txt" & ) & + sxmo_notificationwrite.sh \ + random \ + "st -e tail -n9999 -f $LOGDIR/$NUM/sms.txt" \ + "$LOGDIR/$NUM/sms.txt" \ + "Message from $(sxmo_contacts.sh | grep -E "^$NUM:"): $TEXT" & done }