Jump to content

_SQLite_Close()


Recommended Posts

Ok so I've tried adding _SQLite_QueryFinalize($Query) and I've tried doing something like

_SQLite_QueryFinalize(_SQLite_Exec($database,"SELECT * FROM users",$Query))

but they both throw the error,even when given together like

_SQLite_QueryFinalize(_SQLite_Exec($database,"SELECT * FROM users",$Query))
_SQLite_QueryFinalize($Query)
_SQLite_Close()

I just can't wrap my head around this one, what am I doing wrong?

P.S. Yes I tried _SQLite_Close($database) too.

Link to comment
Share on other sites

@

Try to run the example from the HELP file.

See if that works. If so you have a wrong syntax in your code.

#include <SQLite.au3>
#include <SQLite.dll.au3>

Local $hQuery, $aRow, $aNames
_SQLite_Startup ()
_SQLite_Open () ; open :memory: Database
_SQLite_Exec (-1, "CREATE TABLE aTest (a,b,c);")
_SQLite_Exec (-1, "INSERT INTO aTest(a,b,c) VALUES ('c','2','World');")
_SQLite_Exec (-1, "INSERT INTO aTest(a,b,c) VALUES ('b','3',' ');")
_SQLite_Exec (-1, "INSERT INTO aTest(a,b,c) VALUES ('a','1','Hello');")
_SQlite_Query (-1, "SELECT ROWID,* FROM aTest ORDER BY a;", $hQuery)
_SQLite_FetchNames ($hQuery, $aNames) ; Read out the Tablenames
MsgBox(0,"SQLite","Row ID is : " & StringFormat(" %-10s  %-10s  %-10s  %-10s ", $aNames[0], $aNames[1], $aNames[2], $aNames[3]) & @CR)
While _SQLite_FetchData ($hQuery, $aRow) = $SQLITE_OK ; This get 1 row at a time
    MsgBox(0,"SQLite","Get Data using FetchData : " &  StringFormat(" %-10s  %-10s  %-10s  %-10s ", $aRow[0], $aRow[1], $aRow[2], $aRow[3]) & @CR)
    _SQLite_QueryFinalize ($hQuery) ; This will stop the query, getting more rows
WEnd
_SQLite_Exec (-1, "DROP TABLE aTest;")
_SQLite_Close ()
_SQLite_Shutdown ()

;~ Output:
;~  
;~  rowid       a           b           c          
;~  3           a           1           Hello

regards

ptrex

Link to comment
Share on other sites

Well that worked but the weird thing is that it only happens sometimes. I'm going to confess I got a chat server/client off the forums (can't remember who wrote it) and I'm trying to modify it to work a bit more effectively (at least for my purposes). I have a few things working better but that still doesn't work properly.

CODE
#RequireAdmin

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****

#AutoIt3Wrapper_outfile=Chat Server.exe

#AutoIt3Wrapper_Compression=4

#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <GUIConstantsEx.au3>

#include <WindowsConstants.au3>

#include <EditConstants.au3>

#include <StaticConstants.au3>

#include <ScrollBarConstants.au3>

#include <GuiEdit.au3>

#include <SQLite.au3>

#include <SQLite.dll.au3>

#include <GUIListBox.au3>

#Include <GuiListView.au3>

#include <RC4.au3>

;First Set the tray menu.

Opt("TrayMenuMode",1)

;Start up TCP.

TCPStartup ()

_SQLite_Startup()

#region Setings

Global $Ip = "0.0.0.0"

Global $Port = 31758

Global $MaxUsers = 100

Global $Encrypt_Key = "@µf¤12" ;This must mach the clients encrypting key.

Global $Log_File = @ScriptDir & "\Logs\log - " & @MDAY & "-" & @MON & "-" & @YEAR & ".log"

Global $Update_Key_Time = 15 ;Time in Sec for how often the server should send a new key to the user, 0 disables it.

Global $Database_File = @ScriptDir & "\Database.db3"

#endregion

#region Declear $var

Global $User_List[$MaxUsers+1][6]

;[0] = socket

;[1] = Username

;[2] = encryption key

;[3] = Unique key timer

;[4] = old encryption key

;[5] = ListviewItem Control for dynamic updateing.

Global $Users_Online = 0

Global $Admins_Online = 0

Global $Refresh_Timer = TimerInit()

Global $Uniqe_Key_Refresh = TimerInit()

Global $Query, $SQL_Data

Global $Selected_ID

Global $Listview_Color = 0xeeeeff

$Update_Key_Time *= 1000

#endregion

$Tray_Menu_Showconsole = TrayCreateItem("Show Console")

$Tray_Menu_Shutdown = TrayCreateItem("Shutdown Server")

If Not FileExists($Database_File) Then

$SQL_DB = _SQLite_Open($Database_File)

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB,"CREATE TABLE Accounts (id INTEGER PRIMARY KEY,username TEXT,password TEXT,admin INTEGER,ban INTEGER,online INTEGER)"))

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB,"INSERT INTO Accounts(username,password,admin,ban,online) VALUES ('ADMIN','Admin',1,0,0);"))

Else

$SQL_DB = _SQLite_Open($Database_File)

EndIf

$Log_File = FileOpen($Log_File,9)

;Create the console gui.

$Form1 = GUICreate("Chat Server", 625, 362, 193, 125)

$Tab1 = GUICtrlCreateTab(5, 5, 611, 351)

$TabSheet1 = GUICtrlCreateTabItem("Chat")

$Edit1 = GUICtrlCreateEdit("", 15, 35, 466, 286,BitOR($ES_AUTOVSCROLL,$ES_WANTRETURN,$WS_VSCROLL,$ES_READONLY))

GUICtrlSetBkColor(-1,0xFFFFFF)

$Input1 = GUICtrlCreateInput("", 15, 325, 421, 21)

$Button1 = GUICtrlCreateButton("Kick", 560, 325, 40, 20, 0)

$Button2 = GUICtrlCreateButton("Ban", 520, 325, 35, 20, 0)

$Button3 = GUICtrlCreateButton("Send", 485, 325, 30, 20, 0x0001)

$List1 = GUICtrlCreateList("", 485, 30, 121, 292)

$Button4 = GUICtrlCreateButton("PM", 445, 325, 35, 20, 0)

$TabSheet2 = GUICtrlCreateTabItem("Database")

$Listview1 = GUICtrlCreateListView("Id|Username|Password|Admin|Ban|Online", 10, 35, 595, 255)

$Label1 = GUICtrlCreateLabel("Username", 10, 295, 52, 17)

$Input2 = GUICtrlCreateInput("", 65, 295, 121, 21)

$Label2 = GUICtrlCreateLabel("Password", 10, 325, 50, 17)

$Input3 = GUICtrlCreateInput("", 65, 320, 121, 21)

$Checkbox1 = GUICtrlCreateCheckbox("Banned", 190, 300, 62, 17)

$Checkbox2 = GUICtrlCreateCheckbox("Admin", 190, 325, 57, 17)

$Button5 = GUICtrlCreateButton("Save", 255, 300, 55, 20, 0)

$Button6 = GUICtrlCreateButton("Delete", 255, 325, 55, 20, 0)

$Button7 = GUICtrlCreateButton("Create", 315, 300, 55, 20, 0)

$TabSheet3 = GUICtrlCreateTabItem("Log")

$Listview2 = GUICtrlCreateListView("Time|Dir|IP|Command|Param1|Param2|Param3|", 10, 35, 595, 314)

GUICtrlSendMsg($Listview1, 0x101E, 0, 25)

GUICtrlSendMsg($Listview1, 0x101E, 1, 100)

GUICtrlSendMsg($Listview1, 0x101E, 2, 100)

GUICtrlSendMsg($Listview1, 0x101E, 3, 50)

GUICtrlSendMsg($Listview1, 0x101E, 4, 50)

GUICtrlSendMsg($Listview1, 0x101E, 5, 50)

GUICtrlSendMsg($Listview2, 0x101E, 0, 65)

GUIRegisterMsg(0x004E,'_Listview')

;start to listen for users and messages.

$MainSocket = TCPListen($Ip, $Port,$MaxUsers)

;if the server was unable to connect to the ip and port then

If $MainSocket = -1 Then

;Show the error.

MsgBox(16,"Error","Uable to Connect." & @CRLF & "IP: " & $Ip & @CRLF & "Port: " & $Port)

;exit.

Exit

EndIf

_LoadDatabase()

GUISetState(@SW_SHOW)

While 1

If GUICtrlRead($Checkbox1) = 1 Then

$Set_Ban = 1

Else

$Set_Ban = 0

EndIf

If GUICtrlRead($Checkbox2) = 1 Then

$Set_Admin = 1

Else

$Set_Admin = 0

EndIf

;get Tray menu message

$Tray_Message = TrayGetMsg()

;Handle the message

Switch $Tray_Message

;If the message is to show the console

Case $Tray_Menu_Showconsole

;Then show the console.

GUISetState(@SW_SHOW,$Form1)

GUISetState(@SW_RESTORE)

;Case it is to shutdown the server.

Case $Tray_Menu_Shutdown

OnAutoitExit()

;Safly shut it down

Exit

EndSwitch

;Get Gui Message.

$Gui_Message = GUIGetMsg()

;Handle the gui Message

Switch $Gui_Message

Case $GUI_EVENT_CLOSE

OnAutoitExit()

Case $GUI_EVENT_MINIMIZE

GUISetState(@SW_HIDE)

Case $Button1 ;Kick

_Kick(GUICtrlRead($List1))

Case $Button2 ;Ban

_Ban(GUICtrlRead($List1))

Case $Button3 ;Say

_SendAll( "message", GUICtrlRead($Input1) & Chr(2) & "1000,0x0000FF,Lucida Console,14", 'SYSTEM')

_GUICtrlEdit_AppendText($Edit1,'SYSTEM: ' & GUICtrlRead($Input1) & @CRLF)

GUICtrlSetData($Input1, '')

Case $Button4 ;PM

_PM('SYSTEM',GUICtrlRead($List1),GUICtrlRead($Input1))

Case $Button5 ;Save, saves the changes to the user

$Selected_ID = _GUICtrlListView_GetSelectedIndices($Listview1) + 1

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB, "UPDATE Accounts SET username = '" & StringUpper(GUICtrlRead($Input2)) & "', password = '" & GUICtrlRead($Input3) & "', ban = " & $Set_Ban & ", Admin = " & $Set_Admin & ", online = 0 WHERE id = " & $Selected_ID & ";"))

GUICtrlSetData(GUICtrlRead($Listview1),$Selected_ID & '|' & StringUpper(GUICtrlRead($Input2)) & '|' & GUICtrlRead($Input3) & '|' & $Set_Admin & '|' & $Set_Ban & '|0')

Case $Button6 ;Delete, removes the user

$Selected_ID = _GUICtrlListView_GetSelectedIndices($Listview1) + 1

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB, 'DELETE FROM Accounts WHERE id = ' & $Selected_ID & ';'))

GUICtrlDelete(GUICtrlRead($Listview1))

Case $Button7 ;Create, Creates a new user in a safe maner.

_CreateUser(GUICtrlRead($Input2), GUICtrlRead($Input3),$Set_Ban, $Set_Admin)

EndSwitch

;check if we should update the user info.

;This is to avoid flickering.

If TimerDiff($Refresh_Timer) > 1000 Then

;Update the userinfo

;~ GUICtrlSetData($Label1,"Users Online: " & $Users_Online)

;~ GUICtrlSetData($Label2,"Admins Online: " & $Admins_Online)

;Restart the refresh timer.

$Refresh_Timer = TimerInit()

EndIf

;Gather Data and look what should be done with it.

_RecvData()

;Look if a new user is trying to connect

$New_Socket = TCPAccept($MainSocket)

;if not the start while 1 loop over again

If $New_Socket = -1 Then ContinueLoop

;Add the user that is trying to connect.

_AddUser($New_Socket)

WEnd

Func _Listview($hWnd, $Msg, $wParam, $lParam)

;0x0089ED88, 0x0089EEB4 = Selection changed in listview, 0x0089EEB4 for XP and 0x0089ED88 for vista.

If $lParam = 0x0089ED88 Or $lParam = 0x0089EEB4 Then

If GUICtrlRead($Tab1) <> 1 Then Return

;Guictrlread($Listview1) returns the control handle for the selected listview item.

;Read that items data and split it by '|'

$Data = StringSplit(GUICtrlRead(GUICtrlRead($Listview1)),'|')

If $Data[0] <> 7 Then Return

$Selected_ID = $Data[1]

GUICtrlSetData($Input2, $Data[2])

GUICtrlSetData($Input3, $Data[3])

If $Data[4] = '1' Then

GUICtrlSetState($Checkbox2,$GUI_CHECKED)

Else

GUICtrlSetState($Checkbox2, $GUI_UNCHECKED)

EndIf

If $Data[5] = '1' Then

GUICtrlSetState($Checkbox1,$GUI_CHECKED)

Else

GUICtrlSetState($Checkbox1, $GUI_UNCHECKED)

EndIf

EndIf

EndFunc

Func _AddUser($iSocket)

;Enter a For loop to check all the users if there is a spot open.

For $x = 1 to $MaxUsers

;If there is a spot open then

If Not $User_List[$x][0] Then

;Set the user to that spot

$User_List[$x][0] = $iSocket

$User_List[$x][2] = _RandomString()

$User_List[$x][3] = TimerInit()

;Send the user his unique key

TCPSend($User_List[$x][0],_StringEncryptRC4( $User_List[$x][2], $Encrypt_Key))

Return 1

EndIf

Next

Return 0

EndFunc

Func _RecvData()

;Enter a loop to check all the users.

For $x = 1 To $MaxUsers

If Not $User_List[$x][0] Then ContinueLoop

;Receive data from the users.

$Recv = TCPRecv($User_List[$x][0],4096)

;If you dont get any data then skip the rest of the loop.

If $Recv = "" Then ContinueLoop

;If @error then the user has problerly disconnected.

If @error Then _DelSocket($User_List[$x][0])

$Recv = _StringDecryptRC4( $Recv, $User_List[$x][2])

If StringLeft($Recv,1) <> Chr(3) Then

$Recv = _StringDecryptRC4( $Recv, $User_List[$x][4])

EndIf

;check if its time to update the users key, This proberly the best spot to add it aswell, after decrypting everything.

If $Update_Key_Time > 0 And TimerDiff($User_List[$x][3]) > $Update_Key_Time Then _KeyGen($x)

;Split the data by when it was sent from the user.

$Data = StringSplit(StringTrimLeft($Recv,1),Chr(1))

If Not IsArray($Data) then ContinueLoop

;Log the data.

_ConsoleWrite("In|" & _TCPSocketToIP($User_List[$x][0]) & "|" & StringReplace(StringTrimLeft(StringTrimRight($Recv,1),1),Chr(2),"|") & @CRLF)

;Loop trought all the data from the user.

For $y = 1 To UBound($Data)-1

;Split that data to the real data we need.

$Real_Data = StringSplit($Data[$y],Chr(2))

;The data looks like this: Command chr(2) Parameter1 chr(2) Parameter2 chr(2) Parameter...

;check and see if we have enought data to work with.

If $Real_data[0] < 2 Then ContinueLoop

Switch $Real_Data[1]

Case "Logon"

;To avoid people sending fake signales in a atempt to crash the server.

If $Real_data[0] = 3 Then

Switch _Login($Real_data[2], $Real_data[3], $x)

Case 0 ;No such user or wrong password

_TCPSend($User_List[$x][0],"Logon" & Chr(2) & "fail",$User_List[$x][2])

_DelSocket($User_List)

Case 1 ;User is banned

_TCPSend($User_List[$x][0],"Logon" & Chr(2) & "Ban",$User_List[$x][2])

_DelSocket($User_List)

Case 2 ;User is already online

_TCPSend($User_List[$x][0],"Logon" & Chr(2) & "online",$User_List[$x][2])

_DelSocket($User_List)

Case 3 ;User can login :(

_TCPSend($User_List[$x][0],"Logon" & Chr(2) & "ok" & Chr(2) & '0',$User_List[$x][2])

_SendAll("Adduser", $User_List[$x][1])

Case 4 ;Admin can login

_TCPSend($User_List[$x][0],"Logon" & Chr(2) & "ok" & Chr(2) & '1',$User_List[$x][2])

_SendAll("Adduser", $User_List[$x][1])

EndSwitch

Else

;send him a message that he had some kind of error.

_TCPSend($User_List[$x][0],"Logon" & Chr(2) & "error", $User_List[$x][2])

_DelSocket($User_List)

EndIf

Case "Register"

;check and see if the $Real_data array is safe to use.

If $Real_data[0] = 3 Then

Switch _CreateUser($Real_data[2], $Real_Data[3])

Case 0

;else send him a message that that user is already in use.

_TCPSend($User_List[$x][0],"Register" & Chr(2) & "fail", $User_List[$x][2])

_DelSocket($User_List[$x][0])

Case 1

;else send him a message that that user is already in use.

_TCPSend($User_List[$x][0],"Register" & Chr(2) & "invalid", $User_List[$x][2])

_DelSocket($User_List[$x][0])

Case 2

;if not the send an ok message to the user.

_TCPSend($User_List[$x][0],"Register" & Chr(2) & "ok", $User_List[$x][2])

_DelSocket($User_List[$x][0])

EndSwitch

Else

;Send him a message that he had some kind of error.

_TCPSend($User_List[$x][0],"Register" & Chr(2) & "error", $User_List[$x][2])

_DelSocket($User_List[$x][0])

EndIf

Case "msg"

;If someone wants to chat then check we have enought options

If $Real_data[0] = 3 Then

_GUICtrlEdit_AppendText($Edit1,$User_List[$x][1] & ': ' & $Real_data[2] & @CRLF)

;send the message to them all.

_SendAll( "message", $Real_data[2] & Chr(2) & $Real_Data[3], $User_List[$x][1])

EndIf

Case "getuser"

_SendUserList($User_List[$x][0],$User_List[$x][2])

Case "Bye"

_DelSocket($User_List[$x][0])

Case "pm"

If $Real_data[0] = 3 Then _Pm($User_List[$x][1],$Real_data[2], $Real_data[3])

Case "kick"

;To avoid people sending fake signales in a atempt to crash the server.

If UBound($Real_Data) > 2 Then

;Check if the user that is trying to kick another user is admin.

If _IsAdmin($User_List[$x][1]) = False Then

;the user was not a admin, Kick him from the server.

_PM("Console",$User_List[$x][1],"You used a function you are now allowed to use.")

_PM("Console",$User_List[$x][1],"You will now be kicked from the server.")

_Kick($User_List[$x][1])

EndIf

EndIf

Case "Ban"

;To avoid people sending fake signales in a atempt to crash the server.

If $Real_data[0] = 2 Then

;Check if the user that is trying to kick another user is admin.

If _IsAdmin($User_List[$x][1]) = True Then

;check if the user we are trying to kick is admin or not.

If _IsAdmin($Real_Data[2]) = False Then

;Ban him if he is not a admin.

_Ban($Real_Data[2])

EndIf

Else

;the user was not a admin, Kick him from the server.

_PM("Console",$User_List[$x][1],"You used a function you are now allowed to use.")

_PM("Console",$User_List[$x][1],"You will now be kicked from the server.")

_Kick($User_List[$x][1])

EndIf

EndIf

Case "unban"

;To avoid people sending fake signales in a atempt to crash the server.

If $Real_data[0] = 2 Then

;Check if the user that is trying to kick another user is admin.

If _IsAdmin($User_List[$x][1]) = False Then

;check if the user we are trying to kick is admin or not.

If _IsAdmin($Real_Data[2]) = True Then

;check if the user that we are trying to ban exists.

_SQLite_QueryFinalize(_SQLite_Query($SQL_DB, "SELECT id FROM Accounts WHERE username = '" & StringUpper($Real_data[2]) & "';", $Query))

If _SQLite_FetchData($Query,$SQL_Data) = $SQLITE_OK Then

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB,"UPDATE Accounts SET ban = 0 WHERE username = '" & StringUpper($Real_Data[2]) & "';"))

_TCPSend($User_List[$x][0],"Unban" & Chr(2) & "ok", $User_List[$x][2])

$User_Index = _GUICtrlListView_FindText($Listview1,$SQL_Data[0],-1, False)

If $User_Index> -1 Then _GUICtrlListView_SetItemText($Listview1, $User_Index, '0', 4)

Else

_TCPSend($User_List[$x][0],"Unban" & Chr(2) & "nouser", $User_List[$x][2])

EndIf

EndIf

Else

;the user was not a admin, Kick him from the server.

_PM("Console",$User_List[$x][1],"You used a function you are now allowed to use.")

_PM("Console",$User_List[$x][1],"You will now be kicked from the server.")

_Kick($User_List[$x][1])

EndIf

EndIf

Case Else

;Debug stuff.

If $Real_Data[1] <> "" Then

ConsoleWrite("Unknow Command: " & $Real_Data[1])

If $Real_Data[0] > 1 Then

For $x = 2 To $Real_data[0]

ConsoleWrite($Real_Data[$x] & ',' )

Next

ConsoleWrite(@CRLF)

EndIf

EndIf

EndSwitch

Next

Next

;Safely exit the function.

Return 1

EndFunc

Func _SendAll($sCommand,$sString, $sFrom="")

;Enter a loop

For $x = 1 To $MaxUsers

;if there is a user on that socket then

If $User_List[$x][0] <> 0 Then

If $sFrom = "" Then

;Send the message to all the users.

_TCPSend($User_List[$x][0], $sCommand & Chr(2) & $sString, $User_List[$x][2])

Else

;Send a message to all the user that a user said something and encrypt it.

_TCPSend($User_List[$x][0],$sCommand & Chr(2) & $sFrom & Chr(2) & $sString, $User_List[$x][2])

EndIf

EndIf

Next

Return 1

EndFunc

Func _DelSocket($sSocket)

;enter a loop to check all the usersers.

For $x = 1 to $MaxUsers

;If we hit the correct user then

If $User_List[$x][0] = $sSocket Then

;Close his socket.

TCPCloseSocket($User_List[$x][0])

;Set his old array to 0 to make room for new people.

$User_List[$x][0] = False

;if a username is register to the arry then.

If $User_List[$x][1] <> "" Then

;tell the rest of the people to delete that user from the user list.

_SendAll("Deluser", $User_List[$x][1])

;Set the uers online status to 0.

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB, "UPDATE Accounts SET online = 0 WHERE username = '" & StringUpper($User_List[$x][1]) & "';"))

;Set the users/admins online for the console.

If _IsAdmin($User_List[$x][1]) Then

$Admins_Online -= 1

Else

$Users_Online -= 1

EndIf

_GUICtrlListBox_DeleteString($List1, _GUICtrlListBox_FindString($List1,$User_List[$x][1]))

If $User_List[$x][5] > -1 Then _GUICtrlListView_SetItemText($Listview1, $User_List[$x][5], '0', 5)

;Set his username array to 0.

$User_List[$x][1] = ""

$User_List[$x][2] = 0

$User_List[$x][3] = ""

$User_List[$x][4] = ""

$User_List[$x][5] = -1

EndIf

Return 1

EndIf

Next

EndFunc

Func _SendUserList($sSocket, $sKey)

;Enter a userlist loop.

For $x = 1 To $MaxUsers

;if a username is register to the array then

If $User_List[$x][1] <> "" Then

GUICtrlSetData($List1, $User_List[$x][1])

;send a message to the new user to add that user to his list.

_TCPSend($sSocket, "adduser" & Chr(2) & $User_List[$x][1], $sKey)

EndIf

Next

Return 1

EndFunc

Func _Login($s_username, $s_password, $s_id)

Local $Query, $SQL_Data

_SQLite_QueryFinalize(_SQLite_Query($SQL_DB, "SELECT * FROM Accounts WHERE username='" & StringUpper($s_username) & "';", $Query))

If _SQLite_FetchData($Query, $SQL_Data) <> $SQLITE_OK Then

Return 0

ElseIf $SQL_Data[4] = "1" Then;Check and see if he is banned form the server

Return 1

ElseIf $SQL_Data[5] = "1" Then ;Check and see if there is a user with that account already online

Return 2

ElseIf $SQL_Data[2] == $s_password Then ;If the user has the correct password the send him a message telling him that it was ok.

$Admins_Online += $SQL_Data[3]

$Users_Online += 1-$SQL_Data[3]

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB, "UPDATE Accounts SET online=1 WHERE username = '" & StringUpper($s_username) & "';"))

If $SQL_Data[3] = 1 Then $s_username = "[A]" & $s_username

;Set that user to the $User_List array

$User_List[$s_id][1] = $s_username

$User_List[$s_id][5] = _GUICtrlListView_FindText($Listview1, $SQL_Data[0], -1, False)

If $User_List[$s_id][5] > -1 Then _GUICtrlListView_SetItemText($Listview1, $User_List[$s_id][5], '1', 5)

Return 3+$SQL_Data[3]

EndIf

Return 0

EndFunc

Func _CreateUser($s_username, $s_password, $s_ban = 0, $s_admin = 0)

If StringIsAlNum($s_username) = 0 Then Return 1

_SQLite_QueryFinalize(_SQLite_Query($SQL_DB, "SELECT id FROM Accounts WHERE username='" & $s_username & "';",$Query))

If _SQLite_FetchData($Query, $SQL_Data) <> $SQLITE_OK Then

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB,"INSERT INTO Accounts(username,password,admin,ban,online) VALUES ('" & StringUpper($s_username) & "','" & $s_password & "'," & $s_admin & "," & $s_ban & ",0);"))

_SQLite_QueryFinalize(_SQLite_Query($SQL_DB, "SELECT * FROM Accounts WHERE username='" & StringUpper($s_username) & "';",$Query))

If _SQLite_FetchData($Query, $SQL_Data) = $SQLITE_OK Then GUICtrlCreateListViewItem($SQL_Data[0] & '|' & $SQL_Data[1] & '|' & $SQL_Data[2] & '|' & $SQL_Data[3] & '|' & $SQL_Data[4] & '|' & $SQL_Data[5], $Listview1)

Else

Return 0

EndIf

Return 2

EndFunc

Func _PM($sUserFrom,$sUserTo,$sMsg)

;Why send a message to your self?!

If $sUserFrom <> $sUserTo And $sMsg <> '' Then

;Enter User Loop, you should know that buy now :P

For $x = 1 to $MaxUsers

;check if the user its to is the user we are at in the loop

If $User_List[$x][1] = $sUserTo Then

;Show it in the Console chat.

GUICtrlSetData($Edit1, GUICtrlRead($Edit1) & $User_List[$x][1] & ': ' & $sMsg & @CRLF)

;Send him a pm command, who its from and the message.

_TCPSend($User_List[$x][0], "pm" & Chr(2) & $sUserFrom & Chr(2) & $sMsg, $User_List[$x][2])

Return True

EndIf

Next

EndIf

Return False

EndFunc

Func _ConsoleWrite($s_Message)

Local $s_time = '[' & @HOUR & ':' & @MIN & ':' & @SEC & ']|'

;Debuging.

ConsoleWrite(StringReplace($s_time & $s_Message,'|',@TAB))

;Write Log.

FileWrite($Log_File,StringReplace($s_time & $s_Message,'|',@TAB))

;Write to the console.

GUICtrlCreateListViewItem($s_time & $s_Message, $Listview2)

GUICtrlSetBkColor(-1,$Listview_Color)

If $Listview_Color = 0xeeeeff Then

$Listview_Color = 0xffffff

Else

$Listview_Color = 0xeeeeff

EndIf

EndFunc

Func _Kick($sUser)

;Enter User loop

For $s_x = 1 To $MaxUsers

;check if this is the user we are looking for.

If $User_List[$s_x][1] = $sUser Then

;If it was then send him the message.

_TCPSend($User_List[$s_x][0],"Kick" & Chr(2) & "", $User_List[$s_x][2])

;Remove him from the server

_DelSocket($User_List[$s_x][0])

Return True

EndIf

Next

Return False

EndFunc

Func _Ban($sUser)

;Enter User loop

For $x = 1 To $MaxUsers

;check if this is the user we are looking for.

If $User_List[$x][1] = $sUser Then

;If it was then send him the message.

_TCPSend($User_List[$x][0],"ban" & Chr(2) & "",$User_List[$x][2])

;write to the database that he is banned.

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB,"UPDATE Accounts SET ban = 0 WHERE username = '" & StringUpper($sUser) & "';"))

If $User_List[$x][5] > -1 Then _GUICtrlListView_SetItemText($Listview1, $User_List[$x][5], '1', 4)

;Remove him from the server

_DelSocket($User_List[$x][0])

Return True

EndIf

Next

Return False

EndFunc

Func _KeyGen($s_id)

;generate new key.

$Key = _RandomString()

;Send the new key to the user.

_TCPSend($User_List[$s_id][0], "key" & Chr(2) & $Key, $User_List[$s_id][2])

;Set the new key to the correct user

$User_List[$s_id][4] = $User_List[$s_id][2]

$User_List[$s_id][2] = $Key

$User_List[$s_id][3] = TimerInit()

EndFunc

Func _IsAdmin($sUser)

_SQLite_QueryFinalize(_SQLite_Query($SQL_DB, "SELECT admin FROM Accounts WHERE username = '" & StringUpper($sUser) & "';",$Query))

If _SQLite_FetchData($Query,$SQL_Data) <> $SQLITE_OK Then

Return False

EndIf

If $SQL_Data[0] = '1' Then

Return True

Else

Return False

EndIf

EndFunc

Func _LoadDatabase()

Local $Query, $SQL_Data

_SQLite_QueryFinalize(_SQLite_Query($SQL_DB, "SELECT * FROM Accounts;", $Query))

While _SQLite_FetchData($Query, $SQL_Data) = $SQLITE_OK

GUICtrlCreateListViewItem($SQL_Data[0] & '|' & $SQL_Data[1] & '|' & $SQL_Data[2] & '|' & $SQL_Data[3] & '|' & $SQL_Data[4] & '|' & $SQL_Data[5], $Listview1)

WEnd

Return

EndFunc

Func _TCPSend($sSocket, $sData,$sEncryptKey)

;Char 3 is the signarture, if the recvied string dosent start with char 3 then use the old key.

TCPSend($sSocket,_StringEncryptRC4( Chr(3) & $sData & Chr(1),$sEncryptKey))

;Log/Debug

_ConsoleWrite("Out|" & _TCPSocketToIP($sSocket) & "|" & StringReplace($sData,Chr(2),'|') & @CRLF)

EndFunc

Func _TCPSocketToIP($iSocket)

Local $pSocketAddress, $aReturn

$pSocketAddress = DllStructCreate("short;ushort;uint;char[8]")

$aReturn = DllCall("Ws2_32.dll", "int", "getpeername", "int", $iSocket, "ptr", DllStructGetPtr($pSocketAddress), "int*", DllStructGetSize($pSocketAddress))

If @error Or $aReturn[0] <> 0 Then Return 0

$aReturn = DllCall("Ws2_32.dll", "str", "inet_ntoa", "int", DllStructGetData($pSocketAddress, 3))

If @error Then Return 0

$pSocketAddress = 0

Return $aReturn[0]

EndFunc

Func OnAutoitExit()

FileClose($Log_File)

;Send a message to the users that we are shuting down the server

_SendAll("Bye","")

;Shut down TCP.

TCPShutdown()

_SQLite_QueryFinalize(_SQLite_Exec($SQL_DB, "UPDATE Accounts SET online=0 WHERE online=1"))

Sleep(1000) ;tried to remove the error, didnt help :s

_SQLite_QueryFinalize($Query)

_SQLite_Close($SQL_DB) ;Gives error sometimes -.-

_SQLite_Shutdown()

EndFunc

You can see I added _SQLite_QueryFinalize() to a few commands (not sure if that helps or hinders) but it didn't seem to make a difference more than once. Edited by dbzfanatic
Link to comment
Share on other sites

I hate to bump (really) but I want to get this out of the way and I've already tried everything I can think of short of writing my own code from the ground up (which I don't have enough TCP experience to do as of yet). If anyone can go through and look at the _SQLite statements and perhaps see where the problem is I'd be very grateful.

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...