Skip to main content

Ressources GTA V

-- 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 = 'monToken'
    self.__index = self
    self._api = "https://mail-rp.com/api/v1/" .. token .. '/'

    if instance.ctor then
        instance:ctor()
    end

    self._instance = instance
    return self._instance
end

function MailRP:__sendGetRequest(url)
    PerformHttpRequest(self._api .. url, function(errorCode, resultData)
        if errorCode then
            print("Returned error code:" .. tostring(errorCode))
            return nil
        else
            return resultData
        end
    end, 'GET')
end

function MailRP:__sendPostRequest(url, data)
    if type(data) ~= table then
        error('post data must be a table')
    end

    PerformHttpRequest(self._api .. url, function(errorCode, resultData)
        if errorCode then
            print("Returned error code:" .. tostring(errorCode))
            return nil
        else
            return resultData
        end
    end, 'POST', data)
end

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

-- Récupérer la liste des sous domaines
function MailRP:getSubdomains(name)
    if name == nil then
        return self:__sendGetRequest('subdomains')
    else
        if type(name) ~= string then
            error('name attribut must be a string')
        end
        return self:__sendGetRequest('server/' .. name)
    end
end

-- Récupérer un sous domaine
function MailRP:getSubdomain(name)
    return self.getSubdomains(name)
end

-- Envoyer un mail
function MailRP:send(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
        print(type(from))
        error('from must be a string')
    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

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

    if confirmOpened and type(confirmOpened) ~= 'boolean' then
        error('confirmOpened must be a boolean and not ' .. type(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 then
        for attachement in attachements do
            if type(attachement) ~= 'string' and type(attachement) ~= 'table' then
                error('value in attachements must be a string or an array (table) and not ' .. type(attachement))
            end
        end
    end

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

    return self.__sendPostRequest('mail/send', data)
end

-- Main
local server = MailRP:new()
server:send("thomas.yaeger@petit-paris.fr", "commandant@police-nationale.fr", "1234")