shithub: gridchat

Download patch

ref: 1484fc83f17c4b303eb7620479f7f87f781779fb
author: Kelly Glenn <[email protected]>
date: Sun Nov 10 23:38:12 EST 2024

initial commit

--- /dev/null
+++ b/LABEL
@@ -1,0 +1,6 @@
+#!/bin/rc
+# LABEL - persistent label
+# usage: LABEL name
+
+label $*
+bind /dev/null /dev/label
--- /dev/null
+++ b/chat
@@ -1,0 +1,48 @@
+#!/bin/rc
+# chat - a gridchat client
+# usage: chat
+# requires:
+#	- chat-fmt
+#	- mq (https://shithub.us/ori/mq/HEAD/info.html)
+#	- newsrv
+#	- LABEL
+
+if(~ $#nick 1)
+	user=$nick
+
+fn chatg{
+	label chatg
+	grep -i '[^a-z0-9]'$user'([^a-z0-9]|$)' /n/chatbuf/chat | grep -v '^'$user' →' | chat-fmt
+}
+
+fn chati{
+	label chati
+	while(){
+		echo -n '→ ' >> /dev/text
+		read | sed '1s/^/'$user' → /' >>/n/chat/chat
+	}
+}
+
+fn chatv{
+	label chatv
+	</n/chatbuf/chat chat-fmt
+}
+
+fn chatstart{
+	srv tcp!chat.tcp80.org!9990 chat.$pid
+	mount /srv/chat.$pid /n/chat tail:0
+
+	dims=`{read -c60 /dev/window}
+	height=`{echo $dims(5) - $dims(3) | hoc}
+	width=`{echo $dims(4) - $dims(2)| hoc}
+	hchat=`{echo $height - 200 | hoc}
+
+	mq -c -m /n/chatbuf -s mq.$pid
+	cat /n/chat/chat > /n/chatbuf/chat &
+	window -m -r 0     0    $width     100 -scroll rc -c chatg
+	window -m -r 0   100    $width  $hchat -scroll rc -c chatv
+	window -m -r 0  $hchat  $width $height -scroll rc -c chati
+}
+
+rfork ne
+window 880,40,1560,860 newsrv 'LABEL gridchat; rio -i ''rc -c chatstart'''
--- /dev/null
+++ b/chat-fmt.c
@@ -1,0 +1,58 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+
+#define LINELEN 80
+
+const Rune sep = L'→';
+
+void fatal(char *s) {
+	sysfatal(s);
+}
+
+int main(void) {
+	Biobuf *i;
+	Biobuf *o;
+	char *line;
+	char *tok;
+	int toklen;
+	int linelen;
+	int j;
+	int f;
+
+	i = Bfdopen(0, OREAD);
+	o = Bfdopen(1, OWRITE);
+	
+	Blethal(i, fatal);
+	Blethal(o, fatal);
+	
+	while((line = Brdstr(i, '\n', 1)) != nil) {
+		f = 1;
+		linelen = 0;
+		tok = line;
+		while((tok = strtok(tok, " ")) != 0) {
+			int nicklen = 0;
+			int toklen = strlen(tok);
+			if (f) {
+				nicklen = toklen;
+				linelen = nicklen + 2;
+				f = 0;
+			}
+			if (linelen + toklen + 1 > LINELEN) {
+				Bputc(o, '\n');
+				for(j = 0; j <= nicklen + 2; ++j, Bwrite(o, " ", 1));
+				linelen = nicklen + 3;
+			}
+			Bwrite(o, tok, toklen);
+			Bwrite(o, " ", 1);
+			linelen += toklen + 1;
+			tok = nil;
+		}
+		Bwrite(o, "\n", 1);
+		Bflush(o);
+	}
+	Bterm(i);
+	Bterm(o);
+
+	return 0;
+}
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,19 @@
+</$objtype/mkfile
+
+BIN=/$objtype/bin
+TARG=chat-fmt
+OFILES=chat-fmt.$O
+
+UPDATE=\
+	mkfile\
+	chat\
+	chat-fmt.c
+
+</sys/src/cmd/mkmany
+
+install:V:
+	for (i in $TARG)
+		mk $MKFLAGS $i.install
+	cp chat /rc/bin/chat
+	cp newsrv /rc/bin/newsrv
+	cp LABEL /rc/bin/LABEL
--- /dev/null
+++ b/newsrv
@@ -1,0 +1,14 @@
+#!/bin/rc -x
+# newsrv - create a new /srv device
+# usage: newsrv command
+
+rfork ens
+
+<[3]/srv/clone{
+	id=`{<[0=3]read}
+	<[3=]@{ # close the clone fd
+		rfork n
+		bind -c /srv/$id /srv
+		rc -c $*
+	}
+}