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

making a kind of active directory thing

Started by JamiePhonic, 14 January 2013 - 08:02 AM
JamiePhonic #1
Posted 14 January 2013 - 09:02 AM
i have a mainframe system in my tekkit world that i use to control things from computers, part of that system is a booster, it takes commands in, and broadcasts them again, displaying a message like command forwarded to ID: [comp ID].
what i want to know is, is there a way i can make the booster program look for a file called DIR or something and have it look through it till it matches an id an replace it with where that computer is,
e.g instead of command forwarded to ID: 4,
have it say:
command forwarded to: castle security office
trogdor17 #2
Posted 14 January 2013 - 09:06 AM
so, your looking for a shell that instead of inturperting the command itself sends it to another computer which executes the command?
remiX #3
Posted 14 January 2013 - 09:06 AM
Well you can have a table for all the saved names:


t_ids = {
[4] = "Castle Security Office",
[10] = "Name of pc ID 10",
}
-- etc...

print("Command forwarded to: " .. t_ids[id]) -- where id would be one of the id's

You could use files, but in this case, tables would do fine.
JamiePhonic #4
Posted 14 January 2013 - 09:25 AM
Well you can have a table for all the saved names:


t_ids = {
[4] = "Castle Security Office",
[10] = "Name of pc ID 10",
}
-- etc...

print("Command forwarded to: " .. t_ids[id]) -- where id would be one of the id's

You could use files, but in this case, tables would do fine.
probably not, the server is hosted so the tables wouldent save. i already have some of the code, the part that reads the file, i just need to know how to make it read the file till it finds a matching id the replace it what whatever after it in the file
zekesonxx #5
Posted 14 January 2013 - 09:32 AM
Still use tables. Just do textutils.serialize and textutils.unserialize.
JamiePhonic #6
Posted 14 January 2013 - 09:37 AM
Still use tables. Just do textutils.serialize and textutils.unserialize.
i was planning on using this:

while true do
print("--------------------------------------------")
print("Waiting for message...")
id, msg, dist = rednet.receive()
print("Message ".. msg .." received from ".. id ..", authenticating...")

continue = false
users = fs.open("users", "r")
while true do
  line = users.readLine()
  if not line then
   print("User ID ".. id .." authentication failed.")
   continue = false
   break
  elseif line == tostring(id) then
   print("User ID ".. id .." authentication succeeded.")
   continue = true
   break
  end
end
users.close()

if continue then
  command = {}
  for match in string.gmatch(msg, "[^ \t]+") do
    table.insert( command, match )
  end
  end
  end

because i plan to implement a kind of e-mail thing so i can send messages to status screens and have them display the message i send them. kind of hard if you don't know its id
NeverCast #7
Posted 14 January 2013 - 09:42 AM
Serializing tables is always easier and better than writing out your own stuff. Because you can have one function that saves the table and one that loads it, and both have no more than 3 lines of code. Then you can add what ever you like to your tables, username, password, an array of emails, last login date, security access level, real name, current location, data of birth, Social Security Number, Current Armour!, Current Health!!, Or how many COOKIES THEY HAVE!!

In a sane conclusion, just serialize your tables to file, It's much easier and it's future proof
JamiePhonic #8
Posted 14 January 2013 - 09:52 AM
Serializing tables is always easier and better than writing out your own stuff. Because you can have one function that saves the table and one that loads it, and both have no more than 3 lines of code. Then you can add what ever you like to your tables, username, password, an array of emails, last login date, security access level, real name, current location, data of birth, Social Security Number, Current Armour!, Current Health!!, Or how many COOKIES THEY HAVE!!

In a sane conclusion, just serialize your tables to file, It's much easier and it's future proof
ok, so how do i do that? just adapt and use the code i posted? how do i get it to check and replace the id though?
NeverCast #9
Posted 14 January 2013 - 10:00 AM


function loadUsers()
  local h = fs.open("users", "r")
  local t = textutils.unserialize( h.readAll() )
  h.close()
  return t or {}
end

function saveUsers( tUsers )
tUsers = tUsers or {}
local h = fs.open("users", "w")
h.write(textutils.serialize(tUsers))
h.close()
end

function handleCommand( sMessage )
  command = {}
  for match in string.gmatch(sMessage, "[^ \t]+") do
	table.insert( command, match )
  end
  return command
end

while true do
print("--------------------------------------------")
print("Waiting for message...")
id, msg, dist = rednet.receive()
print("Message ".. msg .." received from ".. id ..", authenticating...")
local users = loadUsers()
if users[id] then
  print("User ID "..id .." authentication succeeded.")
  local tCommand = handleCommand(msg)
else
  print("User ID "..id.." failed")
end

end

This handles the loading, saving and command execution like your previous code did.
What else this offers is that you can go users[id].emailAddress = "name@myserver", and when you save it and load it, that value will still be there, you didn't have to change the save or loading code at all.
JamiePhonic #10
Posted 14 January 2013 - 11:20 AM


function loadUsers()
  local h = fs.open("users", "r")
  local t = textutils.unserialize( h.readAll() )
  h.close()
  return t or {}
end

function saveUsers( tUsers )
tUsers = tUsers or {}
local h = fs.open("users", "w")
h.write(textutils.serialize(tUsers))
h.close()
end

function handleCommand( sMessage )
  command = {}
  for match in string.gmatch(sMessage, "[^ \t]+") do
	table.insert( command, match )
  end
  return command
end

while true do
print("--------------------------------------------")
print("Waiting for message...")
id, msg, dist = rednet.receive()
print("Message ".. msg .." received from ".. id ..", authenticating...")
local users = loadUsers()
if users[id] then
  print("User ID "..id .." authentication succeeded.")
  local tCommand = handleCommand(msg)
else
  print("User ID "..id.." failed")
end

end

This handles the loading, saving and command execution like your previous code did.
What else this offers is that you can go users[id].emailAddress = "name@myserver", and when you save it and load it, that value will still be there, you didn't have to change the save or loading code at all.
ah.. the code i supplied was just example code is found from a video on youtube.
i need to be able to integrate that into this program…
Spoiler

version="12.3.4"
--clear screen function (lazyness)
function clear()
term.clear()
term.setCursorPos(1,1)
end
function shutdown()
term.setCursorPos(1,15)
sp("Shutting Down: 5") --Print "shutting down: 5"
sleep(1) --sleep for a second
term.setCursorPos(1,15) --set cursor pos to line 4, character 1
print("Shutting Down: 4") --repeat, subrtacting 1 from the timer each time
sleep(1)
term.setCursorPos(1,15)
print("Shutting Down: 3")
sleep(1)
term.setCursorPos(1,15)
print("Shutting Down: 2")
sleep(1)
term.setCursorPos(1,15)
print("Shutting Down: 1")
sleep(1)
os.shutdown() --shut the system down
end
--reboots comp after 5 second countdown
function reboot()
clear()
term.setCursorPos(1,15)
sp("Rebooting In: 5") --Print "Rebooting In: 5"
sleep(1) --sleep for a second
term.setCursorPos(1,15) --set cursor pos to line 4, character 1
print("Rebooting In: 4") --repeat, subrtacting 1 from the timer each time
sleep(1)
term.setCursorPos(1,15)
print("Rebooting In: 3")
sleep(1)
term.setCursorPos(1,15)
print("Rebooting In: 2")
sleep(1)
term.setCursorPos(1,15)
print("Rebooting In: 1")
sleep(1)
os.reboot() --reboot the system
end
--Program Starts! detect if a modem is attached, if it is, enable it
if peripheral.getType("top") == "modem" then
rednet.open("top")
elseif peripheral.getType("back") == "modem" then
rednet.open("back")
elseif peripheral.getType("left") == "modem" then
rednet.open("left")
elseif peripheral.getType("right") == "modem" then
rednet.open("right")
elseif peripheral.getType("bottom") == "modem" then
rednet.open("bottom")
else
clear()
print("Please Connect A Modem And Try Again.")
print("")
sleep(1)
shutdown()
end

print("G&J Tech Mainframe System: BoostIt, V"..version)
print("(C) 2013 G&J Tech Software Inc.")
print("")
print("hold CTRL+S to shutdown")
print("hold CTRL+R to reboot")
ostime = os.time()
time = textutils.formatTime(ostime, false)
while true do
sender, message = rednet.receive()
if message == "relay" then
print("responded to presence check")
rednet.send(sender, "True")
elseif message == "RebootRelay" then
print("reboot command issued")
rednet.send(sender, "rebooting!")
else
--actual repeater part--
rednet.send(0, message) --send "message" to id 0 (the server)
print("[", time, "] Command forwarded to server")
serverID, reply = rednet.receive() --wait for a responce
rednet.send(sender, reply) --send the servers reply back to the original sender
print("[", time, "] Command forwarded to client ID ", sender)		 <<===Code needs to replce the sender part of this line
end
end
NeverCast #11
Posted 14 January 2013 - 11:32 AM
Not quite sure where you want me to implement it, that's a whole lot of left aligned code.
If you pastebin it or use the code tag dialog box, it'll keep the tabbing.
JamiePhonic #12
Posted 15 January 2013 - 09:11 AM
Not quite sure where you want me to implement it, that's a whole lot of left aligned code.
If you pastebin it or use the code tag dialog box, it'll keep the tabbing.
3rd line from the bottom, its labeled :-)
remiX #13
Posted 15 January 2013 - 05:28 PM
Add a table like I said,



table_of_ids = {
[4] = "Castle Security Office",
[10] = "Name of pc ID 10",
}

and then print:

print("[", time, "] Command forwarded to client ID ", table_of_ids[sender] or sender) -- if the ID doesn't exist in the table, it will just print the ID

It's the easiest way to do it !
JamiePhonic #14
Posted 16 January 2013 - 08:51 AM
Add a table like I said,



table_of_ids = {
[4] = "Castle Security Office",
[10] = "Name of pc ID 10",
}

and then print:

print("[", time, "] Command forwarded to client ID ", table_of_ids[sender] or sender) -- if the ID doesn't exist in the table, it will just print the ID

It's the easiest way to do it !
ok, that works great, i'm some what familiar with the serialize and unserialize command but i haven't a clue how to use the file system write commands

so far i have:


DIR = {
[2] = "IT Center Office Terminal",
[4] = "Castle Security Office",
[10] = "Name of pc ID 10",
}
Directory = fs.open("Directory", "w")
Table = textutils.serialize(DIR)
Directory.write(Table)
Directory.close()
which generated the Directory file, so now i can edit it out of tekkit. but how do i do this in reverse to read it?
is it just:

Directory = fs.open("Directory", "r")
Table = Directory.readAll()
DIR = textutils.unserialize(Table)
Directory.close()

EDIT: i gave this code a shot and it worked until i tried to edit the file in notepad, mow its just returning attempted to index nil errors
EDIT 2: worked it out, it was looking for a non existent file because i call all the software from the rom folder with a shell.run from the in the startup file (saves having to update 20 copies of the code)
JamiePhonic #15
Posted 19 January 2013 - 04:57 AM
@NeverCast : when you said…
Then you can add what ever you like to your tables, username, password, an array of emails, last login date, security access level, real name, current location, data of birth, Social Security Number, Current Armour!, Current Health!!, Or how many COOKIES THEY HAVE!!
how would i do that? (add multiple fields to 1 record so to speak)
remiX #16
Posted 19 January 2013 - 06:14 AM
Tables are fun…


t_IDs = {
[1] = {ID = 3, name = "Security Office", colorOfText = colours.red},
[2] = {ID = 10, name = "Name of PC with ID 10", colorOfText = colours.purple},
}

--[[ Now, how the hell do you access
    that mysteriously wonderful
    information? :o/>
]]--

print(t_IDs[1].ID) -- output is 3
term.setTextColour(t_IDs[1].colorOfText) -- will  set the text colour to red
print(t_IDs[2].name) -- output is Security Office.

--[[ To check if the senderID of a rednet message
      is within the table
--]]
id, msg = rednet.receive()
for i, opt in pairs(t_IDs) do
  if id == opt.ID then
    print("Yes, " .. id .. " is within the table with name: " .. opt.name .. ".")
    break -- end loop
  end
end

JamiePhonic #17
Posted 19 January 2013 - 06:52 AM
Tables are fun…


t_IDs = {
[1] = {ID = 3, name = "Security Office", colorOfText = colours.red},
[2] = {ID = 10, name = "Name of PC with ID 10", colorOfText = colours.purple},
}

--[[ Now, how the hell do you access
	that mysteriously wonderful
	information? :o/>/>
]]--

print(t_IDs[1].ID) -- output is 3
term.setTextColour(t_IDs[1].colorOfText) -- will  set the text colour to red
print(t_IDs[2].name) -- output is Security Office.

--[[ To check if the senderID of a rednet message
	  is within the table
--]]
id, msg = rednet.receive()
for i, opt in pairs(t_IDs) do
  if id == opt.ID then
	print("Yes, " .. id .. " is within the table with name: " .. opt.name .. ".")
	break -- end loop
  end
end


ok, the initial project has become a whois lookup system, that ( ^ ) is the directory thing im after.
if you want to help me with more tables, come see if you can help with this one: http://www.computercraft.info/forums2/index.php?/topic/9386-computer-controlled-item-request-system/