Jump to content

Simple Chat


Snarg
 Share

Recommended Posts

Greetings. I am attempting to make some upgrades to the simple chat service found on this thread. I thought I would start small and have added a function to log all chat messages. All chat messages are logged to a text file residing in the servers directory however, it has one bug I am unable to track down. It logs a line containing a user name, with no additional text, whenever a user leaves the server. Although it is not a horrible bug, it's still annoying and I would like to make it go away. Following is the changed code. The extra comments are me attempting to understand what is going on in the program. I have no doubts I am wrong on a lot of my assumptions. Feel free to correct/educate me.

I added one line to the following function:

Func _CheckConn()
   Local $recv = "" ;Starts $recv at nothing
   For $i = 0 to 16 ;Counter corresponds to number of allowed connections
      If $list[$i][0] Then
         $recv = TCPRecv($list[$i][0], 2048) ;Check for data from all connections
         If @error Then
            ; disconnect client
            TCPCloseSocket($list[$i][0]) ;Closes the socket that was in use
            _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1]) ;Instructs clients to remove user from user list
            $list[$i][0] = 0
            $list[$i][1] = ""
         Else
            If $recv Then ;Some type of data has been recieved. Begin parsing commands
               $recv = StringSplit($recv, Chr(1)) ;SOH character. Start of Heading. No idea what that means. $recv is converted into an array
               For $j = 1 to $recv[0] ;Counter based upon number of elements in $recv array
                  $temp = StringSplit($recv[$j], Chr(2)) ;Breaks input up into chunks of text (?) STX character. Start of Text. If the first word = case, do something
                  Switch $temp[1]
                     Case "bye" ;Client departing
                        TCPCloseSocket($list[$i][0])
                        _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])
                        $list[$i][0] = 0
                        $list[$i][1] = ""
                     Case "kick" ;Client kicked
                        _Kick($temp[2])
                     Case "PM" ;Client recieving a PM
                        _PM($temp[2], $temp[3], $list[$i][1])
                     Case Else ;If it wasn't a command, it must be a message. Display it
                        If $recv[$j] Then _SendAll($recv[$j], $list[$i][1]) ;message, from
                        ChatLog ($recv[$j], $list[$i][1]) ;Added this line to create logs. For some reason it logs clients departing. No idea why
                  EndSwitch
               Next
            EndIf
         EndIf
      EndIf
  Next
EndFuncoÝ÷ Ù8b²+-éh§û§rبZ½çhæ ¢[Ú­æx(Ø^~«Æ§v¯y§ÞÂ'^jÊ'-¢f¤z+Zn+f¢·}øz{[ºÒ¢¯zު笶*'iº.¶Z )à2ë-Ê¢{-j{eÊ^©ÝrZ,zØ^~)^¶-¢·­ê.¶­"Ü(ºWlyé­«pØZnë2±êïz»aÇ(ºWZuÖ¥¢Ú²ÚÞ²ËhiÊ&¦ë^¬    òÁ¬­+"³Z´Z½æ®¶­sdgVæ26DÆörb33c·7G"Âb33c¶g&öÒÒgV÷C²gV÷C² ´7&VFRÆörfÆRâFW&R2¤tõB¢Fò&R&WGFW"vFòFòF2b333° b33c¶6EöÆörÒfÆT÷Vâ67&DF"fײgV÷C²b3#¶6EöÆörçGBgV÷C²Â  bb33c¶6EöÆörÒÓFVâ´6÷VÆFâb33·B7&VFRö÷VâFRÆöp ×6t&÷ÂgV÷C´W'&÷"gV÷C²ÂgV÷CµVæ&ÆRFò7&VFR6BÆörâgV÷C² W@ VæD`  bb33c¶g&öÒæB7G&ætÆVgBb33c·7G"ÂfÇC²fwC²6"FVà fÆUw&FRb33c¶6EöÆörÂôæ÷rfײgV÷C·ÂgV÷C²fײb33c¶g&öÒfײgV÷C²fwC²gV÷C²fײb33c·7G"µw&FRFòÆöp VæD`  fÆT6Æ÷6Rb33c¶6EöÆör¤VæDgVæ0

Thank you for your time.

A little reading goes a long way. Post count means nothing.

Link to comment
Share on other sites

You're going to hate this part: Look up _FileWriteLog().

:whistle:

As for the bug wherein a client is logged leaving, apparently something is getting into $recv after the socket closes. Because it doesn't fit any of your criteria, it gets tossed down to where your "it must be a message" code will handle it.

-S

(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

You're going to hate this part: Look up _FileWriteLog().

Yeah, I found that *after* I posted. Go figure...

As for the bug wherein a client is logged leaving, apparently something is getting into $recv after the socket closes. Because it doesn't fit any of your criteria, it gets tossed down to where your "it must be a message" code will handle it.

Below is the exit function from the client:

Func OnAutoItExit()
   TCPSend($sock, Chr(1) & "bye")
   TCPShutdown()
   Exit
EndFuncoÝ÷ ئ笲)àÝý²Ø^
õ¶­È¶Ø§Z çH«ÞvÞ²­ëj·!½êÈq©÷ö×âÖ ¢^Æ^«b¢z­
õ­æ¥'hzÂ$"¶ËZ×è¯Rµªí¡ñÞi׫ßÖ§uú.ÖÚwè®f­ß®®®

It appears, however, that the Chr(1) is sent first, and then the "bye" thus allowing the Chr(1) to get through and be logged.

As for the rest of the comments, how far off am I?

A little reading goes a long way. Post count means nothing.

Link to comment
Share on other sites

Finally got it figured out. Now it will log all chat messages. Following is the code I changed. If any of my comments on what I *think* is going on in this function are way off please, let me know:

Func _CheckConn() ;Function to check to activity on the TCP port
   Local $recv = "" ;Starts $recv at nothing
   For $i = 0 to 16 ;Counter corresponds to number of allowed connections
      If $list[$i][0] Then ;Activity on the socket
         $recv = TCPRecv($list[$i][0], 2048) ; Receive data from the client with a max char length of 2048
         If @error Then
            ; disconnect client
            TCPCloseSocket($list[$i][0]) ;Closes the socket that was in use
            _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1]) ;Instructs clients to remove user from user list
            $list[$i][0] = 0 ;Clear out the socket in the array
            $list[$i][1] = "" ;Remove username from the array
         Else
            If $recv Then ;Some type of data has been recieved. Begin parsing commands
               $recv = StringSplit($recv, Chr(1)) ;SOH character. Start of Heading. No idea what that means. $recv is converted into an array
               For $j = 1 to $recv[0] ;Loop counter based upon number of elements in $recv array
                  $temp = StringSplit($recv[$j], Chr(2)) ;Breaks input up into chunks of text. If the first word = case, do something
                  Switch $temp[1]
                     Case "bye" ;Client departing
                        TCPCloseSocket($list[$i][0]) ;Close the socket
                        _SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1]) ;Tell clients to remove user from user list
                        $list[$i][0] = 0 ;Clear out the socket in the array
                        $list[$i][1] = "" ;Remove username from the array
                     Case "kick" ;Client kicked
                        _Kick($temp[2])
                     Case "PM" ;Client recieving a PM
                        _PM($temp[2], $temp[3], $list[$i][1])
                     Case Else ;If it wasn't a command, it must be a message. Display it
                        If $recv[$j] Then _SendAll($recv[$j], $list[$i][1]) ;message, from
                        If $recv[$j] > (Chr(1)) Then ChatLog ($recv[$j], $list[$i][1]) ;Added this line to create logs. Will not log clients leaving
                  EndSwitch
               Next
            EndIf
         EndIf
      EndIf
  Next
EndFuncoÝ÷ Ø ÝêÞËazZ )àr^jëh×6Global $ChatLogFile
Global $DateNow
Global $TimeNow

$DateNow = @YEAR & "-" & @MON & "-" & @MDAY
$TimeNow = @HOUR & ":" & @MIN & ":" & @SEC

CreateLog()

Func CreateLog()
    
    Local $StartChatLog
    
    $ChatLogFile = FileOpen (@ScriptDir & "\ChatLog.txt", 1)

    If $ChatLogFile = -1 Then ;Couldn't create/open the logs, give an error
        MsgBox (0, "Error", "Unable to start chat log files.")
        Exit
    EndIf

    $StartChatLog = FileWriteLine ($ChatLogFile, "Chat log started on: " & $DateNow & " " & $TimeNow & @CRLF & @CRLF) ;Create a starting entry with date/time
    If $StartChatLog = -1 Then
        SetError(2)
        Return 0
    EndIf

    FileClose($ChatLogFile) ;Close the file

EndFunc

Func ChatLog ($Str, $From = "")
    ;This is a modified version of _FileWriteLog found in File.au3 in the AutoIt3 beta includes directory
    ;Credit to Jeremy Landes <jlandes at landeserve dot com>

    ;Set local variables
    Local $Msg
    Local $WriteFile

    $Msg = $DateNow & " " & $TimeNow & " : " & $From & " > " & $Str & @CRLF ;Create the message to log

    $ChatLogFile = FileOpen (@ScriptDir & "\ChatLog.txt", 1) ;Open the file for writing

    If $ChatLogFile = -1 Then ;Couldn't create/open the log, give an error
        MsgBox (0, "Error", "Unable to write to the chat log.")
        Exit
    EndIf

    If $From and StringLeft ($Str, 1) <> Chr(1) Then ;Check that this is a valid message to log
        $WriteFile = FileWriteLine ($ChatLogFile, $Msg) ;Write to the log
        If $WriteFile = -1 Then
            SetError(2)
            Return 0
        EndIf
    EndIf

    FileClose($ChatLogFile) ;Close the file
    Return 1
EndFunc

A little reading goes a long way. Post count means nothing.

Link to comment
Share on other sites

  • 1 month later...

Well i edited the code a bit for my server but now i have the problem that only 1 user can connect at time ? pls help :/

(when a second user connects the first one gets disconnected?...)

Client:

CODE
#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)

If IniRead(@ScriptDir & "\client.ini", "main", "port", "0") = "0" Then

$PORT = 31758

Else

$PORT = IniRead(@ScriptDir & "\client.ini", "main", "port", "")

EndIf

If IniRead(@ScriptDir & "\client.ini", "main", "ip", "0") = "0" Then

$IP1 = @IPAddress1

Else

$IP1 = IniRead(@ScriptDir & "\client.ini", "main", "ip", "")

EndIf

If IniRead(@ScriptDir & "\client.ini", "main", "user", "0") = "0" Then

$USERNAME = "Username"

Else

$USERNAME = IniRead(@ScriptDir & "\client.ini", "main", "user", "")

EndIf

;Global Const $PORT = 31758

Global $list, $sock

Global $userlist[17][2] ; [ username ][ ctrl id ]

TCPStartUp()

$gui = GUICreate("Login", 280, 160)

$ipctrl = GUICtrlCreateInput($IP1, 40, 10, 230, 20)

$userctrl = GUICtrlCreateInput($USERNAME, 40, 40, 230, 20)

$passctrl = GUICtrlCreateInput("", 40, 70, 230, 20, $ES_PASSWORD)

$portctrl = GUICtrlCreateInput($PORT, 40, 100, 230, 20)

GUICtrlCreateLabel("IP:", 2, 12, 35, 20, $ES_RIGHT)

GUICtrlCreateLabel("Nick:", 2, 42, 35, 20, $ES_RIGHT)

GUICtrlCreateLabel("Port:", 2, 102, 35, 20, $ES_RIGHT)

GUICtrlCreateLabel("Pass:", 2, 72, 35, 20, $ES_RIGHT)

$login = GUICtrlCreateButton("Login", 105, 130, 70, 20, $BS_DEFPUSHBUTTON)

GUICtrlSetOnEvent($login, "_Login")

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUISetState()

$loop = True

While $loop

Sleep(10)

WEnd

$gui = GUICreate("Client - " & $user, 460, 230)

$list = GUICtrlCreateListView(" Users ", 350, 10, 100, 210)

$edit = GUICtrlCreateEdit("", 10, 70, 330, 150, $ES_AUTOVSCROLL+$WS_VSCROLL+$ES_READONLY)

$input = GUICtrlCreateInput("", 10, 10, 330, 20)

$send = GUICtrlCreateButton("Send", 10, 40, 80, 20, $BS_DEFPUSHBUTTON)

$pm = GUICtrlCreateButton("PM", 100, 40, 80, 20)

GUICtrlSetOnEvent($send, "_Send")

GUICtrlSetOnEvent($pm, "_PM")

GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

GUICtrlSetState($input, $GUI_FOCUS)

GUISetState()

_ReduceMemory()

While 1

Sleep(0)

$recv = TCPRecv($sock, 2048)

If not $recv Then ContinueLoop

$recv = StringSplit($recv, Chr(1))

For $i = 1 to $recv[0]

$temp = StringSplit($recv[$i], Chr(2))

Switch $temp[1]

Case "rejected"

mError("Connection rejected.", 1)

Case "kick"

mError("Connection kicked.", 1)

Case "accepted"

;

Case "accepted2"

;If $USER = "admin" Then

$kick = GUICtrlCreateButton("Kick", 190, 40, 80, 20)

GUICtrlSetOnEvent($kick, "_Kick")

;EndIf

Case "adduser"

_AddUser($temp[2])

Case "deluser"

_DelUser($temp[2])

Case "exit"

mError("Server has closed.", 1)

Case Else

If $recv[$i] Then GUICtrlSetData($edit, $recv[$i] & @CRLF, 1)

EndSwitch

Next

_ReduceMemory()

WEnd

Func _Login()

$loop = False

IniWrite(@ScriptDir & "\client.ini", "main", "ip", GUICtrlRead($ipctrl))

IniWrite(@ScriptDir & "\client.ini", "main", "port", GUICtrlRead($portctrl))

IniWrite(@ScriptDir & "\client.ini", "main", "user", GUICtrlRead($userctrl))

$pass = GUICtrlRead($passctrl)

$PORT = GUICtrlRead($portctrl)

Global Const $IP = GUICtrlRead($ipctrl)

Global Const $USER = GUICtrlRead($userctrl)

GUIDelete($gui)

$sock = TCPConnect($IP, $PORT)

If $sock = -1 Then mError("Unable to connect.", 1)

;If $USER = "admin" Then

;Local $pass = InputBox("Enter Password:", "Please enter the server password below.", "", "")

If @error Then Return

TCPSend($sock, Chr(1) & $USER & Chr(2) & $pass)

;Else

; TCPSend($sock, $USER)

;EndIf

EndFunc

Func _Send()

TCPSend($sock, StringReplace(StringReplace(GUICtrlRead($input), Chr(1), ""), Chr(2), ""))

GUICtrlSetData($input, "")

EndFunc

Func _PM()

Local $user2 = GUICtrlRead($list), $data = StringReplace(StringReplace(GUICtrlRead($input), Chr(1), ""), Chr(2), "")

If not $user2 Then Return 0

$user2 = GUICtrlRead($user2)

If $user2 <> $user Then

TCPSend($sock, Chr(1) & "PM" & Chr(2) & $user2 & Chr(2) & $data)

GUICtrlSetData($input, "")

GUICtrlSetData($edit, "<** PM sent to: " & $user2 & " **>" & @CRLF & "<** " & $data & " **>" & @CRLF, 1)

EndIf

Return 1

EndFunc

Func _Kick()

Local $user2 = GUICtrlRead($list)

If not $user2 Then Return 0

$user2 = GUICtrlRead($user2)

TCPSend($sock, Chr(1) & "kick" & Chr(2) & $user2)

GUICtrlSetData($edit, "<** Kick: " & $user2 & " **>" & @CRLF, 1)

Return 1

EndFunc

Func _AddUser( $user2 )

;If $user = "admin" and not $userlist[16][0] Then

;$userlist[16][0] = $user

;$userlist[16][1] = GUICtrlCreateListViewItem($user, $list)

;Return 1

;EndIf

For $i = 0 to 15

If not $userlist[$i][0] Then

$userlist[$i][0] = $user2

$userlist[$i][1] = GUICtrlCreateListViewItem($user2, $list)

Return 1

EndIf

Next

Return 0

EndFunc

Func _DelUser( $user2 )

For $i = 0 to 16

If $userlist[$i][0] = $user2 Then

$userlist[$i][0] = ""

GUICtrlDelete($userlist[$i][1])

$userlist[$i][1] = 0

Return 1

EndIf

Next

Return 0

EndFunc

Func _ReduceMemory()

DllCall("psapi.dll", "int", "EmptyWorkingSet", "long", -1)

EndFunc

Func mError( $sText, $iFatal = 0, $sTitle = "Error", $iOpt = 0 )

Local $ret = MsgBox(48 + 4096 + 262144 + $iOpt, $sTitle, $sText)

If $iFatal Then Exit

Return $ret

EndFunc

Func _Exit()

Exit

EndFunc

Func OnAutoItExit()

TCPSend($sock, Chr(1) & "bye")

TCPShutdown()

Exit

EndFunc

Server:

CODE
;HotKeySet("{ESC}", "OnAutoItExit")

#include <INet.au3>

If IniRead(@ScriptDir & "\config.ini", "main", "pass", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "main", "pass", "admin")

$PASS = "admin"

Else

$PASS = IniRead(@ScriptDir & "\config.ini", "main", "pass", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "main", "port", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "main", "port", "31758")

$PORT = 31758

Else

$PORT = IniRead(@ScriptDir & "\config.ini", "main", "port", "")

EndIf

;Global Const $PASS = "admin"

Global Const $IP = @IPAddress1

Global Const $IP2 = _GetIP()

;Global Const $PORT = 31758

;$load = 0

Global $listen

Global $list[17][2]; [ socket ][ username ]

$log = FileOpen(@ScriptDir & "\log.txt", 1)

FileWrite($log, @CRLF & "---------------------------" & @CRLF)

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " - Server Started" & @CRLF)

TCPStartup()

$listen = TCPListen($IP, $PORT, 100)

If $listen = -1 Then mError("Unable to connect.", 1)

_ReduceMemory()

While 1

FileClose($log)

$log = FileOpen(@ScriptDir & "\log.txt", 1)

_CheckConn()

Sleep(0)

$sock = TCPAccept($listen)

If $sock = -1 Then ContinueLoop

_AddConn($sock)

_ReduceMemory()

WEnd

Func _AddConn( $iSocket )

Local $recv = _SockRecv($iSocket), $temp

If @error Then

_DelConn($iSocket)

Else

$temp = StringSplit($recv, Chr(2))

$username = StringReplace($temp[1], Chr(1), "")

If $temp[0] >= 2 Then

For $i = 0 to 15

$list[$i][0] = $iSocket

$list[$i][1] = $username

If $temp[2] = $PASS Then

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " Admin logged in: " & $username & @CRLF)

TCPSend($iSocket, Chr(1) & "accepted2")

Else

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " User logged in: " & $username & @CRLF)

TCPSend($iSocket, Chr(1) & "accepted")

EndIf

_SendUsers($iSocket)

_SendAll(Chr(1) & "adduser" & Chr(2) & $username, "", $iSocket)

Return True

Next

TCPSend($iSocket, Chr(1) & "rejected")

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " User connection rejected (i error): " & $username & @CRLF)

TCPCloseSocket($iSocket)

Else

TCPSend($iSocket, Chr(1) & "rejected")

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " User connection rejected (temp error): " & $username & @CRLF)

EndIf

EndIf

Return False

EndFunc

Func _DelConn( $iSocket )

For $i = 0 to 16

If $list[$i][0] = $iSocket Then

TCPCloseSocket($iSocket)

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " Connection socket deleted: " & $iSocket & @CRLF)

$list[$i][0] = 0

$list[$i][1] = ""

Return

EndIf

Next

EndFunc

Func _CheckConn()

;If $load = 0 Then

If IniRead(@ScriptDir & "\config.ini", "cs", "folder", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "cs", "folder", "F:\hlserver")

$start_cs = "F:\hlserver"

Else

$start_cs = IniRead(@ScriptDir & "\config.ini", "cs", "folder", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "cs", "exe", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "cs", "exe", "hlds.exe")

$file_cs = "hlds.exe"

Else

$file_cs = IniRead(@ScriptDir & "\config.ini", "cs", "exe", "hlds.exe")

EndIf

If IniRead(@ScriptDir & "\config.ini", "cs", "para", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "cs", "para", "-game cstrike +maxplayers 22 -nojoy +port 27016 +map de_dust2 +servercfgfile server.cfg +mapcyclefile mapcycle.txt +motdfile motd.txt +logsdir logs -zone 2048 -console -insecure")

$para_cs = "-game cstrike +maxplayers 22 -nojoy +port 27016 +map de_dust2 +servercfgfile server.cfg +mapcyclefile mapcycle.txt +motdfile motd.txt +logsdir logs -zone 2048 -console -insecure"

Else

$para_cs = IniRead(@ScriptDir & "\config.ini", "cs", "para", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "cs", "port", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "cs", "port", ":27016")

$port_cs = ":27016"

Else

$port_cs = IniRead(@ScriptDir & "\config.ini", "cs", "port", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "wow", "folder", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "wow", "folder", "F:\WoWServer\delfin")

$start_wow = "F:\WoWServer\delfin"

Else

$start_wow = IniRead(@ScriptDir & "\config.ini", "wow", "folder", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "wow", "exe", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "wow", "exe", "GameServer.exe")

$file_wow = "GameServer.exe"

Else

$file_wow = IniRead(@ScriptDir & "\config.ini", "wow", "exe", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "wow", "exe2", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "wow", "exe2", "LoginServer.exe")

$file_wow2 = "LoginServer.exe"

Else

$file_wow2 = IniRead(@ScriptDir & "\config.ini", "wow", "exe2", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "wow", "port", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "wow", "port", ":8085 (Login server: 3724)")

$port_wow = ":8085 (Login server: 3724)"

Else

$port_wow = IniRead(@ScriptDir & "\config.ini", "wow", "port", "")

EndIf

;If IniRead(@ScriptDir & "\config.ini", "l2", "port", "") = "" Then

; IniWrite(@ScriptDir & "\config.ini", "l2", "port", ":?")

; $port_l2 = ":?"

;Else

; $port_l2 = IniRead(@ScriptDir & "\config.ini", "l2", "port", "")

;EndIf

If IniRead(@ScriptDir & "\config.ini", "ts", "folder", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "ts", "folder", "F:\Teamspeak2_RC2")

$start_ts = "F:\Teamspeak2_RC2"

Else

$start_ts = IniRead(@ScriptDir & "\config.ini", "ts", "folder", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "ts", "exe", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "ts", "exe", "server_windows.exe")

$file_ts = "server_windows.exe"

Else

$file_ts = IniRead(@ScriptDir & "\config.ini", "ts", "exe", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "ts", "port", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "ts", "port", ":8767")

$port_ts = ":8767"

Else

$port_ts = IniRead(@ScriptDir & "\config.ini", "ts", "port", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "ven", "folder", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "ven", "folder", "F:\VentSrv")

$start_ven = "F:\VentSrv"

Else

$start_ven = IniRead(@ScriptDir & "\config.ini", "ven", "folder", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "ven", "exe", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "ven", "exe", "ventrilo_srv.exe")

$file_ven = "ventrilo_srv.exe"

Else

$file_ven = IniRead(@ScriptDir & "\config.ini", "ven", "exe", "")

EndIf

If IniRead(@ScriptDir & "\config.ini", "ven", "port", "") = "" Then

IniWrite(@ScriptDir & "\config.ini", "ven", "port", ":?")

$port_ven = ":?"

Else

$port_ven = IniRead(@ScriptDir & "\config.ini", "ven", "port", "")

EndIf

;$load = 1

;EndIf

Local $recv = ""

For $i = 0 to 16

If $list[$i][0] Then

$recv = TCPRecv($list[$i][0], 2048)

If @error Then

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " recv error" & @CRLF)

TCPCloseSocket($list[$i][0])

_SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])

$list[$i][0] = 0

$list[$i][1] = ""

Else

If $recv Then

$recv = StringSplit($recv, Chr(1))

For $j = 1 to $recv[0]

$temp = StringSplit($recv[$j], Chr(2))

Switch $temp[1]

Case "bye"

TCPCloseSocket($list[$i][0])

_SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " User Disconnected: " & $list[$i][1] & @CRLF)

$list[$i][0] = 0

$list[$i][1] = ""

Case "kick"

If $temp[0] >= 2 Then

_Kick($temp[2])

EndIf

Case "PM"

If $temp[0] >= 3 Then

_PM($temp[2], $temp[3], $list[$i][1])

EndIf

Case Else

;--------------------------------------------

;If $recv[$j] = "help" Then

; - command

; _PM($list[$i][1], "MuHahAha", "")

If $recv[$j] = "shutdown" Then

_SendAll("Shuting down", $list[$i][1], 0, 1)

Shutdown(13)

ElseIf $recv[$j] = "hibernate" Then

_SendAll("Hibernating", $list[$i][1], 0, 1)

;_SendAll($list[$i][1], "Hibernating", "")

Shutdown(64)

ElseIf StringLeft($recv[$j], 5) = "start" Then

If $recv[$j] = "start cs" Then

Run(@ComSpec & " /C """ & $start_cs & "\" & $file_cs & " " & $para_cs & "", $start_cs)

;_PM($list[$i][1], "cs server is now online", "")

_SendAll("cs server is now online", $list[$i][1], 0, 1)

ElseIf $recv[$j] = "start wow" Then

Run(@ComSpec & " /C """ & $start_wow & "\" & $file_wow & "", $start_wow)

Run(@ComSpec & " /C """ & $start_wow & "\" & $file_wow2 & "", $start_wow)

;_PM($list[$i][1], "wow and wow login server are now online", "")

_SendAll("wow and wow login server are now online", $list[$i][1], 0, 1)

;ElseIf $recv[$j] = "start l2" Then

;

ElseIf $recv[$j] = "start ts" Then

Run(@ComSpec & " /C """ & $start_ts & "\" & $file_ts & "", $start_ts)

ElseIf $recv[$j] = "start ven" Then

Run(@ComSpec & " /C """ & $start_ven & "\" & $file_ven & "", $start_ven)

Else

$start_error = StringSplit($recv[$j], " ")

If $start_error[0] > 1 Then

_PM($list[$i][1], "invalid application: " & $start_error[2], "")

Else

_PM($list[$i][1], "Please enter an app to launch (""apps help"" for more info)", "")

EndIf

EndIf

ElseIf StringLeft($recv[$j], 4) = "stop" Then

If $recv[$j] = "stop cs" Then

If ProcessExists($file_cs) Then

ProcessClose($file_cs)

;_PM($list[$i][1], "cs server is now offline", "")

_SendAll("cs server is now offline", $list[$i][1], 0, 1)

Else

_PM($list[$i][1], "cs server is already offline", "")

EndIf

ElseIf $recv[$j] = "stop wow" Then

If ProcessExists($file_wow) Then

ProcessClose($file_wow)

;_PM($list[$i][1], "wow server is now offline", "")

_SendAll("wow server is now offline", $list[$i][1], 0, 1)

Else

_PM($list[$i][1], "wow server is already offline", "")

EndIf

If ProcessExists($file_wow2) Then

ProcessClose($file_wow2)

_SendAll("wow login server is now offline", $list[$i][1], 0, 1)

;_PM($list[$i][1], "wow login server is now offline", "")

Else

_PM($list[$i][1], "wow login server is already offline", "")

EndIf

;ElseIf $recv[$j] = "stop l2" Then

;

ElseIf $recv[$j] = "stop ts" Then

If ProcessExists($file_ts) Then

ProcessClose($file_ts)

_SendAll("ts server is now offline", $list[$i][1], 0, 1)

;_PM($list[$i][1], "ts server is now offline", "")

Else

_PM($list[$i][1], "ts server is already offline", "")

EndIf

ElseIf $recv[$j] = "stop ven" Then

If ProcessExists($file_ven) Then

ProcessClose($file_ven)

_SendAll("ven server is now offline", $list[$i][1], 0, 1)

;_PM($list[$i][1], "ven server is now offline", "")

Else

_PM($list[$i][1], "ven server is already offline", "")

EndIf

Else

$stop_error = StringSplit($recv[$j], " ")

If $stop_error[0] > 1 Then

_PM($list[$i][1], "invalid application: " & $stop_error[2], "")

Else

_PM($list[$i][1], "Please enter an app to shutdown (""apps help"" for more info)", "")

EndIf

EndIf

ElseIf StringLeft($recv[$j], 4) = "apps" Then

If $recv[$j] = "apps ip" Then

$apps_ip_01 = "cs - " &$ip2 &$port_cs

$apps_ip_02 = "wow - " &$ip2 &$port_wow

;$apps_ip_03 = "l2 - " &$ip &$port_l2

$apps_ip_04 = "ts - " &$ip2 &$port_ts

$apps_ip_05 = "ven - " &$ip2 &$port_ven

_PM($list[$i][1], @CRLF &$apps_ip_01 &@CRLF &$apps_ip_02 &@CRLF &$apps_ip_04 &@CRLF &$apps_ip_05, "")

ElseIf $recv[$j] = "apps online" Then

If ProcessExists($file_cs) Then

$running_cs = "Online"

Else

$running_cs = "Offline"

EndIf

If ProcessExists($file_wow) Then

$running_wow = "Online"

Else

$running_wow = "Offline"

EndIf

If ProcessExists($file_wow2) Then

$running_wow2 = "Online"

Else

$running_wow2 = "Offline"

EndIf

If ProcessExists($file_ts) Then

$running_ts = "Online"

Else

$running_ts = "Offline"

EndIf

If ProcessExists($file_ven) Then

$running_ven = "Online"

Else

$running_ven = "Offline"

EndIf

_PM($list[$i][1], @CRLF &"cs - " &$running_cs &@CRLF &"wow server - " &$running_wow &@CRLF &"wow login server - " &$running_wow2 &@CRLF &"ts server - " &$running_ts &@CRLF &"ven server - " &$running_ven, "")

ElseIf $recv[$j] = "apps help" Then

$apps_help_01 = "cs - Counter-Strike 1.6 game server"

$apps_help_02 = "wow - Word Of Warcraft 1.12.1 game server (Delphin 0.1.0.44 server)"

;$apps_help_03 = "l2 - Lineage 2 game server"

$apps_help_04 = "ts - Team Speak speech server"

$apps_help_05 = "ven - Ventrilo speech server"

_PM($list[$i][1], @CRLF &$apps_help_01 &@CRLF &$apps_help_02 &@CRLF &$apps_help_04 &@CRLF &$apps_help_05, "")

Else

$apps_error = StringSplit($recv[$j], " ")

If $apps_error[0] > 1 Then

_PM($list[$i][1], "invalid command: " & $apps_error[2], "")

Else

_PM($list[$i][1], "use ""help"" for more info", "")

EndIf

EndIf

ElseIf $recv[$j] = "help" Then

$help_01 = "help - This list"

;$help_02 = "";".pass <pass> - Enter the server login pass here"

$help_03 = "shutdown - Shutdown the server"

$help_04 = "hibernate - Hibernate the server"

$help_05 = "start <servername> - start a cs/wow/l2/ts/ven server"

$help_06 = "stop <servername> - stop a server if running"

$help_07 = "apps help - info about available servers"

$help_08 = "apps online - see the status of the servers"

$help_09 = "apps ip - view ips of the servers"

;$help_10 = "";".adduser - adds an user to the server's msn"

_PM($list[$i][1], @CRLF &$help_01 &@CRLF &$help_03 &@CRLF &$help_04 &@CRLF &$help_05 &@CRLF &$help_06 &@CRLF &$help_07 &@CRLF &$help_08 &@CRLF &$help_09, "")

ElseIf $recv[$j] Then

_SendAll($recv[$j], $list[$i][1])

EndIf

;--------------------------------------------

EndSwitch

Next

EndIf

EndIf

EndIf

Next

EndFunc

Func _SendAll( $str, $from = "", $except = 0, $com = 0 )

For $i = 0 to 16

If $list[$i][0] and $list[$i][0] <> $except Then

If $from and StringLeft($str, 1) <> Chr(1) Then

If $com = 1 Then

TCPSend($list[$i][0], $from & " C> " & $str)

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " MessageC(" &$from& "): " & $str & @CRLF)

Else

TCPSend($list[$i][0], $from & " > " & $str)

;$array = StringSplit($str, Chr(2) & Chr(1))

;ConsoleWrite($str)

;If $str = $array[1] Then

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " Message(" &$from& "): " & $str & @CRLF)

;EndIf

EndIf

Else

;FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " Message: " & $str & @CRLF)

TCPSend($list[$i][0], $str)

EndIf

EndIf

Next

EndFunc

Func _Kick( $user )

For $i = 0 to 15

If $list[$i][1] = $user Then

TCPSend($list[$i][0], Chr(1) & "kick")

TCPCloseSocket($list[$i][0])

$list[$i][0] = 0

_SendAll(Chr(1) & "deluser" & Chr(2) & $list[$i][1])

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " User kicked: " & $user & @CRLF)

$list[$i][1] = ""

Return

EndIf

Next

EndFunc

Func _PM( $user, $str, $from )

If $user <> $from AND $str <> "" Then

For $i = 0 to 16

If $list[$i][1] = $user Then

TCPSend($list[$i][0], "~" & $from & "> " & $str)

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " User PM (" &$from&" to "&$user& "): " & $str & @CRLF)

Return

EndIf

Next

EndIf

EndFunc

Func _SendUsers( $iSocket )

For $i = 0 to 16

If $list[$i][0] Then TCPSend($iSocket, Chr(1) & "adduser" & Chr(2) & $list[$i][1])

Next

EndFunc

Func _SockRecv( $iSocket, $iBytes = 2048 )

Local $sData = ""

While $sData = ""

$sData = TCPRecv($iSocket, $iBytes)

If @error Then

SetError(1)

Return ""

EndIf

Wend

Return $sData

EndFunc

Func _ReduceMemory()

DllCall("psapi.dll", "int", "EmptyWorkingSet", "long", -1)

EndFunc

Func mError( $sText, $iFatal = 0, $sTitle = "Error", $iOpt = 0 )

Local $ret = MsgBox(48 + 4096 + 262144 + $iOpt, $sTitle, $sText)

If $iFatal Then Exit

Return $ret

EndFunc

Func OnAutoItExit()

FileWrite($log, @HOUR &":"& @MIN &" "& @MDAY &"\"& @MON & "\" & @YEAR & " - Server shuting down" & @CRLF)

FileClose($log)

_SendAll(Chr(1) & "exit")

TCPShutdown()

Exit

EndFunc

Edited by Dellairion
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...