-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIrcToDiscordConverter.cs
289 lines (255 loc) · 9.02 KB
/
IrcToDiscordConverter.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
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace IrcDiscordRelay
{
internal static class IrcToDiscordConverter
{
private const char CharBold = '\x02';
private const char CharMonospace = '\x11';
private const char CharItalics = '\x1D';
private const char CharStrikethrough = '\x1E';
private const char CharUnderline = '\x1F';
private const char CharColor = '\x03';
private const char CharReverseColor = '\x16';
private const char CharReset = '\x0F';
private static readonly Regex colorRegex = new("\x03(\\d\\d?)?(?:,(\\d\\d?))?");
public static List<Block> Parse(string text)
{
List<Block> result = new();
Block prev = Block.Empty;
int startIndex = 0;
Dictionary<int, Color> indexToColor = GetIndexToColorMap(text);
// Append a resetter to simplify code a bit
text += CharReset;
for (int i = 0; i < text.Length; i++)
{
Block current = prev;
bool updated = true;
int nextStart = -1;
char ch = text[i];
switch (ch)
{
case CharBold:
case CharMonospace:
case CharItalics:
case CharStrikethrough:
case CharUnderline:
current.SetField(ch, !prev.GetField(ch));
break;
case CharColor:
Color color = indexToColor[i];
current.Foreground = color.foreground;
current.Background = color.background;
nextStart = i + color.strSize;
break;
case CharReverseColor:
if (prev.Foreground != -1)
{
current.Foreground = prev.Background;
current.Background = prev.Foreground;
if (current.Foreground == -1)
{
current.Foreground = 0;
}
}
current.Reverse = !prev.Reverse;
break;
case CharReset:
current = Block.Empty;
break;
default:
updated = false;
break;
}
if (updated)
{
prev.Text = text[startIndex..i];
startIndex = nextStart != -1 ? nextStart : i + 1;
if (prev.Text.Length > 0)
{
result.Add(prev);
}
prev = current;
}
}
return result;
}
public static string BlocksToMarkdown(List<Block> blocks)
{
string mdText = "";
for (int i = 0; i < blocks.Count + 1; i++)
{
// Default to unstyled blocks when index out of range
Block block = Block.Empty;
if (i < blocks.Count)
{
block = blocks[i];
}
Block prevBlock = Block.Empty;
if (i > 0)
{
prevBlock = blocks[i - 1];
}
// Consider reverse as italic, some IRC clients use that
bool prevItalic = prevBlock.Italic || prevBlock.Reverse;
bool italic = block.Italic || block.Reverse;
// If foreground == background, then spoiler
bool prevSpoiler = prevBlock.Foreground != -1 && prevBlock.Foreground == prevBlock.Background;
bool spoiler = block.Foreground != -1 && block.Foreground == block.Background;
// Add start markers when style turns from false to true
if (!prevItalic && italic)
{
mdText += "*";
}
if (!prevBlock.Bold && block.Bold)
{
mdText += "**";
}
if (!prevBlock.Underline && block.Underline)
{
mdText += "__";
}
if (!prevBlock.Strikethrough && block.Strikethrough)
{
mdText += "~~";
}
if (!prevBlock.Monospace && block.Monospace)
{
mdText += "`";
}
// NOTE: non-standard discord spoilers
if (!prevSpoiler && spoiler)
{
mdText += "||";
}
// Add end markers when style turns from true to false
// (and apply in reverse order to maintain nesting)
if (prevBlock.Monospace && !block.Monospace)
{
mdText += "`";
}
if (prevBlock.Strikethrough && !block.Strikethrough)
{
mdText += "~~";
}
if (prevBlock.Underline && !block.Underline)
{
mdText += "__";
}
if (prevBlock.Bold && !block.Bold)
{
mdText += "**";
}
if (prevItalic && !italic)
{
mdText += "*";
}
// NOTE: non-standard discord spoilers
if (prevSpoiler && !spoiler)
{
mdText += "||";
}
mdText += block.Text;
}
return mdText;
}
private static Dictionary<int, Color> GetIndexToColorMap(string text)
{
Dictionary<int, Color> indexToColor = new();
MatchCollection matches = colorRegex.Matches(text);
foreach (Match match in matches.Cast<Match>())
{
// The index where the entire colour submatch starts/ends
int startIndex = match.Index;
int endIndex = startIndex + match.Length;
Color c = new()
{
foreground = -1,
background = -1,
strSize = endIndex - startIndex,
};
// Errors are impossible, our regex only matches numbers
if (match.Groups[1].Success)
{
c.foreground = int.Parse(match.Groups[1].Value);
}
if (match.Groups[2].Success)
{
c.background = int.Parse(match.Groups[2].Value);
}
indexToColor[startIndex] = c;
}
return indexToColor;
}
private struct Color
{
public int foreground;
public int background;
public int strSize;
}
}
public struct Block
{
private const char CharBold = '\x02';
private const char CharMonospace = '\x11';
private const char CharItalics = '\x1D';
private const char CharStrikethrough = '\x1E';
private const char CharUnderline = '\x1F';
public static readonly Block Empty = new("");
public string Text;
public bool Bold;
public bool Monospace;
public bool Italic;
public bool Strikethrough;
public bool Underline;
public bool Reverse;
public int Foreground;
public int Background;
public Block(string text)
{
Text = text;
Bold = false;
Monospace = false;
Italic = false;
Strikethrough = false;
Underline = false;
Reverse = false;
Foreground = -1;
Background = -1;
}
public void SetField(char key, bool value)
{
switch (key)
{
case CharBold:
Bold = value;
break;
case CharMonospace:
Monospace = value;
break;
case CharItalics:
Italic = value;
break;
case CharStrikethrough:
Strikethrough = value;
break;
case CharUnderline:
Underline = value;
break;
}
}
public bool GetField(char key)
{
return key switch
{
CharBold => Bold,
CharMonospace => Monospace,
CharItalics => Italic,
CharStrikethrough => Strikethrough,
CharUnderline => Underline,
_ => false,
};
}
}
}