/
CDC-1.2b/
CDC-1.2b/src/
parent $command_cache
object $commands

var $root dbref 'commands
var $root child_index 0
var $root fertile 0
var $root manager $commands
var $root owned [$commands]
var $root owners [$]
var $root writable []
var $root readable ['parameters, 'methods, 'code]
var $root inited 1
var $commands commands #[["@cmd", [["@cmd", "* on *", "@cmd <this> on <any>", 'foo, 'local, #[[1, ["this", []]], [2, ["any", []]]]]]]]
var $commands shortcuts #[]

method add_command
    arg template, method, type;
    var cmd, tmp, x, loc, types;
    
    (> .perms(sender()) <);
    
    // ----- parse it first
    tmp = template.explode_delimited("<", ">");
    loc = [];
    cmd = tmp[1];
    types = tmp[2];
    
    // Clean up cmd and set locations correctly
    for x in [1 .. listlen(cmd)] {
        if (type(cmd[x]) == 'string) {
            cmd = replace(cmd, x, (cmd[x]).trim());
        } else {
            loc = [@loc, cmd[x]];
            cmd = replace(cmd, x, "*");
        }
    }
    cmd = [cmd[1], (cmd.sublist(2)).to_string()];
    
    // parse command argument types
    for x in [1 .. listlen(types)]
        types = replace(types, x, (> $command.get_argument_type(types[x]) <));
    
    // generate the types correctly
    tmp = #[];
    for x in [1 .. listlen(loc)]
        tmp = tmp.add(loc[x], types[x]);
    types = tmp;
    
    // validate type
    if (!(type in ['local, 'remote]))
        throw(~type, "Command types can be either 'local or 'remote");
    
    // add it
    ._add_command(@cmd, template, method, type, types);
.

method _add_command
    arg cmd, cmd_args, tmpl, method, type, types;
    
    (> .perms(sender(), 'this) <);
    commands = commands.add_elem(cmd, [cmd, cmd_args, tmpl, method, type, types]);
.

method _del_command
    arg cmd, cmd_args, [args];
    var cinfo, c;
    
    (> .perms(sender(), 'this) <);
    cinfo = (| commands[cmd] |) || throw(~command, (("Command " + cmd) + " is not defined on ") + (.dbref()));
    for c in (cinfo) {
        if ((c[2]) == cmd_args)
            cinfo = cinfo.del(c);
    }
    if (!cinfo)
        commands = commands.del(cmd);
    else
        commands = commands.add(cmd, cinfo);
.