ref: b1fec2fa9e17ee7e27fcb8bbd85625534296feb8
parent: 8f0f9e003f76427e9a8c3d37deafd5267864c735
author: grobe0ba <grobe0ba@tcp80.org>
date: Tue Aug 2 09:09:56 EDT 2022
Lets do this in C
--- a/chat
+++ b/chat
@@ -38,6 +38,9 @@
case -j
channel = $2
shift 2
+ case -s
+ chatsrv = $2
+ shift 2
case *
echo bad flag $1 '-n NICK -j CHANNEL'
shift
@@ -45,9 +48,9 @@
}
if(! ~ $1 '')
chatsrv=$1
-if(! test -e /n/$chatsrv/chat)
+if(! test -e /n/$chatsrv/ctl)
mount /srv/$chatsrv /n/$chatsrv
-if(! test -e /n/$chatsrv/chat){
+if(! test -e /n/$chatsrv/ctl){
echo 'no chat service named $chatsrv found' >[1=2]
exit no.chat
}
@@ -61,13 +64,7 @@
}
echo JOIN $name to $channel >>/n/$chatsrv/$channel
-fn docat {
- cat /n/$chatsrv/$channel | while(line=`{read}) {
- echo $line | fmt | chat-post-fmt
- }
-}
-
-docat &
+chat-fmt </n/$chatsrv/$channel &
catkill=$apid
while(talk=`{read}){
@@ -84,10 +81,10 @@
case /j
if (~ $talk(2) '')
echo must specify a channel to join
- if not if(test -e /n/chat/$talk(2)){
+ if not if(test -e /n/$chatsrv/$talk(2)){
close
channel=$talk(2)
- cat /n/chat/$channel &
+ cat /n/$chatsrv/$channel &
catkill=$apid
echo JOIN $name to $channel >>/n/$chatsrv/$channel
}
@@ -108,7 +105,7 @@
# ts=`{echo '('^$ts(4)^')'}
if(! ~ $talk ''){
line=`{echo $name '→ ' $talk}
- echo $line >>/n/chat/$channel
+ echo $line >>/n/$chatsrv/$channel
}
}
}
--- /dev/null
+++ b/chat-fmt.c
@@ -1,0 +1,60 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <stdio.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;
+ char *idx;
+ int nicklen;
+ 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;
+ toklen = 0;
+ while((tok = strtok(tok, " ")) != 0) {
+ 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);
+ }
+ Bterm(i);
+ Bterm(o);
+
+ return 0;
+}
--- a/chat-post-fmt
+++ /dev/null
@@ -1,19 +1,0 @@
-#!/bin/awk -f
-
-BEGIN { FIRST=1 }
-
-/^.* → / {
- FS="→"
- NICK=$1
- NICKLEN=length($1)+2
- NICKFMT="%"NICKLEN"s %s\n"
-}
-
-{
- if(FIRST) {
- print $0
- FIRST=0
- } else {
- printf(NICKFMT, "", $0)
- }
-}
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,23 @@
+</$objtype/mkfile
+
+BIN=$home/bin/$objtype
+
+TARG=chat-fmt
+
+OFILES=chat-fmt.$O
+
+BIN=$home/bin/$objtype
+
+UPDATE=\
+ mkfile\
+ chat\
+ chat-fmt.c
+
+</sys/src/cmd/mkmany
+
+install:V:
+ for (i in $TARG)
+ mk $MKFLAGS $i.install
+ cp chat $home/bin/rc/chat
+
+