-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
IrcDiscordRelay.cs
511 lines (438 loc) · 21 KB
/
IrcDiscordRelay.cs
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
using Discord;
using Discord.Net.Rest;
using Discord.Net.WebSockets;
using Discord.WebSocket;
using IniParser;
using IniParser.Model;
using Meebey.SmartIrc4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace IrcDiscordRelay
{
internal class IrcDiscordRelay
{
private const int MAX_MESSAGE_LENGTH = 400;
private readonly string ircServer;
private readonly int ircPort;
private readonly string ircNickname;
private readonly string ircRealname;
private readonly string ircUsername;
private readonly string ircPassword;
private readonly bool ircUseSSL;
private readonly string discordBotToken;
private readonly string discordProxy;
private readonly bool discordIncludeEdited;
private readonly Dictionary<ulong, string> discordToIrcChannelMap;
private readonly Dictionary<string, ulong> ircToDiscordChannelMap;
private readonly Dictionary<string, IMessageChannel> discordChannelsMap;
private HashSet<string> ircIgnoredUsers;
private HashSet<Regex> ircIgnoredUsersRegex;
private HashSet<string> discordIgnoredUsers;
private HashSet<Regex> discordIgnoredUsersRegex;
private readonly IrcClient ircClient;
private readonly DiscordSocketClient discordClient;
public IrcDiscordRelay()
{
FileIniDataParser parser = new();
IniData data = parser.ReadFile("config.ini");
ircServer = data["IRC"]["Server"];
ircPort = int.Parse(data["IRC"]["Port"]);
ircNickname = data["IRC"]["Nickname"];
ircRealname = data["IRC"]["Realname"] ?? ircNickname;
ircUsername = data["IRC"]["Username"] ?? ircNickname;
ircPassword = data["IRC"]["Password"];
ircUseSSL = bool.Parse(data["IRC"]["UseSSL"]);
discordBotToken = data["Discord"]["BotToken"];
discordProxy = data["Discord"]["Proxy"];
discordIncludeEdited = bool.Parse(data["Discord"]["IncludeEdited"] ?? "false");
discordToIrcChannelMap = new Dictionary<ulong, string>();
ircToDiscordChannelMap = new Dictionary<string, ulong>();
SectionData channelMappingSection = data.Sections.GetSectionData("ChannelMapping");
if (channelMappingSection != null)
{
foreach (KeyData key in channelMappingSection.Keys)
{
ulong discordChannelId = ulong.Parse(key.KeyName);
string ircChannel = key.Value;
discordToIrcChannelMap[discordChannelId] = ircChannel;
ircToDiscordChannelMap[ircChannel] = discordChannelId;
}
}
discordChannelsMap = new Dictionary<string, IMessageChannel>();
ParseIgnoreUsers(data);
// Create the IRC client and register event handlers
ircClient = new IrcClient
{
Encoding = Encoding.UTF8,
AutoReconnect = true,
AutoRejoin = true,
SendDelay = 300,
UseSsl = ircUseSSL
};
ircClient.OnConnected += IrcClient_OnConnected;
ircClient.OnDisconnected += IrcClient_OnDisconnected;
ircClient.OnChannelMessage += IrcClient_OnChannelMessage;
ircClient.OnChannelNotice += IrcClient_OnChannelNotice;
ircClient.OnChannelAction += IrcClient_OnChannelAction;
ircClient.OnError += IrcClient_OnError;
if (discordProxy != null)
{
HttpClient.DefaultProxy = new WebProxy(discordProxy);
}
// Create the Discord client
discordClient = new DiscordSocketClient(
new DiscordSocketConfig
{
FormatUsersInBidirectionalUnicode = false,
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent,
RestClientProvider = DefaultRestClientProvider.Create(useProxy: discordProxy != null),
WebSocketProvider = DefaultWebSocketProvider.Create(discordProxy != null ? HttpClient.DefaultProxy : null)
}
);
discordClient.MessageReceived += DiscordClient_MessageReceived;
if (discordIncludeEdited)
{
discordClient.MessageUpdated += DiscordClient_MessageUpdated;
}
}
public async Task Start()
{
// Connect to the IRC server and start listening for messages
ircClient.Connect(ircServer, ircPort);
ircClient.Login(ircNickname, ircRealname, 4, ircUsername, ircPassword);
FileIniDataParser parser = new();
IniData data = parser.ReadFile("config.ini");
// Join all the IRC channels specified in the configuration file
SectionData channelMappingSection = data.Sections.GetSectionData("ChannelMapping");
if (channelMappingSection != null)
{
foreach (KeyData channelKey in channelMappingSection.Keys)
{
ircClient.RfcJoin(channelKey.Value);
}
}
// Connect to the Discord bot
await discordClient.LoginAsync(TokenType.Bot, discordBotToken);
await discordClient.StartAsync();
// Map each Discord channel to its corresponding IMessageChannel object
foreach (KeyValuePair<ulong, string> entry in discordToIrcChannelMap)
{
try
{
ulong discordChannelId = entry.Key;
string ircChannel = entry.Value;
IMessageChannel message = await discordClient.Rest.GetChannelAsync(discordChannelId) as IMessageChannel;
discordChannelsMap[ircChannel] = message;
}
catch (Discord.Net.HttpException e) when (e.DiscordCode == DiscordErrorCode.MissingPermissions)
{
// Ignore, bot does not have permissions to view the channel
}
}
ircClient.Listen();
}
public async Task Stop()
{
// Disconnect from the IRC server and stop listening for messages
ircClient.Disconnect();
ircClient.OnChannelMessage -= IrcClient_OnChannelMessage;
ircClient.OnChannelNotice -= IrcClient_OnChannelNotice;
ircClient.OnChannelAction -= IrcClient_OnChannelAction;
ircClient.OnError -= IrcClient_OnError;
// Disconnect from the Discord bot
await discordClient.StopAsync();
await discordClient.LogoutAsync();
}
private void ParseIgnoreUsers(IniData data)
{
SectionData ignoreUsersSection = data.Sections.GetSectionData("IgnoreUsers");
if (ignoreUsersSection != null)
{
KeyData ircIgnoreUsers = ignoreUsersSection.Keys.FirstOrDefault(k => k.KeyName == "IRC");
if (ircIgnoreUsers != null)
{
ircIgnoredUsers = new HashSet<string>(ircIgnoreUsers.Value.Split(',').Select(val => val.Trim()));
ircIgnoredUsers = new HashSet<string>(ircIgnoredUsers.Where(val => !val.StartsWith("/") || !val.EndsWith("/") ||
!Regex.IsMatch("", val.Trim('/'), RegexOptions.IgnoreCase)));
ircIgnoredUsersRegex = new HashSet<Regex>(ircIgnoredUsers.Where(val => val.StartsWith("/") && val.EndsWith("/"))
.Select(val => new Regex(val.Trim('/'), RegexOptions.IgnoreCase)));
}
KeyData discordIgnoreUsers = ignoreUsersSection.Keys.FirstOrDefault(k => k.KeyName == "Discord");
if (discordIgnoreUsers != null)
{
discordIgnoredUsers = new HashSet<string>(discordIgnoreUsers.Value.Split(',').Select(val => val.Trim()));
discordIgnoredUsers = new HashSet<string>(discordIgnoredUsers.Where(val => !val.StartsWith("/") || !val.EndsWith("/") ||
!Regex.IsMatch("", val.Trim('/'), RegexOptions.IgnoreCase)));
discordIgnoredUsersRegex = new HashSet<Regex>(discordIgnoredUsers.Where(val => val.StartsWith("/") && val.EndsWith("/"))
.Select(val => new Regex(val.Trim('/'), RegexOptions.IgnoreCase)));
}
}
}
private async Task DiscordClient_MessageReceived(SocketMessage message)
{
// Ignore messages from the bot itself and ignored users
if (
message.Author.Id == discordClient.CurrentUser.Id ||
discordIgnoredUsers.Contains(message.Author.Username) ||
discordIgnoredUsersRegex.Any(r => r.IsMatch(message.Author.Username))
)
{
return;
}
// Get the IRC channel for the Discord channel, if available
if (discordToIrcChannelMap.TryGetValue(message.Channel.Id, out string ircChannel))
{
// Determine if this message is a reply to another message
string author = $"<{message.Author}>";
if (message.Reference != null)
{
IMessage repliedToMessage = await message.Channel.GetMessageAsync(message.Reference.MessageId.Value);
// If replying to the bot, return the actual user that the reply is for
if (repliedToMessage.Author.Id == discordClient.CurrentUser.Id)
{
string[] parts = repliedToMessage.Content.Split(' ');
if (parts.Length > 1 && parts[0].StartsWith("<") && parts[0].EndsWith(">"))
{
author = $"<{message.Author}, replying to {parts[0][1..^1]}>";
}
}
else
{
author = $"<{message.Author}, replying to {repliedToMessage.Author}>";
}
}
await SendMessageToIrcChannel(ircChannel, DiscordToIrcConverter.Convert(message), author);
}
}
private async Task DiscordClient_MessageUpdated(Cacheable<IMessage, ulong> cacheable, SocketMessage message, ISocketMessageChannel channel)
{
// Ignore messages from the bot itself and ignored users
if (
message.Author.Id == discordClient.CurrentUser.Id ||
discordIgnoredUsers.Contains(message.Author.Username) ||
discordIgnoredUsersRegex.Any(r => r.IsMatch(message.Author.Username))
)
{
return;
}
// Get the IRC channel for the Discord channel, if available
if (discordToIrcChannelMap.TryGetValue(message.Channel.Id, out string ircChannel))
{
// Determine if this message is a reply to another message
string author = $"<{message.Author}>";
if (message.Reference != null)
{
IMessage repliedToMessage = await message.Channel.GetMessageAsync(message.Reference.MessageId.Value);
// If replying to the bot, return the actual user that the reply is for
if (repliedToMessage.Author.Id == discordClient.CurrentUser.Id)
{
string[] parts = repliedToMessage.Content.Split(' ');
if (parts.Length > 1 && parts[0].StartsWith("<") && parts[0].EndsWith(">"))
{
author = $"<{message.Author}, replying to {parts[0][1..^1]}>";
}
}
else
{
author = $"<{message.Author}, replying to {repliedToMessage.Author}>";
}
}
author += " (edited)";
await SendMessageToIrcChannel(ircChannel, DiscordToIrcConverter.Convert(message), author);
}
}
private async Task SendMessageToIrcChannel(string ircChannel, string message, string author)
{
int maxMessageLength = MAX_MESSAGE_LENGTH - author.Length - 5;
string[] chunks = SplitMessage(message, maxMessageLength);
for (int i = 0; i < chunks.Length; i++)
{
string formattedLine = author;
int messageNumber = i + 1;
int totalMessages = chunks.Length;
if (chunks.Length > 1)
{
formattedLine += $" [{messageNumber}/{totalMessages}]";
}
formattedLine += $" {chunks[i]}";
await Task.Run(() => ircClient.SendMessage(SendType.Message, ircChannel, formattedLine));
}
}
private static string[] SplitMessage(string message, int chunkSize)
{
if (message == null || chunkSize <= 0)
{
return new[] { message };
}
List<string> parts = new();
string[] lines = message.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
foreach (string line in lines)
{
if (line.Length == 0 || line.Trim().Length == 0)
{
continue;
}
if (line.Length <= chunkSize)
{
parts.Add(line);
}
else
{
int startIndex = 0;
while (startIndex < line.Length)
{
int length = Math.Min(chunkSize, line.Length - startIndex);
string part = line.Substring(startIndex, length);
parts.Add(part);
startIndex += length;
}
}
}
return parts.ToArray();
}
private void IrcClient_OnConnected(object sender, EventArgs e)
{
// Notify the user that the IRC client has successfully connected
Console.WriteLine($"Connected to {ircServer}:{ircPort}.");
}
private void IrcClient_OnDisconnected(object sender, EventArgs e)
{
// Reconnect to the IRC server
ircClient.Connect(ircServer, ircPort);
ircClient.Login(ircNickname, ircRealname, 4, ircUsername, ircPassword);
FileIniDataParser parser = new();
IniData data = parser.ReadFile("config.ini");
// Join all the IRC channels specified in the configuration file
SectionData channelMappingSection = data.Sections.GetSectionData("ChannelMapping");
if (channelMappingSection != null)
{
foreach (KeyData channelKey in channelMappingSection.Keys)
{
ircClient.RfcJoin(channelKey.Value);
}
}
}
private void IrcClient_OnChannelMessage(object sender, IrcEventArgs e)
{
// Ignore messages from the bot itself and ignored users
if (
e.Data.Nick == ircNickname ||
ircIgnoredUsers.Contains(e.Data.Nick) ||
ircIgnoredUsersRegex.Any(r => r.IsMatch(e.Data.Nick))
)
{
return;
}
// Get the corresponding Discord channel for the IRC channel, if available
if (ircToDiscordChannelMap.TryGetValue(e.Data.Channel, out ulong discordChannelId))
{
// Relay the IRC message to the Discord channel asynchronously
_ = SendMessageToDiscordChannel(discordChannelId.ToString(), $"<{e.Data.Nick}> {IrcToDiscordConverter.BlocksToMarkdown(IrcToDiscordConverter.Parse(e.Data.Message))}");
}
}
private void IrcClient_OnChannelNotice(object sender, IrcEventArgs e)
{
// Ignore messages from the bot itself and ignored users
if (
e.Data.Nick == ircNickname ||
ircIgnoredUsers.Contains(e.Data.Nick) ||
ircIgnoredUsersRegex.Any(r => r.IsMatch(e.Data.Nick))
)
{
return;
}
// Get the corresponding Discord channel for the IRC channel, if available
if (ircToDiscordChannelMap.TryGetValue(e.Data.Channel, out ulong discordChannelId))
{
// Relay the IRC notice message to the Discord channel asynchronously
_ = SendMessageToDiscordChannel(discordChannelId.ToString(), $"<{e.Data.Nick}> NOTICE: {IrcToDiscordConverter.BlocksToMarkdown(IrcToDiscordConverter.Parse(e.Data.Message.Replace("*", "\\*")))}");
}
}
private void IrcClient_OnChannelAction(object sender, ActionEventArgs e)
{
// Ignore messages from the bot itself and ignored users
if (
e.Data.Nick == ircNickname ||
ircIgnoredUsers.Contains(e.Data.Nick) ||
ircIgnoredUsersRegex.Any(r => r.IsMatch(e.Data.Nick))
)
{
return;
}
// Get the corresponding Discord channel for the IRC channel, if available
if (ircToDiscordChannelMap.TryGetValue(e.Data.Channel, out ulong discordChannelId))
{
// Format the action message as an italicized text with the sender's nickname
string formattedMessage = $"_**{e.Data.Nick}** {IrcToDiscordConverter.BlocksToMarkdown(IrcToDiscordConverter.Parse(e.ActionMessage))}_";
// Relay the action message to the Discord channel asynchronously
_ = SendMessageToDiscordChannel(discordChannelId.ToString(), formattedMessage);
}
}
private void IrcClient_OnError(object sender, ErrorEventArgs e)
{
// Log any IRC error messages to the console
Console.WriteLine($"IRC Error: {e.Data.Message}");
}
private async Task SendMessageToDiscordChannel(string discordChannelId, string message)
{
// Get the Discord channel by ID
if (!discordChannelsMap.TryGetValue(discordChannelId, out IMessageChannel messageChannel))
{
Discord.Rest.RestChannel channel = await discordClient.Rest.GetChannelAsync(ulong.Parse(discordChannelId));
messageChannel = channel as IMessageChannel;
discordChannelsMap[discordChannelId] = messageChannel;
}
// Parse mentions
message = await ParseIrcMentions(message, messageChannel);
// Send the message to the Discord channel asynchronously
_ = await messageChannel.SendMessageAsync(message);
}
private static async Task<string> ParseIrcMentions(string message, IMessageChannel channel)
{
_ = await Task.Run(() =>
{
List<string> mentions = message.Split(' ').Where(s => s.StartsWith('@') || s.EndsWith(':')).ToList();
if (!mentions.Any())
{
return message;
}
// Get the current Discord server
IGuild guild = (channel as IGuildChannel)?.Guild;
// Parse mentions to Discord mentions
foreach (string mention in mentions)
{
// Convert to lower case for case-insensitive comparison
string mentionLower = mention.ToLowerInvariant();
// Replace @everyone and @here with escaped versions
if (mentionLower.StartsWith("@everyone") || mentionLower.StartsWith("@here"))
{
message = message.Replace(mention, $"`{mention}`");
continue;
}
// Remove the @, :, or , characters from the mention
string cleanedMention = mentionLower.Trim('@', ':', ',');
// Try to find the user by username
IReadOnlyCollection<IGuildUser> users = guild?.SearchUsersAsync(cleanedMention)?.Result;
// Check if there is at least one matching user
if (users != null && users.Count >= 1)
{
IGuildUser user = users.FirstOrDefault(u => u.Username.ToLowerInvariant() == cleanedMention);
// Replace the mention with a Discord mention if found, or leave it as is otherwise
if (user != null)
{
message = message.Replace(mention, $"<@{user.Id}>");
}
}
}
return message;
});
return message;
}
}
}