Use only a single place to decide if a message is CTCP.
[xudocci.git] / src / main / java / net / pterodactylus / irc / connection / MessageHandler.java
1 package net.pterodactylus.irc.connection;
2
3 import static net.pterodactylus.irc.connection.CtcpHandler.messageIsCtcp;
4
5 import java.util.List;
6
7 import net.pterodactylus.irc.Connection;
8 import net.pterodactylus.irc.Reply;
9 import net.pterodactylus.irc.Source;
10 import net.pterodactylus.irc.event.ChannelMessageReceived;
11 import net.pterodactylus.irc.event.ChannelNoticeReceived;
12 import net.pterodactylus.irc.event.PrivateMessageReceived;
13 import net.pterodactylus.irc.event.PrivateNoticeReceived;
14
15 import com.google.common.eventbus.EventBus;
16
17 /**
18  * Handles {@code PRIVMSG}s and {@code NOTICE}s that are not CTCP messages.
19  *
20  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
21  */
22 public class MessageHandler implements Handler {
23
24         private final EventBus eventBus;
25         private final Connection connection;
26         private final PrefixHandler prefixHandler;
27
28         public MessageHandler(EventBus eventBus, Connection connection,
29                         PrefixHandler prefixHandler) {
30                 this.eventBus = eventBus;
31                 this.connection = connection;
32                 this.prefixHandler = prefixHandler;
33         }
34
35         @Override
36         public boolean willHandle(Reply reply) {
37                 return (commandIs(reply, "PRIVMSG") || commandIs(reply, "NOTICE"))
38                                 && !messageIsCtcp(reply.parameters().get(1));
39         }
40
41         private boolean commandIs(Reply reply, String command) {
42                 return reply.command().equalsIgnoreCase(command);
43         }
44
45         @Override
46         public void handleReply(Reply reply) {
47                 boolean isNotice = commandIs(reply, "NOTICE");
48                 List<String> parameters = reply.parameters();
49                 String recipient = parameters.get(0);
50                 String message = parameters.get(1);
51                 Source source = reply.source().get();
52                 if (!prefixHandler.isChannel(recipient)) {
53                         if (isNotice) {
54                                 firePrivateNoticeEvent(source, message);
55                         } else {
56                                 firePrivateMessageEvent(source, message);
57                         }
58                 } else {
59                         if (isNotice) {
60                                 fireChannelNoticeEvent(source, recipient, message);
61                         } else {
62                                 fireChannelMessageEvent(source, recipient, message);
63                         }
64                 }
65         }
66
67         private void firePrivateNoticeEvent(Source source, String message) {
68                 eventBus.post(new PrivateNoticeReceived(connection, source, message));
69         }
70
71         private void firePrivateMessageEvent(Source source, String message) {
72                 eventBus.post(new PrivateMessageReceived(connection, source, message));
73         }
74
75         private void fireChannelNoticeEvent(Source source, String channel, String message) {
76                 eventBus.post(new ChannelNoticeReceived(connection, source, channel, message));
77         }
78
79         private void fireChannelMessageEvent(Source source, String channel, String message) {
80                 eventBus.post(new ChannelMessageReceived(connection, source, channel, message));
81         }
82
83 }