This repository has been archived by the owner on Jul 27, 2019. It is now read-only.
/
IRCMessageQueueWatcher.java
145 lines (129 loc) · 4.73 KB
/
IRCMessageQueueWatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (C) 2014 cnaude
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.cnaude.purpleirc;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.pircbotx.Channel;
import org.pircbotx.User;
/**
*
* @author Chris Naude Poll the command queue and dispatch to Bukkit
*/
public class IRCMessageQueueWatcher {
private final PurpleIRC plugin;
private final PurpleBot ircBot;
private final Timer timer;
private final BlockingQueue<IRCMessage> queue = new LinkedBlockingQueue<>();
private final String REGEX_CLEAN = "^[\\r\\n]|[\\r\\n]$";
private final String REGEX_CRLF = "\\r\\n";
private final String LF = "\\n";
/**
*
* @param plugin the PurpleIRC plugin
* @param ircBot
*/
public IRCMessageQueueWatcher(final PurpleBot ircBot, final PurpleIRC plugin) {
this.plugin = plugin;
this.ircBot = ircBot;
this.timer = new Timer();
startWatcher();
}
private void startWatcher() {
timer.schedule(new TimerTask() {
@Override
public void run() {
queueAndSend();
}
}, 0, 5);
}
private void queueAndSend() {
IRCMessage ircMessage = queue.poll();
if (ircMessage != null) {
plugin.logDebug("[" + queue.size() + "]: queueAndSend message detected");
for (String s : cleanupAndSplitMessage(ircMessage.message)) {
if (ircMessage.ctcpResponse) {
blockingCTCPMessage(ircMessage.target, s);
} else {
blockingIRCMessage(ircMessage.target, s);
}
}
}
}
private void blockingIRCMessage(final String target, final String message) {
if (!ircBot.isConnected()) {
return;
}
plugin.logDebug("[blockingIRCMessage] About to send IRC message to " + target + ": " + message);
ircBot.bot.sendIRC().message(target, message);
plugin.logDebug("[blockingIRCMessage] Message sent to " + target + ": " + message);
}
private void blockingCTCPMessage(final String target, final String message) {
if (!ircBot.isConnected()) {
return;
}
plugin.logDebug("[blockingCTCPMessage] About to send IRC message to " + target + ": " + message);
ircBot.bot.sendIRC().ctcpResponse(target, message);
plugin.logDebug("[blockingCTCPMessage] Message sent to " + target + ": " + message);
}
private String addZeroWidthSpace(String s) {
if (s.length() > 1) {
String a = s.substring(0, 1);
String b = s.substring(1);
return a + "\u200B" + b;
}
return s;
}
private String pingFix(String message) {
for (Channel channel : ircBot.bot.getUserBot().getChannels()) {
for (User user : channel.getUsers()) {
if (user.getNick().equalsIgnoreCase(ircBot.botNick)) {
continue;
}
if (message.toLowerCase().contains(user.getNick().toLowerCase())) {
message = message.replaceAll("(?i)" + user.getNick(), addZeroWidthSpace(user.getNick()));
plugin.logDebug("Adding ZWS to " + user.getNick());
}
}
}
return message;
}
private String[] cleanupAndSplitMessage(String message) {
if (ircBot.pingFix) {
message = pingFix(message);
}
return message.replaceAll(REGEX_CLEAN, "").replaceAll(REGEX_CRLF, "\n").split(LF);
}
public void cancel() {
timer.cancel();
}
public String clearQueue() {
int size = queue.size();
if (!queue.isEmpty()) {
queue.clear();
}
return "Elements removed from message queue: " + size;
}
/**
*
* @param ircMessage
*/
public void add(IRCMessage ircMessage) {
queue.offer(ircMessage);
}
}