Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- serverID = os.getComputerID()
- modem = peripheral.wrap("back")
- fs.delete("TimerKey.lua")
- modem.open(2410)
- term.clear()
- term.setCursorPos(1,1)
- function str2tbl (str)
- file = fs.open("temp.lua","w")
- file.write("return "..str)
- file.close()
- local data = dofile("temp.lua")
- fs.delete("temp.lua")
- return data
- end
- function findItemID (file, item, position)
- local data = dofile(file)
- for i = 1, table.maxn(data) do
- checkItem = str2tbl(data[i])
- if tostring(checkItem[position]) == tostring(item) then
- return i
- end
- end
- return nil
- end
- function returnTableItem (file, tbl, position)
- local data = dofile(file)
- checkTable = str2tbl(data[tbl])
- if tostring(checkTable[position]) then
- return checkTable
- else
- return nil
- end
- end
- function removeTableLine (file, position)
- itemID = findItemID("Users.lua", Username, 1)
- if itemID ~= nil then
- local tbl = dofile(file)
- table.remove(tbl, itemID)
- data = 'return {\n"'..table.concat(tbl,'",\n"')..'"\n}'
- fs.delete(file)
- file = fs.open(file,"a")
- file.write(data)
- file.close()
- return itemID
- else
- return nil
- end
- end
- function newTableLine (file, data) -- Func 'newTableLine' expects the 'data' variable to be a string in the following format: "{ 'var1', 'var2', 'etc'}"
- if fs.exists(file) then
- local tbl = dofile(file)
- else
- tbl = {}
- end
- table.insert(tbl, (table.maxn(tbl) + 1), data)
- data = 'return {\n"'..table.concat(tbl,'",\n"')..'"\n}'
- fs.delete(file)
- file = fs.open(file,"a")
- file.write(data)
- file.close()
- return table.maxn(tbl)
- end
- function refreshTimer (channel,senderID,message)
- fileID = findItemID("TimerKey.lua", tostring(senderID), 1)
- if fileID ~= nil then
- savedID = returnTableItem("TimerKey.lua", fileID, 1)
- savedChannel = returnTableItem("TimerKey.lua", fileID, 2)
- savedKey = returnTableItem("TimerKey.lua", fileID, 3)
- if senderID == savedID and channel == savedChannel and message[1] == savedKey then
- os.cancelTimer(key)
- removeTableLine("TimerKey.lua", fileID)
- newKey = os.startTimer(10)
- secureChannel = math.random(4096,65536)
- modem.open(secureChannel)
- modem.transmit(channel, secureChannel, newKey)
- if findItemID("TimerKey.lua", channel, 2) == nil then
- modem.close(channel)
- end
- newTableLine("TimerKey.lua","{'"..senderID.."','"..secureChannel.."','"..newKey.."'}")
- else
- modem.close(channel)
- removeTableLine("TimerKey.lua", fileID)
- modem.transmit(2410, senderID, -1)
- modem.transmit(2410, savedID, -1)
- modem.transmit(2410, savedChannel, -1)
- modem.transmit(2410, savedKey, -1)
- end
- end
- end
- while true do
- event, var1, var2, var3, var4, var5 = os.pullEventRaw()
- if event == "terminate" then
- print("Rebooting")
- RebootKey = os.startTimer(3)
- modem.transmit(2410,0,-1)
- elseif event == "modem_message" then
- side = var1; channel = var2; senderID = var3; message = var4; distance = var5
- if message == -1 then
- secureChannel = math.random(4096,65536)
- modem.open(secureChannel)
- newKey = os.startTimer(10)
- modem.transmit(2410, secureChannel, newKey)
- newTableLine("TimerKey.lua","{'"..senderID.."','"..secureChannel.."','"..newKey.."'}")
- else
- refreshTimer(channel,senderID,message)
- end
- elseif event == "timer" then
- key = var1
- if key == RebootKey then
- os.reboot()
- end
- elseif event == "key" then
- print(var1)
- if var1 == 211 then
- return
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement