Allow to disable auto-join
Closes https://git.autonomic.zone/decentral1se/xbotlib/issues/26.
This commit is contained in:
parent
8d8c3f8070
commit
eca289cb59
@ -7,6 +7,7 @@
|
|||||||
- Migrate Redis environment naming: `REDIS_URL` -> `XBOT_REDIS_URL` ([#23](https://git.autonomic.zone/decentral1se/xbotlib/issues/23))
|
- Migrate Redis environment naming: `REDIS_URL` -> `XBOT_REDIS_URL` ([#23](https://git.autonomic.zone/decentral1se/xbotlib/issues/23))
|
||||||
- Allow to load custom plugins ([#24](https://git.autonomic.zone/decentral1se/xbotlib/issues/24))
|
- Allow to load custom plugins ([#24](https://git.autonomic.zone/decentral1se/xbotlib/issues/24))
|
||||||
- Supports rooms configuration for auto-joining ([#25](https://git.autonomic.zone/decentral1se/xbotlib/issues/25))
|
- Supports rooms configuration for auto-joining ([#25](https://git.autonomic.zone/decentral1se/xbotlib/issues/25))
|
||||||
|
- Add `--no-auto-join` to disable automatically responding to invites ([#26](https://git.autonomic.zone/decentral1se/xbotlib/issues/26))
|
||||||
|
|
||||||
# xbotlib 0.10.0 (2021-01-16)
|
# xbotlib 0.10.0 (2021-01-16)
|
||||||
|
|
||||||
|
@ -159,6 +159,7 @@ deployments.
|
|||||||
- **XBOT_AVATAR**: The bot avatar icon
|
- **XBOT_AVATAR**: The bot avatar icon
|
||||||
- **XBOT_REDIS_URL**: Redis key store connection URL
|
- **XBOT_REDIS_URL**: Redis key store connection URL
|
||||||
- **XBOT_ROOMS**: The rooms to automatically join
|
- **XBOT_ROOMS**: The rooms to automatically join
|
||||||
|
- **XBOT_NO_AUTO_JOIN**: Disable auto-joining on invite
|
||||||
|
|
||||||
## Deploy your bots
|
## Deploy your bots
|
||||||
|
|
||||||
|
40
xbotlib.py
40
xbotlib.py
@ -123,10 +123,17 @@ class Config:
|
|||||||
def rooms(self):
|
def rooms(self):
|
||||||
"""A list of rooms to automatically join."""
|
"""A list of rooms to automatically join."""
|
||||||
rooms = self.section.get("rooms", None)
|
rooms = self.section.get("rooms", None)
|
||||||
if isinstance(rooms, str):
|
|
||||||
return [rooms]
|
if rooms is None:
|
||||||
|
return None
|
||||||
|
|
||||||
return rooms.split(",")
|
return rooms.split(",")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def no_auto_join(self):
|
||||||
|
"""Disable auto-join when invited."""
|
||||||
|
return self.section.get("no_auto_join", None)
|
||||||
|
|
||||||
|
|
||||||
class Bot(ClientXMPP):
|
class Bot(ClientXMPP):
|
||||||
"""XMPP bots for humans."""
|
"""XMPP bots for humans."""
|
||||||
@ -197,6 +204,13 @@ class Bot(ClientXMPP):
|
|||||||
nargs="+",
|
nargs="+",
|
||||||
help="Rooms to automatically join",
|
help="Rooms to automatically join",
|
||||||
)
|
)
|
||||||
|
self.parser.add_argument(
|
||||||
|
"--no-auto-join",
|
||||||
|
default=False,
|
||||||
|
action="store_true",
|
||||||
|
dest="no_auto_join",
|
||||||
|
help="Disable automatically joining rooms when invited",
|
||||||
|
)
|
||||||
|
|
||||||
self.args = self.parser.parse_args()
|
self.args = self.parser.parse_args()
|
||||||
|
|
||||||
@ -229,7 +243,8 @@ class Bot(ClientXMPP):
|
|||||||
nick = input("Nickname: ")
|
nick = input("Nickname: ")
|
||||||
avatar = input("Avatar: ")
|
avatar = input("Avatar: ")
|
||||||
redis_url = input("Redis URL: ")
|
redis_url = input("Redis URL: ")
|
||||||
rooms = input("Rooms (comma separated): ")
|
rooms = input("Rooms: ")
|
||||||
|
no_auto_join = input("Disable auto-join on invite? ")
|
||||||
|
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
config[self.name] = {"account": account, "password": password}
|
config[self.name] = {"account": account, "password": password}
|
||||||
@ -242,6 +257,8 @@ class Bot(ClientXMPP):
|
|||||||
config[self.name]["redis_url"] = redis_url
|
config[self.name]["redis_url"] = redis_url
|
||||||
if rooms:
|
if rooms:
|
||||||
config[self.name]["rooms"] = rooms
|
config[self.name]["rooms"] = rooms
|
||||||
|
if no_auto_join:
|
||||||
|
config[self.name]["auto_join"] = no_auto_join
|
||||||
|
|
||||||
with open(self.CONFIG_FILE, "w") as file_handle:
|
with open(self.CONFIG_FILE, "w") as file_handle:
|
||||||
config.write(file_handle)
|
config.write(file_handle)
|
||||||
@ -277,6 +294,11 @@ class Bot(ClientXMPP):
|
|||||||
or self.config.rooms
|
or self.config.rooms
|
||||||
or environ.get("XBOT_ROOMS", None)
|
or environ.get("XBOT_ROOMS", None)
|
||||||
)
|
)
|
||||||
|
no_auto_join = (
|
||||||
|
self.args.no_auto_join
|
||||||
|
or self.config.no_auto_join
|
||||||
|
or environ.get("XBOT_NO_AUTO_JOIN", None)
|
||||||
|
)
|
||||||
|
|
||||||
if not account:
|
if not account:
|
||||||
self.log.error("Unable to discover account")
|
self.log.error("Unable to discover account")
|
||||||
@ -296,6 +318,7 @@ class Bot(ClientXMPP):
|
|||||||
self.avatar = avatar
|
self.avatar = avatar
|
||||||
self.redis_url = redis_url
|
self.redis_url = redis_url
|
||||||
self.rooms = rooms
|
self.rooms = rooms
|
||||||
|
self.no_auto_join = no_auto_join
|
||||||
|
|
||||||
def register_xmpp_event_handlers(self):
|
def register_xmpp_event_handlers(self):
|
||||||
"""Register functions against specific XMPP event handlers."""
|
"""Register functions against specific XMPP event handlers."""
|
||||||
@ -354,13 +377,22 @@ class Bot(ClientXMPP):
|
|||||||
|
|
||||||
def join_rooms(self):
|
def join_rooms(self):
|
||||||
"""Automatically join rooms if specified."""
|
"""Automatically join rooms if specified."""
|
||||||
|
if self.rooms is None:
|
||||||
|
return
|
||||||
|
|
||||||
for room in self.rooms:
|
for room in self.rooms:
|
||||||
self.plugin["xep_0045"].join_muc(room, self.config.nick)
|
self.plugin["xep_0045"].join_muc(room, self.config.nick)
|
||||||
self.log.info(f"Joining {room} automatically")
|
self.log.info(f"Joining {room} automatically")
|
||||||
|
|
||||||
def group_invite(self, message):
|
def group_invite(self, message):
|
||||||
"""Accept invites to group chats."""
|
"""Accept invites to group chats."""
|
||||||
self.plugin["xep_0045"].join_muc(message["from"], self.config.nick)
|
room = message["from"]
|
||||||
|
|
||||||
|
if self.no_auto_join:
|
||||||
|
return self.log.info(f"Not joining {room} (disabled)")
|
||||||
|
|
||||||
|
self.plugin["xep_0045"].join_muc(room, self.config.nick)
|
||||||
|
self.log.info(f"Joining {room} as invited")
|
||||||
|
|
||||||
def group_message(self, message):
|
def group_message(self, message):
|
||||||
"""Handle group chat message events."""
|
"""Handle group chat message events."""
|
||||||
|
Reference in New Issue
Block a user