1 package net.pterodactylus.irc.connection;
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;
13 import com.google.common.eventbus.EventBus;
16 * Handles {@code PRIVMSG}s and {@code NOTICE}s that are not CTCP messages.
18 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
20 public class MessageHandler implements Handler {
22 private final EventBus eventBus;
23 private final Connection connection;
24 private final PrefixHandler prefixHandler;
26 public MessageHandler(EventBus eventBus, Connection connection,
27 PrefixHandler prefixHandler) {
28 this.eventBus = eventBus;
29 this.connection = connection;
30 this.prefixHandler = prefixHandler;
34 public boolean willHandle(Reply reply) {
35 return (commandIs(reply, "PRIVMSG") || commandIs(reply, "NOTICE"))
36 && !messageIsCtcp(reply.parameters().get(1));
39 private boolean commandIs(Reply reply, String command) {
40 return reply.command().equalsIgnoreCase(command);
43 private boolean messageIsCtcp(String message) {
44 return message.startsWith("\u0001") && message.endsWith("\u0001");
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)) {
56 firePrivateNoticeEvent(source, message);
58 firePrivateMessageEvent(source, message);
62 fireChannelNoticeEvent(source, recipient, message);
64 fireChannelMessageEvent(source, recipient, message);
69 private void firePrivateNoticeEvent(Source source, String message) {
70 eventBus.post(new PrivateNoticeReceived(connection, source, message));
73 private void firePrivateMessageEvent(Source source, String message) {
74 eventBus.post(new PrivateMessageReceived(connection, source, message));
77 private void fireChannelNoticeEvent(Source source, String channel, String message) {
78 eventBus.post(new ChannelNoticeReceived(connection, source, channel, message));
81 private void fireChannelMessageEvent(Source source, String channel, String message) {
82 eventBus.post(new ChannelMessageReceived(connection, source, channel, message));