Advertisement
PaymentOption

File Server for cmez

Jul 31st, 2012
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.43 KB | None | 0 0
  1. -- File server for OMNI INC. by PaymentOption --
  2. -- Credits:
  3. --  Main Programmer: PaymentOption
  4. --  Lead Designer: PaymentOption
  5. --  Co Designer: cmez123
  6. --  Funded by: cmez123, iananimatorpro
  7.  
  8. --[[
  9. NOTES:
  10.     TEST_SERVER_ID = 670 (Payment's computer hosting a server in his office at OMNI INC.)
  11.     When sending the list of files and directories, the server sends the name of the file concatenated with '!SP!' and a D or an F depending on the file.
  12.    
  13.     COMMANDS FROM CLIENT:
  14.         list - lists the files in the current directory.
  15.         download - downloads a file
  16.         upload - uploads a file
  17.         delete - ADMIN ONLY: deletes a file
  18.        
  19.         Typical argument structure for functions linked to commands: tCommandArgs[1] = command
  20.                                                                      tCommandArgs[2] = fileName
  21.                                                                      tCommandArgs[3] = sPath
  22. --]]
  23.  
  24. -- VARS --
  25. -- tFiles = {} -- File object structure: name, path
  26. yPos = 5 -- The y position for logged console messages to be displayed.
  27. tAdmins = { 2 } -- Store admin id's here.
  28. tCommandArgs = {}
  29.  
  30. screenWidth, screenHeight = term.getSize()
  31. consolePos = 1
  32.  
  33. VERSION = 1.1
  34. -- END VARS --
  35.  
  36. -- Code taken directly from: http://lua-users.org/wiki/SplitJoin --
  37. -- Compatibility: Lua-5.1
  38. function split(str, pat)
  39.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  40.    local fpat = "(.-)" .. pat
  41.    local last_end = 1
  42.    local s, e, cap = str:find(fpat, 1)
  43.    while s do
  44.       if s ~= 1 or cap ~= "" then
  45.      table.insert(t,cap)
  46.       end
  47.       last_end = e+1
  48.       s, e, cap = str:find(fpat, last_end)
  49.    end
  50.    if last_end <= #str then
  51.       cap = str:sub(last_end)
  52.       table.insert(t, cap)
  53.    end
  54.    return t
  55. end
  56. -- End code snippet --
  57.  
  58. -- Update functions --
  59. local function update(nNewVer)
  60.     local sCode = "dpV3vgmf"
  61.    
  62.     file = fs.open(".serverVersion", "w")
  63.     file.write(nNewVer)
  64.     file.close()
  65.        
  66.     clear()
  67.     printBorder()
  68.     cPrint(6, "***************")
  69.     cPrint(7, "* Updating... *")
  70.     cPrint(8, "***************")
  71.        
  72.     while true do
  73.         local response = http.get(
  74.             "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  75.             )
  76.                
  77.         if response then
  78.             local sResponse = response.readAll()
  79.             response.close()
  80.                
  81.             file = fs.open("/FILE_Server", "w")
  82.             file.write(sResponse)
  83.             file.close()
  84.             return
  85.         else return end
  86.     end
  87. end
  88.  
  89. local function checkForUpdate()
  90.     local sCode = "yQ02nWsz"
  91.        
  92.     clear()
  93.     printBorder()
  94.     cPrint(6, "***********************")
  95.     cPrint(7, "* Checking Version... *")
  96.     cPrint(8, "***********************")
  97.        
  98.     while true do
  99.         local response = http.get(
  100.             "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  101.             )
  102.                
  103.         if response then
  104.             local sResponse = response.readAll()
  105.             response.close()
  106.                
  107.             if VERSION < tonumber(sResponse) then return tonumber(sResponse), true
  108.             else return false end
  109.         else return false end
  110.     end
  111. end
  112. -- END Updating Functions --
  113.  
  114. -- Networking functions (non file related) --
  115. function openExistantModem()
  116.     local tSides = { "front", "back", "top", "bottom", "right", "left" } -- Reset to nil to free memory, if LUA doesn't garbage collect :P.
  117.    
  118.     for i=1, #tSides do
  119.         if peripheral.isPresent( tSides[i] ) and peripheral.getType( tSides[i] ) == "modem" then
  120.             rednet.open( tSides[i] ); tSides = nil; return true
  121.         end
  122.     end
  123.    
  124.     tSides = nil; return false
  125. end
  126.  
  127. function closeExistantModem()
  128.     local tSides = { "front", "back", "top", "bottom", "right", "left" } -- Reset to nil to free memory, if LUA doesn't garbage collect :P.
  129.    
  130.     for i=1, #tSides do
  131.         if peripheral.isPresent( tSides[i] ) and peripheral.getType( tSides[i] ) == "modem" then
  132.             rednet.close( tSides[i] ); tSides = nil; return true
  133.         end
  134.     end
  135.    
  136.     tSides = nil; return false
  137. end
  138.  
  139. function ping( id )
  140.     rednet.send( id, "pingingYouFromServer" )
  141.     local sender, message = rednet.receive( 0.15 )
  142.     if sender == id and message == "respondingToPingFromYou" then return true
  143.     else return false end
  144. end
  145. ---------------------------------------------
  146.  
  147. -- Helpful functions for drawing, this and that --
  148. function writeToLog( msg )
  149.     local file = nil
  150.    
  151.     if not fs.exists( ".fileServerLog" ) then -- If the file doesn't exist then populate it so it can be read without calling a nil function or variable.
  152.         file = fs.open( ".fileServerLog", "w" )
  153.         file.write( "AUTO-GENERATED-LINE" )
  154.         file.close()
  155.     end
  156.    
  157.     file = fs.open( ".fileServerLog", "r" )
  158.     local fileContents = file.readAll()
  159.     file.close()
  160.    
  161.     file = fs.open( ".fileServerLog", "w" )
  162.     file.write( msg .. "\n" .. fileContents )
  163.     file.close()
  164. end
  165.  
  166. function cPrint( height, string )
  167.     term.setCursorPos( screenWidth/2 - string.len( string )/2, height ); term.write( string )
  168. end
  169.  
  170. function rPrint( height, string )
  171.     term.setCursorPos( screenWidth - string.len( string ), height ); term.write( string )
  172. end
  173.  
  174. function clear() term.clear(); term.setCursorPos( 1, 1 ) end
  175.  
  176. function printBorder()
  177.     for i=1, screenHeight do
  178.         term.setCursorPos( 1, i )
  179.         if i == 1 or i == screenHeight then term.write( string.rep( "*", screenWidth ) )
  180.         else term.write( "*" ); term.setCursorPos( screenWidth, i ); term.write( "*" ) end
  181.     end
  182.    
  183.     rPrint( 17, "Version: " .. VERSION )
  184.     rPrint( 16, "~ PaymentOption" )
  185. end
  186.  
  187. function printLogo()
  188.     term.setCursorPos( 3, 3 )
  189.     write( [[    ________                 .__
  190. *     \_____  \   _____   ____ |__|
  191. *      /   |   \ /     \ /    \|  |
  192. *     /    |    \  Y Y  \   |  \  |
  193. *     \_______  /__|_|  /___|  /__|
  194. *             \/      \/     \/
  195. *        .___                  
  196. *        |   | ____   ____      
  197. *        |   |/    \_/ ___\    
  198. *        |   |   |  \  \___    
  199. *        |___|___|  /\___  > /\
  200. *                 \/     \/  \/
  201. ]] )
  202.    
  203. end
  204. --------------------------------------------------
  205.  
  206. -- Networking functions (file related) --
  207. function downloadFile( id, fileName, sPath )
  208.     if ping( id ) then
  209.         if fs.exists( sPath .. "/" .. fileName ) and not fs.isDir( sPath .. "/" .. fileName ) then
  210.             if not sPath then printToConsole( id .. " failed to download " .. fileName  .. "." ); return false
  211.             else
  212.                 local file = fs.open( sPath .. "/" .. fileName, "r" )
  213.                 local fileContents = file.readAll()
  214.                 file.close()
  215.                
  216.                 writeToLog( id .. " successfully downloaded " .. fileName )
  217.                 term.setCursorPos( 4, yPos ); term.write( id .. " successfully downloaded " .. fileName ); yPos = yPos+1
  218.                 rednet.send( id, fileContents )
  219.                 return true
  220.             end
  221.         else return false end
  222.     else
  223.         writeToLog( ( id .. " failed to download " .. fileName ) ); term.setCursorPos( 4, yPos ); term.write( id .. " failed to download " .. fileName ); yPos = yPos+1
  224.         return false
  225.     end
  226. end
  227.  
  228. function uploadFile( id, fileName, sPath )
  229.     if ping( id ) then
  230.         if not fs.exists( sPath .. "/" .. fileName ) and fs.isDir( sPath ) then
  231.             sender, fileContents = rednet.receive( 1 )
  232.            
  233.             if sender == id then
  234.                 local file = fs.open( sPath .. "/" .. fileName, "w" )
  235.                 file.write( fileContents )
  236.                 file.close()
  237.                
  238.                 term.setCursorPos( 4, yPos ); term.write( id .. " successfuly uploaded " .. fileName ); yPos = yPos+1
  239.                 writeToLog( ( id .. " successfuly uploaded " .. fileName ) )
  240.                 return true
  241.             else return false end
  242.         else
  243.             writeToLog( ( id .. " failed to upload " .. fileName ) ); term.setCursorPos( 4, yPos ); term.write( id .. " failed to upload " .. fileName ); yPos = yPos+1
  244.             return false
  245.         end
  246.     else
  247.         writeToLog( ( id .. " failed to upload " .. fileName ) ); term.setCursorPos( 4, yPos ); term.write( id .. " failed to upload " .. fileName ); yPos = yPos+1
  248.         return false
  249.     end
  250. end
  251.  
  252. function sendFilesForDownload( id, sPath )
  253.     if not fs.isDir( sPath ) then rednet.send( id, "NonexistantPath" ); return false end
  254.     local tFilesInPath = fs.list( sPath )
  255.    
  256.     if ping( id ) then
  257.         term.setCursorPos( 4, yPos ); term.write( id .. " accessing " .. sPath ); yPos = yPos+1
  258.         writeToLog( ( id .. " accessing " .. sPath ) )
  259.         for i=1, #tFilesInPath do
  260.             if fs.isDir( sPath .. "/" .. tFilesInPath[i] ) then
  261.                 rednet.send( id, tFilesInPath[i] .. "!SP!" .. "D" ) -- !SP!D for directories, !SP!F for files.
  262.             else
  263.                 rednet.send( id, tFilesInPath[i] .. "!SP!" .. "F" )
  264.             end
  265.         end
  266.     end
  267. end
  268.  
  269. function deleteFile( id, fileName, sPath )
  270.     for i=1, #tAdmins do
  271.         if tAdmins[i] == id then fs.delete( sPath .. "/" .. fileName )
  272.             writeToLog( id .. " successfully deleted " .. fileName )
  273.             term.setCursorPos( 4, yPos ); term.write( id .. " successfully deleted " .. fileName ); yPos = yPos+1
  274.             return true
  275.         end
  276.     end
  277.    
  278.     writeToLog( id .. " attempted to delete " .. fileName )
  279.     return false
  280. end
  281.  
  282. function addDirectory( id, dirName, sPath )
  283.     for i=1, #tAdmins do
  284.         if id == tAdmins[i] then break
  285.         else writeToLog( ( id .. " failed to create " .. dirName ) ); return false end
  286.     end
  287.    
  288.     if not fs.exists( sPath .. "/" .. dirName ) then
  289.         fs.makeDir( sPath .. "/" .. dirName )
  290.         term.setCursorPos( 4, yPos ); term.write( id .. " successfully created " .. dirName ); yPos = yPos+1
  291.         writeToLog( ( id .. " successfully created " .. dirName ) )
  292.         return true
  293.     else term.setCursorPos( 4, yPos ); term.write( id .. " failed to create " .. dirName ); yPos = yPos+1; writeToLog( ( id .. " failed to create " .. dirName ) ); return false end
  294. end
  295. -----------------------------------------
  296.  
  297. openExistantModem()
  298. clear()
  299. printBorder()
  300. printLogo()
  301. sleep(2)
  302.  
  303. newVersion, bNeedsUpdate = checkForUpdate()
  304. if bNeedsUpdate then update(newVersion); shell.run("FILE_Server") end
  305. clear() -- Clear the "Updating" or whatever is there.
  306.  
  307. while true do
  308.     printBorder()
  309.     if yPos >= 13 then -- Clear the section where console commands are printed.
  310.         for i=5, 17 do
  311.             term.setCursorPos( 4, i ); term.write( string.rep( " ", 33 ) )
  312.         end
  313.         yPos = 5
  314.     end
  315.  
  316.     term.setCursorPos( 3, 2 ); print( "Listening for activity..." ); print("*   Console: ")
  317.    
  318.     sender, command = rednet.receive()
  319.    
  320.     -- Process command --
  321.     tCommandArgs = split( command, "!SP!" ) -- Split the arguments for usage.
  322.    
  323.     if tCommandArgs[1] == "list" then
  324.         sendFilesForDownload( sender, tCommandArgs[2] ) -- The second index and beyond is the arguments to the command.
  325.         tCommandArgs = {} -- Reset the command arguments.
  326.     elseif tCommandArgs[1] == "download" then downloadFile( sender, tCommandArgs[2], tCommandArgs[3] )
  327.     elseif tCommandArgs[1] == "upload" then uploadFile( sender, tCommandArgs[2], tCommandArgs[3] )
  328.     elseif tCommandArgs[1] == "delete" then deleteFile( sender, tCommandArgs[2], tCommandArgs[3] )
  329.     elseif tCommandArgs[1] == "addDir" then addDirectory( sender, tCommandArgs[2], tCommandArgs[3] )
  330.     end
  331. end
  332.  
  333. closeExistantModem()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement