Advertisement
PaymentOption

webserverBrowser

Apr 29th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.26 KB | None | 0 0
  1. -- Basic website client by PaymentOption --
  2. VERSION = "ALPHA 0.0.1"
  3. -------------------------------------------
  4.  
  5. rednet.open("top")
  6. rednet.open("bottom")
  7. rednet.open("right")
  8. rednet.open("left")
  9. rednet.open("back")
  10. rednet.open("bottom")
  11.  
  12. -- VARS --
  13. selection = 1
  14. ----------
  15.  
  16. shell.run("mkdir", "NeXuS/Website/")
  17.  
  18. -- Helper Functions --
  19. function cPrint(height, string)
  20.     local w, h = term.getSize()
  21.     local xPosition = w/2 - string.len(string)/2
  22.    
  23.     term.setCursorPos(xPosition, height)
  24.     term.write(string)
  25. end
  26.  
  27. function rPrint(height, string)
  28.     local w,h = term.getSize()
  29.     local xPos = w - string.len(string)
  30.    
  31.     term.setCursorPos(xPos, height)
  32.     term.write(string)
  33. end
  34.  
  35. function clear()
  36.     term.clear()
  37.     term.setCursorPos(1,1)
  38. end
  39. ----------------------
  40.  
  41. -- Networking related functions --
  42. function connect()
  43.     clear()
  44.    
  45.     if fs.exists("NeXuS/Website/history") == false then
  46.         term.setCursorPos(1,1)
  47.         write("Previous connections: ")
  48.         local file = fs.open("NeXuS/Website/history", "w")
  49.         file.close()
  50.     else
  51.         file = fs.open("NeXuS/Website/history", "r")
  52.         local fileContents = file.readAll()
  53.         file.close()
  54.         term.setCursorPos(1,1)
  55.         write("Previous connections: ")
  56.         term.setCursorPos(1,2)
  57.         write(fileContents)
  58.     end
  59.    
  60.     cPrint(6, "Website Address: ")
  61.     local server = read()
  62.     clear()
  63.    
  64.     file = fs.open("NeXuS/Website/history", "r")
  65.     fileContents = file.readAll()
  66.     file.close()
  67.    
  68.     if not string.find(tostring(fileContents), server) then
  69.         file = fs.open("NeXuS/Website/history", "w")
  70.         file.write(tostring(server).."\n"..tostring(fileContents))
  71.         file.close()
  72.     end
  73.    
  74.     rednet.send(tonumber(server), "connect")
  75.     sender, message = rednet.receive(1) -- Waiting for server code to be sent for execution
  76.    
  77.     shell.run("mkdir", "NeXuS/Website/"..server.."/")
  78.     file = fs.open("NeXuS/Website/"..server.."/code", "w")
  79.     file.write(tostring(message))
  80.     file.close()
  81.    
  82.     shell.run("NeXuS/Website/"..server.."/code", server)
  83. end
  84.  
  85. function disconnect(server)
  86.     rednet.send(server, "disconnect")
  87.     fs.delete("NeXuS/Website/"..server)
  88. end
  89. ----------------------------------
  90.  
  91. -- Menues and other screens --
  92. function printMenu()
  93.     if selection == 1 then cPrint(6, "[Connect]")
  94.     else                   cPrint(6, " Connect ") end
  95.    
  96.     if selection == 2 then cPrint(7, "  [Exit] ")
  97.     else                   cPrint(7, "   Exit  ") end
  98. end
  99.  
  100. function printHeader()
  101.     term.setCursorPos(1,1)
  102.     write("Welcome User")
  103.    
  104.     rPrint(16, "Version: "..VERSION)
  105.     cPrint(1, "NeXuS WebBrowser")
  106. end
  107. ------------------------------
  108.  
  109. clear()
  110. print("\n\n\n\n")
  111. print(" _   _     __   __          _____ _     ")
  112. print("| \\ | |    \\ \\ / /         |  ___| |    ")
  113. print("|  \\| | ___ \\ V /__      __| |__ | |__  ")
  114. print("| . ` |/ _ \\/   \\\\ \\ /\\ / /|  __|| '_ \\ ")
  115. print("| |\\  |  __/ /^\\ \\\\ V  V / | |___| |_) |")
  116. print("\\_| \\_/\\___\\/   \\/ \\_/\\_/  \\____/|_.__/ ")
  117. sleep(2)
  118.  
  119. while true do
  120.     clear()
  121.     printHeader()
  122.     printMenu()
  123.    
  124.     event, key = os.pullEvent("key")
  125.    
  126.     if key == 200 and selection > 1 then selection = selection-1
  127.     elseif key == 208 and selection < 2 then selection = selection+1
  128.     end
  129.    
  130.     if key == 28 and selection == 1 then connect() -- Will find a better name for this function later...
  131.     elseif key == 28 and selection == 2 then clear(); os.reboot()
  132.     end
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement