Advertisement
PaymentOption

File Client for cmez

Jul 31st, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.26 KB | None | 0 0
  1. -- File client 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.     A ping from the server consists of the message "pingingYouFromServer" to which the client should respond with "respondingToPingFromYou".
  11.     CODE:
  12.         function ping( id )
  13.             rednet.send( id, "pingingYouFromServer" )
  14.             local sender, message = rendet.receive( 0.15 )
  15.             if sender == id and message == "respondingToPingFromYou" then return true
  16.             else return false end
  17.         end
  18. --]]
  19.  
  20. -- VARS --
  21. sCurrentLocationInServer = "/" -- The current path or "location" that the user is located in at any given time.
  22. tFilesInLocation = {}
  23. temp = {}
  24. nSelect = 1
  25. nSubSelect = 1 -- For sub menues, like the commands menu.
  26. scroll = 0 -- For the scrolling of menues.
  27.  
  28. bInFileMenu = false
  29. bConnected = false -- True if you're currently viewing files on a server.
  30. nServerID = nil
  31. bRunning = true
  32.  
  33. VERSION = 1.12
  34. screenWidth, screenHeight = term.getSize()
  35. ----------
  36.  
  37. -- Helper Functions --
  38. -- Code taken directly from: http://lua-users.org/wiki/SplitJoin --
  39. -- Compatibility: Lua-5.1
  40. function split(str, pat)
  41.    local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  42.    local fpat = "(.-)" .. pat
  43.    local last_end = 1
  44.    local s, e, cap = str:find(fpat, 1)
  45.    while s do
  46.       if s ~= 1 or cap ~= "" then
  47.      table.insert(t,cap)
  48.       end
  49.       last_end = e+1
  50.       s, e, cap = str:find(fpat, last_end)
  51.    end
  52.    if last_end <= #str then
  53.       cap = str:sub(last_end)
  54.       table.insert(t, cap)
  55.    end
  56.    return t
  57. end
  58. -- End code snippet --
  59.  
  60. function limitRead( nLength, cReplaceChar )
  61.     term.setCursorBlink( true )
  62.    
  63.     nLength = nLength or -1 -- -1 is unlimited
  64.     sReturnString = ""
  65.    
  66.     xPos, yPos = term.getCursorPos()
  67.    
  68.     while true do
  69.         event, char = os.pullEvent()
  70.        
  71.         if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check
  72.        
  73.         if event == "char" then sReturnString = sReturnString .. char
  74.         elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
  75.         elseif event == "key" and char == 14 then -- Backspace
  76.             term.setCursorPos( xPos, yPos )
  77.             term.write( string.rep( " ", string.len( sReturnString ) ) )
  78.             sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
  79.             term.setCursorPos( xPos, yPos )
  80.            
  81.             if not cReplaceChar then term.write( sReturnString )
  82.             else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
  83.         end
  84.        
  85.         term.setCursorPos( xPos, yPos )
  86.         term.write( string.rep( " ", string.len( sReturnString ) ) )
  87.         term.setCursorPos( xPos, yPos )
  88.         if not cReplaceChar then term.write( sReturnString )
  89.         else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
  90.     end
  91. end
  92.  
  93. function cPrint( height, string )
  94.     term.setCursorPos( screenWidth/2 - string.len( string )/2, height ); term.write( string )
  95. end
  96.  
  97. function rPrint( height, string )
  98.     term.setCursorPos( screenWidth - string.len( string ), height ); term.write( string )
  99. end
  100.  
  101. function clear() term.clear(); term.setCursorPos( 1, 1 ) end
  102.  
  103. function printBorder()
  104.     for i=1, screenHeight do
  105.         term.setCursorPos( 1, i )
  106.         if i == 1 or i == screenHeight then term.write( string.rep( "*", screenWidth ) )
  107.         else term.write( "*" .. string.rep( " ", screenWidth - 2 ) .. "*" ) end
  108.     end
  109. end
  110.  
  111. function printLogo()
  112.     term.setCursorPos( 3, 3 )
  113.     write( [[    ________                 .__
  114. *     \_____  \   _____   ____ |__|
  115. *      /   |   \ /     \ /    \|  |
  116. *     /    |    \  Y Y  \   |  \  |
  117. *     \_______  /__|_|  /___|  /__|
  118. *             \/      \/     \/
  119. *        .___                  
  120. *        |   | ____   ____      
  121. *        |   |/    \_/ ___\    
  122. *        |   |   |  \  \___    
  123. *        |___|___|  /\___  > /\
  124. *                 \/     \/  \/
  125. ]] )
  126.  
  127.     rPrint( 17, "Version: " .. VERSION )
  128.     rPrint( 16, "~ PaymentOption" )
  129.    
  130. end
  131.  
  132. function checkDirs()
  133.     if not fs.isDir( "Files" ) then fs.makeDir( "Files" ) end
  134. end
  135. ----------------------
  136.  
  137. -- Update functions --
  138. local function update(nNewVer)
  139.     local sCode = "xWTMwPnx"
  140.    
  141.     file = fs.open(".serverVersion", "w")
  142.     file.write(nNewVer)
  143.     file.close()
  144.        
  145.     clear()
  146.     printBorder()
  147.     cPrint(6, "***************")
  148.     cPrint(7, "* Updating... *")
  149.     cPrint(8, "***************")
  150.        
  151.     while true do
  152.         local response = http.get(
  153.             "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  154.             )
  155.                
  156.         if response then
  157.             local sResponse = response.readAll()
  158.             response.close()
  159.                
  160.             file = fs.open("/FILE_Client", "w")
  161.             file.write(sResponse)
  162.             file.close()
  163.             return
  164.         else return end
  165.     end
  166. end
  167.  
  168. local function checkForUpdate()
  169.     local sCode = "R4ujbMuL"
  170.        
  171.     clear()
  172.     printBorder()
  173.     cPrint(6, "***********************")
  174.     cPrint(7, "* Checking Version... *")
  175.     cPrint(8, "***********************")
  176.        
  177.     while true do
  178.         local response = http.get(
  179.             "http://pastebin.com/raw.php?i="..textutils.urlEncode( sCode )
  180.             )
  181.                
  182.         if response then
  183.             local sResponse = response.readAll()
  184.             response.close()
  185.                
  186.             if VERSION < tonumber(sResponse) then return tonumber(sResponse), true
  187.             else return false end
  188.         else return false end
  189.     end
  190. end
  191. -- END Updating Functions --
  192.  
  193. -- Networking functions (non file related) --
  194. function openExistantModem()
  195.     local tSides = { "front", "back", "top", "bottom", "right", "left" } -- Reset to nil to free memory, if LUA doesn't garbage collect :P.
  196.    
  197.     for i=1, #tSides do
  198.         if peripheral.isPresent( tSides[i] ) and peripheral.getType( tSides[i] ) == "modem" then
  199.             rednet.open( tSides[i] ); tSides = nil; return true
  200.         end
  201.     end
  202.    
  203.     tSides = nil; return false
  204. end
  205.  
  206. function closeExistantModem()
  207.     local tSides = { "front", "back", "top", "bottom", "right", "left" } -- Reset to nil to free memory, if LUA doesn't garbage collect :P.
  208.    
  209.     for i=1, #tSides do
  210.         if peripheral.isPresent( tSides[i] ) and peripheral.getType( tSides[i] ) == "modem" then
  211.             rednet.close( tSides[i] ); tSides = nil; return true
  212.         end
  213.     end
  214.    
  215.     tSides = nil; return false
  216. end
  217.  
  218. function respondToPing( id )
  219.     sender, ping = rednet.receive( 0.5 )
  220.     if sender == id and ping == "pingingYouFromServer" then rednet.send( id, "respondingToPingFromYou" ); return true
  221.     else return false end
  222. end
  223.  
  224. function connectToServer( id )
  225.     rednet.send( id, "list!SP!" .. sCurrentLocationInServer )
  226.    
  227.     if respondToPing( id ) then
  228.         os.startTimer( 1 )
  229.         while true do
  230.             event, param1, param2 = os.pullEvent()
  231.            
  232.             if event == "rednet_message" and param1 == id then
  233.                 t = split( param2, "!SP!" )
  234.                 tFilesInLocation[#tFilesInLocation+1] = { name = t[1], objType = t[2] }
  235.                 t = {}
  236.             elseif event == "timer" then break end
  237.         end
  238.         nServerID = id
  239.         bConnected = true
  240.        
  241.         return true
  242.     else return false end
  243. end
  244. ---------------------------------------------
  245.  
  246. -- Networking functions (file related) --
  247. function addDirectory( sName )
  248.     rednet.send( nServerID, "addDir!SP!" .. sName .. "!SP!" .. sCurrentLocationInServer )
  249.     return true
  250. end
  251.  
  252. function changeLocation( sNewPath )
  253.     rednet.send( nServerID, "list!SP!" .. sNewPath )
  254.    
  255.     if respondToPing( nServerID ) then
  256.         tFilesInLocation = {}
  257.         nSelect = 1
  258.         sCurrentLocationInServer = sNewPath
  259.         os.startTimer( 1 )
  260.         while true do
  261.             event, param1, param2 = os.pullEvent()
  262.                        
  263.             if event == "rednet_message" and param1 == nServerID then
  264.                 t = split( param2, "!SP!" )
  265.                 tFilesInLocation[#tFilesInLocation+1] = { name = t[1], objType = t[2] }
  266.                 t = {}
  267.             elseif event == "timer" then break end
  268.         end
  269.        
  270.         return true
  271.     else return false end
  272. end
  273.  
  274. function downloadFile( sFileName )
  275.     if bConnected then
  276.         if fs.exists( "Files/" .. sFileName ) then return false end
  277.         rednet.send( nServerID, "download!SP!" .. sFileName .. "!SP!" .. sCurrentLocationInServer )
  278.        
  279.         if respondToPing( nServerID ) then
  280.             os.startTimer( 1 )
  281.            
  282.             while true do
  283.                 event, param1, param2 = os.pullEvent()
  284.                
  285.                 if event == "rednet_message" and param1 == nServerID then
  286.                     local file = fs.open( "Files/" .. sFileName, "w" )
  287.                     file.write( param2 )
  288.                     file.close()
  289.                     return true
  290.                 elseif event == "timer" then return false end
  291.             end
  292.         else return false end
  293.     else return false end
  294. end
  295.  
  296. function uploadFile( sFileName, sPath )
  297.     if bConnected then
  298.         if not fs.exists( sFileName ) then return false end
  299.         rednet.send( nServerID, "upload!SP!" .. sFileName .. "!SP!" .. sCurrentLocationInServer )
  300.        
  301.         if respondToPing( nServerID ) then
  302.             local file = fs.open( sPath .. "/" .. sFileName, "r" )
  303.             local fileContents = file.readAll()
  304.             file.close()
  305.            
  306.             rednet.send( nServerID, fileContents )
  307.             return true
  308.         else return false end
  309.     else return false end
  310. end
  311.  
  312. function deleteFile( fileName )
  313.     rednet.send( nServerID, "delete!SP!" .. fileName .. "!SP!" .. sCurrentLocationInServer )
  314.     return true
  315. end
  316. -----------------------------------------
  317.  
  318. -- Menues and displays --
  319. function removeLogFileFromList( t )
  320.     for i=1, #t do
  321.         if sCurrentLocationInServer == "/" and t[i].name == ".fileServerLog" then table.remove( t, i ); return true end
  322.     end
  323.    
  324.     return false
  325. end
  326.  
  327. function neutralizeROM( t )
  328.     for i=1, #t do
  329.         if sCurrentLocationInServer == "rom" then t[i].objType = "ROM"
  330.         else return false end
  331.     end
  332.    
  333.     return true
  334. end
  335.  
  336. function displayFiles()
  337.     if bConnected then
  338.         local y = 3
  339.        
  340.         rPrint( 17, "Path: " .. sCurrentLocationInServer )
  341.        
  342.         removeLogFileFromList( tFilesInLocation ) -- Remove the log file from the table, just so we don't download it ;)
  343.         neutralizeROM( tFilesInLocation ) -- Make it so we can't do anything to the ROM files.
  344.        
  345.         for i=1 + scroll, #tFilesInLocation do
  346.             term.setCursorPos( 3, y )
  347.             if nSelect == i then term.write( "[" .. tFilesInLocation[i].name .. "]" )
  348.             else                 term.write( " " .. tFilesInLocation[i].name .. " " ) end
  349.            
  350.             term.setCursorPos( 40, y )
  351.             if tFilesInLocation[i].objType == "F" then term.write( "File" )
  352.             elseif tFilesInLocation[i].objType == "D" then term.write( "Dir." )
  353.             elseif tFilesInLocation[i].objType == "ROM" then term.write( "ROM" ) end
  354.            
  355.             if scroll <= 0 and #tFilesInLocation > 13 then term.setCursorPos( 2, 17 ); term.write( "More..." ) end
  356.             y = y+1
  357.             if y == 16 then break end
  358.         end
  359.     else return end
  360. end
  361.  
  362. function displayConnectToServer()
  363.     local temp_ServerID = nil
  364.    
  365.     cPrint( 6, "*************" )
  366.     cPrint( 7, "* Server ID *" )
  367.     cPrint( 8, "*************" )
  368.     cPrint(10, "ID: ")
  369.    
  370.     while not bConnected do
  371.         term.setCursorPos( screenWidth/2 - string.len( "ID: " )/2 + 4, 10 )
  372.         if temp_ServerID then string.rep( " ", string.len( temp_ServerID ) ); term.setCursorPos( screenWidth/2 - string.len( "ID: " )/2 + 4, 10 ) end
  373.         temp_ServerID = limitRead( 12 )
  374.         if not tonumber( temp_ServerID ) then
  375.             cPrint( 13, "******************" )
  376.             cPrint( 14, "* Not a valid ID *" )
  377.             cPrint( 15, "******************" )
  378.             sleep( 2 )
  379.            
  380.             for i=13, 16 do
  381.                 if i == 15 then break end
  382.                 cPrint( i, string.rep( " ", string.len( "**********************" ) ) )
  383.             end
  384.         else
  385.             if connectToServer( tonumber( temp_ServerID ) ) then bConnected = true
  386.             else
  387.                 cPrint( 13, "**********************" )
  388.                 cPrint( 14, "* Connection Failure *" )
  389.                 cPrint( 15, "**********************" )
  390.                 sleep( 2 )
  391.             end
  392.         end
  393.     end
  394. end
  395.  
  396. function uploadMenu() -- Requires path attached to file name.
  397.     term.setCursorPos( 2, 16 ); term.write(string.rep( " ", screenWidth-5 ))
  398.     term.setCursorPos( 2, 16 ); term.write( "Filename: " )
  399.     term.setCursorPos( 2 + string.len( "Filename: " ), 16 )
  400.     local temp_FileToUpload = limitRead( 14 )
  401.     local posInString = string.find( temp_FileToUpload, fs.getName( temp_FileToUpload ) ) -- Get the position that the file path ends.
  402.     local sPath = string.sub( temp_FileToUpload, 1, posInString - 1 ) -- Seperate the file path from the file name.
  403.    
  404.     if fs.exists( sPath .. ( fs.getName( temp_FileToUpload ) ) ) then
  405.         if uploadFile( temp_FileToUpload, sPath ) then
  406.             term.setCursorPos( 2, 16 ); term.write(string.rep( " ", screenWidth-5 ))
  407.             term.setCursorPos( 2, 16 ); term.write( "Successfully uploaded." ); sleep( 2 )
  408.             changeLocation( sCurrentLocationInServer ) -- Refresh the files on screen.
  409.             bInFileMenu = false; return
  410.         else
  411.             term.setCursorPos( 2, 16 ); term.write(string.rep( " ", screenWidth-5 ))
  412.             term.setCursorPos( 2, 16 ); term.write( "Upload failure." ); sleep( 2 )
  413.             bInFileMenu = false; return
  414.         end
  415.     else
  416.         term.setCursorPos( 2, 16 ); term.write(string.rep( " ", screenWidth-5 ))
  417.         term.setCursorPos( 2, 16 ); term.write( "File does not exist." ); sleep( 2 )
  418.         bInfileMenu = false; return
  419.     end
  420. end
  421.  
  422. function newDirMenu()
  423.     term.setCursorPos( 2, 16 ); term.write(string.rep( " ", screenWidth-5 ))
  424.     term.setCursorPos( 2, 16 ); term.write( "Dir name: " )
  425.     term.setCursorPos( 2 + string.len( "Dir name: " ), 16 )
  426.     local temp_DirToCreate = limitRead( 14 )
  427.    
  428.     if addDirectory( temp_DirToCreate ) then
  429.         term.setCursorPos( 2, 16 ); term.write(string.rep( " ", screenWidth-5 ))
  430.         term.setCursorPos( 2, 16 ); term.write( "Attempted to create directory." ); sleep( 2 )
  431.         changeLocation( sCurrentLocationInServer )
  432.         return
  433.     end
  434. end
  435.  
  436. function fileMenu() -- Should probably get around to this lol.
  437.     while bInFileMenu do
  438.         term.setCursorPos( 3, 16 ); term.write(string.rep( " ", screenWidth-5 ))
  439.         term.setCursorPos( 3, 16 )
  440.         if nSubSelect == 1 then term.write( "[ Download ]  Delete" )
  441.     elseif nSubSelect == 2 then term.write( "  Download  [ Delete ]" ) end
  442.  
  443.         event, key = os.pullEvent( "key" )
  444.        
  445.         if key == 203 and nSubSelect > 1 then nSubSelect = nSubSelect - 1
  446.         elseif key == 205 and nSubSelect < 2 then nSubSelect = nSubSelect + 1
  447.         elseif key == 28 then
  448.             if nSubSelect == 1 then
  449.                 if downloadFile( tFilesInLocation[nSelect].name ) then
  450.                     term.setCursorPos( 3, 16 ); term.write( string.rep( " ", screenWidth-5 ))
  451.                     term.setCursorPos( 3, 16 ); term.write( "Successfull download." ); sleep( 1.3 )
  452.                     bInFileMenu = false
  453.                     return
  454.                 else
  455.                     term.setCursorPos( 3, 16 ); term.write( string.rep( " ", screenWidth-5 ))
  456.                     term.setCursorPos( 3, 16 ); term.write( "Failed download." ); sleep( 1.3 )
  457.                     bInFileMenu = false
  458.                     return
  459.                 end
  460.             elseif nSubSelect == 2 then
  461.                 if deleteFile( tFilesInLocation[nSelect].name ) then
  462.                     term.setCursorPos( 3, 16 ); term.write( string.rep( " ", screenWidth-5 ))
  463.                     term.setCursorPos( 3, 16 ); term.write( "Successfully deleted." ); sleep( 1.3 )
  464.                     changeLocation( sCurrentLocationInServer ) -- Refresh the files on screen.
  465.                     bInFileMenu = false
  466.                     return
  467.                 else
  468.                     term.setCursorPos( 3, 17 ); term.write( string.rep( " ", screenWidth-5 ))
  469.                     term.setCursorPos( 3, 17 ); term.write( "Failed deletion." ); sleep( 1.3 )
  470.                     bInFileMenu = false
  471.                     return
  472.                 end
  473.             end
  474.         elseif key == 14 then bInFileMenu = false; return end
  475.     end
  476. end
  477.  
  478. function handleDisplayInput( key ) -- Using the new 'keys' API
  479.     if key == 208 and nSelect < #tFilesInLocation then
  480.             if nSelect >= 13 and scroll < #tFilesInLocation then scroll = scroll+1; nSelect = nSelect+1
  481.             else nSelect = nSelect+1 end
  482.         elseif key == 200 and nSelect > 1 then
  483.             if nSelect >= 13 and scroll > 0 then scroll = scroll-1; nSelect = nSelect-1
  484.             else nSelect = nSelect-1 end
  485.     elseif key == 28 then
  486.         if tFilesInLocation[nSelect].objType == "D" then changeLocation( tFilesInLocation[nSelect].name )
  487.         elseif tFilesInLocation[nSelect].objType == "F" then bInFileMenu = true; fileMenu() end
  488.     elseif key == 14 and sCurrentLocationInServer ~= "/" then
  489.         changeLocation( "/" )
  490.     elseif key == 22 then uploadMenu() -- 'U'
  491.     elseif key == 18 then bRunning = false
  492.     elseif key == 32 then newDirMenu()
  493.     end
  494. end
  495. -------------------------
  496.  
  497. newVersion, bNeedsUpdate = checkForUpdate()
  498. if bNeedsUpdate then update( newVersion ); shell.run( "FILE_Client" ) end
  499. checkDirs()
  500. openExistantModem()
  501.  
  502. while bRunning do -- Main loop
  503.     clear()
  504.     printBorder()
  505.     if not bConnected then displayConnectToServer(); clear(); printBorder() end
  506.    
  507.     displayFiles()
  508.     term.setCursorPos( 2, 16 ); term.write( "D - New Dir" )
  509.     term.setCursorPos( 2, 17 ); term.write( "U - Upload | E - Exit" )
  510.     event, key = os.pullEvent( "key" )
  511.     if not bInFileMenu then handleDisplayInput( key )
  512.     else fileMenu() end
  513. end
  514.  
  515. closeExistantModem()
  516. clear()
  517. print( os.version() )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement