Added administration.lua.

Adds self-hosted, single-realm administrative functions to otouto.
Check out the README for a detailed description.

Also added msg.from.name, which is msg.from.first_name + msg.from.last_name (if it
exists), because I'm too lazy to build it each time.
Fixed a crash-causing bug (ironically) in the handle_exception() function.
This commit is contained in:
topkecleon 2016-02-20 05:07:20 -05:00
parent 5cb058ec6b
commit 180cd6078a
10 changed files with 1105 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,2 +1 @@
plugins/administration.lua
plugins/CUSTOM.lua

View File

@ -60,6 +60,69 @@ Here is a list of most otouto plugins.
* * *
## administration.lua
The administration plugin enables self-hosted, single-realm group administration, supporting both normal groups and supergroups. This works by sending TCP commands to an instance of tg running on the owner's account.
To get started, run `./tg-install.sh`. Note that this script is written for Ubuntu/Debian. If you're running Arch (the only acceptable alternative), you'll have to do it yourself. If that is the case, note that otouto uses the "test" branch of tg, and the AUR package `telegram-cli-git` will not be sufficient, as it does not have support for supergroups yet.
Once the installation is finished, enable `administration.lua` in your config file. You may have reason to change the default TCP port (4567); if that is the case, remember to change it in `tg-launch.sh` as well. Run `./tg-launch.sh` in a separate screen/tmux window. You'll have to enter your phone number and go through the login process the first time. The script is set to restart tg after two seconds, so you'll need to Ctrl+C after exiting.
While tg is running, you may start/reload otouto with administration.lua enabled, and have access to a wide variety of administrative commands and automata. The administration "database" is stored in `administration.json`. To start using otouto to administrate a group (note that you must be the owner (or an administrator)), send `/gadd` to that group. For a list of commands, use `/ahelp`. Below I'll describe various functions now available to you.
| Command | Function | Privilege | Internal? |
|---------|----------|-----------|-----------|
| /groups | Returns a list of administrated groups (except those flagged "unlisted". | 1 | N |
| /ahelp | Returns a list of administrative commands and their required privileges. | 1 | Y |
| /ops | Returns a list of moderators, governors, and administrators. | 1 | Y |
| /rules | Returns the rules of a group. | 1 | Y |
| /motd | Returns a group's "Message of the Day". | 1 | Y |
| /link | Returns the link for a group. | 1 | Y |
| /leave | Removes the user from the group. | 1 | Y |
| /kick | Removes the target from the group. | 2 | Y |
| /ban | Bans the target from the group. | 2 | Y |
| /unban | Unbans the target from the group. | 2 | Y |
| /setrules | Sets the rules for a group. | 3 | Y |
| /setmotd | Sets a group's "Message of the Day". | 3 | Y |
| /setlink | Sets a group's link. | 3 | Y |
| /flag | Returns a list of available flags and their settings, or toggles a flag. | 3 | Y |
| /mod | Promotes a user to a moderator. | 3 | Y |
| /demod | Demotes a moderator to a user. | 3 | Y |
| /gov | Promotes a user to a governor. | 4 | Y |
| /degov | Demotes a governor to a user. | 4 | Y |
| /hammer | Bans a user from all groups. | 4 | N |
| /unhammer | Removes a global ban. | 4 | N |
| /admin | Promotes a user to an administrator. | 5 | N |
| /deadmin | Demotes an administrator to a user. | 5 | N |
| /gadd | Adds a group to the administrative system. | 5 | N |
| /grem | Removes a group from the administrative system | 5 | Y |
| /broadcast | Broadcasts a message to all administrated groups. | 5 | N |
Internal commands can only be run within an administrated group.
###Description of Privileges
| # | Title | Description | Scope |
|------|-------|-------------|-------|
| 0 | Banned | Cannot enter the group(s). | Either |
| 1 | User | Default rank. | Local |
| 2 | Moderator | Can kick/ban/unban users from a group. | Local |
| 3 | Governor | Can set rules/motd/link. Can promote/demote moderators. Can modify flags. | Local |
| 4 | Administrator | Can globally ban/unban users. Can promote/demote governors. | Global |
| 5 | Owner | Can add/remove groups. Can broadcast. Can promote/demote administrators. | Global |
Obviously, each greater rank inherits the privileges of the lower, positive ranks.
###Flags
| # | Name | Description |
|---|------|-------------|
| 1 | unlisted | Removes a group from the /groups listing. |
| 2 | antisquig | Automatically removes users for posting Arabic script or RTL characters. |
| 3 | antisquig Strict | Automatically removes users whose names contain Arabic script or RTL characters. |
| 4 | antibot | Prevents bots from being added by non-moderators. |
* * *
##Liberbot Plugins
Some plugins are only useful when the bot is used in a Liberbot group, like floodcontrol.lua and moderation.lua.

View File

@ -54,12 +54,16 @@ on_msg_receive = function(msg) -- The fn run whenever a message is received.
msg.chat.id_str = tostring(msg.chat.id)
msg.from.id_str = tostring(msg.from.id)
msg.text_lower = msg.text:lower()
msg.from.name = msg.from.first_name
if msg.from.last_name then
msg.from.name = msg.from.first_name .. ' ' .. msg.from.last_name
end
local success, result = pcall(function()
return v.action(msg)
end)
if not success then
sendReply(msg, 'An unexpected error occurred.')
sendReply(msg, 'Sorry, an unexpected error occurred.')
handle_exception(result, msg.text)
return
end

View File

@ -12,6 +12,7 @@ return {
time_offset = 0,
lang = 'en',
antisquig = false,
-- If you change this, make sure you also modify launch-tg.sh.
cli_port = 4567,
admin = 00000000,
admin_name = 'John Smith',

View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
while true; do
lua bot.lua

1008
plugins/administration.lua Normal file

File diff suppressed because it is too large Load Diff

View File

@ -63,7 +63,7 @@ local action = function(msg)
end
text = text:gsub('</?.->', '')
local l = text:find('\n')
local l = text:find('<h2>')
if l then
text = text:sub(1, l-1)
end
@ -78,15 +78,15 @@ local action = function(msg)
output = output .. '[Read more.](' .. url .. ')'
end
sendMessage(msg.chat.id, output, true, nil, true)
--
--[[ Comment the previous block and uncomment this one for full-message,
-- "unlinked" link previews.
-- Invisible zero-width, non-joiner.
local output = '[](' .. jdat.responseData.results[1].url .. ')'
sendMessage(msg.chat.id, output, false, nil, true)
]]--
sendMessage(msg.chat.id, output, true, nil, true)
end
return {

12
tg-install.sh Executable file
View File

@ -0,0 +1,12 @@
#!/bin/sh
# Will download lua-tg and will download and build tg's "test" branch.
# Written for Ubuntu/Debian. If you're running Arch (the only acceptable
# alternative), figure it out yourself.
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/vysheng/tg --recursive -b test
cd tg
./configure
make

10
tg-launch.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
# Launch tg listening on the default port (change this if you've changed it in
# config.lua), delete state file after stop, wait two seconds, and restart.
while true; do
tg/bin/telegram-cli -P 4567 -E
rm ~/.telegram-cli/state
sleep 2s
done

View File

@ -148,6 +148,8 @@ end
handle_exception = function(err, message)
if not err then err = '' end
local output = '\n[' .. os.date('%F %T', os.time()) .. ']\n' .. bot.username .. ': ' .. err .. '\n' .. message .. '\n'
if config.log_chat then