@@ -9,6 +9,12 @@
 | 
			
		||||
        drua.PORT = 4567
 | 
			
		||||
        drua.message(chat_id, text)
 | 
			
		||||
 | 
			
		||||
    To run multiple commands on the same TCP session:
 | 
			
		||||
        s = drua.sopen()
 | 
			
		||||
        drua.message(chat_id, text, s)
 | 
			
		||||
        drua.send_photo(chat_id, filename, s)
 | 
			
		||||
        s:close()
 | 
			
		||||
 | 
			
		||||
    Copyright 2015-2016 Juan Potato
 | 
			
		||||
 | 
			
		||||
    Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
@@ -69,71 +75,86 @@ local drua = {
 | 
			
		||||
    PORT = 4567
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function drua.send(command, do_receive)
 | 
			
		||||
function drua.sopen()
 | 
			
		||||
    local s = SOCKET.connect(drua.IP, drua.PORT)
 | 
			
		||||
    assert(s, '\nUnable to connect to tg session.')
 | 
			
		||||
    return s
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.simple(s, command, do_receive)
 | 
			
		||||
    s:send(command..'\n')
 | 
			
		||||
    local output
 | 
			
		||||
    if do_receive then
 | 
			
		||||
        output = string.match(s:receive('*l'), 'ANSWER (%d+)')
 | 
			
		||||
        output = s:receive(tonumber(output)):gsub('\n$', '')
 | 
			
		||||
        s:receive('*l')
 | 
			
		||||
    end
 | 
			
		||||
    s:close()
 | 
			
		||||
    return output
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.message(target, text)
 | 
			
		||||
function drua.send(s, command, do_receive)
 | 
			
		||||
    if s then
 | 
			
		||||
        return drua.simple(s, command, do_receive)
 | 
			
		||||
    else
 | 
			
		||||
        s = drua.sopen()
 | 
			
		||||
        local output = drua.simple(s, command, do_receive)
 | 
			
		||||
        s:close()
 | 
			
		||||
        return output
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.message(target, text, s)
 | 
			
		||||
    target = format_target(target)
 | 
			
		||||
    text = escape(text)
 | 
			
		||||
    local command = 'msg %s "%s"'
 | 
			
		||||
    command = command:format(target, text)
 | 
			
		||||
    return drua.send(command)
 | 
			
		||||
    return drua.send(s, command)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.send_photo(target, photo)
 | 
			
		||||
function drua.send_photo(target, photo, s)
 | 
			
		||||
    target = format_target(target)
 | 
			
		||||
    local command = 'send_photo %s %s'
 | 
			
		||||
    command = command:format(target, photo)
 | 
			
		||||
    return drua.send(command)
 | 
			
		||||
    return drua.send(s, command)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.add_user(chat, target)
 | 
			
		||||
function drua.add_user(chat, target, s)
 | 
			
		||||
    local a
 | 
			
		||||
    chat, a = format_target(chat)
 | 
			
		||||
    target = format_target(target)
 | 
			
		||||
    local command = comtab.add[a]:format(chat, target)
 | 
			
		||||
    return drua.send(command)
 | 
			
		||||
    return drua.send(s, command)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.kick_user(chat, target)
 | 
			
		||||
function drua.kick_user(chat, target, s)
 | 
			
		||||
    -- Get the group info so tg will recognize the target.
 | 
			
		||||
    drua.get_info(chat)
 | 
			
		||||
    drua.get_info(chat, s)
 | 
			
		||||
    local a
 | 
			
		||||
    chat, a = format_target(chat)
 | 
			
		||||
    target = format_target(target)
 | 
			
		||||
    local command = comtab.kick[a]:format(chat, target)
 | 
			
		||||
    return drua.send(command)
 | 
			
		||||
    return drua.send(s, command)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.rename_chat(chat, name)
 | 
			
		||||
function drua.rename_chat(chat, name, s)
 | 
			
		||||
    local a
 | 
			
		||||
    chat, a = format_target(chat)
 | 
			
		||||
    local command = comtab.rename[a]:format(chat, name)
 | 
			
		||||
    return drua.send(command)
 | 
			
		||||
    return drua.send(s, command)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.export_link(chat)
 | 
			
		||||
function drua.export_link(chat, s)
 | 
			
		||||
    local a
 | 
			
		||||
    chat, a = format_target(chat)
 | 
			
		||||
    local command = comtab.link[a]:format(chat)
 | 
			
		||||
    return drua.send(command, true)
 | 
			
		||||
    return drua.send(s, command, true)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.get_photo(chat)
 | 
			
		||||
function drua.get_photo(chat, s)
 | 
			
		||||
    local a
 | 
			
		||||
    chat, a = format_target(chat)
 | 
			
		||||
    local command = comtab.photo_get[a]:format(chat)
 | 
			
		||||
    local output = drua.send(command, true)
 | 
			
		||||
    local output = drua.send(s, command, true)
 | 
			
		||||
    if output:match('FAIL') then
 | 
			
		||||
        return false
 | 
			
		||||
    else
 | 
			
		||||
@@ -141,42 +162,42 @@ function drua.get_photo(chat)
 | 
			
		||||
    end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.set_photo(chat, photo)
 | 
			
		||||
function drua.set_photo(chat, photo, s)
 | 
			
		||||
    local a
 | 
			
		||||
    chat, a = format_target(chat)
 | 
			
		||||
    local command = comtab.photo_set[a]:format(chat, photo)
 | 
			
		||||
    return drua.send(command)
 | 
			
		||||
    return drua.send(s, command)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.get_info(target)
 | 
			
		||||
function drua.get_info(target, s)
 | 
			
		||||
    local a
 | 
			
		||||
    target, a = format_target(target)
 | 
			
		||||
    local command = comtab.info[a]:format(target)
 | 
			
		||||
    return drua.send(command, true)
 | 
			
		||||
    return drua.send(s, command, true)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.channel_set_admin(chat, user, rank)
 | 
			
		||||
function drua.channel_set_admin(chat, user, rank, s)
 | 
			
		||||
    chat = format_target(chat)
 | 
			
		||||
    user = format_target(user)
 | 
			
		||||
    local command = 'channel_set_admin %s %s %s'
 | 
			
		||||
    command = command:format(chat, user, rank)
 | 
			
		||||
    return drua.send(command)
 | 
			
		||||
    return drua.send(s, command)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.channel_set_about(chat, text)
 | 
			
		||||
function drua.channel_set_about(chat, text, s)
 | 
			
		||||
    chat = format_target(chat)
 | 
			
		||||
    text = escape(text)
 | 
			
		||||
    local command = 'channel_set_about %s "%s"'
 | 
			
		||||
    command = command:format(chat, text)
 | 
			
		||||
    return drua.send(command)
 | 
			
		||||
    return drua.send(s, command)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.block(user)
 | 
			
		||||
    return drua.send('block_user user#' .. user)
 | 
			
		||||
function drua.block(user, s)
 | 
			
		||||
    return drua.send(s, 'block_user user#' .. user)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function drua.unblock(user)
 | 
			
		||||
    return drua.send('unblock_user user#' .. user)
 | 
			
		||||
function drua.unblock(user, s)
 | 
			
		||||
    return drua.send(s, 'unblock_user user#' .. user)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
return drua
 | 
			
		||||
 
 | 
			
		||||
@@ -340,8 +340,8 @@ function administration:update_desc(chat, config)
 | 
			
		||||
    drua.channel_set_about(chat, desc)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
function administration:kick_user(chat, target, reason, config)
 | 
			
		||||
    drua.kick_user(chat, target)
 | 
			
		||||
function administration:kick_user(chat, target, reason, config, s)
 | 
			
		||||
    drua.kick_user(chat, target, s)
 | 
			
		||||
    local victim = target
 | 
			
		||||
    if self.database.users[tostring(target)] then
 | 
			
		||||
        victim = utilities.build_name(
 | 
			
		||||
@@ -812,6 +812,7 @@ function administration.init_command(self_, config_)
 | 
			
		||||
                local targets = administration.get_targets(self, msg, config)
 | 
			
		||||
                if targets then
 | 
			
		||||
                    local output = ''
 | 
			
		||||
                    local s = drua.sopen()
 | 
			
		||||
                    for _, target in ipairs(targets) do
 | 
			
		||||
                        if target.err then
 | 
			
		||||
                            output = output .. target.err .. '\n'
 | 
			
		||||
@@ -819,12 +820,13 @@ function administration.init_command(self_, config_)
 | 
			
		||||
                            output = output .. target.name .. ' is too privileged to be kicked.\n'
 | 
			
		||||
                        else
 | 
			
		||||
                            output = output .. target.name .. ' has been kicked.\n'
 | 
			
		||||
                            administration.kick_user(self, msg.chat.id, target.id, 'kicked by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']', config)
 | 
			
		||||
                            administration.kick_user(self, msg.chat.id, target.id, 'kicked by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']', config, s)
 | 
			
		||||
                            if msg.chat.type == 'supergroup' then
 | 
			
		||||
                                bindings.unbanChatMember{ chat_id = msg.chat.id, user_id = target.id }
 | 
			
		||||
                            end
 | 
			
		||||
                        end
 | 
			
		||||
                    end
 | 
			
		||||
                    s:close()
 | 
			
		||||
                    utilities.send_reply(msg, output)
 | 
			
		||||
                else
 | 
			
		||||
                    utilities.send_reply(msg, 'Please specify a user or users via reply, username, or ID.')
 | 
			
		||||
@@ -844,6 +846,7 @@ function administration.init_command(self_, config_)
 | 
			
		||||
                local targets = administration.get_targets(self, msg, config)
 | 
			
		||||
                if targets then
 | 
			
		||||
                    local output = ''
 | 
			
		||||
                    local s = drua.sopen()
 | 
			
		||||
                    for _, target in ipairs(targets) do
 | 
			
		||||
                        if target.err then
 | 
			
		||||
                            output = output .. target.err .. '\n'
 | 
			
		||||
@@ -853,11 +856,12 @@ function administration.init_command(self_, config_)
 | 
			
		||||
                            output = output .. target.name .. ' is too privileged to be banned.\n'
 | 
			
		||||
                        else
 | 
			
		||||
                            output = output .. target.name .. ' has been banned.\n'
 | 
			
		||||
                            administration.kick_user(self, msg.chat.id, target.id, 'banned by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']', config)
 | 
			
		||||
                            administration.kick_user(self, msg.chat.id, target.id, 'banned by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']', config, s)
 | 
			
		||||
                            group.mods[target.id_str] = nil
 | 
			
		||||
                            group.bans[target.id_str] = true
 | 
			
		||||
                        end
 | 
			
		||||
                    end
 | 
			
		||||
                    s:close()
 | 
			
		||||
                    utilities.send_reply(msg, output)
 | 
			
		||||
                else
 | 
			
		||||
                    utilities.send_reply(msg, 'Please specify a user or users via reply, username, or ID.')
 | 
			
		||||
@@ -1115,7 +1119,11 @@ function administration.init_command(self_, config_)
 | 
			
		||||
                            output = 'Not a valid message type or number.'
 | 
			
		||||
                        elseif key == 'autoban' then
 | 
			
		||||
                            group.autoban = tonumber(val)
 | 
			
		||||
                            output = 'Users will now be autobanned after *' .. val .. '* autokicks.'
 | 
			
		||||
                            output = string.format(
 | 
			
		||||
                                'Users will now be automatically banned after *%s* automatic kick%s.',
 | 
			
		||||
                                val,
 | 
			
		||||
                                group.autoban == 1 and '' or 's'
 | 
			
		||||
                            )
 | 
			
		||||
                        else
 | 
			
		||||
                            group.antiflood[key] = tonumber(val)
 | 
			
		||||
                            output = '*' .. key:gsub('^%l', string.upper) .. '* messages are now worth *' .. val .. '* points.'
 | 
			
		||||
@@ -1130,11 +1138,13 @@ function administration.init_command(self_, config_)
 | 
			
		||||
usage: `%santiflood <type> <i>`
 | 
			
		||||
example: `%santiflood text 5`
 | 
			
		||||
Use this command to configure the point values for each message type. When a user reaches 100 points, he is kicked. The points are reset each minute. The current values are:
 | 
			
		||||
%s
 | 
			
		||||
%sUsers are automatically banned after *%s* automatic kick%s.
 | 
			
		||||
                            ]],
 | 
			
		||||
                            config.cmd_pat,
 | 
			
		||||
                            config.cmd_pat,
 | 
			
		||||
                            output
 | 
			
		||||
                            output,
 | 
			
		||||
                            group.autoban,
 | 
			
		||||
                            group.autoban == 1 and '' or 's'
 | 
			
		||||
                        )
 | 
			
		||||
                    end
 | 
			
		||||
                    utilities.send_message(msg.chat.id, output, true, msg.message_id, true)
 | 
			
		||||
@@ -1154,6 +1164,7 @@ Use this command to configure the point values for each message type. When a use
 | 
			
		||||
                local targets = administration.get_targets(self, msg, config)
 | 
			
		||||
                if targets then
 | 
			
		||||
                    local output = ''
 | 
			
		||||
                    local s = drua.sopen()
 | 
			
		||||
                    for _, target in ipairs(targets) do
 | 
			
		||||
                        if target.err then
 | 
			
		||||
                            output = output .. target.err .. '\n'
 | 
			
		||||
@@ -1168,11 +1179,12 @@ Use this command to configure the point values for each message type. When a use
 | 
			
		||||
                            if group.grouptype == 'supergroup' then
 | 
			
		||||
                                local chat_member = bindings.getChatMember{ chat_id = msg.chat.id, user_id = target.id }
 | 
			
		||||
                                if chat_member and chat_member.result.status == 'member' then
 | 
			
		||||
                                    drua.channel_set_admin(msg.chat.id, target.id, 2)
 | 
			
		||||
                                    drua.channel_set_admin(msg.chat.id, target.id, 2, s)
 | 
			
		||||
                                end
 | 
			
		||||
                            end
 | 
			
		||||
                        end
 | 
			
		||||
                    end
 | 
			
		||||
                    s:close()
 | 
			
		||||
                    utilities.send_reply(msg, output)
 | 
			
		||||
                else
 | 
			
		||||
                    utilities.send_reply(msg, 'Please specify a user or users via reply, username, or ID.')
 | 
			
		||||
@@ -1192,6 +1204,7 @@ Use this command to configure the point values for each message type. When a use
 | 
			
		||||
                local targets = administration.get_targets(self, msg, config)
 | 
			
		||||
                if targets then
 | 
			
		||||
                    local output = ''
 | 
			
		||||
                    local s = drua.sopen()
 | 
			
		||||
                    for _, target in ipairs(targets) do
 | 
			
		||||
                        if target.err then
 | 
			
		||||
                            output = output .. target.err .. '\n'
 | 
			
		||||
@@ -1203,10 +1216,11 @@ Use this command to configure the point values for each message type. When a use
 | 
			
		||||
                                group.mods[target.id_str] = nil
 | 
			
		||||
                            end
 | 
			
		||||
                            if group.grouptype == 'supergroup' then
 | 
			
		||||
                                drua.channel_set_admin(msg.chat.id, target.id, 0)
 | 
			
		||||
                                drua.channel_set_admin(msg.chat.id, target.id, 0, s)
 | 
			
		||||
                            end
 | 
			
		||||
                        end
 | 
			
		||||
                    end
 | 
			
		||||
                    s:close()
 | 
			
		||||
                    utilities.send_reply(msg, output)
 | 
			
		||||
                else
 | 
			
		||||
                    utilities.send_reply(msg, 'Please specify a user or users via reply, username, or ID.')
 | 
			
		||||
@@ -1295,6 +1309,7 @@ Use this command to configure the point values for each message type. When a use
 | 
			
		||||
                local targets = administration.get_targets(self, msg, config)
 | 
			
		||||
                if targets then
 | 
			
		||||
                    local output = ''
 | 
			
		||||
                    local s = drua.sopen()
 | 
			
		||||
                    for _, target in ipairs(targets) do
 | 
			
		||||
                        if target.err then
 | 
			
		||||
                            output = output .. target.err .. '\n'
 | 
			
		||||
@@ -1307,12 +1322,10 @@ Use this command to configure the point values for each message type. When a use
 | 
			
		||||
                                local reason = 'hammered by ' .. utilities.build_name(msg.from.first_name, msg.from.last_name) .. ' [' .. msg.from.id .. ']'
 | 
			
		||||
                                administration.kick_user(self, msg.chat.id, target.id, reason, config)
 | 
			
		||||
                            end
 | 
			
		||||
                            if #targets == 1 then
 | 
			
		||||
                                for k,v in pairs(self.database.administration.groups) do
 | 
			
		||||
                                    if not v.flags[6] then
 | 
			
		||||
                                        v.mods[target.id_str] = nil
 | 
			
		||||
                                        bindings.kickChatMember{chat_id = k, user_id = target.id}
 | 
			
		||||
                                    end
 | 
			
		||||
                            for k,v in pairs(self.database.administration.groups) do
 | 
			
		||||
                                if not v.flags[6] then
 | 
			
		||||
                                    v.mods[target.id_str] = nil
 | 
			
		||||
                                    drua.kick_user(k, target.id, s)
 | 
			
		||||
                                end
 | 
			
		||||
                            end
 | 
			
		||||
                            self.database.administration.globalbans[target.id_str] = true
 | 
			
		||||
@@ -1325,6 +1338,7 @@ Use this command to configure the point values for each message type. When a use
 | 
			
		||||
                            end
 | 
			
		||||
                        end
 | 
			
		||||
                    end
 | 
			
		||||
                    s:close()
 | 
			
		||||
                    utilities.send_reply(msg, output)
 | 
			
		||||
                else
 | 
			
		||||
                    utilities.send_reply(msg, 'Please specify a user or users via reply, username, or ID.')
 | 
			
		||||
@@ -1405,6 +1419,7 @@ Use this command to configure the point values for each message type. When a use
 | 
			
		||||
                local targets = administration.get_targets(self, msg, config)
 | 
			
		||||
                if targets then
 | 
			
		||||
                    local output = ''
 | 
			
		||||
                    local s = drua.sopen()
 | 
			
		||||
                    for _, target in ipairs(targets) do
 | 
			
		||||
                        if target.err then
 | 
			
		||||
                            output = output .. target.err .. '\n'
 | 
			
		||||
@@ -1413,13 +1428,14 @@ Use this command to configure the point values for each message type. When a use
 | 
			
		||||
                        else
 | 
			
		||||
                            for chat_id, group in pairs(self.database.administration.groups) do
 | 
			
		||||
                                if group.grouptype == 'supergroup' then
 | 
			
		||||
                                    drua.channel_set_admin(chat_id, target.id, 0)
 | 
			
		||||
                                    drua.channel_set_admin(chat_id, target.id, 0, s)
 | 
			
		||||
                                end
 | 
			
		||||
                            end
 | 
			
		||||
                            self.database.administration.admins[target.id_str] = nil
 | 
			
		||||
                            output = output .. target.name .. ' is no longer an administrator.\n'
 | 
			
		||||
                        end
 | 
			
		||||
                    end
 | 
			
		||||
                    s:close()
 | 
			
		||||
                    utilities.send_reply(msg, output)
 | 
			
		||||
                else
 | 
			
		||||
                    utilities.send_reply(msg, 'Please specify a user or users via reply, username, or ID.')
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user