Guest User

web

a guest
Jul 23rd, 2016
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.81 KB | None | 0 0
  1. local arg={...}--command line arguments
  2.  
  3. term.setBackgroundColor(colours.white)
  4. term.setTextColor(colours.black)
  5. term.clear()
  6. term.setCursorPos(1,1)
  7. term.setCursorBlink(false)
  8. version="TMNET Browser 1.2"
  9. siteName=""
  10. currentSite=""
  11. siteID=-1
  12. pageName=""
  13. currentX=1
  14. currentY=1
  15. menuMode=false
  16. ddns=true
  17. ddnsID=-1
  18. webType=0--0=no site, 1=normal site, 2=menu
  19. -------------------------
  20. --web related functions--
  21. -------------------------
  22.  
  23. function whois(name)--returns ID if found, -1 if not on dns, and nil if no dns
  24.     --prevent unnecessary communication
  25.     if name~=currentSite then
  26.         if ddns==true then rednet.send(ddnsID,"@whois "..name)
  27.         else rednet.broadcast("@whois "..name) end
  28.         _,webID=rednet.receive(1)
  29.         siteID=tonumber(webID)--convert it to a number
  30.         currentSite=name
  31.     end
  32.     return siteID
  33. end
  34. function getColour(value)
  35.     --TODO prevent crashing on invalid number
  36.     number=tonumber(value)
  37.     if number==nil then--convert hexadecimal
  38.         if value=="A" then
  39.             number=10
  40.         elseif value=="B" then
  41.             number=11
  42.         elseif value=="C" then
  43.             number=12
  44.         elseif value=="D" then
  45.             number=13
  46.         elseif value=="E" then
  47.             number=14
  48.         elseif value=="F" then
  49.             number=15
  50.         else
  51.             return colours.black
  52.         end
  53.     end
  54.     return bit.blshift(1,number)
  55. end
  56. function loadPage(webID,page)
  57.     rednet.send(webID,page or "/home")
  58.     pagetext=""
  59.     for i=1,17 do
  60.         from,text=rednet.receive(0.5)
  61.         if text==nil then break end
  62.         pagetext=pagetext..text.."\n"
  63.     end
  64.    
  65.     --term.write(pagetext)--doesn't support newline apparently
  66.     --print(pagetext)
  67.     line=1
  68.     term.setCursorPos(2,line)
  69.     colourmode=0--1=^ detected 2=nextchar text 3=nextchar backround
  70.     term.setBackgroundColor(colours.white)
  71.     term.setTextColor(colours.black)
  72.     --my slow and tedious method of rendering the page
  73.     for current=1,string.len(pagetext) do
  74.         letter=string.sub(pagetext,current,current)--TODO surely an easier way?
  75.         if letter==nil then letter=" " end
  76.         if colourmode==0 then --normal
  77.             if letter=="^" then
  78.                 colourmode=1
  79.             elseif letter=="\n" then
  80.                 line=line+1--next line
  81.                 if line==17 then break end--too much! TODO tweak this value
  82.                 term.setCursorPos(2,line)
  83.                 term.setBackgroundColor(colours.white)
  84.                 term.setTextColor(colours.black)--reset colours for next line
  85.             else
  86.                 term.write(letter)--nothing special
  87.             end
  88.         elseif colourmode==1 then --might be a colour
  89.             if letter=="f" then--foreground colour
  90.                 colourmode=2--next char will set foreground
  91.             elseif letter=="b" then--background colour
  92.                 colourmode=3--next char will set background
  93.             else--not correct, probably not intended as a colour
  94.                 term.write("^"..letter)--put ^ back as well
  95.             end
  96.         elseif colourmode==2 then--set text colour
  97.             --print(letter)--debug
  98.             term.setTextColor(getColour(letter))
  99.             colourmode=0--back to normal
  100.         elseif colourmode==3 then--set background colour
  101.             term.setBackgroundColor(getColour(letter))
  102.             colourmode=0--back to normal
  103.         end
  104.     end
  105. end
  106.  
  107. -----------------
  108. --GUI functions--
  109. -----------------
  110. function drawError(text)
  111.     term.setBackgroundColor(colours.white)
  112.     term.setTextColor(colours.red)
  113.     term.setCursorPos(2,1)
  114.     term.write("ERROR:")
  115.     term.setCursorPos(2,2)
  116.     term.write(text)
  117.     webType=0
  118. end
  119. function drawButton(x,y,text,conditionA,conditionB,active)
  120.     term.setCursorPos(x,y)
  121.     if conditionA==conditionB and active then
  122.         term.setBackgroundColor(colours.grey)
  123.         term.write("[")
  124.     else
  125.         term.setBackgroundColor(colours.lightGrey)
  126.         term.write(" ")
  127.     end
  128.    
  129.     term.write(text)
  130.    
  131.     if conditionA==conditionB and active then
  132.         term.write("]")
  133.     else
  134.         --term.write(" ")
  135.     end
  136. end
  137.  
  138. function renderGUI()
  139.     --draw left bar
  140.     term.setTextColor(colours.white)
  141.     term.setBackgroundColor(colours.lightGrey)
  142.     for line=1,18 do
  143.         term.setCursorPos(1,line)
  144.         term.write(" ")
  145.     end
  146.     --draw right cursor
  147.     if menuMode==false then
  148.         term.setCursorPos(1,currentY)
  149.         term.setBackgroundColor(colours.grey)
  150.         term.write(">")
  151.     end
  152.     --draw bottom bar
  153.     term.setBackgroundColor(colours.lightGrey)
  154.     term.setCursorPos(1,19)
  155.     for line=1,51 do
  156.         term.write(" ")
  157.     end
  158.     --term.setCursorPos(1,19)
  159.     --term.write(siteName..pageName)
  160.     --draw menu bar items
  161.     drawButton(42,19,"Refresh",currentX,3,menuMode)
  162.     drawButton(36,19,"Menu",currentX,2,menuMode)
  163.     drawButton(1,19,siteName..pageName,currentX,1,menuMode)
  164. end
  165.  
  166. function popup(text)--a nice GUI popup asking for text
  167.     term.setTextColor(colours.white)
  168.     term.setCursorPos(7,5)
  169.     term.setBackgroundColor(colours.blue)
  170.     for y=5,13 do
  171.         term.setCursorPos(7,y)
  172.         for x=7,47 do
  173.             if y==8 then--user input row
  174.                 if x==7 or x== 47 then
  175.                     term.setBackgroundColor(colours.lightGrey)
  176.                 else
  177.                     term.setBackgroundColor(colours.grey)
  178.                 end
  179.             end
  180.             term.write(" ")
  181.         end
  182.         term.setBackgroundColor(colours.lightGrey)
  183.     end
  184.     --finished drawing window. add text
  185.     term.setBackgroundColor(colours.blue)
  186.     term.setCursorPos(7,5)--TODO: center text
  187.     term.write(text)
  188.     term.setBackgroundColor(colours.grey)
  189.     term.setCursorPos(8,8)
  190.     return io.read()
  191. end
  192.  
  193. function refresh()
  194.     webType=1--website
  195.     term.setTextColor(colours.black)
  196.     term.setBackgroundColor(colours.white)
  197.    
  198.     term.clear()
  199.     term.setCursorPos(1,1)
  200.    
  201.     renderGUI()
  202.    
  203.     webID=whois(siteName)
  204.     if webID==nil or webID==-1 then--find what went wrong
  205.             if webID==nil and ddns==false then drawError("Server Not Found (Spelling?)")
  206.         elseif webID==nil and ddns==true then drawError("No Response from DDNS")
  207.         elseif webID==-1 and ddns==true then drawError("Site Not Found on DDNS (Spelling?)")
  208.         else drawError("Unknown Error, Contact TMNET Support") end
  209.        
  210.     else loadPage(webID,pageName) end
  211.    
  212.     renderGUI()
  213. end
  214. -------------------
  215. --other functions--
  216. -------------------
  217. function getDeviceSide(deviceType)
  218.   -- List of all sides
  219.   local lstSides = {"left","right","top","bottom","front","back"};
  220.   -- Now loop through all the sides
  221.   for i, side in pairs(lstSides) do
  222.     if (peripheral.isPresent(side)) then
  223.       -- Yup, there is something on this side
  224.       if (peripheral.getType(side) == string.lower(deviceType)) then
  225.         -- Yes, this is the device type we need, so return the side
  226.         return side;
  227.       end
  228.     end
  229.   end
  230.   --nothing found, return nill
  231.   return nil;
  232. end
  233. function split(text,splitAt)
  234.     state=false
  235.     outA=""
  236.     outB=""
  237.     for i=1,string.len(text) do
  238.         if string.sub(text,i,i)==splitAt then
  239.             state=true
  240.         end
  241.         if state==false then
  242.             outA=outA..string.sub(text,i,i)
  243.         else
  244.             outB=outB..string.sub(text,i,i)
  245.         end
  246.     end
  247.     return outA,outB
  248. end
  249. --like split, but removes extra char
  250. function split2(text,splitAt)
  251.     outA,outB=split(text,splitAt)
  252.     outB=string.sub(outB,2,-1)--remove first char (= to splitAt)
  253.     return outA,outB
  254. end
  255.  
  256. function interpret(text)
  257.     command,args=split2(text,":")
  258.     if command=="glob" then
  259.         siteName,pageName=split2(args,"/")
  260.         pageName="/"..pageName
  261.         refresh()
  262.     end
  263.     if command=="loc" then
  264.         pageName="/"..args
  265.         refresh()
  266.     end
  267.     if command=="ref" then
  268.         refresh()
  269.     end
  270.     if command=="ask" then
  271.         arg1,arg2=split2(args,":")--expected format: ask:cookie:question
  272.         rednet.send(siteID,"ans:"..arg1..":"..popup(arg2))
  273.         print("please wait...")--TODO place this nicely in the text entry field
  274.         --note: server should send refresh as soon as it is done
  275.     end
  276.     --TODO add input command
  277. end
  278. function enterPage()
  279.     siteName,pageName=split(popup("web address"),"/")
  280.     if pageName==nil or pageName=="" then
  281.         pageName="/home"
  282.     end
  283.     refresh()
  284. end
  285. function renderMenu()
  286.     term.setTextColor(colours.black)
  287.     term.setBackgroundColor(colours.white)
  288.  
  289.     term.clear()
  290.     term.setCursorPos(1,1)
  291.     --add one space before every line due to side bar
  292.     print(" "..version)
  293.     print(" Designed by Train Master Network Systems, Inc.")
  294.     print("")
  295.     if ddnsID~=nil then print(" ID of Dedicated DNS: "..ddnsID) end
  296.     if siteID~=nil then print(" ID of Current Website: "..siteID) end
  297.     print(" Using Modem on Side: "..portSide)
  298.     renderGUI()
  299. end
  300. function handleSelect()
  301.     if menuMode==true then
  302.         --edit web adress
  303.         if currentX==1 then
  304.             enterPage()
  305.         end
  306.         if currentX==2 then
  307.             renderMenu()
  308.         end
  309.         --refresh
  310.         if currentX==3 then
  311.             refresh()
  312.         end
  313.     else--clicked on site
  314.         if webType==1 then
  315.             rednet.send(siteID,"exec:"..pageName..":"..currentY)
  316.         end
  317.         --TODO add interactive menu
  318.     end
  319. end
  320. -----------------------
  321. --program begins here--
  322. -----------------------
  323. portSide=getDeviceSide("modem")
  324. if portSide==nil then
  325.     print("No Modems Found!")
  326. end
  327. rednet.open(portSide)
  328. rednet.broadcast("@ddns")--search for dedicated dns
  329. ddnsID,result=rednet.receive(1)
  330. if result==nil then
  331. ddns=false
  332. print(" Warning: No DDNS Found")
  333. end
  334. renderGUI()
  335. enterPage()
  336. renderGUI()
  337. while true do
  338.     event, p1, p2, p3 = os.pullEventRaw()
  339.     if event=="key" or event=="char" then
  340.         --up
  341.         if p1==200 then
  342.             if currentY~=1 then currentY=currentY-1 end
  343.             menuMode=false
  344.         end
  345.         --down
  346.         if p1==208  then
  347.             if currentY~=18 then currentY=currentY+1 end
  348.             menuMode=false
  349.         end
  350.        
  351.         --left
  352.         if p1==203 then
  353.             if currentX~=1 then currentX=currentX-1 end
  354.             menuMode=true
  355.         end
  356.         --right
  357.         if p1==205 then
  358.             if currentX~=3 then currentX=currentX+1 end
  359.             menuMode=true
  360.         end
  361.         --enter or space (select)
  362.         if p1==28 or p1==" "  then --removed 57
  363.             handleSelect()
  364.         end
  365.         renderGUI()
  366.     elseif event=="mouse_click" then
  367.         if p3==19 then
  368.             menuMode=true
  369.             currentX=1--default if below is false
  370.             if p2>36 then currentX=2 end
  371.             if p2>42 then currentX=3 end
  372.         else
  373.             menuMode=false
  374.             currentY=p3
  375.         end
  376.         handleSelect()
  377.         renderGUI()
  378.     --only receive messages from current website
  379.     elseif event=="rednet_message" and p1==siteID then
  380.         interpret(p2)
  381.     elseif event=="terminate" then
  382.         term.setBackgroundColor(colours.black)
  383.         term.setTextColor(colours.white)
  384.         term.clear()
  385.         term.setCursorPos(1,1)
  386.     break
  387.     end
  388.     --TODO make an options menu
  389.     --TODO add error handling
  390. end
Advertisement
Add Comment
Please, Sign In to add comment