Jump to content

Telnet and Color?


piccaso
 Share

Recommended Posts

I wrote a little chat server in autoit for intranet use.

and telnet is its client because everybody has it allready.

edit: Telnet problems (should be) gone

come on in and help me stress test it :whistle:

Type into the runbox:

Telnet

Type into the Telnet window

set localecho
o lazarus7.ath.cx 1500

The actual question is:

Can i use color in this environment?

with escape sequences?

thanks...

edit: source below is a bit out of date ;)

Edited by piccaso
CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

da source :whistle:

Global $sAdress = ""
Global $iPort = 1500
Global $iMaxConnect = 256
Global $sMotd = "Hello! Welcome to the Telnet Chat Relay." & @CRLF & _
                "Available Commands:" & @CRLF & _
                "/nick <NickName>          ; Set Nick" & @CRLF & _
                "/users                    ; Display Online Peers" & @CRLF & _
                "/msg <NickName> <Message> ; Send Private message" & @CRLF & _
                "" & @CRLF & _
                "Please Set your Nick!" & @CRLF & _
                "Use This on your own Risk!" & @CRLF & _
                "Happy chatting" & @CRLF & _
                "" & @CRLF
Global $fDebug = True
Global $iDebugClients = 3
;-------------
#include <array.au3>

Global $iMainSocket
Global $aClientSockets[$iMaxConnect + 1]
Global $aClientNicks[$iMaxConnect + 1]
Global $aClientData[$iMaxConnect + 1]

TCPStartup()
$iMainSocket = TCPListen($sAdress,$iPort,$iMaxConnect)
If $fDebug Then
    HotKeySet("{ESC}","_Bye")
    If $iDebugClients > 1 Then
        For $i = 1 To $iDebugClients
            Run(@ComSpec & " /c " & 'telnet localhost ' & $iPort)
        Next
    EndIf
EndIf
If $iMainSocket < 0 Then Exit -1
While 1
    $iNewClient = TCPAccept($iMainSocket)
    If $iNewClient > 0 Then
        For $i = 0 To UBound($aClientSockets) -1
            If Not $aClientSockets[$i] Then
                $aClientSockets[$i] = $iNewClient
                $aClientNicks[$i] = "Client" & $i
                TCPSend($iNewClient,$sMotd)
                _SendAll("New Peer: " & $i & ", " & SocketToIP($iNewClient) & @CRLF)
                $iNewClient = -1
                ExitLoop
            EndIf
        Next
        If $iNewClient <> -1 Then
            TCPSend($iNewClient,"Too many Clients...")
            TCPCloseSocket($iNewClient)
        EndIf
    EndIf
    For $i = 0 To UBound($aClientSockets) -1
        If $aClientSockets[$i] > 0 Then
            $sBuff = TCPRecv($aClientSockets[$i],256)
            If @error Then
                _SendAll("Connection Closed: " & $i & ", @error = " & @error & ", " & SocketToIP($aClientSockets[$i]) & ", " & $aClientNicks[$i] & @CRLF)
                TCPCloseSocket($aClientSockets[$i])
                $aClientSockets[$i] = 0
                $aClientData[$i] = ""
                $aClientNicks[$i] = ""
            ElseIf $sBuff Then
                ; If $fDebug Then ConsoleWrite($i & ":0x" & Hex(BinaryString($sBuff)) & " ")
                $aClientData[$i] &= $sBuff
            EndIf
            If StringInStr($aClientData[$i],@LF) Then
                If StringInStr($aClientData[$i],"/nick") = 1 Then
                    $sBuff = StringStripWS(StringMid($aClientData[$i],6),1+2)
                    _SendAll("Peer " & $i & ", " & SocketToIP($aClientSockets[$i]) & " Changed Nick from " & $aClientNicks[$i] & " to " & $sBuff & @CRLF)
                    $aClientNicks[$i] = $sBuff
                ElseIf StringInStr($aClientData[$i],"/users") = 1 Then
                    _showPeers($aClientSockets[$i])
                ElseIf StringInStr($aClientData[$i],"/msg") = 1 Then
                    $aBuff = StringSplit($aClientData[$i]," ")
                    If $aBuff[0] > 2 Then
                        $sBuff = StringMid($aClientData[$i], StringInStr($aClientData[$i],$aBuff[3]))
                        $iTmp = _ArraySearch($aClientNicks,$aBuff[2],0,0,1)
                        If $iTmp > -1 Then
                            TCPSend($aClientSockets[$iTmp],"- " & $aClientNicks[$i] & " ->" & $sBuff)
                        EndIf
                    EndIf
                ElseIf Not StringStripWS($aClientData[$i],1+2) Then
                    ; Empty Line
                Else
                    $aClientData[$i] = StringReplace($aClientData[$i],BinaryString("0x1B5B41"),"") ; Up
                    $aClientData[$i] = StringReplace($aClientData[$i],BinaryString("0x1B5B42"),"") ; Down
                    _SendAll($aClientNicks[$i] & ">" & $aClientData[$i])
                EndIf
                $aClientData[$i] = ""
            EndIf   
        EndIf
    Next
WEnd

Func _showPeers($iSock)
    Local $i
    For $i = 0 To UBound($aClientSockets) -1
        If $aClientSockets[$i] > 0 Then
            TCPSend($iSock,$i & ", " & SocketToIP($aClientSockets[$i])& ", " & $aClientNicks[$i] & @CRLF)
            If @error Then ExitLoop
        EndIf
    Next
EndFunc

Func _SendAll($sMsg)
    Local $i
    For $i = 0 To UBound($aClientSockets) -1
        if $aClientSockets[$i] > 0 Then
            TCPSend($aClientSockets[$i],$sMsg)
        EndIf
    Next
EndFunc

Func SocketToIP($iSock)
    Local $sockaddr = DllStructCreate("short;ushort;uint;char[8]")
    Local $aRet = DllCall("Ws2_32.dll", "int", "getpeername", "int", $iSock, _
            "ptr", DllStructGetPtr($sockaddr), "int_ptr", DllStructGetSize($sockaddr))
    If Not @error And $aRet[0] = 0 Then
        $aRet = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($sockaddr, 3))
        If Not @error Then $aRet = $aRet[0]
    Else
        $aRet = 0
    EndIf
    $sockaddr = 0
    Return $aRet
EndFunc   ;==>SocketToIP

Func _Bye()
    Exit
EndFunc
CoProc Multi Process Helper libraryTrashBin.nfshost.com store your AutoIt related files here!AutoIt User Map
Link to comment
Share on other sites

/msg sends to every user online, probably. At least I could read what you guys were saying. Whenever I was typing and someone else responded to me, my text was gone.. It was abit frustrating.

After trying some more, I thought it wasn't bad. I have no idea about the colors though.

Edited by Manadar
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...