plugins/basechat.sp
author David Anderson <dvander@alliedmods.net>
Fri Oct 31 22:47:13 2008 -0700 (2008-10-31)
changeset 2385 2265a1fd35c9
parent 2382 4d49a5e1feb7
child 2434 3d7b3896cf50
permissions -rw-r--r--
Fixed gamedata file refcounting bug in the structs extension.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod Basic Chat Plugin
     5  * Implements basic communication commands.
     6  *
     7  * SourceMod (C)2004-2008 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 
    54 new bool:g_DoColor = true;
    55 
    56 public OnPluginStart()
    57 {
    58 	LoadTranslations("common.phrases");
    59 
    60 	g_Cvar_Chatmode = CreateConVar("sm_chat_mode", "1", "Allows player's to send messages to admin chat.", 0, true, 0.0, true, 1.0);
    61 
    62 	RegConsoleCmd("say", Command_SayChat);
    63 	RegConsoleCmd("say_team", Command_SayAdmin);		
    64 	RegAdminCmd("sm_say", Command_SmSay, ADMFLAG_CHAT, "sm_say <message> - sends message to all players");
    65 	RegAdminCmd("sm_csay", Command_SmCsay, ADMFLAG_CHAT, "sm_csay <message> - sends centered message to all players");
    66 	RegAdminCmd("sm_hsay", Command_SmHsay, ADMFLAG_CHAT, "sm_hsay <message> - sends hint message to all players");
    67 	RegAdminCmd("sm_tsay", Command_SmTsay, ADMFLAG_CHAT, "sm_tsay [color] <message> - sends top-left message to all players");
    68 	RegAdminCmd("sm_chat", Command_SmChat, ADMFLAG_CHAT, "sm_chat <message> - sends message to admins");
    69 	RegAdminCmd("sm_psay", Command_SmPsay, ADMFLAG_CHAT, "sm_psay <name or #userid> <message> - sends private message");
    70 	RegAdminCmd("sm_msay", Command_SmMsay, ADMFLAG_CHAT, "sm_msay <message> - sends message as a menu panel");
    71 	
    72 	decl String:modname[64];
    73 	GetGameFolderName(modname, sizeof(modname));
    74 	
    75 	if (strcmp(modname, "hl2mp") == 0)
    76 	{
    77 		g_DoColor = false;
    78 	}
    79 }
    80 
    81 public Action:Command_SayChat(client, args)
    82 {	
    83 	decl String:text[192];
    84 	if (IsChatTrigger() || GetCmdArgString(text, sizeof(text)) < 1)
    85 	{
    86 		return Plugin_Continue;
    87 	}
    88 	
    89 	new startidx;
    90 	if (text[strlen(text)-1] == '"')
    91 	{
    92 		text[strlen(text)-1] = '\0';
    93 		startidx = 1;
    94 	}
    95 	
    96 	if (text[startidx] != CHAT_SYMBOL)
    97 		return Plugin_Continue;
    98 	
    99 	new msgStart = 1;
   100 	
   101 	if (text[startidx+1] == CHAT_SYMBOL)
   102 	{
   103 		msgStart = 2;
   104 		
   105 		if (text[startidx+2] == CHAT_SYMBOL)
   106 			msgStart = 3;
   107 	}
   108 	
   109 	decl String:message[192];
   110 	strcopy(message, 192, text[startidx+msgStart]);
   111 	
   112 	decl String:name[64];
   113 	GetClientName(client, name, sizeof(name));
   114 	
   115 	if (msgStart == 1 && CheckCommandAccess(client, "sm_say", ADMFLAG_CHAT)) // sm_say alias
   116 	{
   117 		SendChatToAll(client, message);
   118 		LogAction(client, -1, "%L triggered sm_say (text %s)", client, message);
   119 	}
   120 	else if (msgStart == 3 && CheckCommandAccess(client, "sm_csay", ADMFLAG_CHAT)) // sm_csay alias
   121 	{
   122 		DisplayCenterTextToAll(client, message);
   123 		LogAction(client, -1, "%L triggered sm_csay (text %s)", client, text);		
   124 	}	
   125 	else if (msgStart == 2 && CheckCommandAccess(client, "sm_psay", ADMFLAG_CHAT)) // sm_psay alias
   126 	{
   127 		decl String:arg[64];
   128 	
   129 		new len = BreakString(message, arg, sizeof(arg));
   130 		new target = FindTarget(client, arg, true, false);
   131 		
   132 		if (target == -1 || len == -1)
   133 			return Plugin_Handled;
   134 		
   135 		decl String:name2[64];
   136 		GetClientName(target, name2, sizeof(name2));
   137 	
   138 		if (g_DoColor)
   139 		{
   140 			PrintToChat(client, "\x04(Private to %s) %s: \x01%s", name2, name, message[len]);
   141 			PrintToChat(target, "\x04(Private to %s) %s: \x01%s", name2, name, message[len]);
   142 		}
   143 		else
   144 		{
   145 			PrintToChat(client, "(Private to %s) %s: %s", name2, name, message[len]);
   146 			PrintToChat(target, "(Private to %s) %s: %s", name2, name, message[len]);			
   147 		}
   148 
   149 		LogAction(client, -1, "%L triggered sm_psay to %L (text %s)", client, target, message);		
   150 	}
   151 	else
   152 		return Plugin_Continue;
   153 	
   154 	return Plugin_Handled;	
   155 }
   156 
   157 public Action:Command_SayAdmin(client, args)
   158 {
   159 	if (!CheckCommandAccess(client, "sm_chat", ADMFLAG_CHAT) && !GetConVarBool(g_Cvar_Chatmode))
   160 	{
   161 		return Plugin_Continue;	
   162 	}
   163 	
   164 	decl String:text[192];
   165 	if (IsChatTrigger() || GetCmdArgString(text, sizeof(text)) < 1)
   166 	{
   167 		return Plugin_Continue;
   168 	}
   169 	
   170 	new startidx;
   171 	if (text[strlen(text)-1] == '"')
   172 	{
   173 		text[strlen(text)-1] = '\0';
   174 		startidx = 1;
   175 	}
   176 	
   177 	if (text[startidx] != CHAT_SYMBOL)
   178 		return Plugin_Continue;
   179 	
   180 	decl String:message[192];
   181 	strcopy(message, 192, text[startidx+1]);
   182 
   183 	decl String:name[64];
   184 	GetClientName(client, name, sizeof(name));
   185 	
   186 	SendChatToAdmins(name, message);
   187 	LogAction(client, -1, "%L triggered sm_chat (text %s)", client, message);
   188 	
   189 	return Plugin_Handled;	
   190 }
   191 
   192 public Action:Command_SmSay(client, args)
   193 {
   194 	if (args < 1)
   195 	{
   196 		ReplyToCommand(client, "[SM] Usage: sm_say <message>");
   197 		return Plugin_Handled;	
   198 	}
   199 	
   200 	decl String:text[192];
   201 	GetCmdArgString(text, sizeof(text));
   202 
   203 	SendChatToAll(client, text);
   204 	LogAction(client, -1, "%L triggered sm_say (text %s)", client, text);
   205 	
   206 	return Plugin_Handled;		
   207 }
   208 
   209 public Action:Command_SmCsay(client, args)
   210 {
   211 	if (args < 1)
   212 	{
   213 		ReplyToCommand(client, "[SM] Usage: sm_csay <message>");
   214 		return Plugin_Handled;	
   215 	}
   216 	
   217 	decl String:text[192];
   218 	GetCmdArgString(text, sizeof(text));
   219 	
   220 	DisplayCenterTextToAll(client, text);
   221 	
   222 	LogAction(client, -1, "%L triggered sm_csay (text %s)", client, text);
   223 	
   224 	return Plugin_Handled;		
   225 }
   226 
   227 public Action:Command_SmHsay(client, args)
   228 {
   229 	if (args < 1)
   230 	{
   231 		ReplyToCommand(client, "[SM] Usage: sm_hsay <message>");
   232 		return Plugin_Handled;  
   233 	}
   234 	
   235 	decl String:text[192];
   236 	GetCmdArgString(text, sizeof(text));
   237  
   238 	decl String:nameBuf[MAX_NAME_LENGTH];
   239 	
   240 	for (new i = 1; i <= MaxClients; i++)
   241 	{
   242 		if (!IsClientConnected(i) || IsFakeClient(i))
   243 		{
   244 			continue;
   245 		}
   246 		FormatActivitySource(client, i, nameBuf, sizeof(nameBuf));
   247 		PrintHintText(i, "%s: %s", nameBuf, text);
   248 	}
   249 	
   250 	LogAction(client, -1, "%L triggered sm_hsay (text %s)", client, text);
   251 	
   252 	return Plugin_Handled;	
   253 }
   254 
   255 public Action:Command_SmTsay(client, args)
   256 {
   257 	if (args < 1)
   258 	{
   259 		ReplyToCommand(client, "[SM] Usage: sm_tsay <message>");
   260 		return Plugin_Handled;  
   261 	}
   262 	
   263 	decl String:text[192], String:colorStr[16];
   264 	GetCmdArgString(text, sizeof(text));
   265 	
   266 	new len = BreakString(text, colorStr, 16);
   267  
   268 	decl String:name[64];
   269 	GetClientName(client, name, sizeof(name));
   270 		
   271 	new color = FindColor(colorStr);
   272 	new String:nameBuf[MAX_NAME_LENGTH];
   273 	
   274 	if (color == -1)
   275 	{
   276 		color = 0;
   277 		len = 0;
   278 	}
   279 	
   280 	for (new i = 1; i <= MaxClients; i++)
   281 	{
   282 		if (!IsClientConnected(i) || IsFakeClient(i))
   283 		{
   284 			continue;
   285 		}
   286 		FormatActivitySource(client, i, nameBuf, sizeof(nameBuf));
   287 		SendDialogToOne(i, color, "%s: %s", nameBuf, text[len]);
   288 	}
   289 
   290 	LogAction(client, -1, "%L triggered sm_tsay (text %s)", client, text);
   291 	
   292 	return Plugin_Handled;	
   293 }
   294 
   295 public Action:Command_SmChat(client, args)
   296 {
   297 	if (args < 1)
   298 	{
   299 		ReplyToCommand(client, "[SM] Usage: sm_chat <message>");
   300 		return Plugin_Handled;	
   301 	}	
   302 	
   303 	decl String:text[192];
   304 	GetCmdArgString(text, sizeof(text));
   305 
   306 	decl String:name[64];
   307 	GetClientName(client, name, sizeof(name));
   308 	
   309 	SendChatToAdmins(name, text);
   310 	LogAction(client, -1, "%L triggered sm_chat (text %s)", client, text);
   311 	
   312 	return Plugin_Handled;	
   313 }
   314 
   315 public Action:Command_SmPsay(client, args)
   316 {
   317 	if (args < 2)
   318 	{
   319 		ReplyToCommand(client, "[SM] Usage: sm_psay <name or #userid> <message>");
   320 		return Plugin_Handled;	
   321 	}	
   322 	
   323 	decl String:text[192], String:arg[64], String:message[192];
   324 	GetCmdArgString(text, sizeof(text));
   325 
   326 	new len = BreakString(text, arg, sizeof(arg));
   327 	BreakString(text[len], message, sizeof(message));
   328 	
   329 	new target = FindTarget(client, arg, true, false);
   330 		
   331 	if (target == -1)
   332 		return Plugin_Handled;	
   333 		
   334 	decl String:name[64], String:name2[64];
   335 
   336 	if (client == 0)
   337 	{
   338 		name = "Console";
   339 	}
   340 	else
   341 	{
   342 		GetClientName(client, name, sizeof(name));
   343 	}
   344 
   345 	GetClientName(target, name2, sizeof(name2));
   346 
   347 	if (client == 0)
   348 	{
   349 		PrintToServer("(Private: %s) %s: %s", name2, name, message);
   350 	}
   351 	else if (g_DoColor)
   352 	{
   353 		PrintToChat(client, "\x04(Private: %s) %s: \x01%s", name2, name, message);
   354 	}
   355 	else
   356 	{
   357 		PrintToChat(client, "(Private: %s) %s: %s", name2, name, message);
   358 	}
   359 
   360 	if (g_DoColor)
   361 	{
   362 		PrintToChat(target, "\x04(Private: %s) %s: \x01%s", name2, name, message);
   363 	}
   364 	else
   365 	{
   366 		PrintToChat(target, "(Private: %s) %s: %s", name2, name, message);		
   367 	}
   368 
   369 	LogAction(client, -1, "%L triggered sm_psay to %L (text %s)", client, target, message);
   370 	
   371 	return Plugin_Handled;	
   372 }
   373 
   374 public Action:Command_SmMsay(client, args)
   375 {
   376 	if (args < 1)
   377 	{
   378 		ReplyToCommand(client, "[SM] Usage: sm_msay <message>");
   379 		return Plugin_Handled;	
   380 	}
   381 	
   382 	decl String:text[192];
   383 	GetCmdArgString(text, sizeof(text));
   384 
   385 	decl String:name[64];
   386 	GetClientName(client, name, sizeof(name));
   387 		
   388 	SendPanelToAll(name, text);
   389 
   390 	LogAction(client, -1, "%L triggered sm_msay (text %s)", client, text);
   391 	
   392 	return Plugin_Handled;		
   393 }
   394 
   395 FindColor(String:color[])
   396 {
   397 	for (new i = 0; i < 13; i++)
   398 	{
   399 		if (strcmp(color, g_ColorNames[i], false) == 0)
   400 			return i;
   401 	}
   402 	
   403 	return -1;
   404 }
   405 
   406 SendChatToAll(client, String:message[])
   407 {
   408 	new String:nameBuf[MAX_NAME_LENGTH];
   409 	
   410 	for (new i = 1; i <= MaxClients; i++)
   411 	{
   412 		if (!IsClientInGame(i) || IsFakeClient(i))
   413 		{
   414 			continue;
   415 		}
   416 		FormatActivitySource(client, i, nameBuf, sizeof(nameBuf));
   417 		
   418 		if (g_DoColor)
   419 		{
   420 			PrintToChat(i, "\x04(ALL) %s: \x01%s", nameBuf, message);
   421 		}
   422 		else
   423 		{
   424 			PrintToChat(i, "%s: %s", nameBuf, message);
   425 		}
   426 	}
   427 }
   428 
   429 DisplayCenterTextToAll(client, String:message[])
   430 {
   431 	new String:nameBuf[MAX_NAME_LENGTH];
   432 	
   433 	for (new i = 1; i < MaxClients; i++)
   434 	{
   435 		if (!IsClientInGame(i) || IsFakeClient(i))
   436 		{
   437 			continue;
   438 		}
   439 		FormatActivitySource(client, i, nameBuf, sizeof(nameBuf));
   440 		PrintCenterText(i, "%s: %s", nameBuf, message);
   441 	}
   442 }
   443 
   444 SendChatToAdmins(String:name[], String:message[])
   445 {
   446 	for (new i = 1; i <= MaxClients; i++)
   447 	{
   448 		if (IsClientInGame(i))
   449 		{
   450 			if (CheckCommandAccess(i, "sm_chat", ADMFLAG_CHAT))
   451 			{
   452 				if (g_DoColor)
   453 				{
   454 					PrintToChat(i, "\x04(ADMINS) %s: \x01%s", name, message);
   455 				}
   456 				else
   457 				{
   458 					PrintToChat(i, "(ADMINS) %s: %s", name, message);
   459 				}
   460 			}
   461 		}	
   462 	}
   463 }
   464 
   465 SendDialogToOne(client, color, String:text[], any:...)
   466 {
   467 	new String:message[100];
   468 	VFormat(message, sizeof(message), text, 4);	
   469 	
   470 	new Handle:kv = CreateKeyValues("Stuff", "title", message);
   471 	KvSetColor(kv, "color", g_Colors[color][0], g_Colors[color][1], g_Colors[color][2], 255);
   472 	KvSetNum(kv, "level", 1);
   473 	KvSetNum(kv, "time", 10);
   474 	
   475 	CreateDialog(client, kv, DialogType_Msg);
   476 	
   477 	CloseHandle(kv);	
   478 }
   479 
   480 SendPanelToAll(String:name[], String:message[])
   481 {
   482 	decl String:title[100];
   483 	Format(title, 64, "%s:", name);
   484 	
   485 	ReplaceString(message, 192, "\\n", "\n");
   486 	
   487 	new Handle:mSayPanel = CreatePanel();
   488 	SetPanelTitle(mSayPanel, title);
   489 	DrawPanelItem(mSayPanel, "", ITEMDRAW_SPACER);
   490 	DrawPanelText(mSayPanel, message);
   491 	DrawPanelItem(mSayPanel, "", ITEMDRAW_SPACER);
   492 
   493 	SetPanelCurrentKey(mSayPanel, 10);
   494 	DrawPanelItem(mSayPanel, "Exit", ITEMDRAW_CONTROL);
   495 
   496 	for(new i = 1; i <= MaxClients; i++)
   497 	{
   498 		if(IsClientInGame(i) && !IsFakeClient(i))
   499 		{
   500 			SendPanelToClient(mSayPanel, i, Handler_DoNothing, 10);
   501 		}
   502 	}
   503 
   504 	CloseHandle(mSayPanel);
   505 }
   506 
   507 public Handler_DoNothing(Handle:menu, MenuAction:action, param1, param2)
   508 {
   509 	/* Do nothing */
   510 }