foundation2_fluffos_v1/
foundation2_fluffos_v1/bin/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/ChangeLog.old/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/Win32/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/compat/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/compat/simuls/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/include/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/clone/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/command/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/data/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/etc/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/include/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/inherit/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/inherit/master/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/log/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/tests/compiler/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/tests/efuns/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/single/tests/operators/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/testsuite/u/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/tmp/
foundation2_fluffos_v1/fluffos-2.9-ds2.13/windows/
foundation2_fluffos_v1/lib/cfg/
foundation2_fluffos_v1/lib/cmds/adm/
foundation2_fluffos_v1/lib/daemon/save/
foundation2_fluffos_v1/lib/daemon/services/
foundation2_fluffos_v1/lib/daemon/soul/
foundation2_fluffos_v1/lib/doc/build/
foundation2_fluffos_v1/lib/doc/classes/
foundation2_fluffos_v1/lib/doc/driver/
foundation2_fluffos_v1/lib/doc/driver/applies/
foundation2_fluffos_v1/lib/doc/driver/applies/interactive/
foundation2_fluffos_v1/lib/doc/driver/concepts/
foundation2_fluffos_v1/lib/doc/driver/driver/
foundation2_fluffos_v1/lib/doc/driver/efuns/arrays/
foundation2_fluffos_v1/lib/doc/driver/efuns/buffers/
foundation2_fluffos_v1/lib/doc/driver/efuns/calls/
foundation2_fluffos_v1/lib/doc/driver/efuns/compile/
foundation2_fluffos_v1/lib/doc/driver/efuns/filesystem/
foundation2_fluffos_v1/lib/doc/driver/efuns/floats/
foundation2_fluffos_v1/lib/doc/driver/efuns/functions/
foundation2_fluffos_v1/lib/doc/driver/efuns/general/
foundation2_fluffos_v1/lib/doc/driver/efuns/mappings/
foundation2_fluffos_v1/lib/doc/driver/efuns/numbers/
foundation2_fluffos_v1/lib/doc/driver/efuns/parsing/
foundation2_fluffos_v1/lib/doc/driver/lpc/constructs/
foundation2_fluffos_v1/lib/doc/driver/lpc/types/
foundation2_fluffos_v1/lib/doc/driver/platforms/
foundation2_fluffos_v1/lib/doc/efun/
foundation2_fluffos_v1/lib/doc/etc/
foundation2_fluffos_v1/lib/doc/faq/
foundation2_fluffos_v1/lib/doc/help/creator/
foundation2_fluffos_v1/lib/doc/help/player/
foundation2_fluffos_v1/lib/doc/lpc/basic/
foundation2_fluffos_v1/lib/doc/lpc/data_types/
foundation2_fluffos_v1/lib/doc/lpc/etc/
foundation2_fluffos_v1/lib/doc/lpc/intermediate/
foundation2_fluffos_v1/lib/doc/lpc/types/
foundation2_fluffos_v1/lib/doc/mudlib/
foundation2_fluffos_v1/lib/doc/mudlib/Features/
foundation2_fluffos_v1/lib/domains/Examples/armour/
foundation2_fluffos_v1/lib/domains/Examples/etc/
foundation2_fluffos_v1/lib/domains/Examples/npc/
foundation2_fluffos_v1/lib/domains/Examples/room/
foundation2_fluffos_v1/lib/domains/Examples/virtual/
foundation2_fluffos_v1/lib/domains/Examples/virtual/exaA/
foundation2_fluffos_v1/lib/domains/Examples/virtual/exaB/
foundation2_fluffos_v1/lib/domains/Examples/weapon/
foundation2_fluffos_v1/lib/domains/School/doc/
foundation2_fluffos_v1/lib/domains/School/room/
foundation2_fluffos_v1/lib/domains/School/room/Classes/
foundation2_fluffos_v1/lib/domains/School/room/Offices/
foundation2_fluffos_v1/lib/domains/Standard/
foundation2_fluffos_v1/lib/domains/Standard/pools/
foundation2_fluffos_v1/lib/domains/Standard/std/
foundation2_fluffos_v1/lib/domains/Standard/xtra/
foundation2_fluffos_v1/lib/include/
foundation2_fluffos_v1/lib/lib/obj/
foundation2_fluffos_v1/lib/news/
foundation2_fluffos_v1/lib/save/
foundation2_fluffos_v1/lib/secure/cfg/
foundation2_fluffos_v1/lib/secure/cmds/player/
foundation2_fluffos_v1/lib/secure/lib/
foundation2_fluffos_v1/old/
foundation2_fluffos_v1/win32/
/*    /daemon/intermud.c
 *    from the Nightmare IV LPC Library
 *    daemon handling the InterMUD-3 Protocol
 *    created by Descartes of Borg 950506
 */

#ifndef __PACKAGE_SOCKETS__
#error You should not try and load /daemon/intermud.c with no sockets package.
#else

#include <lib.h>
#include <save.h>
#include <config.h>
#include <daemons.h>
#include <network.h>
#include "intermud.h"

inherit LIB_CLIENT;

private int Password;
private class list MudList, ChannelList;
private mapping Banned;
private mixed *Nameservers;
private static int Connected, Tries;

static void create() {
    client::create();
    Connected = 0;
    Password = 0;
    Tries = 0;
    Banned = ([]);
    //Nameservers = ({ ({ "*gjs", "205.218.122.131 9000" }) });
    Nameservers = ({ ({ "*i4", "204.209.44.3 8080" }) });
    MudList = new(class list);
    ChannelList = new(class list);
    MudList->ID = -1;
    MudList->List = ([]);
    ChannelList->ID = -1;
    ChannelList->List = ([]);
    if( file_size( SAVE_INTERMUD __SAVE_EXTENSION__ ) > 0 )
      unguarded( (: restore_object, SAVE_INTERMUD, 1 :) );
    SetNoClean(1);
    SetDestructOnClose(1);
    SetSocketType(MUD);
    call_out( (: Setup :), 2);
}

static void Setup() {
    string ip;
    int port;

    if( !Nameservers || !sizeof(Nameservers) ) return;
    sscanf(Nameservers[0][1], "%s %d", ip, port);
    if( eventCreateSocket(ip, port) < 0 ) return;
    eventWrite( ({ "startup-req-3", 5, mud_name(), 0, Nameservers[0][0], 0,
		   Password, MudList->ID, ChannelList->ID, query_host_port(),
		   PORT_OOB, PORT_UDP, mudlib() + " " + mudlib_version(), 
		   mudlib() + " " + mudlib_version(), version(), "LPMud",
		   MUD_STATUS, ADMIN_EMAIL,
		   (mapping)SERVICES_D->GetServices(), ([]) }) );
}

static void eventRead(mixed *packet) {
    string *cles;
    mixed val;
    string ns, cle;
    int i, maxi;

    if( !packet || sizeof(packet) < 6 ) return; /* should send error */
    if( Banned[packet[1]] ) {
	/* build error packet, let them know why they are banned */
	return;
    }
    switch(packet[0]) {
	case "startup-reply":
	  if( sizeof(packet) != 8 ) return;  /* should send error */
	  if( !sizeof(packet[6]) ) return;
	  if( packet[6][0][0] == Nameservers[0][0] ) {
	      Nameservers = packet[6];
	      Connected = Nameservers[0][0];
	      Password = packet[7];
	      save_object(SAVE_INTERMUD);
	  }
	  else {
	      Nameservers = packet[6];
	      Setup();
	  }
	  return;
        case "mudlist":
	  if( sizeof(packet) != 8 ) return;
	  if( packet[6] == MudList->ID ) return; 
	  if( packet[2] != Nameservers[0][0] ) return;
	  MudList->ID = packet[6];
	  foreach(cle, val in packet[7]) {
	      if( !val && MudList->List[cle] != 0 ) 
		map_delete(MudList->List, cle);
	      else if( val ) MudList->List[cle] = val;
	  }
	  save_object(SAVE_INTERMUD);
	  return;
	case "channel-e":
	  SERVICES_D->eventReceiveChannelEmote(packet);
	  break;
	case "channel-m":
	  SERVICES_D->eventReceiveChannelMessage(packet);
	  break;
	case "chan-who-reply":
	  SERVICES_D->eventReceiveChannelWhoReply(packet);
	  break;
	case "chan-who-req":
	  SERVICES_D->eventReceiveChannelWhoRequest(packet);
	  break;
        case "chan-user-req":
	  SERVICES_D->eventReceiveChannelUserRequest(packet);
	  break;
        case "chanlist-reply":
//	  if( packet[6] == ChannelList->ID ) return; 
	  if( packet[2] != Nameservers[0][0] ) return;
	  ChannelList->ID = packet[6];
	  foreach(cle, val in packet[7]) { 
	      if( !val && ChannelList->List != 0 ) 
		map_delete(ChannelList->List, cle);
	      else if( val ) ChannelList->List[cle] = val;
	  } 
	  save_object(SAVE_INTERMUD);
	  SERVICES_D->eventRegisterChannels(packet[7]);
	  return;
        case "emoteto":
	  SERVICES_D->eventReceiveEmote(packet);
	  break;
	case "finger-req":
	  SERVICES_D->eventReceiveFingerRequest(packet);
	  break;
	case "finger-reply":
	  SERVICES_D->eventReceiveFingerReply(packet);
	  break;
        case "locate-req":
	  SERVICES_D->eventReceiveLocateRequest(packet);
	  break;
        case "locate-reply":
	  SERVICES_D->eventReceiveLocateReply(packet);
	  break;
        case "tell":
	  SERVICES_D->eventReceiveTell(packet);
	  break;
        case "who-req":
	  SERVICES_D->eventReceiveWhoRequest(packet);
	  break;
        case "who-reply":
	  SERVICES_D->eventReceiveWhoReply(packet);
	  break;
        case "news":
	  SERVICES_D->eventReceiveNews(packet);
	  break;
        case "mail":
	  SERVICES_D->eventReceiveMail(packet);
	  break;
        case "mail-ok":
	  SERVICES_D->eventReceiveMailOk(packet);
	  break;
        case "file":
	  break;
        case "error":
	  SERVICES_D->eventReceiveError(packet);
	  break;
        default:
	  break;
    }
}

static void eventSocketClose() {
    int extra_wait;

    extra_wait = (Tries++) * 20;
    if( extra_wait > 600 ) extra_wait = 600;
    Connected = 0;
    call_out( (: Setup :), 20 + extra_wait);
}

static void eventConnectionFailure() {
    if( Connected ) return;
    error("Failed to find a useful name server.\n");
}

int SetDestructOnClose(int x) { return 0; }

int SetSocketType(int x) { return client::SetSocketType(MUD); }

string GetMudName(string mud) {
    string *lc, *uc;
    int x;

    if( MudList->List[mud] ) return mud;
    lc = map(uc = keys(MudList->List), function(string str) {
      if( !str ) return "";
      else return lower_case(str);
    });
    x = member_array(lower_case(mud), lc);
    if( x < 0 ) return 0;
    else return uc[x];
}

mapping GetMudList() { return copy(MudList->List); }

string *GetMuds() { return keys(MudList->List); }

mapping GetChannelList() { return copy(ChannelList->List); }

string *GetChannels() { return keys(ChannelList->List); }

string *GetMatch(string mud) {
    string *uc, *lc;

    mud = lower_case(mud);
    lc = map(uc = keys(MudList->List), (: lower_case :));
    return map(filter(regexp(lc, "^"+mud, 1), (: intp :)), (: $(uc)[$1] :));
}

string GetNameserver() { return Nameservers[0][0]; }

#endif /* __PACKAGE_SOCKETS__ */