Telnet Console Client With ANSI - Help!

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pantcho!.

Hello PPl.
i am a new member :P
this code snippet is from Rapid-Q language if anyone knows it...
and i Kinda converted the Telnet source code into purebasic
but the program have some diffuclties with communication - Very Very Slow....
and i think i didnt do the same things as the Telnet RFC is explaining to do.

so here is the code for u ppl to help me how to improve it.

thank u.

[BEGIN CODE]
;-Vars
Row=1
Col=1
fore=7
back=0
;command.s = LCase(ProgramParameter())
command.s = "malkaviabbs.com"
*Buffer= AllocateMemory(1, 1000, 0)
IAC.s = Chr(255)
tDo.s = Chr(253) ;** values based on RFC854
tDont.s = Chr(254)
tWill.s = Chr(251)
tWont.s = Chr(252)
SE.s = Chr(240)
SB.s = Chr(250)
TermType.s = Chr(24)
TermTypeSB.s = SB + TermType + Chr(1) + IAC + SE
TermTypeInfo.s = IAC + SB +TermType +Chr(0)+ "ANSI" +IAC +SE
A.s ;Keyboard Input
B.s ;Term Read Byte Buffer
Dim Keys.s(255) ;Key maps For special ANSI - Arrows etc
Keys(59)=Chr(27)+"[M" ;F1 ;terminal keys
Keys(60)=Chr(27)+"[N" ;F2 ;basically, we're grabbing the
Keys(61)=Chr(27)+"[O" ;F3 ;scancodes And sending the ansi
Keys(62)=Chr(27)+"[P" ;F4 ;equivalent
Keys(63)=Chr(27)+"[Q" ;F5
Keys(64)=Chr(27)+"[R" ;F6
Keys(65)=Chr(27)+"[S" ;F7
Keys(66)=Chr(27)+"[T" ;F8
Keys(67)=Chr(27)+"[U" ;F9
Keys(68)=Chr(27)+"[V" ;F10
Keys(71)=Chr(27)+"[H" ;home
keys(72)=Chr(27)+"[A" ;Up Arrow
keys(73)=Chr(27)+"[I" ;PgUp
keys(80)=Chr(27)+"[D" ;Down Arrow
keys(75)=Chr(27)+"[C" ;Right Arrow
Keys(79)=Chr(27)+"[F" ;End
keys(77)=Chr(27)+"[B" ;Left Arrow
Keys(81)=Chr(27)+"[G" ;PgDn
;-Begin
OpenConsole()
PrintN("")
PrintN("PureBasic Telnet Client")
PrintN("^^^^^^^^^^^^^^^^^^^^^^^")
PrintN("")
If command.s = ""
PrintN("")
PrintN("Usage - Tel ")
PrintN("")
Delay(3000)
CloseConsole()
End
EndIf
PrintN("")
Print("Connecting To - " + command + " ...")
If InitNetwork() = 0
PrintN("")
PrintN("Error - Can't initialize the network !")
CloseConsole()
End
EndIf
ConnectionID = OpenNetworkConnection(command, 23)
If ConnectionID
PrintN("[ONLine]")
;-Main Loop
Repeat
A.s=Inkey()
If A""
If Len(A)=2 And Left(A,1)=Chr(255)
;Special Key Detected ... Translate A$ To keymap
A=Keys(Asc(Right(A,1)))
PokeS(*Buffer,A,Len(A))
SendNetworkData(ConnectionID,*Buffer,Len(A))
Delay(20)
Else
PokeS(*Buffer,Left(A,1),Len(A)-1)
SendNetworkData(ConnectionID,*Buffer,Len(A)-1)
Delay(20)
EndIf
EndIf

SendChk = NetworkClientEvent(ConnectionID)
If SendChk = 2
RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 1000)
B.s = PeekS(*Buffer,RequestLength)
If B=IAC
Gosub HandleOPT ; Process Telnet Protocol
Else
Gosub AnsiPrintChars ; Process ANSI scan codes
EndIf
EndIf
If Len(B) SE
RequestLength.l = ReceiveNetworkData(ConnectionID, *Buffer, 1000)
More.s = PeekS(*Buffer)
B=B+More
Wend
If B=TermTypeSB
PrintN("REQUEST FOR TermType")
C.s = TermTypeInfo
SendNetworkData(ConnectionID,C,Len(C))
PrintN("SENT TermType INFORMATION")
Delay(2000)
Else
PrintN("Unknown SUBOption... Ignoring")
Delay(2000)
EndIf
Default
PrintN("Unknown Option...Ignoring")
Delay(2000)
EndSelect
EndIf
Return


CloseConsole()
End



;================================================================

;-AnsiPrintChars
AnsiPrintChars:
For disp = 1 To Len(B)
ch.s=Mid(B,disp,1)
Gosub AnsiPrint
Next
Return



;-AnsiPrint
AnsiPrint:
If first=0
fore=7
back=0
first=-1
EndIf

If ANSIcode
If Left(ANSIst$,2)="[M"
If Asc(ch)=14
;If Music THEN PLAY "MB" + MID$( ANSIst$, 4 )
ANSIst$=""
ANSIcode=0
Else
ANSIst$+ch
EndIf

ElseIf Asc(UCase(ch))>64 And Asc(UCase(ch)) 80
Gosub CursorDown
EndIf

Print(ANSIst$)
ANSIcode=0
ANSIst$=""
EndSelect
ANSIst$ = ""
ANSIcode = 0

ElseIf Asc(ch)60
Print(ANSIst$)
Col = Col +1
ANSIcode=0
ANSIst$=""
Else
ANSIst$+ch
EndIf

ElseIf Asc(ch)=27
ANSIcode=-1
ANSIst$=""

ElseIf Asc(ch)=13
ConsoleLocate(1,Row)
Col = 0

ElseIf Asc(ch)=10
PrintN("")
Row=Row+1
ConsoleLocate(col,Row+1)


Else
Print(ch)
Col = Col + 1
EndIf
Return


CursorUp:
Tmp=Val(Mid(ANSIst$,2,Len(ANSIst$)))
If Tmp25:Row=25:EndIf
ConsoleLocate(Col,Row)
Return

CursorLeft:
Tmp=Val(Mid(ANSIst$,2,Len(ANSIst$)-1))
If Tmp80:Col=80:EndIf
ConsoleLocate(Col,Row)
Return



;-CursorLocate
CursorLocate:
tmp.l
Tmp=FindString(ANSIst$,";",1)
If Tmp
Col=Val(Mid(ANSIst$,Tmp+1,Len(ANSIst$)-1))
Select tmp
Case 3
Row= Val(Mid(ANSIst$,2,1 ))
Case 4
Row= Val(Mid(ANSIst$,2,2 ))
EndSelect
Else
Row=Val(Mid(ANSIst$,2,Len(ANSIst$)))
Col=1
EndIf

If Row25:Row=25:EndIf
If Col80:Col=80:EndIf

ConsoleLocate(Col,Row)
Return



;-SaveCursorPosn
SaveCursorPosn:
SaveRow=Row
SaveCol=Col
Return


;-RestCursorPosn
RestCursorPosn:
If SaveRow>0
ConsoleLocate(SaveCol,SaveRow)
Row=SaveRow
Col=SaveCol
EndIf
Return


;-BigErase
BigErase:
Delay(1000)
ClearConsole()
Col = 1
Row = 1
ConsoleLocate(1,1)
Return


;-SmallErase
SmallErase:
ConsoleLocate(Col,Row)
PrintN(Space(80-Col))
Row=Row+1
ConsoleLocate(Col,Row)
Return



;-SetColors
SetColors:
ANSIst$=Mid(ANSIst$,2,Len(ANSIst$))
col3 = -1
Tmp=FindString(ANSIst$,";",1)
If Tmp
Tmp2=FindString(ANSIst$,";",Tmp+1)
If Tmp2
col3=Val(Mid(ANSIst$,Tmp2+1,Len(ANSIst$)-Tmp2 ))
EndIf

Select Tmp
Case 3
col1 = Val(Mid(ANSIst$,1,2))
If col3>0
col2 = Val(Mid(ANSIst$,Tmp+1,Tmp2-1-Tmp))
Else
col2 = Val(Mid(ANSIst$,Tmp+1,Len(ANSIst$)-Tmp))
EndIf
Case 2
col1 = Val(Mid(ANSIst$,1,1))
If col3>=0
col2 = Val(Mid(ANSIst$,Tmp+1,Tmp2-1-Tmp))
Else
col2 = Val(Mid(ANSIst$,Tmp+1,Len(ANSIst$)-Tmp))
EndIf
EndSelect

Else
col1 = Val(ANSIst$)
col2 = -1
EndIf
Dim AnsCol.l(3)
AnsCol(1) = col1
AnsCol(2) = col2
AnsCol(3) = col3
For x = 1 To 3
Tmp=AnsCol(x)
Select Tmp
Case 0: fore= 7:back=0 ;'reset colors
Case 1: fore=fore|8 ;'high intensity
Case 2: fore=fore & $17 ;'normal intensity
Case 5: fore=fore|15 ;'blink - dont know how to do it in purebasic! :P
Case 7: fore=0:back=7 ;'reverse video
Case 8: fore=0:back=0 ;'invisible
Case 30:fore= fore & $18 ;'black foreground
Case 31:fore=(fore & $18)|4 ;'red foreground
Case 32:fore=(fore & $18)|2 ;'green foreground
Case 33:fore=(fore & $18)|6 ;'yellow foreground
Case 34:fore=(fore & $18)|1 ;'blue foreground
Case 35:fore=(fore & $18)|5 ;'magenta foreground
Case 36:fore=(fore & $18)|3 ;'cyan foreground
Case 37:fore=fore|7 ;'white foreground
Case 40:back=0 ;'black background
Case 41:back=4 ;'red background
Case 42:back=2 ;'green background
Case 44:back=6 ;'yellow background
Case 44:back=1 ;'blue background
Case 45:back=5 ;'magenta background
Case 46:back=3 ;'cyan background
Case 47:back=7 ;'white background
EndSelect
Next x
ConsoleColor(fore,back)
[END CODE]