3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "PlayerManager.h"
33 #include "AdminCache.h"
34 #include "sm_stringutil.h"
35 #include "HalfLife2.h"
36 #include "sourcemod.h"
37 #include "ChatTriggers.h"
38 #include <inetchannel.h>
41 ConVar sm_show_activity("sm_show_activity", "13", FCVAR_SPONLY, "Activity display setting (see sourcemod.cfg)");
43 static cell_t sm_GetClientCount(IPluginContext *pCtx, const cell_t *params)
47 return g_Players.NumPlayers();
50 int maxplayers = g_Players.MaxClients();
52 for (int i=1; i<=maxplayers; ++i)
54 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
55 if ((pPlayer->IsConnected()) && !(pPlayer->IsInGame()))
61 return (g_Players.NumPlayers() + count);
64 static cell_t sm_GetMaxClients(IPluginContext *pCtx, const cell_t *params)
66 return g_Players.MaxClients();
69 static cell_t sm_GetMaxHumanPlayers(IPluginContext *pCtx, const cell_t *params)
73 #if SOURCE_ENGINE >= SE_LEFT4DEAD
74 maxHumans = serverClients->GetMaxHumanPlayers();
79 return g_Players.MaxClients();
85 static cell_t sm_GetClientName(IPluginContext *pCtx, const cell_t *params)
87 int index = params[1];
91 static ConVar *hostname = NULL;
94 hostname = icvar->FindVar("hostname");
97 return pCtx->ThrowNativeError("Could not find \"hostname\" cvar");
100 pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), hostname->GetString(), NULL);
104 if ((index < 1) || (index > g_Players.GetMaxClients()))
106 return pCtx->ThrowNativeError("Client index %d is invalid", index);
109 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
110 if (!pPlayer->IsConnected())
112 return pCtx->ThrowNativeError("Client %d is not connected", index);
115 pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
119 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
121 int index = params[1];
122 if ((index < 1) || (index > g_Players.GetMaxClients()))
124 return pCtx->ThrowNativeError("Client index %d is invalid", index);
127 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
128 if (!pPlayer->IsConnected())
130 return pCtx->ThrowNativeError("Client %d is not connected", index);
134 strcpy(buf, pPlayer->GetIPAddress());
136 if (params[4] && (ptr = strchr(buf, ':')))
141 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
145 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
147 int index = params[1];
148 if ((index < 1) || (index > g_Players.GetMaxClients()))
150 return pCtx->ThrowNativeError("Client index %d is invalid", index);
153 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
154 if (!pPlayer->IsConnected())
156 return pCtx->ThrowNativeError("Client %d is not connected", index);
159 bool validate = true;
162 validate = !!params[4];
165 const char *authstr = pPlayer->GetAuthString(validate);
167 if (!authstr || authstr[0] == '\0')
172 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), authstr);
177 static cell_t sm_IsClientConnected(IPluginContext *pCtx, const cell_t *params)
179 int index = params[1];
180 if ((index < 1) || (index > g_Players.GetMaxClients()))
182 return pCtx->ThrowNativeError("Client index %d is invalid", index);
185 return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
188 static cell_t sm_IsClientInGame(IPluginContext *pCtx, const cell_t *params)
190 int index = params[1];
191 if ((index < 1) || (index > g_Players.GetMaxClients()))
193 return pCtx->ThrowNativeError("Client index %d is invalid", index);
196 return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
199 static cell_t sm_IsClientAuthorized(IPluginContext *pCtx, const cell_t *params)
201 int index = params[1];
202 if ((index < 1) || (index > g_Players.GetMaxClients()))
204 return pCtx->ThrowNativeError("Client index %d is invalid", index);
207 return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
210 static cell_t sm_IsClientFakeClient(IPluginContext *pCtx, const cell_t *params)
212 int index = params[1];
213 if ((index < 1) || (index > g_Players.GetMaxClients()))
215 return pCtx->ThrowNativeError("Client index %d is invalid", index);
218 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
219 if (!pPlayer->IsConnected())
221 return pCtx->ThrowNativeError("Client %d is not connected", index);
224 return (pPlayer->IsFakeClient()) ? 1 : 0;
227 static cell_t sm_IsClientSourceTV(IPluginContext *pCtx, const cell_t *params)
229 int index = params[1];
230 if ((index < 1) || (index > g_Players.GetMaxClients()))
232 return pCtx->ThrowNativeError("Client index %d is invalid", index);
235 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
236 if (!pPlayer->IsConnected())
238 return pCtx->ThrowNativeError("Client %d is not connected", index);
241 return (pPlayer->IsSourceTV()) ? 1 : 0;
244 static cell_t sm_IsClientReplay(IPluginContext *pCtx, const cell_t *params)
246 int index = params[1];
247 if ((index < 1) || (index > g_Players.GetMaxClients()))
249 return pCtx->ThrowNativeError("Client index %d is invalid", index);
252 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
253 if (!pPlayer->IsConnected())
255 return pCtx->ThrowNativeError("Client %d is not connected", index);
258 return (pPlayer->IsReplay()) ? 1 : 0;
261 static cell_t IsClientObserver(IPluginContext *pContext, const cell_t *params)
263 int client = params[1];
265 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
268 return pContext->ThrowNativeError("Client index %d is invalid", client);
269 } else if (!pPlayer->IsInGame()) {
270 return pContext->ThrowNativeError("Client %d is not in game", client);
273 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
276 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
279 return pInfo->IsObserver() ? 1 : 0;
282 static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
284 int client = params[1];
285 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
288 return pContext->ThrowNativeError("Client index %d is invalid", client);
290 if (!pPlayer->IsConnected())
292 return pContext->ThrowNativeError("Client %d is not connected", client);
296 pContext->LocalToString(params[2], &key);
298 const char *val = engine->GetClientConVarValue(client, key);
304 pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
308 static cell_t SetUserAdmin(IPluginContext *pContext, const cell_t *params)
310 int client = params[1];
311 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
314 return pContext->ThrowNativeError("Client index %d is invalid", client);
316 if (!pPlayer->IsConnected())
318 return pContext->ThrowNativeError("Client %d is not connected", client);
320 if (!g_Admins.IsValidAdmin(params[2]) && params[2] != INVALID_ADMIN_ID)
322 return pContext->ThrowNativeError("AdminId %x is invalid", params[2]);
325 pPlayer->SetAdminId(params[2], params[3] ? true : false);
330 static cell_t GetUserAdmin(IPluginContext *pContext, const cell_t *params)
332 int client = params[1];
333 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
336 return pContext->ThrowNativeError("Client index %d is invalid", client);
338 if (!pPlayer->IsConnected())
340 return pContext->ThrowNativeError("Client %d is not connected", client);
343 return pPlayer->GetAdminId();
346 static cell_t AddUserFlags(IPluginContext *pContext, const cell_t *params)
348 int client = params[1];
349 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
352 return pContext->ThrowNativeError("Client index %d is invalid", client);
354 if (!pPlayer->IsConnected())
356 return pContext->ThrowNativeError("Client %d is not connected", client);
360 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
362 id = g_Admins.CreateAdmin(NULL);
363 pPlayer->SetAdminId(id, true);
367 for (int i=2; i<=params[0]; i++)
369 pContext->LocalToPhysAddr(params[i], &addr);
370 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, true);
376 static cell_t RemoveUserFlags(IPluginContext *pContext, const cell_t *params)
378 int client = params[1];
379 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
382 return pContext->ThrowNativeError("Client index %d is invalid", client);
384 if (!pPlayer->IsConnected())
386 return pContext->ThrowNativeError("Client %d is not connected", client);
390 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
396 for (int i=2; i<=params[0]; i++)
398 pContext->LocalToPhysAddr(params[i], &addr);
399 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, false);
405 static cell_t SetUserFlagBits(IPluginContext *pContext, const cell_t *params)
407 int client = params[1];
408 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
411 return pContext->ThrowNativeError("Client index %d is invalid", client);
413 if (!pPlayer->IsConnected())
415 return pContext->ThrowNativeError("Client %d is not connected", client);
419 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
421 id = g_Admins.CreateAdmin(NULL);
422 pPlayer->SetAdminId(id, true);
425 g_Admins.SetAdminFlags(id, Access_Effective, params[2]);
430 static cell_t GetUserFlagBits(IPluginContext *pContext, const cell_t *params)
432 int client = params[1];
433 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
436 return pContext->ThrowNativeError("Client index %d is invalid", client);
438 if (!pPlayer->IsConnected())
440 return pContext->ThrowNativeError("Client %d is not connected", client);
444 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
449 return g_Admins.GetAdminFlags(id, Access_Effective);
452 static cell_t GetClientUserId(IPluginContext *pContext, const cell_t *params)
454 int client = params[1];
455 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
458 return pContext->ThrowNativeError("Client index %d is invalid", client);
460 if (!pPlayer->IsConnected())
462 return pContext->ThrowNativeError("Client %d is not connected", client);
465 return engine->GetPlayerUserId(pPlayer->GetEdict());
468 static cell_t CanUserTarget(IPluginContext *pContext, const cell_t *params)
470 int client = params[1];
471 int target = params[2];
478 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
481 return pContext->ThrowNativeError("Client index %d is invalid", client);
482 } else if (!pPlayer->IsConnected()) {
483 return pContext->ThrowNativeError("Client %d is not connected", client);
486 CPlayer *pTarget = g_Players.GetPlayerByIndex(target);
489 return pContext->ThrowNativeError("Client index %d is invalid", target);
490 } else if (!pTarget->IsConnected()) {
491 return pContext->ThrowNativeError("Client %d is not connected", target);
494 return g_Admins.CanAdminTarget(pPlayer->GetAdminId(), pTarget->GetAdminId()) ? 1 : 0;
497 static cell_t GetClientTeam(IPluginContext *pContext, const cell_t *params)
499 int client = params[1];
501 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
504 return pContext->ThrowNativeError("Client index %d is invalid", client);
505 } else if (!pPlayer->IsInGame()) {
506 return pContext->ThrowNativeError("Client %d is not in game", client);
509 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
512 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
515 return pInfo->GetTeamIndex();
518 static cell_t GetFragCount(IPluginContext *pContext, const cell_t *params)
520 int client = params[1];
522 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
525 return pContext->ThrowNativeError("Client index %d is invalid", client);
526 } else if (!pPlayer->IsInGame()) {
527 return pContext->ThrowNativeError("Client %d is not in game", client);
530 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
533 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
536 return pInfo->GetFragCount();
539 static cell_t GetDeathCount(IPluginContext *pContext, const cell_t *params)
541 int client = params[1];
543 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
546 return pContext->ThrowNativeError("Client index %d is invalid", client);
547 } else if (!pPlayer->IsInGame()) {
548 return pContext->ThrowNativeError("Client %d is not in game", client);
551 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
554 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
557 return pInfo->GetDeathCount();
560 static cell_t GetArmorValue(IPluginContext *pContext, const cell_t *params)
562 int client = params[1];
564 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
567 return pContext->ThrowNativeError("Client index %d is invalid", client);
568 } else if (!pPlayer->IsInGame()) {
569 return pContext->ThrowNativeError("Client %d is not in game", client);
572 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
575 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
578 return pInfo->GetArmorValue();
581 static cell_t GetAbsOrigin(IPluginContext *pContext, const cell_t *params)
583 int client = params[1];
585 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
588 return pContext->ThrowNativeError("Client index %d is invalid", client);
589 } else if (!pPlayer->IsInGame()) {
590 return pContext->ThrowNativeError("Client %d is not in game", client);
593 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
596 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
600 pContext->LocalToPhysAddr(params[2], &pVec);
602 Vector vec = pInfo->GetAbsOrigin();
603 pVec[0] = sp_ftoc(vec.x);
604 pVec[1] = sp_ftoc(vec.y);
605 pVec[2] = sp_ftoc(vec.z);
610 static cell_t GetAbsAngles(IPluginContext *pContext, const cell_t *params)
612 int client = params[1];
614 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
617 return pContext->ThrowNativeError("Client index %d is invalid", client);
618 } else if (!pPlayer->IsInGame()) {
619 return pContext->ThrowNativeError("Client %d is not in game", client);
622 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
625 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
629 pContext->LocalToPhysAddr(params[2], &pAng);
631 QAngle ang = pInfo->GetAbsAngles();
632 pAng[0] = sp_ftoc(ang.x);
633 pAng[1] = sp_ftoc(ang.y);
634 pAng[2] = sp_ftoc(ang.z);
639 static cell_t GetPlayerMins(IPluginContext *pContext, const cell_t *params)
641 int client = params[1];
643 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
646 return pContext->ThrowNativeError("Client index %d is invalid", client);
647 } else if (!pPlayer->IsInGame()) {
648 return pContext->ThrowNativeError("Client %d is not in game", client);
651 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
654 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
658 pContext->LocalToPhysAddr(params[2], &pVec);
660 Vector vec = pInfo->GetPlayerMins();
661 pVec[0] = sp_ftoc(vec.x);
662 pVec[1] = sp_ftoc(vec.y);
663 pVec[2] = sp_ftoc(vec.z);
668 static cell_t GetPlayerMaxs(IPluginContext *pContext, const cell_t *params)
670 int client = params[1];
672 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
675 return pContext->ThrowNativeError("Client index %d is invalid", client);
676 } else if (!pPlayer->IsInGame()) {
677 return pContext->ThrowNativeError("Client %d is not in game", client);
680 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
683 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
687 pContext->LocalToPhysAddr(params[2], &pVec);
689 Vector vec = pInfo->GetPlayerMaxs();
690 pVec[0] = sp_ftoc(vec.x);
691 pVec[1] = sp_ftoc(vec.y);
692 pVec[2] = sp_ftoc(vec.z);
697 static cell_t GetWeaponName(IPluginContext *pContext, const cell_t *params)
699 int client = params[1];
701 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
704 return pContext->ThrowNativeError("Client index %d is invalid", client);
705 } else if (!pPlayer->IsInGame()) {
706 return pContext->ThrowNativeError("Client %d is not in game", client);
709 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
712 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
715 const char *weapon = pInfo->GetWeaponName();
716 pContext->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), weapon ? weapon : "", NULL);
721 static cell_t GetModelName(IPluginContext *pContext, const cell_t *params)
723 int client = params[1];
725 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
728 return pContext->ThrowNativeError("Client index %d is invalid", client);
729 } else if (!pPlayer->IsInGame()) {
730 return pContext->ThrowNativeError("Client %d is not in game", client);
733 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
736 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
739 const char *model = pInfo->GetModelName();
740 pContext->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), model ? model : "", NULL);
745 static cell_t GetHealth(IPluginContext *pContext, const cell_t *params)
747 int client = params[1];
749 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
752 return pContext->ThrowNativeError("Client index %d is invalid", client);
753 } else if (!pPlayer->IsInGame()) {
754 return pContext->ThrowNativeError("Client %d is not in game", client);
757 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
760 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
763 return pInfo->GetHealth();
766 static cell_t GetTimeConnected(IPluginContext *pContext, const cell_t *params)
768 int client = params[1];
770 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
773 return pContext->ThrowNativeError("Client index %d is invalid", client);
774 } else if (!pPlayer->IsInGame()) {
775 return pContext->ThrowNativeError("Client %d is not in game", client);
776 } else if (pPlayer->IsFakeClient()) {
777 return pContext->ThrowNativeError("Client %d is a bot", client);
780 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
786 return sp_ftoc(pInfo->GetTimeConnected());
789 static cell_t GetDataRate(IPluginContext *pContext, const cell_t *params)
791 int client = params[1];
793 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
796 return pContext->ThrowNativeError("Client index %d is invalid", client);
797 } else if (!pPlayer->IsInGame()) {
798 return pContext->ThrowNativeError("Client %d is not in game", client);
799 } else if (pPlayer->IsFakeClient()) {
800 return pContext->ThrowNativeError("Client %d is a bot", client);
803 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
809 return pInfo->GetDataRate();
812 static cell_t IsTimingOut(IPluginContext *pContext, const cell_t *params)
814 int client = params[1];
816 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
819 return pContext->ThrowNativeError("Client index %d is invalid", client);
820 } else if (!pPlayer->IsInGame()) {
821 return pContext->ThrowNativeError("Client %d is not in game", client);
822 } else if (pPlayer->IsFakeClient()) {
823 return pContext->ThrowNativeError("Client %d is a bot", client);
826 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
832 return pInfo->IsTimingOut() ? 1 : 0;
835 static cell_t GetLatency(IPluginContext *pContext, const cell_t *params)
837 int client = params[1];
840 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
843 return pContext->ThrowNativeError("Client index %d is invalid", client);
845 else if (!pPlayer->IsInGame())
847 return pContext->ThrowNativeError("Client %d is not in game", client);
849 else if (pPlayer->IsFakeClient())
851 return pContext->ThrowNativeError("Client %d is a bot", client);
854 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
860 if (params[2] == MAX_FLOWS)
862 value = pInfo->GetLatency(FLOW_INCOMING) + pInfo->GetLatency(FLOW_OUTGOING);
866 value = pInfo->GetLatency(params[2]);
869 return sp_ftoc(value);
872 static cell_t GetAvgLatency(IPluginContext *pContext, const cell_t *params)
874 int client = params[1];
877 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
880 return pContext->ThrowNativeError("Client index %d is invalid", client);
882 else if (!pPlayer->IsInGame())
884 return pContext->ThrowNativeError("Client %d is not in game", client);
886 else if (pPlayer->IsFakeClient())
888 return pContext->ThrowNativeError("Client %d is a bot", client);
891 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
897 if (params[2] == MAX_FLOWS)
899 value = pInfo->GetAvgLatency(FLOW_INCOMING) + pInfo->GetAvgLatency(FLOW_OUTGOING);
903 value = pInfo->GetAvgLatency(params[2]);
906 return sp_ftoc(value);
909 static cell_t GetAvgLoss(IPluginContext *pContext, const cell_t *params)
911 int client = params[1];
914 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
917 return pContext->ThrowNativeError("Client index %d is invalid", client);
919 else if (!pPlayer->IsInGame())
921 return pContext->ThrowNativeError("Client %d is not in game", client);
923 else if (pPlayer->IsFakeClient())
925 return pContext->ThrowNativeError("Client %d is a bot", client);
928 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
934 if (params[2] == MAX_FLOWS)
936 value = pInfo->GetAvgLoss(FLOW_INCOMING) + pInfo->GetAvgLoss(FLOW_OUTGOING);
940 value = pInfo->GetAvgLoss(params[2]);
943 return sp_ftoc(value);
946 static cell_t GetAvgChoke(IPluginContext *pContext, const cell_t *params)
948 int client = params[1];
951 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
954 return pContext->ThrowNativeError("Client index %d is invalid", client);
956 else if (!pPlayer->IsInGame())
958 return pContext->ThrowNativeError("Client %d is not in game", client);
960 else if (pPlayer->IsFakeClient())
962 return pContext->ThrowNativeError("Client %d is a bot", client);
965 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
971 if (params[2] == MAX_FLOWS)
973 value = pInfo->GetAvgChoke(FLOW_INCOMING) + pInfo->GetAvgChoke(FLOW_OUTGOING);
977 value = pInfo->GetAvgChoke(params[2]);
980 return sp_ftoc(value);
983 static cell_t GetAvgData(IPluginContext *pContext, const cell_t *params)
985 int client = params[1];
988 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
991 return pContext->ThrowNativeError("Client index %d is invalid", client);
993 else if (!pPlayer->IsInGame())
995 return pContext->ThrowNativeError("Client %d is not in game", client);
997 else if (pPlayer->IsFakeClient())
999 return pContext->ThrowNativeError("Client %d is a bot", client);
1002 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1008 if (params[2] == MAX_FLOWS)
1010 value = pInfo->GetAvgData(FLOW_INCOMING) + pInfo->GetAvgData(FLOW_OUTGOING);
1014 value = pInfo->GetAvgData(params[2]);
1017 return sp_ftoc(value);
1020 static cell_t GetAvgPackets(IPluginContext *pContext, const cell_t *params)
1022 int client = params[1];
1025 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1028 return pContext->ThrowNativeError("Client index %d is invalid", client);
1030 else if (!pPlayer->IsInGame())
1032 return pContext->ThrowNativeError("Client %d is not in game", client);
1034 else if (pPlayer->IsFakeClient())
1036 return pContext->ThrowNativeError("Client %d is a bot", client);
1039 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1046 if (params[2] == MAX_FLOWS)
1048 value = pInfo->GetAvgPackets(FLOW_INCOMING) + pInfo->GetAvgPackets(FLOW_OUTGOING);
1052 value = pInfo->GetAvgPackets(params[2]);
1055 return sp_ftoc(value);
1058 static cell_t GetClientOfUserId(IPluginContext *pContext, const cell_t *params)
1060 return g_Players.GetClientOfUserId(params[1]);
1063 static cell_t _ShowActivity(IPluginContext *pContext,
1064 const cell_t *params,
1070 int value = sm_show_activity.GetInt();
1071 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1072 int client = params[1];
1074 const char *name = "Console";
1075 const char *sign = "ADMIN";
1076 bool display_in_chat = false;
1079 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1080 if (!pPlayer || !pPlayer->IsConnected())
1082 return pContext->ThrowNativeError("Client index %d is invalid", client);
1084 name = pPlayer->GetName();
1085 AdminId id = pPlayer->GetAdminId();
1086 if (id == INVALID_ADMIN_ID
1087 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1092 /* Display the message to the client? */
1093 if (replyto == SM_REPLY_CONSOLE)
1095 g_SourceMod.SetGlobalTarget(client);
1096 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1098 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1103 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1104 engine->ClientPrintf(pPlayer->GetEdict(), message);
1105 display_in_chat = true;
1110 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1111 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1113 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1118 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1119 META_CONPRINT(message);
1127 int maxClients = g_Players.GetMaxClients();
1128 for (int i=1; i<=maxClients; i++)
1130 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1131 if (!pPlayer->IsInGame()
1132 || pPlayer->IsFakeClient()
1133 || (display_in_chat && i == client))
1137 AdminId id = pPlayer->GetAdminId();
1138 g_SourceMod.SetGlobalTarget(i);
1139 if (id == INVALID_ADMIN_ID
1140 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1142 /* Treat this as a normal user */
1143 if ((value & 1) || (value & 2))
1145 const char *newsign = sign;
1146 if ((value & 2) || (i == client))
1150 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1152 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1157 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1158 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1163 /* Treat this as an admin user */
1164 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1167 || ((value & 16) && is_root))
1169 const char *newsign = sign;
1170 if ((value & 8) || ((value & 16) && is_root) || (i == client))
1174 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1176 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1181 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1182 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1190 static cell_t _ShowActivity2(IPluginContext *pContext,
1191 const cell_t *params,
1197 int value = sm_show_activity.GetInt();
1198 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1199 int client = params[1];
1201 const char *name = "Console";
1202 const char *sign = "ADMIN";
1205 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1206 if (!pPlayer || !pPlayer->IsConnected())
1208 return pContext->ThrowNativeError("Client index %d is invalid", client);
1210 name = pPlayer->GetName();
1211 AdminId id = pPlayer->GetAdminId();
1212 if (id == INVALID_ADMIN_ID
1213 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1218 g_SourceMod.SetGlobalTarget(client);
1219 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1221 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1226 /* We don't display directly to the console because the chat text
1227 * simply gets added to the console, so we don't want it to print
1230 if (replyto == SM_REPLY_CONSOLE)
1233 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1234 engine->ClientPrintf(pPlayer->GetEdict(), message);
1236 UTIL_Format(message, sizeof(message), "%s%s", tag, buffer);
1237 g_HL2.TextMsg(client, HUD_PRINTTALK, message);
1241 UTIL_Format(message, sizeof(message), "%s%s", tag, buffer);
1242 g_HL2.TextMsg(client, HUD_PRINTTALK, message);
1247 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1248 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1250 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1255 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1256 META_CONPRINT(message);
1264 int maxClients = g_Players.GetMaxClients();
1265 for (int i=1; i<=maxClients; i++)
1267 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1268 if (!pPlayer->IsInGame()
1269 || pPlayer->IsFakeClient()
1274 AdminId id = pPlayer->GetAdminId();
1275 g_SourceMod.SetGlobalTarget(i);
1276 if (id == INVALID_ADMIN_ID
1277 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1279 /* Treat this as a normal user */
1280 if ((value & 1) || (value & 2))
1282 const char *newsign = sign;
1287 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1289 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1294 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1295 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1300 /* Treat this as an admin user */
1301 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1304 || ((value & 16) && is_root))
1306 const char *newsign = sign;
1307 if ((value & 8) || ((value & 16) && is_root))
1311 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1313 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1318 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1319 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1327 static cell_t ShowActivity(IPluginContext *pContext, const cell_t *params)
1329 return _ShowActivity(pContext, params, "[SM] ", 2);
1332 static cell_t ShowActivityEx(IPluginContext *pContext, const cell_t *params)
1335 pContext->LocalToString(params[2], &str);
1337 return _ShowActivity(pContext, params, str, 3);
1340 static cell_t ShowActivity2(IPluginContext *pContext, const cell_t *params)
1343 pContext->LocalToString(params[2], &str);
1345 return _ShowActivity2(pContext, params, str, 3);
1348 static cell_t KickClient(IPluginContext *pContext, const cell_t *params)
1350 int client = params[1];
1352 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1355 return pContext->ThrowNativeError("Client index %d is invalid", client);
1357 else if (!pPlayer->IsConnected())
1359 return pContext->ThrowNativeError("Client %d is not connected", client);
1362 /* Ignore duplicate kicks */
1363 if (pPlayer->IsInKickQueue())
1368 g_SourceMod.SetGlobalTarget(client);
1371 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1373 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1378 if (pPlayer->IsFakeClient())
1380 // Kick uses the kickid command for bots. It is already delayed
1381 // until the next frame unless someone flushes command buffer
1382 pPlayer->Kick(buffer);
1386 pPlayer->MarkAsBeingKicked();
1387 g_HL2.AddDelayedKick(client, pPlayer->GetUserId(), buffer);
1392 static cell_t KickClientEx(IPluginContext *pContext, const cell_t *params)
1394 int client = params[1];
1396 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1399 return pContext->ThrowNativeError("Client index %d is invalid", client);
1401 else if (!pPlayer->IsConnected())
1403 return pContext->ThrowNativeError("Client %d is not connected", client);
1406 g_SourceMod.SetGlobalTarget(client);
1409 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1411 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1416 pPlayer->Kick(buffer);
1421 static cell_t ChangeClientTeam(IPluginContext *pContext, const cell_t *params)
1423 int client = params[1];
1425 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1428 return pContext->ThrowNativeError("Client index %d is invalid", client);
1430 else if (!pPlayer->IsInGame())
1432 return pContext->ThrowNativeError("Client %d is not in game", client);
1435 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
1438 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
1441 pInfo->ChangeTeam(params[2]);
1446 static cell_t RunAdminCacheChecks(IPluginContext *pContext, const cell_t *params)
1448 int client = params[1];
1450 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1453 return pContext->ThrowNativeError("Client index %d is invalid", client);
1455 else if (!pPlayer->IsInGame())
1457 return pContext->ThrowNativeError("Client %d is not in game", client);
1459 else if (!pPlayer->IsAuthorized())
1461 return pContext->ThrowNativeError("Client %d is not authorized", client);
1464 AdminId id = pPlayer->GetAdminId();
1465 pPlayer->DoBasicAdminChecks();
1467 return (id != pPlayer->GetAdminId()) ? 1 : 0;
1470 static cell_t NotifyPostAdminCheck(IPluginContext *pContext, const cell_t *params)
1472 int client = params[1];
1474 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1477 return pContext->ThrowNativeError("Client index %d is invalid", client);
1479 else if (!pPlayer->IsInGame()) {
1480 return pContext->ThrowNativeError("Client %d is not in game", client);
1482 else if (!pPlayer->IsAuthorized())
1484 return pContext->ThrowNativeError("Client %d is not authorized", client);
1487 pPlayer->NotifyPostAdminChecks();
1492 static cell_t IsClientInKickQueue(IPluginContext *pContext, const cell_t *params)
1494 int client = params[1];
1496 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1500 return pContext->ThrowNativeError("Client index %d is invalid", client);
1502 else if (!pPlayer->IsConnected())
1504 return pContext->ThrowNativeError("Client %d is not in game", client);
1507 return pPlayer->IsInKickQueue() ? 1 : 0;
1510 static cell_t ProcessTargetString(IPluginContext *pContext, const cell_t *params)
1512 cmd_target_info_t info;
1514 pContext->LocalToString(params[1], (char **)&info.pattern);
1515 info.admin = params[2];
1516 pContext->LocalToPhysAddr(params[3], &info.targets);
1517 info.max_targets = params[4];
1518 info.flags = params[5];
1519 pContext->LocalToString(params[6], &info.target_name);
1520 info.target_name_maxlength = params[7];
1523 pContext->LocalToPhysAddr(params[8], &tn_is_ml);
1525 g_Players.ProcessCommandTarget(&info);
1527 if (info.target_name_style == COMMAND_TARGETNAME_ML)
1536 if (info.num_targets == 0)
1542 return info.num_targets;
1546 static cell_t FormatActivitySource(IPluginContext *pContext, const cell_t *params)
1553 const char *identity[2] = {"Console", "ADMIN"};
1558 if ((pTarget = g_Players.GetPlayerByIndex(target)) == NULL)
1560 return pContext->ThrowNativeError("Invalid client index %d", target);
1562 if (!pTarget->IsConnected())
1564 return pContext->ThrowNativeError("Client %d not connected", target);
1567 value = sm_show_activity.GetInt();
1573 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
1575 return pContext->ThrowNativeError("Invalid client index %d", client);
1577 if (!pPlayer->IsConnected())
1579 return pContext->ThrowNativeError("Client %d not connected", client);
1582 identity[0] = pPlayer->GetName();
1584 AdminId id = pPlayer->GetAdminId();
1585 if (id == INVALID_ADMIN_ID
1586 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1588 identity[1] = "PLAYER";
1593 bool bShowActivity = false;
1595 if ((aidTarget = pTarget->GetAdminId()) == INVALID_ADMIN_ID
1596 || !g_Admins.GetAdminFlag(aidTarget, Admin_Generic, Access_Effective))
1598 /* Treat this as a normal user */
1599 if ((value & 1) || (value & 2))
1601 if ((value & 2) || (target == client))
1605 bShowActivity = true;
1610 /* Treat this as an admin user */
1611 bool is_root = g_Admins.GetAdminFlag(aidTarget, Admin_Root, Access_Effective);
1614 || ((value & 16) && is_root))
1616 if ((value & 8) || ((value & 16) && is_root) || (target == client))
1620 bShowActivity = true;
1624 /* Otherwise, send it back to the script. */
1625 pContext->StringToLocalUTF8(params[3], params[4], identity[mode], NULL);
1627 return bShowActivity ? 1 : 0;
1630 static cell_t sm_GetClientSerial(IPluginContext *pContext, const cell_t *params)
1632 int client = params[1];
1634 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1637 return pContext->ThrowNativeError("Client index %d is invalid", client);
1640 return pPlayer->GetSerial();
1643 static cell_t sm_GetClientFromSerial(IPluginContext *pContext, const cell_t *params)
1645 return g_Players.GetClientFromSerial(params[1]);
1648 REGISTER_NATIVES(playernatives)
1650 {"AddUserFlags", AddUserFlags},
1651 {"CanUserTarget", CanUserTarget},
1652 {"ChangeClientTeam", ChangeClientTeam},
1653 {"GetClientAuthString", sm_GetClientAuthStr},
1654 {"GetClientCount", sm_GetClientCount},
1655 {"GetClientInfo", sm_GetClientInfo},
1656 {"GetClientIP", sm_GetClientIP},
1657 {"GetClientName", sm_GetClientName},
1658 {"GetClientTeam", GetClientTeam},
1659 {"GetClientUserId", GetClientUserId},
1660 {"GetMaxClients", sm_GetMaxClients},
1661 {"GetMaxHumanPlayers", sm_GetMaxHumanPlayers},
1662 {"GetUserAdmin", GetUserAdmin},
1663 {"GetUserFlagBits", GetUserFlagBits},
1664 {"IsClientAuthorized", sm_IsClientAuthorized},
1665 {"IsClientConnected", sm_IsClientConnected},
1666 {"IsFakeClient", sm_IsClientFakeClient},
1667 {"IsClientSourceTV", sm_IsClientSourceTV},
1668 {"IsClientReplay", sm_IsClientReplay},
1669 {"IsClientInGame", sm_IsClientInGame},
1670 {"IsClientObserver", IsClientObserver},
1671 {"RemoveUserFlags", RemoveUserFlags},
1672 {"SetUserAdmin", SetUserAdmin},
1673 {"SetUserFlagBits", SetUserFlagBits},
1674 {"GetClientDeaths", GetDeathCount},
1675 {"GetClientFrags", GetFragCount},
1676 {"GetClientArmor", GetArmorValue},
1677 {"GetClientAbsOrigin", GetAbsOrigin},
1678 {"GetClientAbsAngles", GetAbsAngles},
1679 {"GetClientMins", GetPlayerMins},
1680 {"GetClientMaxs", GetPlayerMaxs},
1681 {"GetClientWeapon", GetWeaponName},
1682 {"GetClientModel", GetModelName},
1683 {"GetClientHealth", GetHealth},
1684 {"GetClientTime", GetTimeConnected},
1685 {"GetClientDataRate", GetDataRate},
1686 {"IsClientTimingOut", IsTimingOut},
1687 {"GetClientLatency", GetLatency},
1688 {"GetClientAvgLatency", GetAvgLatency},
1689 {"GetClientAvgLoss", GetAvgLoss},
1690 {"GetClientAvgChoke", GetAvgChoke},
1691 {"GetClientAvgData", GetAvgData},
1692 {"GetClientAvgPackets", GetAvgPackets},
1693 {"GetClientOfUserId", GetClientOfUserId},
1694 {"ShowActivity", ShowActivity},
1695 {"ShowActivityEx", ShowActivityEx},
1696 {"ShowActivity2", ShowActivity2},
1697 {"KickClient", KickClient},
1698 {"KickClientEx", KickClientEx},
1699 {"RunAdminCacheChecks", RunAdminCacheChecks},
1700 {"NotifyPostAdminCheck", NotifyPostAdminCheck},
1701 {"IsClientInKickQueue", IsClientInKickQueue},
1702 {"ProcessTargetString", ProcessTargetString},
1703 {"FormatActivitySource", FormatActivitySource},
1704 {"GetClientSerial", sm_GetClientSerial},
1705 {"GetClientFromSerial", sm_GetClientFromSerial},