Creeper9207

ftpserver

Feb 21st, 2015
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. --Creeper9207 Code: ftp server--
  2. local tArgs = { ... }
  3. rednet.open("back")
  4. function getlist(tdir)
  5. -- Get all the files in the directory
  6. local sDir = shell.dir()
  7. if tdir ~= nil then
  8. sDir = shell.resolve( tdir )
  9. end
  10.  
  11. -- Sort into dirs/files, and calculate column count
  12. local tAll = fs.list( sDir )
  13. local tFiles = {}
  14. local tDirs = {}
  15.  
  16. for n, sItem in pairs( tAll ) do
  17. if string.sub( sItem, 1, 1 ) ~= "." then
  18. local sPath = fs.combine( sDir, sItem )
  19. if fs.isDir( sPath ) then
  20. table.insert( tDirs, sItem )
  21. else
  22. table.insert( tFiles, sItem )
  23. end
  24. end
  25. end
  26. table.sort( tDirs )
  27. table.sort( tFiles )
  28.  
  29. rednet.broadcast("dirs: " .. textutils.serialize(tDirs) .. "files: " .. textutils.serialize(tFiles) .. ":text", "incoming")
  30. end
  31. function transferFile(xFile)
  32. j = string.match(xFile, "(%b::)")
  33. j = string.gsub(j, ":", "")
  34. xFile = xFile:gsub(":" .. j .. ":", "")
  35. if fs.exists(j) then
  36. z = fs.open(j, "r")
  37. y = z.readAll()
  38. z.close()
  39. rednet.broadcast(y .. ":fileincoming:<" .. j .. ">", "incoming")
  40. else
  41. rednet.broadcast("File does not exist:text")
  42. end
  43. end
  44. function recieveFile(mFile)
  45. g = string.match(mFile, "(%b<>)")
  46. f = string.match(mFile, "(%b::)")
  47. f = f:gsub(":", "")
  48. uFile = mFile:gsub(":" .. f .. ":", "")
  49. uFile = uFile:gsub("<Rec>", "")
  50. ow = fs.open(f, "w")
  51. ow.write(uFile)
  52. ow.flush()
  53. ow.close()
  54. rednet.broadcast("Success:text", "incoming")
  55.  
  56. end
  57. if tArgs[1] then
  58. while true do
  59. senderID, msg, dis, protocol = rednet.receive("outgoing")
  60. if string.match(msg, "<") then
  61. msgp = string.match(msg, "(%b<>)")
  62. end
  63. passwd = string.match(msg, "(%b??)")
  64. msg = msg:gsub("?" .. passwd .. "?", "")
  65. passwd = string.gsub(passwd, "?", "")
  66. function verify()
  67. if passwd == tArgs[1] then
  68. else
  69. rednet.broadcast("Login Incorrect:text", "incoming")
  70. verify()
  71. end
  72. end
  73. verify()
  74. function commandrun()
  75. a = shell.run(msg)
  76. rednet.broadcast(tostring(a), "incoming")
  77. print("CMD $EXEC " .. msg)
  78. end
  79. commandr = 0
  80. if msg:match("cmd") then
  81. msg = msg:gsub("cmd ", "")
  82. commandrun()
  83. commandr = 1
  84. end
  85. if commandr == 0 then
  86. if msgp == "<dir>" then
  87. msg = msg:gsub(msgp, "")
  88. print("CMD $DIR " .. msg)
  89. getlist(msg)
  90. elseif msgp == "<Trans>" then
  91. msg = msg:gsub(msgp, "")
  92. print("CMD $GET " .. msg)
  93. transferFile(msg)
  94. elseif msgp == "<Rec>" then
  95. print("CMD $SEND " .. msg)
  96. recieveFile(msg)
  97. else
  98. end
  99. end
  100. end
  101. else
  102. print("usage: ftpserver <password>")
  103. end
Advertisement
Add Comment
Please, Sign In to add comment