~aleteoryx/ntalk

7eec9cbb83919be6714ee36af2307097aea93db5 — Aleteoryx a month ago b756b2c
mildly friendlier dee, ntalk.sh STAT support
2 files changed, 32 insertions(+), 5 deletions(-)

M dee.py
M ntalk.sh
M dee.py => dee.py +8 -2
@@ 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:

M ntalk.sh => ntalk.sh +24 -3
@@ 2,15 2,29 @@

[ $# -eq 1 ] || {
	echo 1>&2 "usage: socat TCP-LISTEN:44322,fork EXEC:'$0 <DBFILE>'"
	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/$$"