administration.lua:

Switched to drua-tg.lua.
  Groups are now listed according to most recent activity.
whoami.lua:
  Displays channel/supergroup IDs properly.
This commit is contained in:
topkecleon 2016-03-27 08:30:41 -04:00
parent 9377107508
commit 5762e12a48
6 changed files with 65 additions and 108 deletions

View File

@ -220,7 +220,7 @@ Bot output from every plugin should follow a consistent style. This style is eas
Titles should be either **bold** (along with their colons) or a [link](http://otou.to) (with plaintext colons) to the content's source. Names should be _italic_. Numbered lists should use bold numbers followed by a bold period followed by a space. Unnumbered lists should use the • bullet point followed by a space. Descriptions and information should be in plaintext, although "flavor" text should be italic. Technical information should be `monospace`. Links should be named. Titles should be either **bold** (along with their colons) or a [link](http://otou.to) (with plaintext colons) to the content's source. Names should be _italic_. Numbered lists should use bold numbers followed by a bold period followed by a space. Unnumbered lists should use the • bullet point followed by a space. Descriptions and information should be in plaintext, although "flavor" text should be italic. Technical information should be `monospace`. Links should be named.
## Contributors ## Contributors
Everybody is free to contribute to otouto. If you are interested, you are invited to fork the [repo](http://github.com/topkecleon/otouto) and start making pull requests.. If you have an idea and you are not sure how to implement it, open an issue or bring it up in the Bot Development group. Everybody is free to contribute to otouto. If you are interested, you are invited to fork the [repo](http://github.com/topkecleon/otouto) and start making pull requests. If you have an idea and you are not sure how to implement it, open an issue or bring it up in the Bot Development group.
The creator and maintainer of otouto is [topkecleon](http://github.com/topkecleon). He can be contacted via [Telegram](http://telegram.me/topkecleon), [Twitter](http://twitter.com/topkecleon), or [email](mailto:drew@otou.to). The creator and maintainer of otouto is [topkecleon](http://github.com/topkecleon). He can be contacted via [Telegram](http://telegram.me/topkecleon), [Twitter](http://twitter.com/topkecleon), or [email](mailto:drew@otou.to).

View File

@ -4,6 +4,7 @@
assert(HTTPS) assert(HTTPS)
assert(JSON) assert(JSON)
assert(URL)
local BASE_URL = 'https://api.telegram.org/bot' .. config.bot_api_key local BASE_URL = 'https://api.telegram.org/bot' .. config.bot_api_key

View File

@ -55,20 +55,6 @@ Send /help to get started.
'thank you' 'thank you'
} }
}, },
moderation = {
admins = {
['00000000'] = 'You'
},
errors = {
antisquig = 'This group is English-only.',
moderation = 'I do not moderate this group.',
not_mod = 'This command must be run by a moderator.',
not_admin = 'This command must be run by an administrator.',
},
admin_group = -00000000,
realm_name = 'My Realm',
antisquig = false
},
plugins = { plugins = {
'control.lua', 'control.lua',
'blacklist.lua', 'blacklist.lua',

View File

@ -1,6 +1,6 @@
--[[ --[[
administration.lua administration.lua
Version 1.5 Version 1.6
Part of the otouto project. Part of the otouto project.
© 2016 topkecleon <drew@otou.to> © 2016 topkecleon <drew@otou.to>
GNU General Public License, version 2 GNU General Public License, version 2
@ -17,13 +17,16 @@
stored in a "groups" array rather than at the top level. Global data is now stored in a "groups" array rather than at the top level. Global data is now
stored at the top level rather than in a "global" array. Automatic migration stored at the top level rather than in a "global" array. Automatic migration
will occur in versions 1.5 and 1.6. will occur in versions 1.5 and 1.6.
/groups will now list groups according to activity.
]]-- ]]--
-- Build the administration db if nonexistent. -- Build the administration db if nonexistent.
if not database.administration then if not database.administration then
database.administration = { database.administration = {
admins = {}, admins = {},
groups = {} groups = {},
activity = {}
} }
end end
@ -50,8 +53,17 @@ for k,v in pairs(database.administration.groups) do
v.rules = v.rules or {} v.rules = v.rules or {}
end end
local sender = dofile('lua-tg/sender.lua') -- Migration code: Remove this in v1.8.
tg = sender('localhost', config.cli_port) -- Most recent group activity is now cached for group listings.
if not database.administration.activity then
database.administration.activity = {}
for k,v in pairs(database.administration.groups) do
table.insert(database.administration.activity, k)
end
end
drua = dofile('drua-tg/drua-tg.lua')
drua.PORT = config.cli_port or 4567
local flags = { local flags = {
[1] = { [1] = {
@ -138,53 +150,6 @@ local get_target = function(msg)
end end
local kick_user = function(chat, target)
chat = math.abs(chat)
if chat > 1000000000000 then
chat = chat - 1000000000000
tg:_send('channel_kick channel#' .. chat .. ' user#' .. target .. '\n')
else
tg:_send('chat_del_user chat#' .. chat .. ' user#' .. target .. '\n')
end
end
local get_photo = function(chat)
chat = math.abs(chat)
local filename
if chat > 1000000000000 then
chat = chat - 1000000000000
filename = tg:_send('load_channel_photo channel#' .. chat .. '\n', true)
else
filename = tg:_send('load_chat_photo chat#' .. chat .. '\n', true)
end
if filename:find('FAIL') then
print('Error downloading photo for group ' .. chat .. '.')
return
end
filename = filename:gsub('Saved to ', '')
return filename
end
local get_link = function(chat)
chat = math.abs(chat)
if chat > 1000000000000 then
chat = chat - 1000000000000
return tg:_send('export_channel_link channel#' .. chat .. '\n', true)
else
return tg:_send('export_chat_link chat#' .. chat .. '\n', true)
end
end
local get_desc = function(chat_id) local get_desc = function(chat_id)
local group = database.administration.groups[tostring(chat_id)] local group = database.administration.groups[tostring(chat_id)]
@ -234,16 +199,14 @@ local commands = {
if not group.flags[2] then if not group.flags[2] then
return true return true
end end
kick_user(msg.chat.id, msg.from.id) drua.kick_user(msg.chat.id, msg.from.id)
local output = flags[2].kicked:gsub('GROUPNAME', msg.chat.title) local output = flags[2].kicked:gsub('GROUPNAME', msg.chat.title)
sendMessage(msg.from.id, output) sendMessage(msg.from.id, output)
end end
}, },
{ -- generic { -- generic
triggers = { triggers = { '' },
''
},
privilege = 0, privilege = 0,
interior = true, interior = true,
@ -254,7 +217,7 @@ local commands = {
-- banned -- banned
if rank == 0 then if rank == 0 then
kick_user(msg.chat.id, msg.from.id) drua.kick_user(msg.chat.id, msg.from.id)
sendMessage(msg.from.id, 'Sorry, you are banned from ' .. msg.chat.title .. '.') sendMessage(msg.from.id, 'Sorry, you are banned from ' .. msg.chat.title .. '.')
return return
end end
@ -264,7 +227,7 @@ local commands = {
-- antisquig Strict -- antisquig Strict
if group.flags[3] == true then if group.flags[3] == true then
if msg.from.name:match('[\216-\219][\128-\191]') or msg.from.name:match('') or msg.from.name:match('') then if msg.from.name:match('[\216-\219][\128-\191]') or msg.from.name:match('') or msg.from.name:match('') then
kick_user(msg.chat.id, msg.from.id) drua.kick_user(msg.chat.id, msg.from.id)
local output = flags[3].kicked:gsub('GROUPNAME', msg.chat.title) local output = flags[3].kicked:gsub('GROUPNAME', msg.chat.title)
sendMessage(msg.from.id, output) sendMessage(msg.from.id, output)
return return
@ -282,7 +245,7 @@ local commands = {
-- banned -- banned
if get_rank(msg.new_chat_participant.id, msg.chat.id) == 0 then if get_rank(msg.new_chat_participant.id, msg.chat.id) == 0 then
kick_user(msg.chat.id, msg.new_chat_participant.id) drua.kick_user(msg.chat.id, msg.new_chat_participant.id)
sendMessage(msg.new_chat_participant.id, 'Sorry, you are banned from ' .. msg.chat.title .. '.') sendMessage(msg.new_chat_participant.id, 'Sorry, you are banned from ' .. msg.chat.title .. '.')
return return
end end
@ -290,7 +253,7 @@ local commands = {
-- antisquig Strict -- antisquig Strict
if group.flags[3] == true then if group.flags[3] == true then
if msg.new_chat_participant.name:match('[\216-\219][\128-\191]') or msg.new_chat_participant.name:match('') or msg.new_chat_participant.name:match('') then if msg.new_chat_participant.name:match('[\216-\219][\128-\191]') or msg.new_chat_participant.name:match('') or msg.new_chat_participant.name:match('') then
kick_user(msg.chat.id, msg.new_chat_participant.id) drua.kick_user(msg.chat.id, msg.new_chat_participant.id)
local output = flags[3].kicked:gsub('GROUPNAME', msg.chat.title) local output = flags[3].kicked:gsub('GROUPNAME', msg.chat.title)
sendMessage(msg.new_chat_participant.id, output) sendMessage(msg.new_chat_participant.id, output)
return return
@ -300,7 +263,7 @@ local commands = {
-- antibot -- antibot
if msg.new_chat_participant.username and msg.new_chat_participant.username:match('bot$') then if msg.new_chat_participant.username and msg.new_chat_participant.username:match('bot$') then
if rank < 2 and group.flags[4] == true then if rank < 2 and group.flags[4] == true then
kick_user(msg.chat.id, msg.new_chat_participant.id) drua.kick_user(msg.chat.id, msg.new_chat_participant.id)
return return
end end
else else
@ -312,7 +275,7 @@ local commands = {
elseif msg.new_chat_title then elseif msg.new_chat_title then
if rank < 3 then if rank < 3 then
tg:rename_chat(msg.chat.id, group.name) drua.rename_chat(msg.chat.id, group.name)
else else
group.name = msg.new_chat_title group.name = msg.new_chat_title
end end
@ -322,12 +285,12 @@ local commands = {
if group.grouptype == 'group' then if group.grouptype == 'group' then
if rank < 3 then if rank < 3 then
tg:chat_set_photo(msg.chat.id, group.photo) drua.set_photo(msg.chat.id, group.photo)
else else
group.photo = get_photo(msg.chat.id) group.photo = drua.get_photo(msg.chat.id)
end end
else else
group.photo = get_photo(msg.chat.id) group.photo = drua.get_photo(msg.chat.id)
end end
return return
@ -335,7 +298,7 @@ local commands = {
if group.grouptype == 'group' then if group.grouptype == 'group' then
if rank < 3 then if rank < 3 then
tg:chat_set_photo(msg.chat.id, group.photo) drua.set_photo(msg.chat.id, group.photo)
else else
group.photo = nil group.photo = nil
end end
@ -346,6 +309,14 @@ local commands = {
end end
-- Last active time for group listing.
for i,v in pairs(database.administration.activity) do
if v == msg.chat.id_str then
table.remove(database.administration.activity, i)
table.insert(database.administration.activity, 1, msg.chat.id_str)
end
end
return true return true
end end
@ -353,8 +324,7 @@ local commands = {
{ -- groups { -- groups
triggers = { triggers = {
'^/groups[@'..bot.username..']*$', '^/groups[@'..bot.username..']*$'
'^/glist[@'..bot.username..']*$'
}, },
command = 'groups', command = 'groups',
@ -363,13 +333,13 @@ local commands = {
action = function(msg) action = function(msg)
local output = '' local output = ''
for k,v in pairs(database.administration.groups) do for i,v in ipairs(database.administration.activity) do
-- no unlisted groups local group = database.administration.groups[v]
if not v.flags[1] then if not group.flags[1] then -- no unlisted groups
if v.link then if group.link then
output = output .. '• [' .. v.name .. '](' .. v.link .. ')\n' output = output .. '• [' .. group.name .. '](' .. group.link .. ')\n'
else else
output = output .. '' .. v.name .. '\n' output = output .. '' .. group.name .. '\n'
end end
end end
end end
@ -495,8 +465,7 @@ local commands = {
{ -- motd { -- motd
triggers = { triggers = {
'^/motd[@'..bot.username..']*', '^/motd[@'..bot.username..']*'
'^/description[@'..bot.username..']*'
}, },
command = 'motd', command = 'motd',
@ -548,7 +517,7 @@ local commands = {
local output = 'Leave this group manually or you will be unable to rejoin.' local output = 'Leave this group manually or you will be unable to rejoin.'
sendMessage(msg.chat.id, output, true, nil, true) sendMessage(msg.chat.id, output, true, nil, true)
else else
kick_user(msg.chat.id, msg.from.id) drua.kick_user(msg.chat.id, msg.from.id)
end end
end end
}, },
@ -571,7 +540,7 @@ local commands = {
sendReply(msg, target.name .. ' is too privileged to be kicked.') sendReply(msg, target.name .. ' is too privileged to be kicked.')
return return
end end
kick_user(msg.chat.id, target.id) drua.kick_user(msg.chat.id, target.id)
sendMessage(msg.chat.id, target.name .. ' has been kicked.') sendMessage(msg.chat.id, target.name .. ' has been kicked.')
end end
}, },
@ -601,7 +570,7 @@ local commands = {
sendReply(msg, target.name .. ' has been unbanned.') sendReply(msg, target.name .. ' has been unbanned.')
else else
group.bans[target.id_str] = true group.bans[target.id_str] = true
kick_user(msg.chat.id, target.id) drua.kick_user(msg.chat.id, target.id)
sendReply(msg, target.name .. ' has been banned.') sendReply(msg, target.name .. ' has been banned.')
end end
end end
@ -733,7 +702,7 @@ local commands = {
sendReply(msg, 'Please specify the new link.') sendReply(msg, 'Please specify the new link.')
return return
elseif input == '--' or input == '' then elseif input == '--' or input == '' then
group.link = get_link(msg.chat.id) group.link = drua.export_link(msg.chat.id)
sendReply(msg, 'The link has been regenerated.') sendReply(msg, 'The link has been regenerated.')
return return
end end
@ -797,7 +766,7 @@ local commands = {
end end
if group.mods[target.id_str] then if group.mods[target.id_str] then
if group.grouptype == 'supergroup' then if group.grouptype == 'supergroup' then
tg:channel_set_admin(msg.chat.id, target, 0) drua.channel_set_admin(msg.chat.id, target.id, 0)
end end
group.mods[target.id_str] = nil group.mods[target.id_str] = nil
sendReply(msg, target.name .. ' is no longer a moderator.') sendReply(msg, target.name .. ' is no longer a moderator.')
@ -807,7 +776,7 @@ local commands = {
return return
end end
if group.grouptype == 'supergroup' then if group.grouptype == 'supergroup' then
tg:channel_set_admin(msg.chat.id, target, 1) drua.channel_set_admin(msg.chat.id, target.id, 1)
end end
group.mods[target.id_str] = true group.mods[target.id_str] = true
sendReply(msg, target.name .. ' is now a moderator.') sendReply(msg, target.name .. ' is now a moderator.')
@ -833,7 +802,7 @@ local commands = {
end end
if group.govs[target.id_str] then if group.govs[target.id_str] then
if group.grouptype == 'supergroup' then if group.grouptype == 'supergroup' then
tg:channel_set_admin(msg.chat.id, target, 0) drua.channel_set_admin(msg.chat.id, target.id, 0)
end end
group.govs[target.id_str] = nil group.govs[target.id_str] = nil
sendReply(msg, target.name .. ' is no longer a governor.') sendReply(msg, target.name .. ' is no longer a governor.')
@ -846,7 +815,7 @@ local commands = {
group.mods[target.id_str] = nil group.mods[target.id_str] = nil
end end
if group.grouptype == 'supergroup' then if group.grouptype == 'supergroup' then
tg:channel_set_admin(msg.chat.id, target, 1) drua.channel_set_admin(msg.chat.id, target.id, 1)
end end
group.govs[target.id_str] = true group.govs[target.id_str] = true
sendReply(msg, target.name .. ' is now a governor.') sendReply(msg, target.name .. ' is now a governor.')
@ -880,7 +849,7 @@ local commands = {
else else
database.blacklist[target.id_str] = true database.blacklist[target.id_str] = true
for k,v in pairs(database.administration.groups) do for k,v in pairs(database.administration.groups) do
kick_user(k, target.id) drua.kick_user(k, target.id)
end end
sendReply(msg, target.name .. ' has been globally banned.') sendReply(msg, target.name .. ' has been globally banned.')
end end
@ -943,10 +912,11 @@ local commands = {
rules = {}, rules = {},
grouptype = msg.chat.type, grouptype = msg.chat.type,
name = msg.chat.title, name = msg.chat.title,
link = get_link(msg.chat.id), link = drua.export_link(msg.chat.id),
photo = get_photo(msg.chat.id), photo = drua.get_photo(msg.chat.id),
founded = os.time() founded = os.time()
} }
table.insert(database.administration.activity, msg.chat.id_str)
sendReply(msg, 'I am now administrating this group.') sendReply(msg, 'I am now administrating this group.')
end end
}, },
@ -1043,17 +1013,12 @@ local action = function(msg)
return true return true
end end
local cron = function()
tg = sender(localhost, config.cli_port)
end
local command = 'groups' local command = 'groups'
local doc = '`Returns a list of administrated groups.\nUse /ahelp for more administrative commands.`' local doc = '`Returns a list of administrated groups.\nUse /ahelp for more administrative commands.`'
return { return {
action = action, action = action,
triggers = triggers, triggers = triggers,
cron = cron,
doc = doc, doc = doc,
command = command command = command
} }

View File

@ -23,9 +23,14 @@ local action = function(msg)
end end
from_name = from_name .. ' (' .. msg.from.id .. ')' from_name = from_name .. ' (' .. msg.from.id .. ')'
local chat_id = math.abs(msg.chat.id)
if chat_id > 1000000000000 then
chat_id = chat_id - 1000000000000
end
local to_name local to_name
if msg.chat.title then if msg.chat.title then
to_name = msg.chat.title .. ' (' .. math.abs(msg.chat.id) .. ').' to_name = msg.chat.title .. ' (' .. chat_id .. ').'
else else
to_name = '@' .. bot.username .. ', AKA ' .. bot.first_name .. ' (' .. bot.id .. ').' to_name = '@' .. bot.username .. ', AKA ' .. bot.first_name .. ' (' .. bot.id .. ').'
end end

View File

@ -7,7 +7,7 @@
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/lua-tg git clone http://github.com/topkecleon/drua-tg
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