plugins/basechat.sp
author Peace-Maker <januk@freenet.de>
Mon Mar 18 19:58:18 2013 -0400 (2013-03-18)
changeset 3849 a0a045ca6d6a
parent 2899 7f1410a31503
child 3961 0dc477c29792
permissions -rw-r--r--
Fixed double print when sending psay to self (bug 5649, r=psychonic).
     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 g_GameEngine = SOURCE_SDK_UNKNOWN;
    55 
    56 public OnPluginStart()
    57 {
    58 	LoadTranslations("common.phrases");
    59 	
    60 	g_GameEngine = GuessSDKVersion();
    61 
    62 	g_Cvar_Chatmode = CreateConVar("sm_chat_mode", "1", "Allows player's to send messages to admin chat.", 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 	
    69 	/* HintText does not work on Dark Messiah */
    70 	if (g_GameEngine != SOURCE_SDK_DARKMESSIAH)
    71 	{
    72 		RegAdminCmd("sm_hsay", Command_SmHsay, ADMFLAG_CHAT, "sm_hsay <message> - sends hint message to all players");	
    73 	}
    74 	
    75 	RegAdminCmd("sm_tsay", Command_SmTsay, ADMFLAG_CHAT, "sm_tsay [color] <message> - sends top-left message to all players");
    76 	RegAdminCmd("sm_chat", Command_SmChat, ADMFLAG_CHAT, "sm_chat <message> - sends message to admins");
    77 	RegAdminCmd("sm_psay", Command_SmPsay, ADMFLAG_CHAT, "sm_psay <name or #userid> <message> - sends private message");
    78 	RegAdminCmd("sm_msay", Command_SmMsay, ADMFLAG_CHAT, "sm_msay <message> - sends message as a menu panel");
    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 	if (msgStart == 1 && CheckCommandAccess(client, "sm_say", ADMFLAG_CHAT)) // sm_say alias
   113 	{
   114 		SendChatToAll(client, message);
   115 		LogAction(client, -1, "\"%L\" triggered sm_say (text %s)", client, message);
   116 	}
   117 	else if (msgStart == 3 && CheckCommandAccess(client, "sm_csay", ADMFLAG_CHAT)) // sm_csay alias
   118 	{
   119 		DisplayCenterTextToAll(client, message);
   120 		LogAction(client, -1, "\"%L\" triggered sm_csay (text %s)", client, text);		
   121 	}	
   122 	else if (msgStart == 2 && CheckCommandAccess(client, "sm_psay", ADMFLAG_CHAT)) // sm_psay alias
   123 	{
   124 		decl String:arg[64];
   125 	
   126 		new len = BreakString(message, arg, sizeof(arg));
   127 		new target = FindTarget(client, arg, true, false);
   128 		
   129 		if (target == -1 || len == -1)
   130 			return Plugin_Handled;
   131 	
   132 		PrintToChat(client, "\x04(Private to %N) %N: \x01%s", target, client, message[len]);
   133 		PrintToChat(target, "\x04(Private to %N) %N: \x01%s", target, client, message[len]);
   134 
   135 		LogAction(client, -1, "\"%L\" triggered sm_psay to \"%L\" (text %s)", client, target, message);		
   136 	}
   137 	else
   138 		return Plugin_Continue;
   139 	
   140 	return Plugin_Handled;	
   141 }
   142 
   143 public Action:Command_SayAdmin(client, args)
   144 {
   145 	if (!CheckCommandAccess(client, "sm_chat", ADMFLAG_CHAT) && !GetConVarBool(g_Cvar_Chatmode))
   146 	{
   147 		return Plugin_Continue;	
   148 	}
   149 	
   150 	decl String:text[192];
   151 	if (IsChatTrigger() || GetCmdArgString(text, sizeof(text)) < 1)
   152 	{
   153 		return Plugin_Continue;
   154 	}
   155 	
   156 	new startidx;
   157 	if (text[strlen(text)-1] == '"')
   158 	{
   159 		text[strlen(text)-1] = '\0';
   160 		startidx = 1;
   161 	}
   162 	
   163 	if (text[startidx] != CHAT_SYMBOL)
   164 		return Plugin_Continue;
   165 	
   166 	decl String:message[192];
   167 	strcopy(message, 192, text[startidx+1]);
   168 
   169 	SendChatToAdmins(client, message);
   170 	LogAction(client, -1, "\"%L\" triggered sm_chat (text %s)", client, message);
   171 	
   172 	return Plugin_Handled;	
   173 }
   174 
   175 public Action:Command_SmSay(client, args)
   176 {
   177 	if (args < 1)
   178 	{
   179 		ReplyToCommand(client, "[SM] Usage: sm_say <message>");
   180 		return Plugin_Handled;	
   181 	}
   182 	
   183 	decl String:text[192];
   184 	GetCmdArgString(text, sizeof(text));
   185 
   186 	SendChatToAll(client, text);
   187 	LogAction(client, -1, "\"%L\" triggered sm_say (text %s)", client, text);
   188 	
   189 	return Plugin_Handled;		
   190 }
   191 
   192 public Action:Command_SmCsay(client, args)
   193 {
   194 	if (args < 1)
   195 	{
   196 		ReplyToCommand(client, "[SM] Usage: sm_csay <message>");
   197 		return Plugin_Handled;	
   198 	}
   199 	
   200 	decl String:text[192];
   201 	GetCmdArgString(text, sizeof(text));
   202 	
   203 	DisplayCenterTextToAll(client, text);
   204 	
   205 	LogAction(client, -1, "\"%L\" triggered sm_csay (text %s)", client, text);
   206 	
   207 	return Plugin_Handled;		
   208 }
   209 
   210 public Action:Command_SmHsay(client, args)
   211 {
   212 	if (args < 1)
   213 	{
   214 		ReplyToCommand(client, "[SM] Usage: sm_hsay <message>");
   215 		return Plugin_Handled;  
   216 	}
   217 	
   218 	decl String:text[192];
   219 	GetCmdArgString(text, sizeof(text));
   220  
   221 	decl String:nameBuf[MAX_NAME_LENGTH];
   222 	
   223 	for (new i = 1; i <= MaxClients; i++)
   224 	{
   225 		if (!IsClientInGame(i) || IsFakeClient(i))
   226 		{
   227 			continue;
   228 		}
   229 		FormatActivitySource(client, i, nameBuf, sizeof(nameBuf));
   230 		PrintHintText(i, "%s: %s", nameBuf, text);
   231 	}
   232 	
   233 	LogAction(client, -1, "\"%L\" triggered sm_hsay (text %s)", client, text);
   234 	
   235 	return Plugin_Handled;	
   236 }
   237 
   238 public Action:Command_SmTsay(client, args)
   239 {
   240 	if (args < 1)
   241 	{
   242 		ReplyToCommand(client, "[SM] Usage: sm_tsay <message>");
   243 		return Plugin_Handled;  
   244 	}
   245 	
   246 	decl String:text[192], String:colorStr[16];
   247 	GetCmdArgString(text, sizeof(text));
   248 	
   249 	new len = BreakString(text, colorStr, 16);
   250 		
   251 	new color = FindColor(colorStr);
   252 	new String:nameBuf[MAX_NAME_LENGTH];
   253 	
   254 	if (color == -1)
   255 	{
   256 		color = 0;
   257 		len = 0;
   258 	}
   259 	
   260 	for (new i = 1; i <= MaxClients; i++)
   261 	{
   262 		if (!IsClientInGame(i) || IsFakeClient(i))
   263 		{
   264 			continue;
   265 		}
   266 		FormatActivitySource(client, i, nameBuf, sizeof(nameBuf));
   267 		SendDialogToOne(i, color, "%s: %s", nameBuf, text[len]);
   268 	}
   269 
   270 	LogAction(client, -1, "\"%L\" triggered sm_tsay (text %s)", client, text);
   271 	
   272 	return Plugin_Handled;	
   273 }
   274 
   275 public Action:Command_SmChat(client, args)
   276 {
   277 	if (args < 1)
   278 	{
   279 		ReplyToCommand(client, "[SM] Usage: sm_chat <message>");
   280 		return Plugin_Handled;	
   281 	}	
   282 	
   283 	decl String:text[192];
   284 	GetCmdArgString(text, sizeof(text));
   285 
   286 	SendChatToAdmins(client, text);
   287 	LogAction(client, -1, "\"%L\" triggered sm_chat (text %s)", client, text);
   288 	
   289 	return Plugin_Handled;	
   290 }
   291 
   292 public Action:Command_SmPsay(client, args)
   293 {
   294 	if (args < 2)
   295 	{
   296 		ReplyToCommand(client, "[SM] Usage: sm_psay <name or #userid> <message>");
   297 		return Plugin_Handled;	
   298 	}	
   299 	
   300 	decl String:text[192], String:arg[64], String:message[192];
   301 	GetCmdArgString(text, sizeof(text));
   302 
   303 	new len = BreakString(text, arg, sizeof(arg));
   304 	BreakString(text[len], message, sizeof(message));
   305 	
   306 	new target = FindTarget(client, arg, true, false);
   307 		
   308 	if (target == -1)
   309 		return Plugin_Handled;	
   310 		
   311 	decl String:name[64];
   312 
   313 	if (client == 0)
   314 	{
   315 		name = "Console";
   316 	}
   317 	else
   318 	{
   319 		GetClientName(client, name, sizeof(name));
   320 	}
   321 
   322 	if (client == 0)
   323 	{
   324 		PrintToServer("(Private: %N) %s: %s", target, name, message);
   325 	}
   326 	else if (target != client)
   327 	{
   328 		PrintToChat(client, "\x04(Private: %N) %s: \x01%s", target, name, message);
   329 	}
   330 
   331 	PrintToChat(target, "\x04(Private: %N) %s: \x01%s", target, name, message);
   332 	LogAction(client, -1, "\"%L\" triggered sm_psay to \"%L\" (text %s)", client, target, message);
   333 	
   334 	return Plugin_Handled;	
   335 }
   336 
   337 public Action:Command_SmMsay(client, args)
   338 {
   339 	if (args < 1)
   340 	{
   341 		ReplyToCommand(client, "[SM] Usage: sm_msay <message>");
   342 		return Plugin_Handled;	
   343 	}
   344 	
   345 	decl String:text[192];
   346 	GetCmdArgString(text, sizeof(text));
   347 
   348 	SendPanelToAll(client, text);
   349 
   350 	LogAction(client, -1, "\"%L\" triggered sm_msay (text %s)", client, text);
   351 	
   352 	return Plugin_Handled;		
   353 }
   354 
   355 FindColor(String:color[])
   356 {
   357 	for (new i = 0; i < 13; i++)
   358 	{
   359 		if (strcmp(color, g_ColorNames[i], false) == 0)
   360 			return i;
   361 	}
   362 	
   363 	return -1;
   364 }
   365 
   366 SendChatToAll(client, String:message[])
   367 {
   368 	new String:nameBuf[MAX_NAME_LENGTH];
   369 	
   370 	for (new i = 1; i <= MaxClients; i++)
   371 	{
   372 		if (!IsClientInGame(i) || IsFakeClient(i))
   373 		{
   374 			continue;
   375 		}
   376 		FormatActivitySource(client, i, nameBuf, sizeof(nameBuf));
   377 		
   378 		PrintToChat(i, "\x04(ALL) %s: \x01%s", nameBuf, message);
   379 	}
   380 }
   381 
   382 DisplayCenterTextToAll(client, String:message[])
   383 {
   384 	new String:nameBuf[MAX_NAME_LENGTH];
   385 	
   386 	for (new i = 1; i <= MaxClients; i++)
   387 	{
   388 		if (!IsClientInGame(i) || IsFakeClient(i))
   389 		{
   390 			continue;
   391 		}
   392 		FormatActivitySource(client, i, nameBuf, sizeof(nameBuf));
   393 		PrintCenterText(i, "%s: %s", nameBuf, message);
   394 	}
   395 }
   396 
   397 SendChatToAdmins(from, String:message[])
   398 {
   399 	new fromAdmin = CheckCommandAccess(from, "sm_chat", ADMFLAG_CHAT);
   400 	for (new i = 1; i <= MaxClients; i++)
   401 	{
   402 		if (IsClientInGame(i) && (from == i || CheckCommandAccess(i, "sm_chat", ADMFLAG_CHAT)))
   403 		{
   404 			PrintToChat(i, "\x04(%sADMINS) %N: \x01%s", fromAdmin ? "" : "TO ", from, message);
   405 		}	
   406 	}
   407 }
   408 
   409 SendDialogToOne(client, color, String:text[], any:...)
   410 {
   411 	new String:message[100];
   412 	VFormat(message, sizeof(message), text, 4);	
   413 	
   414 	new Handle:kv = CreateKeyValues("Stuff", "title", message);
   415 	KvSetColor(kv, "color", g_Colors[color][0], g_Colors[color][1], g_Colors[color][2], 255);
   416 	KvSetNum(kv, "level", 1);
   417 	KvSetNum(kv, "time", 10);
   418 	
   419 	CreateDialog(client, kv, DialogType_Msg);
   420 	
   421 	CloseHandle(kv);	
   422 }
   423 
   424 SendPanelToAll(from, String:message[])
   425 {
   426 	decl String:title[100];
   427 	Format(title, 64, "%N:", from);
   428 	
   429 	ReplaceString(message, 192, "\\n", "\n");
   430 	
   431 	new Handle:mSayPanel = CreatePanel();
   432 	SetPanelTitle(mSayPanel, title);
   433 	DrawPanelItem(mSayPanel, "", ITEMDRAW_SPACER);
   434 	DrawPanelText(mSayPanel, message);
   435 	DrawPanelItem(mSayPanel, "", ITEMDRAW_SPACER);
   436 
   437 	SetPanelCurrentKey(mSayPanel, 10);
   438 	DrawPanelItem(mSayPanel, "Exit", ITEMDRAW_CONTROL);
   439 
   440 	for(new i = 1; i <= MaxClients; i++)
   441 	{
   442 		if(IsClientInGame(i) && !IsFakeClient(i))
   443 		{
   444 			SendPanelToClient(mSayPanel, i, Handler_DoNothing, 10);
   445 		}
   446 	}
   447 
   448 	CloseHandle(mSayPanel);
   449 }
   450 
   451 public Handler_DoNothing(Handle:menu, MenuAction:action, param1, param2)
   452 {
   453 	/* Do nothing */
   454 }