From e4e42ebb8f4ab0591b44817ddd56cecebd094153 Mon Sep 17 00:00:00 2001 From: Jaidyn Ann <10477760+JadedCtrl@users.noreply.github.com> Date: Mon, 29 May 2023 12:15:13 -0500 Subject: [PATCH] Move utility scripts from irc-chatdir to here --- bin/README.md | 25 +++++++++ bin/chat_file_notify.sh | 60 +++++++++++++++++++++ bin/chatdir-in.sh | 113 ++++++++++++++++++++++++++++++++++++++++ bin/proc_notify.sh | 45 ++++++++++++++++ 4 files changed, 243 insertions(+) create mode 100644 bin/README.md create mode 100755 bin/chat_file_notify.sh create mode 100755 bin/chatdir-in.sh create mode 100755 bin/proc_notify.sh diff --git a/bin/README.md b/bin/README.md new file mode 100644 index 0000000..80f786a --- /dev/null +++ b/bin/README.md @@ -0,0 +1,25 @@ +# utils + +Some simple scripts that can ease the use of chatdir clients. + +## chatdir-in.sh +Messages are sent, in chatdir, via .in/ directories under each channel's dir. +The file can be named whatever, it just has to be a text-file, and it'll be +sent. But `$ echo "Hi there!" > #general/.in/msg` is annoying. + +This is a wrapper around that. It will send whatever you type to the selected +channel; selecting a channel is done by just typing a channel name verbatim. +There is auto-completion for both nicks and channel-names. + +You might run it like, `$ chatdir-in.sh ~/chat/libera.chat/` + + +## irc_file_notify.sh +Sends a pretty notification (with icon, sender, and message text!) whenever a +message is sent. You might run like, `$ irc_file_notify.sh ~/chat/libera.chat/*` + + +## proc_notify.sh +Displays a warning notifcation when irc-chatdir dies. +You might run like, `$ proc_notify.sh irc-chatd.scm.*leagueh` + diff --git a/bin/chat_file_notify.sh b/bin/chat_file_notify.sh new file mode 100755 index 0000000..4f56413 --- /dev/null +++ b/bin/chat_file_notify.sh @@ -0,0 +1,60 @@ +#!/bin/sh +#――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― +# Name: chat_file_notify +# Desc: Watches a directory for any newly-created files, and then sends a +# notification! +# Reqs: inotifywait, notify-send, shell +# Date: 2022 +# Lisc: CC0; jadedctrl +#――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― + +notification_title() { + local file="$1" + local sender="$(attr -qg chat.sender "$file")" + local channel="$(basename "$(dirname "$file")")" + + echo "$sender [$channel]" +} + + +notification_message() { + local file="$1" + local message="$(head -1 "$file")" + + echo "$message" +} + + +monitor_dirs() { + inotifywait --format "%w%f" \ + --event MOVED_TO,CREATE \ + $@ +} + + +TARGET="$1" + +if test -z "$IRC_ICON"; then + # Set this variable, or you'll get my preferred icon. Suffer! c:< + IRC_ICON="$XDG_DATA_HOME/icons/retrosmart-icon-theme/scalable/chat.svg" +fi + + +if test -z "$TARGET"; then + echo "usage: $(basename "$0") CHAT_DIRECTORY" + echo "" + echo "Monitors a specific channel's directory for any new messages, sending" + echo "a notification when one is receieved." + exit 2 +fi + + +while test 1 -eq 1; do + FILE="$(monitor_dirs $@)" + notify-send \ + --app-name="$(basename "$0")" \ + --icon "$IRC_ICON" \ + "$(notification_title "$FILE")" \ + "$(notification_message "$FILE")" + +done diff --git a/bin/chatdir-in.sh b/bin/chatdir-in.sh new file mode 100755 index 0000000..63a2972 --- /dev/null +++ b/bin/chatdir-in.sh @@ -0,0 +1,113 @@ +#!/bin/sh +#――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― +# Name: chatdir-in +# Desc: A simple wrapper for sending messages in a chatdir, w autocomplete +# and all that sweet stuff. +# Reqs: rwlrap, shell +# Date: 2023-02-14 +# Lisc: CC0; jadedctrl +#――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― + +# Create the auto-complete file used by rlwrap, containing users anc channel-names +prep_auto_complete() { + local file="$1" + + ls "$CHATDIR" \ + > "$file" + + if test -n "$CHANNEL"; then + ls "$CHATDIR/$CHANNEL/.users/online/" \ + >> "$file" + fi +} + + +# Take in user-input with the oh-so-pretty rlwrap +# seriously, <3<3,3 rlwrap I love you marry me +# have my babies rlwrap! have my babies! +# we'll live in a secluded cottage, with nary a +# bad piece of code. it'll be a very sugary life, +# my dearest rlwrap. just you and I in the world, +# no-one else +get_input() { + prep_auto_complete "$AUTO_COMPLETE_FILE" + + rlwrap --one-shot \ + --substitute-prompt='» ' \ + --break-chars "$(printf '\n')" \ + --extra-char-after-completion='' \ + --file "$AUTO_COMPLETE_FILE" \ + cat +} + + +# Find out if the user is selecting a channel, or sending a message; act appropriately. +parse_input() { + local input="$1" + + if test -d "$CHATDIR/$input" -a -d "$CHATDIR/$input/.in/"; then + set_channel "$input" + elif test -n "$input"; then + send_message "$input" + fi +} + + +# Send a message to the selected channel, and echo it +send_message() { + local message="$1" + + if test -z "$CHANNEL"; then + echo "No channel set; type in its name verbatim, first." + else + echo "$message" \ + > "$CHATDIR/$CHANNEL/.in/input" + echo "$CHATDIR/$CHANNEL ← $message" + fi +} + + +# Set the selected channel, and brag about it +set_channel() { + local channel="$1" + + if test -d "$CHATDIR/$channel" -a -d "$CHATDIR/$channel/.in/"; then + CHANNEL="$channel" + echo "— $CHANNEL —" + fi +} + + +help() { + echo "usage: $(basename "$0") CHATD_DIR" + echo "" + echo "Chatdir daemons send plain-text files placed in .in/ directories as messages." + echo "This script is a wrapper around that: Just type in a message and hit ENTER" + echo "to send it to the currently selected channel. To select a channel, type in" + echo "a line solely containing the verbatim channel name." + echo "TAB-completion is supported, for both channel-names and for online users." + echo "rlwrap is used for input." + exit 2 +} + + +if test ! -d "$1"; then + help +fi + +echo "~~ $(basename $0) ~~" +echo "No channel selected." +echo "Select a channel by typing in its name verbatim!" + +CHATDIR="$1" +CHANNEL="" +AUTO_COMPLETE_FILE="$(mktemp --suffix=.chatd-in)" + + +INPUT="" +while INPUT="$(get_input)"; do + parse_input "$INPUT" +done + + +rm "$AUTO_COMPLETE_FILE" diff --git a/bin/proc_notify.sh b/bin/proc_notify.sh new file mode 100755 index 0000000..aab86c9 --- /dev/null +++ b/bin/proc_notify.sh @@ -0,0 +1,45 @@ +#!/bin/sh +#――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― +# Name: proc_notify +# Desc: Bad script that checks over and over and over if a program is running, +# by greping `ps ax` w the passed arg. If the program isn't running, +# send an URGENT notification, telling the user. +# Reqs: notify-send, shell +# Date: 2023 +# Lisc: CC0; jadedctrl +#――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― + +watch_loop() { + local program="$1" + local us="$2" + local last_id="$3" + if test -z "$last_id"; then + last_id=13375 + fi + + if test -z "$(ps ax | grep "$program" | grep -ve "grep" -e "$us")"; then + notify-send --replace-id="$last_id" \ + --print-id \ + --urgency=critical \ + "$program not running!" + fi + sleep 30 +} + + +PROGRAM="$1" +LAST_ID=13375 + +if test -z "$PROGRAM"; then + echo "usage: $(basename "$0") REGEX" + echo "" + echo "Monitors output of `ps ax` for the expect program, defined by regex." + echo "If said program isn't found, then an URGENT notification will be sent." + exit 2 +fi + +while test "life" = "life"; do + id="$(watch_loop "$PROGRAM" "$0" "$LAST_ID")" + last_id="$id" + echo "$last_id" +done