This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
cheekycharlie101's profile picture

editing a certain line of a file over rednet?

Started by cheekycharlie101, 04 December 2012 - 07:58 AM
cheekycharlie101 #1
Posted 04 December 2012 - 08:58 AM
ok so i have a rednet login server set up, and i want to be able to change certain details server side from the client computer. here is a little server code. i dont think you need it all but if you do just ask.
anyway it works like this


users = { "user1", "user2" }
pass = { "pass1", "pass2" }

now, you type in the user name and password on the client computer. it sends the user name to the server. if the username matches one of the strings in the users table it sends back the password. if the pass sent matches the pass you put in to the client it logs you in. now what i want is to be able to change the password from the client. is this possible or is the file writing system not that advanced?
thanks -Cheeky
Lyqyd #2
Posted 04 December 2012 - 09:02 AM
Just update your variables with the new password and write them back out to the file wholesale.
bjornir90 #3
Posted 04 December 2012 - 09:02 AM
One way of doing this is to save everything in the table in variables, then rewrite the table with the new values. And re-add old variable that you haven't overwritten.
Edit : ninja'ed :(/>
cheekycharlie101 #4
Posted 04 December 2012 - 09:41 AM
One way of doing this is to save everything in the table in variables, then rewrite the table with the new values. And re-add old variable that you haven't overwritten.
Edit : ninja'ed :(/>
Just update your variables with the new password and write them back out to the file wholesale.
I may sound like a mass noob, but i dont really know what you mean :P/>
Lyqyd #5
Posted 04 December 2012 - 09:45 AM
Well, you load your variables from a file, right? All you have to do is update the password variable in the server program when it gets the new one from the client, then save the passwords back into the file in whatever format you use.
cheekycharlie101 #6
Posted 04 December 2012 - 09:55 AM
Well, you load your variables from a file, right? All you have to do is update the password variable in the server program when it gets the new one from the client, then save the passwords back into the file in whatever format you use.
the tables are at the top of the server code. so at all times the server is in a while loop listening for rednet messages. so i still dont really see how i could do this
anonimo182 #7
Posted 04 December 2012 - 10:34 AM
You could do some code that checks if the message is a specific message, and if it is, it sends a confirmation code, recieving back the number of user/password to change and another with the new user/password then changing with users[number_of_user] = newuser or using table.delete(users, number_of_user) table.insert(users, number_of_user, newuser)
Lyqyd #8
Posted 04 December 2012 - 11:04 AM
Well, you load your variables from a file, right? All you have to do is update the password variable in the server program when it gets the new one from the client, then save the passwords back into the file in whatever format you use.
the tables are at the top of the server code. so at all times the server is in a while loop listening for rednet messages. so i still dont really see how i could do this

Post the code and we'll show you.
cheekycharlie101 #9
Posted 04 December 2012 - 11:17 AM
Well, you load your variables from a file, right? All you have to do is update the password variable in the server program when it gets the new one from the client, then save the passwords back into the file in whatever format you use.
the tables are at the top of the server code. so at all times the server is in a while loop listening for rednet messages. so i still dont really see how i could do this

Post the code and we'll show you.


-- Rednet Login Server
term.clear()
term.setCursorPos(1,1)
print("This Is A Password Server!")
print("No User Interaction Here!")
clients = { 1, 2, 3 } -- The allowed client computer id's
local modem_Side = "back"
logins = {"Hello", "AnotherUser" } --usernames
pass = {"1234", "spongebob" } -- passwords (They must match up. so 1234 will only allow access for the username Hello)
while true do
  rednet.open(modem_Side)
  senderId, message, distance = rednet.receive()
  for i,v in ipairs(clients) do
    if v == senderId then
   for i,v in ipairs(logins) do
	 if message == v then
    password = pass[i]
    rednet.send(senderId, password, true)
  end
	  end
    end
  end
end 
  
Here is the code for the login server. note this is my first attempt at using tables so it may be a little noobish:P
Lyqyd #10
Posted 04 December 2012 - 03:41 PM
Here's a vague restructuring with some work left to be done, but including file operations to store the user data in separate files:


local function loadData()
    local userNames = {}
    local passwords = {}
    userHandle = io.open("/.logins", "r")
    if userHandle and passHandle then
        for line in userHandle:lines() do
            table.insert(userNames, line)
        end
        userHandle:close()
    else
        print("Cannot find username file!")
        return nil
    end
    passHandle = io.open("/.passwords", "r")
    if passHandle then
        for line in passHandle:lines() do
            table.insert(passwords, line)
        end
        passHandle:close()
    else
        print("Cannot find passwords file!")
        return nil
    end
    local userData = {}
    for i=1,#userNames do
        userData[userNames[i]] = passwords[i]
    end
    return userData
end

local function saveData(userData)
    userHandle = io.open("/.logins", "w")
    passHandle = io.open("/.passwords", "w")
    if userHandle and passHandle then
        for name, pass in pairs(userData) do
            userHandle:write(name.."\n")
            passHandle:write(pass.."\n")
        end
        userHandle:close()
        passHandle:close()
    else
        print("Could not save data!")
        return nil
    end
    return true
end

-- Rednet Login Server
term.clear()
term.setCursorPos(1,1)
print("This Is A Password Server!")
print("No User Interaction Here!")
clients = { [1] = true, [2] = true, [3] = true } -- The allowed client computer id's
local modem_Side = "back"
local loginData = loadData()
while true do
    rednet.open(modem_Side)
    senderId, message, distance = rednet.receive()
    if clients[senderID] and userData[message] then
        rednet.send(senderId, userData[message], true)
    elseif clients[senderID] and message == "!changePass" then
        --you need to keep track of who's logged in, then just loginData[whoIsLoggedInAt[senderID]] = newPasswordWhateverItIs
        saveData(loginData)
    end
end