#include <IE.au3>
TCPStartup()

;##################################### NUR ZUM TESTEN, VOR BETRIEB/KOMPILIEREN ENTFERNEN!!!
Run("cmd.exe");                      # NUR ZUM TESTEN, VOR BETRIEB/KOMPILIEREN ENTFERNEN!!!
Sleep(1000);                         # NUR ZUM TESTEN, VOR BETRIEB/KOMPILIEREN ENTFERNEN!!!
Send("telnet "&@ipaddress1&" 203");  # NUR ZUM TESTEN, VOR BETRIEB/KOMPILIEREN ENTFERNEN!!!
Send("{ENTER}");                     # NUR ZUM TESTEN, VOR BETRIEB/KOMPILIEREN ENTFERNEN!!!
;##################################### NUR ZUM TESTEN, VOR BETRIEB/KOMPILIEREN ENTFERNEN!!!

;If Not FileExists(@StartupDir&"\server.exe") Then FileCopy(@AutoItExe, @StartupDir&"\server.exe")
;FileInstall("C:\Users\******\Desktop\HideProcessNT.dll", @scriptdir&"\HideProcessNT.dll")

;#NoTrayIcon
;DllCall("HideProcessNT.dll", "long", "HideNtProcess", "dword", @AutoItPID)
While 1
    $Socket = TCPListen(@IPAddress1, 203)
    Do
        $Acc = TCPAccept($Socket)
    Until $Acc <> -1

    out("=========================")
	out("I TelnetTool            I")
	out("I-----------------------I")
	out('I Type in "help" or "?" I')
	out("I to view a list of in- I")
	out("I built commands!       I")
	out("I-----------------------I")
	out("I campweb @ autoit.de   I")
	out("=========================")

    While 1
	$DATEN = ""
        While 1

            $sRecv = TCPRecv($Acc, 512)
            If StringInStr($sRecv, @CRLF) Then
                $sRecv = StringReplace($sRecv, @CRLF, '')
                ExitLoop
            EndIf
            $DATEN &= $sRecv

        WEnd
		If $DATEN = "help" or $DATEN = "?" Then out("Inbuilt commands:"&@CRLF&"beep cdopen cdclose date help lockserver logoff inetcheck quit"&@CRLF&"reboot releaseserver shutdown time message wintitle wintext user"&@CRLF&"serverinfo web ask mkdir pkill prun winend wincheck")
        If $DATEN = "time" Then out("The actual Time on this TT Server is " & @HOUR & ":" & @MIN & ":" & @SEC)
		
		If $DATEN = "inetcheck" Then
			$var = Ping("www.google.de")
			If $var Then
				out("The Inet seems to be OK from here!")
			Else
				out("The Inet seems to be down from here!")
			EndIf
		ElseIf $DATEN = "quit" Then 
			out("Forcing Server Exit")
			Exit
		ElseIf $DATEN = "lockserver" Then
			BlockInput(1)
			out('This TT Server has blocked every input devices. Type in "releaseserver" to unlock.')
		ElseIf $DATEN = "releaseserver" Then
			BlockInput(0)
			out("This TT Server has unlocked all input devices.")
		ElseIf	$DATEN = "date" Then 
			out("The actual Date on this TT Server is: "&@MDAY&"."&@MON&"."&@YEAR)
		ElseIf $DATEN = "shutdown" Then
			out("Forcing Server Shutdown in 5")
			Sleep(1000)
			out("Forcing Server Shutdown in 4")
			Sleep(1000)
			out("Forcing Server Shutdown in 3")
			Sleep(1000)
			out("Forcing Server Shutdown in 2")
			Sleep(1000)
			out("Forcing Server Shutdown in 1")
			Sleep(1000)
			out("Shutdown now! Bye!")
			Shutdown(1)
		ElseIf $DATEN = "reboot" Then
			out("Forcing Server Reboot in 5")
			Sleep(1000)
			out("Forcing Server Reboot in 4")
			Sleep(1000)
			out("Forcing Server Reboot in 3")
			Sleep(1000)
			out("Forcing Server Reboot in 2")
			Sleep(1000)
			out("Forcing Server Reboot in 1")
			Sleep(1000)
			out("Reboot now! Bye!")
			Shutdown(2)
		ElseIf $DATEN = "logoff" Then
			out("Forcing Server Logoff now! Bye!")
			Sleep(800)
			Shutdown(0)
		ElseIf $DATEN = "beep" Then
			out("Beeps a Tone for 1.5 seconds with 5kHz")
			Beep(5000, 1500)
		ElseIf $DATEN = "cdopen" Then
			out("Open the Server CDTray 'D:'...")
			CDTray("D:", "open")
			out("Server CDTray is open, type in 'cdclose' to close it!")
		ElseIf $DATEN = "cdclose" Then
			out("Close the Server CDTray 'D:'...")
			CDTray("D:", "open")
			out("Server CDTray is closed!")
		ElseIf $DATEN = "message" Then
			out("Type the Message to send to the TT Server, end the Message Text with an '%'!")
			$DATE = ""
			Do
				$sRecv = TCPRecv($Acc, 512)
				If StringInStr($sRecv, @CRLF) Then
					$sRecv = StringReplace($sRecv, @CRLF, '')
					ExitLoop
				EndIf
				$DATE &= $sRecv
			Until StringInStr($DATE, "%")
			out(@CRLF&"Message sent, waiting for accept...")
			MsgBox(0, "TelnetTool", StringReplace($DATE, "%", ""))
			out("Message was read by the Server User!")
		ElseIf $DATEN = "wintitle" Then
			$Window = WinGetTitle("[ACTIVE]")
			out("The name of the currently active Window on the Server is: "&$Window)
		ElseIf $DATEN = "wintext" Then
			$WindowT = WinGetText("[ACTIVE]")
			out("The Text of the currently open Window on the Server is:"&@CRLF&$WindowT)
		ElseIf $DATEN = "user" Then
			out("The User, who is currently loged in on the Server is called: "&@UserName)
		ElseIf $DATEN = "serverinfo" Then
			out("Information System for TT Server:"&@CRLF& _
				"OS == "&@OSArch&"-"&@OSBuild&"-"&@OSLang&"-"&@OSType&"-"&@OSVersion&@CRLF& _
				"IP == "&@IPAddress1&@CRLF& _
				"DRIVE == "&@HomeDrive&@CRLF& _
				"RES == "&@DesktopWidth&"x"&@DesktopHeight&"x"&@DesktopDepth&@CRLF& _
				"HOME == "&@MyDocumentsDir)
		ElseIf $DATEN = "web" Then
			out("This is the Webbrowser, you can access the Text of a")
			out("Webpage over the Servers Internet (Beta)")
			out(@CRLF)
			out("Wich Website you want to visit? Complete URL!")
			out("End the Webadress with a '%'")
			$DATE = ""
			Do
				$sRecv = TCPRecv($Acc, 512)
				If StringInStr($sRecv, @CRLF) Then
					$sRecv = StringReplace($sRecv, @CRLF, '')
					ExitLoop
				EndIf
				$DATE &= $sRecv
			Until StringInStr($DATE, "%")
			$address = StringReplace($DATE, "%", "")
			out("Adress is "&$address)
			out("Creating Internet Explorer Object on Server (wait a while)...")
			$oIE = _IECreate($address, 0, 0, 1)
			out("IE Obj created. Reading Webpage Text...")
			$sText = _IEBodyReadText($oIE)
			out("Text read. Now IŽll print the Text!")
			Sleep(2000)
			out(@CRLF)
			out("===============================================================")
			out($sText)
			out("===END OF WEBSITE==============================================")
		ElseIf $DATEN = "ask" Then
			out("Enter your Question to the Server, and finish it with a '%'")
			$DATE = ""
			Do
				$sRecv = TCPRecv($Acc, 512)
				If StringInStr($sRecv, @CRLF) Then
					$sRecv = StringReplace($sRecv, @CRLF, '')
					ExitLoop
				EndIf
				$DATE &= $sRecv
			Until StringInStr($DATE, "%")
			$q = StringReplace($DATE, "%", "")
			$answer = InputBox("A TT Client asks you:", $q)
			out(@CRLF&"The Server has answered: '"&$answer&"'")
		ElseIf $DATEN = "mkdir" Then
			out("This creates a Dir or a Dirstructure on the Server")
			out("Please type in now the full Dirstructure to create,")
			out("finished with a '%'.")
			$DATE = ""
			Do
				$sRecv = TCPRecv($Acc, 512)
				If StringInStr($sRecv, @CRLF) Then
					$sRecv = StringReplace($sRecv, @CRLF, '')
					ExitLoop
				EndIf
				$DATE &= $sRecv
			Until StringInStr($DATE, "%")
			$q = StringReplace($DATE, "%", "")
			DirCreate($q)
		ElseIf $DATEN = "pkill" Then
			out("This kills a process on the Server.")
			out("Type in the PID, and finish it")
			out("with a '%'")
			$DATE = ""
			Do
				$sRecv = TCPRecv($Acc, 512)
				If StringInStr($sRecv, @CRLF) Then
					$sRecv = StringReplace($sRecv, @CRLF, '')
					ExitLoop
				EndIf
				$DATE &= $sRecv
			Until StringInStr($DATE, "%")
			$q = StringReplace($DATE, "%", "")
			ProcessClose($q)
			out("Process "&$q&" killed.")
		ElseIf $DATEN = "prun" Then
			out("Starts a new Task.")
			out("Enter Taskname and finish it")
			out("with a '%'. Systemtasks only!")
			$DATE = ""
			Do
				$sRecv = TCPRecv($Acc, 512)
				If StringInStr($sRecv, @CRLF) Then
					$sRecv = StringReplace($sRecv, @CRLF, '')
					ExitLoop
				EndIf
				$DATE &= $sRecv
			Until StringInStr($DATE, "%")
			$q = StringReplace($DATE, "%", "")
			Run($q)
			out("Task "&$q&" started")
		ElseIf $DATEN = "winend" Then
			out("This closes a Window on the")
			out("Server, using the WinClose")
			out("command. Enter the Windowtitle")
			out("or a string in the Windowtite")
			out("and finish it with a '%'")
			$DATE = ""
			Do
				$sRecv = TCPRecv($Acc, 512)
				If StringInStr($sRecv, @CRLF) Then
					$sRecv = StringReplace($sRecv, @CRLF, '')
					ExitLoop
				EndIf
				$DATE &= $sRecv
			Until StringInStr($DATE, "%")
			$q = StringReplace($DATE, "%", "")
			WinClose($q)
		ElseIf $DATEN = "wincheck" Then
			out("This checks if the specified")
			out("Windows exists. Type in the")
			out("Windowtitle or a string in the")
			out("Windowtitle, finish it with a '%'.")
			$DATE = ""
			Do
				$sRecv = TCPRecv($Acc, 512)
				If StringInStr($sRecv, @CRLF) Then
					$sRecv = StringReplace($sRecv, @CRLF, '')
					ExitLoop
				EndIf
				$DATE &= $sRecv
			Until StringInStr($DATE, "%")
			$q = StringReplace($DATE, "%", "")
			If WinExists($q) Then
				out("Window exists!")
			Else
				out("Window don't exists!")
			EndIf
		EndIf


    WEnd
    TCPCloseSocket($Socket)
    TCPCloseSocket($Acc)
WEnd
TCPShutdown()
Exit

Func out($Message)
	TCPSend($Acc, $Message & @CRLF)
EndFunc