(* Crappy ass multinode chat.  Should be totally redone. *)

unit nodechat;
interface
procedure multichat;
implementation

uses dos,crt,io,records,genstr,filehandle,protcomm;

procedure multichat;
var nodemci:mcis;
    done:boolean;
    str:string;
    act:string;

type nodefile=record
     from:String[15];
     msg:string;
     end;

var f:file;
    nf:file;
    lx,ly:byte;
    temp:string;
    last:longint;
         procedure box(a:byte);
         begin
         case a of
         1:swrite(#27'[1;'+strr(nodemci[2].y)+'r');
         2:swrite(#27'[1;25r');
         end;
         end;


procedure addstr(S:string);
begin
if ly=nodemci[2].y then begin
                        box(1);
                        goxy(1,ly);
                        print(s);
                        swriteln('');
                        box(2);
                        end
                        else
                        begin
                        goxy(1,ly);
                        print(s);
                        inc(ly);
                        end;

end;

procedure check;
begin
if last<>filesize(F) then
   begin
   nseek(f,last);
   repeat
      nread(f,str,sizeof(Str));
      addstr(str);
      last:=filesize(F);
   until filepos(f)=filesize(F);
   beep;
   last:=filesize(F);
   end;
end;


Procedure NodeReadLn (Var S : String);
Var Ch : Char;
    x:integer;
Begin
  S := '';
  x:=1;
  Repeat
    if scroller.used then
    repeat
    scrolltext(2);
    until skeypressed;
    if ((Not Carrier) AND (Not Localon)) or (hungup) or (loggedoff) then exit;
    repeat
    check;
    until skeypressed;
    Ch := SReadKey;
    IF X<BUFLEN+1 THEN
    If Ord (Ch) in [32..122] Then
    Begin
      inc(x);
      S := S + Ch;
      If Hide then
       Begin
        if charit.use then
        ansicolor(charit.Back shl 4 + charit.Fore);
        Write(CH);
        If not localon then AWrite('*');
       End else Swrite(CH);
    End;
    If Ord(Ch) = Ord(8) Then
    Begin
      If Length (S) > 0 Then
      Begin
        if charit.use then
        SWrite (#8+charit.ch+#8) else
        SWrite (#8' '#8);
        Delete (S, Length (S), 1);
        dec(x);
      End;
    End;
  check;
  Until (Ord (Ch) = 13) OR ((Not Carrier) AND (Not Localon)) or (hungup) or (loggedoff);
{  SWrite (#13#10);}
End;

begin
node:=1;
nassign(f,config.data_Dir+'NODECHAT.'+strr(node));
done:=false;
FILLCHAR(nodemci,sizeof(nodemci),#0);
nodemci[1].mci:='%NT';
nodemci[2].mci:='%NB';
nodemci[3].mci:='%NX';
printfile(Config.Textfile_Dir+'NODECHAT.ANS',nodemci,False);
lx:=1;
ly:=nodemci[1].y;

last:=filesize(f);
repeat

if skeypressed then
   begin
   box(2);
   goxy(nodemci[3].x,nodemci[3].y);
   buflen:=80;
   swrite(#27+'[K');
   nodereadln(str);
   goxy(nodemci[3].x,nodemci[3].y);
   buflen:=80;
   swrite(#27+'[K');
   goxy(nodemci[1].x,nodemci[1].y);

case str[1] of
'/':begin
   act:=copy(str,2,5);
   if act='QUIT' then done:=true;
   end
   else
   begin
   temp:='|B0|07<'+USER.ALIAS+'>';
   insert(temp,str,1);
   nreset(f);
   nseek(f,filesize(F));
   nwrite(f,str,sizeof(str));
   addstr(Str);
   last:=filesize(F);
   check;
   end;
end;
end;

until (done) or ((Not Carrier) AND (Not Localon)) or (hungup) or (loggedoff);
end;

begin
end.