Knux14

Pastecraft Server

Sep 17th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.21 KB | None | 0 0
  1. -- The location of the pastes
  2. pasteFolder = "disk/paste/"
  3.  
  4. function LOG (str)
  5.     print(str)
  6.     fichier = pasteFolder .. "log"
  7.     if (not fs.exists(fichier)) then
  8.         file = fs.open(fichier, "w")
  9.         file.writeLine("-- CREATING FILE --")
  10.         file.close()
  11.     end
  12.     file = fs.open(fichier, "a")
  13.     file.writeLine("> " .. tostring(os.getComputerID()) .. " : " ..str)
  14.     file.close()
  15. end
  16.  
  17. function split (str)
  18.   t = {} ; i = 1
  19.   for st in string.gmatch(str, "([^=]+)") do
  20.     t[i] = st
  21.     i = i + 1
  22.   end
  23.   return t
  24. end
  25.  
  26. function startsWith(self, piece)
  27.   return string.sub(self, 1, string.len(piece)) == piece
  28. end
  29.  
  30.  
  31. function getChar (a, b)
  32.   return string.char(math.random(a, b))
  33. end
  34.  
  35. function getFileName ()
  36.   str = ""
  37.   for i = 1, 6 do
  38.    rand = math.random (1, 3)
  39.    if rand == 1 then
  40.       str = str .. getChar (48, 57)
  41.    elseif rand == 2 then
  42.       str = str .. getChar(65, 90)
  43.    else
  44.       str = str..getChar(97, 122)
  45.    end
  46.   end
  47.   return str
  48. end
  49.  
  50. function getFile()
  51.   str = getFileName()
  52.   while (fs.exists(pasteFolder .. tostring(str))) do
  53.     print ("File " .. str .. " already exists. Generating new name...")
  54.     str = getFileName()
  55.   end
  56.   return str
  57. end
  58.  
  59. if (not fs.exists(pasteFolder)) then
  60.     fs.makeDir(pasteFolder)
  61. end
  62.  
  63. LOG("Starting up Pastecraft server on id " .. tostring(os.getComputerID()) .. " ...")
  64. rednet.open("back")
  65. while true do
  66.  
  67.     local e, id, text = os.pullEvent("rednet_message")
  68.     if (startsWith(text, "get")) then
  69.         pasteID = split(text)[2]
  70.         if (fs.exists(pasteFolder .. pasteID)) then
  71.             fichierLu = fs.open(pasteFolder..pasteID, "r")
  72.             toSend = textutils.serialize(fichierLu.readAll())
  73.             fichierLu.close()
  74.             rednet.send(id, "get=" .. toSend)
  75.             LOG("GET : " .. pasteID .. " by " .. tostring (id))
  76.         else
  77.             rednet.send(id, "404")
  78.             LOG("GET : NIL by " .. tostring (id) .. " (fail)")
  79.         end
  80.     elseif (startsWith(text, "put")) then
  81.         paste = textutils.unserialize(split(text)[2])
  82.         fileName = getFile()
  83.         fichierPaste = fs.open(pasteFolder .. fileName, "w")
  84.         fichierPaste.write(paste)
  85.         fichierPaste.close()
  86.         rednet.send(id, "put=" .. fileName)
  87.         LOG("PUT : " .. fileName .. " by " .. tostring(id))
  88.     else
  89.         rednet.send(id, "UNKNOWN FUNCTION")
  90.     end
  91. end
Advertisement
Add Comment
Please, Sign In to add comment