Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --#### Wojbie's AerPatche Server v 0.4
- -- Copyright (c) 2017-2021 Wojbie (wojbie@wojbie.net)
- -- Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met:
- -- 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- -- 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- -- 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
- -- 4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
- -- 5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
- -- NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. YOU ACKNOWLEDGE THAT THIS SOFTWARE IS NOT DESIGNED, LICENSED OR INTENDED FOR USE IN THE DESIGN, CONSTRUCTION, OPERATION OR MAINTENANCE OF ANY NUCLEAR FACILITY.
- for i,k in pairs(redstone.getSides()) do
- if peripheral.getType(k) == "modem" then
- rednet.open(k)
- end
- end
- local id,message,domain
- local root = "/web"
- local filebrowser = [[
- local tArgs= {...}
- --# Number clamp
- local function clamp(nMin,nNumber,nMax)
- if type(nMin) ~= "number" or type(nNumber) ~= "number" or type(nMax) ~= "number" then error("Not a number.",2) end
- return math.min(math.max( nMin, nNumber ), nMax )
- end
- --Basic Menu implementation (List mode) (tFunctions is optional and will run function assigned to number selected from tOptions is exists)
- local function simpleMenu(tOptions,nStartPoint,tFunctions)
- if type( tOptions ) ~= "table" or
- (nStartPoint ~= nil and type( nStartPoint ) ~= "number") or
- (tFunctions ~= nil and type( tFunctions ) ~= "table") then
- error( "Expected table, [number], [table]", 2 )
- end
- tFunctions = tFunctions or {}
- local x,y=term.getSize()
- local cText,CBack = term.getTextColor(),term.getBackgroundColor()
- local selected = clamp(1,nStartPoint or 1,#tOptions)
- local offset = 0
- local function list()
- offset = #tOptions <= y and 0 or clamp(0,selected-math.floor(y/2),#tOptions-y)
- term.clear()
- for i=1,#tOptions do
- term.setCursorPos(1,i)
- if i+offset == selected then term.setTextColor(CBack) term.setBackgroundColor(cText) term.clearLine()
- elseif i+offset == selected+1 then term.setTextColor(cText) term.setBackgroundColor(CBack) end
- term.write(tOptions[i+offset])
- end
- term.setTextColor(cText) term.setBackgroundColor(CBack)
- end
- list()
- local event,x,y
- while true do
- event= {os.pullEvent()}
- x,y = term.getSize()
- if event[1]=="key" then
- if event[2]==keys.numPadEnter or event[2]==keys.enter then
- if type(tFunctions) == "function" then -- If table of functions is a function then run it with seleced number
- return tFunctions(selected)
- elseif tFunctions[selected] then --If not function (like true) its non selectable entry. If a function run it.
- if type(tFunctions[selected]) == "function" then return selected,tFunctions[selected]() end
- else
- return selected
- end
- elseif event[2]==keys.down then
- selected=math.min(selected+1,#tOptions)
- list()
- elseif event[2]==keys.up then
- selected=math.max(selected-1,1)
- list()
- elseif event[2]==keys.pageDown then
- selected=math.min(selected+y,#tOptions)
- list()
- elseif event[2]==keys.pageUp then
- selected=math.max(selected-y,1)
- list()
- end
- elseif event[1] == "mouse_click" then
- local line = offset + event[4]
- if line == selected then
- if type(tFunctions) == "function" then -- If table of functions is a function then run it with seleced number
- return tFunctions(selected)
- elseif tFunctions[selected] then --If not function (like true) its non selectable entry. If a function run it.
- if type(tFunctions[selected]) == "function" then return selected,tFunctions[selected]() end
- else
- return selected
- end
- else
- selected = clamp(1,line,#tOptions)
- list()
- end
- elseif event[1] == "mouse_scroll" then
- selected = clamp(1,selected+event[2],#tOptions)
- list()
- end
- end
- end
- local selected = simpleMenu(list,2,{true})
- if selected then return shell.run(tArgs[1].." "..adress[selected]) end
- ]]
- --Main part of program.
- local function isPage(sPath)
- local path = fs.combine(root,sPath)
- if fs.isDir(path) then
- local p = fs.combine(path,"index")
- if fs.exists(p) then path = p end
- end
- return fs.exists(path) and path or false
- end
- local function getPage(sPath)
- local path = isPage(sPath)
- if not path then return false end
- local data = {}
- if fs.isDir(path) then
- local list,adress = {sPath.." : "},{""}
- if string.match(sPath,"[/%\\]") then
- list[2] = ".."
- adress[2] = fs.combine(sPath,"..")
- end
- for i,k in pairs(fs.list(path)) do
- if fs.isDir(fs.combine(path,k)) then
- table.insert(list,k.."/")
- else
- table.insert(list,k)
- end
- table.insert(adress,fs.combine(sPath,k))
- end
- data[1]= "local list = {'"..table.concat(list , "','").."'}"
- data[2]= "local adress = {'"..table.concat(adress , "','").."'}"
- data[3]= filebrowser
- else
- local f = fs.open(path,"r")
- for line in f.readLine do
- table.insert(data,line)
- end
- f.close()
- end
- return data
- end
- while true do
- id,message = rednet.receive("GET")
- if type(message) == "string" then
- print("Req: "..message.." from:"..id)
- if isPage(message) then
- rednet.send(id,getPage(message),"GET")
- else
- rednet.send(id,{[[print("ERROR 404 - No page ]]..message..[[ on this server")]]},"GET")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement