@name CchatT
@inputs Scr:wirelink In
@persist Channel Name:string CurCMD:string LastChat:string On FloodCheck
@outputs Reset Num:string
interval(100)
Scr:writeString("CchatT Console v1",0,0)
if (first() | changed(Scr)) {
    Reset=1
    timer("rst",10)
}
if (On==0) {
    Scr:writeString("setname [s] | connect [n]",0,1)
    Scr:writeString(CurCMD+" ":repeat(30),0,2)
    if (In==13 & CurCMD:sub(1,"connect":length())=="connect") {
        CurCMD=CurCMD:sub("connect ":length())
        Scr:writeString("Connecting to channel "+(CurCMD:toNumber()),0,2)
        Channel=CurCMD:toNumber()
        CurCMD=""
        timer("connect",1000)
    } elseif (In==13 & CurCMD:sub(1,"setname":length())=="setname") {
        CurCMD=CurCMD:sub("setname  ":length())
        Name=CurCMD
        Scr:writeString("Name set to "+Name,0,17)
    
    }
} else {
    Scr:writeString("Channel "+Channel+", type quit to exit",0,1)
    Table=gTable("Chat"+Channel)
    Table2=gTable("Chatsend"+Channel)
    #16-2
    
    Scr:writeString(CurCMD+" ":repeat(30-CurCMD:length()),0,17,vec(255,255,255),vec(128,128,128))
    if (In==13 & !FloodCheck) {
        if (CurCMD:lower()=="quit") {
            Channel=0
            On=0
            Reset=1
            timer("rst",10)
        } else {
        FloodCheck=1
        timer("noflood",500)
            #send chat message
            Table[Table:count()+1,string]=CurCMD
            Table2[Table2:count()+1,string]=Name
        }
        
    }
    LastChat=Table[Table:count(),string]
    if (changed(LastChat)) {
        for (I=1, 16-2) {
            String="["+Table2[Table2:count()-I+1,string]+"] "+Table[Table:count()-I+1,string]
            if (String:length()>30) {
                String=String:sub(1,30)
            }
            Scr:writeString(String+" ":repeat(30-String:length()),0,16-I)
        }
}

}
if (changed(In)) {
    if (In!=13 & In!=127 & In!=154 & CurCMD:length()<30) {
        CurCMD=CurCMD+toChar(In)
        if (On) {
            Scr:writeString(CurCMD+" ":repeat(30-CurCMD:length()),0,17,vec(255,255,255),vec(128,128,128))
        } else {
        Scr:writeString(CurCMD+" ":repeat(30),0,2)
        
    }
    } elseif (In==127) {
        CurCMD=CurCMD:sub(1,CurCMD:length()-1)
}
if (In==13) {
    CurCMD=""
}
}

if (clk("connect")) {
    Reset=1
    timer("enter",100)
}
if (clk("enter")) {
    Reset=0
    On=1
}

if (clk("rst")) {
    Reset=0
}
if (clk("noflood")) {
    FloodCheck=0
}
Num=toChar(In)
