🚧 Parse token from DCC SEND
[xudocci.git] / irc / connection / ChannelNickHandler.java
1 package net.pterodactylus.irc.connection;
2
3 import static java.util.Arrays.asList;
4
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import net.pterodactylus.irc.Connection;
9 import net.pterodactylus.irc.Nickname;
10 import net.pterodactylus.irc.Reply;
11 import net.pterodactylus.irc.event.ChannelNicknames;
12
13 import com.google.common.eventbus.EventBus;
14
15 /**
16  * Handles the list of nicknames that are sent on a {@code JOIN}.
17  *
18  * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
19  */
20 public class ChannelNickHandler implements Handler {
21
22         private static final List<String> COMMANDS_TO_HANDLE =
23                         asList("353", "366");
24         private final EventBus eventBus;
25         private final Connection connection;
26         private final PrefixHandler prefixHandler;
27         private final List<Nickname> nicknames = new ArrayList<>();
28
29         public ChannelNickHandler(EventBus eventBus, Connection connection,
30                         PrefixHandler prefixHandler) {
31                 this.eventBus = eventBus;
32                 this.connection = connection;
33                 this.prefixHandler = prefixHandler;
34         }
35
36         @Override
37         public boolean willHandle(Reply reply) {
38                 return COMMANDS_TO_HANDLE.contains(reply.command());
39         }
40
41         @Override
42         public void handleReply(Reply reply) {
43                 String command = reply.command();
44                 List<String> parameters = reply.parameters();
45                 if (command.equals("353")) {
46                         asList(parameters.get(3).split(" ")).stream().forEach(
47                                         (nick) -> nicknames.add(prefixHandler.getNickname(nick)));
48                 } else if (command.equals("366")) {
49                         eventBus.post(new ChannelNicknames(connection, parameters.get(1),
50                                         nicknames));
51                         nicknames.clear();
52                 }
53         }
54
55 }