]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - netconn.c
better handling of color tints by colormap
[xonotic/darkplaces.git] / netconn.c
index 916dc6885ea85bd2e9834db53823bc06692b4a35..e8b38cf5f022c19effd7289de1d82c684bd690c1 100755 (executable)
--- a/netconn.c
+++ b/netconn.c
@@ -38,7 +38,7 @@ static cvar_t sv_masters [] =
        {CVAR_SAVE, "sv_master4", "", "user-chosen master server 4"},
        {0, "sv_masterextra1", "ghdigital.com", "default master server 1 (admin: LordHavoc)"}, // admin: LordHavoc
        {0, "sv_masterextra2", "dpmaster.deathmask.net", "default master server 2 (admin: Willis)"}, // admin: Willis
-       {0, "sv_masterextra3", "excalibur.nvg.ntnu.no", "default master server 3 (admin: tChr)"}, // admin: tChr
+       {0, "sv_masterextra3", "dpmaster.tchr.no", "default master server 3 (admin: tChr)"}, // admin: tChr
        {0, NULL, NULL, NULL}
 };
 
@@ -48,19 +48,11 @@ static cvar_t sv_qwmasters [] =
        {CVAR_SAVE, "sv_qwmaster2", "", "user-chosen qwmaster server 2"},
        {CVAR_SAVE, "sv_qwmaster3", "", "user-chosen qwmaster server 3"},
        {CVAR_SAVE, "sv_qwmaster4", "", "user-chosen qwmaster server 4"},
-       {0, "sv_qwmasterextra1", "192.246.40.37:27000", "id Limbo (admin: id Software)"},
-       {0, "sv_qwmasterextra2", "192.246.40.37:27002", "id CTF (admin: id Software)"},
-       {0, "sv_qwmasterextra3", "192.246.40.37:27003", "id TeamFortress (admin: id Software)"},
-       {0, "sv_qwmasterextra4", "192.246.40.37:27004", "id Miscilaneous (admin: id Software)"},
-       {0, "sv_qwmasterextra5", "192.246.40.37:27006", "id Deathmatch Only (admin: id Software)"},
-       {0, "sv_qwmasterextra6", "150.254.66.120:27000", "Poland's master server. (admin: unknown)"},
-       {0, "sv_qwmasterextra7", "62.112.145.129:27000", "Ocrana master server. (admin: unknown)"},
-       {0, "sv_qwmasterextra8", "master.edome.net", "edome master server. (admin: unknown)"},
-       {0, "sv_qwmasterextra9", "qwmaster.barrysworld.com", "barrysworld master server. (admin: unknown)"},
-       {0, "sv_qwmasterextra10", "qwmaster.ocrana.de:27000", "Ocrana2 master server. (admin: unknown)"},
-       {0, "sv_qwmasterextra11", "213.221.174.165:27000", "unknown1 master server. (admin: unknown)"},
-       {0, "sv_qwmasterextra12", "195.74.0.8", "unknown2 master server. (admin: unknown)"},
-       {0, "sv_qwmasterextra13", "204.182.161.2", "unknown3 master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra1", "master.quakeservers.net:27000", "Global master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra2", "asgaard.morphos-team.net:27000", "Global master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra3", "qwmaster.ocrana.de:27000", "German master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra4", "masterserver.exhale.de:27000", "German master server. (admin: unknown)"},
+       {0, "sv_qwmasterextra5", "kubus.rulez.pl:27000", "Poland master server. (admin: unknown)"},
        {0, NULL, NULL, NULL}
 };
 
@@ -81,9 +73,13 @@ static cvar_t cl_netpacketloss_receive = {0, "cl_netpacketloss_receive","0", "dr
 static cvar_t net_slist_queriespersecond = {0, "net_slist_queriespersecond", "20", "how many server information requests to send per second"};
 static cvar_t net_slist_queriesperframe = {0, "net_slist_queriesperframe", "4", "maximum number of server information requests to send each rendered frame (guards against low framerates causing problems)"};
 static cvar_t net_slist_timeout = {0, "net_slist_timeout", "4", "how long to listen for a server information response before giving up"};
+static cvar_t net_slist_pause = {0, "net_slist_pause", "0", "when set to 1, the server list won't update until it is set back to 0"};
 static cvar_t net_slist_maxtries = {0, "net_slist_maxtries", "3", "how many times to ask the same server for information (more times gives better ping reports but takes longer)"};
+static cvar_t net_slist_favorites = {CVAR_SAVE, "net_slist_favorites", "", "contains a list of IP addresses and ports to always query explicitly"};
 
 static cvar_t gameversion = {0, "gameversion", "0", "version of game data (mod-specific), when client and server gameversion mismatch in the server browser the server is shown as incompatible"};
+static cvar_t rcon_restricted_password = {CVAR_PRIVATE, "rcon_restricted_password", "", "password to authenticate rcon commands in restricted mode"};
+static cvar_t rcon_restricted_commands = {0, "rcon_restricted_commands", "", "allowed commands for rcon when the restricted mode password was used"};
 
 /* statistic counters */
 static int packetsSent = 0;
@@ -105,6 +101,7 @@ int serverreplycount = 0;
 
 // this is only false if there are still servers left to query
 static qboolean serverlist_querysleep = true;
+static qboolean serverlist_paused = false;
 // this is pushed a second or two ahead of realtime whenever a master server
 // reply is received, to avoid issuing queries while master replies are still
 // flooding in (which would make a mess of the ping times)
@@ -135,7 +132,7 @@ serverlist_mask_t serverlist_andmasks[SERVERLIST_ANDMASKCOUNT];
 serverlist_mask_t serverlist_ormasks[SERVERLIST_ORMASKCOUNT];
 
 serverlist_infofield_t serverlist_sortbyfield;
-qboolean serverlist_sortdescending;
+int serverlist_sortflags;
 
 int serverlist_viewcount = 0;
 serverlist_entry_t *serverlist_viewlist[SERVERLIST_VIEWLISTSIZE];
@@ -175,6 +172,12 @@ static qboolean _ServerList_Entry_Compare( serverlist_entry_t *A, serverlist_ent
 {
        int result = 0; // > 0 if for numbers A > B and for text if A < B
 
+       if( serverlist_sortflags & SLSF_FAVORITESFIRST )
+       {
+               if(A->info.isfavorite != B->info.isfavorite)
+                       return A->info.isfavorite;
+       }
+
        switch( serverlist_sortbyfield ) {
                case SLIF_PING:
                        result = A->info.ping - B->info.ping;
@@ -191,6 +194,9 @@ static qboolean _ServerList_Entry_Compare( serverlist_entry_t *A, serverlist_ent
                case SLIF_NUMHUMANS:
                        result = A->info.numhumans - B->info.numhumans;
                        break;
+               case SLIF_FREESLOTS:
+                       result = A->info.freeslots - B->info.freeslots;
+                       break;
                case SLIF_PROTOCOL:
                        result = A->info.protocol - B->info.protocol;
                        break;
@@ -198,26 +204,36 @@ static qboolean _ServerList_Entry_Compare( serverlist_entry_t *A, serverlist_ent
                        result = strcmp( B->info.cname, A->info.cname );
                        break;
                case SLIF_GAME:
-                       result = strcmp( B->info.game, A->info.game );
+                       result = strcasecmp( B->info.game, A->info.game );
                        break;
                case SLIF_MAP:
-                       result = strcmp( B->info.map, A->info.map );
+                       result = strcasecmp( B->info.map, A->info.map );
                        break;
                case SLIF_MOD:
-                       result = strcmp( B->info.mod, A->info.mod );
+                       result = strcasecmp( B->info.mod, A->info.mod );
                        break;
                case SLIF_NAME:
-                       result = strcmp( B->info.name, A->info.name );
+                       result = strcasecmp( B->info.name, A->info.name );
+                       break;
+               case SLIF_QCSTATUS:
+                       result = strcasecmp( B->info.qcstatus, A->info.qcstatus ); // not really THAT useful, though
+                       break;
+               case SLIF_ISFAVORITE:
+                       result = !!B->info.isfavorite - !!A->info.isfavorite;
                        break;
                default:
                        Con_DPrint( "_ServerList_Entry_Compare: Bad serverlist_sortbyfield!\n" );
                        break;
        }
 
-       if( serverlist_sortdescending )
-               return result > 0;
        if (result != 0)
-               return result < 0;
+       {
+               if( serverlist_sortflags & SLSF_DESCENDING )
+                       return result > 0;
+               else
+                       return result < 0;
+       }
+
        // if the chosen sort key is identical, sort by index
        // (makes this a stable sort, so that later replies from servers won't
        //  shuffle the servers around when they have the same ping)
@@ -241,6 +257,8 @@ static qboolean _ServerList_CompareInt( int A, serverlist_maskop_t op, int B )
                case SLMO_GREATEREQUAL:
                case SLMO_CONTAINS:
                case SLMO_NOTCONTAIN:
+               case SLMO_STARTSWITH:
+               case SLMO_NOTSTARTSWITH:
                        return A >= B;
                default:
                        Con_DPrint( "_ServerList_CompareInt: Bad op!\n" );
@@ -251,9 +269,10 @@ static qboolean _ServerList_CompareInt( int A, serverlist_maskop_t op, int B )
 static qboolean _ServerList_CompareStr( const char *A, serverlist_maskop_t op, const char *B )
 {
        int i;
-       char bufferA[ 256 ], bufferB[ 256 ]; // should be more than enough
-       for (i = 0;i < (int)sizeof(bufferA)-1 && A[i];i++)
-               bufferA[i] = (A[i] >= 'A' && A[i] <= 'Z') ? (A[i] + 'a' - 'A') : A[i];
+       char bufferA[ 1400 ], bufferB[ 1400 ]; // should be more than enough
+       COM_StringDecolorize(A, 0, bufferA, sizeof(bufferA), false);
+       for (i = 0;i < (int)sizeof(bufferA)-1 && bufferA[i];i++)
+               bufferA[i] = (bufferA[i] >= 'A' && bufferA[i] <= 'Z') ? (bufferA[i] + 'a' - 'A') : bufferA[i];
        bufferA[i] = 0;
        for (i = 0;i < (int)sizeof(bufferB)-1 && B[i];i++)
                bufferB[i] = (B[i] >= 'A' && B[i] <= 'Z') ? (B[i] + 'a' - 'A') : B[i];
@@ -266,6 +285,11 @@ static qboolean _ServerList_CompareStr( const char *A, serverlist_maskop_t op, c
                        return *bufferB && !!strstr( bufferA, bufferB ); // we want a real bool
                case SLMO_NOTCONTAIN:
                        return !*bufferB || !strstr( bufferA, bufferB );
+               case SLMO_STARTSWITH:
+                       //Con_Printf("startsWith: %s %s\n", bufferA, bufferB);
+                       return *bufferB && !memcmp(bufferA, bufferB, strlen(bufferB));
+               case SLMO_NOTSTARTSWITH:
+                       return !*bufferB || memcmp(bufferA, bufferB, strlen(bufferB));
                case SLMO_LESS:
                        return strcmp( bufferA, bufferB ) < 0;
                case SLMO_LESSEQUAL:
@@ -296,6 +320,8 @@ static qboolean _ServerList_Entry_Mask( serverlist_mask_t *mask, serverlist_info
                return false;
        if( !_ServerList_CompareInt( info->numhumans, mask->tests[SLIF_NUMHUMANS], mask->info.numhumans ) )
                return false;
+       if( !_ServerList_CompareInt( info->freeslots, mask->tests[SLIF_FREESLOTS], mask->info.freeslots ) )
+               return false;
        if( !_ServerList_CompareInt( info->protocol, mask->tests[SLIF_PROTOCOL], mask->info.protocol ))
                return false;
        if( *mask->info.cname
@@ -313,24 +339,45 @@ static qboolean _ServerList_Entry_Mask( serverlist_mask_t *mask, serverlist_info
        if( *mask->info.name
                && !_ServerList_CompareStr( info->name, mask->tests[SLIF_NAME], mask->info.name ) )
                return false;
+       if( *mask->info.qcstatus
+               && !_ServerList_CompareStr( info->qcstatus, mask->tests[SLIF_QCSTATUS], mask->info.qcstatus ) )
+               return false;
+       if( *mask->info.players
+               && !_ServerList_CompareStr( info->players, mask->tests[SLIF_PLAYERS], mask->info.players ) )
+               return false;
+       if( !_ServerList_CompareInt( info->isfavorite, mask->tests[SLIF_ISFAVORITE], mask->info.isfavorite ))
+               return false;
        return true;
 }
 
 static void ServerList_ViewList_Insert( serverlist_entry_t *entry )
 {
        int start, end, mid;
+       const char *text;
 
        // reject incompatible servers
        if (entry->info.gameversion != gameversion.integer)
                return;
 
+       // refresh the "favorite" status
+       text = net_slist_favorites.string;
+       entry->info.isfavorite = false;
+       while(COM_ParseToken_Console(&text))
+       {
+               if(!strcmp(com_token, entry->info.cname))
+               {
+                       entry->info.isfavorite = true;
+                       break;
+               }
+       }
+
        // FIXME: change this to be more readable (...)
        // now check whether it passes through the masks
-       for( start = 0 ; serverlist_andmasks[start].active && start < SERVERLIST_ANDMASKCOUNT ; start++ )
+       for( start = 0 ; start < SERVERLIST_ANDMASKCOUNT && serverlist_andmasks[start].active; start++ )
                if( !_ServerList_Entry_Mask( &serverlist_andmasks[start], &entry->info ) )
                        return;
 
-       for( start = 0 ; serverlist_ormasks[start].active && start < SERVERLIST_ORMASKCOUNT ; start++ )
+       for( start = 0 ; start < SERVERLIST_ORMASKCOUNT && serverlist_ormasks[start].active ; start++ )
                if( _ServerList_Entry_Mask( &serverlist_ormasks[start], &entry->info ) )
                        break;
        if( start == SERVERLIST_ORMASKCOUNT || (start > 0 && !serverlist_ormasks[start].active) )
@@ -386,9 +433,12 @@ void ServerList_RebuildViewList(void)
        int i;
 
        serverlist_viewcount = 0;
-       for( i = 0 ; i < serverlist_cachecount ; i++ )
-               if( serverlist_cache[i].query == SQS_QUERIED )
-                       ServerList_ViewList_Insert( &serverlist_cache[i] );
+       for( i = 0 ; i < serverlist_cachecount ; i++ ) {
+               serverlist_entry_t *entry = &serverlist_cache[i];
+               // also display entries that are currently being refreshed [11/8/2007 Black]
+               if( entry->query == SQS_QUERIED || entry->query == SQS_REFRESHING )
+                       ServerList_ViewList_Insert( entry );
+       }
 }
 
 void ServerList_ResetMasks(void)
@@ -404,6 +454,22 @@ void ServerList_ResetMasks(void)
                serverlist_ormasks[i].info.numbots = -1;
 }
 
+void ServerList_GetPlayerStatistics(int *numplayerspointer, int *maxplayerspointer)
+{
+       int i;
+       int numplayers = 0, maxplayers = 0;
+       for (i = 0;i < serverlist_cachecount;i++)
+       {
+               if (serverlist_cache[i].query == SQS_QUERIED)
+               {
+                       numplayers += serverlist_cache[i].info.numhumans;
+                       maxplayers += serverlist_cache[i].info.maxplayers;
+               }
+       }
+       *numplayerspointer = numplayers;
+       *maxplayerspointer = maxplayers;
+}
+
 #if 0
 static void _ServerList_Test(void)
 {
@@ -413,23 +479,33 @@ static void _ServerList_Test(void)
                serverlist_cache[serverlist_cachecount].info.ping = 1000 + 1024 - i;
                dpsnprintf( serverlist_cache[serverlist_cachecount].info.name, sizeof(serverlist_cache[serverlist_cachecount].info.name), "Black's ServerList Test %i", i );
                serverlist_cache[serverlist_cachecount].finished = true;
-               sprintf( serverlist_cache[serverlist_cachecount].line1, "%i %s", serverlist_cache[serverlist_cachecount].info.ping, serverlist_cache[serverlist_cachecount].info.name );
+               dpsnprintf( serverlist_cache[serverlist_cachecount].line1, sizeof(serverlist_cache[serverlist_cachecount].info.line1), "%i %s", serverlist_cache[serverlist_cachecount].info.ping, serverlist_cache[serverlist_cachecount].info.name );
                ServerList_ViewList_Insert( &serverlist_cache[serverlist_cachecount] );
                serverlist_cachecount++;
        }
 }
 #endif
 
-void ServerList_QueryList(qboolean querydp, qboolean queryqw)
+void ServerList_QueryList(qboolean resetcache, qboolean querydp, qboolean queryqw, qboolean consoleoutput)
 {
        masterquerytime = realtime;
        masterquerycount = 0;
        masterreplycount = 0;
-       serverquerycount = 0;
-       serverreplycount = 0;
-       serverlist_cachecount = 0;
-       serverlist_viewcount = 0;
-       serverlist_consoleoutput = false;
+       if( resetcache ) {
+               serverquerycount = 0;
+               serverreplycount = 0;
+               serverlist_cachecount = 0;
+               serverlist_viewcount = 0;
+       } else {
+               // refresh all entries
+               int n;
+               for( n = 0 ; n < serverlist_cachecount ; n++ ) {
+                       serverlist_entry_t *entry = &serverlist_cache[ n ];
+                       entry->query = SQS_REFRESHING;
+                       entry->querycounter = 0;
+               }
+       }
+       serverlist_consoleoutput = consoleoutput;
 
        //_ServerList_Test();
 
@@ -505,7 +581,7 @@ qboolean NetConn_CanSend(netconn_t *conn)
        }
 }
 
-int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate)
+int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolversion_t protocol, int rate, qboolean quakesignon_suppressreliables)
 {
        int totallen = 0;
 
@@ -537,18 +613,18 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                        sendreliable = true;
                }
                // outgoing unreliable packet number, and outgoing reliable packet number (0 or 1)
-               *((int *)(sendbuffer + 0)) = LittleLong((unsigned int)conn->qw.outgoing_sequence | ((unsigned int)sendreliable<<31));
+               *((int *)(sendbuffer + 0)) = LittleLong((unsigned int)conn->outgoing_unreliable_sequence | ((unsigned int)sendreliable<<31));
                // last received unreliable packet number, and last received reliable packet number (0 or 1)
                *((int *)(sendbuffer + 4)) = LittleLong((unsigned int)conn->qw.incoming_sequence | ((unsigned int)conn->qw.incoming_reliable_sequence<<31));
                packetLen = 8;
-               conn->qw.outgoing_sequence++;
+               conn->outgoing_unreliable_sequence++;
                // client sends qport in every packet
                if (conn == cls.netcon)
                {
                        *((short *)(sendbuffer + 8)) = LittleShort(cls.qw_qport);
                        packetLen += 2;
                        // also update cls.qw_outgoing_sequence
-                       cls.qw_outgoing_sequence = conn->qw.outgoing_sequence;
+                       cls.qw_outgoing_sequence = conn->outgoing_unreliable_sequence;
                }
                if (packetLen + (sendreliable ? conn->sendMessageLength : 0) > 1400)
                {
@@ -564,7 +640,7 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                        conn->outgoing_reliablesize[conn->outgoing_packetcounter] += conn->sendMessageLength;
                        memcpy(sendbuffer + packetLen, conn->sendMessage, conn->sendMessageLength);
                        packetLen += conn->sendMessageLength;
-                       conn->qw.last_reliable_sequence = conn->qw.outgoing_sequence;
+                       conn->qw.last_reliable_sequence = conn->outgoing_unreliable_sequence;
                }
 
                // add the unreliable message if possible
@@ -622,7 +698,7 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                }
 
                // if we have a new reliable message to send, do so
-               if (!conn->sendMessageLength && conn->message.cursize)
+               if (!conn->sendMessageLength && conn->message.cursize && !quakesignon_suppressreliables)
                {
                        if (conn->message.cursize > (int)sizeof(conn->sendMessage))
                        {
@@ -673,7 +749,7 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
                }
 
                // if we have an unreliable message to send, do so
-               //if (data->cursize)
+               if (data->cursize)
                {
                        packetLen = NET_HEADERSIZE + data->cursize;
 
@@ -685,10 +761,10 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
 
                        header = (unsigned int *)sendbuffer;
                        header[0] = BigLong(packetLen | NETFLAG_UNRELIABLE);
-                       header[1] = BigLong(conn->nq.unreliableSendSequence);
+                       header[1] = BigLong(conn->outgoing_unreliable_sequence);
                        memcpy(sendbuffer + NET_HEADERSIZE, data->data, data->cursize);
 
-                       conn->nq.unreliableSendSequence++;
+                       conn->outgoing_unreliable_sequence++;
 
                        conn->outgoing_unreliablesize[conn->outgoing_packetcounter] += packetLen;
 
@@ -711,6 +787,16 @@ int NetConn_SendUnreliableMessage(netconn_t *conn, sizebuf_t *data, protocolvers
        return 0;
 }
 
+qboolean NetConn_HaveClientPorts(void)
+{
+       return !!cl_numsockets;
+}
+
+qboolean NetConn_HaveServerPorts(void)
+{
+       return !!sv_numsockets;
+}
+
 void NetConn_CloseClientPorts(void)
 {
        for (;cl_numsockets > 0;cl_numsockets--)
@@ -748,7 +834,10 @@ void NetConn_OpenClientPorts(void)
        port = bound(0, cl_netport.integer, 65535);
        if (cl_netport.integer != port)
                Cvar_SetValueQuick(&cl_netport, port);
-       Con_Printf("Client using port %i\n", port);
+       if(port == 0)
+               Con_Printf("Client using an automatically assigned port\n");
+       else
+               Con_Printf("Client using port %i\n", port);
        NetConn_OpenClientPort("local:2", 0);
        NetConn_OpenClientPort(net_address.string, port);
        //NetConn_OpenClientPort(net_address_ipv6.string, port);
@@ -1063,7 +1152,7 @@ static int NetConn_ReceivedMessage(netconn_t *conn, unsigned char *data, int len
                                                        unsigned int *header;
 
                                                        conn->sendMessageLength -= MAX_PACKETFRAGMENT;
-                                                       memcpy(conn->sendMessage, conn->sendMessage+MAX_PACKETFRAGMENT, conn->sendMessageLength);
+                                                       memmove(conn->sendMessage, conn->sendMessage+MAX_PACKETFRAGMENT, conn->sendMessageLength);
 
                                                        if (conn->sendMessageLength <= MAX_PACKETFRAGMENT)
                                                        {
@@ -1165,7 +1254,6 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
        cls.signon = 0;                         // need all the signon messages before playing
        cls.protocol = initialprotocol;
        // reset move sequence numbering on this new connection
-       cls.movesequence = 1;
        cls.servermovesequence = 0;
        if (cls.protocol == PROTOCOL_QUAKEWORLD)
                Cmd_ForwardStringToServer("new");
@@ -1179,7 +1267,7 @@ void NetConn_ConnectionEstablished(lhnetsocket_t *mysocket, lhnetaddress_t *peer
                msg.data = buf;
                msg.maxsize = sizeof(buf);
                MSG_WriteChar(&msg, clc_nop);
-               NetConn_SendUnreliableMessage(cls.netcon, &msg, cls.protocol, 10000);
+               NetConn_SendUnreliableMessage(cls.netcon, &msg, cls.protocol, 10000, false);
        }
 }
 
@@ -1194,10 +1282,15 @@ static int NetConn_ClientParsePacket_ServerList_ProcessReply(const char *address
 {
        int n;
        int pingtime;
+       serverlist_entry_t *entry = NULL;
+
        // search the cache for this server and update it
-       for (n = 0;n < serverlist_cachecount;n++)
-               if (!strcmp(addressstring, serverlist_cache[n].info.cname))
+       for (n = 0;n < serverlist_cachecount;n++) {
+               entry = &serverlist_cache[ n ];
+               if (!strcmp(addressstring, entry->info.cname))
                        break;
+       }
+
        if (n == serverlist_cachecount)
        {
                // LAN search doesnt require an answer from the master server so we wont
@@ -1207,43 +1300,105 @@ static int NetConn_ClientParsePacket_ServerList_ProcessReply(const char *address
                if (serverlist_cachecount == SERVERLIST_TOTALSIZE)
                        return -1;
 
-               memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
+               entry = &serverlist_cache[n];
+
+               memset(entry, 0, sizeof(*entry));
                // store the data the engine cares about (address and ping)
-               strlcpy(serverlist_cache[serverlist_cachecount].info.cname, addressstring, sizeof(serverlist_cache[serverlist_cachecount].info.cname));
-               serverlist_cache[serverlist_cachecount].info.ping = 100000;
-               serverlist_cache[serverlist_cachecount].querytime = realtime;
+               strlcpy(entry->info.cname, addressstring, sizeof(entry->info.cname));
+               entry->info.ping = 100000;
+               entry->querytime = realtime;
                // if not in the slist menu we should print the server to console
                if (serverlist_consoleoutput)
                        Con_Printf("querying %s\n", addressstring);
                ++serverlist_cachecount;
+
+#if 0
+               // we should not NEED this part...
+               text = net_slist_favorites.string;
+               while(COM_ParseToken_Console(&text))
+               {
+                       if(!strcmp(com_token, addressstring))
+                               entry->isfavorite = 1;
+               }
+#endif
        }
        // if this is the first reply from this server, count it as having replied
-       if (serverlist_cache[n].info.ping == 100000)
-               serverreplycount++;
-       pingtime = (int)((realtime - serverlist_cache[n].querytime) * 1000.0 + 0.5);
+       pingtime = (int)((realtime - entry->querytime) * 1000.0 + 0.5);
        pingtime = bound(0, pingtime, 9999);
-       // update the ping
-       serverlist_cache[n].info.ping = min(serverlist_cache[n].info.ping, pingtime);
+       if (entry->query == SQS_REFRESHING) {
+               entry->info.ping = pingtime;
+               entry->query = SQS_QUERIED;
+       } else {
+               // convert to unsigned to catch the -1
+               // I still dont like this but its better than the old 10000 magic ping number - as in easier to type and read :( [11/8/2007 Black]
+               entry->info.ping = min((unsigned) entry->info.ping, (unsigned) pingtime);
+               serverreplycount++;
+       }
+       
        // other server info is updated by the caller
        return n;
 }
 
 static void NetConn_ClientParsePacket_ServerList_UpdateCache(int n)
 {
-       serverlist_info_t *info = &serverlist_cache[n].info;
+       serverlist_entry_t *entry = &serverlist_cache[n];
+       serverlist_info_t *info = &entry->info;
        // update description strings for engine menu and console output
-       dpsnprintf(serverlist_cache[n].line1, sizeof(serverlist_cache[n].line1), "^%c%5d^7 ^%c%3u^7/%3u %-65.65s", info->ping >= 300 ? '1' : (info->ping >= 200 ? '3' : '7'), (int)info->ping, ((info->numhumans > 0 && info->numhumans < info->maxplayers) ? (info->numhumans >= 4 ? '7' : '3') : '1'), info->numplayers, info->maxplayers, info->name);
-       dpsnprintf(serverlist_cache[n].line2, sizeof(serverlist_cache[n].line2), "^4%-21.21s %-19.19s ^%c%-17.17s^4 %-20.20s", info->cname, info->game, (info->gameversion != gameversion.integer) ? '1' : '4', info->mod, info->map);
-       if (serverlist_cache[n].query == SQS_QUERIED)
-               ServerList_ViewList_Remove(&serverlist_cache[n]);
+       dpsnprintf(entry->line1, sizeof(serverlist_cache[n].line1), "^%c%5d^7 ^%c%3u^7/%3u %-65.65s", info->ping >= 300 ? '1' : (info->ping >= 200 ? '3' : '7'), (int)info->ping, ((info->numhumans > 0 && info->numhumans < info->maxplayers) ? (info->numhumans >= 4 ? '7' : '3') : '1'), info->numplayers, info->maxplayers, info->name);
+       dpsnprintf(entry->line2, sizeof(serverlist_cache[n].line2), "^4%-21.21s %-19.19s ^%c%-17.17s^4 %-20.20s", info->cname, info->game, (info->gameversion != gameversion.integer) ? '1' : '4', info->mod, info->map);
+       if (entry->query == SQS_QUERIED)
+       {
+               if(!serverlist_paused)
+                       ServerList_ViewList_Remove(entry);
+       }
        // if not in the slist menu we should print the server to console (if wanted)
        else if( serverlist_consoleoutput )
                Con_Printf("%s\n%s\n", serverlist_cache[n].line1, serverlist_cache[n].line2);
        // and finally, update the view set
-       ServerList_ViewList_Insert( &serverlist_cache[n] );
+       if(!serverlist_paused)
+               ServerList_ViewList_Insert( entry );
+       //      update the entry's state
        serverlist_cache[n].query = SQS_QUERIED;
 }
 
+// returns true, if it's sensible to continue the processing
+static qboolean NetConn_ClientParsePacket_ServerList_PrepareQuery( int protocol, const char *ipstring, qboolean isfavorite ) {
+       int n;
+       serverlist_entry_t *entry;
+
+       //      ignore the rest of the message if the serverlist is full
+       if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
+               return false;
+       //      also ignore     it      if      we      have already queried    it      (other master server    response)
+       for( n =        0 ; n   < serverlist_cachecount ; n++   )
+               if( !strcmp( ipstring, serverlist_cache[ n ].info.cname ) )
+                       break;
+
+       entry = &serverlist_cache[n];
+
+       if( n < serverlist_cachecount ) {
+               // the entry has already been queried once or 
+               return true;
+       }
+
+       memset(entry, 0, sizeof(entry));
+       entry->protocol =       protocol;
+       //      store   the data        the engine cares about (address and     ping)
+       strlcpy (entry->info.cname, ipstring, sizeof(entry->info.cname));
+
+       entry->info.isfavorite = isfavorite;
+       
+       // no, then reset the ping right away
+       entry->info.ping = -1;
+       // we also want to increase the serverlist_cachecount then
+       serverlist_cachecount++;
+       serverquerycount++;
+
+       entry->query =  SQS_QUERYING;
+
+       return true;
+}
+
 static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress)
 {
        qboolean fromserver;
@@ -1306,6 +1461,58 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        M_Update_Return_Reason(rejectreason);
                        return true;
                }
+               if (length >= 15 && !memcmp(string, "statusResponse\x0A", 15))
+               {
+                       serverlist_info_t *info;
+                       char *p;
+                       int n;
+
+                       string += 15;
+                       // search the cache for this server and update it
+                       n = NetConn_ClientParsePacket_ServerList_ProcessReply(addressstring2);
+                       if (n < 0)
+                               return true;
+
+                       info = &serverlist_cache[n].info;
+                       info->game[0] = 0;
+                       info->mod[0]  = 0;
+                       info->map[0]  = 0;
+                       info->name[0] = 0;
+                       info->qcstatus[0] = 0;
+                       info->players[0] = 0;
+                       info->protocol = -1;
+                       info->numplayers = 0;
+                       info->numbots = -1;
+                       info->maxplayers  = 0;
+                       info->gameversion = 0;
+
+                       p = strchr(string, '\n');
+                       if(p)
+                       {
+                               *p = 0; // cut off the string there
+                               ++p;
+                       }
+                       else
+                               Con_Printf("statusResponse without players block?\n");
+
+                       if ((s = SearchInfostring(string, "gamename"     )) != NULL) strlcpy(info->game, s, sizeof (info->game));
+                       if ((s = SearchInfostring(string, "modname"      )) != NULL) strlcpy(info->mod , s, sizeof (info->mod ));
+                       if ((s = SearchInfostring(string, "mapname"      )) != NULL) strlcpy(info->map , s, sizeof (info->map ));
+                       if ((s = SearchInfostring(string, "hostname"     )) != NULL) strlcpy(info->name, s, sizeof (info->name));
+                       if ((s = SearchInfostring(string, "protocol"     )) != NULL) info->protocol = atoi(s);
+                       if ((s = SearchInfostring(string, "clients"      )) != NULL) info->numplayers = atoi(s);
+                       if ((s = SearchInfostring(string, "bots"         )) != NULL) info->numbots = atoi(s);
+                       if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) info->maxplayers = atoi(s);
+                       if ((s = SearchInfostring(string, "gameversion"  )) != NULL) info->gameversion = atoi(s);
+                       if ((s = SearchInfostring(string, "qcstatus"     )) != NULL) strlcpy(info->qcstatus, s, sizeof(info->qcstatus));
+                       if (p                                               != NULL) strlcpy(info->players, p, sizeof(info->players));
+                       info->numhumans = info->numplayers - max(0, info->numbots);
+                       info->freeslots = info->maxplayers - info->numplayers;
+
+                       NetConn_ClientParsePacket_ServerList_UpdateCache(n);
+
+                       return true;
+               }
                if (length >= 13 && !memcmp(string, "infoResponse\x0A", 13))
                {
                        serverlist_info_t *info;
@@ -1322,11 +1529,14 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        info->mod[0]  = 0;
                        info->map[0]  = 0;
                        info->name[0] = 0;
+                       info->qcstatus[0] = 0;
+                       info->players[0] = 0;
                        info->protocol = -1;
                        info->numplayers = 0;
                        info->numbots = -1;
                        info->maxplayers  = 0;
                        info->gameversion = 0;
+
                        if ((s = SearchInfostring(string, "gamename"     )) != NULL) strlcpy(info->game, s, sizeof (info->game));
                        if ((s = SearchInfostring(string, "modname"      )) != NULL) strlcpy(info->mod , s, sizeof (info->mod ));
                        if ((s = SearchInfostring(string, "mapname"      )) != NULL) strlcpy(info->map , s, sizeof (info->map ));
@@ -1336,7 +1546,9 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                        if ((s = SearchInfostring(string, "bots"         )) != NULL) info->numbots = atoi(s);
                        if ((s = SearchInfostring(string, "sv_maxclients")) != NULL) info->maxplayers = atoi(s);
                        if ((s = SearchInfostring(string, "gameversion"  )) != NULL) info->gameversion = atoi(s);
+                       if ((s = SearchInfostring(string, "qcstatus"     )) != NULL) strlcpy(info->qcstatus, s, sizeof(info->qcstatus));
                        info->numhumans = info->numplayers - max(0, info->numbots);
+                       info->freeslots = info->maxplayers - info->numplayers;
 
                        NetConn_ClientParsePacket_ServerList_UpdateCache(n);
 
@@ -1352,30 +1564,12 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                Con_Print("received DarkPlaces server list...\n");
                        while (length >= 7 && data[0] == '\\' && (data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF || data[4] != 0xFF) && data[5] * 256 + data[6] != 0)
                        {
-                               int n;
-
                                dpsnprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[1], data[2], data[3], data[4], data[5] * 256 + data[6]);
                                if (serverlist_consoleoutput && developer_networking.integer)
                                        Con_Printf("Requesting info from DarkPlaces server %s\n", ipstring);
-                               // ignore the rest of the message if the serverlist is full
-                               if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
+                               
+                               if( !NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_DARKPLACES7, ipstring, false ) ) {
                                        break;
-                               // also ignore it if we have already queried it (other master server response)
-                               for( n = 0 ; n < serverlist_cachecount ; n++ )
-                                       if( !strcmp( ipstring, serverlist_cache[ n ].info.cname ) )
-                                               break;
-                               if( n >= serverlist_cachecount )
-                               {
-                                       serverquerycount++;
-
-                                       memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
-                                       serverlist_cache[serverlist_cachecount].protocol = PROTOCOL_DARKPLACES7;
-                                       // store the data the engine cares about (address and ping)
-                                       strlcpy (serverlist_cache[serverlist_cachecount].info.cname, ipstring, sizeof (serverlist_cache[serverlist_cachecount].info.cname));
-                                       serverlist_cache[serverlist_cachecount].info.ping = 100000;
-                                       serverlist_cache[serverlist_cachecount].query = SQS_QUERYING;
-
-                                       ++serverlist_cachecount;
                                }
 
                                // move on to next address in packet
@@ -1397,30 +1591,12 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                Con_Printf("received QuakeWorld server list from %s...\n", addressstring2);
                        while (length >= 6 && (data[0] != 0xFF || data[1] != 0xFF || data[2] != 0xFF || data[3] != 0xFF) && data[4] * 256 + data[5] != 0)
                        {
-                               int n;
-
                                dpsnprintf (ipstring, sizeof (ipstring), "%u.%u.%u.%u:%u", data[0], data[1], data[2], data[3], data[4] * 256 + data[5]);
                                if (serverlist_consoleoutput && developer_networking.integer)
                                        Con_Printf("Requesting info from QuakeWorld server %s\n", ipstring);
-                               // ignore the rest of the message if the serverlist is full
-                               if( serverlist_cachecount == SERVERLIST_TOTALSIZE )
+                               
+                               if( !NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_QUAKEWORLD, ipstring, false ) ) {
                                        break;
-                               // also ignore it if we have already queried it (other master server response)
-                               for( n = 0 ; n < serverlist_cachecount ; n++ )
-                                       if( !strcmp( ipstring, serverlist_cache[ n ].info.cname ) )
-                                               break;
-                               if( n >= serverlist_cachecount )
-                               {
-                                       serverquerycount++;
-
-                                       memset(&serverlist_cache[serverlist_cachecount], 0, sizeof(serverlist_cache[serverlist_cachecount]));
-                                       serverlist_cache[serverlist_cachecount].protocol = PROTOCOL_QUAKEWORLD;
-                                       // store the data the engine cares about (address and ping)
-                                       strlcpy (serverlist_cache[serverlist_cachecount].info.cname, ipstring, sizeof (serverlist_cache[serverlist_cachecount].info.cname));
-                                       serverlist_cache[serverlist_cachecount].info.ping = 100000;
-                                       serverlist_cache[serverlist_cachecount].query = SQS_QUERYING;
-
-                                       ++serverlist_cachecount;
                                }
 
                                // move on to next address in packet
@@ -1485,12 +1661,13 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                return true;
 
                        info = &serverlist_cache[n].info;
-                       strlcpy(info->game, "QuakeWorld", sizeof(info->game));;
+                       strlcpy(info->game, "QuakeWorld", sizeof(info->game));
                        if ((s = SearchInfostring(string, "*gamedir"     )) != NULL) strlcpy(info->mod , s, sizeof (info->mod ));else info->mod[0]  = 0;
                        if ((s = SearchInfostring(string, "map"          )) != NULL) strlcpy(info->map , s, sizeof (info->map ));else info->map[0]  = 0;
                        if ((s = SearchInfostring(string, "hostname"     )) != NULL) strlcpy(info->name, s, sizeof (info->name));else info->name[0] = 0;
                        info->protocol = 0;
                        info->numplayers = 0; // updated below
+                       info->numhumans = 0; // updated below
                        if ((s = SearchInfostring(string, "maxclients"   )) != NULL) info->maxplayers = atoi(s);else info->maxplayers  = 0;
                        if ((s = SearchInfostring(string, "gameversion"  )) != NULL) info->gameversion = atoi(s);else info->gameversion = 0;
 
@@ -1507,6 +1684,7 @@ static int NetConn_ClientParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                                        if (s >= string + length)
                                                break;
                                        info->numplayers++;
+                                       info->numhumans++;
                                        s++;
                                }
                        }
@@ -1620,6 +1798,10 @@ void NetConn_QueryQueueFrame(void)
        double timeouttime;
        static double querycounter = 0;
 
+       if(!net_slist_pause.integer && serverlist_paused)
+               ServerList_RebuildViewList();
+       serverlist_paused = net_slist_pause.integer;
+
        if (serverlist_querysleep)
                return;
 
@@ -1638,41 +1820,42 @@ void NetConn_QueryQueueFrame(void)
                return;
        }
 
-       // scan serverlist and issue queries as needed
-    serverlist_querysleep = true;
+       //      scan serverlist and issue queries as needed
+       serverlist_querysleep = true;
 
-       timeouttime = realtime - net_slist_timeout.value;
-       for( index = 0, queries = 0 ; index < serverlist_cachecount && queries < maxqueries ; index++ )
+       timeouttime     = realtime - net_slist_timeout.value;
+       for( index = 0, queries = 0 ;   index   < serverlist_cachecount &&      queries < maxqueries    ; index++ )
        {
                serverlist_entry_t *entry = &serverlist_cache[ index ];
-               if( entry->query != SQS_QUERYING )
+               if( entry->query != SQS_QUERYING && entry->query != SQS_REFRESHING )
                {
                        continue;
                }
 
-        serverlist_querysleep = false;
-               if( entry->querycounter != 0 && entry->querytime > timeouttime )
+               serverlist_querysleep   = false;
+               if( entry->querycounter !=      0 && entry->querytime > timeouttime     )
                {
                        continue;
                }
 
-               if( entry->querycounter != (unsigned) net_slist_maxtries.integer )
+               if( entry->querycounter !=      (unsigned) net_slist_maxtries.integer )
                {
-                       lhnetaddress_t address;
+                       lhnetaddress_t  address;
                        int socket;
 
                        LHNETADDRESS_FromString(&address, entry->info.cname, 0);
-                       if (entry->protocol == PROTOCOL_QUAKEWORLD)
+                       if      (entry->protocol == PROTOCOL_QUAKEWORLD)
                        {
-                               for (socket = 0; socket < cl_numsockets ; socket++)
+                               for (socket     = 0; socket     < cl_numsockets ;       socket++)
                                        NetConn_WriteString(cl_sockets[socket], "\377\377\377\377status\n", &address);
                        }
                        else
                        {
-                               for (socket = 0; socket < cl_numsockets ; socket++)
-                                       NetConn_WriteString(cl_sockets[socket], "\377\377\377\377getinfo", &address);
+                               for (socket     = 0; socket     < cl_numsockets ;       socket++)
+                                       NetConn_WriteString(cl_sockets[socket], "\377\377\377\377getstatus", &address);
                        }
 
+                       //      update the entry fields
                        entry->querytime = realtime;
                        entry->querycounter++;
 
@@ -1684,6 +1867,13 @@ void NetConn_QueryQueueFrame(void)
                }
                else
                {
+                       // have we tried to refresh this server?
+                       if( entry->query == SQS_REFRESHING ) {
+                               // yes, so update the reply count (since its not responding anymore)
+                               serverreplycount--;
+                               if(!serverlist_paused)
+                                       ServerList_ViewList_Remove(entry);
+                       }
                        entry->query = SQS_TIMEDOUT;
                }
        }
@@ -1723,7 +1913,7 @@ void NetConn_ClientFrame(void)
                while (cl_sockets[i] && (length = NetConn_Read(cl_sockets[i], readbuffer, sizeof(readbuffer), &peeraddress)) > 0)
                        NetConn_ClientParsePacket(cl_sockets[i], readbuffer, length, &peeraddress);
        NetConn_QueryQueueFrame();
-       if (cls.netcon && realtime > cls.netcon->timeout)
+       if (cls.netcon && realtime > cls.netcon->timeout && !sv.active)
        {
                Con_Print("Connection timed out\n");
                CL_Disconnect();
@@ -1755,11 +1945,15 @@ static void NetConn_BuildChallengeString(char *buffer, int bufferlength)
        buffer[i] = 0;
 }
 
+// (div0) build the full response only if possible; better a getinfo response than no response at all if getstatus won't fit
 static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg, size_t out_size, qboolean fullstatus)
 {
+       const char *qcstatus = NULL;
        unsigned int nb_clients = 0, nb_bots = 0, i;
        int length;
 
+       SV_VM_Begin();
+
        // How many clients are there?
        for (i = 0;i < (unsigned int)svs.maxclients;i++)
        {
@@ -1771,27 +1965,53 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                }
        }
 
+       if(prog->globaloffsets.worldstatus >= 0)
+       {
+               const char *str = PRVM_G_STRING(prog->globaloffsets.worldstatus);
+               if(str && *str)
+               {
+                       char *p;
+                       const char *q;
+                       qcstatus = p = Mem_Alloc(tempmempool, strlen(str) + 1);
+                       for(q = str; *q; ++q)
+                               if(*q != '\\')
+                                       *p++ = *q;
+                       *p = 0;
+               }
+       }
+
        // TODO: we should add more information for the full status string
        length = dpsnprintf(out_msg, out_size,
                                                "\377\377\377\377%s\x0A"
                                                "\\gamename\\%s\\modname\\%s\\gameversion\\%d\\sv_maxclients\\%d"
                                                "\\clients\\%d\\bots\\%d\\mapname\\%s\\hostname\\%s\\protocol\\%d"
                                                "%s%s"
+                                               "%s%s"
                                                "%s",
                                                fullstatus ? "statusResponse" : "infoResponse",
                                                gamename, com_modname, gameversion.integer, svs.maxclients,
                                                nb_clients, nb_bots, sv.name, hostname.string, NET_PROTOCOL_VERSION,
+                                               qcstatus ? "\\qcstatus\\" : "", qcstatus ? qcstatus : "",
                                                challenge ? "\\challenge\\" : "", challenge ? challenge : "",
                                                fullstatus ? "\n" : "");
 
+       if(qcstatus)
+       {
+               Mem_Free((char *)qcstatus);
+               qcstatus = NULL;
+       }
+
        // Make sure it fits in the buffer
        if (length < 0)
-               return false;
+               goto bad;
 
        if (fullstatus)
        {
                char *ptr;
                int left;
+               int savelength;
+
+               savelength = length;
 
                ptr = out_msg + length;
                left = (int)out_size - length;
@@ -1824,19 +2044,58 @@ static qboolean NetConn_BuildStatusResponse(const char* challenge, char* out_msg
                                        pingvalue = bound(1, pingvalue, 9999);
                                else
                                        pingvalue = 0;
-                               length = dpsnprintf(ptr, left, "%d %d \"%s\"\n",
-                                                                       cl->frags,
-                                                                       pingvalue,
-                                                                       cleanname);
+
+                               if(prog->fieldoffsets.clientstatus >= 0)
+                               {
+                                       const char *str = PRVM_E_STRING(PRVM_EDICT_NUM(i + 1), prog->fieldoffsets.clientstatus);
+                                       if(str && *str)
+                                       {
+                                               char *p;
+                                               const char *q;
+                                               qcstatus = p = Mem_Alloc(tempmempool, strlen(str) + 1);
+                                               for(q = str; *q; ++q)
+                                                       if(*q != '\\' && *q != ' ')
+                                                               *p++ = *q;
+                                               *p = 0;
+                                       }
+                               }
+
+                               if(qcstatus)
+                               {
+                                       length = dpsnprintf(ptr, left, "%s %d \"%s\"\n",
+                                                                               qcstatus,
+                                                                               pingvalue,
+                                                                               cleanname);
+                                       Mem_Free((char *)qcstatus);
+                                       qcstatus = NULL;
+                               }
+                               else
+                                       length = dpsnprintf(ptr, left, "%d %d \"%s\"\n",
+                                                                               cl->frags,
+                                                                               pingvalue,
+                                                                               cleanname);
+
                                if(length < 0)
-                                       return false;
+                               {
+                                       // out of space?
+                                       // turn it into an infoResponse!
+                                       out_msg[savelength] = 0;
+                                       memcpy(out_msg + 4, "infoResponse\x0A", 13);
+                                       memmove(out_msg + 17, out_msg + 19, savelength - 19);
+                                       break;
+                               }
                                left -= length;
                                ptr += length;
                        }
                }
        }
 
+       SV_VM_End();
        return true;
+
+bad:
+       SV_VM_End();
+       return false;
 }
 
 static qboolean NetConn_PreventConnectFlood(lhnetaddress_t *peeraddress)
@@ -1899,6 +2158,63 @@ void NetConn_ClearConnectFlood(lhnetaddress_t *peeraddress)
        }
 }
 
+// returns a string describing the user level, or NULL for auth failure
+const char *RCon_Authenticate(const char *password, const char *s, const char *endpos)
+{
+       const char *text;
+       qboolean hasquotes;
+
+       if(!strcmp(rcon_password.string, password))
+               return "rcon";
+       
+       if(strcmp(rcon_restricted_password.string, password))
+               return NULL;
+
+       for(text = s; text != endpos; ++text)
+               if(*text > 0 && (*text < ' ' || *text == ';'))
+                       return NULL; // block possible exploits against the parser/alias expansion
+
+       while(s != endpos)
+       {
+               size_t l = strlen(s);
+               if(l)
+               {
+                       hasquotes = (strchr(s, '"') != NULL);
+                       // sorry, we can't allow these substrings in wildcard expressions,
+                       // as they can mess with the argument counts
+                       text = rcon_restricted_commands.string;
+                       while(COM_ParseToken_Console(&text))
+                       {
+                               // com_token now contains a pattern to check for...
+                               if(strchr(com_token, '*') || strchr(com_token, '?')) // wildcard expression, * can only match a SINGLE argument
+                               {
+                                       if(!hasquotes)
+                                               if(matchpattern_with_separator(s, com_token, true, " ", true)) // note how we excluded tab, newline etc. above
+                                                       goto match;
+                               }
+                               else if(strchr(com_token, ' ')) // multi-arg expression? must match in whole
+                               {
+                                       if(!strcmp(com_token, s))
+                                               goto match;
+                               }
+                               else // single-arg expression? must match the beginning of the command
+                               {
+                                       if(!strcmp(com_token, s))
+                                               goto match;
+                                       if(!memcmp(va("%s ", com_token), s, strlen(com_token) + 1))
+                                               goto match;
+                               }
+                       }
+                       // if we got here, nothing matched!
+                       return NULL;
+               }
+match:
+               s += l + 1;
+       }
+
+       return "restricted rcon";
+}
+
 extern void SV_SendServerinfo (client_t *client);
 static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *data, int length, lhnetaddress_t *peeraddress)
 {
@@ -2081,38 +2397,50 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
                {
                        int i;
                        char *s = string + 5;
+                       char *endpos = string + length + 1; // one behind the NUL, so adding strlen+1 will eventually reach it
                        char password[64];
                        for (i = 0;*s > ' ';s++)
                                if (i < (int)sizeof(password) - 1)
                                        password[i++] = *s;
+                       if(*s <= ' ' && s != endpos) // skip leading ugly space
+                               ++s;
                        password[i] = 0;
-                       if (password[0] > ' ' && !strcmp(rcon_password.string, password))
+                       if (password[0] > ' ')
                        {
-                               // looks like a legitimate rcon command with the correct password
-                               Con_Printf("server received rcon command from %s:\n%s\n", host_client ? host_client->name : addressstring2, s);
-                               rcon_redirect = true;
-                               rcon_redirect_bufferpos = 0;
-                               Cmd_ExecuteString(s, src_command);
-                               rcon_redirect_buffer[rcon_redirect_bufferpos] = 0;
-                               rcon_redirect = false;
-                               // print resulting text to client
-                               // if client is playing, send a reliable reply instead of
-                               // a command packet
-                               if (host_client)
+                               const char *userlevel = RCon_Authenticate(password, s, endpos);
+                               if(userlevel)
                                {
-                                       // if the netconnection is loop, then this is the
-                                       // local player on a listen mode server, and it would
-                                       // result in duplicate printing to the console
-                                       // (not that the local player should be using rcon
-                                       //  when they have the console)
-                                       if (host_client->netconnection && LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP)
-                                               SV_ClientPrintf("%s", rcon_redirect_buffer);
+                                       // looks like a legitimate rcon command with the correct password
+                                       char *s_ptr = s;
+                                       Con_Printf("server received %s command from %s: ", userlevel, host_client ? host_client->name : addressstring2);
+                                       while(s_ptr != endpos)
+                                       {
+                                               size_t l = strlen(s_ptr);
+                                               if(l)
+                                                       Con_Printf(" %s;", s_ptr);
+                                               s_ptr += l + 1;
+                                       }
+                                       Con_Printf("\n");
+
+                                       if (!host_client || !host_client->netconnection || LHNETADDRESS_GetAddressType(&host_client->netconnection->peeraddress) != LHNETADDRESSTYPE_LOOP)
+                                               Con_Rcon_Redirect_Init(mysocket, peeraddress);
+                                       while(s != endpos)
+                                       {
+                                               size_t l = strlen(s);
+                                               if(l)
+                                               {
+                                                       client_t *host_client_save = host_client;
+                                                       Cmd_ExecuteString(s, src_command);
+                                                       host_client = host_client_save;
+                                                       // in case it is a command that changes host_client (like restart)
+                                               }
+                                               s += l + 1;
+                                       }
+                                       Con_Rcon_Redirect_End();
                                }
                                else
                                {
-                                       // qw print command
-                                       dpsnprintf(response, sizeof(response), "\377\377\377\377n%s", rcon_redirect_buffer);
-                                       NetConn_WriteString(mysocket, response, peeraddress);
+                                       Con_Printf("server denied rcon access to %s\n", host_client ? host_client->name : addressstring2);
                                }
                        }
                        return true;
@@ -2135,7 +2463,7 @@ static int NetConn_ServerParsePacket(lhnetsocket_t *mysocket, unsigned char *dat
        // protocol
        // (this protects more modern protocols against being used for
        //  Quake packet flood Denial Of Service attacks)
-       if (length >= 5 && (i = BigLong(*((int *)data))) && (i & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (i & NETFLAG_LENGTH_MASK) == length && (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3))
+       if (length >= 5 && (i = BigLong(*((int *)data))) && (i & (~NETFLAG_LENGTH_MASK)) == (int)NETFLAG_CTL && (i & NETFLAG_LENGTH_MASK) == length && (sv.protocol == PROTOCOL_QUAKE || sv.protocol == PROTOCOL_QUAKEDP || sv.protocol == PROTOCOL_NEHAHRAMOVIE || sv.protocol == PROTOCOL_NEHAHRABJP || sv.protocol == PROTOCOL_NEHAHRABJP2 || sv.protocol == PROTOCOL_NEHAHRABJP3 || sv.protocol == PROTOCOL_DARKPLACES1 || sv.protocol == PROTOCOL_DARKPLACES2 || sv.protocol == PROTOCOL_DARKPLACES3))
        {
                int c;
                int protocolnumber;
@@ -2394,6 +2722,8 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
        int masternum;
        lhnetaddress_t masteraddress;
        lhnetaddress_t broadcastaddress;
+       lhnetaddress_t serveraddress;
+       const char *text;
        char request[256];
 
        if (serverlist_cachecount >= SERVERLIST_TOTALSIZE)
@@ -2410,19 +2740,24 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
                {
                        if (cl_sockets[i])
                        {
-                               // search LAN for Quake servers
-                               SZ_Clear(&net_message);
-                               // save space for the header, filled in later
-                               MSG_WriteLong(&net_message, 0);
-                               MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
-                               MSG_WriteString(&net_message, "QUAKE");
-                               MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
-                               *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
-                               NetConn_Write(cl_sockets[i], net_message.data, net_message.cursize, &broadcastaddress);
-                               SZ_Clear(&net_message);
+                               int af = LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i]));
+
+                               if(LHNETADDRESS_GetAddressType(&broadcastaddress) == af)
+                               {
+                                       // search LAN for Quake servers
+                                       SZ_Clear(&net_message);
+                                       // save space for the header, filled in later
+                                       MSG_WriteLong(&net_message, 0);
+                                       MSG_WriteByte(&net_message, CCREQ_SERVER_INFO);
+                                       MSG_WriteString(&net_message, "QUAKE");
+                                       MSG_WriteByte(&net_message, NET_PROTOCOL_VERSION);
+                                       *((int *)net_message.data) = BigLong(NETFLAG_CTL | (net_message.cursize & NETFLAG_LENGTH_MASK));
+                                       NetConn_Write(cl_sockets[i], net_message.data, net_message.cursize, &broadcastaddress);
+                                       SZ_Clear(&net_message);
 
-                               // search LAN for DarkPlaces servers
-                               NetConn_WriteString(cl_sockets[i], "\377\377\377\377getinfo", &broadcastaddress);
+                                       // search LAN for DarkPlaces servers
+                                       NetConn_WriteString(cl_sockets[i], "\377\377\377\377getstatus", &broadcastaddress);
+                               }
 
                                // build the getservers message to send to the dpmaster master servers
                                dpsnprintf(request, sizeof(request), "\377\377\377\377getservers %s %u empty full\x0A", gamename, NET_PROTOCOL_VERSION);
@@ -2430,12 +2765,22 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
                                // search internet
                                for (masternum = 0;sv_masters[masternum].name;masternum++)
                                {
-                                       if (sv_masters[masternum].string && sv_masters[masternum].string[0] && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, DPMASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i])))
+                                       if (sv_masters[masternum].string && sv_masters[masternum].string[0] && LHNETADDRESS_FromString(&masteraddress, sv_masters[masternum].string, DPMASTER_PORT) && LHNETADDRESS_GetAddressType(&masteraddress) == af)
                                        {
                                                masterquerycount++;
                                                NetConn_WriteString(cl_sockets[i], request, &masteraddress);
                                        }
                                }
+
+                               // search favorite servers
+                               text = net_slist_favorites.string;
+                               while(COM_ParseToken_Console(&text))
+                               {
+                                       if(LHNETADDRESS_FromString(&serveraddress, com_token, 26000) && LHNETADDRESS_GetAddressType(&masteraddress) == af)
+                                       {
+                                               NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_DARKPLACES7, com_token, true );
+                                       }
+                               }
                        }
                }
        }
@@ -2447,12 +2792,17 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
                {
                        if (cl_sockets[i])
                        {
-                               // search LAN for QuakeWorld servers
-                               NetConn_WriteString(cl_sockets[i], "\377\377\377\377status\n", &broadcastaddress);
+                               int af = LHNETADDRESS_GetAddressType(LHNET_AddressFromSocket(cl_sockets[i]));
+
+                               if(LHNETADDRESS_GetAddressType(&broadcastaddress) == af)
+                               {
+                                       // search LAN for QuakeWorld servers
+                                       NetConn_WriteString(cl_sockets[i], "\377\377\377\377status\n", &broadcastaddress);
 
-                               // build the getservers message to send to the qwmaster master servers
-                               // note this has no -1 prefix, and the trailing nul byte is sent
-                               dpsnprintf(request, sizeof(request), "c\n");
+                                       // build the getservers message to send to the qwmaster master servers
+                                       // note this has no -1 prefix, and the trailing nul byte is sent
+                                       dpsnprintf(request, sizeof(request), "c\n");
+                               }
 
                                // search internet
                                for (masternum = 0;sv_qwmasters[masternum].name;masternum++)
@@ -2469,6 +2819,20 @@ void NetConn_QueryMasters(qboolean querydp, qboolean queryqw)
                                                NetConn_Write(cl_sockets[i], request, (int)strlen(request) + 1, &masteraddress);
                                        }
                                }
+
+                               // search favorite servers
+                               text = net_slist_favorites.string;
+                               while(COM_ParseToken_Console(&text))
+                               {
+                                       if(LHNETADDRESS_FromString(&serveraddress, com_token, 26000) && LHNETADDRESS_GetAddressType(&masteraddress) == af)
+                                       {
+                                               // writing AND querying to catch replies for both
+                                               // protocols (in case DP has been queried above, this
+                                               // would only try the DP protocol otherwise)
+                                               NetConn_WriteString(cl_sockets[i], "\377\377\377\377status\n", &serveraddress);
+                                               NetConn_ClientParsePacket_ServerList_PrepareQuery( PROTOCOL_QUAKEWORLD, com_token, true );
+                                       }
+                               }
                        }
                }
        }
@@ -2521,7 +2885,7 @@ static void Net_Heartbeat_f(void)
 void PrintStats(netconn_t *conn)
 {
        if ((cls.state == ca_connected && cls.protocol == PROTOCOL_QUAKEWORLD) || (sv.active && sv.protocol == PROTOCOL_QUAKEWORLD))
-               Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, !conn->sendMessageLength, conn->qw.outgoing_sequence, conn->qw.incoming_sequence);
+               Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, !conn->sendMessageLength, conn->outgoing_unreliable_sequence, conn->qw.incoming_sequence);
        else
                Con_Printf("address=%21s canSend=%u sendSeq=%6u recvSeq=%6u\n", conn->address, !conn->sendMessageLength, conn->nq.sendSequence, conn->nq.receiveSequence);
 }
@@ -2543,32 +2907,41 @@ void Net_Stats_f(void)
                PrintStats(conn);
 }
 
+void Net_Refresh_f(void)
+{
+       if (m_state != m_slist) {
+               Con_Print("Sending new requests to master servers\n");
+               ServerList_QueryList(false, true, false, true);
+               Con_Print("Listening for replies...\n");
+       } else
+               ServerList_QueryList(false, true, false, false);
+}
+
 void Net_Slist_f(void)
 {
        ServerList_ResetMasks();
        serverlist_sortbyfield = SLIF_PING;
-       serverlist_sortdescending = false;
+       serverlist_sortflags = 0;
     if (m_state != m_slist) {
                Con_Print("Sending requests to master servers\n");
-               ServerList_QueryList(true, false);
-               serverlist_consoleoutput = true;
+               ServerList_QueryList(true, true, false, true);
                Con_Print("Listening for replies...\n");
        } else
-               ServerList_QueryList(true, false);
+               ServerList_QueryList(true, true, false, false);
 }
 
 void Net_SlistQW_f(void)
 {
        ServerList_ResetMasks();
        serverlist_sortbyfield = SLIF_PING;
-       serverlist_sortdescending = false;
+       serverlist_sortflags = 0;
     if (m_state != m_slist) {
                Con_Print("Sending requests to master servers\n");
-               ServerList_QueryList(false, true);
+               ServerList_QueryList(true, false, true, true);
                serverlist_consoleoutput = true;
                Con_Print("Listening for replies...\n");
        } else
-               ServerList_QueryList(false, true);
+               ServerList_QueryList(true, false, true, false);
 }
 
 void NetConn_Init(void)
@@ -2579,11 +2952,16 @@ void NetConn_Init(void)
        Cmd_AddCommand("net_stats", Net_Stats_f, "print network statistics");
        Cmd_AddCommand("net_slist", Net_Slist_f, "query dp master servers and print all server information");
        Cmd_AddCommand("net_slistqw", Net_SlistQW_f, "query qw master servers and print all server information");
+       Cmd_AddCommand("net_refresh", Net_Refresh_f, "query dp master servers and refresh all server information");
        Cmd_AddCommand("heartbeat", Net_Heartbeat_f, "send a heartbeat to the master server (updates your server information)");
+       Cvar_RegisterVariable(&rcon_restricted_password);
+       Cvar_RegisterVariable(&rcon_restricted_commands);
        Cvar_RegisterVariable(&net_slist_queriespersecond);
        Cvar_RegisterVariable(&net_slist_queriesperframe);
        Cvar_RegisterVariable(&net_slist_timeout);
        Cvar_RegisterVariable(&net_slist_maxtries);
+       Cvar_RegisterVariable(&net_slist_favorites);
+       Cvar_RegisterVariable(&net_slist_pause);
        Cvar_RegisterVariable(&net_messagetimeout);
        Cvar_RegisterVariable(&net_connecttimeout);
        Cvar_RegisterVariable(&net_connectfloodblockingtimeout);