|
1
|
|
|
/* |
|
2
|
|
|
* This file is part of Araknemu. |
|
3
|
|
|
* |
|
4
|
|
|
* Araknemu is free software: you can redistribute it and/or modify |
|
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
7
|
|
|
* (at your option) any later version. |
|
8
|
|
|
* |
|
9
|
|
|
* Araknemu is distributed in the hope that it will be useful, |
|
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12
|
|
|
* GNU Lesser General Public License for more details. |
|
13
|
|
|
* |
|
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
|
15
|
|
|
* along with Araknemu. If not, see <https://www.gnu.org/licenses/>. |
|
16
|
|
|
* |
|
17
|
|
|
* Copyright (c) 2017-2019 Vincent Quatrevieux |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
package fr.quatrevieux.araknemu.game.chat.channel; |
|
21
|
|
|
|
|
22
|
|
|
import fr.quatrevieux.araknemu.game.GameConfiguration; |
|
23
|
|
|
import fr.quatrevieux.araknemu.game.chat.ChannelType; |
|
24
|
|
|
import fr.quatrevieux.araknemu.game.chat.ChatException; |
|
25
|
|
|
import fr.quatrevieux.araknemu.game.player.GamePlayer; |
|
26
|
|
|
import fr.quatrevieux.araknemu.network.game.in.chat.Message; |
|
27
|
|
|
import fr.quatrevieux.araknemu.network.game.out.info.Information; |
|
28
|
|
|
|
|
29
|
|
|
import java.util.concurrent.ConcurrentHashMap; |
|
30
|
|
|
import java.util.concurrent.ConcurrentMap; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Decorate channel for adding flood guard |
|
34
|
|
|
*/ |
|
35
|
|
|
public final class FloodGuardChannel implements Channel { |
|
36
|
|
|
private final Channel channel; |
|
37
|
|
|
private final GameConfiguration.ChatConfiguration configuration; |
|
38
|
|
|
|
|
39
|
1 |
|
private final ConcurrentMap<Integer, Long> lastSentTime = new ConcurrentHashMap<>(); |
|
40
|
|
|
|
|
41
|
1 |
|
public FloodGuardChannel(Channel channel, GameConfiguration.ChatConfiguration configuration) { |
|
42
|
1 |
|
this.channel = channel; |
|
43
|
1 |
|
this.configuration = configuration; |
|
44
|
1 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
@Override |
|
47
|
|
|
public ChannelType type() { |
|
48
|
1 |
|
return channel.type(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
@Override |
|
52
|
|
|
public boolean authorized(GamePlayer from) { |
|
53
|
1 |
|
return channel.authorized(from); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
@Override |
|
57
|
|
|
public void send(GamePlayer from, Message message) throws ChatException { |
|
58
|
1 |
|
if (!checkFlood(from)) { |
|
59
|
1 |
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
channel.send(from, message); |
|
63
|
1 |
|
lastSentTime.put(from.id(), System.currentTimeMillis()); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check for flood timer |
|
68
|
|
|
* |
|
69
|
|
|
* @param sender The message sender |
|
70
|
|
|
* |
|
71
|
|
|
* @return true if can sent the message |
|
72
|
|
|
*/ |
|
73
|
|
|
private boolean checkFlood(GamePlayer sender) { |
|
74
|
1 |
|
if (configuration.floodTime() < 0) { |
|
75
|
|
|
return true; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
1 |
|
if (!lastSentTime.containsKey(sender.id())) { |
|
79
|
1 |
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
final int remainingTime = (int) ((lastSentTime.get(sender.id()) + configuration.floodTime() * 1000 - System.currentTimeMillis()) / 1000); |
|
83
|
|
|
|
|
84
|
1 |
|
if (remainingTime <= 0) { |
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
1 |
|
sender.send(Information.chatFlood(remainingTime)); |
|
89
|
1 |
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|