Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Included Blacklist option #252

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lua/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ local _M =
},
chat_hashes = {'extra', 'info', 'links', 'warns', 'mediawarn', 'spamwarns', 'blocked', 'report', 'defpermissions',
'defpermduration'},
chat_sets = {'whitelist'},--, 'mods'},
chat_sets = {'whitelist', 'blacklist'},--, 'mods'},
bot_keys = {
d3 = {'bot:general', 'bot:usernames', 'bot:chat:latsmsg'},
d2 = {'bot:groupsid', 'bot:groupsid:removed', 'tempbanned', 'bot:blocked', 'remolden_chats'} --remolden_chats: chat removed with $remold command
Expand Down
76 changes: 75 additions & 1 deletion lua/plugins/antispam.lua
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,73 @@ function plugin.onTextMessage(msg, blocks)
db:srem(('chat:%d:whitelist'):format(msg.chat.id), blocks[2])
api.sendReply(msg, 'Done')
end

if (blocks[1] == 'bl' or blocks[1] == 'blacklist') and blocks[2] then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for this.

if blocks[2] == '-' then
local set = ('chat:%d:blacklist'):format(msg.chat.id)
local n = db:scard(set) or 0
local text
if n == 0 then
text = i18n("_The blacklist was already empty_")
else
db:del(set)
text = i18n("*Blacklist cleaned*\n%d links have been removed"):format(n)
end
api.sendReply(msg, text, true)
else
local text
if msg.entities then
local links = urls_table(msg.entities, msg.text)
if not next(links) then
text = i18n("_I can't find any url in this message_")
else
local new = db:sadd(('chat:%d:blacklist'):format(msg.chat.id), table.unpack(links))
text = i18n("%d link(s) will be blacklisted"):format(#links - (#links - new))
if new ~= #links then
text = text..i18n("\n%d links were already in the list"):format(#links - new)
end
end
else
text = i18n("_I can't find any url in this message_")
end
api.sendReply(msg, text, true)
end
end
if (blocks[1] == 'bl' or blocks[1] == 'blacklist') and not blocks[2] then
local links = db:smembers(('chat:%d:blacklist'):format(msg.chat.id))
if not next(links) then
api.sendReply(msg, i18n("_The blacklist is empty_.\nUse `/bl [links]` to add some links to the blacklist"), true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use msg:send_reply() instead.

else
local text = i18n("Blacklisted links:\n\n")
for i=1, #links do
text = text..'• '..links[i]..'\n'
end
api.sendReply(msg, text)
end
end
if blocks[1] == 'unbl' or blocks[1] == 'unblacklist' then
local text
if msg.entities then
local links = urls_table(msg.entities, msg.text)
if not next(links) then
text = i18n("_I can't find any url in this message_")
else
local removed = db:srem(('chat:%d:blacklist'):format(msg.chat.id), table.unpack(links))
text = i18n("%d link(s) removed from the blacklist"):format(removed)
if removed ~= #links then
text = text.._("\n%d links were already in the list"):format(#links - removed)
end
end
else
text = i18n("_I can't find any url in this message_")
end
api.sendReply(msg, text, true)
end
if blocks[1] == 'funbl' then --force the unblacklist of a link
db:srem(('chat:%d:blacklist'):format(msg.chat.id), blocks[2])
api.sendReply(msg, 'Done')
end

if blocks[1] == 'wlchan' and not blocks[2] then
local channels = db:smembers(('chat:%d:chanwhitelist'):format(msg.chat.id))
if not next(channels) then
Expand Down Expand Up @@ -433,7 +500,14 @@ plugin.triggers = {
config.cmd..'(wl)$',
config.cmd..'(whitelist)$',
--config.cmd..'(wlchan)$',
config.cmd..'(funwl) (.+)'
config.cmd..'(funwl) (.+)',
config.cmd..'(bl) (.+)$',
config.cmd..'(blacklist) (.+)$',
config.cmd..'(bl)$',
config.cmd..'(blacklist)$',
config.cmd..'(unbl) (.+)$',
config.cmd..'(unblacklist) (.+)$',
config.cmd..'(funbl) (.+)'
}
}

Expand Down
20 changes: 18 additions & 2 deletions lua/plugins/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,19 @@ If an user sends a whitelisted link, he won't be warned or kicked.

When the group link is saved with `/setlink`, it gets automatically added to the whitelist.

*Why links are saved without* _https://_ *and* _www_*?*
The bot auto-removes _https://, http:// and www_ from every link to reduce the possibility of having the same link saved twice.
]])
elseif key == 'blacklist' then
return _([[*Blacklist settings*

If an user sends a blacklisted link, it will be deleted.

`/blacklist [link(s)]` or `/bl [link(s)]`: add one or more links to the blacklist.
`/unblacklist [link(s)]` or `/unbl [link(s)]`: remove one or more links from the blacklist.
`/blacklist` or `/bl`: get the blacklist.
`/blacklistl -` or `/bl -`: empty the blacklist.

*Why links are saved without* _https://_ *and* _www_*?*
The bot auto-removes _https://, http:// and www_ from every link to reduce the possibility of having the same link saved twice.
]])
Expand Down Expand Up @@ -303,9 +316,12 @@ local function dk_admins()
[i18n("Extra commands")] = 'extra',
[i18n("Warns")] = 'warns'
},
{
[i18n("Welcome settings")] = 'welcome',
{
[i18n("Links whitelist")] = 'whitelist',
[i18n("Links blacklist")] = 'blacklist',
},
{
[i18n("Welcome settings")] = 'welcome',
}

}
Expand Down
21 changes: 20 additions & 1 deletion lua/plugins/onmessage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ local function is_whitelisted(chat_id, text)
end
end

local function is_blacklisted(chat_id, text)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could've generalized the whitelist function and share some code between the two.

local set = ('chat:%d:blacklist'):format(chat_id)
local links = db:smembers(set)
if links and next(links) then
for i=1, #links do
if text:match(links[i]:gsub('%-', '%%-')) then
return true
end
end
end
end

function plugin.onEveryMessage(msg)

if not msg.inline then
Expand Down Expand Up @@ -120,7 +132,14 @@ function plugin.onEveryMessage(msg)
local media = msg.media_type
local hash = 'chat:'..msg.chat.id..':media'
local media_status = (db:hget(hash, media)) or config.chat_settings.media[media]
if media_status ~= 'ok' then
local blacklisted
if media == 'link' then
blacklisted = is_blacklisted(msg.chat.id, msg.text)
end

if blacklisted then
api.deleteMessage(msg.chat.id, msg.message_id)
elseif media_status ~= 'ok' then
local whitelisted
if media == 'link' then
whitelisted = is_whitelisted(msg.chat.id, msg.text)
Expand Down