Exemplo n.º 1
0
def load_config():
    config = get_config()
    config_file = "minqlx/config.cfg"
    if os.path.isfile(config_file):
        config.read(config_file)
        config["DEFAULT"] = { 
                                "PluginsFolder" : "minqlx/plugins",
                                "CommandPrefix" : "!"
                            }

        # Set default database if present.
        if "Database" in config["Core"]:
            db = config["Core"]["Database"]
            if db.lower() == "redis":
                minqlx.Plugin.database = minqlx.database.Redis

        # Set owner.
        # TODO: Convert regular SteamID format to 64-bit format automatically.
        if "Owner" in config["Core"]:
            try:
                global _owner
                _owner = int(config["Core"]["Owner"])
            except ValueError:
                logger = minqlx.get_logger()
                logger.error("Failed to parse the Owner Steam ID. Make sure it's in SteamID64 format.")
        else:
            logger = minqlx.get_logger()
            logger.warning("Owner not set in the config. Consider setting it.")

        sys.path.append(os.path.dirname(config["Core"]["PluginsFolder"]))
        global _command_prefix
        _command_prefix = config["Core"]["CommandPrefix"].strip()
        return config
    else:
        raise(RuntimeError("Config file '{}' not found.".format(os.path.abspath(config_file))))
Exemplo n.º 2
0
    def dispatch(self, passed):
        # Check if there's a current vote in the first place.
        cs = minqlx.get_configstring(9)
        if not cs:
            minqlx.get_logger().warning("vote_ended went off without configstring 9.")
            return

        res = _re_vote.match(cs)
        vote = res.group("cmd")
        args = res.group("args") if res.group("args") else ""
        votes = (int(minqlx.get_configstring(10)), int(minqlx.get_configstring(11)))
        super().dispatch(votes, vote, args, passed)
Exemplo n.º 3
0
def handle_new_game(is_restart):
    # This is called early in the launch process, so it's a good place to initialize
    # minqlx stuff that needs QLDS to be initialized.
    global _first_game
    if _first_game:
        minqlx.late_init()
        _first_game = False

        # A good place to warn the owner if ZMQ stats are disabled.
        global _zmq_warning_issued
        if not bool(int(minqlx.get_cvar("zmq_stats_enable"))) and not _zmq_warning_issued:
            logger = minqlx.get_logger()
            logger.warning("Some events will not work because ZMQ stats is not enabled. "
                "Launch the server with \"zmq_stats_enable 1\"")
            _zmq_warning_issued = True

    minqlx.set_map_subtitles()

    if not is_restart:
        try:
            minqlx.EVENT_DISPATCHERS["map"].dispatch(
                minqlx.get_cvar("mapname"), 
                minqlx.get_cvar("g_factory"))
        except:
            minqlx.log_exception()
            return True

    try:
        minqlx.EVENT_DISPATCHERS["new_game"].dispatch()
    except:
        minqlx.log_exception()
        return True
Exemplo n.º 4
0
def parse_variables(varstr, ordered=False):
    """
    Parses strings of key-value pairs delimited by "\\" and puts
    them into a dictionary.

    :param varstr: The string with variables.
    :type varstr: str
    :param ordered: Whether it should use :class:`collections.OrderedDict` or not.
    :type ordered: bool
    :returns: dict -- A dictionary with the variables added as key-value pairs.
    """
    if ordered:
        res = collections.OrderedDict()
    else:
        res = {}
    if not varstr.strip():
        return res

    vars = varstr.lstrip("\\").split("\\")
    try:
        for i in range(0, len(vars), 2):
            res[vars[i]] = vars[i + 1]
    except IndexError:
        # Log and return incomplete dict.
        logger = minqlx.get_logger()
        logger.warning("Uneven number of keys and values: {}".format(varstr))

    return res
Exemplo n.º 5
0
    def dispatch(self, *args, **kwargs):
        """Calls all the handlers that have been registered when hooking this event.
        The recommended way to use this for events that inherit this class is to
        override the method with explicit arguments (as opposed to the this one's)
        and call this method by using ``super().dispatch()``.

        Handlers have several options for return values that can affect the flow:
            - minqlx.RET_NONE or None -- Continue execution normally.
            - minqlx.RET_STOP -- Stop any further handlers from being called.
            - minqlx.RET_STOP_EVENT -- Let handlers process it, but stop the event
                at the engine-level.
            - minqlx.RET_STOP_ALL -- Stop handlers **and** the event.
            - Any other value -- Passed on to :func:`self.handle_return`, which will
                by default simply send a warning to the logger about an unknown value
                being returned. Can be overridden so that events can have their own
                special return values.

        :param args: Any arguments.
        :param kwargs: Any keyword arguments.

        """
        # Allow subclasses of this to edit the arguments without having
        # to reimplement this method. Whenever an unknown return value
        # is returned, we pass it on to handle_return.
        self.args = args
        self.kwargs = kwargs
        logger = minqlx.get_logger()
        # Log the events as they come in.
        if self.name not in self.no_debug:
            dbgstr = "{}{}".format(self.name, args)
            if len(dbgstr) > 100:
                dbgstr = dbgstr[0:99] + ")"
            logger.debug(dbgstr)

        plugins = self.plugins.copy()
        self.return_value = True
        for i in range(5):
            for plugin in plugins:
                for handler in plugins[plugin][i]:
                    try:
                        res = handler(*self.args, **self.kwargs)
                        if res == minqlx.RET_NONE or res is None:
                            continue
                        elif res == minqlx.RET_STOP:
                            return True
                        elif res == minqlx.RET_STOP_EVENT:
                            self.return_value = False
                        elif res == minqlx.RET_STOP_ALL:
                            return False
                        else: # Got an unknown return value.
                            return_handler = self.handle_return(handler, res)
                            if return_handler is not None:
                                return return_handler
                    except:
                        minqlx.log_exception(plugin)
                        continue

        return self.return_value
Exemplo n.º 6
0
def handle_set_configstring(index, value):
    """Called whenever the server tries to set a configstring. Can return
    False to stop the event.

    """
    try:
        res = minqlx.EVENT_DISPATCHERS["set_configstring"].dispatch(index, value)
        if res == False:
            return False
        elif isinstance(res, str):
            value = res

        # GAME STATE CHANGES
        if index == 0:
            old_cs = minqlx.parse_variables(minqlx.get_configstring(index))
            if not old_cs:
                return

            new_cs = minqlx.parse_variables(value)
            old_state = old_cs["g_gameState"]
            new_state = new_cs["g_gameState"]
            if old_state != new_state:
                if old_state == "PRE_GAME" and new_state == "IN_PROGRESS":
                    minqlx.EVENT_DISPATCHERS["vote_ended"].cancel()  # Cancel current vote if any.
                    # minqlx.EVENT_DISPATCHERS["game_start"].dispatch()
                elif old_state == "PRE_GAME" and new_state == "COUNT_DOWN":
                    minqlx.EVENT_DISPATCHERS["game_countdown"].dispatch()
                elif old_state == "COUNT_DOWN" and new_state == "IN_PROGRESS":
                    minqlx.EVENT_DISPATCHERS["vote_ended"].cancel()  # Cancel current vote if any.
                    # minqlx.EVENT_DISPATCHERS["game_start"].dispatch()
                elif old_state == "IN_PROGRESS" and new_state == "PRE_GAME":
                    pass
                elif old_state == "COUNT_DOWN" and new_state == "PRE_GAME":
                    pass
                else:
                    logger = minqlx.get_logger()
                    logger.warning("UNKNOWN GAME STATES: {} - {}".format(old_state, new_state))

        # ROUND COUNTDOWN AND START
        if index == 661:
            cvars = minqlx.parse_variables(value)
            if cvars:
                round_number = int(cvars["round"])
                if round_number and "time" in cvars:
                    if round_number == 1:  # This is the case when the first countdown starts.
                        minqlx.EVENT_DISPATCHERS["round_countdown"].dispatch(round_number)
                        return

                    minqlx.EVENT_DISPATCHERS["round_countdown"].dispatch(round_number)
                    return
                elif round_number:
                    minqlx.EVENT_DISPATCHERS["round_start"].dispatch(round_number)
                    return

        return res
    except:
        minqlx.log_exception()
        return True
Exemplo n.º 7
0
def owner():
    """Returns the SteamID64 of the owner. This is set in the config."""
    try:
        sid = int(minqlx.get_cvar("qlx_owner"))
        if sid == -1:
            raise RuntimeError
        return sid
    except:
        logger = minqlx.get_logger()
        logger.error("Failed to parse the Owner Steam ID. Make sure it's in SteamID64 format.")
Exemplo n.º 8
0
 def handle_return(self, handler, value):
     """Handle an unknown return value. If this returns anything but None,
     the it will stop execution of the event and pass the return value on
     to the C-level handlers. This method can be useful to override,
     because of the fact that you can edit the arguments that will be
     passed on to any handler after whatever handler returned *value*
     by editing *self.args*, *self.kwargs*. Furthermore, *self.return_value*
     is the return value that will be sent to the C-level handler if the
     event isn't stopped later along the road.
     """
     logger = minqlx.get_logger()
     logger.warning("Handler '{}' returned unknown value '{}' for event '{}'"
         .format(handler.__name__, value, self.name))
Exemplo n.º 9
0
 def run(self):
     loop = asyncio.new_event_loop()
     logger = minqlx.get_logger("irc")
     asyncio.set_event_loop(loop)
     while not self.stop_event.is_set():
         try:
             loop.run_until_complete(self.connect())
         except Exception:
             minqlx.log_exception()
         
         # Disconnected. Try reconnecting in 30 seconds.
         logger.info("Disconnected from IRC. Reconnecting in 30 seconds...")
         time.sleep(30)
     loop.close()
Exemplo n.º 10
0
def handle_console_print(text):
    """Called whenever the server prints something to the console and when rcon is used."""
    try:
        text = text.rstrip()
        if not text:
            return
        
        # Log console output. Removes the need to have stdout logs in addition to minqlx.log.
        minqlx.get_logger().debug(text)

        res = minqlx.EVENT_DISPATCHERS["console_print"].dispatch(text)
        if res == False:
            return False
        elif isinstance(res, str):
            text = res

        if _print_redirection:
            global _print_buffer
            _print_buffer += text

        return res
    except:
        minqlx.log_exception()
        return True
Exemplo n.º 11
0
 def run(self):
     loop = asyncio.new_event_loop()
     logger = minqlx.get_logger("irc")
     asyncio.set_event_loop(loop)
     while not self.stop_event.is_set():
         try:
             loop.run_until_complete(self.connect())
         except Exception:
             minqlx.log_exception()
         
         # Disconnected. Try reconnecting in 30 seconds.
         logger.info("Disconnected from IRC. Reconnecting in 30 seconds...")
         minqlx.CHAT_CHANNEL.reply("Connection attempt to ^3CommLink^7 server failed, retrying in 30 seconds.")
         time.sleep(30)
     loop.close()
Exemplo n.º 12
0
def handle_new_game():
    # This is called early in the launch process, so it's a good place to warn
    # the owner if ZMQ stats are disabled.
    if not bool(int(minqlx.get_cvar("zmq_stats_enable"))) and not _zmq_warning_issued:
        global _zmq_warning_issued
        logger = minqlx.get_logger()
        logger.warning(
            "Some events will not work because ZMQ stats is not enabled. " 'Launch the server with "zmq_stats_enable 1"'
        )
        _zmq_warning_issued = True

    try:
        minqlx.EVENT_DISPATCHERS["map"].dispatch(minqlx.get_cvar("mapname"), minqlx.get_cvar("g_factory"))
    except:
        minqlx.log_exception()
        return True
Exemplo n.º 13
0
    def state(self):
        """A string describing the state of the game.

        Possible values:
        - *warmup* -- The game has yet to start and is waiting for players to ready up.
        - *countdown* -- Players recently readied up, and it's counting down until the game starts.
        - *in_progress* -- The game is in progress.

        """
        s = self["g_gameState"]
        if s == "PRE_GAME":
            return "warmup"
        elif s == "COUNT_DOWN":
            return "countdown"
        elif s == "IN_PROGRESS":
            return "in_progress"
        else:
            logger = minqlx.get_logger()
            logger.warning("Got unknown game state: {}".format(s))
            return s
Exemplo n.º 14
0
    def handle_input(self, player, msg, channel):
        if not msg.strip():
            return

        name = msg.split(" ", 1)[0].lower()
        is_client_cmd = channel == "client_command"
        pass_through = True
        
        for priority_level in self._commands:
            for cmd in priority_level:
                if cmd.is_eligible_name(name) and cmd.is_eligible_channel(channel) and cmd.is_eligible_player(player, is_client_cmd):
                    # Client commands will not pass through to the engine unless told to explicitly.
                    # This is to avoid having to return RET_STOP_EVENT just to not get the "unknown cmd" msg.
                    if is_client_cmd:
                        pass_through = cmd.client_cmd_pass

                    # Dispatch "command" and allow people to stop it from being executed.
                    if minqlx.EVENT_DISPATCHERS["command"].dispatch(player, cmd, msg) == False:
                        return True
                    
                    res = cmd.execute(player, msg, channel)
                    if res == minqlx.RET_STOP:
                        return
                    elif res == minqlx.RET_STOP_EVENT:
                        pass_through = False
                    elif res == minqlx.RET_STOP_ALL:
                        # C-level dispatchers expect False if it shouldn't go to the engine.
                        return False
                    elif res == minqlx.RET_USAGE and cmd.usage:
                        channel.reply("^7Usage: ^6{} {}".format(name, cmd.usage))
                    elif res != None and res != minqlx.RET_NONE:
                        logger = minqlx.get_logger(None)
                        logger.warning("Command '{}' with handler '{}' returned an unknown return value: {}"
                            .format(cmd.name, cmd.handler.__name__, res))

        return pass_through
Exemplo n.º 15
0
 def logger(self):
     """An instance of :class:`logging.Logger`, but initialized for this plugin."""
     return minqlx.get_logger(self)
Exemplo n.º 16
0
 def logger(self):
     return minqlx.get_logger(self.plugin)
Exemplo n.º 17
0
 def execute(self, player, msg, channel):
     logger = minqlx.get_logger(self.plugin)
     logger.debug("{} executed: {} @ {} -> {}"
         .format(player.steam_id, self.name[0], self.plugin.name, channel))
     return self.handler(player, msg.split(), channel)