IW4M-Admin/Plugins/Welcome/Plugin.cs

125 lines
4.5 KiB
C#
Raw Permalink Normal View History

using System;
using System.Threading.Tasks;
using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
using SharedLibraryCore.Database.Models;
using System.Linq;
using Microsoft.EntityFrameworkCore;
2022-01-26 11:32:16 -05:00
using System.Net.Http;
2023-02-11 21:56:52 -05:00
using System.Threading;
using Newtonsoft.Json.Linq;
using Humanizer;
using Data.Abstractions;
using Data.Models;
2023-02-11 21:56:52 -05:00
using Microsoft.Extensions.DependencyInjection;
using SharedLibraryCore.Events.Management;
using SharedLibraryCore.Interfaces.Events;
using static Data.Models.Client.EFClient;
2023-02-11 21:56:52 -05:00
namespace IW4MAdmin.Plugins.Welcome;
public class Plugin : IPluginV2
{
2023-02-11 21:56:52 -05:00
public string Author => "RaidMax";
public string Version => "1.1";
public string Name => "Welcome Plugin";
2023-02-11 21:56:52 -05:00
public static void RegisterDependencies(IServiceCollection serviceCollection)
{
serviceCollection.AddConfiguration<WelcomeConfiguration>("WelcomePluginSettings");
}
2023-02-11 21:56:52 -05:00
private readonly WelcomeConfiguration _configuration;
private readonly IDatabaseContextFactory _contextFactory;
2023-02-11 21:56:52 -05:00
public Plugin(WelcomeConfiguration configuration, IDatabaseContextFactory contextFactory)
{
_configuration = configuration;
_contextFactory = contextFactory;
2023-02-11 21:56:52 -05:00
IManagementEventSubscriptions.ClientStateAuthorized += OnClientStateAuthorized;
}
2023-02-11 21:56:52 -05:00
private async Task OnClientStateAuthorized(ClientStateEvent clientState, CancellationToken token)
{
var newPlayer = clientState.Client;
2023-02-11 21:56:52 -05:00
if (newPlayer.Level >= Permission.Trusted && !newPlayer.Masked ||
!string.IsNullOrEmpty(newPlayer.Tag) &&
newPlayer.Level != Permission.Flagged && newPlayer.Level != Permission.Banned &&
!newPlayer.Masked)
newPlayer.CurrentServer.Broadcast(
await ProcessAnnouncement(_configuration.PrivilegedAnnouncementMessage,
newPlayer));
2023-02-11 21:56:52 -05:00
newPlayer.Tell(await ProcessAnnouncement(_configuration.UserWelcomeMessage, newPlayer));
2023-02-11 21:56:52 -05:00
if (newPlayer.Level == Permission.Flagged)
{
2023-02-11 21:56:52 -05:00
string penaltyReason;
await using var context = _contextFactory.CreateContext(false);
{
2023-02-11 21:56:52 -05:00
penaltyReason = await context.Penalties
.Where(p => p.OffenderId == newPlayer.ClientId && p.Type == EFPenalty.PenaltyType.Flag)
.OrderByDescending(p => p.When)
.Select(p => p.AutomatedOffense ?? p.Offense)
.FirstOrDefaultAsync(cancellationToken: token);
}
2023-02-11 21:56:52 -05:00
newPlayer.CurrentServer.ToAdmins(Utilities.CurrentLocalization
.LocalizationIndex["PLUGINS_WELCOME_FLAG_MESSAGE"]
.FormatExt(newPlayer.Name, penaltyReason));
}
2023-02-11 21:56:52 -05:00
else
{
newPlayer.CurrentServer.Broadcast(await ProcessAnnouncement(_configuration.UserAnnouncementMessage,
newPlayer));
}
}
2023-02-11 21:56:52 -05:00
private async Task<string> ProcessAnnouncement(string msg, EFClient joining)
{
msg = msg.Replace("{{ClientName}}", joining.Name);
msg = msg.Replace("{{ClientLevel}}",
$"{Utilities.ConvertLevelToColor(joining.Level, joining.ClientPermission.Name)}{(string.IsNullOrEmpty(joining.Tag) ? "" : $" (Color::White){joining.Tag}(Color::White)")}");
// this prevents it from trying to evaluate it every message
if (msg.Contains("{{ClientLocation}}"))
{
2023-02-11 21:56:52 -05:00
msg = msg.Replace("{{ClientLocation}}", await GetCountryName(joining.IPAddressString));
}
2023-02-11 21:56:52 -05:00
msg = msg.Replace("{{TimesConnected}}",
joining.Connections.Ordinalize(Utilities.CurrentLocalization.Culture));
2023-02-11 21:56:52 -05:00
return msg;
}
2023-02-11 21:56:52 -05:00
/// <summary>
/// makes a webrequest to determine IP origin
/// </summary>
/// <param name="ip">IP address to get location of</param>
/// <returns></returns>
private async Task<string> GetCountryName(string ip)
{
using var wc = new HttpClient();
try
{
2023-02-11 21:56:52 -05:00
var response =
await wc.GetStringAsync(new Uri(
$"http://ip-api.com/json/{ip}?lang={Utilities.CurrentLocalization.LocalizationName.Split("-").First().ToLower()}"));
var responseObj = JObject.Parse(response);
response = responseObj["country"]?.ToString();
return string.IsNullOrEmpty(response)
? Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_COUNTRY"]
: response;
}
2023-02-11 21:56:52 -05:00
catch
{
return Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_IP"];
}
}
2022-01-26 11:32:16 -05:00
}