Advertisement
LDDestroier

mSend String Metatable File Transfer

Feb 5th, 2015
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.05 KB | None | 0 0
  1. --This program uses the String Metatable hack for transfering files without modems.
  2. --[[
  3. THIS PROGRAM HAS BEEN PATCHED AS OF CC 1.7. Sorry.
  4. You will only be able to use this program on servers that have enabled it manually...but good luck on that!
  5. --]]
  6.  
  7. local tArg = {...}
  8. function showControls()
  9.     print("*msend send [filename] <target id>")
  10.     print("*msend receive <filter id>")
  11.     print("*msend about")
  12.     print("*msend update")
  13.     print("[neccessary] <optional>")
  14. end
  15.  
  16. function showAbout()
  17.     print("    mSend was programmed by LDDestroier.")
  18.     print("    mSend is a utility to send and receive individual files from machine to machine across infinite distance and across different multiplayer worlds. It does this by setting a string metatable to the contents of a file, along with some metadata to make sure that the intended recipiant gets the file, then converts it back to a file.")
  19.     os.pullEvent("key")
  20.     print("    mSend can send files to anyone listening and send files to individual computer IDs. mSend can also receive files from anyone, or only receive files from a certain ID.")
  21.     print("    Beware, that not inputting an ID argument will allow anyone to send you a file, or anyone to take your file. For private functions, it is reccommended that you input ID arguments.")
  22.     print("    If there is an update, it is reccommended that you update. LDDestroier put a lot of hard work so that you could easily update it using a simple argument. New updates bring new features, usually.")
  23.     print("    mSend may not work in ComputerCraft 1.7 and later. Sorry.")
  24. end
  25.  
  26. local function makeFile(getFile, name)
  27.     local file = fs.open(name, "w")
  28.     file.write( fileContents )
  29.     file.close()
  30. end
  31.  
  32. local function updateProgram(directory)
  33.     local updateFile = http.get("http://pastebin.com/raw.php?i=ZaenH6wc")
  34.     if fs.exists("/msendupdate") then
  35.         fs.delete("/msendupdate")
  36.     end
  37.     local file = fs.open("/msendupdate", "w")
  38.     local update = updateFile:readAll()
  39.     file.write(update)
  40.     file.close()
  41.     shell.run("msendupdate " .. directory)
  42.     print("done.")
  43.     fs.delete("/msendupdate")
  44.     return true
  45. end
  46.  
  47. local function receive(id, timeout)
  48.     if id == -1 then
  49.         print("Receiving file...ID is " .. os.getComputerID())
  50.     else
  51.         print("Receiving file from " .. id .. "...ID is " .. os.getComputerID())
  52.     end
  53.     while true do
  54.         if id == -1 then
  55.             if getmetatable("").targetFileID == -1 or getmetatable("").targetFileID == os.getComputerID() then
  56.                 receiveFile()
  57.                 return true
  58.             end
  59.         else
  60.             if getmetatable("").senderFileID == tonumber(id) then
  61.                 if getmetatable("").targetFileID == -1 or getmetatable("").targetFileID == os.getComputerID() then
  62.                     receiveFile()
  63.                     return true
  64.                 end
  65.             end
  66.         end
  67.         sleep(0.04)
  68.     end
  69. end
  70.  
  71. function local receiveFile()
  72.     localID = getmetatable("").targetFileID
  73.     fileName = getmetatable("").fileName
  74.     fileContents = getmetatable("").fileContents
  75.     senderFileID = getmetatable("").senderFileID
  76.     if fs.exists(shell.dir() .. fileName) then
  77.         if fileName == "msend" then
  78.             print("'" .. fileName .. "' would overwrite msend! Canceled.")
  79.             return false
  80.         else
  81.             print("'" .. fileName .. "' already exists. Overwrite? (y/n)")
  82.             while true do
  83.                 local event, key = os.pullEvent("char")
  84.                 if key == "n" then
  85.                     print("Cancelled.")
  86.                     return false
  87.                 end
  88.                 if key == "y" then
  89.                     fs.delete(fs.combine(shell.dir(), fileName))
  90.                     makeFile(fileContents, fileName)
  91.                     print(fileName .. " overwritten from " .. senderFileID .. ".")
  92.                     return true
  93.                 end
  94.             end
  95.         end
  96.     else
  97.         makeFile(fileContents, fileName)
  98.         print(fileName .. " received from " .. senderFileID .. ".")
  99.     end
  100. end
  101.  
  102. local function send(name, id)
  103.     if fs.exists(name) == true then
  104.         local file = fs.open(name, "r")
  105.         getmetatable("").targetFileID = tonumber(id)
  106.         getmetatable("").senderFileID = os.getComputerID()
  107.         getmetatable("").fileContents = file.readAll()
  108.         getmetatable("").fileName = name
  109.         sleep(0.05)
  110.         getmetatable("").targetFileID = nil
  111.         getmetatable("").senderFileID = nil
  112.         getmetatable("").fileContents = nil
  113.         getmetatable("").fileName = nil
  114.         getmetatable("").isServing = false
  115.         return true
  116.     else
  117.         error(name .. ": file does not exist")
  118.     end
  119. end
  120.  
  121. if tArg[ 1 ] == nil then
  122.     showControls()
  123.     return false
  124. else
  125.     if tArg[ 1 ] == "about" then
  126.         showAbout()
  127.         return false
  128.     else
  129.         if tArg[ 1 ] == "update" then
  130.             updateProgram(shell.dir())
  131.             return true
  132.         else
  133.             if tArg[ 1 ] == "send" then
  134.                 if tArg[ 3 ] == nil then
  135.                     send(tArg[ 2 ], -1)
  136.                 else
  137.                     if tonumber(tArg[ 3 ]) < 0 then
  138.                         send(tArg[ 2 ], -1)
  139.                     else
  140.                         send(tArg[ 2 ], tArg[ 3 ])
  141.                     end
  142.                 end
  143.             else
  144.                 if tArg[ 1 ] == "receive" then
  145.                     if tArg[ 2 ] == nil then
  146.                         receive(-1)
  147.                     else
  148.                         if tonumber(tArg[ 2 ]) >= 0 then
  149.                             receive(tArg[ 2 ])
  150.                         end
  151.                     end
  152.                 else
  153.                     if tArg[ 2 ] == nil then
  154.                         send(tArg[ 1 ], -1)
  155.                     else
  156.                         if tonumber(tArg[ 2 ]) < 0 then
  157.                             send(tArg[ 1 ], -1)
  158.                         else
  159.                             send(tArg[ 1 ], tArg[ 2 ])
  160.                         end
  161.                     end
  162.                 end
  163.                 return false
  164.             end
  165.         end
  166.     end
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement