Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- File server for OMNI INC. by PaymentOption --
- -- Credits:
- -- Main Programmer: PaymentOption
- -- Lead Designer: PaymentOption
- -- Co Designer: cmez123
- -- Funded by: cmez123, iananimatorpro
- --[[
- NOTES:
- TEST_SERVER_ID = 670 (Payment's computer hosting a server in his office at OMNI INC.)
- 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.
- COMMANDS FROM CLIENT:
- list - lists the files in the current directory.
- download - downloads a file
- upload - uploads a file
- delete - ADMIN ONLY: deletes a file
- Typical argument structure for functions linked to commands: tCommandArgs[1] = command
- tCommandArgs[2] = fileName
- tCommandArgs[3] = sPath
- --]]
- -- VARS --
- -- tFiles = {} -- File object structure: name, path
- yPos = 5 -- The y position for logged console messages to be displayed.
- tAdmins = { 2 } -- Store admin id's here.
- tCommandArgs = {}
- screenWidth, screenHeight = term.getSize()
- consolePos = 1
- VERSION = 1.1
- -- END VARS --
- -- Code taken directly from: http://lua-users.org/wiki/SplitJoin --
- -- Compatibility: Lua-5.1
- function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- -- End code snippet --
- -- Update functions --
- local function update(nNewVer)
- local sCode = "dpV3vgmf"
- file = fs.open(".serverVersion", "w")
- file.write(nNewVer)
- file.close()
- clear()
- printBorder()
- cPrint(6, "***************")
- cPrint(7, "* Updating... *")
- cPrint(8, "***************")
- while true do
- local response = http.get(
- "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
- )
- if response then
- local sResponse = response.readAll()
- response.close()
- file = fs.open("/FILE_Server", "w")
- file.write(sResponse)
- file.close()
- return
- else return end
- end
- end
- local function checkForUpdate()
- local sCode = "yQ02nWsz"
- clear()
- printBorder()
- cPrint(6, "***********************")
- cPrint(7, "* Checking Version... *")
- cPrint(8, "***********************")
- while true do
- local response = http.get(
- "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
- )
- if response then
- local sResponse = response.readAll()
- response.close()
- if VERSION < tonumber(sResponse) then return tonumber(sResponse), true
- else return false end
- else return false end
- end
- end
- -- END Updating Functions --
- -- Networking functions (non file related) --
- function openExistantModem()
- local tSides = { "front", "back", "top", "bottom", "right", "left" } -- Reset to nil to free memory, if LUA doesn't garbage collect :P.
- for i=1, #tSides do
- if peripheral.isPresent( tSides[i] ) and peripheral.getType( tSides[i] ) == "modem" then
- rednet.open( tSides[i] ); tSides = nil; return true
- end
- end
- tSides = nil; return false
- end
- function closeExistantModem()
- local tSides = { "front", "back", "top", "bottom", "right", "left" } -- Reset to nil to free memory, if LUA doesn't garbage collect :P.
- for i=1, #tSides do
- if peripheral.isPresent( tSides[i] ) and peripheral.getType( tSides[i] ) == "modem" then
- rednet.close( tSides[i] ); tSides = nil; return true
- end
- end
- tSides = nil; return false
- end
- function ping( id )
- rednet.send( id, "pingingYouFromServer" )
- local sender, message = rednet.receive( 0.15 )
- if sender == id and message == "respondingToPingFromYou" then return true
- else return false end
- end
- ---------------------------------------------
- -- Helpful functions for drawing, this and that --
- function writeToLog( msg )
- local file = nil
- 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.
- file = fs.open( ".fileServerLog", "w" )
- file.write( "AUTO-GENERATED-LINE" )
- file.close()
- end
- file = fs.open( ".fileServerLog", "r" )
- local fileContents = file.readAll()
- file.close()
- file = fs.open( ".fileServerLog", "w" )
- file.write( msg .. "\n" .. fileContents )
- file.close()
- end
- function cPrint( height, string )
- term.setCursorPos( screenWidth/2 - string.len( string )/2, height ); term.write( string )
- end
- function rPrint( height, string )
- term.setCursorPos( screenWidth - string.len( string ), height ); term.write( string )
- end
- function clear() term.clear(); term.setCursorPos( 1, 1 ) end
- function printBorder()
- for i=1, screenHeight do
- term.setCursorPos( 1, i )
- if i == 1 or i == screenHeight then term.write( string.rep( "*", screenWidth ) )
- else term.write( "*" ); term.setCursorPos( screenWidth, i ); term.write( "*" ) end
- end
- rPrint( 17, "Version: " .. VERSION )
- rPrint( 16, "~ PaymentOption" )
- end
- function printLogo()
- term.setCursorPos( 3, 3 )
- write( [[ ________ .__
- * \_____ \ _____ ____ |__|
- * / | \ / \ / \| |
- * / | \ Y Y \ | \ |
- * \_______ /__|_| /___| /__|
- * \/ \/ \/
- * .___
- * | | ____ ____
- * | |/ \_/ ___\
- * | | | \ \___
- * |___|___| /\___ > /\
- * \/ \/ \/
- ]] )
- end
- --------------------------------------------------
- -- Networking functions (file related) --
- function downloadFile( id, fileName, sPath )
- if ping( id ) then
- if fs.exists( sPath .. "/" .. fileName ) and not fs.isDir( sPath .. "/" .. fileName ) then
- if not sPath then printToConsole( id .. " failed to download " .. fileName .. "." ); return false
- else
- local file = fs.open( sPath .. "/" .. fileName, "r" )
- local fileContents = file.readAll()
- file.close()
- writeToLog( id .. " successfully downloaded " .. fileName )
- term.setCursorPos( 4, yPos ); term.write( id .. " successfully downloaded " .. fileName ); yPos = yPos+1
- rednet.send( id, fileContents )
- return true
- end
- else return false end
- else
- writeToLog( ( id .. " failed to download " .. fileName ) ); term.setCursorPos( 4, yPos ); term.write( id .. " failed to download " .. fileName ); yPos = yPos+1
- return false
- end
- end
- function uploadFile( id, fileName, sPath )
- if ping( id ) then
- if not fs.exists( sPath .. "/" .. fileName ) and fs.isDir( sPath ) then
- sender, fileContents = rednet.receive( 1 )
- if sender == id then
- local file = fs.open( sPath .. "/" .. fileName, "w" )
- file.write( fileContents )
- file.close()
- term.setCursorPos( 4, yPos ); term.write( id .. " successfuly uploaded " .. fileName ); yPos = yPos+1
- writeToLog( ( id .. " successfuly uploaded " .. fileName ) )
- return true
- else return false end
- else
- writeToLog( ( id .. " failed to upload " .. fileName ) ); term.setCursorPos( 4, yPos ); term.write( id .. " failed to upload " .. fileName ); yPos = yPos+1
- return false
- end
- else
- writeToLog( ( id .. " failed to upload " .. fileName ) ); term.setCursorPos( 4, yPos ); term.write( id .. " failed to upload " .. fileName ); yPos = yPos+1
- return false
- end
- end
- function sendFilesForDownload( id, sPath )
- if not fs.isDir( sPath ) then rednet.send( id, "NonexistantPath" ); return false end
- local tFilesInPath = fs.list( sPath )
- if ping( id ) then
- term.setCursorPos( 4, yPos ); term.write( id .. " accessing " .. sPath ); yPos = yPos+1
- writeToLog( ( id .. " accessing " .. sPath ) )
- for i=1, #tFilesInPath do
- if fs.isDir( sPath .. "/" .. tFilesInPath[i] ) then
- rednet.send( id, tFilesInPath[i] .. "!SP!" .. "D" ) -- !SP!D for directories, !SP!F for files.
- else
- rednet.send( id, tFilesInPath[i] .. "!SP!" .. "F" )
- end
- end
- end
- end
- function deleteFile( id, fileName, sPath )
- for i=1, #tAdmins do
- if tAdmins[i] == id then fs.delete( sPath .. "/" .. fileName )
- writeToLog( id .. " successfully deleted " .. fileName )
- term.setCursorPos( 4, yPos ); term.write( id .. " successfully deleted " .. fileName ); yPos = yPos+1
- return true
- end
- end
- writeToLog( id .. " attempted to delete " .. fileName )
- return false
- end
- function addDirectory( id, dirName, sPath )
- for i=1, #tAdmins do
- if id == tAdmins[i] then break
- else writeToLog( ( id .. " failed to create " .. dirName ) ); return false end
- end
- if not fs.exists( sPath .. "/" .. dirName ) then
- fs.makeDir( sPath .. "/" .. dirName )
- term.setCursorPos( 4, yPos ); term.write( id .. " successfully created " .. dirName ); yPos = yPos+1
- writeToLog( ( id .. " successfully created " .. dirName ) )
- return true
- else term.setCursorPos( 4, yPos ); term.write( id .. " failed to create " .. dirName ); yPos = yPos+1; writeToLog( ( id .. " failed to create " .. dirName ) ); return false end
- end
- -----------------------------------------
- openExistantModem()
- clear()
- printBorder()
- printLogo()
- sleep(2)
- newVersion, bNeedsUpdate = checkForUpdate()
- if bNeedsUpdate then update(newVersion); shell.run("FILE_Server") end
- clear() -- Clear the "Updating" or whatever is there.
- while true do
- printBorder()
- if yPos >= 13 then -- Clear the section where console commands are printed.
- for i=5, 17 do
- term.setCursorPos( 4, i ); term.write( string.rep( " ", 33 ) )
- end
- yPos = 5
- end
- term.setCursorPos( 3, 2 ); print( "Listening for activity..." ); print("* Console: ")
- sender, command = rednet.receive()
- -- Process command --
- tCommandArgs = split( command, "!SP!" ) -- Split the arguments for usage.
- if tCommandArgs[1] == "list" then
- sendFilesForDownload( sender, tCommandArgs[2] ) -- The second index and beyond is the arguments to the command.
- tCommandArgs = {} -- Reset the command arguments.
- elseif tCommandArgs[1] == "download" then downloadFile( sender, tCommandArgs[2], tCommandArgs[3] )
- elseif tCommandArgs[1] == "upload" then uploadFile( sender, tCommandArgs[2], tCommandArgs[3] )
- elseif tCommandArgs[1] == "delete" then deleteFile( sender, tCommandArgs[2], tCommandArgs[3] )
- elseif tCommandArgs[1] == "addDir" then addDirectory( sender, tCommandArgs[2], tCommandArgs[3] )
- end
- end
- closeExistantModem()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement