summaryrefslogtreecommitdiffstats
path: root/EssentialsAntiCheat/src/com/earth2me/essentials/anticheat/checks/chat/SpamCheck.java
blob: 8bf8930910f8a8bc8b10649cbf2b40e703de23a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.earth2me.essentials.anticheat.checks.chat;

import com.earth2me.essentials.anticheat.NoCheat;
import com.earth2me.essentials.anticheat.NoCheatPlayer;
import com.earth2me.essentials.anticheat.actions.ParameterName;
import com.earth2me.essentials.anticheat.data.Statistics.Id;
import java.util.Locale;


/**
 * The SpamCheck will count messages and commands over a short timeframe to see if the player tried to send too many of
 * them
 *
 */
public class SpamCheck extends ChatCheck
{
	public SpamCheck(NoCheat plugin)
	{
		super(plugin, "chat.spam");
	}

	public boolean check(NoCheatPlayer player, ChatData data, ChatConfig cc)
	{

		boolean cancel = false;
		// Maybe it's a command and on the whitelist
		for (String s : cc.spamWhitelist)
		{
			if (data.message.startsWith(s))
			{
				// It is
				return false;
			}
		}

		int commandLimit = cc.spamCommandLimit;
		int messageLimit = cc.spamMessageLimit;
		long timeframe = cc.spamTimeframe;

		final long time = System.currentTimeMillis();

		// Has enough time passed? Then reset the counters
		if (data.spamLastTime + timeframe <= time)
		{
			data.spamLastTime = time;
			data.messageCount = 0;
			data.commandCount = 0;
		}
		// Security check, if the system time changes
		else if (data.spamLastTime > time)
		{
			data.spamLastTime = Integer.MIN_VALUE;
		}

		// Increment appropriate counter
		if (data.message.startsWith("/"))
		{
			data.commandCount++;
		}
		else
		{
			data.messageCount++;
		}

		// Did the player go over the limit on at least one of the counters?
		if (data.messageCount > messageLimit || data.commandCount > commandLimit)
		{

			// Set the vl as the number of messages above the limit and
			// increment statistics
			data.spamVL = Math.max(0, data.messageCount - messageLimit);
			data.spamVL += Math.max(0, data.commandCount - commandLimit);
			incrementStatistics(player, Id.CHAT_SPAM, 1);

			// Execute whatever actions are associated with this check and the
			// violation level and find out if we should cancel the event
			cancel = executeActions(player, cc.spamActions, data.spamVL);
		}

		return cancel;
	}

	@Override
	public String getParameter(ParameterName wildcard, NoCheatPlayer player)
	{

		if (wildcard == ParameterName.VIOLATIONS)
		{
			return String.format(Locale.US, "%d", getData(player).spamVL);
		}
		else
		{
			return super.getParameter(wildcard, player);
		}
	}
}