plugins/basechat.sp
author Michael McKoy <ferret99gt@gmail.com>
Fri Nov 09 23:46:05 2007 +0000 (2007-11-09)
changeset 1664 e054be79e28a
parent 1663 a6b8326686af
child 1706 122b0601a01e
permissions -rw-r--r--
New adt_array functions: CloneArray, FindStringInArray, FindCellInArray
Adjusted mapchooser/randomcycle/rockthevote and LoadMaps stock to use new array functions
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basic Chat Plugin
     5  * Implements basic communication commands.
     6  *
     7  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     8  * =============================================================================
     9  *
    10  * This program is free software; you can redistribute it and/or modify it under
    11  * the terms of the GNU General Public License, version 3.0, as published by the
    12  * Free Software Foundation.
    13  * 
    14  * This program is distributed in the hope that it will be useful, but WITHOUT
    15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    17  * details.
    18  *
    19  * You should have received a copy of the GNU General Public License along with
    20  * this program.  If not, see <http://www.gnu.org/licenses/>.
    21  *
    22  * As a special exception, AlliedModders LLC gives you permission to link the
    23  * code of this program (as well as its derivative works) to "Half-Life 2," the
    24  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    25  * by the Valve Corporation.  You must obey the GNU General Public License in
    26  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    27  * this exception to all derivative works.  AlliedModders LLC defines further
    28  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    29  * or <http://www.sourcemod.net/license.php>.
    30  *
    31  * Version: $Id$
    32  */
    33 
    34 #pragma semicolon 1
    35 
    36 #include <sourcemod>
    37 
    38 public Plugin:myinfo = 
    39 {
    40 	name = "Basic Chat",
    41 	author = "AlliedModders LLC",
    42 	description = "Basic Communication Commands",
    43 	version = SOURCEMOD_VERSION,
    44 	url = "http://www.sourcemod.net/"
    45 };
    46 
    47 #define CHAT_SYMBOL '@'
    48 
    49 new String:g_ColorNames[13][10] = {"White", "Red", "Green", "Blue", "Yellow", "Purple", "Cyan", "Orange", "Pink", "Olive", "Lime", "Violet", "Lightblue"};
    50 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}};
    51 
    52 new Handle:g_Cvar_Chatmode = INVALID_HANDLE;
    53 new Handle:g_Cvar_Psaymode = INVALID_HANDLE;
    54 
    55 new bool:g_DoColor = true;
    56 
    57 public OnPluginStart()
    58 {
    59 	LoadTranslations("common.phrases");
    60 
    61 	g_Cvar_Chatmode = CreateConVar("sm_chat_mode", "1", "Allows player's to send messages to admin chat.", 0, true, 0.0, true, 1.0);
    62 	g_Cvar_Psaymode = CreateConVar("sm_psay_mode", "0", "Allows player's to use psay 'say @@' alias.", 0, true, 0.0, true, 1.0);
    63 
    64 	RegConsoleCmd("say", Command_SayChat);
    65 	RegConsoleCmd("say_team", Command_SayAdmin);		
    66 	RegAdminCmd("sm_say", Command_SmSay, ADMFLAG_CHAT, "sm_say <message> - sends message to all players");
    67 	RegAdminCmd("sm_csay", Command_SmCsay, ADMFLAG_CHAT, "sm_csay <message> - sends centered message to all players");
    68 	RegAdminCmd("sm_hsay", Command_SmHsay, ADMFLAG_CHAT, "sm_hsay <message> - sends hint message to all players");
    69 	RegAdminCmd("sm_tsay", Command_SmTsay, ADMFLAG_CHAT, "sm_tsay [color] <message> - sends top-left message to all players");
    70 	RegAdminCmd("sm_chat", Command_SmChat, ADMFLAG_CHAT, "sm_chat <message> - sends message to admins");
    71 	RegAdminCmd("sm_psay", Command_SmPsay, ADMFLAG_CHAT, "sm_psay <name or #userid> <message> - sends private message");
    72 	RegAdminCmd("sm_msay", Command_SmMsay, ADMFLAG_CHAT, "sm_msay <message> - sends message as a menu panel");
    73 	
    74 	decl String:modname[64];
    75 	GetGameFolderName(modname, sizeof(modname));
    76 	
    77 	if (strcmp(modname, "hl2mp") == 0)
    78 	{
    79 		g_DoColor = false;
    80 	}
    81 }
    82 
    83 public Action:Command_SayChat(client, args)
    84 {	
    85 	decl String:text[192];
    86 	if (IsChatTrigger() || GetCmdArgString(text, sizeof(text)) < 1)
    87 	{
    88 		return Plugin_Continue;
    89 	}
    90 	
    91 	new startidx;
    92 	if (text[strlen(text)-1] == '"')
    93 	{
    94 		text[strlen(text)-1] = '\0';
    95 		startidx = 1;
    96 	}
    97 	
    98 	if (text[startidx] != CHAT_SYMBOL)
    99 		return Plugin_Continue;
   100 	
   101 	new msgStart = 1;
   102 	
   103 	if (text[startidx+1] == CHAT_SYMBOL)
   104 	{
   105 		msgStart = 2;
   106 		
   107 		if (text[startidx+2] == CHAT_SYMBOL)
   108 			msgStart = 3;
   109 	}
   110 	
   111 	decl String:message[192];
   112 	strcopy(message, 192, text[startidx+msgStart]);
   113 	
   114 	decl String:name[64];
   115 	GetClientName(client, name, sizeof(name));
   116 	
   117 	if (msgStart == 1 && CheckAdminForChat(client)) // sm_say alias
   118 	{
   119 		SendChatToAll(name, message);
   120 		LogAction(client, -1, "%L triggered sm_say (text %s)", client, message);
   121 	}
   122 	else if (msgStart == 3 && CheckAdminForChat(client)) // sm_csay alias
   123 	{
   124 		PrintCenterTextAll("%s: %s", name, message);
   125 		LogAction(client, -1, "%L triggered sm_csay (text %s)", client, text);		
   126 	}	
   127 	else if (msgStart == 2 && (CheckAdminForChat(client) || GetConVarBool(g_Cvar_Psaymode))) // sm_psay alias
   128 	{
   129 		decl String:arg[64];
   130 	
   131 		new len = BreakString(message, arg, sizeof(arg));
   132 		new target = FindTarget(client, arg, true, false);
   133 		
   134 		if (target == -1)
   135 			return Plugin_Handled;
   136 		
   137 		decl String:name2[64];
   138 		GetClientName(target, name2, sizeof(name2));
   139 	
   140 		if (g_DoColor)
   141 		{
   142 			PrintToChat(client, "\x04(Private to %s) %s: \x01%s", name2, name, message[len]);
   143 			PrintToChat(target, "\x04(Private to %s) %s: \x01%s", name2, name, message[len]);
   144 		}
   145 		else
   146 		{
   147 			PrintToChat(client, "(Private to %s) %s: %s", name2, name, message[len]);
   148 			PrintToChat(target, "(Private to %s) %s: %s", name2, name, message[len]);			
   149 		}
   150 
   151 		LogAction(client, -1, "%L triggered sm_psay to %L (text %s)", client, target, message);		
   152 	}
   153 	else
   154 		return Plugin_Continue;
   155 	
   156 	return Plugin_Handled;	
   157 }
   158 
   159 public Action:Command_SayAdmin(client, args)
   160 {
   161 	if (!CheckAdminForChat(client) && !GetConVarBool(g_Cvar_Chatmode))
   162 	{
   163 		return Plugin_Continue;	
   164 	}
   165 	
   166 	decl String:text[192];
   167 	if (IsChatTrigger() || GetCmdArgString(text, sizeof(text)) < 1)
   168 	{
   169 		return Plugin_Continue;
   170 	}
   171 	
   172 	new startidx;
   173 	if (text[strlen(text)-1] == '"')
   174 	{
   175 		text[strlen(text)-1] = '\0';
   176 		startidx = 1;
   177 	}
   178 	
   179 	if (text[startidx] != CHAT_SYMBOL)
   180 		return Plugin_Continue;
   181 	
   182 	decl String:message[192];
   183 	strcopy(message, 192, text[startidx+1]);
   184 
   185 	decl String:name[64];
   186 	GetClientName(client, name, sizeof(name));
   187 	
   188 	SendChatToAdmins(name, message);
   189 	LogAction(client, -1, "%L triggered sm_chat (text %s)", client, message);
   190 	
   191 	return Plugin_Handled;	
   192 }
   193 
   194 public Action:Command_SmSay(client, args)
   195 {
   196 	if (args < 1)
   197 	{
   198 		ReplyToCommand(client, "[SM] Usage: sm_say <message>");
   199 		return Plugin_Handled;	
   200 	}
   201 	
   202 	decl String:text[192];
   203 	GetCmdArgString(text, sizeof(text));
   204 
   205 	decl String:name[64];
   206 	GetClientName(client, name, sizeof(name));
   207 			
   208 	SendChatToAll(name, text);
   209 	LogAction(client, -1, "%L triggered sm_say (text %s)", client, text);
   210 	
   211 	return Plugin_Handled;		
   212 }
   213 
   214 public Action:Command_SmCsay(client, args)
   215 {
   216 	if (args < 1)
   217 	{
   218 		ReplyToCommand(client, "[SM] Usage: sm_csay <message>");
   219 		return Plugin_Handled;	
   220 	}
   221 	
   222 	decl String:text[192];
   223 	GetCmdArgString(text, sizeof(text));
   224 
   225 	new String:name[64];
   226 	GetClientName(client, name, sizeof(name));
   227 	
   228 	PrintCenterTextAll("%s: %s", name, text);
   229 	LogAction(client, -1, "%L triggered sm_csay (text %s)", client, text);
   230 	
   231 	return Plugin_Handled;		
   232 }
   233 
   234 public Action:Command_SmHsay(client, args)
   235 {
   236 	if (args < 1)
   237 	{
   238 		ReplyToCommand(client, "[SM] Usage: sm_hsay <message>");
   239 		return Plugin_Handled;  
   240 	}
   241     
   242 	decl String:text[192];
   243 	GetCmdArgString(text, sizeof(text));
   244  
   245 	decl String:name[64];
   246 	GetClientName(client, name, sizeof(name));
   247     
   248 	PrintHintTextToAll("%s: %s", name, text);
   249 	LogAction(client, -1, "%L triggered sm_hsay (text %s)", client, text);
   250     
   251 	return Plugin_Handled;    
   252 }
   253 
   254 public Action:Command_SmTsay(client, args)
   255 {
   256 	if (args < 1)
   257 	{
   258 		ReplyToCommand(client, "[SM] Usage: sm_tsay <message>");
   259 		return Plugin_Handled;  
   260 	}
   261     
   262 	decl String:text[192], String:colorStr[16];
   263 	GetCmdArgString(text, sizeof(text));
   264 	
   265 	new len = BreakString(text, colorStr, 16);
   266  
   267 	decl String:name[64];
   268 	GetClientName(client, name, sizeof(name));
   269 		
   270 	new color = FindColor(colorStr);
   271 
   272 	if (color == -1)    
   273 		SendDialogToAll(_, "%s: %s", name, text);
   274 	else
   275 		SendDialogToAll(color, "%s: %s", name, text[len]);
   276 
   277 	LogAction(client, -1, "%L triggered sm_tsay (text %s)", client, text);
   278     
   279 	return Plugin_Handled;    
   280 }
   281 
   282 public Action:Command_SmChat(client, args)
   283 {
   284 	if (args < 1)
   285 	{
   286 		ReplyToCommand(client, "[SM] Usage: sm_chat <message>");
   287 		return Plugin_Handled;	
   288 	}	
   289 	
   290 	decl String:text[192];
   291 	GetCmdArgString(text, sizeof(text));
   292 
   293 	decl String:name[64];
   294 	GetClientName(client, name, sizeof(name));
   295 	
   296 	SendChatToAdmins(name, text);
   297 	LogAction(client, -1, "%L triggered sm_chat (text %s)", client, text);
   298 	
   299 	return Plugin_Handled;	
   300 }
   301 
   302 public Action:Command_SmPsay(client, args)
   303 {
   304 	if (args < 2)
   305 	{
   306 		ReplyToCommand(client, "[SM] Usage: sm_psay <name or #userid> <message>");
   307 		return Plugin_Handled;	
   308 	}	
   309 	
   310 	decl String:text[192], String:arg[64], String:message[192];
   311 	GetCmdArgString(text, sizeof(text));
   312 
   313 	new len = BreakString(text, arg, sizeof(arg));
   314 	BreakString(text[len], message, sizeof(message));
   315 	
   316 	new target = FindTarget(client, arg, true, false);
   317 		
   318 	if (target == -1)
   319 		return Plugin_Handled;	
   320 		
   321 	decl String:name[64], String:name2[64];
   322 	GetClientName(client, name, sizeof(name));
   323 	GetClientName(target, name2, sizeof(name2));
   324 
   325 	if (g_DoColor)
   326 	{
   327 		PrintToChat(client, "\x04(Private: %s) %s: \x01%s", name2, name, message);
   328 		PrintToChat(target, "\x04(Private: %s) %s: \x01%s", name2, name, message);
   329 	}
   330 	else
   331 	{
   332 		PrintToChat(client, "(Private: %s) %s: %s", name2, name, message);
   333 		PrintToChat(target, "(Private: %s) %s: %s", name2, name, message);		
   334 	}
   335 
   336 	LogAction(client, -1, "%L triggered sm_psay to %L (text %s)", client, target, message);
   337 	
   338 	return Plugin_Handled;	
   339 }
   340 
   341 public Action:Command_SmMsay(client, args)
   342 {
   343 	if (args < 1)
   344 	{
   345 		ReplyToCommand(client, "[SM] Usage: sm_msay <message>");
   346 		return Plugin_Handled;	
   347 	}
   348 	
   349 	decl String:text[192];
   350 	GetCmdArgString(text, sizeof(text));
   351 
   352 	decl String:name[64];
   353 	GetClientName(client, name, sizeof(name));
   354 		
   355 	SendPanelToAll(name, text);
   356 
   357 	LogAction(client, -1, "%L triggered sm_msay (text %s)", client, text);
   358 	
   359 	return Plugin_Handled;		
   360 }
   361 
   362 bool:CheckAdminForChat(client)
   363 {
   364 	if (client == 0)
   365 	{
   366 		return true;
   367 	}
   368 	
   369 	return CheckCommandAccess(client, "sm_chat", ADMFLAG_CHAT);
   370 }
   371 
   372 FindColor(String:color[])
   373 {
   374 	for (new i = 0; i < 13; i++)
   375 	{
   376 		if (strcmp(color, g_ColorNames[i], false) == 0)
   377 			return i;
   378 	}
   379 	
   380 	return -1;
   381 }
   382 
   383 SendChatToAll(String:name[], String:message[])
   384 {
   385 	if (g_DoColor)
   386 	{
   387 		PrintToChatAll("\x04(ALL) %s: \x01%s", name, message);
   388 	}
   389 	else
   390 	{
   391 		PrintToChatAll("(ALL) %s: %s", name, message);
   392 	}
   393 }
   394 
   395 SendChatToAdmins(String:name[], String:message[])
   396 {
   397 	new iMaxClients = GetMaxClients();
   398 	
   399 	for (new i = 1; i <= iMaxClients; i++)
   400 	{
   401 		if (IsClientInGame(i))
   402 		{
   403 			if (CheckAdminForChat(i))
   404 			{
   405 				if (g_DoColor)
   406 				{
   407 					PrintToChat(i, "\x04(ADMINS) %s: \x01%s", name, message);
   408 				}
   409 				else
   410 				{
   411 					PrintToChat(i, "(ADMINS) %s: %s", name, message);
   412 				}
   413 			}
   414 		}	
   415 	}
   416 }
   417 
   418 SendDialogToAll(color = 0, String:text[], any:...)
   419 {
   420 	new String:message[100];
   421 	VFormat(message, sizeof(message), text, 3);	
   422 	
   423 	new MaxClients = GetMaxClients();
   424 	for(new i = 1; i < MaxClients; i++)
   425 	{
   426 		if(IsClientInGame(i))
   427 		{
   428 			new Handle:kv = CreateKeyValues("Stuff", "title", message);
   429 			KvSetColor(kv, "color", g_Colors[color][0], g_Colors[color][1], g_Colors[color][2], 255);
   430 			KvSetNum(kv, "level", 1);
   431 			KvSetNum(kv, "time", 10);
   432 			
   433 			CreateDialog(i, kv, DialogType_Msg);
   434 			
   435 			CloseHandle(kv);
   436 		}
   437 	}	
   438 }
   439 
   440 SendPanelToAll(String:name[], String:message[])
   441 {
   442 	decl String:title[100];
   443 	Format(title, 64, "%s:", name);
   444 	
   445 	ReplaceString(message, 192, "\\n", "\n");
   446 	
   447 	new Handle:mSayPanel = CreatePanel();
   448 	SetPanelTitle(mSayPanel, title);
   449 	DrawPanelItem(mSayPanel, "", ITEMDRAW_SPACER);
   450 	DrawPanelText(mSayPanel, message);
   451 	DrawPanelItem(mSayPanel, "", ITEMDRAW_SPACER);
   452 
   453 	SetPanelCurrentKey(mSayPanel, 10);
   454 	DrawPanelItem(mSayPanel, "Exit", ITEMDRAW_CONTROL);
   455 
   456 	new MaxClients = GetMaxClients();
   457 	for(new i = 1; i < MaxClients; i++)
   458 	{
   459 		if(IsClientInGame(i) && !IsFakeClient(i))
   460 		{
   461 			SendPanelToClient(mSayPanel, i, Handler_DoNothing, 10);
   462 		}
   463 	}
   464 
   465 	CloseHandle(mSayPanel);
   466 }
   467 
   468 public Handler_DoNothing(Handle:menu, MenuAction:action, param1, param2)
   469 {
   470 	/* Do nothing */
   471 }