Increase webhook name limit to 80

This commit is contained in:
Ske
2019-08-14 07:16:48 +02:00
parent 5ef8a9303d
commit 74e0508065
7 changed files with 476 additions and 457 deletions

View File

@@ -134,18 +134,16 @@ namespace PluralKit.Bot
private IServiceProvider _services; private IServiceProvider _services;
private DiscordShardedClient _client; private DiscordShardedClient _client;
private CommandService _commands; private CommandService _commands;
private ProxyService _proxy;
private Timer _updateTimer; private Timer _updateTimer;
private IMetrics _metrics; private IMetrics _metrics;
private PeriodicStatCollector _collector; private PeriodicStatCollector _collector;
private ILogger _logger; private ILogger _logger;
public Bot(IServiceProvider services, IDiscordClient client, CommandService commands, ProxyService proxy, IMetrics metrics, PeriodicStatCollector collector, ILogger logger) public Bot(IServiceProvider services, IDiscordClient client, CommandService commands, IMetrics metrics, PeriodicStatCollector collector, ILogger logger)
{ {
_services = services; _services = services;
_client = client as DiscordShardedClient; _client = client as DiscordShardedClient;
_commands = commands; _commands = commands;
_proxy = proxy;
_metrics = metrics; _metrics = metrics;
_collector = collector; _collector = collector;
_logger = logger.ForContext<Bot>(); _logger = logger.ForContext<Bot>();
@@ -367,7 +365,14 @@ namespace PluralKit.Bot
else else
{ {
// If not, try proxying anyway // If not, try proxying anyway
await _proxy.HandleMessageAsync(arg); try
{
await _proxy.HandleMessageAsync(arg);
}
catch (PKError e)
{
await msg.Channel.SendMessageAsync($"{Emojis.Error} {e.Message}");
}
} }
} }

View File

@@ -89,7 +89,7 @@ namespace PluralKit.Bot.Commands
if (unproxyableMembers.Count > 0) if (unproxyableMembers.Count > 0)
{ {
var msg = await Context.Channel.SendMessageAsync( var msg = await Context.Channel.SendMessageAsync(
$"{Emojis.Warn} Changing your system tag to '{newTag}' will result in the following members being unproxyable, since the tag would bring their name over 32 characters:\n**{string.Join(", ", unproxyableMembers.Select((m) => m.Name))}**\nDo you want to continue anyway?"); $"{Emojis.Warn} Changing your system tag to '{newTag}' will result in the following members being unproxyable, since the tag would bring their name over {Limits.MaxProxyNameLength} characters:\n**{string.Join(", ", unproxyableMembers.Select((m) => m.Name))}**\nDo you want to continue anyway?");
if (!await Context.PromptYesNo(msg)) throw new PKError("Tag change cancelled."); if (!await Context.PromptYesNo(msg)) throw new PKError("Tag change cancelled.");
} }
} }

View File

@@ -76,5 +76,7 @@ namespace PluralKit.Bot {
public static PKError DisplayNameTooLong(string displayName, int maxLength) => new PKError( public static PKError DisplayNameTooLong(string displayName, int maxLength) => new PKError(
$"Display name too long ({displayName.Length} > {maxLength} characters). Use a shorter display name, or shorten your system tag."); $"Display name too long ({displayName.Length} > {maxLength} characters). Use a shorter display name, or shorten your system tag.");
public static PKError ProxyNameTooShort(string name) => new PKError($"The webhook's name, `{name}`, is shorter than two characters, and thus cannot be proxied. Please change the member name or use a longer system tag.");
public static PKError ProxyNameTooLong(string name) => new PKError($"The webhook's name, `{name}`, is too long ({name.Length} > {Limits.MaxProxyNameLength} characters), and thus cannot be proxied. Please change the member name or use a shorter system tag.");
} }
} }

View File

@@ -2,15 +2,14 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
using App.Metrics;
using Dapper;
using Discord; using Discord;
using Discord.Net; using Discord.Net;
using Discord.Webhook;
using Discord.WebSocket; using Discord.WebSocket;
using Microsoft.Extensions.Caching.Memory;
using PluralKit.Core;
using Serilog; using Serilog;
namespace PluralKit.Bot namespace PluralKit.Bot
@@ -100,6 +99,10 @@ namespace PluralKit.Bot
var proxyName = match.Member.ProxyName(match.System.Tag); var proxyName = match.Member.ProxyName(match.System.Tag);
var avatarUrl = match.Member.AvatarUrl ?? match.System.AvatarUrl; var avatarUrl = match.Member.AvatarUrl ?? match.System.AvatarUrl;
// If the name's too long (or short), bail
if (proxyName.Length < 2) throw Errors.ProxyNameTooShort(proxyName);
if (proxyName.Length > Limits.MaxProxyNameLength) throw Errors.ProxyNameTooLong(proxyName);
// Sanitize @everyone, but only if the original user wouldn't have permission to // Sanitize @everyone, but only if the original user wouldn't have permission to
var messageContents = SanitizeEveryoneMaybe(message, match.InnerText); var messageContents = SanitizeEveryoneMaybe(message, match.InnerText);
@@ -143,6 +146,7 @@ namespace PluralKit.Bot
if (!permissions.ManageWebhooks) if (!permissions.ManageWebhooks)
{ {
// todo: PKError-ify these
await channel.SendMessageAsync( await channel.SendMessageAsync(
$"{Emojis.Error} PluralKit does not have the *Manage Webhooks* permission in this channel, and thus cannot proxy messages. Please contact a server administrator to remedy this."); $"{Emojis.Error} PluralKit does not have the *Manage Webhooks* permission in this channel, and thus cannot proxy messages. Please contact a server administrator to remedy this.");
return false; return false;

View File

@@ -1,9 +1,12 @@
namespace PluralKit.Core { namespace PluralKit.Core {
public static class Limits { public static class Limits
{
public static readonly int MaxProxyNameLength = 80;
public static readonly int MaxSystemNameLength = 100; public static readonly int MaxSystemNameLength = 100;
public static readonly int MaxSystemTagLength = 31; public static readonly int MaxSystemTagLength = MaxProxyNameLength - 1;
public static readonly int MaxDescriptionLength = 1000; public static readonly int MaxDescriptionLength = 1000;
public static readonly int MaxMemberNameLength = 50; public static readonly int MaxMemberNameLength = 100; // Fair bit larger than MaxProxyNameLength for bookkeeping
public static readonly int MaxPronounsLength = 100; public static readonly int MaxPronounsLength = 100;
public static readonly long AvatarFileSizeLimit = 1024 * 1024; public static readonly long AvatarFileSizeLimit = 1024 * 1024;

View File

@@ -3,6 +3,8 @@ using Newtonsoft.Json;
using NodaTime; using NodaTime;
using NodaTime.Text; using NodaTime.Text;
using PluralKit.Core;
namespace PluralKit namespace PluralKit
{ {
public class PKSystem public class PKSystem
@@ -18,7 +20,7 @@ namespace PluralKit
[JsonProperty("created")] public Instant Created { get; set; } [JsonProperty("created")] public Instant Created { get; set; }
[JsonProperty("tz")] public string UiTz { get; set; } [JsonProperty("tz")] public string UiTz { get; set; }
[JsonIgnore] public int MaxMemberNameLength => Tag != null ? 32 - Tag.Length - 1 : 32; [JsonIgnore] public int MaxMemberNameLength => Tag != null ? Limits.MaxProxyNameLength - Tag.Length - 1 : Limits.MaxProxyNameLength;
[JsonIgnore] public DateTimeZone Zone => DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz); [JsonIgnore] public DateTimeZone Zone => DateTimeZoneProviders.Tzdb.GetZoneOrNull(UiTz);
} }

View File

@@ -4,6 +4,9 @@ using System.Threading.Tasks;
using App.Metrics.Logging; using App.Metrics.Logging;
using Dapper; using Dapper;
using NodaTime; using NodaTime;
using PluralKit.Core;
using Serilog; using Serilog;
namespace PluralKit { namespace PluralKit {
@@ -139,7 +142,7 @@ namespace PluralKit {
return (await GetBySystem(system)) return (await GetBySystem(system))
.Where((m) => { .Where((m) => {
var proxiedName = $"{m.Name} {system.Tag}"; var proxiedName = $"{m.Name} {system.Tag}";
return proxiedName.Length > 32 || proxiedName.Length < 2; return proxiedName.Length > Limits.MaxProxyNameLength || proxiedName.Length < 2;
}).ToList(); }).ToList();
} }