Advertisement
Wojbie

AerPatche

Jul 20th, 2017 (edited)
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.66 KB | None | 0 0
  1. --#### Wojbie's AerPatche Server v 0.4
  2.  
  3. --   Copyright (c) 2017-2021 Wojbie (wojbie@wojbie.net)
  4. --   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:
  5. --   1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  6. --   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.
  7. --   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.
  8. --   4. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  9. --   5. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  10. --   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.
  11.  
  12. for i,k in pairs(redstone.getSides()) do
  13.     if peripheral.getType(k) == "modem" then
  14.         rednet.open(k)
  15.     end
  16. end
  17.  
  18. local id,message,domain
  19. local root = "/web"
  20.  
  21. local filebrowser = [[
  22.  
  23. local tArgs= {...}
  24.  
  25. --# Number clamp
  26. local function clamp(nMin,nNumber,nMax)
  27.     if type(nMin) ~= "number" or type(nNumber) ~= "number" or type(nMax) ~= "number" then error("Not a number.",2) end
  28.     return math.min(math.max( nMin, nNumber ), nMax )
  29. end
  30.  
  31. --Basic Menu implementation (List mode) (tFunctions is optional and will run function assigned to number selected from tOptions is exists)
  32. local function simpleMenu(tOptions,nStartPoint,tFunctions)
  33.  
  34.     if type( tOptions ) ~= "table" or
  35.         (nStartPoint ~= nil and type( nStartPoint ) ~= "number") or
  36.         (tFunctions ~= nil and type( tFunctions ) ~= "table") then
  37.         error( "Expected table, [number], [table]", 2 )
  38.     end
  39.     tFunctions = tFunctions or {}
  40.    
  41.     local x,y=term.getSize()
  42.     local cText,CBack = term.getTextColor(),term.getBackgroundColor()
  43.     local selected = clamp(1,nStartPoint or 1,#tOptions)
  44.     local offset = 0
  45.    
  46.     local function list()
  47.         offset = #tOptions <= y and 0 or clamp(0,selected-math.floor(y/2),#tOptions-y)
  48.         term.clear()
  49.         for i=1,#tOptions do
  50.             term.setCursorPos(1,i)
  51.             if i+offset == selected then term.setTextColor(CBack) term.setBackgroundColor(cText) term.clearLine()
  52.             elseif i+offset == selected+1 then term.setTextColor(cText) term.setBackgroundColor(CBack) end
  53.             term.write(tOptions[i+offset])
  54.         end
  55.         term.setTextColor(cText) term.setBackgroundColor(CBack)
  56.     end
  57.     list()
  58.     local event,x,y
  59.     while true do
  60.         event= {os.pullEvent()}
  61.         x,y = term.getSize()
  62.         if event[1]=="key" then
  63.             if event[2]==keys.numPadEnter or event[2]==keys.enter then
  64.                 if type(tFunctions) == "function" then -- If table of functions is a function then run it with seleced number
  65.                     return tFunctions(selected)
  66.                 elseif tFunctions[selected] then --If not function (like true) its non selectable entry. If a function run it.
  67.                     if type(tFunctions[selected]) == "function" then return selected,tFunctions[selected]() end
  68.                 else
  69.                     return selected
  70.                 end
  71.             elseif event[2]==keys.down then
  72.                     selected=math.min(selected+1,#tOptions)
  73.                     list()
  74.             elseif event[2]==keys.up then
  75.                     selected=math.max(selected-1,1)
  76.                     list()
  77.             elseif event[2]==keys.pageDown then
  78.                     selected=math.min(selected+y,#tOptions)
  79.                     list()
  80.             elseif event[2]==keys.pageUp then
  81.                     selected=math.max(selected-y,1)
  82.                     list()
  83.             end
  84.         elseif event[1] == "mouse_click" then
  85.             local line = offset + event[4]
  86.             if line == selected then
  87.                 if type(tFunctions) == "function" then -- If table of functions is a function then run it with seleced number
  88.                     return tFunctions(selected)
  89.                 elseif tFunctions[selected] then --If not function (like true) its non selectable entry. If a function run it.
  90.                     if type(tFunctions[selected]) == "function" then return selected,tFunctions[selected]() end
  91.                 else
  92.                     return selected
  93.                 end
  94.             else
  95.                 selected = clamp(1,line,#tOptions)
  96.                 list()
  97.             end
  98.         elseif event[1] == "mouse_scroll" then
  99.             selected = clamp(1,selected+event[2],#tOptions)
  100.             list()
  101.         end
  102.     end
  103.  
  104. end
  105.  
  106. local selected = simpleMenu(list,2,{true})
  107. if selected then return shell.run(tArgs[1].." "..adress[selected]) end
  108. ]]
  109.  
  110. --Main part of program.
  111.  
  112. local function isPage(sPath)
  113.     local path = fs.combine(root,sPath)
  114.     if fs.isDir(path) then
  115.         local p = fs.combine(path,"index")
  116.         if fs.exists(p) then path = p end
  117.     end
  118.     return fs.exists(path) and path or false
  119. end
  120.  
  121. local function getPage(sPath)
  122.     local path = isPage(sPath)
  123.     if not path then return false end
  124.     local data = {}
  125.     if fs.isDir(path) then
  126.         local list,adress = {sPath.." : "},{""}
  127.         if string.match(sPath,"[/%\\]") then
  128.             list[2] = ".."
  129.             adress[2] = fs.combine(sPath,"..")
  130.         end
  131.         for i,k in pairs(fs.list(path)) do
  132.             if fs.isDir(fs.combine(path,k)) then
  133.                 table.insert(list,k.."/")
  134.             else
  135.                 table.insert(list,k)
  136.             end
  137.             table.insert(adress,fs.combine(sPath,k))
  138.         end
  139.         data[1]= "local list = {'"..table.concat(list , "','").."'}"
  140.         data[2]= "local adress = {'"..table.concat(adress , "','").."'}"
  141.         data[3]= filebrowser
  142.     else
  143.         local f = fs.open(path,"r")
  144.         for line in f.readLine do
  145.             table.insert(data,line)
  146.         end
  147.         f.close()
  148.     end
  149.     return data
  150. end
  151.  
  152. while true do
  153.     id,message = rednet.receive("GET")
  154.     if type(message) == "string" then
  155.         print("Req: "..message.." from:"..id)
  156.         if isPage(message) then
  157.             rednet.send(id,getPage(message),"GET")
  158.         else
  159.             rednet.send(id,{[[print("ERROR 404 - No page ]]..message..[[ on this server")]]},"GET")
  160.         end
  161.     end
  162. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement