Skip to content

Commit

Permalink
channel_requester: Auto-move clients to existing channels
Browse files Browse the repository at this point in the history
A client can join the channel requester, which creates a channel for this user and moves the client
respectively. However, when the client leaves the channel and joins the channel requester channel
instead while the own channel still exists, the client stays in the channel requester channel. This
change ensures, that either a new channel gets created or that the client gets moved to the already
existing channel.
  • Loading branch information
Sebbo94BY committed Dec 21, 2023
1 parent 77549a0 commit 59583af
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions modules/channel_requester/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,41 @@ def find_channels_by_prefix(self, channel_name_prefix=None):

return managed_channels_list

def find_channel_by_name(self, channel_name=None):
"""
Find a channel with a specific name.
:param: channel_name: The name of the channel, which you want to search for.
:return: dict
:type: dict
"""
if channel_name is None:
return None

try:
all_channels = self.ts3conn.channellist()
except TS3Exception:
ChannelRequester.logger.exception("Could not get the current channel list.")
raise

for channel in all_channels:
if channel.get("channel_name") == channel_name:
return channel

return None

def move_client_to_channel_id(self, client, channel_id):
"""
Moves a client into a specific channel.
"""
try:
self.ts3conn.clientmove(int(channel_id), int(client.clid))
except TS3QueryException:
ChannelRequester.logger.exception(
"Failed to move the client into his private channel: %s",
str(client),
)
raise

def create_channel(self, client=None):
"""
Creates a channel and grants a specific client channel admin permissions.
Expand Down Expand Up @@ -328,6 +363,17 @@ def create_channel(self, client=None):
str(main_channel_name),
)

existing_user_channel = self.find_channel_by_name(
str(client_info.get("client_nickname"))
)
if existing_user_channel is not None:
ChannelRequester.logger.info(
"client_nickname=%s has already an existing channel. Moving the client directly.",
str(client_info.get("client_nickname")),
)
self.move_client_to_channel_id(client, existing_user_channel.get("cid"))
return

channel_properties = []
channel_properties.append("channel_flag_semi_permanent=1")
channel_properties.append(f"cpid={int(main_channel_cid)}")
Expand Down Expand Up @@ -449,16 +495,7 @@ def create_channel(self, client=None):
)
raise

try:
self.ts3conn.clientmove(
int(recently_created_channel.get("cid")), int(client.clid)
)
except TS3QueryException:
ChannelRequester.logger.exception(
"Failed to move the client into his private channel: %s",
str(client),
)
raise
self.move_client_to_channel_id(client, recently_created_channel.get("cid"))

try:
self.ts3conn._send(
Expand Down

0 comments on commit 59583af

Please sign in to comment.