From 7eec9cbb83919be6714ee36af2307097aea93db5 Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Sat, 1 Nov 2025 12:58:16 -0400 Subject: [PATCH] mildly friendlier dee, ntalk.sh STAT support --- dee.py | 10 ++++++++-- ntalk.sh | 27 ++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/dee.py b/dee.py index b02ea11d2e7409207bb5f86f458b868d8299ff71..23f7f5cef6e5ae45b97f07426556c91f8b81239b 100755 --- a/dee.py +++ b/dee.py @@ -82,6 +82,7 @@ class DiceParseException(Exception): def parse_number(word): return int(word) + ''' dice = 'then'? spec offset? @@ -135,7 +136,10 @@ def parse_dice(nick, words): raise DiceParseException(f'roll {count} what?') if d := re.match('d([0-9]+|[A-Za-z]+)', words[0]): - last_dice = parse_number(d.group(1)) + try: + last_dice = parse_number(d.group(1)) + except (IndexError, ValueError): + raise DiceParseException('I don\'t have any dice like that!') words.pop(0) if len(words) > 0 and words[0] == 'more': words.pop(0) @@ -162,6 +166,8 @@ def parse_dice(nick, words): raise DiceParseException(f'I don\'t know how to roll that!') def dice(nick, words, pwords): + global MAN + result = nick terms = botlib.split_list(words, pwords) @@ -170,7 +176,7 @@ def dice(nick, words, pwords): try: count, size, offset = parse_dice(nick, term) except DiceParseException as e: - send(f'{nick}, {e.args[0]}') + send(f'{nick}, {e.args[0]} See {MAN} for syntax.') return if count == 0: diff --git a/ntalk.sh b/ntalk.sh index 2e62d31a67b416fb0a89656b29cf12d1d2796f3d..207d82be6c546232600e4d410fe9ec21007497d9 100755 --- a/ntalk.sh +++ b/ntalk.sh @@ -2,15 +2,29 @@ [ $# -eq 1 ] || { echo 1>&2 "usage: socat TCP-LISTEN:44322,fork EXEC:'$0 '" - exit -1 + exit 1 } DBFILE="$1" -touch $DBFILE +touch "$DBFILE" +mkdir -p "$DBFILE.conn" +touch "$DBFILE.conn/$$" numlines() { wc -l <"$DBFILE"; } +numbytes() { wc -c <"$DBFILE"; } +numconns() { ls "$DBFILE.conn/" | wc -l; } + +cleanup() { + cd "$DBFILE.conn/" + for i in *; do + [ -e "/proc/$i" ] || rm "$i"; + done + cd - >/dev/null +} while read line; do + [ $((RANDOM % 64)) -eq 0 ] && cleanup + case "$line" in (SEND\ *) echo "${line##SEND\ }" >>"$DBFILE" @@ -21,7 +35,7 @@ while read line; do slen=$((len - num)) slen=$((slen < 0 ? 0 : slen)) echo "$slen" - head -n "$len" <"$DBFILE" | tail -n "$slen" + head -n "$len" <"$DBFILE" 2>/dev/null | tail -n "$slen" echo "$len" ;; (LAST\ *) num="${line##LAST\ }" @@ -38,7 +52,14 @@ while read line; do echo "$len" head -n "$len" <"$DBFILE" echo "$len" ;; + (STAT) + echo "$(numlines) messages" + echo "$(numbytes) bytes" + echo "$(numconns) clients" ;; (QUIT) + rm "$DBFILE.conn/$$" exit 0 ;; esac done + +rm "$DBFILE.conn/$$"