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_GetClientName(IPluginContext *pCtx, const cell_t *params)
71 int index = params[1];
75 static ConVar *hostname = NULL;
78 hostname = icvar->FindVar("hostname");
81 return pCtx->ThrowNativeError("Could not find \"hostname\" cvar");
84 pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), hostname->GetString(), NULL);
88 if ((index < 1) || (index > g_Players.GetMaxClients()))
90 return pCtx->ThrowNativeError("Client index %d is invalid", index);
93 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
94 if (!pPlayer->IsConnected())
96 return pCtx->ThrowNativeError("Client %d is not connected", index);
99 pCtx->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), pPlayer->GetName(), NULL);
103 static cell_t sm_GetClientIP(IPluginContext *pCtx, const cell_t *params)
105 int index = params[1];
106 if ((index < 1) || (index > g_Players.GetMaxClients()))
108 return pCtx->ThrowNativeError("Client index %d is invalid", index);
111 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
112 if (!pPlayer->IsConnected())
114 return pCtx->ThrowNativeError("Client %d is not connected", index);
118 strcpy(buf, pPlayer->GetIPAddress());
120 if (params[4] && (ptr = strchr(buf, ':')))
125 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), buf);
129 static cell_t sm_GetClientAuthStr(IPluginContext *pCtx, const cell_t *params)
131 int index = params[1];
132 if ((index < 1) || (index > g_Players.GetMaxClients()))
134 return pCtx->ThrowNativeError("Client index %d is invalid", index);
137 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
138 if (!pPlayer->IsConnected())
140 return pCtx->ThrowNativeError("Client %d is not connected", index);
143 if (!pPlayer->IsAuthorized())
148 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
153 static cell_t sm_IsClientConnected(IPluginContext *pCtx, const cell_t *params)
155 int index = params[1];
156 if ((index < 1) || (index > g_Players.GetMaxClients()))
158 return pCtx->ThrowNativeError("Client index %d is invalid", index);
161 return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
164 static cell_t sm_IsClientInGame(IPluginContext *pCtx, const cell_t *params)
166 int index = params[1];
167 if ((index < 1) || (index > g_Players.GetMaxClients()))
169 return pCtx->ThrowNativeError("Client index %d is invalid", index);
172 return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
175 static cell_t sm_IsClientAuthorized(IPluginContext *pCtx, const cell_t *params)
177 int index = params[1];
178 if ((index < 1) || (index > g_Players.GetMaxClients()))
180 return pCtx->ThrowNativeError("Client index %d is invalid", index);
183 return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
186 static cell_t sm_IsClientFakeClient(IPluginContext *pCtx, const cell_t *params)
188 int index = params[1];
189 if ((index < 1) || (index > g_Players.GetMaxClients()))
191 return pCtx->ThrowNativeError("Client index %d is invalid", index);
194 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
195 if (!pPlayer->IsConnected())
197 return pCtx->ThrowNativeError("Client %d is not connected", index);
200 return (pPlayer->IsFakeClient()) ? 1 : 0;
203 static cell_t sm_IsClientSourceTV(IPluginContext *pCtx, const cell_t *params)
205 int index = params[1];
206 if ((index < 1) || (index > g_Players.GetMaxClients()))
208 return pCtx->ThrowNativeError("Client index %d is invalid", index);
211 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
212 if (!pPlayer->IsConnected())
214 return pCtx->ThrowNativeError("Client %d is not connected", index);
217 return (pPlayer->IsSourceTV()) ? 1 : 0;
220 static cell_t sm_IsClientReplay(IPluginContext *pCtx, const cell_t *params)
222 int index = params[1];
223 if ((index < 1) || (index > g_Players.GetMaxClients()))
225 return pCtx->ThrowNativeError("Client index %d is invalid", index);
228 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
229 if (!pPlayer->IsConnected())
231 return pCtx->ThrowNativeError("Client %d is not connected", index);
234 return (pPlayer->IsReplay()) ? 1 : 0;
237 static cell_t IsClientObserver(IPluginContext *pContext, const cell_t *params)
239 int client = params[1];
241 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
244 return pContext->ThrowNativeError("Client index %d is invalid", client);
245 } else if (!pPlayer->IsInGame()) {
246 return pContext->ThrowNativeError("Client %d is not in game", client);
249 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
252 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
255 return pInfo->IsObserver() ? 1 : 0;
258 static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
260 int client = params[1];
261 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
264 return pContext->ThrowNativeError("Client index %d is invalid", client);
266 if (!pPlayer->IsConnected())
268 return pContext->ThrowNativeError("Client %d is not connected", client);
272 pContext->LocalToString(params[2], &key);
274 const char *val = engine->GetClientConVarValue(client, key);
280 pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
284 static cell_t SetUserAdmin(IPluginContext *pContext, const cell_t *params)
286 int client = params[1];
287 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
290 return pContext->ThrowNativeError("Client index %d is invalid", client);
292 if (!pPlayer->IsConnected())
294 return pContext->ThrowNativeError("Client %d is not connected", client);
296 if (!g_Admins.IsValidAdmin(params[2]) && params[2] != INVALID_ADMIN_ID)
298 return pContext->ThrowNativeError("AdminId %x is invalid", params[2]);
301 pPlayer->SetAdminId(params[2], params[3] ? true : false);
306 static cell_t GetUserAdmin(IPluginContext *pContext, const cell_t *params)
308 int client = params[1];
309 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
312 return pContext->ThrowNativeError("Client index %d is invalid", client);
314 if (!pPlayer->IsConnected())
316 return pContext->ThrowNativeError("Client %d is not connected", client);
319 return pPlayer->GetAdminId();
322 static cell_t AddUserFlags(IPluginContext *pContext, const cell_t *params)
324 int client = params[1];
325 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
328 return pContext->ThrowNativeError("Client index %d is invalid", client);
330 if (!pPlayer->IsConnected())
332 return pContext->ThrowNativeError("Client %d is not connected", client);
336 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
338 id = g_Admins.CreateAdmin(NULL);
339 pPlayer->SetAdminId(id, true);
343 for (int i=2; i<=params[0]; i++)
345 pContext->LocalToPhysAddr(params[i], &addr);
346 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, true);
352 static cell_t RemoveUserFlags(IPluginContext *pContext, const cell_t *params)
354 int client = params[1];
355 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
358 return pContext->ThrowNativeError("Client index %d is invalid", client);
360 if (!pPlayer->IsConnected())
362 return pContext->ThrowNativeError("Client %d is not connected", client);
366 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
372 for (int i=2; i<=params[0]; i++)
374 pContext->LocalToPhysAddr(params[i], &addr);
375 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, false);
381 static cell_t SetUserFlagBits(IPluginContext *pContext, const cell_t *params)
383 int client = params[1];
384 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
387 return pContext->ThrowNativeError("Client index %d is invalid", client);
389 if (!pPlayer->IsConnected())
391 return pContext->ThrowNativeError("Client %d is not connected", client);
395 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
397 id = g_Admins.CreateAdmin(NULL);
398 pPlayer->SetAdminId(id, true);
401 g_Admins.SetAdminFlags(id, Access_Effective, params[2]);
406 static cell_t GetUserFlagBits(IPluginContext *pContext, const cell_t *params)
408 int client = params[1];
409 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
412 return pContext->ThrowNativeError("Client index %d is invalid", client);
414 if (!pPlayer->IsConnected())
416 return pContext->ThrowNativeError("Client %d is not connected", client);
420 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
425 return g_Admins.GetAdminFlags(id, Access_Effective);
428 static cell_t GetClientUserId(IPluginContext *pContext, const cell_t *params)
430 int client = params[1];
431 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
434 return pContext->ThrowNativeError("Client index %d is invalid", client);
436 if (!pPlayer->IsConnected())
438 return pContext->ThrowNativeError("Client %d is not connected", client);
441 return engine->GetPlayerUserId(pPlayer->GetEdict());
444 static cell_t CanUserTarget(IPluginContext *pContext, const cell_t *params)
446 int client = params[1];
447 int target = params[2];
454 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
457 return pContext->ThrowNativeError("Client index %d is invalid", client);
458 } else if (!pPlayer->IsConnected()) {
459 return pContext->ThrowNativeError("Client %d is not connected", client);
462 CPlayer *pTarget = g_Players.GetPlayerByIndex(target);
465 return pContext->ThrowNativeError("Client index %d is invalid", target);
466 } else if (!pTarget->IsConnected()) {
467 return pContext->ThrowNativeError("Client %d is not connected", target);
470 return g_Admins.CanAdminTarget(pPlayer->GetAdminId(), pTarget->GetAdminId()) ? 1 : 0;
473 static cell_t GetClientTeam(IPluginContext *pContext, const cell_t *params)
475 int client = params[1];
477 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
480 return pContext->ThrowNativeError("Client index %d is invalid", client);
481 } else if (!pPlayer->IsInGame()) {
482 return pContext->ThrowNativeError("Client %d is not in game", client);
485 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
488 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
491 return pInfo->GetTeamIndex();
494 static cell_t GetFragCount(IPluginContext *pContext, const cell_t *params)
496 int client = params[1];
498 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
501 return pContext->ThrowNativeError("Client index %d is invalid", client);
502 } else if (!pPlayer->IsInGame()) {
503 return pContext->ThrowNativeError("Client %d is not in game", client);
506 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
509 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
512 return pInfo->GetFragCount();
515 static cell_t GetDeathCount(IPluginContext *pContext, const cell_t *params)
517 int client = params[1];
519 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
522 return pContext->ThrowNativeError("Client index %d is invalid", client);
523 } else if (!pPlayer->IsInGame()) {
524 return pContext->ThrowNativeError("Client %d is not in game", client);
527 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
530 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
533 return pInfo->GetDeathCount();
536 static cell_t GetArmorValue(IPluginContext *pContext, const cell_t *params)
538 int client = params[1];
540 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
543 return pContext->ThrowNativeError("Client index %d is invalid", client);
544 } else if (!pPlayer->IsInGame()) {
545 return pContext->ThrowNativeError("Client %d is not in game", client);
548 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
551 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
554 return pInfo->GetArmorValue();
557 static cell_t GetAbsOrigin(IPluginContext *pContext, const cell_t *params)
559 int client = params[1];
561 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
564 return pContext->ThrowNativeError("Client index %d is invalid", client);
565 } else if (!pPlayer->IsInGame()) {
566 return pContext->ThrowNativeError("Client %d is not in game", client);
569 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
572 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
576 pContext->LocalToPhysAddr(params[2], &pVec);
578 Vector vec = pInfo->GetAbsOrigin();
579 pVec[0] = sp_ftoc(vec.x);
580 pVec[1] = sp_ftoc(vec.y);
581 pVec[2] = sp_ftoc(vec.z);
586 static cell_t GetAbsAngles(IPluginContext *pContext, const cell_t *params)
588 int client = params[1];
590 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
593 return pContext->ThrowNativeError("Client index %d is invalid", client);
594 } else if (!pPlayer->IsInGame()) {
595 return pContext->ThrowNativeError("Client %d is not in game", client);
598 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
601 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
605 pContext->LocalToPhysAddr(params[2], &pAng);
607 QAngle ang = pInfo->GetAbsAngles();
608 pAng[0] = sp_ftoc(ang.x);
609 pAng[1] = sp_ftoc(ang.y);
610 pAng[2] = sp_ftoc(ang.z);
615 static cell_t GetPlayerMins(IPluginContext *pContext, const cell_t *params)
617 int client = params[1];
619 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
622 return pContext->ThrowNativeError("Client index %d is invalid", client);
623 } else if (!pPlayer->IsInGame()) {
624 return pContext->ThrowNativeError("Client %d is not in game", client);
627 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
630 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
634 pContext->LocalToPhysAddr(params[2], &pVec);
636 Vector vec = pInfo->GetPlayerMins();
637 pVec[0] = sp_ftoc(vec.x);
638 pVec[1] = sp_ftoc(vec.y);
639 pVec[2] = sp_ftoc(vec.z);
644 static cell_t GetPlayerMaxs(IPluginContext *pContext, const cell_t *params)
646 int client = params[1];
648 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
651 return pContext->ThrowNativeError("Client index %d is invalid", client);
652 } else if (!pPlayer->IsInGame()) {
653 return pContext->ThrowNativeError("Client %d is not in game", client);
656 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
659 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
663 pContext->LocalToPhysAddr(params[2], &pVec);
665 Vector vec = pInfo->GetPlayerMaxs();
666 pVec[0] = sp_ftoc(vec.x);
667 pVec[1] = sp_ftoc(vec.y);
668 pVec[2] = sp_ftoc(vec.z);
673 static cell_t GetWeaponName(IPluginContext *pContext, const cell_t *params)
675 int client = params[1];
677 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
680 return pContext->ThrowNativeError("Client index %d is invalid", client);
681 } else if (!pPlayer->IsInGame()) {
682 return pContext->ThrowNativeError("Client %d is not in game", client);
685 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
688 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
691 const char *weapon = pInfo->GetWeaponName();
692 pContext->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), weapon ? weapon : "", NULL);
697 static cell_t GetModelName(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 *model = pInfo->GetModelName();
716 pContext->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), model ? model : "", NULL);
721 static cell_t GetHealth(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 return pInfo->GetHealth();
742 static cell_t GetTimeConnected(IPluginContext *pContext, const cell_t *params)
744 int client = params[1];
746 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
749 return pContext->ThrowNativeError("Client index %d is invalid", client);
750 } else if (!pPlayer->IsInGame()) {
751 return pContext->ThrowNativeError("Client %d is not in game", client);
752 } else if (pPlayer->IsFakeClient()) {
753 return pContext->ThrowNativeError("Client %d is a bot", client);
756 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
762 return sp_ftoc(pInfo->GetTimeConnected());
765 static cell_t GetDataRate(IPluginContext *pContext, const cell_t *params)
767 int client = params[1];
769 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
772 return pContext->ThrowNativeError("Client index %d is invalid", client);
773 } else if (!pPlayer->IsInGame()) {
774 return pContext->ThrowNativeError("Client %d is not in game", client);
775 } else if (pPlayer->IsFakeClient()) {
776 return pContext->ThrowNativeError("Client %d is a bot", client);
779 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
785 return pInfo->GetDataRate();
788 static cell_t IsTimingOut(IPluginContext *pContext, const cell_t *params)
790 int client = params[1];
792 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
795 return pContext->ThrowNativeError("Client index %d is invalid", client);
796 } else if (!pPlayer->IsInGame()) {
797 return pContext->ThrowNativeError("Client %d is not in game", client);
798 } else if (pPlayer->IsFakeClient()) {
799 return pContext->ThrowNativeError("Client %d is a bot", client);
802 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
808 return pInfo->IsTimingOut() ? 1 : 0;
811 static cell_t GetLatency(IPluginContext *pContext, const cell_t *params)
813 int client = params[1];
816 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
819 return pContext->ThrowNativeError("Client index %d is invalid", client);
821 else if (!pPlayer->IsInGame())
823 return pContext->ThrowNativeError("Client %d is not in game", client);
825 else if (pPlayer->IsFakeClient())
827 return pContext->ThrowNativeError("Client %d is a bot", client);
830 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
836 if (params[2] == MAX_FLOWS)
838 value = pInfo->GetLatency(FLOW_INCOMING) + pInfo->GetLatency(FLOW_OUTGOING);
842 value = pInfo->GetLatency(params[2]);
845 return sp_ftoc(value);
848 static cell_t GetAvgLatency(IPluginContext *pContext, const cell_t *params)
850 int client = params[1];
853 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
856 return pContext->ThrowNativeError("Client index %d is invalid", client);
858 else if (!pPlayer->IsInGame())
860 return pContext->ThrowNativeError("Client %d is not in game", client);
862 else if (pPlayer->IsFakeClient())
864 return pContext->ThrowNativeError("Client %d is a bot", client);
867 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
873 if (params[2] == MAX_FLOWS)
875 value = pInfo->GetAvgLatency(FLOW_INCOMING) + pInfo->GetAvgLatency(FLOW_OUTGOING);
879 value = pInfo->GetAvgLatency(params[2]);
882 return sp_ftoc(value);
885 static cell_t GetAvgLoss(IPluginContext *pContext, const cell_t *params)
887 int client = params[1];
890 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
893 return pContext->ThrowNativeError("Client index %d is invalid", client);
895 else if (!pPlayer->IsInGame())
897 return pContext->ThrowNativeError("Client %d is not in game", client);
899 else if (pPlayer->IsFakeClient())
901 return pContext->ThrowNativeError("Client %d is a bot", client);
904 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
910 if (params[2] == MAX_FLOWS)
912 value = pInfo->GetAvgLoss(FLOW_INCOMING) + pInfo->GetAvgLoss(FLOW_OUTGOING);
916 value = pInfo->GetAvgLoss(params[2]);
919 return sp_ftoc(value);
922 static cell_t GetAvgChoke(IPluginContext *pContext, const cell_t *params)
924 int client = params[1];
927 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
930 return pContext->ThrowNativeError("Client index %d is invalid", client);
932 else if (!pPlayer->IsInGame())
934 return pContext->ThrowNativeError("Client %d is not in game", client);
936 else if (pPlayer->IsFakeClient())
938 return pContext->ThrowNativeError("Client %d is a bot", client);
941 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
947 if (params[2] == MAX_FLOWS)
949 value = pInfo->GetAvgChoke(FLOW_INCOMING) + pInfo->GetAvgChoke(FLOW_OUTGOING);
953 value = pInfo->GetAvgChoke(params[2]);
956 return sp_ftoc(value);
959 static cell_t GetAvgData(IPluginContext *pContext, const cell_t *params)
961 int client = params[1];
964 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
967 return pContext->ThrowNativeError("Client index %d is invalid", client);
969 else if (!pPlayer->IsInGame())
971 return pContext->ThrowNativeError("Client %d is not in game", client);
973 else if (pPlayer->IsFakeClient())
975 return pContext->ThrowNativeError("Client %d is a bot", client);
978 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
984 if (params[2] == MAX_FLOWS)
986 value = pInfo->GetAvgData(FLOW_INCOMING) + pInfo->GetAvgData(FLOW_OUTGOING);
990 value = pInfo->GetAvgData(params[2]);
993 return sp_ftoc(value);
996 static cell_t GetAvgPackets(IPluginContext *pContext, const cell_t *params)
998 int client = params[1];
1001 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1004 return pContext->ThrowNativeError("Client index %d is invalid", client);
1006 else if (!pPlayer->IsInGame())
1008 return pContext->ThrowNativeError("Client %d is not in game", client);
1010 else if (pPlayer->IsFakeClient())
1012 return pContext->ThrowNativeError("Client %d is a bot", client);
1015 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1022 if (params[2] == MAX_FLOWS)
1024 value = pInfo->GetAvgPackets(FLOW_INCOMING) + pInfo->GetAvgPackets(FLOW_OUTGOING);
1028 value = pInfo->GetAvgPackets(params[2]);
1031 return sp_ftoc(value);
1034 static cell_t GetClientOfUserId(IPluginContext *pContext, const cell_t *params)
1036 return g_Players.GetClientOfUserId(params[1]);
1039 static cell_t _ShowActivity(IPluginContext *pContext,
1040 const cell_t *params,
1046 int value = sm_show_activity.GetInt();
1047 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1048 int client = params[1];
1050 const char *name = "Console";
1051 const char *sign = "ADMIN";
1052 bool display_in_chat = false;
1055 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1056 if (!pPlayer || !pPlayer->IsConnected())
1058 return pContext->ThrowNativeError("Client index %d is invalid", client);
1060 name = pPlayer->GetName();
1061 AdminId id = pPlayer->GetAdminId();
1062 if (id == INVALID_ADMIN_ID
1063 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1068 /* Display the message to the client? */
1069 if (replyto == SM_REPLY_CONSOLE)
1071 g_SourceMod.SetGlobalTarget(client);
1072 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1074 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1079 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1080 engine->ClientPrintf(pPlayer->GetEdict(), message);
1081 display_in_chat = true;
1086 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1087 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1089 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1094 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1095 META_CONPRINT(message);
1103 int maxClients = g_Players.GetMaxClients();
1104 for (int i=1; i<=maxClients; i++)
1106 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1107 if (!pPlayer->IsInGame()
1108 || pPlayer->IsFakeClient()
1109 || (display_in_chat && i == client))
1113 AdminId id = pPlayer->GetAdminId();
1114 g_SourceMod.SetGlobalTarget(i);
1115 if (id == INVALID_ADMIN_ID
1116 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1118 /* Treat this as a normal user */
1119 if ((value & 1) || (value & 2))
1121 const char *newsign = sign;
1122 if ((value & 2) || (i == client))
1126 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1128 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1133 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1134 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1139 /* Treat this as an admin user */
1140 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1143 || ((value & 16) && is_root))
1145 const char *newsign = sign;
1146 if ((value & 8) || ((value & 16) && is_root) || (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);
1166 static cell_t _ShowActivity2(IPluginContext *pContext,
1167 const cell_t *params,
1173 int value = sm_show_activity.GetInt();
1174 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1175 int client = params[1];
1177 const char *name = "Console";
1178 const char *sign = "ADMIN";
1181 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1182 if (!pPlayer || !pPlayer->IsConnected())
1184 return pContext->ThrowNativeError("Client index %d is invalid", client);
1186 name = pPlayer->GetName();
1187 AdminId id = pPlayer->GetAdminId();
1188 if (id == INVALID_ADMIN_ID
1189 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1194 g_SourceMod.SetGlobalTarget(client);
1195 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1197 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1202 /* We don't display directly to the console because the chat text
1203 * simply gets added to the console, so we don't want it to print
1206 if (replyto == SM_REPLY_CONSOLE)
1209 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1210 engine->ClientPrintf(pPlayer->GetEdict(), message);
1212 UTIL_Format(message, sizeof(message), "%s%s", tag, buffer);
1213 g_HL2.TextMsg(client, HUD_PRINTTALK, message);
1217 UTIL_Format(message, sizeof(message), "%s%s", tag, buffer);
1218 g_HL2.TextMsg(client, HUD_PRINTTALK, message);
1223 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1224 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1226 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1231 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1232 META_CONPRINT(message);
1240 int maxClients = g_Players.GetMaxClients();
1241 for (int i=1; i<=maxClients; i++)
1243 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1244 if (!pPlayer->IsInGame()
1245 || pPlayer->IsFakeClient()
1250 AdminId id = pPlayer->GetAdminId();
1251 g_SourceMod.SetGlobalTarget(i);
1252 if (id == INVALID_ADMIN_ID
1253 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1255 /* Treat this as a normal user */
1256 if ((value & 1) || (value & 2))
1258 const char *newsign = sign;
1263 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1265 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1270 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1271 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1276 /* Treat this as an admin user */
1277 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1280 || ((value & 16) && is_root))
1282 const char *newsign = sign;
1283 if ((value & 8) || ((value & 16) && is_root))
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);
1303 static cell_t ShowActivity(IPluginContext *pContext, const cell_t *params)
1305 return _ShowActivity(pContext, params, "[SM] ", 2);
1308 static cell_t ShowActivityEx(IPluginContext *pContext, const cell_t *params)
1311 pContext->LocalToString(params[2], &str);
1313 return _ShowActivity(pContext, params, str, 3);
1316 static cell_t ShowActivity2(IPluginContext *pContext, const cell_t *params)
1319 pContext->LocalToString(params[2], &str);
1321 return _ShowActivity2(pContext, params, str, 3);
1324 static cell_t KickClient(IPluginContext *pContext, const cell_t *params)
1326 int client = params[1];
1328 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1331 return pContext->ThrowNativeError("Client index %d is invalid", client);
1333 else if (!pPlayer->IsConnected())
1335 return pContext->ThrowNativeError("Client %d is not connected", client);
1338 /* Ignore duplicate kicks */
1339 if (pPlayer->IsInKickQueue())
1344 g_SourceMod.SetGlobalTarget(client);
1347 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1349 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1354 if (pPlayer->IsFakeClient())
1356 // Kick uses the kickid command for bots. It is already delayed
1357 // until the next frame unless someone flushes command buffer
1358 pPlayer->Kick(buffer);
1362 pPlayer->MarkAsBeingKicked();
1363 g_HL2.AddDelayedKick(client, pPlayer->GetUserId(), buffer);
1368 static cell_t KickClientEx(IPluginContext *pContext, const cell_t *params)
1370 int client = params[1];
1372 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1375 return pContext->ThrowNativeError("Client index %d is invalid", client);
1377 else if (!pPlayer->IsConnected())
1379 return pContext->ThrowNativeError("Client %d is not connected", client);
1382 g_SourceMod.SetGlobalTarget(client);
1385 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1387 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1392 pPlayer->Kick(buffer);
1397 static cell_t ChangeClientTeam(IPluginContext *pContext, const cell_t *params)
1399 int client = params[1];
1401 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1404 return pContext->ThrowNativeError("Client index %d is invalid", client);
1406 else if (!pPlayer->IsInGame())
1408 return pContext->ThrowNativeError("Client %d is not in game", client);
1411 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
1414 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
1417 pInfo->ChangeTeam(params[2]);
1422 static cell_t RunAdminCacheChecks(IPluginContext *pContext, const cell_t *params)
1424 int client = params[1];
1426 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1429 return pContext->ThrowNativeError("Client index %d is invalid", client);
1431 else if (!pPlayer->IsInGame())
1433 return pContext->ThrowNativeError("Client %d is not in game", client);
1435 else if (!pPlayer->IsAuthorized())
1437 return pContext->ThrowNativeError("Client %d is not authorized", client);
1440 AdminId id = pPlayer->GetAdminId();
1441 pPlayer->DoBasicAdminChecks();
1443 return (id != pPlayer->GetAdminId()) ? 1 : 0;
1446 static cell_t NotifyPostAdminCheck(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()) {
1456 return pContext->ThrowNativeError("Client %d is not in game", client);
1458 else if (!pPlayer->IsAuthorized())
1460 return pContext->ThrowNativeError("Client %d is not authorized", client);
1463 pPlayer->NotifyPostAdminChecks();
1468 static cell_t IsClientInKickQueue(IPluginContext *pContext, const cell_t *params)
1470 int client = params[1];
1472 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1476 return pContext->ThrowNativeError("Client index %d is invalid", client);
1478 else if (!pPlayer->IsConnected())
1480 return pContext->ThrowNativeError("Client %d is not in game", client);
1483 return pPlayer->IsInKickQueue() ? 1 : 0;
1486 static cell_t ProcessTargetString(IPluginContext *pContext, const cell_t *params)
1488 cmd_target_info_t info;
1490 pContext->LocalToString(params[1], (char **)&info.pattern);
1491 info.admin = params[2];
1492 pContext->LocalToPhysAddr(params[3], &info.targets);
1493 info.max_targets = params[4];
1494 info.flags = params[5];
1495 pContext->LocalToString(params[6], &info.target_name);
1496 info.target_name_maxlength = params[7];
1499 pContext->LocalToPhysAddr(params[8], &tn_is_ml);
1501 g_Players.ProcessCommandTarget(&info);
1503 if (info.target_name_style == COMMAND_TARGETNAME_ML)
1512 if (info.num_targets == 0)
1518 return info.num_targets;
1522 static cell_t FormatActivitySource(IPluginContext *pContext, const cell_t *params)
1529 const char *identity[2] = {"Console", "ADMIN"};
1534 if ((pTarget = g_Players.GetPlayerByIndex(target)) == NULL)
1536 return pContext->ThrowNativeError("Invalid client index %d", target);
1538 if (!pTarget->IsConnected())
1540 return pContext->ThrowNativeError("Client %d not connected", target);
1543 value = sm_show_activity.GetInt();
1549 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
1551 return pContext->ThrowNativeError("Invalid client index %d", client);
1553 if (!pPlayer->IsConnected())
1555 return pContext->ThrowNativeError("Client %d not connected", client);
1558 identity[0] = pPlayer->GetName();
1560 AdminId id = pPlayer->GetAdminId();
1561 if (id == INVALID_ADMIN_ID
1562 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1564 identity[1] = "PLAYER";
1569 bool bShowActivity = false;
1571 if ((aidTarget = pTarget->GetAdminId()) == INVALID_ADMIN_ID
1572 || !g_Admins.GetAdminFlag(aidTarget, Admin_Generic, Access_Effective))
1574 /* Treat this as a normal user */
1575 if ((value & 1) || (value & 2))
1577 if ((value & 2) || (target == client))
1581 bShowActivity = true;
1586 /* Treat this as an admin user */
1587 bool is_root = g_Admins.GetAdminFlag(aidTarget, Admin_Root, Access_Effective);
1590 || ((value & 16) && is_root))
1592 if ((value & 8) || ((value & 16) && is_root) || (target == client))
1596 bShowActivity = true;
1600 /* Otherwise, send it back to the script. */
1601 pContext->StringToLocalUTF8(params[3], params[4], identity[mode], NULL);
1603 return bShowActivity ? 1 : 0;
1606 static cell_t sm_GetClientSerial(IPluginContext *pContext, const cell_t *params)
1608 int client = params[1];
1610 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1613 return pContext->ThrowNativeError("Client index %d is invalid", client);
1616 return pPlayer->GetSerial();
1619 static cell_t sm_GetClientFromSerial(IPluginContext *pContext, const cell_t *params)
1621 return g_Players.GetClientFromSerial(params[1]);
1624 REGISTER_NATIVES(playernatives)
1626 {"AddUserFlags", AddUserFlags},
1627 {"CanUserTarget", CanUserTarget},
1628 {"ChangeClientTeam", ChangeClientTeam},
1629 {"GetClientAuthString", sm_GetClientAuthStr},
1630 {"GetClientCount", sm_GetClientCount},
1631 {"GetClientInfo", sm_GetClientInfo},
1632 {"GetClientIP", sm_GetClientIP},
1633 {"GetClientName", sm_GetClientName},
1634 {"GetClientTeam", GetClientTeam},
1635 {"GetClientUserId", GetClientUserId},
1636 {"GetMaxClients", sm_GetMaxClients},
1637 {"GetUserAdmin", GetUserAdmin},
1638 {"GetUserFlagBits", GetUserFlagBits},
1639 {"IsClientAuthorized", sm_IsClientAuthorized},
1640 {"IsClientConnected", sm_IsClientConnected},
1641 {"IsFakeClient", sm_IsClientFakeClient},
1642 {"IsClientSourceTV", sm_IsClientSourceTV},
1643 {"IsClientReplay", sm_IsClientReplay},
1644 {"IsClientInGame", sm_IsClientInGame},
1645 {"IsClientObserver", IsClientObserver},
1646 {"RemoveUserFlags", RemoveUserFlags},
1647 {"SetUserAdmin", SetUserAdmin},
1648 {"SetUserFlagBits", SetUserFlagBits},
1649 {"GetClientDeaths", GetDeathCount},
1650 {"GetClientFrags", GetFragCount},
1651 {"GetClientArmor", GetArmorValue},
1652 {"GetClientAbsOrigin", GetAbsOrigin},
1653 {"GetClientAbsAngles", GetAbsAngles},
1654 {"GetClientMins", GetPlayerMins},
1655 {"GetClientMaxs", GetPlayerMaxs},
1656 {"GetClientWeapon", GetWeaponName},
1657 {"GetClientModel", GetModelName},
1658 {"GetClientHealth", GetHealth},
1659 {"GetClientTime", GetTimeConnected},
1660 {"GetClientDataRate", GetDataRate},
1661 {"IsClientTimingOut", IsTimingOut},
1662 {"GetClientLatency", GetLatency},
1663 {"GetClientAvgLatency", GetAvgLatency},
1664 {"GetClientAvgLoss", GetAvgLoss},
1665 {"GetClientAvgChoke", GetAvgChoke},
1666 {"GetClientAvgData", GetAvgData},
1667 {"GetClientAvgPackets", GetAvgPackets},
1668 {"GetClientOfUserId", GetClientOfUserId},
1669 {"ShowActivity", ShowActivity},
1670 {"ShowActivityEx", ShowActivityEx},
1671 {"ShowActivity2", ShowActivity2},
1672 {"KickClient", KickClient},
1673 {"KickClientEx", KickClientEx},
1674 {"RunAdminCacheChecks", RunAdminCacheChecks},
1675 {"NotifyPostAdminCheck", NotifyPostAdminCheck},
1676 {"IsClientInKickQueue", IsClientInKickQueue},
1677 {"ProcessTargetString", ProcessTargetString},
1678 {"FormatActivitySource", FormatActivitySource},
1679 {"GetClientSerial", sm_GetClientSerial},
1680 {"GetClientFromSerial", sm_GetClientFromSerial},