Advertisement
Oeed

oeedPay API

Nov 8th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.68 KB | None | 0 0
  1. --  Hideously Smashed Together by Compilr, a Hideous Smash-Stuff-Togetherer, (c) 2014 oeed  --
  2.  
  3. --  This file REALLLLLLLY isn't suitable to be used for anything other than being executed --
  4.  
  5. --  To extract all the files, run: "<filename> --extract" in the Shell --
  6. local files = {
  7.   oeedPay = "local function getModem()\
  8.     for i, side in ipairs(peripheral.getNames()) do\
  9.         local _type = peripheral.getType(side)\
  10.         if _type == 'modem' then\
  11.             local isWireless = false\
  12.             if not pcall(function()isWireless = peripheral.call(side, 'isWireless') end) then\
  13.                 isWireless = true\
  14.             end     \
  15.             if not isWireless then\
  16.                 return peripheral.wrap(side)\
  17.             end\
  18.         end\
  19.     end\
  20. end\
  21. \
  22. local oeedPayAPIRequest = 4275\
  23. local oeedPayAPIRequestReply = 4276\
  24. \
  25. function requestPayment(amount, accountNumber)\
  26.     local modem = getModem()\
  27.     if not modem then\
  28.         error('No modem attached.')\
  29.     end\
  30. \
  31.     modem.open(oeedPayAPIRequest)\
  32.     modem.open(oeedPayAPIRequestReply)\
  33. \
  34.     modem.transmit(oeedPayAPIRequest, oeedPayAPIRequestReply, textutils.serialize({Amount = amount, Creditor = accountNumber}))\
  35.     local timeout = os.startTimer(60)\
  36.     while true do\
  37.         local event, side, channel, reply, message = os.pullEvent()\
  38.         if event == 'modem_message' and message then\
  39.             if message == 'YES' then\
  40.                 return true\
  41.             elseif message == 'NO' then\
  42.                 return false\
  43.             end\
  44.         elseif event == 'timer' and side == timeout then\
  45.             return false\
  46.         end\
  47.     end\
  48. end",
  49.   startup = "os.loadAPI('oeedPay')\
  50. \
  51. while true do\
  52.     print('Enter cost:')\
  53.     local n = tonumber(read())\
  54.     print(oeedPay.requestPayment(n, 987654321))\
  55. end",
  56. }
  57.  
  58. local function run(tArgs)
  59.  
  60.   local fnFile, err = loadstring(files['startup'], 'startup')
  61.   if err then
  62.     error(err)
  63.   end
  64.  
  65.   local function split(str, pat)
  66.      local t = {}
  67.      local fpat = "(.-)" .. pat
  68.      local last_end = 1
  69.      local s, e, cap = str:find(fpat, 1)
  70.      while s do
  71.         if s ~= 1 or cap ~= "" then
  72.      table.insert(t,cap)
  73.         end
  74.         last_end = e+1
  75.         s, e, cap = str:find(fpat, last_end)
  76.      end
  77.      if last_end <= #str then
  78.         cap = str:sub(last_end)
  79.         table.insert(t, cap)
  80.      end
  81.      return t
  82.   end
  83.  
  84.   local function resolveTreeForPath(path, single)
  85.     local _files = files
  86.     local parts = split(path, '/')
  87.     if parts then
  88.       for i, v in ipairs(parts) do
  89.         if #v > 0 then
  90.           if _files[v] then
  91.             _files = _files[v]
  92.           else
  93.             _files = nil
  94.             break
  95.           end
  96.         end
  97.       end
  98.     elseif #path > 0 and path ~= '/' then
  99.       _files = _files[path]
  100.     end
  101.     if not single or type(_files) == 'string' then
  102.       return _files
  103.     end
  104.   end
  105.  
  106.   local oldFs = fs
  107.   local env
  108.   env = {
  109.     fs = {
  110.       list = function(path)
  111.               local list = {}
  112.               if fs.exists(path) then
  113.             list = fs.list(path)
  114.               end
  115.         for k, v in pairs(resolveTreeForPath(path)) do
  116.           if not fs.exists(path .. '/' ..k) then
  117.             table.insert(list, k)
  118.           end
  119.         end
  120.         return list
  121.       end,
  122.  
  123.       exists = function(path)
  124.         if fs.exists(path) then
  125.           return true
  126.         elseif resolveTreeForPath(path) then
  127.           return true
  128.         else
  129.           return false
  130.         end
  131.       end,
  132.  
  133.       isDir = function(path)
  134.         if fs.isDir(path) then
  135.           return true
  136.         else
  137.           local tree = resolveTreeForPath(path)
  138.           if tree and type(tree) == 'table' then
  139.             return true
  140.           else
  141.             return false
  142.           end
  143.         end
  144.       end,
  145.  
  146.       isReadOnly = function(path)
  147.         if not fs.isReadOnly(path) then
  148.           return false
  149.         else
  150.           return true
  151.         end
  152.       end,
  153.  
  154.       getName = fs.getName,
  155.  
  156.       getSize = fs.getSize,
  157.  
  158.       getFreespace = fs.getFreespace,
  159.  
  160.       makeDir = fs.makeDir,
  161.  
  162.       move = fs.move,
  163.  
  164.       copy = fs.copy,
  165.  
  166.       delete = fs.delete,
  167.  
  168.       combine = fs.combine,
  169.  
  170.       open = function(path, mode)
  171.         if fs.exists(path) then
  172.           return fs.open(path, mode)
  173.         elseif type(resolveTreeForPath(path)) == 'string' then
  174.           local handle = {close = function()end}
  175.           if mode == 'r' then
  176.             local content = resolveTreeForPath(path)
  177.             handle.readAll = function()
  178.               return content
  179.             end
  180.  
  181.             local line = 1
  182.             local lines = split(content, '\n')
  183.             handle.readLine = function()
  184.               if line > #lines then
  185.                 return nil
  186.               else
  187.                 return lines[line]
  188.               end
  189.               line = line + 1
  190.             end
  191.                       return handle
  192.           else
  193.             error('Cannot write to read-only file (compilr archived).')
  194.           end
  195.         else
  196.           return fs.open(path, mode)
  197.         end
  198.       end
  199.     },
  200.  
  201.     io = {
  202.       input = io.input,
  203.       output = io.output,
  204.       type = io.type,
  205.       close = io.close,
  206.       write = io.write,
  207.       flush = io.flush,
  208.       lines = io.lines,
  209.       read = io.read,
  210.       open = function(path, mode)
  211.         if fs.exists(path) then
  212.           return io.open(path, mode)
  213.         elseif type(resolveTreeForPath(path)) == 'string' then
  214.           local content = resolveTreeForPath(path)
  215.           local f = fs.open(path, 'w')
  216.           f.write(content)
  217.           f.close()
  218.           if mode == 'r' then
  219.             return io.open(path, mode)
  220.           else
  221.             error('Cannot write to read-only file (compilr archived).')
  222.           end
  223.         else
  224.           return io.open(path, mode)
  225.         end
  226.       end
  227.     },
  228.  
  229.     loadfile = function( _sFile )
  230.         local file = env.fs.open( _sFile, "r" )
  231.         if file then
  232.             local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  233.             file.close()
  234.             return func, err
  235.         end
  236.         return nil, "File not found: ".._sFile
  237.     end,
  238.  
  239.     dofile = function( _sFile )
  240.         local fnFile, e = env.loadfile( _sFile )
  241.         if fnFile then
  242.             setfenv( fnFile, getfenv(2) )
  243.             return fnFile()
  244.         else
  245.             error( e, 2 )
  246.         end
  247.     end
  248.   }
  249.  
  250.   setmetatable( env, { __index = _G } )
  251.  
  252.   local tAPIsLoading = {}
  253.   env.os.loadAPI = function( _sPath )
  254.       local sName = fs.getName( _sPath )
  255.       if tAPIsLoading[sName] == true then
  256.           printError( "API "..sName.." is already being loaded" )
  257.           return false
  258.       end
  259.       tAPIsLoading[sName] = true
  260.          
  261.       local tEnv = {}
  262.       setmetatable( tEnv, { __index = env } )
  263.       local fnAPI, err = env.loadfile( _sPath )
  264.       if fnAPI then
  265.           setfenv( fnAPI, tEnv )
  266.           fnAPI()
  267.       else
  268.           printError( err )
  269.           tAPIsLoading[sName] = nil
  270.           return false
  271.       end
  272.      
  273.       local tAPI = {}
  274.       for k,v in pairs( tEnv ) do
  275.           tAPI[k] =  v
  276.       end
  277.      
  278.       env[sName] = tAPI    
  279.       tAPIsLoading[sName] = nil
  280.       return true
  281.   end
  282.  
  283.   env.shell = shell
  284.  
  285.   setfenv( fnFile, env )
  286.   fnFile(unpack(tArgs))
  287. end
  288.  
  289. local function extract()
  290.     local function node(path, tree)
  291.         if type(tree) == 'table' then
  292.             fs.makeDir(path)
  293.             for k, v in pairs(tree) do
  294.                 node(path .. '/' .. k, v)
  295.             end
  296.         else
  297.             local f = fs.open(path, 'w')
  298.             if f then
  299.                 f.write(tree)
  300.                 f.close()
  301.             end
  302.         end
  303.     end
  304.     node('', files)
  305. end
  306.  
  307. local tArgs = {...}
  308. if #tArgs == 1 and tArgs[1] == '--extract' then
  309.   extract()
  310. else
  311.   run(tArgs)
  312. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement