Adds configuration system. (#125)

This commit is contained in:
Kamron Batman 2020-05-02 16:44:05 -07:00 committed by GitHub
parent 2b4a6e1de1
commit 5b01754e3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 409 additions and 420 deletions

View file

@ -1,84 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Server.Mobiles;
using Server.Network;
namespace Server.Commands
{
public class ConvertPlayers
{
public static void Initialize()
{
CommandSystem.Register("ConvertPlayers", AccessLevel.Administrator, Convert_OnCommand);
}
public static void Convert_OnCommand(CommandEventArgs e)
{
e.Mobile.SendMessage(
"Converting all players to PlayerMobile. You will be disconnected. Please Restart the server after the world has finished saving.");
List<Mobile> mobs = new List<Mobile>(World.Mobiles.Values);
int count = 0;
foreach (Mobile m in mobs)
if (m.Player && !(m is PlayerMobile))
{
count++;
m.NetState?.Dispose();
PlayerMobile pm = new PlayerMobile(m.Serial);
pm.DefaultMobileInit();
List<Item> copy = new List<Item>(m.Items);
for (int i = 0; i < copy.Count; i++)
pm.AddItem(copy[i]);
CopyProps(pm, m);
for (int i = 0; i < m.Skills.Length; i++)
{
pm.Skills[i].Base = m.Skills[i].Base;
pm.Skills[i].SetLockNoRelay(m.Skills[i].Lock);
}
World.Mobiles[m.Serial] = pm;
}
if (count > 0)
{
NetState.ProcessDisposedQueue();
World.Save();
Console.WriteLine("{0} players have been converted to PlayerMobile. {1}.", count,
Core.Service ? "The server is now restarting" : "Press any key to restart the server");
if (!Core.Service)
Console.ReadKey(true);
Core.Kill(true);
}
else
{
e.Mobile.SendMessage("Couldn't find any Players to convert.");
}
}
private static readonly PropertyInfo[] _mobProps =
typeof(Mobile).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(prop => prop.CanRead && prop.CanWrite).ToArray();
private static void CopyProps(Mobile to, Mobile from)
{
foreach (PropertyInfo prop in _mobProps)
try
{
prop.SetValue(to, prop.GetValue(from, null), null);
}
catch
{
// ignored
}
}
}
}

View file

@ -0,0 +1,75 @@
using System.IO;
using System.Text.Json.Serialization;
using MimeKit;
using Server.Json;
namespace Server.Configurations
{
public static class EmailConfiguration
{
public static readonly bool EmailEnabled;
public static readonly MailboxAddress FromAddress;
public static readonly MailboxAddress CrashAddress;
public static readonly MailboxAddress SpeechLogPageAddress;
public static readonly string EmailServer;
public static readonly int EmailPort;
public static readonly string EmailServerUsername;
public static readonly string EmailServerPassword;
public static readonly int EmailSendRetryCount = 5; // seconds
public static readonly int EmailSendRetryDelay = 2; // seconds
static EmailConfiguration()
{
string filePath = Path.Join(Core.BaseDirectory, "Configuration/email-settings.json");
Settings settings = JsonConfig.Deserialize<Settings>(filePath) ?? new Settings();
if (settings.emailServer == null || settings.fromAddress == null)
{
JsonConfig.Serialize(filePath, settings);
return;
}
EmailEnabled = true;
FromAddress = new MailboxAddress(settings.fromName, settings.fromAddress);
CrashAddress = new MailboxAddress(settings.crashName, settings.crashAddress);
SpeechLogPageAddress = new MailboxAddress(settings.speechLogPageName, settings.speechLogPageAddress);
EmailServer = settings.emailServer;
EmailPort = settings.emailPort;
EmailServerUsername = settings.emailUsername;
EmailServerPassword = settings.emailPassword;
}
internal class Settings
{
[JsonPropertyName("fromAddress")]
internal string fromAddress { get; set; }
[JsonPropertyName("fromName")]
internal string fromName { get; set; }
[JsonPropertyName("crashAddress")]
internal string crashAddress { get; set; }
[JsonPropertyName("crashName")]
internal string crashName { get; set; }
[JsonPropertyName("speechLogPageAddress")]
internal string speechLogPageAddress { get; set; }
[JsonPropertyName("speechLogPageName")]
internal string speechLogPageName { get; set; }
[JsonPropertyName("emailServer")]
internal string emailServer { get; set; }
[JsonPropertyName("emailPort")]
internal int emailPort { get; set; }
[JsonPropertyName("emailUsername")]
internal string emailUsername { get; set; }
[JsonPropertyName("emailPassword")]
internal string emailPassword { get; set; }
}
}
}

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Server.Configurations;
using Server.Misc;
using Server.Mobiles;
using Server.Network;
@ -227,7 +228,7 @@ namespace Server.Engines.Help
entry.Sender.SendMessage(
"We are sorry, but no staff members are currently available to assist you. Your page will remain in the queue until one becomes available, or until you cancel it manually.");
if (Email.FROM_ADDRESS != null && Email.SPEECH_LOG_PAGE_ADDRESS != null && entry.SpeechLog != null)
if (entry.SpeechLog != null)
Email.SendQueueEmail(entry, GetPageTypeName(entry.Type));
}
}

View file

@ -7,7 +7,7 @@ namespace Server.Misc
{
public static void Initialize()
{
if (Accounts.Count == 0 && !Core.Service)
if (Accounts.Count == 0)
{
Console.WriteLine("This server has no accounts.");
Console.Write("Do you want to create the owner account now? (y/n)");
@ -36,4 +36,4 @@ namespace Server.Misc
}
}
}
}
}

View file

@ -41,8 +41,7 @@ namespace Server.Misc
{
Console.Write("Crash: Sending email...");
if (Email.FROM_ADDRESS != null && Email.CRASH_ADDRESS != null)
Email.SendCrashEmail(filePath);
Email.SendCrashEmail(filePath);
}
private static string GetRoot()
@ -229,8 +228,7 @@ namespace Server.Misc
Console.WriteLine("done");
if (Email.FROM_ADDRESS != null && Email.CRASH_ADDRESS != null)
SendEmail(filePath);
SendEmail(filePath);
}
catch
{

View file

@ -4,22 +4,13 @@ using System.Threading.Tasks;
using MailKit.Net.Smtp;
using MimeKit;
using Server.Accounting;
using Server.Configurations;
using Server.Engines.Help;
namespace Server.Misc
{
public static class Email
{
public static readonly MailboxAddress FROM_ADDRESS = new MailboxAddress(Configuration.Instance.emailSettings.FromName, Configuration.Instance.emailSettings.FromAddress);
public static readonly MailboxAddress CRASH_ADDRESS = new MailboxAddress(Configuration.Instance.emailSettings.crashName, Configuration.Instance.emailSettings.crashAddress);
public static readonly MailboxAddress SPEECH_LOG_PAGE_ADDRESS = new MailboxAddress(Configuration.Instance.emailSettings.speechLogPageName, Configuration.Instance.emailSettings.speechLogPageAddress);
public static readonly string EMAIL_SERVER = Configuration.Instance.emailSettings.emailServer;
public static readonly int EMAIL_PORT = Configuration.Instance.emailSettings.emailPort;
public static readonly string EMAIL_SERVER_USERNAME = Configuration.Instance.emailSettings.emailUsername;
public static readonly string EMAIL_SERVER_PASSWORD = Configuration.Instance.emailSettings.emailPassword;
public static readonly int RETRY_SEND_EMAIL_COUNT = 5;
public static readonly int SEND_DELAY_SECONDS = 2;
/// <summary>
/// Sends Queue-Page request using Email
/// </summary>
@ -27,12 +18,14 @@ namespace Server.Misc
/// <param name="pageType"></param>
public static void SendQueueEmail(PageEntry entry, string pageType)
{
if (!EmailConfiguration.EmailEnabled) return;
Mobile sender = entry.Sender;
DateTime time = DateTime.UtcNow;
var message = new MimeMessage();
message.From.Add(FROM_ADDRESS);
message.To.Add(SPEECH_LOG_PAGE_ADDRESS);
message.From.Add(EmailConfiguration.FromAddress);
message.To.Add(EmailConfiguration.SpeechLogPageAddress);
message.Subject = "ModernUO Speech Log Page Forwarding";
using (StringWriter writer = new StringWriter())
@ -77,9 +70,11 @@ namespace Server.Misc
/// <param name="filePath"></param>
public static void SendCrashEmail(string filePath)
{
if (EmailConfiguration.EmailEnabled) return;
var message = new MimeMessage();
message.From.Add(FROM_ADDRESS);
message.To.Add(CRASH_ADDRESS);
message.From.Add(EmailConfiguration.FromAddress);
message.To.Add(EmailConfiguration.CrashAddress);
message.Subject = "Automated ModernUO Crash Report";
var builder = new BodyBuilder
{
@ -96,19 +91,21 @@ namespace Server.Misc
/// <param name="message"></param>
private static async void SendAsync(MimeMessage message)
{
if (!EmailConfiguration.EmailEnabled) return;
DateTime now = DateTime.UtcNow;
string messageID = $"<{now:yyyyMMdd}.{now:HHmmssff}@{EMAIL_SERVER}>";
string messageID = $"<{now:yyyyMMdd}.{now:HHmmssff}@{EmailConfiguration.EmailServer}>";
message.Headers.Add("Message-ID", messageID);
message.From.Add(FROM_ADDRESS);
message.From.Add(EmailConfiguration.FromAddress);
int delay = SEND_DELAY_SECONDS;
int delay = EmailConfiguration.EmailSendRetryDelay;
for (int i = 0; i < RETRY_SEND_EMAIL_COUNT; i++)
for (int i = 0; i < EmailConfiguration.EmailSendRetryCount; i++)
try
{
using SmtpClient client = new SmtpClient();
await client.ConnectAsync(EMAIL_SERVER, EMAIL_PORT, true);
await client.AuthenticateAsync(EMAIL_SERVER_USERNAME, EMAIL_SERVER_PASSWORD);
await client.ConnectAsync(EmailConfiguration.EmailServer, EmailConfiguration.EmailPort, true);
await client.AuthenticateAsync(EmailConfiguration.EmailServerUsername, EmailConfiguration.EmailServerPassword);
await client.SendAsync(message);
await client.DisconnectAsync(true);
return;