Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- The location of the pastes
- pasteFolder = "disk/paste/"
- function LOG (str)
- print(str)
- fichier = pasteFolder .. "log"
- if (not fs.exists(fichier)) then
- file = fs.open(fichier, "w")
- file.writeLine("-- CREATING FILE --")
- file.close()
- end
- file = fs.open(fichier, "a")
- file.writeLine("> " .. tostring(os.getComputerID()) .. " : " ..str)
- file.close()
- end
- function split (str)
- t = {} ; i = 1
- for st in string.gmatch(str, "([^=]+)") do
- t[i] = st
- i = i + 1
- end
- return t
- end
- function startsWith(self, piece)
- return string.sub(self, 1, string.len(piece)) == piece
- end
- function getChar (a, b)
- return string.char(math.random(a, b))
- end
- function getFileName ()
- str = ""
- for i = 1, 6 do
- rand = math.random (1, 3)
- if rand == 1 then
- str = str .. getChar (48, 57)
- elseif rand == 2 then
- str = str .. getChar(65, 90)
- else
- str = str..getChar(97, 122)
- end
- end
- return str
- end
- function getFile()
- str = getFileName()
- while (fs.exists(pasteFolder .. tostring(str))) do
- print ("File " .. str .. " already exists. Generating new name...")
- str = getFileName()
- end
- return str
- end
- if (not fs.exists(pasteFolder)) then
- fs.makeDir(pasteFolder)
- end
- LOG("Starting up Pastecraft server on id " .. tostring(os.getComputerID()) .. " ...")
- rednet.open("back")
- while true do
- local e, id, text = os.pullEvent("rednet_message")
- if (startsWith(text, "get")) then
- pasteID = split(text)[2]
- if (fs.exists(pasteFolder .. pasteID)) then
- fichierLu = fs.open(pasteFolder..pasteID, "r")
- toSend = textutils.serialize(fichierLu.readAll())
- fichierLu.close()
- rednet.send(id, "get=" .. toSend)
- LOG("GET : " .. pasteID .. " by " .. tostring (id))
- else
- rednet.send(id, "404")
- LOG("GET : NIL by " .. tostring (id) .. " (fail)")
- end
- elseif (startsWith(text, "put")) then
- paste = textutils.unserialize(split(text)[2])
- fileName = getFile()
- fichierPaste = fs.open(pasteFolder .. fileName, "w")
- fichierPaste.write(paste)
- fichierPaste.close()
- rednet.send(id, "put=" .. fileName)
- LOG("PUT : " .. fileName .. " by " .. tostring(id))
- else
- rednet.send(id, "UNKNOWN FUNCTION")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment