Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("top")
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.gray)
- term.clear()
- term.setCursorPos(1, 1)
- local script
- if fs.exists("website/website.lua") then
- script = dofile("website/website.lua")
- end
- local site = settings.get("serverIp") or math.random(1, 100)
- local siteIp = "mc."..site..".sol"
- local printLog = function(path, senderId)
- local path = "website/"..path
- term.setTextColor(colors.gray) term.write("[request] ")
- if fs.exists(path) then term.setTextColor(colors.green)
- else term.setTextColor(colors.orange) end term.write(path)
- for i = 10+#path, 40 do
- term.write(" ")
- end
- term.setTextColor(colors.gray) term.write(" from ")
- term.setTextColor(colors.blue) print(senderId)
- end
- local sendFile = function(path, message, id)
- file = fs.open("website/"..path, "r")
- if file then
- siteData = {}
- while true do
- local line = file.readLine()
- if not line then break end
- table.insert(siteData, line)
- end
- term.setTextColor(colors.green)
- rednet.send(id, textutils.serialize(siteData), "send"..message)
- end
- end
- local getPath = function(message)
- local path = string.sub(message, #siteIp+2)
- local file
- if path == "" then
- path = "website"
- end
- return path
- end
- local searchThroughFile
- searchThroughFile = function(path, str)
- local files = fs.list(path)
- for fileNum = 1, #files do
- local filepath = path.."/"..files[fileNum]
- if fs.isDir(filepath) then
- searchThroughFile(filepath, str)
- elseif not string.find(string.sub(filepath, 8), "%.") then
- local file = fs.open(filepath, "r")
- local fileText = ""
- while true do
- local line = file.readLine()
- if not line then break end
- if string.sub(line, 1, 1) == " " then
- fileText = fileText..string.sub(line, 2)
- end
- end
- file.close()
- local strNum = 0
- for _ in string.gmatch(string.lower(fileText.." "..siteIp..string.sub(filepath, 8)), string.lower(str)) do
- strNum = strNum + 1
- end
- table.insert(resultPages, {page = siteIp..string.sub(filepath, 8), num = strNum})
- end
- end
- end
- print("[info] started website server")
- while true do
- local event, senderId, message, protocol = os.pullEvent("rednet_message")
- if script then
- script(senderId, message, protocol)
- end
- if protocol == "get" and message and string.sub(message, 1, #siteIp) == siteIp then
- local path = getPath(message)
- sendFile(path, message, senderId)
- printLog(path, senderId)
- elseif protocol == "find" then
- resultPages = {}
- searchThroughFile("website", message)
- print("[search] "..message)
- rednet.send(senderId, textutils.serialize(resultPages), "findResult")
- elseif protocol == "getSite" then
- print("[getSite]")
- rednet.send(senderId, siteIp, "sendSite")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement