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