Skip to content
This repository was archived by the owner on Sep 14, 2024. It is now read-only.

Commit

Permalink
Support to ban groups. Change whitelist to alloweduserlist
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Rios committed Jan 30, 2021
1 parent 34f9516 commit a374399
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ server migration and automates the download of all dependencies. Look at the
## Bot Owner
The **Bot Owner** can run special commands that no one else can use, like /allowgroup (if the Bot is private, this allow groups where the Bot can be used) or /whitelist (to make Bot don't ask for captcha to some users, useful for blind users).
The **Bot Owner** can run special commands that no one else can use, like /allowgroup (if the Bot is private, this allow groups where the Bot can be used) or /allowuserlist (to make Bot don't ask for captcha to some users, useful for blind users).

You can setup a Bot Owner by specifying the Telegram User ID or Alias in "settings.py" file:

Expand Down
28 changes: 16 additions & 12 deletions sources/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,22 @@
os_getenv("CAPTCHABOT_CAPTCHAS_DIR", \
SETTINGS["CAPTCHABOT_CAPTCHAS_DIR"]),

# Global whitelist file path (to allow whitelist blind users in
# all groups)
"F_WHITE_LIST": \
os_getenv("CAPTCHABOT_F_WHITE_LIST", \
SETTINGS["CAPTCHABOT_F_WHITE_LIST"]),

# Global whitelist file path (to allow whitelist blind users in
# all groups)
# Global allowed users file path (i.e. to allow blind users)
"F_ALLOWED_USERS": \
os_getenv("CAPTCHABOT_F_ALLOWED_USERS", \
SETTINGS["CAPTCHABOT_F_ALLOWED_USERS"]),

# Allowed groups to use the Bot when it is Private
"F_ALLOWED_GROUPS": \
os_getenv("CAPTCHABOT_F_ALLOWED_GROUPS", \
SETTINGS["CAPTCHABOT_F_ALLOWED_GROUPS"]),

# Blocked groups to deny Bot usage (i.e. bad groups that misuse Bot and
# cause overload)
"F_BAN_GROUPS": \
os_getenv("CAPTCHABOT_F_BAN_GROUPS", \
SETTINGS["CAPTCHABOT_F_BAN_GROUPS"]),

# Initial enable/disable status at Bot start
"INIT_ENABLE": \
bool(int(os_getenv("CAPTCHABOT_INIT_ENABLE", \
Expand Down Expand Up @@ -162,10 +166,10 @@
# Bot added to channel, leave text
"BOT_LEAVE_CHANNEL": "This Bot can't be used in channels, just in groups.",

# Whitelist usage
"WHITELIST_USAGE": "Command usage (user ID or Alias):\n" \
"/whitelist add @peter123\n" \
"/whitelist rm 123456789",
# Allowed users list usage
"ALLOWUSERLIST_USAGE": "Command usage (user ID or Alias):\n" \
"/allowuserlist add @peter123\n" \
"/allowuserlist rm 123456789",

# Allowgroup usage
"ALLOWGROUP_USAGE": "Command usage (group ID):\n" \
Expand Down
63 changes: 39 additions & 24 deletions sources/join_captcha_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,12 @@ def initialize_resources():
if path.exists(CONST["CAPTCHAS_DIR"]):
rmtree(CONST["CAPTCHAS_DIR"])
makedirs(CONST["CAPTCHAS_DIR"])
# Create whitelist file if it does not exists
if not path.exists(CONST["F_WHITE_LIST"]):
file_write(CONST["F_WHITE_LIST"], "")
# Create allowed users file if it does not exists
if not path.exists(CONST["F_ALLOWED_USERS"]):
file_write(CONST["F_ALLOWED_USERS"], "")
# Create banned groups file if it does not exists
if not path.exists(CONST["F_BAN_GROUPS"]):
file_write(CONST["F_BAN_GROUPS"], "")
# Create allowed groups file if it does not exists
if CONST["BOT_PRIVATE"]:
if not path.exists(CONST["F_ALLOWED_GROUPS"]):
Expand Down Expand Up @@ -392,9 +395,9 @@ def is_user_in_ignored_list(chat_id, user):
return False


def is_user_in_white_list(user):
'''Check if user is in global whitelist.'''
l_white_users = file_read(CONST["F_WHITE_LIST"])
def is_user_in_allowed_list(user):
'''Check if user is in global allowed list.'''
l_white_users = file_read(CONST["F_ALLOWED_USERS"])
if user.id in l_white_users:
return True
if user.username is not None:
Expand All @@ -414,6 +417,14 @@ def is_group_in_allowed_list(chat_id):
return True
return False


def is_group_in_banned_list(chat_id):
'''Check if group is in banned list.'''
l_banned_groups = file_read(CONST["F_BAN_GROUPS"])
if str(chat_id) in l_banned_groups:
return True
return False

###############################################################################
### Received Telegram not-command messages handlers

Expand Down Expand Up @@ -444,6 +455,10 @@ def new_member_join(update: Update, context: CallbackContext):
tlg_send_selfdestruct_msg_in(bot, chat_id, msg, 1)
tlg_leave_chat(bot, chat_id)
return
if is_group_in_banned_list(chat_id):
printts("Warning: Bot added to banned group: {}".format(chat_id))
tlg_leave_chat(bot, chat_id)
return
# Leave the chat if it is a channel
if chat.type == "channel":
printts("Bot try to be added to a channel")
Expand Down Expand Up @@ -523,8 +538,8 @@ def new_member_join(update: Update, context: CallbackContext):
printts("[{}] User is in ignore list.".format(chat_id))
printts("Skipping the captcha process.")
continue
if is_user_in_white_list(join_user):
printts("[{}] User is in global whitelist.".format(chat_id))
if is_user_in_allowed_list(join_user):
printts("[{}] User is in global allowed list.".format(chat_id))
printts("Skipping the captcha process.")
continue
# Check and remove previous join messages of that user (if any)
Expand Down Expand Up @@ -1565,8 +1580,8 @@ def cmd_captcha(update: Update, context: CallbackContext):
remove(captcha["image"])


def cmd_whitelist(update: Update, context: CallbackContext):
'''Command /whitelist message handler. To Global Whitelist blind users.
def cmd_allowuserlist(update: Update, context: CallbackContext):
'''Command /allowuserlist message handler. To Global allowed list blind users.
Just Bot Owner can use it.'''
bot = context.bot
args = context.args
Expand All @@ -1588,42 +1603,42 @@ def cmd_whitelist(update: Update, context: CallbackContext):
tlg_msg_to_selfdestruct(update_msg)
# Check if no argument was provided with the command
if len(args) == 0:
# Show Actual Global Whitelisted Users
l_white_users = file_read(CONST["F_WHITE_LIST"])
# Show Actual Global allowed list Users
l_white_users = file_read(CONST["F_ALLOWED_USERS"])
bot_msg = "\n".join([str(user) for user in l_white_users])
bot_msg = "Global WhiteList:\n--------------------\n{}".format(bot_msg)
bot_msg = "Global Allowed Users List:\n--------------------\n{}".format(bot_msg)
tlg_send_selfdestruct_msg(bot, chat_id, bot_msg)
tlg_send_selfdestruct_msg(bot, chat_id, CONST["WHITELIST_USAGE"])
tlg_send_selfdestruct_msg(bot, chat_id, CONST["ALLOWUSERLIST_USAGE"])
return
else:
if len(args) <= 1:
tlg_send_selfdestruct_msg(bot, chat_id, CONST["WHITELIST_USAGE"])
tlg_send_selfdestruct_msg(bot, chat_id, CONST["ALLOWUSERLIST_USAGE"])
return
if (args[0] != "add") and (args[0] != "rm"):
tlg_send_selfdestruct_msg(bot, chat_id, CONST["WHITELIST_USAGE"])
tlg_send_selfdestruct_msg(bot, chat_id, CONST["ALLOWUSERLIST_USAGE"])
return
add_rm = args[0]
user = args[1]
l_white_users = file_read(CONST["F_WHITE_LIST"])
l_white_users = file_read(CONST["F_ALLOWED_USERS"])
if add_rm == "add":
if not tlg_is_valid_user_id_or_alias(user):
tlg_send_selfdestruct_msg(bot, chat_id, "Invalid User ID/Alias.")
return
if user not in l_white_users:
file_write(CONST["F_WHITE_LIST"], "{}\n".format(user))
tlg_send_selfdestruct_msg(bot, chat_id, "User added to Global Whitelist.")
file_write(CONST["F_ALLOWED_USERS"], "{}\n".format(user))
tlg_send_selfdestruct_msg(bot, chat_id, "User added to Global allowed list.")
else:
tlg_send_selfdestruct_msg(bot, chat_id, "The User is already in Global Whitelist.")
tlg_send_selfdestruct_msg(bot, chat_id, "The User is already in Global allowed list.")
return
if add_rm == "rm":
if not tlg_is_valid_user_id_or_alias(user):
tlg_send_selfdestruct_msg(bot, chat_id, "Invalid User ID/Alias.")
return
if list_remove_element(l_white_users, user):
file_write(CONST["F_WHITE_LIST"], l_white_users, "w")
tlg_send_selfdestruct_msg(bot, chat_id, "User removed from Global Whitelist.")
file_write(CONST["F_ALLOWED_USERS"], l_white_users, "w")
tlg_send_selfdestruct_msg(bot, chat_id, "User removed from Global allowed list.")
else:
tlg_send_selfdestruct_msg(bot, chat_id, "The User is not in Global Whitelist.")
tlg_send_selfdestruct_msg(bot, chat_id, "The User is not in Global allowed list.")


def cmd_allowgroup(update: Update, context: CallbackContext):
Expand Down Expand Up @@ -1899,7 +1914,7 @@ def main():
dp.add_handler(CommandHandler("about", cmd_about))
if (CONST["BOT_OWNER"] != "XXXXXXXXX"):
dp.add_handler(CommandHandler("captcha", cmd_captcha))
dp.add_handler(CommandHandler("whitelist", cmd_whitelist, pass_args=True))
dp.add_handler(CommandHandler("allowuserlist", cmd_allowuserlist, pass_args=True))
if (CONST["BOT_OWNER"] != "XXXXXXXXX") and CONST["BOT_PRIVATE"]:
dp.add_handler(CommandHandler("allowgroup", cmd_allowgroup, pass_args=True))
# Set to dispatcher a not-command text messages handler
Expand Down
12 changes: 7 additions & 5 deletions sources/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@
# Directory where create/generate temporary captchas
"CAPTCHABOT_CAPTCHAS_DIR": SCRIPT_PATH + "/data/captchas",

# Global whitelist file path (to allow whitelist blind users in all
# groups)
"CAPTCHABOT_F_WHITE_LIST": SCRIPT_PATH + "/data/whitelist.txt",
# Global allowed users file path (i.e. to allow blind users)
"CAPTCHABOT_F_ALLOWED_USERS": SCRIPT_PATH + "/data/allowedusers.txt",

# Global whitelist file path (to allow whitelist blind users in all
# groups)
# Allowed groups to use the Bot when it is Private
"CAPTCHABOT_F_ALLOWED_GROUPS": SCRIPT_PATH + "/data/allowedgroups.txt",

# Blocked groups to deny Bot usage (i.e. bad groups that misuse Bot and
# cause overload)
"CAPTCHABOT_F_BAN_GROUPS": SCRIPT_PATH + "/data/bannedgroups.txt",

# Initial language at Bot start
"CAPTCHABOT_INIT_LANG": "EN",

Expand Down

0 comments on commit a374399

Please sign in to comment.