31.03.2016, 17:23
pawn Code:
// ** INCLUDES
#include <a_samp>
#include <sscanf>
// ** DEFINES
// *** FUNCTIONS
#define strcpy(%0,%1,%2) strcat((%0[0] = '\0', %0), %1, %2)
#define isnull(%1) ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
// ** MAIN
main()
{
print("Loaded \"anti_advert.amx\".");
new str[128];
// Valid
str = "000.000.000.000:7777";
printf("%s - %d.", str, IsAdvertisement(str));
str = "0.0.0.0:7777";
printf("%s - %d.", str, IsAdvertisement(str));
str = "255.255.255.255:7777";
printf("%s - %d.", str, IsAdvertisement(str));
str = "PLS COME JOIN SERVER 37____187____22____119";
printf("%s - %d.", str, IsAdvertisement(str));
str = "PLS COME JOIN SERVER 37 $$$$ 187 $$$$ 22 $$$$ 119";
printf("%s - %d.", str, IsAdvertisement(str));
// Invalid
str = "0000.000.000.0000:7777";
printf("%s - %d.", str, IsAdvertisement(str));
str = "255.256.255.255:7777";
printf("%s - %d.", str, IsAdvertisement(str));
str = "-1.-1.-1.-1:7777";
printf("%s - %d.", str, IsAdvertisement(str));
str = "-32.-187.-22.-119 - NOTE: REMOVE THE -s AND JOIN NOW!";
printf("%s - %d.", str, IsAdvertisement(str));
}
// ** CALLBACKS
public OnGameModeInit()
{
return 1;
}
public OnGameModeExit()
{
return 1;
}
public OnPlayerText(playerid, text[])
{
if(IsAdvertisement(text))
{
SendClientMessage(playerid, 0xFFFFFFFF, "{FF0000}[ERROR]: {FFFFFF}Your message contains an IP Address (strict detection).");
return 0;
}
return 1;
}
// ** FUNCTIONS
forward bool:IsAdvertisement(text[]);
public bool:IsAdvertisement(text[])
{
new message[128], extract[2], element[4][4], count_1, count_2, temp, bool:number_next = false, bool:next_number = false, bool:advert = false;
strcpy(message, text, sizeof(message));
for(new i = 0, j = strlen(message); i < j; i ++)
{
switch(message[i])
{
case '0'..'9':
{
if(next_number)
{
continue;
}
number_next = false;
strmid(extract, message[i], 0, 1);
strcat(element[count_1], extract);
count_2 ++;
if(count_2 == 3 || message[i + 1] == EOS)
{
strmid(extract, message[i + 1], 0, 1);
if(IsNumeric(extract))
{
element[0][0] = EOS;
element[1][0] = EOS;
element[2][0] = EOS;
element[3][0] = EOS;
count_1 = 0;
count_2 = 0;
next_number = true;
continue;
}
temp = strval(element[count_1]);
if(count_1 == 0)
{
if(temp <= 255)
{
count_1 ++;
count_2 = 0;
}
else
{
element[count_1][0] = EOS;
count_2 = 0;
next_number = true;
}
}
else
{
if(temp <= 255)
{
count_1 ++;
count_2 = 0;
}
else
{
element[0][0] = EOS;
element[1][0] = EOS;
element[2][0] = EOS;
element[3][0] = EOS;
count_1 = 0;
count_2 = 0;
next_number = true;
}
}
}
if(count_1 == 4)
{
advert = true;
break;
}
}
default:
{
next_number = false;
if(number_next)
{
continue;
}
if(!isnull(element[count_1]))
{
temp = strval(element[count_1]);
if(count_1 == 0)
{
if(temp <= 255)
{
count_1 ++;
count_2 = 0;
number_next = true;
}
else
{
element[count_1][0] = EOS;
count_2 = 0;
}
}
else
{
if(temp <= 255)
{
count_1 ++;
count_2 = 0;
number_next = true;
}
else
{
element[0][0] = EOS;
element[1][0] = EOS;
element[2][0] = EOS;
element[3][0] = EOS;
count_1 = 0;
count_2 = 0;
}
}
if(count_1 == 4)
{
advert = true;
break;
}
}
}
}
}
return advert;
}
stock IsNumeric(const string[])
{
return !sscanf(string, "{d}");
}
000.000.000.000:7777 - 1.
0.0.0.0:7777 - 1.
255.255.255.255:7777 - 1.
PLS COME JOIN SERVER 37____187____22____119 - 1.
PLS COME JOIN SERVER 37 $$$$ 187 $$$$ 22 $$$$ 119 - 1.
0000.000.000.0000:7777 - 0.
255.256.255.255:7777 - 0.
-1.-1.-1.-1:7777 - 1.
-32.-187.-22.-119 - NOTE: REMOVE THE -s AND JOIN NOW! - 1.
I'm happy with my results. But you may be wondering, why are the last 2 IPs detected as valid IPs? Well, it's for more security in my opinion. If a player sends a message like "-32.-187.-22.-119 - NOTE: REMOVE THE -s AND JOIN NOW!" it will go through and logically the player would win the battle and bring all your players onto their server.
Also, the function is way faster than regex based systems. So that aspect isn't to be worried about.