2017-05-26 18:49:27 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2018-04-09 15:17:10 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using System.Linq;
|
2018-10-07 22:34:30 -04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-10-08 22:15:59 -04:00
|
|
|
|
using System.Net;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
2021-01-24 12:47:19 -05:00
|
|
|
|
using Humanizer;
|
2021-03-22 12:09:25 -04:00
|
|
|
|
using Data.Abstractions;
|
|
|
|
|
using Data.Models;
|
|
|
|
|
using static Data.Models.Client.EFClient;
|
2017-06-07 17:08:29 -04:00
|
|
|
|
|
2018-04-09 15:17:10 -04:00
|
|
|
|
namespace IW4MAdmin.Plugins.Welcome
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
|
|
|
|
public class Plugin : IPlugin
|
|
|
|
|
{
|
2017-11-15 16:04:13 -05:00
|
|
|
|
public string Author => "RaidMax";
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2017-11-15 16:04:13 -05:00
|
|
|
|
public float Version => 1.0f;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2017-11-15 16:04:13 -05:00
|
|
|
|
public string Name => "Welcome Plugin";
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
private readonly IConfigurationHandler<WelcomeConfiguration> _configHandler;
|
2020-11-27 22:52:52 -05:00
|
|
|
|
private readonly IDatabaseContextFactory _contextFactory;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory, IDatabaseContextFactory contextFactory)
|
2020-02-11 17:44:06 -05:00
|
|
|
|
{
|
|
|
|
|
_configHandler = configurationHandlerFactory.GetConfigurationHandler<WelcomeConfiguration>("WelcomePluginSettings");
|
2020-11-27 22:52:52 -05:00
|
|
|
|
_contextFactory = contextFactory;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
}
|
2018-02-11 20:17:20 -05:00
|
|
|
|
|
2017-10-16 23:47:41 -04:00
|
|
|
|
public async Task OnLoadAsync(IManager manager)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
if (_configHandler.Configuration() == null)
|
2018-03-18 22:25:11 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
_configHandler.Set((WelcomeConfiguration)new WelcomeConfiguration().Generate());
|
|
|
|
|
await _configHandler.Save();
|
2018-03-18 22:25:11 -04:00
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public Task OnUnloadAsync() => Task.CompletedTask;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public Task OnTickAsync(Server S) => Task.CompletedTask;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2018-04-13 02:32:30 -04:00
|
|
|
|
public async Task OnEventAsync(GameEvent E, Server S)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2018-11-25 21:00:36 -05:00
|
|
|
|
if (E.Type == GameEvent.EventType.Join)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2018-11-05 22:01:29 -05:00
|
|
|
|
EFClient newPlayer = E.Origin;
|
2021-01-24 12:47:19 -05:00
|
|
|
|
if (newPlayer.Level >= Permission.Trusted && !E.Origin.Masked || !string.IsNullOrEmpty(newPlayer.GetAdditionalProperty<string>("ClientTag")))
|
2020-02-11 17:44:06 -05:00
|
|
|
|
E.Owner.Broadcast(await ProcessAnnouncement(_configHandler.Configuration().PrivilegedAnnouncementMessage, newPlayer));
|
2017-05-28 21:07:33 -04:00
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
newPlayer.Tell(await ProcessAnnouncement(_configHandler.Configuration().UserWelcomeMessage, newPlayer));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
if (newPlayer.Level == Permission.Flagged)
|
2018-04-13 02:32:30 -04:00
|
|
|
|
{
|
2018-10-07 22:34:30 -04:00
|
|
|
|
string penaltyReason;
|
|
|
|
|
|
2020-11-27 22:52:52 -05:00
|
|
|
|
await using var context = _contextFactory.CreateContext(false);
|
2018-10-07 22:34:30 -04:00
|
|
|
|
{
|
2020-11-27 22:52:52 -05:00
|
|
|
|
penaltyReason = await context.Penalties
|
2019-05-29 17:55:35 -04:00
|
|
|
|
.Where(p => p.OffenderId == newPlayer.ClientId && p.Type == EFPenalty.PenaltyType.Flag)
|
2018-10-07 22:34:30 -04:00
|
|
|
|
.OrderByDescending(p => p.When)
|
|
|
|
|
.Select(p => p.AutomatedOffense ?? p.Offense)
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-08 22:15:59 -04:00
|
|
|
|
E.Owner.ToAdmins($"^1NOTICE: ^7Flagged player ^5{newPlayer.Name} ^7({penaltyReason}) has joined!");
|
2018-04-13 02:32:30 -04:00
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
else
|
2020-02-11 17:44:06 -05:00
|
|
|
|
E.Owner.Broadcast(await ProcessAnnouncement(_configHandler.Configuration().UserAnnouncementMessage, newPlayer));
|
2017-06-07 17:08:29 -04:00
|
|
|
|
}
|
2018-02-11 20:17:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
private async Task<string> ProcessAnnouncement(string msg, EFClient joining)
|
2018-02-11 20:17:20 -05:00
|
|
|
|
{
|
|
|
|
|
msg = msg.Replace("{{ClientName}}", joining.Name);
|
2021-01-24 12:47:19 -05:00
|
|
|
|
msg = msg.Replace("{{ClientLevel}}", $"{Utilities.ConvertLevelToColor(joining.Level, joining.ClientPermission.Name)}{(string.IsNullOrEmpty(joining.GetAdditionalProperty<string>("ClientTag")) ? "" : $" ^7({joining.GetAdditionalProperty<string>("ClientTag")}^7)")}");
|
2018-10-08 22:15:59 -04:00
|
|
|
|
// this prevents it from trying to evaluate it every message
|
|
|
|
|
if (msg.Contains("{{ClientLocation}}"))
|
2018-02-11 20:17:20 -05:00
|
|
|
|
{
|
2018-10-08 22:15:59 -04:00
|
|
|
|
msg = msg.Replace("{{ClientLocation}}", await GetCountryName(joining.IPAddressString));
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2021-01-24 12:47:19 -05:00
|
|
|
|
msg = msg.Replace("{{TimesConnected}}", joining.Connections.Ordinalize());
|
2018-02-11 20:17:20 -05:00
|
|
|
|
|
|
|
|
|
return msg;
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2018-10-08 22:15:59 -04: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 WebClient())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string response = await wc.DownloadStringTaskAsync(new Uri($"http://extreme-ip-lookup.com/json/{ip}"));
|
|
|
|
|
var responseObj = JObject.Parse(response);
|
2019-02-09 16:35:13 -05:00
|
|
|
|
response = responseObj["country"].ToString();
|
2018-10-08 22:15:59 -04:00
|
|
|
|
|
2019-02-09 16:35:13 -05:00
|
|
|
|
return string.IsNullOrEmpty(response) ? Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_COUNTRY"] : response;
|
2018-10-08 22:15:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch
|
|
|
|
|
{
|
2019-02-09 16:35:13 -05:00
|
|
|
|
return Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_IP"];
|
2018-10-08 22:15:59 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
|
|
|
|
}
|