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