Advertisement
NuAoA

Untitled

Feb 24th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. -- NSA logger by nuaoa.
  2. --save as 'startup'
  3.  
  4. local directions = {"back","front","left","right","top","bottom"}
  5. local sensor
  6. local data = {}
  7.  
  8.  
  9. -- Makes a copy of a table
  10. function deepcopy(orig)
  11. local orig_type = type(orig)
  12. local copy
  13. if orig_type == 'table' then
  14. copy = {}
  15. for orig_key, orig_value in next, orig, nil do
  16. copy[deepcopy(orig_key)] = deepcopy(orig_value)
  17. end
  18. setmetatable(copy, deepcopy(getmetatable(orig)))
  19. else -- number, string, boolean, etc
  20. copy = orig
  21. end
  22. return copy
  23. end
  24.  
  25. --Log stuff to a file.
  26. function Log(mes)
  27. local file
  28. local prev = ""
  29. if fs.exists("log.txt") then
  30. file = fs.open("log.txt","r")
  31. prev = file.readAll()
  32. file.close()
  33. else
  34. prev = "Theft Log".."\n"
  35. end
  36. local tab =sensor.getPlayerNames()
  37. local names = tostring(tab[1])
  38. if #tab>1 then
  39. for i=2,#tab do
  40. names = names..","..tab[i]
  41. end
  42. end
  43. file = fs.open("log.txt","w")
  44. file.write(prev.."\n")
  45. file.write("["..os.day()..","..os.time().."]["..names.."]"..mes)
  46. file.close()
  47. end
  48.  
  49. --get a table of items in a chest.
  50. function getChestData()
  51. local p = peripheral.wrap("top")
  52. --p.condenseItems()
  53. local retTab = p.getAllStacks()
  54. --sometimes retTab is a number, tested this loop and found it runs only 3 times, so no idea whats going on.
  55. while type(retTab) ~= "table" do
  56. retTab = p.getAllStacks()
  57. end
  58. local retTab2 = {}
  59. for k,v in pairs(retTab) do
  60. if retTab2[v.name] == nil then
  61. retTab2[v.name]=deepcopy(v)
  62. else
  63. retTab2[v.name].qty = retTab2[v.name].qty +v.qty
  64. end
  65. end
  66. return deepcopy(retTab2)
  67. end
  68.  
  69. function antiHack()
  70. while true do
  71. local event,side = os.pullEventRaw()
  72. if event == "peripheral" then
  73. if peripheral.getType(side) == "drive" then
  74. -- we got someone putting a drive next to the terminal
  75. elseif peripheral.getType(side) == "computer" then
  76.  
  77. end
  78. elseif event == "disk" then
  79. if fs.exists("disk/startup") then
  80. fs.delete("disk/startup")
  81. disk.setLabel(side,"Broken Floppy")
  82. end
  83. end
  84. end
  85. end
  86.  
  87. function lockTerminal()
  88. local last_key = 0
  89. while true do
  90. local pullEvent = os.pullEvent
  91. os.pullEvent = os.pullEventRaw
  92. local event,key = os.pullEventRaw()
  93. if event == "terminate" then
  94. Log("[Computer] Terminate attempt")
  95. elseif event == "key" then
  96. if last_key == 29 and key == 19 then --attempting a restart
  97. Log("[Computer] Restart")
  98. elseif last_key== 29 and key == 41 then -- ask for password
  99. Log("[Computer] Password screen accessed")
  100. term.write("Please enter the password: ")
  101. local pass = read("*")
  102. if pass == data.password then
  103. shell.run("edit","log.txt")
  104. term.clear()
  105. else
  106. Log("[Computer] Incorrect password attempt")
  107. print("Password accpeted: Opening secret door!")
  108. sleep(2)
  109. term.clear()
  110. end
  111. end
  112. last_key = key
  113. end
  114. end
  115. end
  116.  
  117. function startup()
  118. term.clear()
  119. for i=1,#directions do
  120. if peripheral.getType(directions[i]) == "openperipheral_sensor" then
  121. sensor = peripheral.wrap(directions[i])
  122. break
  123. end
  124. end
  125. if not fs.exists("settings") then
  126. term.setCursorPos(1,1)
  127. Log("First Boot! Chest contains: "..textutils.serialize(getChestData()))
  128. print("First Boot!, lets setup some crap!")
  129. term.write("Password used to access logs: ")
  130. data["password"] = read()
  131. term.setCursorPos(1,3)
  132. term.write("Amount of seconds inbetween scanning for")
  133. term.setCursorPos(1,4)
  134. term.write("local players: ")
  135. data["scanUpdate"] = tonumber(read())
  136. term.setCursorPos(1,6)
  137. term.write("Setup finished. To access logs press 'Ctrl ~'.")
  138. term.setCursorPos(1,7)
  139. term.write("Press enter to restart ")
  140. local file = fs.open("settings","w")
  141. file.write(textutils.serialize(data))
  142. file.close()
  143. read()
  144. os.reboot()
  145. else
  146. Log("[Computer] Startup")
  147. local file = fs.open("settings","r")
  148. data = textutils.unserialize(file.readAll())
  149. file.close()
  150. end
  151. term.clear()
  152. end
  153.  
  154. function watchChest()
  155.  
  156. end
  157. function playerListener()
  158. local active = false
  159. local function listen()
  160. while true do
  161. if #sensor.getPlayerNames() > 0 then
  162. active = true
  163. else
  164. active = false
  165. end
  166. sleep(data.scanUpdate)
  167. end
  168. end
  169. local function logChest()
  170. local tab = getChestData()
  171. sleep(0) -- offset program
  172. while true do
  173. if not active then sleep(data.scanUpdate)
  174. else
  175. --active player
  176. local tab2 = {}
  177. local logTab = {}
  178. tab2 = getChestData()
  179. for k,v in pairs(tab2) do
  180. if tab[k] == nil then
  181. table.insert(logTab,("[ ADDED ] "..v.name.." x"..(v.qty)))
  182. elseif tab[k].qty<v.qty then --items added
  183. --table.insert(logTab,("[ ADDED ] "..v.name.." x"..(v.qty-tab[k].qty)))
  184. elseif tab[k].qty > v.qty then -- items removed
  185. --table.insert(logTab,("[REMOVED] "..v.name.." x"..(tab[k].qty-v.qty)))
  186. end
  187. end
  188. for k,v in pairs(tab) do
  189. if tab2[k] == nil then
  190. table.insert(logTab,("[REMOVED] "..v.name.." x"..(v.qty)))
  191. elseif tab2[k].qty<v.qty then --items removed
  192. table.insert(logTab,("[REMOVED] "..v.name.." x"..(v.qty-tab2[k].qty)))
  193. elseif tab2[k].qty > v.qty then -- items added
  194. table.insert(logTab,("[ ADDED ] "..v.name.." x"..(tab2[k].qty-v.qty)))
  195. end
  196. end
  197.  
  198. tab ={}
  199. tab = getChestData()
  200. local stab = textutils.serialize(tab)
  201. local stab2 = textutils.serialize(tab2)
  202. if stab ~= stab2 then
  203. Log("[ ERROR ] Items updated while CPU performed tasks Compare following tables to determine event:")
  204. Log("[ ERROR ][Before] "..stab2)
  205. Log("[ ERROR ][After ] "..stab1)
  206. end
  207. for k,v in pairs(logTab) do
  208. Log(v)
  209. end
  210. sleep(1)
  211. end
  212. end
  213. end
  214. parallel.waitForAny(logChest,listen)
  215. end
  216. startup()
  217. while true do
  218. parallel.waitForAny(playerListener,lockTerminal,antiHack)
  219. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement