Advertisement
Stiepen

Derpping sandbox

Oct 8th, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. sandbox = {}
  2. local apilist = {"textutils", "pairs", "ipairs", "error", "sleep", "tostring"}
  3.  
  4. --
  5. local target_ = ""
  6. local sender_ = ""
  7. local tmpout = ""
  8.  
  9. local function echo(s)
  10. _G['irc_api.lua'].echo(s)
  11. end
  12.  
  13. sandboxedprint = function(...)
  14. local outputs = {...}
  15. for _, v in ipairs(outputs) do
  16. if tmpout ~= "" then tmpout = tmpout..", " end
  17. tmpout = tmpout..tostring(v)
  18. end
  19. end
  20.  
  21. function resetSandbox()
  22. sandbox = {}
  23. --Creating own os copy
  24. sandbox.os = {version = os.version, time = os.time}
  25.  
  26. --Creating own fs copy
  27. sandbox.fs = {}
  28. sandbox.fs.list = function(path)
  29. return fs.list("/sandbox/"..path)
  30. end
  31. sandbox.fs.exists = function(path)
  32. return fs.exists("/sandbox/"..path)
  33. end
  34. sandbox.fs.isDir = function(path)
  35. return fs.isDir("/sandbox/"..path)
  36. end
  37.  
  38. sandbox.fs.isReadOnly = function(path)
  39. return fs.isReadOnly("/sandbox/"..path)
  40. end
  41.  
  42. sandbox.fs.getName = function(path)
  43. return fs.getName("/sandbox/"..path)
  44. end
  45.  
  46. sandbox.fs.getDrive = function(path)
  47. return fs.getDrive("/sandbox/"..path)
  48. end
  49.  
  50. sandbox.fs.getSize = function(path)
  51. return fs.getSize("/sandbox/"..path)
  52. end
  53.  
  54. sandbox.fs.makeDir = function(path)
  55. return fs.makeDir("/sandbox/"..path)
  56. end
  57.  
  58. sandbox.fs.move = function(from, tp)
  59. return fs.move("/sandbox/"..from, "/sandbox/"..to)
  60. end
  61.  
  62. sandbox.fs.copy = function(from, tp)
  63. return fs.copy("/sandbox/"..from, "/sandbox/"..to)
  64. end
  65.  
  66. sandbox.fs.delete = function(path)
  67. return fs.delete("/sandbox/"..path)
  68. end
  69.  
  70. sandbox.fs.combine = function(from, tp)
  71. return fs.combine(from, to)
  72. end
  73.  
  74. sandbox.fs.open = function(path, mode)
  75. return fs.open("/sandbox/"..path, mode)
  76. end
  77.  
  78. --Creating own rednet copy
  79. --local rednet_ = {receiver = receive, open = open
  80.  
  81. -- Actual reset
  82. for k, v in ipairs(apilist) do
  83. sandbox[v] = _G[v]
  84. end
  85. --echo(tostring(sandboxedprint))
  86. sandbox.print = sandboxedprint
  87. sandbox._G = sandbox
  88. end
  89.  
  90. function runlua(code, target, sender)
  91. echo(code)
  92. echo(target)
  93. echo(sender)
  94. f, m = loadstring(code)
  95. target_ = target
  96. sender_ = sender
  97. if not f then
  98. sandboxedprint(m)
  99. else
  100. setfenv(f, sandbox)
  101. --local p = print
  102. --rawset(_G, "print", sandboxedprint)
  103. local s, m = pcall(function() parallel.waitForAny(f, function() sleep(5) error("Time limit exceeded") end) end)
  104. --rawset(_G, "print", p)
  105. if not s then tmpout = m end
  106. end
  107. local ret = tmpout
  108. tmpout = ""
  109. return ret
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement