ds3.0/bin/
ds3.0/extra/
ds3.0/extra/crat/
ds3.0/extra/creremote/
ds3.0/extra/mingw/
ds3.0/extra/wolfpaw/
ds3.0/fluffos-2.18-ds07/
ds3.0/fluffos-2.18-ds07/Win32/
ds3.0/fluffos-2.18-ds07/compat/
ds3.0/fluffos-2.18-ds07/compat/simuls/
ds3.0/fluffos-2.18-ds07/testsuite/
ds3.0/fluffos-2.18-ds07/testsuite/clone/
ds3.0/fluffos-2.18-ds07/testsuite/command/
ds3.0/fluffos-2.18-ds07/testsuite/data/
ds3.0/fluffos-2.18-ds07/testsuite/etc/
ds3.0/fluffos-2.18-ds07/testsuite/include/
ds3.0/fluffos-2.18-ds07/testsuite/inherit/
ds3.0/fluffos-2.18-ds07/testsuite/inherit/master/
ds3.0/fluffos-2.18-ds07/testsuite/log/
ds3.0/fluffos-2.18-ds07/testsuite/single/
ds3.0/fluffos-2.18-ds07/testsuite/single/tests/compiler/
ds3.0/fluffos-2.18-ds07/testsuite/single/tests/efuns/
ds3.0/fluffos-2.18-ds07/testsuite/single/tests/operators/
ds3.0/fluffos-2.18-ds07/testsuite/u/
ds3.0/fluffos-2.18-ds07/tmp/
ds3.0/lib/cmds/admins/
ds3.0/lib/cmds/common/
ds3.0/lib/cmds/creators/include/
ds3.0/lib/daemon/services/
ds3.0/lib/daemon/tmp/
ds3.0/lib/doc/
ds3.0/lib/doc/bguide/
ds3.0/lib/doc/efun/all/
ds3.0/lib/doc/efun/arrays/
ds3.0/lib/doc/efun/buffers/
ds3.0/lib/doc/efun/compile/
ds3.0/lib/doc/efun/floats/
ds3.0/lib/doc/efun/functions/
ds3.0/lib/doc/efun/mixed/
ds3.0/lib/doc/efun/numbers/
ds3.0/lib/doc/efun/parsing/
ds3.0/lib/doc/help/classes/
ds3.0/lib/doc/help/races/
ds3.0/lib/doc/lfun/
ds3.0/lib/doc/lfun/all/
ds3.0/lib/doc/lfun/lib/abilities/
ds3.0/lib/doc/lfun/lib/armor/
ds3.0/lib/doc/lfun/lib/bank/
ds3.0/lib/doc/lfun/lib/bot/
ds3.0/lib/doc/lfun/lib/clay/
ds3.0/lib/doc/lfun/lib/clean/
ds3.0/lib/doc/lfun/lib/clerk/
ds3.0/lib/doc/lfun/lib/client/
ds3.0/lib/doc/lfun/lib/combat/
ds3.0/lib/doc/lfun/lib/connect/
ds3.0/lib/doc/lfun/lib/container/
ds3.0/lib/doc/lfun/lib/corpse/
ds3.0/lib/doc/lfun/lib/creator/
ds3.0/lib/doc/lfun/lib/daemon/
ds3.0/lib/doc/lfun/lib/damage/
ds3.0/lib/doc/lfun/lib/deterioration/
ds3.0/lib/doc/lfun/lib/donate/
ds3.0/lib/doc/lfun/lib/door/
ds3.0/lib/doc/lfun/lib/equip/
ds3.0/lib/doc/lfun/lib/file/
ds3.0/lib/doc/lfun/lib/fish/
ds3.0/lib/doc/lfun/lib/fishing/
ds3.0/lib/doc/lfun/lib/flashlight/
ds3.0/lib/doc/lfun/lib/follow/
ds3.0/lib/doc/lfun/lib/ftp_client/
ds3.0/lib/doc/lfun/lib/ftp_data_connection/
ds3.0/lib/doc/lfun/lib/fuel/
ds3.0/lib/doc/lfun/lib/furnace/
ds3.0/lib/doc/lfun/lib/genetics/
ds3.0/lib/doc/lfun/lib/holder/
ds3.0/lib/doc/lfun/lib/id/
ds3.0/lib/doc/lfun/lib/interactive/
ds3.0/lib/doc/lfun/lib/lamp/
ds3.0/lib/doc/lfun/lib/leader/
ds3.0/lib/doc/lfun/lib/light/
ds3.0/lib/doc/lfun/lib/limb/
ds3.0/lib/doc/lfun/lib/living/
ds3.0/lib/doc/lfun/lib/load/
ds3.0/lib/doc/lfun/lib/look/
ds3.0/lib/doc/lfun/lib/manipulate/
ds3.0/lib/doc/lfun/lib/meal/
ds3.0/lib/doc/lfun/lib/messages/
ds3.0/lib/doc/lfun/lib/player/
ds3.0/lib/doc/lfun/lib/poison/
ds3.0/lib/doc/lfun/lib/position/
ds3.0/lib/doc/lfun/lib/post_office/
ds3.0/lib/doc/lfun/lib/potion/
ds3.0/lib/doc/lfun/lib/room/
ds3.0/lib/doc/lfun/lib/server/
ds3.0/lib/doc/lfun/lib/spell/
ds3.0/lib/doc/lfun/lib/torch/
ds3.0/lib/doc/lfun/lib/vendor/
ds3.0/lib/doc/lfun/lib/virt_sky/
ds3.0/lib/doc/lfun/lib/weapon/
ds3.0/lib/doc/lfun/lib/worn_storage/
ds3.0/lib/doc/lpc/constructs/
ds3.0/lib/doc/lpc/etc/
ds3.0/lib/doc/lpc/intermediate/
ds3.0/lib/doc/lpc/types/
ds3.0/lib/doc/misc/
ds3.0/lib/doc/old/
ds3.0/lib/doc/phints/
ds3.0/lib/domains/
ds3.0/lib/domains/Praxis/adm/
ds3.0/lib/domains/Praxis/attic/
ds3.0/lib/domains/Praxis/cemetery/mon/
ds3.0/lib/domains/Praxis/data/
ds3.0/lib/domains/Praxis/death/
ds3.0/lib/domains/Praxis/mountains/
ds3.0/lib/domains/Praxis/obj/armour/
ds3.0/lib/domains/Praxis/obj/magic/
ds3.0/lib/domains/Praxis/obj/weapon/
ds3.0/lib/domains/Praxis/orc_valley/
ds3.0/lib/domains/Ylsrim/
ds3.0/lib/domains/Ylsrim/adm/
ds3.0/lib/domains/Ylsrim/armor/
ds3.0/lib/domains/Ylsrim/broken/
ds3.0/lib/domains/Ylsrim/fish/
ds3.0/lib/domains/Ylsrim/meal/
ds3.0/lib/domains/Ylsrim/npc/
ds3.0/lib/domains/Ylsrim/obj/
ds3.0/lib/domains/Ylsrim/virtual/
ds3.0/lib/domains/Ylsrim/weapon/
ds3.0/lib/domains/campus/adm/
ds3.0/lib/domains/campus/chamber/
ds3.0/lib/domains/campus/etc/
ds3.0/lib/domains/campus/meals/
ds3.0/lib/domains/campus/txt/ai/charles/
ds3.0/lib/domains/campus/txt/ai/charles/bak2/
ds3.0/lib/domains/campus/txt/ai/charles/bak2/bak1/
ds3.0/lib/domains/campus/txt/ai/charly/
ds3.0/lib/domains/campus/txt/ai/charly/bak/
ds3.0/lib/domains/campus/txt/jenny/
ds3.0/lib/domains/cave/doors/
ds3.0/lib/domains/cave/etc/
ds3.0/lib/domains/cave/meals/
ds3.0/lib/domains/cave/weap/
ds3.0/lib/domains/default/chamber/
ds3.0/lib/domains/default/creator/
ds3.0/lib/domains/default/doors/
ds3.0/lib/domains/default/etc/
ds3.0/lib/domains/default/vehicle/
ds3.0/lib/domains/default/virtual/
ds3.0/lib/domains/town/save/
ds3.0/lib/domains/town/txt/shame/
ds3.0/lib/domains/town/virtual/
ds3.0/lib/domains/town/virtual/bottom/
ds3.0/lib/domains/town/virtual/space/
ds3.0/lib/estates/
ds3.0/lib/ftp/
ds3.0/lib/lib/comp/
ds3.0/lib/lib/daemons/
ds3.0/lib/lib/daemons/include/
ds3.0/lib/lib/lvs/
ds3.0/lib/lib/user/
ds3.0/lib/lib/virtual/
ds3.0/lib/log/
ds3.0/lib/log/adm/
ds3.0/lib/log/archive/
ds3.0/lib/log/chan/
ds3.0/lib/log/errors/
ds3.0/lib/log/law/adm/
ds3.0/lib/log/law/email/
ds3.0/lib/log/law/names/
ds3.0/lib/log/law/sites-misc/
ds3.0/lib/log/law/sites-register/
ds3.0/lib/log/law/sites-tempban/
ds3.0/lib/log/law/sites-watch/
ds3.0/lib/log/open/
ds3.0/lib/log/reports/
ds3.0/lib/log/router/
ds3.0/lib/log/secure/
ds3.0/lib/log/watch/
ds3.0/lib/obj/book_source/
ds3.0/lib/obj/include/
ds3.0/lib/powers/prayers/
ds3.0/lib/powers/spells/
ds3.0/lib/realms/template/
ds3.0/lib/realms/template/adm/
ds3.0/lib/realms/template/area/
ds3.0/lib/realms/template/area/armor/
ds3.0/lib/realms/template/area/npc/
ds3.0/lib/realms/template/area/obj/
ds3.0/lib/realms/template/area/room/
ds3.0/lib/realms/template/area/weap/
ds3.0/lib/realms/template/bak/
ds3.0/lib/realms/template/cmds/
ds3.0/lib/save/kills/o/
ds3.0/lib/secure/cfg/classes/
ds3.0/lib/secure/cmds/builders/
ds3.0/lib/secure/cmds/creators/include/
ds3.0/lib/secure/cmds/players/
ds3.0/lib/secure/cmds/players/include/
ds3.0/lib/secure/daemon/imc2server/
ds3.0/lib/secure/daemon/include/
ds3.0/lib/secure/lib/
ds3.0/lib/secure/lib/include/
ds3.0/lib/secure/lib/net/include/
ds3.0/lib/secure/lib/std/
ds3.0/lib/secure/log/adm/
ds3.0/lib/secure/log/bak/
ds3.0/lib/secure/log/intermud/
ds3.0/lib/secure/log/network/
ds3.0/lib/secure/modules/
ds3.0/lib/secure/npc/
ds3.0/lib/secure/obj/include/
ds3.0/lib/secure/room/
ds3.0/lib/secure/save/
ds3.0/lib/secure/save/backup/
ds3.0/lib/secure/save/boards/
ds3.0/lib/secure/save/players/g/
ds3.0/lib/secure/tmp/
ds3.0/lib/secure/upgrades/files/
ds3.0/lib/secure/verbs/creators/
ds3.0/lib/std/board/
ds3.0/lib/std/lib/
ds3.0/lib/verbs/admins/include/
ds3.0/lib/verbs/builders/
ds3.0/lib/verbs/common/
ds3.0/lib/verbs/common/include/
ds3.0/lib/verbs/creators/
ds3.0/lib/verbs/creators/include/
ds3.0/lib/verbs/rooms/
ds3.0/lib/verbs/rooms/include/
ds3.0/lib/www/client/
ds3.0/lib/www/errors/
ds3.0/lib/www/images/
ds3.0/win32/
string *blacklisted_muds = ({});
static string *graylisted_muds = ({});
static mapping packet_counter = ([]); 

void read_callback(mixed fd, mixed info){
    mixed tmp;
    // This is called when messages come in from a MUD.
    // Should reject all messages if they have not done a (successful) startup-req,
    // Should check to make sure the fd matches with the mud they are claiming to be, else error.
    // because all packets require an originator_mudname.

    // If target is the name of the router, then call the function in this object.
    // if target is 0, then broadcast to muds.
    // Do an error if target mudname is not known.
    // 

    // According to: http://www.intermud.org/i3/specs/formats.php3
    // Transmissions are LPC arrays with a predefined set of six initial elements:
    // ({ type, ttl, originator mudname, originator username, target mudname, target username, ... }). 
    // info[0]=type
    // info[1]=ttl
    // info[2]=originator mudname
    // info[3]=originator username
    // info[4]=target mudname
    // info[5]=target username

    string mudname;
    int i;
    string *logspam = ({ "auth-mud-req", "auth-mud-reply", "tell", "emoteto",
            "ping","channel-listen", "ping-req", "ping-reply" });
    string ip_addr = (stringp(fd) ? fd : clean_fd(socket_address(fd)));

    if(!packet_counter) packet_counter = ([]);

    if(!strsrch(info[0],"irn-")){
        irn_read_callback(fd, info);
        return;
    }

    if(stringp(fd) && member_array(info[0],logspam) == -1)

        if(info[4] && !grepp(info[0],"startup-req") && 
                member_array(info[4], keys(connected_muds)) == -1 &&
                member_array(info[2], keys(connected_muds)) == -1){
            return;
        }
    validate();
    if(sizeof(blacklisted_muds)){
        if(member_array(info[2], blacklisted_muds) != -1) return;
        if(intp(fd)){
            if(member_array(ip_addr, blacklisted_muds) != -1) return;
            foreach(string bmud in blacklisted_muds){
                if(sscanf(bmud, "%*d.%*s") && !strsrch(ip_addr, bmud)){
                    return;
                }
            }
        }
    }
    if(!packet_counter[info[2]]){
        packet_counter[info[2]] = ([ "time" : time(), "count" : 0 ]);
    }
    if(packet_counter[info[2]]["time"] != time()){
        packet_counter[info[2]]["count"] = 0;
        packet_counter[info[2]]["time"] = time();
    }
    if(info[0] != "channel-listen"){
        if(!strsrch(info[0], "startup-req")){
            packet_counter[info[2]]["count"] += 6;
        }
        else packet_counter[info[2]]["count"]++;
    }
    if(sizeof(graylisted_muds)){
        if((info[2] && member_array(info[2], graylisted_muds) != -1) ||
                (intp(fd) && member_array(ip_addr, graylisted_muds) != -1)){
            if(info[2] && member_array(info[2], keys(connected_muds)) != -1){
                if(packet_counter[info[2]]["count"] < 2){
                    send_error(info[2],info[3],"not-allowed",
                        "Your packets are being temporarily dropped "+
                        "due to flooding.", info);
                }
            }
            return;
        }
    }

    if(packet_counter[info[2]]["count"] > 10 &&
            member_array(fd, keys(ROUTER_D->query_irn_sockets())) == -1){
        if(info[2] && member_array(info[2], keys(connected_muds)) != -1){
            send_error(info[2],info[3],"not-allowed",
                    "Flood detected! Your packets will be temporarily ignored.",
                    info);
        }
        graylisted_muds += ({ info[2] });
        trr("Graylisting "+identify(info[2])+" from "+ip_addr,"red");
        return;
    }
    if(member_array(info[0],logspam) == -1 && strsrch(info[0],"irn-")){
        string foo = "IRN";
        if(intp(fd)) foo = socket_address(fd);
        trr(timestamp()+" Received from fd("+fd+"), source: "+foo+"\n"+identify(info), "green");
    }
    // Base info in a packet is of size 6.
    // comment out the statements below to avoid a big channel log file
    if(grepp(info[0],"chan") && !grepp(info[0],"channel-listen")) 
        server_log(identify(info),"chan_log");

    if(sizeof(info)<6 ||
            !stringp(info[0]) ||
            !intp(info[1]) || !stringp(info[2]) ||
            (!stringp(info[3]) && info[3]!=0) ||
            (!stringp(info[4]) && info[4]!=0) ||
            (!stringp(info[5]) && info[5]!=0) ) {
        write_data(fd,({
                    // okay, their initial data isn't fully there...
                    // careful about array out of bounds...
                    "error",5,router_name,0,
                    (sizeof(info)>=3 ? info[2] : 0), // mud name
                    (sizeof(info)>=4 ? info[3] : 0), // user name
                    "bad-pkt","Invalid initial data",info
                    }));
        return;
    }
    if(info[4]!=0 && !mudinfo[info[4]] && info[4]!=router_name){
        // if target mud is not 0 (broadcasting), not the router name,
        // and not a known MUD
        write_data(fd,({
                    "error",
                    5,
                    router_name,
                    0,
                    info[2],
                    info[3],
                    "unk-dst", // same as I3
                    "destination unknown", // same as I3
                    info
                    }));
        trr("Error [unk-dst], because target is "+info[4]+" and thus invalid.");
        return;
    }
    if(sscanf(info[0],"startup-req-%d",i)==1){
        // special condition for startup-req...
        trr("calling process_startup_req, i="+i+", fd="+fd+" which is: "+socket_address(fd));
        //call_other(this_object(),"process_startup_req",i,info,fd);
        this_object()->process_startup_req(i,info,fd);
        return;
    }
    if(intp(fd) && !undefinedp(connected_muds[info[2]]) && (connected_muds[info[2]]!=fd)){
        // MUD hasn't done a startup-req yet
        write_data(fd,({
                    "error",
                    5,
                    router_name,
                    0, // originator username
                    info[2], // mud name
                    info[3], // user name
                    "unk-src", // error code
                    "Your MUD hasn't registered as "+info[2]+" yet", // Error message
                    info
                    }));
        server_log("They have not done a startup-req for fd="+fd+", mudname="+info[2]);
        return;
    }
    // at this point, I guess it has a valid origin and stuff
    if(sscanf(info[0],"channel-%*s")==1){ // command has a "channel-" prefix
        // special case for channel stuff
        process_channel(fd,info);
        return;
    }
    if(info[0]=="chan-filter-reply"){
        if(!stringp(info[6]) || sizeof(info[7])<9 ){
            // avoid out-of-bounds
            send_error(info[2],info[3],"bad-pkt",
                    "Invalid chan-filter-reply packet.",info);
        }
        if(channels[info[6]][1]!=info[2]){
            send_error(info[2],info[3],"not-allowed",
                    "You are not the owner of "+info[6],info);
            return;
        }
        if(member_array(info[7][6],({ "channel-a", "channel-e", "channel-t"}))==-1){
            // Not a valid channel packet.
            send_error(info[2],info[3],"not-allowed","*giggles*",info);
        }
        if(info[6]!=info[7][6]){
            // They're trying to trick me into broadcasting
            // fake messages on another channel!
            // That's pretty funny :P
            // I'll have to remember to check if the official router falls for it.
            send_error(info[2],info[3],"not-allowed","Hehe!",info);
            return;
        }
        if(member_array(info[7][6],({ "channel-a", "channel-e", "channel-t"}))){
        }
        // Broadcast the message...
        if(intp(fd)) this_object()->SendMessage(info);
        foreach(mudname in keys(connected_muds)){
            if(member_array(mudname, listening[info[6]])!=-1)
                write_data(connected_muds[mudname],info[7]);
        }
        return;
    }
    if(info[4]==0){ // if broadcasting this...
        if(intp(fd)) {
            this_object()->SendMessage(info);
        }
        broadcast_data(connected_muds,info);
        return;   
    }
    if(info[4]==router_name) {
        switch(info[0]){
            case "shutdown" :
                if(sizeof(info) != 7){
                    send_error(info[2],info[3],"bad-pkt","Wrong number of elements for packet: "+info[0],info);
                    return;
                }
            if(info[6] > 604800) {
                server_log("ROUTER_D: deleting "+this_object()->query_connected_fds()[fd]+" per "+
                        "request of "+info[2]+"\n");
                remove_mud(this_object()->query_connected_fds()[fd], 1);
            }
            else {
                server_log("ROUTER_D: disconnecting "+this_object()->query_connected_fds()[fd]+" per "+
                        "request of "+info[2]);
                this_object()->disconnect_mud(this_object()->query_connected_fds()[fd]);
            }
            break;
            case "chanlist-req" : case "chanlist-request" :
                broadcast_chanlist("foo",info[2]);
            break;
            default :
            // Something meant for the router but not handled by now!
            send_error(info[2],info[3],"not-imp","Unknown command sent to router: "+info[0],info);
            server_log("%^RED%^UNHANDLED PACKET:\n"+identify(info));
        }
        return;
    }
    if(info[0]=="startup-reply" || info[0]=="chan-filter-reply"||
            info[0]=="chanlist-reply" || info[0]=="mudlist" ||
            info[0]=="bad-mojo"){
        // Thanks to Tricky for pointing out the need for this safeguard
        send_error(info[2],info[3],"not-allowed",
                "You are not allowed to send this kind of packet.",info);
        return;
    }
    // at this point, I guess you should forward it to the destination...
    if(info[0]=="locate-reply"){
        // Special case for locate-reply, because protocol 3 has a larger packet...
        if(sizeof(info)==8 && mudinfo[info[4]]["protocol"]>2){
            // originator mud is sending a protocol 1/2 response,
            // but target understands 3, so add the extra info
            write_data(connected_muds[info[4]], ({
                        info[0],info[1],info[2],info[3],info[4],info[5],
                        info[6],info[7],0,0
                        }));
            return;
        }	if(sizeof(info)==10 && mudinfo[info[4]]["protocol"]<=2){
            // target mud is being sent a protocol 3 response,
            // but only understands 1 & 2, so strip the extra info
            write_data(connected_muds[info[4]], ({
                        info[0],info[1],info[2],info[3],info[4],info[5],
                        info[6],info[7]
                        }));
            return;
        }
    }
    // if fd is an integer (meaning it's locally connected) and info[4] is zero
    // (meaning it's a boradcast) then we send data to irn. Also, if fd is an
    // integer, info[4] is non-zero, and info[4] is not a connected mud.
    if((!info[4] && intp(fd)) || 
            (intp(fd) && info[4] && undefinedp(connected_muds[info[4]])) ){
        this_object()->SendMessage(info);
    }
    tmp = (this_object()->query_connected_muds()[info[4]] || -1);
    if(tmp > -1){
        write_data(connected_muds[info[4]], info);
    }
    return;
}