textdump

./ColdCore-3.0a7/textdump · 1.6 MB

From the archive ColdCore3.0a7.tar.gz

Download the archive   « Back to the archive contents

object $root;

var $root manager = $root;
var $root trusted = [];
var $root child_index = 21;
var $root fertile = 1;
var $root inited = 0;
var $root quota = 75000;
var $root managed = 0;
var $root writers = [$root];
var $root created_on = 0;
var $root flags = ['methods, 'code, 'fertile, 'core, 'variables];
var $root quota_exempt = 0;
var $root writes = 0;
var $root trusted_by = 0;
var $root help_node = 0;
var $root settings = 0;
var $root defined_settings = #[];

root method .init_root(): nooverride {
    .change_manager(this());
    flags = ['variables, 'methods, 'code];
    created_on = time();
};

root method .uninit_root(): nooverride {
    var obj;
    
    (| manager.del_managed_obj() |);
    catch any {
        for obj in (.managed())
            obj.change_manager($reaper);
    }
    catch any {
        for obj in (.writers('literal))
            .del_writer(obj);
    }
    catch any {
        for obj in (.writes())
            obj.del_writer(this());
    }
    catch any {
        for obj in (.trusted('literal))
            .del_trusted(obj);
    }
    catch any {
        for obj in (.trusted_by())
            obj.del_trusted(this());
    }
    
    // tell $sys we are going away
    $sys.sender_going_away();
};

public method .initialize(): nooverride {
    var ancestors, pos, len, method, a, def;
    
    if ((caller() != $sys) && (sender() != this()))
        throw(~perm, "Caller is not $sys and sender is not this.");
    if (inited)
        throw(~perm, "Already initialized.");
    ancestors = ancestors();
    len = ancestors.length();
    for pos in [0 .. len - 1] {
        refresh();
        a = ancestors[len - pos];
        if (!(method = (| tosym("init_" + tostr(a.objname())) |)))
            continue;
        if ((def = (| find_method(method) |))) {
            if (def != a) {
                (| (def.manager()).tell(((("Uninitialization method for " + a) + " in wrong place (") + find_method(method)) + ")") |);
            } else {
                catch any {
                    .(method)();
                } with {
                    if (def) {
                        (| (def.manager()).tell(((("UNINIT ERROR " + this()) + "<") + def) + ">:") |);
                        (| (def.manager()).tell_traceback(traceback()) |);
                    }
                }
            }
        }
    }
    inited = 1;
};

public method .uninitialize(): nooverride {
    var a, v, d, def, method, objs;
    
    (> .perms(caller(), $root, $sys) <);
    objs = (.descendants()) + [this()];
    for a in (ancestors()) {
        pause();
        if (!(method = tosym("uninit_" + tostr(a.objname()))))
            continue;
        if ((def = (| find_method(method) |))) {
            if (def != a) {
                (| (def.manager()).tell(((("Uninitialization method for " + a) + " in wrong place (") + find_method(method)) + ")") |);
                continue;
            }
            for d in (objs) {
                pause();
                catch any {
                    d.(method)();
                } with {
                    if (def) {
                        (| (def.manager()).tell(((("UNINIT ERROR " + this()) + "<") + def) + ">:") |);
                        (| (def.manager()).tell_traceback(traceback()) |);
                        (| sender().tell_traceback(traceback()) |);
                    }
                }
            }
            $sys.clear_definer_vars(a, objs);
        }
    }
};

public method .perms(): nooverride {
    arg what, @args;
    var flag;
    
    if (!args)
        args = ['writer];
    if (type(args[1]) == 'symbol) {
        switch (args[1]) {
            case 'system:
                if (!($sys.is_system(what)))
                    throw(~perm, ("Permission Denied: " + what) + " is not of the system.", what);
            case 'manager:
                if (((.manager()) != what) && (!($sys.is_system(what))))
                    throw(~perm, ("Permission Denied: " + what) + " is not the manager.", what);
            case 'trusts:
                if (!(.trusts(what)))
                    throw(~perm, ("Permission Denied: " + what) + " is not a trustee.", what);
            case 'command:
                if ((what != $user) && (what != $body))
                    throw(~perm, what + " is not a valid command invoking object.");
            default:
                if (!(.is_writable_by(what)))
                    throw(~perm, ("Permission Denied: " + what) + " is not a writer.", what);
        }
    } else if (type(what) == 'objnum) {
        if (!(what in args))
            throw(~perm, (what + " is not one of: ") + ((args.mmap('namef, 'ref)).to_english()), what);
    }
};

public method .chparents() {
    arg @parents;
    var parent, cur;
    
    if (!(| .perms(sender(), 'manager) |))
        (> .perms(caller(), $root, $sys) <);
    if (!parents)
        throw(~noparents, "There must be at least 1 parent for each object.");
    
    // Notify new parents of impending change.
    cur = parents();
    for parent in (parents) {
        if (!(parent in cur))
            (> parent.will_inherit(sender()) <);
    }
    
    // Everything's okay, go ahead and try it.
    .change_parents(parents);
};

public method .will_inherit(): nooverride {
    arg who;
    
    if (this() in (sender().parents()))
        throw(~perm, ((sender() + " already has ") + this()) + " as a parent.");
    if ((!(.has_flag('fertile, sender()))) && (!(.trusts(who))))
        throw(~perm, ((this() + " refuses to be parent of ") + sender()) + ".");
};

public method .manager(): nooverride {
    return manager || $reaper;
};

public method .writers(): nooverride {
    arg @literal;
    
    if (literal)
        return writers || [];
    
    // for speed, just add rather than setadd, use .compress() later if necessary
    return (writers || []) + [.manager(), this()];
};

public method .trusted(): nooverride {
    arg @literal;
    
    if (literal)
        return trusted || [];
    return (trusted || []) + (.writers());
};

public method .is_writable_by(): nooverride {
    arg obj;
    
    return (| obj in (.writers()) |) || ($sys.is_system(obj));
};

public method .trusts(): nooverride {
    arg obj;
    
    return (| obj in (.trusted()) |) || ((obj == $scheduler) || ($sys.is_system(obj)));
};

public method .change_manager(): nooverride {
    arg new;
    var old;
    
    if ((caller() != definer()) && (!($sys.is_system(sender()))))
        throw(~perm, "You must have system privileges to change the manager.");
    if (type(new) != 'objnum)
        throw(~invarg, "Managers must be given as a single dbref.");
    old = .manager();
    manager = new;
    old.del_managed_obj();
    new.add_managed_obj();
};

public method .add_writer(): nooverride {
    arg obj;
    
    (> .perms(sender(), 'manager) <);
    writers = (writers || []).setadd(obj);
    obj.add_writable_obj();
};

public method .del_writer(): nooverride {
    arg obj;
    
    (caller() == definer()) || (> .perms(sender(), 'manager) <);
    writers = (writers || []).setremove(obj);
    if (!writers)
        (| clear_var('writers) |);
    obj.del_writable_obj();
};

public method .add_trusted(): nooverride {
    arg obj;
    
    (caller() == definer()) || (> .perms(sender(), 'manager) <);
    trusted = (trusted || []).setadd(obj);
    obj.add_trusted_obj();
};

public method .del_trusted(): nooverride {
    arg obj;
    
    (> .perms(sender(), 'manager) <);
    trusted = (trusted || []).setremove(obj);
    if (!trusted)
        clear_var('trusted);
    obj.del_trusted_obj();
};

public method .as_this_run() {
    arg obj, method, args;
    
    if (caller() != $scheduler)
        throw(~perm, "Sender not allowed to gain access to object perms.");
    return (> obj.(method)(@args) <);
};

public method .add_parent() {
    arg parent;
    
    (> .perms(sender(), 'manager) <);
    (> parent.will_inherit(sender()) <);
    if (.has_ancestor(parent))
        throw(~perm, ((sender() + " already has ") + this()) + " as an ancestor.");
    .change_parents(parents() + [parent]);
};

public method .del_parent() {
    arg parent;
    var parents;
    
    (> .perms(sender(), 'manager) <);
    if (!valid(parent))
        throw(~type, "Not a valid parent, must send a valid object.");
    parents = .parents();
    if (!(parent in parents))
        throw(~parentnf, ((parent + " is not a parent of ") + this()) + ".");
    parents = parents.setremove(parent);
    (> .change_parents(parents) <);
};

public method .spawn(): nooverride {
    arg @suffix;
    var obj, tmp, objname, mngr, na;
    
    (> .will_spawn(sender()) <);
    suffix = (> .get_obj_suffix(@suffix) <);
    return $sys.spawn_sender(suffix, sender());
};

public method .destroy(): nooverride {
    // This doesn't actually destroy us immediately, but we will go away when
    // nothing is holding onto us any more.
    (> .perms(sender(), 'manager) <);
    if (.has_flag('core))
        throw(~perm, "This object is a core object, and cannot be destroyed!");
    (> .uninitialize() <);
    destroy();
};

public method .add_var() {
    arg name, @args;
    var tmp, kid;
    
    (> .perms(sender()) <);
    if (!(tostr(name).valid_ident()))
        throw(~invident, name + " is not a valid ident.");
    (> add_var(name) <);
    
    // The following code is a kludge as we can't send a default value to the
    // primitive.
    if (args) {
        tmp = tosym((("__set_" + tostr(name)) + "_") + time());
        catch any {
            add_method([((tostr(name) + " = ") + toliteral(args[1])) + ";"], tmp);
            .(tmp)();
            if (((args.length()) > 1) && ((args[2]) == 'inherit)) {
                for kid in (.descendants())
                    kid.(tmp)();
            }
            del_method(tmp);
        } with {
            del_method(tmp);
            rethrow(error());
        }
    }
    
    // $#Edited: 08 Jan 96 09:34 Lynx ($lynx)
};

public method .variables(): nooverride {
    return variables();
};

public method .del_var(): nooverride {
    arg name;
    var n, obj;
    
    (caller() == definer()) || (> .perms(sender()) <);
    
    // try and clear the variable on all of the descendants, before deleting
    // the variable...
    (> .clear_variable(name) <);
    
    // now delete the variable
    (> del_var(name) <);
};

public method .del_method() {
    arg name;
    
    (> .perms(sender()) <);
    (> del_method(name) <);
};

public method .methods(): nooverride {
    if (!(.has_flag('methods, sender())))
        throw(~perm, (sender() + " doesn't have permission to find methods on ") + this());
    return methods();
};

public method .parents(): nooverride {
    return parents();
};

public method .children(): nooverride {
    return children();
};

public method .ancestors(): nooverride {
    return ancestors();
};

public method .find_method(): nooverride {
    arg name;
    
    if (!(.has_flag('methods, sender())))
        throw(~perm, (sender() + " doesn't have permission to find methods on ") + this());
    return (> find_method(name) <);
};

public method .find_next_method(): nooverride {
    arg name, after;
    
    if (!(.has_flag('methods, sender())))
        throw(~perm, (sender() + " doesn't have permission to find methods on ") + this());
    return (> find_next_method(name, after) <);
};

private method .corify_descendants_of(): nooverride {
    arg obj;
    var d, name, l;
    
    name = (| tosym("core_" + (obj.objname())) |);
    catch ~methodnf {
        if ((> obj.find_method(name) <) != obj) {
            $sys.log(((("** Coremethod for " + obj) + " in wrong place (on ") + (obj.find_method(name))) + ") **");
            return;
        }
    } with {
        return;
    }
    (| obj.(name)() |);
    for d in (obj.descendants()) {
        catch any {
            (> d.(name)() <);
        } with {
            $sys.log(((("ERROR encountered in " + d) + ".") + name) + "():");
            for l in ($parse_lib.traceback(traceback()))
                $sys.log(l);
        }
        refresh();
    }
};

public method .add_method() {
    arg code, name;
    var l, m, line;
    
    (> .perms(sender()) <);
    (| $sys.touch() |);
    
    // check for a few things only admins can do
    if (!(sender().is($admin))) {
        for l in [1 .. listlen(code)] {
            line = code[l];
            if ((m = line.match_regexp("[^a-z0-9_.]anticipate_assignment\("))) {
                (> sender().tell($code_lib.point_to_line("ERROR: call to anticipate_assignment()", ((m[1])[1]) + 2, ((m[1])[2]) - 2, l, line)) <);
                throw(~perm, "anticipate_assignment() may only be used by an administrator.");
            }
        }
    }
    return (> add_method(code, name) <);
};

public method .has_ancestor(): nooverride {
    arg obj;
    
    return (> has_ancestor(obj) <);
};

public method .eval(): nooverride {
    arg code, @dest;
    var errors, result, method;
    
    dest = dest ? dest[1] : this();
    if (!(sender() in [$scheduler, $root]))
        (> .perms(sender()) <);
    
    // Compile the code.
    method = tosym("tmp_eval_" + tostr(time()));
    errors = .add_method(code, method);
    if (errors)
        return ['errors, errors, 0, 0];
    
    // Evaluate the expression.  Be sure to remove it afterwards, so that no
    // one else can call it.
    catch any {
        result = (> dest.(method)() <);
    } with {
        (| del_method(method) |);
        rethrow(error());
    }
    (| del_method(method) |);
    return ['result, result];
    
    // $#Edited: 06 Jan 96 14:58 Lynx ($lynx)
};

public method .method_info(): nooverride {
    arg @args;
    
    return (> method_info(@args) <);
};

public method .data(): nooverride {
    arg @parent;
    var par, data, out;
    
    if (!(.has_flag('variables, sender())))
        throw(~perm, ((sender().namef('ref)) + " is not allowed to read variables on ") + (.namef('ref)));
    if (parent) {
        if (type(parent[1]) != 'objnum)
            throw(~type, (parent[1]) + " is not an object.");
        return (> data(parent[1]) <);
    } else {
        data = (> data() <);
        out = #[];
        for par in (data) {
            // if the parent doesn't exist anymore, just let them see the data.
            if ((!valid(par[1])) || ((par[1]).has_flag('variables, sender())))
                out = out.add(par[1], par[2]);
            else
                out = out.add(par[1], ["*** Permission Denied ***"]);
        }
        return out;
    }
};

public method .size(): nooverride {
    arg @args;
    
    [(args ?= 'int)] = args;
    switch (args) {
        case 'string:
            return tostr(size());
        case 'english:
            return size().to_english();
        default:
            return size();
    }
};

public method .descendants(): nooverride {
    var kids, i, c, child, d;
    
    d = #[];
    for child in (children())
        d = dict_add(d, child, 1);
    while ((| (c = dict_keys(d)[++i]) |)) {
        for child in (c.children()) {
            pause();
            d = dict_add(d, child, 1);
        }
        pause();
    }
    return dict_keys(d);
};

public method .get_quota(): nooverride {
    return quota;
};

public method .quota_valid(): nooverride {
    if (quota_exempt)
        return 1;
    if (!(.is($user)))
        return 0;
    return (.quota_byte_usage()) < (.quota());
};

public method .match_children() {
    arg string;
    var children, child_names, c;
    
    children = .children();
    child_names = children.mmap('name);
    
    // direct matches first.
    for c in (child_names) {
        if (c == string)
            return children[c in child_names];
    }
    
    // ok, try partial matches
    for c in (child_names) {
        if ($string.match_begin(c, string))
            return children[c in child_names];
    }
    return 0;
};

public method .del_flag(): nooverride {
    arg flag;
    
    (> .perms(sender(), 'manager) <);
    
    // let them add any flag they want
    flags = (flags || []).setremove(flag);
};

public method ._display_descendants() {
    arg space, checked, levels, maxlev;
    var c, anc, list, id, perms;
    
    id = ((space + this()) + " ") + ($object_lib.see_perms(this()));
    for anc in (dict_keys(checked)) {
        if (.has_ancestor(anc))
            return [];
    
        // [id + "  (above)"];
    }
    if (((.parents()).length()) > 1)
        id += " (MI)";
    list = [id];
    space += "  ";
    levels++;
    
    // check children
    if ((!maxlev) || (maxlev != levels)) {
        for c in (.children()) {
            list += c._display_descendants(space, checked, levels, maxlev);
            checked = dict_add(checked, c, 1);
            pause();
        }
    }
    return list;
};

public method ._get_method_info(): nooverride {
    arg anc, method;
    var code, lines, dis_flag, meth_args, flags, first_comment;
    
    code = anc.list_method(method);
    lines = code.length();
    if (lines > 5)
        code = code.subrange(1, 5);
    flags = anc.method_flags(method);
    if (code) {
        meth_args = regexp(code[1], "arg ([^;]);");
        if (meth_args) {
            meth_args = meth_args[1];
            code = code.delete(1);
        } else {
            meth_args = "";
        }
        if (code && ((!(code[1])) || (((code[1])[1]) == "v")))
            code = code.delete(1);
        if (code && ((!(code[1])) || (((code[1])[1]) == "v")))
            code = code.delete(1);
        first_comment = code ? (code[1]) + " " : " ";
        first_comment = (((first_comment[1]) == "/") || ((first_comment[1]) == "r")) ? first_comment : "";
    } else {
        meth_args = "";
        first_comment = "";
    }
    return [anc, method, meth_args, flags, lines, first_comment];
};

public method .namef() {
    arg type;
    
    return tostr(this());
    
    // $# Edited 28 Oct 1995 21:06 Lynx ($lynx)
};

public method .match_descendants() {
    arg string;
    var match, child;
    
    match = .match_children(string);
    if (match)
        return match;
    for child in (.children()) {
        match = child.match_descendants(string);
        if (match)
            return match;
    }
    return 0;
};

public method .debug() {
    arg @stuff;
    var x, line, mngr, meth;
    
    meth = (| (stack()[2])[3] |);
    if (meth)
        line = ((("DEBUG " + sender()) + ".") + meth) + "(): ";
    else
        line = ("DEBUG " + sender()) + ": ";
    for x in (stuff)
        line = (line + " ") + toliteral(x);
    (| (.manager()).tell(line) |);
    
    // $#Edited: 13 Jun 96 01:13 $levi
};

public method .set_quota(): nooverride {
    arg value;
    
    (> .perms(caller(), $user, @$sys.system(), $root) <);
    quota = value;
};

public method .name() {
    arg @args;
    
    return tostr(this());
};

public method .quota(): nooverride {
    return quota;
};

public method .quota_byte_usage(): nooverride {
    var total, obj;
    
    for obj in (.managed()) {
        if (!valid(obj))
            continue;
        total += obj.size();
    }
    return total;
};

public method .corify(): nooverride {
    var d;
    
    if (sender() != $sys)
        throw(~sysonly, "This should only be called by $sys.make_core().");
    
    // reverse engineer it--if coreify_<object> exists,
    // call it on this object and all of its descendants.
    for d in (.descendants()) {
        .corify_descendants_of(d);
        refresh();
    }
};

root method .change_parents(): nooverride {
    arg parents;
    var old_a, old_p, p, o, a, new_a, m, def, objs, d;
    
    // NOTE: add a system at some point for queing messages to a
    // user when they aren't connected (so the tell's below will be seen)
    if (!parents)
        throw(~noparents, "Objects must have at least 1 parent");
    
    // remember the old ancestors and parents
    old_a = ancestors();
    old_p = .parents();
    
    // build the new ancestors list by hand, so we can uninit befor changing
    new_a = [this(), @parents];
    for p in (parents)
        new_a = new_a.union(p.ancestors());
    
    // uninit any old parents
    objs = (.descendants()) + [this()];
    for a in (old_a.set_difference(new_a)) {
        pause();
        if (!(m = tosym("uninit_" + tostr(a.objname()))))
            continue;
        if ((def = (| find_method(m) |))) {
            if (def != a) {
                (| (def.manager()).tell(((("Uninitialization method for " + a) + " in wrong place (") + find_method(m)) + ")") |);
                continue;
            }
            for d in (objs) {
                pause();
                catch any {
                    d.(m)();
                } with {
                    if (def) {
                        (| (def.manager()).tell(((("UNINIT ERROR " + this()) + "<") + def) + ">:") |);
                        (| (def.manager()).tell_traceback(traceback()) |);
                    }
                }
            }
            $sys.clear_definer_vars(a, objs);
        }
    }
    
    // do the change
    (> chparents(parents) <);
    
    // init new
    for a in (ancestors().set_difference(old_a)) {
        if (!(m = tosym("init_" + tostr(a.objname()))))
            continue;
        if ((def = (| find_method(m) |))) {
            if (def != a) {
                (| (def.manager()).tell(((("Initialization method for " + a) + " in wrong place (") + find_method(m)) + ")") |);
                continue;
            }
            for d in (objs) {
                pause();
                catch any {
                    d.(m)();
                } with {
                    if (def) {
                        (| (def.manager()).tell(((("INIT ERROR " + this()) + "<") + def) + ">:") |);
                        (| (def.manager()).tell_traceback(traceback()) |);
                    }
                }
            }
        }
    }
};

public method .objname(): nooverride {
    return (> objname() <);
};

public method .set_objname() {
    arg objname;
    
    (> .perms(sender()) <);
    if (.has_flag('core))
        throw(~perm, this() + " is a core object; you cannot change its object name!");
    
    // Make sure first argument is a symbol.
    if (type(objname) != 'symbol)
        throw(~type, "New objname is not a symbol.");
    
    // Make sure everything is lowercase.
    objname = tosym(tostr(objname).lowercase());
    
    // Do nothing if objname isn't different.
    if (objname == (| objname() |))
        return;
    return (> set_objname(objname) <);
};

public method .created_on(): nooverride {
    return created_on;
};

public method .is_of(): nooverride {
    arg obj;
    
    return obj in ancestors();
};

public method .is(): nooverride {
    arg @args;
    
    if (!args)
        throw(~invarg, "Must have one argument.");
    if (type(args[1]) == 'dictionary)
        return has_ancestor(args[2]);
    return has_ancestor(args[1]);
};

public method .hname() {
    arg @args;
    
    return ((("<a href=\"/bin/display?" + this()) + "\">") + this()) + "</a>";
};

public method .add_flag(): nooverride {
    arg flag;
    
    (> .perms(sender(), 'manager) <);
    if ((flag == 'core) && (!($sys.is_system(sender()))))
        throw(~perm, "Only system objects can set the 'core flag.");
    (flag == 'fertile) && (> .perms(sender(), 'manager) <);
    
    // let them add any flag they want
    flags = (flags || []).setadd(flag);
};

public method .build_name() {
    arg @args;
    var output, type, part, rval;
    
    output = "";
    for part in (args) {
        type = type(part);
        if (type == 'list)
            output += (| .(part[1])(@part.subrange(2)) |) || "";
        else if (type == 'string)
            output += part;
    }
    return output;
    
    // $#Edited: 30 Nov 96 20:00 $miro
};

public method .clear_variable(): nooverride {
    arg name;
    var n, obj;
    
    (> .perms(sender()) <);
    n = tosym("_clear_var_" + tostr(time()));
    catch any {
        .add_method([("clear_var(" + toliteral(name)) + ");"], n);
        for obj in (.descendants()) {
            (| obj.(n)() |);
            pause();
        }
        (| del_method(n) |);
    } with {
        (| del_method(n) |);
    }
};

public method .ancestors_to(): nooverride {
    arg checked, levels, maxlev;
    var c, list;
    
    list = [this()];
    levels++;
    
    // check parents
    if ((!maxlev) || (maxlev != levels)) {
        for c in (.parents()) {
            list += [c.ancestors_to(checked, levels, maxlev)];
            checked = checked.setadd(c);
        }
    }
    return list;
};

public method .descendants_to(): nooverride {
    arg checked, levels, maxlev;
    var c, list;
    
    list = [this()];
    levels++;
    
    // check parents
    if ((!maxlev) || (maxlev != levels)) {
        for c in (.children()) {
            list += [c.descendants_to(checked, levels, maxlev)];
            checked = checked.setadd(c);
        }
    }
    return list;
};

public method .ancestry(): nooverride {
    arg gen;
    var i, out;
    
    out = [];
    if (type(gen) == 'objnum) {
        for i in (.ancestors()) {
            if (i.has_ancestor(gen))
                out += [i];
        }
        return out;
    }
    if (gen != 0) {
        for i in (.parents())
            out = out.union(i.ancestry(gen - 1));
    }
    return out;
    
    // $#Edited: 22 Nov 96 17:42 $miro
};

public method .generations(): nooverride {
    arg gen;
    var p, out;
    
    out = [this()];
    if (gen != 0) {
        for p in (.parents())
            out = out.union(p.generations(gen - 1));
    }
    return out;
};

public method .variable_info() {
    arg gen, def, fltr, match;
    var ancs, data, pp, p;
    
    if (!(.has_flag('variables, sender())))
        throw(~perm, (sender() + " cannot read variables on ") + this());
    if (def)
        ancs = [def];
    else
        ancs = .(gen[1])(gen[2]) || [this()];
    data = [];
    for pp in ((data().to_list()).reverse()) {
        if (valid(pp[1]) && ((pp[1]) in ancs)) {
            for p in (pp[2]) {
                if (tostr(p[1]).(match)(fltr) != 0)
                    data += [[pp[1], @p]];
            }
        }
    }
    return data;
    
    // $#Edited: 22 Nov 96 17:42 $miro
};

public method .method_flags(): nooverride {
    arg method;
    
    if (!(.has_flag('methods, sender())))
        throw(~perm, (sender() + " doesn't have permission to find methods on ") + this());
    return (> method_flags(method) <);
};

public method .method_access(): nooverride {
    arg method;
    
    if (!(.has_flag('methods, sender())))
        throw(~perm, (sender() + " doesn't have permission to find methods on ") + this());
    return (> method_access(method) <);
};

public method .set_method_flags(): nooverride {
    arg method, flags;
    
    if (!(.is_writable_by(sender())))
        throw(~perm, (sender() + " cannot write to ") + this());
    if (('locked in flags) && (!($sys.is_system(sender()))))
        throw(~perm, "Only administrators can set the locked method flag.");
    return (> set_method_flags(method, flags) <);
};

public method .set_method_access(): nooverride {
    arg method, state;
    
    if (!(.is_writable_by(sender())))
        throw(~perm, (sender() + " cannot write to ") + this());
    return (> set_method_access(method, state) <);
};

public method .set_flags(): nooverride {
    arg new_flags;
    
    (> .perms(sender(), 'manager) <);
    if (type(new_flags) != 'list)
        throw(~invflags, "Flags must be submitted as a list of symbols.");
    if ((!new_flags) && flags)
        return clear_var('flags);
    if (('core in new_flags) && (!($sys.is_system(sender()))))
        throw(~perm, "Only system objects can set the 'core flag.");
    ('fertile in new_flags) && (> .perms(sender(), 'manager) <);
    flags = new_flags;
};

public method .managed(): nooverride {
    return managed || [];
};

root method .add_managed_obj(): nooverride {
    managed = (managed || []).setadd(sender());
};

root method .del_managed_obj(): nooverride {
    managed = (managed || []).setremove(sender());
    if (!managed)
        clear_var('managed);
};

public method .writes(): nooverride {
    (> .perms(sender(), 'trusts) <);
    return writes || [];
};

root method .add_writable_obj(): nooverride {
    writes = (writes || []).setadd(sender());
};

root method .del_writable_obj(): nooverride {
    writes = (writes || []).setremove(sender());
    if (!writes)
        clear_var('writes);
};

public method .trusted_by(): nooverride {
    return trusted_by || [];
};

root method .add_trusted_obj(): nooverride {
    trusted_by = (trusted_by || []).setadd(sender());
};

root method .del_trusted_obj(): nooverride {
    trusted_by = (trusted_by || []).setremove(sender());
    if (!trusted_by)
        clear_var('trusted_by);
};

public method .has_flag(): nooverride {
    arg flag, @sender;
    
    [(sender ?= sender())] = sender;
    if (flag == 'core)
        return flag in (.flags());
    return (flag in (.flags())) || (.trusts(sender));
};

public method .flags(): nooverride {
    return flags || [];
};

public method .quota_exempt(): nooverride {
    return quota_exempt;
};

public method .clean_root() {
    var obj;
    
    // Called by $sys.clean_database()
    (> .perms(caller(), $sys) <);
    (| ._clean_root('trusted, 'trusted_by) |);
    (| ._clean_root('trusted_by, 'trusted) |);
    (| ._clean_root('writers, 'writes) |);
    (| ._clean_root('writes, 'writers) |);
    if (!manager) {
        manager = this();
        .change_manager($reaper);
    }
    if (managed) {
        managed = managed.valid_objects();
        for obj in (managed) {
            refresh();
            if ((obj.manager()) != this())
                managed = setremove(managed, obj);
        }
        if (!managed)
            clear_var('managed);
    }
};

public method .objnum(): nooverride {
    return objnum();
    
    // $#Edited: 06 Jan 96 13:28 Lynx ($lynx)
};

public method .list_methods() {
    arg gen, def, fltr, match;
    var ancs, a, m, i, methods;
    
    if (!(.has_flag('methods, sender())))
        throw(~perm, (sender() + " cannot view methods on ") + this());
    if (def)
        ancs = [def];
    else
        ancs = .(gen[1])(gen[2]) || [this()];
    methods = #[];
    for a in (ancs) {
        for m in (a.methods()) {
            if (tostr(m).(match)(fltr) != 0) {
                i = a.method_info(m);
                methods = methods.add_elem(i[5], [a, m, @i]);
            }
        }
    }
    return methods;
};

public method .method_bytecode() {
    arg method;
    
    throw(~wait, "this is broken right now.");
    return (> method_bytecode(method) <);
};

public method .rename_method() {
    arg now, new;
    
    (> .perms(sender()) <);
    (| $sys.touch() |);
    return (> rename_method(now, new) <);
};

public method .examine() {
    return [];
};

public method .new_descendants() {
    var kid, kids;
    
    kids = #[];
    for kid in (children()) {
        kids = kids.add(kid, 1);
        kids = kids.union(kid.new_descendants());
        pause();
    }
    return kids.keys();
    
    // $#Edited: 30 Jun 96 23:06 $jenner
};

private method ._clean_root() {
    arg v, m;
    var obj, value;
    
    if ((value = get_var(v))) {
        value = value.valid_objects();
        for obj in (value) {
            if (!(this() in obj.(m)()))
                value = value.setremove(obj);
            refresh();
        }
        if (value)
            set_var(v, value);
        else
            clear_var(v);
    }
};

private method .set_quota_exempt(): nooverride {
    arg bool;
    
    if (bool)
        quota_exempt = 1;
    else
        (| clear_var('quota_exempt) |);
};

public method .help_node() {
    return help_node;
    
    // $#Edited: 30 Jul 96 16:45 $jenner
};

public method .set_hnode() {
    arg node;
    
    (> .perms(sender(), 'manager) <);
    help_node = node;
    
    // $#Edited: 30 Jul 96 16:46 $jenner
};

public method .list_method() {
    arg @args;
    
    if (!(.has_flag('code, sender())))
        throw(~perm, (("Method code on " + (.namef('ref))) + " is not readable by ") + (sender().namef('ref)));
    return (> list_method(@args) <);
};

public method .new() {
    arg @suffix;
    var obj, tmp, objname, mngr, na;
    
    // this difference between this and .spawn() is that you
    // can override this method to return a frob instead.
    (> .will_spawn(sender()) <);
    suffix = (> .get_obj_suffix(@suffix) <);
    return $sys.spawn_sender(suffix, sender());
};

public method .will_spawn(): nooverride {
    arg who;
    
    if (!(.has_flag('fertile, who)))
        throw(~perm, "Not fertile or readable.");
    
    // available quota?
    if (!(who.quota_valid()))
        throw(~quota, "Sender does not have the available quota");
};

public method .get_obj_suffix() {
    arg @suffix;
    var objname, tmp;
    
    // Figure out the suffix from the arguments and child index.
    [(suffix ?= 0)] = suffix;
    if (suffix) {
        // so they dont confuse child_index:
        if (suffix.is_numeric())
            throw(~perm, "You cannot specify a numeric suffix.");
    
        // so we get correct symbols & it is always lowercase:
        suffix = lowercase(strsed(suffix, "[^a-z0-9_]+", "", "g"));
    } else {
        // get the next valid objname
        objname = tostr((| .objname() |) || "unknown");
        tmp = tosym(objname);
        while ((| lookup(tmp) |)) {
            child_index++;
            tmp = tosym((objname + "_") + tostr(child_index));
        }
        suffix = tostr(child_index);
    }
    return suffix;
};

public method ._display_ancestors() {
    arg space, checked, levels, maxlev;
    var c, anc, list, id, perms;
    
    id = ((space + this()) + " ") + ($object_lib.see_perms(this()));
    for anc in (dict_keys(checked)) {
        if (.has_ancestor(anc))
            return [];
    
        // [id + "  (above)"];
    }
    if (((.parents()).length()) > 1)
        id += " (MI)";
    list = [id];
    space += "  ";
    levels++;
    
    // check parents
    if ((!maxlev) || (maxlev != levels)) {
        for c in (.parents()) {
            list += c._display_ancestors(space, checked, levels, maxlev);
            checked = dict_add(checked, c, 1);
            pause();
        }
    }
    return list;
};

public method .format_descendants() {
    arg ind, done, depth, max, yes, no, above;
    var c, anc, list, id, perms, f, show;
    
    show = 1;
    for f in (.flags()) {
        if (yes && (!(f in yes))) {
            show = 0;
            break;
        }
        if (no && (f in no)) {
            show = 0;
            break;
        }
    }
    if (show) {
        id = ((ind + this()) + " ") + ($object_lib.see_perms(this()));
        for anc in (dict_keys(done)) {
            if (has_ancestor(anc))
                return above ? [id + " (ABOVE)"] : [];
        }
        if (listlen(parents()) > 1)
            id += " (MI)";
        list = [id];
    } else {
        list = [];
    }
    ind += "  ";
    depth++;
    
    // check children
    if ((!max) || (max != depth)) {
        for c in (children()) {
            list += c.format_descendants(ind, done, depth, max, yes, no, above);
            done = dict_add(done, c, 1);
            pause();
        }
    }
    return list;
};

public method .undefine_setting(): nooverride {
    arg name;
    var d;
    
    (> .perms(sender()) <);
    if (!((.defined_settings()).contains(name)))
        throw(~setnf, (("Setting \"" + name) + "\" is not defined by ") + this());
    
    // clear it on all descendants, then us
    for d in ((.descendants()) + [this()]) {
        d._clear_setting(name);
        pause();
    }
    
    // bye bye
    defined_settings = dict_del(defined_settings, name);
    if (!defined_settings)
        clear_var('defined_settings);
};

public method .define_setting(): nooverride {
    arg name, @info;
    var i;
    
    (> .perms(sender()) <);
    if (info) {
        info = info[1];
        for i in (info) {
            if (!(> .valid_setting_attr(@i) <))
                info = dict_del(info, i[1]);
        }
    } else {
        info = #[];
    }
    if ((.all_defined_settings()).contains(name))
        throw(~setexists, ("Setting \"" + name) + "\" is already defined.");
    if (!($code_lib.valid_setting_id(name)))
        throw(~setbad, ("Setting name \"" + name) + "\" is unacceptable.");
    defined_settings = (.defined_settings()).add(name, info);
    return defined_settings[name];
};

public method .all_defined_settings(): nooverride {
    var a, all, d;
    
    (> .perms(sender()) <);
    all = #[];
    for a in (ancestors()) {
        catch any {
            for d in (dict_keys(a.defined_settings()))
                all = dict_add(all, d, a);
        }
    }
    return all;
};

public method .defined_settings(): nooverride {
    return defined_settings || #[];
};

public method .settings(): nooverride {
    (> .perms(sender()) <);
    return settings || #[];
};

public method .setting_info(): nooverride {
    arg setting;
    
    return defined_settings[setting];
    
    // $#Edited: 05 Mar 97 20:50 $brandon
};

public method .get_setting(): nooverride {
    arg name, definer;
    var i;
    
    i = definer.setting_info(name);
    if (dict_contains(i, 'access))
        (> .((i['access])[1])(name, sender(), caller(), @sublist(i['access], 2)) <);
    if (dict_contains(i, 'get))
        return (> .((i['get])[1])(name, definer, @sublist(i['get], 2)) <);
    catch any
        return settings[name];
    with
        return (> definer.get_default_setting(name) <);
};

public method .get_default_setting(): nooverride {
    arg name;
    
    return settings[name];
};

public method .clear_setting(): nooverride {
    arg name, definer;
    var info, args;
    
    (caller() == definer()) || (> .perms(sender()) <);
    info = (> definer.setting_info(name) <);
    if (dict_contains(info, 'clear)) {
        args = sublist(info['clear], 2);
        (> .((info['clear])[1])(name) <);
    } else if (settings && dict_contains(settings, name)) {
        settings = dict_del(settings, name);
        if (!settings)
            clear_var('settings);
    }
};

public method .format_setting(): nooverride {
    arg name, definer, value;
    var i, args;
    
    i = definer.setting_info(name);
    if (dict_contains(i, 'format)) {
        args = sublist(i['format], 2);
        catch ~methodnf
            return (> .((i['format])[1])(value, @args) <);
        with
            return (> $settings.((i['format])[1])(value, @args) <);
    }
    if (type(value) == 'objnum)
        return value.namef('ref);
    else
        return "" + value;
};

public method .set_setting_attr(): nooverride {
    arg name, attr, value;
    var info;
    
    (> .perms(sender()) <);
    if ((!defined_settings) || (!dict_contains(defined_settings, name)))
        throw(~setnf, (("Setting \"" + name) + "\" is not defined on ") + this());
    if (value && (!(> .valid_setting_attr(attr, value) <)))
        return;
    info = defined_settings[name];
    if (!value)
        info = dict_del(info, attr);
    else
        info = dict_add(info, attr, value);
    defined_settings = dict_add(defined_settings, name, info);
};

public method .get_setting_attr(): nooverride {
    arg name, attr;
    
    return (defined_settings[name])[attr];
};

public method .setting_definer(): nooverride {
    arg name;
    var a;
    
    for a in (ancestors()) {
        if (dict_contains(a.defined_settings(), name))
            return a;
    }
    throw(~setnf, ("Setting \"" + name) + "\" is not defined.");
};

public method .set_setting(): nooverride {
    arg name, definer, value;
    var i, args;
    
    (> .perms(sender()) <);
    i = (> definer.setting_info(name) <);
    if (dict_contains(i, 'parse)) {
        args = sublist(i['parse], 2);
        catch ~methodnf
            value = (> .((i['parse])[1])(value, @args) <);
        with
            value = (> $settings.((i['parse])[1])(value, @args) <);
    }
    if (dict_contains(i, 'set))
        (> .((i['set])[1])(name, definer, value, @sublist(i['set], 2)) <);
    else
        settings = dict_add(settings || #[], name, value);
};

public method .valid_setting_attr(): nooverride {
    arg name, value;
    
    if (!(name in ['get, 'set, 'parse, 'clear, 'format, 'access]))
        throw(~setattr, "Invalid setting attribute '" + name);
    if (!value)
        return 0;
    else if (type(value) != 'list)
        throw(~setattr, "Setting attribute must be a list");
    else if (type(value[1]) != 'symbol)
        throw(~setattr, "Setting attribute[1] is not a symbol");
    return 1;
};

private method ._clear_setting(): nooverride {
    arg name;
    
    if (settings && dict_contains(settings, name)) {
        settings = dict_del(settings, name);
        if (!settings)
            clear_var('settings);
    }
};

public method .get_local_setting(): nooverride {
    arg name, definer;
    var i;
    
    i = definer.setting_info(name);
    if (dict_contains(i, 'access))
        (> .((i['access])[1])(name, sender(), caller(), @sublist(i['access], 2)) <);
    return (| settings[name] |) || (> definer.get_default_setting(name) <);
};


object $sys: $root;

var $sys admins = [];
var $sys agents = [$root, $daemon];
var $sys startup = #[['time, 859065084], ['objects, [$login_daemon, $lag_watcher, $http_daemon, $smtp_daemon, $world]], ['heartbeat_interval, 2]];
var $sys starting = #[['quota, 75000], ['new_user_class, $admin], ['anonymous_user_class, $guest]];
var $sys server_address = ["129.123.4.76", "ice.cold.org"];
var $sys system_email_addresses = #[['default, "nobody@need.to.set.this.address"]];
var $sys core_version = "3.0a7";
var $sys validate_email_addresses = 1;
var $sys backup = #[['interval, 3600], ['last, 859064901], ['next, 859068684], ['started, 859064901]];
var $sys system = [$sys, $root];
var $sys touched = 859065152;
var $sys loggers = [$http_interface, $daemon, $user, $connection];
var $root created_on = 796268969;
var $root inited = 1;
var $root flags = ['methods, 'code, 'core, 'variables];
var $root quota_exempt = 1;
var $root manager = $sys;
var $sys bindings = #[['atomic, $sys], ['create, $sys], ['backup, $sys], ['shutdown, $sys], ['set_heartbeat, $sys], ['cancel, $scheduler], ['task_info, $scheduler], ['execute, $sys], ['bind_function, $sys], ['unbind_function, $sys], ['bind_port, $daemon], ['unbind_port, $daemon], ['open_connection, $connection], ['reassign_connection, $daemon], ['fopen, $file], ['fstat, $file], ['fchmod, $file], ['fmkdir, $file], ['frmdir, $file], ['files, $file], ['fremove, $file], ['frename, $file], ['fclose, $file], ['fseek, $file], ['feof, $file], ['fwrite, $file], ['fread, $file], ['fflush, $file], ['chparents, $root], ['destroy, $root], ['dblog, $sys], ['add_var, $root], ['del_var, $root], ['variables, $root], ['list_method, $root], ['add_method, $root], ['del_method, $root], ['method_bytecode, $root], ['methods, $root], ['rename_method, $root], ['set_method_access, $root], ['set_method_flags, $root], ['data, $root], ['del_objname, $root], ['set_objname, $root], ['suspend, $scheduler], ['resume, $scheduler], ['set_user, $user], ['config, $sys]];
var $sys blacklist = [];
var $root managed = [$sys];
var $root owned = [$sys];

public method .next_objnum(): native;

public method .version(): native;

driver method .startup() {
    arg args;
    var opt, str, obj, f;
    
    if (args && ("-clean" in args)) {
        catch any
            (> .clean_database() <);
        with
            .log($parse_lib.traceback(traceback()));
        shutdown();
        return;
    }
    catch any {
        // Bind functions for security
        for f in (bindings) {
            catch any
                bind_function(@f);
            with
                dblog((("** Unable to bind function " + (f[1])) + "() to ") + (f[2]));
        }
    
        // Set up five-second heartbeat.
        set_heartbeat(startup['heartbeat_interval]);
    
        // set the startup time, reset backup time.
        startup = startup.add('time, time());
        backup = backup.add('next, time() + (backup['interval]));
    
        // tell objects who should know, that we are up.
        if (type(args) != 'list)
            args = [];
        for obj in (startup['objects]) {
            .log(("Calling " + obj) + ".startup()");
            catch any
                (> obj.startup(@args) <);
            with
                .log($parse_lib.traceback(traceback()));
        }
    } with {
        .log(("Startup ERROR at " + ctime()) + ":");
        .log(toliteral(traceback()));
        .log($parse_lib.traceback(traceback(), -1, ""));
    }
};

public method .server_info() {
    arg what, @long;
    var tmp;
    
    switch (what) {
        case 'up_time:
            return time() - (startup['time]);
        case 'startup_time:
            return startup['time];
        case 'server_hostname:
            return $network.hostname();
        case 'server_ip:
            return $network.ip();
        case 'last_backup:
            return backup['last];
        case 'driver_version:
            tmp = .version();
            return (((((long ? "Cold Genesis " : "") + (tmp[1])) + ".") + (tmp[2])) + "p") + (tmp[3]);
        case 'core_version:
            return (long ? "ColdCore " : "") + core_version;
        default:
            throw(~unknown, "Unknown flag.");
    }
};

public method .create_user() {
    arg name, password, email, @type;
    var user;
    
    if ((!(| .perms(caller(), $login_interface) |)) && (!(| .perms(sender(), 'system) |)))
        throw(~perm, "Caller and Sender are not allowed to call this method.");
    [(type ?= 'new_user_class)] = type;
    catch any {
        user = (starting[type]).spawn(name);
        user.set_name(name);
        if (type == 'new_user_class)
            user.set_password(password);
        user.change_manager(user);
        user.set_user_info("rl-email", $user_info, email);
    } with {
        // Failed to initialize the child; destroy it.
        (| user.destroy() |);
        rethrow(error());
    }
    return user;
};

public method .admins() {
    return admins;
};

public method .is_admin() {
    arg obj;
    
    return (obj == $sys) || (obj in admins);
};

public method .shutdown() {
    var opt, str, obj;
    
    (> .perms(sender(), $sys) <);
    
    // tell startup objects that we are closing shop
    for obj in (startup['objects]) {
        .log(("Calling " + obj) + ".shutdown()");
        catch any
            (> obj.shutdown() <);
        with
            .log($parse_lib.traceback(traceback()));
    }
    return shutdown();
};

public method .spawn_sender() {
    arg suffix, manager;
    var namestr;
    
    (> .perms(caller(), $root, $sys) <);
    namestr = (tostr(sender().objname()) + "_") + suffix;
    return .create([sender()], tosym(namestr), manager);
};

public method .is_system() {
    arg obj;
    
    return obj in system;
};

public method .log() {
    arg text;
    var l;
    
    if ((!sender()) in loggers) {
        if ((!(| .perms(sender(), 'system) |)) && (!(| .perms(caller(), 'system) |)))
            throw(~perm, "Only system objects can log.");
    }
    if (type(text) == 'list) {
        for l in (text)
            .log(l);
    } else {
        dblog((("[" + ($time.format("%d %h %y %H:%M"))) + "] ") + text);
    }
};

driver method .heartbeat() {
    if (sender() != 0)
        throw(~perm, "Sender is not the server.");
    (| $scheduler.pulse() |);
    if (time() > (backup['next]))
        .do_backup(this(), 'interval);
};

public method .do_backup() {
    arg @args;
    var line, who, how, name, dirty;
    
    (> .perms(sender(), 'system) <);
    dirty = .dirty();
    [(who ?= sender()), (how ?= 'request)] = args;
    if (!valid(who))
        who = sender();
    backup = backup.add('next, time() + (backup['interval]));
    backup = backup.add('last, time());
    if ((how == 'interval) && (!dirty))
        return;
    catch any {
        name = who.namef('ref);
        .log(("BACKUP (" + name) + ") ");
        line = (("Backup started at " + ($time.format("%r"))) + " by ") + name;
        $channel_ui._broadcast('System, line);
    }
    
    // double pause will hopefully let people know about it before it happens
    pause();
    pause();
    backup = backup.add('started, time());
    (> backup() <);
};

public method .set_startup() {
    arg what, value;
    var valid;
    
    (> .perms(sender(), 'system) <);
    valid = startup.keys();
    if ((!what) in valid)
        throw(~type, "Key must be one of " + toliteral(valid));
    startup = startup.add(what, value);
};

public method .get_system_email() {
    arg what;
    var email;
    
    // email directory for system services, such as arch admins.
    email = (| system_email_addresses[what] |) || ((| system_email_addresses['default] |) || "<no email set>");
    return email;
    
    // $#Edited: 30 Nov 96 20:00 $miro
};

public method .new_admin() {
    if (caller() != $admin)
        throw(~perm, "Caller is not $admin.");
    admins = setadd(admins, sender());
};

public method .agents() {
    return agents;
};

public method .get_startup() {
    arg what;
    
    return startup[what];
};

public method .set_starting() {
    arg what, value;
    var valid;
    
    (> .perms(sender(), 'system) <);
    valid = starting.keys();
    if ((!what) in valid)
        throw(~type, "Key must be one of " + toliteral(valid));
    starting = starting.add(what, value);
};

public method .execute() {
    arg script, args, @background;
    
    (> .perms(sender(), 'system) <);
    (> execute(script, args, @background) <);
};

public method .get_starting() {
    arg what;
    
    return starting[what];
};

private method .create() {
    arg parents, name, manager;
    var new;
    
    new = create(parents);
    catch any {
        new.set_objname(name);
        new.initialize();
        new.change_manager(manager);
    } with {
        // Failed to initialize the child; destroy it.
        (| new.destroy() |);
        rethrow(error());
    }
    return new;
};

public method .system() {
    return system;
};

public method .del_system_email() {
    arg key;
    
    (> .perms(sender(), 'manager) <);
    system_email_addresses = system_email_addresses.del(key);
};

public method .add_system_email() {
    arg key, email;
    
    (> .perms(sender(), 'manager) <);
    if (type(key) != 'symbol)
        throw(~type, "Key is not a symbol.");
    if (type(email) != 'string)
        throw(~type, "Email address is not a string.");
    system_email_addresses = system_email_addresses.add(key, email);
};

public method .add_method() {
    arg code, name;
    var line;
    
    (> .perms(sender()) <);
    line = ("SYSTEM: ." + tostr(name)) + "() MODIFIED";
    line = (line + " by ") + (sender().namef('ref));
    .log(line);
    return (> pass(code, name) <);
};

public method .atomic() {
    arg @args;
    
    (> .perms(sender(), 'system) <);
    return (> atomic(@args) <);
    
    // $#Edited: 26 Sep 96 11:56 $brandon
};

public method .add_to_blacklist() {
    arg domain, duration;
    
    (> .perms(sender(), 'system) <);
    blacklist = blacklist.setadd(domain);
    if (duration > 0)
        $scheduler.add_task(duration, 'del_from_blacklist, domain);
    
    // $#Edited: 30 Sep 96 10:30 $brandon
};

public method .del_from_blacklist() {
    arg domain;
    
    (> .perms(sender(), 'system) <);
    blacklist = blacklist.setremove(domain);
    
    // $#Edited: 30 Sep 96 10:30 $brandon
};

public method .validate_email_addresses() {
    return validate_email_addresses;
};

public method .blacklist() {
    return blacklist;
    
    // $#Edited: 30 Sep 96 10:30 $brandon
};

driver method .signal() {
    arg signal;
    var line;
    
    // the driver will send the following signals:
    //   HUP  -- driver will drop out of atomic mode and will flush i/o
    //   USR2 -- driver does nothing
    //   USR1 -- driver aborts all currently executing tasks, not suggested
    //   QUIT -- For both of these the driver will shutdown after current
    //   INT     tasks finish their current execution frame.  Note: do not
    //           preempt tasks at this point as they will not resume.
    line = ("** Received Signal: " + signal) + " **";
    (| $channel_ui._broadcast('System, line) |);
    if (sig in ['QUIT, 'INT]) {
        (| $channel_ui._broadcast('all, "******************************") |);
        (| $channel_ui._broadcast('all, "** IMMINENT SERVER SHUTDOWN **") |);
        (| $channel_ui._broadcast('all, "******************************") |);
    }
};

root method .sender_going_away() {
    admins = admins.setremove(sender());
    agents = agents.setremove(sender());
    system = system.setremove(sender());
};

public method ._status(): native;

bind_native .status() ._status();

public method .status() {
    return (> ._status() <) + [backup['interval], backup['last], backup['next]];
};

private method ._loop_for_core() {
    arg code;
    var obj;
    
    $root.add_method(code, '___coretmp);
    for obj in ($root.descendants()) {
        obj.___coretmp();
        pause();
    }
    $root.del_method('___coretmp);
};

private method .initialize_core() {
    var obj;
    
    (| .clean_database() |);
    
    // reset child indices
    ._loop_for_core(["child_index = 0;"]);
};

public method .clean_database() {
    var obj, p, c, cmd, other;
    
    (> .perms(caller()) <);
    
    // cleanup some of $root's messiness
    for obj in ($root.descendants()) {
        (| obj.clean_root() |);
        refresh();
    }
    other = (((($command_cache.children()).setremove($user_interfaces)).mmap('descendants)).flatten()).compress();
    
    // purge all command caches
    for obj in ($has_commands.descendants()) {
        (| obj.purge_caches() |);
        refresh();
    }
    
    // get back the 'other' caches
    for obj in (other) {
        obj.rehash_caches();
        refresh();
    }
    
    // create user caches
    for obj in ((| $user_db.connected() |) || []) {
        for p in ((| obj.parents() |) || [])
            (| p.cache_init() |);
        refresh();
    }
    
    // create location caches
    for obj in ((| $location.descendants() |) || []) {
        for p in (obj.contents()) {
            (| obj.add_object_to_remote_cache(p) |);
            refresh();
        }
        refresh();
    }
    
    // check user info (move'em home etc)
    for obj in ($user.descendants()) {
        if ((| (obj.home()) != (obj.location()) |))
            (| obj.move_to(obj.home()) |);
        refresh();
    }
    
    // validate all locations and location content's
    for obj in ($physical.descendants()) {
        (| obj.validate_contents() |);
        if (obj.has_ancestor($located)) {
            if ((!valid(obj.location())) || (!(obj in ((obj.location()).contents()))))
                obj.move_to((| obj.home() |) || $lost_and_found);
        }
        refresh();
    }
};

public method .blacklisted() {
    arg remote;
    var t;
    
    for t in (blacklist) {
        if (match_pattern(remote, t))
            return 1;
    }
    return 0;
};

public method .add_to_system() {
    arg obj;
    
    if (!(.is_admin(sender())))
        throw(~perm, "Sender is not an admin.");
    if (!((obj in admins) || (obj in agents)))
        throw(~perm, "Object is not an agent or admin.");
    system = system.union([obj]);
};

public method .del_from_system() {
    arg obj;
    
    if (!(.is_admin(sender())))
        throw(~perm, "Sender is not an admin.");
    if (!((obj in admins) || (obj in agents)))
        throw(~perm, "Object is not an agent or admin.");
    system = system.setremove(obj);
};

public method .touch() {
    touched = time();
};

public method .touched() {
    return touched;
};

public method .backup() {
    return backup;
};

public method .dirty() {
    return touched > (backup['last]);
};

public method .do_shutdown() {
    arg time, why;
    var increments, line, name, mins;
    
    if ((!($sys.is_admin(sender()))) || (definer() != this()))
        throw(~perm, "Sender is not an admin.");
    increments = [600, 300, 180, 60];
    while (increments && (time < (increments[1])))
        increments = increments.delete(1);
    name = sender().namef('xref);
    .log(("*** SHUTDOWN called by " + name) + " ***");
    if (why) {
        why = ("*** REASON: " + why) + " ***";
        .log(why);
    }
    while (1) {
        if (!increments) {
            $channel_ui._broadcast('all, "*** SYSTEM SHUTDOWN ***");
            if (why)
                $channel_ui._broadcast('all, why);
            break;
        }
        line = "*** SYSTEM SHUTDOWN IN ";
        mins = (increments[1]) / 60;
        line = ((line + tostr(mins)) + " MINUTE") + ((mins == 1) ? "" : "S");
        line = ((line + " CALLED BY ") + name) + " ***";
        $channel_ui._broadcast('all, line);
        if (why)
            $channel_ui._broadcast('all, why);
        $scheduler.sleep(increments[1]);
        increments = increments.delete(1);
    }
    pause();
    pause();
    return .shutdown();
};

public method .loggers() {
    return loggers;
};

root method .core_sys(): nooverride {
    starting = dict_add(starting, 'new_user_class, $admin);
    system_email_addresses = #[['default, "nobody@need.to.set.this.address"]];
};

driver method .backup_done() {
    var elapsed;
    
    elapsed = time() - (backup['started]);
    backup = backup.del('started);
    catch any {
        $channel_ui._broadcast('System, "Backup completed, elapsed time " + ($time.elapsed(elapsed, 'long)));
        $channel_ui._broadcast('System, "Executing filesystem cleanup.. ");
        pause();
        pause();
        .execute("backup", []);
    }
};

private method .make_core() {
    arg ver;
    var obj, d, o, top, x, admin, tmp, name;
    
    // core rooms should be +core, and cant be destroyed
    // traverse the list inverseley, less unseen heirarchial shuffling
    d = $root.descendants();
    top = listlen(d);
    core_version = ver;
    dblog(("** " + top) + " objects, destroying all non-core objects...");
    catch any {
        for x in [1 .. top] {
            obj = d[(top - x) + 1];
            (| obj.destroy() |);
            if (valid(obj)) {
                obj.change_manager(obj);
                for o in (obj.writers('literal)) {
                    obj.del_writer(o);
                    refresh();
                }
            }
            refresh();
        }
    
        // shutdown this task so that references can have
        // a chance to clear out, on destroyed objects.
        dblog("** done, suspending for new task **");
    
        // do this the hard way, to be secure
        .add_var('TMP_HEARTBEAT_CODE, .list_method('heartbeat));
        .add_method([".finish_core();"], 'heartbeat);
    } with {
        dblog("traceback: " + traceback());
    }
};

public method .task_info() {
    arg @args;
    
    (> .perms(sender(), 'system) <);
    return (> task_info(@args) <);
    
    // $#Edited: 19 Sep 96 09:59 $brandon
};

public method .destroy_sender() {
    // potential problem spot, but sometimes its needed
    // add core definer items to the list, if you want them to call it
    (> .perms(caller(), $exit, $connection, $connection_interface) <);
    (> sender().destroy() <);
};

private method .finish_core() {
    // cleanup heartbeat
    .add_method(TMP_HEARTBEAT_CODE, 'heartbeat);
    .del_var('TMP_HEARTBEAT_CODE);
    
    // ok, finish up
    catch any {
        dblog("** corifying remaining objects");
        $root.corify();
        dblog("** cleaning database..");
        .clean_database();
        dblog("** shutting down..");
        shutdown();
    } with {
        dblog("traceback: " + traceback());
    }
};

public method .old_admin() {
    if (caller() != $admin)
        throw(~perm, "Caller is not $admin.");
    admins = setremove(admins, sender());
    system = setremove(system, sender());
};

root method .clear_definer_vars(): nooverride {
    arg definer, objs;
    var code, v, a, meth, errs, d;
    
    code = [];
    for v in (definer.variables())
        code += [("(| clear_var('" + v) + ") |);"];
    meth = tosym("_root_tmp_" + time());
    if (!(definer.add_method(code, meth))) {
        for d in (objs) {
            pause();
            (| d.(meth)() |);
        }
        (| definer.del_method(meth) |);
    }
};

public method .tmp_eval_859065108() {
    var me, here;
    
    me = $brandon;
    here = $the_pit;
    return (> .make_core("3.0a7") <);
};


new object $foundation: $root;

var $root child_index = 9;
var $root fertile = 1;
var $root trusted = [];
var $root inited = 1;
var $root created_on = 796268969;
var $root flags = ['methods, 'code, 'fertile, 'core, 'variables];
var $root manager = $foundation;
var $root managed = [$foundation];
var $root owned = [$foundation];
var $foundation edit_types = 0;
var $foundation defined_msgs = 0;
var $foundation msgs = 0;

public method .configure() {
    arg set;
    
    // This is for post-creation configuration of a VR object.  It is used
    // to interactively configure the VR aspects and behaviour of an object.
    // It should be optional, any command hooking into confingure should check
    // for a -conf?igure option first (which would imply skipping configuration).
    //
    // Overriding methods should pass() first, giving priority to their
    // ancestors.  The argument 'set' is a dictionary with symbol keys
    // defining what has been set.  Use the definer's name + "_" + setting
    // as the name of the key ("name" on $has_name would be 'has_name_name).
    // If something is already in the set dictionary, do not re-set it
    // again (for instance, if the command hooking into configure accepted
    // the name of the object on it's command line it would put
    // 'has_name_name in the dictionary and $has_name.configure() would not 
    // prompt for the name).
    //
    // "@skip" should always skip the setting, and not alter it.
    // if .prompt() returns 'aborted, throw ~abort.
    //
    (> .perms(sender()) <);
    return set;
};

public method .environment() {
    return [];
};

public method .match_environment() {
    arg str;
    var obj, env, found, match, target;
    
    if (!str)
        throw(~objnf, "No object specified.", str);
    str = str.strip_article();
    
    // Handle special cases.
    if (str in ["me", "my"])
        return this();
    else if (((str[1]) == "$") || ((str[1]) == "#"))
        return (> $object_lib.to_dbref(str) <);
    else if (str in ["it", "him", "her"])
        return (| .match_context(str) |) || throw(~context, ("I don't see " + str) + " here, do you?");
    
    // Start matching
    found = [];
    env = .environment();
    
    // special case ordinal references
    if ((match = $parse_lib.ordinal_reference(str)))
        return env.match_nth(@match);
    if ((match = $parse_lib.possessive_reference(str))) {
        if ((match[1]) == "me")
            obj = this();
        else
            obj = (> env.match_object(match[1]) <);
        if (!(obj.match_name(str))) {
            catch ~objnf, ~ambig, ~range {
                env = (| obj.contents() |) || [];
                if ((found = $parse_lib.ordinal_reference(match[2])))
                    return (> env.match_nth(@found) <);
                else
                    return (> env.match_object(match[2]) <);
            } with {
                if (error() == ~objnf)
                    throw(~objnf, (((obj.name()) + " does not have ") + ((match[2]).add_indefinite())) + ".");
                else if (error() == ~ambig)
                    throw(~ambig, ((("\"" + str) + "\" can match ") + ((((traceback()[1])[3]).mmap('namef, 'ref)).to_english("", " or "))) + ".");
                else
                    throw(~objnf, (obj.name()) + "'s what?");
            }
        }
    }
    catch ~objnf, ~ambig {
        return (> env.match_object(str) <);
    } with {
        if (error() == ~objnf)
            throw(~objnf, ("You do not see " + (str.add_indefinite())) + " anywhere.");
        throw(~ambig, ((("\"" + str) + "\" can match ") + ((((traceback()[1])[3]).mmap('namef, 'ref)).to_english("", " or "))) + ".");
    }
};

public method .local_to_environment() {
    arg obj;
    
    return obj in (.environment());
};

public method .get_edit_types() {
    return edit_types || [];
    
    // $#Edited: 07 Nov 96 13:56 $brandon
};

public method .set_edit_types(): nooverride {
    arg t;
    
    (> .perms(sender()) <);
    if (t)
        edit_types = t;
};

public method .all_edit_types(): nooverride {
    var i, l, t;
    
    l = [];
    for i in (.ancestors()) {
        if (type((| (t = i.get_edit_types()) |)) == 'list)
            l = l.union(i.get_edit_types());
    }
    return l;
};

public method .defined_msgs(): nooverride {
    return defined_msgs || #[];
};

public method .all_defined_msgs(): nooverride {
    var msgs, m, a;
    
    msgs = #[];
    for a in ([this()] + ancestors()) {
        if (a == definer())
            break;
        catch any
            msgs = dict_union(msgs, a.defined_msgs());
    }
    return msgs;
};

public method .clear_msg(): nooverride {
    arg name, @branches;
    var messages, branch, msg;
    
    (caller() != definer()) && (> .perms(sender()) <);
    messages = msgs || #[];
    if (!dict_contains(messages, name))
        return;
    if (!branches) {
        messages = dict_del(messages, name);
    } else {
        msg = messages[name];
        for branch in (branches) {
            if (dict_contains(msg, branch))
                msg = dict_del(msg, branch);
        }
        if (!msg)
            messages = dict_del(messages, name);
    }
    if (!messages)
        clear_var('msgs);
    else
        msgs = messages;
};

public method .define_msg(): nooverride {
    arg name;
    
    (> .perms(sender()) <);
    if ((.all_defined_msgs()).contains(name))
        throw(~msgexists, ("Message \"" + name) + "\" is already defined.");
    if (!($code_lib.valid_message_id(name)))
        throw(~msgbad, ("Message \"" + name) + "\" contains invalid characters.");
    defined_msgs = (.defined_msgs()).add(name, #[['branches, ["general"]]]);
};

public method .undefine_msg(): nooverride {
    arg name;
    var d;
    
    (> .perms(sender()) <);
    if (!((.defined_msgs()).contains(name)))
        throw(~msgnf, (("Message \"" + name) + "\" is not defined by ") + this());
    
    // clear it on all descendants, then us
    for d in (.descendants()) {
        d.clear_msg(name);
        pause();
    }
    .clear_msg(name);
    
    // bye bye
    defined_msgs = dict_del(defined_msgs, name);
    if (!defined_msgs)
        clear_var('defined_msgs);
};

public method .msgs(): nooverride {
    return msgs || #[];
};

public method .get_default_msg(): nooverride {
    arg name;
    var def, b;
    
    catch any {
        return msgs[name];
    } with {
        def = $compiler.compile_cml(">>NO DEFAULT<<");
        return hash b in (.get_msg_attr(name, 'branches)) to ([b, def]);
    }
};

public method .get_msg(): nooverride {
    arg name, definer;
    
    return dict_union(definer.get_default_msg(name), (| msgs[name] |) || #[]);
};

public method .get_msg_attr(): nooverride {
    arg name, attr;
    
    return (defined_msgs[name])[attr];
};

public method .set_msg_attr(): nooverride {
    arg name, attr, value;
    var attrs, branch;
    
    (> .perms(sender()) <);
    if ((!defined_msgs) || (!dict_contains(defined_msgs, name)))
        throw(~msgnf, (("Message \"" + name) + "\" is not defined on ") + this());
    if (attr == 'branches) {
        if (!value)
            value = ["general"];
        for branch in (value) {
            if (!($code_lib.valid_message_id(branch)))
                throw(~msgbad, ((("Message branch \"" + name) + ".") + branch) + "\" contains invalid characters.");
        }
    }
    if (!value)
        attrs = dict_del(defined_msgs[name], attr);
    else
        attrs = dict_add(defined_msgs[name], attr, value);
    defined_msgs = dict_add(defined_msgs, name, attrs);
};

public method .set_msg(): nooverride {
    arg name, branch, definer, value;
    var compiler, branches, msg, definer;
    
    (> .perms(sender()) <);
    compiler = (| definer.get_msg_attr(name, 'compiler) |) || $compiler;
    value = (> compiler.compile_cml(value) <);
    branch ?= "general";
    if (!(branch in (definer.get_msg_attr(name, 'branches))))
        throw(~badbranch, ((("Message branch \"" + name) + ".") + branch) + "\" is not defined.");
    msgs ?= #[];
    msg = dict_add((| msgs[name] |) || #[], branch, value);
    msgs = dict_add(msgs, name, msg);
};

public method .all_msgs() {
    arg @ms;
    var def, out, m, a, ams, av, v, b;
    
    out = #[];
    if (ms)
        ms = ms[1];
    else
        ms = msgs || #[];
    def = $compiler.compile_cml(">>NO DEFAULT<<");
    for a in ([this()] + ancestors()) {
        if (a == definer())
            break;
        catch any {
            ams = a.msgs();
            for m in (a.defined_msgs()) {
                v = (| ms[m[1]] |);
                av = (| ams[m[1]] |);
                if (v) {
                    if (av)
                        v = dict_union(av, v);
                    else
                        v = dict_union(hash b in (m[2]) to ([b, def]), v);
                } else if (av) {
                    v = av;
                } else {
                    v = #[["general", def]];
                }
                out = dict_add(out, m[1], v);
            }
        }
    }
    return out;
};

public method .msg_definer(): nooverride {
    arg name;
    var a;
    
    for a in (ancestors()) {
        if (a == definer())
            break;
        catch any {
            if ((a.defined_msgs())[name])
                return a;
        }
    }
    throw(~invmsg, ("Message \"" + name) + "\" is not defined.");
};

public method .eval_message() {
    arg name, definer, vars;
    var eval, msg;
    
    eval = (| definer.get_msg_attr(name, 'evaluator) |) || $bs_eval;
    msg = $message_frob.new_with(.get_msg(name, definer));
    vars = dict_add(vars, 'evaluator, eval);
    msg = msg.set_vars(vars);
    vars = dict_add(vars, 'time, 'pre);
    return msg.eval_ctext(vars);
};

public method .gender() {
    return $gender_neuter;
};

public method .gender_context() {
    return "it";
};


new object $has_commands: $foundation;

var $root fertile = 1;
var $root inited = 1;
var $root created_on = 796268969;
var $root flags = ['methods, 'code, 'fertile, 'core, 'variables];
var $root child_index = 6;
var $has_commands shortcuts = #[];
var $has_commands local = 0;
var $has_commands remote = 0;
var $root manager = $has_commands;
var $root managed = [$has_commands];
var $root owned = [$has_commands];

public method .add_command() {
    arg template, method, @type;
    var cmd, types, count, x;
    
    (> .perms(sender()) <);
    [(type ?= 'local)] = type;
    if ("*" in template)
        throw(~invcmd, "Invalid command, command templates cannot contain \"*\"!.");
    cmd = (> $command_lib.validate_command_template(template) <);
    if (!(type in ['local, 'remote]))
        throw(~type, "Command types can be either 'local or 'remote");
    if ('this in (((cmd[2]).values()).slice(1)))
        type = 'remote;
    if (type == 'remote) {
        for x in (((cmd[2]).values()).slice(1)) {
            if (x == 'this)
                count++;
        }
        if (!count)
            throw(~add_command, "Command type defined as remote with no <this> argument.");
        else if (count > 1)
            throw(~add_command, "More than one <this> argument specified in template.");
    }
    if (!get_var(type))
        set_var(type, #[]);
    set_var(type, get_var(type).setadd_elem((cmd[1])[1], [@cmd[1], template, method, cmd[2]]));
    
    // $#Edited: 22 Nov 96 17:42 $miro
    // $#Edited: 30 Nov 96 20:00 $miro
    // $#Edited: 30 Nov 96 21:22 $miro
};

public method .all_local_commands() {
    var cmds, a, acmds;
    
    cmds = #[];
    for a in (ancestors()) {
        if (a == definer())
            break;
        if ((acmds = (| a.local_commands() |)))
            cmds = cmds.add(a, acmds);
    }
    return cmds;
};

public method .local_commands() {
    return local || #[];
};

public method .get_command_info() {
    arg type, cmd;
    var info, a, ainfo;
    
    info = [];
    for a in (ancestors()) {
        if (a == definer())
            break;
        if ((ainfo = (| a.command_info(type, cmd) |)))
            info = info.union(ainfo);
    }
    return info;
};

public method .del_command() {
    arg template, method;
    var cmd, c, d, info, type;
    
    (> .perms(sender()) <);
    cmd = template.explode();
    if (!cmd)
        throw(~type, "Invalid template.");
    cmd = cmd[1];
    info = #[['local, .get_command_info('local, cmd)]];
    info = info.add('remote, .get_command_info('remote, cmd));
    for type in (info) {
        for c in (type[2]) {
            if (((c[3]) == template) && ((c[4]) == method)) {
                set_var(type[1], get_var(type[1]).del_elem(cmd, c));
                d++;
            }
        }
    }
    return d;
};

public method .remote_commands() {
    return remote || #[];
};

root method .init_has_commands() {
    local = (remote = (shortcuts = #[]));
};

root method .uninit_has_commands() {
    .clear_variables(@.variables());
};

public method .add_shortcut() {
    arg shortcut, template, method;
    var relation;
    
    (> .perms(sender()) <);
    if ((type(shortcut) != 'string) || (type(template) != 'string))
        throw(~type, "Both shortcut and template must be strings.");
    if (type(method) != 'symbol)
        throw(~type, "Method must be submitted as a symbol.");
    relation = (> $command_lib.parse_relation(shortcut, template) <);
    shortcut = (relation[1])[1];
    relation = (relation[2])[2];
    if (!shortcuts)
        shortcuts = #[];
    shortcuts = shortcuts.add(shortcut, [method, relation]);
};

public method .del_shortcut() {
    arg shortcut;
    var value;
    
    (> .perms(sender()) <);
    value = (| shortcuts.del(shortcut) |);
    if (type(value) != 'dictionary)
        throw(~shortcutnf, ("Shortcut \"" + shortcut) + "\" is not defined on this object.");
    shortcuts = value;
};

public method .all_shortcuts() {
    var s, a, as;
    
    s = [];
    for a in (ancestors()) {
        if (a == definer())
            break;
        if ((as = (| a.shortcuts() |)))
            s += as.to_list();
    }
    return s;
    
    // $#Edited: 22 Nov 96 17:43 $miro
};

public method .get_shortcut_info() {
    arg shortcut;
    
    return (| shortcuts[shortcut] |) || throw(~shortcutnf, ("Shortcut \"" + shortcut) + "\" is not defined on this object.", shortcut);
};

public method .shortcuts() {
    return shortcuts || #[];
};

public method .command_info() {
    arg type, cmd;
    
    return (| get_var(type)[cmd] |) || throw(~cmdnf, ("Command \"" + cmd) + "\" is not defined on this object.", cmd);
};

public method .all_remote_commands() {
    var cmds, a, acmds;
    
    cmds = #[];
    for a in (ancestors()) {
        if (a == definer())
            break;
        if ((acmds = (| a.remote_commands() |)))
            cmds = cmds.add(a, acmds);
    }
    return cmds;
};


new object $has_name: $foundation;

var $root child_index = 11;
var $root fertile = 1;
var $root inited = 1;
var $root created_on = 796268969;
var $root flags = ['methods, 'code, 'fertile, 'core, 'variables];
var $has_name name = ['normal, "named object", "a named object"];
var $root manager = $has_name;
var $has_name templates = 0;
var $root owned = [$has_name];
var $root managed = [$has_name];

public method .set_name() {
    arg new_name, @args;
    var type;
    
    (> .perms(sender()) <);
    if (new_name && ((new_name[1]) in ["$", "#"]))
        throw(~invname, "Names cannot begin with \"$\" or \"#\".");
    if (type(new_name) != 'string)
        throw(~type, "New name must be given as a string.");
    
    // this will not catch them all, but we can try.
    if (new_name.match_regexp("^(a|an|the) +"))
        throw(~bad_name, "Do not include articles in name, use +u +n or +pinstead.");
    [(type ?= 'normal)] = args;
    if (!(type in ['prop, 'normal, 'uniq]))
        throw(~invarg, "Type must be one of: 'prop, 'normal or 'uniq");
    switch (type) {
        case 'prop:
            new_name = [new_name, new_name];
        case 'uniq:
            new_name = [new_name, "the " + new_name];
        case 'normal:
            new_name = [new_name, new_name.add_indefinite()];
    }
    name = [type, @new_name];
    
    // $#Edited: 22 Nov 96 17:42 $miro
    // $#Edited: 30 Nov 96 20:00 $miro
};

public method .match_name() {
    arg str;
    var m, t;
    
    if ((m = match_begin(name[2], str)))
        return m;
    for t in (templates || []) {
        if ((m = match_template(str, t)))
            return m;
    }
    return 0;
};

public method .name() {
    arg @args;
    
    if (!name)
        return tostr(this());
    if (!args)
        return name[3];
    switch (args[1]) {
        case 'type:
            return name[1];
        case 'noarticle:
            return name[2];
        default:
            return name;
    }
};

public method .name_templates() {
    return templates || [];
};

public method .namef() {
    arg type;
    
    switch (type) {
        case 'ref:
            return (((.name()) + " (") + this()) + ")";
        case 'xref:
            return ((this() + " (") + (.name())) + ")";
        case 'name:
            return .name();
        default:
            return (> pass(type) <);
    }
    
    // $# Edited 28 Oct 1995 21:06 Lynx ($lynx)
    // $#Edited: 15 Jul 96 22:18 $jenner
};

public method .hname() {
    arg @args;
    
    return ((("<a href=\"/bin/describe?target=" + (.objname())) + "\">") + (.name())) + "</a>";
};

public method .add_name_template() {
    arg template;
    var p;
    
    (> .perms(sender()) <);
    for p in (explode(template, "|")) {
        if (match_begin(name[2], p.strip()))
            throw(~redundant, ("Redundant name template part \"" + p) + "\" already matches name.");
    }
    templates = setadd(templates || [], template);
};

public method .del_name_template() {
    arg template;
    
    (> .perms(sender()) <);
    templates = setremove(templates || [], template);
    if (!templates)
        (| clear_var('templates) |);
};

public method .init_has_name() {
    var objname;
    
    objname = tostr(.objname());
    name = ['prop, tostr(objname), tostr(objname)];
};


new object $described: $has_name, $has_commands;

var $described prose = [];
var $root inited = 1;
var $root created_on = 796268969;
var $root flags = ['methods, 'code, 'variables, 'core];
var $has_name name = ['uniq, "Generic Described Object", "the Generic Described Object"];
var $has_commands shortcuts = #[];
var $has_commands remote = #[];
var $root manager = $described;
var $root owned = [$described];
var $foundation edit_types = ["prose"];
var $root child_index = 1;
var $root managed = [$described];

root method .init_described() {
    prose = [];
};

protected method .description() {
    arg flags;
    var out, name;
    
    out = (<$ctext_frob, [[(<$format, ["subj", [], [.name()], 'do_subj]>)], #[]]>);
    if ((| flags['prose] |))
        return [out, .prose()];
    return [out];
};

public method .prose() {
    arg @no_default;
    
    return prose || (no_default ? 0 : "You see nothing special");
    
    // $#Edited: 18 Oct 96 13:13 $miro
};

public method .set_prose() {
    arg new;
    
    (> .perms(sender()) <);
    switch (type(new)) {
        case 'string, 'list:
            new = (> $compiler.compile_cml(new) <);
        case 'frob:
            // we'll let this pass by unharmed
        default:
            throw(~invalid, "Prose can be submitted as CML or Ctext");
    }
    prose = new;
};

root method .uninit_described() {
    prose = 0;
};

public method .get_description(): nooverride {
    arg @dflags;
    var flags, f;
    
    flags = #[['prose, 1], ['actor, sender()]];
    if (dflags && (type(dflags[1]) == 'dictionary)) {
        dflags = dflags[1];
        for f in (dflags.keys())
            flags = dict_add(flags, f, dflags[f]);
    }
    return .description(flags);
};

public method .edit_prose() {
    var p;
    
    (> .perms(sender()) <);
    p = .prose();
    if (type(p) == 'frob)
        p = p.uncompile();
    (> sender().invoke_editor(this(), '_edit_prose_callback, p, []) <);
    
    // $#Edited: 18 Aug 96 21:02 $jenner
};

public method ._edit_prose_callback() {
    arg text, client_data;
    
    (> .perms(sender()) <);
    .set_prose(text);
    return "Description set.";
    
    // $#Edited: 18 Aug 96 20:14 $jenner
};

public method .configure() {
    arg set;
    var p, end, ctext, s, still, type;
    
    set = (> pass(set) <);
    s = sender();
    still = ("Do you still want to describe " + (.name())) + "? [no] ";
    if (!(set.contains('described_prose))) {
        while (!end) {
            if (.is($exit))
                type = "exit ";
            else if (.is($place))
                type = "place ";
            else
                type = "";
            p = s.read((("Describe " + type) + (.name())) + ", Enter \".\" to finish or \"@abort\" to abort description.");
            if (p == 'aborted) {
                end = !(s.prompt_yesno(still, 0));
            } else {
                catch any {
                    ctext = (> $compiler.compile_cml(p) <);
                    s.tell(["You submitted the following description:", ""]);
                    s.tell(ctext);
                    s.tell("");
                    if (!(end = s.prompt_yesno("Keep this description? [yes] ")))
                        ctext = 0;
                } with {
                    s.tell(["The following CML compiler error occurred:", "  ", (traceback()[1])[2]]);
                    end = !(s.prompt_yesno(still, 0));
                }
            }
        }
        if (ctext)
            .set_prose(ctext);
        set = set.add('described_prose, 1);
    }
    return set;
};

public method .get_detail() {
    arg name;
    var details, d, matches;
    
    details = (| (.prose()).get_var('details) |);
    if (!details)
        throw(~nodetail, ("No \"" + name) + "\" in your environment.");
    matches = [];
    for d in (details.keys()) {
        if (match_begin(d, name))
            matches += [d];
    }
    if (!matches)
        throw(~nodetail, ("No \"" + name) + "\" in your environment.");
    if (listlen(matches) != 1)
        throw(~ambig, (("\"" + name) + "\" can match ") + (matches.to_english()));
    return (<$ctext_frob, [details[matches[1]], #[['this, this()]]]>);
};


new object $event_handler: $foundation;

var $root manager = $event_handler;
var $root managed = [$event_handler];
var $root flags = ['variables, 'methods, 'code, 'core];
var $root created_on = 850790316;
var $root inited = 1;
var $event_handler hooks = 0;
var $event_handler hooked = 0;
var $event_handler events = 0;

root method .uninit_event_handler() {
    var e, o, u;
    
    for u in ((events || #[]).keys())
        (| .unhook_events(u) |);
    for e in ((hooks || #[]).keys())
        (| o.event_hook_removed(e) |);
    (| (.location()).unhook_from_all() |);
};

public method .clear_event_hook() {
    arg event;
    var o;
    
    (> .perms(sender(), 'writer) <);
    if ((!hooks) || (!(hooks.contains(event))))
        return;
    for o in (hooks[event])
        (| o.event_hook_removed(event) |);
    hooks = dict_del(hooks, event);
    if (!hooks) {
        (| clear_var('hooks) |);
        (| clear_var('hooked) |);
    }
};

public method .event_hooks() {
    if (hooks)
        return dict_keys(hooks);
    return [];
};

public method .hook_into_event() {
    arg event;
    
    if (!hooks)
        hooks = #[];
    (> .will_hook(event, sender()) <);
    hooks = dict_add(hooks, event, setadd((| hooks[event] |) || [], sender()));
    hooked = (hooked || #[]).setadd_elem(sender(), event);
    (| .did_hook(event, sender()) |);
};

public method .unhook_from_event() {
    arg event;
    
    if (!hooks)
        return;
    hooks = dict_add(hooks, event, setremove(hooks[event], sender()));
    hooked = hooked.del_elem(sender(), event);
};

public method .unhook_from_all() {
    var e;
    
    if ((!hooked) || (!(hooked.contains(sender()))))
        return;
    for e in (hooked[sender()])
        hooks = dict_add(hooks, e, setremove(hooks[e], sender()));
    hooked = dict_del(hooked, sender());
};

protected method .will_hook() {
    arg event, obj;
    
};

protected method .did_hook() {
    arg event, obj;
    
};

public method .send_event() {
    arg event, @args;
    var o;
    
    // some sort of perms checking..
    if ((!hooks) || (!(hooks.contains(event))))
        return;
    for o in (hooks[event]) {
        if (!valid(o)) {
            hooks = dict_add(hooks, event, setremove(hooks[event], o));
            hooked = hooked.del_elem(o, event);
        }
        (| o.event_notify(event, sender(), @args) |);
    }
};

public method .event_notify() {
    arg event, origin, @args;
    
    if (caller() != $event_handler)
        throw(~perm, caller() + " is not $event_handler.");
};

public method .event_hook_removed() {
    arg event;
    
    (> .perms(caller(), $event_handler) <);
};

public method .deregister_event() {
    arg event, update_on;
    var value;
    
    (> .perms(sender()) <);
    if ((events.contains(update_on)) && ((events[update_on]).contains(event))) {
        value = (events[update_on]).del(event);
        if (value)
            events = events.add(update_on, value);
        else
            events = events.del(update_on);
        if (!events)
            clear_var('events);
    }
    
    // $#Edited: 21 Dec 96 13:04 $brandon
};

public method .hook_events() {
    arg type;
    var status, source, event, l, all;
    
    if (events && (events.contains(type))) {
        all = events[type];
        events = events.del(type);
        l = .location();
        for event in (all) {
            [event, [status, source]] = event;
            switch (source) {
                case 'location:
                    l.hook_into_event(event);
                case 'this:
                    .hook_into_event(event);
                default:
                    source.hook_into_event(event);
            }
            all = all.add(event, [1, source]);
        }
        events = events.add(type, all);
    }
    
    // $#Edited: 21 Dec 96 13:05 $brandon
};

public method .register_event() {
    arg event, update_on, src;
    var value, all;
    
    (> .perms(sender()) <);
    if (!events)
        events = #[];
    if ((update_on != 'move) && (update_on != 'startup))
        throw(~type, "Update on must be either 'move or 'startup");
    if (type(src) == 'symbol) {
        if ((src != 'location) && (src != 'this))
            throw(~type, "Source types must be either 'location or 'this.");
    } else if (type(src) != 'objnum) {
        throw(~type, "Source type must be either a symbol or object.");
    } else if (!(src.is($event_handler))) {
        throw(~type, ("Source " + (src.namef('ref))) + " is not an event handler.");
    }
    if (events.contains(update_on)) {
        all = events[update_on];
        if ((events[update_on]).contains(event)) {
            value = replace(all[event], 2, src);
            events = events.add(update_on, all.add(event, value));
        } else {
            events = events.add(update_on, all.add(event, [0, src]));
        }
    } else {
        events = events.add(update_on, #[[event, [0, src]]]);
    }
    
    // $#Edited: 07 Mar 97 15:45 $miro
};

public method .unhook_events() {
    arg type, @loc;
    var all, event, status, source;
    
    if (events && (events.contains(type))) {
        all = events[type];
        events = events.del(type);
        loc = loc ? loc[1] : (.location());
        for event in (all) {
            [event, [status, source]] = event;
            switch (source) {
                case 'location:
                    (| loc.unhook_from_event(event) |);
                case 'this:
                    (| .unhook_from_event(event) |);
                default:
                    (| source.hook_into_event(event) |);
            }
            all = all.add(event, [0, source]);
        }
        events = events.add(type, all);
    }
};


new object $physical: $described, $event_handler;

var $root trusted = [];
var $root inited = 1;
var $root created_on = 796268969;
var $root flags = ['methods, 'code, 'variables, 'core];
var $described prose = [];
var $physical visibility = 0;
var $has_name name = ['uniq, "Generic Physical Object", "the Generic Physical Object"];
var $root manager = $physical;
var $root owned = [$physical];
var $root defined_settings = #[["visibility", #[['get, ['visibility]], ['set, ['set_visibility]], ['parse, ['is_type, 'integer]]]]];
var $root managed = [$physical];

public method .set_visibility() {
    arg name, definer, value, @args;
    
    (> .perms(sender()) <);
    visibility = value;
};

public method .visibility() {
    arg @args;
    
    return visibility;
};

public method .is_visible_to() {
    arg whom;
    
    return (.visibility()) >= ((whom.location()).darkness());
};

public method .set_darkness() {
    arg name, definer, value, @args;
    
    (> .perms(sender()) <);
    darkness = value;
};

public method .vr_examine() {
    // $#Edited: 12 Jun 96 22:44 $lynx
};


new object $located: $physical;

var $located inited = 0;
var $located location = $lost_and_found;
var $located obvious = 1;
var $root created_on = 796268969;
var $root flags = ['methods, 'code, 'variables, 'core];
var $described prose = [];
var $has_name name = ['uniq, "Generic Located Object", "the Generic Located Object"];
var $root manager = $located;
var $root child_index = 1;
var $root managed = [$located];
var $root owned = [$located];

root method .init_located() {
    location = $void;
    location.add_sender_to_contents();
    obvious = 1;
};

public method .uninit_guest_guest() {
    if (caller() != $root)
        throw(~perm, "Caller is not root.");
    location.del_sender_from_contents();
    location = 0;
};

public method .environment() {
    return [this()] + ((location.environment()).setremove(this()));
};

public method .match_environment() {
    arg str;
    var thing, matches;
    
    if (str == "here")
        return location;
    return (> pass(str) <);
};

public method .location() {
    return location || $void;
};

public method .will_move() {
    arg mover, place;
    
};

protected method .did_move() {
    arg mover, old_place;
    
    // cleanup events  
    .unhook_events('move, old_place);
    old_place.send_event('movement, 'leave, old_place);
    location.send_event('movement, 'arrive);
    .hook_events('move);
};

public method .realm() {
    return realm;
};

root method .uninit_located() {
    (.location()).del_sender_from_contents();
};

public method .move_to() {
    arg place;
    var old;
    
    // Don't do anything if we're already here.
    if (place == location)
        return;
    if (!(place.has_ancestor($location)))
        throw(~type, "Argument is not a location.");
    
    // Notify involved parties of impending move, allowing them to throw
    // errors.
    if (!valid(location))
        location = $nowhere;
    (> .will_move(sender(), place) <);
    (> location.will_leave(place) <);
    (> place.will_arrive(location) <);
    
    // Set location.
    old = location;
    location = place;
    old.del_sender_from_contents();
    place.add_sender_to_contents();
    
    // Notify involved parties of completed move, in reverse order.
    place.did_arrive(old);
    old.did_leave(place);
    .did_move(sender(), old);
};

public method .match_environment_all() {
    arg s;
    
    if (s == "here")
        return [location, @(> pass(@args) <)];
    else
        return (> pass(s) <);
};

public method .obvious() {
    return obvious;
};

public method .set_obvious() {
    arg obv;
    
    .perms(sender());
    obvious = obv;
};

public method .realm_name() {
    arg @args;
    
    return (.location()).realm_name(@args);
};

public method .is_obvious_to() {
    arg whom;
    
    // will later do something creative here
    return 1;
};


new object $thing: $located;

var $root child_index = 219;
var $root fertile = 1;
var $root inited = 1;
var $root created_on = 796268969;
var $root flags = ['methods, 'code, 'fertile, 'variables, 'core];
var $located location = $void;
var $located obvious = 1;
var $described prose = [];
var $has_name name = ['normal, "thing", "a thing"];
var $has_commands shortcuts = #[];
var $has_commands remote = #[["@boot", [["@boot", "*", "@boot <this>", 'boot_cmd, #[[1, ['this, []]]]]]]];
var $has_commands local = #[];
var $thing lock = <$true_lock_frob, []>;
var $root settings = #[["home", $lost_and_found]];
var $root manager = $thing;
var $root owned = [$thing];
var $root defined_settings = #[["home", #[['get, ['home]], ['parse, ['parse_home]]]], ["gender", #[['get, ['gender]], ['set, ['set_gender]], ['parse, ['parse_gender_setting]]]], ["lock", #[['get, ['get_lock]], ['set, ['set_lock]], ['parse, ['parse_lock]], ['format, ['format_lock]]]]];
var $thing gender = 0;
var $root managed = [$thing];

public method .boot_cmd() {
    arg cmdstr, cmd, this;
    var loc, dest, exit;
    
    loc = .location();
    if (!(| .perms(sender(), 'manager) |)) {
        .tell((((sender().name()) + " tried to boot you from ") + (loc.name())) + "!");
        loc.announce((((((sender().name()) + " tried to boot ") + (.name())) + " from ") + (loc.name())) + "!", sender(), this());
        return ((("Only " + ((loc.manager()).name())) + " can boot people from ") + (loc.name())) + "!";
    }
    dest = .home();
    catch any {
        sender().tell(("You boot " + (.name())) + ".");
        loc.announce((((((sender().name()) + " boots ") + (.name())) + " from ") + ((.location()).name())) + ".", this(), sender());
        if ((sender().location()) != loc)
            (sender().location()).announce((((((sender().name()) + " boots ") + (.name())) + " from ") + ((.location()).name())) + ".", this(), sender());
        (> .move_to(dest) <);
    } with {
        return (traceback()[1])[2];
    }
    
    // $# Edited 05 Nov 1995 14:10 Lynx ($lynx)
};

public method .lock() {
    return lock;
};

public method .lock_cmd() {
    arg cmdstr, cmd, this;
    
    if (!(| .perms(sender()) |))
        return ((("Only " + ((.manager()).name())) + " can lock ") + (.name())) + "!";
    lock = $false_lock_frob.new();
    return "You lock " + (.name());
};

public method .lock_with_cmd() {
    arg cmdstr, cmd, this, prep, str;
    
    if (!(| .perms(sender()) |))
        return ((("Only " + ((.manager()).name())) + " can lock ") + (.name())) + "!";
    catch ~objnf, ~parse {
        lock = $lock_parser.parse(str, sender());
        return ((((("You lock " + (.name())) + " ") + prep) + " ") + (lock.lock_name('thing))) + ".";
    } with {
        switch (error()) {
            case ~objnf:
                return "Object not found in lock string.";
            case ~parse:
                return "Invalid lock string.";
        }
    }
};

public method .unlock_cmd() {
    arg cmdstr, cmd, this;
    
    if (!(| .perms(sender()) |))
        return ((("Only " + ((.manager()).name())) + " can lock ") + (.name())) + "!";
    lock = $true_lock_frob.new();
    return "You unlock " + (.name());
};

public method .will_move() {
    arg mover, place;
    
    (> pass(mover, place) <);
    if (mover.is($housekeeper))
        return;
    if (lock && ((mover != $exit) && (!(lock.try(mover)))))
        throw(~locked, ((((.name()).capitalize()) + " is locked to ") + (lock.lock_name('thing))) + ".");
    else if (!(.is_writable_by(sender())))
        throw(~move, "You cannot move " + this());
};

public method .home() {
    arg @args;
    var home;
    
    home = .get_local_setting("home", $thing);
    if (home)
        return home;
    if ((.manager()).is($user))
        return .manager();
    return $lost_and_found;
};

public method .try_lock() {
    arg mover;
    
    return lock && ((mover != $exit) && (lock.try(mover)));
};

public method .check_gender() {
    arg definer, value, @args;
    var g, gs;
    
    gs = [$gender_female, $gender_male, $gender_neuter];
    g = value in (gs.mmap('name));
    if (!g)
        throw(~set, "Gender must be one of: " + ((gs.mmap('name)).to_english("", " or ")));
    return gs[g];
};

public method .tell() {
    arg @args;
    
    // $#Edited: 23 Dec 96 15:38 $brandon
};

public method .directed_tell() {
    arg @args;
    
};

public method .parse_home() {
    arg value, @args;
    var home;
    
    home = (> .match_environment(value) <);
    if ((!(home.trusts(sender()))) && (!(home.get_setting("public-home", $place))))
        throw(~perm, ("You do not have permission to make " + (home.name())) + " your home.");
    return home;
};

public method .parse_gender_setting() {
    arg value, @args;
    var g, gs;
    
    gs = [$gender_female, $gender_male, $gender_neuter];
    g = value in (gs.mmap('name));
    if (!g)
        throw(~set, "Gender must be one of: " + ((gs.mmap('name)).to_english("", " or ")));
    return gs[g];
};

protected method .set_gender() {
    arg name, definer, value;
    
    (> .perms(sender(), 'manager) <);
    gender = value;
};

public method .gender() {
    arg @args;
    
    return gender;
};

root method .init_thing() {
    gender = $gender_neuter;
};

public method .gender_context() {
    return gender.pronoun('po);
};

root method .get_lock() {
    arg name, definer;
    
    return lock || (<$true_lock_frob, []>);
};

root method .set_lock() {
    arg name, definer, value;
    
    if (class(value) == $true_lock_frob)
        (| clear_var('lock) |);
    else
        lock = value;
};

root method .parse_lock() {
    arg value;
    
    if (value == "none")
        value = "no";
    else if (value == "self")
        value = "yes";
    return (> $lock_parser.parse(value, this()) <);
};

root method .format_lock() {
    arg value;
    var unparse;
    
    unparse = value.unparse();
    if (unparse == "yes")
        return "self";
    else if (unparse == "no")
        return "none";
    return unparse;
};


new object $command_cache: $has_commands;

var $root child_index = 3;
var $root fertile = 1;
var $root manager = $command_cache;
var $root created_on = 796605573;
var $root inited = 1;
var $root flags = ['methods, 'code, 'fertile, 'variables, 'core];
var $command_cache shortcut_cache = 0;
var $command_cache remote_cache = 0;
var $command_cache local_cache = 0;
var $has_commands shortcuts = #[];
var $root managed = [$command_cache];
var $root owned = [$command_cache];
var $command_cache interfaces = 0;

root method .uninit_command_cache() {
    var i;
    
    (| .purge_caches() |);
    for i in (interfaces || [])
        i.interface_unlink();
};

public method .rehash_caches() {
    var cmd, obj, part, element;
    
    (> .perms(sender()) <);
    (| .purge_caches() |);
    if (!(.is_command_cache())) {
        // if we are not an official 'cache', just cache commands defined on us
        for cmd in (.local_commands()) {
            for part in (cmd[2])
                .add_to_local_cache(part[1]);
        }
        shortcut_cache = (.shortcuts()).to_list();
    } else {
        // otherwise cache all defined commands
        (> .add_object_to_local_cache(this()) <);
        shortcut_cache = .all_shortcuts();
    }
    
    // remote caches are different, and HAVE to be specific to the user
    if (.is($location)) {
        for obj in ([this()] + (.contents()))
            (> .add_object_to_remote_cache(obj) <);
    }
};

public method .add_object_to_remote_cache() {
    arg obj;
    var info, thing, element, part;
    
    info = (| obj.all_remote_commands() |);
    if (info) {
        for element in (info) {
            for part in (element[2])
                .add_to_remote_cache(part[1], element[1]);
        }
    }
};

public method .purge_caches() {
    (> .perms(sender()) <);
    (| clear_var('shortcut_cache) |);
    (| clear_var('remote_cache) |);
    (| clear_var('local_cache) |);
};

private method .add_object_to_local_cache() {
    arg obj;
    var info, thing, element, part;
    
    info = (| obj.all_local_commands() |);
    if (info) {
        for element in (info) {
            for part in (element[2])
                .add_to_local_cache(part[1]);
        }
    }
    
    // $#Edited: 06 Jan 96 13:18 Lynx ($lynx)
};

protected method .add_to_remote_cache() {
    arg command, definer;
    var part, cmd, value, cmds, defs, refs;
    
    if (type(remote_cache) != 'dictionary)
        remote_cache = #[];
    
    // if this dies, it will also fail on explode_template_word
    cmd = (| (command.explode())[1] |);
    for part in (command.explode_template_word()) {
        if ((value = (| remote_cache[part] |))) {
            cmds = (value[1]).setadd(cmd);
            refs = ((| (value[2])[definer] |) || 0) + 1;
            defs = (value[2]).add(definer, refs);
            remote_cache = remote_cache.add(part, [cmds, defs]);
        } else {
            remote_cache = remote_cache.add(part, [[cmd], #[[definer, 1]]]);
        }
    }
};

protected method .add_to_local_cache() {
    arg command;
    var part, cmd;
    
    if (type(local_cache) != 'dictionary)
        local_cache = #[];
    
    // if this dies, it will also fail on explode_template_word
    cmd = (| (command.explode())[1] |);
    for part in (command.explode_template_word())
        local_cache = local_cache.setadd_elem(part, cmd);
};

public method .match_in_shortcut_cache() {
    arg str, cmd, args;
    var shortcut, match, obj, shorts;
    
    for obj in ([this()] + parents()) {
        if ((shorts = obj.shortcut_cache())) {
            for shortcut in (shorts) {
                match = match_pattern(str, shortcut[1]);
                if (match != 0)
                    return ['shortcut, [(shortc

Showing the first 100 KB of 1.6 MB. Download the archive for the full file.

Site Stats

Snippets: 1791
Codebases: 571