//TODO stop users logging in if they're already logged in, it'll circumvent the logout event D: void OnServerMessage(ConnectionService con, NetworkMessage data) { if (data.tag == tag) { //Login if (data.subject == loginSubject) { try { using (DarkRiftReader reader = (DarkRiftReader)data.data) { bool isLoggedIn = false; try { isLoggedIn = (bool)con.GetData(name, "IsLoggedIn"); } catch (KeyNotFoundException) { } if (!isLoggedIn) { try { //Query then database DatabaseRow[] rows = DarkRiftServer.database.ExecuteQuery( "SELECT id FROM users WHERE username = @username AND password = @password LIMIT 1 ", new QueryParameter("username", reader.ReadString()), new QueryParameter("password", reader.ReadString()) ); //If 1 is returned then the details are correct if (rows.Length == 1) { int id = Convert.ToInt32(rows[0]["id"]); con.SetData(name, "IsLoggedIn", true); con.SetData(name, "UserID", id); if (onSuccessfulLogin != null) onSuccessfulLogin(id, con); if (debug) Interface.Log("[LoginPlugin] Successfull login (" + id + ")."); con.SendReply(tag, loginSuccessSubject, id); } else { if (onUnsucessfulLogin != null) onUnsucessfulLogin(con); if (debug) Interface.Log("[LoginPlugin] Unsuccessfull login."); con.SendReply(tag, loginFailedSubject, 0); } } catch (DatabaseException e) { Interface.LogError("[LoginPlugin] SQL error during login:"******"[LoginPlugin] Unsuccessfull login."); con.SendReply(tag, loginFailedSubject, 0); } } else { Interface.LogError("[LoginPlugin] Client tried to login while still logged in!"); if (onUnsucessfulLogin != null) onUnsucessfulLogin(con); if (debug) Interface.Log("[LoginPlugin] Unsuccessfull login."); con.SendReply(tag, loginFailedSubject, 0); } } } catch(InvalidCastException) { Interface.LogError("[LoginPlugin] Invalid data recieved in a Login request."); if (onUnsucessfulLogin != null) onUnsucessfulLogin(con); if (debug) Interface.Log("[LoginPlugin] Unsuccessfull login."); con.SendReply(tag, loginFailedSubject, 0); } } //Logout if (data.subject == logoutSubject) { con.SetData(name, "IsLoggedIn", false); int id = (int)con.GetData(name, "UserID"); con.SetData(name, "UserID", -1); if( onLogout != null ) onLogout(id, con); if( debug ) Interface.Log("[LoginPlugin] Successful logout."); con.SendReply (tag, logoutSuccessSubject, 0); } //Add User if (data.subject == addUserSubject && settings.IsTrue ("AllowAddUser")) { try { using (DarkRiftReader reader = (DarkRiftReader)data.data) { string username = reader.ReadString(); string password = reader.ReadString(); try { object o = DarkRiftServer.database.ExecuteScalar ( "SELECT EXISTS(SELECT 1 FROM users WHERE username = @username AND password = @password) ", new QueryParameter("username", username), new QueryParameter("password", password) ); if (!Convert.ToBoolean(o)) { int id = AddUserToDatabase (username, password); con.SetData (name, "IsLoggedIn", true); con.SetData (name, "UserID", id); if (onAddUser != null) onAddUser (id, username, con); if( debug ) Interface.Log("[LoginPlugin] User added."); con.SendReply (tag, addUserSuccessSubject, id); } else { if (onAddUserFailed != null) onAddUserFailed (username, con); if( debug ) Interface.Log("[LoginPlugin] Add user failed."); con.SendReply (tag, addUserFailedSubject, 0); } } catch(DatabaseException e) { Interface.LogError("[LoginPlugin] SQL error during AddUser:"******"[LoginPlugin] Add user failed."); con.SendReply (tag, loginFailedSubject, 0); } } } catch(InvalidCastException) { Interface.LogError("[LoginPlugin] Invalid data recieved in an AddUser request."); if( onAddUserFailed != null ) onAddUserFailed("", con); if( debug ) Interface.Log("[LoginPlugin] Add user failed."); con.SendReply (tag, loginFailedSubject, 0); } } } }
//TODO stop users logging in if they're already logged in, it'll circumvent the logout event D: void OnServerMessage(ConnectionService con, NetworkMessage data) { if (data.tag == tag) { //Login if (data.subject == loginSubject) { try { using (DarkRiftReader reader = (DarkRiftReader)data.data) { bool isLoggedIn = false; try { isLoggedIn = (bool)con.GetData(name, "IsLoggedIn"); } catch (KeyNotFoundException) { } if (!isLoggedIn) { try { //Query then database DatabaseRow[] rows = DarkRiftServer.database.ExecuteQuery( "SELECT id FROM users WHERE username = @username AND password = @password LIMIT 1 ", new QueryParameter("username", reader.ReadString()), new QueryParameter("password", reader.ReadString()) ); //If 1 is returned then the details are correct if (rows.Length == 1) { int id = Convert.ToInt32(rows[0]["id"]); con.SetData(name, "IsLoggedIn", true); con.SetData(name, "UserID", id); if (onSuccessfulLogin != null) { onSuccessfulLogin(id, con); } if (debug) { Interface.Log("[LoginPlugin] Successfull login (" + id + ")."); } con.SendReply(tag, loginSuccessSubject, id); } else { if (onUnsucessfulLogin != null) { onUnsucessfulLogin(con); } if (debug) { Interface.Log("[LoginPlugin] Unsuccessfull login."); } con.SendReply(tag, loginFailedSubject, 0); } } catch (DatabaseException e) { Interface.LogError("[LoginPlugin] SQL error during login:"******"[LoginPlugin] Unsuccessfull login."); } con.SendReply(tag, loginFailedSubject, 0); } } else { Interface.LogError("[LoginPlugin] Client tried to login while still logged in!"); if (onUnsucessfulLogin != null) { onUnsucessfulLogin(con); } if (debug) { Interface.Log("[LoginPlugin] Unsuccessfull login."); } con.SendReply(tag, loginFailedSubject, 0); } } } catch (InvalidCastException) { Interface.LogError("[LoginPlugin] Invalid data recieved in a Login request."); if (onUnsucessfulLogin != null) { onUnsucessfulLogin(con); } if (debug) { Interface.Log("[LoginPlugin] Unsuccessfull login."); } con.SendReply(tag, loginFailedSubject, 0); } } //Logout if (data.subject == logoutSubject) { con.SetData(name, "IsLoggedIn", false); int id = (int)con.GetData(name, "UserID"); con.SetData(name, "UserID", -1); if (onLogout != null) { onLogout(id, con); } if (debug) { Interface.Log("[LoginPlugin] Successful logout."); } con.SendReply(tag, logoutSuccessSubject, 0); } //Add User if (data.subject == addUserSubject && settings.IsTrue("AllowAddUser")) { try { using (DarkRiftReader reader = (DarkRiftReader)data.data) { string username = reader.ReadString(); string password = reader.ReadString(); try { object o = DarkRiftServer.database.ExecuteScalar( "SELECT EXISTS(SELECT 1 FROM users WHERE username = @username AND password = @password) ", new QueryParameter("username", username), new QueryParameter("password", password) ); if (!Convert.ToBoolean(o)) { int id = AddUserToDatabase(username, password); con.SetData(name, "IsLoggedIn", true); con.SetData(name, "UserID", id); if (onAddUser != null) { onAddUser(id, username, con); } if (debug) { Interface.Log("[LoginPlugin] User added."); } con.SendReply(tag, addUserSuccessSubject, id); } else { if (onAddUserFailed != null) { onAddUserFailed(username, con); } if (debug) { Interface.Log("[LoginPlugin] Add user failed."); } con.SendReply(tag, addUserFailedSubject, 0); } } catch (DatabaseException e) { Interface.LogError("[LoginPlugin] SQL error during AddUser:"******"[LoginPlugin] Add user failed."); } con.SendReply(tag, loginFailedSubject, 0); } } } catch (InvalidCastException) { Interface.LogError("[LoginPlugin] Invalid data recieved in an AddUser request."); if (onAddUserFailed != null) { onAddUserFailed("", con); } if (debug) { Interface.Log("[LoginPlugin] Add user failed."); } con.SendReply(tag, loginFailedSubject, 0); } } } }