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