diff --git a/drua-tg.lua b/drua-tg.lua new file mode 100644 index 0000000..d487728 --- /dev/null +++ b/drua-tg.lua @@ -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 diff --git a/plugins/administration.lua b/plugins/administration.lua index 4000f03..771b788 100644 --- a/plugins/administration.lua +++ b/plugins/administration.lua @@ -15,11 +15,10 @@ 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- - hammer group will also be banned locally. Bot will now also attempt to kick - via bot API before using tg. Added autobanning after (default) 3 autokicks. - Threshold onfigurable with antiflood. Autokick counters reset within twenty- - four hours. Merged antisquig action into generic. There is no automatic - migration; simply add the following to database.administration: + hammer group will also be banned locally. Added autobanning after (default) + 3 autokicks. Threshold onfigurable with antiflood. Autokick counters reset + within twenty-four hours. Merged antisquig action into generic. There is no + automatic migration; simply add the following to database.administration: autokick_timer = 0 groups[*].flags[6] = false groups[*].autoban = 3 @@ -29,7 +28,7 @@ ]]-- local JSON = require('dkjson') -local drua = dofile('drua-tg/drua-tg.lua') +local drua = dofile('drua-tg.lua') local bindings = require('bindings') local utilities = require('utilities') @@ -114,7 +113,7 @@ administration.antiflood = { video = 20, location = 20, document = 20, - sticker = 40 + sticker = 30 } administration.ranks = { @@ -271,24 +270,18 @@ function administration.init_command(self_) user.do_kick = true user.reason = 'banned' user.output = 'Sorry, you are banned from ' .. msg.chat.title .. '.' - end - - -- antisquig - if group.flags[2] and ( + elseif group.flags[2] and ( -- antisquig msg.text:match(utilities.char.arabic) - or msg.text:match(utilities.char.rtl) - or msg.text:match(utilities.char.flush_right) + or msg.text:match(utilities.char.rtl_override) + or msg.text:match(utilities.char.rtl_mark) ) then user.do_kick = true user.reason = 'antisquig' user.output = administration.flags[2].kicked:gsub('GROUPNAME', msg.chat.title) - end - - -- antisquig++ - if group.flags[3] and ( + elseif group.flags[3] and ( -- antisquig++ msg.from.name:match(utilities.char.arabic) - or msg.from.name:match(utilities.char.rtl) - or msg.from.name:match(utilities.char.flush_right) + or msg.from.name:match(utilities.char.rtl_override) + or msg.from.name:match(utilities.char.rtl_mark) ) then user.do_kick = true user.reason = 'antisquig++' @@ -357,21 +350,15 @@ function administration.init_command(self_) new_user.do_kick = true new_user.reason = 'banned' new_user.output = 'Sorry, you are banned from ' .. msg.chat.title .. '.' - end - - -- antisquig++ - if group.flags[3] and ( + elseif group.flags[3] and ( -- antisquig++ newguy.name:match(utilities.char.arabic) - or newguy.name:match(utilities.char.rtl) - or newguy.name:match(utilities.char.flush_right) + or newguy.name:match(utilities.char.rtl_override) + or newguy.name:match(utilities.char.rtl_mark) ) then new_user.do_kick = true new_user.reason = 'antisquig++' new_user.output = administration.flags[3].kicked:gsub('GROUPNAME', msg.chat.title) - end - - -- antibot - if newguy.username and newguy.username:match('bot$') and group.flags[4] and rank < 2 then + elseif group.flags[4] and newguy.username and newguy.username:match('bot') and rank < 2 then new_user.do_kick = true new_user.reason = 'antibot' end @@ -379,7 +366,6 @@ function administration.init_command(self_) end elseif msg.new_chat_title then - if rank < 3 then drua.rename_chat(msg.chat.id, group.name) else @@ -388,9 +374,7 @@ function administration.init_command(self_) administration.update_desc(self, msg.chat.id) end end - elseif msg.new_chat_photo then - if group.grouptype == 'group' then if rank < 3 then drua.set_photo(msg.chat.id, group.photo) @@ -400,9 +384,7 @@ function administration.init_command(self_) else group.photo = drua.get_photo(msg.chat.id) end - elseif msg.delete_chat_photo then - if group.grouptype == 'group' then if rank < 3 then drua.set_photo(msg.chat.id, group.photo) @@ -412,24 +394,15 @@ function administration.init_command(self_) else group.photo = nil end - end - if new_user ~= user 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) - if new_user.output then - bindings.sendMessage(self, msg.new_chat_participant.id, new_user.output) - end - if msg.chat.type == 'supergroup' then - bindings.unbanChatMember(self, msg.chat.id, msg.from.id) - end + if new_user ~= user and new_user.do_kick 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 + if msg.chat.type == 'supergroup' then + bindings.unbanChatMember(self, msg.chat.id, msg.from.id) end end @@ -443,7 +416,7 @@ function administration.init_command(self_) group.autokicks[msg.from.id_str] = 0 user.do_ban = true 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 @@ -463,7 +436,7 @@ function administration.init_command(self_) 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) bindings.sendMessage(self, msg.new_chat_participant.id, output, true, nil, true) end diff --git a/tg-install.sh b/tg-install.sh index beac33e..a78b911 100755 --- a/tg-install.sh +++ b/tg-install.sh @@ -7,8 +7,9 @@ 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' 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 cd tg ./configure make +echo 'All done! Use ./tg-launch.sh to launch tg. Be sure to log in with your Telegram account.' diff --git a/utilities.lua b/utilities.lua index 2d09cbe..c92d546 100755 --- a/utilities.lua +++ b/utilities.lua @@ -335,8 +335,8 @@ end utilities.char = { zwnj = '‌', arabic = '[\216-\219][\128-\191]', - rtl = '‮', - flush_right = '‏' + rtl_override = '‮', + rtl_mark = '‏' } return utilities