Converting to file-scoped namespaces

This commit is contained in:
2023-04-02 22:10:42 +02:00
parent 2637741703
commit 4f3a768432
11 changed files with 525 additions and 536 deletions

View File

@@ -1,14 +1,13 @@
namespace Common
{
class Config
{
public string PasswordStorePath { get; set; }
public string Recipient { get; set; }
namespace Common;
public Config(string PasswordStorePath, string Recipient)
{
this.PasswordStorePath = PasswordStorePath;
this.Recipient = Recipient;
}
class Config
{
public string PasswordStorePath { get; set; }
public string Recipient { get; set; }
public Config(string PasswordStorePath, string Recipient)
{
this.PasswordStorePath = PasswordStorePath;
this.Recipient = Recipient;
}
}

View File

@@ -1,9 +1,9 @@
using Microsoft.VisualBasic.FileIO;
namespace Common
namespace Common;
static class ConfigFileManager
{
static class ConfigFileManager
{
#region Config flags
/*Configuration: (format: fieldname=value)
@@ -20,75 +20,74 @@ namespace Common
public static void Init()
{
bool success = false;
bool success = false;
while (!success)
while (!success)
{
if (File.Exists(CONFIGPATH))
{
if (File.Exists(CONFIGPATH))
StreamReader sr = new StreamReader(CONFIGPATH);
string? path = null, recipient = null;
while (!sr.EndOfStream)
{
StreamReader sr = new StreamReader(CONFIGPATH);
string? path = null, recipient = null;
while (!sr.EndOfStream)
string[]? fields = sr.ReadLine().Split(DELIMETER);
if (fields != null)
{
string[]? fields = sr.ReadLine().Split(DELIMETER);
if (fields != null)
if (fields[0] == PATH)
{
if (fields[0] == PATH)
{
path = fields[1];
}
else if (fields[0] == RECIPIENT)
{
recipient = fields[1];
}
path = fields[1];
}
else //probably an empty line or something
else if (fields[0] == RECIPIENT)
{
continue;
recipient = fields[1];
}
}
else //probably an empty line or something
{
continue;
}
}
if (path != null && recipient != null)
{
Configuration = new Config(path, recipient);
success = true;
}
else
{
throw new InvalidConfigurationException("One or more required fileds were missing from the configuration file.");
}
if (path != null && recipient != null)
{
Configuration = new Config(path, recipient);
success = true;
}
else
{
CreateDefaultConfig();
throw new InvalidConfigurationException("One or more required fileds were missing from the configuration file.");
}
}
else
{
CreateDefaultConfig();
}
}
}
public static string GetPath()
{
public static string GetPath()
{
return Configuration.PasswordStorePath;
}
}
public static string GetRecipient()
{
return Configuration.Recipient;
}
private static void CreateDefaultConfig()
private static void CreateDefaultConfig()
{
try
{
try
{
string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
StreamWriter sw = new StreamWriter(CONFIGPATH);
sw.WriteLine(RECIPIENT + DELIMETER + user);
sw.WriteLine(PATH + DELIMETER + SpecialDirectories.CurrentUserApplicationData);
sw.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
StreamWriter sw = new StreamWriter(CONFIGPATH);
sw.WriteLine(RECIPIENT + DELIMETER + user);
sw.WriteLine(PATH + DELIMETER + SpecialDirectories.CurrentUserApplicationData);
sw.Close();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}

View File

@@ -1,9 +1,8 @@
namespace Common
namespace Common;
class InvalidConfigurationException : Exception
{
class InvalidConfigurationException : Exception
{
public InvalidConfigurationException() : base() { }
public InvalidConfigurationException(string message) : base(message) { }
public InvalidConfigurationException(string message, Exception inner) : base(message, inner) { }
}
public InvalidConfigurationException() : base() { }
public InvalidConfigurationException(string message) : base(message) { }
public InvalidConfigurationException(string message, Exception inner) : base(message, inner) { }
}

View File

@@ -1,39 +1,38 @@
using System.Text;
namespace Common
namespace Common;
static class PasswordGenerator
{
static class PasswordGenerator
private static string RandomStr(int length, bool no_symbols = false)
{
private static string RandomStr(int length, bool no_symbols = false)
StringBuilder builder = new StringBuilder();
Random rnd = new Random();
for (int i = 0; i < length; i++)
{
StringBuilder builder = new StringBuilder();
Random rnd = new Random();
for (int i = 0; i < length; i++)
if (rnd.Next(0, 101) <= 60) //60% chance for a character
{
if (rnd.Next(0, 101) <= 60) //60% chance for a character
char c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65))); //random character
if (no_symbols && Char.IsSymbol(c))
{
char c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65))); //random character
if (no_symbols && Char.IsSymbol(c))
while (Char.IsSymbol(c))
{
while (Char.IsSymbol(c))
{
c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65))); //random character
}
c = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * rnd.NextDouble() + 65))); //random character
}
builder.Append(c);
}
else //40% change for number
{
builder.Append(Convert.ToChar(rnd.Next(0, 10)));
}
builder.Append(c);
}
else //40% change for number
{
builder.Append(Convert.ToChar(rnd.Next(0, 10)));
}
return builder.ToString();
}
public static string? New(string recipient, int length, bool no_symbols = false)
{
return builder.ToString();
}
public static string? New(string recipient, int length, bool no_symbols = false)
{
return new ProcessBuilder().GetOutput("cmd.exe", $"echo {RandomStr(length, no_symbols)} | gpg --quiet --encrypt --recipient {recipient}");
}
}
}

View File

@@ -1,61 +1,60 @@
using System.Diagnostics;
using System.Text;
namespace Common
namespace Common;
public delegate void ProcessSuccess();
public delegate void ProcessFailure(Exception e);
sealed class ProcessBuilder
{
public delegate void ProcessSuccess();
public delegate void ProcessFailure(Exception e);
sealed class ProcessBuilder
public event ProcessSuccess? ProcessFinished;
public event ProcessFailure? ProcessFailed;
public void Run(string procName, string args)
{
public event ProcessSuccess? ProcessFinished;
public event ProcessFailure? ProcessFailed;
public void Run(string procName, string args)
try
{
try
{
Process.Start(procName, args);
ProcessFinished?.Invoke();
}
catch (Exception e)
{
ProcessFailed?.Invoke(e);
}
Process.Start(procName, args);
ProcessFinished?.Invoke();
}
public string? GetOutput(string procName, string args)
catch (Exception e)
{
try
{
Process proc = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = procName,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
StringBuilder builder = new StringBuilder();
while (!proc.StandardOutput.EndOfStream)
{
builder.Append(proc.StandardOutput.ReadLine());
}
ProcessFinished?.Invoke();
return builder.ToString();
} catch (Exception e)
{
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ProcessFailed?.Invoke(e);
}
return null;
ProcessFailed?.Invoke(e);
}
}
public string? GetOutput(string procName, string args)
{
try
{
Process proc = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = procName,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
StringBuilder builder = new StringBuilder();
while (!proc.StandardOutput.EndOfStream)
{
builder.Append(proc.StandardOutput.ReadLine());
}
ProcessFinished?.Invoke();
return builder.ToString();
} catch (Exception e)
{
MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
ProcessFailed?.Invoke(e);
}
return null;
}
}