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