Skip to main content

Ressources GTA V

Nous rejoindre sur Discord

-- Meta class
MailRP = {}

-- Initialisation de la classe Mail RP
function MailRP:new ()
    if self._instance then
        return self._instance
    end

    local instance = setmetatable({}, self)
    local token = 'apiToken' -- change the value with your api token
    self.__index = self
    self._api = "https://mail-rp.com/api/v1/" .. token .. '/'
    --self._api = "http://localhost/api/v1/" .. token .. '/'

    if instance.ctor then
        instance:ctor()
    end

    self._instance = instance
    return self._instanceself
end

-- Envoyer une requête GET
function MailRP:__sendGetRequest(url)url, callBack)
    if callBack and type(callBack) ~= 'table' then
        error('The callBack must be a function and not ' .. type(callBack))
    end

    PerformHttpRequest(self._api .. url, function(errorCode,statusCode, resultData)
        if errorCode ~= 200callBack then
            print("ReturnedcallBack(statusCode, error code:" .. tostring(errorCode) .. ' => ' .. tostring(json.decode(resultData))
            return nil
        else
            return resultData
        end
    end, 'GET')
end

-- Envoyer une requête POST
function MailRP:__sendPostRequest(url, data)data, callBack)
    if type(data) ~= 'table' then
        error('post data must be a table')
    end

    if callBack and type(callBack) ~= 'table' then
        error('The callBack must be a function and not ' .. type(callBack))
    end

    PerformHttpRequest(self._api .. url, function(errorCode,statusCode, resultData)
        if errorCode ~= 200callBack then
            print("ReturnedcallBack(statusCode, error code:" .. tostring(errorCode) .. ' => ' .. tostring(json.decode(resultData))
            return nil
        else
            return resultData
        end
    end, 'POST', json.encode(data), { ['Content-Type'] = 'application/json' })
end

-- Récupérer les informations générales du serveur
function MailRP:getServer()
    returncallBack)
    self:__sendGetRequest('server'), callBack)
end

-- Récupérer la liste desun sous domainesdomaine
function MailRP:getSubdomains(getSubdomain(callBack, name)
    if name == nil then
        return self:__sendGetRequest('subdomains'), callBack)
    else
        if type(name) ~= string then
            error('name attribut must be a string')
        end

        returnif callBack and type(callBack) ~= 'table' then
            error('The callBack must be a function and not ' .. type(callBack))
        end

        self:__sendGetRequest('server/' .. name)name, callBack)
    end
end

-- Récupérer unla liste des sous domainedomaines
function MailRP:getSubdomains(callBack)
    self:getSubdomain(name)
    return self.getSubdomains(name)callBack)
end

-- Ajouter un sous domaine
function MailRP:addSubdomain(callBack, name, public, manager, carnet)
    if name == nil then
        error('name is required !')
    end

    if public == nil then
        error('public is required !')
    end

    if type(name) ~= 'string' then
        error('name must be a string ' .. type(name))
    end

    if type(public) ~= 'boolean' then
        error('public must be a boolean ' .. type(public))
    end

    local data = {
        name = name,
        public = public,
    }

    if manager and type(manager) ~= 'string' then
        error('manager must be a string ' .. type(manager))
    else
        data.manager = manager
    end

    if carnet and type(carnet) ~= 'boolean' then
        error('carnet must be a boolean ' .. type(carnet))
    else
        data.carnet = carnet
    end

    returnif self.callBack and type(callBack) ~= 'table' then
        error('The callBack must be a function and not ' .. type(callBack))
    end

    self:__sendPostRequest(self, 'subdomains/add', data)data, callBack)
end

-- Envoyer un mail
function MailRP:send(self,callBack, from, to, subject, content, confirmOpened, attachements)
    if from == nil then
        error('Sender email is required !')
    end

    if to == nil then
        error('Recipient\'s email is required')
    end

    if subject == nil then
        error('Subject email is required')
    end

    if type(from) ~= 'string' then
        error('from must be a string and not ' .. type(from))
    end

    if type(to) ~= 'string' and type(to) ~= 'table' then
        error('to must be a string or an array (table) and not ' .. type(to))
    end

    if type(subject) ~= 'string' then
        error('subject must be a string and not ' .. type(subject))
    end

    local data = {
        from = from,
        to = to,
        subject = subject
    }

    if content and type(content) ~= 'string' then
        error('content must be a string and not ' .. type(content))
    else
        data.content = content
    end

    if confirmOpened and type(confirmOpened) ~= 'boolean' then
        error('confirmOpened must be a boolean and not ' .. type(confirmOpened))
    else
        data.confirmOpened = confirmOpened
    end

    if attachements and type(attachements) ~= 'table' then
        error('attachements must be a string or an array (table) and not ' .. type(attachements))
    elseif attachements and #attachements > 0 then
        local tempAttachements = {}
        for k in pairs(attachements) do
            if type(attachements[k]) ~= 'string' and type(attachements[k]) ~= 'table' then
                error('value in attachements must be a string or an array (table) and not ' .. type(attachements[k]))
            else
                table.insert(tempAttachements, attachements[k])
            end
        end

        if #tempAttachements > 0 then
            data.attachements = tempAttachements
        end
    end

    returnif self.callBack and type(callBack) ~= 'table' then
        error('The callBack must be a function and not ' .. type(callBack))
    end

    self:__sendPostRequest(self, 'mail/send', data)data, callBack)
end

function MailRP:addAddress(callBack, name, firstname, email, password)
    local params = {
        { var = name, name = 'name' },
        { var = firstname, name = 'firstname' },
        { var = email, name = 'email' },
        { var = password, name = 'password' }
    }
    for p in pairs(params) do
        if params[p].var == nil then
            error(params[p].name .. ' param is required !')
        end

        if type(params[p].var) ~= 'string' then
            error(params[p].name .. ' must be a string and not ' .. type(params[p].var))
        end
    end

    if callBack and type(callBack) ~= 'table' then
        error('The callBack must be a function and not ' .. type(callBack))
    end

    local data = {
        name = name,
        firstname = firstname,
        email = email,
        password = password,
    }

    self:__sendPostRequest(self, 'address/create', data, callBack)
end

function MailRP:getAddress(callBack, email)
    if email == nil then
        error('email param is required !')
    end

    if type(email) ~= 'string' then
        error('email must be a string and not ' .. type(email))
    end

    if callBack and type(callBack) ~= 'table' then
        error('The callBack must be a function and not ' .. type(callBack))
    end

    self:__sendGetRequest('address/get/' .. email, callBack)
end

RegisterNetEvent("xelyos:MailRP") -- For opening the emote menu from another resource.
AddEventHandler("xelyos:MailRP", function(cb)
    cb(MailRP:new())
end)