blob: f3abd40f6e8a2029823c86bc53d6b02fdd42d72f (
plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
package dev.plutorocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class IrcClient {
private final String host;
private final int port;
private final String channel;
private final String nick;
private volatile boolean running = false;
private volatile boolean connected = false;
private Socket socket;
private BufferedWriter writer;
public IrcClient(String host, int port, String channel, String nick) {
this.host = host;
this.port = port;
this.channel = channel.startsWith("#") ? channel : "#" + channel;
this.nick = nick;
}
/**
* start the IRC thread.
*/
public void connect() {
if (running) return;
running = true;
Thread thread = new Thread(this::runLoop, "MinecraftIRC-Thread");
thread.setDaemon(true);
thread.start();
}
public boolean isConnected() {
return connected;
}
public void disconnect() {
running = false;
try {
if (socket != null && !socket.isClosed()) {
socket.close();
}
} catch (IOException ignored) {}
connected = false;
}
private void runLoop() {
try (Socket sock = new Socket(host, port);
BufferedReader reader = new BufferedReader(
new InputStreamReader(sock.getInputStream(), StandardCharsets.UTF_8));
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(sock.getOutputStream(), StandardCharsets.UTF_8))) {
this.socket = sock;
this.writer = writer;
this.connected = true;
sendRaw("NICK " + nick);
sendRaw("USER " + nick + " 0 * :" + nick);
sendRaw("JOIN " + channel);
sendClientChat(
Text.literal("[IRC] ").formatted(Formatting.AQUA)
.append(Text.literal("Connected to " + host + " " + channel + " as " + nick)
.formatted(Formatting.GRAY))
);
String line;
while (running && (line = reader.readLine()) != null) {
handleLine(line);
}
if (running) {
sendClientChat(
Text.literal("[IRC] ").formatted(Formatting.AQUA)
.append(Text.literal("Disconnected from server. Use /irc connect to reconnect.")
.formatted(Formatting.GRAY))
);
}
} catch (IOException e) {
sendClientChat(
Text.literal("[IRC] ").formatted(Formatting.AQUA)
.append(Text.literal("Connection error: " + e.getMessage()
+ " (use /irc connect to try again)")
.formatted(Formatting.GRAY))
);
} finally {
connected = false;
running = false;
}
}
private void handleLine(String line) {
// respond to server PINGs
if (line.startsWith("PING")) {
String payload = line.length() > 5 ? line.substring(5) : "";
sendRaw("PONG " + payload);
return;
}
// only care about PRIVMSG
if (!line.contains(" PRIVMSG ")) {
return;
}
int prefixEnd = line.indexOf(' ');
if (!line.startsWith(":") || prefixEnd <= 1) {
return;
}
String prefix = line.substring(1, prefixEnd);
String nick = prefix;
int bang = prefix.indexOf('!');
if (bang != -1) {
nick = prefix.substring(0, bang);
}
String[] split = line.split(" :", 2);
if (split.length < 2) {
return;
}
String trailing = split[1];
String commandPart = split[0];
String[] cmdParts = commandPart.split(" ");
if (cmdParts.length < 3) {
return;
}
String target = cmdParts[2];
if (!target.equalsIgnoreCase(this.channel)
&& !target.equalsIgnoreCase(this.nick)) {
return;
}
MutableText prefixText = Text.literal("[IRC] ")
.formatted(Formatting.AQUA);
MutableText nickText = Text.literal("<" + nick + "> ")
.formatted(Formatting.WHITE);
MutableText msgText = Text.literal(trailing)
.formatted(Formatting.WHITE);
sendClientChat(prefixText.append(nickText).append(msgText));
}
/**
* sends a message to the configured channel
*/
public void sendChannelMessage(String message) {
if (!connected) return;
sendRaw("PRIVMSG " + channel + " :" + message);
}
/**
* low-level raw IRC send
*/
private synchronized void sendRaw(String line) {
if (writer == null) return;
try {
writer.write(line);
writer.write("\r\n");
writer.flush();
} catch (IOException e) {
sendClientChat(
Text.literal("[IRC] ").formatted(Formatting.AQUA)
.append(Text.literal("Send error: " + e.getMessage())
.formatted(Formatting.GRAY))
);
disconnect();
}
}
private void sendClientChat(Text text) {
MinecraftClient client = MinecraftClient.getInstance();
if (client == null) return;
client.execute(() -> {
if (client.inGameHud != null) {
client.inGameHud.getChatHud().addMessage(text);
}
});
}
}
|