Tip of the day: On IRC you can use the HELPOP command to read about various IRC commands.

Dev:Command Overrides

From UnrealIRCd documentation wiki
Jump to navigation Jump to search

Command overrides allow you to "override" a core command (or a command from other modules). Your code will then execute either before or after the actual command is executed. Or, you can choose not to execute the real command at all.

Generally people prefer the Dev:Hook API as it's more fine-grained and requires less work on your hand. People generally revert to Command Override's if Hooks do not provide (all) the functionality they are looking for.

About the variables available via CMD_OVERRIDE_FUNC

For command overrides we will use the CMD_OVERRIDE_FUNC() macro. These are the variables that you will receive in your override handler in your module:

Variable Type Description
ovr CommandOverride * The override information, usually not touched, except for passing to CallCommandOverride()
client Client * Client pointer of user executing the command.
recv_mtags MessageTag * Message tags associated with the command. If you don't know what this is then just ignore it for now.
parc int Command + parameter count
parv char *[] Parameters

Registering your command override

Registering your override is easy. Example:

CMD_OVERRIDE_FUNC(mymod_nickoverride); /* Forward declaration */

MOD_LOAD(mymod)
{
    CommandOverrideAdd(modinfo->handle, "NICK", mymod_nickoverride);
}

Important: Command overrides must be registered in MOD_LOAD, not in MOD_INIT. This is because in MOD_INIT the command you want to override may not have been added yet (due to module load ordering).

The command override function

Generally you want your code to execute before the real (core?) function is called:

CMD_OVERRIDE_FUNC(mymod_nickoverride)
{
    sendnotice(client, "We are about to call the real function. We could do something useful here.");

    CALL_NEXT_COMMAND_OVERRIDE();
}

If you want to execute your function after this is also possible, it just takes some extra care:

CMD_OVERRIDE_FUNC(mymod_nickoverride)
{
    CALL_NEXT_COMMAND_OVERRIDE();

    if (IsDead(client))
        return; /* killed or client exited, we MUST return */

    sendnotice(client, "The command was executed. We could do something useful here afterwards.");
}

Note: The CALL_NEXT_COMMAND_OVERRIDE() macro passes all arguments automatically and is future-proof. If you see older code using CallCommandOverride(ovr, client, recv_mtags, parc, parv) directly, it still works but the macro is preferred for new code.

More information

For more information on parc and parv[], see Dev:Command_API#Parameter_checking

You can also look at the source of other modules or UnrealIRCd itself. Overrides are used in the hideserver module, restrict-commands and in the antimixedutf8 module.