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

[LUA][Question] Reading variables from a file

Started by Saugrim, 15 October 2012 - 04:46 AM
Saugrim #1
Posted 15 October 2012 - 06:46 AM
Well.. I have been working on an Email server, and I have been having issues getting it to read a group of variables from a file, at the moment I am simply attempting to make it read user names off of a file called USERS..

I have attempted placing it in a table format, and a format such as 1 variable per line, though none of my attempts have been successful, basically what I am trying to do is have the server compare the email address that is sent by the computer using the application to the users in the user config so that it can check to make sure that the user is actually legit. Then of course it can go from there, but I am having issues on how to arrange this or how to format my code so that such a feat would be possible, any assistance would be helpful, thanks.

Thanks for any assistance that can be provided,
Saugrim
JoshhT #2
Posted 15 October 2012 - 06:52 AM
Two files.
One called users, containing all the user names, one per line.

Second file, called whatever you want. This is not the exact code, but this will give you everything you need to accomplish what you want.


local file = fs.open("/disk/users")
local list = {}
local line = file.readLine()

repeat
table.insert(list, line)
line = file.readLine()
until line == nil

file.close()

for _, v in pairs(list) do
  print(v)
  sleep(0.5)
end
Saugrim #3
Posted 15 October 2012 - 07:05 AM
Current server test file


local file = fs.open("Users", "r")
local list = {}
local line = file.readLine()
repeat
table.insert(list, line)
line = file.readLine()
until line == nil
file.close()
for _, v in pairs(list) do
  print(v)
  sleep(0.5)
end
ID, message = rednet.receive()
if v == message then
print("Testing complete!")
elseif v ~= message then
print("Testing failed.. Bun dun dun!")
end

Users File


Bob@gmail.com
Phil@gmail.com

The server receives the message however, when it comes up it does come up as testing failed.
I am using a simple LUA interaction on the other computer, and using it to send

Bob@gmail.com

which is what one of the users in the USER config is listed as.

rednet.send(8, "Bob@gmail.com")
JoshhT #4
Posted 15 October 2012 - 07:22 AM
The variable v is only valid within the for loop.
To get the first user in the list, use this.

list[1] -- will return the first value.
list[2] -- will return the second, so on, so forth.

So, for example, you wanted to check for Phil.
You'd do something like,

if msg == list[2] then
  -- Message is Phil.
end
Saugrim #5
Posted 15 October 2012 - 07:27 AM
Yeah, I got the program to check for each of them individually but on a server wide scale the problem is, that comes out to be ALOT of code.. So I wasn't sure if there was some way to make the program to skim through all the code and search for a specific variable..

For example, let's say that the terminal sent the server a request to pull down the messages from the user Phil@gmail.com
then the server would automatically check and make sure that Phil@gmail.com was a legit address before it sent the password authorization protocol.


Specific Checking Code


rednet.open("left")
local file = fs.open("Users", "r")
local list = {}
local line = file.readLine()
repeat
table.insert(list, line)
line = file.readLine()
until line == nil
file.close()
for _, v in pairs(list) do
  print(v)
  sleep(0.5)
end
ID, message = rednet.receive()
if list[1] == message then
print("Testing complete!")
elseif list ~= message then
print("Testing failed.. Bun dun dun!")
end
JoshhT #6
Posted 15 October 2012 - 07:33 AM
Ohh, so you intend to have lots of users.
Try this out.

function checkFor(name)
  for _, v in pairs(list) do
    if v == name then
      return true
    end
  end
  return false
end

To use, simply do like,


-- Message receiving code.
if checkFor(msg) then -- Checks the entire user list for msg(received user name)
  -- User is valid, send password.
else
  -- User invalid.
end
JoshhT #7
Posted 15 October 2012 - 07:37 AM
I also marked here what isn't essential to your program, it was just to show you what it was doing.



rednet.open("left")
local file = fs.open("Users", "r")
local list = {}
local line = file.readLine()
repeat
table.insert(list, line)
line = file.readLine()
until line == nil
file.close()
for _, v in pairs(list) do -- From here
  print(v)
  sleep(0.5)
end -- To here, is just so it showed you what was happening.
ID, message = rednet.receive()
if list[1] == message then -- Replace this,
print("Testing complete!")
elseif list ~= message then
print("Testing failed.. Bun dun dun!")
end -- To this, with the code I provided above. Make sure you've declared and initialized the function before you call it.
Saugrim #8
Posted 15 October 2012 - 07:47 AM
Yeah, I usually do the same thing in my coding.. I make sure I can see what is happening before I hide it all from the users..

This is part of the program that is going to be hosted on my Server as part of the Internet on Tekkit, I already have the main server up and running as well as the security protocols, etc.. A browser and what not but an Internet isn't an Internet without Email which was where this came into play..

Anyways, I believe this is how you meant it should look?


rednet.open("left")
local file = fs.open("Users", "r")
local list = {}
local line = file.readLine()
repeat
table.insert(list, line)
line = file.readLine()
until line == nil
file.close()
for _, v in pairs(list) do -- From here
  print(v)
  sleep(0.5)
end -- To here, is just so it showed you what was happening.
ID, message = rednet.receive()
function checkFor(name)
  for _, v in pairs(list) do
    if v == name then
	  return true
    end
  end
  return false
end
-- Message receiving code.
if checkFor(message) then -- Checks the entire user list for msg(received user name)
  print("Valid")
else
  print("Huh")
end
JoshhT #9
Posted 15 October 2012 - 07:56 AM
Yeah that should work perfectly :D/>/>
Saugrim #10
Posted 15 October 2012 - 07:58 AM
Awesome, thank you for the assistance and from the data I am receiving on the game it seems to be working perfectly now. :3

However.. Now I have a problem, because I don't know if there's a way to make it so that when it goes through the password protocol each username can have a separate password.. If you know what I mean, because I believe that makes it a lot more intense, and complex..

Basically I don't want a global password, rather a password for each user that uses the system.
Which can then be cross referenced, so that if you used the email
Bob@gmail.com
you HAVE to use the password that is assigned to
Bob@gmail.com,
Mads #11
Posted 15 October 2012 - 08:09 AM
To load variables from a file, you could simply do this:

datafile.data

return { "Bob@gmail.com"; "Phil@gmail.com"; }

main.lua (the code that is run)

function getData()
    local t = dofile("datafile.data")   
    return t[1], t[2] -- returns "Bob@gmail.com", "Phil@gmail.com"
end
local var1, var2 = getData()