Advertisement
aodminecraft

Lanteacraft_Dialer (LC2) lieven121

Aug 2nd, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 85.65 KB | None | 0 0
  1. dirProg = "LC_Dialer/Program/"
  2.  
  3. term.clear()
  4. fr = fs.open(shell.getRunningProgram(),"r")
  5. while true do
  6.  Line = fr.readLine()
  7.  if Line == "--[[End Instalation]]--" then
  8.  break
  9.  else
  10.  end
  11. end
  12.  
  13. fw = fs.open("LC_Dialer/Program/Lanteacraft_Dialer","w")
  14. while true do
  15.  Line = fr.readLine()
  16.  if Line == "--[[This is the end off the program]]--" then
  17.   break
  18.  else
  19.  fw.writeLine(Line)
  20.  end
  21. end
  22. fw.close()
  23.  
  24. fw = fs.open("LC_Dialer/Program/Button","w")
  25. while true do
  26.  Line = fr.readLine()
  27.  if Line == "--[[This is the end off the API]]--" then
  28.   break
  29.  else
  30.  fw.writeLine(Line)
  31.  end
  32. end
  33. fw.close()
  34.  
  35. fw = fs.open("startup","w")
  36. fw.writeLine('shell.run("LC_Dialer/Program/Lanteacraft_Dialer")')
  37. fw.close()
  38.  
  39. os.reboot()
  40.  
  41. --[[End Instalation]]--
  42. dirProg = "LC_Dialer/Program/"
  43. dirAddress = "LC_Dialer/Address's/"
  44. dirData = "LC_Dialer/Data/"
  45.  
  46. --colors
  47. CGR = colors.gray
  48. CLGR = colors.lightGray
  49. CW = colors.white
  50. CR = colors.red
  51. CO = colors.orange
  52. CC= colors.cyan
  53. CG = colors.green
  54. CBl = colors.blue
  55. CLBl = colors.lightBlue
  56. CB = colors.black
  57. CL = colors.lime
  58. CY = colors.yellow
  59. CM = colors.magenta
  60. CP = colors.purple
  61. --colors
  62. --valid SG Glyphs
  63. validSGChar = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9","+","-"}
  64. --valid SG Glyphs
  65.  
  66. SG = {}
  67. function SG.splitAdress(address)
  68.  if sg.getStargateAddressString() == nil then
  69.   os.reboot()
  70.  end
  71.  if type(address) == "string" then
  72.   if string.len(address) == 7 or string.len(address) == 9 then
  73.    local AddressLength = string.len(address)  
  74.    local tblAdress = {}
  75.    for i = 1,AddressLength do
  76.     tblAdress[i] = string.reverse(string.sub(string.reverse(string.sub(address,i)),AddressLength-i+1))
  77.    end
  78.    return(tblAdress)
  79.    else error("Address is to short/long")
  80.   end
  81.   else error("Address has to be a string")
  82.  end
  83. end
  84.  
  85. function SG.getIrisType()
  86.  if sg.getStargateAddressString() == nil then
  87.   os.reboot()
  88.  end
  89.  --return(sg.getIrisType())
  90.  --return(nil)
  91.  return("Atlantis")
  92. end
  93.  
  94. function SG.getIrisState()
  95.  if sg.getStargateAddressString() == nil then
  96.   os.reboot()
  97.  end
  98.  --return(sg.getIrisState())
  99.  return("Closed")
  100. end
  101.  
  102. function SG.openIris()
  103.  if sg.getStargateAddressString() == nil then
  104.   os.reboot()
  105.  end
  106. end
  107.  
  108. function SG.closeIris()
  109.  if sg.getStargateAddressString() == nil then
  110.   os.reboot()
  111.  end
  112. end
  113.  
  114. function SG.selectGlyph(glpyh)
  115.  if sg.getStargateAddressString() == nil then
  116.   os.reboot()
  117.  end
  118.  sg.selectGlyph(glpyh)
  119.  sleep(0.4)
  120. end
  121.  
  122. function SG.activateChevron()
  123.  if sg.getStargateAddressString() == nil then
  124.   os.reboot()
  125.  end
  126.  sg.activateChevron()
  127.  sleep(1)
  128. end
  129.  
  130. function SG.deactivateLastChevron()
  131.  if sg.getStargateAddressString() == nil then
  132.   os.reboot()
  133.  end
  134.  sleep(0.4)
  135.  sg.deactivateChevron()
  136.  sleep(1)
  137. end
  138.  
  139. function SG.deactivateAllChevrons()
  140.  if sg.getStargateAddressString() == nil then
  141.   os.reboot()
  142.  end
  143.  SGbg.gateDialing = false
  144.  SGbg.Pause = true
  145.  sleep(0.4)
  146.  while sg.getActivatedChevrons() ~= 0 do
  147.   if sg.getStargateAddressString() == nil then
  148.    os.reboot()
  149.   end
  150.   sg.deactivateChevron()
  151.   sleep(1)
  152.  end
  153. end
  154.  
  155. function SG.deactivateAllWrongChevrons(address)
  156.  if sg.getStargateAddressString() == nil then
  157.   os.reboot()
  158.  end
  159.  if SG.getActivatedChevrons() ~= 0 then
  160.   while SG.getActivatedGlyphs() ~= string.reverse(string.sub(string.reverse(string.sub(SGbg.Address)),10-SG.getActivatedChevrons())) do
  161.    if SG.getActivatedChevrons() == 0 then
  162.     break
  163.    else
  164.     sg.deactivateChevron()
  165.     sleep(1)
  166.    end
  167.   end
  168.  end
  169. end
  170.  
  171. function SG.connect()
  172.  if sg.getStargateAddressString() == nil then
  173.   os.reboot()
  174.  end
  175.  sg.engageStargate()
  176. end
  177.  
  178. function SG.disconnect()
  179.  if sg.getStargateAddressString() == nil then
  180.   os.reboot()
  181.  end
  182.  sg.disengageStargate()
  183. end
  184.  
  185. function SG.dial(address,autoCon)
  186.  if sg.getStargateAddressString() == nil then
  187.   os.reboot()
  188.  end
  189.  SGbg.RestartDialing = true
  190.  SGbg.Address = address
  191.  SGbg.gateDialing = true
  192.  SGbg.ReadyToConnect = true
  193.  SGbg.gateRunning = true
  194.  SGbg.Pause = false
  195.  if type(autoCon) ~= "boolean" then
  196.   autoCon = true
  197.  end
  198.   SGbg.AutoConnect = autoCon
  199.  ansR = {true,address}
  200.  return ansR
  201. end
  202.  
  203. function SG.getActivatedChevrons()
  204.  if sg.getStargateAddressString() == nil then
  205.   os.reboot()
  206.  end
  207.  return(sg.getActivatedChevrons())
  208. end
  209.  
  210. function SG.getStargateAddressString()
  211.  if sg.getStargateAddressString() == nil then
  212.   os.reboot()
  213.  end
  214.  return(sg.getStargateAddressString())
  215. end
  216.  
  217. function SG.getActivatedGlyphs()
  218.  if sg.getStargateAddressString() == nil then
  219.   os.reboot()
  220.  end
  221.  return(sg.getActivatedGlyphs())
  222. end
  223.  
  224. SGbg = {}
  225. SGbg.gateDialing = false
  226. SGbg.RestartDialing = false
  227. SGbg.Pause = true
  228. SGbg.Address = nil
  229. SGbg.AutoConnect = false
  230.  
  231. function SG.gateProcces()
  232.  while true do
  233.   if type(SGbg.Address) == "string" then
  234.    if string.len(SGbg.Address) == 9 or string.len(SGbg.Address) == 7 then
  235.     SG.disconnect()
  236.     --SG.deactivateAllWrongChevrons(SGbg.Address)
  237.     SG.deactivateAllChevrons()
  238.     SGbg.gateDialing = true
  239.     SGbg.Pause = false
  240.     local Chevrons = SG.splitAdress(SGbg.Address)
  241.     AddressLength = string.len(SGbg.Address)
  242.     local Chevron = 0
  243.     local LastChevron = nil
  244.     while Chevron ~= AddressLength-1 and SGbg.gateDialing == true and SGbg.RestartDialing ~= true do
  245.      if SGbg.Pause == false then
  246.       Chevron = SG.getActivatedChevrons()
  247.       if Chevrons[Chevron+1] ~= LastChevron and SGbg.RestartDialing ~= true then
  248.        SG.selectGlyph(Chevrons[Chevron+1])
  249.        LastChevron = Chevrons[Chevron+1]
  250.       end
  251.       for delay = 1,4 do
  252.        if SGbg.RestartDialing == true then
  253.         break
  254.        end
  255.        sleep(0.1)
  256.       end
  257.       if SGbg.Pause == false then
  258.        if SGbg.RestartDialing ~= true then
  259.         SG.activateChevron()
  260.        end
  261.        for delay = 1,10 do
  262.         if SGbg.RestartDialing == true then
  263.          break
  264.         end
  265.         sleep(0.1)
  266.        end
  267.       end
  268.       if SGbg.RestartDialing ~= true then
  269.        --glymps
  270.        --while SG.getActivatedGlyphs() ~= string.reverse(string.sub(string.reverse(string.sub(SGbg.Address)),10-SG.getActivatedChevrons())) do
  271.        -- SG.deactivateLastChevron()
  272.        --end
  273.       end
  274.      else
  275.       sleep(0.2)
  276.      end
  277.     end
  278.     if SGbg.RestartDialing ~= true then
  279.      SGbg.Address = nil
  280.     end
  281.     sleep(0.4)
  282.    else sleep(0.4)
  283.    end
  284.   else sleep(0.4)
  285.   end
  286.   if SGbg.AutoConnect == true and SGbg.Pause == false and (SG.getActivatedChevrons() == 9 or SG.getActivatedChevrons() == 7) and SGbg.RestartDialing ~= true then
  287.    SG.connect()
  288.    SGbg.ReadyToConnect = false
  289.    SGbg.gateDialing = false
  290.    SGbg.Pause = true
  291.    SGbg.AutoConnect = false
  292.   end
  293.   SGbg.RestartDialing = false
  294.  end
  295. end
  296.  
  297.  
  298. function roundDown(num, idp)
  299.   local mult = 10^(idp or 0)
  300.   return math.floor(num * mult ) / mult
  301. end
  302.  
  303. function isValidAddress(address)
  304.  if type(address) == "string" then
  305.   if string.len(address) == 7 or string.len(address) == 9 then
  306.    address = string.upper(address)
  307.    Chevrons = SG.splitAdress(address)
  308.    for i = 1,string.len(address) do
  309.     local valid = false
  310.     for j = 1,38 do
  311.      if Chevrons[i] == validSGChar[j] then
  312.       valid = true
  313.      end
  314.     end
  315.     if valid == false then
  316.      return(false)
  317.     end
  318.    end
  319.    return(true)
  320.   else
  321.    return(false)
  322.   end
  323.  else
  324.   return(false)
  325.  end
  326. end
  327.  
  328.  
  329. function splitWord(word)
  330.  if type(word) == "string" then
  331.   local wordLength = string.len(word)  
  332.   local tblWord = {}
  333.   for i = 1,wordLength do
  334.    tblWord[i] = string.reverse(string.sub(string.reverse(string.sub(word,i)),wordLength-i+1))
  335.   end
  336.   return(tblWord)
  337.   else
  338.   --error("word has to be a string")
  339.  end
  340. end
  341.  
  342. function isValidAddressPart(addressPart)
  343.  if type(addressPart) == "number" then addressPart = tostring(addressPart) end
  344.  if type(addressPart) == "string" then
  345.   addressPart = string.upper(addressPart)
  346.   Chevrons = splitWord(addressPart)
  347.   for i = 1,string.len(addressPart) do
  348.    local valid = false
  349.    for j = 1,38 do
  350.     if Chevrons[i] == validSGChar[j] then
  351.      valid = true
  352.     end
  353.    end
  354.    if valid == false then
  355.     return(false)
  356.    end
  357.   end
  358.   return(true)
  359.  else
  360.   return(false)
  361.  end
  362. end
  363.  
  364. function Read(maxLength,word,BGcolorR,onlySG,mouseclick,paste,autoClose,displayChar,blinkOn)
  365.  ReadDone = false
  366.  xc = 0 yc = 0
  367.  if type(mouseButtons) ~= "boolean" then
  368.   mouseButtons = false
  369.  end
  370.  if type(blinkOn) ~= "boolean" then
  371.   blinkOn = true
  372.  end
  373.  BT.setBackgroundColor(BGcolorR)
  374.  if word == nil or word == "" then
  375.   word = ""
  376.   wordLength = 0
  377.   if blinkOn then
  378.    term.setCursorBlink(true)
  379.   end
  380.   xRead,yRead = term.getCursorPos()
  381.  else
  382.   wordLength = string.len(word)
  383.   if blinkOn then
  384.    term.setCursorBlink(true)
  385.   end
  386.   xRead,yRead = term.getCursorPos()
  387.   if displayChar == nil then
  388.    BT.write(word,xRead,yRead)
  389.   else
  390.    for i = 0,string.len(word) do
  391.     BT.write(displayChar,xRead+i,yRead,CB,BGcolorR)
  392.    end
  393.   end
  394.  end
  395.  xRead = xRead + (wordLength)
  396.  if onlySG == nil then
  397.   onlySG = false
  398.  end
  399.  if wordLength ~= maxLength and blinkOn ~= true then
  400.   BT.write("_",xRead,yRead)
  401.  end
  402.  BT.draw()
  403.  term.setCursorPos(xRead,yRead)
  404.  while true do
  405.   if blinkOn then
  406.    term.setCursorBlink(true)
  407.   end
  408.   event, key,xc,yc = os.pullEvent()
  409.   if type(xc) ~= "number" or type(yc) ~= "number" then
  410.    xc = 0 yc = 0
  411.   end  
  412.   if event == "terminate" then
  413.    quitProgram()
  414.   elseif paste and event == "paste" and wordLength ~= maxLength then
  415.    if key ~= nil then
  416.     if string.len(key) + wordLength <= maxLength then
  417.      if onlySG == true then
  418.       key = string.upper(key)
  419.      end
  420.      if onlySG ~= true or (onlySG and isValidAddressPart(key)) then
  421.       wordLength = wordLength + string.len(key)
  422.       word = word..key
  423.       if displayChar ~= nil then
  424.        for i = 0,string.len(key) do
  425.         BT.write(displayChar,xRead+i,yRead,CB,BGcolorR)
  426.        end
  427.       else
  428.        BT.write(key,xRead,yRead,CB,BGcolorR)
  429.       end
  430.       xRead = xRead + string.len(key)
  431.       if wordLength ~= maxLength and blinkOn ~= true then
  432.        BT.write("_",xRead,yRead)
  433.       end
  434.       BT.draw()
  435.      end
  436.     end
  437.    end
  438.   end
  439.   if event == "key" then
  440.    if onlySG == true then
  441.     if (key >= 16 and key <= 25) or (key >= 30 and key <= 38) or (key >= 44 and key <= 50) or (key >= 71 and key <= 82) or key == 28 or key == 14 then
  442.      keyOK = true
  443.     else keyOK = false
  444.     end
  445.    else keyOK = true
  446.    end
  447.   elseif event == "char" then
  448.    if onlySG == true then
  449.     if type(tonumber(key)) == "number" then
  450.      keyOK = true
  451.     else
  452.      --keyOK = false
  453.     end
  454.     else keyOK = true
  455.    end
  456.   end
  457.   if keyOK == true then
  458.    BT.setBackgroundColor(BGcolorR)
  459.    if event == "char" and (maxLength == nil or wordLength < maxLength) then
  460.     if onlySG == true then
  461.      key = string.upper(key)
  462.     end
  463.     word = word..key
  464.     wordLength = wordLength + 1
  465.     BT.write(displayChar or key,xRead,yRead,CB,BGcolorR)   
  466.     xRead = xRead + 1
  467.     if wordLength ~= maxLength and blinkOn ~= true then
  468.      BT.write("_",xRead,yRead)
  469.     end
  470.     if key == " " then
  471.      term.setCursorPos(xRead,yRead)
  472.     end
  473.     BT.draw()
  474.    elseif event == "key" then
  475.     if key == 14 and wordLength ~= 0 then --backspace
  476.      xRead = xRead - 1
  477.      term.setCursorPos(xRead,yRead)
  478.      BT.write(' ') --delete the last character
  479.      if wordLength ~= maxLength and blinkOn ~= true then
  480.       BT.write("_ ",xRead,yRead)
  481.      end
  482.      BT.draw()
  483.      term.setCursorPos(xRead,yRead) --move the cursor to that same spot
  484.      wordLength = wordLength - 1
  485.      word = word:sub(1,wordLength) --delete the last character of the real string
  486.     elseif key == 28 then --enter
  487.      term.setCursorBlink(false)
  488.      ReadDone = true
  489.      return word
  490.     end
  491.    elseif autoClose and maxLength ~= nil and wordLength == maxLength then
  492.     term.setCursorBlink(false)
  493.     return(word)
  494.    end
  495.   end
  496.   if event == "mouse_click" then
  497.    if mouseclick == true and maxLength ~= nil then
  498.     --xRead,yRead = term.getCursorPos()
  499.     if xc <= xRead-wordLength or xc >= xRead-wordLength+maxLength or yRead ~= yc then
  500.      term.setCursorBlink(false)
  501.      return word
  502.     end
  503.    end
  504.   end
  505.  end
  506. end
  507.  
  508. function quitProgram(yOffset)
  509.  --OldMenu = Menu
  510.  if Menu ~= "startup" and Menu ~= "RequestPass" then
  511.   if type(yOffset) ~= "number" then yOffset = 0 end
  512.   BT.deleteAllButtons()
  513.   if Settings.ExitingProgramReqPass and Settings.PasswordEnabled then
  514.    local ans = RequestPassword(-1,false,yOffset,true," Quit Program")
  515.    if ans == true or ans == "No Pass" then
  516.     ENDPROGRAM = true
  517.    end
  518.   else
  519.    ENDPROGRAM = true
  520.   end
  521.  end
  522.  --Menu = OldMenu
  523. end
  524.  
  525. function RequestPassword(MaxAttempt,autoShutdown,yOfset,backAllowed,msg)
  526.  OldMenu = Menu
  527.  Menu = "RequestPass"
  528.  if type(msg) ~= "string" then msg = "" end
  529.  if Settings.PasswordEnabled ~= true then return(true) end
  530.  if type(backAllowed) ~= "boolean" then backAllowed = false end
  531.  if type(yOfset) ~= "number" then yOfset = 0 end
  532.  if type(MaxAttempt) ~= "number" or MaxAttempt == 0 then MaxAttempt = -1 end
  533.  local attempt = 0
  534.  local EnterdPass = nil
  535.  local CorrectPass = nil
  536.  if fs.exists(dirData.."Settings/Password") then
  537.   f = fs.open(dirData.."Settings/Password","r")
  538.   CorrectPass = f.readLine()
  539.   f.close()
  540.   if CorrectPass == nil or CorrectPass == "" then return("No Pass") end
  541.  else
  542.   Menu = OldMenu
  543.   return("No Pass")
  544.  end
  545.  BT.deleteAllButtons()
  546.  back = false
  547.  while attempt ~= MaxAttempt do
  548.   BT.setF(11,6+yOfset,41,8+yOfset,CGR,CB)
  549.   BT.setF(13,8+yOfset,34,8+yOfset,CLGR,CB)
  550.   if backAllowed then
  551.    BT.setF(11,9+yOfset,41,11+yOfset,CGR,CB)
  552.    BT.createButton("back","Back",goBack,"" ,13,10+yOfset,39,10+yOfset,CR,CR,CB)
  553.   end
  554.   if MaxAttempt > 1 then
  555.    BT.write(MaxAttempt-attempt.."/MaxAttempt",36,7+yOfset,CB,CGR)
  556.   end
  557.   BT.write(msg,51/2-string.len(msg)/2+1,6+yOfset,CB,CGR)
  558.   BT.write("Password: ",13,8+yOfset,CB,CGR)
  559.   BT.drawButtons()
  560.   term.setBackgroundColor(CLGR)
  561.   term.setCursorPos(23,8+yOfset)
  562.   EnterdPass = Read(12,"",CLGR,false,true,Settings.PastePasswordAllowed,false,"*",false,true)
  563.   if EnterdPass == CorrectPass then
  564.    Menu = OldMenu
  565.    return(true)
  566.   end
  567.   if EnterdPass ~= nil and EnterdPass ~= "" and EnterdPass ~= " " then
  568.    attempt = attempt + 1
  569.   elseif ((xc <= 11 or xc >= 41 or yc <= 6+yOfset or yc >= 11+yOfset) or (xc >= 13 and xc <= 39 and yc == 10+yOfset)) and event == "mouse_click" and backAllowed then
  570.    Menu = OldMenu  
  571.    return("back")
  572.   end
  573.  end
  574.  if autoShutdown and done ~= true then
  575.   os.shutdown()
  576.  end
  577.  Menu = OldMenu
  578.  return(false)
  579. end
  580.  
  581. function setSettings()
  582.  dirSettings = dirData.."Settings/"
  583.  for NameSlider,Value in pairs(Settings) do
  584.   --oldValue = nil
  585.   if type(Value) == "boolean" then
  586.    f = fs.open(dirSettings..NameSlider,"r")
  587.    oldValue = f.readLine()
  588.    f.close()
  589.    if oldValue == "false" then
  590.    oldValue = false
  591.    elseif oldValue == "true" then
  592.     oldValue = true
  593.    end
  594.    if oldValue ~= Value then
  595.     f = fs.open(dirSettings..NameSlider,"w")
  596.     f.writeLine(Value)
  597.     f.close()
  598.    end
  599.   end
  600.  end
  601. end
  602.  
  603.  EventsBC = os.pullEvent
  604. function getSettings()
  605.  Settings = {}
  606.  os.pullEvent = os.pullEventRaw
  607.  dirSettings = dirData.."Settings/"
  608.  if fs.exists(dirSettings.."PasswordEnabled") then
  609.   f = fs.open(dirSettings.."PasswordEnabled","r")
  610.   Settings.PasswordEnabled = f.readLine()
  611.   f.close()
  612.  else
  613.   Settings.PasswordEnabled = false
  614.   f = fs.open(dirSettings.."PasswordEnabled","w")
  615.   f.writeLine(false)
  616.   f.close()
  617.  end
  618.  if fs.exists(dirSettings.."StartupReqPass") then
  619.   f = fs.open(dirSettings.."StartupReqPass","r")
  620.   Settings.StartupReqPass = f.readLine()
  621.   f.close()
  622.  else
  623.   Settings.StartupReqPass = false
  624.   f = fs.open(dirSettings.."StartupReqPass","w")
  625.   f.writeLine(false)
  626.   f.close()
  627.  end
  628.  if fs.exists(dirSettings.."ChangingSettingsReqPass") then
  629.   f = fs.open(dirSettings.."ChangingSettingsReqPass","r")
  630.   Settings.ChangingSettingsReqPass = f.readLine()
  631.   f.close()
  632.  else
  633.   Settings.ChangingSettingsReqPass = false
  634.   f = fs.open(dirSettings.."ChangingSettingsReqPass","w")
  635.   f.writeLine(false)
  636.   f.close()
  637.  end
  638.  if fs.exists(dirSettings.."ExitingProgramReqPass") then
  639.   f = fs.open(dirSettings.."ExitingProgramReqPass","r")
  640.   Settings.ExitingProgramReqPass = f.readLine()
  641.   f.close()
  642.  else
  643.   Settings.ExitingProgramReqPass = false
  644.   f = fs.open(dirSettings.."ExitingProgramReqPass","w")
  645.   f.writeLine(false)
  646.   f.close()
  647.  end
  648.  if fs.exists(dirSettings.."OpeningIrisReqPas") then
  649.   f = fs.open(dirSettings.."OpeningIrisReqPas","r")
  650.   Settings.OpeningIrisReqPas = f.readLine()
  651.   f.close()
  652.  else
  653.   Settings.OpeningIrisReqPas = false
  654.   f = fs.open(dirSettings.."OpeningIrisReqPas","w")
  655.   f.writeLine(false)
  656.   f.close()
  657.  end
  658.  if fs.exists(dirSettings.."PastePasswordAllowed") then
  659.   f = fs.open(dirSettings.."PastePasswordAllowed","r")
  660.   Settings.PastePasswordAllowed = f.readLine()
  661.   f.close()
  662.  else
  663.   Settings.PastePasswordAllowed = false
  664.   f = fs.open(dirSettings.."PastePasswordAllowed","w")
  665.   f.writeLine(false)
  666.   f.close()
  667.  end
  668.  if fs.exists(dirSettings.."DialingReqPass") then
  669.   f = fs.open(dirSettings.."DialingReqPass","r")
  670.   Settings.DialingReqPass = f.readLine()
  671.   f.close()
  672.  else
  673.   Settings.DialingReqPass = false
  674.   f = fs.open(dirSettings.."DialingReqPass","w")
  675.   f.writeLine(false)
  676.   f.close()
  677.  end
  678.  if fs.exists(dirSettings.."EditAddressReqPass") then
  679.   f = fs.open(dirSettings.."EditAddressReqPass","r")
  680.   Settings.EditAddressReqPass = f.readLine()
  681.   f.close()
  682.  else
  683.   Settings.EditAddressReqPass = false
  684.   f = fs.open(dirSettings.."EditAddressReqPass","w")
  685.   f.writeLine(false)
  686.   f.close()
  687.  end
  688.  if fs.exists(dirSettings.."AddAddressReqPass") then
  689.   f = fs.open(dirSettings.."AddAddressReqPass","r")
  690.   Settings.AddAddressReqPass = f.readLine()
  691.   f.close()
  692.  else
  693.   Settings.AddAddressReqPass = false
  694.   f = fs.open(dirSettings.."AddAddressReqPass","w")
  695.   f.writeLine(false)
  696.   f.close()
  697.  end
  698.  if fs.exists(dirSettings.."DelAddressReqPass") then
  699.   f = fs.open(dirSettings.."DelAddressReqPass","r")
  700.   Settings.DelAddressReqPass = f.readLine()
  701.   f.close()
  702.  else
  703.   Settings.DelAddressReqPass = false
  704.   f = fs.open(dirSettings.."DelAddressReqPass","w")
  705.   f.writeLine(false)
  706.   f.close()
  707.  end
  708.   if fs.exists(dirSettings.."AutoFindGate") then
  709.   f = fs.open(dirSettings.."AutoFindGate","r")
  710.   Settings.AutoFindGate = f.readLine()
  711.   f.close()
  712.  else
  713.   Settings.AutoFindGate = false
  714.   f = fs.open(dirSettings.."AutoFindGate","w")
  715.   f.writeLine(false)
  716.   f.close()
  717.  end
  718.  --control
  719.  if fs.exists(dirSettings.."KeyControl") then
  720.   f = fs.open(dirSettings.."KeyControl","r")
  721.   Settings.KeyControl = f.readLine()
  722.   f.close()
  723.  else
  724.   Settings.KeyControl = true
  725.   f = fs.open(dirSettings.."KeyControl","w")
  726.   f.writeLine(true)
  727.   f.close()
  728.  end
  729.  if fs.exists(dirSettings.."ScrollControl") then
  730.   f = fs.open(dirSettings.."ScrollControl","r")
  731.   Settings.ScrollControl = f.readLine()
  732.   f.close()
  733.  else
  734.   Settings.ScrollControl = true
  735.   f = fs.open(dirSettings.."ScrollControl","w")
  736.   f.writeLine(true)
  737.   f.close()
  738.  end
  739.  if fs.exists(dirSettings.."RemoteControl") then
  740.   f = fs.open(dirSettings.."RemoteControl","r")
  741.   Settings.RemoteControl = f.readLine()
  742.   f.close()
  743.  else
  744.   Settings.RemoteControl = true
  745.   f = fs.open(dirSettings.."RemoteControl","w")
  746.   f.writeLine(true)
  747.   f.close()
  748.  end
  749.  --making boolean
  750.  for CommandName,CommandSetting in pairs(Settings) do
  751.   if CommandSetting == "false" then
  752.    Settings[CommandName] = false
  753.   elseif CommandSetting == "true" then
  754.    Settings[CommandName] = true
  755.   end
  756.  end
  757.  os.unloadAPI(dirProg.."Button")
  758.  BT = {}
  759.  os.loadAPI(dirProg.."Button")
  760.  BT = Button
  761. end
  762.  
  763.  
  764. function setColors()
  765.  --needs to be moved
  766. --  CMidBTA = CC
  767. --  CMidBTNA = CBL
  768. --  CMidBTA = CL
  769. --  CMidBTNA = CG
  770. --  CMidBTA = CM
  771. --  CMidBTNA = CP
  772. --  CMidBTA = CO
  773. --  CMidBTNA = CR
  774.   COL = CGR
  775.   CMidBTA = nil
  776.   CMidBTNA = nil
  777.   COtherBT = nil
  778.   CBG = nil
  779.  
  780.  if fs.exists(dirData.."Settings/Colors") then
  781.   loadfile(dirData.."Settings/Colors")()
  782.  else
  783.   CMidBTA = CGR
  784.   CMidBTNA = CLGR
  785.   COtherBT = CLGR
  786.   CBG = CW
  787.   f = fs.open(dirData.."Settings/Colors","w")
  788.   f.writeLine("--Dail Button (active,inactive) default = gray/light gray = 128/256 ,red/orange = 16384/2, light blue/blue = 512/2048, magenta/purple = 4/1024, lime/green = 32/8192")
  789.   f.writeLine("CMidBTA = "..CGR)
  790.   f.writeLine("CMidBTNA = "..CLGR)
  791.   f.writeLine("--BG")
  792.   f.writeLine("CBG = "..CW)
  793.   f.writeLine("--Other Buttons")
  794.   f.writeLine("COtherBT = "..CLGR)
  795.   f.close()
  796.  end
  797. end
  798.  
  799.  
  800.  
  801. function Boot(xmid,ymid,cycle,stat,redraw)
  802.   --boot Menu
  803.  if redraw == nil or redraw == true then
  804.   BT.setF(1,1,51,1,CGR)
  805.   BT.setF(51,1,51,19,CGR)
  806.   BT.setF(1,1,1,19,CGR)
  807.   BT.setF(1,19,51,19,CGR)
  808.   BT.setF(2,2,50,18,CBG)
  809.   BT.draw()
  810.  end
  811.  BT.setBackgroundColor(CBG)
  812.  BT.setTextColor(CGR)
  813.  if stat ~= nil then
  814.   BT.setF(2,ymid+5,50,ymid+5,CBG)
  815.   BT.write(stat,xmid-(string.len(stat)/2-1),ymid+5,CB,CBG)
  816.  end
  817.  
  818.   local char = {"#"," "}
  819.   local xmid = 26
  820.   local ymid = 13
  821.   local xCo = {0,1,2,3,3, 3, 2, 1, 0,-1,-2,-3,-3,-3,-2,-1}
  822.   local yCo = {2,2,2,1,0,-1,-2,-2,-2,-2,-2,-1, 0, 1, 2, 2}
  823.   BT.write("Booting",xmid-3,ymid-4,CB)
  824.   for i = 1,cycle do
  825.    for ch = 1,2 do
  826.     for c = 1,16 do
  827.      BT.write(char[ch],xmid+xCo[c],ymid-yCo[c],CB,CBG)
  828.      BT.draw()
  829.      sleep(0.04)
  830.     end
  831.    end
  832.   end
  833. end
  834.  
  835. function Startup()
  836.  Menu = "startup"
  837.  getSettings()
  838.  setColors()
  839.  Boot(26,13,1,"initializing",true)
  840.  if Settings.StartupReqPass and Settings.PasswordEnabled then
  841.   BT.setF(2,2,50,18,CGR)
  842.   RequestPassword(-1,false,5,nil,"Startup")
  843.  end
  844. end
  845.  
  846. Startup()
  847.  
  848. if os.getComputerLabel() == nil then
  849.  os.setComputerLabel("LC_Dialer/Address")
  850. end
  851.  
  852.  
  853. function SetPeripheral()
  854.  Boot(26,13,1,"initializing",true)
  855.  perps = false
  856.  mon = {}
  857.  mon["StargatePicture"] = {}
  858.  while perps ~= true do
  859.  
  860.  -- if fs.exists(dirData.."monitors/")
  861.  -- then
  862.  --  MonitorFile = {}
  863.  --  f = fs.open(dirData.."Monitor","r")
  864.  --  Line = 1
  865.  --  MonitorFile[Line] = f.readLine()
  866.  --  Line = Line + 1
  867.  --  while MonitorFile[Line] ~= nil
  868.  --  MonitorFile[Line] = f.readLine()
  869.  --  Line = Line + 1
  870.  --  f.close()
  871.  -- else
  872.  
  873.  
  874.   monitor = {}
  875.   monitor[1] = "monitor_2"
  876.   monitor[2] = "monitor_3"
  877.  
  878.   for i = 1,#monitor do
  879.    mon["StargatePicture"][i] = peripheral.wrap(monitor[i])
  880.   end
  881.  
  882.   dispMonitor = "monitor_4"
  883.   mon["DispMsg"] = peripheral.wrap(dispMonitor)
  884.  
  885.   Boot(26,13,1,"loading peripherals",redraw)
  886.  
  887.  
  888.  
  889.   --if  (mon3 ~= true or mon["DispMsg"] ~= nil) then
  890.    perps = true
  891.    if mon["DispMsg"] ~= nil then
  892.     mon["DispMsg"].setTextScale(2)
  893.     mon["DispMsg"].setBackgroundColor(CBG)
  894.     mon["DispMsg"].clear()
  895.    else
  896.     --change peripherals
  897.    end
  898.   --end
  899.  end
  900. end
  901.  
  902.  
  903.  
  904.  
  905. function Peripherals()
  906.  CCON = CGR
  907.  function Continue()
  908.   if CCON == CG then
  909.    done2 = true  
  910.   end
  911.  end
  912.  Menu = "Peripherals"
  913.  if fs.exists(dirData.."Peripherals_Stargate") then
  914.   f = fs.open(dirData.."Peripherals_Stargate","r")
  915.   stargate = f.readLine()
  916.   f.close()
  917.  else
  918.   stargate = ""
  919.  end
  920.  if stargate == nil then
  921.   stargate = ""
  922.  end
  923.  done2 = false
  924.  while done2 ~=  true do
  925.   BT.deleteAllButtons()
  926.   BT.setF(1,1,51,19,COL)
  927.   BT.setF(2,2,50,17,CBG)
  928.   BT.createButton("Continue","Continue ",Continue,"" ,3,16,49,16,CCON,CCON,CB)
  929.   BT.CreateSildeBar(3,3,"AutoFindGate","Automatically find a gate",Settings.AutoFindGate)
  930.   if Settings.AutoFindGate ~= true then
  931.    BT.setF(3,5,30,5,CLGR)
  932.    BT.write("StargateBase: ",3,5,CB,CW)
  933.    BT.write(stargate,18,5,CB,CLGR)
  934.    if peripheral.getType(stargate) == "StargateBase" then
  935.     BT.setF(37,5,36,5,CG)
  936.    else
  937.     BT.setF(37,5,36,5,CR)
  938.    end
  939.   end
  940.   ev = "timer"
  941.   BT.drawButtons()
  942.   while ev == "timer" and done2 ~= true do
  943.    ev,bt,x,y = os.pullEvent()
  944.    if Settings.AutoFindGate == true then
  945.     local tmpsg = nil
  946.     tmpsg = peripheral.find("StargateBase")
  947.     CCON = CGR
  948.     if tmpsg ~= nil then
  949.      sg = {}
  950.      sg = tmpsg
  951.      CCON = CG
  952.      done2 = true
  953.     else
  954.      BT.drawButtons()
  955.      CCON = CGR
  956.     end
  957.    end
  958.    if ev == "terminate" then
  959.     quitProgram()
  960.    elseif ev == "mouse_click" then
  961.     BT.checkxy(x,y)
  962.     BT.checkSlideBar(x,y)
  963.     Settings.AutoFindGate = BT.getSliderValue("AutoFindGate")
  964.     setSettings()
  965.     if (x >= 18 and x <= 35 and y == 5) and Settings.AutoFindGate ~= true then
  966.      BT.write("StargateBase: ",3,5,CB,CW)
  967.      BT.drawButtons()
  968.      term.setBackgroundColor(CLGR)
  969.      term.setCursorPos(18,5)
  970.      stargate = Read(18,stargate,CLGR,false,true,Settings.PastePasswordAllowed,false,nil,true)
  971.      CCON = CGR
  972.      if peripheral.getType(stargate) == "StargateBase" then
  973.       sg = {}
  974.       sg = peripheral.wrap(stargate)
  975.       f = fs.open(dirData.."Peripherals_Stargate","w")
  976.       f.writeLine(stargate)
  977.       f.close()
  978.       CCON = CG
  979.      end
  980.     elseif Settings.AutoFindGate == true then
  981.      local tmpsg = nil
  982.      tmpsg = peripheral.find("StargateBase")
  983.      CCON = CGR
  984.      if tmpsg ~= nil then
  985.       sg = {}
  986.       sg = tmpsg
  987.       CCON = CG
  988.      else
  989.       BT.drawButtons()
  990.       CCON = CGR
  991.      end
  992.     else
  993.      BT.drawButtons()
  994.     end
  995.    end
  996.   end
  997.  end
  998. end
  999.  
  1000. function CheckStargate()
  1001.  OldMenu = Menu
  1002.  if Settings.AutoFindGate ~= true then  
  1003.   if fs.exists(dirData.."Peripherals_Stargate") then
  1004.    f = fs.open(dirData.."Peripherals_Stargate","r")
  1005.    stargate = f.readLine()
  1006.    f.close()
  1007.    if peripheral.getType(stargate) == "StargateBase" then
  1008.     sg = peripheral.wrap(stargate)
  1009.    else
  1010.     Peripherals()
  1011.    end
  1012.   else
  1013.    Peripherals()
  1014.   end
  1015.  elseif Settings.AutoFindGate == true then
  1016.   local tmpsg = nil
  1017.   tmpsg = peripheral.find("StargateBase")
  1018.   if tmpsg ~= nil then
  1019.    sg = tmpsg
  1020.   else
  1021.    Peripherals()
  1022.   end  
  1023.  end
  1024. end
  1025.  
  1026. CheckStargate()
  1027.  
  1028. --SetPeripheral()
  1029.  
  1030. function DispMsg(msg,clr)
  1031.  msgcheck = msg
  1032.  BT.setBackgroundColor(CBG)
  1033.  BT.setTextColor(clr)
  1034.  if msg ~= newMSG then
  1035.   BT.write("                               ",11,7,clr,CBG)
  1036.  end
  1037.  leng = string.len(msg)
  1038.  BT.write(msg,51/2-leng/2+1,7,clr,CBG)
  1039.  --if mon["DispMsg"] ~= nil then
  1040.  -- mon["DispMsg"].setBackgroundColor(CBG)
  1041.  -- mon["DispMsg"].setTextColor(clr)
  1042.  -- if msg ~= newMSG then
  1043.   -- mon["DispMsg"].setCursorPos(1,2)
  1044.  --  mon["DispMsg"].write("                                    ")
  1045.  -- end
  1046.  -- mon["DispMsg"].setCursorPos(36/2-leng/2+1,2)
  1047.  -- mon["DispMsg"].write(msg)
  1048.  --end
  1049.  newMSG = msg
  1050.  BT.draw()
  1051. end
  1052.  
  1053. function tmpBT()
  1054. end
  1055.  
  1056. function MainM()
  1057.  Menu = "Main"
  1058.  setColors()
  1059.  BT.deleteAllButtons()
  1060.  BT.setF(1,1,51,1,COL)
  1061.  BT.setF(1,1,1,19,COL)
  1062.  BT.setF(51,1,51,19,COL)
  1063.  BT.setF(1,6,51,8,COL)
  1064.  BT.setF(1,18,51,19,COL)
  1065.  BT.setF(2,2,50,5,CBG)
  1066.  BT.setF(11,7,41,7,CBG)
  1067.  BT.setF(2,9,50,17,CBG)
  1068.  BT.deleteAllButtons()
  1069.  BT.createButton("lockdown","Lockdown",lockdown,"" ,2,7,9,7,CR,CR,CB)
  1070.  BT.createButton("Settings","Settings",settings.main,"" ,43,7,50,7,CLBl,CBl,CB)
  1071.  --[[middle button]]--
  1072.  BT.createButton("middleBT1"," ",middleButtonPause,"" ,22,10,30,10,CMidBTA,CMidBTNA,CB)
  1073.  BT.createButton("middleBT2"," ",middleButton,"" ,21,11,31,11,CMidBTA,CMidBTNA,CB)
  1074.  BT.createButton("middleBT3"," ",middleButton,"" ,20,12,32,12,CMidBTA,CMidBTNA,CB)
  1075.  BT.createButton("middleBT4","dial",middleButton,"" ,20,13,32,13,CMidBTA,CMidBTNA,CB)
  1076.  BT.createButton("middleBT5"," ",middleButton,"" ,20,14,32,14,CMidBTA,CMidBTNA,CB)
  1077.  BT.createButton("middleBT6"," ",middleButton,"" ,21,15,31,15,CMidBTA,CMidBTNA,CB)
  1078.  BT.createButton("middleBT7"," ",middleButtonConnect,"" ,22,16,30,16,CMidBTA,CMidBTNA,CB)
  1079.  
  1080.  BT.createButton("bt1.1"," ",log,"" ,3,10,19,10,COtherBT,COtherBT,CB)
  1081.  BT.createButton("bt1.2","log ",log,"" ,3,11,18,11,COtherBT,COtherBT,CB)
  1082.  BT.createButton("bt1.3"," ",log,"" ,3,12,17,12,COtherBT,COtherBT,CB)
  1083.  
  1084.  BT.createButton("bt2.1"," ",tmpBT,"" ,3,14,17,14,COtherBT,COtherBT,CB)
  1085.  BT.createButton("bt2.2","Communication ",tmpBT,"" ,3,15,18,15,COtherBT,COtherBT,CB)
  1086.  BT.createButton("bt2.3","Centre ",tmpBT,"" ,3,16,19,16,COtherBT,COtherBT,CB)
  1087.  
  1088.  BT.createButton("bt3.1"," ",Iris,"" ,33,10,49,10,COtherBT,COtherBT,CB)
  1089.  BT.createButton("bt3.2","iris ",Iris,"" ,34,11,49,11,COtherBT,COtherBT,CB)
  1090.  BT.createButton("bt3.3"," ",Iris,"" ,35,12,49,12,COtherBT,COtherBT,CB)
  1091.  
  1092.  BT.createButton("bt4.1"," ",tmpBT,"" ,35,14,49,14,COtherBT,COtherBT,CB)
  1093.  BT.createButton("bt4.2"," ",tmpBT,"" ,34,15,49,15,COtherBT,COtherBT,CB)
  1094.  BT.createButton("bt4.3"," ",tmpBT,"" ,33,16,49,16,COtherBT,COtherBT,CB)
  1095.  
  1096.  --BT.drawButtons()
  1097.  term.setCursorPos(1,19)
  1098. end
  1099.  
  1100. function lockdown()
  1101.  Menu = "lockdown"
  1102.  lockdownMode = true
  1103.  BT.deleteAllButtons()
  1104.  BT.setF(1,6,51,19,CGR)
  1105.  BT.setF(11,7,41,7,CBG)
  1106.  BT.setF(2,9,50,17,CBG)
  1107.  if RequestPassword(-1,false,5,nil,"lockdown") then
  1108.   MainM()
  1109.  end
  1110. end
  1111.  
  1112. settings = {}
  1113.  
  1114. function settings.Back()
  1115.  done2 = true
  1116. end
  1117.  
  1118. function settings.Exit()
  1119.  done2 = true
  1120.  done = true
  1121. end
  1122.  
  1123. function settings.Save()
  1124.  if NewPassword ~= "" and NewPassword == NewPassword2 then
  1125.   f = fs.open(dirSettings.."Password","w")
  1126.   f.writeLine(NewPassword)
  1127.   f.close()
  1128.   settings.Exit()
  1129.  end
  1130. end
  1131.  
  1132. function settings.PasswordSecurity()
  1133.  oldPage = -1
  1134.  if Settings.PasswordEnabled == true then
  1135.   local ans = RequestPassword(-1,false,2,true,"Edit password")
  1136.   if ans == true or ans == "No Pass" then
  1137.    NewPassword = ""
  1138.    NewPassword2 = ""
  1139.    BT.deleteAllButtons()
  1140.    while done ~= true do  
  1141.     BT.setF(11,5,41,12,CGR,CB)
  1142.     BT.setF(13,7,38,7,CLGR,CB)
  1143.     BT.setF(13,9,38,9,CLGR,CB)
  1144.     BT.write("Edit password",51/2-string.len("Edit password")/2+1,5,CB,CGR)
  1145.     BT.write("New Password: ",13,7,CB,CGR)
  1146.     BT.write("Confirmation: ",13,9,CB,CGR)
  1147.     for i = 1,string.len(NewPassword) do
  1148.      BT.write("*",27+i-1,7,CB,CLGR)
  1149.     end
  1150.     for i = 1,string.len(NewPassword2) do
  1151.      BT.write("*",27+i-1,9,CB,CLGR)
  1152.     end
  1153.     BT.createButton("back","Back",settings.Exit,"" ,13,11,25,11,CR,CR,CB)
  1154.     SC = CGR
  1155.     if NewPassword ~= "" and NewPassword == NewPassword2 then
  1156.      SC = CBl
  1157.     end
  1158.     BT.createButton("save","Save",settings.Save,"",27,11,39,11,SC,SC,CB)
  1159.     BT.drawButtons()
  1160.     done2 = false
  1161.     while done2 ~= true and done ~= true do
  1162.      ev,bt,x,y = os.pullEvent()
  1163.      if ev == "terminate" then
  1164.       quitProgram()
  1165.       done2 = true
  1166.      elseif ev == "mouse_click" then
  1167.       done2 = true
  1168.       BT.checkxy(x,y)
  1169.       if (x >= 13 and x <= 38 and y == 7)  then
  1170.        BT.write("New Password: ",13,7,CB,CGR)
  1171.        BT.drawButtons()
  1172.        term.setBackgroundColor(CLGR)
  1173.        term.setCursorPos(27,7)
  1174.        NewPassword = Read(12,NewPassword,CLGR,false,true,Settings.PastePasswordAllowed,false,"*",false,true)
  1175.       elseif (x >= 13 and x <= 38 and y == 9) then
  1176.        BT.write("Confirmation: ",13,9,CB,CGR)
  1177.        BT.drawButtons()
  1178.        term.setBackgroundColor(CLGR)
  1179.        term.setCursorPos(27,9)
  1180.        NewPassword2 = Read(12,NewPassword2,CLGR,false,true,Settings.PastePasswordAllowed,false,"*",false,true)
  1181.       elseif (x <= 11 or x >= 41 or y <= 5 or y >= 12) then
  1182.        done2 = false
  1183.        return("back")
  1184.       end
  1185.      end
  1186.     end
  1187.    end
  1188.   end
  1189.  end
  1190.  NewPassword = ""
  1191.  NewPassword2 = ""
  1192.  done = false
  1193.  done2 = false
  1194. end
  1195.  
  1196. function settings.DrawSecurity()
  1197.  BT.deleteAllButtons()
  1198.  BT.setF(1,1,51,19,COL)
  1199.  BT.setF(2,2,50,17,CBG)
  1200.  BT.createButton("Back","Back",settings.Back,"" ,3,17,25,17,CR,CR,CB)
  1201.  BT.createButton("Exit","Exit",settings.Exit,"" ,27,17,49,17,CR,CR,CB)
  1202.  BT.CreateSildeBar(3,3,"PasswordEnabled","Password is enabled",Settings.PasswordEnabled)
  1203.  slider = Settings.PasswordEnabled
  1204.  if Settings.PasswordEnabled then
  1205.   BT.createButton("EditPassword","Edit password",settings.PasswordSecurity,"" ,3,5,49,5,CLGR,CLGR,CB)
  1206.   if page == 0 then
  1207.    BT.createButton("NextPage","---------->",swpage,1,27,15,49,15,CBl,CBl,CB)
  1208.    BT.CreateSildeBar(3,7,"StartupReqPass","Startup requires password",Settings.StartupReqPass)
  1209.    BT.CreateSildeBar(3,9,"ChangingSettingsReqPass","Opening settings requires password",Settings.ChangingSettingsReqPass)
  1210.    BT.CreateSildeBar(3,11,"OpeningIrisReqPas","Opening the iris requires password",Settings.OpeningIrisReqPas)
  1211.    BT.CreateSildeBar(3,13,"ExitingProgramReqPass","Exit the program requires password",Settings.ExitingProgramReqPass)
  1212.   elseif page == 1 then
  1213.    BT.createButton("PreviousPage","<----------",swpage,-1,3,15,25,15,CBl,CBl,CB)
  1214.    BT.createButton("NextPage","---------->",swpage,1,27,15,49,15,CBl,CBl,CB)
  1215.    BT.CreateSildeBar(3,7,"PastePasswordAllowed","You can paste the password",Settings.PastePasswordAllowed)
  1216.    BT.CreateSildeBar(3,9,"DialingReqPass","Opening dialer requires password",Settings.DialingReqPass)
  1217.    BT.CreateSildeBar(3,11,"EditAddressReqPass","Edit an address requires password",Settings.EditAddressReqPass)
  1218.    BT.CreateSildeBar(3,13,"DelAddressReqPass","Delete an address requires password",Settings.DelAddressReqPass)
  1219.   elseif page == 2 then
  1220.    BT.createButton("PreviousPage","<----------",swpage,-1,3,15,25,15,CBl,CBl,CB)
  1221.    BT.CreateSildeBar(3,7,"AddAddressReqPass","Adding an address requires password",Settings.AddAddressReqPass)
  1222.   end
  1223.  end
  1224.  BT.drawButtons()
  1225.  BT.DrawSlideBars()
  1226. end
  1227.  
  1228. function settings.Security()
  1229.  page = 0
  1230.  Menu = "Settings-Security"
  1231.  slider = Settings.PasswordEnabled oldSlider = Settings.PasswordEnabled
  1232.  getSettings()
  1233.  done2 = false
  1234.  while done2 ~=  true do
  1235.   if Settings.PasswordEnabled and slider == false then
  1236.    local ans = RequestPassword(-1,false,2,true,"Disable security")
  1237.    if ans == true or ans == "No Pass" then
  1238.     page = 0
  1239.     Settings.PasswordEnabled = false
  1240.     setSettings()
  1241.     getSettings()
  1242.    else
  1243.     Settings.PasswordEnabled = true
  1244.     setSettings()
  1245.     getSettings()
  1246.    end
  1247.   elseif slider == true then
  1248.    Settings.PasswordEnabled = true
  1249.    setSettings()
  1250.    getSettings()
  1251.  -- end
  1252.   elseif Settings.PasswordEnabled then
  1253.    
  1254.    setSettings()
  1255.    getSettings()
  1256.   end
  1257.   settings.DrawSecurity()
  1258.   ev = "timer"
  1259.   oldPage = page
  1260.   while ev == "timer" and done2 ~= true do
  1261.    ev,bt,x,y = os.pullEvent()
  1262.    if ev == "terminate" then
  1263.     quitProgram()
  1264.    elseif ev == "mouse_click" then
  1265.     BT.checkxy(x,y)
  1266.     BT.checkSlideBar(x,y)
  1267.     if oldPage ~= -1 then
  1268.      slider = BT.getSliderValue("PasswordEnabled")
  1269.     end
  1270.     if Settings.PasswordEnabled == true then
  1271.      if oldPage == 0 then
  1272.       Settings.StartupReqPass = BT.getSliderValue("StartupReqPass")
  1273.       Settings.ChangingSettingsReqPass = BT.getSliderValue("ChangingSettingsReqPass")
  1274.       Settings.OpeningIrisReqPas = BT.getSliderValue("OpeningIrisReqPas")
  1275.       Settings.ExitingProgramReqPass = BT.getSliderValue("ExitingProgramReqPass")
  1276.      elseif oldPage == 1 then
  1277.       Settings.PastePasswordAllowed = BT.getSliderValue("PastePasswordAllowed")
  1278.       Settings.DialingReqPass = BT.getSliderValue("DialingReqPass")
  1279.       Settings.EditAddressReqPass = BT.getSliderValue("EditAddressReqPass")
  1280.       Settings.DelAddressReqPass = BT.getSliderValue("DelAddressReqPass")
  1281.      elseif oldPage == 2 then
  1282.       Settings.AddAddressReqPass = BT.getSliderValue("AddAddressReqPass")
  1283.      end
  1284.     end
  1285.    end
  1286.   end
  1287.  
  1288.  
  1289.   --done2 = true
  1290.  end
  1291. end
  1292.  
  1293. function settings.Peripherals()
  1294.  Menu = "Settings-Peripherals"
  1295.  if fs.exists(dirData.."Peripherals_Stargate") then
  1296.   f = fs.open(dirData.."Peripherals_Stargate","r")
  1297.   stargate = f.readLine()
  1298.   f.close()
  1299.  else
  1300.   stargate = ""
  1301.  end
  1302.  if stargate == nil then
  1303.   stargate = ""
  1304.  end
  1305.  done2 = false
  1306.  while done2 ~=  true do
  1307.   BT.deleteAllButtons()
  1308.   BT.setF(1,1,51,19,COL)
  1309.   BT.setF(2,2,50,17,CBG)
  1310.   BT.createButton("Back","Back",settings.Back,"" ,3,17,25,17,CR,CR,CB)
  1311.   BT.createButton("Exit","Exit",settings.Exit,"" ,27,17,49,17,CR,CR,CB)
  1312.   BT.write("Peripheral's",51/2-string.len("Peripheral's")/2+1,1,CB,COL)
  1313.   BT.CreateSildeBar(3,3,"AutoFindGate","Automatically find a gate",Settings.AutoFindGate)
  1314.   if Settings.AutoFindGate ~= true then
  1315.    BT.setF(3,5,30,5,CLGR)
  1316.    BT.write("StargateBase: ",3,5,CB,CW)
  1317.    BT.write(stargate,18,5,CB,CLGR)
  1318.    if peripheral.getType(stargate) == "StargateBase" then
  1319.     BT.setF(37,5,36,5,CG)
  1320.    else
  1321.     BT.setF(37,5,36,5,CR)
  1322.    end
  1323.   end
  1324.   ev = "timer"
  1325.   BT.drawButtons()
  1326.   while ev == "timer" and done2 ~= true do
  1327.    ev,bt,x,y = os.pullEvent()
  1328.    if ev == "terminate" then
  1329.     quitProgram()
  1330.    elseif ev == "mouse_click" then
  1331.     BT.checkxy(x,y)
  1332.     BT.checkSlideBar(x,y)
  1333.     Settings.AutoFindGate = BT.getSliderValue("AutoFindGate")
  1334.     setSettings()
  1335.     if (x >= 18 and x <= 35 and y == 5) and Settings.AutoFindGate ~= true then
  1336.      BT.write("StargateBase: ",3,5,CB,CW)
  1337.      BT.drawButtons()
  1338.      term.setBackgroundColor(CLGR)
  1339.      term.setCursorPos(18,5)
  1340.      BT.write(stargate,18,5,CB,CLGR)
  1341.      stargate = Read(18,stargate,CLGR,false,true,Settings.PastePasswordAllowed,false,nil,true)
  1342.      if peripheral.getType(stargate) == "StargateBase" then
  1343.       sg = {}
  1344.       sg = peripheral.wrap(stargate)
  1345.       f = fs.open(dirData.."Peripherals_Stargate","w")
  1346.       f.writeLine(stargate)
  1347.       f.close()
  1348.      end
  1349.     elseif Settings.AutoFindGate == true then
  1350.      local tmpsg = nil
  1351.      tmpsg = peripheral.find("StargateBase")
  1352.      if tmpsg ~= nil then
  1353.       sg = {}
  1354.       sg = tmpsg
  1355.      else
  1356.       BT.drawButtons()
  1357.      end
  1358.     else
  1359.      BT.drawButtons()
  1360.     end
  1361.    end
  1362.   end
  1363.  end
  1364. end
  1365.  
  1366. function settings.Colors()
  1367.  Menu = "Settings-Colors"
  1368.  
  1369.  done2 = true
  1370. end
  1371.  
  1372. function settings.Other()
  1373.  Menu = "Settings-Other"
  1374.  
  1375.  done2 = true
  1376. end
  1377.  
  1378. function settings.Help()
  1379.  Menu = "Settings-Help"
  1380.  
  1381.  done2 = true  
  1382. end
  1383. function settings.main()
  1384.  Menu = "Settings-Main"
  1385.  scroll = 1
  1386.  maxScroll = 10
  1387.  BT.deleteAllButtons()
  1388.  BT.setF(1,1,51,19,COL)
  1389.  BT.setF(2,2,50,17,CBG)
  1390.  local pas = ""
  1391.  if Settings.ChangingSettingsReqPass and Settings.PasswordEnabled then
  1392.   pas = RequestPassword(-1,false,0,true,"Settings")
  1393.  end
  1394.   done = false
  1395.    BT.deleteAllButtons()
  1396.    BT.setF(2,2,50,17,CBG)
  1397.   if Settings.ChangingSettingsReqPass ~= true or Settings.PasswordEnabled ~= true or pas == true or pas == "No Pass"then
  1398.   done = false
  1399.    while done ~= true do
  1400.    BT.deleteAllButtons()
  1401.    BT.setF(2,2,50,17,CBG)
  1402.    BT.createButton("Exit","Exit",settings.Exit,"" ,3,17,25,17,CR,CR,CB)
  1403.    BT.createButton("Exit program","Exit program (Ctrl-T)",quitProgram,"" ,27,17,49,17,CR,CR,CB)
  1404.    BT.createButton("Security","Security",settings.Security,"" ,3,3,49,5,COtherBT,COtherBT,CB)
  1405.    BT.createButton("Peripherals","Peripherals",settings.Peripherals,"" ,3,7,49,9,COtherBT,COtherBT,CB)
  1406.    BT.createButton("Colors","Colors",settings.Colors,"" ,3,11,49,13,COtherBT,COtherBT,CB)
  1407.    BT.createButton("Other","Other",settings.Other,"" ,3,15,25,15,COtherBT,COtherBT,CB)
  1408.    BT.createButton("Help","Help",settings.Help,"" ,27,15,49,15,COtherBT,COtherBT,CB)
  1409.    BT.drawButtons()
  1410.    done2 = false
  1411.    while done2 ~= true and done ~= true do
  1412.     ev,bt,x,y = os.pullEvent()
  1413.     if ev == "terminate" then
  1414.      quitProgram()
  1415.      done2 = true
  1416.     elseif ev == "mouse_click" then
  1417.      BT.checkxy(x,y)
  1418.     end
  1419.    end
  1420.   end
  1421.  end
  1422.  MainM()
  1423. end
  1424.  
  1425. function toggleMiddleButton()
  1426.  if SG.getActivatedChevrons() ~= 0 then
  1427.   BT.changeDispName("middleBT4","disconnect")
  1428.   if SGbg.Pause == false then
  1429.    BT.changeDispName("middleBT1","Pause")
  1430.    BT.setButton("middleBT1",false)
  1431.   else
  1432.    BT.changeDispName("middleBT1","Continue")
  1433.    BT.setButton("middleBT1",true)
  1434.   end
  1435.   BT.changeDispName("middleBT7","Connect")
  1436.   BT.setButton("middleBT7",SGbg.AutoConnect)
  1437.   stat = true
  1438.  else
  1439.   BT.changeDispName("middleBT1"," ")
  1440.   BT.changeDispName("middleBT4","dial")
  1441.   BT.changeDispName("middleBT7"," ")
  1442.   BT.setButton("middleBT1",false)
  1443.   BT.setButton("middleBT7",false)
  1444.   stat = false
  1445.  end
  1446.  BT.setButton("middleBT2",stat)
  1447.  BT.setButton("middleBT3",stat)
  1448.  BT.setButton("middleBT4",stat)
  1449.  BT.setButton("middleBT5",stat)
  1450.  BT.setButton("middleBT6",stat)
  1451.  BT.drawButtons()
  1452. end
  1453.  
  1454. function middleButtonPause()
  1455.  if SG.getActivatedChevrons() == 0 or button ~= 1 then
  1456.   middleButton()
  1457.  else
  1458.   SGbg.Pause = not(SGbg.Pause)
  1459.   toggleMiddleButton()
  1460.  end
  1461. end
  1462.  
  1463. function middleButtonConnect()
  1464.  if SG.getActivatedChevrons() == 0 or button ~= 1 then
  1465.   middleButton()
  1466.  else
  1467.   SGbg.AutoConnect = not(SGbg.AutoConnect)
  1468.  end
  1469. end
  1470.  
  1471. function middleButton()
  1472.  if SG.getActivatedChevrons() ~= 0 and button == 1 then
  1473.   stat = pcall(SG.disconnect)
  1474.   stat = pcall(SG.deactivateAllChevrons)
  1475.   if stat == true then
  1476.    DispMsg("disconnecting",CG)
  1477.    sleep(0.8)
  1478.   else
  1479.    DispMsg("couldn't disconnect",CR)
  1480.   end
  1481.  else
  1482.   --gui
  1483.   if sg ~= nil then
  1484.    Dial()
  1485.    if ans[1] == true then
  1486.     if ans[2] == 0 then
  1487.      DispMsg(" ",CB)
  1488.     else
  1489.      DispMsg("dialling to: "..ans[2],CB)
  1490.      if not(fs.exists(dirData.."log/day="..os.day().." time="..os.time()..".log")) then
  1491.       f = fs.open(dirData.."log/day="..os.day().." time="..os.time()..".log","w")
  1492.       f.writeLine("dialled to:")
  1493.       f.writeLine("Address:")
  1494.       f.writeLine(ans[2])
  1495.       f.close()
  1496.      end
  1497.     end
  1498.    else
  1499.     DispMsg(ans[2],CR)
  1500.    end
  1501.   else
  1502.    DispMsg("No stargate connected",CR)
  1503.   end
  1504.  end
  1505. end
  1506.  
  1507.  
  1508. function bgr()
  1509. end
  1510.  
  1511. function favorite()
  1512.  f = fs.open(dirAddress..nc.."/"..curAddress,"r")
  1513.  local nm = f.readLine()
  1514.  local fav = f.readLine()
  1515.  f.close()
  1516.  if fav == "true" then fav = false
  1517.  elseif fav == "false" then fav = true end
  1518.  BT.setButton("fav",fav)
  1519.  f = fs.open(dirAddress..nc.."/"..curAddress,"w")
  1520.  f.writeLine(nm)
  1521.  f.writeLine(fav)
  1522.  f.close()
  1523. end
  1524.  
  1525. function favorite2()
  1526.  if fav == true then fav = false
  1527.  elseif fav == false then fav = true end
  1528.  BT.setButton("fav2",fav)
  1529.  BT.drawButtons()
  1530. end
  1531.  
  1532.  
  1533. function addAddress()
  1534.  local pas = ""
  1535.  if Settings.AddAddressReqPass and Settings.PasswordEnabled then
  1536.   pas = RequestPassword(-1,false,1,true,"Add address")
  1537.  end
  1538.  if pas == "No Pass" or pas == true or Settings.AddAddressReqPass ~= true or Settings.PasswordEnabled ~= true then
  1539.   if filter == "favorite" then fav = true
  1540.   else fav = false
  1541.   end
  1542.   newAddress = "" newName = ""
  1543.   done2 = false
  1544.   --BT.setF(1,1,51,19,CGR)
  1545.   BT.deleteAllButtons()
  1546.   BT.createButton("bg","",bgr,"" ,11,5,41,13,CGR,CGR,CB)
  1547.   BT.createButton("bg2","",bgr,"" ,13,6,39,9,CLGR,CLGR,CB)
  1548.   BT.createButton("fav2","favorite",favorite2,"" ,11,14,41,16,CY,CB,CC)
  1549.   BT.setButton("fav2",fav)
  1550.   BT.drawButtons()
  1551.   while done2 ~= true do
  1552.    if string.len(newAddress) == 9 then
  1553.     nc = 9
  1554.    elseif string.len(newAddress) == 7 then
  1555.     nc = 7
  1556.    else
  1557.     nc = "invalid"
  1558.    end
  1559.    BT.setTextColor(CB)
  1560.    dr = dirAddress..nc.."/"
  1561.    if string.len(newAddress) ~= nc or fs.exists(dr..newAddress)  
  1562.    then
  1563.     bgc = CR
  1564.     addressSave = false
  1565.    else
  1566.     bgc = CBl
  1567.     addressSave = true
  1568.    end
  1569.    BT.setF(13,11,25,12,bgc)
  1570.    BT.write("save",18,12,CB,bgc)
  1571.    BT.setF(27,11,39,12,CR)
  1572.    BT.write("cancel",31,12,CB,CR)
  1573.    BT.setBackgroundColor(CLGR)
  1574.    for i = 1,9 do
  1575.     BT.write(" ",21+i,6,CB,CLGR)
  1576.    end
  1577.    BT.write("Address: "..newAddress,13,6,CB)
  1578.    for i = 1,13  do
  1579.     BT.write(" ",18+i,8,CB,CLGR)
  1580.    end
  1581.    BT.write("Name: "..newName,13,8,CB)
  1582.    BT.draw()
  1583.    ev,button,x,y = os.pullEvent("mouse_click")
  1584.    if ev == "terminate" then
  1585.     quitProgram()
  1586.     done2 = true
  1587.    elseif ev == "mouse_click" then
  1588.     done2 = not(BT.checkxy(x,y))
  1589.     if y == 6 and x >= 22 and x <= 30 then
  1590.      BT.setBackgroundColor(CW)
  1591.      for i = 1,7 do
  1592.       BT.write(" ",21+i,6,CB,CW)
  1593.      end
  1594.      BT.write("++",29,6,CLGR,CW)
  1595.      BT.setTextColor(CB)
  1596.      term.setTextColor(CB)
  1597.      term.setCursorPos(22,6)
  1598.      newAddress = Read(9,newAddress,CW,true,true,true)
  1599.     end
  1600.     if y == 8 and x >= 19 and x <= 31 then
  1601.      BT.setBackgroundColor(CLGR)
  1602.      BT.setBackgroundColor(CW)
  1603.      term.setCursorPos(19,8)
  1604.      BT.write("             ")
  1605.      term.setCursorPos(19,8)
  1606.      newName = Read(13,newName,CW,false,true,true)
  1607.     end
  1608.     if x >= 27 and x <= 39 and y >= 11 and y <= 12 then
  1609.      done2 = true
  1610.     end
  1611.     if x >= 13 and x <= 25 and y >= 11 and y <= 12  then
  1612.      if addressSave == true then
  1613.       if not(fs.exists(dr..newAddress)) then  
  1614.        f = fs.open(dr..newAddress,"w")
  1615.        f.writeLine(newName)
  1616.        f.writeLine(fav)
  1617.        f.close()
  1618.        done2 = true
  1619.       end
  1620.      end
  1621.     end
  1622.    end
  1623.   end
  1624.  end
  1625.  BT.deleteAllButtons()
  1626. end
  1627.  
  1628. function editAddress(num)
  1629.  done2 = false
  1630.  local pas = ""
  1631.  if Settings.EditAddressReqPass and Settings.PasswordEnabled then
  1632.   pas = RequestPassword(-1,false,1,true,"Edit address")
  1633.  end
  1634.  if pas == "No Pass" or pas == true or Settings.EditAddressReqPass ~= true or Settings.PasswordEnabled ~= true then
  1635.   --BT.setF(1,1,51,19,CGR)
  1636.   BT.deleteAllButtons()
  1637.   BT.createButton("bg","",bgr,"" ,11,5,41,13,CGR,CGR,CB)
  1638.   BT.createButton("bg2","",bgr,"" ,13,6,33,9,CLGR,CLGR,CB)
  1639.   BT.createButton("fav","favorite",favorite,"" ,11,14,41,16,CY,CB,CC)
  1640.   if sorting == "F" or addressListSearch[1] == nil then
  1641.    if addressListFilter[num]["favorite"] == "true" then
  1642.     BT.toggleButton("fav")
  1643.    end
  1644.    curName = addressListFilter[num]["name"]
  1645.    curAddress = addressListFilter[num]["address"]
  1646.   else
  1647.    if addressListSearch[num]["favorite"] == "true" then
  1648.     BT.toggleButton("fav")
  1649.    end
  1650.    curName = addressListSearch[num]["name"]
  1651.    curAddress = addressListSearch[num]["address"]
  1652.   end
  1653.   newName = curName  newAddress = curAddress
  1654.   nameSave = false  addressSave = false  delAD = 1
  1655.   while done2 ~= true do
  1656.    BT.drawButtons()
  1657.    BT.setTextColor(CB)
  1658.    BT.setF(13,11,33,12,CR)
  1659.    if delAD == 2 then
  1660.     BT.write("press again to",17,11,CB,CR)
  1661.    end
  1662.    BT.write("delete",21,12,CB,CR)
  1663.    BT.write("Edit Menu",18,5,CB,CGR)
  1664.    if sorting == "F" or addressListSearch[1] == nil then
  1665.     nc = string.len(addressListFilter[num]["address"])
  1666.    else
  1667.     nc = string.len(addressListSearch[num]["address"])
  1668.    end
  1669.    BT.setBackgroundColor(CLGR)
  1670.    BT.write("Address: "..newAddress,13,6,CB,CLGR)
  1671.    term.setCursorPos(13,8)
  1672.    BT.write("Name: "..newName,13,8,CB,CLGR)
  1673.    if curAddress ~= newAddress then
  1674.     dr = dirAddress..nc.."/"
  1675.     if string.len(newAddress) ~= nc or fs.exists(dr..newAddress) then
  1676.      SColor = CR
  1677.      addressSave = false
  1678.     else
  1679.      SColor = CBl
  1680.      addressSave = true
  1681.     end
  1682.    else
  1683.     SColor = CLGR
  1684.     addressSave = false
  1685.    end
  1686.    term.setCursorPos(36,6)
  1687.    BT.write("save",36,6,CB,SColor)
  1688.    if curName ~= newName then
  1689.     SColor = CBl
  1690.     nameSave = true
  1691.    else
  1692.     SColor = CLGR
  1693.     nameSave = false
  1694.    end
  1695.    term.setCursorPos(36,8)
  1696.    BT.write("save",36,8,CB,SColor)
  1697.    BT.draw()
  1698.    ev,button,x,y = os.pullEvent("mouse_click")
  1699.    if ev == "terminate" then
  1700.     quitProgram()
  1701.     done2 = true
  1702.    elseif ev == "mouse_click" then
  1703.     done2 = not(BT.checkxy(x,y))
  1704.     if y == 6 and x >= 22 and x <= 21+nc then
  1705.      BT.write("old: "..curAddress,17,7,CB,CLGR)
  1706.      BT.setBackgroundColor(CW)
  1707.      BT.draw()
  1708.      for i = 1,nc do
  1709.       BT.write(" ",21+i,6,CB,CW)
  1710.      end
  1711.      term.setCursorPos(22,6)
  1712.      newAddress = Read(nc,newAddress,CW,true,true,true)
  1713.     end
  1714.     if y == 6 and x >= 36 and x <= 39 then
  1715.      if addressSave == true then
  1716.       dr = dirAddress..nc.."/"
  1717.       fs.move(dr..curAddress,dr..newAddress)
  1718.       if fs.exists(dr..newAddress) then
  1719.        curAddress = newAddress
  1720.       end
  1721.      end
  1722.     end
  1723.     if y == 8 and x >= 19 and x <= 31 then
  1724.      BT.setBackgroundColor(CLGR)
  1725.      BT.write("old: "..curName,14,9,CB,CLGR)
  1726.      BT.draw()
  1727.      BT.setBackgroundColor(CW)
  1728.      BT.write("             ",19,8,CB,CW)
  1729.      term.setCursorPos(19,8)
  1730.      newName = Read(13,newName,CW,false,true,true)
  1731.     end
  1732.     if y == 8 and x >= 36 and x <= 39 then
  1733.      if nameSave == true then
  1734.       curName = newName
  1735.       f = fs.open(dirAddress..nc.."/"..curAddress,"r")
  1736.       f.readLine()
  1737.       fav = f.readLine()
  1738.       f.close()
  1739.       f = fs.open(dirAddress..nc.."/"..curAddress,"w")
  1740.       f.writeLine(newName)
  1741.       f.writeLine(fav )
  1742.       f.close()
  1743.      end
  1744.     end
  1745.     if y >= 11 and y <= 12 and x >= 13 and x <= 33 then
  1746.      if delAD == 2 then
  1747.       if Settings.DelAddressReqPass and Settings.PasswordEnabled then
  1748.        pas = RequestPassword(-1,false,4,true,"Delete address")
  1749.        done2 = true
  1750.       end
  1751.       if pas == "No Pass" or pas == true or Settings.DelAddressReqPass ~= true or Settings.PasswordEnabled ~= true then
  1752.        fs.delete(dirAddress..nc.."/"..curAddress)
  1753.        done2 = true
  1754.        delAD = 1
  1755.       end
  1756.      else
  1757.       delAD = 2
  1758.      end
  1759.     else
  1760.      delAD = 1
  1761.     end
  1762.     BT.setF(13,11,33,12,CR)
  1763.    end
  1764.   end
  1765.  end
  1766.  BT.deleteAllButtons()
  1767. end
  1768.  
  1769. function getAddresses(filter)
  1770.  local addressList = {}
  1771.  local ADC = {}
  1772.  local BadAddress = 0
  1773.  for i = 0,1 do
  1774.   addressList[7+2*i] = {} ADC[7+2*i] = 0
  1775.   if fs.exists(dirAddress..7+2*i) then
  1776.    for num,address in pairs(fs.list(dirAddress..7+2*i)) do
  1777.     if string.len(address) == 7+2*i and isValidAddress(address) then
  1778.      f = fs.open(dirAddress..7+2*i.."/"..address,"r")
  1779.      addressList[7+2*i][num-BadAddress] = {}
  1780.      addressList[7+2*i][num-BadAddress]["address"] = address
  1781.      addressList[7+2*i][num-BadAddress]["name"] = f.readLine()
  1782.      addressList[7+2*i][num-BadAddress]["favorite"] = f.readLine()
  1783.      f.close()
  1784.      ADC[7+2*i] = num - BadAddress
  1785.     else
  1786.      BadAddress = BadAddress + 1
  1787.     end
  1788.    end
  1789.   end
  1790.  end
  1791.  return({ADC,addressList})
  1792. end
  1793.  
  1794. function swpage(num)
  1795.  page = page + num
  1796. end
  1797.  
  1798. function filterFunc()
  1799.  page = 0
  1800.  if button == 1 then
  1801.   if filter == "all" then
  1802.    filter = "7 chevrons"
  1803.   elseif filter == "7 chevrons" then
  1804.    filter = "9 chevrons"
  1805.   elseif filter == "9 chevrons" then
  1806.    filter = "favorite"
  1807.   else
  1808.    filter = "all"
  1809.   end
  1810.  elseif button == 2 or button == -1 then
  1811.   if filter == "7 chevrons" then
  1812.    filter = "all"
  1813.   elseif filter == "9 chevrons" then
  1814.    filter = "7 chevrons"
  1815.   elseif filter == "favorite" then
  1816.    filter = "9 chevrons"
  1817.   else
  1818.    filter = "favorite"
  1819.   end
  1820.  elseif button == 3 then
  1821.   filter = "all"
  1822.  end
  1823. end
  1824.  
  1825.  
  1826.  
  1827. function exit()
  1828.  exitv = true
  1829.  done = true
  1830. end
  1831.  
  1832. function goBack()
  1833.  back = true
  1834. end
  1835.  
  1836. function optionsSelectAddress()
  1837.  BT.deleteAllButtons()
  1838.  done2 = false
  1839.  if y > 14 then y = 14 end
  1840.  if x > 39 then x = 39 end
  1841.  while done2 ~= true do
  1842.   BT.setF(x,y,x+10,y+2,CGR)
  1843.   BT.setF(x+1,y+1,x+9,y+1,CGR)
  1844.   BT.createButton("Export","Export",Export,"" ,x+1,y+1,x+9,y+1,CR,CR,CB)
  1845.   BT.drawButtons()
  1846.   while done2 ~= true do
  1847.    local ev,button,xev,yev = os.pullEvent("mouse_click")
  1848.    if ev == "terminate" then
  1849.     quitProgram(1)
  1850.     done2 = true
  1851.    elseif ev == "mouse_click" then
  1852.     if xev < x or yev < y or xev > x+10 or yev > y+2  then done2 = true end
  1853.     BT.checkxy(x,y)
  1854.    end
  1855.   end
  1856.  end
  1857.  BT.deleteAllButtons()
  1858. end
  1859.  
  1860. function deleteAddress(address)
  1861.  nc = string.len(address)
  1862.  if fs.exists(dirAddress..nc.."/"..address) then
  1863.   fs.delete(dirAddress..nc.."/"..address)
  1864.  end
  1865. end
  1866.  
  1867. function deleteAllSelectedAddresses()
  1868.  local pas = ""
  1869.  deletePress = true
  1870.  if delAD ~= 2 then delAD = 1 end
  1871.  if delAD == 2 then
  1872.   if Settings.DelAddressReqPass and Settings.PasswordEnabled then
  1873.    pas = RequestPassword(-1,false,4,true,"Delete address")  
  1874.   end
  1875.   if pas == "No Pass" or pas == true or Settings.DelAddressReqPass ~= true or Settings.PasswordEnabled ~= true then  
  1876.    table.foreach(selectAddresses,deleteAddress)
  1877.    clearSelection()
  1878.    done2 = true
  1879.    delAD = 1
  1880.   end
  1881.  else
  1882.   delAD = 2
  1883.  end
  1884. end
  1885.  
  1886. function doesAddressStillExist(address)
  1887.  nc = string.len(address)
  1888.  if fs.exists(dirAddress..nc.."/"..address) then
  1889.   selectAddresses[address] = true
  1890.  end
  1891. end
  1892.  
  1893. function getNumSelectAddress()
  1894.  local function addNumSelectAddresses() numSelectAddresses = numSelectAddresses + 1 end
  1895.  numSelectAddresses = 0
  1896.  table.foreach(selectAddresses,addNumSelectAddresses)
  1897.  return(numSelectAddresses)
  1898. end
  1899.  
  1900. function clearSelection()
  1901.  selectAddresses = {}
  1902. end
  1903.  
  1904. function selectAllAdresses()
  1905.  if addressListFilter ~= nil then
  1906.   for i = 1,table.getn(addressListFilter) do
  1907.    selectAddresses[addressListFilter[i]["address"]] = true
  1908.   end
  1909.  end
  1910. end
  1911.  
  1912. function SelectAddress(num)
  1913.  if button == 1 then
  1914.   if getNumSelectAddress() == 0 then
  1915.    done = true
  1916.    if sorting == "F" or addressListSearch[1] == nil then
  1917.     SAddress = addressListFilter[num]["address"]
  1918.    else
  1919.     SAddress = addressListSearch[num]["address"]
  1920.    end
  1921.   else
  1922.    if sorting == "F" or addressListSearch[1] == nil then
  1923.     if selectAddresses[addressListFilter[num]["address"]] == nil then
  1924.      selectAddresses[addressListFilter[num]["address"]] = true
  1925.     else
  1926.      selectAddresses[addressListFilter[num]["address"]] = nil
  1927.     end
  1928.    else
  1929.     if selectAddresses[addressListSearch[num]["address"]] == nil then
  1930.      selectAddresses[addressListSearch[num]["address"]] = true
  1931.     else
  1932.      selectAddresses[addressListSearch[num]["address"]] = nil
  1933.     end
  1934.    end
  1935.   end
  1936.  elseif button == 2 then
  1937.   if getNumSelectAddress() == 0 then
  1938.    editAddress(num)
  1939.   else
  1940.    optionsSelectAddress()
  1941.   end
  1942.  elseif button == 3 then
  1943.   if sorting == "F" or addressListSearch[1] == nil then
  1944.    if selectAddresses[addressListFilter[num]["address"]] == nil then
  1945.     selectAddresses[addressListFilter[num]["address"]] = true
  1946.    else
  1947.     selectAddresses[addressListFilter[num]["address"]] = nil
  1948.    end
  1949.   else
  1950.    if selectAddresses[addressListSearch[num]["address"]] == nil then
  1951.     selectAddresses[addressListSearch[num]["address"]] = true
  1952.    else
  1953.     selectAddresses[addressListSearch[num]["address"]] = nil
  1954.    end
  1955.   end
  1956.  end
  1957. end
  1958.  
  1959. function switchSorting()
  1960.  if sorting == "F" then
  1961.   sorting = "S"
  1962.  else
  1963.   sorting = "F"
  1964.  end
  1965. end
  1966.  
  1967. function getSearchResults()
  1968.  if Word == "" or Word == nil then
  1969.   addressListSearch = {}
  1970.  else
  1971.   counter = 1
  1972.   counter2 = 1
  1973.   addressListSearch = {}
  1974.   while addressListFilter[counter] ~= nill do
  1975.    MatchAdr = string.match(addressListFilter[counter]["address"],Word)
  1976.    MatchName = string.match(addressListFilter[counter]["name"],Word)
  1977.    if (MatchAdr ~= nil and MatchAdr ~= "" and MatchAdr ~= " " and MatchAdr ~= "  ") or (MatchName ~= nil and MatchName ~= "" and MatchName ~= " " and MatchName ~= "  ")  then
  1978.     addressListSearch[counter2] = {}
  1979.     addressListSearch[counter2]["address"] = addressListFilter[counter]["address"]
  1980.     addressListSearch[counter2]["name"] = addressListFilter[counter]["name"]
  1981.     addressListSearch[counter2]["favorite"] = addressListFilter[counter]["favorite"]
  1982.     counter2 = counter2 + 1
  1983.     else
  1984.    end
  1985.    counter = counter + 1
  1986.   end
  1987.   addressCountSearch = counter2
  1988.   if addressListSearch[1] == nil then
  1989.    addressListSearch[1] = {}
  1990.    addressListSearch[1]["name"] = "No Matching Addresses Found"
  1991.    addressCount = 0
  1992.   end
  1993.  end
  1994. end
  1995.  
  1996. function searchAddress()
  1997.  if Word == nil then Word = ""  end
  1998.  BT.write("            ",20,2,CB,CW)
  1999.  BT.draw()
  2000.  term.setCursorPos(20,2)
  2001.  Word = Read(12,Word,CW,false,true,true)
  2002.  page = 0
  2003.  getSearchResults()
  2004. end
  2005.  
  2006. function list()
  2007.  selectAddresses = {}
  2008.  addressListSearch = {}
  2009.  addressCountSearch = 0
  2010.  page = 0 done = false delAD = 1 deletePress = false Word = nil
  2011.  if fs.exists(dirData.."FilterAndSorting") then
  2012.   f = fs.open(dirData.."FilterAndSorting","r")
  2013.   filter = f.readLine()
  2014.   sorting = f.readLine()
  2015.   f.close()
  2016.  else
  2017.   f = fs.open(dirData.."FilterAndSorting","w")
  2018.   filter = f.writeLine("all")
  2019.   sorting = f.writeLine("F")
  2020.   f.close()
  2021.   filter = "all"
  2022.   sorting = "F"
  2023.  end
  2024.  while done ~= true do  
  2025.   BT.setF(1,1,51,19,COL)
  2026.   BT.setF(2,4,50,17,CBG)
  2027.   local ADC = {}
  2028.   local tmpVal = getAddresses()
  2029.   ADC = tmpVal[1]
  2030.   addressList = tmpVal[2]
  2031.   newSelectAddresses = selectAddresses
  2032.   selectAddresses = {}
  2033.   table.foreach(newSelectAddresses,doesAddressStillExist)
  2034.   BT.deleteAllButtons()
  2035.   if sorting == "F" then
  2036.    BT.createButton("filter",filter,filterFunc,"" ,19,2,32,2,CGR,CLGR,CB)
  2037.   else
  2038.    if Word == nil or Word == "" then
  2039.     BT.createButton("search","search",searchAddress,"" ,19,2,32,2,CGR,CW,CLGR)
  2040.    else
  2041.     BT.createButton("search",Word,searchAddress,"" ,19,2,32,2,CW,CW,CB)
  2042.    end
  2043.   end
  2044.   BT.createButton("sorting",sorting,switchSorting,"" ,33,2,33,2,CGR,CLGR,CGR)
  2045.   if getNumSelectAddress() == 0 then
  2046.    BT.createButton("addAddress","Add address",addAddress,"" ,35,2,49,2,CL,CG,CB)
  2047.    BT.createButton("exit","exit",exit,"" ,19,17,33,17,CR,CR,CB)
  2048.   else
  2049.    BT.createButton("select all:","select all:",selectAllAdresses,"" ,3,2,16,2,CLGR,CLGR,CB)
  2050.    BT.createButton("delete"," delete".." ",deleteAllSelectedAddresses,"" ,35,2,49,2,CR,CR,CB)
  2051.    if delAD == 2 then
  2052.     BT.changeDispName("delete","press again")
  2053.    end
  2054.    BT.createButton("Clear Selection","Clear Selection",clearSelection,"" ,18,17,34,17,CR,CR,CB)
  2055.   end
  2056.   addressListFilter = {}
  2057.   if filter == "all"  then
  2058.    addressCount = ADC[7] + ADC[9]
  2059.    for i = 1,ADC[7] do
  2060.     addressListFilter[i] = addressList[7][i]
  2061.    end
  2062.    for i = 1,ADC[9] do
  2063.     addressListFilter[ADC[7]+i] = addressList[9][i]
  2064.    end
  2065.   elseif filter == "7 chevrons"  then
  2066.    addressCount = ADC[7]
  2067.    for i = 1,ADC[7] do
  2068.     addressListFilter[i] = addressList[7][i]
  2069.    end
  2070.   elseif filter == "9 chevrons" then
  2071.    addressCount = ADC[9]  
  2072.    for i = 1,ADC[9] do
  2073.     addressListFilter[i] = addressList[9][i]
  2074.    end
  2075.   elseif filter == "favorite" then
  2076.    addressCount = 0
  2077.    for i = 1,ADC[7] do
  2078.     if addressList[7][i]["favorite"] == "true" then
  2079.      addressCount = addressCount + 1
  2080.      addressListFilter[addressCount] = addressList[7][i]
  2081.     end
  2082.    end
  2083.    for i = 1,ADC[9] do
  2084.     if addressList[9][i]["favorite"] == "true" then
  2085.      addressCount = addressCount + 1
  2086.      addressListFilter[addressCount] = addressList[9][i]
  2087.     end
  2088.    end
  2089.   end
  2090.   getSearchResults()
  2091.   if page > 0 then
  2092.    BT.createButton("ppage","<--------",swpage,-1,3,17,15,17,CLBl,CBl,CB)
  2093.   else
  2094.    BT.setF(3,17,15,17,CBG)
  2095.   end
  2096.   if (addressCount > 9*(page+1) and (sorting == "F" or addressListSearch[1] == nil)) or (sorting == "S" and addressCountSearch > 9*(page+1) ) then
  2097.    BT.createButton("npage","-------->",swpage, 1,37,17,49,17,CLBl,CBl,CB)
  2098.   else
  2099.    BT.setF(37,17,49,17,CBG)
  2100.   end
  2101.   xf = 16
  2102.   yf = 4
  2103.   fa = {0,2,4}
  2104.   for i = 1,3 do
  2105.    for j = 0,2 do
  2106.     counter = i+j+fa[i]+9*page
  2107.     if sorting == "F" or addressListSearch[1] == nil then
  2108.      if addressListFilter[1] == nil then
  2109.       BT.createButton("bt1","No addresses found",addAddress,"" ,8,6,45,6,CGR,CBG,CGR)
  2110.       BT.createButton("bt2",'Press "Add address" to add a address',addAddress,"" ,8,7,45,7,CGR,CBG,CGR)
  2111.      end
  2112.      if addressListFilter[counter] ~= nill then  
  2113.       name = addressListFilter[counter]["name"]
  2114.       adr = addressListFilter[counter]["address"]
  2115.       --roundDown(num, idp)
  2116.       local boxColor = COtherBT
  2117.       if selectAddresses[addressListFilter[counter]["address"]] == true then
  2118.        boxColor = CC
  2119.       end
  2120.       BT.createButton("bt"..counter,adr,SelectAddress,counter,3+xf*j,5+yf*(i-1),17+xf*j,7+yf*(i-1),CGR,boxColor,CB)
  2121.       BT.addExtraText("bt"..counter,name)
  2122.      else
  2123.      end
  2124.     else
  2125.      if addressListSearch[counter] ~= nill then  
  2126.       if addressListSearch[1]["name"] ~= "No Matching Addresses Found" then
  2127.        name = addressListSearch[counter]["name"]
  2128.        adr = addressListSearch[counter]["address"]
  2129.        --roundDown(num, idp)
  2130.        local boxColor = COtherBT
  2131.        if selectAddresses[addressListSearch[counter]["address"]] == true then
  2132.         boxColor = CC
  2133.        end
  2134.        BT.createButton("bt"..counter,adr,SelectAddress,counter,3+xf*j,5+yf*(i-1),17+xf*j,7+yf*(i-1),CGR,boxColor,CB)
  2135.        BT.addExtraText("bt"..counter,name)
  2136.       else
  2137.        BT.createButton("bt1","No matching addresses found",searchAddress,"" ,13,6,39,6,CGR,CBG,CGR)
  2138.       end
  2139.      end
  2140.     end
  2141.    end
  2142.   end
  2143.   BT.drawButtons()
  2144.   done2 = false
  2145.   while done2 ~= true do
  2146.    ev,button,x,y = os.pullEvent()
  2147.    if deletePress ~= true then delAD = 1 end
  2148.    --clicks
  2149.    if ev == "terminate" then
  2150.     quitProgram(1)
  2151.     done2 = true
  2152.    elseif ev == "mouse_click" then
  2153.     done2 = BT.checkxy(x,y)
  2154.    --scroll
  2155.    elseif ev == "mouse_scroll" and Settings.ScrollControl then
  2156.     if x >=2 and y >= 4 and x <= 50 and y <= 17 then
  2157.      if button == -1 then
  2158.       if page > 0 then
  2159.        swpage(-1)
  2160.        done2 = true
  2161.       end
  2162.      elseif button == 1 then
  2163.       if (addressCount > 9*(page+1) and (sorting == "F" or addressListSearch[1] == nil)) or (sorting == "S" and addressCountSearch > 9*(page+1) ) then
  2164.        swpage(1)
  2165.        done2 = true
  2166.       end
  2167.      end
  2168.     elseif x >= 19 and x <= 32 and y == 2 and sorting == "F" then
  2169.      filterFunc()
  2170.      done2 = true
  2171.     end
  2172.    --keys
  2173.    elseif ev == "key" and Settings.KeyControl then
  2174.     if button == 203 then
  2175.      if page > 0 then
  2176.       swpage(-1)
  2177.       done2 = true
  2178.      end
  2179.     elseif button == 205 then
  2180.      if (addressCount > 9*(page+1) and (sorting == "F" or addressListSearch[1] == nil)) or (sorting == "S" and addressCountSearch > 9*(page+1) ) then
  2181.       swpage(1)
  2182.       done2 = true
  2183.      end
  2184.     elseif button == 200 and sorting == "F" then
  2185.      button = 2
  2186.      filterFunc()
  2187.      done2 = true
  2188.     elseif button == 208 and sorting == "F" then
  2189.      button = 1
  2190.      filterFunc()
  2191.      done2 = true
  2192.     end
  2193.    end
  2194.    BT.drawButtons()
  2195.   end
  2196.  end
  2197. end
  2198.  
  2199. function Dial()
  2200.  exitv = false
  2201.  ans = {false,""}
  2202.  local pas = ""
  2203.  if Settings.DialingReqPass and Settings.PasswordEnabled then
  2204.   pas = RequestPassword(-1,false,4,true,"Dial")
  2205.  end
  2206.  if pas == "No Pass" or pas == true or Settings.DialingReqPass ~= true or Settings.PasswordEnabled ~= true then
  2207.   BT.deleteAllButtons()
  2208.   Menu = "Dial"
  2209.   if fs.exists(dirData.."Dialer") then
  2210.    f = fs.open(dirData.."Dialer","r")
  2211.    Mode = f.readLine()
  2212.    f.close()
  2213.   else
  2214.    Mode = "list"
  2215.    f = fs.open(dirData.."Dialer","w")
  2216.    f.writeLine("list")
  2217.    f.close()
  2218.   end
  2219.   if Mode == "list" then
  2220.    SAddress = 0
  2221.    list()
  2222.   else
  2223.    
  2224.   end
  2225.   ans = {false,"error: couldn't dial "..SAddress}
  2226.   if exitv == true then
  2227.    ans[2] = " "
  2228.   else
  2229.    if string.len(SAddress) == 7 or string.len(SAddress) == 9 then
  2230.     ThisGate = SG.getStargateAddressString()
  2231.     if SAddress == ThisGate then
  2232.      ans[2] = "you must be very lonely"
  2233.     --elseif SGData.isDialing or SGData.isConnected then
  2234.     --elseif SG.getActivatedChevrons() == 9 then
  2235.     -- ans[2] = "stargate is busy"
  2236.     else
  2237.      ans = SG.dial(SAddress,true)
  2238.     end
  2239.    else
  2240.     ans[2] = "invalid address"
  2241.    end
  2242.   end
  2243.   MainM()
  2244.  else
  2245.  MainM()
  2246.  end
  2247. end
  2248.  
  2249.  
  2250. function getLog()
  2251.  LogList = {}
  2252.  if fs.exists(dirData.."log") then
  2253.   BadData = 0
  2254.   for num,logData in pairs(fs.list(dirData.."log")) do
  2255.    day = ""
  2256.    time = ""
  2257.    logDataTbl = splitWord(logData)
  2258.    local counter = 5
  2259.    while logDataTbl[counter] ~= " " and logDataTbl[counter] ~= "t" and logDataTbl[counter] ~= nil do
  2260.     day = day..logDataTbl[counter]
  2261.     counter = counter + 1
  2262.    end
  2263.    counter = counter + 6
  2264.    while logDataTbl[counter] ~= " " and logDataTbl[counter] ~= nil and logDataTbl[counter] ~= "l"  do
  2265.     time = time..logDataTbl[counter]
  2266.     counter = counter + 1
  2267.    end
  2268.    time = time:sub(1,string.len(time)-1)
  2269.    if day ~= "" and time ~= "" then
  2270.     f = fs.open(dirData.."log/"..logData,"r")
  2271.     LogList[num-BadData] = {}
  2272.     LogList[num-BadData]["time"] = tonumber(time)
  2273.     LogList[num-BadData]["day"] = tonumber(day)
  2274.     LogList[num-BadData]["action"] = f.readLine()
  2275.     f.readLine()
  2276.     LogList[num-BadData]["address"] = f.readLine()
  2277.     f.close()
  2278.    else
  2279.     BadData = BadData + 1
  2280.    end
  2281.   end
  2282.  end
  2283. end
  2284.  
  2285. function deleteLog()
  2286.  fs.delete(dirData.."log")
  2287. end
  2288.  
  2289. function log()
  2290.  --gui
  2291.  Menu = "Log"
  2292.  scroll = 0
  2293.  back = false
  2294.  while back ~= true do
  2295.   getLog()
  2296.   BT.setF(1,1,51,19,CGR)
  2297.   BT.setF(2,2,50,17,CW)
  2298.   BT.setF(2,16,50,17,CGR)
  2299.   BT.setF(3,2,3,15,CGR)
  2300.   BT.setF(2,3,50,3,CGR)
  2301.   BT.setP(2,2,CGR)
  2302.   BT.deleteAllButtons()
  2303.   term.setCursorBlink(false)
  2304.   BT.write("day:",4,2,CB,CW)
  2305.   BT.write("time:",12,2,CB,CW)
  2306.   term.setCursorPos(2,2)
  2307.   BT.createButton(" delete log "," delete log ",deleteLog,"" ,11,17,24,17,CR,CR,CB)
  2308.   BT.createButton(" back "," back ",goBack,"" ,2,17,7,17,CR,CR,CB)
  2309.   leng = #LogList
  2310.   for y = 4,15 do
  2311.    counter = y-3+scroll
  2312.    counter = leng-(y-4+scroll)
  2313.    if (counter+scroll)/2 == roundDown((counter+scroll)/2,0) then logColor = CW else logColor = CLGR end
  2314.    BT.setF(2,y,50,y,logColor) BT.setF(3,2,3,15,CGR)
  2315.    if LogList[counter] ~= nil then
  2316.     BT.write(LogList[counter]["day"],4,y,CB,logColor)
  2317.     if string.len(textutils.formatTime(LogList[counter]["time"],true)) == 5 then
  2318.      BT.write(textutils.formatTime(LogList[counter]["time"],true),12,y,CB,logColor)
  2319.     else
  2320.      BT.write("0"..textutils.formatTime(LogList[counter]["time"],true),12,y,CB,logColor)
  2321.     end
  2322.     BT.write(LogList[counter]["action"].." "..LogList[counter]["address"],19,y,CB,logColor)
  2323.    end
  2324.   end
  2325.   BT.drawButtons()
  2326.   ev,bt,x,y = os.pullEvent()
  2327.   if ev == "terminate" then
  2328.    quitProgram()
  2329.   elseif ev == "mouse_click" then
  2330.    BT.checkxy(x,y)
  2331.   elseif ev == "mouse_scroll" and Settings.ScrollControl then
  2332.    if x >=2 and y >= 4 and x <= 50 and y <= 15 then
  2333.     if bt == -1 and scroll ~= 0 then
  2334.      scroll = scroll - 1
  2335.     elseif bt == 1 and scroll+12 < leng then
  2336.      scroll = scroll + 1
  2337.     end
  2338.    end
  2339.   end
  2340.  end
  2341.  MainM()
  2342. end
  2343.  
  2344. function ReDial()
  2345.  if not(SGData.isConnected) or not(SGData.isDialing) then
  2346.   return(SG.dial(LastAddress,true))
  2347.  else
  2348.   return("busy")
  2349.  end
  2350. end
  2351.  
  2352. function Iris()
  2353.  if SG.getIrisType() ~= nil then
  2354.   if SG.getIrisState() == "Closed" or SG.getIrisState() == "Closing" then
  2355.    if Settings.OpeningIrisReqPas and Settings.PasswordEnabled then
  2356.     if RequestPassword(-1,false,4,true,"Open Iris") == true then
  2357.      SG.openIris()
  2358.       DispMsg("Opening iris",CB)
  2359.      MainM()
  2360.     else
  2361.      MainM()
  2362.     end
  2363.    else
  2364.     SG.openIris()
  2365.     DispMsg("Closing iris",CB)
  2366.    end
  2367.   else
  2368.    SG.closeIris()
  2369.   end
  2370.  end
  2371. end
  2372.  
  2373.  
  2374. function bgProces()
  2375.  while true do
  2376.   if Menu == "Main" or Menu == "lockdown" or (Menu == "RequestPass" and OldMenu == "Main") then
  2377.    --timestat
  2378.    --stat
  2379.    if Menu ~= "lockdown" then
  2380.     toggleMiddleButton()
  2381.    end
  2382.   end
  2383.   sleep(0.2)
  2384.  end
  2385. end
  2386.  
  2387. function click()
  2388.  while true do
  2389.   ev,button,x,y = os.pullEvent("mouse_click")
  2390.   if ev == "terminate" then
  2391.    Menu = "lockdown"
  2392.    quitProgram(4)
  2393.    MainM()
  2394.   elseif ev == "mouse_click" then
  2395.    BT.checkxy(x,y)
  2396.   end
  2397.  end
  2398. end
  2399.  
  2400. function chevronLock()
  2401.  while true do
  2402.   event,address = os.pullEvent("sgOutgoing")
  2403.   event,Chevron = os.pullEvent("sgChevronEncode")
  2404.  
  2405.  end
  2406. end
  2407.  
  2408. function sgIncoming()
  2409.  oldAddr = SGData.Connection.Address
  2410.  while true do
  2411.   if Menu == "Main" or Menu == "lockdown" then
  2412.    if SGData.Connection.Address ~= oldAddr then
  2413.      BT.setTextColor(CB)
  2414.      BT.setBackgroundColor(CW)
  2415.      term.setCursorPos(11,4)
  2416.      
  2417.      if SGData.Connection.Address ~= nil and (SGData.isConnected or SGData.isDialing)  then
  2418.       BT.write(SGData.Connection.Address.."         ")
  2419.      else
  2420.       BT.write("none         ")
  2421.      end
  2422.     --DispMsg("incoming "..SGData.Connection.Address,CB)
  2423.    end
  2424.   end
  2425.   sleep(0.2)
  2426.  end
  2427. end
  2428.  
  2429. function chevronLock2()
  2430.  oldC = SGData.Connection.Chevron oldStat = SGData.Status
  2431.  while true do
  2432.   if Menu == "Main" or Menu == "lockdown" or oldC ~= SGData.Connection.Chevron or oldStat ~= SGData.Status then
  2433.    oldC = SGData.Connection.Chevron oldStat = SGData.Status
  2434.    if SGData.isDialing then
  2435.     if SGData.Connection.InOrOut == "incoming" then
  2436.      DispMsg("incoming "..SGData.Connection.Address,CB)
  2437.     else
  2438.      if SGData.Connection.Chevron ~= nil then
  2439.       DispMsg("chevron "..SGData.Connection.Chevron.." encoded",CB)
  2440.      end
  2441.     end
  2442.    elseif SGData.isConnected  then
  2443.     if SGData.Connection.InOrOut == "incoming" then
  2444.      --add to log
  2445.      DispMsg("dialed form: "..SGData.Connection.Address,CB)
  2446.     else
  2447.      if SGData.Connection.Chevron ~= nil then
  2448.       DispMsg("chevron "..SGData.Connection.Chevron.." locked",CB)
  2449.      end
  2450.     end
  2451.    end
  2452.   end
  2453.   sleep(0.4)
  2454.  end
  2455. end
  2456.  
  2457. function RecEvents()
  2458.  oldAddr = SGData.Connection.Address EV = {}
  2459.  while true do
  2460.   RecEvents = {}
  2461.   ID,RecEvents = rednet.receive(protocol.."Event")
  2462.   if RecEvents["event"] == "sgIncoming" then
  2463.    EV["data"] = RecEvents["data"] EV["event"] = RecEvents["event"] Chevron = RecEvents["chevron"]
  2464.    if Menu == "Main" or Menu == "lockdown" then
  2465.     if SGData.isDialing then
  2466.      DispMsg("chevron "..Chevron.." encoded",CB)
  2467.     elseif SGData.isConnected  then
  2468.      DispMsg("chevron "..Chevron.." locked",CB)
  2469.     end
  2470.    end
  2471.   elseif RecEvents["event"] == "sgOutgoing" then
  2472.    EV["data"] = RecEvents["data"] EV["event"] = RecEvents["event"] Chevron = RecEvents["chevron"]
  2473.    if Menu == "Main" or Menu == "lockdown" then
  2474.     if SGData.isDialing then
  2475.      DispMsg("chevron "..Chevron.." encoded",CB)
  2476.     elseif SGData.isConnected  then
  2477.      DispMsg("chevron "..Chevron.." locked",CB)
  2478.     end
  2479.    end
  2480.   elseif RecEvents["event"] == "sgWormholeOpening" then
  2481.    if not(fs.exists(dirData.."log/day="..os.day().." time="..os.time()..".log")) then
  2482.     f = fs.open(dirData.."log/day="..os.day().." time="..os.time()..".log","w")
  2483.     f.writeLine(EV["event"]..":")
  2484.     f.writeLine("Address:")
  2485.     f.writeLine(EV["data"])
  2486.     f.close()
  2487.    end
  2488.   end
  2489.  end
  2490. end
  2491.  
  2492.  
  2493. function sgStayOpen()
  2494.  while true do
  2495.   timer = 0
  2496.   os.pullEvent("sgWormholeOpening")
  2497.   while SGData.isConnected do
  2498.    timer = timer + 1 --303 sec to stay open
  2499.    sleep(1)
  2500.   end
  2501.  end
  2502. end
  2503.  
  2504. function clearMSG()
  2505.  clearMSGNum = 0
  2506.  while true do
  2507.   if curMSG ~= newMSG then
  2508.    curMSG = newMSG clearMSGNum = 0
  2509.   elseif clearMSGNum == 4 then
  2510.    if Menu == "Main" or Menu == "lockdown" or (Menu == "RequestPass" and OldMenu == "Main") then
  2511.     BT.setBackgroundColor(CBG)
  2512.     term.setCursorPos(11,7)
  2513.     BT.write("                               ")
  2514.    end
  2515.  --  if mon["DispMsg"] ~= nil then
  2516.   --  mon["DispMsg"].setBackgroundColor(CBG)
  2517.   --  mon["DispMsg"].setCursorPos(1,2)
  2518.   --  mon["DispMsg"].write("                                    ")
  2519.   -- end
  2520.   else
  2521.    clearMSGNum = clearMSGNum + 1
  2522.   end
  2523.   sleep(1)
  2524.  end
  2525. end
  2526.  
  2527.  
  2528. --TODO: needs some editing
  2529. function StargateMon()
  2530.  mon = {}
  2531.  mon["StargatePicture"] = {}
  2532.  while true do
  2533.  --mon["StargatePicture"][1] = peripheral.wrap("monitor_1")
  2534.   if mon["StargatePicture"][1] ~= nil or mon["StargatePicture"][2] ~= nil then
  2535.    local function setP(x,y,bgc)
  2536.     for i = 1,#mon["StargatePicture"] do
  2537.      if mon["StargatePicture"][i] ~= nil then
  2538.       mon["StargatePicture"][i].setTextScale(0.8)
  2539.       mon["StargatePicture"][i].setBackgroundColor(bgc)
  2540.       mon["StargatePicture"][i].setCursorPos(x,y)
  2541.       mon["StargatePicture"][i].write(" ")
  2542.      end
  2543.     end
  2544.    end
  2545.  
  2546.    local function setF(x1,y1,x2,y2,bgc)
  2547.     for i = 1,#mon["StargatePicture"] do
  2548.      if mon["StargatePicture"][i] ~= nil then
  2549.       mon["StargatePicture"][i].setBackgroundColor(bgc)
  2550.       for x = x1,x2 do
  2551.        for y = y1,y2 do
  2552.         mon["StargatePicture"][i].setCursorPos(x,y)
  2553.         mon["StargatePicture"][i].write(" ")
  2554.        end
  2555.       end
  2556.      end
  2557.     end
  2558.    end
  2559.  
  2560.    local function drawChevrons(numCEnc,typeMonSG)
  2561.     if typeMonSG == "SG1" or typeMonSG == nil then
  2562.      ColAct = CR
  2563.      ColDeAct = CO
  2564.     elseif typeMonSG == "SGA" then
  2565.      ColAct = CC
  2566.      ColDeAct = CBl
  2567.     elseif typeMonSG == "SGWR" then
  2568.      ColAct = CM
  2569.      ColDeAct = CP  
  2570.     elseif type == "SGNOX" then
  2571.      ColAct = CL
  2572.      ColDeAct = CG
  2573.     end
  2574.     --if statCOn == true then
  2575.     -- statC = ColAct
  2576.     --else
  2577.     -- statC = ColDeAct
  2578.     --end
  2579.     chevronFunc = {x = {23,25,23,7,5,7,18,12,15},y = {5,10,15,15,10,5,17,17,3}}
  2580.     --chevron1
  2581.     --chevronFunc[1] = setP(23,5,statC)
  2582.     --chevron2
  2583.     --chevronFunc[2] = setP(25,10,statC)
  2584.     --chevron3
  2585.     --chevronFunc[3] = setP(23,15,statC)
  2586.     --chevron6
  2587.     --chevronFunc[4] = setP(7,15,statC)
  2588.     --chevron7
  2589.     --chevronFunc[5] = setP(5,10,statC)
  2590.     --chevron8
  2591.     --chevronFunc[6] = setP(7,5,statC)
  2592.     --chevron4
  2593.     --chevronFunc[7] = setP(18,17,statC)
  2594.     --chevron5
  2595.     --chevronFunc[8] = setP(12,17,statC)
  2596.     --chevron9
  2597.     --chevronFunc[9] = setP(15,3,statC)
  2598.     for num = numCEnc,8 do
  2599.      setP(chevronFunc.x[num+1],chevronFunc.y[num+1],ColDeAct)
  2600.     end
  2601.     for num = 1,numCEnc do
  2602.      setP(chevronFunc.x[num],chevronFunc.y[num],ColAct)
  2603.     end
  2604.    end
  2605.  
  2606.    local function drawStargateFrame(typeMonSG)
  2607.     --line1
  2608.     setF(12,2,18,2,CGR)
  2609.     --line2
  2610.     setF(9,3,21,3,CGR)
  2611.     --line3
  2612.     setF(7,4,23,4,CGR)
  2613.     --line4
  2614.     setF(6,5,9,5,CGR)
  2615.     setF(21,5,24,5,CGR)
  2616.     --line5
  2617.     setF(5,6,8,6,CGR)
  2618.     setF(22,6,25,6,CGR)
  2619.     --line6
  2620.     setF(4,7,7,7,CGR)
  2621.     setF(23,7,26,7,CGR)
  2622.     --line7
  2623.     setF(4,8,6,8,CGR)
  2624.     setF(24,8,26,8,CGR)
  2625.     --line8
  2626.     setF(4,9,6,9,CGR)
  2627.     setF(24,9,26,9,CGR)
  2628.     --line9
  2629.     setF(4,10,6,10,CGR)
  2630.     setF(24,10,26,10,CGR)
  2631.     --line10
  2632.     setF(4,11,6,11,CGR)
  2633.     setF(24,11,26,11,CGR)
  2634.     --line11
  2635.     setF(4,12,6,12,CGR)
  2636.     setF(24,12,26,12,CGR)
  2637.     --line12
  2638.     setF(4,13,7,13,CGR)
  2639.     setF(23,13,26,13,CGR)
  2640.     --line13
  2641.     setF(5,14,8,14,CGR)
  2642.     setF(22,14,25,14,CGR)
  2643.     --line14
  2644.     setF(6,15,9,15,CGR)
  2645.     setF(21,15,24,15,CGR)
  2646.     --line15
  2647.     setF(7,16,23,16,CGR)
  2648.     --line16
  2649.     setF(8,17,22,17,CGR)
  2650.     --line16
  2651.     setF(10,18,20,18,CGR)
  2652.     drawChevrons(0,typeMonSG)
  2653.    end
  2654.  
  2655.    local function drawWormhole()
  2656.     setF(10,5,20,5,CBl)
  2657.     setF(9,6,21,6,CBl)
  2658.     setF(8,7,22,7,CBl)
  2659.     setF(7,8,23,12,CBl)
  2660.     setF(8,13,22,13,CBl)
  2661.     setF(9,14,21,14,CBl)
  2662.     setF(10,15,20,15,CBl)
  2663.    end
  2664.    
  2665.    local function drawTable()
  2666.     setF(32,1,32,19,CGR)
  2667.     setF(36,1,36,19,CGR)
  2668.     for i = 0,9 do
  2669.      setF(33,1+2*i,35,1+2*i,CGR)
  2670.     end
  2671.    end
  2672.    
  2673.    changeMonSG = nill
  2674.    oldChangeMonSG = "hi"
  2675.    typeGateMonSG = "SG1"
  2676.    while true do
  2677.     if changeMonSG ~= oldChangeMonSG then
  2678.      oldChangeMonSG = changeMonSG
  2679.      for i = 1,#mon["StargatePicture"] do
  2680.       if mon["StargatePicture"][i] ~= nil then
  2681.        mon["StargatePicture"][i].setBackgroundColor(CB)
  2682.        mon["StargatePicture"][i].clear()
  2683.       end
  2684.      end
  2685.     end
  2686.     --needs to be changed
  2687.     drawTable()
  2688.     if SG.getActivatedChevrons() ~= 0 and SG.getActivatedChevrons() ~= 9 then
  2689.      changeMonSG = "dialing"
  2690.      drawStargateFrame(typeGateMonSG)
  2691.      drawChevrons(SG.getActivatedChevrons(),typeGateMonSG)
  2692.     elseif SG.getActivatedChevrons() == 9 then
  2693.      changeMonSG = "connected"
  2694.      drawStargateFrame(typeGateMonSG)
  2695.      drawChevrons(9,typeGateMonSG)
  2696.      drawWormhole()
  2697.     else
  2698.      changeMonSG = "idel"
  2699.      drawStargateFrame(typeGateMonSG)
  2700.     end
  2701.     sleep(0.2)  
  2702.    end
  2703.    else
  2704.     sleep(1)
  2705.   end
  2706.  end
  2707. end
  2708.  
  2709. function timeStat()
  2710.  while true do
  2711.   --timer
  2712.   OSTime = textutils.formatTime(os.time(),true)
  2713.   WTime = OSTime --when you can change time for like a age
  2714.   if string.len(WTime) < 5 then WTime = "0"..WTime end
  2715.  -- if mon["DispMsg"] ~= nil then
  2716.  --  mon["DispMsg"].setBackgroundColor(CBG)
  2717.  --  mon["DispMsg"].setTextColor(CB)
  2718.  --  mon["DispMsg"].setCursorPos(1,1)
  2719.  --  mon["DispMsg"].write("time: "..WTime)
  2720.  -- end
  2721.   if Menu == "Main" or Menu == "lockdown" or (Menu == "RequestPass" and OldMenu == "Main") then
  2722.    term.setCursorPos(2,2)
  2723.    BT.setBackgroundColor(CBG)
  2724.    BT.setTextColor(CB)
  2725.    BT.write("Time: "..WTime,2,2,CB,CBG)
  2726.    -- term.setCursorPos(26,2)
  2727.    -- BT.write("day: "..os.day())
  2728.    OSDay = os.day()
  2729.    WDay = OSDay --when you can change day for like a age
  2730.    --term.setCursorPos(50-string.len("day: "..WDay),2)
  2731.    BT.write("Day: "..WDay,16,2,CB,CBG)
  2732.    BT.drawButtons()
  2733.   end
  2734.   sleep(0.4)
  2735.  end
  2736. end
  2737.  
  2738. function SGStat()
  2739.  while true do
  2740.    --sg
  2741.     --if SGData.isDialing then
  2742.     if SG.getActivatedChevrons() ~= 0 and SG.getActivatedChevrons() ~= 9 then
  2743.      c = CO
  2744.      sgstat = "dialing"
  2745.     --elseif SGData.isConnected then
  2746.     elseif SG.getActivatedChevrons() == 9 then
  2747.      c = CG
  2748.      sgstat = "Connected"
  2749.     else
  2750.      c = CR  
  2751.      sgstat = "idle"
  2752.     end
  2753.     if Menu == "Main" or Menu == "lockdown" or (Menu == "RequestPass" and OldMenu == "Main") then
  2754.      term.setCursorBlink(false)
  2755.      term.setCursorPos(2,3)
  2756.      BT.setBackgroundColor(CBG)
  2757.      BT.setTextColor(CB)
  2758.      BT.write("Status: ")
  2759.      term.setCursorPos(2,4)
  2760.      BT.write("Chevrons encoded: ")
  2761.      BT.write(SG.getActivatedChevrons(),20,4)
  2762.      BT.write("Address: ",2,5)
  2763.      BT.setF(11,5,20,5)
  2764.      if SGbg.Address ~= nil then
  2765.       local Chevrons = SG.splitAdress(SGbg.Address)
  2766.       BT.write(SGbg.Address,11,5,CLGR,CBG)
  2767.       --BT.write(SG.getActivatedGlyphs(),11,5,CB,CBG)
  2768.       --delete
  2769.       for i = 1,SG.getActivatedChevrons() do
  2770.        BT.write(Chevrons[i],11+(i-1),5,CB,CBG)
  2771.       end
  2772.       --delete
  2773.      else
  2774.       --BT.write(SG.getActivatedGlyphs(),11,5,CB,CBG)
  2775.      end
  2776.      term.setCursorBlink(false)
  2777.      BT.setTextColor(c)
  2778.      term.setCursorPos(10,3)
  2779.      BT.write(sgstat.."         ")
  2780.      term.setCursorPos(26,2)
  2781.      BT.setTextColor(CB)
  2782.      BT.write("Iris: ".."none")
  2783.      --BT.write("Iris: "..SG.getIrisState(),26,2)
  2784.      --if SG.getIrisState() ~= nil then
  2785.       --BT.write("Health: "..SG.getIrisHealth(),26,3)
  2786.      --end
  2787.     end
  2788.   sleep(0.2)
  2789.  end
  2790. end
  2791.  
  2792.  
  2793. function checkID(ID)
  2794.  return(true)
  2795. end
  2796.  
  2797. function RemoteControl()
  2798.  while true do
  2799.   ID,Data = rednet.receive()
  2800.   if Settings.RemoteControl and type(Data) == "table" then
  2801.    if Data["command"] == "Verify" then
  2802.     rednet.send(ID,checkID(ID))
  2803.    elseif Data["command"] == "disconnect" then
  2804.     if checkID(ID) then
  2805.      SG.disconnect()
  2806.      SG.deactivateAllChevrons()
  2807.     end
  2808.    elseif Data["command"] == "deactivateLastChevron" then
  2809.     if checkID(ID) then
  2810.      SG.deactivateLastChevrons()
  2811.     end
  2812.    elseif Data["command"] == "dial" then
  2813.     if checkID(ID) then
  2814.      SG.dial(Data["Data1"],Data["Data2"])
  2815.     end
  2816.    --more soon
  2817.    end
  2818.   end
  2819.   if type(Data) == "table" then
  2820.    if Data["command"] == "msg" then
  2821.    
  2822.    end
  2823.   end  
  2824.  end
  2825. end
  2826.  
  2827. function sgStat1()
  2828. -- parallel.waitForAll(chevronLock,sgIncoming,sgStayOpen)
  2829.  while true do
  2830.   sleep(1)
  2831.  end
  2832. end
  2833. --StargateMon
  2834.  
  2835. function stargateStats()
  2836.  parallel.waitForAll(SGStat,clearMSG,timeStat,SG.gateProcces,RemoteControl,StargateMon)
  2837.  while true do
  2838.   sleep(1)
  2839.  end
  2840. end
  2841.  
  2842. function ENDPROGRAMLOOP()
  2843.  ENDPROGRAM = false
  2844.  while ENDPROGRAM ~= true do
  2845.   sleep(0.4)
  2846.  end
  2847.  BT.clearAndDraw()
  2848.  BT = nil
  2849.  os.unloadAPI(dirProg.."Button")
  2850.  term.setCursorPos(1,1)
  2851. end
  2852.  
  2853. --,sgStat1,clearMSG
  2854. --RecEvents,getStargateData
  2855.  
  2856. MainM()
  2857. parallel.waitForAny(click,bgProces,stargateStats,ENDPROGRAMLOOP)
  2858. --sgStat1
  2859.  
  2860.  
  2861. --[[This is the end off the program]]--
  2862.  
  2863. --[[Button API (+ more)]]--
  2864.  
  2865. buttons = {}
  2866.  
  2867. function roundDown(num, idp)
  2868.   local mult = 10^(idp or 0)
  2869.   return math.floor(num * mult ) / mult
  2870. end
  2871.  
  2872. --Frame[y][x]["bgColor"]
  2873. --Frame[y][x]["textColor"]
  2874. --Frame[y][x]["text"]
  2875.  
  2876. function clear()
  2877.  BackgroundColor = colors.black
  2878.  TextColor = colors.white
  2879.  Frame = {}
  2880.  OldFrame = {}
  2881.  for y = -50,50 do
  2882.   Frame[y] = {}
  2883.   OldFrame[y] = {}
  2884.   for x = -100,100 do
  2885.    Frame[y][x] = {}
  2886.    Frame[y][x]["bgColor"] = colors.black
  2887.    Frame[y][x]["textColor"] = colors.white
  2888.    Frame[y][x]["text"] = " "
  2889.    OldFrame[y][x] = {}
  2890.    OldFrame[y][x]["bgColor"] = " "
  2891.    OldFrame[y][x]["textColor"] = " "
  2892.    OldFrame[y][x]["text"] = " "
  2893.   end
  2894.  end
  2895.  
  2896. end
  2897.  
  2898. clear()
  2899.  
  2900. function clearAndDraw()
  2901.  clear()
  2902.  draw()
  2903. end
  2904.  
  2905. function getFrame()
  2906.  return(Frame)
  2907. end
  2908. function getOFrame()
  2909.  return(OldFrame)
  2910. end
  2911.  
  2912. function draw()
  2913.  for y = 1,19 do
  2914.   for x = 1,51 do
  2915.    if OldFrame[y][x]["bgColor"] ~= Frame[y][x]["bgColor"] or OldFrame[y][x]["textColor"] ~= Frame[y][x]["textColor"] or OldFrame[y][x]["text"] ~= Frame[y][x]["text"] then
  2916.     term.setCursorPos(x,y)
  2917.     term.setBackgroundColor(Frame[y][x]["bgColor"])
  2918.     term.setTextColor(Frame[y][x]["textColor"])
  2919.     term.write(Frame[y][x]["text"])
  2920.     OldFrame[y][x]["bgColor"] = Frame[y][x]["bgColor"]
  2921.     OldFrame[y][x]["textColor"] = Frame[y][x]["textColor"]
  2922.     OldFrame[y][x]["text"] = Frame[y][x]["text"]
  2923.    end
  2924.   end
  2925.  end  
  2926.  
  2927. end
  2928.  
  2929. function setBackgroundColor(bgc)
  2930.  BackgroundColor = bgc
  2931. end
  2932. function setTextColor(tc)
  2933.  TextColor = tc
  2934. end
  2935.  
  2936.  
  2937. function setP(x,y,bgc)
  2938.  if bgc ~= nil and bgc ~= "" then
  2939.   Frame[y][x]["bgColor"] = bgc
  2940.  else
  2941.   Frame[y][x]["bgColor"] = BackgroundColor
  2942.  end
  2943.  Frame[y][x]["text"] = " "
  2944. end
  2945.  
  2946. function setF(x1,y1,x2,y2,bgc)
  2947.  for x = x1,x2 do
  2948.   for y = y1,y2 do
  2949.    if bgc ~= nil and bgc ~= "" then
  2950.     Frame[y][x]["bgColor"] = bgc
  2951.    else
  2952.     Frame[y][x]["bgColor"] = BackgroundColor
  2953.    end
  2954.    Frame[y][x]["text"] = " "
  2955.   end
  2956.  end
  2957. end
  2958.  
  2959. function write(Text,x,y,textColor,bgc)
  2960.  local trash = 0
  2961.  if x == nill or x == "" then
  2962.   x,trash = term.getCursorPos()
  2963.  else
  2964.   x = roundDown(x, 0)
  2965.  end
  2966.  if y == nill or y == "" then
  2967.   trash,y = term.getCursorPos()
  2968.  else
  2969.   y = roundDown(y, 0)
  2970.  end
  2971.  
  2972.  local tblText = {}
  2973.  for i = 1,string.len(Text) do
  2974.   tblText[i] = string.reverse(string.sub(string.reverse(string.sub(Text,i)),string.len(Text)-i+1))
  2975.  end
  2976.  
  2977.  for i = 0,string.len(Text)-1 do
  2978.   if textColor ~= nil and textColor ~= "" then
  2979.    Frame[y][x+i]["textColor"] = textColor
  2980.   else
  2981.    Frame[y][x+i]["textColor"] = TextColor
  2982.   end
  2983.   if bgc ~= nil and bgc ~= "" then
  2984.    Frame[y][x+i]["bgColor"] = bgc
  2985.   else
  2986.    Frame[y][x+i]["bgColor"] = BackgroundColor
  2987.   end
  2988.   Frame[y][x+i]["text"] = tblText[i+1]
  2989.  end
  2990. end
  2991.  
  2992.  
  2993. --removes all button's
  2994. function deleteAllButtons()
  2995.  buttons = {}
  2996.  SlideBars = {}
  2997. end
  2998.  
  2999. --name off the button
  3000. --removes a button
  3001. function deleteButton(name)
  3002.  buttons[name] = {}
  3003. end
  3004.  
  3005. --name off the button, the new name
  3006. --changes the displayed name off a button
  3007. function changeDispName(name,newDispName)
  3008.  if buttons[name] ~= nil then
  3009.   if buttons[name]["dispname"] then
  3010.    buttons[name]["dispname"] = newDispName
  3011.   end
  3012.  end
  3013. end
  3014.  
  3015. --name off the button
  3016. --returns the displayed name off a button
  3017. function getDispName(name)
  3018.  return(buttons[name]["dispname"])
  3019. end
  3020.  
  3021. --name off the button, number button, the new color
  3022. --sets the color off a button
  3023. function setColor(name,num,newcolor)
  3024.  if num == 1 then chgn = "colorOn"
  3025.  elseif num == 2 then chgn = "colorOff"
  3026.  elseif num == 3 then chgn = "colorText" end
  3027.  buttons[name][chgn] = newcolor
  3028. end
  3029.  
  3030. --name off the button, number button
  3031. --returns the color off a button in numbers
  3032. function getColor(name,num)
  3033.  if num == 1 then chgn = "colorOn"
  3034.  elseif num == 2 then chgn = "colorOff"
  3035.  elseif num == 3 then chgn = "colorText" end
  3036.  return(buttons[name][chgn])
  3037. end
  3038.  
  3039. function addExtraText(name,text)
  3040.  buttons[name]["extraText"] = text
  3041. end
  3042.  
  3043. function setButton(name,stat)
  3044.  if buttons[name] ~= nil then
  3045.   if buttons[name]["active"] ~= nil then
  3046.    buttons[name]["active"] = stat
  3047.   end
  3048.  end
  3049. end
  3050.  
  3051. function toggleButton(name)
  3052.  if buttons[name] ~= nil then
  3053.   if buttons[name]["active"] ~= nil then
  3054.    buttons[name]["active"] = not(buttons[name]["active"])
  3055.   end
  3056.  end
  3057. end
  3058.  
  3059. function flashButton(name,delay)
  3060.  if delay == nil then
  3061.   delay = 0.16
  3062.  elseif type(delay) ~= "number" then
  3063.   if delay <= 0 or delay > 4 then
  3064.    error ("'delay' not valid")
  3065.   end
  3066.  end
  3067.  toggleButton(name)
  3068.  sleep(delay)
  3069.  toggleButton(name)
  3070. end
  3071.  
  3072. function createButton(name,dtxt,func,par,x1,y1,x2,y2,c1,c2,c3)
  3073.  --button name, display name, fucntion,parameter,x1,y1,x2,y2
  3074.  --color On, color Off, text color
  3075.  if c1 == nill then
  3076.   c1 = colors.lime
  3077.  end
  3078.  if c2 == nill then
  3079.   c2 = colors.red
  3080.  end
  3081.  if c3 == nill then
  3082.   c3 = colors.white
  3083.  end
  3084.  buttons[name] = {}
  3085.  buttons[name]["active"] = false
  3086.  buttons[name]["dispname"] = dtxt
  3087.  buttons[name]["function"] = func
  3088.  buttons[name]["parameter"] = par
  3089.  buttons[name]["x1"] = x1
  3090.  buttons[name]["y1"] = y1
  3091.  buttons[name]["x2"] = x2
  3092.  buttons[name]["y2"] = y2
  3093.  buttons[name]["colorOn"] = c1
  3094.  buttons[name]["colorOff"] = c2
  3095.  buttons[name]["colorText"] = c3
  3096. end
  3097.  
  3098. function fill(text,color,cData)
  3099.   setTextColor(cData["colorText"])
  3100.   setBackgroundColor(color)
  3101.   local yspot = math.floor((cData["y1"] + cData["y2"]) /2)
  3102.   local xspot = math.floor((cData["x2"] - cData["x1"] - string.len(text)) /2) +1
  3103.   setF(cData["x1"],cData["y1"],cData["x2"],cData["y2"],color)
  3104.   write(text,cData["x1"]+xspot, yspot,cData["colorText"],color)
  3105.   --setBackgroundColor(colors.black)
  3106. end
  3107.  
  3108. function fill2(text,color,cData)
  3109.   setTextColor(cData["colorText"])
  3110.   setBackgroundColor(color)
  3111.   local yspot = math.floor((cData["y1"] + cData["y2"]) /2)
  3112.   local xspot = math.floor((cData["x2"] - cData["x1"] - string.len(text)) /2) +1
  3113.   for j = cData["y1"], cData["y2"] do
  3114.    --term.setCursorPos(cData["x1"], j)
  3115.    if j == yspot then
  3116.     for k = 0, cData["x2"]-cData["x1"]-string.len(text)+1 do
  3117.      if k == xspot then
  3118.       write(text,cData["x1"]+k, j)
  3119.      else
  3120.       if cData["x1"]+k >= xspot and cData["x1"]+k <= xspot+string.len(text) then
  3121.        a = string.len(text)-1
  3122.       else
  3123.        a = 0
  3124.       end
  3125.       write(" ",cData["x1"]+k+a, j)
  3126.      end
  3127.     end  
  3128.    else
  3129.     for i = cData["x1"], cData["x2"] do
  3130.      setP(i,j,color)
  3131.     end
  3132.    end
  3133.   end
  3134.   --setBackgroundColor(colors.black)
  3135. end
  3136.  
  3137. function extraText(text,currColor,data)
  3138.  x = roundDown(data["x1"]+(data["x2"]-data["x1"])/2-string.len(text)/2+1,0)
  3139.  write(text,x,data["y2"],TextColor,currColor)
  3140. end
  3141.  
  3142. function drawButtons()
  3143.  local currColor
  3144.  for name,data in pairs(buttons) do
  3145.   if data["active"] == true then
  3146.    currColor = data["colorOn"]
  3147.   else
  3148.    currColor = data["colorOff"]
  3149.   end
  3150.   txt = data["dispname"]
  3151.   fill(txt, currColor, data)
  3152.   if data["extraText"] ~= nill then
  3153.    extraTxt = data["extraText"]
  3154.    extraText(extraTxt,currColor,data)
  3155.   end
  3156.  end
  3157.  DrawSlideBars()
  3158. -- draw()
  3159. end
  3160.  
  3161. function checkxy(x, y)
  3162.  for name, data in pairs(buttons) do
  3163.   if y>=data["y1"] and  y <= data["y2"] then
  3164.    if x>=data["x1"] and x<= data["x2"] then
  3165.     if data["parameter"] == "" then
  3166.      data["function"]()
  3167.     else
  3168.      data["function"](data["parameter"])
  3169.     end
  3170.     return true
  3171.    end
  3172.   end
  3173.  end
  3174.  return false
  3175. end
  3176.  
  3177.  
  3178. --sliders
  3179. SlideBars = {}
  3180.  
  3181. function checkSlideBar(x,y)
  3182.  for NameSlider,Value in pairs(SlideBars) do
  3183.   if x >= Value["xSB"]+6 and x <= Value["xSB"]+9 and y == Value["ySB"] then
  3184.    SlideBars[NameSlider]["Boolean"] = not(SlideBars[NameSlider]["Boolean"])
  3185.   end
  3186.  end
  3187. end
  3188.  
  3189. function DrawSlideBars()
  3190.  for NameSlider,Value in pairs(SlideBars) do
  3191.   setF(Value["xSB"],Value["ySB"],Value["xSB"]+12+string.len(Value.dispName),Value["ySB"],colors.white,colors.black)
  3192.   setF(Value["xSB"]+6,Value["ySB"],Value["xSB"]+9,Value["ySB"],colors.lightGray,colors.black)
  3193.   if Value.Boolean then
  3194.    write("true",Value["xSB"],Value["ySB"],colors.black,colors.white)
  3195.    setP(Value["xSB"]+6,Value["ySB"],colors.gray,colors.black)
  3196.   else
  3197.    write("false",Value["xSB"],Value["ySB"],colors.black,colors.white)
  3198.    setP(Value["xSB"]+9,Value["ySB"],colors.gray,colors.black)
  3199.   end
  3200.   write(Value.dispName,Value["xSB"]+12,Value["ySB"],colors.black,colors.white)
  3201.  end
  3202.  draw()
  3203. end
  3204.  
  3205. function DeleteAllSliders()
  3206.  SlideBars = {}
  3207. end
  3208.  
  3209. function getSliderValue(Name)
  3210.  return(SlideBars[Name]["Boolean"])
  3211. end
  3212.  
  3213. function setSliderValue(Name,value)
  3214.  SlideBars[Name]["Boolean"] = value
  3215. end
  3216.  
  3217. function CreateSildeBar(x,y,name,dispName,value)
  3218.  if SlideBars[name] == nil then
  3219.   if type(value) == "boolean" and type(x) == "number" and type(y) == "number" then
  3220.    SlideBars[name] = {}
  3221.    SlideBars[name]["xSB"] = x
  3222.    SlideBars[name]["ySB"] = y
  3223.    SlideBars[name]["Boolean"] = value
  3224.    SlideBars[name]["dispName"] = dispName
  3225.   end
  3226.  end
  3227. end
  3228.  
  3229. --[[This is the end off the API]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement