#!/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 2>/dev/null | while read -r file; do if [ -f "$file" ] && awk NR==1 "$file" 2>/dev/null | grep -lxq "$1"; then rm -f "$file" fi done } clear_all() { rm -f "$SXMO_NOTIFDIR"/* 2>/dev/null || true dunstctl close-all >/dev/null 2>&1 || true dunstctl history-clear >/dev/null 2>&1 || true pkill -x dunstify >/dev/null 2>&1 || true sxmo_hook_statusbar.sh notifications >/dev/null 2>&1 || true } _handle_new_notif_file() { file="$1" [ -f "$file" ] || return 0 if [ "$(wc -l < "$file" 2>/dev/null || echo 0)" -lt 3 ]; then sxmo_log "Invalid notification file $file found, deleting!" rm -f "$file" return 0 fi sxmo_hook_notification.sh "$file" & group="$(awk NR==1 "$file" 2>/dev/null)" action="$(awk NR==2 "$file" 2>/dev/null)" msg="$(tail -n+3 "$file" 2>/dev/null | cut -c1-70)" ( case "$(dunstify --action="default,Open" "$msg" 2>/dev/null)" in "2") ;; "default") _clear_notif_group "$group" sxmo_hook_statusbar.sh notifications >/dev/null 2>&1 || true if [ -n "$action" ] && [ "$action" != "none" ]; then setsid -f sh -c "$action" > /dev/null 2>&1 fi ;; 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 2>/dev/null | while read -r file; do _handle_new_notif_file "$file" done _notifications_hook fifo="$(mktemp -u)" mkfifo "$fifo" inotifywait -mq -e close_write,moved_to,delete "$SXMO_NOTIFDIR" >> "$fifo" & inotif_pid=$! finish() { kill "$inotif_pid" 2>/dev/null || true rm -f "$fifo" exit } trap 'finish' TERM INT EXIT while read -r notif_folder notif_type notif_file; do if echo "$notif_type" | grep -qE "CLOSE_WRITE|MOVED_TO"; then _handle_new_notif_file "$notif_folder$notif_file" fi _notifications_hook done < "$fifo" wait "$inotif_pid" } _menu_lines() { find "$SXMO_NOTIFDIR" -type f 2>/dev/null | while read -r file; do [ -f "$file" ] || continue msg="$(tail -n+3 "$file" 2>/dev/null | tr "\n^" " ")" hrandmin="$(stat -c %y "$file" 2>/dev/null | grep -oE '[0-9]{2}:[0-9]{2}' | head -n1)" cat </dev/null)" action="$(awk NR==2 "$file" 2>/dev/null)" _clear_notif_group "$group" rm -f "$file" 2>/dev/null || true dunstctl close-all >/dev/null 2>&1 || true sxmo_hook_statusbar.sh notifications >/dev/null 2>&1 || true if [ -n "$action" ] && [ "$action" != "none" ]; then setsid -f sh -c "$action" > /dev/null 2>&1 fi fi ;; 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" mkdir -p "$SXMO_NOTIFDIR" 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 2>/dev/null || true case "$cmd" in monitor) monitor "$@" ;; menu) menu "$@" ;; clear) clear_all ;; new) new "$@" ;; *) usage exit 1 ;; esac