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;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
2018-04-13 02:32:30 -04:00
|
|
|
|
using SharedLibraryCore.Services;
|
|
|
|
|
using SharedLibraryCore.Database.Models;
|
|
|
|
|
using System.Linq;
|
2018-10-07 22:34:30 -04:00
|
|
|
|
using SharedLibraryCore.Database;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-10-08 22:15:59 -04:00
|
|
|
|
using System.Net;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using static SharedLibraryCore.Database.Models.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
|
|
|
|
|
{
|
2018-11-05 22:01:29 -05:00
|
|
|
|
String TimesConnected(EFClient P)
|
2017-06-12 17:47:31 -04:00
|
|
|
|
{
|
|
|
|
|
int connection = P.Connections;
|
|
|
|
|
String Prefix = String.Empty;
|
|
|
|
|
if (connection % 10 > 3 || connection % 10 == 0 || (connection % 100 > 9 && connection % 100 < 19))
|
|
|
|
|
Prefix = "th";
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (connection % 10)
|
|
|
|
|
{
|
|
|
|
|
case 1:
|
|
|
|
|
Prefix = "st";
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
Prefix = "nd";
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
Prefix = "rd";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (connection)
|
|
|
|
|
{
|
|
|
|
|
case 0:
|
|
|
|
|
case 1:
|
|
|
|
|
return "first";
|
|
|
|
|
case 2:
|
|
|
|
|
return "second";
|
|
|
|
|
case 3:
|
|
|
|
|
return "third";
|
|
|
|
|
case 4:
|
|
|
|
|
return "fourth";
|
|
|
|
|
case 5:
|
|
|
|
|
return "fifth";
|
|
|
|
|
default:
|
|
|
|
|
return connection.ToString() + Prefix;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
|
|
|
|
|
{
|
|
|
|
|
_configHandler = configurationHandlerFactory.GetConfigurationHandler<WelcomeConfiguration>("WelcomePluginSettings");
|
|
|
|
|
}
|
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;
|
|
|
|
|
if (newPlayer.Level >= Permission.Trusted && !E.Origin.Masked)
|
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;
|
|
|
|
|
|
|
|
|
|
using (var ctx = new DatabaseContext(disableTracking: true))
|
|
|
|
|
{
|
|
|
|
|
penaltyReason = await ctx.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);
|
2018-08-03 22:11:58 -04:00
|
|
|
|
msg = msg.Replace("{{ClientLevel}}", Utilities.ConvertLevelToColor(joining.Level, joining.ClientPermission.Name));
|
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
|
|
|
|
}
|
2018-02-11 20:17:20 -05:00
|
|
|
|
msg = msg.Replace("{{TimesConnected}}", TimesConnected(joining));
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|