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 = '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._instance
end

-- Envoyer une requête GET
function MailRP:__sendGetRequest(url)
    PerformHttpRequest(self._api .. url, function(errorCode, resultData)
        if errorCode ~= 200 then
            print("Returned error code:" .. tostring(errorCode) .. ' => ' .. tostring(resultData))
            return nil
        else
            return resultData
        end
    end, 'GET')
end

-- Envoyer une requête POST
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 ~= 200 then
            print("Returned error code:" .. tostring(errorCode) .. ' => ' .. tostring(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()
    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

-- Ajouter un sous domaine
function MailRP:addSubdomain(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

    return self.__sendPostRequest(self, 'subdomains/add', data)
end

-- Envoyer un mail
function MailRP:send(self, 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

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