fish-station/Content.Server/Database/ServerBanDef.cs
VigersRay 0441b8b814 Merge remote-tracking branch 'refs/remotes/wizards/master'
# Conflicts:
#	Content.Client/Launcher/LauncherConnecting.cs
#	Content.Client/Launcher/LauncherConnectingGui.xaml
#	Content.Client/Launcher/LauncherConnectingGui.xaml.cs
#	Content.Server/Administration/Managers/BanManager.cs
#	Content.Server/Medical/Components/HealthAnalyzerComponent.cs
#	Content.Server/Medical/HealthAnalyzerSystem.cs
#	Content.Server/Nutrition/EntitySystems/CreamPieSystem.cs
#	Content.Server/StationEvents/Events/RandomSentienceRule.cs
#	Content.Shared/Interaction/Components/BlockMovementComponent.cs
#	Content.Shared/Interaction/SharedInteractionSystem.Blocking.cs
#	Content.Shared/Materials/SharedMaterialReclaimerSystem.cs
#	Resources/Prototypes/Accents/word_replacements.yml
#	Resources/Prototypes/Catalog/Fills/Books/bookshelf.yml
#	Resources/Prototypes/Entities/Mobs/Customization/Markings/cat_parts.yml
#	Resources/Prototypes/Entities/Mobs/NPCs/silicon.yml
#	Resources/Prototypes/Entities/Objects/Weapons/Guns/Projectiles/projectiles.yml
#	Resources/Prototypes/Entities/Structures/Machines/lathe.yml
#	Resources/Prototypes/Objectives/objectiveGroups.yml
#	Resources/Prototypes/Objectives/stealTargetGroups.yml
#	Resources/Prototypes/Roles/Jobs/Security/detective.yml
#	Resources/Prototypes/Species/human.yml
#	Resources/Prototypes/Voice/speech_emotes.yml
#	Resources/ServerInfo/Guidebook/Antagonist/Antagonists.xml
#	Resources/ServerInfo/Guidebook/Antagonist/MinorAntagonists.xml
#	Resources/ServerInfo/Guidebook/Antagonist/Nuclear Operatives.xml
#	Resources/ServerInfo/Guidebook/Antagonist/Revolutionaries.xml
#	Resources/ServerInfo/Guidebook/Antagonist/SpaceNinja.xml
#	Resources/ServerInfo/Guidebook/Antagonist/Traitors.xml
#	Resources/ServerInfo/Guidebook/Antagonist/Zombies.xml
#	Resources/migration.yml
2024-08-22 06:50:04 +03:00

95 lines
3.4 KiB
C#

using System.Collections.Immutable;
using System.Net;
using Content.Shared.CCVar;
using Content.Shared.Database;
using Robust.Shared.Configuration;
using Robust.Shared.Network;
namespace Content.Server.Database
{
public sealed class ServerBanDef
{
public int? Id { get; }
public NetUserId? UserId { get; }
public (IPAddress address, int cidrMask)? Address { get; }
public ImmutableArray<byte>? HWId { get; }
public DateTimeOffset BanTime { get; }
public DateTimeOffset? ExpirationTime { get; }
public int? RoundId { get; }
public TimeSpan PlaytimeAtNote { get; }
public string Reason { get; }
public NoteSeverity Severity { get; set; }
public NetUserId? BanningAdmin { get; }
public ServerUnbanDef? Unban { get; }
public ServerBanExemptFlags ExemptFlags { get; }
public ServerBanDef(int? id,
NetUserId? userId,
(IPAddress, int)? address,
ImmutableArray<byte>? hwId,
DateTimeOffset banTime,
DateTimeOffset? expirationTime,
int? roundId,
TimeSpan playtimeAtNote,
string reason,
NoteSeverity severity,
NetUserId? banningAdmin,
ServerUnbanDef? unban,
ServerBanExemptFlags exemptFlags = default)
{
if (userId == null && address == null && hwId == null)
{
throw new ArgumentException("Must have at least one of banned user, banned address or hardware ID");
}
if (address is {} addr && addr.Item1.IsIPv4MappedToIPv6)
{
// Fix IPv6-mapped IPv4 addresses
// So that IPv4 addresses are consistent between separate-socket and dual-stack socket modes.
address = (addr.Item1.MapToIPv4(), addr.Item2 - 96);
}
Id = id;
UserId = userId;
Address = address;
HWId = hwId;
BanTime = banTime;
ExpirationTime = expirationTime;
RoundId = roundId;
PlaytimeAtNote = playtimeAtNote;
Reason = reason;
Severity = severity;
BanningAdmin = banningAdmin;
Unban = unban;
ExemptFlags = exemptFlags;
}
public string FormatBanMessage(IConfigurationManager cfg, ILocalizationManager loc)
{
string expires;
if (ExpirationTime is { } expireTime)
{
var duration = expireTime - BanTime;
var utc = expireTime.ToUniversalTime();
expires = loc.GetString("ban-expires", ("duration", duration.TotalMinutes.ToString("N0")), ("time", utc.ToString("f")));
}
else
{
var appeal = cfg.GetCVar(CCVars.InfoLinksAppeal);
expires = !string.IsNullOrWhiteSpace(appeal)
? loc.GetString("ban-banned-permanent-appeal", ("link", appeal))
: loc.GetString("ban-banned-permanent");
}
return $"""
{loc.GetString("ban-banned-1")}
{loc.GetString("ban-banned-2", ("id", Id.ToString() ?? ""))}
{loc.GetString("ban-banned-3", ("reason", Reason))}
{expires}
{loc.GetString("ban-banned-4")}
""";
}
}
}