Advertisement
pepeknamornik

Webový prohlížeč 1.2

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