Advertisement
xXm0dzXx

RNET Protocol (rnet://)

Nov 10th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.56 KB | None | 0 0
  1. local x,y = term.getSize()
  2. local currentURL = "home"
  3. local hasModem = true
  4. local theme = "default"
  5. local rednetType = "Rednet"
  6. browserAgent = "rex_renewed"
  7.  
  8. local WebsiteDatabase = "http://nexusindustries.x10.mx/Websites/folder/" --Folder with all the sites
  9. local ApiPath = "http://nexusindustries.x10.mx/Websites/mcmain.php" --Path API is stored
  10.  
  11. local function cPrint(text)
  12. x2,y2 = term.getCursorPos()
  13. term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  14. write(text.. "\n")
  15. end
  16.  
  17. local function homePage()
  18. cPrint("__________ _______________ ___")
  19. cPrint("\\______ \\\\_ _____/\\ \\/ /")
  20. cPrint(" | _/ | __)_ \\ / ")
  21. cPrint(" | | \\ | \\ / \\ ")
  22. cPrint(" |____|_ //_______ //___/\\ \\")
  23. cPrint(" \\/ \\/ \\_/")
  24. cPrint("~~=> Renewed <=~~")
  25. print()
  26. cPrint("Default sites: ")
  27. cPrint("rdnt://home Default Homepage")
  28. cPrint("rdnt://settings Settings ")
  29. cPrint("rdnt://newsite Website Maker ")
  30. print()
  31. cPrint("rdnt://exit")
  32. cPrint("Check out")
  33. cPrint("play.geevancraft.com")
  34. end
  35.  
  36. function rnetHost()
  37. term.clear()
  38. term.setCursorPos(1,1)
  39. cPrint("Rednet Explorer Online Servers")
  40. write("Domain (ex: YourDomain.com): rnet://")
  41. domain = read()
  42. write("Path to file: ")
  43. path = read()
  44. if fs.exists(path) then
  45. file = fs.open(path, "r")
  46. if file then
  47. data = file.readAll()
  48. file.close()
  49. if not data then
  50. print("Failed to read " ..path)
  51. return
  52. end
  53. local response = http.post(
  54. ApiPath,
  55. "type=upload&"..
  56. "user=guest&"..
  57. "pass=guest&"..
  58. "name=".. textutils.urlEncode(domain) .. "&"..
  59. "data=" ..textutils.urlEncode(data)
  60. )
  61.  
  62. if response then
  63. local sResponse = response.readAll()
  64. response.close()
  65. print("Done!")
  66. print("Log: ")
  67. sleep(0.5)
  68. if string.find(sResponse, "success") then
  69. print("Uploading " ..shell.resolve(path).. " complete!")
  70. print("Errors: 0")
  71. print("\nGo to " ..domain.. " to check the site!")
  72. else
  73. print("Failed : " ..sResponse)
  74. print()
  75. if sResponse == "Write_lock" then
  76. print("The write_lock error means you have been locked out from uploading because are a hacker, a troll, or a spammer that is trying to upload a file to mess up my database and you are gonna be hunted down and killed (not IRL) by my team :)\n\nFYI, your IP has been traced.")
  77. end
  78. end
  79. else
  80. print("Failed to connect to database.")
  81. end
  82. else
  83. print("Failed to open " ..path)
  84. end
  85. else
  86. print(path.. " doesn't exist.")
  87. end
  88. end
  89.  
  90. local function newButton( currentID, id, text, fez )
  91. if not fez then
  92. term.clearLine()
  93. end
  94. if currentID == id then
  95. cPrint("[ " ..text.. " ]")
  96. else
  97. cPrint(text)
  98. end
  99. end
  100.  
  101. local function settings()
  102. local currentID = 1
  103. local function settingMenu()
  104. term.setCursorPos(1,5)
  105. cPrint("Configuration")
  106. print()
  107. newButton( currentID, 1, "Download Settings (" ..rednetType.. ")" )
  108. newButton( currentID, 2, "Homepage" )
  109. newButton( currentID, 3, "Extensions (NYI)" )
  110. newButton( currentID, 4, "Theme (" ..theme.. ")" )
  111. print()
  112. newButton( currentID, 5, "Exit" )
  113. end
  114.  
  115. while true do
  116. settingMenu()
  117. local event, key = os.pullEvent("key")
  118. if key == keys.up then
  119. if currentID ~= 1 then
  120. currentID = currentID -1
  121. end
  122. elseif key == keys.down then
  123. if currentID ~= 5 then
  124. currentID = currentID +1
  125. end
  126. elseif key == keys.enter then
  127. if currentID == 1 then
  128. if rednetType == "Rednet" then
  129. rednetType = "HTTP"
  130. elseif rednetType == "HTTP" then
  131. rednetType = "rnet"
  132. elseif rednetType == "rnet" then
  133. rednetType = "Rednet"
  134. end
  135.  
  136. loadWebpage( "settings" )
  137. elseif currentID == 2 then
  138. local newID = 1
  139. local currentTheme = ""
  140. local function settingMenu()
  141. term.clear()
  142. term.setCursorPos(1,1)
  143. cPrint("Homepage")
  144. file = fs.open( ".rexsettings", "r" )
  145. cPrint( "rdnt://" ..file.readLine() )
  146. currentTheme = file.readLine()
  147. file.close()
  148. print()
  149. newButton( newID, 1, "Change" )
  150. newButton( newID, 2, "Done" )
  151. end
  152.  
  153. while true do
  154. settingMenu()
  155. local event, key = os.pullEvent("key")
  156. if key == keys.up then
  157. newID = 1
  158. elseif key == keys.down then
  159. newID = 2
  160. elseif key == keys.enter then
  161. if newID == 1 then
  162. term.setCursorPos(1,2)
  163. term.clearLine()
  164. write("rdnt://")
  165. local address = read()
  166. file = fs.open( ".rexsettings", "w" )
  167. file.write( address.. "\n" )
  168. file.write( currentTheme )
  169. file.close()
  170. else
  171. loadWebpage("settings")
  172. end
  173. end
  174. end
  175. elseif currentID == 3 then
  176. elseif currentID == 4 then
  177. if theme == "default" then
  178. theme = "old"
  179. elseif theme == "old" then
  180. theme = "edit"
  181. elseif theme == "edit" then
  182. theme = "default"
  183. end
  184.  
  185. file = fs.open( ".rexsettings", "r" )
  186. local humpage = file.readLine()
  187. file.close()
  188. file = fs.open( ".rexsettings", "w" )
  189. file.write( humpage.. "\n" )
  190. file.write( theme )
  191. file.close()
  192. loadWebpage( "settings" )
  193. elseif currentID == 5 then
  194. break
  195. end
  196. end
  197. end
  198.  
  199. loadWebpage( "home" )
  200. end
  201.  
  202. local function newButton2( currentID, id, text )
  203. term.clearLine()
  204. if currentID == id then
  205. print("[>] " ..text)
  206. else
  207. print("[ ] " ..text)
  208. end
  209. end
  210.  
  211. function newServer()
  212. print("Welcome to Rednet Servers!")
  213. print("Before we begin, please enter the URL of the website you want: ")
  214. write("\nrdnt://")
  215. newURL = read()
  216. rednet.broadcast( newURL )
  217. local id,message = rednet.receive( 0.5 )
  218. if message then
  219. print( "A website with this URL is already owned by " ..id.. ", continue? (Y/N)" )
  220. if string.lower( read() ) ~= "y" then
  221. return
  222. end
  223. end
  224.  
  225. function drawFooter()
  226. term.clear()
  227. term.setCursorPos(1,1)
  228. write("Hosting rdnt://" ..newURL)
  229. local x,y = term.getSize()
  230. for i=1,x do
  231. term.setCursorPos(i,2)
  232. write("-")
  233. end
  234. end
  235.  
  236. function drawBrowser()
  237. term.setCursorPos(1,3)
  238. if fs.exists( "." ..newURL ) and fs.isDir( "." ..newURL ) then
  239. if fs.exists( "." ..newURL.. "/index" ) then
  240. else
  241. file = fs.open( "." ..newURL.. "/index", "w" )
  242. file.write( "print(\"This site has not yet been configured.\")" )
  243. file.close()
  244. end
  245. else
  246. fs.makeDir( "." ..newURL )
  247. file = fs.open( "." ..newURL.. "/index", "w" )
  248. file.write( "print(\"This site has not yet been configured.\")" )
  249. file.close()
  250. end
  251.  
  252. local filePath = fs.list( "." ..newURL )
  253. for i=1,#filePath do
  254. newButton2( currentID, i, filePath[i] )
  255. end
  256. end
  257.  
  258. local status = "[Delete]New Edit"
  259. currentID = 1
  260. while true do
  261. drawFooter()
  262. drawBrowser()
  263. term.setCursorPos(1,y)
  264. term.clearLine()
  265. write(status)
  266. local event, key, message = os.pullEvent()
  267. if event == "rednet_message" then
  268. if message == newURL then
  269. file = fs.open( "." ..newURL.. "/index", "r" )
  270. rednet.send( key, file.readAll() )
  271. file.close()
  272. elseif fs.exists( "." ..message ) then
  273. file = fs.open( "." ..message, "r" )
  274. if file then
  275. rednet.send( key, file.readAll() )
  276. file.close()
  277. end
  278. end
  279. elseif event == "key" then
  280. if key == keys.left then
  281. if status == " Delete[New]Edit" then
  282. status = "[Delete]New Edit"
  283. elseif status == " Delete New[Edit]" then
  284. status = " Delete[New]Edit"
  285. end
  286. elseif key == keys.right then
  287. if status == " Delete[New]Edit" then
  288. status = " Delete New[Edit]"
  289. elseif status == "[Delete]New Edit" then
  290. status = " Delete[New]Edit"
  291. end
  292. elseif key == keys.up then
  293. if currentID ~= 1 then
  294. currentID = currentID -1
  295. end
  296. elseif key == keys.down then
  297. if currentID ~= #fs.list( "." ..newURL ) then
  298. currentID = currentID +1
  299. end
  300. elseif key == keys.enter then
  301. if status == "[Delete]New Edit" then
  302. local filePath = fs.list( "." ..newURL )
  303. fs.delete( "." ..newURL.. "/" ..filePath[ currentID ] )
  304. currentID = 1
  305. elseif status == " Delete[New]Edit" then
  306. term.setCursorPos(1,y)
  307. term.clearLine()
  308. write("URL: rdnt://" ..newURL.. "/")
  309. local fileName = read()
  310. local number = 1
  311. local origName = fileName
  312.  
  313. while fs.exists( "." ..newURL.. "/" ..fileName ) do
  314. fileName = origName.. " [" ..number.. "]"
  315. number = number +1
  316. end
  317.  
  318. file = fs.open( "." ..newURL.. "/" ..fileName, "w" )
  319. file.close()
  320. else
  321. local filePath = fs.list( "." ..newURL )
  322. shouldIendThis = false
  323.  
  324. function a()
  325. while true do
  326. local timer = os.startTimer( 0.5 )
  327. local event, key, message = os.pullEvent()
  328. if event == "rednet_message" then
  329. if message == newURL then
  330. file = fs.open( "." ..newURL.. "/index", "r" )
  331. rednet.send( key, file.readAll() )
  332. file.close()
  333. elseif fs.exists( "." ..message ) then
  334. file = fs.open( "." ..message, "r" )
  335. if file then
  336. rednet.send( key, file.readAll() )
  337. file.close()
  338. end
  339. end
  340. elseif shouldIendThis then
  341. break
  342. end
  343. end
  344. end
  345.  
  346. function b()
  347. shell.run( "edit", "." ..newURL.. "/" ..filePath[ currentID ] )
  348. shouldIendThis = true
  349. end
  350.  
  351. parallel.waitForAny( a, b )
  352. drawFooter()
  353. end
  354. end
  355. end
  356. end
  357. end
  358.  
  359. function loadWebpage( url, id )
  360. if not url then
  361. url = currentURL
  362. end
  363. currentURL = url
  364.  
  365. term.clear()
  366. term.setCursorPos(1,1)
  367.  
  368. if theme == "default" then
  369. if rednetType == "HTTP" then
  370. print("http://" ..currentURL)
  371. elseif rednetType == "rnet" then
  372. print("rnet://" ..currentURL)
  373. else
  374. print("rdnt://" ..currentURL)
  375. end
  376.  
  377. for i=1,x do
  378. term.setCursorPos(i,2)
  379. write("-")
  380. end
  381. term.setCursorPos(1,3)
  382. elseif theme == "old" then
  383. cPrint("Rednet Explorer")
  384. if rednetType == "HTTP" then
  385. cPrint("http://" ..currentURL )
  386. elseif rednetType == "rnet" then
  387. cPrint("rnet://" ..currentURL )
  388. else
  389. cPrint("rdnt://" ..currentURL)
  390. end
  391. term.setCursorPos(1,4)
  392. elseif theme == "edit" then
  393. term.setCursorPos(1,y)
  394. write("Press CTRL to access menu")
  395. end
  396.  
  397. if url == "home" then
  398. homePage()
  399. elseif url == "newsite" then
  400. if rednetType == "rnet" then
  401. rnetHost()
  402. else
  403. newServer()
  404. end
  405. elseif url == "settings" then
  406. settings()
  407. elseif url == "exit" then
  408. error()
  409. else
  410. if rednetType == "HTTP" then
  411. http.request( "http://" ..url )
  412. readTimedOut = os.startTimer(3)
  413. local text = ""
  414. while true do
  415. local event, body, url = os.pullEvent()
  416. if event == "http_success" then
  417. text = url.readAll()
  418. break
  419. elseif event == "http_failed" then
  420. text = "Failed to load"
  421. break
  422. elseif event == "timer" and body == readTimedOut then
  423. text = "Read timed out"
  424. break
  425. end
  426. end
  427.  
  428. print( text )
  429. elseif rednetType == "rnet" then
  430. local response = http.post(
  431. ApiPath,
  432. "user=guest&"..
  433. "pass=guest&"..
  434. "type=download&"..
  435. "data=blank&"..
  436. "name=".. textutils.urlEncode(url)
  437. )
  438.  
  439. if response then
  440. local body = response.readAll()
  441. response.close()
  442. if body == nil or body == "" or string.find(body, "auth_error") or string.find(body, "file_not_found") then
  443. print("Unable to load webpage.")
  444. print("\nThe website you have requested was unable to work. If you beleive this is a error please contact your rednet service providers. (RSP)")
  445. print("\nThis may be the result of the list below: ")
  446. print("1) The site you requested is down.")
  447. print("2) The site you requested is corrupted.")
  448. print("3) The site you requested forgot to put a modem")
  449. print("4) You have no modem")
  450. print("5) This message is faked, and you have a virus now.")
  451. else
  452. fs.delete(".downloadedWebsite")
  453. webpage = fs.open(".downloadedWebsite", "w")
  454. webpage.write(body)
  455. webpage.close()
  456. shell.run(".downloadedWebsite")
  457. end
  458. end
  459. else
  460. if id then
  461. fs.delete(".downloadedWebsite")
  462. file = fs.open(".downloadedWebsite", "w")
  463. file.write( id )
  464. file.close()
  465. shell.run(".downloadedWebsite")
  466. else
  467. if hasModem then
  468. if id then
  469. rednet.send( id, url )
  470. else
  471. rednet.broadcast( url )
  472. end
  473.  
  474. local timer = os.startTimer(0)
  475. local messages = {}
  476. repeat
  477. local event, key, message = os.pullEvent()
  478. if event == "rednet_message" then
  479. if id then
  480. if key == id then
  481. messages[#messages+1] = {
  482. ["id"] = key,
  483. ["code"] = message,
  484. }
  485. end
  486. else
  487. messages[#messages+1] = {
  488. ["id"] = key,
  489. ["code"] = message,
  490. }
  491. end
  492. end
  493. until event == "timer" and key == timer
  494.  
  495. if #messages == 1 then
  496. fs.delete(".downloadedWebsite")
  497. file = fs.open(".downloadedWebsite", "w")
  498. file.write( messages[1]["code"] )
  499. file.close()
  500. shell.run(".downloadedWebsite")
  501. elseif #messages == 0 then
  502. print("Unable to load webpage.")
  503. print("\nThe website you have requested was unable to work. If you beleive this is a error please contact your rednet service providers. (RSP)")
  504. print("\nThis may be the result of the list below: ")
  505. print("1) The site you requested is down.")
  506. print("2) The site you requested is corrupted.")
  507. print("3) The site you requested forgot to put a modem")
  508. print("4) You have no modem")
  509. print("5) This message is faked, and you have a virus now.")
  510. else
  511. local id = 0
  512. local valvez = true
  513. id = messages[1]["id"]
  514. for i=2,#messages do
  515. if messages[i]["id"] ~= id then
  516. valvez = false
  517. end
  518. end
  519.  
  520. if valvez then
  521. fs.delete(".downloadedWebsite")
  522. file = fs.open(".downloadedWebsite", "w")
  523. file.write( messages[1]["code"] )
  524. file.close()
  525. shell.run(".downloadedWebsite")
  526. else
  527. local x1, y1 = term.getCursorPos()
  528. local currentID = 1
  529. while true do
  530. term.setCursorPos(1,y1)
  531. cPrint("+---Conflict Warning---+")
  532. cPrint("| 2 or more hosts have |")
  533. cPrint("| been found, please |")
  534. cPrint("| select the correct ID|")
  535. cPrint("+----------------------+")
  536. for i=1,#messages do
  537. local xx, yy = term.getCursorPos()
  538. cPrint("| |")
  539. term.setCursorPos(xx, yy)
  540. newButton( currentID, i, "#" ..messages[i]["id"], true )
  541. end
  542.  
  543. cPrint("+----------------------+")
  544.  
  545. local event, key = os.pullEvent("key")
  546. if key == keys.up then
  547. if currentID ~= 1 then
  548. currentID = currentID -1
  549. end
  550. elseif key == keys.down then
  551. if currentID ~= #messages then
  552. currentID = currentID +1
  553. end
  554. elseif key == keys.enter then
  555. local code = messages[currentID]["code"]
  556. loadWebpage( url, code )
  557. break
  558. end
  559. end
  560. end
  561. end
  562. else
  563. print("\nUnable to connect because no modem has been detected on your computer. Please attatch a modem and restart this program. ")
  564. end
  565. end
  566. end
  567. end
  568.  
  569. if theme == "default" then
  570. term.setCursorPos(x-#("Press CTRL to brows"), 1)
  571. write("Press CTRL to browse")
  572. elseif theme == "old" then
  573. term.setCursorPos(x-#("Press CTRL to explor"), y)
  574. write("Press CTRL to explore")
  575. end
  576.  
  577. repeat
  578. local _, key = os.pullEvent("key")
  579. until key == 29 or key == 157
  580.  
  581. if theme == "default" then
  582. term.setCursorPos(1,1)
  583. term.clearLine()
  584.  
  585. if rednetType == "HTTP" then
  586. write("http://")
  587. elseif rednetType == "rnet" then
  588. write("rnet://")
  589. else
  590. write("rdnt://")
  591. end
  592. elseif theme == "old" or theme == "edit" then
  593. term.setCursorPos(1,2)
  594. term.clearLine()
  595.  
  596. if rednetType == "HTTP" then
  597. write("http://")
  598. elseif rednetType == "rnet" then
  599. write("rnet://")
  600. else
  601. write("rdnt://")
  602. end
  603. end
  604.  
  605. loadWebpage( read() )
  606. end
  607.  
  608. local function openModems()
  609. for i,v in pairs( rs.getSides() ) do
  610. if rednet.open( v ) then
  611. return true
  612. end
  613. end
  614. end
  615.  
  616. openModems()
  617.  
  618. if fs.exists(".rexsettings") then
  619. file = fs.open(".rexsettings", "r")
  620. currentURL = file.readLine()
  621. theme = file.readLine()
  622. file.close()
  623. else
  624. file = fs.open(".rexsettings", "w")
  625. file.write( "home\n" )
  626. file.write( "default" )
  627. file.close()
  628. end
  629.  
  630. loadWebpage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement