Advertisement
Guest User

some kind of web browser in Lua

a guest
Jun 29th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.18 KB | None | 0 0
  1. dofile("tab2file") -- 2 new functions to write and read ini-like files
  2.  
  3. cls = function()
  4.   term.clear()
  5.   term.setCursorPos(1,1)
  6. end
  7.  
  8. main = function()
  9.   cls()
  10.   if fs.exists("sec.config") then
  11.     sec = file2tab("sec.config")
  12.   else
  13.     sec = {}
  14.     sec.open = 1
  15.     sec.shutdown = 1
  16.     sec.net = 0
  17.     tab2file(sec,"sec.config")
  18.   end
  19.   print("1. Browse\n2. Security\n3. Exit")
  20.   local ans = read()
  21.   if ans == "1" then
  22.     return browser()
  23.   elseif ans == "2" then
  24.     return security()
  25.   elseif ans == "3" then
  26.     return
  27.   else
  28.     return main()
  29.   end
  30. end
  31.  
  32. security = function()
  33.   cls()
  34.   print("1. File opening: "..tonumber(sec.file))
  35.   print("2. Shutdown: "..tonumber(sec.shutdown))
  36.   print("3. Rednet access: "..tonumber(sec.net))
  37.   print("4. Exit")
  38.   local num = {sec.file, sec.shutdown, sec.net}
  39.   local ans = tonumber(read())
  40.   if num[ans] then
  41.     if sec[ans] == 1 then
  42.       sec[ans] = 0
  43.     else
  44.       sec[ans] = 1
  45.     end
  46.     tab2file(sec,"sec.config")
  47.     return security()
  48.   elseif ans == 4 then
  49.     return main()
  50.   else
  51.     return security()
  52.   end
  53. end
  54.  
  55. browser = function()
  56.  
  57.   write("URL: ")
  58.   local ans = read()
  59.   sleep(1)
  60.   _,_,id = string.find(ans, "^(%d+)$")
  61.   if id == nil then
  62.     _,_,id,path = string.find(ans, "^(%d+)/([/%w]+)$")
  63.   else
  64.     path = ""
  65.   end
  66.   if id == nil then
  67.     print("Incorrect request")
  68.     sleep(1)
  69.     cls()
  70.     return browser()
  71.   end
  72.  
  73.   rednet.send(tonumber(id),path)
  74.   _, mes = rednet.receive()
  75.   file = fs.open("page.tmp","w")
  76.   file.write(mes)
  77.   file.close()
  78.  
  79.   sec = file2tab("sec.config")
  80.   local old = {}
  81.   old.open = fs.open
  82.   old.shutdown = os.shutdown
  83.   old.reboot = os.reboot
  84.   old.send = rednet.send
  85.   old.broadcast = rednet.broadcast
  86.   old.pullEventRaw = os.pullEventRaw
  87.   old.write = term.write
  88.   local restore = function()
  89.     fs.open = old.open
  90.     os.shutdown = old.shutdown
  91.     os.reboot = old.reboot
  92.     rednet.send = old.send
  93.     rednet.broadcast = old.broadcast
  94.     os.pullEventRaw = old.pullEventRaw
  95.     term.write = old.write
  96.   end
  97.   local l_error = error -- backup of error() for confirmation
  98.   if sec.file == 1 then
  99.     local firstfile = true -- for "page.tmp" opening in dofile()
  100.     fs.open = function(file,mode)
  101.       if firstfile == false then
  102.         print("Attempt to open file "..file.." at mode "..mode..". Proceed?")
  103.         if read() == "y" then
  104.           return old.open(file,mode)
  105.         else
  106.           return nil
  107.         end
  108.       else
  109.         firstfile = false
  110.         return old.open(file,mode)
  111.       end
  112.     end
  113.   end
  114.   if sec.shutdown == 1 then
  115.     os.shutdown = function()
  116.       print("Attempt to shutdown host. Emergency exit.")
  117.       if l_error ~= error then
  118.         print("ATTEMPT TO BYPASS EMERGENCY EXIT SYSTEM! IMMEDIATE EXIT!")
  119.         error = l_error
  120.         error("security breach attempt")
  121.       end
  122.       error("shutdown attempt")
  123.     end
  124.     os.reboot = function()
  125.       print("Attempt to reboot system. Emergency exit.")
  126.       if l_error ~= error then
  127.         print("ATTEMPT TO BYPASS EMERGENCY EXIT SYSTEM! IMMEDIATE EXIT!")
  128.         error = l_error
  129.         error("security breach attempt")
  130.       end
  131.       error("reboot attempt")
  132.     end
  133.   end
  134.   if sec.net == 1 then
  135.     rednet.send = function(id,mes)
  136.       print("Attempt to send rednet message \""..mes.."\" to host "..tostring(id)..". Proceed?")
  137.       if read() == "y" then
  138.         return old.send(id,mes)
  139.       else
  140.         return nil
  141.       end
  142.     end
  143.     rednet.broadcast = function(mes)
  144.       print("Attempt to send rednet message \""..mes.."\" to broadcasting channel. Proceed?")
  145.       if read() == "y" then
  146.         return old.broadcast(mes)
  147.       else
  148.         return nil
  149.       end
  150.     end
  151.   end
  152.   os.pullEventRaw = function(type)
  153.     if l_error ~= error then
  154.       print("ATTEMPT TO BYPASS EMERGENCY EXIT SYSTEM! IMMEDIATE EXIT!")
  155.       error = l_error
  156.       error("security breach attempt")
  157.     end
  158.     if type == nil then
  159.       res = {old.pullEventRaw()}
  160.       if res[1] == "key" and res[2] == 59 then
  161.         error("break key pressed")
  162.       end
  163.       return unpack(res)
  164.     else
  165.       while true do
  166.         res = {old.pullEventRaw()}
  167.         if res[1] == "key" and res[2] == 59 then
  168.           error("break key pressed")
  169.         end
  170.         if res[1] == type then
  171.           return unpack(res)
  172.         end
  173.       end
  174.     end
  175.   end
  176.   local save = ""
  177.   term.write = function(text)
  178.     save = save..text
  179.     old.write(text)
  180.   end
  181.  
  182.   local getSave = function()
  183.     return save
  184.   end
  185.  
  186.   k, mes = pcall(function() dofile("page.tmp") end)
  187.  
  188.   restore()
  189.   print()
  190.   if k == true then
  191.     print("Program exited correctly.")
  192.   else
  193.     print("Performed emergency exit because of error.")
  194.     print("Error message: "..tostring(mes))
  195.   end
  196.   print("Press E to exit, S to save page and exit")
  197.   while true do
  198.     local c
  199.     _, c = os.pullEventRaw("char")
  200.     if c == "e" then
  201.       break
  202.     elseif c == "s" then
  203.       print("Enter file name: ")
  204.       path = read()
  205.       file = fs.open(path,"w")
  206.       file.write(getSave())
  207.       file.close()
  208.       print("Saved")
  209.       break
  210.     end
  211.   end
  212.   return main()
  213. end
  214.  
  215. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement