Skip to content

Commit bfba5f0

Browse files
committed
add tclip mode to hastebin handler
1 parent a37fc28 commit bfba5f0

File tree

6 files changed

+96
-16
lines changed

6 files changed

+96
-16
lines changed

Helpers/HasteBinClient.cs

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
1-
// https://gist.github.com/b5e14d0c36f5a972060655b1aa875dbf
2-
// CODE HERE COMES FROM ABOVE GIST. MAY BE MODIFIED.
3-
// All rights belong to original creator, and not the author of this software.
1+
// Based on https://gist.github.com/b5e14d0c36f5a972060655b1aa875dbf
42

53
namespace Cliptok.Helpers
64
{
75
public class HasteBinClient
86
{
97
private static readonly HttpClient _httpClient;
108
private readonly string _baseUrl;
9+
public readonly string _hasteType = "haste";
1110

1211
static HasteBinClient()
1312
{
1413
_httpClient = new HttpClient();
1514
}
1615

17-
public HasteBinClient(string baseUrl)
16+
public HasteBinClient(string baseUrl, string hasteType = "haste")
1817
{
1918
_baseUrl = baseUrl;
19+
_hasteType = hasteType;
2020
}
2121

22-
public async Task<HasteBinResult> Post(string content)
22+
public async Task<HasteBinResult> PostAsync(string content, string language = default)
23+
{
24+
switch (_hasteType)
25+
{
26+
case "haste":
27+
return await PostHastebinAsync(content, language);
28+
case "tclip":
29+
return await PostTclipAsync(content, language);
30+
default:
31+
throw new NotSupportedException($"Haste type '{_hasteType}' is not supported.");
32+
}
33+
}
34+
35+
public async Task<HasteBinResult> PostHastebinAsync(string content, string language)
2336
{
2437
string fullUrl = _baseUrl;
2538
if (!fullUrl.EndsWith("/"))
@@ -44,6 +57,12 @@ public async Task<HasteBinResult> Post(string content)
4457
hasteBinResult.FullUrl = $"{fullUrl}{hasteBinResult.Key}";
4558
hasteBinResult.IsSuccess = true;
4659
hasteBinResult.StatusCode = 200;
60+
hasteBinResult.RawUrl = $"{fullUrl}raw/{hasteBinResult.Key}";
61+
62+
if (language != default)
63+
{
64+
hasteBinResult.FullUrl = $"{hasteBinResult.FullUrl}.{language}";
65+
}
4766
return hasteBinResult;
4867
}
4968
}
@@ -55,13 +74,69 @@ public async Task<HasteBinResult> Post(string content)
5574
StatusCode = (int)result.StatusCode
5675
};
5776
}
77+
78+
public async Task<HasteBinResult> PostTclipAsync(string content, string language)
79+
{
80+
string fullUrl = _baseUrl;
81+
if (!fullUrl.EndsWith("/"))
82+
{
83+
fullUrl += "/";
84+
}
85+
string postUrl = $"{fullUrl}api/post";
86+
if (language == default)
87+
language = "txt";
88+
89+
var formdata = new MultipartFormDataContent
90+
{
91+
{ new StringContent(content), "content"},
92+
{ new StringContent(Program.discord.CurrentUser.Username + "." + (language)), "filename" }
93+
};
94+
//formdata.Add(new StringContent(Program.discord.CurrentUser.Username) + "." + language), "filename")
95+
96+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri(postUrl))
97+
{
98+
Content = formdata,
99+
Headers = { { "Accept", "text/plain" } }
100+
};
101+
102+
HttpResponseMessage result = await _httpClient.SendAsync(request);
103+
104+
if (result.StatusCode == HttpStatusCode.OK)
105+
{
106+
var responseText = await result.Content.ReadAsStringAsync();
107+
HasteBinResult hasteBinResult = new HasteBinResult
108+
{
109+
FullUrl = responseText,
110+
RawUrl = responseText + "/raw",
111+
IsSuccess = true,
112+
StatusCode = 200
113+
};
114+
return hasteBinResult;
115+
}
116+
117+
return new HasteBinResult()
118+
{
119+
FullUrl = fullUrl,
120+
IsSuccess = false,
121+
StatusCode = (int)result.StatusCode
122+
};
123+
}
124+
125+
}
126+
127+
public class TclipRequest
128+
{
129+
[JsonProperty("content")]
130+
public string Content { get; set; }
131+
[JsonProperty("filename")]
132+
public string Filename { get; set; }
58133
}
59134

60-
// Define other methods and classes here
61135
public class HasteBinResult
62136
{
63-
public string Key { get; set; }
137+
public string Key { get; set; } = null;
64138
public string FullUrl { get; set; }
139+
public string RawUrl { get; set; }
65140
public bool IsSuccess { get; set; }
66141
public int StatusCode { get; set; }
67142
}

Helpers/LogChannelHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,11 @@ public static async Task<DiscordMessageBuilder> DumpMessageFromStringAsync(strin
172172
var stream = new MemoryStream(Encoding.UTF8.GetBytes(messageLog));
173173
var msg = new DiscordMessageBuilder().WithContent(content).AddFile("messages.txt", stream);
174174

175-
var hasteResult = await Program.hasteUploader.Post(messageLog);
175+
var hasteResult = await Program.hasteUploader.PostAsync(messageLog);
176176

177177
if (hasteResult.IsSuccess)
178178
{
179-
msg.AddEmbed(new DiscordEmbedBuilder().WithDescription($"[`📄 View online`]({Program.cfgjson.HastebinEndpoint}/raw/{hasteResult.Key})"));
179+
msg.AddEmbed(new DiscordEmbedBuilder().WithDescription($"[`📄 View online`]({hasteResult.RawUrl})"));
180180
}
181181

182182
return msg;

Helpers/StringHelpers.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,10 @@ public static async Task<string> CodeOrHasteBinAsync(string input, string langua
3434
bool inputHasCodeBlock = input.Contains("```");
3535
if (input.Length > charLimit || inputHasCodeBlock)
3636
{
37-
HasteBinResult hasteResult = await Program.hasteUploader.Post(input);
37+
HasteBinResult hasteResult = await Program.hasteUploader.PostAsync(input, language);
3838
if (hasteResult.IsSuccess)
3939
{
4040
var hasteUrl = hasteResult.FullUrl;
41-
if (language != "")
42-
{
43-
hasteUrl = hasteUrl + "." + language;
44-
}
4541

4642
if (plain)
4743
return hasteUrl;

Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static async Task Main(string[] _)
143143

144144
Log.Logger = loggerConfig.CreateLogger();
145145

146-
hasteUploader = new HasteBinClient(cfgjson.HastebinEndpoint);
146+
hasteUploader = new HasteBinClient(cfgjson.HastebinEndpoint, cfgjson.HastebinType);
147147

148148
UpdateLists();
149149

@@ -180,7 +180,11 @@ static async Task Main(string[] _)
180180
{
181181
// create db context that we can use
182182
dbContext = new CliptokDbContext();
183-
dbContext.Database.Migrate();
183+
var pendingMigrations = await dbContext.Database.GetPendingMigrationsAsync();
184+
if (pendingMigrations.Any())
185+
{
186+
await dbContext.Database.MigrateAsync();
187+
}
184188
}
185189

186190
DiscordClientBuilder discordBuilder = DiscordClientBuilder.CreateDefault(token, DiscordIntents.All);

Types/Config.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ public ulong InsidersChannel
291291

292292
[JsonProperty("noFun")]
293293
public bool NoFun { get; set; } = false;
294+
295+
[JsonProperty("hastebinType")]
296+
public string HastebinType { get; set; } = "haste";
297+
294298
}
295299

296300
public class WorkflowConfig

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
"autoDehoistCharacters": " !\"#$%&'()*+,-./:;<=>?@[\\]^_`'",
230230
"secondaryAutoDehoistCharacters": "0123456789",
231231
"hastebinEndpoint": "https://haste.erisa.uk",
232+
"hastebinType": "haste",
232233
"userRoles": {
233234
"insiderCanary": 434426594271166474,
234235
"insiderDev": 1082364328474329179,

0 commit comments

Comments
 (0)