summaryrefslogtreecommitdiff
path: root/serverbrowser/BlacklistedServers.cpp
blob: c310f3912726857cd9b7b39a092481bec9f67c2d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================

#include "pch_serverbrowser.h"

using namespace vgui;

ConVar sb_showblacklists( "sb_showblacklists", "0", FCVAR_NONE, "If set to 1, blacklist rules will be printed to the console as they're applied." );

//-----------------------------------------------------------------------------
// Purpose: Server name comparison function
//-----------------------------------------------------------------------------
int __cdecl BlacklistedServerNameCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
{
	blacklisted_server_t *pSvr1 = ServerBrowserDialog().GetBlacklistPage()->GetBlacklistedServer( p1.userData );
	blacklisted_server_t *pSvr2 = ServerBrowserDialog().GetBlacklistPage()->GetBlacklistedServer( p2.userData );

	if ( !pSvr1 && pSvr2 ) 
		return -1;
	if ( !pSvr2 && pSvr1 )
		return 1;
	if ( !pSvr1 && !pSvr2 )
		return 0;

	return Q_stricmp( pSvr1->m_szServerName, pSvr2->m_szServerName );
}

//-----------------------------------------------------------------------------
// Purpose: list column sort function
//-----------------------------------------------------------------------------
int __cdecl BlacklistedIPAddressCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
{
	blacklisted_server_t *pSvr1 = ServerBrowserDialog().GetBlacklistPage()->GetBlacklistedServer( p1.userData );
	blacklisted_server_t *pSvr2 = ServerBrowserDialog().GetBlacklistPage()->GetBlacklistedServer( p2.userData );

	if ( !pSvr1 && pSvr2 ) 
		return -1;
	if ( !pSvr2 && pSvr1 )
		return 1;
	if ( !pSvr1 && !pSvr2 )
		return 0;

	if ( pSvr1->m_NetAdr < pSvr2->m_NetAdr )
		return -1;
	else if ( pSvr2->m_NetAdr < pSvr1->m_NetAdr )
		return 1;

	return 0;
}

//-----------------------------------------------------------------------------
// Purpose: Player number comparison function
//-----------------------------------------------------------------------------
int __cdecl BlacklistedAtCompare(ListPanel *pPanel, const ListPanelItem &p1, const ListPanelItem &p2)
{
	blacklisted_server_t *pSvr1 = ServerBrowserDialog().GetBlacklistPage()->GetBlacklistedServer( p1.userData );
	blacklisted_server_t *pSvr2 = ServerBrowserDialog().GetBlacklistPage()->GetBlacklistedServer( p2.userData );

	if ( !pSvr1 && pSvr2 ) 
		return -1;
	if ( !pSvr2 && pSvr1 )
		return 1;
	if ( !pSvr1 && !pSvr2 )
		return 0;

	if ( pSvr1->m_ulTimeBlacklistedAt > pSvr2->m_ulTimeBlacklistedAt )
		return -1;
	if ( pSvr1->m_ulTimeBlacklistedAt < pSvr2->m_ulTimeBlacklistedAt )
		return 1;

	return 0;
}

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CBlacklistedServers::CBlacklistedServers( vgui::Panel *parent ) : 
	vgui::PropertyPage( parent, "BlacklistedGames" )
{
	SetSize( 624, 278 );

	m_pAddServer = new Button(this, "AddServerButton", "#ServerBrowser_AddServer");
	m_pAddCurrentServer = new Button(this, "AddCurrentServerButton", "#ServerBrowser_AddCurrentServer");
	m_pGameList = vgui::SETUP_PANEL( new vgui::ListPanel(this, "gamelist") );
	m_pGameList->SetAllowUserModificationOfColumns(true);

	// Add the column headers
	m_pGameList->AddColumnHeader(0, "Name", "#ServerBrowser_BlacklistedServers", 50, ListPanel::COLUMN_RESIZEWITHWINDOW | ListPanel::COLUMN_UNHIDABLE);
	m_pGameList->AddColumnHeader(1, "IPAddr", "#ServerBrowser_IPAddress", 64, ListPanel::COLUMN_HIDDEN);
	m_pGameList->AddColumnHeader(2, "BlacklistedAt", "#ServerBrowser_BlacklistedDate", 100);

	//m_pGameList->SetColumnHeaderTooltip(0, "#ServerBrowser_PasswordColumn_Tooltip");

	// setup fast sort functions
	m_pGameList->SetSortFunc(0, BlacklistedServerNameCompare);
	m_pGameList->SetSortFunc(1, BlacklistedIPAddressCompare);
	m_pGameList->SetSortFunc(2, BlacklistedAtCompare);

	// Sort by name by default
	m_pGameList->SetSortColumn(0);

	m_blackList.Reset();
	m_blackListTimestamp = 0;
}

//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
CBlacklistedServers::~CBlacklistedServers()
{
	ClearServerList();
}

//-----------------------------------------------------------------------------
// Purpose: loads the initial blacklist from disk
//-----------------------------------------------------------------------------
void CBlacklistedServers::LoadBlacklistedList()
{
	m_pGameList->SetEmptyListText("#ServerBrowser_NoBlacklistedServers");

	ClearServerList();
	m_blackList.Reset();

	int count = m_blackList.LoadServersFromFile( BLACKLIST_DEFAULT_SAVE_FILE, false );

	m_blackListTimestamp = g_pFullFileSystem->GetFileTime( BLACKLIST_DEFAULT_SAVE_FILE );

	for( int i=0; i<count; ++i )
	{
		UpdateBlacklistUI( m_blackList.GetServer(i) );
	}
}

//-----------------------------------------------------------------------------
// Purpose: adds all the servers inside the specified file to the blacklist
//-----------------------------------------------------------------------------
bool CBlacklistedServers::AddServersFromFile( const char *pszFilename, bool bResetTimes )
{
	KeyValues *pKV = new KeyValues( "serverblacklist" );
	if ( !pKV->LoadFromFile( g_pFullFileSystem, pszFilename, "GAME" ) )
		return false;

	for ( KeyValues *pData = pKV->GetFirstSubKey(); pData != NULL; pData = pData->GetNextKey() )
	{
		const char *pszName = pData->GetString( "name" );

		uint32 ulDate = pData->GetInt( "date" );
		if ( bResetTimes )
		{
			time_t today;
			time( &today );
			ulDate = today;
		}

		const char *pszNetAddr = pData->GetString( "addr" );

		if ( pszNetAddr && pszNetAddr[0] && pszName && pszName[0] )
		{
			blacklisted_server_t *blackServer = m_blackList.AddServer( pszName, pszNetAddr, ulDate );

			UpdateBlacklistUI( blackServer );
		}
	}

	// write out blacklist to preserve changes
	SaveBlacklistedList();

	pKV->deleteThis();

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: save blacklist to disk
//-----------------------------------------------------------------------------
void CBlacklistedServers::SaveBlacklistedList()
{
	m_blackList.SaveToFile( BLACKLIST_DEFAULT_SAVE_FILE );
	m_blackListTimestamp = g_pFullFileSystem->GetFileTime( BLACKLIST_DEFAULT_SAVE_FILE );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBlacklistedServers::AddServer( gameserveritem_t &server )
{
	blacklisted_server_t *blackServer = m_blackList.AddServer( server );

	if ( !blackServer )
	{
		return;
	}

	SaveBlacklistedList();

	UpdateBlacklistUI( blackServer );

	if ( GameSupportsReplay() )
	{
		// send command to propagate to the client so the client can send it on to the GC
		char command[ 256 ];
		Q_snprintf( command, Q_ARRAYSIZE( command ), "rbgc %s\n", blackServer->m_NetAdr.ToString() );
		g_pRunGameEngine->AddTextCommand( command );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
blacklisted_server_t *CBlacklistedServers::GetBlacklistedServer( int iServerID )
{
	return m_blackList.GetServer( iServerID );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
bool CBlacklistedServers::IsServerBlacklisted( gameserveritem_t &server )
{
	// if something has changed the blacklist file, reload it
	if ( g_pFullFileSystem->GetFileTime( BLACKLIST_DEFAULT_SAVE_FILE ) != m_blackListTimestamp )
	{
		LoadBlacklistedList();
	}

	return m_blackList.IsServerBlacklisted( server );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBlacklistedServers::UpdateBlacklistUI( blacklisted_server_t *blackServer )
{
	if ( !blackServer )
		return;

	KeyValues *kv;
	int iItemId = m_pGameList->GetItemIDFromUserData( blackServer->m_nServerID );
	if ( m_pGameList->IsValidItemID( iItemId ) )
	{
		// we're updating an existing entry
		kv = m_pGameList->GetItem( iItemId );
		m_pGameList->SetUserData( iItemId, blackServer->m_nServerID );
	}
	else
	{
		// new entry
		kv = new KeyValues("Server");
	}

	kv->SetString( "name", blackServer->m_szServerName );

	// construct a time string for blacklisted time
	struct tm *now;
	now = localtime( (time_t*)&blackServer->m_ulTimeBlacklistedAt );
	if ( now ) 
	{
		char buf[64];
		strftime(buf, sizeof(buf), "%a %d %b %I:%M%p", now);
		Q_strlower(buf + strlen(buf) - 4);
		kv->SetString("BlacklistedAt", buf);
	}

	kv->SetString( "IPAddr", blackServer->m_NetAdr.ToString() );

	if ( !m_pGameList->IsValidItemID( iItemId ) )
	{
		// new server, add to list
		iItemId = m_pGameList->AddItem(kv, blackServer->m_nServerID, false, false);
		kv->deleteThis();
	}
	else
	{
		// tell the list that we've changed the data
		m_pGameList->ApplyItemChanges( iItemId );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBlacklistedServers::ApplySchemeSettings(vgui::IScheme *pScheme)
{
	BaseClass::ApplySchemeSettings(pScheme);

	const char *pPathID = "PLATFORM";
	const char *pszFileName = "servers/BlacklistedServersPage.res";
	if ( g_pFullFileSystem->FileExists( pszFileName, "MOD" ) )
	{
		pPathID = "MOD";
	}	
	LoadControlSettings( pszFileName, pPathID );

	vgui::HFont hFont = pScheme->GetFont( "ListSmall", IsProportional() );
	if ( !hFont )
	{
		hFont = pScheme->GetFont( "DefaultSmall", IsProportional() );
	}
	m_pGameList->SetFont( hFont );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnPageShow( void )
{
	// reload list since client may have changed it
	LoadBlacklistedList();

	m_pGameList->SetEmptyListText("#ServerBrowser_NoBlacklistedServers");
	m_pGameList->SortList();

	BaseClass::OnPageShow();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
int CBlacklistedServers::GetSelectedServerID( void )
{
	int serverID = -1;
	if ( m_pGameList->GetSelectedItemsCount() )
	{
		serverID = m_pGameList->GetItemUserData( m_pGameList->GetSelectedItem(0) );
	}

	return serverID;
}

//-----------------------------------------------------------------------------
// Purpose: opens context menu (user right clicked on a server)
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnOpenContextMenu(int itemID)
{
	CServerContextMenu *menu = ServerBrowserDialog().GetContextMenu( m_pGameList );

	// get the server
	int serverID = GetSelectedServerID();

	menu->ShowMenu( this,(uint32)-1, false, false, false, false );
	if ( serverID != -1 )
	{
		menu->AddMenuItem("RemoveServer", "#ServerBrowser_RemoveServerFromBlacklist", new KeyValues("RemoveFromBlacklist"), this);
	}

	menu->AddMenuItem("AddServerByName", "#ServerBrowser_AddServerByIP", new KeyValues("AddServerByName"), this);
}


//-----------------------------------------------------------------------------
// Purpose: Adds a server by IP address
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnAddServerByName()
{
	// open the add server dialog
	CDialogAddBlacklistedServer *dlg = new CDialogAddBlacklistedServer( &ServerBrowserDialog(), NULL );
	dlg->MoveToCenterOfScreen();
	dlg->DoModal();
}

//-----------------------------------------------------------------------------
// Purpose: removes a server from the blacklist
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnRemoveFromBlacklist()
{
	// iterate the selection
	for ( int iGame = (m_pGameList->GetSelectedItemsCount() - 1); iGame >= 0; iGame-- )
	{
		int itemID = m_pGameList->GetSelectedItem( iGame );
		int serverID = m_pGameList->GetItemData( itemID )->userData;

		m_pGameList->RemoveItem( itemID );
		m_blackList.RemoveServer( serverID );
	}

	InvalidateLayout();
	Repaint();

	ServerBrowserDialog().BlacklistsChanged();

	SaveBlacklistedList();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBlacklistedServers::ClearServerList( void )
{
	m_pGameList->RemoveAll();
	m_blackList.Reset();
	m_blackListTimestamp = 0;
}

//-----------------------------------------------------------------------------
// Purpose: Adds the currently connected server to the list
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnAddCurrentServer()
{
	gameserveritem_t *pConnected = ServerBrowserDialog().GetCurrentConnectedServer();

	if ( pConnected )
	{
		blacklisted_server_t *blackServer = m_blackList.AddServer( *pConnected );

		if ( !blackServer )
		{
			return;
		}

		UpdateBlacklistUI( blackServer );

		ServerBrowserDialog().BlacklistsChanged();

		SaveBlacklistedList();

		if ( GameSupportsReplay() )
		{
			// send command to propagate to the client so the client can send it on to the GC
			char command[ 256 ];
			Q_snprintf( command, Q_ARRAYSIZE( command ), "rbgc %s\n", pConnected->m_NetAdr.GetConnectionAddressString() );
			g_pRunGameEngine->AddTextCommand( command );
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnImportBlacklist()
{
	if ( m_hImportDialog.Get() )
	{
		m_hImportDialog.Get()->MarkForDeletion();
	}

	m_hImportDialog = new FileOpenDialog( this, "#ServerBrowser_ImportBlacklistTitle", true );
	if ( m_hImportDialog.Get() )
	{
		m_hImportDialog->SetStartDirectory( "cfg/" );
		m_hImportDialog->AddFilter( "*.txt", "#ServerBrowser_BlacklistFiles", true );
		m_hImportDialog->DoModal( false );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnFileSelected( char const *fullpath )
{
	AddServersFromFile( fullpath, true );

	if ( m_hImportDialog.Get() )
	{
		m_hImportDialog.Get()->MarkForDeletion();
	}
}

//-----------------------------------------------------------------------------
// Purpose: Parse posted messages
//			 
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnCommand(const char *command)
{
	if (!Q_stricmp(command, "AddServerByName"))
	{
		OnAddServerByName();
	}
	else if (!Q_stricmp(command, "AddCurrentServer" ))
	{
		OnAddCurrentServer();
	}
	else if (!Q_stricmp(command, "ImportBlacklist" ))
	{
		OnImportBlacklist();
	}
	else
	{
		BaseClass::OnCommand(command);
	}
}

//-----------------------------------------------------------------------------
// Purpose: enables adding server
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnConnectToGame()
{
	m_pAddCurrentServer->SetEnabled( true );
}

//-----------------------------------------------------------------------------
// Purpose: disables adding current server
//-----------------------------------------------------------------------------
void CBlacklistedServers::OnDisconnectFromGame( void )
{
	m_pAddCurrentServer->SetEnabled( false );
}