SpaceRanger4321

ggggg

Feb 26th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.89 KB | None | 0 0
  1. local version = "0.5.2"
  2. local code_is_a_mess = "not necessarily"
  3.  
  4. local _KSML_ = loadfile("kst/ksml.lua")
  5. local _SAND_ = loadfile("kst/sandbox.lua")
  6. local _INQU_ = loadfile("kst/inquire.lua")
  7.  
  8. local function CALL(server)
  9. http.request(server, nil, headers)
  10. end
  11.  
  12. local hardware = "Computer"
  13.  
  14. local useHTTPSproxy = _HOST:find("CCEmuRedux") ~= nil
  15.  
  16. do
  17. if turtle then
  18. hardware = "Turtle"
  19. end
  20.  
  21. if pocket then
  22. hardware = "Pocket Computer"
  23. end
  24.  
  25. if term.isColour() then
  26. hardware = "Advanced " .. hardware
  27. end
  28.  
  29. if command then
  30. hardware = "Command Computer"
  31. end
  32. end
  33.  
  34. local t = {}
  35. local tabs = 0
  36. local activetab = 1
  37. local baredit = ""
  38. local certificates = ""
  39.  
  40. local coloxrs = colours or colors
  41. coloxrs.grxy = coloxrs.grey or coloxrs.gray
  42. coloxrs.lightGrxy = coloxrs.lightGrey or coloxrs.lightGray
  43.  
  44. local w, h = term.getSize()
  45. local xy, tc, bg = term.setCursorPos, term.setTextColor, term.setBackgroundColor
  46.  
  47. local port = window.create(term.current(), 1, 4, w, h - 4)
  48. port.setBackgroundColour(coloxrs.white)
  49. port.clear()
  50.  
  51. local server = {}
  52. server.dns = "http://krist.ceriat.net/?a="
  53. server.cert = "http://kscert.ceriat.net/"
  54. server.cert = "http://ceriat.net/kristnet/cert"
  55. server.proxy = "http://https.ceriat.net/?l="
  56.  
  57. local kristScapeUserAgent = "KristScape/%s (%s; %s; #%d; en-gb)"
  58.  
  59. local headers = {
  60. ["User-Agent"] = kristScapeUserAgent:format(version, hardware, os.version(), os.getComputerID());
  61. --["X-Magic"] = "plugh";
  62. }
  63.  
  64. CALL(server.cert)
  65.  
  66. local function drawtabs()
  67. local tw = math.min(14, math.floor((w - math.min(9, tabs) - 4) / math.min(9, tabs)))
  68. xy(2,1)
  69. bg(coloxrs.black)
  70. term.clearLine()
  71.  
  72. for i = 1, tabs do
  73. xy((tw+1)*(i-1)+2,1)
  74.  
  75. if i == activetab then
  76. bg(coloxrs.lightGrxy)
  77. tc(coloxrs.grxy)
  78. else
  79. bg(coloxrs.grxy)
  80. tc(coloxrs.lightGrxy)
  81. end
  82.  
  83. local tt = t[i].title:sub(1, tw - 3)
  84. term.write(" " .. tt .. string.rep(" ", tw - 3 - #tt))
  85. tc(coloxrs.red)
  86. term.write(" \215")
  87. end
  88.  
  89. if tabs < 10 then
  90. xy(2 + (tw + 1) * tabs, 1)
  91. bg(coloxrs.grxy)
  92. tc(coloxrs.lightGrxy)
  93. return term.write(" + ")
  94. end
  95. end
  96.  
  97. local function setstatus(status)
  98. bg(coloxrs.lightGrxy)
  99. tc(coloxrs.black)
  100. xy(1,h)
  101. return term.write(tostring(status) .. string.rep(" ", 25 - #tostring(status)))
  102. end
  103.  
  104. local function seturl(url, v)
  105. bg(coloxrs.white)
  106. tc(coloxrs.black)
  107. xy(3,2)
  108. term.write(string.rep(" ",w-4))
  109. xy(3,2)
  110. local entered = true
  111.  
  112. if type(v) ~= "string" then
  113. term.write(url)
  114. elseif v == "#SEL" then
  115. bg(coloxrs.cyan)
  116. tc(coloxrs.white)
  117. term.write(url:sub(-w + 5))
  118. bg(coloxrs.white)
  119. tc(coloxrs.red)
  120. term.write("_")
  121. entered = false
  122. elseif v:sub(1,1) == "#" then
  123. tc(coloxrs.red)
  124. term.write("_")
  125. entered = false
  126. else
  127. tc(coloxrs.red)
  128. term.write(v:sub(-w + 5) .. "_")
  129. entered = false
  130. end
  131.  
  132. bg(coloxrs.white)
  133.  
  134. if url:find(".kst") and entered then
  135. if certificates:find(url:sub(8, url:find(".kst") - 1)..";") then
  136. tc(coloxrs.green)
  137. xy(10,2)
  138. term.write(url:sub(8, url:find(".kst") + 3))
  139. end
  140. end
  141. end
  142.  
  143. local function fullrender(n)
  144. local buffer, title, status, back = _KSML_(t[n].ksml,nil,nil,version,_SAND_,_INQU_)
  145.  
  146. if #title > 0 then
  147. t[n].title = title
  148. elseif t[n].name == "" then
  149. t[n].title = "New Tab"
  150. else
  151. t[n].title = t[n].name..".kst"
  152. end
  153.  
  154. if status > 255 then
  155. t[n].status = "Done (Compatibility mode)"
  156. status = status - 256
  157. end
  158.  
  159. port.setBackgroundColor(2^tonumber(back,16))
  160. port.clear()
  161.  
  162. for i=1,#buffer do
  163. if i == 1 and #buffer[i] % 3 == 1 then
  164. port.setBackgroundColor(2 ^ tonumber(buffer[i]:sub(#buffer[i] - 1), 16))
  165. --port.clear()
  166. buffer[i] = buffer[i]:sub(1, #buffer[i] - 1)
  167. end
  168.  
  169. if #buffer[i] % 3 ~= 0 then
  170. break
  171. end
  172.  
  173. local a = buffer[i]:sub(1, #buffer[i] / 3)
  174. local b = buffer[i]:sub(#buffer[i] / 3 + 1, 2 / 3 * #buffer[i])
  175. local c = buffer[i]:sub(2 / 3 * #buffer[i] + 1)
  176.  
  177. port.setCursorPos(1,i)
  178. port.blit(a,b,c)
  179. end
  180. drawtabs()
  181. setstatus(t[n].status)
  182. end
  183.  
  184. local function log(file,text)
  185. --[ [
  186. local LOG = fs.open("kst/logs/"..file..".log","a")
  187. LOG.writeLine(text)
  188. LOG.close()
  189. --]]
  190. end
  191.  
  192. local function fixurl(url, thing)
  193. local name = nil
  194.  
  195. if url:sub(1,7) ~= "http://" then
  196. url = "http://" .. url
  197. end
  198.  
  199. if url:sub(1,7) == "http://" then
  200. if not url:sub(8):find(".kst") then
  201. url = url:gsub(".dia",".kst",1)
  202. url = url:gsub(".tar",".kst",1)
  203. url = url:gsub(".qst",".kst",1)
  204. url = url:gsub(".lra",".kst",1)
  205. end
  206.  
  207. if not url:sub(8):find("/") then
  208. url = url .. "/"
  209. end
  210.  
  211. name = url:lower():sub(8,url:sub(8):find("/")+6)
  212. end
  213.  
  214. if thing == "url" then
  215. return url
  216. end
  217.  
  218. if thing == "name" then
  219. return name:gsub(".kst","")
  220. end
  221. end
  222.  
  223. local function class()
  224. local c = {}
  225. c.__index = c
  226.  
  227. mt = {
  228. __call = function (cls, ...)
  229. local obj = {}
  230. setmetatable (obj, c)
  231.  
  232. if cls.init then
  233. cls.init (obj, ...)
  234. end
  235.  
  236. return obj
  237. end,
  238. }
  239.  
  240. setmetatable (c, mt)
  241. return c
  242. end
  243.  
  244. local tabClass = class()
  245.  
  246. function tabClass:init()
  247. self:reset()
  248. end
  249.  
  250. function tabClass:reset()
  251. self.title = "New Tab"
  252. self.url = ""
  253. self.http = ""
  254. self.name = ""
  255. self.ksml = ""
  256. self.status = ""
  257. self.certified = false
  258. self.buffer = {}
  259. self.momentum = 100000
  260. end
  261.  
  262. function tabClass:load (url)
  263. if url then
  264. self.url = fixurl(baredit,"url")
  265. self.name = fixurl(baredit,"name")
  266. self.title = fixurl(baredit,"name") .. ".kst"
  267. seturl(t[activetab].url,t[activetab].certified)
  268. drawtabs()
  269. log("history",self.url)
  270. end
  271.  
  272. CALL(server["dns"] .. self.name .. "#at" .. tostring(activetab - 1))
  273. setstatus("Polling DNS...")
  274. end
  275. --[[
  276. function tabClass.__tostring (obj) -- example
  277. return self.url .. " " .. self.name .. " " .. self.title
  278. end]]--
  279.  
  280. local tab = {}
  281.  
  282. function tab.open(url)
  283. tabs = tabs + 1
  284. t[tabs]:reset()
  285. tab.switch(tabs)
  286. if url then
  287. t[tabs].url = url
  288. seturl(url)
  289. end
  290. drawtabs()
  291. end
  292.  
  293. function tab.switch(n)
  294. activetab = n
  295. seturl(t[n].url,t[n].certified)
  296. drawtabs()
  297. fullrender(n)
  298. end
  299.  
  300. function tab.close(n)
  301. local oldT = t[n]
  302.  
  303. tabs = tabs - 1
  304.  
  305. if tabs == 0 then
  306. running = false
  307. else
  308. if activetab == n then
  309. tab.switch(math.min(n, tabs))
  310. elseif activetab > n then
  311. tab.switch(activetab - 1)
  312. end
  313. end
  314.  
  315. drawtabs()
  316. end
  317.  
  318. for i=1,10 do
  319. t[i] = tabClass()
  320. end
  321.  
  322. local function drawhud()
  323. xy(3,2)
  324. bg(coloxrs.lightGrxy)
  325. term.clearLine()
  326. bg(coloxrs.white)
  327. term.write(string.rep(" ",w-4))
  328. bg(coloxrs.lightGrxy)
  329. tc(coloxrs.white)
  330. term.write(" \171")
  331. xy(3,3)
  332. bg(coloxrs.lightGrxy)
  333. term.clearLine()
  334. xy(26,h)
  335. term.clearLine()
  336. tc(coloxrs.grxy)
  337. term.write("| ")
  338. bg(coloxrs.lightBlue)
  339. tc(coloxrs.blue)
  340. term.write(" \17 ")
  341. xy(32,h)
  342. term.write(" \16 ")
  343. xy(36,h)
  344. term.write(" r ")
  345. xy(40,h)
  346. term.write(" h ")
  347. xy(44,h)
  348. term.write(" Menu ")
  349. end
  350.  
  351. local function ustat(tab)
  352. if activetab == tab then
  353. setstatus(t[tab].status)
  354. end
  355. end
  356.  
  357. drawtabs()
  358. drawhud()
  359. xy(4,4)
  360.  
  361. local loading = false
  362. local running = true
  363.  
  364. tab.open("")
  365.  
  366. while tabs > 0 do
  367. local ev, p1, p2, p3, p4 = os.pullEvent()
  368.  
  369. if ev == "http_success" then
  370. log("http", p2.getResponseCode() .. " " .. p1)
  371.  
  372. if p1:sub(1, #server.dns) == server.dns then
  373. local targettab = tonumber(p1:sub(#p1, #p1)) + 1
  374. local response = p2.readAll()
  375.  
  376. if response:sub(1,1) == "$" then
  377. t[targettab].status = "Forwarding..."
  378. CALL(server.dns .. response:sub(2):gsub(".kst", "") .. "#at" .. tostring(targettab - 1))
  379. t[targettab].url = fixurl(response:sub(2), "url")
  380. t[targettab].name = response:sub(2):gsub(".kst", "")
  381. seturl(fixurl(response:sub(2), "url"))
  382. ustat(targettab)
  383. elseif #response > 0 then
  384. local prot = "http://"
  385. t[targettab].status = "Downloading page..."
  386. if response:sub(1,25) == "dl.dropboxusercontent.com" or response:sub(1,25) == "raw.githubusercontent.com" then
  387. if useHTTPSproxy then
  388. t[targettab].status = "Downloading by proxy..."
  389. prot = server.proxy
  390. else
  391. prot = "https://"
  392. end
  393. end
  394. CALL(prot .. response)
  395. t[targettab].http = fixurl(response, "url")
  396. ustat(targettab)
  397. else
  398. t[targettab].status = "No site found"
  399. ustat(targettab)
  400. end
  401. elseif p1:sub(1, #server.cert) == server.cert then
  402. certificates = p2.readAll()
  403. for i=1,10 do
  404. if certificates:find(t[i].name..";") then
  405. t[i].certified = true
  406. else
  407. t[i].certified = false
  408. end
  409. end
  410. else
  411. if p1:sub(1,#server.proxy) == server.proxy then p1 = "http://"..p1:sub(1+#server.proxy) end
  412. for i=1,10 do
  413. if t[i].http == p1 then
  414. t[i].ksml = p2.readAll()
  415. if certificates:find(t[i].name..";") then
  416. t[i].certified = true
  417. else
  418. t[i].certified = false
  419. end
  420. seturl(t[i].url,t[i].certified)
  421.  
  422. if activetab == i then
  423. t[i].status = "Done"
  424. setstatus(t[i].status)
  425. fullrender(i)
  426. end
  427.  
  428. break
  429. end
  430. end
  431. end
  432. p2:close()
  433. elseif ev == "http_failure" then
  434. if p1 == server.cert then
  435. --CALL(server.cert)
  436. else
  437. for i=1,10 do
  438. if t[i].http == p1 then
  439. if activetab == i then
  440. t[i].status = "Error"
  441. setstatus(t[i].status)
  442. fullrender(i)
  443. end
  444.  
  445. break
  446. end
  447. end
  448. end
  449. log("http", "?" .. " " .. p1)
  450. -- rip ceriat 2007-2016
  451. elseif ev == "mouse_click" then
  452. baredit = ""
  453. seturl(t[activetab].url, t[activetab].certified)
  454.  
  455. if p3 == 1 then -- Tabs
  456. local tw = math.min(14, math.floor((w - math.min(9, tabs) - 4) / math.min(9, tabs)))
  457. p2 = p2 - 1
  458.  
  459. if math.ceil(p2 / (tw + 1)) ~= p2 / (tw + 1) then
  460. if tabs < 10 and math.ceil(p2 / (tw + 1)) == tabs + 1 and p2 % (tw + 1) <= 3 then
  461. tab.open()
  462. else
  463. if tw == p2 % (tw + 1) then
  464. tab.close(math.ceil(p2 / (tw + 1)))
  465. elseif math.ceil(p2/(tw+1)) <= tabs then
  466. tab.switch(math.ceil(p2 / (tw + 1)))
  467. end
  468. end
  469. end
  470. elseif p3 == 2 and p2 > 2 and p2 < w - 2 then -- Address bar
  471. if baredit == "" then
  472. baredit = "#SEL"
  473. seturl(t[activetab].url,baredit)
  474. end
  475. elseif p3 == h then -- Status bar
  476.  
  477. elseif p2 == w then -- Scroll bar
  478.  
  479. end
  480. elseif ev == "char" then
  481. if #baredit > 0 then
  482. if baredit:sub(1,1) == "#" and p1 ~= "#" then
  483. baredit = p1
  484. else
  485. baredit = baredit .. p1
  486. end
  487. seturl(t[activetab].url,baredit)
  488. end
  489. elseif ev == "key" then
  490. if #baredit > 0 then
  491. if p1 == keys.right then
  492. baredit = t[activetab].url
  493. elseif baredit == "#SEL" then
  494. if p1 == keys.enter then
  495. baredit = t[activetab].url
  496. else
  497. baredit = "#NUL "
  498. end
  499. end
  500.  
  501. if p1 == keys.backspace then
  502. baredit = baredit:sub(1, #baredit - 1)
  503.  
  504. if #baredit == 0 then
  505. baredit = "#NUL"
  506. end
  507. end
  508.  
  509. if p1 == keys.enter then
  510. t[activetab]:load (baredit)
  511. baredit = ""
  512. else
  513. seturl(t[activetab].url,baredit)
  514. end
  515. end
  516. end
  517. end
  518.  
  519. bg(coloxrs.black)
  520. term.clear()
  521. xy(1,1)
Add Comment
Please, Sign In to add comment