administration.lua: Generic action is optimized.
drua-tg.lua: Now packed with otouto because why keep it in a separate repo?
This commit is contained in:
parent
3203b8fe7e
commit
85c9785099
145
drua-tg.lua
Normal file
145
drua-tg.lua
Normal file
@ -0,0 +1,145 @@
|
|||||||
|
--[[
|
||||||
|
drua-tg
|
||||||
|
A fork of JuanPotato's lua-tg (https://github.com/juanpotato/lua-tg),
|
||||||
|
modified to work more naturally from an API bot.
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
drua = require('drua-tg')
|
||||||
|
drua.IP = 'localhost'
|
||||||
|
drua.PORT = 4567
|
||||||
|
drua.message(chat_id, text)
|
||||||
|
]]--
|
||||||
|
|
||||||
|
local SOCKET = require('socket')
|
||||||
|
|
||||||
|
local comtab = {
|
||||||
|
add = { 'chat_add_user %s %s', 'channel_invite %s %s' },
|
||||||
|
kick = { 'chat_del_user %s %s', 'channel_kick %s %s' },
|
||||||
|
rename = { 'rename_chat %s "%s"', 'rename_channel %s "%s"' },
|
||||||
|
link = { 'export_chat_link %s', 'export_channel_link %s' },
|
||||||
|
photo_set = { 'chat_set_photo %s %s', 'channel_set_photo %s %s' },
|
||||||
|
photo_get = { [0] = 'load_user_photo %s', 'load_chat_photo %s', 'load_channel_photo %s' },
|
||||||
|
info = { [0] = 'user_info %s', 'chat_info %s', 'channel_info %s' }
|
||||||
|
}
|
||||||
|
|
||||||
|
local format_target = function(target)
|
||||||
|
target = tonumber(target)
|
||||||
|
if target < -1000000000000 then
|
||||||
|
target = 'channel#' .. math.abs(target) - 1000000000000
|
||||||
|
return target, 2
|
||||||
|
elseif target < 0 then
|
||||||
|
target = 'chat#' .. math.abs(target)
|
||||||
|
return target, 1
|
||||||
|
else
|
||||||
|
target = 'user#' .. target
|
||||||
|
return target, 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local escape = function(text)
|
||||||
|
text = text:gsub('\\', '\\\\')
|
||||||
|
text = text:gsub('\n', '\\n')
|
||||||
|
text = text:gsub('\t', '\\t')
|
||||||
|
text = text:gsub('"', '\\"')
|
||||||
|
return text
|
||||||
|
end
|
||||||
|
|
||||||
|
local drua = {
|
||||||
|
IP = 'localhost',
|
||||||
|
PORT = 4567
|
||||||
|
}
|
||||||
|
|
||||||
|
drua.send = function(command, do_receive)
|
||||||
|
command = command .. '\n'
|
||||||
|
local s = SOCKET.connect('localhost', 4567)
|
||||||
|
s:send(command)
|
||||||
|
if do_receive then
|
||||||
|
-- Get the size of the output, and get the output.
|
||||||
|
-- Thanks Juan for making this so easy to read. :^)
|
||||||
|
local output = s:receive(tonumber(string.match(s:receive("*l"), "ANSWER (%d+)")))
|
||||||
|
s:close()
|
||||||
|
return output:gsub('\n$', '')
|
||||||
|
end
|
||||||
|
s:close()
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.message = function(target, text)
|
||||||
|
local target = format_target(target)
|
||||||
|
local text = escape(text)
|
||||||
|
local command = 'msg %s "%s"'
|
||||||
|
command = command:format(target, text)
|
||||||
|
return drua.send(command)
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.send_photo = function(target, photo)
|
||||||
|
local target = format_target(target)
|
||||||
|
local command = 'send_photo %s %s'
|
||||||
|
command = command:format(target, photo)
|
||||||
|
return drua.send(command)
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.add_user = function(chat, target)
|
||||||
|
local chat,a = format_target(chat)
|
||||||
|
local target = format_target(target)
|
||||||
|
local command = comtab.add[a]:format(chat, target)
|
||||||
|
return drua.send(command)
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.kick_user = function(chat, target)
|
||||||
|
-- Get the group info so tg will recognize the target.
|
||||||
|
drua.get_info(chat)
|
||||||
|
local chat,a = format_target(chat)
|
||||||
|
local target = format_target(target)
|
||||||
|
local command = comtab.kick[a]:format(chat, target)
|
||||||
|
return drua.send(command)
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.rename_chat = function(chat, name)
|
||||||
|
local chat,a = format_target(chat)
|
||||||
|
local command = comtab.rename[a]:format(chat, name)
|
||||||
|
return drua.send(command)
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.export_link = function(chat)
|
||||||
|
local chat,a = format_target(chat)
|
||||||
|
local command = comtab.link[a]:format(chat)
|
||||||
|
return drua.send(command, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.get_photo = function(chat)
|
||||||
|
local chat,a = format_target(chat)
|
||||||
|
local command = comtab.photo_get[a]:format(chat)
|
||||||
|
local output = drua.send(command, true)
|
||||||
|
if output:match('FAIL') then return false end
|
||||||
|
return output:match('Saved to (.+)')
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.set_photo = function(chat, photo)
|
||||||
|
local chat,a = format_target(chat)
|
||||||
|
local command = comtab.photo_set[a]:format(chat, photo)
|
||||||
|
return drua.send(command)
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.get_info = function(target)
|
||||||
|
local target,a = format_target(target)
|
||||||
|
local command = comtab.info[a]:format(target)
|
||||||
|
return drua.send(command, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.channel_set_admin = function(chat, user, rank)
|
||||||
|
local chat = format_target(chat)
|
||||||
|
local user = format_target(user)
|
||||||
|
local command = 'channel_set_admin %s %s %s'
|
||||||
|
command = command:format(chat, user, rank)
|
||||||
|
return drua.send(command)
|
||||||
|
end
|
||||||
|
|
||||||
|
drua.channel_set_about = function(chat, text)
|
||||||
|
local chat = format_target(chat)
|
||||||
|
local text = escape(text)
|
||||||
|
local command = 'channel_set_about %s "%s"'
|
||||||
|
command = command:format(chat, text)
|
||||||
|
return drua.send(command)
|
||||||
|
end
|
||||||
|
|
||||||
|
return drua
|
@ -15,11 +15,10 @@
|
|||||||
|
|
||||||
1.9 - Added flag antihammer. Groups with antihammer enabled will not be
|
1.9 - Added flag antihammer. Groups with antihammer enabled will not be
|
||||||
affected by global bans. However, users who are hammer'd from an anti-
|
affected by global bans. However, users who are hammer'd from an anti-
|
||||||
hammer group will also be banned locally. Bot will now also attempt to kick
|
hammer group will also be banned locally. Added autobanning after (default)
|
||||||
via bot API before using tg. Added autobanning after (default) 3 autokicks.
|
3 autokicks. Threshold onfigurable with antiflood. Autokick counters reset
|
||||||
Threshold onfigurable with antiflood. Autokick counters reset within twenty-
|
within twenty-four hours. Merged antisquig action into generic. There is no
|
||||||
four hours. Merged antisquig action into generic. There is no automatic
|
automatic migration; simply add the following to database.administration:
|
||||||
migration; simply add the following to database.administration:
|
|
||||||
autokick_timer = 0
|
autokick_timer = 0
|
||||||
groups[*].flags[6] = false
|
groups[*].flags[6] = false
|
||||||
groups[*].autoban = 3
|
groups[*].autoban = 3
|
||||||
@ -29,7 +28,7 @@
|
|||||||
]]--
|
]]--
|
||||||
|
|
||||||
local JSON = require('dkjson')
|
local JSON = require('dkjson')
|
||||||
local drua = dofile('drua-tg/drua-tg.lua')
|
local drua = dofile('drua-tg.lua')
|
||||||
local bindings = require('bindings')
|
local bindings = require('bindings')
|
||||||
local utilities = require('utilities')
|
local utilities = require('utilities')
|
||||||
|
|
||||||
@ -114,7 +113,7 @@ administration.antiflood = {
|
|||||||
video = 20,
|
video = 20,
|
||||||
location = 20,
|
location = 20,
|
||||||
document = 20,
|
document = 20,
|
||||||
sticker = 40
|
sticker = 30
|
||||||
}
|
}
|
||||||
|
|
||||||
administration.ranks = {
|
administration.ranks = {
|
||||||
@ -271,24 +270,18 @@ function administration.init_command(self_)
|
|||||||
user.do_kick = true
|
user.do_kick = true
|
||||||
user.reason = 'banned'
|
user.reason = 'banned'
|
||||||
user.output = 'Sorry, you are banned from ' .. msg.chat.title .. '.'
|
user.output = 'Sorry, you are banned from ' .. msg.chat.title .. '.'
|
||||||
end
|
elseif group.flags[2] and ( -- antisquig
|
||||||
|
|
||||||
-- antisquig
|
|
||||||
if group.flags[2] and (
|
|
||||||
msg.text:match(utilities.char.arabic)
|
msg.text:match(utilities.char.arabic)
|
||||||
or msg.text:match(utilities.char.rtl)
|
or msg.text:match(utilities.char.rtl_override)
|
||||||
or msg.text:match(utilities.char.flush_right)
|
or msg.text:match(utilities.char.rtl_mark)
|
||||||
) then
|
) then
|
||||||
user.do_kick = true
|
user.do_kick = true
|
||||||
user.reason = 'antisquig'
|
user.reason = 'antisquig'
|
||||||
user.output = administration.flags[2].kicked:gsub('GROUPNAME', msg.chat.title)
|
user.output = administration.flags[2].kicked:gsub('GROUPNAME', msg.chat.title)
|
||||||
end
|
elseif group.flags[3] and ( -- antisquig++
|
||||||
|
|
||||||
-- antisquig++
|
|
||||||
if group.flags[3] and (
|
|
||||||
msg.from.name:match(utilities.char.arabic)
|
msg.from.name:match(utilities.char.arabic)
|
||||||
or msg.from.name:match(utilities.char.rtl)
|
or msg.from.name:match(utilities.char.rtl_override)
|
||||||
or msg.from.name:match(utilities.char.flush_right)
|
or msg.from.name:match(utilities.char.rtl_mark)
|
||||||
) then
|
) then
|
||||||
user.do_kick = true
|
user.do_kick = true
|
||||||
user.reason = 'antisquig++'
|
user.reason = 'antisquig++'
|
||||||
@ -357,21 +350,15 @@ function administration.init_command(self_)
|
|||||||
new_user.do_kick = true
|
new_user.do_kick = true
|
||||||
new_user.reason = 'banned'
|
new_user.reason = 'banned'
|
||||||
new_user.output = 'Sorry, you are banned from ' .. msg.chat.title .. '.'
|
new_user.output = 'Sorry, you are banned from ' .. msg.chat.title .. '.'
|
||||||
end
|
elseif group.flags[3] and ( -- antisquig++
|
||||||
|
|
||||||
-- antisquig++
|
|
||||||
if group.flags[3] and (
|
|
||||||
newguy.name:match(utilities.char.arabic)
|
newguy.name:match(utilities.char.arabic)
|
||||||
or newguy.name:match(utilities.char.rtl)
|
or newguy.name:match(utilities.char.rtl_override)
|
||||||
or newguy.name:match(utilities.char.flush_right)
|
or newguy.name:match(utilities.char.rtl_mark)
|
||||||
) then
|
) then
|
||||||
new_user.do_kick = true
|
new_user.do_kick = true
|
||||||
new_user.reason = 'antisquig++'
|
new_user.reason = 'antisquig++'
|
||||||
new_user.output = administration.flags[3].kicked:gsub('GROUPNAME', msg.chat.title)
|
new_user.output = administration.flags[3].kicked:gsub('GROUPNAME', msg.chat.title)
|
||||||
end
|
elseif group.flags[4] and newguy.username and newguy.username:match('bot') and rank < 2 then
|
||||||
|
|
||||||
-- antibot
|
|
||||||
if newguy.username and newguy.username:match('bot$') and group.flags[4] and rank < 2 then
|
|
||||||
new_user.do_kick = true
|
new_user.do_kick = true
|
||||||
new_user.reason = 'antibot'
|
new_user.reason = 'antibot'
|
||||||
end
|
end
|
||||||
@ -379,7 +366,6 @@ function administration.init_command(self_)
|
|||||||
end
|
end
|
||||||
|
|
||||||
elseif msg.new_chat_title then
|
elseif msg.new_chat_title then
|
||||||
|
|
||||||
if rank < 3 then
|
if rank < 3 then
|
||||||
drua.rename_chat(msg.chat.id, group.name)
|
drua.rename_chat(msg.chat.id, group.name)
|
||||||
else
|
else
|
||||||
@ -388,9 +374,7 @@ function administration.init_command(self_)
|
|||||||
administration.update_desc(self, msg.chat.id)
|
administration.update_desc(self, msg.chat.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif msg.new_chat_photo then
|
elseif msg.new_chat_photo then
|
||||||
|
|
||||||
if group.grouptype == 'group' then
|
if group.grouptype == 'group' then
|
||||||
if rank < 3 then
|
if rank < 3 then
|
||||||
drua.set_photo(msg.chat.id, group.photo)
|
drua.set_photo(msg.chat.id, group.photo)
|
||||||
@ -400,9 +384,7 @@ function administration.init_command(self_)
|
|||||||
else
|
else
|
||||||
group.photo = drua.get_photo(msg.chat.id)
|
group.photo = drua.get_photo(msg.chat.id)
|
||||||
end
|
end
|
||||||
|
|
||||||
elseif msg.delete_chat_photo then
|
elseif msg.delete_chat_photo then
|
||||||
|
|
||||||
if group.grouptype == 'group' then
|
if group.grouptype == 'group' then
|
||||||
if rank < 3 then
|
if rank < 3 then
|
||||||
drua.set_photo(msg.chat.id, group.photo)
|
drua.set_photo(msg.chat.id, group.photo)
|
||||||
@ -412,17 +394,9 @@ function administration.init_command(self_)
|
|||||||
else
|
else
|
||||||
group.photo = nil
|
group.photo = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if new_user ~= user then
|
if new_user ~= user and new_user.do_kick then
|
||||||
if new_user.do_ban then
|
|
||||||
administration.kick_user(self, msg.chat.id, msg.new_chat_participant.id, new_user.reason)
|
|
||||||
if new_user.output then
|
|
||||||
bindings.sendMessage(self, msg.new_chat_participant.id, new_user.output)
|
|
||||||
end
|
|
||||||
group.bans[msg.new_chat_participant.id_str] = true
|
|
||||||
elseif new_user.do_kick then
|
|
||||||
administration.kick_user(self, msg.chat.id, msg.new_chat_participant.id, new_user.reason)
|
administration.kick_user(self, msg.chat.id, msg.new_chat_participant.id, new_user.reason)
|
||||||
if new_user.output then
|
if new_user.output then
|
||||||
bindings.sendMessage(self, msg.new_chat_participant.id, new_user.output)
|
bindings.sendMessage(self, msg.new_chat_participant.id, new_user.output)
|
||||||
@ -431,7 +405,6 @@ function administration.init_command(self_)
|
|||||||
bindings.unbanChatMember(self, msg.chat.id, msg.from.id)
|
bindings.unbanChatMember(self, msg.chat.id, msg.from.id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
if group.flags[5] and user.do_kick then
|
if group.flags[5] and user.do_kick then
|
||||||
if group.autokicks[msg.from.id_str] then
|
if group.autokicks[msg.from.id_str] then
|
||||||
@ -443,7 +416,7 @@ function administration.init_command(self_)
|
|||||||
group.autokicks[msg.from.id_str] = 0
|
group.autokicks[msg.from.id_str] = 0
|
||||||
user.do_ban = true
|
user.do_ban = true
|
||||||
user.reason = 'antiflood autoban: ' .. user.reason
|
user.reason = 'antiflood autoban: ' .. user.reason
|
||||||
user.output = 'You have been banned for being autokicked too many times.'
|
user.output = user.output .. '\nYou have been banned for being autokicked too many times.'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -463,7 +436,7 @@ function administration.init_command(self_)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if msg.new_chat_participant and not (new_user.do_kick or new_user.do_ban) then
|
if msg.new_chat_participant and not new_user.do_kick then
|
||||||
local output = administration.get_desc(self, msg.chat.id)
|
local output = administration.get_desc(self, msg.chat.id)
|
||||||
bindings.sendMessage(self, msg.new_chat_participant.id, output, true, nil, true)
|
bindings.sendMessage(self, msg.new_chat_participant.id, output, true, nil, true)
|
||||||
end
|
end
|
||||||
|
@ -7,8 +7,9 @@
|
|||||||
echo 'Requesting root privileges to install necessary packages:'
|
echo 'Requesting root privileges to install necessary packages:'
|
||||||
echo 'libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make'
|
echo 'libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make'
|
||||||
sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make
|
sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make
|
||||||
git clone http://github.com/topkecleon/drua-tg
|
echo 'Compiling tg, test branch.'
|
||||||
git clone http://github.com/vysheng/tg --recursive -b test
|
git clone http://github.com/vysheng/tg --recursive -b test
|
||||||
cd tg
|
cd tg
|
||||||
./configure
|
./configure
|
||||||
make
|
make
|
||||||
|
echo 'All done! Use ./tg-launch.sh to launch tg. Be sure to log in with your Telegram account.'
|
||||||
|
@ -335,8 +335,8 @@ end
|
|||||||
utilities.char = {
|
utilities.char = {
|
||||||
zwnj = '',
|
zwnj = '',
|
||||||
arabic = '[\216-\219][\128-\191]',
|
arabic = '[\216-\219][\128-\191]',
|
||||||
rtl = '',
|
rtl_override = '',
|
||||||
flush_right = ''
|
rtl_mark = ''
|
||||||
}
|
}
|
||||||
|
|
||||||
return utilities
|
return utilities
|
||||||
|
Reference in New Issue
Block a user