/
CDC-1.2b/
CDC-1.2b/src/
parent $user_interfaces
object $command_aliases

var $root child_index 0
var $root owners [$command_aliases]
var $root owned [$command_aliases]
var $root fertile 0
var $has_commands commands []
var $has_commands shortcuts []
var $root inited 1
var $command_aliases command_aliases []
var $root manager $command_aliases
var $root writable [$command_aliases]
var $root readable ['parameters, 'methods, 'code]
var $root dbref 'command_aliases
var $old_command_environment verb_cache #[]
var $old_command_environment command_cache []
var $old_command_environment shortcuts_cache []

method init_command_aliases
    if (caller() != $root)
        throw(~perm, "Caller is not $root.");
    command_aliases = [];
.

method uninit_command_aliases
    if (caller() != $root)
        throw(~perm, "Caller is not $root.");
    command_aliases = [];
.

method command_aliases
    return command_aliases;
.

method all_command_aliases
    var user, aliases, userc;
    
    // Collect complete command alias list from ancestors.
    aliases = [];
    for user in (.ancestors()) {
        userc = (| user.command_aliases() |);
        if (userc)
            aliases = aliases + userc;
        if (user == definer())
            break;
    }
    return aliases;
.

method match_command_aliases
    arg str;
    var alias, argf, match, newstr;
    
    // attempts to rebuild the string for an alias.
    if (sender() != this())
        throw(~perm, "Sender is not this.");
    for alias in (.all_command_aliases()) {
        match = match_pattern(alias[1], str);
        if (match != 0) {
            newstr = alias[2];
            for argf in [1 .. listlen(match)]
                newstr = strsub(newstr, "%" + tostr(argf), match[argf]);
            return newstr;
        }
    }
    return str;
.

method add_command_alias
    arg alias, actual;
    var index, a;
    
    (> .perms(sender()) <);
    if ((type(alias) != 'string) || (type(actual) != 'string))
        throw(~type, "alias and actual are not strings.");
    while ("%" in alias) {
        alias = strsub(alias, "%" + tostr(index), "*");
        index = index + 1;
    }
    
    // have it 'replace' the old alias (if one exists) by first removing
    // the old one, and adding the new one later.
    for a in (command_aliases) {
        if ((a[1]) == alias)
            command_aliases = setremove(command_aliases, a);
    }
    command_aliases = [@command_aliases, [alias, actual]];
.

method del_command_alias
    arg alias;
    var ca;
    
    (> .perms(sender()) <);
    if (type(alias) != 'string)
        throw(~type, "alias is not a string.");
    for ca in (command_aliases) {
        if ((ca[1]) == alias) {
            command_aliases = setremove(command_aliases, ca);
            return;
        }
    }
    throw(~aliasnf, ("alias `" + alias) + "' is not found");
.