// This file written mostly by Tim Johnson (Tim@TimMUD)

// broadcast the mudlist when a mud just now got changed...
varargs void broadcast_mudlist(string mudname, int remote, string single){
    string targ_mudname;
    string *audience = ({});
    mixed *packet1; // for muds that use protocol 1 or 2
    mixed *packet3; // for muds that use protocol 3
    int mudstat;
    mudinfo_update_counter++;

    if(single && sizeof(single) && connected_muds[single]){
	audience = ({ single });
    }
    else audience = keys(connected_muds);

    if(mudinfo[mudname]){
	//trr("router: disconnect time for "+mudname+": "+mudinfo[mudname]["disconnect_time"]);
    }
    //else //trr("apparently i know nothing of "+mudname);
    if(!connected_muds[mudname] && mudinfo[mudname]){
	//trr("disconnect time for "+mudname+": "+mudinfo[mudname]["disconnect_time"]);
	if(!mudinfo[mudname]["disconnect_time"]) mudstat = -1;
	else mudstat = 0;
    }
    else {
	//trr("evidently i think we are connected.");
	mudstat = -1;
    }
    if(!mapp(mudinfo[mudname])){
	//trr("WTF. NOT A MAPPING: "+get_stack(),"white");
	return;
    }
    //trr("mudstat: "+mudstat,"white");
    //trr("mud: "+identify(mudinfo[mudname]),"green");
    //trr("broadcasting mudlist to let them know about "+mudname,"red");
    packet1 = ({ "mudlist", 5, router_name, 0, 0, 0, mudinfo_update_counter,([
	mudname:({
	  mudstat,
	  // -1=up, 0=down, n=down for n secs
	  mudinfo[mudname]["ip"], // ip_addr
	  mudinfo[mudname]["player_port"], // player_port
	  mudinfo[mudname]["imud_tcp_port"], // imud_tcp_port
	  mudinfo[mudname]["imud_udp_port"], // imud_udp_port
	  mudinfo[mudname]["mudlib"], // mudlib
	  mudinfo[mudname]["base_mudlib"], // base_mudlib
	  mudinfo[mudname]["driver"], // driver
	  mudinfo[mudname]["mud_type"], // mud_type
	  mudinfo[mudname]["open_status"], // open_status
	  mudinfo[mudname]["services"], // services
	})
      ]) });
    packet3 = ({ "mudlist", 5, router_name, 0, 0, 0, mudinfo_update_counter,([
	mudname:({
	  mudstat,
	  // -1=up, 0=down, n=down for n secs
	  mudinfo[mudname]["ip"], // ip_addr
	  mudinfo[mudname]["player_port"], // player_port
	  mudinfo[mudname]["imud_tcp_port"], // imud_tcp_port
	  mudinfo[mudname]["imud_udp_port"], // imud_udp_port
	  mudinfo[mudname]["mudlib"], // mudlib
	  mudinfo[mudname]["base_mudlib"], // base_mudlib
	  mudinfo[mudname]["driver"], // driver
	  mudinfo[mudname]["mud_type"], // mud_type
	  mudinfo[mudname]["open_status"], // open_status
	  mudinfo[mudname]["admin_email"], // admin_email
	  mudinfo[mudname]["services"], // services
	  mudinfo[mudname]["other_data"], // other_data
	})
      ]) });
    if(!mudinfo[mudname]){ // deleted mud...
	// just send a 0 to everyone
	foreach(targ_mudname in audience){
	    write_data(connected_muds[targ_mudname], ({
		"mudlist", 5, router_name, 0, targ_mudname,
		0, mudinfo_update_counter, ([mudname:0])
	      }) );
	}
	return;
    }
    if(!remote){
	//trr("mudstat for "+mudname+": "+mudstat);
	this_object()->SendList( ([ mudname : mudinfo[mudname] ]) );
}
foreach(targ_mudname in audience){
    switch(mudinfo[targ_mudname]["protocol"]){
    case 1:
    case 2:
	packet1[4]=targ_mudname;
	write_data(connected_muds[targ_mudname], packet1);
	break;
    case 3:
	packet3[4]=targ_mudname;
	write_data(connected_muds[targ_mudname], packet3);
	break;
    }
}
}
