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 if (!pPlayer->IsAuthorized())
164 pCtx->StringToLocal(params[2], static_cast<size_t>(params[3]), pPlayer->GetAuthString());
169 static cell_t sm_IsClientConnected(IPluginContext *pCtx, const cell_t *params)
171 int index = params[1];
172 if ((index < 1) || (index > g_Players.GetMaxClients()))
174 return pCtx->ThrowNativeError("Client index %d is invalid", index);
177 return (g_Players.GetPlayerByIndex(index)->IsConnected()) ? 1 : 0;
180 static cell_t sm_IsClientInGame(IPluginContext *pCtx, const cell_t *params)
182 int index = params[1];
183 if ((index < 1) || (index > g_Players.GetMaxClients()))
185 return pCtx->ThrowNativeError("Client index %d is invalid", index);
188 return (g_Players.GetPlayerByIndex(index)->IsInGame()) ? 1 : 0;
191 static cell_t sm_IsClientAuthorized(IPluginContext *pCtx, const cell_t *params)
193 int index = params[1];
194 if ((index < 1) || (index > g_Players.GetMaxClients()))
196 return pCtx->ThrowNativeError("Client index %d is invalid", index);
199 return (g_Players.GetPlayerByIndex(index)->IsAuthorized()) ? 1 : 0;
202 static cell_t sm_IsClientFakeClient(IPluginContext *pCtx, const cell_t *params)
204 int index = params[1];
205 if ((index < 1) || (index > g_Players.GetMaxClients()))
207 return pCtx->ThrowNativeError("Client index %d is invalid", index);
210 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
211 if (!pPlayer->IsConnected())
213 return pCtx->ThrowNativeError("Client %d is not connected", index);
216 return (pPlayer->IsFakeClient()) ? 1 : 0;
219 static cell_t sm_IsClientSourceTV(IPluginContext *pCtx, const cell_t *params)
221 int index = params[1];
222 if ((index < 1) || (index > g_Players.GetMaxClients()))
224 return pCtx->ThrowNativeError("Client index %d is invalid", index);
227 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
228 if (!pPlayer->IsConnected())
230 return pCtx->ThrowNativeError("Client %d is not connected", index);
233 return (pPlayer->IsSourceTV()) ? 1 : 0;
236 static cell_t sm_IsClientReplay(IPluginContext *pCtx, const cell_t *params)
238 int index = params[1];
239 if ((index < 1) || (index > g_Players.GetMaxClients()))
241 return pCtx->ThrowNativeError("Client index %d is invalid", index);
244 CPlayer *pPlayer = g_Players.GetPlayerByIndex(index);
245 if (!pPlayer->IsConnected())
247 return pCtx->ThrowNativeError("Client %d is not connected", index);
250 return (pPlayer->IsReplay()) ? 1 : 0;
253 static cell_t IsClientObserver(IPluginContext *pContext, const cell_t *params)
255 int client = params[1];
257 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
260 return pContext->ThrowNativeError("Client index %d is invalid", client);
261 } else if (!pPlayer->IsInGame()) {
262 return pContext->ThrowNativeError("Client %d is not in game", client);
265 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
268 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
271 return pInfo->IsObserver() ? 1 : 0;
274 static cell_t sm_GetClientInfo(IPluginContext *pContext, const cell_t *params)
276 int client = params[1];
277 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
280 return pContext->ThrowNativeError("Client index %d is invalid", client);
282 if (!pPlayer->IsConnected())
284 return pContext->ThrowNativeError("Client %d is not connected", client);
288 pContext->LocalToString(params[2], &key);
290 const char *val = engine->GetClientConVarValue(client, key);
296 pContext->StringToLocalUTF8(params[3], params[4], val, NULL);
300 static cell_t SetUserAdmin(IPluginContext *pContext, const cell_t *params)
302 int client = params[1];
303 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
306 return pContext->ThrowNativeError("Client index %d is invalid", client);
308 if (!pPlayer->IsConnected())
310 return pContext->ThrowNativeError("Client %d is not connected", client);
312 if (!g_Admins.IsValidAdmin(params[2]) && params[2] != INVALID_ADMIN_ID)
314 return pContext->ThrowNativeError("AdminId %x is invalid", params[2]);
317 pPlayer->SetAdminId(params[2], params[3] ? true : false);
322 static cell_t GetUserAdmin(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);
335 return pPlayer->GetAdminId();
338 static cell_t AddUserFlags(IPluginContext *pContext, const cell_t *params)
340 int client = params[1];
341 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
344 return pContext->ThrowNativeError("Client index %d is invalid", client);
346 if (!pPlayer->IsConnected())
348 return pContext->ThrowNativeError("Client %d is not connected", client);
352 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
354 id = g_Admins.CreateAdmin(NULL);
355 pPlayer->SetAdminId(id, true);
359 for (int i=2; i<=params[0]; i++)
361 pContext->LocalToPhysAddr(params[i], &addr);
362 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, true);
368 static cell_t RemoveUserFlags(IPluginContext *pContext, const cell_t *params)
370 int client = params[1];
371 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
374 return pContext->ThrowNativeError("Client index %d is invalid", client);
376 if (!pPlayer->IsConnected())
378 return pContext->ThrowNativeError("Client %d is not connected", client);
382 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
388 for (int i=2; i<=params[0]; i++)
390 pContext->LocalToPhysAddr(params[i], &addr);
391 g_Admins.SetAdminFlag(id, (AdminFlag)*addr, false);
397 static cell_t SetUserFlagBits(IPluginContext *pContext, const cell_t *params)
399 int client = params[1];
400 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
403 return pContext->ThrowNativeError("Client index %d is invalid", client);
405 if (!pPlayer->IsConnected())
407 return pContext->ThrowNativeError("Client %d is not connected", client);
411 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
413 id = g_Admins.CreateAdmin(NULL);
414 pPlayer->SetAdminId(id, true);
417 g_Admins.SetAdminFlags(id, Access_Effective, params[2]);
422 static cell_t GetUserFlagBits(IPluginContext *pContext, const cell_t *params)
424 int client = params[1];
425 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
428 return pContext->ThrowNativeError("Client index %d is invalid", client);
430 if (!pPlayer->IsConnected())
432 return pContext->ThrowNativeError("Client %d is not connected", client);
436 if ((id=pPlayer->GetAdminId()) == INVALID_ADMIN_ID)
441 return g_Admins.GetAdminFlags(id, Access_Effective);
444 static cell_t GetClientUserId(IPluginContext *pContext, const cell_t *params)
446 int client = params[1];
447 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
450 return pContext->ThrowNativeError("Client index %d is invalid", client);
452 if (!pPlayer->IsConnected())
454 return pContext->ThrowNativeError("Client %d is not connected", client);
457 return engine->GetPlayerUserId(pPlayer->GetEdict());
460 static cell_t CanUserTarget(IPluginContext *pContext, const cell_t *params)
462 int client = params[1];
463 int target = params[2];
470 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
473 return pContext->ThrowNativeError("Client index %d is invalid", client);
474 } else if (!pPlayer->IsConnected()) {
475 return pContext->ThrowNativeError("Client %d is not connected", client);
478 CPlayer *pTarget = g_Players.GetPlayerByIndex(target);
481 return pContext->ThrowNativeError("Client index %d is invalid", target);
482 } else if (!pTarget->IsConnected()) {
483 return pContext->ThrowNativeError("Client %d is not connected", target);
486 return g_Admins.CanAdminTarget(pPlayer->GetAdminId(), pTarget->GetAdminId()) ? 1 : 0;
489 static cell_t GetClientTeam(IPluginContext *pContext, const cell_t *params)
491 int client = params[1];
493 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
496 return pContext->ThrowNativeError("Client index %d is invalid", client);
497 } else if (!pPlayer->IsInGame()) {
498 return pContext->ThrowNativeError("Client %d is not in game", client);
501 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
504 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
507 return pInfo->GetTeamIndex();
510 static cell_t GetFragCount(IPluginContext *pContext, const cell_t *params)
512 int client = params[1];
514 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
517 return pContext->ThrowNativeError("Client index %d is invalid", client);
518 } else if (!pPlayer->IsInGame()) {
519 return pContext->ThrowNativeError("Client %d is not in game", client);
522 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
525 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
528 return pInfo->GetFragCount();
531 static cell_t GetDeathCount(IPluginContext *pContext, const cell_t *params)
533 int client = params[1];
535 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
538 return pContext->ThrowNativeError("Client index %d is invalid", client);
539 } else if (!pPlayer->IsInGame()) {
540 return pContext->ThrowNativeError("Client %d is not in game", client);
543 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
546 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
549 return pInfo->GetDeathCount();
552 static cell_t GetArmorValue(IPluginContext *pContext, const cell_t *params)
554 int client = params[1];
556 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
559 return pContext->ThrowNativeError("Client index %d is invalid", client);
560 } else if (!pPlayer->IsInGame()) {
561 return pContext->ThrowNativeError("Client %d is not in game", client);
564 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
567 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
570 return pInfo->GetArmorValue();
573 static cell_t GetAbsOrigin(IPluginContext *pContext, const cell_t *params)
575 int client = params[1];
577 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
580 return pContext->ThrowNativeError("Client index %d is invalid", client);
581 } else if (!pPlayer->IsInGame()) {
582 return pContext->ThrowNativeError("Client %d is not in game", client);
585 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
588 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
592 pContext->LocalToPhysAddr(params[2], &pVec);
594 Vector vec = pInfo->GetAbsOrigin();
595 pVec[0] = sp_ftoc(vec.x);
596 pVec[1] = sp_ftoc(vec.y);
597 pVec[2] = sp_ftoc(vec.z);
602 static cell_t GetAbsAngles(IPluginContext *pContext, const cell_t *params)
604 int client = params[1];
606 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
609 return pContext->ThrowNativeError("Client index %d is invalid", client);
610 } else if (!pPlayer->IsInGame()) {
611 return pContext->ThrowNativeError("Client %d is not in game", client);
614 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
617 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
621 pContext->LocalToPhysAddr(params[2], &pAng);
623 QAngle ang = pInfo->GetAbsAngles();
624 pAng[0] = sp_ftoc(ang.x);
625 pAng[1] = sp_ftoc(ang.y);
626 pAng[2] = sp_ftoc(ang.z);
631 static cell_t GetPlayerMins(IPluginContext *pContext, const cell_t *params)
633 int client = params[1];
635 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
638 return pContext->ThrowNativeError("Client index %d is invalid", client);
639 } else if (!pPlayer->IsInGame()) {
640 return pContext->ThrowNativeError("Client %d is not in game", client);
643 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
646 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
650 pContext->LocalToPhysAddr(params[2], &pVec);
652 Vector vec = pInfo->GetPlayerMins();
653 pVec[0] = sp_ftoc(vec.x);
654 pVec[1] = sp_ftoc(vec.y);
655 pVec[2] = sp_ftoc(vec.z);
660 static cell_t GetPlayerMaxs(IPluginContext *pContext, const cell_t *params)
662 int client = params[1];
664 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
667 return pContext->ThrowNativeError("Client index %d is invalid", client);
668 } else if (!pPlayer->IsInGame()) {
669 return pContext->ThrowNativeError("Client %d is not in game", client);
672 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
675 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
679 pContext->LocalToPhysAddr(params[2], &pVec);
681 Vector vec = pInfo->GetPlayerMaxs();
682 pVec[0] = sp_ftoc(vec.x);
683 pVec[1] = sp_ftoc(vec.y);
684 pVec[2] = sp_ftoc(vec.z);
689 static cell_t GetWeaponName(IPluginContext *pContext, const cell_t *params)
691 int client = params[1];
693 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
696 return pContext->ThrowNativeError("Client index %d is invalid", client);
697 } else if (!pPlayer->IsInGame()) {
698 return pContext->ThrowNativeError("Client %d is not in game", client);
701 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
704 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
707 const char *weapon = pInfo->GetWeaponName();
708 pContext->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), weapon ? weapon : "", NULL);
713 static cell_t GetModelName(IPluginContext *pContext, const cell_t *params)
715 int client = params[1];
717 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
720 return pContext->ThrowNativeError("Client index %d is invalid", client);
721 } else if (!pPlayer->IsInGame()) {
722 return pContext->ThrowNativeError("Client %d is not in game", client);
725 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
728 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
731 const char *model = pInfo->GetModelName();
732 pContext->StringToLocalUTF8(params[2], static_cast<size_t>(params[3]), model ? model : "", NULL);
737 static cell_t GetHealth(IPluginContext *pContext, const cell_t *params)
739 int client = params[1];
741 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
744 return pContext->ThrowNativeError("Client index %d is invalid", client);
745 } else if (!pPlayer->IsInGame()) {
746 return pContext->ThrowNativeError("Client %d is not in game", client);
749 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
752 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
755 return pInfo->GetHealth();
758 static cell_t GetTimeConnected(IPluginContext *pContext, const cell_t *params)
760 int client = params[1];
762 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
765 return pContext->ThrowNativeError("Client index %d is invalid", client);
766 } else if (!pPlayer->IsInGame()) {
767 return pContext->ThrowNativeError("Client %d is not in game", client);
768 } else if (pPlayer->IsFakeClient()) {
769 return pContext->ThrowNativeError("Client %d is a bot", client);
772 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
778 return sp_ftoc(pInfo->GetTimeConnected());
781 static cell_t GetDataRate(IPluginContext *pContext, const cell_t *params)
783 int client = params[1];
785 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
788 return pContext->ThrowNativeError("Client index %d is invalid", client);
789 } else if (!pPlayer->IsInGame()) {
790 return pContext->ThrowNativeError("Client %d is not in game", client);
791 } else if (pPlayer->IsFakeClient()) {
792 return pContext->ThrowNativeError("Client %d is a bot", client);
795 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
801 return pInfo->GetDataRate();
804 static cell_t IsTimingOut(IPluginContext *pContext, const cell_t *params)
806 int client = params[1];
808 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
811 return pContext->ThrowNativeError("Client index %d is invalid", client);
812 } else if (!pPlayer->IsInGame()) {
813 return pContext->ThrowNativeError("Client %d is not in game", client);
814 } else if (pPlayer->IsFakeClient()) {
815 return pContext->ThrowNativeError("Client %d is a bot", client);
818 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
824 return pInfo->IsTimingOut() ? 1 : 0;
827 static cell_t GetLatency(IPluginContext *pContext, const cell_t *params)
829 int client = params[1];
832 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
835 return pContext->ThrowNativeError("Client index %d is invalid", client);
837 else if (!pPlayer->IsInGame())
839 return pContext->ThrowNativeError("Client %d is not in game", client);
841 else if (pPlayer->IsFakeClient())
843 return pContext->ThrowNativeError("Client %d is a bot", client);
846 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
852 if (params[2] == MAX_FLOWS)
854 value = pInfo->GetLatency(FLOW_INCOMING) + pInfo->GetLatency(FLOW_OUTGOING);
858 value = pInfo->GetLatency(params[2]);
861 return sp_ftoc(value);
864 static cell_t GetAvgLatency(IPluginContext *pContext, const cell_t *params)
866 int client = params[1];
869 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
872 return pContext->ThrowNativeError("Client index %d is invalid", client);
874 else if (!pPlayer->IsInGame())
876 return pContext->ThrowNativeError("Client %d is not in game", client);
878 else if (pPlayer->IsFakeClient())
880 return pContext->ThrowNativeError("Client %d is a bot", client);
883 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
889 if (params[2] == MAX_FLOWS)
891 value = pInfo->GetAvgLatency(FLOW_INCOMING) + pInfo->GetAvgLatency(FLOW_OUTGOING);
895 value = pInfo->GetAvgLatency(params[2]);
898 return sp_ftoc(value);
901 static cell_t GetAvgLoss(IPluginContext *pContext, const cell_t *params)
903 int client = params[1];
906 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
909 return pContext->ThrowNativeError("Client index %d is invalid", client);
911 else if (!pPlayer->IsInGame())
913 return pContext->ThrowNativeError("Client %d is not in game", client);
915 else if (pPlayer->IsFakeClient())
917 return pContext->ThrowNativeError("Client %d is a bot", client);
920 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
926 if (params[2] == MAX_FLOWS)
928 value = pInfo->GetAvgLoss(FLOW_INCOMING) + pInfo->GetAvgLoss(FLOW_OUTGOING);
932 value = pInfo->GetAvgLoss(params[2]);
935 return sp_ftoc(value);
938 static cell_t GetAvgChoke(IPluginContext *pContext, const cell_t *params)
940 int client = params[1];
943 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
946 return pContext->ThrowNativeError("Client index %d is invalid", client);
948 else if (!pPlayer->IsInGame())
950 return pContext->ThrowNativeError("Client %d is not in game", client);
952 else if (pPlayer->IsFakeClient())
954 return pContext->ThrowNativeError("Client %d is a bot", client);
957 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
963 if (params[2] == MAX_FLOWS)
965 value = pInfo->GetAvgChoke(FLOW_INCOMING) + pInfo->GetAvgChoke(FLOW_OUTGOING);
969 value = pInfo->GetAvgChoke(params[2]);
972 return sp_ftoc(value);
975 static cell_t GetAvgData(IPluginContext *pContext, const cell_t *params)
977 int client = params[1];
980 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
983 return pContext->ThrowNativeError("Client index %d is invalid", client);
985 else if (!pPlayer->IsInGame())
987 return pContext->ThrowNativeError("Client %d is not in game", client);
989 else if (pPlayer->IsFakeClient())
991 return pContext->ThrowNativeError("Client %d is a bot", client);
994 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1000 if (params[2] == MAX_FLOWS)
1002 value = pInfo->GetAvgData(FLOW_INCOMING) + pInfo->GetAvgData(FLOW_OUTGOING);
1006 value = pInfo->GetAvgData(params[2]);
1009 return sp_ftoc(value);
1012 static cell_t GetAvgPackets(IPluginContext *pContext, const cell_t *params)
1014 int client = params[1];
1017 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1020 return pContext->ThrowNativeError("Client index %d is invalid", client);
1022 else if (!pPlayer->IsInGame())
1024 return pContext->ThrowNativeError("Client %d is not in game", client);
1026 else if (pPlayer->IsFakeClient())
1028 return pContext->ThrowNativeError("Client %d is a bot", client);
1031 INetChannelInfo *pInfo = engine->GetPlayerNetInfo(client);
1038 if (params[2] == MAX_FLOWS)
1040 value = pInfo->GetAvgPackets(FLOW_INCOMING) + pInfo->GetAvgPackets(FLOW_OUTGOING);
1044 value = pInfo->GetAvgPackets(params[2]);
1047 return sp_ftoc(value);
1050 static cell_t GetClientOfUserId(IPluginContext *pContext, const cell_t *params)
1052 return g_Players.GetClientOfUserId(params[1]);
1055 static cell_t _ShowActivity(IPluginContext *pContext,
1056 const cell_t *params,
1062 int value = sm_show_activity.GetInt();
1063 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1064 int client = params[1];
1066 const char *name = "Console";
1067 const char *sign = "ADMIN";
1068 bool display_in_chat = false;
1071 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1072 if (!pPlayer || !pPlayer->IsConnected())
1074 return pContext->ThrowNativeError("Client index %d is invalid", client);
1076 name = pPlayer->GetName();
1077 AdminId id = pPlayer->GetAdminId();
1078 if (id == INVALID_ADMIN_ID
1079 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1084 /* Display the message to the client? */
1085 if (replyto == SM_REPLY_CONSOLE)
1087 g_SourceMod.SetGlobalTarget(client);
1088 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1090 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1095 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1096 engine->ClientPrintf(pPlayer->GetEdict(), message);
1097 display_in_chat = true;
1102 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1103 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1105 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1110 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1111 META_CONPRINT(message);
1119 int maxClients = g_Players.GetMaxClients();
1120 for (int i=1; i<=maxClients; i++)
1122 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1123 if (!pPlayer->IsInGame()
1124 || pPlayer->IsFakeClient()
1125 || (display_in_chat && i == client))
1129 AdminId id = pPlayer->GetAdminId();
1130 g_SourceMod.SetGlobalTarget(i);
1131 if (id == INVALID_ADMIN_ID
1132 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1134 /* Treat this as a normal user */
1135 if ((value & 1) || (value & 2))
1137 const char *newsign = sign;
1138 if ((value & 2) || (i == client))
1142 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1144 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1149 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1150 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1155 /* Treat this as an admin user */
1156 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1159 || ((value & 16) && is_root))
1161 const char *newsign = sign;
1162 if ((value & 8) || ((value & 16) && is_root) || (i == client))
1166 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1168 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1173 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1174 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1182 static cell_t _ShowActivity2(IPluginContext *pContext,
1183 const cell_t *params,
1189 int value = sm_show_activity.GetInt();
1190 unsigned int replyto = g_ChatTriggers.GetReplyTo();
1191 int client = params[1];
1193 const char *name = "Console";
1194 const char *sign = "ADMIN";
1197 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1198 if (!pPlayer || !pPlayer->IsConnected())
1200 return pContext->ThrowNativeError("Client index %d is invalid", client);
1202 name = pPlayer->GetName();
1203 AdminId id = pPlayer->GetAdminId();
1204 if (id == INVALID_ADMIN_ID
1205 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1210 g_SourceMod.SetGlobalTarget(client);
1211 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1213 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1218 /* We don't display directly to the console because the chat text
1219 * simply gets added to the console, so we don't want it to print
1222 if (replyto == SM_REPLY_CONSOLE)
1225 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1226 engine->ClientPrintf(pPlayer->GetEdict(), message);
1228 UTIL_Format(message, sizeof(message), "%s%s", tag, buffer);
1229 g_HL2.TextMsg(client, HUD_PRINTTALK, message);
1233 UTIL_Format(message, sizeof(message), "%s%s", tag, buffer);
1234 g_HL2.TextMsg(client, HUD_PRINTTALK, message);
1239 g_SourceMod.SetGlobalTarget(SOURCEMOD_SERVER_LANGUAGE);
1240 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1242 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1247 UTIL_Format(message, sizeof(message), "%s%s\n", tag, buffer);
1248 META_CONPRINT(message);
1256 int maxClients = g_Players.GetMaxClients();
1257 for (int i=1; i<=maxClients; i++)
1259 CPlayer *pPlayer = g_Players.GetPlayerByIndex(i);
1260 if (!pPlayer->IsInGame()
1261 || pPlayer->IsFakeClient()
1266 AdminId id = pPlayer->GetAdminId();
1267 g_SourceMod.SetGlobalTarget(i);
1268 if (id == INVALID_ADMIN_ID
1269 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1271 /* Treat this as a normal user */
1272 if ((value & 1) || (value & 2))
1274 const char *newsign = sign;
1279 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1281 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1286 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1287 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1292 /* Treat this as an admin user */
1293 bool is_root = g_Admins.GetAdminFlag(id, Admin_Root, Access_Effective);
1296 || ((value & 16) && is_root))
1298 const char *newsign = sign;
1299 if ((value & 8) || ((value & 16) && is_root))
1303 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, fmt_param);
1305 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1310 UTIL_Format(message, sizeof(message), "%s%s: %s", tag, newsign, buffer);
1311 g_HL2.TextMsg(i, HUD_PRINTTALK, message);
1319 static cell_t ShowActivity(IPluginContext *pContext, const cell_t *params)
1321 return _ShowActivity(pContext, params, "[SM] ", 2);
1324 static cell_t ShowActivityEx(IPluginContext *pContext, const cell_t *params)
1327 pContext->LocalToString(params[2], &str);
1329 return _ShowActivity(pContext, params, str, 3);
1332 static cell_t ShowActivity2(IPluginContext *pContext, const cell_t *params)
1335 pContext->LocalToString(params[2], &str);
1337 return _ShowActivity2(pContext, params, str, 3);
1340 static cell_t KickClient(IPluginContext *pContext, const cell_t *params)
1342 int client = params[1];
1344 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1347 return pContext->ThrowNativeError("Client index %d is invalid", client);
1349 else if (!pPlayer->IsConnected())
1351 return pContext->ThrowNativeError("Client %d is not connected", client);
1354 /* Ignore duplicate kicks */
1355 if (pPlayer->IsInKickQueue())
1360 g_SourceMod.SetGlobalTarget(client);
1363 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1365 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1370 if (pPlayer->IsFakeClient())
1372 // Kick uses the kickid command for bots. It is already delayed
1373 // until the next frame unless someone flushes command buffer
1374 pPlayer->Kick(buffer);
1378 pPlayer->MarkAsBeingKicked();
1379 g_HL2.AddDelayedKick(client, pPlayer->GetUserId(), buffer);
1384 static cell_t KickClientEx(IPluginContext *pContext, const cell_t *params)
1386 int client = params[1];
1388 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1391 return pContext->ThrowNativeError("Client index %d is invalid", client);
1393 else if (!pPlayer->IsConnected())
1395 return pContext->ThrowNativeError("Client %d is not connected", client);
1398 g_SourceMod.SetGlobalTarget(client);
1401 g_SourceMod.FormatString(buffer, sizeof(buffer), pContext, params, 2);
1403 if (pContext->GetLastNativeError() != SP_ERROR_NONE)
1408 pPlayer->Kick(buffer);
1413 static cell_t ChangeClientTeam(IPluginContext *pContext, const cell_t *params)
1415 int client = params[1];
1417 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1420 return pContext->ThrowNativeError("Client index %d is invalid", client);
1422 else if (!pPlayer->IsInGame())
1424 return pContext->ThrowNativeError("Client %d is not in game", client);
1427 IPlayerInfo *pInfo = pPlayer->GetPlayerInfo();
1430 return pContext->ThrowNativeError("IPlayerInfo not supported by game");
1433 pInfo->ChangeTeam(params[2]);
1438 static cell_t RunAdminCacheChecks(IPluginContext *pContext, const cell_t *params)
1440 int client = params[1];
1442 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1445 return pContext->ThrowNativeError("Client index %d is invalid", client);
1447 else if (!pPlayer->IsInGame())
1449 return pContext->ThrowNativeError("Client %d is not in game", client);
1451 else if (!pPlayer->IsAuthorized())
1453 return pContext->ThrowNativeError("Client %d is not authorized", client);
1456 AdminId id = pPlayer->GetAdminId();
1457 pPlayer->DoBasicAdminChecks();
1459 return (id != pPlayer->GetAdminId()) ? 1 : 0;
1462 static cell_t NotifyPostAdminCheck(IPluginContext *pContext, const cell_t *params)
1464 int client = params[1];
1466 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1469 return pContext->ThrowNativeError("Client index %d is invalid", client);
1471 else if (!pPlayer->IsInGame()) {
1472 return pContext->ThrowNativeError("Client %d is not in game", client);
1474 else if (!pPlayer->IsAuthorized())
1476 return pContext->ThrowNativeError("Client %d is not authorized", client);
1479 pPlayer->NotifyPostAdminChecks();
1484 static cell_t IsClientInKickQueue(IPluginContext *pContext, const cell_t *params)
1486 int client = params[1];
1488 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1492 return pContext->ThrowNativeError("Client index %d is invalid", client);
1494 else if (!pPlayer->IsConnected())
1496 return pContext->ThrowNativeError("Client %d is not in game", client);
1499 return pPlayer->IsInKickQueue() ? 1 : 0;
1502 static cell_t ProcessTargetString(IPluginContext *pContext, const cell_t *params)
1504 cmd_target_info_t info;
1506 pContext->LocalToString(params[1], (char **)&info.pattern);
1507 info.admin = params[2];
1508 pContext->LocalToPhysAddr(params[3], &info.targets);
1509 info.max_targets = params[4];
1510 info.flags = params[5];
1511 pContext->LocalToString(params[6], &info.target_name);
1512 info.target_name_maxlength = params[7];
1515 pContext->LocalToPhysAddr(params[8], &tn_is_ml);
1517 g_Players.ProcessCommandTarget(&info);
1519 if (info.target_name_style == COMMAND_TARGETNAME_ML)
1528 if (info.num_targets == 0)
1534 return info.num_targets;
1538 static cell_t FormatActivitySource(IPluginContext *pContext, const cell_t *params)
1545 const char *identity[2] = {"Console", "ADMIN"};
1550 if ((pTarget = g_Players.GetPlayerByIndex(target)) == NULL)
1552 return pContext->ThrowNativeError("Invalid client index %d", target);
1554 if (!pTarget->IsConnected())
1556 return pContext->ThrowNativeError("Client %d not connected", target);
1559 value = sm_show_activity.GetInt();
1565 if ((pPlayer = g_Players.GetPlayerByIndex(client)) == NULL)
1567 return pContext->ThrowNativeError("Invalid client index %d", client);
1569 if (!pPlayer->IsConnected())
1571 return pContext->ThrowNativeError("Client %d not connected", client);
1574 identity[0] = pPlayer->GetName();
1576 AdminId id = pPlayer->GetAdminId();
1577 if (id == INVALID_ADMIN_ID
1578 || !g_Admins.GetAdminFlag(id, Admin_Generic, Access_Effective))
1580 identity[1] = "PLAYER";
1585 bool bShowActivity = false;
1587 if ((aidTarget = pTarget->GetAdminId()) == INVALID_ADMIN_ID
1588 || !g_Admins.GetAdminFlag(aidTarget, Admin_Generic, Access_Effective))
1590 /* Treat this as a normal user */
1591 if ((value & 1) || (value & 2))
1593 if ((value & 2) || (target == client))
1597 bShowActivity = true;
1602 /* Treat this as an admin user */
1603 bool is_root = g_Admins.GetAdminFlag(aidTarget, Admin_Root, Access_Effective);
1606 || ((value & 16) && is_root))
1608 if ((value & 8) || ((value & 16) && is_root) || (target == client))
1612 bShowActivity = true;
1616 /* Otherwise, send it back to the script. */
1617 pContext->StringToLocalUTF8(params[3], params[4], identity[mode], NULL);
1619 return bShowActivity ? 1 : 0;
1622 static cell_t sm_GetClientSerial(IPluginContext *pContext, const cell_t *params)
1624 int client = params[1];
1626 CPlayer *pPlayer = g_Players.GetPlayerByIndex(client);
1629 return pContext->ThrowNativeError("Client index %d is invalid", client);
1632 return pPlayer->GetSerial();
1635 static cell_t sm_GetClientFromSerial(IPluginContext *pContext, const cell_t *params)
1637 return g_Players.GetClientFromSerial(params[1]);
1640 REGISTER_NATIVES(playernatives)
1642 {"AddUserFlags", AddUserFlags},
1643 {"CanUserTarget", CanUserTarget},
1644 {"ChangeClientTeam", ChangeClientTeam},
1645 {"GetClientAuthString", sm_GetClientAuthStr},
1646 {"GetClientCount", sm_GetClientCount},
1647 {"GetClientInfo", sm_GetClientInfo},
1648 {"GetClientIP", sm_GetClientIP},
1649 {"GetClientName", sm_GetClientName},
1650 {"GetClientTeam", GetClientTeam},
1651 {"GetClientUserId", GetClientUserId},
1652 {"GetMaxClients", sm_GetMaxClients},
1653 {"GetMaxHumanPlayers", sm_GetMaxHumanPlayers},
1654 {"GetUserAdmin", GetUserAdmin},
1655 {"GetUserFlagBits", GetUserFlagBits},
1656 {"IsClientAuthorized", sm_IsClientAuthorized},
1657 {"IsClientConnected", sm_IsClientConnected},
1658 {"IsFakeClient", sm_IsClientFakeClient},
1659 {"IsClientSourceTV", sm_IsClientSourceTV},
1660 {"IsClientReplay", sm_IsClientReplay},
1661 {"IsClientInGame", sm_IsClientInGame},
1662 {"IsClientObserver", IsClientObserver},
1663 {"RemoveUserFlags", RemoveUserFlags},
1664 {"SetUserAdmin", SetUserAdmin},
1665 {"SetUserFlagBits", SetUserFlagBits},
1666 {"GetClientDeaths", GetDeathCount},
1667 {"GetClientFrags", GetFragCount},
1668 {"GetClientArmor", GetArmorValue},
1669 {"GetClientAbsOrigin", GetAbsOrigin},
1670 {"GetClientAbsAngles", GetAbsAngles},
1671 {"GetClientMins", GetPlayerMins},
1672 {"GetClientMaxs", GetPlayerMaxs},
1673 {"GetClientWeapon", GetWeaponName},
1674 {"GetClientModel", GetModelName},
1675 {"GetClientHealth", GetHealth},
1676 {"GetClientTime", GetTimeConnected},
1677 {"GetClientDataRate", GetDataRate},
1678 {"IsClientTimingOut", IsTimingOut},
1679 {"GetClientLatency", GetLatency},
1680 {"GetClientAvgLatency", GetAvgLatency},
1681 {"GetClientAvgLoss", GetAvgLoss},
1682 {"GetClientAvgChoke", GetAvgChoke},
1683 {"GetClientAvgData", GetAvgData},
1684 {"GetClientAvgPackets", GetAvgPackets},
1685 {"GetClientOfUserId", GetClientOfUserId},
1686 {"ShowActivity", ShowActivity},
1687 {"ShowActivityEx", ShowActivityEx},
1688 {"ShowActivity2", ShowActivity2},
1689 {"KickClient", KickClient},
1690 {"KickClientEx", KickClientEx},
1691 {"RunAdminCacheChecks", RunAdminCacheChecks},
1692 {"NotifyPostAdminCheck", NotifyPostAdminCheck},
1693 {"IsClientInKickQueue", IsClientInKickQueue},
1694 {"ProcessTargetString", ProcessTargetString},
1695 {"FormatActivitySource", FormatActivitySource},
1696 {"GetClientSerial", sm_GetClientSerial},
1697 {"GetClientFromSerial", sm_GetClientFromSerial},