Method code for $robot.add_reaction_cmd()

[Turn on line numbering]
arg cmdstr, cmd, args, prep1, hook, prep2, me;
var t, type, method, times, chance, min, max, hook_args, i, opts, o, def_opts;

(> .perms(caller(), 'command) <);
hook = (| hook.to_symbol() |);
if (!hook)
    throw(~args, "Invalid hook");
def_opts = [["t?ype", 1], ["m?ethod", 1], ["ti?mes", 1], ["c?hance", 1], ["min", 1], ["max", 1], ["a?rgs", 1]];
[t, opts] = $parse_lib.getopt(args, def_opts);
t = t.join();

// Set the defaults
method = 'regexp;
type = 'tosay;
chance = 100;
times = -1;
hook_args = [];
min = 0;
max = 0;
catch any {
    // check the options!
    o = opts.slice(1);
    if ((i = "m?ethod" in o)) {
        method = (| opts[i][4].to_symbol() |);
        if (!method)
            throw(~args, "Invalid method specified with +m?ethod=??");
    }
    if ((i = "t?ype" in o)) {
        type = (| opts[i][4].to_symbol() |);
        if (!type)
            throw(~args, "No type specified with +t?ype=??");
    }
    if ((i = "c?hance" in o)) {
        chance = (| opts[i][4].to_number() |);
        if (chance < 1 || chance > 100)
            throw(~args, "+c?hance=?? requires an integer between 1 and 100");
    }
    if ((i = "ti?mes" in o)) {
        times = (| opts[i][4].to_number() |);
        if (!times || times < -1)
            throw(~args, "+ti?mes=?? requires an integer >= -1");
    }
    if ((i = "min" in o)) {
        min = (| opts[i][4].to_number() |);
        if (!min || min < 0)
            throw(~args, "+min=?? requires an integer >= 0");
    }
    if ((i = "max" in o)) {
        max = (| opts[i][4].to_number() |);
        if (!max || max < 0)
            throw(~args, "+max=?? requires an integer >= 0");
    }
    if ((i = "a?rgs" in o)) {
        hook_args = (> fromliteral(opt[i][4]) <);
        if (type(hook_args) != 'list)
            hook_args = [hook_args];
    }
    if (max < min)
        max = min;
    (> .add_reaction(method, t, type, chance, times, [hook, hook_args], min, max) <);
} with {
    return traceback()[1][2];
}
return strfmt("Reaction %d added to %s.%s() with match method %s, type %s, chance %d, times %d, args %s, min_delay %s, and max_delay %s.", t, toliteral(this()), hook, toliteral(method), toliteral(type), chance, times, toliteral(hook_args), min, max);

// $#Copied 21 Feb 98 01:50 from $programmer.add_command_cmd() by $user_sean
// $#Edited: 21 Feb 98 03:23 $user_sean
// $#Edited: 21 Feb 98 03:24 $user_sean

["// Created 26-Mar-1995 as a part of ColdCore, see: @help Credit"]

the Cold Dark