Use dedicated handlers for normal messages and for CTCP messages.
[xudocci.git] / src / main / java / net / pterodactylus / irc / connection / CtcpHandler.java
1 package net.pterodactylus.irc.connection;
2
3 import static com.google.common.base.Optional.absent;
4 import static com.google.common.base.Optional.fromNullable;
5 import static com.google.common.base.Optional.of;
6 import static java.lang.String.format;
7 import static java.net.InetAddress.getByName;
8 import static org.apache.log4j.Logger.getLogger;
9
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
12 import java.util.List;
13
14 import net.pterodactylus.irc.Connection;
15 import net.pterodactylus.irc.Reply;
16 import net.pterodactylus.irc.Source;
17 import net.pterodactylus.irc.event.DccAcceptReceived;
18 import net.pterodactylus.irc.event.DccSendReceived;
19
20 import com.google.common.base.Optional;
21 import com.google.common.eventbus.EventBus;
22 import com.google.common.primitives.Ints;
23 import com.google.common.primitives.Longs;
24 import org.apache.log4j.Logger;
25
26 /**
27  * Handles all CTCP messages relevant to DCC transfers.
28  *
29  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30  */
31 public class CtcpHandler implements Handler {
32
33         private static final Logger logger = getLogger(CtcpHandler.class);
34         private final EventBus eventBus;
35         private final Connection connection;
36
37         public CtcpHandler(EventBus eventBus, Connection connection) {
38                 this.eventBus = eventBus;
39                 this.connection = connection;
40         }
41
42         @Override
43         public boolean willHandle(Reply reply) {
44                 return (commandIs(reply, "PRIVMSG") || commandIs(reply, "NOTICE"))
45                                 && messageIsCtcp(reply.parameters().get(1));
46         }
47
48         private boolean commandIs(Reply reply, String command) {
49                 return reply.command().equalsIgnoreCase(command);
50         }
51
52         private boolean messageIsCtcp(String message) {
53                 return message.startsWith("\u0001") && message.endsWith("\u0001");
54         }
55
56         @Override
57         public void handleReply(Reply reply) {
58                 List<String> parameters = reply.parameters();
59                 String message = parameters.get(1);
60                 Source source = reply.source().get();
61                 handleCtcp(source, message);
62         }
63
64         private void handleCtcp(Source client, String message) {
65                 String[] messageWords = message.substring(1, message.length() - 1).split(" +");
66                 String ctcpCommand = messageWords[0];
67                 if (ctcpCommand.equalsIgnoreCase("DCC")) {
68                         if (messageWords[1].equalsIgnoreCase("SEND")) {
69                                 Optional<InetAddress> inetAddress = parseInetAddress(messageWords[3]);
70                                 Optional<Integer> port = fromNullable(Ints.tryParse(messageWords[4]));
71                                 long fileSize = fromNullable(Longs.tryParse(messageWords[5])).or(-1L);
72                                 if (inetAddress.isPresent() && port.isPresent()) {
73                                         eventBus.post(new DccSendReceived(connection, client, messageWords[2], inetAddress.get(), port.get(), fileSize));
74                                 } else {
75                                         logger.warn(format("Received malformed DCC SEND: “%s”", message));
76                                 }
77                         } else if (messageWords[1].equalsIgnoreCase("ACCEPT")) {
78                                 Optional<Integer> port = fromNullable(Ints.tryParse(messageWords[3]));
79                                 long position = (messageWords.length > 4) ? fromNullable(Longs.tryParse(messageWords[4])).or(-1L) : -1;
80                                 if (port.isPresent()) {
81                                         eventBus.post(new DccAcceptReceived(connection, client, messageWords[2], port.get(), position));
82                                 } else {
83                                         logger.warn(format("Received malformed DCC ACCEPT: “%s”", message));
84                                 }
85                         }
86                 }
87         }
88
89         private Optional<InetAddress> parseInetAddress(String ip) {
90                 Long ipNumber = Longs.tryParse(ip);
91                 if (ipNumber == null) {
92                         return absent();
93                 }
94
95                 StringBuilder hostname = new StringBuilder(15);
96                 hostname.append((ipNumber >>> 24) & 0xff).append('.');
97                 hostname.append((ipNumber >>> 16) & 0xff).append('.');
98                 hostname.append((ipNumber >>> 8) & 0xff).append('.');
99                 hostname.append(ipNumber & 0xff);
100                 try {
101                         return of(getByName(hostname.toString()));
102                 } catch (UnknownHostException uhe1) {
103                         return absent();
104                 }
105         }
106
107 }