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;
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using System.Net.Http;
|
2018-10-08 22:15:59 -04:00
|
|
|
|
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
|
|
|
|
{
|
2021-05-04 20:01:09 -04: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
|
|
|
|
{
|
2021-05-04 20:01:09 -04:00
|
|
|
|
_configHandler.Set((WelcomeConfiguration) new WelcomeConfiguration().Generate());
|
2020-02-11 17:44:06 -05:00
|
|
|
|
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
|
|
|
|
|
2021-11-23 18:26:33 -05:00
|
|
|
|
public async Task OnEventAsync(GameEvent gameEvent, Server server)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2021-11-23 18:26:33 -05:00
|
|
|
|
if (gameEvent.Type == GameEvent.EventType.Join)
|
2017-05-26 18:49:27 -04:00
|
|
|
|
{
|
2021-11-23 18:26:33 -05:00
|
|
|
|
var newPlayer = gameEvent.Origin;
|
|
|
|
|
if (newPlayer.Level >= Permission.Trusted && !gameEvent.Origin.Masked||
|
|
|
|
|
!string.IsNullOrEmpty(newPlayer.GetAdditionalProperty<string>("ClientTag")) &&
|
2021-05-04 20:01:09 -04:00
|
|
|
|
newPlayer.Level != Permission.Flagged && newPlayer.Level != Permission.Banned &&
|
2021-11-23 18:26:33 -05:00
|
|
|
|
!newPlayer.Masked)
|
|
|
|
|
gameEvent.Owner.Broadcast(
|
2021-05-04 20:01:09 -04:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-23 18:26:33 -05:00
|
|
|
|
gameEvent.Owner.ToAdmins(Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_FLAG_MESSAGE"]
|
|
|
|
|
.FormatExt(newPlayer.Name, penaltyReason));
|
2018-04-13 02:32:30 -04:00
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
else
|
2021-11-23 18:26:33 -05:00
|
|
|
|
gameEvent.Owner.Broadcast(await ProcessAnnouncement(_configHandler.Configuration().UserAnnouncementMessage,
|
2021-05-04 20:01:09 -04:00
|
|
|
|
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-05-04 20:01:09 -04:00
|
|
|
|
msg = msg.Replace("{{ClientLevel}}",
|
2021-11-23 18:26:33 -05:00
|
|
|
|
$"{Utilities.ConvertLevelToColor(joining.Level, joining.ClientPermission.Name)}{(string.IsNullOrEmpty(joining.GetAdditionalProperty<string>("ClientTag")) ? "" : $" (Color::White)({joining.GetAdditionalProperty<string>("ClientTag")}(Color::White))")}");
|
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-05-04 20:01:09 -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)
|
|
|
|
|
{
|
2022-01-26 11:32:16 -05:00
|
|
|
|
using var wc = new HttpClient();
|
2021-11-01 18:06:17 -04:00
|
|
|
|
try
|
2018-10-08 22:15:59 -04:00
|
|
|
|
{
|
2021-11-01 18:06:17 -04:00
|
|
|
|
var response =
|
2022-01-26 11:32:16 -05:00
|
|
|
|
await wc.GetStringAsync(new Uri($"http://extreme-ip-lookup.com/json/{ip}?key=demo"));
|
2021-11-01 18:06:17 -04:00
|
|
|
|
var responseObj = JObject.Parse(response);
|
|
|
|
|
response = responseObj["country"]?.ToString();
|
|
|
|
|
|
|
|
|
|
return string.IsNullOrEmpty(response)
|
|
|
|
|
? Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_COUNTRY"]
|
|
|
|
|
: response;
|
|
|
|
|
}
|
2018-10-08 22:15:59 -04:00
|
|
|
|
|
2021-11-01 18:06:17 -04:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_WELCOME_UNKNOWN_IP"];
|
2018-10-08 22:15:59 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-26 18:49:27 -04:00
|
|
|
|
}
|
2022-01-26 11:32:16 -05:00
|
|
|
|
}
|