Public Functions
ConsoleDoc("@class <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">BanList\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Miscellaneous\n</a>" "@brief Used <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> kicking and banning players from <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">server.\n</a>" "There is only <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> single instance of BanList. It is very important <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> note that you do not ever create this object in script " "like you would other game play objects. You simply reference it via <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">namespace.\n\n</a>" "For this <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> be used effectively, make sure you are hooking up other functions <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> BanList. " "For example, functions like GameConnection::onConnectRequestRejected(%this, %msg) and function GameConnection::onConnectRequest are excellent " "places <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> make use of the BanList. Other systems can be used in conjunction <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> strict <a href="/coding/file/guieditctrl_8cpp/#guieditctrl_8cpp_1abb04e3738c4c5a96b3ade6fa47013a6c">control</a> over <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">server\n\n</a>" " @see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">addBadWord\n</a>" " @see <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">containsBadWords\n</a>" )
DefineEngineStaticMethod(BanList , add , void , (S32 uniqueId, const char *transportAddress, S32 banLength) , "Ban <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> user <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> banLength <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">seconds.\n\n</a>" "@param uniqueId Unique ID of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">player.\n</a>" "@param transportAddress Address from which the player <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">connected.\n</a>" "@param banLength Time period over which <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> ban the player." "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "// Kick someone off the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">server\n</a>" "// %client - This is the connection <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the person we are <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">kicking\n</a>" "function kick(%client)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " // Let the server know what <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">happened\n</a>" " messageAll( 'MsgAdminForce', '\\c2The Admin has kicked %1.', %client.playerName);\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" " // If it is not an AI <a href="/coding/class/classplayer/">Player</a>, execute the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">ban.\n</a>" " <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> (!%client.isAIControlled())\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " BanList::add(%client.guid, %client.getAddress(), $pref::Server::KickBanTime);\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" " // Let the player know they messed <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">up\n</a>" " %client.delete(\"You have been kicked from this server\");\n" "}\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" )
DefineEngineStaticMethod(BanList , addAbsolute , void , (S32 uniqueId, const char *transportAddress, S32 banTime) , "Ban <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> user until <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> given <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">time.\n\n</a>" "@param uniqueId Unique ID of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">player.\n</a>" "@param transportAddress Address from which the player <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">connected.\n</a>" "@param banTime Time at which they will be allowed back in." "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "// Kick someone off the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">server\n</a>" "// %client - This is the connection <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the person we are <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">kicking\n</a>" "function kick(%client)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " // Let the server know what <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">happened\n</a>" " messageAll( 'MsgAdminForce', '\\c2The Admin has kicked %1.', %client.playerName);\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" " // If it is not an AI <a href="/coding/class/classplayer/">Player</a>, execute the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">ban.\n</a>" " <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> (!%client.isAIControlled())\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " BanList::addAbsolute(%client.guid, %client.getAddress(), $pref::Server::KickBanTime);\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" " // Let the player know they messed <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">up\n</a>" " %client.delete(\"You have been kicked from this server\");\n" "}\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" )
DefineEngineStaticMethod(BanList , export , void , (const char *filename) , "Dump the banlist <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">file.\n\n</a>" "@param filename Path of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a702945180aa732857b380a007a7e2a21">file</a> <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> write the list <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">to.\n\n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "BanList::Export(\"./server/banlist." TORQUE_SCRIPT_EXTENSION "\");\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" )
DefineEngineStaticMethod(BanList , isBanned , bool , (S32 uniqueId, const char *transportAddress) , "Is someone banned?\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" "@param uniqueId Unique ID of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">player.\n</a>" "@param transportAddress Address from which the player <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">connected.\n\n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "//-----------------------------------------------------------------------------\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "// This script function is called before <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> client <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">connection\n</a>" "// is accepted. Returning "" will accept the connection, \<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "//anything else will be sent back as an error <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">client.\n</a>" "//All the connect args are passed also <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">onConnectRequest\n</a>" "function GameConnection::onConnectRequest(%client, %netAddress, %name)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "{\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "//Find out who is trying <a href="/coding/file/cmdgram_8cpp/#cmdgram_8cpp_1a5bafda9519252aa2d0fd038153f77dca">to</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">connect\n</a>" " <a href="/coding/file/ggendoflinefix_8txt/#ggendoflinefix_8txt_1a7f8fee038817022b75b8865e5da4eba3">echo</a>(\"Connect request from: \" @ %netAddress);\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n\n</a>" " // Are they allowed in?\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a>(<a href="/coding/class/classbanlist/#classbanlist_1a9f9bf319c07c348a270c458729eb0321">BanList::isBanned</a>(%client.guid, %netAddress))\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " return \"CR_YOUAREBANNED\";\n\n" " // Is there room <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> an unbanned player?\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a>($Server::PlayerCount >= $pref::Server::MaxPlayers)\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" " return \"CR_SERVERFULL\";\n" " return "";\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "}\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" )
DefineEngineStaticMethod(BanList , removeBan , void , (S32 uniqueId, const char *transportAddress) , "Unban <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">someone.\n\n</a>" "@param uniqueId Unique ID of the <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">player.\n</a>" "@param transportAddress Address from which the player <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">connected.\n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">tsexample\n</a>" "<a href="/coding/class/classbanlist/#classbanlist_1a469d6b76f34dd62a0fea88271911543e">BanList::removeBan</a>(%userID, %ipAddress);\<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">n</a>" "@<a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">endtsexample\n\n</a>" )
IMPLEMENT_STATIC_CLASS(BanList , "Functions <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> maintaing <a href="/coding/file/pointer_8h/#pointer_8h_1aeeddce917cf130d62c370b8f216026dd">a</a> list of banned users." )
1
2//-----------------------------------------------------------------------------
3// Copyright (c) 2012 GarageGames, LLC
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to
7// deal in the Software without restriction, including without limitation the
8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22//-----------------------------------------------------------------------------
23
24#include "app/banList.h"
25#include "core/stream/fileStream.h"
26#include "core/module.h"
27#include "console/engineAPI.h"
28
29
30IMPLEMENT_STATIC_CLASS( BanList,, "Functions for maintaing a list of banned users." );
31
32ConsoleDoc(
33 "@class BanList\n"
34 "@ingroup Miscellaneous\n"
35 "@brief Used for kicking and banning players from a server.\n"
36 "There is only a single instance of BanList. It is very important to note that you do not ever create this object in script "
37 "like you would other game play objects. You simply reference it via namespace.\n\n"
38 "For this to be used effectively, make sure you are hooking up other functions to BanList. "
39 "For example, functions like GameConnection::onConnectRequestRejected( %this, %msg ) and function GameConnection::onConnectRequest are excellent "
40 "places to make use of the BanList. Other systems can be used in conjunction for strict control over a server\n\n"
41 "@see addBadWord\n"
42 "@see containsBadWords\n"
43);
44
45BanList* BanList::smInstance;
46
47MODULE_BEGIN( BanList )
48 MODULE_INIT
49 {
50 new BanList;
51 }
52 MODULE_SHUTDOWN
53 {
54 delete BanList::instance();
55 }
56MODULE_END;
57
58
59//------------------------------------------------------------------------------
60
61BanList::BanList()
62{
63 AssertFatal( !smInstance, "BanList::BanList - already instantiated" );
64 VECTOR_SET_ASSOCIATION( list );
65
66 smInstance = this;
67}
68
69//------------------------------------------------------------------------------
70
71void BanList::addBan(S32 uniqueId, const char *TA, S32 banTime)
72{
73 S32 curTime = Platform::getTime();
74
75 if(banTime != 0 && banTime < curTime)
76 return;
77
78 // make sure this bastard isn't already banned on this server
79 Vector<BanInfo>::iterator i;
80 for(i = list.begin();i != list.end();i++)
81 {
82 if(uniqueId == i->uniqueId)
83 {
84 i->bannedUntil = banTime;
85 return;
86 }
87 }
88
89 BanInfo b;
90 dStrcpy(b.transportAddress, TA, 128);
91 b.uniqueId = uniqueId;
92 b.bannedUntil = banTime;
93
94 if(!dStrnicmp(b.transportAddress, "ip:", 3))
95 {
96 char *c = dStrchr(b.transportAddress+3, ':');
97 if(c)
98 {
99 *(c+1) = '*';
100 *(c+2) = 0;
101 }
102 }
103
104 list.push_back(b);
105}
106
107//------------------------------------------------------------------------------
108
109void BanList::addBanRelative(S32 uniqueId, const char *TA, S32 numSeconds)
110{
111 S32 curTime = Platform::getTime();
112 S32 banTime = 0;
113 if(numSeconds != -1)
114 banTime = curTime + numSeconds;
115
116 addBan(uniqueId, TA, banTime);
117}
118
119//------------------------------------------------------------------------------
120
121void BanList::removeBan(S32 uniqueId, const char *)
122{
123 Vector<BanInfo>::iterator i;
124 for(i = list.begin();i != list.end();i++)
125 {
126 if(uniqueId == i->uniqueId)
127 {
128 list.erase(i);
129 return;
130 }
131 }
132}
133
134//------------------------------------------------------------------------------
135
136bool BanList::isBanned(S32 uniqueId, const char *)
137{
138 S32 curTime = Platform::getTime();
139
140 Vector<BanInfo>::iterator i;
141 for(i = list.begin();i != list.end();)
142 {
143 if(i->bannedUntil != 0 && i->bannedUntil < curTime)
144 {
145 list.erase(i);
146 continue;
147 }
148 else if(uniqueId == i->uniqueId)
149 return true;
150 i++;
151 }
152 return false;
153}
154
155//------------------------------------------------------------------------------
156
157bool BanList::isTAEq(const char *bannedTA, const char *TA)
158{
159 char a, b;
160 for(;;)
161 {
162 a = *bannedTA++;
163 b = *TA++;
164 if(a == '*' || (!a && b == ':')) // ignore port
165 return true;
166 if(dTolower(a) != dTolower(b))
167 return false;
168 if(!a)
169 return true;
170 }
171}
172
173//------------------------------------------------------------------------------
174
175void BanList::exportToFile(const char *name)
176{
177 FileStream *banlist;
178
179 char filename[1024];
180 Con::expandScriptFilename(filename, sizeof(filename), name);
181 if((banlist = FileStream::createAndOpen( filename, Torque::FS::File::Write )) == NULL)
182 return;
183
184 char buf[1024];
185 Vector<BanInfo>::iterator i;
186 for(i = list.begin(); i != list.end(); i++)
187 {
188 dSprintf(buf, sizeof(buf), "BanList::addAbsolute(%d, \"%s\", %d);\r\n", i->uniqueId, i->transportAddress, i->bannedUntil);
189 banlist->write(dStrlen(buf), buf);
190 }
191
192 delete banlist;
193}
194
195//=============================================================================
196// API.
197//=============================================================================
198// MARK: ---- API ----
199
200//-----------------------------------------------------------------------------
201
202DefineEngineStaticMethod( BanList, addAbsolute, void, ( S32 uniqueId, const char* transportAddress, S32 banTime ),,
203 "Ban a user until a given time.\n\n"
204 "@param uniqueId Unique ID of the player.\n"
205 "@param transportAddress Address from which the player connected.\n"
206 "@param banTime Time at which they will be allowed back in."
207 "@tsexample\n"
208 "// Kick someone off the server\n"
209 "// %client - This is the connection to the person we are kicking\n"
210 "function kick(%client)\n"
211 "{\n"
212 " // Let the server know what happened\n"
213 " messageAll( 'MsgAdminForce', '\\c2The Admin has kicked %1.', %client.playerName);\n\n"
214 " // If it is not an AI Player, execute the ban.\n"
215 " if (!%client.isAIControlled())\n"
216 " BanList::addAbsolute(%client.guid, %client.getAddress(), $pref::Server::KickBanTime);\n\n"
217 " // Let the player know they messed up\n"
218 " %client.delete(\"You have been kicked from this server\");\n"
219 "}\n"
220 "@endtsexample\n\n")
221{
222 BanList::instance()->addBan( uniqueId, transportAddress, banTime );
223}
224
225//-----------------------------------------------------------------------------
226
227DefineEngineStaticMethod( BanList, add, void, ( S32 uniqueId, const char* transportAddress, S32 banLength ),,
228 "Ban a user for banLength seconds.\n\n"
229 "@param uniqueId Unique ID of the player.\n"
230 "@param transportAddress Address from which the player connected.\n"
231 "@param banLength Time period over which to ban the player."
232 "@tsexample\n"
233 "// Kick someone off the server\n"
234 "// %client - This is the connection to the person we are kicking\n"
235 "function kick(%client)\n"
236 "{\n"
237 " // Let the server know what happened\n"
238 " messageAll( 'MsgAdminForce', '\\c2The Admin has kicked %1.', %client.playerName);\n\n"
239 " // If it is not an AI Player, execute the ban.\n"
240 " if (!%client.isAIControlled())\n"
241 " BanList::add(%client.guid, %client.getAddress(), $pref::Server::KickBanTime);\n\n"
242 " // Let the player know they messed up\n"
243 " %client.delete(\"You have been kicked from this server\");\n"
244 "}\n"
245 "@endtsexample\n\n")
246{
247 BanList::instance()->addBanRelative( uniqueId, transportAddress, banLength );
248}
249
250//-----------------------------------------------------------------------------
251
252DefineEngineStaticMethod( BanList, removeBan, void, ( S32 uniqueId, const char* transportAddress ),,
253 "Unban someone.\n\n"
254 "@param uniqueId Unique ID of the player.\n"
255 "@param transportAddress Address from which the player connected.\n"
256 "@tsexample\n"
257 "BanList::removeBan(%userID, %ipAddress);\n"
258 "@endtsexample\n\n")
259{
260 BanList::instance()->removeBan( uniqueId, transportAddress );
261}
262
263//-----------------------------------------------------------------------------
264
265DefineEngineStaticMethod( BanList, isBanned, bool, ( S32 uniqueId, const char* transportAddress ),,
266 "Is someone banned?\n\n"
267 "@param uniqueId Unique ID of the player.\n"
268 "@param transportAddress Address from which the player connected.\n\n"
269 "@tsexample\n"
270 "//-----------------------------------------------------------------------------\n"
271 "// This script function is called before a client connection\n"
272 "// is accepted. Returning "" will accept the connection,\n"
273 "// anything else will be sent back as an error to the client.\n"
274 "// All the connect args are passed also to onConnectRequest\n"
275 "function GameConnection::onConnectRequest( %client, %netAddress, %name )\n"
276 "{\n"
277 " // Find out who is trying to connect\n"
278 " echo(\"Connect request from: \" @ %netAddress);\n\n"
279 " // Are they allowed in?\n"
280 " if(BanList::isBanned(%client.guid, %netAddress))\n"
281 " return \"CR_YOUAREBANNED\";\n\n"
282 " // Is there room for an unbanned player?\n"
283 " if($Server::PlayerCount >= $pref::Server::MaxPlayers)\n"
284 " return \"CR_SERVERFULL\";\n"
285 " return "";\n"
286 "}\n"
287 "@endtsexample\n\n")
288{
289 return BanList::instance()->isBanned( uniqueId, transportAddress );
290}
291
292//-----------------------------------------------------------------------------
293
294DefineEngineStaticMethod( BanList, export, void, ( const char* filename ),,
295 "Dump the banlist to a file.\n\n"
296 "@param filename Path of the file to write the list to.\n\n"
297 "@tsexample\n"
298 "BanList::Export(\"./server/banlist." TORQUE_SCRIPT_EXTENSION "\");\n"
299 "@endtsexample\n\n")
300{
301 BanList::instance()->exportToFile( filename );
302}
303