Advertisement
Virgilcore

RobcOS Termlink

Jun 9th, 2022
881
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.85 KB | None | 0 0
  1. os.pullEvent = os.pullEventRaw
  2. --=================================
  3. --FalloutLock created by Savior67
  4. --Do not Redistribute
  5. --=================================
  6. --CONFIG OPTIONS-------------------
  7. local output = "bottom"   --output side
  8. local wordFile = "codes"  --where the words are stored
  9. local textColor = colors.green
  10. local attempts = 4        --how many attempts to guess the word?
  11. local permLockout = true  --lock the terminal permanently?
  12. local script = "RobcOS"
  13. --only way to enter if permLockout==true is with disk
  14. --then delete lock_status, or edit and set Locked=false
  15. ----------------------------------
  16.  
  17. local usedWords = {}
  18. local status = {}
  19.  
  20. --check for advanced monitor
  21. if not term.isColor() then
  22.   term.clear()
  23.   print("ERROR CODE 0x357C5001")
  24.   print("Bad Sectors Found In Boot Block. Terminal Error.")
  25.   return
  26. end
  27. --check for wordFile
  28. if not fs.exists(wordFile) then
  29.   print("The word file \""..wordFile.."\" is not populated.")
  30.   return
  31. end
  32. --centers the x value of a string
  33. function center(str,y)
  34.   local maxX,maxY = term.getSize()
  35.   term.setCursorPos(math.floor(maxX/2)-math.floor(string.len(str)/2),y)
  36.   term.write(str)
  37. end
  38. --locks the terminal
  39. function lockout()
  40.   term.clear()
  41.   term.setTextColor(textColor)
  42.   center("TERMINAL LOCKED",8)
  43.   center("PLEASE CONTACT AN ADMINISTRATOR",10)
  44.   local h = fs.open("lock_status","w")
  45.   h.writeLine("Locked: true")
  46.   h.close()
  47.   while true do
  48.     sleep(1000)
  49.   end
  50.   os.reboot()
  51. end
  52. --check for permLock
  53. if not fs.exists("lock_status") then
  54.   local h = fs.open("lock_status","w")
  55.   h.writeLine("Locked: false")
  56.   h.close()
  57. else
  58.   local h = fs.open("lock_status","r")
  59.   local isLocked = string.sub(h.readLine(),9)
  60.   if isLocked=="true" and permLockout then
  61.     lockout()
  62.   end
  63. end
  64.  
  65. function main()
  66.   local rows=12
  67.   local cols=10
  68.   term.clear()
  69.   term.setCursorPos(1,1)
  70.   local dict = readWords(wordFile)
  71.   local eString1 = getEncoded(rows*cols,dict)
  72.   local eString2 = getEncoded(rows*cols,dict)
  73.   drawTitle()
  74.   drawAttempts(attempts)
  75.   drawHex(2,7)
  76.   drawHex(20,7)
  77.   drawEncoded(9,7,rows,cols,eString1)
  78.   drawEncoded(27,7,rows,cols,eString2)
  79.   local password=getPass()
  80.   eventListener(eString1,eString2,password)
  81.   term.setCursorPos(1,1)
  82. end
  83.  
  84. function getStatus(choice,pass)
  85.   local sameChars = commonLetters(choice,pass)
  86.   table.insert(status,string.upper(choice))
  87.   if sameChars==#pass and string.len(choice)==string.len(pass) then
  88.      table.insert(status,"Exact match!")
  89.      table.insert(status,"Please wait")
  90.      table.insert(status,"while system")
  91.      table.insert(status,"is accessed.")
  92.   else
  93.      table.insert(status,"Entry denied")
  94.      table.insert(status,sameChars.."/"..string.len(pass).." correct.")
  95.      attempts=attempts-1
  96.   end
  97.  
  98.   if attempts==0 then
  99.     lockout()
  100.   end  
  101. end
  102.  
  103.  
  104.  
  105. function drawStatus()
  106.   local startX=38
  107.   local startY=16
  108.   local currentStat=#status
  109.   repeat
  110.     if status[currentStat]==nil then break end
  111.     clearFrom(startX,startY)
  112.     term.setCursorPos(startX,startY)
  113.     term.write(">"..status[currentStat])
  114.     currentStat=currentStat-1
  115.     startY=startY-1
  116.   until startY<=6
  117. end
  118.  
  119. --highlights part of an eString
  120.  
  121. --detects keypresses
  122. function eventListener(leftStr,rightStr,pass)
  123.   local side="left"     --which eString is it
  124.   local charPos=1       --which  number character
  125.   local cmdLineX,cmdLineY=38,18
  126.   local selection = string.sub(leftStr,1,1)
  127.   local onWord=false
  128.   local cursorX,cursorY=9,7
  129.   local upperX,upperY=36,18 --upper limits
  130.   local lowerX,lowerY=9,7   --lower limits
  131.   local keys = {200,208,203,205}
  132.  
  133.   highlight(cursorX,cursorY,selection)
  134.   while true do
  135.     local ev,p1,p2,p3 = os.pullEventRaw()
  136.     if ev=="key" then
  137.       if inTable(p1,keys) and not onWord then
  138.         lowlight(cursorX,cursorY,selection)
  139.       elseif inTable(p1,keys) and onWord then
  140.         if side=="left" then
  141.           lowWord(selection,leftStr,side)
  142.         else
  143.           lowWord(selection,rightStr,side)
  144.         end
  145.       end
  146.      
  147.       if p1==200 and cursorY>lowerY then  --up arrow
  148.          cursorY=cursorY-1
  149.       elseif p1==208 and cursorY<upperY then --down arrow
  150.          cursorY=cursorY+1
  151.       elseif p1==203 and cursorX>lowerX then --left arrow
  152.          if cursorX==27 then
  153.            side="left"
  154.            cursorX=cursorX-9
  155.          else
  156.            cursorX=cursorX-1
  157.          end
  158.       elseif p1==205 and cursorX<upperX then --right arrow
  159.          if cursorX==18 then
  160.            side="right"
  161.            cursorX=cursorX+9
  162.          else
  163.            cursorX=cursorX+1
  164.          end
  165.       elseif p1==28 then                     --enter key
  166.         getStatus(selection,pass)
  167.         drawStatus()
  168.         drawAttempts(attempts)
  169.     if selection==pass then
  170.           os.sleep(2)
  171.           shell.run(script)
  172.         end
  173.       end
  174.     --elseif ev=="mouse_click" and p1==1 then --left mouse button
  175.     --  if lowerX<=p2 and p2<=upperX and lowerY<=p3 and p3<=upperY then
  176.     --    cursorX,cursorY=p2,p3
  177.     --  end
  178.     end
  179.    
  180.     if side=="left" then
  181.       charPos=math.floor(((cursorY-7)*10)+(cursorX-9)+1)
  182.       selection=string.sub(leftStr,charPos,charPos)
  183.     else
  184.       charPos=math.floor(((cursorY-7)*10)+(cursorX-27)+1)
  185.       selection=string.sub(rightStr,charPos,charPos)
  186.     end
  187.    
  188.     if isalpha(selection) and side=="left" then
  189.       selection = findWord(cursorX,cursorY,charPos,leftStr,side)
  190.       lightWord(selection,leftStr,side)
  191.       onWord=true
  192.     elseif isalpha(selection) and side=="right" then
  193.       selection = findWord(cursorX,cursorY,charPos,rightStr,side)
  194.       lightWord(selection,rightStr,side)
  195.       onWord=true
  196.     else
  197.       onWord=false
  198.       highlight(cursorX,cursorY,selection)
  199.     end
  200.     clearFrom(cmdLineX,cmdLineY)
  201.     term.setCursorPos(cmdLineX,cmdLineY)
  202.     term.write(">"..selection)
  203.   end
  204. end
  205.  
  206. function lightWord(sel,eString,side)
  207.   local j=1
  208.   local l,r = string.find(eString,sel)
  209.   for i=l,r do
  210.     local x,y=posToCoords(i,side)
  211.     highlight(x,y,string.sub(sel,j,j))
  212.     j=j+1
  213.   end
  214. end
  215.  
  216. function lowWord(sel,eString,side)
  217.   local j=1
  218.   local l,r = string.find(eString,sel)
  219.   for i=l,r do
  220.     local x,y = posToCoords(i,side)
  221.     lowlight(x,y,string.sub(sel,j,j))
  222.     j=j+1
  223.   end
  224. end
  225.  
  226. function findWord(x,y,charPos,eString,side)
  227.   --check left and right
  228.   local left = charPos
  229.   local right = charPos
  230.   while isalpha(string.sub(eString,left-1,left-1)) do
  231.     left=left-1
  232.   end
  233.   while isalpha(string.sub(eString,right+1,right+1)) do
  234.     right=right+1
  235.   end
  236.   --highlight the word
  237.   for i=left,right do
  238.     local x,y = posToCoords(i,side)
  239.     highlight(x,y,string.sub(eString,i,i))
  240.   end
  241.   return(string.sub(eString,left,right))  
  242. end
  243.  
  244. function posToCoords(pos,side)
  245.   local x,y=0,0
  246.   if side=="left" then
  247.     if pos%10~=0 then
  248.       x=pos%10+8
  249.       y=math.floor(pos/10)+7
  250.     else
  251.       x=10+8
  252.       y=math.floor(pos/10)+6
  253.     end
  254.   else
  255.     if pos%10~=0 then
  256.       x=pos%10+26
  257.       y=math.floor(pos/10)+7
  258.     else
  259.       x=10+26
  260.       y=math.floor(pos/10)+6
  261.     end
  262.   end
  263.   return x,y
  264. end
  265.  
  266. function highlight(x,y,sel)
  267.   term.setBackgroundColor(textColor)
  268.   term.setTextColor(colors.black)
  269.   term.setCursorPos(x,y)
  270.   term.write(sel)
  271.   term.setBackgroundColor(colors.black)
  272.   term.setTextColor(textColor)
  273. end
  274.  
  275. function lowlight(x,y,sel)
  276.   term.setBackgroundColor(colors.black)
  277.   term.setTextColor(textColor)
  278.   term.setCursorPos(x,y)
  279.   term.write(sel)
  280. end
  281.  
  282. --returns a password from the usedWords list
  283. function getPass()
  284.   return(usedWords[math.random(1,#usedWords)])
  285. end
  286.  
  287. function drawTitle()
  288.   term.setCursorPos(2,2)
  289.   term.setTextColor(textColor)
  290.   term.write("ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL")
  291.   term.setCursorPos(2,3)
  292.   term.write("ENTER PASSWORD NOW")
  293. end
  294.  
  295. function drawAttempts(a)
  296.   term.setCursorPos(2,5)
  297.   term.setTextColor(textColor)
  298.   term.clearLine()
  299.   term.write(a.." ATTEMPT(S) LEFT: ")
  300.   local x = 21
  301.   for i=1,a do
  302.     term.setBackgroundColor(textColor)
  303.     term.setCursorPos(x,5)
  304.     term.write(" ")
  305.     x = x + 2
  306.   end
  307.   term.setBackgroundColor(colors.black)
  308. end
  309.  
  310. function drawHex(x,y)
  311.   local rows = 12
  312.   term.setTextColor(textColor)
  313.   for i=1,rows do
  314.     term.setCursorPos(x,y)
  315.     local hstring="0x"
  316.     for j=1,4 do hstring=hstring..randomHex() end
  317.     term.write(hstring)
  318.     y = y + 1
  319.   end    
  320. end
  321.  
  322. --returns string containing random nonletter characters
  323. --and various words. does not have '\n' included
  324. function getEncoded(size,words)
  325.   local step = 1
  326.   local totalWords=0
  327.   local curWord = nil
  328.   local onWord = false
  329.   local charIter = 1 --what letter in curWord are we at?
  330.   local chance = 0
  331.   local encodedString=nonletter()
  332.   local lastIsWord=false
  333.  
  334.   --fills encodedString with nonletter chars and words
  335.   repeat
  336.     step=step+1
  337.     chance = math.random(1,13)  
  338.     if chance==6 and not onWord and not lastIsWord and totalWords<=8 then
  339.       local reps=0
  340.       repeat
  341.         if reps>20 then break end
  342.         curWord=words[math.random(1,#words)]
  343.         reps=reps+1
  344.       until not inTable(curWord,usedWords) and step+#curWord<size
  345.       if reps<=20 then onWord=true end
  346.     end
  347.     --determines whether to add random nonletter or char from curWord
  348.     if onWord then
  349.       encodedString=encodedString..string.sub(curWord,charIter,charIter)
  350.       charIter=charIter+1
  351.       if charIter>#curWord then
  352.          table.insert(usedWords,curWord)
  353.          onWord=false
  354.          totalWords=totalWords+1
  355.          lastIsWord=true
  356.          charIter=1
  357.        end
  358.     else
  359.       encodedString=encodedString..nonletter()
  360.       lastIsWord=false
  361.     end
  362.   until string.len(encodedString)==size
  363.   return encodedString
  364. end
  365.  
  366. function isalpha(char)
  367.   local alpha="abcdefghijklmnopqrstuvwxyz"
  368.   for i=1,string.len(alpha) do
  369.     if string.sub(alpha,i,i)==char then return true end
  370.   end
  371.   return false
  372. end
  373. --lightS is a table {x1,y1} and lightE is table {x2,y2}
  374. function drawEncoded(x,y,rows,cols,eString,lightS,lightE)
  375.   term.setTextColor(textColor)
  376.   local curX=x
  377.   local curY=y
  378.   for i=1,rows*cols do
  379.     term.setCursorPos(curX,curY)
  380.     term.write(string.sub(eString,i,i))
  381.     if i%cols==0 then
  382.        curY=curY+1
  383.        curX=x
  384.     else
  385.        curX=curX+1
  386.     end
  387.   end
  388. end
  389.  
  390. --read the codewords from the file
  391. --returns list
  392. function readWords(fileName)
  393.   local dict = {}
  394.   local h = fs.open(fileName,"r")
  395.   for word in string.gmatch(h.readAll(), "%a+") do table.insert(dict,word) end
  396.   return dict
  397. end
  398.  
  399.  
  400. function findPattern(text,pattern,start)
  401.   return string.sub(text,string.find(text,pattern,start))
  402. end
  403.  
  404. --returns one nonletter char (random)
  405. function nonletter()
  406.   local possible = {'!','@','#','$','%','^','&','*',
  407.                    '(',')','_','-','+','=','[','{',
  408.                    ']','|',',','\'','\"','}',
  409.                    ';',':','.','>','<','?'}
  410.   return(possible[math.random(1,#possible)])
  411. end  
  412.  
  413. --returns one valid hex num (random)
  414. function randomHex()
  415.   local possible = {'A','B','C','D','E','F'}
  416.   for i=0,9 do
  417.     table.insert(possible,i)
  418.   end
  419.   return(possible[math.random(1,#possible)])
  420. end
  421.  
  422. --returns int of same letters in same positions in two strings.
  423. function commonLetters(s1,s2)
  424.   charsInCommon=0
  425.   local long,short=nil,nil
  426.   if string.len(s1)>=string.len(s2) then long,short=s1,s2
  427.   else long,short=s2,s1 end
  428.   for i=1,#long do
  429.     if string.sub(long,i,i)==string.sub(short,i,i) then
  430.       charsInCommon=charsInCommon+1
  431.     elseif string.sub(short,i,i)==nil then
  432.       break
  433.     end
  434.   end
  435.   return charsInCommon
  436. end
  437.  
  438. --clears a line from x,y till the end of the screen
  439. function clearFrom(x,y)
  440.   term.setCursorPos(x,y)
  441.   for i=0,51-x do
  442.     term.setCursorPos(x+i,y)
  443.     term.write(" ")
  444.   end
  445. end
  446.  
  447. --checks if a value is in a given table
  448. function inTable(val,t)
  449.   for k,v in ipairs(t) do
  450.     if v==val then
  451.       return true
  452.     end
  453.   end
  454.   return false
  455. end
  456.  
  457. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement