Advertisement
ChaosBeing

Client API

Mar 4th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. local s = io.open("serverID", r)
  2. local server = s:read()
  3. server = tonumber(server)
  4. s:close()
  5.  
  6. function upload(path)
  7.  
  8.  if not fs.exists(path) then
  9.   print("ERROR: File " .. path .. " does not exist!")
  10.   return
  11.  end
  12.  
  13.  name = os.getComputerLabel()
  14.  
  15.  if name then
  16.   path2 = name .. "/" .. path
  17.  end
  18.  
  19.  path2 = path2 or path
  20.  
  21.  rednet.send(server, "ul." .. path2)
  22.  os.startTimer(1)
  23.  type, ignore, msg = os.pullEvent()
  24.  
  25.  file = io.open(path, "r")
  26.  fileBody = file:read("*a")
  27.  file:close()
  28.  
  29.  if type == "rednet_message" then
  30.   if msg:find("Overwrite?") then
  31.    write(msg)
  32.  
  33.    overwrite = read()
  34.    overwrite:lower()
  35.    rednet.send(server, overwrite)
  36.  
  37.    if overwrite ~= "y" then
  38.     print("Transfer Aborted.")
  39.     return
  40.    end
  41.   end
  42.  end
  43.  
  44.  sleep(0.2)
  45.  rednet.send(server, fileBody)
  46.  os.startTimer(4)
  47.  type, ignore, msg = os.pullEvent()
  48.  
  49.  if type == "rednet_message" then
  50.   if msg == "finished" then
  51.    print("Transfer completed successfully.")
  52.    return
  53.   end
  54.  end
  55.  
  56. print("Errors occured during upload. Please try again.")
  57.  
  58. end
  59.  
  60.  
  61. function download(path)
  62.  
  63.  name = os.getComputerLabel()
  64.  
  65.  if name then
  66.   path2 = name .. "/" .. path
  67.  end
  68.  
  69.  path2 = path2 or path
  70.  
  71.  rednet.send(server, "dl." .. path2)
  72.  ignore, msg = rednet.receive(1)
  73.  
  74.  if msg:sub(1, 6) == "ERROR:" then
  75.   print(msg)
  76.   return
  77.  end
  78.  
  79.  if fs.exists(path) then
  80.   print("File exists. Overwrite? (y/n): ")
  81.  
  82.   overwrite = read()
  83.   overwrite:lower()
  84.   rednet.send(server, overwrite)
  85.  
  86.   if overwrite ~= "y" then
  87.    print("Transfer Aborted.")
  88.    return
  89.   end
  90.  end
  91.  
  92.  file = io.open(path, "w")
  93.  file:write(msg)
  94.  file:close()
  95.  print("Transfer completed successfully.")
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement