Sharidan

(oc)ephack.lua

Jan 29th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.74 KB | None | 0 0
  1. --
  2. -- Enhanced Portals 3
  3. -- Address Hacker
  4. --
  5. -- Author: Sharidan
  6. --     www.sharidan.dk
  7. --
  8.  
  9. local app = {
  10.   name    = "ephack",
  11.   version = "1.0-c"
  12. }
  13.  
  14. local fileConfig  = "/etc/ephack.cfg"
  15. local fileResults = "/etc/addresses.eph"
  16.  
  17. local components = require("component")
  18. local term = require("term")
  19. local event = require("event")
  20. local ser = require("serialization")
  21. local fs = require("filesystem")
  22.  
  23. local running = true
  24.  
  25. local function getComponent(compType)
  26.   for addr, ct in components.list(compType, false) do
  27.     return components.proxy(addr)
  28.   end
  29.   return nil
  30. end
  31.  
  32. local controller = getComponent("ep_controller")
  33. local dialer = getComponent("dialling_device")
  34.  
  35. local config = {
  36.   symbols = 0,
  37.   glyphs = { -1, -1, -1, -1, -1, -1, -1, -1, -1 },
  38. }
  39.  
  40. local function showUsage()
  41.   print("Usage: ephack [-c/-r] symbols")
  42.   print("  symbols = how many symbols to start with.")
  43.   print("            valid: 1-9")
  44.   print("  -c      = continue. symbols can be skipped.")
  45.   print("  -r      = reset with new symbols.")
  46. end
  47.  
  48. local function checkArgs(args)
  49.   local opts, syms = table.unpack(args)
  50.   if (opts) then
  51.     local opt = tonumber(opts)
  52.     if (opt) then
  53.       if (opt >= 1 and opt <= 9) then
  54.         return 1, opt
  55.       end
  56.     else
  57.       local o = string.lower(opts)
  58.       local s = tonumber(syms) or -1
  59.       if (o == "-c") then
  60.         return 3, 0
  61.       elseif (o == "-r") then
  62.         if (s >= 1 and s <= 9) then
  63.           return 2, s
  64.         end
  65.       end
  66.     end
  67.   end
  68.   return nil, nil
  69. end
  70.  
  71. local function killFile(fn)
  72.   if (fs.exists(fn)) then
  73.     fs.remove(fn)
  74.   end
  75. end
  76.  
  77. local function loadConfig()
  78.   local success
  79.   if (fs.exists(fileConfig)) then
  80.     local f = io.open(fileConfig, "r")
  81.     if (f) then
  82.       local data = f:read()
  83.       f:close()
  84.       if (data) then
  85.         config = ser.unserialize(data)
  86.         success = true
  87.       end
  88.     end
  89.     f = nil
  90.   end
  91.   return success
  92. end
  93.  
  94. local function saveConfig()
  95.   killFile(fileConfig)
  96.   if (not fs.exists(fileConfig)) then
  97.     local f = io.open(fileConfig, "w")
  98.     if (f) then
  99.       f:write(ser.serialize(config))
  100.       f:close()
  101.     end
  102.     f = nil
  103.   end
  104. end
  105.  
  106. local function appendResult(address)
  107.   local f
  108.   if (not fs.exists(fileResults)) then
  109.     f = io.open(fileResults, "w")
  110.   else
  111.     f = io.open(fileResults, "a")
  112.   end
  113.   if (f) then
  114.     f:write(address.."\n")
  115.     f:close()
  116.   end
  117.   f = nil
  118. end
  119.  
  120. local function setupGlyphs(syms)
  121.   config.symbols = tonumber(syms)
  122.   for g = 1, 9 do
  123.     if (g <= syms) then
  124.       config.glyphs[g] = 0
  125.     else
  126.       config.glyphs[g] = -1
  127.     end
  128.   end
  129. end
  130.  
  131. local function getCurrentPortalAddress()
  132.   local address = string.gsub(table.concat(config.glyphs, " "), "-1", "")
  133.   address = string.gsub(address, " ", "-")
  134.   return address
  135. end
  136.  
  137. local function nextGlyph()
  138.   for g = 1, 9 do
  139.     config.glyphs[g] = config.glyphs[g] + 1
  140.     if (config.glyphs[g] == 27) then
  141.       config.glyphs[g] = 0
  142.     else
  143.       break
  144.     end
  145.   end
  146. end
  147.  
  148. local function tryDial()
  149.   local address = getCurrentPortalAddress()
  150.   if (controller and dialer) then
  151.     dialer.dial(address)
  152.     if (controller.isPortalActive()) then
  153.       local cx, cy = term.getCursor()
  154.       term.setCursor(1, cy)
  155.       term.write(string.rep(" ", 40))
  156.       term.setCursor(1, cy)
  157.       print("Address found: "..address)
  158.       appendResult(address)
  159.       dialer.terminate()
  160.     end
  161.   end
  162.   nextGlyph()
  163. end
  164.  
  165. local function showAddy()
  166.   local address = getCurrentPortalAddress()
  167.   local cx, cy = term.getCursor()
  168.   term.setCursor(1, cy)
  169.   term.write(string.rep(" ", 40))
  170.   term.setCursor(1, cy)
  171.   term.write(address)
  172. end
  173.  
  174. local function runHack(mode, syms)
  175.   if (mode == 1 or mode == 2) then -- New / Reset
  176.     killFile(fileResults)
  177.     setupGlyphs(syms)
  178.   elseif (mode == 3) then -- Continue
  179.     loadConfig()
  180.   end
  181.   print("ephack is running!")
  182.   print("Press \"Q\" to quit.")
  183.   local dtid = event.timer(0.1, tryDial, math.huge)
  184.   local stid = event.timer(1, showAddy, math.huge)
  185.   while running do
  186.     local e, p1, p2, p3, p4, p5 = event.pull()
  187.     if (e == "key_down" and p3 == 16) then
  188.       running = nil
  189.     end
  190.   end
  191.   event.cancel(stid)
  192.   event.cancel(dtid)
  193.   saveConfig()
  194.   print("")
  195.   print("Any found addresses were saved in:")
  196.   print(fileResults)
  197. end
  198.  
  199. if (controller and dialer) then
  200.   local opt, syms = checkArgs({...})
  201.   if (opt) then
  202.     runHack(opt, syms)
  203.   else
  204.     showUsage()
  205.   end
  206. else
  207.   print(app.name.." "..app.version)
  208.   if (not controller) then
  209.     print("No portal controller found!")
  210.   elseif (not dialer) then
  211.     print("No dialing device found!")
  212.   end
  213. end
  214. dialer = nil
  215. controller = nil
  216. components = nil
  217. event = nil
  218. term = nil
  219. ser = nil
  220. fs = nil
Add Comment
Please, Sign In to add comment