Advertisement
Guest User

build

a guest
May 30th, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.91 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.   sha256 = "\
  8. --  \
  9. --  Adaptation of the Secure Hashing Algorithm (SHA-244/256)\
  10. --  Found Here: http://lua-users.org/wiki/SecureHashAlgorithm\
  11. --  \
  12. --  Using an adapted version of the bit library\
  13. --  Found Here: https://bitbucket.org/Boolsheet/bslf/src/1ee664885805/bit.lua\
  14. --  \
  15. \
  16. local MOD = 2^32\
  17. local MODM = MOD-1\
  18. \
  19. local function memoize(f)\
  20.     local mt = {}\
  21.     local t = setmetatable({}, mt)\
  22.     function mt:__index(k)\
  23.         local v = f(k)\
  24.         t[k] = v\
  25.         return v\
  26.     end\
  27.     return t\
  28. end\
  29. \
  30. local function make_bitop_uncached(t, m)\
  31.     local function bitop(a, b)\
  32.         local res,p = 0,1\
  33.         while a ~= 0 and b ~= 0 do\
  34.             local am, bm = a % m, b % m\
  35.             res = res + t[am][bm] * p\
  36.             a = (a - am) / m\
  37.             b = (b - bm) / m\
  38.             p = p*m\
  39.         end\
  40.         res = res + (a + b) * p\
  41.         return res\
  42.     end\
  43.     return bitop\
  44. end\
  45. \
  46. local function make_bitop(t)\
  47.     local op1 = make_bitop_uncached(t,2^1)\
  48.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)\
  49.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))\
  50. end\
  51. \
  52. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})\
  53. \
  54. local function bxor(a, b, c, ...)\
  55.     local z = nil\
  56.     if b then\
  57.         a = a % MOD\
  58.         b = b % MOD\
  59.         z = bxor1(a, b)\
  60.         if c then z = bxor(z, c, ...) end\
  61.         return z\
  62.     elseif a then return a % MOD\
  63.     else return 0 end\
  64. end\
  65. \
  66. local function band(a, b, c, ...)\
  67.     local z\
  68.     if b then\
  69.         a = a % MOD\
  70.         b = b % MOD\
  71.         z = ((a + b) - bxor1(a,b)) / 2\
  72.         if c then z = bit32_band(z, c, ...) end\
  73.         return z\
  74.     elseif a then return a % MOD\
  75.     else return MODM end\
  76. end\
  77. \
  78. local function bnot(x) return (-1 - x) % MOD end\
  79. \
  80. local function rshift1(a, disp)\
  81.     if disp < 0 then return lshift(a,-disp) end\
  82.     return math.floor(a % 2 ^ 32 / 2 ^ disp)\
  83. end\
  84. \
  85. local function rshift(x, disp)\
  86.     if disp > 31 or disp < -31 then return 0 end\
  87.     return rshift1(x % MOD, disp)\
  88. end\
  89. \
  90. local function lshift(a, disp)\
  91.     if disp < 0 then return rshift(a,-disp) end \
  92.     return (a * 2 ^ disp) % 2 ^ 32\
  93. end\
  94. \
  95. local function rrotate(x, disp)\
  96.    x = x % MOD\
  97.    disp = disp % 32\
  98.    local low = band(x, 2 ^ disp - 1)\
  99.    return rshift(x, disp) + lshift(low, 32 - disp)\
  100. end\
  101. \
  102. local k = {\
  103.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\
  104.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
  105.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,\
  106.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
  107.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,\
  108.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
  109.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\
  110.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
  111.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,\
  112.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
  113.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,\
  114.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
  115.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,\
  116.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
  117.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,\
  118.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,\
  119. }\
  120. \
  121. local function str2hexa(s)\
  122.     return (string.gsub(s, \".\", function(c) return string.format(\"%02x\", string.byte(c)) end))\
  123. end\
  124. \
  125. local function num2s(l, n)\
  126.     local s = \"\"\
  127.     for i = 1, n do\
  128.         local rem = l % 256\
  129.         s = string.char(rem) .. s\
  130.         l = (l - rem) / 256\
  131.     end\
  132.     return s\
  133. end\
  134. \
  135. local function s232num(s, i)\
  136.     local n = 0\
  137.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end\
  138.     return n\
  139. end\
  140. \
  141. local function preproc(msg, len)\
  142.     local extra = 64 - ((len + 9) % 64)\
  143.     len = num2s(8 * len, 8)\
  144.     msg = msg .. \"\\128\" .. string.rep(\"\\0\", extra) .. len\
  145.     assert(#msg % 64 == 0)\
  146.     return msg\
  147. end\
  148. \
  149. local function initH256(H)\
  150.     H[1] = 0x6a09e667\
  151.     H[2] = 0xbb67ae85\
  152.     H[3] = 0x3c6ef372\
  153.     H[4] = 0xa54ff53a\
  154.     H[5] = 0x510e527f\
  155.     H[6] = 0x9b05688c\
  156.     H[7] = 0x1f83d9ab\
  157.     H[8] = 0x5be0cd19\
  158.     return H\
  159. end\
  160. \
  161. local function digestblock(msg, i, H)\
  162.     local w = {}\
  163.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end\
  164.     for j = 17, 64 do\
  165.         local v = w[j - 15]\
  166.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))\
  167.         v = w[j - 2]\
  168.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))\
  169.     end\
  170. \
  171.     local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]\
  172.     for i = 1, 64 do\
  173.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))\
  174.         local maj = bxor(band(a, b), band(a, c), band(b, c))\
  175.         local t2 = s0 + maj\
  176.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))\
  177.         local ch = bxor (band(e, f), band(bnot(e), g))\
  178.         local t1 = h + s1 + ch + k[i] + w[i]\
  179.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2\
  180.     end\
  181. \
  182.     H[1] = band(H[1] + a)\
  183.     H[2] = band(H[2] + b)\
  184.     H[3] = band(H[3] + c)\
  185.     H[4] = band(H[4] + d)\
  186.     H[5] = band(H[5] + e)\
  187.     H[6] = band(H[6] + f)\
  188.     H[7] = band(H[7] + g)\
  189.     H[8] = band(H[8] + h)\
  190. end\
  191. \
  192. function sha256(msg)\
  193.     msg = preproc(msg, #msg)\
  194.     local H = initH256({})\
  195.     for i = 1, #msg, 64 do digestblock(msg, i, H) end\
  196.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..\
  197.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))\
  198. end",
  199.   cst = "function getBalance(usr)\
  200.  local stream = http.get(\"http://crystalcoins.site88.net/user.php?action=getBalance&user=\"..usr)\
  201.  local bal = tonumber(stream.readLine())\
  202.  stream.close()\
  203.  return bal\
  204. end\
  205. function transact(usr,pw,to,amt)\
  206.  local stream = http.get(\"http://crystalcoins.site88.net/user.php?action=transaction&user=\"..usr..\"&pass=\"..pw..\"&to=\"..to..\"&amt=\"..amt)\
  207.  local suc = stream.readLine()\
  208.  stream.close()\
  209.  return suc\
  210. end",
  211.   krist = "local KristAPI = {}\
  212. local nodeURL = \"https://raw.githubusercontent.com/BTCTaras/kristwallet/master/staticapi/syncNode\"\
  213. \
  214. local function getNode()\
  215.     local node = http.get(nodeURL).readAll()\
  216.     if node then\
  217.         if node:match(\"http\") then\
  218.             return node\
  219.         end\
  220.         error(\"Error: Invalid response from node\", 2)\
  221.     end\
  222.     error(\"Error: Node Offline\", 2)\
  223. end\
  224. \
  225. -- SHA256 By GravityScore\
  226. \
  227. local MOD = 2^32\
  228. local MODM = MOD-1\
  229. \
  230. local function memoize(f)\
  231.     local mt = {}\
  232.     local t = setmetatable({}, mt)\
  233.     function mt:__index(k)\
  234.         local v = f(k)\
  235.         t[k] = v\
  236.         return v\
  237.     end\
  238.     return t\
  239. end\
  240. \
  241. local function make_bitop_uncached(t, m)\
  242.     local function bitop(a, b)\
  243.         local res,p = 0,1\
  244.         while a ~= 0 and b ~= 0 do\
  245.             local am, bm = a % m, b % m\
  246.             res = res + t[am][bm] * p\
  247.             a = (a - am) / m\
  248.             b = (b - bm) / m\
  249.             p = p*m\
  250.         end\
  251.         res = res + (a + b) * p\
  252.         return res\
  253.     end\
  254.     return bitop\
  255. end\
  256. \
  257. local function make_bitop(t)\
  258.     local op1 = make_bitop_uncached(t,2^1)\
  259.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)\
  260.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))\
  261. end\
  262. \
  263. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})\
  264. \
  265. local function bxor(a, b, c, ...)\
  266.     local z = nil\
  267.     if b then\
  268.         a = a % MOD\
  269.         b = b % MOD\
  270.         z = bxor1(a, b)\
  271.         if c then z = bxor(z, c, ...) end\
  272.         return z\
  273.     elseif a then return a % MOD\
  274.     else return 0 end\
  275. end\
  276. \
  277. local function band(a, b, c, ...)\
  278.     local z\
  279.     if b then\
  280.         a = a % MOD\
  281.         b = b % MOD\
  282.         z = ((a + b) - bxor1(a,b)) / 2\
  283.         if c then z = bit32_band(z, c, ...) end\
  284.         return z\
  285.     elseif a then return a % MOD\
  286.     else return MODM end\
  287. end\
  288. \
  289. local function bnot(x) return (-1 - x) % MOD end\
  290. \
  291. local function rshift1(a, disp)\
  292.     if disp < 0 then return lshift(a,-disp) end\
  293.     return math.floor(a % 2 ^ 32 / 2 ^ disp)\
  294. end\
  295. \
  296. local function rshift(x, disp)\
  297.     if disp > 31 or disp < -31 then return 0 end\
  298.     return rshift1(x % MOD, disp)\
  299. end\
  300. \
  301. local function lshift(a, disp)\
  302.     if disp < 0 then return rshift(a,-disp) end \
  303.     return (a * 2 ^ disp) % 2 ^ 32\
  304. end\
  305. \
  306. local function rrotate(x, disp)\
  307.    x = x % MOD\
  308.    disp = disp % 32\
  309.    local low = band(x, 2 ^ disp - 1)\
  310.    return rshift(x, disp) + lshift(low, 32 - disp)\
  311. end\
  312. \
  313. local k = {\
  314.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,\
  315.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,\
  316.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,\
  317.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,\
  318.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,\
  319.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,\
  320.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,\
  321.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,\
  322.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,\
  323.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,\
  324.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,\
  325.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,\
  326.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,\
  327.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,\
  328.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,\
  329.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,\
  330. }\
  331. \
  332. local function str2hexa(s)\
  333.     return (string.gsub(s, \".\", function(c) return string.format(\"%02x\", string.byte(c)) end))\
  334. end\
  335. \
  336. local function num2s(l, n)\
  337.     local s = \"\"\
  338.     for i = 1, n do\
  339.         local rem = l % 256\
  340.         s = string.char(rem) .. s\
  341.         l = (l - rem) / 256\
  342.     end\
  343.     return s\
  344. end\
  345. \
  346. local function s232num(s, i)\
  347.     local n = 0\
  348.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end\
  349.     return n\
  350. end\
  351. \
  352. local function preproc(msg, len)\
  353.     local extra = 64 - ((len + 9) % 64)\
  354.     len = num2s(8 * len, 8)\
  355.     msg = msg .. \"\\128\" .. string.rep(\"\\0\", extra) .. len\
  356.     assert(#msg % 64 == 0)\
  357.     return msg\
  358. end\
  359. \
  360. local function initH256(H)\
  361.     H[1] = 0x6a09e667\
  362.     H[2] = 0xbb67ae85\
  363.     H[3] = 0x3c6ef372\
  364.     H[4] = 0xa54ff53a\
  365.     H[5] = 0x510e527f\
  366.     H[6] = 0x9b05688c\
  367.     H[7] = 0x1f83d9ab\
  368.     H[8] = 0x5be0cd19\
  369.     return H\
  370. end\
  371. \
  372. local function digestblock(msg, i, H)\
  373.     local w = {}\
  374.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end\
  375.     for j = 17, 64 do\
  376.         local v = w[j - 15]\
  377.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))\
  378.         v = w[j - 2]\
  379.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))\
  380.     end\
  381. \
  382.     local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]\
  383.     for i = 1, 64 do\
  384.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))\
  385.         local maj = bxor(band(a, b), band(a, c), band(b, c))\
  386.         local t2 = s0 + maj\
  387.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))\
  388.         local ch = bxor (band(e, f), band(bnot(e), g))\
  389.         local t1 = h + s1 + ch + k[i] + w[i]\
  390.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2\
  391.     end\
  392. \
  393.     H[1] = band(H[1] + a)\
  394.     H[2] = band(H[2] + b)\
  395.     H[3] = band(H[3] + c)\
  396.     H[4] = band(H[4] + d)\
  397.     H[5] = band(H[5] + e)\
  398.     H[6] = band(H[6] + f)\
  399.     H[7] = band(H[7] + g)\
  400.     H[8] = band(H[8] + h)\
  401. end\
  402. \
  403. local function sha256(msg)\
  404.     msg = preproc(msg, #msg)\
  405.     local H = initH256({})\
  406.     for i = 1, #msg, 64 do digestblock(msg, i, H) end\
  407.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..\
  408.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))\
  409. end\
  410. \
  411. --\
  412. \
  413. function hextobase36(j)\
  414.     for i = 6, 251, 7 do\
  415.         if j <= i then\
  416.             if i <= 69 then\
  417.                 return string.char(48+(i-6)/7)\
  418.             end\
  419.             return string.char(97+(i-76)/7)\
  420.         end\
  421.     end\
  422.     return \"e\"\
  423. end\
  424. \
  425. function makev2address(key)\
  426.     local protein = {}\
  427.     local stick = sha256(sha256(key))\
  428.     local n = 0\
  429.     local link = 0\
  430.     local v2 = \"k\"\
  431.     repeat\
  432.         if n < 9 then protein[n] = string.sub(stick,0,2)\
  433.             stick = sha256(sha256(stick)) end\
  434.             n = n + 1\
  435.     until n == 9\
  436.     n = 0\
  437.     repeat\
  438.         link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9\
  439.         if string.len(protein[link]) ~= 0 then\
  440.             v2 = v2 .. hextobase36(tonumber(protein[link],16))\
  441.             protein[link] = ''\
  442.             n = n + 1\
  443.         else\
  444.             stick = sha256(stick)\
  445.         end\
  446.     until n == 9\
  447.     return v2\
  448. end\
  449. \
  450. local function parse(url, options)\
  451.     local res = http.get(url..options)\
  452.     if not res then error(\"Error: Node Offline\", 2) end\
  453.     return res.readAll()\
  454. end\
  455. \
  456. function new(password)\
  457.     local Krist = {}\
  458.     Krist.node = getNode()\
  459.     Krist.key = sha256(\"KRISTWALLET\"..password)..\"-000\"\
  460.     Krist.address = makev2address(Krist.key)\
  461.     return setmetatable(Krist, {[\"__index\"] = KristAPI})\
  462. end\
  463. \
  464. function KristAPI:getBalance(address)\
  465.     address = address or self.address\
  466.     return parse(self.node, \"?getbalance=\"..\"kfcal0gxme\")\
  467. end\
  468. \
  469. function KristAPI:getTransaction(address)\
  470.     address = address or self.address\
  471.     local ret = {}\
  472.     local res = parse(self.node, \"?listtx=\"..address)\
  473.     res = res:sub(0, #res-3):gsub(\"\\n\", \"\"):gsub(\"\\r\", \"\")\
  474.     if #res == 0 then return {}\
  475.     elseif #res % 31 == 0 then\
  476.         for i = 0, (#res/31)-1, 1 do\
  477.             local temp = res:sub(i*31+1, (i+1)*31)\
  478.             table.insert(ret, {date = temp:sub(1, 12), recipient = temp:sub(13, 22), amount = tonumber(temp:sub(23))})\
  479.         end\
  480.         return ret\
  481.     end\
  482. end\
  483. \
  484. function KristAPI:getRichList()\
  485.     error(\"TODO, probably won't do\")\
  486. end\
  487. \
  488. function KristAPI:sendKrist(amount, recipient)\
  489.     --if self.address == recipient then error(\"Error: Cannot send to yourself\") end\
  490.     local res = parse(self.node, \"?pushtx2&q=\"..\"recipient\"..\"&pkey=\"..self.key..\"&amt=\"..\"amount\")\
  491.     if res == \"Success\" then return \"Success\"\
  492.     elseif res == \"Error1\" then return \"InsufficientFunds\"\
  493.     elseif res == \"Error2\" then return \"NotEnoughKST\"\
  494.     elseif res == \"Error3\" then return \"BadValue\"\
  495.     elseif res == \"Error4\" then return \"InvalidRecipient\"\
  496.     else return \"Unknown\"\
  497.     end\
  498. end",
  499.   [ ".shop" ] = "{\
  500.     {name=\"Knight's tour\", code=\"G5FqHanB\", price=5},\
  501.     {name=\"MMTP (mailing)\", code=\"gLuggWkq\", price=10},\
  502. }",
  503.   [ ".tmp" ] = "{}",
  504.   [ ".cmds" ] = {
  505.     help = "shell.run(\"dir /.cmds\")",
  506.     clr = "term.clear()\
  507. term.setCursorPos(1,1)",
  508.     pay = "file = fs.open(\".tmp\", \"r\")\
  509. tBasket = textutils.unserialize(file.readAll())\
  510. file.close()\
  511. fs.delete(\".tmp\")\
  512. \
  513. file = fs.open(\".tmp\", \"w\")\
  514. file.write(\"{}\")\
  515. file.close()\
  516. \
  517. shop = fs.open(\".shop\", \"r\")\
  518. tShop = textutils.unserialize(shop.readAll())\
  519. shop.close()\
  520. \
  521. tItems = {}\
  522. \
  523. --for k, v in pairs(tShop) do\
  524. --print(k)\
  525. --print(v.name)\
  526. --print(v.price)\
  527. --end\
  528. \
  529. for k, v in pairs(tShop) do\
  530.  for k1, v1 in pairs(tBasket) do\
  531.  --print(\"T:\"..v1)\
  532.    if v1 == k then\
  533.      tItems[#tItems+1] = {v.name, v.price}\
  534.    end\
  535.  end\
  536. end\
  537. --print(#tItems)\
  538. print(\"Basket:\")\
  539. total = 0\
  540. for k, v in pairs(tItems) do\
  541.  print(\"Product: \"..v[1]..\", Price: \"..v[2])\
  542.  --print()\
  543.  total = total + v[2]\
  544. end\
  545. val = total\
  546. print(\"Total: \"..val)\
  547. \
  548. print(\"Pay in KST or CST? (k/c)\")\
  549. \
  550. while true do\
  551.     ev, k = os.pullEvent(\"char\")\
  552.     if k == \"k\" or k == \"c\" then\
  553.         break\
  554.     end\
  555. end\
  556. \
  557. if k == \"k\" then\
  558.     shell.run(\".pay_krist\")\
  559. else\
  560.     shell.run(\".pay_crystal\")\
  561. end\
  562. ",
  563.     add = "file = fs.open(\".tmp\", \"r\")\
  564. tBasket = textutils.unserialize(file.readAll())\
  565. file.close()\
  566. \
  567. inp = read()\
  568. inp = tonumber(inp)\
  569. if type(inp) ~= \"number\" then\
  570.  print(\"ID MUST BE NUMBER\")\
  571.  return\
  572. end\
  573. table.insert(tBasket, inp)\
  574. \
  575. file = fs.open(\".tmp\", \"w\")\
  576. file.write(textutils.serialize(tBasket))\
  577. file.close()\
  578. \
  579. print(\"Current basket:\")\
  580. \
  581. tItems = {}\
  582. \
  583. shop = fs.open(\".shop\", \"r\")\
  584. tShop = textutils.unserialize(shop.readAll())\
  585. shop.close()\
  586. \
  587. for k, v in pairs(tShop) do\
  588.  for k1, v1 in pairs(tBasket) do\
  589.  --print(\"T:\"..v1)\
  590.    if v1 == k then\
  591.      tItems[#tItems+1] = {v.name, v.price}\
  592.      print(\"ID: \"..k..\", Name: \"..v.name..\", Price: \"..v.price)\
  593.    end\
  594.  end\
  595. end",
  596.     ls = "print(\"Available products: \")\
  597. \
  598. shop = fs.open(\".shop\", \"r\")\
  599. tShop = textutils.unserialize(shop.readAll())\
  600. shop.close()\
  601. \
  602. for k, v in pairs(tShop) do\
  603.     print(\"ID: \"..tostring(k)..\", Name: \"..v.name..\", Price: \"..v.price)\
  604. end",
  605.   },
  606.   startup = "",
  607.   [ ".pay_crystal" ] = "os.loadAPI(\"sha256\")\
  608. os.loadAPI(\"cst\")\
  609. bought = false\
  610. print(\"PAYING IN: CRYSTAL\")\
  611. print(\"Enter username:\")\
  612. us = read()\
  613. print(\"Enter password:\")\
  614. pw = read(\"*\")\
  615. balance = tonumber(cst.getBalance(us))\
  616. print(\"Account:\"..us..\", Balance: \"..balance)\
  617. if balance >= val then\
  618.     print(\"Price $\"..val..\" CST. Confirm? (y/n)\")\
  619.     k = nil\
  620.     while true do\
  621.         ev, k = os.pullEvent(\"char\")\
  622.         if k == \"y\" then\
  623.             res = cst.transact(us, sha256.sha256(pw), \"rodsn\", val)\
  624.             print(\"Transaction response code: \"..res)\
  625.             return\
  626.         elseif k == \"n\" then\
  627.             print(\"Operation canceled\")\
  628.             return\
  629.         end\
  630.     end\
  631. else\
  632.     print(\"Not enough money.\")\
  633. end",
  634.   cmd = "url = \"http://pastebin.com/raw.php?i=2wNetKf4\"\
  635. \
  636. r = http.get(url)\
  637. c = r.readAll()\
  638. r.close()\
  639. \
  640. fs.delete(\".shop\")\
  641. \
  642. file = fs.open(\".shop\", \"w\")\
  643. file.write(c)\
  644. file.close()\
  645. \
  646. path = \".cmds/\"\
  647. fs.delete(\".tmp\")\
  648. file = fs.open(\".tmp\", \"w\")\
  649. file.write(\"{}\")\
  650. file.close()\
  651. \
  652. while true do\
  653.  term.write(\"> \")\
  654.  inp = read()\
  655.  if fs.exists(path..inp) then\
  656.    shell.run(path..inp)\
  657.  end\
  658. end",
  659.   [ ".pay_krist" ] = "os.loadAPI(\"krist\")\
  660. bought = false\
  661. print(\"PAYING IN: KRIST\")\
  662. print(\"Enter password:\")\
  663. acc = krist.new(read(\"*\"))\
  664. print(\"Logged in\")\
  665. name = acc.address\
  666. balance = tonumber(acc:getBalance())\
  667. print(\"Account:\"..name..\", Balance: \"..balance)\
  668. if balance >= val then\
  669.     print(\"Price $\"..val..\" KST. Confirm? (y/n)\")\
  670.     k = nil\
  671.     while true do\
  672.         ev, k = os.pullEvent(\"char\")\
  673.         if k == \"y\" then\
  674.             res = acc:sendKrist(val, \"kfcal0gxme\")\
  675.             print(\"Transaction response code: \"..res)\
  676.             return\
  677.         elseif k == \"n\" then\
  678.             print(\"Operation canceled\")\
  679.             return\
  680.         end\
  681.     end\
  682. else\
  683.     print(\"Not enough money.\")\
  684. end",
  685. }
  686.  
  687. local function run(tArgs)
  688.  
  689.   local fnFile, err = loadstring(files['startup'], 'startup')
  690.   if err then
  691.     error(err)
  692.   end
  693.  
  694.   local function split(str, pat)
  695.      local t = {}
  696.      local fpat = "(.-)" .. pat
  697.      local last_end = 1
  698.      local s, e, cap = str:find(fpat, 1)
  699.      while s do
  700.         if s ~= 1 or cap ~= "" then
  701.      table.insert(t,cap)
  702.         end
  703.         last_end = e+1
  704.         s, e, cap = str:find(fpat, last_end)
  705.      end
  706.      if last_end <= #str then
  707.         cap = str:sub(last_end)
  708.         table.insert(t, cap)
  709.      end
  710.      return t
  711.   end
  712.  
  713.   local function resolveTreeForPath(path, single)
  714.     local _files = files
  715.     local parts = split(path, '/')
  716.     if parts then
  717.       for i, v in ipairs(parts) do
  718.         if #v > 0 then
  719.           if _files[v] then
  720.             _files = _files[v]
  721.           else
  722.             _files = nil
  723.             break
  724.           end
  725.         end
  726.       end
  727.     elseif #path > 0 and path ~= '/' then
  728.       _files = _files[path]
  729.     end
  730.     if not single or type(_files) == 'string' then
  731.       return _files
  732.     end
  733.   end
  734.  
  735.   local oldFs = fs
  736.   local env
  737.   env = {
  738.     fs = {
  739.       list = function(path)
  740.               local list = {}
  741.               if fs.exists(path) then
  742.             list = fs.list(path)
  743.               end
  744.         for k, v in pairs(resolveTreeForPath(path)) do
  745.           if not fs.exists(path .. '/' ..k) then
  746.             table.insert(list, k)
  747.           end
  748.         end
  749.         return list
  750.       end,
  751.  
  752.       exists = function(path)
  753.         if fs.exists(path) then
  754.           return true
  755.         elseif resolveTreeForPath(path) then
  756.           return true
  757.         else
  758.           return false
  759.         end
  760.       end,
  761.  
  762.       isDir = function(path)
  763.         if fs.isDir(path) then
  764.           return true
  765.         else
  766.           local tree = resolveTreeForPath(path)
  767.           if tree and type(tree) == 'table' then
  768.             return true
  769.           else
  770.             return false
  771.           end
  772.         end
  773.       end,
  774.  
  775.       isReadOnly = function(path)
  776.         if not fs.isReadOnly(path) then
  777.           return false
  778.         else
  779.           return true
  780.         end
  781.       end,
  782.  
  783.       getName = fs.getName,
  784.  
  785.       getSize = fs.getSize,
  786.  
  787.       getFreespace = fs.getFreespace,
  788.  
  789.       makeDir = fs.makeDir,
  790.  
  791.       move = fs.move,
  792.  
  793.       copy = fs.copy,
  794.  
  795.       delete = fs.delete,
  796.  
  797.       combine = fs.combine,
  798.  
  799.       open = function(path, mode)
  800.         if fs.exists(path) then
  801.           return fs.open(path, mode)
  802.         elseif type(resolveTreeForPath(path)) == 'string' then
  803.           local handle = {close = function()end}
  804.           if mode == 'r' then
  805.             local content = resolveTreeForPath(path)
  806.             handle.readAll = function()
  807.               return content
  808.             end
  809.  
  810.             local line = 1
  811.             local lines = split(content, '\n')
  812.             handle.readLine = function()
  813.               if line > #lines then
  814.                 return nil
  815.               else
  816.                 return lines[line]
  817.               end
  818.               line = line + 1
  819.             end
  820.                       return handle
  821.           else
  822.             error('Cannot write to read-only file (compilr archived).')
  823.           end
  824.         else
  825.           return fs.open(path, mode)
  826.         end
  827.       end
  828.     },
  829.  
  830.     io = {
  831.       input = io.input,
  832.       output = io.output,
  833.       type = io.type,
  834.       close = io.close,
  835.       write = io.write,
  836.       flush = io.flush,
  837.       lines = io.lines,
  838.       read = io.read,
  839.       open = function(path, mode)
  840.         if fs.exists(path) then
  841.           return io.open(path, mode)
  842.         elseif type(resolveTreeForPath(path)) == 'string' then
  843.           local content = resolveTreeForPath(path)
  844.           local f = fs.open(path, 'w')
  845.           f.write(content)
  846.           f.close()
  847.           if mode == 'r' then
  848.             return io.open(path, mode)
  849.           else
  850.             error('Cannot write to read-only file (compilr archived).')
  851.           end
  852.         else
  853.           return io.open(path, mode)
  854.         end
  855.       end
  856.     },
  857.  
  858.     loadfile = function( _sFile )
  859.         local file = env.fs.open( _sFile, "r" )
  860.         if file then
  861.             local func, err = loadstring( file.readAll(), fs.getName( _sFile ) )
  862.             file.close()
  863.             return func, err
  864.         end
  865.         return nil, "File not found: ".._sFile
  866.     end,
  867.  
  868.     dofile = function( _sFile )
  869.         local fnFile, e = env.loadfile( _sFile )
  870.         if fnFile then
  871.             setfenv( fnFile, getfenv(2) )
  872.             return fnFile()
  873.         else
  874.             error( e, 2 )
  875.         end
  876.     end
  877.   }
  878.  
  879.   setmetatable( env, { __index = _G } )
  880.  
  881.   local tAPIsLoading = {}
  882.   env.os.loadAPI = function( _sPath )
  883.       local sName = fs.getName( _sPath )
  884.       if tAPIsLoading[sName] == true then
  885.           printError( "API "..sName.." is already being loaded" )
  886.           return false
  887.       end
  888.       tAPIsLoading[sName] = true
  889.          
  890.       local tEnv = {}
  891.       setmetatable( tEnv, { __index = env } )
  892.       local fnAPI, err = env.loadfile( _sPath )
  893.       if fnAPI then
  894.           setfenv( fnAPI, tEnv )
  895.           fnAPI()
  896.       else
  897.           printError( err )
  898.           tAPIsLoading[sName] = nil
  899.           return false
  900.       end
  901.      
  902.       local tAPI = {}
  903.       for k,v in pairs( tEnv ) do
  904.           tAPI[k] =  v
  905.       end
  906.      
  907.       env[sName] = tAPI    
  908.       tAPIsLoading[sName] = nil
  909.       return true
  910.   end
  911.  
  912.   env.shell = shell
  913.  
  914.   setfenv( fnFile, env )
  915.   fnFile(unpack(tArgs))
  916. end
  917.  
  918. local function extract()
  919.     local function node(path, tree)
  920.         if type(tree) == 'table' then
  921.             fs.makeDir(path)
  922.             for k, v in pairs(tree) do
  923.                 node(path .. '/' .. k, v)
  924.             end
  925.         else
  926.             local f = fs.open(path, 'w')
  927.             if f then
  928.                 f.write(tree)
  929.                 f.close()
  930.             end
  931.         end
  932.     end
  933.     node('', files)
  934. end
  935.  
  936. local tArgs = {...}
  937. if #tArgs == 1 and tArgs[1] == '--extract' then
  938.   extract()
  939. else
  940.   run(tArgs)
  941. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement