forked from discord-csharp/MODiX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLabShortenerModule.cs
More file actions
249 lines (211 loc) · 8.63 KB
/
LabShortenerModule.cs
File metadata and controls
249 lines (211 loc) · 8.63 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
#nullable enable
using System;
using System.Buffers.Text;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Compression;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Discord;
using Discord.Interactions;
using LZStringCSharp;
using Modix.Bot.Responders.AutoRemoveMessages;
using Modix.Services.CommandHelp;
using Modix.Services.Utilities;
using ProtoBuf;
namespace Modix.Bot.Modules
{
[ModuleHelp("LabShortener", "Commands for working with sharplab.io/lab.razor.fyi.")]
public partial class LabShortenerModule : InteractionModuleBase
{
private readonly AutoRemoveMessageService _autoRemoveMessageService;
public LabShortenerModule(AutoRemoveMessageService autoRemoveMessageService)
{
_autoRemoveMessageService = autoRemoveMessageService;
}
[SlashCommand("shorten", "Shortens the provided link.")]
public async Task LinkAsync(
[Summary(description: "The link to shorten.")]
Uri uri)
{
var host = uri.Host;
if (!_allowedHosts.Contains(host))
{
await FollowupAsync(embed: new EmbedBuilder()
.WithColor(Color.Red)
.WithDescription($"Links to {host} cannot be shortened.")
.Build());
return;
}
var urlMarkdown = Format.Url($"{host} (click here)", uri.ToString());
string? preview = null;
var previewSuccess = host switch
{
"sharplab.io" => TryPrepareSharplabPreview(uri.Fragment, urlMarkdown.Length + 1, out preview),
"lab.razor.fyi" => TryPrepareRazorLabPreview(uri.Fragment, urlMarkdown.Length + 1, out preview),
_ => throw new UnreachableException("already checked for other hosts"),
};
var description = previewSuccess
? $"{urlMarkdown}\n{preview}"
: urlMarkdown;
if (description.Length > EmbedBuilder.MaxDescriptionLength)
{
await FollowupAsync("Error: The provided link is too long to be converted to an embed.");
return;
}
var embed = new EmbedBuilder()
.WithDescription(description)
.WithUserAsAuthor(Context.User)
.WithColor(Color.LightGrey);
await _autoRemoveMessageService.RegisterRemovableMessageAsync(Context.User, embed, async e => await FollowupAsync(embed: e.Build()));
}
private static bool TryPrepareSharplabPreview(string fragment, int markdownLength, [NotNullWhen(true)] out string? preview)
{
if (!fragment.StartsWith("#v2:"))
{
preview = null;
return false;
}
try
{
// Decode the compressed code from the URL payload
var base64Text = fragment[4..];
var plainText = LZString.DecompressFromBase64(base64Text);
// Extract the option and get the target language
var textParts = Regex.Match(plainText, @"([^|]*)\|([\s\S]*)$");
var languageOption = Regex.Match(textParts.Groups[1].Value, @"l:(\w+)");
var language = languageOption.Success ? languageOption.Groups[1].Value : "cs";
var sourceCode = textParts.Groups[2].Value;
// Replace the compression tokens
if (language is "cs")
{
sourceCode = ReplaceTokens(sourceCode, _sharplabCSTokens);
// Strip using directives
sourceCode = RemoveUsings(sourceCode);
}
else if (language is "il")
sourceCode = ReplaceTokens(sourceCode, _sharplabILTokens);
else
sourceCode = sourceCode.Replace("@@", "@");
var maxPreviewLength = EmbedBuilder.MaxDescriptionLength - (markdownLength + language.Length + "```\n\n```".Length);
preview = FormatUtilities.FormatCodeForEmbed(language, sourceCode, maxPreviewLength);
return !string.IsNullOrWhiteSpace(preview);
}
catch
{
preview = null;
return false;
}
}
private static bool TryPrepareRazorLabPreview(string fragment, int markdownLength, [NotNullWhen(true)] out string? preview)
{
if (string.IsNullOrWhiteSpace(fragment))
{
preview = null;
return false;
}
try
{
var bytes = Base64Url.DecodeFromChars(fragment.AsSpan(1));
using var deflateStream = new DeflateStream(new MemoryStream(bytes), CompressionMode.Decompress);
var savedState = Serializer.Deserialize<RazorLabSavedState>(deflateStream);
var selectedFile = savedState.Inputs[savedState.SelectedInputIndex];
if (selectedFile.FileExtension != ".cs")
{
preview = null;
return false;
}
var source = RemoveUsings(selectedFile.Text);
var maxPreviewLength = EmbedBuilder.MaxDescriptionLength - (markdownLength + "```cs\n\n```".Length);
preview = FormatUtilities.FormatCodeForEmbed("cs", source, maxPreviewLength);
return !string.IsNullOrWhiteSpace(preview);
}
catch
{
preview = null;
return false;
}
}
[GeneratedRegex(@"using \w+(?:\.\w+)*;")]
private static partial Regex UsingsRegex();
private static string RemoveUsings(string sourceCode) => UsingsRegex().Replace(sourceCode, "");
private static string ReplaceTokens(string sourceCode, ImmutableArray<string> tokens)
{
return Regex.Replace(sourceCode, @"@(\d+|@)", match =>
{
if (match.Value is "@@")
return "@";
return tokens[int.Parse(match.Groups[1].Value)];
});
}
private static readonly ImmutableArray<string> _allowedHosts
= [
"sharplab.io",
"lab.razor.fyi"
];
private static readonly ImmutableArray<string> _sharplabCSTokens
= [
"using",
"System",
"class",
"public",
"void",
"Func",
"Task",
"return",
"async",
"await",
"string",
"yield",
"Action",
"IEnumerable",
"System.Collections.Generic",
"System.Threading.Tasks",
"static",
"Program",
"Main",
"Console.WriteLine",
"", // <help.run.csharp>
"using System;",
"public static void Main()",
"public static class Program",
"Inspect.Allocations(() =>",
"Inspect.MemoryGraph("
];
private static readonly ImmutableArray<string> _sharplabILTokens
= [
"Main ()",
"Program",
"ConsoleApp",
"cil managed",
".entrypoint",
".maxstack",
".assembly",
".class public auto ansi abstract sealed beforefieldinit",
"extends System.Object",
".method public hidebysig",
"call void [System.Console]System.Console::WriteLine("
];
}
// the below definitions were taken from https://github.com/jjonescz/DotNetLab at commit dedcefec241a1d32fe8a6683ccaa39ff40dc1730
// as such they are licensed under the MIT license in that repo, https://github.com/jjonescz/DotNetLab/blob/dedcefec241a1d32fe8a6683ccaa39ff40dc1730/LICENSE
[ProtoContract]
sealed file record RazorLabInputCode
{
[ProtoMember(1)]
public required string FileName { get; init; }
[ProtoMember(2)]
public required string Text { get; init; }
public string FileExtension => Path.GetExtension(FileName);
}
[ProtoContract]
sealed file record RazorLabSavedState
{
[ProtoMember(1)]
public ImmutableArray<RazorLabInputCode> Inputs { get; init; }
[ProtoMember(8)]
public int SelectedInputIndex { get; init; }
}
}