Esempio n. 1
0
class TsuServer3:
    def __init__(self):
        self.config = None
        self.allowed_iniswaps = None
        self.loaded_ips = {}
        self.load_config()
        self.load_iniswaps()
        self.load_gimps()
        self.client_manager = ClientManager(self)
        self.area_manager = AreaManager(self)
        self.serverpoll_manager = ServerpollManager(self)
        self.ban_manager = BanManager()
        self.software = 'tsuserver3'
        self.version = 'tsuserver3dev'
        self.release = 3
        self.major_version = 3
        self.minor_version = 0
        self.char_list = None
        self.char_pages_ao1 = None
        self.music_list = None
        self.music_list_ao2 = None
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.data = None
        self.features = set()
        self.load_characters()
        self.load_music()
        self.load_backgrounds()
        self.load_data()
        self.load_ids()
        self.enable_features()
        self.stats_manager = Database(self)
        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        self.runner = True
        self.runtime = 0
        logger.setup_logger(debug=self.config['debug'],
                            log_size=self.config['log_size'],
                            log_backups=self.config['log_backups'],
                            areas=self.area_manager.areas)

    def start(self):
        loop = asyncio.get_event_loop()

        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'

        ao_server_crt = loop.create_server(lambda: AOProtocol(self), bound_ip,
                                           self.config['port'])
        ao_server = loop.run_until_complete(ao_server_crt)

        if self.config['use_district']:
            self.district_client = DistrictClient(self)
            asyncio.ensure_future(self.district_client.connect(), loop=loop)

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            asyncio.ensure_future(self.ms_client.connect(), loop=loop)

        logger.log_debug('Server started.')
        print("Welcome to AOVserver version " + str(self.release) + "." +
              str(self.major_version) + "." + str(self.minor_version))

        loop.run_until_complete(self.load_runner())
        try:
            loop.run_forever()
        except KeyboardInterrupt:
            pass

        logger.log_debug('Server shutting down.')
        self.runner = False
        ao_server.close()
        loop.run_until_complete(ao_server.wait_closed())
        loop.close()

    def get_version_string(self):
        return str(self.release) + '.' + str(self.major_version) + '.' + str(
            self.minor_version)

    def new_client(self, transport):
        ip = transport.get_extra_info('peername')[0]
        c = self.client_manager.new_client(transport)
        if ip not in self.loaded_ips:
            self.loaded_ips[ip] = 0
        self.loaded_ips[ip] += 1
        if self.rp_mode:
            c.in_rp = True
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client):
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    def get_player_count(self):
        return len(self.client_manager.clients)

    async def running_check(self):
        while self.runner:
            await asyncio.sleep(1)
            self.runtime += 1
            if self.runtime % 300 == 0:
                for area in self.area_manager.areas:
                    area.last_talked = None
                self.stats_manager.save_alldata()

    async def load_runner(self):
        asyncio.ensure_future(self.running_check())

    def load_config(self):
        with open('config/config.yaml', 'r', encoding='utf-8') as cfg:
            self.config = yaml.load(cfg)
            self.config['motd'] = self.config['motd'].replace('\\n', ' \n')
        if 'music_change_floodguard' not in self.config:
            self.config['music_change_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        if 'wtce_floodguard' not in self.config:
            self.config['wtce_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        if 'log_size' not in self.config:
            self.config['log_size'] = 1048576
        if 'log_backups' not in self.config:
            self.config['log_backups'] = 5

    def load_gimps(self):
        with open('config/gimp.yaml', 'r', encoding='utf-8') as cfg:
            self.gimp_list = yaml.load(cfg)

    def load_ids(self):
        self.hdid_list = {}
        # load hdids
        try:
            with open('storage/hd_ids.json', 'r',
                      encoding='utf-8') as whole_list:
                self.hdid_list = json.loads(whole_list.read())
        except:
            logger.log_debug(
                'Failed to load hd_ids.json from ./storage. If hd_ids.json is exist then remove it.'
            )

    def load_characters(self):
        with open('config/characters.yaml', 'r', encoding='utf-8') as chars:
            self.char_list = yaml.load(chars)
        self.build_char_pages_ao1()

    def load_music(self):
        with open('config/music.yaml', 'r', encoding='utf-8') as music:
            self.music_list = yaml.load(music)
        self.build_music_pages_ao1()
        self.build_music_list_ao2()

    def load_data(self):
        with open('config/data.yaml', 'r') as data:
            self.data = yaml.load(data)

    def save_data(self):
        with open('config/data.yaml', 'w') as data:
            json.dump(self.data, data)

    def save_id(self):
        with open('storage/hd_ids.json', 'w') as data:
            json.dump(self.hdid_list, data)

    def get_ipid(self, ip):
        x = ip + str(self.config['server_number'])
        hash_object = hashlib.sha256(x.encode('utf-8'))
        hash = hash_object.hexdigest()[:12]
        return hash

    def load_backgrounds(self):
        with open('config/backgrounds.yaml', 'r', encoding='utf-8') as bgs:
            self.backgrounds = yaml.load(bgs)

    def load_iniswaps(self):
        try:
            with open('config/iniswaps.yaml', 'r',
                      encoding='utf-8') as iniswaps:
                self.allowed_iniswaps = yaml.load(iniswaps)
        except:
            logger.log_debug('cannot find iniswaps.yaml')

    def enable_features(self):
        self.features.add('modcall_reason')

    def build_char_pages_ao1(self):
        self.char_pages_ao1 = [
            self.char_list[x:x + 10] for x in range(0, len(self.char_list), 10)
        ]
        for i in range(len(self.char_list)):
            self.char_pages_ao1[i // 10][i % 10] = '{}#{}&&0&&&0&'.format(
                i, self.char_list[i])

    def build_music_pages_ao1(self):
        self.music_pages_ao1 = []
        index = 0
        # add areas first
        for area in self.area_manager.areas:
            self.music_pages_ao1.append('{}#{}'.format(index, area.name))
            index += 1
        # then add music
        for item in self.music_list:
            self.music_pages_ao1.append('{}#{}'.format(index,
                                                       item['category']))
            index += 1
            for song in item['songs']:
                self.music_pages_ao1.append('{}#{}'.format(
                    index, song['name']))
                index += 1
        self.music_pages_ao1 = [
            self.music_pages_ao1[x:x + 10]
            for x in range(0, len(self.music_pages_ao1), 10)
        ]

    def build_music_list_ao2(self):
        self.music_list_ao2 = []
        # add areas first
        for area in self.area_manager.areas:
            self.music_list_ao2.append(area.name)
            # then add music
        for item in self.music_list:
            self.music_list_ao2.append(item['category'])
            for song in item['songs']:
                self.music_list_ao2.append(song['name'])

    def is_valid_char_id(self, char_id):
        return len(self.char_list) > char_id >= 0

    def get_char_id_by_name(self, name):
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError('Character not found.')

    def get_song_data(self, music):
        for item in self.music_list:
            if item['category'] == music:
                return item['category'], -1
            for song in item['songs']:
                if song['name'] == music:
                    try:
                        return song['name'], song['length']
                    except KeyError:
                        return song['name'], -1
        raise ServerError('Music not found.')

    def send_all_cmd_pred(self, cmd, *args, pred=lambda x: True):
        for client in self.client_manager.clients:
            if pred(client):
                client.send_command(cmd, *args)

    def broadcast_global(self, client, msg, as_mod=False):
        char_name = client.get_char_name()
        ooc_name = '{}[{}][{}]'.format('<dollar>G', client.area.id, char_name)
        if as_mod:
            ooc_name += '[M]'
        self.send_all_cmd_pred('CT',
                               ooc_name,
                               msg,
                               pred=lambda x: not x.muted_global)
        if self.config['use_district']:
            self.district_client.send_raw_message('GLOBAL#{}#{}#{}#{}'.format(
                int(as_mod), client.area.id, char_name, msg))

    def broadcast_need(self, client, msg):
        char_name = client.get_char_name()
        area_name = client.area.name
        area_id = client.area.id
        self.send_all_cmd_pred(
            'CT',
            '{}'.format(self.config['hostname']),
            '=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
            format(char_name, area_name, area_id, msg),
            pred=lambda x: not x.muted_adverts)
        if self.config['use_district']:
            self.district_client.send_raw_message('NEED#{}#{}#{}#{}'.format(
                char_name, area_name, area_id, msg))
Esempio n. 2
0
class TsuServer3:
    """The main class for tsuserver3 server software."""
    def __init__(self):
        self.software = 'tsuserver3'
        self.release = 3
        self.major_version = 3
        self.minor_version = 0

        self.config = None
        self.allowed_iniswaps = []
        self.char_list = None
        self.char_emotes = None
        self.char_pages_ao1 = None
        self.music_list = []
        self.music_list_ao2 = None
        self.music_pages_ao1 = None
        self.bglock = False
        self.backgrounds = None
        self.zalgo_tolerance = None
        self.ipRange_bans = []
        self.geoIpReader = None
        self.useGeoIp = False

        try:
            self.geoIpReader = geoip2.database.Reader('./storage/GeoLite2-ASN.mmdb')
            self.useGeoIp = True
            # on debian systems you can use /usr/share/GeoIP/GeoIPASNum.dat if the geoip-database-extra package is installed
        except FileNotFoundError:
            self.useGeoIp = False
            pass

        self.ms_client = None

        try:
            self.load_config()
            self.area_manager = AreaManager(self)
            self.load_iniswaps()
            self.load_characters()
            self.load_music()
            self.load_backgrounds()
            self.load_ipranges()
        except yaml.YAMLError as exc:
            print('There was a syntax error parsing a configuration file:')
            print(exc)
            print('Please revise your syntax and restart the server.')
            sys.exit(1)
        except OSError as exc:
            print('There was an error opening or writing to a file:')
            print(exc)
            sys.exit(1)
        except Exception as exc:
            print('There was a configuration error:')
            print(exc)
            print('Please check sample config files for the correct format.')
            sys.exit(1)

        self.client_manager = ClientManager(self)
        server.logger.setup_logger(debug=self.config['debug'])

    def start(self):
        """Start the server."""
        loop = asyncio.get_event_loop()

        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'

        ao_server_crt = loop.create_server(lambda: AOProtocol(self), bound_ip,
                                           self.config['port'])
        ao_server = loop.run_until_complete(ao_server_crt)

        if self.config['use_websockets']:
            ao_server_ws = websockets.serve(new_websocket_client(self),
                                            bound_ip,
                                            self.config['websocket_port'])
            asyncio.ensure_future(ao_server_ws)

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            asyncio.ensure_future(self.ms_client.connect(), loop=loop)

        if self.config['zalgo_tolerance']:
            self.zalgo_tolerance = self.config['zalgo_tolerance']

        asyncio.ensure_future(self.schedule_unbans())

        database.log_misc('start')
        print('Server started and is listening on port {}'.format(
            self.config['port']))

        try:
            loop.run_forever()
        except KeyboardInterrupt:
            pass

        database.log_misc('stop')

        ao_server.close()
        loop.run_until_complete(ao_server.wait_closed())
        loop.close()

    async def schedule_unbans(self):
        while True:
            database.schedule_unbans()
            await asyncio.sleep(3600 * 12)

    @property
    def version(self):
        """Get the server's current version."""
        return f'{self.release}.{self.major_version}.{self.minor_version}'

    def new_client(self, transport):
        """
        Create a new client based on a raw transport by passing
        it to the client manager.
        :param transport: asyncio transport
        :returns: created client object
        """
        peername = transport.get_extra_info('peername')[0]

        if self.useGeoIp:
            try:
                geoIpResponse = self.geoIpReader.asn(peername)
                asn = str(geoIpResponse.autonomous_system_number)
            except geoip2.errors.AddressNotFoundError:
                asn = "Loopback"
                pass
        else:
            asn = "Loopback"

        for line,rangeBan in enumerate(self.ipRange_bans):
            if rangeBan != "" and peername.startswith(rangeBan) or asn == rangeBan:
                msg =   'BD#'
                msg +=  'Abuse\r\n'
                msg += f'ID: {line}\r\n'
                msg +=  'Until: N/A'
                msg +=  '#%'

                transport.write(msg.encode('utf-8'))
                raise ClientError

        c = self.client_manager.new_client(transport)
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client):
        """
        Remove a disconnected client.
        :param client: client object

        """
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    @property
    def player_count(self):
        """Get the number of non-spectating clients."""
        return len([client for client in self.client_manager.clients
            if client.char_id != -1])

    def load_config(self):
        """Load the main server configuration from a YAML file."""
        try:
            with open('config/config.yaml', 'r', encoding='utf-8') as cfg:
                self.config = yaml.safe_load(cfg)
                self.config['motd'] = self.config['motd'].replace('\\n', ' \n')
        except OSError:
            print('error: config/config.yaml wasn\'t found.')
            print('You are either running from the wrong directory, or')
            print('you forgot to rename config_sample (read the instructions).')
            sys.exit(1)

        if 'music_change_floodguard' not in self.config:
            self.config['music_change_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        if 'wtce_floodguard' not in self.config:
            self.config['wtce_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }

        if 'zalgo_tolerance' not in self.config:
            self.config['zalgo_tolerance'] = 3

        if isinstance(self.config['modpass'], str):
            self.config['modpass'] = {'default': {'password': self.config['modpass']}}
        if 'multiclient_limit' not in self.config:
            self.config['multiclient_limit'] = 16

    def load_characters(self):
        """Load the character list from a YAML file."""
        with open('config/characters.yaml', 'r', encoding='utf-8') as chars:
            self.char_list = yaml.safe_load(chars)
        self.build_char_pages_ao1()
        self.char_emotes = {char: Emotes(char) for char in self.char_list}

    def load_music(self):
        self.build_music_list()
        self.music_pages_ao1 = self.build_music_pages_ao1(self.music_list)
        self.music_list_ao2 = self.build_music_list_ao2(self.music_list)

    def load_backgrounds(self):
        """Load the backgrounds list from a YAML file."""
        with open('config/backgrounds.yaml', 'r', encoding='utf-8') as bgs:
            self.backgrounds = yaml.safe_load(bgs)

    def load_iniswaps(self):
        """Load a list of characters for which INI swapping is allowed."""
        try:
            with open('config/iniswaps.yaml', 'r',
                      encoding='utf-8') as iniswaps:
                self.allowed_iniswaps = yaml.safe_load(iniswaps)
        except:
            logger.debug('Cannot find iniswaps.yaml')

    def load_ipranges(self):
        """Load a list of banned IP ranges."""
        try:
            with open('config/iprange_ban.txt', 'r',
                      encoding='utf-8') as ipranges:
                self.ipRange_bans = ipranges.read().splitlines()
        except:
            logger.debug('Cannot find iprange_ban.txt')

    def build_char_pages_ao1(self):
        """
        Cache a list of characters that can be used for the
        AO1 connection handshake.
        """
        self.char_pages_ao1 = [
            self.char_list[x:x + 10] for x in range(0, len(self.char_list), 10)
        ]
        for i in range(len(self.char_list)):
            self.char_pages_ao1[i // 10][i % 10] = '{}#{}&&0&&&0&'.format(
                i, self.char_list[i])

    def build_music_list(self):
        with open('config/music.yaml', 'r', encoding='utf-8') as music:
            self.music_list = yaml.safe_load(music)

    def build_music_pages_ao1(self, music_list):
        song_list = []
        index = 0
        for item in music_list:
            if 'category' not in item:
                continue
            song_list.append('{}#{}'.format(index, item['category']))
            index += 1
            for song in item['songs']:
                if self._is_valid_song_name(song['name']):
                    song_list.append('{}#{}'.format(index, song['name']))
                    index += 1
                else:
                    logger.debug(f"{song['name']} is not a valid song name")
        song_list = [song_list[x:x + 10] for x in range(0, len(song_list), 10)]
        return song_list

    def build_music_list_ao2(self, music_list):
        song_list = []
        for item in music_list:
            if 'category' not in item: #skip settings n stuff
                continue
            song_list.append(item['category'])
            for song in item['songs']:
                if self._is_valid_song_name(song['name']):
                    song_list.append(song['name'])
                else:
                    logger.debug(f"{song['name']} is not a valid song name")
        return song_list

    @staticmethod
    def _is_valid_song_name(song_name: str) -> bool:
        return '.' in song_name

    def is_valid_char_id(self, char_id):
        """
        Check if a character ID is a valid one.
        :param char_id: character ID
        :returns: True if within length of character list; False otherwise

        """
        return len(self.char_list) > char_id >= 0

    def get_char_id_by_name(self, name):
        """
        Get a character ID by the name of the character.
        :param name: name of character
        :returns: Character ID

        """
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError('Character not found.')

    def get_song_data(self, music_list, music):
        """
        Get information about a track, if exists.
        :param music_list: music list to search
        :param music: track name
        :returns: tuple (name, length or -1)
        :raises: ServerError if track not found
        """
        for item in music_list:
            if 'category' not in item: #skip settings n stuff
                continue
            if item['category'] == music:
                return item['category'], -1
            for song in item['songs']:
                if song['name'] == music:
                    try:
                        return song['name'], song['length']
                    except KeyError:
                        return song['name'], -1
        raise ServerError('Music not found.')

    def get_song_is_category(self, music_list, music):
        """
        Get whether a track is a category.
        :param music_list: music list to search
        :param music: track name
        :returns: bool
        """
        for item in music_list:
            if 'category' not in item: #skip settings n stuff
                continue
            if item['category'] == music:
                return True
        return False
    
    def send_all_cmd_pred(self, cmd, *args, pred=lambda x: True):
        """
        Broadcast an AO-compatible command to all clients that satisfy
        a predicate.
        """
        for client in self.client_manager.clients:
            if pred(client):
                client.send_command(cmd, *args)

    def broadcast_global(self, client, msg, as_mod=False):
        """
        Broadcast an OOC message to all clients that do not have
        global chat muted.
        :param client: sender
        :param msg: message
        :param as_mod: add moderator prefix (Default value = False)

        """
        char_name = client.char_name
        ooc_name = '{}[{}][{}]'.format('<dollar>G', client.area.abbreviation,
                                       char_name)
        if as_mod:
            ooc_name += '[M]'
        self.send_all_cmd_pred('CT',
                               ooc_name,
                               msg,
                               pred=lambda x: not x.muted_global)

    def send_modchat(self, client, msg):
        """
        Send an OOC message to all mods.
        :param client: sender
        :param msg: message

        """
        name = client.name
        ooc_name = '{}[{}][{}]'.format('<dollar>M', client.area.abbreviation,
                                       name)
        self.send_all_cmd_pred('CT', ooc_name, msg, pred=lambda x: x.is_mod)

    def broadcast_need(self, client, msg):
        """
        Broadcast an OOC "need" message to all clients who do not
        have advertisements muted.
        :param client: sender
        :param msg: message

        """
        char_name = client.char_name
        area_name = client.area.name
        area_id = client.area.abbreviation
        self.send_all_cmd_pred(
            'CT',
            '{}'.format(self.config['hostname']),
            '=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
            format(char_name, area_name, area_id, msg),
            '1',
            pred=lambda x: not x.muted_adverts)

    def send_arup(self, args):
        """Update the area properties for 2.6 clients.

        Playercount:
            ARUP#0#<area1_p: int>#<area2_p: int>#...
        Status:
            ARUP#1##<area1_s: string>##<area2_s: string>#...
        CM:
            ARUP#2##<area1_cm: string>##<area2_cm: string>#...
        Lockedness:
            ARUP#3##<area1_l: string>##<area2_l: string>#...

        :param args:

        """
        if len(args) < 2:
            # An argument count smaller than 2 means we only got the identifier of ARUP.
            return
        if args[0] not in (0, 1, 2, 3):
            return

        if args[0] == 0:
            for part_arg in args[1:]:
                try:
                    _sanitised = int(part_arg)
                except:
                    return
        elif args[0] in (1, 2, 3):
            for part_arg in args[1:]:
                try:
                    _sanitised = str(part_arg)
                except:
                    return

        self.send_all_cmd_pred('ARUP', *args, pred=lambda x: True)

    def refresh(self):
        """
        Refresh as many parts of the server as possible:
         - MOTD
         - Mod credentials (unmodding users if necessary)
         - Characters
         - Music
         - Backgrounds
         - Commands
         - Banlists
        """
        with open('config/config.yaml', 'r') as cfg:
            cfg_yaml = yaml.safe_load(cfg)
            self.config['motd'] = cfg_yaml['motd'].replace('\\n', ' \n')

            # Reload moderator passwords list and unmod any moderator affected by
            # credential changes or removals
            if isinstance(self.config['modpass'], str):
                self.config['modpass'] = {'default': {'password': self.config['modpass']}}
            if isinstance(cfg_yaml['modpass'], str):
                cfg_yaml['modpass'] = {'default': {'password': cfg_yaml['modpass']}}

            for profile in self.config['modpass']:
                if profile not in cfg_yaml['modpass'] or \
                   self.config['modpass'][profile] != cfg_yaml['modpass'][profile]:
                    for client in filter(
                            lambda c: c.mod_profile_name == profile,
                            self.client_manager.clients):
                        client.is_mod = False
                        client.mod_profile_name = None
                        database.log_misc('unmod.modpass', client)
                        client.send_ooc(
                            'Your moderator credentials have been revoked.')
            self.config['modpass'] = cfg_yaml['modpass']

        self.load_characters()
        self.load_iniswaps()
        self.load_music()
        self.load_backgrounds()
        self.load_ipranges()

        import server.commands
        importlib.reload(server.commands)
        server.commands.reload()
Esempio n. 3
0
class TsuServer3:
    def __init__(self):
        self.client_manager = ClientManager(self)
        self.area_manager = AreaManager(self)
        self.ban_manager = BanManager()
        self.version = 'tsuserver3dev'
        self.software = 'tsuserver3'
        self.release = 3
        self.major_version = 0
        self.minor_version = 1
        self.char_list = None
        self.char_pages_ao1 = None
        self.music_list = None
        self.music_list_ao2 = None
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.config = None
        self.load_config()
        self.load_characters()
        self.load_music()
        self.load_backgrounds()
        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        logger.setup_logger(debug=self.config['debug'])

    def start(self):
        loop = asyncio.get_event_loop()

        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'

        ao_server_crt = loop.create_server(lambda: AOProtocol(self), bound_ip,
                                           self.config['port'])
        ao_server = loop.run_until_complete(ao_server_crt)

        if self.config['use_district']:
            self.district_client = DistrictClient(self)
            asyncio.ensure_future(self.district_client.connect(), loop=loop)

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            asyncio.ensure_future(self.ms_client.connect(), loop=loop)

        logger.log_debug('Server started.')

        try:
            loop.run_forever()
        except KeyboardInterrupt:
            pass

        logger.log_debug('Server shutting down.')

        ao_server.close()
        loop.run_until_complete(ao_server.wait_closed())
        loop.close()

    def get_version_string(self):
        return str(self.release) + '.' + str(self.major_version) + '.' + str(
            self.minor_version)

    def new_client(self, transport):
        c = self.client_manager.new_client(transport)
        if self.rp_mode:
            c.in_rp = True
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client):
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    def get_player_count(self):
        return len(self.client_manager.clients)

    def load_config(self):
        with open('config/config.yaml', 'r') as cfg:
            self.config = yaml.load(cfg)

    def load_characters(self):
        with open('config/characters.yaml', 'r') as chars:
            self.char_list = yaml.load(chars)
        self.build_char_pages_ao1()

    def load_music(self):
        with open('config/music.yaml', 'r') as music:
            self.music_list = yaml.load(music)
        self.build_music_pages_ao1()
        self.build_music_list_ao2()

    def load_backgrounds(self):
        with open('config/backgrounds.yaml', 'r') as bgs:
            self.backgrounds = yaml.load(bgs)

    def build_char_pages_ao1(self):
        self.char_pages_ao1 = [
            self.char_list[x:x + 10] for x in range(0, len(self.char_list), 10)
        ]
        for i in range(len(self.char_list)):
            self.char_pages_ao1[i // 10][i % 10] = '{}#{}&&0&&&0&'.format(
                i, self.char_list[i])

    def build_music_pages_ao1(self):
        self.music_pages_ao1 = []
        index = 0
        # add areas first
        for area in self.area_manager.areas:
            self.music_pages_ao1.append('{}#{}'.format(index, area.name))
            index += 1
        # then add music
        for item in self.music_list:
            self.music_pages_ao1.append('{}#{}'.format(index,
                                                       item['category']))
            index += 1
            for song in item['songs']:
                self.music_pages_ao1.append('{}#{}'.format(
                    index, song['name']))
                index += 1
        self.music_pages_ao1 = [
            self.music_pages_ao1[x:x + 10]
            for x in range(0, len(self.music_pages_ao1), 10)
        ]

    def build_music_list_ao2(self):
        self.music_list_ao2 = []
        # add areas first
        for area in self.area_manager.areas:
            self.music_list_ao2.append(area.name)
            # then add music
        for item in self.music_list:
            self.music_list_ao2.append(item['category'])
            for song in item['songs']:
                self.music_list_ao2.append(song['name'])

    def is_valid_char_id(self, char_id):
        return len(self.char_list) > char_id >= 0

    def get_char_id_by_name(self, name):
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError('Character not found.')

    def get_song_data(self, music):
        for item in self.music_list:
            if item['category'] == music:
                return item['category'], -1
            for song in item['songs']:
                if song['name'] == music:
                    try:
                        return song['name'], song['length']
                    except KeyError:
                        return song['name'], -1
        raise ServerError('Music not found.')

    def send_all_cmd_pred(self, cmd, *args, pred=lambda x: True):
        for client in self.client_manager.clients:
            if pred(client):
                client.send_command(cmd, *args)

    def broadcast_global(self, client, msg, as_mod=False):
        char_name = client.get_char_name()
        ooc_name = '{}[{}][{}]'.format('<dollar>G', client.area.id, char_name)
        if as_mod:
            ooc_name += '[M]'
        self.send_all_cmd_pred('CT',
                               ooc_name,
                               msg,
                               pred=lambda x: not x.muted_global)
        if self.config['use_district']:
            self.district_client.send_raw_message('GLOBAL#{}#{}#{}#{}'.format(
                int(as_mod), client.area.id, char_name, msg))

    def broadcast_need(self, client, msg):
        char_name = client.get_char_name()
        area_name = client.area.name
        area_id = client.area.id
        self.send_all_cmd_pred(
            'CT',
            '{}'.format(self.config['hostname']),
            '=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
            format(char_name, area_name, area_id, msg),
            pred=lambda x: not x.muted_adverts)
        if self.config['use_district']:
            self.district_client.send_raw_message('NEED#{}#{}#{}#{}'.format(
                char_name, area_name, area_id, msg))
Esempio n. 4
0
class TsuServer3:
    """The main class for tsuserver3 server software."""
    def __init__(self):
        self.software = 'tsuserver3'
        self.release = 3
        self.major_version = 3
        self.minor_version = 0

        self.config = None
        self.allowed_iniswaps = []
        self.char_list = None
        self.char_emotes = None
        self.char_pages_ao1 = None
        self.music_list = None
        self.music_list_ao2 = None
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.zalgo_tolerance = None

        self.ms_client = None

        try:
            self.load_config()
            self.area_manager = AreaManager(self)
            self.load_iniswaps()
            self.load_characters()
            self.load_music()
            self.load_backgrounds()
        except yaml.YAMLError as exc:
            print('There was a syntax error parsing a configuration file:')
            print(exc)
            print('Please revise your syntax and restart the server.')
            # Truly idiotproof
            if os.name == 'nt':
                input('(Press Enter to exit)')
            sys.exit(1)

        self.client_manager = ClientManager(self)
        server.logger.setup_logger(debug=self.config['debug'])

    def start(self):
        """Start the server."""
        loop = asyncio.get_event_loop()

        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'

        ao_server_crt = loop.create_server(lambda: AOProtocol(self), bound_ip,
                                           self.config['port'])
        ao_server = loop.run_until_complete(ao_server_crt)

        if self.config['use_websockets']:
            ao_server_ws = websockets.serve(new_websocket_client(self),
                                            bound_ip,
                                            self.config['websocket_port'])
            asyncio.ensure_future(ao_server_ws)

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            asyncio.ensure_future(self.ms_client.connect(), loop=loop)

        if self.config['zalgo_tolerance']:
            self.zalgo_tolerance = self.config['zalgo_tolerance']

        asyncio.ensure_future(self.schedule_unbans())

        database.log_misc('start')
        print('Server started and is listening on port {}'.format(
            self.config['port']))

        try:
            loop.run_forever()
        except KeyboardInterrupt:
            pass

        database.log_misc('stop')

        ao_server.close()
        loop.run_until_complete(ao_server.wait_closed())
        loop.close()

    async def schedule_unbans(self):
        while True:
            database.schedule_unbans()
            await asyncio.sleep(3600 * 12)

    @property
    def version(self):
        """Get the server's current version."""
        return f'{self.release}.{self.major_version}.{self.minor_version}'

    def new_client(self, transport):
        """
        Create a new client based on a raw transport by passing
        it to the client manager.
        :param transport: asyncio transport
        :returns: created client object
        """
        c = self.client_manager.new_client(transport)
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client):
        """
        Remove a disconnected client.
        :param client: client object

        """
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    @property
    def player_count(self):
        """Get the number of non-spectating clients."""
        return len([
            client for client in self.client_manager.clients
            if client.char_id != -1
        ])

    def load_config(self):
        """Load the main server configuration from a YAML file."""
        with open('config/config.yaml', 'r', encoding='utf-8') as cfg:
            self.config = yaml.safe_load(cfg)
            self.config['motd'] = self.config['motd'].replace('\\n', ' \n')
        if 'music_change_floodguard' not in self.config:
            self.config['music_change_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        if 'wtce_floodguard' not in self.config:
            self.config['wtce_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }

        if 'zalgo_tolerance' not in self.config:
            self.config['zalgo_tolerance'] = 3

        if isinstance(self.config['modpass'], str):
            self.config['modpass'] = {
                'default': {
                    'password': self.config['modpass']
                }
            }

    def load_characters(self):
        """Load the character list from a YAML file."""
        with open('config/characters.yaml', 'r', encoding='utf-8') as chars:
            self.char_list = yaml.safe_load(chars)
        self.build_char_pages_ao1()
        self.char_emotes = {char: Emotes(char) for char in self.char_list}

    def load_music(self):
        """Load the music list from a YAML file."""
        with open('config/music.yaml', 'r', encoding='utf-8') as music:
            self.music_list = yaml.safe_load(music)
        self.build_music_pages_ao1()
        self.build_music_list_ao2()

    def load_backgrounds(self):
        """Load the backgrounds list from a YAML file."""
        with open('config/backgrounds.yaml', 'r', encoding='utf-8') as bgs:
            self.backgrounds = yaml.safe_load(bgs)

    def load_iniswaps(self):
        """Load a list of characters for which INI swapping is allowed."""
        try:
            with open('config/iniswaps.yaml', 'r',
                      encoding='utf-8') as iniswaps:
                self.allowed_iniswaps = yaml.safe_load(iniswaps)
        except:
            logger.debug('Cannot find iniswaps.yaml')

    def build_char_pages_ao1(self):
        """
        Cache a list of characters that can be used for the
        AO1 connection handshake.
        """
        self.char_pages_ao1 = [
            self.char_list[x:x + 10] for x in range(0, len(self.char_list), 10)
        ]
        for i in range(len(self.char_list)):
            self.char_pages_ao1[i // 10][i % 10] = '{}#{}&&0&&&0&'.format(
                i, self.char_list[i])

    def build_music_pages_ao1(self):
        """
        Cache a list of tracks that can be used for the
        AO1 connection handshake.
        """
        self.music_pages_ao1 = []
        index = 0
        # add areas first
        for area in self.area_manager.areas:
            self.music_pages_ao1.append(f'{index}#{area.name}')
            index += 1
        # then add music
        for item in self.music_list:
            self.music_pages_ao1.append('{}#{}'.format(index,
                                                       item['category']))
            index += 1
            for song in item['songs']:
                self.music_pages_ao1.append('{}#{}'.format(
                    index, song['name']))
                index += 1
        self.music_pages_ao1 = [
            self.music_pages_ao1[x:x + 10]
            for x in range(0, len(self.music_pages_ao1), 10)
        ]

    def build_music_list_ao2(self):
        """
        Cache a list of tracks that can be used for the
        AO2 connection handshake.
        """
        self.music_list_ao2 = []
        # add areas first
        for area in self.area_manager.areas:
            self.music_list_ao2.append(area.name)
            # then add music
        for item in self.music_list:
            self.music_list_ao2.append(item['category'])
            for song in item['songs']:
                self.music_list_ao2.append(song['name'])

    def is_valid_char_id(self, char_id):
        """
        Check if a character ID is a valid one.
        :param char_id: character ID
        :returns: True if within length of character list; False otherwise

        """
        return len(self.char_list) > char_id >= 0

    def get_char_id_by_name(self, name):
        """
        Get a character ID by the name of the character.
        :param name: name of character
        :returns: Character ID

        """
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError('Character not found.')

    def get_song_data(self, music):
        """
        Get information about a track, if exists.
        :param music: track name
        :returns: tuple (name, length or -1)
        :raises: ServerError if track not found
        """
        for item in self.music_list:
            if item['category'] == music:
                return item['category'], -1
            for song in item['songs']:
                if song['name'] == music:
                    try:
                        return song['name'], song['length']
                    except KeyError:
                        return song['name'], -1
        raise ServerError('Music not found.')

    def send_all_cmd_pred(self, cmd, *args, pred=lambda x: True):
        """
        Broadcast an AO-compatible command to all clients that satisfy
        a predicate.
        """
        for client in self.client_manager.clients:
            if pred(client):
                client.send_command(cmd, *args)

    def broadcast_global(self, client, msg, as_mod=False):
        """
        Broadcast an OOC message to all clients that do not have
        global chat muted.
        :param client: sender
        :param msg: message
        :param as_mod: add moderator prefix (Default value = False)

        """
        char_name = client.char_name
        ooc_name = '{}[{}][{}]'.format('<dollar>G', client.area.abbreviation,
                                       char_name)
        if as_mod:
            ooc_name += '[M]'
        self.send_all_cmd_pred('CT',
                               ooc_name,
                               msg,
                               pred=lambda x: not x.muted_global)

    def send_modchat(self, client, msg):
        """
        Send an OOC message to all mods.
        :param client: sender
        :param msg: message

        """
        name = client.name
        ooc_name = '{}[{}][{}]'.format('<dollar>M', client.area.abbreviation,
                                       name)
        self.send_all_cmd_pred('CT', ooc_name, msg, pred=lambda x: x.is_mod)

    def broadcast_need(self, client, msg):
        """
        Broadcast an OOC "need" message to all clients who do not
        have advertisements muted.
        :param client: sender
        :param msg: message

        """
        char_name = client.char_name
        area_name = client.area.name
        area_id = client.area.abbreviation
        self.send_all_cmd_pred(
            'CT',
            '{}'.format(self.config['hostname']),
            '=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
            format(char_name, area_name, area_id, msg),
            '1',
            pred=lambda x: not x.muted_adverts)

    def send_arup(self, args):
        """Update the area properties for 2.6 clients.
        
        Playercount:
            ARUP#0#<area1_p: int>#<area2_p: int>#...
        Status:
            ARUP#1##<area1_s: string>##<area2_s: string>#...
        CM:
            ARUP#2##<area1_cm: string>##<area2_cm: string>#...
        Lockedness:
            ARUP#3##<area1_l: string>##<area2_l: string>#...

        :param args: 

        """
        if len(args) < 2:
            # An argument count smaller than 2 means we only got the identifier of ARUP.
            return
        if args[0] not in (0, 1, 2, 3):
            return

        if args[0] == 0:
            for part_arg in args[1:]:
                try:
                    _sanitised = int(part_arg)
                except:
                    return
        elif args[0] in (1, 2, 3):
            for part_arg in args[1:]:
                try:
                    _sanitised = str(part_arg)
                except:
                    return

        self.send_all_cmd_pred('ARUP', *args, pred=lambda x: True)

    def refresh(self):
        """
        Refresh as many parts of the server as possible:
         - MOTD
         - Mod credentials (unmodding users if necessary)
         - Characters
         - Music
         - Backgrounds
         - Commands
        """
        with open('config/config.yaml', 'r') as cfg:
            cfg_yaml = yaml.safe_load(cfg)
            self.config['motd'] = cfg_yaml['motd'].replace('\\n', ' \n')

            # Reload moderator passwords list and unmod any moderator affected by
            # credential changes or removals
            if isinstance(self.config['modpass'], str):
                self.config['modpass'] = {
                    'default': {
                        'password': self.config['modpass']
                    }
                }
            if isinstance(cfg_yaml['modpass'], str):
                cfg_yaml['modpass'] = {
                    'default': {
                        'password': cfg_yaml['modpass']
                    }
                }

            for profile in self.config['modpass']:
                if profile not in cfg_yaml['modpass'] or \
                   self.config['modpass'][profile] != cfg_yaml['modpass'][profile]:
                    for client in filter(
                            lambda c: c.mod_profile_name == profile,
                            self.client_manager.clients):
                        client.is_mod = False
                        client.mod_profile_name = None
                        database.log_misc('unmod.modpass', client)
                        client.send_ooc(
                            'Your moderator credentials have been revoked.')
            self.config['modpass'] = cfg_yaml['modpass']

        self.load_characters()
        self.load_iniswaps()
        self.load_music()
        self.load_backgrounds()

        import server.commands
        importlib.reload(server.commands)
        server.commands.reload()
Esempio n. 5
0
    def __init__(self, protocol=None, client_manager=None, in_test=False):
        self.release = 4
        self.major_version = 2
        self.minor_version = 5
        self.segment_version = 'post3'
        self.internal_version = '200807a'
        version_string = self.get_version_string()
        self.software = 'TsuserverDR {}'.format(version_string)
        self.version = 'TsuserverDR {} ({})'.format(version_string,
                                                    self.internal_version)
        self.in_test = in_test

        self.protocol = AOProtocol if protocol is None else protocol
        client_manager = ClientManager if client_manager is None else client_manager
        logger.log_print = logger.log_print2 if self.in_test else logger.log_print
        logger.log_server = logger.log_server2 if self.in_test else logger.log_server
        self.random = importlib.reload(random)

        logger.log_print('Launching {}...'.format(self.version))
        logger.log_print('Loading server configurations...')

        self.config = None
        self.local_connection = None
        self.district_connection = None
        self.masterserver_connection = None
        self.shutting_down = False
        self.loop = None
        self.last_error = None
        self.allowed_iniswaps = None
        self.area_list = None
        self.old_area_list = None
        self.default_area = 0
        self.all_passwords = list()

        self.load_config()
        self.load_iniswaps()
        self.char_list = list()
        self.char_pages_ao1 = None
        self.load_characters()
        self.load_commandhelp()
        self.client_manager = client_manager(self)
        self.zone_manager = ZoneManager(self)
        self.area_manager = AreaManager(self)
        self.ban_manager = BanManager(self)
        self.party_manager = PartyManager(self)

        self.ipid_list = {}
        self.hdid_list = {}
        self.music_list = None
        self._music_list_ao2 = None  # Pending deprecation in 4.3
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.load_music()
        self.load_backgrounds()
        self.load_ids()
        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        self.user_auth_req = False
        # self.client_tasks = dict() # KEPT FOR BACKWARDS COMPATIBILITY
        # self.active_timers = dict() # KEPT FOR BACKWARDS COMPATIBILITY
        self.showname_freeze = False
        self.commands = importlib.import_module('server.commands')
        self.commands_alt = importlib.import_module('server.commands_alt')
        self.logger_handlers = logger.setup_logger(debug=self.config['debug'])

        logger.log_print('Server configurations loaded successfully!')
Esempio n. 6
0
class TsuServer3:
    def __init__(self):
        self.config = None
        self.allowed_iniswaps = None
        self.load_config()
        self.load_iniswaps()
        self.client_manager = ClientManager(self)
        self.area_manager = AreaManager(self)
        self.ban_manager = BanManager()
        self.software = 'tsuserver3'
        self.version = 'vanilla'
        self.release = 3
        self.major_version = 2
        self.minor_version = 0
        self.ipid_list = {}
        self.hdid_list = {}
        self.char_list = None
        self.char_pages_ao1 = None
        self.music_list = None
        self.music_list_ao2 = None
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.load_characters()
        self.load_music()
        self.load_backgrounds()
        self.load_ids()
        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        logger.setup_logger(debug=self.config['debug'])

    def start(self):
        loop = asyncio.get_event_loop()

        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'

        ao_server_crt = loop.create_server(lambda: AOProtocol(self), bound_ip,
                                           self.config['port'])
        ao_server = loop.run_until_complete(ao_server_crt)

        if self.config['use_websockets']:
            ao_server_ws = websockets.serve(new_websocket_client(self),
                                            bound_ip,
                                            self.config['websocket_port'])
            asyncio.ensure_future(ao_server_ws)

        if self.config['use_district']:
            self.district_client = DistrictClient(self)
            asyncio.ensure_future(self.district_client.connect(), loop=loop)

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            asyncio.ensure_future(self.ms_client.connect(), loop=loop)

        logger.log_debug('Server started.')
        print('Server started and is listening on port {}'.format(
            self.config['port']))

        try:
            loop.run_forever()
        except KeyboardInterrupt:
            pass

        logger.log_debug('Server shutting down.')

        ao_server.close()
        loop.run_until_complete(ao_server.wait_closed())
        loop.close()

    def get_version_string(self):
        return str(self.release) + '.' + str(self.major_version) + '.' + str(
            self.minor_version)

    def new_client(self, transport):
        c = self.client_manager.new_client(transport)
        if self.rp_mode:
            c.in_rp = True
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client):
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    def get_player_count(self):
        return len(self.client_manager.clients)

    def load_config(self):
        with open('config/config.yaml', 'r', encoding='utf-8') as cfg:
            self.config = yaml.load(cfg)
            self.config['motd'] = self.config['motd'].replace('\\n', ' \n')
        if 'music_change_floodguard' not in self.config:
            self.config['music_change_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        if 'wtce_floodguard' not in self.config:
            self.config['wtce_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }

    def load_characters(self):
        with open('config/characters.yaml', 'r', encoding='utf-8') as chars:
            self.char_list = yaml.load(chars)
        self.build_char_pages_ao1()

    def load_music(self):
        with open('config/music.yaml', 'r', encoding='utf-8') as music:
            self.music_list = yaml.load(music)
        self.build_music_pages_ao1()
        self.build_music_list_ao2()

    def load_ids(self):
        self.ipid_list = {}
        self.hdid_list = {}
        # load ipids
        try:
            with open('storage/ip_ids.json', 'r',
                      encoding='utf-8') as whole_list:
                self.ipid_list = json.loads(whole_list.read())
        except:
            logger.log_debug(
                'Failed to load ip_ids.json from ./storage. If ip_ids.json is exist then remove it.'
            )
        # load hdids
        try:
            with open('storage/hd_ids.json', 'r',
                      encoding='utf-8') as whole_list:
                self.hdid_list = json.loads(whole_list.read())
        except:
            logger.log_debug(
                'Failed to load hd_ids.json from ./storage. If hd_ids.json is exist then remove it.'
            )

    def dump_ipids(self):
        with open('storage/ip_ids.json', 'w') as whole_list:
            json.dump(self.ipid_list, whole_list)

    def dump_hdids(self):
        with open('storage/hd_ids.json', 'w') as whole_list:
            json.dump(self.hdid_list, whole_list)

    def get_ipid(self, ip):
        if not (ip in self.ipid_list):
            self.ipid_list[ip] = len(self.ipid_list)
            self.dump_ipids()
        return self.ipid_list[ip]

    def load_backgrounds(self):
        with open('config/backgrounds.yaml', 'r', encoding='utf-8') as bgs:
            self.backgrounds = yaml.load(bgs)

    def load_iniswaps(self):
        try:
            with open('config/iniswaps.yaml', 'r',
                      encoding='utf-8') as iniswaps:
                self.allowed_iniswaps = yaml.load(iniswaps)
        except:
            logger.log_debug('cannot find iniswaps.yaml')

    def build_char_pages_ao1(self):
        self.char_pages_ao1 = [
            self.char_list[x:x + 10] for x in range(0, len(self.char_list), 10)
        ]
        for i in range(len(self.char_list)):
            self.char_pages_ao1[i // 10][i % 10] = '{}#{}&&0&&&0&'.format(
                i, self.char_list[i])

    def build_music_pages_ao1(self):
        self.music_pages_ao1 = []
        index = 0
        # add areas first
        for area in self.area_manager.areas:
            self.music_pages_ao1.append('{}#{}'.format(index, area.name))
            index += 1
        # then add music
        for item in self.music_list:
            self.music_pages_ao1.append('{}#{}'.format(index,
                                                       item['category']))
            index += 1
            for song in item['songs']:
                self.music_pages_ao1.append('{}#{}'.format(
                    index, song['name']))
                index += 1
        self.music_pages_ao1 = [
            self.music_pages_ao1[x:x + 10]
            for x in range(0, len(self.music_pages_ao1), 10)
        ]

    def build_music_list_ao2(self):
        self.music_list_ao2 = []
        # add areas first
        for area in self.area_manager.areas:
            self.music_list_ao2.append(area.name)
            # then add music
        for item in self.music_list:
            self.music_list_ao2.append(item['category'])
            for song in item['songs']:
                self.music_list_ao2.append(song['name'])

    def is_valid_char_id(self, char_id):
        return len(self.char_list) > char_id >= 0

    def get_char_id_by_name(self, name):
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError('Character not found.')

    def get_song_data(self, music):
        for item in self.music_list:
            if item['category'] == music:
                return item['category'], -1
            for song in item['songs']:
                if song['name'] == music:
                    try:
                        return song['name'], song['length']
                    except KeyError:
                        return song['name'], -1
        raise ServerError('Music not found.')

    def send_all_cmd_pred(self, cmd, *args, pred=lambda x: True):
        for client in self.client_manager.clients:
            if pred(client):
                client.send_command(cmd, *args)

    def broadcast_global(self, client, msg, as_mod=False):
        char_name = client.get_char_name()
        ooc_name = '{}[{}][{}]'.format('<dollar>G', client.area.abbreviation,
                                       char_name)
        if as_mod:
            ooc_name += '[M]'
        self.send_all_cmd_pred('CT',
                               ooc_name,
                               msg,
                               pred=lambda x: not x.muted_global)
        if self.config['use_district']:
            self.district_client.send_raw_message('GLOBAL#{}#{}#{}#{}'.format(
                int(as_mod), client.area.id, char_name, msg))

    def send_modchat(self, client, msg):
        char_name = client.get_char_name()
        name = client.name
        ooc_name = '{}[{}][{}]'.format('<dollar>M', client.area.abbreviation,
                                       name)
        self.send_all_cmd_pred('CT', ooc_name, msg, pred=lambda x: x.is_mod)
        if self.config['use_district']:
            self.district_client.send_raw_message('MODCHAT#{}#{}#{}'.format(
                client.area.id, char_name, msg))

    def broadcast_need(self, client, msg):
        char_name = client.get_char_name()
        area_name = client.area.name
        area_id = client.area.abbreviation
        self.send_all_cmd_pred(
            'CT',
            '{}'.format(self.config['hostname']),
            '=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
            format(char_name, area_name, area_id, msg),
            '1',
            pred=lambda x: not x.muted_adverts)
        if self.config['use_district']:
            self.district_client.send_raw_message('NEED#{}#{}#{}#{}'.format(
                char_name, area_name, area_id, msg))

    def send_arup(self, args):
        """ Updates the area properties on the Case Café Custom Client.

        Playercount: 
            ARUP#0#<area1_p: int>#<area2_p: int>#...
        Status:
            ARUP#1##<area1_s: string>##<area2_s: string>#...
        CM:
            ARUP#2##<area1_cm: string>##<area2_cm: string>#...
        Lockedness:
            ARUP#3##<area1_l: string>##<area2_l: string>#...

        """
        if len(args) < 2:
            # An argument count smaller than 2 means we only got the identifier of ARUP.
            return
        if args[0] not in (0, 1, 2, 3):
            return

        if args[0] == 0:
            for part_arg in args[1:]:
                try:
                    sanitised = int(part_arg)
                except:
                    return
        elif args[0] in (1, 2, 3):
            for part_arg in args[1:]:
                try:
                    sanitised = str(part_arg)
                except:
                    return

        self.send_all_cmd_pred('ARUP', *args, pred=lambda x: True)

    def refresh(self):
        with open('config/config.yaml', 'r') as cfg:
            self.config['motd'] = yaml.load(cfg)['motd'].replace('\\n', ' \n')
        with open('config/characters.yaml', 'r') as chars:
            self.char_list = yaml.load(chars)
        with open('config/music.yaml', 'r') as music:
            self.music_list = yaml.load(music)
        self.build_music_pages_ao1()
        self.build_music_list_ao2()
        with open('config/backgrounds.yaml', 'r') as bgs:
            self.backgrounds = yaml.load(bgs)
Esempio n. 7
0
    def __init__(self):
        self.config = None
        self.allowed_iniswaps = None
        self.load_config()
        self.load_iniswaps()
        self.client_manager = ClientManager(self)
        self.area_manager = AreaManager(self)
        self.musiclist_manager = MusicListManager(self)
        self.hub_manager = HubManager(self)
        self.software = 'tsuservercc'

        self.release = 1
        self.major_version = 5
        self.minor_version = 0

        self.config = None
        self.allowed_iniswaps = []
        self.char_list = None
        self.char_emotes = None
        self.char_pages_ao1 = None
        self.music_list = None
        self.music_list_ao2 = None
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.zalgo_tolerance = None
        self.ipRange_bans = []
        self.geoIpReader = None
        self.useGeoIp = False
        self.webperms = []

        try:
            self.geoIpReader = geoip2.database.Reader(
                './storage/GeoLite2-ASN.mmdb')
            self.useGeoIp = True
            # on debian systems you can use /usr/share/GeoIP/GeoIPASNum.dat if the geoip-database-extra package is installed
        except FileNotFoundError:
            self.useGeoIp = False
            pass

        self.is_poll = False
        self.poll = ''
        self.pollyay = []
        self.pollnay = []
        self.parties = []
        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        self.runner = False
        self.runtime = 0

        try:
            self.load_config()
            self.load_characters()
            self.load_music()
            self.load_backgrounds()
            self.load_ipranges()
            self.load_webperms()
            self.load_gimps()
        except yaml.YAMLError as exc:
            print('There was a syntax error parsing a configuration file:')
            print(exc)
            print('Please revise your syntax and restart the server.')
            # Truly idiotproof
            if os.name == 'nt':
                input('(Press Enter to exit)')
            sys.exit(1)

        server.logger.setup_logger(debug=self.config['debug'])
Esempio n. 8
0
class TsuserverDR:
    def __init__(self, protocol=None, client_manager=None, in_test=False):
        self.release = 4
        self.major_version = 2
        self.minor_version = 5
        self.segment_version = 'post3'
        self.internal_version = '200807a'
        version_string = self.get_version_string()
        self.software = 'TsuserverDR {}'.format(version_string)
        self.version = 'TsuserverDR {} ({})'.format(version_string,
                                                    self.internal_version)
        self.in_test = in_test

        self.protocol = AOProtocol if protocol is None else protocol
        client_manager = ClientManager if client_manager is None else client_manager
        logger.log_print = logger.log_print2 if self.in_test else logger.log_print
        logger.log_server = logger.log_server2 if self.in_test else logger.log_server
        self.random = importlib.reload(random)

        logger.log_print('Launching {}...'.format(self.version))
        logger.log_print('Loading server configurations...')

        self.config = None
        self.local_connection = None
        self.district_connection = None
        self.masterserver_connection = None
        self.shutting_down = False
        self.loop = None
        self.last_error = None
        self.allowed_iniswaps = None
        self.area_list = None
        self.old_area_list = None
        self.default_area = 0
        self.all_passwords = list()

        self.load_config()
        self.load_iniswaps()
        self.char_list = list()
        self.char_pages_ao1 = None
        self.load_characters()
        self.load_commandhelp()
        self.client_manager = client_manager(self)
        self.zone_manager = ZoneManager(self)
        self.area_manager = AreaManager(self)
        self.ban_manager = BanManager(self)
        self.party_manager = PartyManager(self)

        self.ipid_list = {}
        self.hdid_list = {}
        self.music_list = None
        self._music_list_ao2 = None  # Pending deprecation in 4.3
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.load_music()
        self.load_backgrounds()
        self.load_ids()
        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        self.user_auth_req = False
        # self.client_tasks = dict() # KEPT FOR BACKWARDS COMPATIBILITY
        # self.active_timers = dict() # KEPT FOR BACKWARDS COMPATIBILITY
        self.showname_freeze = False
        self.commands = importlib.import_module('server.commands')
        self.commands_alt = importlib.import_module('server.commands_alt')
        self.logger_handlers = logger.setup_logger(debug=self.config['debug'])

        logger.log_print('Server configurations loaded successfully!')

    def start(self):
        try:
            self.loop = asyncio.get_event_loop()
        except RuntimeError:
            self.loop = asyncio.new_event_loop()

        self.tasker = Tasker(self, self.loop)
        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'
            server_name = 'localhost'
            logger.log_print('Starting a local server...')
        else:
            server_name = self.config['masterserver_name']
            logger.log_print('Starting a nonlocal server...')

        ao_server_crt = self.loop.create_server(lambda: self.protocol(self),
                                                bound_ip, self.config['port'])
        ao_server = self.loop.run_until_complete(ao_server_crt)

        logger.log_pserver('Server started successfully!')

        if self.config['local']:
            host_ip = '127.0.0.1'
        else:
            try:
                host_ip = (urllib.request.urlopen(
                    'https://api.ipify.org',
                    context=ssl.SSLContext()).read().decode('utf8'))
            except urllib.error.URLError as ex:
                host_ip = None
                logger.log_pdebug(
                    'Unable to obtain personal IP from https://api.ipify.org\n'
                    '{}: {}\n'
                    'Players may be unable to join.'.format(
                        type(ex).__name__, ex.reason))
        if host_ip is not None:
            logger.log_pdebug(
                'Server should be now accessible from {}:{}:{}'.format(
                    host_ip, self.config['port'], server_name))
        if not self.config['local']:
            logger.log_pdebug(
                'If you want to join your server from this device, you may need to '
                'join with this IP instead: 127.0.0.1:{}:localhost'.format(
                    self.config['port']))

        if self.config['local']:
            self.local_connection = asyncio.ensure_future(
                self.tasker.do_nothing(), loop=self.loop)

        if self.config['use_district']:
            self.district_client = DistrictClient(self)
            self.district_connection = asyncio.ensure_future(
                self.district_client.connect(), loop=self.loop)
            print(' ')
            logger.log_print(
                'Attempting to connect to district at {}:{}.'.format(
                    self.config['district_ip'], self.config['district_port']))

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            self.masterserver_connection = asyncio.ensure_future(
                self.ms_client.connect(), loop=self.loop)
            print(' ')
            logger.log_print(
                'Attempting to connect to the master server at {}:{} with the '
                'following details:'.format(self.config['masterserver_ip'],
                                            self.config['masterserver_port']))
            logger.log_print('*Server name: {}'.format(
                self.config['masterserver_name']))
            logger.log_print('*Server description: {}'.format(
                self.config['masterserver_description']))

        try:
            self.loop.run_forever()
        except KeyboardInterrupt:
            pass

        print('')  # Lame
        logger.log_pdebug('You have initiated a server shut down.')
        self.shutdown()

        ao_server.close()
        self.loop.run_until_complete(ao_server.wait_closed())
        self.loop.close()
        logger.log_pserver('Server has successfully shut down.')

    def shutdown(self):
        # Cleanup operations
        self.shutting_down = True

        # Cancel further polling for district/master server
        if self.local_connection:
            self.local_connection.cancel()
            self.loop.run_until_complete(
                self.tasker.await_cancellation(self.local_connection))

        if self.district_connection:
            self.district_connection.cancel()
            self.loop.run_until_complete(
                self.tasker.await_cancellation(self.district_connection))

        if self.masterserver_connection:
            self.masterserver_connection.cancel()
            self.loop.run_until_complete(
                self.tasker.await_cancellation(self.masterserver_connection))
            self.loop.run_until_complete(
                self.tasker.await_cancellation(self.ms_client.shutdown()))

        # Cancel pending client tasks and cleanly remove them from the areas
        players = self.get_player_count()
        logger.log_print('Kicking {} remaining client{}.'.format(
            players, 's' if players != 1 else ''))

        for client in self.client_manager.clients:
            client.disconnect()

    def get_version_string(self):
        mes = '{}.{}.{}'.format(self.release, self.major_version,
                                self.minor_version)
        if self.segment_version:
            mes = '{}-{}'.format(mes, self.segment_version)
        return mes

    def reload(self):
        with Constants.fopen('config/characters.yaml', 'r') as chars:
            self.char_list = Constants.yaml_load(chars)
        with Constants.fopen('config/music.yaml', 'r') as music:
            self.music_list = Constants.yaml_load(music)
        self.build_music_pages_ao1()
        self.build_music_list_ao2()
        with Constants.fopen('config/backgrounds.yaml', 'r') as bgs:
            self.backgrounds = Constants.yaml_load(bgs)

    def reload_commands(self):
        try:
            self.commands = importlib.reload(self.commands)
            self.commands_alt = importlib.reload(self.commands_alt)
        except Exception as error:
            return error

    def new_client(self, transport, ip=None, my_protocol=None):
        c = self.client_manager.new_client(transport, my_protocol=my_protocol)
        if self.rp_mode:
            c.in_rp = True
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client):
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    def get_player_count(self):
        # Ignore players in the server selection screen.
        return len([
            client for client in self.client_manager.clients
            if client.char_id is not None
        ])

    def load_backgrounds(self):
        with Constants.fopen('config/backgrounds.yaml', 'r',
                             encoding='utf-8') as bgs:
            self.backgrounds = Constants.yaml_load(bgs)

    def load_config(self):
        with Constants.fopen('config/config.yaml', 'r',
                             encoding='utf-8') as cfg:
            self.config = Constants.yaml_load(cfg)
            self.config['motd'] = self.config['motd'].replace('\\n', ' \n')
            self.all_passwords = list()
            # Mandatory passwords must be present in the configuration file. If they are not,
            # a server error will be raised.
            mandatory_passwords = ['modpass', 'cmpass', 'gmpass']
            for password in mandatory_passwords:
                if not (password not in self.config
                        or not str(self.config[password])):
                    self.all_passwords.append(self.config[password])
                else:
                    err = (
                        f'Password "{password}" is not defined in server/config.yaml. Please '
                        f'make sure it is set and try again.')
                    raise ServerError(err)

            # Daily (and guard) passwords are handled differently. They may optionally be left
            # blank or be not available. What this means is the server does not want a daily
            # password for that day (or a guard password)
            optional_passwords = ['guardpass'
                                  ] + [f'gmpass{i}' for i in range(1, 8)]
            for password in optional_passwords:
                if not (password not in self.config
                        or not str(self.config[password])):
                    self.all_passwords.append(self.config[password])
                else:
                    self.config[password] = None

        # Default values to fill in config.yaml if not present
        defaults_for_tags = {
            'utc_offset': 'local',
            'discord_link': None,
            'max_numdice': 20,
            'max_numfaces': 11037,
            'max_modifier_length': 12,
            'max_acceptable_term': 22074,
            'def_numdice': 1,
            'def_numfaces': 6,
            'def_modifier': '',
            'blackout_background': 'Blackout_HD',
            'default_area_description': 'No description.',
            'party_lights_timeout': 10,
            'show_ms2-prober': True,
            'showname_max_length': 30,
            'sneak_handicap': 5,
            'spectator_name': 'SPECTATOR',
            'music_change_floodguard': {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        }

        for (tag, value) in defaults_for_tags.items():
            if tag not in self.config:
                self.config[tag] = value

        # Check that all passwords were generated are unique
        passwords = [
            'guardpass', 'modpass', 'cmpass', 'gmpass', 'gmpass1', 'gmpass2',
            'gmpass3', 'gmpass4', 'gmpass5', 'gmpass6', 'gmpass7'
        ]

        for (i, password1) in enumerate(passwords):
            for (j, password2) in enumerate(passwords):
                if i != j and self.config[password1] == self.config[
                        password2] != None:
                    info = (
                        'Passwords "{}" and "{}" in server/config.yaml match. '
                        'Please change them so they are different and try again.'
                        .format(password1, password2))
                    raise ServerError(info)

    def load_characters(self):
        with Constants.fopen('config/characters.yaml', 'r',
                             encoding='utf-8') as chars:
            self.char_list = Constants.yaml_load(chars)
        self.build_char_pages_ao1()

    def load_commandhelp(self):
        with Constants.fopen('README.md', 'r', encoding='utf-8') as readme:
            lines = [x.rstrip() for x in readme.readlines()]

        self.linetorank = {
            '### User Commands': 'normie',
            '### GM Commands': 'gm',
            '### Community Manager Commands': 'cm',
            '### Moderator Commands': 'mod'
        }

        self.commandhelp = {
            'normie': dict(),
            'gm': dict(),
            'cm': dict(),
            'mod': dict()
        }

        # Look for the start of the command list
        try:
            start_index = lines.index('## Commands')
            end_index = lines.index('### Debug commands')
        except ValueError as error:
            error_mes = ", ".join([str(s) for s in error.args])
            message = (
                'Unable to generate help based on README.md: {}. Are you sure you have the '
                'latest README.md?'.format(error_mes))
            raise ServerError(message)

        rank = None
        current_command = None

        for line in lines[start_index:end_index]:
            # Check if empty line
            if not line:
                continue

            # Check if this line defines the rank we are taking a look at right now
            if line in self.linetorank.keys():
                rank = self.linetorank[line]
                current_command = None
                continue

            # Otherwise, check if we do not have a rank yet
            if rank is None:
                continue

            # Otherwise, check if this is the start of a command
            if line[0] == '*':
                # Get the command name
                command_split = line[4:].split('** ')
                if len(command_split) == 1:
                    # Case: * **version**
                    current_command = command_split[0][:-2]
                else:
                    # Case: * **uninvite** "ID/IPID"
                    current_command = command_split[0]

                formatted_line = '/{}'.format(line[2:])
                formatted_line = formatted_line.replace('**', '')
                self.commandhelp[rank][current_command] = [formatted_line]
                continue

            # Otherwise, line is part of command description, so add it to its current command desc
            #     - Unlocks your area, provided the lock came as a result of /lock.
            # ... assuming we have a command
            if current_command:
                self.commandhelp[rank][current_command].append(line[4:])
                continue

            # Otherwise, we have a line that is a description of the rank
            # Do nothing about them
            continue  # Not really needed, but made explicit

    def load_ids(self):
        self.ipid_list = {}
        self.hdid_list = {}

        #load ipids
        try:
            with Constants.fopen('storage/ip_ids.json', 'r',
                                 encoding='utf-8') as whole_list:
                self.ipid_list = json.loads(whole_list.read())
        except Exception as ex:
            message = 'WARNING: Error loading storage/ip_ids.json. Will assume empty values.\n'
            message += '{}: {}'.format(type(ex).__name__, ex)

            logger.log_pdebug(message)

        #load hdids
        try:
            with Constants.fopen('storage/hd_ids.json', 'r',
                                 encoding='utf-8') as whole_list:
                self.hdid_list = json.loads(whole_list.read())
        except Exception as ex:
            message = 'WARNING: Error loading storage/hd_ids.json. Will assume empty values.\n'
            message += '{}: {}'.format(type(ex).__name__, ex)

            logger.log_pdebug(message)

    def load_iniswaps(self):
        try:
            with Constants.fopen('config/iniswaps.yaml', 'r',
                                 encoding='utf-8') as iniswaps:
                self.allowed_iniswaps = Constants.yaml_load(iniswaps)
        except Exception as ex:
            message = 'WARNING: Error loading config/iniswaps.yaml. Will assume empty values.\n'
            message += '{}: {}'.format(type(ex).__name__, ex)

            logger.log_pdebug(message)

    def load_music(self,
                   music_list_file='config/music.yaml',
                   server_music_list=True):
        with Constants.fopen(music_list_file, 'r', encoding='utf-8') as music:
            music_list = Constants.yaml_load(music)

        if server_music_list:
            self.music_list = music_list
            self.build_music_pages_ao1()
            self.build_music_list_ao2(music_list=music_list)

        return music_list

    def dump_ipids(self):
        with Constants.fopen('storage/ip_ids.json', 'w') as whole_list:
            json.dump(self.ipid_list, whole_list)

    def dump_hdids(self):
        with Constants.fopen('storage/hd_ids.json', 'w') as whole_list:
            json.dump(self.hdid_list, whole_list)

    def get_ipid(self, ip):
        if not ip in self.ipid_list:
            while True:
                ipid = random.randint(0, 10**10 - 1)
                if ipid not in self.ipid_list.values():
                    break
            self.ipid_list[ip] = ipid
            self.dump_ipids()
        return self.ipid_list[ip]

    def build_char_pages_ao1(self):
        self.char_pages_ao1 = [
            self.char_list[x:x + 10] for x in range(0, len(self.char_list), 10)
        ]
        for i in range(len(self.char_list)):
            self.char_pages_ao1[i // 10][i % 10] = '{}#{}&&0&&&0&'.format(
                i, self.char_list[i])

    def build_music_pages_ao1(self):
        self.music_pages_ao1 = []
        index = 0
        # add areas first
        for area in self.area_manager.areas:
            self.music_pages_ao1.append('{}#{}'.format(index, area.name))
            index += 1
        # then add music
        try:
            for item in self.music_list:
                self.music_pages_ao1.append('{}#{}'.format(
                    index, item['category']))
                index += 1
                for song in item['songs']:
                    self.music_pages_ao1.append('{}#{}'.format(
                        index, song['name']))
                    index += 1
        except KeyError as err:
            msg = (
                "The music list expected key '{}' for item {}, but could not find it."
                .format(err.args[0], item))
            raise ServerError.MusicInvalid(msg)
        except TypeError:
            msg = (
                "The music list expected songs to be listed for item {}, but could not find any."
                .format(item))
            raise ServerError.MusicInvalid(msg)

        self.music_pages_ao1 = [
            self.music_pages_ao1[x:x + 10]
            for x in range(0, len(self.music_pages_ao1), 10)
        ]

    def build_music_list_ao2(self,
                             from_area=None,
                             c=None,
                             music_list=None,
                             include_areas=True,
                             include_music=True):
        built_music_list = list()

        # add areas first, if needed
        if include_areas:
            built_music_list.extend(
                self.prepare_area_list(c=c, from_area=from_area))

        # then add music, if needed
        if include_music:
            built_music_list.extend(
                self.prepare_music_list(c=c, specific_music_list=music_list))

        self._music_list_ao2 = built_music_list  # Backwards compatibility
        return built_music_list

    def prepare_area_list(self, c=None, from_area=None):
        """
        Return the area list of the server. If given c and from_area, it will send an area list
        that matches the perspective of client `c` as if they were in area `from_area`.

        Parameters
        ----------
        c: ClientManager.Client
            Client whose perspective will be taken into account, by default None
        from_area: AreaManager.Area
            Area from which the perspective will be considered, by default None

        Returns
        -------
        list of AreaManager.Area
            Area list that matches intended perspective.
        """

        # Determine whether to filter the areas in the results
        need_to_check = (from_area is None
                         or '<ALL>' in from_area.reachable_areas or
                         (c is not None and (c.is_staff() or c.is_transient)))

        # Now add areas
        prepared_area_list = list()
        for area in self.area_manager.areas:
            if need_to_check or area.name in from_area.reachable_areas:
                prepared_area_list.append("{}-{}".format(area.id, area.name))

        return prepared_area_list

    def prepare_music_list(self, c=None, specific_music_list=None):
        """
        If `specific_music_list` is not None, return a client-ready version of that music list.
        Else, if `c` is a client with a custom chosen music list, return their latest music list.
        Otherwise, return a client-ready version of the server music list.

        Parameters
        ----------
        c: ClientManager.Client
            Client whose current music list if it exists will be considered if `specific_music_list`
            is None
        specific_music_list: list of dictionaries with key sets {'category', 'songs'}
            Music list to use if given

        Returns
        -------
        list of str
            Music list ready to be sent to clients
        """

        # If not provided a specific music list to overwrite
        if specific_music_list is None:
            specific_music_list = self.music_list  # Default value
            # But just in case, check if this came as a request of a client who had a
            # previous music list preference
            if c and c.music_list is not None:
                specific_music_list = c.music_list

        prepared_music_list = list()
        try:
            for item in specific_music_list:
                prepared_music_list.append(item['category'])
                for song in item['songs']:
                    if 'length' not in song:
                        name, length = song['name'], -1
                    else:
                        name, length = song['name'], song['length']

                    # Check that length is a number, and if not, abort.
                    if not isinstance(length, (int, float)):
                        msg = (
                            "The music list expected a numerical length for track '{}', but "
                            "found it had length '{}'.").format(
                                song['name'], song['length'])
                        raise ServerError.MusicInvalidError(msg)

                    prepared_music_list.append(name)

        except KeyError as err:
            msg = (
                "The music list expected key '{}' for item {}, but could not find it."
                .format(err.args[0], item))
            raise ServerError.MusicInvalid(msg)
        except TypeError:
            msg = (
                "The music list expected songs to be listed for item {}, but could not find any."
                .format(item))
            raise ServerError.MusicInvalid(msg)

        return prepared_music_list

    def is_valid_char_id(self, char_id):
        return len(self.char_list) > char_id >= -1

    def get_char_id_by_name(self, name):
        if name == self.config['spectator_name']:
            return -1
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError('Character not found.')

    def get_song_data(self, music, c=None):
        # The client's personal music list should also be a valid place to search
        # so search in there too if possible
        if c and c.music_list:
            valid_music = self.music_list + c.music_list
        else:
            valid_music = self.music_list

        for item in valid_music:
            if item['category'] == music:
                return item['category'], -1
            for song in item['songs']:
                if song['name'] == music:
                    try:
                        return song['name'], song['length']
                    except KeyError:
                        return song['name'], -1
        raise ServerError.MusicNotFoundError('Music not found.')

    def send_all_cmd_pred(self, cmd, *args, pred=lambda x: True):
        for client in self.client_manager.clients:
            if pred(client):
                client.send_command(cmd, *args)

    def make_all_clients_do(self,
                            function,
                            *args,
                            pred=lambda x: True,
                            **kwargs):
        for client in self.client_manager.clients:
            if pred(client):
                getattr(client, function)(*args, **kwargs)

    def send_error_report(self, client, cmd, args, ex):
        """
        In case of an error caused by a client packet, send error report to user, notify moderators
        and have full traceback available on console and through /lasterror
        """

        # Send basic logging information to user
        info = (
            '=========\nThe server ran into a Python issue. Please contact the server owner '
            'and send them the following logging information:')
        etype, evalue, etraceback = sys.exc_info()
        tb = traceback.extract_tb(tb=etraceback)
        current_time = Constants.get_time()
        file, line_num, module, func = tb[-1]
        file = file[file.rfind('\\') + 1:]  # Remove unnecessary directories
        info += '\r\n*Server time: {}'.format(current_time)
        info += '\r\n*Packet details: {} {}'.format(cmd, args)
        info += '\r\n*Client status: {}'.format(client)
        info += '\r\n*Area status: {}'.format(client.area)
        info += '\r\n*File: {}'.format(file)
        info += '\r\n*Line number: {}'.format(line_num)
        info += '\r\n*Module: {}'.format(module)
        info += '\r\n*Function: {}'.format(func)
        info += '\r\n*Error: {}: {}'.format(type(ex).__name__, ex)
        info += '\r\nYour help would be much appreciated.'
        info += '\r\n========='
        client.send_ooc(info)
        client.send_ooc_others(
            'Client {} triggered a Python error through a client packet. '
            'Do /lasterror to take a look at it.'.format(client.id),
            pred=lambda c: c.is_mod)

        # Print complete traceback to console
        info = 'TSUSERVERDR HAS ENCOUNTERED AN ERROR HANDLING A CLIENT PACKET'
        info += '\r\n*Server time: {}'.format(current_time)
        info += '\r\n*Packet details: {} {}'.format(cmd, args)
        info += '\r\n*Client status: {}'.format(client)
        info += '\r\n*Area status: {}'.format(client.area)
        info += '\r\n\r\n{}'.format("".join(
            traceback.format_exception(etype, evalue, etraceback)))
        logger.log_print(info)
        self.last_error = [info, etype, evalue, etraceback]

        # Log error to file
        logger.log_error(info, server=self, errortype='C')

        if self.in_test:
            raise

    def broadcast_global(self,
                         client,
                         msg,
                         as_mod=False,
                         mtype="<dollar>G",
                         condition=lambda x: not x.muted_global):
        username = client.name
        ooc_name = '{}[{}][{}]'.format(mtype, client.area.id, username)
        if as_mod:
            ooc_name += '[M]'
        self.send_all_cmd_pred('CT', ooc_name, msg, pred=condition)
        if self.config['use_district']:
            self.district_client.send_raw_message('GLOBAL#{}#{}#{}#{}'.format(
                int(as_mod), client.area.id, username, msg))

    def broadcast_need(self, client, msg):
        char_name = client.displayname
        area_name = client.area.name
        area_id = client.area.id
        self.send_all_cmd_pred(
            'CT',
            '{}'.format(self.config['hostname']),
            '=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
            format(char_name, area_name, area_id, msg),
            pred=lambda x: not x.muted_adverts)
        if self.config['use_district']:
            self.district_client.send_raw_message('NEED#{}#{}#{}#{}'.format(
                char_name, area_name, area_id, msg))

    """
    OLD CODE.
    KEPT FOR BACKWARDS COMPATIBILITY WITH PRE 4.2 TSUSERVERDR.
    """

    def create_task(self, client, args):
        self.task_deprecation_warning()
        self.tasker.create_task(client, args)

    def cancel_task(self, task):
        """ Cancels current task and sends order to await cancellation """
        self.task_deprecation_warning()
        self.tasker.cancel_task(task)

    def remove_task(self, client, args):
        """ Given client and task name, removes task from server.client_tasks, and cancels it """
        self.task_deprecation_warning()
        self.tasker.remove_task(client, args)

    def get_task(self, client, args):
        """ Returns actual task instance """
        self.task_deprecation_warning()
        return self.tasker.get_task(client, args)

    def get_task_args(self, client, args):
        """ Returns input arguments of task """
        self.task_deprecation_warning()
        return self.tasker.get_task_args(client, args)

    def get_task_attr(self, client, args, attr):
        """ Returns task attribute """
        self.task_deprecation_warning()
        return self.tasker.get_task_attr(client, args, attr)

    def set_task_attr(self, client, args, attr, value):
        """ Sets task attribute """
        self.task_deprecation_warning()
        self.tasker.set_task_attr(client, args, attr, value)

    @property
    def active_timers(self):
        self.task_deprecation_warning()
        return self.tasker.active_timers

    @property
    def client_tasks(self):
        self.task_deprecation_warning()
        return self.tasker.client_tasks

    @active_timers.setter
    def active_timers(self, value):
        self.task_deprecation_warning()
        self.tasker.active_timers = value

    @client_tasks.setter
    def client_tasks(self, value):
        self.task_deprecation_warning()
        self.tasker.client_tasks = value

    @property
    def music_list_ao2(self):
        self.music_list_ao2_deprecation_warning()
        return self._music_list_ao2

    @music_list_ao2.setter
    def music_list_ao2(self, value):
        self.music_list_ao2_deprecation_warning()
        self._music_list_ao2 = value

    def task_deprecation_warning(self):
        message = (
            'Code is using old task syntax (assuming it is a server property/method). '
            'Please change it (or ask your server developer) so that it uses '
            'server.tasker instead (pending removal in 4.3).')
        warnings.warn(message, category=UserWarning, stacklevel=3)

    def music_list_ao2_deprecation_warning(self):
        message = (
            'Code is currently using old music_list_ao2_syntax (assuming it is a server '
            'property/method). Please change it (or ask your server develop) so that it '
            'uses the return value of self.build_music_list_ao2() instead (pending removal '
            'in 4.3).')
        warnings.warn(message, category=UserWarning, stacklevel=3)
Esempio n. 9
0
class TsuServerCC:
    """The main class for tsuserverCC server software."""
    def __init__(self):
        self.config = None
        self.allowed_iniswaps = None
        self.load_config()
        self.load_iniswaps()
        self.client_manager = ClientManager(self)
        self.area_manager = AreaManager(self)
        self.musiclist_manager = MusicListManager(self)
        self.hub_manager = HubManager(self)
        self.software = 'tsuservercc'

        self.release = 1
        self.major_version = 5
        self.minor_version = 0

        self.config = None
        self.allowed_iniswaps = []
        self.char_list = None
        self.char_emotes = None
        self.char_pages_ao1 = None
        self.music_list = None
        self.music_list_ao2 = None
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.zalgo_tolerance = None
        self.ipRange_bans = []
        self.geoIpReader = None
        self.useGeoIp = False
        self.webperms = []

        try:
            self.geoIpReader = geoip2.database.Reader(
                './storage/GeoLite2-ASN.mmdb')
            self.useGeoIp = True
            # on debian systems you can use /usr/share/GeoIP/GeoIPASNum.dat if the geoip-database-extra package is installed
        except FileNotFoundError:
            self.useGeoIp = False
            pass

        self.is_poll = False
        self.poll = ''
        self.pollyay = []
        self.pollnay = []
        self.parties = []
        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        self.runner = False
        self.runtime = 0

        try:
            self.load_config()
            self.load_characters()
            self.load_music()
            self.load_backgrounds()
            self.load_ipranges()
            self.load_webperms()
            self.load_gimps()
        except yaml.YAMLError as exc:
            print('There was a syntax error parsing a configuration file:')
            print(exc)
            print('Please revise your syntax and restart the server.')
            # Truly idiotproof
            if os.name == 'nt':
                input('(Press Enter to exit)')
            sys.exit(1)

        server.logger.setup_logger(debug=self.config['debug'])

    def start(self):
        """Start the server."""
        loop = asyncio.get_event_loop()
        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'

        ao_server_crt = loop.create_server(lambda: AOProtocol(self), bound_ip,
                                           self.config['port'])
        ao_server = loop.run_until_complete(ao_server_crt)

        if self.config['use_websockets']:
            ao_server_ws = websockets.serve(new_websocket_client(self),
                                            bound_ip,
                                            self.config['websocket_port'])
            asyncio.ensure_future(ao_server_ws)

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            asyncio.ensure_future(self.ms_client.connect(), loop=loop)

        if self.config['zalgo_tolerance']:
            self.zalgo_tolerance = self.config['zalgo_tolerance']

        asyncio.ensure_future(self.schedule_unbans())

        database.log_misc('start')
        print('Server started and is listening on port {}'.format(
            self.config['port']))

        try:
            loop.run_forever()
        except KeyboardInterrupt:
            pass

        database.log_misc('stop')

        ao_server.close()
        loop.run_until_complete(ao_server.wait_closed())
        loop.close()

    def get_version_string(self):
        return str(self.release) + '.' + str(self.major_version) + '.' + str(
            self.minor_version)

    async def schedule_unbans(self):
        while True:
            database.schedule_unbans()
            await asyncio.sleep(3600 * 12)

    @property
    def version(self):
        """Get the server's current version."""
        return f'{self.release}.{self.major_version}.{self.minor_version}'

    def get_version_string(self):
        return str(self.release) + '.' + str(self.major_version) + '.' + str(
            self.minor_version)

    """redundant so I don't break anything"""

    def new_client(self, transport):
        """
		Create a new client based on a raw transport by passing
		it to the client manager.
		:param transport: asyncio transport
		:returns: created client object
		"""
        peername = transport.get_extra_info('peername')[0]

        if self.useGeoIp:
            try:
                geoIpResponse = self.geoIpReader.asn(peername)
                asn = str(geoIpResponse.autonomous_system_number)
            except geoip2.errors.AddressNotFoundError:
                asn = "Loopback"
                pass
        else:
            asn = "Loopback"

        for line, rangeBan in enumerate(self.ipRange_bans):
            if rangeBan != "" and peername.startswith(
                    rangeBan) or asn == rangeBan:
                msg = 'BD#'
                msg += 'Abuse\r\n'
                msg += f'ID: {line}\r\n'
                msg += 'Until: N/A'
                msg += '#%'

                transport.write(msg.encode('utf-8'))
                raise ClientError

        c = self.client_manager.new_client(transport)
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client):
        """
		Remove a disconnected client.
		:param client: client object

		"""
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    @property
    def player_count(self):
        """Get the number of non-spectating clients."""
        cnt = len(self.client_manager.clients)
        return cnt

    def load_config(self):
        """Load the main server configuration from a YAML file."""
        with open('config/config.yaml', 'r', encoding='utf-8') as cfg:
            self.config = yaml.safe_load(cfg)
            self.config['motd'] = self.config['motd'].replace('\\n', ' \n')
        if 'music_change_floodguard' not in self.config:
            self.config['music_change_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        if 'wtce_floodguard' not in self.config:
            self.config['wtce_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }

        if 'zalgo_tolerance' not in self.config:
            self.config['zalgo_tolerance'] = 3

        if 'webhooks_enabled' not in self.config:
            self.config['webhooks_enabled'] = False

        if 'webhook_url' not in self.config:
            self.config['webhook_url'] = "example.com"

        if 'webhook2_url' not in self.config:
            self.config['webhook2_url'] = None

        if 'ooc_delay' not in self.config:
            self.config['ooc_delay'] = 0

        if 'afk_delay' not in self.config:
            self.config['afk_delay'] = 0

        #if isinstance(self.config['modpass'], str):
        #	self.config['modpass'] = {'default': {'password': self.config['modpass']}}

    def load_characters(self):
        """Load the character list from a YAML file."""
        with open('config/characters.yaml', 'r', encoding='utf-8') as chars:
            self.char_list = yaml.safe_load(chars)
        self.build_char_pages_ao1()
        self.char_emotes = {char: Emotes(char) for char in self.char_list}

    def load_music(self):
        """Load the music list from a YAML file."""
        with open('config/music.yaml', 'r', encoding='utf-8') as music:
            self.music_list = yaml.safe_load(music)
        self.build_music_pages_ao1()
        self.build_music_list_ao2()

    def load_gimps(self):
        with open('config/gimp.yaml', 'r', encoding='utf-8') as cfg:
            self.gimp_list = yaml.safe_load(cfg)

    def load_webperms(self):
        permfile = 'config/webaoperms.yaml'
        new = not os.path.exists(permfile)
        if not new:
            with open('config/webaoperms.yaml', 'r', encoding='utf-8') as cfg:
                self.webperms = yaml.safe_load(cfg)

    def load_backgrounds(self):
        """Load the backgrounds list from a YAML file."""
        with open('config/backgrounds.yaml', 'r', encoding='utf-8') as bgs:
            self.backgrounds = yaml.safe_load(bgs)

    def load_iniswaps(self):
        """Load a list of characters for which INI swapping is allowed."""
        try:
            with open('config/iniswaps.yaml', 'r',
                      encoding='utf-8') as iniswaps:
                self.allowed_iniswaps = yaml.safe_load(iniswaps)
        except:
            logger.debug('Cannot find iniswaps.yaml')

    def load_ipranges(self):
        """Load a list of banned IP ranges."""
        try:
            with open('config/iprange_ban.txt', 'r',
                      encoding='utf-8') as ipranges:
                self.ipRange_bans = ipranges.read().splitlines()
        except:
            logger.debug('Cannot find iprange_ban.txt')

    def build_char_pages_ao1(self):
        """
		Cache a list of characters that can be used for the
		AO1 connection handshake.
		"""
        self.char_pages_ao1 = [
            self.char_list[x:x + 10] for x in range(0, len(self.char_list), 10)
        ]
        for i in range(len(self.char_list)):
            self.char_pages_ao1[i // 10][i % 10] = '{}#{}&&0&&&0&'.format(
                i, self.char_list[i])

    def build_music_pages_ao1(self):
        """
		Cache a list of tracks that can be used for the
		AO1 connection handshake.
		"""
        self.music_pages_ao1 = []
        index = 0
        # add areas first
        for area in self.area_manager.areas:
            self.music_pages_ao1.append(f'{index}#{area.name}')
            index += 1
        # then add music
        for item in self.music_list:
            self.music_pages_ao1.append('{}#{}'.format(index,
                                                       item['category']))
            index += 1
            for song in item['songs']:
                self.music_pages_ao1.append('{}#{}'.format(
                    index, song['name']))
                index += 1
        self.music_pages_ao1 = [
            self.music_pages_ao1[x:x + 10]
            for x in range(0, len(self.music_pages_ao1), 10)
        ]

    def build_music_list_ao2(self):
        """
		Cache a list of tracks that can be used for the
		AO2 connection handshake.
		"""
        self.music_list_ao2 = []
        # add areas first
        for area in self.area_manager.areas:
            self.music_list_ao2.append(area.name)
            # then add music
        #self.music_list_ao2.append("===MUSIC START===.mp3") #>lol lets just have the music and area lists be the same f*****g thing, the mp3 is there for older clients who aren't looking for this to determine the start of the music list
        for item in self.music_list:
            self.music_list_ao2.append(item['category'])
            try:
                if not item['mod'] == 1:
                    for song in item['songs']:
                        if not song['mod'] == 1:
                            self.music_list_ao2.append(song['name'])
            except KeyError:
                for song in item['songs']:
                    try:
                        if not song['mod'] == 1:
                            self.music_list_ao2.append(song['name'])
                    except KeyError:
                        self.music_list_ao2.append(song['name'])

    def is_valid_char_id(self, char_id):
        """
		Check if a character ID is a valid one.
		:param char_id: character ID
		:returns: True if within length of character list; False otherwise

		"""
        return len(self.char_list) > char_id >= 0

    def get_char_id_by_name(self, name):
        """
		Get a character ID by the name of the character.
		:param name: name of character
		:returns: Character ID

		"""
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError('Character not found.')

    def get_song_data(self, music, area):
        """
		Get information about a track, if exists.
		:param music: track name
		:returns: tuple (name, length or -1)
		:raises: ServerError if track not found
		"""
        for item in self.music_list:
            if item['category'] == music:
                return '~stop.mp3', 0, -1, False
            for song in item['songs']:
                if song['name'] == music:
                    try:
                        return song['name'], song['length'], song['mod']
                    except KeyError:
                        try:
                            return song['name'], song['length'], -1, False
                        except KeyError:
                            return song['name'], 0, -1, False
        if len(area.cmusic_list) != 0:
            for item in area.cmusic_list:
                if item['category'] == music:
                    return '~stop.mp3', 0, -1, True
                if len(item['songs']) != 0:
                    for song in item['songs']:
                        if song['name'] == music:
                            try:
                                return song['name'], song['length'], song[
                                    'mod'], True
                            except KeyError:
                                return song['name'], song['length'], -1, True
        raise ServerError('Music not found.')

    def send_all_cmd_pred(self, cmd, *args, pred=lambda x: True):
        """
		Broadcast an AO-compatible command to all clients that satisfy
		a predicate.
		"""
        for client in self.client_manager.clients:
            if pred(client):
                client.send_command(cmd, *args)

    def broadcast_global(self, client, msg, as_mod=False):
        """
		Broadcast an OOC message to all clients that do not have
		global chat muted.
		:param client: sender
		:param msg: message
		:param as_mod: add moderator prefix (Default value = False)

		"""
        char_name = client.char_name
        ooc_name = '{}[{}][{}]'.format('<dollar>G', client.area.abbreviation,
                                       char_name)
        if as_mod:
            ooc_name += '[MOD]'
        self.send_all_cmd_pred('CT',
                               ooc_name,
                               msg,
                               pred=lambda x: not x.muted_global)

    def send_modchat(self, client, msg):
        """
		Send an OOC message to all mods.
		:param client: sender
		:param msg: message

		"""
        name = client.name
        ooc_name = '{}[{}][{}]'.format('<dollar>M', client.area.abbreviation,
                                       name)
        self.send_all_cmd_pred('CT', ooc_name, msg, pred=lambda x: x.is_mod)

    def send_partychat(self, client, msg):
        """
		Send an OOC message to all mods.
		:param client: sender
		:param msg: message

		"""
        name = client.name
        ooc_name = '{}[{}]'.format(f'<dollar>[{client.party.name}]', name)
        self.send_all_cmd_pred(
            'CT',
            ooc_name,
            msg,
            pred=lambda x: x.is_mod or x.party == client.party)

    def broadcast_need(self, client, msg):
        """
		Broadcast an OOC "need" message to all clients who do not
		have advertisements muted.
		:param client: sender
		:param msg: message

		"""
        char_name = client.char_name
        area_name = client.area.name
        area_id = client.area.abbreviation
        self.send_all_cmd_pred(
            'CT',
            '{}'.format(self.config['hostname']),
            '=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
            format(char_name, area_name, area_id, msg),
            '1',
            pred=lambda x: not x.muted_adverts)

    def send_arup(self, args):
        """Update the area properties for 2.6 clients.
		
		Playercount:
			ARUP#0#<area1_p: int>#<area2_p: int>#...
		Status:
			ARUP#1##<area1_s: string>##<area2_s: string>#...
		CM:
			ARUP#2##<area1_cm: string>##<area2_cm: string>#...
		Lockedness:
			ARUP#3##<area1_l: string>##<area2_l: string>#...


		:param args: 

		"""
        if len(args) < 2:
            # An argument count smaller than 2 means we only got the identifier of ARUP.
            return
        if args[0] not in (0, 1, 2, 3):
            return

        if args[0] == 0:
            for part_arg in args[1:]:
                try:
                    _sanitised = int(part_arg)
                except:
                    return
        elif args[0] in (1, 2, 3, 4):
            for part_arg in args[1:]:
                try:
                    _sanitised = str(part_arg)
                except:
                    return
        self.send_all_cmd_pred(
            'ARUP', *args, pred=lambda x: not x.area.is_hub and not x.area.sub)

    def send_hub_arup(self, args, mainhub):
        """Update the area properties for 2.6 clients.
		
		Playercount:
			ARUP#0#<area1_p: int>#<area2_p: int>#...
		Status:
			ARUP#1##<area1_s: string>##<area2_s: string>#...
		CM:
			ARUP#2##<area1_cm: string>##<area2_cm: string>#...
		Lockedness:
			ARUP#3##<area1_l: string>##<area2_l: string>#...


		:param args: 

		"""
        if len(args) < 2:
            # An argument count smaller than 2 means we only got the identifier of ARUP.
            return
        if args[0] not in (0, 1, 2, 3):
            return

        if args[0] == 0:
            for part_arg in args[1:]:
                try:
                    _sanitised = int(part_arg)
                except:
                    return
        elif args[0] in (1, 2, 3, 4):
            for part_arg in args[1:]:
                try:
                    _sanitised = str(part_arg)
                except:
                    return
        self.send_all_cmd_pred(
            'ARUP',
            *args,
            pred=lambda x: x.area == mainhub or x.area.hub == mainhub and not x
            .area.is_restricted or x.area.hub == mainhub and x.area.
            is_restricted and x in x.area.hub.owners)

    def send_conn_arup(self, args, area):
        """Update the area properties for 2.6 clients.
		
		Playercount:
			ARUP#0#<area1_p: int>#<area2_p: int>#...
		Status:
			ARUP#1##<area1_s: string>##<area2_s: string>#...
		CM:
			ARUP#2##<area1_cm: string>##<area2_cm: string>#...
		Lockedness:
			ARUP#3##<area1_l: string>##<area2_l: string>#...


		:param args: 

		"""
        if len(args) < 2:
            # An argument count smaller than 2 means we only got the identifier of ARUP.
            return
        if args[0] not in (0, 1, 2, 3):
            return

        if args[0] == 0:
            for part_arg in args[1:]:
                try:
                    _sanitised = int(part_arg)
                except:
                    return
        elif args[0] in (1, 2, 3, 4):
            for part_arg in args[1:]:
                try:
                    _sanitised = str(part_arg)
                except:
                    return
        self.send_all_cmd_pred(
            'ARUP',
            *args,
            pred=lambda x: x.area == area and not x in x.area.hub.owners)

    def refresh(self):
        """
		Refresh as many parts of the server as possible:
		 - MOTD
		 - Mod credentials (unmodding users if necessary)
		 - Characters
		 - Music
		 - Backgrounds
		 - Commands
		 - Banlists
		"""
        with open('config/config.yaml', 'r') as cfg:
            cfg_yaml = yaml.safe_load(cfg)
            self.config['motd'] = cfg_yaml['motd'].replace('\\n', ' \n')

            # Reload moderator passwords list and unmod any moderator affected by
            # credential changes or removals
        modfile = 'config/moderation.yaml'
        new = not os.path.exists(modfile)
        if not new:
            with open(modfile, 'r') as chars:
                mods = yaml.safe_load(chars)
            for client in self.client_manager.clients:
                if client.is_mod:
                    check = False
                    for item in mods:
                        if item['name'] == client.mod_profile_name:
                            check = True
                    if check == False:
                        client.is_mod = False
                        client.mod_profile_name = None
                        database.log_misc('unmod.modpass', client)
                        client.send_ooc(
                            'Your moderator credentials have been revoked.')
                        client.send_command('AUTH', '-1')
            if isinstance(self.config['modpass'], str):
                self.config['modpass'] = {
                    'default': {
                        'password': self.config['modpass']
                    }
                }
            if isinstance(cfg_yaml['modpass'], str):
                cfg_yaml['modpass'] = {
                    'default': {
                        'password': cfg_yaml['modpass']
                    }
                }

            for profile in self.config['modpass']:
                if profile not in cfg_yaml['modpass'] or \
                   self.config['modpass'][profile] != cfg_yaml['modpass'][profile]:
                    for client in filter(
                            lambda c: c.mod_profile_name == profile,
                            self.client_manager.clients):
                        client.is_mod = False
                        client.mod_profile_name = None
                        database.log_misc('unmod.modpass', client)
                        client.send_ooc(
                            'Your moderator credentials have been revoked.')
                        client.send_command('AUTH', '-1')
            self.config['modpass'] = cfg_yaml['modpass']

        self.load_characters()
        self.load_iniswaps()
        self.load_music()
        self.load_backgrounds()
        self.load_ipranges()

        import server.commands
        importlib.reload(server.commands)
        server.commands.reload()

    def load_data(self):
        with open('config/data.yaml', 'r') as data:
            self.data = yaml.load(data)

    def save_data(self):
        with open('config/data.yaml', 'w') as data:
            json.dump(self.data, data)
Esempio n. 10
0
class TsuServer3:
    def __init__(self):
        self.release = 3
        self.major_version = 'DR'
        self.minor_version = '190622b'
        self.software = 'tsuserver{}'.format(self.get_version_string())
        self.version = 'tsuserver{}dev'.format(self.get_version_string())

        logger.log_print('Launching {}...'.format(self.software))

        logger.log_print('Loading server configurations...')
        self.config = None
        self.global_connection = None
        self.shutting_down = False
        self.loop = None

        self.allowed_iniswaps = None
        self.default_area = 0
        self.load_config()
        self.load_iniswaps()
        self.char_list = list()
        self.load_characters()
        self.client_manager = ClientManager(self)
        self.area_manager = AreaManager(self)
        self.ban_manager = BanManager(self)

        self.ipid_list = {}
        self.hdid_list = {}
        self.char_pages_ao1 = None
        self.music_list = None
        self.music_list_ao2 = None
        self.music_pages_ao1 = None
        self.backgrounds = None
        self.load_music()
        self.load_backgrounds()
        self.load_ids()
        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        self.user_auth_req = False
        self.client_tasks = dict()
        self.active_timers = dict()
        self.showname_freeze = False
        self.commands = importlib.import_module('server.commands')
        logger.setup_logger(debug=self.config['debug'])

    def start(self):
        self.loop = asyncio.get_event_loop()

        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'
            logger.log_print(
                'Starting a local server. Ignore outbound connection attempts.'
            )

        ao_server_crt = self.loop.create_server(lambda: AOProtocol(self),
                                                bound_ip, self.config['port'])
        ao_server = self.loop.run_until_complete(ao_server_crt)

        logger.log_pdebug('Server started successfully!\n')

        if self.config['use_district']:
            self.district_client = DistrictClient(self)
            self.global_connection = asyncio.ensure_future(
                self.district_client.connect(), loop=self.loop)
            logger.log_print(
                'Attempting to connect to district at {}:{}.'.format(
                    self.config['district_ip'], self.config['district_port']))

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            self.global_connection = asyncio.ensure_future(
                self.ms_client.connect(), loop=self.loop)
            logger.log_print(
                'Attempting to connect to the master server at {}:{} with the following details:'
                .format(self.config['masterserver_ip'],
                        self.config['masterserver_port']))
            logger.log_print('*Server name: {}'.format(
                self.config['masterserver_name']))
            logger.log_print('*Server description: {}'.format(
                self.config['masterserver_description']))

        try:
            self.loop.run_forever()
        except KeyboardInterrupt:
            pass

        print('')  # Lame
        logger.log_pdebug('You have initiated a server shut down.')
        self.shutdown()

        ao_server.close()
        self.loop.run_until_complete(ao_server.wait_closed())
        self.loop.close()
        logger.log_print('Server has successfully shut down.')

    def shutdown(self):
        # Cleanup operations
        self.shutting_down = True

        # Cancel further polling for district/master server
        if self.global_connection:
            self.global_connection.cancel()
            self.loop.run_until_complete(
                self.await_cancellation(self.global_connection))

        # Cancel pending client tasks and cleanly remove them from the areas
        logger.log_print('Kicking {} remaining clients.'.format(
            self.get_player_count()))

        for area in self.area_manager.areas:
            while area.clients:
                client = next(iter(area.clients))
                area.remove_client(client)
                for task_id in self.client_tasks[client.id].keys():
                    task = self.get_task(client, [task_id])
                    self.loop.run_until_complete(self.await_cancellation(task))

    def get_version_string(self):
        return str(self.release) + '.' + str(self.major_version) + '.' + str(
            self.minor_version)

    def reload(self):
        with open('config/characters.yaml', 'r') as chars:
            self.char_list = yaml.safe_load(chars)
        with open('config/music.yaml', 'r') as music:
            self.music_list = yaml.safe_load(music)
        self.build_music_pages_ao1()
        self.build_music_list_ao2()
        with open('config/backgrounds.yaml', 'r') as bgs:
            self.backgrounds = yaml.safe_load(bgs)

    def reload_commands(self):
        try:
            self.commands = importlib.reload(self.commands)
        except Exception as error:
            return error

    def new_client(self, transport):
        c = self.client_manager.new_client(transport)
        if self.rp_mode:
            c.in_rp = True
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client):
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    def get_player_count(self):
        # Ignore players in the server selection screen.
        return len([
            client for client in self.client_manager.clients
            if client.char_id is not None
        ])

    def load_config(self):
        with open('config/config.yaml', 'r', encoding='utf-8') as cfg:
            self.config = yaml.safe_load(cfg)
            self.config['motd'] = self.config['motd'].replace('\\n', ' \n')
        if 'music_change_floodguard' not in self.config:
            self.config['music_change_floodguard'] = {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        # Backwards compatibility checks
        if 'spectator_name' not in self.config:
            self.config['spectator_name'] = 'SPECTATOR'
        if 'showname_max_length' not in self.config:
            self.config['showname_max_length'] = 30
        if 'sneak_handicap' not in self.config:
            self.config['sneak_handicap'] = 5  # Seconds
        if 'blackout_background' not in self.config:
            self.config['blackout_background'] = 'Blackout_HD'
        if 'discord_link' not in self.config:
            self.config['discord_link'] = 'None'
        if 'default_area_description' not in self.config:
            self.config['default_area_description'] = 'No description.'

        # Check for uniqueness of all passwords
        passwords = [
            'guardpass', 'modpass', 'cmpass', 'gmpass', 'gmpass1', 'gmpass2',
            'gmpass3', 'gmpass4', 'gmpass5', 'gmpass6', 'gmpass7'
        ]

        for (i, password1) in enumerate(passwords):
            for (j, password2) in enumerate(passwords):
                if i != j and self.config[password1] == self.config[password2]:
                    info = (
                        'Passwords "{}" and "{}" in server/config.yaml match. '
                        'Please change them so they are different.'.format(
                            password1, password2))
                    raise ServerError(info)

    def load_characters(self):
        with open('config/characters.yaml', 'r', encoding='utf-8') as chars:
            self.char_list = yaml.safe_load(chars)
        self.build_char_pages_ao1()

    def load_music(self,
                   music_list_file='config/music.yaml',
                   server_music_list=True):
        try:
            with open(music_list_file, 'r', encoding='utf-8') as music:
                music_list = yaml.safe_load(music)
        except FileNotFoundError:
            raise ServerError(
                'Could not find music list file {}'.format(music_list_file))

        if server_music_list:
            self.music_list = music_list
            self.build_music_pages_ao1()
            self.build_music_list_ao2(music_list=music_list)

        return music_list

    def load_ids(self):
        self.ipid_list = {}
        self.hdid_list = {}
        #load ipids
        try:
            with open('storage/ip_ids.json', 'r',
                      encoding='utf-8') as whole_list:
                self.ipid_list = json.loads(whole_list.read())
        except:
            logger.log_debug(
                'Failed to load ip_ids.json from ./storage. If ip_ids.json exists, then remove it.'
            )
        #load hdids
        try:
            with open('storage/hd_ids.json', 'r',
                      encoding='utf-8') as whole_list:
                self.hdid_list = json.loads(whole_list.read())
        except:
            logger.log_debug(
                'Failed to load hd_ids.json from ./storage. If hd_ids.json exists, then remove it.'
            )

    def dump_ipids(self):
        with open('storage/ip_ids.json', 'w') as whole_list:
            json.dump(self.ipid_list, whole_list)

    def dump_hdids(self):
        with open('storage/hd_ids.json', 'w') as whole_list:
            json.dump(self.hdid_list, whole_list)

    def get_ipid(self, ip):
        if not ip in self.ipid_list:
            while True:
                ipid = random.randint(0, 10**10 - 1)
                if ipid not in self.ipid_list:
                    break
            self.ipid_list[ip] = ipid
            self.dump_ipids()
        return self.ipid_list[ip]

    def load_backgrounds(self):
        with open('config/backgrounds.yaml', 'r', encoding='utf-8') as bgs:
            self.backgrounds = yaml.safe_load(bgs)

    def load_iniswaps(self):
        try:
            with open('config/iniswaps.yaml', 'r',
                      encoding='utf-8') as iniswaps:
                self.allowed_iniswaps = yaml.safe_load(iniswaps)
        except:
            logger.log_debug('cannot find iniswaps.yaml')

    def build_char_pages_ao1(self):
        self.char_pages_ao1 = [
            self.char_list[x:x + 10] for x in range(0, len(self.char_list), 10)
        ]
        for i in range(len(self.char_list)):
            self.char_pages_ao1[i // 10][i % 10] = '{}#{}&&0&&&0&'.format(
                i, self.char_list[i])

    def build_music_pages_ao1(self):
        self.music_pages_ao1 = []
        index = 0
        # add areas first
        for area in self.area_manager.areas:
            self.music_pages_ao1.append('{}#{}'.format(index, area.name))
            index += 1
        # then add music
        for item in self.music_list:
            self.music_pages_ao1.append('{}#{}'.format(index,
                                                       item['category']))
            index += 1
            for song in item['songs']:
                self.music_pages_ao1.append('{}#{}'.format(
                    index, song['name']))
                index += 1
        self.music_pages_ao1 = [
            self.music_pages_ao1[x:x + 10]
            for x in range(0, len(self.music_pages_ao1), 10)
        ]

    def build_music_list_ao2(self, from_area=None, c=None, music_list=None):
        # If not provided a specific music list to overwrite
        if music_list is None:
            music_list = self.music_list  # Default value
            # But just in case, check if this came as a request of a client who had a
            # previous music list preference
            if c and c.music_list is not None:
                music_list = c.music_list

        self.music_list_ao2 = []
        # Determine whether to filter the music list or not
        need_to_check = (from_area is None
                         or '<ALL>' in from_area.reachable_areas or
                         (c is not None and (c.is_staff() or c.is_transient)))

        # add areas first
        for area in self.area_manager.areas:
            if need_to_check or area.name in from_area.reachable_areas:
                self.music_list_ao2.append("{}-{}".format(area.id, area.name))

        # then add music
        for item in music_list:
            self.music_list_ao2.append(item['category'])
            for song in item['songs']:
                self.music_list_ao2.append(song['name'])

    def is_valid_char_id(self, char_id):
        return len(self.char_list) > char_id >= -1

    def get_char_id_by_name(self, name):
        if name == self.config['spectator_name']:
            return -1
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError('Character not found.')

    def get_song_data(self, music, c=None):
        # The client's personal music list should also be a valid place to search
        # so search in there too if possible
        if c and c.music_list:
            valid_music = self.music_list + c.music_list
        else:
            valid_music = self.music_list

        for item in valid_music:
            if item['category'] == music:
                return item['category'], -1
            for song in item['songs']:
                if song['name'] == music:
                    try:
                        return song['name'], song['length']
                    except KeyError:
                        return song['name'], -1
        raise ServerError('Music not found.')

    def send_all_cmd_pred(self, cmd, *args, pred=lambda x: True):
        for client in self.client_manager.clients:
            if pred(client):
                client.send_command(cmd, *args)

    def broadcast_global(self,
                         client,
                         msg,
                         as_mod=False,
                         mtype="<dollar>G",
                         condition=lambda x: not x.muted_global):
        username = client.name
        ooc_name = '{}[{}][{}]'.format(mtype, client.area.id, username)
        if as_mod:
            ooc_name += '[M]'
        self.send_all_cmd_pred('CT', ooc_name, msg, pred=condition)
        if self.config['use_district']:
            self.district_client.send_raw_message('GLOBAL#{}#{}#{}#{}'.format(
                int(as_mod), client.area.id, username, msg))

    def broadcast_need(self, client, msg):
        char_name = client.get_char_name()
        area_name = client.area.name
        area_id = client.area.id
        self.send_all_cmd_pred(
            'CT',
            '{}'.format(self.config['hostname']),
            '=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
            format(char_name, area_name, area_id, msg),
            pred=lambda x: not x.muted_adverts)
        if self.config['use_district']:
            self.district_client.send_raw_message('NEED#{}#{}#{}#{}'.format(
                char_name, area_name, area_id, msg))

    def create_task(self, client, args):
        # Abort old task if it exists
        try:
            old_task = self.get_task(client, args)
            if not old_task.done() and not old_task.cancelled():
                self.cancel_task(old_task)
        except KeyError:
            pass

        # Start new task
        self.client_tasks[client.id][args[0]] = (asyncio.ensure_future(
            getattr(self, args[0])(client,
                                   args[1:]), loop=self.loop), args[1:])

    def cancel_task(self, task):
        """ Cancels current task and sends order to await cancellation """
        task.cancel()
        asyncio.ensure_future(self.await_cancellation(task))

    def remove_task(self, client, args):
        """ Given client and task name, removes task from server.client_tasks, and cancels it """
        task = self.client_tasks[client.id].pop(args[0])
        self.cancel_task(task[0])

    def get_task(self, client, args):
        """ Returns actual task instance """
        return self.client_tasks[client.id][args[0]][0]

    def get_task_args(self, client, args):
        """ Returns input arguments of task """
        return self.client_tasks[client.id][args[0]][1]

    async def await_cancellation(self, old_task):
        # Wait until it is able to properly retrieve the cancellation exception
        try:
            await old_task
        except asyncio.CancelledError:
            pass

    async def as_afk_kick(self, client, args):
        afk_delay, afk_sendto = args
        try:
            delay = int(
                afk_delay
            ) * 60  # afk_delay is in minutes, so convert to seconds
        except (TypeError, ValueError):
            raise ServerError(
                'The area file contains an invalid AFK kick delay for area {}: {}'
                .format(client.area.id, afk_delay))

        if delay <= 0:  # Assumes 0-minute delay means that AFK kicking is disabled
            return

        try:
            await asyncio.sleep(delay)
        except asyncio.CancelledError:
            raise
        else:
            try:
                area = client.server.area_manager.get_area_by_id(
                    int(afk_sendto))
            except:
                raise ServerError(
                    'The area file contains an invalid AFK kick destination area for area {}: {}'
                    .format(client.area.id, afk_sendto))

            if client.area.id == afk_sendto:  # Don't try and kick back to same area
                return
            if client.char_id < 0:  # Assumes spectators are exempted from AFK kicks
                return
            if client.is_staff():  # Assumes staff are exempted from AFK kicks
                return

            try:
                original_area = client.area
                client.change_area(area,
                                   override_passages=True,
                                   override_effects=True,
                                   ignore_bleeding=True)
            except:
                pass  # Server raised an error trying to perform the AFK kick, ignore AFK kick
            else:
                client.send_host_message(
                    "You were kicked from area {} to area {} for being inactive for {} minutes."
                    .format(original_area.id, afk_sendto, afk_delay))

                if client.area.is_locked or client.area.is_modlocked:
                    client.area.invite_list.pop(client.ipid)

    async def as_timer(self, client, args):
        _, length, name, is_public = args  # Length in seconds, already converted
        client_name = client.name  # Failsafe in case client disconnects before task is cancelled/expires

        try:
            await asyncio.sleep(length)
        except asyncio.CancelledError:
            self.send_all_cmd_pred(
                'CT',
                '{}'.format(self.config['hostname']),
                'Timer "{}" initiated by {} has been canceled.'.format(
                    name, client_name),
                pred=lambda c: (c == client or c.is_staff() or
                                (is_public and c.area == client.area)))
        else:
            self.send_all_cmd_pred(
                'CT',
                '{}'.format(self.config['hostname']),
                'Timer "{}" initiated by {} has expired.'.format(
                    name, client_name),
                pred=lambda c: (c == client or c.is_staff() or
                                (is_public and c.area == client.area)))
        finally:
            del self.active_timers[name]

    async def as_handicap(self, client, args):
        _, length, _, announce_if_over = args
        client.is_movement_handicapped = True

        try:
            await asyncio.sleep(length)
        except asyncio.CancelledError:
            pass  # Cancellation messages via send_host_messages must be sent manually
        else:
            if announce_if_over and not client.is_staff():
                client.send_host_message(
                    'Your movement handicap has expired. You may now move to a new area.'
                )
        finally:
            client.is_movement_handicapped = False

    def timer_remaining(self, start, length):
        current = time.time()
        remaining = start + length - current
        if remaining < 10:
            remain_text = "{} seconds".format('{0:.1f}'.format(remaining))
        elif remaining < 60:
            remain_text = "{} seconds".format(int(remaining))
        elif remaining < 3600:
            remain_text = "{}:{}".format(int(remaining // 60),
                                         '{0:02d}'.format(int(remaining % 60)))
        else:
            remain_text = "{}:{}:{}".format(
                int(remaining // 3600),
                '{0:02d}'.format(int((remaining % 3600) // 60)),
                '{0:02d}'.format(int(remaining % 60)))
        return remaining, remain_text
Esempio n. 11
0
    def __init__(self,
                 protocol: AOProtocol = None,
                 client_manager: ClientManager = None,
                 in_test: bool = False):
        self.logged_packet_limit = 100  # Arbitrary
        self.logged_packets = []
        self.print_packets = False  # For debugging purposes
        self._server = None  # Internal server object, changed to proper object later

        self.release = 4
        self.major_version = 3
        self.minor_version = 0
        self.segment_version = 'post5'
        self.internal_version = '220304a'
        version_string = self.get_version_string()
        self.software = 'TsuserverDR {}'.format(version_string)
        self.version = 'TsuserverDR {} ({})'.format(version_string,
                                                    self.internal_version)
        self.in_test = in_test

        self.protocol = AOProtocol if protocol is None else protocol
        client_manager = ClientManager if client_manager is None else client_manager
        logger.log_print = logger.log_print2 if self.in_test else logger.log_print
        logger.log_server = logger.log_server2 if self.in_test else logger.log_server
        self.random = importlib.reload(random)

        logger.log_print('Launching {}...'.format(self.version))
        logger.log_print('Loading server configurations...')

        self.config = None
        self.local_connection = None
        self.district_connection = None
        self.masterserver_connection = None
        self.shutting_down = False
        self.loop = None
        self.last_error = None
        self.allowed_iniswaps = None
        self.area_list = None
        self.old_area_list = None
        self.default_area = 0
        self.all_passwords = list()
        self.global_allowed = True
        self.server_select_name = 'SERVER_SELECT'

        self.load_config()
        self.client_manager: ClientManager = client_manager(self)
        self.char_list = list()
        self.load_iniswaps()
        self.load_characters()

        self.game_manager = GameManager(self)
        self.trial_manager = TrialManager(self)
        self.zone_manager = ZoneManager(self)
        self.area_manager = AreaManager(self)
        self.ban_manager = BanManager(self)
        self.party_manager = PartyManager(self)

        self.ipid_list = {}
        self.hdid_list = {}
        self.music_list = None
        self.backgrounds = None
        self.gimp_list = list()
        self.load_commandhelp()
        self.load_music()
        self.load_backgrounds()
        self.load_ids()
        self.load_gimp()

        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        self.user_auth_req = False
        self.showname_freeze = False
        self.commands = importlib.import_module('server.commands')
        self.commands_alt = importlib.import_module('server.commands_alt')
        self.logger_handlers = logger.setup_logger(debug=self.config['debug'])

        logger.log_print('Server configurations loaded successfully!')

        self.error_queue = None
        self._server = None
Esempio n. 12
0
class TsuserverDR:
    def __init__(self,
                 protocol: AOProtocol = None,
                 client_manager: ClientManager = None,
                 in_test: bool = False):
        self.logged_packet_limit = 100  # Arbitrary
        self.logged_packets = []
        self.print_packets = False  # For debugging purposes
        self._server = None  # Internal server object, changed to proper object later

        self.release = 4
        self.major_version = 3
        self.minor_version = 0
        self.segment_version = 'post5'
        self.internal_version = '220304a'
        version_string = self.get_version_string()
        self.software = 'TsuserverDR {}'.format(version_string)
        self.version = 'TsuserverDR {} ({})'.format(version_string,
                                                    self.internal_version)
        self.in_test = in_test

        self.protocol = AOProtocol if protocol is None else protocol
        client_manager = ClientManager if client_manager is None else client_manager
        logger.log_print = logger.log_print2 if self.in_test else logger.log_print
        logger.log_server = logger.log_server2 if self.in_test else logger.log_server
        self.random = importlib.reload(random)

        logger.log_print('Launching {}...'.format(self.version))
        logger.log_print('Loading server configurations...')

        self.config = None
        self.local_connection = None
        self.district_connection = None
        self.masterserver_connection = None
        self.shutting_down = False
        self.loop = None
        self.last_error = None
        self.allowed_iniswaps = None
        self.area_list = None
        self.old_area_list = None
        self.default_area = 0
        self.all_passwords = list()
        self.global_allowed = True
        self.server_select_name = 'SERVER_SELECT'

        self.load_config()
        self.client_manager: ClientManager = client_manager(self)
        self.char_list = list()
        self.load_iniswaps()
        self.load_characters()

        self.game_manager = GameManager(self)
        self.trial_manager = TrialManager(self)
        self.zone_manager = ZoneManager(self)
        self.area_manager = AreaManager(self)
        self.ban_manager = BanManager(self)
        self.party_manager = PartyManager(self)

        self.ipid_list = {}
        self.hdid_list = {}
        self.music_list = None
        self.backgrounds = None
        self.gimp_list = list()
        self.load_commandhelp()
        self.load_music()
        self.load_backgrounds()
        self.load_ids()
        self.load_gimp()

        self.district_client = None
        self.ms_client = None
        self.rp_mode = False
        self.user_auth_req = False
        self.showname_freeze = False
        self.commands = importlib.import_module('server.commands')
        self.commands_alt = importlib.import_module('server.commands_alt')
        self.logger_handlers = logger.setup_logger(debug=self.config['debug'])

        logger.log_print('Server configurations loaded successfully!')

        self.error_queue = None
        self._server = None

    async def start(self):
        self.loop = asyncio.get_event_loop()
        self.error_queue = asyncio.Queue()

        self.tasker = Tasker(self)
        bound_ip = '0.0.0.0'
        if self.config['local']:
            bound_ip = '127.0.0.1'
            server_name = 'localhost'
            logger.log_print('Starting a local server...')
        else:
            server_name = self.config['masterserver_name']
            logger.log_print('Starting a nonlocal server...')

        # Check if port is available
        port = self.config['port']
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            try:
                s.bind((bound_ip, port))
            except socket.error as exc:
                if exc.errno == errno.EADDRINUSE:
                    msg = (
                        f'Port {port} is in use by another application. Make sure to close any '
                        f'conflicting applications (even another instance of this server) and '
                        f'try again.')
                    raise ServerError(msg)
                raise exc
            except OverflowError as exc:
                msg = str(exc).replace('bind(): ', '').capitalize()
                msg += ' Make sure to set your port number to an appropriate value and try again.'
                raise ServerError(msg)

        # Yes there is a race condition here (between checking if port is available, and actually
        # using it). The only side effect of a race condition is a slightly less nice error
        # message, so it's not that big of a deal.
        self._server = await self.loop.create_server(
            lambda: self.protocol(self), bound_ip, port, start_serving=False)
        asyncio.create_task(self._server.serve_forever())
        logger.log_pserver('Server started successfully!')

        if self.config['local']:
            host_ip = '127.0.0.1'
        else:
            try:
                host_ip = (urllib.request.urlopen(
                    'https://api.ipify.org',
                    context=ssl.SSLContext()).read().decode('utf8'))
            except urllib.error.URLError as ex:
                host_ip = None
                logger.log_pdebug(
                    'Unable to obtain personal IP from https://api.ipify.org\n'
                    '{}: {}\n'
                    'Players may be unable to join.'.format(
                        type(ex).__name__, ex.reason))
        if host_ip is not None:
            logger.log_pdebug(
                'Server should be now accessible from {}:{}:{}'.format(
                    host_ip, self.config['port'], server_name))
        if not self.config['local']:
            logger.log_pdebug(
                'If you want to join your server from this device, you may need to '
                'join with this IP instead: 127.0.0.1:{}:localhost'.format(
                    self.config['port']))

        if self.config['local']:
            self.local_connection = asyncio.create_task(
                self.tasker.do_nothing())

        if self.config['use_district']:
            self.district_client = DistrictClient(self)
            self.district_connection = asyncio.create_task(
                self.district_client.connect())
            print(' ')
            logger.log_print(
                'Attempting to connect to district at {}:{}.'.format(
                    self.config['district_ip'], self.config['district_port']))

        if self.config['use_masterserver']:
            self.ms_client = MasterServerClient(self)
            self.masterserver_connection = asyncio.create_task(
                self.ms_client.connect())
            print(' ')
            logger.log_print(
                'Attempting to connect to the master server at {}:{} with the '
                'following details:'.format(self.config['masterserver_ip'],
                                            self.config['masterserver_port']))
            logger.log_print('*Server name: {}'.format(
                self.config['masterserver_name']))
            logger.log_print('*Server description: {}'.format(
                self.config['masterserver_description']))

        raise await self.error_queue.get()

    async def normal_shutdown(self):

        # Cleanup operations
        self.shutting_down = True

        # Cancel further polling for district/master server
        if self.local_connection:
            self.local_connection.cancel()
            await self.tasker.await_cancellation(self.local_connection)

        if self.district_connection:
            self.district_connection.cancel()
            await self.tasker.await_cancellation(self.district_connection)

        if self.masterserver_connection:
            self.masterserver_connection.cancel()
            await self.tasker.await_cancellation(self.masterserver_connection)
            await self.tasker.await_cancellation(self.ms_client.shutdown())

        # Cancel pending client tasks and cleanly remove them from the areas
        players = self.get_player_count()
        logger.log_print('Kicking {} remaining client{}.'.format(
            players, 's' if players != 1 else ''))

        for client in self.get_clients():
            client.disconnect()

        if self._server:
            self._server.close()
            await self._server.wait_closed()

    def get_version_string(self):
        mes = '{}.{}.{}'.format(self.release, self.major_version,
                                self.minor_version)
        if self.segment_version:
            mes = '{}-{}'.format(mes, self.segment_version)
        return mes

    def reload(self):
        # Keep backups in case of failure
        backup = [
            self.char_list.copy(),
            self.music_list.copy(),
            self.backgrounds.copy()
        ]

        # Do a dummy YAML load to see if the files can be loaded and parsed at all first.
        reloaded_assets = [
            self.load_characters, self.load_backgrounds, self.load_music
        ]
        for reloaded_asset in reloaded_assets:
            try:
                reloaded_asset()
            except ServerError.YAMLInvalidError as exc:
                # The YAML exception already provides a full description. Just add the fact the
                # reload was undone to ease the person who ran the command's nerves.
                msg = (f'{exc} Reload was undone.')
                raise ServerError.YAMLInvalidError(msg)
            except ServerError.FileSyntaxError as exc:
                msg = f'{exc} Reload was undone.'
                raise ServerError(msg)

        # Only on success reload
        self.load_characters()
        self.load_backgrounds()

        try:
            self.load_music()
        except ServerError as exc:
            self.char_list, self.music_list, self.backgrounds = backup
            msg = (
                'The new music list returned the following error when loading: `{}`. Fix the '
                'error and try again. Reload was undone.'.format(exc))
            raise ServerError.FileSyntaxError(msg)

    def reload_commands(self):
        try:
            self.commands = importlib.reload(self.commands)
            self.commands_alt = importlib.reload(self.commands_alt)
        except Exception as error:
            return error

    def log_packet(self, client: ClientManager.Client, packet: str,
                   incoming: bool):
        while len(self.logged_packets) > self.logged_packet_limit:
            self.logged_packets.pop(0)
        entry = ('R:' if incoming else 'S:', Constants.get_time_iso(),
                 str(client.id), packet)
        self.logged_packets.append(entry)

    def new_client(self,
                   transport,
                   ip=None,
                   my_protocol=None) -> ClientManager.Client:
        c = self.client_manager.new_client(transport, my_protocol=my_protocol)
        if self.rp_mode:
            c.in_rp = True
        c.server = self
        c.area = self.area_manager.default_area()
        c.area.new_client(c)
        return c

    def remove_client(self, client: ClientManager.Client):
        client.area.remove_client(client)
        self.client_manager.remove_client(client)

    def is_client(self, client: ClientManager.Client):
        # This should only be False for clients that have been disconnected.
        return not client.disconnected and self.client_manager.is_client(
            client)

    def get_clients(self) -> List[ClientManager.Client]:
        """
        Return a copy of all the clients connected to the server, sorted in ascending order by
        client ID.

        Returns
        -------
        list of ClientManager.Client
            Clients connected to the server.

        """
        return sorted(self.client_manager.clients)

    def get_player_count(self) -> int:
        # Ignore players in the server selection screen.
        return len([
            client for client in self.get_clients()
            if client.char_id is not None
        ])

    def load_backgrounds(self) -> List[str]:
        backgrounds = ValidateBackgrounds().validate('config/backgrounds.yaml')

        self.backgrounds = backgrounds
        default_background = self.backgrounds[0]
        # Make sure each area still has a valid background
        for area in self.area_manager.areas:
            if area.background not in self.backgrounds and not area.cbg_allowed:
                # The area no longer has a valid background, so change it to some valid background
                # like the first one
                area.change_background(default_background)
                area.broadcast_ooc(
                    f'After a server refresh, your area no longer had a valid '
                    f'background. Switching to {default_background}.')

        return backgrounds.copy()

    def load_config(self) -> Dict[str, Any]:
        self.config = ValidateConfig().validate('config/config.yaml')

        self.config['motd'] = self.config['motd'].replace('\\n', ' \n')
        self.all_passwords = list()
        passwords = [
            'modpass',
            'cmpass',
            'gmpass',
            'gmpass1',
            'gmpass2',
            'gmpass3',
            'gmpass4',
            'gmpass5',
            'gmpass6',
            'gmpass7',
        ]

        self.all_passwords = [
            self.config[password] for password in passwords
            if self.config[password]
        ]

        # Default values to fill in config.yaml if not present
        defaults_for_tags = {
            'show_ms2-prober': True,
            'discord_link': None,
            'utc_offset': 'local',
            'max_numdice': 20,
            'max_numfaces': 11037,
            'max_modifier_length': 12,
            'max_acceptable_term': 22074,
            'def_numdice': 1,
            'def_numfaces': 6,
            'def_modifier': '',
            'blackout_background': 'Blackout_HD',
            'default_area_description': 'No description.',
            'party_lights_timeout': 10,
            'showname_max_length': 30,
            'sneak_handicap': 5,
            'spectator_name': 'SPECTATOR',
            'music_change_floodguard': {
                'times_per_interval': 1,
                'interval_length': 0,
                'mute_length': 0
            }
        }

        for (tag, value) in defaults_for_tags.items():
            if tag not in self.config:
                self.config[tag] = value

        return self.config

    def load_characters(self) -> List[str]:
        characters = ValidateCharacters().validate('config/characters.yaml')

        if self.char_list != characters:
            # Inconsistent character list, so change everyone to spectator
            for client in self.get_clients():
                if client.char_id != -1:
                    # Except those that are already spectators
                    client.change_character(-1)
                client.send_ooc(
                    'The server character list was changed and no longer reflects your '
                    'client character list. Please rejoin the server.')

        self.char_list = characters
        return characters.copy()

    def load_commandhelp(self):
        with Constants.fopen('README.md', 'r', encoding='utf-8') as readme:
            lines = [x.rstrip() for x in readme.readlines()]

        self.linetorank = {
            '### User Commands': 'normie',
            '### GM Commands': 'gm',
            '### Community Manager Commands': 'cm',
            '### Moderator Commands': 'mod'
        }

        self.commandhelp = {
            'normie': dict(),
            'gm': dict(),
            'cm': dict(),
            'mod': dict()
        }

        # Look for the start of the command list
        try:
            start_index = lines.index('## Commands')
            end_index = lines.index('### Debug commands')
        except ValueError as error:
            error_mes = ", ".join([str(s) for s in error.args])
            message = (
                'Unable to generate help based on README.md: {}. Are you sure you have the '
                'latest README.md?'.format(error_mes))
            raise ServerError(message)

        rank = None
        current_command = None

        for line in lines[start_index:end_index]:
            # Check if empty line
            if not line:
                continue

            # Check if this line defines the rank we are taking a look at right now
            if line in self.linetorank.keys():
                rank = self.linetorank[line]
                current_command = None
                continue

            # Otherwise, check if we do not have a rank yet
            if rank is None:
                continue

            # Otherwise, check if this is the start of a command
            if line[0] == '*':
                # Get the command name
                command_split = line[4:].split('** ')
                if len(command_split) == 1:
                    # Case: * **version**
                    current_command = command_split[0][:-2]
                else:
                    # Case: * **uninvite** "ID/IPID"
                    current_command = command_split[0]

                formatted_line = '/{}'.format(line[2:])
                formatted_line = formatted_line.replace('**', '')
                self.commandhelp[rank][current_command] = [formatted_line]
                continue

            # Otherwise, line is part of command description, so add it to its current command desc
            #     - Unlocks your area, provided the lock came as a result of /lock.
            # ... assuming we have a command
            if current_command:
                self.commandhelp[rank][current_command].append(line[4:])
                continue

            # Otherwise, we have a line that is a description of the rank
            # Do nothing about them
            continue  # Not really needed, but made explicit

    def load_ids(self):
        self.ipid_list = dict()
        self.hdid_list = dict()

        # load ipids
        try:
            with Constants.fopen('storage/ip_ids.json', 'r',
                                 encoding='utf-8') as whole_list:
                self.ipid_list = json.load(whole_list)
        except ServerError.FileNotFoundError:
            with Constants.fopen('storage/ip_ids.json', 'w',
                                 encoding='utf-8') as whole_list:
                json.dump(dict(), whole_list)
            message = 'WARNING: File not found: storage/ip_ids.json. Creating a new one...'
            logger.log_pdebug(message)
        except Exception as ex:
            message = 'WARNING: Error loading storage/ip_ids.json. Will assume empty values.\n'
            message += '{}: {}'.format(type(ex).__name__, ex)
            logger.log_pdebug(message)

        # If the IPID list is not a dict, fix the file
        # Why on earth is it called an IPID list if it is a Python dict is beyond me.
        if not isinstance(self.ipid_list, dict):
            message = (
                f'WARNING: File storage/ip_ids.json had a structure of the wrong type: '
                f'{self.ipid_list}. Replacing it with a proper type.')
            logger.log_pdebug(message)
            self.ipid_list = dict()
            self.dump_ipids()

        # load hdids
        try:
            with Constants.fopen('storage/hd_ids.json', 'r',
                                 encoding='utf-8') as whole_list:
                self.hdid_list = json.loads(whole_list.read())
        except ServerError.FileNotFoundError:
            with Constants.fopen('storage/hd_ids.json', 'w',
                                 encoding='utf-8') as whole_list:
                json.dump(dict(), whole_list)
            message = 'WARNING: File not found: storage/hd_ids.json. Creating a new one...'
            logger.log_pdebug(message)
        except Exception as ex:
            message = 'WARNING: Error loading storage/hd_ids.json. Will assume empty values.\n'
            message += '{}: {}'.format(type(ex).__name__, ex)
            logger.log_pdebug(message)

        # If the HDID list is not a dict, fix the file
        # Why on earth is it called an HDID list if it is a Python dict is beyond me.
        if not isinstance(self.hdid_list, dict):
            message = (
                f'WARNING: File storage/hd_ids.json had a structure of the wrong type: '
                f'{self.hdid_list}. Replacing it with a proper type.')
            logger.log_pdebug(message)
            self.hdid_list = dict()
            self.dump_hdids()

    def load_iniswaps(self):
        try:
            with Constants.fopen('config/iniswaps.yaml', 'r',
                                 encoding='utf-8') as iniswaps:
                self.allowed_iniswaps = Constants.yaml_load(iniswaps)
        except Exception as ex:
            message = 'WARNING: Error loading config/iniswaps.yaml. Will assume empty values.\n'
            message += '{}: {}'.format(type(ex).__name__, ex)

            logger.log_pdebug(message)

    def load_music(self,
                   music_list_file: str = 'config/music.yaml',
                   server_music_list: bool = True) -> List[Dict[str, Any]]:
        music_list = ValidateMusic().validate(music_list_file)

        if server_music_list:
            self.music_list = music_list
            try:
                self.build_music_list(music_list=music_list)
            except ServerError.FileSyntaxError as exc:
                msg = (
                    f'File {music_list_file} returned the following error when loading: '
                    f'`{exc.message}`')
                raise ServerError.FileSyntaxError(msg)

        return music_list.copy()

    def load_gimp(self):
        try:
            gimp_list = ValidateGimp().validate('config/gimp.yaml')
        except ServerError.FileNotFoundError:
            gimp_list = [
                'ERP IS BAN',
                'HELP ME',
                '(((((case????)))))',
                'Anyone else a fan of MLP?',
                'does this server have sans from undertale?',
                'what does call mod do',
                'Join my discord server please',
                'can I have mod pls?',
                'why is everyone a missingo?',
                'how 2 change areas?',
                '19 years of perfection, i don\'t play games to f*****g lose',
                ('nah... your taunts are f*****g useless... only defeat angers me... by trying '
                 'to taunt just earns you my pitty'),
                'When do we remove dangits',
                'MODS STOP GIMPING ME',
                'PLAY NORMIES PLS',
                'share if you not afraid of herobrine',
                'New Killer Choosen! Hold On!!',
                'The cake killed Nether.',
                'How you win Class Trials is simple, call your opposition cucks.',
            ]
            with Constants.fopen('config/gimp.yaml', 'w') as gimp:
                Constants.yaml_dump(gimp_list, gimp)
            message = 'WARNING: File not found: config/gimp.yaml. Creating a new one...'
            logger.log_pdebug(message)

        self.gimp_list = gimp_list
        return gimp_list.copy()

    def dump_ipids(self):
        with Constants.fopen('storage/ip_ids.json', 'w',
                             encoding='utf-8') as whole_list:
            json.dump(self.ipid_list, whole_list)

    def dump_hdids(self):
        with Constants.fopen('storage/hd_ids.json', 'w',
                             encoding='utf-8') as whole_list:
            json.dump(self.hdid_list, whole_list)

    def get_ipid(self, ip: str) -> int:
        if ip not in self.ipid_list:
            while True:
                ipid = random.randint(0, 10**10 - 1)
                if ipid not in self.ipid_list.values():
                    break
            self.ipid_list[ip] = ipid
            self.dump_ipids()
        return self.ipid_list[ip]

    def build_music_list(self,
                         from_area: AreaManager.Area = None,
                         c: ClientManager.Client = None,
                         music_list: List[Dict[str, Any]] = None,
                         include_areas: bool = True,
                         include_music: bool = True) -> List[str]:
        built_music_list = list()

        # add areas first, if needed
        if include_areas:
            built_music_list.extend(
                self.prepare_area_list(c=c, from_area=from_area))

        # then add music, if needed
        if include_music:
            built_music_list.extend(
                self.prepare_music_list(c=c, specific_music_list=music_list))

        return built_music_list

    def prepare_area_list(self,
                          c: ClientManager.Client = None,
                          from_area: AreaManager.Area = None) -> List[str]:
        """
        Return the area list of the server. If given c and from_area, it will send an area list
        that matches the perspective of client `c` as if they were in area `from_area`.

        Parameters
        ----------
        c: ClientManager.Client
            Client whose perspective will be taken into account, by default None
        from_area: AreaManager.Area
            Area from which the perspective will be considered, by default None

        Returns
        -------
        list of str
            Area list that matches intended perspective.
        """

        # Determine whether to filter the areas in the results
        need_to_check = (from_area is None or
                         (c is not None and (c.is_staff() or c.is_transient)))

        # Now add areas
        prepared_area_list = list()
        for area in self.area_manager.areas:
            if need_to_check or area.name in from_area.visible_reachable_areas:
                prepared_area_list.append("{}-{}".format(area.id, area.name))

        return prepared_area_list

    def prepare_music_list(
            self,
            c: ClientManager.Client = None,
            specific_music_list: List[Dict[str, Any]] = None) -> List[str]:
        """
        If `specific_music_list` is not None, return a client-ready version of that music list.
        Else, if `c` is a client with a custom chosen music list, return their latest music list.
        Otherwise, return a client-ready version of the server music list.

        Parameters
        ----------
        c: ClientManager.Client
            Client whose current music list if it exists will be considered if `specific_music_list`
            is None
        specific_music_list: list of dictionaries with key sets {'category', 'songs'}
            Music list to use if given

        Returns
        -------
        list of str
            Music list ready to be sent to clients
        """

        # If not provided a specific music list to overwrite
        if specific_music_list is None:
            specific_music_list = self.music_list  # Default value
            # But just in case, check if this came as a request of a client who had a
            # previous music list preference
            if c and c.music_list is not None:
                specific_music_list = c.music_list

        prepared_music_list = list()
        for item in specific_music_list:
            category = item['category']
            songs = item['songs']
            prepared_music_list.append(category)
            for song in songs:
                name = song['name']
                prepared_music_list.append(name)

        return prepared_music_list

    def is_valid_char_id(self, char_id: int) -> bool:
        return len(self.char_list) > char_id >= -1

    def get_char_id_by_name(self, name: str) -> int:
        if name == self.config['spectator_name']:
            return -1
        for i, ch in enumerate(self.char_list):
            if ch.lower() == name.lower():
                return i
        raise ServerError(f'Character {name} not found.')

    def get_song_data(self,
                      music: str,
                      c: ClientManager.Client = None) -> Tuple[str, int, str]:
        # The client's personal music list should also be a valid place to search
        # so search in there too if possible
        if c and c.music_list:
            valid_music = self.music_list + c.music_list
        else:
            valid_music = self.music_list

        for item in valid_music:
            if item['category'] == music:
                return item['category'], -1, ''
            for song in item['songs']:
                if song['name'] == music:
                    name = song['name']
                    length = song['length'] if 'length' in song else -1
                    source = song['source'] if 'source' in song else ''
                    return name, length, source
        raise ServerError.MusicNotFoundError('Music not found.')

    def send_all_cmd_pred(self,
                          cmd: str,
                          *args: List[str],
                          pred: Callable[[ClientManager.Client],
                                         bool] = lambda x: True):
        for client in self.get_clients():
            if pred(client):
                client.send_command(cmd, *args)

    def make_all_clients_do(self,
                            function: str,
                            *args: List[str],
                            pred: Callable[[ClientManager.Client],
                                           bool] = lambda x: True,
                            **kwargs):
        for client in self.get_clients():
            if pred(client):
                getattr(client, function)(*args, **kwargs)

    def send_error_report(self, client: ClientManager.Client, cmd: str,
                          args: List[str], ex: Exception):
        """
        In case of an error caused by a client packet, send error report to user, notify moderators
        and have full traceback available on console and through /lasterror
        """

        # Send basic logging information to user
        info = (
            '=========\nThe server ran into a Python issue. Please contact the server owner '
            'and send them the following logging information:')
        etype, evalue, etraceback = sys.exc_info()
        tb = traceback.extract_tb(tb=etraceback)
        current_time = Constants.get_time()
        file, line_num, module, func = tb[-1]
        file = file[file.rfind('\\') + 1:]  # Remove unnecessary directories
        version = self.version
        info += '\r\n*Server version: {}'.format(version)
        info += '\r\n*Server time: {}'.format(current_time)
        info += '\r\n*Packet details: {} {}'.format(cmd, args)
        info += '\r\n*Client status: {}'.format(client)
        info += '\r\n*Area status: {}'.format(client.area)
        info += '\r\n*File: {}'.format(file)
        info += '\r\n*Line number: {}'.format(line_num)
        info += '\r\n*Module: {}'.format(module)
        info += '\r\n*Function: {}'.format(func)
        info += '\r\n*Error: {}: {}'.format(type(ex).__name__, ex)
        info += '\r\nYour help would be much appreciated.'
        info += '\r\n========='
        client.send_ooc(info)
        client.send_ooc_others(
            'Client {} triggered a Python error through a client packet. '
            'Do /lasterror to take a look at it.'.format(client.id),
            pred=lambda c: c.is_mod)

        # Print complete traceback to console
        info = 'TSUSERVERDR HAS ENCOUNTERED AN ERROR HANDLING A CLIENT PACKET'
        info += '\r\n*Server time: {}'.format(current_time)
        info += '\r\n*Packet details: {} {}'.format(cmd, args)
        info += '\r\n*Client status: {}'.format(client)
        info += '\r\n*Area status: {}'.format(client.area)
        info += '\r\n\r\n{}'.format("".join(
            traceback.format_exception(etype, evalue, etraceback)))
        logger.log_print(info)
        self.last_error = [info, etype, evalue, etraceback]

        # Log error to file
        logger.log_error(info, server=self, errortype='C')

        if self.in_test:
            raise ex

    def broadcast_global(
            self,
            client: ClientManager.Client,
            msg: str,
            as_mod: bool = False,
            mtype: str = "<dollar>G",
            condition: Constants.ClientBool = lambda x: not x.muted_global):
        username = client.name
        ooc_name = '{}[{}][{}]'.format(mtype, client.area.id, username)
        if as_mod:
            ooc_name += '[M]'
        ooc_name_ipid = f'{ooc_name}[{client.ipid}]'
        targets = [c for c in self.get_clients() if condition(c)]
        for c in targets:
            if c.is_officer():
                c.send_ooc(msg, username=ooc_name_ipid)
            else:
                c.send_ooc(msg, username=ooc_name)

        if self.config['use_district']:
            msg = 'GLOBAL#{}#{}#{}#{}'.format(int(as_mod), client.area.id,
                                              username, msg)
            self.district_client.send_raw_message(msg)

    def broadcast_need(self, client: ClientManager.Client, msg: str):
        char_name = client.displayname
        area_name = client.area.name
        area_id = client.area.id

        targets = [c for c in self.get_clients() if not c.muted_adverts]
        msg = ('=== Advert ===\r\n{} in {} [{}] needs {}\r\n==============='.
               format(char_name, area_name, area_id, msg))
        for c in targets:
            c.send_ooc(msg)

        if self.config['use_district']:
            msg = 'NEED#{}#{}#{}#{}'.format(char_name, area_name, area_id, msg)
            self.district_client.send_raw_message(msg)
Esempio n. 13
0
    def load(self, path="config/areas.yaml", hub_id=-1):
        try:
            with open(path, "r", encoding="utf-8") as stream:
                hubs = yaml.safe_load(stream)
        except Exception:
            raise AreaError(
                f"Trying to load Hub list: File path {path} is invalid!")

        if hub_id != -1:
            try:
                self.hubs[hub_id].load(hubs[hub_id], destructive=True)
            except ValueError:
                raise AreaError(
                    f"Invalid Hub ID {hub_id}! Please contact the server host."
                )
            return

        if "area" in hubs[0]:
            # Legacy support triggered! Abort operation
            if len(self.hubs) <= 0:
                self.hubs.append(AreaManager(self, "Hub 0"))
            self.hubs[0].load_areas(hubs)

            is_dr_hub = False
            # tsuserverDR conversion hell
            for i, area in enumerate(hubs):
                # oh God why did they do it this way
                if "reachable_areas" in area:
                    reachable_areas = area["reachable_areas"].split(",")
                    # I hate this
                    for a_name in reachable_areas:
                        a_name = a_name.strip()
                        target_area = self.hubs[0].get_area_by_name(
                            a_name, case_sensitive=True)
                        self.hubs[0].areas[i].link(target_area.id)
                        print(
                            f"[tsuDR conversion] Linking area {self.hubs[0].areas[i].name} to {target_area.name}"
                        )
                        is_dr_hub = True
                if "default_description" in area:
                    self.hubs[0].areas[i].desc = area["default_description"]
                if "song_switch_allowed" in area:
                    self.hubs[0].areas[i].can_dj = area["song_switch_allowed"]
            if is_dr_hub:
                self.hubs[0].arup_enabled = False
                print(
                    "[tsuDR conversion] Setting hub 0 ARUP to False due to TsuserverDR yaml supplied. Please use /save_hub as a mod to adapt the areas.yaml to KFO style."
                )
            return

        i = 0
        for hub in hubs:
            while len(self.hubs) < len(hubs):
                # Make sure that the hub manager contains enough hubs to update with new information
                self.hubs.append(AreaManager(self, f"Hub {len(self.hubs)}"))
            while len(self.hubs) > len(hubs):
                # Clean up excess hubs
                h = self.hubs.pop()
                clients = h.clients.copy()
                for client in clients:
                    client.set_area(self.default_hub().default_area())

            self.hubs[i].load(hub)
            self.hubs[i].o_name = self.hubs[i].name
            self.hubs[i].o_abbreviation = self.hubs[i].abbreviation
            i += 1