Advertisement
Guest User

sPhone

a guest
Jul 18th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.73 KB | None | 0 0
  1. local function kernel()
  2.     _G.sPhone = {
  3.         version = "Alpha 2.13.7",
  4.         user = "Guest",
  5.         devMode = false,
  6.         mainTerm = term.current()
  7.     }
  8.    
  9.     sPhone.theme = { --Default colors
  10.         ["header"] = colors.blue,
  11.         ["headerText"] = colors.white,
  12.         ["text"] = colors.black,
  13.         ["background"] = "",
  14.         ["backgroundColor"] = colors.white,
  15.         ["window.background"] = colors.lightBlue,
  16.         ["window.side"] = colors.blue,
  17.         ["window.button"] = colors.lightBlue,
  18.         ["window.text"] = colors.white,
  19.     }
  20.    
  21.     sPhone.defaultTheme = sPhone.theme
  22.    
  23.     if not fs.exists("/.sPhone/config/newIDSystem") then
  24.         fs.delete("/.sPhone/config/username")
  25.         fs.delete("/.sPhone/config/.sIDpw")
  26.         f = fs.open("/.sPhone/config/newIDSystem","w")
  27.         f.write("Ignore Me. I just check if you use the new Sertex ID system to fix password issues")
  28.         f.close()
  29.     end
  30.    
  31.     if not fs.exists("/.sPhone/config/newPassword") and fs.exists("/.sPhone/.password") then
  32.         fs.move("/.sPhone/.password","/.sPhone/config/.password")
  33.         f = fs.open("/.sPhone/config/newPassword","w")
  34.         f.write("Ignore Me. I just check if the password is moved to the config folder")
  35.         f.close()
  36.     end
  37.    
  38.     if not fs.exists("/.sPhone/apis") then
  39.         fs.makeDir("/.sPhone/apis")
  40.     end
  41.    
  42.     for k, v in pairs(fs.list("/.sPhone/apis")) do
  43.         if not fs.isDir("/.sPhone/apis/"..v) then
  44.             os.loadAPI("/.sPhone/apis/"..v)
  45.         end
  46.     end
  47.  
  48.     local sPath = shell.path()
  49.     sPath = sPath..":/bin"
  50.     shell.setPath(sPath)
  51.    
  52.     if not fs.exists("/.sPhone/autorun") then
  53.         fs.makeDir("/.sPhone/autorun")
  54.     end
  55.    
  56.     term.setBackgroundColor(colors.white)
  57.     term.clear()
  58.     term.setCursorPos(1,1)
  59.     for k, v in pairs(fs.list("/.sPhone/autorun")) do
  60.         term.setTextColor(colors.black)
  61.         if not fs.isDir("/.sPhone/autorun/"..v) then
  62.             if not safemode then
  63.                 local f = fs.open("/.sPhone/autorun/"..v,"r")
  64.                 local script = f.readAll()
  65.                 f.close()
  66.                 print("Loading script "..v)
  67.                 sleep(0)
  68.                 local ok, err = pcall(function() setfenv(loadstring(script),getfenv())() end)
  69.                 if not ok then
  70.                     term.setTextColor(colors.red)
  71.                     print("Script error: "..v..": "..err)
  72.                     fs.move("/.sPhone/autorun/"..v, "/.sPhone/autorun/disabled/"..v)
  73.                     term.setTextColor(colors.blue)
  74.                     print(v.." disabled to prevent errors")
  75.                     sleep(0.5)
  76.                 end
  77.             else
  78.                 print("Script "..v.." not loaded because Safe Mode")
  79.                 sleep(0)
  80.             end
  81.         end
  82.     end
  83.  
  84.     if safemode then
  85.         _G.safemode = nil
  86.     end
  87.    
  88.     if runningOnStartup then
  89.         fs.open("/startup","r")
  90.     end
  91.    
  92.     if fs.exists("/.sPhone/config/username") then
  93.         local u = fs.open("/.sPhone/config/username","r")
  94.         sPhone.user = u.readLine()
  95.         u.close()
  96.     end
  97.    
  98.     if not fs.exists("/.sPhone/config/sPhone") then
  99.         config.write("/.sPhone/config/sPhone","devMode",false)
  100.     end
  101.    
  102.     sPhone.devMode = config.read("/.sPhone/config/sPhone","devMode")
  103.    
  104.     if sPhone.devMode then
  105.         sPhone.crash = crash
  106.     end
  107.    
  108.     _G.crash = nil
  109.    
  110.     function os.version()
  111.         return "sPhone "..sPhone.version
  112.     end
  113.    
  114.     function sPhone.getSize()
  115.         return term.getSize()
  116.     end
  117.    
  118.     local fileTheme = "/.sPhone/config/theme"
  119.     if fs.exists(fileTheme) then
  120.         sPhone.theme["header"] = config.read(fileTheme, "header")
  121.         sPhone.theme["headerText"] = config.read(fileTheme, "headerText")
  122.         sPhone.theme["text"] = config.read(fileTheme, "text")
  123.         sPhone.theme["background"] = config.read(fileTheme, "background")
  124.         sPhone.theme["backgroundColor"] = config.read(fileTheme, "backgroundColor")
  125.         sPhone.theme["window.background"] = config.read(fileTheme, "window.background")
  126.         sPhone.theme["window.side"] = config.read(fileTheme, "window.side")
  127.         sPhone.theme["window.button"] = config.read(fileTheme, "window.button")
  128.         sPhone.theme["window.text"] = config.read(fileTheme, "window.text")
  129.     else
  130.         for k, v in pairs(sPhone.theme) do
  131.             config.write(fileTheme, k, v)
  132.         end
  133.     end
  134.    
  135.     function sPhone.applyTheme(id, value)
  136.         if not value or not id then
  137.             error("bad arguement: double expected, got nil",2)
  138.         end
  139.         sPhone.theme[id] = value
  140.         config.write(fileTheme, id, value)
  141.     end
  142.    
  143.     function sPhone.getTheme(id)
  144.         if not id then
  145.             error("bad arguement: double expected, got nil",2)
  146.         end
  147.         local n = config.read(fileTheme, id)
  148.         return n
  149.     end
  150.    
  151.     local function clear()
  152.         term.setBackgroundColor(sPhone.theme["backgroundColor"])
  153.         term.clear()
  154.         term.setCursorPos(1,1)
  155.         term.setTextColor(sPhone.theme["text"])
  156.     end
  157.    
  158.     sPhone.forceShutdown = os.shutdown
  159.     sPhone.forceReboot = os.reboot
  160.    
  161.     function os.shutdown()
  162.         sPhone.inHome = false
  163.         os.pullEvent = os.pullEventRaw
  164.         while true do
  165.             if sPhone.doneShutdown then
  166.                 clear()
  167.                 w, h = term.getSize()
  168.                 term.setCursorPos( (w/2)- 7, h/2)
  169.                 write("Shutdown Aborted.")
  170.                 sPhone.winOk("Error","Can not shutdown",colors.lightBlue,colors.red, colors.white, colors.lightBlue)
  171.                 return
  172.             end
  173.             sPhone.doneShutdown = true
  174.             clear()
  175.             w, h = term.getSize()
  176.             term.setCursorPos( (w / 2) - 1, h / 2)
  177.             write(" ")
  178.             for i = 1,3 do
  179.                 sleep(0.3)
  180.                 write(".")
  181.             end
  182.             sleep(0.2)
  183.             sPhone.forceShutdown()
  184.         end
  185.     end
  186.    
  187.     function os.reboot()
  188.         sPhone.inHome = false
  189.         os.pullEvent = os.pullEventRaw
  190.         while true do
  191.             if sPhone.doneShutdown then
  192.                 clear()
  193.                 w, h = term.getSize()
  194.                 term.setCursorPos( (w/2)- 7, h/2)
  195.                 write("Reboot Aborted.")
  196.                 sPhone.winOk("Error","Can not reboot",colors.lightBlue,colors.red, colors.white, colors.lightBlue)
  197.                 return
  198.             end
  199.             sPhone.doneShutdown = true
  200.             clear()
  201.             w, h = term.getSize()
  202.             term.setCursorPos( (w / 2) - 1, h / 2)
  203.             write(" ")
  204.             for i = 1,3 do
  205.                 sleep(0.3)
  206.                 write(".")
  207.             end
  208.             sleep(0.2)
  209.             sPhone.forceReboot()
  210.         end
  211.     end
  212.  
  213.   function sPhone.header(title, butt)
  214.        
  215.         if not title then
  216.             title = "sPhone"
  217.         end
  218.  
  219.         local w, h = term.getSize()
  220.         paintutils.drawLine(1,1,w,1, sPhone.theme["header"])
  221.         term.setTextColor(sPhone.theme["headerText"])
  222.         term.setCursorPos(1,1)
  223.         write(" "..title)
  224.         term.setCursorPos(w,1)
  225.         if butt then
  226.             write(butt)
  227.         end
  228.         term.setBackgroundColor(sPhone.theme["backgroundColor"])
  229.         term.setTextColor(sPhone.theme["text"])
  230.         term.setCursorPos(1,3)
  231.   end
  232.  
  233.   function sPhone.menu(items, title, closeButton)
  234.     local function cprint(text)
  235.       if type(text) ~= 'table' then
  236.         text = {text}
  237.       end
  238.    
  239.       local w, h = term.getSize()
  240.    
  241.       for i=1,#text do
  242.         local x, y = term.getCursorPos()
  243.         term.setCursorPos(math.floor(w/2)-math.floor(text[i]:len()/2), y)
  244.         print(text[i])
  245.       end
  246.     end
  247.     local function clear()
  248.       term.clear()
  249.       term.setCursorPos(1, 1)
  250.     end
  251.    
  252.     local termWidth, termHeight = term.getSize()
  253.     local drawSize = termHeight - 6
  254.    
  255.     local function maxPages()
  256.     local itemCount = #items
  257.     local pageCount = 0
  258.         while itemCount > 0 do
  259.             itemCount = itemCount - drawSize
  260.             pageCount = pageCount + 1
  261.         end
  262.         return pageCount
  263.     end
  264.    
  265.     local function iif(cond, trueval, falseval)
  266.         if cond then
  267.             return trueval
  268.         else
  269.             return falseval
  270.         end
  271.     end
  272.    
  273.     local function pagedItems()
  274.         local ret = {}
  275.         for i = 1, maxPages() do
  276.             local tmp = {}
  277.             local nElements = 0
  278.             for j = drawSize*(i-1)+1, iif(drawSize*(i+1) > #items, #items, drawSize*(i+1)) do
  279.                 if nElements < drawSize then
  280.                     table.insert(tmp, items[j])
  281.                     nElements = nElements + 1
  282.                 end
  283.             end
  284.             table.insert(ret, tmp)
  285.         end
  286.         return ret
  287.     end
  288.    
  289.     local selected = 1
  290.     if start then
  291.         selected = start
  292.     end
  293.     local page = 1
  294.    
  295.     local function redraw()
  296.         term.setBackgroundColor(sPhone.theme["backgroundColor"])
  297.         term.setTextColor(sPhone.theme["text"])
  298.         term.clear()
  299.     term.setCursorPos(1,1)
  300.         sPhone.header("",closeButton)
  301.         term.setCursorPos(1,3)
  302.         if not title then
  303.             title = "  sPhone"
  304.         end
  305.         cprint("  "..title)
  306.         if moreTitle then
  307.             head = moreTitle
  308.         else
  309.             head = {"\n",}
  310.             if not allowNil or allowNil == true then
  311.                 --head[3] = 'Terminate to cancel.'
  312.             end
  313.         end
  314.         for i=1,#head do
  315.             print(head[i])
  316.         end
  317.         if maxPages() > 1 then
  318.             pages = "<- (page "..page.." of "..maxPages()..") ->"
  319.             print(pages)
  320.         end
  321.         for i = 1, #pagedItems()[page] do
  322.             if selected == drawSize*(page-1)+i then
  323.                 term.setBackgroundColor(sPhone.theme["backgroundColor"])
  324.                 term.setTextColor(sPhone.theme["text"])
  325.             else
  326.                 term.setBackgroundColor(sPhone.theme["backgroundColor"])
  327.                 term.setTextColor(sPhone.theme["text"])
  328.             end
  329.             term.clearLine()
  330.             cprint(iif(selected == drawSize*(page-1)+i,"","").." "..pagedItems()[page][i])
  331.             term.setBackgroundColor(sPhone.theme["backgroundColor"])
  332.             term.setTextColor(sPhone.theme["text"])
  333.         end
  334.     end
  335.  
  336.     local function changePage(pW)
  337.         if pW == 1 and page < maxPages() then
  338.             page = page + 1
  339.             if selected + drawSize > #items then
  340.                 selected = #items
  341.             else
  342.                 selected = selected + drawSize
  343.             end
  344.         elseif pW == -1 and page > 1 then
  345.             page = page - 1
  346.             if selected - drawSize < 1 then
  347.                 selected = 1
  348.             else
  349.                 selected = selected - drawSize
  350.             end
  351.         end
  352.     end
  353.    
  354.     while true do
  355.         redraw()
  356.         local eventData = {os.pullEventRaw()}
  357.         if eventData[1] == 'mouse_click' then
  358.             if eventData[4] == 1 and eventData[3] == termWidth then
  359.                 return false, 0
  360.             elseif eventData[4] > 3 then
  361.                 clear()
  362.                 selected = (eventData[4]-6+((page-1)*drawSize))+1
  363.                 if selected then
  364.                     return items[selected], selected
  365.                 end
  366.             end
  367.         end
  368.     end
  369. end
  370.    
  371.     function sPhone.yesNo(title, desc, hideUser)
  372.         term.setCursorBlink(false)
  373.         term.setBackgroundColor(sPhone.theme["backgroundColor"])
  374.         term.clear()
  375.         term.setCursorPos(1,1)
  376.         term.setTextColor(sPhone.theme["text"])
  377.         local w, h = term.getSize()
  378.         paintutils.drawLine(1,1,w,1, sPhone.theme["header"])
  379.         term.setTextColor(sPhone.theme["headerText"])
  380.         term.setCursorPos(1,1)
  381.         if not hideUser then
  382.             if not sPhone.user then
  383.                 write(" sPhone")
  384.             else
  385.                 write(" "..sPhone.user)
  386.             end
  387.         end
  388.         term.setCursorPos(1,3)
  389.         term.setBackgroundColor(sPhone.theme["backgroundColor"])
  390.         term.setTextColor(sPhone.theme["text"])
  391.         visum.align("center", "  "..title, false, 3)
  392.         if desc then
  393.             visum.align("center", "  "..desc,false,6)
  394.         end
  395.         paintutils.drawFilledBox(3, 16, 9, 18, colors.green)
  396.         paintutils.drawFilledBox(18, 16, 24, 18, colors.red)
  397.         term.setTextColor(colors.white)
  398.         term.setCursorPos(5,17)
  399.         term.setBackgroundColor(colors.green)
  400.         write("Yes")
  401.         term.setCursorPos(20,17)
  402.         term.setBackgroundColor(colors.red)
  403.         write("No")
  404.         while true do
  405.             local _,_,x,y = os.pullEvent("mouse_click")
  406.             if (x > 2 and y > 15) and (x < 10 and y < 19) then
  407.                 return true
  408.             elseif (x > 17 and y > 15) and (x < 25 and y < 19) then
  409.                 return false
  410.             end
  411.         end
  412.     end
  413.    
  414.     function sPhone.winOk(fmessage, smessage, bg, side, text, button)
  415.         if not fmessage then
  416.             fmessage = ""
  417.         end
  418.         if not smessage then
  419.             smessage = ""
  420.         end
  421.         if not bg then
  422.             bg = sPhone.theme["window.background"]
  423.         end
  424.         if not text then
  425.             text = sPhone.theme["window.text"]
  426.         end
  427.         if not button then
  428.             button = sPhone.theme["window.button"]
  429.         end
  430.         if not side then
  431.             side = sPhone.theme["window.side"]
  432.         end
  433.         term.setCursorBlink(false)
  434.         if #fmessage >= #smessage then
  435.             local w, h = term.getSize
  436.             term.setBackgroundColor(side)
  437.             paintutils.drawBox(12 - math.ceil(#fmessage / 2), 5, 15 + math.ceil(#fmessage / 2), 10, side)
  438.             term.setBackgroundColor(bg)
  439.             paintutils.drawFilledBox(13 - math.ceil(#fmessage / 2), 6, 14 + math.ceil(#fmessage / 2), 9, bg)
  440.             term.setCursorPos(14 - math.ceil(#fmessage / 2), 7)
  441.             term.setTextColor(text)
  442.             write(fmessage)
  443.             term.setCursorPos(14 - math.ceil(#smessage / 2), 8)
  444.             write(smessage)
  445.         else
  446.             local w, h = term.getSize
  447.             term.setBackgroundColor(side)
  448.             paintutils.drawBox(12 - math.ceil(#smessage / 2), 5, 15 + math.ceil(#smessage / 2), 10, side)
  449.             term.setBackgroundColor(bg)
  450.             paintutils.drawFilledBox(13 - math.ceil(#smessage / 2), 6, 14 + math.ceil(#smessage / 2), 9, bg)
  451.             term.setCursorPos(14 - math.ceil(#fmessage / 2), 7)
  452.             term.setTextColor(text)
  453.             write(fmessage)
  454.             term.setCursorPos(14 - math.ceil(#smessage / 2), 8)
  455.             write(smessage)
  456.         end
  457.         term.setCursorPos(13,10)
  458.         term.setBackgroundColor(button)
  459.         write("Ok")
  460.         while true do
  461.             local e, k, x,y = os.pullEvent()
  462.             if e == "mouse_click" then
  463.                 if y == 10 then
  464.                     if x == 13 or x == 14 then
  465.                         return
  466.                     end
  467.                 end
  468.             elseif e == "key" then
  469.                 if k == 28 then
  470.                     return
  471.                 end
  472.             end
  473.         end
  474.     end
  475.    
  476.     function sPhone.colorPicker(message, old) -- From Impulse
  477.         local current = math.log(old) / math.log(2)
  478.         -- first line is already code wizardry
  479.         local function redraw()
  480.             term.setBackgroundColour(sPhone.theme["backgroundColor"])
  481.             term.clear()
  482.             sPhone.header(message)
  483.             term.setCursorPos(2,5)
  484.             term.setTextColor(colors.white)
  485.             term.setBackgroundColor(colors.lime)
  486.             write(" Ok ")
  487.             term.setCursorPos(7,5)
  488.             term.setTextColor(colors.white)
  489.             term.setBackgroundColor(colors.red)
  490.             write(" Cancel ")
  491.             term.setTextColor(colors.black)
  492.             term.setCursorPos(2, 3)
  493.             for i = 0, 15 do
  494.                 term.setBackgroundColour(2^i)
  495.                 term.write(i == current and "#" or ":")
  496.             end
  497.         end
  498.         while true do
  499.             redraw()
  500.             local ev = {os.pullEvent()}
  501.             if ev[1] == "key" and ev[2] == keys.enter then
  502.                 return 2^current
  503.             elseif ev[1] == "mouse_click" then
  504.                 if ev[4] == 3 and ev[3] >= 2 and ev[3] <= 17 then
  505.                     current = ev[3] - 2 % 16
  506.                 elseif ev[4] == 5 and ev[3] >= 2 and ev[3] <= 6 then
  507.                     return 2^current
  508.                 elseif ev[4] == 5 and ev[3] >= 7 and ev[3] <= 14 then
  509.                     return old
  510.                 end
  511.             end
  512.         end
  513.     end
  514.    
  515.     sPhone.colourPicker = sPhone.colorPicker -- For UK
  516.    
  517.     function sPhone.run(rApp, ...)
  518.         if not fs.exists(rApp) or fs.isDir(rApp) then
  519.             sPhone.winOk("App not found")
  520.             return false
  521.         end
  522.         if sPhone.inHome then
  523.             local sPhoneWasInHome = true
  524.             sPhone.inHome = false
  525.         end
  526.         os.pullEvent = os.oldPullEvent
  527.         local ok, err = pcall(function(...) setfenv(loadfile(rApp),getfenv())(...) end, ...)
  528.         if not ok then
  529.             os.pullEvent = os.pullEventRaw
  530.             term.setBackgroundColor(colors.white)
  531.             term.setTextColor(colors.black)
  532.             term.clear()
  533.             term.setCursorPos(1,2)
  534.             visum.align("center","  "..fs.getName(rApp).." crashed",false,2)
  535.             term.setCursorPos(1,4)
  536.             print(err)
  537.             print("")
  538.             visum.align("center","  Press Any Key")
  539.             os.pullEvent("key")
  540.         end
  541.         os.pullEvent = os.pullEventRaw
  542.         if sPhoneWasInHome then
  543.             sPhone.inHome = true
  544.         end
  545.     end
  546.    
  547.     local function lChat()
  548.         clear()
  549.         sPhone.header("RedNet Chat")
  550.         term.setBackgroundColor(sPhone.theme["backgroundColor"])
  551.         term.setTextColor(sPhone.theme["text"])
  552.         term.setCursorPos(2, 5)
  553.         if not peripheral.isPresent("back") or not peripheral.getType("back") == "modem" then
  554.             print("Modem not found")
  555.             print(" Press any key")
  556.             os.pullEvent("key")
  557.             return
  558.         end
  559.         write("Host: ")
  560.         local h = read()
  561.         term.setCursorPos(2,6)
  562.         shell.run("/rom/programs/rednet/chat", "join", h, sPhone.user)
  563.         sleep(1)
  564.     end
  565.    
  566.     local function installedApps()
  567.         sPhone.winOk("Work In","Progress")
  568.         local dir = "/.sPhone/apps/storeApps/"
  569.         if not fs.exists(dir) then
  570.             fs.makeDir(dir)
  571.         end
  572.        
  573.         local apps = {}
  574.         local appsName = {}
  575.        
  576.         for k, v in pairs(fs.list(dir)) do
  577.             if fs.isDir(dir..v) then
  578.                 if fs.exists(dir..v.."/sPhone-Main.lua") then
  579.                     local nDir = dir..v.."/sPhone-Main.lua"
  580.                    
  581.                     pDir = dir..v
  582.                     local run = config.read(nDir, "run")
  583.                     local name = config.read(nDir, "name")
  584.                     local author = config.read(nDir, "author")
  585.                     local version = config.read(nDir, "version")
  586.                    
  587.                     appsName[name] = run
  588.                 end
  589.             end
  590.         end
  591.        
  592.         for k, v in pairs(appsName) do
  593.             table.insert(apps, v)
  594.         end
  595.         local function drawHome()
  596.             term.setBackgroundColor(sPhone.theme["backgroundColor"])
  597.             term.clear()
  598.             term.setTextColor(sPhone.theme["text"])
  599.             sPhone.header("Apps","X")
  600.             term.setTextColor(sPhone.theme["backgroundColor"])
  601.             term.setBackgroundColor(sPhone.theme["text"])
  602.            
  603.             term.setCursorPos(1,3)
  604.             for k, v in pairs(appsName) do
  605.                 print(k)
  606.             end
  607.         end
  608.        
  609.         drawHome()
  610.        
  611.         local w, h = term.getSize()
  612.        
  613.         while true do
  614.             drawHome()
  615.             local _,_,x,y = os.pullEvent("mouse_click")
  616.             if x == w and y == 1 then
  617.                 break
  618.             elseif y >= 2 then
  619.                 if apps[y-2] then
  620.                     sPhone.run("/.sPhone/apps/storeApps/"..pDir.."/"..apps[y-2])
  621.                 end
  622.             end
  623.         end
  624.     end
  625.    
  626.     local function home()
  627.    
  628.         sPhone.inHome = true
  629.    
  630.         local buttonsInHome = {
  631.             {"sPhone.header",23,1,25,1,sPhone.theme["header"],sPhone.theme["headerText"],"vvv"},
  632.             {"sPhone.appsButton",12,20,14,20,sPhone.theme["backgroundColor"],sPhone.theme["header"],"==="},
  633.             {"sPhone.shell",2,3,8,5,colors.black,colors.yellow," Shell",2},
  634.             {"sPhone.lock",19,3,24,5,colors.lightGray,colors.black," Lock",2},
  635.             {"sPhone.chat",11,3,16,5,colors.black,colors.white," Chat",2},
  636.             {"sPhone.kst",2,7,8,9,colors.green,colors.white," Krist",2},
  637.             {"sPhone.gps",11,7,15,9,colors.red,colors.black," GPS",2},
  638.             {"sPhone.info",18,7,23,9,colors.lightGray,colors.black," Info",2},
  639.             {"sPhone.store",2,11,8,13,colors.orange,colors.white," Store",2},
  640.         }
  641.        
  642.        
  643.         local appsOnHome = {
  644.             ["sPhone.shell"] = "/.sPhone/apps/shell",
  645.             ["sPhone.sID"] = "/.sPhone/apps/system/sID",
  646.             ["sPhone.buddies"] = "/.sPhone/apps/buddies",
  647.             ["sPhone.SMS"] = "/.sPhone/apps/sms",
  648.             ["sPhone.gps"] = "/.sPhone/apps/gps",
  649.             ["sPhone.kst"] = "/.sPhone/apps/kstwallet",
  650.             ["sPhone.info"] = "/.sPhone/apps/system/info",
  651.             ["sPhone.store"] = "/.sPhone/apps/store",
  652.         }
  653.            
  654.             if not sPhone.locked then
  655.                 sPhone.lock()
  656.                 if sPhone.newUpdate then
  657.                     sPhone.winOk("New Update!")
  658.                 end
  659.             end
  660.             if fs.exists("/.sPhone/config/resetDBNews") then
  661.                 fs.delete("/.sPhone/config/resetDBNews")
  662.             end
  663.         local function drawHome()
  664.             local function box(x,y,text,bg,colorText,page)
  665.                 graphics.box(x,y,x+1+#text,y+2,bg)
  666.                 term.setCursorPos(x+1,y+1)
  667.                 term.setTextColor(colorText)
  668.                 write(text)
  669.             end
  670.             clear()
  671.            
  672.            
  673.             visum.buttons(buttonsInHome,true)
  674.            
  675.             local w, h = term.getSize()
  676.             paintutils.drawLine(1,1,w,1, sPhone.theme["header"])
  677.             term.setTextColor(sPhone.theme["headerText"])
  678.             visum.align("right","vvv ",false,1)
  679.         end
  680.         local function footerMenu()
  681.             sPhone.isFooterMenuOpen = true
  682.             function redraw()
  683.                 drawHome()
  684.                 local w, h = term.getSize()
  685.                 graphics.box(1,2,w,4,sPhone.theme["header"])
  686.                 term.setTextColor(sPhone.theme["headerText"])
  687.                 term.setBackgroundColor(sPhone.theme["header"])
  688.                 visum.align("right","^^^ ",false,1)
  689.                 visum.align("right", "Reboot ",false,3)
  690.                 term.setCursorPos(11,3)
  691.                 write("Settings")
  692.                 term.setCursorPos(2,3)
  693.                 write("Shutdown")
  694.             end
  695.             while true do
  696.                 term.redirect(sPhone.mainTerm)
  697.                 drawHome()
  698.                 redraw()
  699.                 local _,_,x,y = os.pullEvent("mouse_click")
  700.                 if y == 3 then
  701.                     if x > 1 and x < 10 then
  702.                         os.shutdown()
  703.                         sPhone.inHome = true
  704.                     elseif x > 19 and x < 26 then
  705.                         os.reboot()
  706.                         sPhone.inHome = true
  707.                     elseif x > 10 and x < 19 then
  708.                         sPhone.inHome = false
  709.                         sPhone.run("/.sPhone/apps/system/settings")
  710.                         sPhone.inHome = true
  711.                         drawHome()
  712.                     end
  713.                 elseif y == 1 then
  714.                     if x < 26 and x > 22 then
  715.                         sPhone.isFooterMenuOpen = false
  716.                         return
  717.                     end
  718.                 end
  719.             end
  720.         end
  721.         local function buttonHomeLoop()
  722.             while true do
  723.                 drawHome()
  724.                 term.setCursorBlink(false)
  725.                 local autoLockTimer = os.startTimer(10)
  726.                 local id = visum.buttons(buttonsInHome)
  727.                
  728.                 if id == "sPhone.header" then
  729.                     footerMenu()
  730.                 elseif id == "sPhone.appsButton" then
  731.                     sPhone.inHome = false
  732.                     installedApps()
  733.                     sPhone.inHome = true
  734.                 elseif id == "sPhone.lock" then
  735.                     sPhone.inHome = false
  736.                     login()
  737.                     sPhone.inHome = true
  738.                 elseif id == "sPhone.chat" then
  739.                     sPhone.inHome = false
  740.                     lChat()
  741.                     sPhone.inHome = true
  742.                 elseif appsOnHome[id] then
  743.                     sPhone.inHome = false
  744.                     sPhone.run(appsOnHome[id])
  745.                     sPhone.inHome = true
  746.                 end
  747.             end
  748.        
  749.             sPhone.inHome = false
  750.        
  751.         end
  752.        
  753.         local function updateClock()
  754.             while true do
  755.                 if sPhone.inHome then
  756.                     term.setCursorPos(1,1)
  757.                     term.setBackgroundColor(sPhone.theme["header"])
  758.                     term.setTextColor(sPhone.theme["headerText"])
  759.                     term.setCursorPos(1,1)
  760.                     write("      ")
  761.                     term.setCursorPos(1,1)
  762.                     write(" "..textutils.formatTime(os.time(),true))
  763.                 end
  764.                 sleep(0)
  765.             end
  766.         end
  767.        
  768.         parallel.waitForAll(buttonHomeLoop, updateClock)
  769.        
  770.         sPhone.inHome = false
  771.        
  772.     end
  773.    
  774.     function login()
  775.         sPhone.locked = true
  776.         if fs.exists("/.sPhone/config/.password") then
  777.             while true do
  778.                 term.setBackgroundColor(sPhone.theme["backgroundColor"])
  779.                 term.clear()
  780.                 term.setCursorPos(1,1)
  781.                 sPhone.header(sPhone.user)
  782.                 paintutils.drawBox(7,9,20,11,sPhone.theme["window.background"])
  783.                 if sPhone.wrongPassword then
  784.                     term.setTextColor(colors.red)
  785.                     term.setBackgroundColor(sPhone.theme["backgroundColor"])
  786.                     visum.align("center","  Wrong Password",false,13)
  787.                 end
  788.                 term.setTextColor(sPhone.theme["text"])
  789.                 term.setBackgroundColor(sPhone.theme["backgroundColor"])
  790.                 visum.align("center","  Insert Password",false,7)
  791.         local loginTerm = window.create(term.native(), 8,10,12,1, true)
  792.         term.redirect(loginTerm)
  793.         term.setBackgroundColor(sPhone.theme["backgroundColor"])
  794.         term.clear()
  795.         term.setCursorPos(1,1)
  796.         term.setTextColor(sPhone.theme["text"])
  797.                 local passwordLogin = read("*")
  798.         term.redirect(sPhone.mainTerm)
  799.                 local fpw = fs.open("/.sPhone/config/.password","r")
  800.                 if sha256.sha256(passwordLogin) == fpw.readLine() then
  801.                     sPhone.wrongPassword = false
  802.                     return
  803.                 else
  804.                     sPhone.wrongPassword = true
  805.                 end
  806.             end
  807.         else
  808.             local name
  809.             local pw
  810.             local pwr
  811.             local rServer
  812.             sPhone.firstBoot = true
  813.             while true do
  814.                 term.setBackgroundColor(sPhone.theme["backgroundColor"])
  815.                 term.clear()
  816.                 term.setCursorPos(1,1)
  817.                 sPhone.header("Setup")
  818.                 paintutils.drawBox(7,9,20,11,sPhone.theme["window.background"])
  819.                 if sPhone.wrongPassword then
  820.                     term.setTextColor(colors.red)
  821.                     visum.align("center","  Wrong Password",false,13)
  822.                 end
  823.                 term.setTextColor(sPhone.theme["text"])
  824.                 term.setBackgroundColor(sPhone.theme["backgroundColor"])
  825.                 visum.align("center","  Insert Password",false,7)
  826.         local loginTerm = window.create(term.native(), 8,10,12,1, true)
  827.         term.redirect(loginTerm)
  828.         term.setBackgroundColor(sPhone.theme["backgroundColor"])
  829.         term.clear()
  830.         term.setCursorPos(1,1)
  831.         term.setTextColor(sPhone.theme["text"])
  832.                 local password1 = read("*")
  833.                 term.redirect(sPhone.mainTerm)
  834.                 term.setBackgroundColor(sPhone.theme["backgroundColor"])
  835.                 term.clear()
  836.                 term.setCursorPos(1,1)
  837.                 sPhone.header("Setup")
  838.                 paintutils.drawBox(7,9,20,11,sPhone.theme["window.background"])
  839.                 term.setTextColor(sPhone.theme["text"])
  840.                 term.setBackgroundColor(sPhone.theme["backgroundColor"])
  841.                 visum.align("center","  Repeat",false,7)
  842.         local loginTerm = window.create(term.native(), 8,10,12,1, true)
  843.         term.redirect(loginTerm)
  844.         term.setBackgroundColor(sPhone.theme["backgroundColor"])
  845.         term.clear()
  846.         term.setCursorPos(1,1)
  847.         term.setTextColor(sPhone.theme["text"])
  848.                 local password2 = read("*")
  849.                 term.redirect(sPhone.mainTerm)
  850.                 if password1 == password2 then
  851.                     local f = fs.open("/.sPhone/config/.password", "w")
  852.                     f.write(sha256.sha256(password1))
  853.                     f.close()
  854.                     term.setTextColor(colors.lime)
  855.                     visum.align("center","  Password set!",false,13)
  856.                     sleep(2)
  857.                     break
  858.                 else
  859.                     sPhone.wrongPassword = true
  860.                 end
  861.             end
  862.            
  863.             local name
  864.            
  865.             if fs.exists("/.sPhone/config/username") then
  866.                 local f = fs.open("/.sPhone/config/username","r")
  867.                 name = f.readLine()
  868.                 f.close()
  869.             else
  870.                 name = "Guest"
  871.             end
  872.             term.setBackgroundColor(sPhone.theme["backgroundColor"])
  873.             term.clear()
  874.             sPhone.header("Setup")
  875.             term.setCursorPos(1,1)
  876.             term.setTextColor(sPhone.theme["text"])
  877.             sPhone.user = name
  878.             local toLabel = sPhone.user.."'s &9sPhone"
  879.             toLabel = toLabel:gsub("&", string.char(0xc2)..string.char(0xa7))
  880.             os.setComputerLabel(sPhone.user.."'s sPhone")
  881.             visum.align("center","  All Set!",false,3)
  882.             visum.align("center","  Have fun with sPhone",false,5)
  883.             sleep(2)
  884.             sPhone.locked = false
  885.             sPhone.inHome = true
  886.             sPhone.firstBoot = false
  887.             return
  888.         end
  889.     end
  890.  
  891.     sPhone.lock = login
  892.     sPhone.login = login
  893.  
  894.     local newVersion = http.get("https://raw.githubusercontent.com/BeaconNet/sPhone/master/src/version").readLine()
  895.    
  896.     if newVersion ~= sPhone.version then
  897.         sPhone.newUpdate = true
  898.     else
  899.         sPhone.newUpdate = false
  900.     end
  901.    
  902.     home()
  903.  
  904. end
  905. if not sPhone then
  906.     kernel(...)
  907. else
  908.     print("sPhone already started")
  909. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement