1 package net.pterodactylus.irc.connection;
3 import static net.pterodactylus.irc.connection.CtcpHandler.messageIsCtcp;
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;
15 import com.google.common.eventbus.EventBus;
18 * Handles {@code PRIVMSG}s and {@code NOTICE}s that are not CTCP messages.
20 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22 public class MessageHandler implements Handler {
24 private final EventBus eventBus;
25 private final Connection connection;
26 private final PrefixHandler prefixHandler;
28 public MessageHandler(EventBus eventBus, Connection connection,
29 PrefixHandler prefixHandler) {
30 this.eventBus = eventBus;
31 this.connection = connection;
32 this.prefixHandler = prefixHandler;
36 public boolean willHandle(Reply reply) {
37 return (commandIs(reply, "PRIVMSG") || commandIs(reply, "NOTICE"))
38 && !messageIsCtcp(reply.parameters().get(1));
41 private boolean commandIs(Reply reply, String command) {
42 return reply.command().equalsIgnoreCase(command);
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)) {
54 firePrivateNoticeEvent(source, message);
56 firePrivateMessageEvent(source, message);
60 fireChannelNoticeEvent(source, recipient, message);
62 fireChannelMessageEvent(source, recipient, message);
67 private void firePrivateNoticeEvent(Source source, String message) {
68 eventBus.post(new PrivateNoticeReceived(connection, source, message));
71 private void firePrivateMessageEvent(Source source, String message) {
72 eventBus.post(new PrivateMessageReceived(connection, source, message));
75 private void fireChannelNoticeEvent(Source source, String channel, String message) {
76 eventBus.post(new ChannelNoticeReceived(connection, source, channel, message));
79 private void fireChannelMessageEvent(Source source, String channel, String message) {
80 eventBus.post(new ChannelMessageReceived(connection, source, channel, message));