-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
173 lines (169 loc) · 7.63 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DenizenPastingWebsite.Highlighters;
using FreneticUtilities.FreneticToolkit;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using DenizenPastingWebsite.Utilities;
using DenizenPastingWebsite.Pasting;
namespace DenizenPastingWebsite
{
public class Program
{
public static IHost CurrentHost;
public static void Main(string[] args)
{
SpecialTools.Internationalize();
CancellationTokenSource cancel = new();
Task consoleThread = Task.Run(RunConsole, cancel.Token);
CurrentHost = CreateHostBuilder(args).Build();
CurrentHost.Run();
cancel.Cancel();
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
public static async void RunConsole()
{
while (true)
{
string line = await Console.In.ReadLineAsync();
if (line == null)
{
return;
}
string[] split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
if (split.Length == 0)
{
continue;
}
switch (split[0])
{
case "remove_bot_post":
{
if (split.Length == 2 && long.TryParse(split[1], out long pasteId) && PasteDatabase.TryGetPaste(pasteId, out Paste paste))
{
paste.Type = "text";
paste.Title = "REMOVED BOT POST";
if (string.IsNullOrWhiteSpace(paste.HistoricalContent))
{
paste.HistoricalContent = paste.Title + "\n\n" + paste.Raw;
}
paste.Raw = "Bot post removed from view.";
paste.Formatted = HighlighterCore.HighlightPlainText("Bot post removed from view.");
PasteDatabase.SubmitPaste(paste);
Console.WriteLine($"paste {pasteId} removed");
}
else
{
Console.WriteLine("remove_bot_post (ID HERE)");
}
}
break;
case "issue_451_takedown":
{
if (split.Length > 2 && long.TryParse(split[1], out long pasteId) && PasteDatabase.TryGetPaste(pasteId, out Paste paste))
{
paste.TakedownFrom = string.Join(' ', split[2..]);
PasteDatabase.SubmitPaste(paste);
Console.WriteLine($"paste {pasteId} removed");
}
else
{
Console.WriteLine("issue_451_takedown (ID HERE) (ISSUING PARTY HERE)");
}
}
break;
case "rerender_type":
{
if (split.Length == 2 && PasteType.ValidPasteTypes.TryGetValue(split[1], out PasteType type))
{
foreach (Paste paste in PasteDatabase.Internal.PasteCollection.Find(p => p.Type == type.Name))
{
try
{
Console.WriteLine($"Rerender paste {paste.ID}...");
PasteDatabase.FillPaste(paste);
string origFormat = paste.Formatted;
paste.Formatted = type.Highlight(paste.Raw);
if (origFormat.TrimEnd() != paste.Formatted.TrimEnd())
{
Console.WriteLine($"Updating paste {paste.ID} (was {origFormat.Length} now {paste.Formatted.Length})...");
PasteDatabase.SubmitPaste(paste);
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to rerender paste {paste.ID}: {ex}");
}
}
Console.WriteLine("Rerender done.");
}
else
{
Console.WriteLine("rerender_type (TYPE HERE)");
}
}
break;
case "resubmit_all":
{
int count = 0;
long cap = PasteDatabase.GetTotalPasteCount();
for (long i = 0; i < cap; i++)
{
try
{
if (PasteDatabase.TryGetPaste(i, out Paste paste))
{
if (count % 1000 == 0)
{
Console.WriteLine($"Resubmitted {count} pastes thus far...");
PasteDatabase.Internal.DB.Checkpoint();
}
count++;
PasteDatabase.SubmitPaste(paste);
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to resubmit paste {i}: {ex}");
}
}
Console.WriteLine($"Resubmitting done, {count} total.");
}
break;
case "flush":
{
PasteDatabase.Internal.DB.Checkpoint();
Console.WriteLine("Flushed.");
}
break;
case "rebuild":
{
PasteDatabase.Internal.DB.Rebuild();
Console.WriteLine("Rebuild complete.");
}
break;
case "stop":
{
PasteDatabase.Shutdown();
CurrentHost.StopAsync().Wait();
return;
}
default:
Console.WriteLine("Unknown command. Use 'remove_bot_post', 'rerender_type', or 'stop'");
break;
}
}
}
}
}