#!/bin/sh # SPDX-License-Identifier: AGPL-3.0-only # Copyright 2022 Sxmo Contributors # include common definitions # shellcheck source=scripts/core/sxmo_common.sh . sxmo_common.sh # shellcheck source=configs/default_hooks/sxmo_hook_icons.sh . sxmo_hook_icons.sh SXMO_NOTIFDIR="${SXMO_NOTIFDIR:-${XDG_DATA_HOME:-$HOME/.local/share}/sxmo/notifications}" [ -z "$SXMO_NOTIFDIR" ] || [ "$SXMO_NOTIFDIR" = "/" ] && SXMO_NOTIFDIR="$HOME/.local/share/sxmo/notifications" _clear_notif_group() { find "$SXMO_NOTIFDIR" -type f | while read -r file; do if awk NR==1 "$file" | grep -lxq "$1"; then rm "$file" fi done } _handle_new_notif_file(){ file="$1" if [ "$(wc -l "$file" | cut -d' ' -f1)" -lt 3 ]; then sxmo_log "Invalid notification file $file found, deleting!" rm -f "$file" return fi sxmo_hook_notification.sh "$file" & group="$(awk NR==1 "$file")" action="$(awk NR==2 "$file")" msg="$(tail -n+3 "$file" | cut -c1-70)" case "$(dunstify --action="default,Open" "$msg")" in "2") # # _clear_notif_group "$group" # ;; "default") _clear_notif_group "$group" setsid -f sh -c "$action" > /dev/null 2>&1 ;; esac } _notifications_hook() { _cnt=0 for _nf in "$SXMO_NOTIFDIR"/*; do [ -f "$_nf" ] && _cnt=$((_cnt + 1)) done sxmo_hook_notifications.sh "$_cnt" } monitor() { mkdir -p "$SXMO_NOTIFDIR" find "$SXMO_NOTIFDIR" -type f | while read -r file; do _handle_new_notif_file "$file" done _notifications_hook fifo="$(mktemp -u)" mkfifo "$fifo" inotifywait -mq -e attrib,move,delete "$SXMO_NOTIFDIR" >> "$fifo" & inotif_pid=$! finish() { kill "$inotif_pid" rm "$fifo" exit } trap 'finish' TERM INT EXIT while read -r notif_folder notif_type notif_file; do if echo "$notif_type" | grep -qE "CREATE|MOVED_TO|ATTRIB"; then _handle_new_notif_file "$notif_folder$notif_file" fi _notifications_hook done < "$fifo" wait "$inotif_pid" } _menu_lines() { find "$SXMO_NOTIFDIR" -type f | while read -r file; do msg="$(tail -n+3 "$file" | tr "\n^" " ")" hrandmin="$(stat --printf %y "$file" | grep -oE '[0-9]{2}:[0-9]{2}')" cat < /dev/null 2>&1 ;; esac } new() { while : ; do case "$1" in -i) id="$2" shift 2 ;; -g) group="$2" shift 2 ;; *) break ;; esac done if [ "$#" -lt 2 ]; then usage exit 1 fi if [ -z "$id" ]; then id="$(tr -dc 'a-zA-Z0-9' < /dev/urandom 2>/dev/null | head -c 10)" fi if [ -z "$group" ]; then group="$(tr -dc 'a-zA-Z0-9' < /dev/urandom 2>/dev/null | head -c 10)" fi action="$1" msg="$2" touch "$SXMO_NOTIFDIR/$id" printf "%s\n%s\n%b\n" "$group" "$action" "$msg" > "$SXMO_NOTIFDIR/$id" sxmo_hook_statusbar.sh notifications } usage() { cat < Register a new notification EOF } cmd="$1" shift case "$cmd" in monitor) monitor "$@";; menu) menu "$@";; new) new "$@";; *) usage exit 1 ;; esac