monolith/legacy/modules/helpers.py

73 lines
2.0 KiB
Python
Raw Normal View History

2022-08-11 18:22:09 +00:00
import main
from modules import chankeep
from utils.logging.debug import debug
2022-08-11 19:09:14 +00:00
2022-08-11 18:22:09 +00:00
def get_first_relay(net):
"""
Get the first relay in the network.
:param net: the network
:param num: number or relay
:return: IRCPool instance for the IRC bot
"""
cur_relay = 0
max_relay = max(main.network[net].relays.keys())
debug(f"get_first_relay() {net}: max_relay:{max_relay}")
2022-08-11 18:22:09 +00:00
activeRelays = chankeep.getActiveRelays(net)
debug(f"get_first_relay() {net}: activeRelays:{activeRelays}")
2022-08-11 18:22:09 +00:00
while cur_relay != max_relay:
cur_relay += 1
if cur_relay not in activeRelays:
continue
name = net + str(cur_relay)
if name in main.IRCPool.keys():
# debug(f"get_first_relay() {net}: found relay {name}")
2022-08-11 18:22:09 +00:00
return main.IRCPool[name]
return None
2022-08-11 19:09:14 +00:00
2022-08-11 18:22:09 +00:00
def is_first_relay(net, num):
"""
Determine if we are the first relay for the network.
:param net: the network
:param num: number or relay
:return: True if we are the first relay, False otherwise
"""
2022-08-11 19:26:19 +00:00
first_relay = get_first_relay(net)
if not first_relay:
return False
return first_relay.num == num
2022-08-13 21:36:18 +00:00
def get_active_relays(net):
"""
Get all active instances for the network.
:param net: the network
:return: list of active instances
:rtype: list of IRCPool instances
"""
active_nums = chankeep.getActiveRelays(net)
active_insts = []
for num in active_nums:
name = net + str(num)
if name in main.IRCPool.keys():
active_insts.append(main.IRCPool[name])
return active_insts
def get_connected_relays(net):
"""
Get all connected instances for the network.
:param net: the network
:return: list of active instances
:rtype: list of IRCPool instances
"""
active_nums = chankeep.getConnectedRelays(net)
active_insts = []
for num in active_nums:
name = net + str(num)
if name in main.IRCPool.keys():
active_insts.append(main.IRCPool[name])
return active_insts