Tobolobo

TCP Chat Client

Jul 13th, 2012
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 5.44 KB | None | 0 0
  1. #RequireAdmin
  2.  
  3. #include <ButtonConstants.au3>
  4. #include <EditConstants.au3>
  5. #include <GUIConstantsEx.au3>
  6. #include <StaticConstants.au3>
  7. #include <WindowsConstants.au3>
  8. #include <ScrollBarConstants.au3>
  9. #include <SendMessage.au3>
  10. #include <GUIRichEdit.au3>
  11. #include <GUIEdit.au3>
  12. #include <Array.au3>
  13. #include <File.au3>
  14. #include <Misc.au3>
  15. #include <Timers.au3>
  16.  
  17. TCPStartup()
  18. OnAutoItExitRegister("_Exit")
  19. AutoItSetOption("GUIOnEventMode", 1)
  20. AutoItSetOption("TrayIconHide", 0)
  21.  
  22. Break(0)
  23.  
  24. Dim $dll = DllOpen("user32.dll"), $Mainsocket = -1, $Bool = True
  25.  
  26. $IP = InputBox("IP", "Bitte IP eingeben:", "127.0.0.1")
  27.  
  28. Do
  29.     Sleep(100)
  30. Until _IsPressed("0D", $dll) = False
  31.  
  32. $Mainsocket = TCPConnect($IP, 12000)
  33. If @error Then
  34.     MsgBox(16, "Fehler", "Konnte nicht zum Server verbinden!")
  35.     _Exit()
  36. EndIf
  37.  
  38. $Form1 = GUICreate("Chatclient", 615, 394, 192, 124)
  39. GUISetBkColor(0x000000)
  40. GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
  41. $Edit1 = _GUICtrlRichEdit_Create($Form1, "", 8, 8, 593, 313, BitOR($ES_MULTILINE, $WS_VSCROLL,$WS_CHILD, $WS_VISIBLE, $WS_TABSTOP))
  42. _GUICtrlRichEdit_SetReadOnly($Edit1)
  43. _GUICtrlRichEdit_SetFont($Edit1, 10, "Comic Sans MS")
  44. _GUICtrlRichEdit_SetBkColor($Edit1, 0x000000)
  45. $Label1 = GUICtrlCreateLabel("Name:", 8, 360, 35, 17)
  46. GUICtrlSetColor($Label1, 0xFFFFFF)
  47. $Input1 = GUICtrlCreateInput("", 48, 360, 209, 21)
  48. GUICtrlSetFont(-1, 9, 400, 0, "Comic Sans MS")
  49. GUICtrlSetBkColor($Input1, 0x000000)
  50. GUICtrlSetColor($Input1, 0xFFFFFF)
  51. $Input2 = GUICtrlCreateInput("", 48, 328, 553, 21)
  52. GUICtrlSetFont(-1, 9, 400, 0, "Comic Sans MS")
  53. GUICtrlSetBkColor($Input2, 0x000000)
  54. GUICtrlSetColor($Input2, 0xFFFFFF)
  55. $Label2 = GUICtrlCreateLabel("Text:", 16, 336, 28, 17)
  56. GUICtrlSetColor($Label2, 0xFFFFFF)
  57. $Button1 = GUICtrlCreateButton("Senden", 264, 360, 339, 25)
  58. Dim $Arr[1][2]
  59. $Arr[0][0] = "{ENTER}"
  60. $Arr[0][1] = $Button1
  61. GUISetAccelerators($Arr, $Form1)
  62. GUICtrlSetOnEvent($Button1, "_ButtonFunc")
  63. GUISetState(@SW_SHOW)
  64.  
  65. While True
  66.     $Msg = TCPRecv($Mainsocket, 1024)
  67.     If $Msg <> "" Then
  68.         If StringInStr($Msg, "IPLIST") = True Then
  69.             $Str = StringSplit($Msg, "|")
  70.             For $i = 1 To $Str[0] Step 1
  71.                 RTBInsertColoredText($Edit1, $Str[$i] & @CRLF, 0xFF0000)
  72.             Next
  73.         ElseIf StringInStr($Msg, "NAMELIST") = True Then
  74.             $Str = StringSplit($Msg, "|")
  75.             For $i = 1 To $Str[0] Step 1
  76.                 RTBInsertColoredText($Edit1, $Str[$i] & @CRLF, 0xFF0000)
  77.             Next
  78.         ElseIf $Msg = "NONAME" Then
  79.             RTBInsertColoredText($Edit1, "Name existiert nicht!" & @CRLF, 0x0000FF)
  80.         ElseIf StringInStr($Msg, "PING", 1) = True Then
  81.             TCPSend($Mainsocket, "PONG")
  82.         ElseIf StringInStr($Msg, "Server: Ping beendet!") = True Then
  83.             RTBInsertColoredText($Edit1, $Msg, 0x00FF00)
  84.             $Bool = True
  85.         ElseIf StringInStr($Msg, "Server: Ping wird durchgeführt! Bitte nichts schreiben!") = True Then
  86.             RTBInsertColoredText($Edit1, $Msg, 0x00FF00)
  87.             $Bool = False
  88.         Else
  89.             If StringMid($Msg, 1, 7) = "Server:" Then
  90.                 RTBInsertColoredText($Edit1, $Msg, 0x00FF00)
  91.             Else
  92.                 RTBInsertColoredText($Edit1, $Msg, 0xFFFFFF)
  93.             EndIf
  94.             If WinGetState($Form1) = 23 Then
  95.                 TrayTip("Nachricht", StringTrimRight($Msg, 2), 3000)
  96.             EndIf
  97.             If $Msg = "Server Shutting down..." Then
  98.                 _SendMessage($Edit1 ,$WM_VSCROLL, $SB_BOTTOM, 0)
  99.                 GUICtrlSetState($Input2, $GUI_FOCUS)
  100.                 _GUICtrlEdit_SetSel($Input2, -1, -1)
  101.                 Sleep(5000)
  102.                 TCPCloseSocket($Mainsocket)
  103.                 TCPShutdown()
  104.                 Exit
  105.             EndIf
  106.         EndIf
  107.         _SendMessage($Edit1 ,$WM_VSCROLL, $SB_BOTTOM, 0)
  108.         GUICtrlSetState($Input2, $GUI_FOCUS)
  109.         _GUICtrlEdit_SetSel($Input2, -1, -1)
  110.     EndIf
  111. WEnd
  112.  
  113. Func _ButtonFunc()
  114.     If $Bool = True Then
  115.         If WinGetState($Form1) = 15 Then
  116.             If GUICtrlRead($Input2) = "" Or GUICtrlRead($Input1) = "" Then
  117.                 TrayTip("Chathelper", "Text und Name dürfen nicht leer sein!", 1000)
  118.             Else
  119.                 If GUICtrlRead($Input2) = "hilfe" Then
  120.                     _GUICtrlRichEdit_SetSel($Edit1, -1, -1)
  121.                     _GUICtrlRichEdit_InsertText($Edit1, "/IPLIST, um IPs anzeigen zu lassen!" & @CRLF)
  122.                     _GUICtrlRichEdit_InsertText($Edit1, "/NAMELIST, um die Namen anzeigen zu lassen!" & @CRLF)
  123.                     _GUICtrlRichEdit_InsertText($Edit1, "/WHISPER NAME NACHRICHT, um private Nachricht zu senden!" & @CRLF)
  124.                     _GUICtrlRichEdit_InsertText($Edit1, "Enter ist der Hotkey zum senden!" & @CRLF)
  125.                     _GUICtrlRichEdit_SetSel($Edit1, -1, -1)
  126.                     _SendMessage($Edit1 ,$WM_VSCROLL, $SB_BOTTOM, 0)
  127.                     GUICtrlSetState($Input2, $GUI_FOCUS)
  128.                     GUICtrlSetData($Input2, "")
  129.                 Else
  130.                     GUICtrlSetData($Input1, StringReplace(GUICtrlRead($Input1), " ", ""))
  131.                     TCPSend($Mainsocket, GUICtrlRead($Input1) & "(" & @HOUR & ":" & @MIN & ":" & @SEC & "): " & GUICtrlRead($Input2) & @CRLF)
  132.                     GUICtrlSetData($Input2, "")
  133.                 EndIf
  134.             EndIf
  135.         Else
  136.             WinFlash(GUICtrlRead($Form1), 0, 5, 1000)
  137.         EndIf
  138.     EndIf
  139. EndFunc
  140.  
  141. Func RTBInsertColoredText($hwnd, $Text, $0xBGR, $Font = "Comic Sans MS", $Size = 10)
  142.     _GUICtrlRichEdit_SetSel($hwnd, -1, -1)
  143.     $RichEditLength = _GUICtrlRichEdit_GetTextLength($hwnd, True, True)
  144.     $RichEditLines = _GUICtrlRichEdit_GetLineCount($hwnd)
  145.     _GUICtrlRichEdit_SetFont($hwnd, $Size, $Font)
  146.     _GUICtrlRichEdit_InsertText($hwnd, $Text)
  147.     _GUICtrlRichEdit_SetSel($hwnd, $RichEditLength - $RichEditLines + 1, -1)
  148.     _GUICtrlRichEdit_SetCharColor($hwnd, $0xBGR)
  149.     _GUICtrlRichEdit_SetSel($hwnd, -1, -1)
  150. EndFunc
  151.  
  152. Func _Exit()
  153.     If $Mainsocket <> -1 Then
  154.         TCPSend($Mainsocket, "USERQUIT|" & GUICtrlRead($Input1))
  155.         TCPCloseSocket($Mainsocket)
  156.     EndIf
  157.     TCPShutdown()
  158.     DllClose($dll)
  159.     Exit
  160. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment