plugins/basechat.sp
changeset 1174 66913d79bd49
child 1178 35e342535d2b
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/basechat.sp	Thu Jul 26 21:37:13 2007 +0000
     1.3 @@ -0,0 +1,551 @@
     1.4 +/**
     1.5 + * basechat.sp
     1.6 + * Implements basic communication commands
     1.7 + * This file is part of SourceMod, Copyright (C) 2004-2007 AlliedModders LLC
     1.8 + *
     1.9 + * This program is free software; you can redistribute it and/or
    1.10 + * modify it under the terms of the GNU General Public License
    1.11 + * as published by the Free Software Foundation; either version 2
    1.12 + * of the License, or (at your option) any later version.
    1.13 + *
    1.14 + * This program is distributed in the hope that it will be useful,
    1.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 + * GNU General Public License for more details.
    1.18 + * 
    1.19 + * You should have received a copy of the GNU General Public License
    1.20 + * along with this program; if not, write to the Free Software
    1.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    1.22 + *
    1.23 + * Version: $Id$
    1.24 + */
    1.25 +
    1.26 +#include <sourcemod>
    1.27 +
    1.28 +#pragma semicolon 1
    1.29 +
    1.30 +public Plugin:myinfo = 
    1.31 +{
    1.32 +	name = "Basic Chat",
    1.33 +	author = "AlliedModders LLC",
    1.34 +	description = "Basic Communication Commands",
    1.35 +	version = SOURCEMOD_VERSION,
    1.36 +	url = "http://www.sourcemod.net/"
    1.37 +};
    1.38 +
    1.39 +decl String:g_ColorNames[13][10] = {"White", "Red", "Green", "Blue", "Yellow", "Purple", "Cyan", "Orange", "Pink", "Olive", "Lime", "Violet", "Lightblue"};
    1.40 +new g_Colors[13][3] = {{255,255,255},{255,0,0},{0,255,0},{0,0,255},{255,255,0},{255,0,255},{0,255,255},{255,128,0},{255,0,128},{128,255,0},{0,255,128},{128,0,255},{0,128,255}};
    1.41 +
    1.42 +new Handle:g_Cvar_Chatmode = INVALID_HANDLE;
    1.43 +new Handle:g_Cvar_Psaymode = INVALID_HANDLE;
    1.44 +
    1.45 +new bool:g_DoColor = false;
    1.46 +
    1.47 +#if 0
    1.48 +new g_INS = false;
    1.49 +#endif
    1.50 +
    1.51 +public OnPluginStart()
    1.52 +{
    1.53 +	LoadTranslations("common.phrases");
    1.54 +	LoadTranslations("plugin.basecommands");
    1.55 +
    1.56 +	g_Cvar_Chatmode = CreateConVar("sm_chat_mode", "1", "Allows player's to send messages to admin chat.", 0, true, 0.0, true, 1.0);
    1.57 +	g_Cvar_Psaymode = CreateConVar("sm_psay_mode", "0", "Allows player's to use psay 'say @@' alias.", 0, true, 0.0, true, 1.0);
    1.58 +
    1.59 +	RegConsoleCmd("say", Command_SayChat);
    1.60 +	RegConsoleCmd("say_team", Command_SayAdmin);		
    1.61 +	RegAdminCmd("sm_say", Command_SmSay, ADMFLAG_CHAT, "sm_say <message> - sends message to all players");
    1.62 +	RegAdminCmd("sm_csay", Command_SmCsay, ADMFLAG_CHAT, "sm_csay <message> - sends centered message to all players");
    1.63 +	//RegAdminCmd("sm_hsay", Command_SmHsay, ADMFLAG_CHAT, "sm_hsay <message> - sends hint message to all players");
    1.64 +	RegAdminCmd("sm_tsay", Command_SmTsay, ADMFLAG_CHAT, "sm_tsay [color] <message> - sends top-left message to all players");
    1.65 +	RegAdminCmd("sm_chat", Command_SmChat, ADMFLAG_CHAT, "sm_chat <message> - sends message to admins");
    1.66 +	RegAdminCmd("sm_psay", Command_SmPsay, ADMFLAG_CHAT, "sm_psay <name or #userid> <message> - sends private message");
    1.67 +	RegAdminCmd("sm_msay", Command_SmMsay, ADMFLAG_CHAT, "sm_msay <message> - sends message as a menu panel");
    1.68 +	
    1.69 +	decl String:modname[64];
    1.70 +	GetGameFolderName(modname, sizeof(modname));
    1.71 +	
    1.72 +	if (strcmp(modname, "cstrike") == 0)
    1.73 +	{
    1.74 +		g_DoColor = true;
    1.75 +		RegAdminCmd("sm_hsay", Command_SmHsay, ADMFLAG_CHAT, "sm_hsay <message> - sends hint message to all players");
    1.76 +	}
    1.77 +	
    1.78 +	#if 0
    1.79 +	if (strcmp(modname, "ins") == 0)
    1.80 +	{
    1.81 +		RegConsoleCmd("say2", Command_SayChat);
    1.82 +		g_INS = true;
    1.83 +	}
    1.84 +	#endif
    1.85 +}
    1.86 +
    1.87 +public Action:Command_SayChat(client, args)
    1.88 +{	
    1.89 +	decl String:text[192];
    1.90 +	GetCmdArgString(text, sizeof(text));
    1.91 +	
    1.92 +	new startidx;
    1.93 +	if (text[strlen(text)-1] == '"')
    1.94 +	{
    1.95 +		text[strlen(text)-1] = '\0';
    1.96 +		startidx = 1;
    1.97 +	}
    1.98 +	
    1.99 +	#if 0
   1.100 +	if (g_INS)
   1.101 +	{
   1.102 +		startidx += 4;
   1.103 +	}
   1.104 +	#endif
   1.105 +	
   1.106 +	if (text[startidx] != '@')
   1.107 +		return Plugin_Continue;
   1.108 +	
   1.109 +	new msgStart = 1;
   1.110 +	
   1.111 +	if (text[startidx+1] == '@')
   1.112 +	{
   1.113 +		msgStart = 2;
   1.114 +		
   1.115 +		if (text[startidx+2] == '@')
   1.116 +			msgStart = 3;
   1.117 +	}
   1.118 +	
   1.119 +	decl String:message[192];
   1.120 +	strcopy(message, 192, text[startidx+msgStart]);
   1.121 +	
   1.122 +	decl String:name[64];
   1.123 +	GetClientName(client, name, sizeof(name));
   1.124 +	
   1.125 +	if (msgStart == 1 && CheckAdminForChat(client)) // sm_say alias
   1.126 +	{
   1.127 +		SendChatToAll(name, message);
   1.128 +		LogMessage("%L triggered sm_say (text %s)", client, message);
   1.129 +	}
   1.130 +	else if (msgStart == 3 && CheckAdminForChat(client)) // sm_csay alias
   1.131 +	{
   1.132 +		PrintCenterTextAll("%s: %s", name, message);
   1.133 +		LogMessage("%L triggered sm_csay (text %s)", client, text);		
   1.134 +	}	
   1.135 +	else if (msgStart == 2 && (CheckAdminForChat(client) || GetConVarBool(g_Cvar_Psaymode))) // sm_psay alias
   1.136 +	{
   1.137 +		decl String:target[64];
   1.138 +	
   1.139 +		new len = BreakString(message, target, sizeof(target));
   1.140 +		
   1.141 +		new clients[2];
   1.142 +		new numClients = SearchForClients(target, clients, 2);
   1.143 +		
   1.144 +		if (numClients == 0)
   1.145 +		{
   1.146 +			PrintToChat(client, "[SM] %t", "No matching client");
   1.147 +			return Plugin_Handled;
   1.148 +		}
   1.149 +		else if (numClients > 1)
   1.150 +		{
   1.151 +			PrintToChat(client, "[SM] %t", "More than one client matches", target);
   1.152 +			return Plugin_Handled;
   1.153 +		}
   1.154 +		
   1.155 +		decl String:name2[64];
   1.156 +		GetClientName(clients[0], name2, sizeof(name2));
   1.157 +	
   1.158 +		if (g_DoColor)
   1.159 +		{
   1.160 +			PrintToChat(client, "\x04(Private to %s) %s: \x01%s", name2, name, message[len]);
   1.161 +			PrintToChat(clients[0], "\x04(Private to %s) %s: \x01%s", name2, name, message[len]);
   1.162 +		}
   1.163 +		else
   1.164 +		{
   1.165 +			PrintToChat(client, "(Private to %s) %s: %s", name2, name, message[len]);
   1.166 +			PrintToChat(clients[0], "(Private to %s) %s: %s", name2, name, message[len]);			
   1.167 +		}
   1.168 +
   1.169 +		LogMessage("%L triggered sm_psay to %L (text %s)", client, clients[0], message);		
   1.170 +	}
   1.171 +	else
   1.172 +		return Plugin_Continue;
   1.173 +	
   1.174 +	return Plugin_Handled;	
   1.175 +}
   1.176 +
   1.177 +public Action:Command_SayAdmin(client, args)
   1.178 +{
   1.179 +	if (!CheckAdminForChat(client) && !GetConVarBool(g_Cvar_Chatmode))
   1.180 +		return Plugin_Continue;	
   1.181 +	
   1.182 +	decl String:text[192];
   1.183 +	GetCmdArgString(text, sizeof(text));
   1.184 +	
   1.185 +	new startidx;
   1.186 +	if (text[strlen(text)-1] == '"')
   1.187 +	{
   1.188 +		text[strlen(text)-1] = '\0';
   1.189 +		startidx = 1;
   1.190 +	}
   1.191 +	
   1.192 +	#if 0
   1.193 +	if (g_INS)
   1.194 +	{
   1.195 +		startidx += 4;
   1.196 +	}
   1.197 +	#endif	
   1.198 +	
   1.199 +	if (text[startidx] != '@')
   1.200 +		return Plugin_Continue;
   1.201 +	
   1.202 +	decl String:message[192];
   1.203 +	strcopy(message, 192, text[startidx+1]);
   1.204 +
   1.205 +	decl String:name[64];
   1.206 +	GetClientName(client, name, sizeof(name));
   1.207 +	
   1.208 +	SendChatToAdmins(name, message);
   1.209 +	LogMessage("%L triggered sm_chat (text %s)", client, message);
   1.210 +	
   1.211 +	return Plugin_Handled;	
   1.212 +}
   1.213 +
   1.214 +public Action:Command_SmSay(client, args)
   1.215 +{
   1.216 +	if (args < 1)
   1.217 +	{
   1.218 +		ReplyToCommand(client, "[SM] Usage: sm_say <message>");
   1.219 +		return Plugin_Handled;	
   1.220 +	}
   1.221 +	
   1.222 +	decl String:text[192];
   1.223 +	GetCmdArgString(text, sizeof(text));
   1.224 +
   1.225 +	decl String:name[64];
   1.226 +	GetClientName(client, name, sizeof(name));
   1.227 +			
   1.228 +	SendChatToAll(name, text);
   1.229 +	LogMessage("%L triggered sm_say (text %s)", client, text);
   1.230 +	
   1.231 +	return Plugin_Handled;		
   1.232 +}
   1.233 +
   1.234 +public Action:Command_SmCsay(client, args)
   1.235 +{
   1.236 +	if (args < 1)
   1.237 +	{
   1.238 +		ReplyToCommand(client, "[SM] Usage: sm_csay <message>");
   1.239 +		return Plugin_Handled;	
   1.240 +	}
   1.241 +	
   1.242 +	decl String:text[192];
   1.243 +	GetCmdArgString(text, sizeof(text));
   1.244 +
   1.245 +	new String:name[64];
   1.246 +	GetClientName(client, name, sizeof(name));
   1.247 +	
   1.248 +	PrintCenterTextAll("%s: %s", name, text);
   1.249 +	LogMessage("%L triggered sm_csay (text %s)", client, text);
   1.250 +	
   1.251 +	return Plugin_Handled;		
   1.252 +}
   1.253 +
   1.254 +public Action:Command_SmHsay(client, args)
   1.255 +{
   1.256 +	if (args < 1)
   1.257 +	{
   1.258 +		ReplyToCommand(client, "[SM] Usage: sm_hsay <message>");
   1.259 +		return Plugin_Handled;  
   1.260 +	}
   1.261 +    
   1.262 +	decl String:text[192];
   1.263 +	GetCmdArgString(text, sizeof(text));
   1.264 + 
   1.265 +	decl String:name[64];
   1.266 +	GetClientName(client, name, sizeof(name));
   1.267 +    
   1.268 +	SendHintToAll("%s: %s", name, text);
   1.269 +	LogMessage("%L triggered sm_hsay (text %s)", client, text);
   1.270 +    
   1.271 +	return Plugin_Handled;    
   1.272 +}
   1.273 +
   1.274 +public Action:Command_SmTsay(client, args)
   1.275 +{
   1.276 +	if (args < 1)
   1.277 +	{
   1.278 +		ReplyToCommand(client, "[SM] Usage: sm_tsay <message>");
   1.279 +		return Plugin_Handled;  
   1.280 +	}
   1.281 +    
   1.282 +	decl String:text[192], String:colorStr[16];
   1.283 +	GetCmdArgString(text, sizeof(text));
   1.284 +	
   1.285 +	new len = BreakString(text, colorStr, 16);
   1.286 + 
   1.287 +	decl String:name[64];
   1.288 +	GetClientName(client, name, sizeof(name));
   1.289 +		
   1.290 +	new color = FindColor(colorStr);
   1.291 +
   1.292 +	if (color == -1)    
   1.293 +	    SendDialogToAll(_, "%s: %s", name, text);
   1.294 +	else
   1.295 +		SendDialogToAll(color, "%s: %s", name, text[len]);
   1.296 +
   1.297 +	LogMessage("%L triggered sm_tsay (text %s)", client, text);
   1.298 +    
   1.299 +	return Plugin_Handled;    
   1.300 +}
   1.301 +
   1.302 +public Action:Command_SmChat(client, args)
   1.303 +{
   1.304 +	if (args < 1)
   1.305 +	{
   1.306 +		ReplyToCommand(client, "[SM] Usage: sm_chat <message>");
   1.307 +		return Plugin_Handled;	
   1.308 +	}	
   1.309 +	
   1.310 +	decl String:text[192];
   1.311 +	GetCmdArgString(text, sizeof(text));
   1.312 +
   1.313 +	decl String:name[64];
   1.314 +	GetClientName(client, name, sizeof(name));
   1.315 +	
   1.316 +	SendChatToAdmins(name, text);
   1.317 +	LogMessage("%L triggered sm_chat (text %s)", client, text);
   1.318 +	
   1.319 +	return Plugin_Handled;	
   1.320 +}
   1.321 +
   1.322 +public Action:Command_SmPsay(client, args)
   1.323 +{
   1.324 +	if (args < 2)
   1.325 +	{
   1.326 +		ReplyToCommand(client, "[SM] Usage: sm_psay <name or #userid> <message>");
   1.327 +		return Plugin_Handled;	
   1.328 +	}	
   1.329 +	
   1.330 +	decl String:text[192];
   1.331 +	GetCmdArgString(text, sizeof(text));
   1.332 +	
   1.333 +	decl String:target[64], String:message[192];
   1.334 +
   1.335 +	new len = BreakString(text, target, sizeof(target));
   1.336 +	BreakString(text[len], message, sizeof(message));
   1.337 +	
   1.338 +	new clients[2];
   1.339 +	new numClients = SearchForClients(target, clients, 2);
   1.340 +	
   1.341 +	if (numClients == 0)
   1.342 +	{
   1.343 +		ReplyToCommand(client, "[SM] %t", "No matching client");
   1.344 +		return Plugin_Handled;
   1.345 +	}
   1.346 +	else if (numClients > 1)
   1.347 +	{
   1.348 +		ReplyToCommand(client, "[SM] %t", "More than one client matches", target);
   1.349 +		return Plugin_Handled;
   1.350 +	}
   1.351 +	else if (!CanUserTarget(client, clients[0]))
   1.352 +	{
   1.353 +		ReplyToCommand(client, "[SM] %t", "Unable to target");
   1.354 +		return Plugin_Handled;
   1.355 +	}
   1.356 +	
   1.357 +	decl String:name[64], String:name2[64];
   1.358 +	GetClientName(client, name, sizeof(name));
   1.359 +	GetClientName(clients[0], name2, sizeof(name2));
   1.360 +
   1.361 +	if (g_DoColor)
   1.362 +	{
   1.363 +		PrintToChat(client, "\x04(Private: %s) %s: \x01%s", name2, name, message);
   1.364 +		PrintToChat(clients[0], "\x04(Private: %s) %s: \x01%s", name2, name, message);
   1.365 +	}
   1.366 +	else
   1.367 +	{
   1.368 +		PrintToChat(client, "(Private: %s) %s: %s", name2, name, message);
   1.369 +		PrintToChat(clients[0], "(Private: %s) %s: %s", name2, name, message);		
   1.370 +	}
   1.371 +
   1.372 +	LogMessage("%L triggered sm_psay to %L (text %s)", client, clients[0], message);
   1.373 +	
   1.374 +	return Plugin_Handled;	
   1.375 +}
   1.376 +
   1.377 +public Action:Command_SmMsay(client, args)
   1.378 +{
   1.379 +	if (args < 1)
   1.380 +	{
   1.381 +		ReplyToCommand(client, "[SM] Usage: sm_msay <message>");
   1.382 +		return Plugin_Handled;	
   1.383 +	}
   1.384 +	
   1.385 +	decl String:text[192];
   1.386 +	GetCmdArgString(text, sizeof(text));
   1.387 +
   1.388 +	decl String:name[64];
   1.389 +	GetClientName(client, name, sizeof(name));
   1.390 +		
   1.391 +	SendPanelToAll(name, text);
   1.392 +
   1.393 +	LogMessage("%L triggered sm_msay (text %s)", client, text);
   1.394 +	
   1.395 +	return Plugin_Handled;		
   1.396 +}
   1.397 +
   1.398 +bool:CheckAdminForChat(client)
   1.399 +{
   1.400 +	new AdminId:aid = GetUserAdmin(client);
   1.401 +	
   1.402 +	if (aid == INVALID_ADMIN_ID)
   1.403 +	{
   1.404 +		return false;			
   1.405 +	}
   1.406 +	
   1.407 +	return GetAdminFlag(aid, Admin_Chat, Access_Effective);
   1.408 +}
   1.409 +
   1.410 +FindColor(String:color[])
   1.411 +{
   1.412 +	for (new i = 0; i < 13; i++)
   1.413 +	{
   1.414 +		if (strcmp(color, g_ColorNames[i], false) == 0)
   1.415 +			return i;
   1.416 +	}
   1.417 +	
   1.418 +	return -1;
   1.419 +}
   1.420 +
   1.421 +SendChatToAll(String:name[], String:message[])
   1.422 +{
   1.423 +	if (g_DoColor)
   1.424 +	{
   1.425 +		PrintToChatAll("\x04(ALL) %s: \x01%s", name, message);
   1.426 +	}
   1.427 +	else
   1.428 +	{
   1.429 +		PrintToChatAll("(ALL) %s: %s", name, message);
   1.430 +	}
   1.431 +}
   1.432 +
   1.433 +SendChatToAdmins(String:name[], String:message[])
   1.434 +{
   1.435 +	new iMaxClients = GetMaxClients();
   1.436 +	
   1.437 +	for (new i = 1; i <= iMaxClients; i++)
   1.438 +	{
   1.439 +		if (IsClientInGame(i))
   1.440 +		{
   1.441 +			if (CheckAdminForChat(i))
   1.442 +			{
   1.443 +				if (g_DoColor)
   1.444 +				{
   1.445 +					PrintToChat(i, "\x04(ADMINS) %s: \x01%s", name, message);
   1.446 +				}
   1.447 +				else
   1.448 +				{
   1.449 +					PrintToChat(i, "(ADMINS) %s: %s", name, message);
   1.450 +				}
   1.451 +			}
   1.452 +		}	
   1.453 +	}
   1.454 +}
   1.455 +
   1.456 +SendHintToAll(String:text[], any:...)
   1.457 +{
   1.458 +	new String:message[192];
   1.459 +	VFormat(message, 191, text, 2);
   1.460 + 
   1.461 +	new iLen = strlen(message);
   1.462 +    
   1.463 +	if (iLen > 30)
   1.464 +	{
   1.465 +		new iLastAdded = 0;
   1.466 + 
   1.467 +		for (new i = 0; i < iLen; i++)
   1.468 +		{
   1.469 +			if((message[i] == ' ' && iLastAdded > 30 && (iLen - i) > 10) || ((GetNextSpaceCount(text, i + 1) + iLastAdded)  > 34))
   1.470 +			{
   1.471 +				message[i] = '\n';
   1.472 +				iLastAdded = 0;
   1.473 +			}
   1.474 +			else
   1.475 +				iLastAdded++;
   1.476 +		}
   1.477 +	}
   1.478 + 
   1.479 +	new Handle:hHintMessage = StartMessageAll("HintText");
   1.480 +	BfWriteByte(hHintMessage, -1);
   1.481 +	BfWriteString(hHintMessage, message);
   1.482 +	EndMessage();
   1.483 +}
   1.484 + 
   1.485 +GetNextSpaceCount(String:text[], iCurIndex)
   1.486 +{
   1.487 +	new iCount = 0;
   1.488 +	new iLen = strlen(text);
   1.489 +	
   1.490 +	for (new i = iCurIndex; i < iLen; i++)
   1.491 +	{
   1.492 +		if (text[i] == ' ')
   1.493 +			return iCount;
   1.494 +		else
   1.495 +			iCount++;
   1.496 +	}
   1.497 + 
   1.498 +	return iCount;
   1.499 +} 
   1.500 +
   1.501 +SendDialogToAll(color = 0, String:text[], any:...)
   1.502 +{
   1.503 +	new String:message[100];
   1.504 +	VFormat(message, sizeof(message), text, 3);	
   1.505 +	
   1.506 +	new MaxClients = GetMaxClients();
   1.507 +	for(new i = 1; i < MaxClients; i++)
   1.508 +	{
   1.509 +		if(IsClientInGame(i))
   1.510 +		{
   1.511 +			new Handle:kv = CreateKeyValues("Stuff", "title", message);
   1.512 +			KvSetColor(kv, "color", g_Colors[color][0], g_Colors[color][1], g_Colors[color][2], 255);
   1.513 +			KvSetNum(kv, "level", 1);
   1.514 +			KvSetNum(kv, "time", 10);
   1.515 +			
   1.516 +			CreateDialog(i, kv, DialogType_Msg);
   1.517 +			
   1.518 +			CloseHandle(kv);
   1.519 +		}
   1.520 +	}	
   1.521 +}
   1.522 +
   1.523 +SendPanelToAll(String:name[], String:message[])
   1.524 +{
   1.525 +	decl String:title[100];
   1.526 +	Format(title, 64, "%s:", name);
   1.527 +	
   1.528 +	ReplaceString(message, 192, "\\n", "\n");
   1.529 +	
   1.530 +	new Handle:mSayPanel = CreatePanel();
   1.531 +	SetPanelTitle(mSayPanel, title);
   1.532 +	DrawPanelItem(mSayPanel, "", ITEMDRAW_SPACER);
   1.533 +	DrawPanelText(mSayPanel, message);
   1.534 +	DrawPanelItem(mSayPanel, "", ITEMDRAW_SPACER);
   1.535 +
   1.536 +	SetPanelCurrentKey(mSayPanel, 10);
   1.537 +	DrawPanelItem(mSayPanel, "Exit", ITEMDRAW_CONTROL);
   1.538 +
   1.539 +	new MaxClients = GetMaxClients();
   1.540 +	for(new i = 1; i < MaxClients; i++)
   1.541 +	{
   1.542 +		if(IsClientInGame(i) && !IsFakeClient(i))
   1.543 +		{
   1.544 +			SendPanelToClient(mSayPanel, i, Handler_DoNothing, 10);
   1.545 +		}
   1.546 +	}
   1.547 +
   1.548 +	CloseHandle(mSayPanel);
   1.549 +}
   1.550 +
   1.551 +public Handler_DoNothing(Handle:menu, MenuAction:action, param1, param2)
   1.552 +{
   1.553 +	/* Do nothing */
   1.554 +}
   1.555 \ No newline at end of file