Advertisement
Jummit

Computercraft Web Server

Mar 24th, 2018
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. rednet.open("top")
  2. term.setBackgroundColor(colors.white)
  3. term.setTextColor(colors.gray)
  4. term.clear()
  5. term.setCursorPos(1, 1)
  6. local script
  7. if fs.exists("website/website.lua") then
  8.   script = dofile("website/website.lua")
  9. end
  10. local site = settings.get("serverIp") or math.random(1, 100)
  11. local siteIp = "mc."..site..".sol"
  12.  
  13. local printLog = function(path, senderId)
  14.   local path = "website/"..path
  15.   term.setTextColor(colors.gray) term.write("[request] ")
  16.   if fs.exists(path) then term.setTextColor(colors.green)
  17.   else term.setTextColor(colors.orange) end term.write(path)
  18.   for i = 10+#path, 40 do
  19.     term.write(" ")
  20.   end
  21.   term.setTextColor(colors.gray) term.write(" from ")
  22.   term.setTextColor(colors.blue) print(senderId)
  23. end
  24.  
  25. local sendFile = function(path, message, id)
  26.   file = fs.open("website/"..path, "r")
  27.  
  28.   if file then
  29.     siteData = {}
  30.     while true do
  31.       local line = file.readLine()
  32.       if not line then break end
  33.       table.insert(siteData, line)
  34.     end
  35.     term.setTextColor(colors.green)
  36.     rednet.send(id, textutils.serialize(siteData), "send"..message)
  37.   end
  38. end
  39.  
  40. local getPath = function(message)
  41.   local path = string.sub(message, #siteIp+2)
  42.   local file
  43.   if path == "" then
  44.     path = "website"
  45.   end
  46.   return path
  47. end
  48.  
  49. local searchThroughFile
  50. searchThroughFile = function(path, str)
  51.   local files = fs.list(path)
  52.   for fileNum = 1, #files do
  53.     local filepath = path.."/"..files[fileNum]
  54.     if fs.isDir(filepath) then
  55.       searchThroughFile(filepath, str)
  56.     elseif not string.find(string.sub(filepath, 8), "%.") then
  57.       local file = fs.open(filepath, "r")
  58.       local fileText = ""
  59.       while true do
  60.         local line = file.readLine()
  61.         if not line then break end
  62.         if string.sub(line, 1, 1) == " " then
  63.           fileText = fileText..string.sub(line, 2)
  64.         end
  65.       end
  66.       file.close()
  67.  
  68.       local strNum = 0
  69.       for _ in string.gmatch(string.lower(fileText.." "..siteIp..string.sub(filepath, 8)), string.lower(str)) do
  70.         strNum = strNum + 1
  71.       end
  72.       table.insert(resultPages, {page = siteIp..string.sub(filepath, 8), num = strNum})
  73.     end
  74.   end
  75. end
  76.  
  77. print("[info] started website server")
  78. while true do
  79.   local event, senderId, message, protocol = os.pullEvent("rednet_message")
  80.   if script then
  81.     script(senderId, message, protocol)
  82.   end
  83.   if protocol == "get" and message and string.sub(message, 1, #siteIp) == siteIp then
  84.     local path = getPath(message)
  85.     sendFile(path, message, senderId)
  86.     printLog(path, senderId)
  87.   elseif protocol == "find" then
  88.     resultPages = {}
  89.     searchThroughFile("website", message)
  90.     print("[search] "..message)
  91.     rednet.send(senderId, textutils.serialize(resultPages), "findResult")
  92.   elseif protocol == "getSite" then
  93.     print("[getSite]")
  94.     rednet.send(senderId, siteIp, "sendSite")
  95.   end
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement