forked from lemon-sh/IRCrarria
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIrcClient.cs
More file actions
304 lines (268 loc) · 10.6 KB
/
Copy pathIrcClient.cs
File metadata and controls
304 lines (268 loc) · 10.6 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Security;
using System.Net.Sockets;
using System.Text;
using TShockAPI;
namespace IRCrarria
{
public class IrcClient
{
private class IrcStream : IDisposable
{
private readonly StreamReader _streamReader;
private readonly Stream _stream;
private readonly object _writeLock = new();
public IrcStream(string server, int port, bool ssl, bool ignoreCert)
{
var tcpClient = new TcpClient();
if (!tcpClient.ConnectAsync(server, port).Wait(10000))
{
throw new TimeoutException("IRC connection timeout");
}
var tcpStream = tcpClient.GetStream();
_stream = ssl ? GetSslStream(tcpStream, server, ignoreCert) : tcpStream;
_streamReader = new StreamReader(_stream, leaveOpen:true);
}
public void WriteLine(string input)
{
lock (_writeLock)
{
_stream.Write(Encoding.UTF8.GetBytes(input));
_stream.Write(new byte[]{0x0d, 0x0a});
}
}
public string? ReadLine()
{
return _streamReader.ReadLine();
}
private static SslStream GetSslStream(Stream tcpStream, string servername, bool ignoreSslCert)
{
SslStream? outputStream = null;
try
{
outputStream = new SslStream(tcpStream, false,
ignoreSslCert ? (_, _, _, _) => true : (_, _, _, s) => s == SslPolicyErrors.None, null);
outputStream.AuthenticateAsClient(servername);
return outputStream;
}
catch (Exception)
{
outputStream?.Dispose();
throw;
}
}
public void Dispose()
{
_streamReader.Dispose();
_stream.Dispose();
}
}
private class ParsedCommand
{
public readonly string? Origin;
public readonly string Command;
public readonly List<string>? Params;
private ParsedCommand(string? origin, string command, List<string>? @params)
{
Origin = origin;
Command = command;
Params = @params;
}
public static ParsedCommand? Parse(string input)
{
if (string.IsNullOrEmpty(input)) return null;
string? origin = null;
var prefixSpace = 0;
if (input[0] == ':')
{
prefixSpace = input.IndexOf(' ');
if (prefixSpace == -1) return null;
origin = input[1..prefixSpace];
}
var commandIndex = prefixSpace == 0 ? 0 : prefixSpace + 1;
var trailingIndex = input.IndexOf(':', prefixSpace);
string fullCommand;
string? trailing = null;
if (trailingIndex == -1)
{
if (input.Length <= commandIndex) return null;
fullCommand = input[commandIndex..];
}
else
{
fullCommand = input[commandIndex..trailingIndex];
if (input.Length <= trailingIndex + 1) return null;
trailing = input[(trailingIndex + 1)..];
}
var tempParams = fullCommand.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
if (tempParams.Length == 0) return null;
var command = tempParams[0];
var parameters = new List<string>();
if (tempParams.Length > 1) parameters.AddRange(tempParams[1..]);
if (trailing != null) parameters.Add(trailing);
return new ParsedCommand(origin, command, parameters);
}
}
private string Server { get; }
private int Port { get; }
private string Username { get; }
private string Nick { get; }
private bool Ssl { get; }
private bool IgnoreSslCert { get; }
private bool IrcLog { get; }
private enum ClientState
{
Dead, Starting, Running
}
private ClientState _state = ClientState.Dead;
private readonly object _stateLock = new();
private IrcStream? _stream;
public delegate void WelcomeEventHandler(IrcClient bot);
public event WelcomeEventHandler? Welcome;
public delegate void MessageEventHandler(IrcClient bot, string source, string author, string content);
public event MessageEventHandler? Message;
public delegate void JoinEventHandler(IrcClient bot, string channel, string user);
public event JoinEventHandler? Join;
public delegate void LeaveEventHandler(IrcClient bot, string channel, string user, string? reason);
public event LeaveEventHandler? Leave;
public delegate void QuitEventHandler(IrcClient bot, string user, string? reason);
public event QuitEventHandler? Quit;
public IrcClient(string server, int port, string username, string nick, bool ssl, bool ignoreSslCert, bool ircLog)
{
Server = server;
Port = port;
Username = username;
Nick = nick;
Ssl = ssl;
IgnoreSslCert = ignoreSslCert;
IrcLog = ircLog;
}
private void EnsureAlive()
{
lock (_stateLock)
{
if (_state != ClientState.Running) throw new InvalidOperationException("This client is not running.");
}
}
public bool IsAlive()
{
lock (_stateLock) return _state == ClientState.Running;
}
private void KillAndDispose()
{
lock (_stateLock)
{
if (_state == ClientState.Dead) return;
_state = ClientState.Dead;
}
_stream?.Dispose(); _stream = null;
}
public void RequestDisconnect()
{
EnsureAlive();
_stream?.WriteLine("QUIT");
}
public void SendMessage(string target, string message)
{
EnsureAlive();
_stream?.WriteLine($"PRIVMSG {target} :{message}");
}
public void ExecuteRaw(string raw)
{
EnsureAlive();
_stream?.WriteLine(raw);
}
public void SetSelfMode(string mode)
{
EnsureAlive();
_stream?.WriteLine($"MODE {Nick} {mode}");
}
public void JoinChannel(string channel)
{
EnsureAlive();
_stream?.WriteLine($"JOIN {channel}");
}
private static string GetAuthor(string prefix)
{
var sepIndex = prefix.IndexOf('!');
return sepIndex == -1 ? prefix : prefix[..sepIndex];
}
public void Start()
{
lock (_stateLock)
{
if (_state != ClientState.Dead) throw new InvalidOperationException("This bot is already running (or still starting).");
_state = ClientState.Starting;
}
try
{
_stream = new IrcStream(Server, Port, Ssl, IgnoreSslCert);
lock (_stateLock) _state = ClientState.Running;
_stream.WriteLine($"NICK {Nick}");
_stream.WriteLine($"USER {Username} 0 * :{Username}");
while (_stream.ReadLine() is { } inputLine)
{
if (IrcLog) TShock.Log.Info($"> {inputLine}");
var message = ParsedCommand.Parse(inputLine);
if (message == null)
{
TShock.Log.Error("IRC Server sent malformed message: '{0}'", inputLine);
continue;
}
if (ushort.TryParse(message.Command, NumberStyles.None, null, out var numericCode))
{
switch (numericCode)
{
case 001:
Welcome?.Invoke(this);
break;
case > 400 and < 600:
TShock.Log.ConsoleWarn("IRC error: '{0}'", inputLine);
break;
}
continue;
}
switch (message.Command)
{
case "PING":
if (message.Params != null)
{
_stream.WriteLine($"PONG {message.Params.Last()}");
}
break;
case "PRIVMSG":
if (message.Params != null && message.Origin != null)
{
Message?.Invoke(this, message.Params[0], GetAuthor(message.Origin),
message.Params[1]);
}
break;
case "PART":
if (message.Params != null && message.Origin != null)
{
Leave?.Invoke(this, message.Params[0], GetAuthor(message.Origin), message.Params.ElementAtOrDefault(1));
}
break;
case "QUIT":
if (message.Params != null && message.Origin != null)
{
Quit?.Invoke(this, GetAuthor(message.Origin), message.Params.LastOrDefault());
}
break;
case "JOIN":
if (message.Params != null && message.Origin != null)
{
Join?.Invoke(this, message.Params.Last(), GetAuthor(message.Origin));
}
break;
}
}
}
finally { KillAndDispose(); }
}
}
}