~aleteoryx/ntalk

84f78582a02ac08869486c436e173aa1ff8fa860 — Aleteoryx 2 months ago
ntalk START!
3 files changed, 70 insertions(+), 0 deletions(-)

A COPYING
A README.md
A ntalk.sh
A  => COPYING +13 -0
@@ 1,13 @@
Copyright (C) 2025 by Aleteoryx <alyx@aleteoryx.me>

Permission to use, copy, modify, and/or distribute this software for
any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

A  => README.md +13 -0
@@ 1,13 @@
# ntalk

[nanotalk][src] utils

## `ntalk.sh`: simple nanotalk server

meant to be run under a connection broker like socat.

probably there's a chance of the file being clobbered.
will get slower with more scrollback, cause it's just
implemented with tail and head and wc. :P

[src]: https://git.phial.org/d6/nanochat

A  => ntalk.sh +44 -0
@@ 1,44 @@
#!/bin/sh

[ $# -eq 1 ] || {
	echo 1>&2 "usage: socat TCP-LISTEN:44322,fork EXEC:'$0 <DBFILE>'"
	exit -1
}

DBFILE="$1"
touch $DBFILE

numlines() { wc -l <"$DBFILE"; }

while read line; do
	case "$line" in
	(SEND\ *)
		echo "${line##SEND\ }" >>"$DBFILE"
		echo $(numlines) ;;
	(SKIP\ *)
		num="${line##SKIP\ }"
		len=$(numlines)
		slen=$((len - num))
		slen=$((slen < 0 ? 0 : slen))
		echo "$slen"
		head -n "$len" <"$DBFILE" | tail -n "$slen"
		echo "$len" ;;
	(LAST\ *)
		num="${line##LAST\ }"
		len=$(numlines)
		slen=$((len > num ? num : len))
		echo "$slen"
		head -n "$len" <"$DBFILE" | tail -n "$slen"
		echo "$len" ;;
	(POLL\ *)
		len=$(($(numlines) - ${line##POLL\ }))
		echo $((len < 0 ? 0 : len)) ;;
	(HIST)
		len=$(numlines)
		echo "$len"
		head -n "$len" <"$DBFILE"
		echo "$len" ;;
	(QUIT)
		exit 0 ;;
	esac
done