2019-05-15 16:42:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2019-05-16 16:14:54 +00:00
|
|
|
|
using System.Windows.Forms;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
|
2019-12-02 16:23:35 +00:00
|
|
|
|
using dezentrale.core;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
using dezentrale.model;
|
2019-06-03 14:03:19 +00:00
|
|
|
|
using dezentrale.model.money;
|
2019-05-16 16:14:54 +00:00
|
|
|
|
using dezentrale.view;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
|
2019-06-10 22:13:35 +00:00
|
|
|
|
/*
|
|
|
|
|
TODO
|
|
|
|
|
----
|
2019-06-11 16:20:28 +00:00
|
|
|
|
- Documentation
|
2019-06-10 22:13:35 +00:00
|
|
|
|
- ErrorLog for all errors
|
|
|
|
|
- frmMain: Menu option to miss an MV for selected/checked users
|
2019-11-27 18:03:19 +00:00
|
|
|
|
- frmMain: Indicator that the data was modified after import + messagebox to remind user to export on quitting.
|
2019-06-18 16:04:04 +00:00
|
|
|
|
- Configuration window: MoneyTransferRegEx
|
2021-02-18 16:00:56 +00:00
|
|
|
|
- Bug: Import/Export gpg uses command line parameter for password - this can be read out by any system user via "ps uxa"
|
2019-06-13 16:09:12 +00:00
|
|
|
|
- CronjobConfig
|
2019-06-10 22:13:35 +00:00
|
|
|
|
*/
|
2019-05-15 16:42:43 +00:00
|
|
|
|
namespace dezentrale
|
|
|
|
|
{
|
|
|
|
|
public class Program
|
|
|
|
|
{
|
2022-03-29 21:43:03 +00:00
|
|
|
|
public static uint VersionNumber { get; private set; } = 0x22032900;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
public static string VersionString { get; private set; } = $"{VersionNumber:x}";
|
|
|
|
|
|
|
|
|
|
public static string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
|
|
|
public static string DmDirectory = Path.Combine(AppData, "dezentrale-members");
|
|
|
|
|
|
|
|
|
|
public static string ConfigFile = Path.Combine(DmDirectory, "configuration.xml");
|
|
|
|
|
public static Configuration config = new Configuration();
|
|
|
|
|
public static MemberList members = new MemberList();
|
2021-01-18 07:43:56 +00:00
|
|
|
|
public static MvList mvList = new MvList();
|
2019-06-05 15:46:17 +00:00
|
|
|
|
|
2023-05-07 10:22:27 +00:00
|
|
|
|
public static MailTemplates mailTemplates = new MailTemplates();
|
2022-01-24 21:04:24 +00:00
|
|
|
|
public static bool MoneyTransfersLoaded
|
|
|
|
|
{
|
|
|
|
|
get { return moneyTransfers != null; }
|
|
|
|
|
set { moneyTransfers = value ? MoneyTransferList.LoadFromFile() : null; }
|
|
|
|
|
}
|
2019-06-05 15:46:17 +00:00
|
|
|
|
private static MoneyTransferList moneyTransfers = null;
|
|
|
|
|
public static MoneyTransferList MoneyTransfers
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (moneyTransfers != null) return moneyTransfers;
|
|
|
|
|
return moneyTransfers = MoneyTransferList.LoadFromFile();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-23 15:11:35 +00:00
|
|
|
|
|
2019-05-15 16:42:43 +00:00
|
|
|
|
public enum eMode
|
|
|
|
|
{
|
|
|
|
|
CommandLine = 0,
|
|
|
|
|
Gui = 1,
|
|
|
|
|
Cronjob = 2,
|
2019-05-27 15:03:55 +00:00
|
|
|
|
Export = 3,
|
2019-05-27 21:45:59 +00:00
|
|
|
|
Import = 4,
|
2019-06-01 01:34:56 +00:00
|
|
|
|
BankImport = 5,
|
2020-11-22 21:44:15 +00:00
|
|
|
|
Test = 255,
|
2019-05-15 16:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 16:14:54 +00:00
|
|
|
|
public static eMode ProgramMode = eMode.Gui;
|
2019-06-01 01:34:56 +00:00
|
|
|
|
private static string csvInput = null;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
|
2019-12-04 17:49:42 +00:00
|
|
|
|
public static int LoadFiles()
|
|
|
|
|
{
|
|
|
|
|
moneyTransfers = null;
|
|
|
|
|
|
|
|
|
|
try { Directory.CreateDirectory(DmDirectory); }
|
|
|
|
|
catch (Exception ex) { Console.WriteLine($"Error while creating data directory:\n{ex.Message}"); return 1; }
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
config = (Configuration)XmlData.LoadFromFile(ConfigFile, typeof(Configuration));
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
config.DbDirectory = Path.Combine(DmDirectory, Configuration.DefaultDbDirectory);
|
2021-03-01 18:15:18 +00:00
|
|
|
|
config.DbChangedSinceExport = false;
|
2019-12-04 17:49:42 +00:00
|
|
|
|
XmlData.SaveToFile(ConfigFile, config); Console.WriteLine("Created new configuration file.");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Error while accessing program data:");
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try { Directory.CreateDirectory(config.DbDirectory); }
|
|
|
|
|
catch (Exception ex) { Console.WriteLine($"Error while creating member data directory:\n{ex.Message}"); return 1; }
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string[] memberFiles = Directory.GetFiles(config.DbDirectory, "member-*.xml");
|
|
|
|
|
//foreach (string s in memberFiles) Console.WriteLine(s);
|
|
|
|
|
members = MemberList.LoadFromFiles(memberFiles);
|
|
|
|
|
Console.WriteLine($"Loaded {members.Entries.Count} member entries");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Error while loading member files:");
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2021-01-18 07:43:56 +00:00
|
|
|
|
|
2023-05-07 10:22:27 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
mailTemplates = (MailTemplates)XmlData.LoadFromFile(Path.Combine(config.DbDirectory, "MailTemplates.xml"), typeof(MailTemplates));
|
|
|
|
|
} catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Error while loading mail templates:");
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
mailTemplates.InitializeNonSerializedFields();
|
|
|
|
|
|
2021-01-18 07:43:56 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string mvFileName = System.IO.Path.Combine(Program.config.DbDirectory, MvList.FileName);
|
|
|
|
|
mvList = (MvList)XmlData.LoadFromFile(mvFileName, typeof(MvList));
|
|
|
|
|
Console.WriteLine($"Loaded MV list with {mvList.Entries.Count} entries");
|
|
|
|
|
}
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Creating new MV list file");
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-03-01 18:15:18 +00:00
|
|
|
|
mvList.SaveToFile(false);
|
2021-01-18 07:43:56 +00:00
|
|
|
|
} catch(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Error while creating new MV list:");
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Error while loading MV list:");
|
|
|
|
|
Console.WriteLine(ex.Message);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-12-04 17:49:42 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-16 16:14:54 +00:00
|
|
|
|
[STAThread]
|
2019-05-15 16:42:43 +00:00
|
|
|
|
static int Main(string[] args)
|
|
|
|
|
{
|
|
|
|
|
bool printHelp = false;
|
|
|
|
|
Console.WriteLine($"dezentrale-members, Version {VersionString}");
|
2023-04-06 08:46:23 +00:00
|
|
|
|
Console.WriteLine($"Working directory: {DmDirectory}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//See https://stackoverflow.com/questions/721161/how-to-detect-which-net-runtime-is-being-used-ms-vs-mono
|
|
|
|
|
bool isRunningOnMono = false;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
isRunningOnMono = (Type.GetType("Mono.Runtime") != null);
|
|
|
|
|
} catch(Exception ex) { Console.WriteLine($"Mono detection failed. Assuming non-Mono. Error: {ex.Message}"); }
|
|
|
|
|
|
|
|
|
|
if(isRunningOnMono)
|
|
|
|
|
{
|
|
|
|
|
//Mono on OpenSSL (boringssl) will lead to Usage of TLS 1.0 instead of 1.2, even
|
|
|
|
|
//if this is explicitly set here!
|
|
|
|
|
Console.WriteLine("Mono detected. Setting MONO_TLS_PROVIDER to btls");
|
|
|
|
|
Environment.SetEnvironmentVariable("MONO_TLS_PROVIDER", "btls");
|
|
|
|
|
}
|
2022-01-19 21:14:16 +00:00
|
|
|
|
System.Net.ServicePointManager.Expect100Continue = true;
|
|
|
|
|
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
|
|
|
|
|
|
|
|
|
|
System.Net.ServicePointManager.ServerCertificateValidationCallback +=
|
|
|
|
|
(sender, certificate, chain, sslPolicyErrors) =>
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
};
|
2019-05-15 16:42:43 +00:00
|
|
|
|
List<string> clArgs = new List<string>();
|
2019-06-01 01:34:56 +00:00
|
|
|
|
foreach (string argIt in args)
|
2019-05-15 16:42:43 +00:00
|
|
|
|
{
|
|
|
|
|
string argn;
|
|
|
|
|
string argv;
|
|
|
|
|
if (argIt.Contains("="))
|
|
|
|
|
{
|
|
|
|
|
argn = argIt.Substring(0, argIt.IndexOf('='));
|
|
|
|
|
argv = argIt.Substring(argIt.IndexOf('=') + 1);
|
2019-06-01 01:34:56 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2019-05-15 16:42:43 +00:00
|
|
|
|
{
|
|
|
|
|
argn = argIt;
|
|
|
|
|
argv = "";
|
|
|
|
|
}
|
2019-11-10 13:54:56 +00:00
|
|
|
|
if (argn.StartsWith("-", StringComparison.InvariantCulture)) argn = argn.Substring(1);
|
|
|
|
|
if (argn.StartsWith("/", StringComparison.InvariantCulture) || argn.StartsWith("-", StringComparison.InvariantCulture)) argn = argn.Substring(1);
|
2019-06-01 01:34:56 +00:00
|
|
|
|
switch (argn.ToLower())
|
2019-05-15 16:42:43 +00:00
|
|
|
|
{
|
2019-06-01 01:34:56 +00:00
|
|
|
|
case "help": printHelp = true; break;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
case "mode":
|
2019-06-01 01:34:56 +00:00
|
|
|
|
switch (argv.ToLower())
|
2019-05-15 16:42:43 +00:00
|
|
|
|
{
|
2020-11-22 21:44:15 +00:00
|
|
|
|
case "test": ProgramMode = eMode.Test; break;
|
2019-06-01 01:34:56 +00:00
|
|
|
|
case "cl": ProgramMode = eMode.CommandLine; break;
|
|
|
|
|
case "gui": ProgramMode = eMode.Gui; break;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
case "cronjob":
|
2019-06-01 01:34:56 +00:00
|
|
|
|
case "auto": ProgramMode = eMode.Cronjob; break;
|
|
|
|
|
case "export": ProgramMode = eMode.Export; break;
|
|
|
|
|
case "import": ProgramMode = eMode.Import; break;
|
|
|
|
|
case "bankimport": ProgramMode = eMode.BankImport; break;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
default:
|
|
|
|
|
Console.WriteLine("Invalid mode.");
|
|
|
|
|
printHelp = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2019-05-27 21:45:59 +00:00
|
|
|
|
Console.WriteLine($"Setting Mode to {ProgramMode}");
|
2019-06-01 01:34:56 +00:00
|
|
|
|
break;
|
|
|
|
|
case "csvinput":
|
|
|
|
|
csvInput = argv;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Console.WriteLine($"Invalid command({argn})");
|
|
|
|
|
printHelp = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (printHelp)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("command line arguments:");
|
|
|
|
|
Console.WriteLine("--help Display this help message");
|
|
|
|
|
Console.WriteLine("--mode=<mode> Starts the program in given mode. Supported modes are:");
|
|
|
|
|
Console.WriteLine(" cl (default) Manual/interactive use from command line.");
|
|
|
|
|
Console.WriteLine(" gui Start in GUI mode (not implemented yet)");
|
|
|
|
|
Console.WriteLine(" auto, cronjob Check and perform pending required membership actions");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 17:49:42 +00:00
|
|
|
|
if(LoadFiles() != 0) return 1;
|
2019-05-15 16:42:43 +00:00
|
|
|
|
|
2019-05-16 16:14:54 +00:00
|
|
|
|
switch (ProgramMode)
|
|
|
|
|
{
|
2020-11-22 21:44:15 +00:00
|
|
|
|
case eMode.Test:
|
|
|
|
|
model.svg.SvgFile.Test("SvgFileTest.svg");
|
|
|
|
|
break;
|
2019-05-16 16:14:54 +00:00
|
|
|
|
case eMode.Gui:
|
|
|
|
|
frmMain w = new frmMain();
|
|
|
|
|
Application.Run(w);
|
|
|
|
|
break;
|
|
|
|
|
case eMode.CommandLine:
|
|
|
|
|
Console.WriteLine("Not implemented yet");
|
|
|
|
|
break;
|
|
|
|
|
case eMode.Cronjob:
|
2019-05-22 16:11:29 +00:00
|
|
|
|
Cronjob.Run();
|
2019-05-16 16:14:54 +00:00
|
|
|
|
break;
|
2022-01-24 21:04:24 +00:00
|
|
|
|
case eMode.Export:
|
|
|
|
|
{
|
|
|
|
|
if (!Program.config.ImportExport.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("ImportExport is disabled in configuration");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-12-03 16:43:02 +00:00
|
|
|
|
ExportProcess export = new ExportProcess()
|
|
|
|
|
{
|
|
|
|
|
ImportExportSettings = Program.config.ImportExport,
|
|
|
|
|
MemberDir = Program.config.DbDirectory,
|
|
|
|
|
OutputDir = Program.DmDirectory,
|
|
|
|
|
};
|
|
|
|
|
ConsoleLogger logger = new ConsoleLogger();
|
|
|
|
|
System.Threading.Thread t = logger.StartRunProcess(export);
|
|
|
|
|
if (t != null) t.Join();
|
|
|
|
|
else return 1;
|
|
|
|
|
//if (!config.ImportExport.Export(config.DbDirectory, DmDirectory)) return 1;
|
|
|
|
|
//if (!config.ImportExport.VerifyExport(config.DbDirectory, DmDirectory)) return 1;
|
|
|
|
|
|
|
|
|
|
if(logger.DialogResult != DialogResult.OK) return 1;
|
|
|
|
|
} break;
|
|
|
|
|
|
2019-05-27 21:45:59 +00:00
|
|
|
|
case eMode.Import:
|
2022-01-24 21:04:24 +00:00
|
|
|
|
{
|
|
|
|
|
if(!Program.config.ImportExport.Enabled)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("ImportExport is disabled in configuration");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-12-02 16:23:35 +00:00
|
|
|
|
ImportProcess import = new ImportProcess()
|
|
|
|
|
{
|
2019-12-03 16:43:02 +00:00
|
|
|
|
ImportExportSettings = Program.config.ImportExport,
|
2019-12-02 16:23:35 +00:00
|
|
|
|
MemberDir = Program.config.DbDirectory,
|
|
|
|
|
InputDir = Program.DmDirectory,
|
|
|
|
|
};
|
|
|
|
|
ConsoleLogger logger = new ConsoleLogger();
|
|
|
|
|
System.Threading.Thread t = logger.StartRunProcess(import);
|
|
|
|
|
if (t != null) t.Join();
|
|
|
|
|
else return 1;
|
2019-05-27 21:45:59 +00:00
|
|
|
|
|
2019-12-02 16:23:35 +00:00
|
|
|
|
//frmProcessWithLog frmImport = new frmProcessWithLog(import, true);
|
|
|
|
|
//if (!config.ImportExport.Import(config.DbDirectory, DmDirectory))
|
|
|
|
|
//return 1;
|
|
|
|
|
if(logger.DialogResult != DialogResult.OK) return 1;
|
2019-12-03 16:43:02 +00:00
|
|
|
|
} break;
|
2019-05-27 21:45:59 +00:00
|
|
|
|
|
2019-06-01 01:34:56 +00:00
|
|
|
|
case eMode.BankImport:
|
|
|
|
|
if (string.IsNullOrEmpty(csvInput))
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("You must provide an input file by using --csvinput=<file>");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-03-04 16:49:33 +00:00
|
|
|
|
if (!ProcessCsv.ProcessCSV(csvInput))
|
2019-05-27 21:45:59 +00:00
|
|
|
|
return 1;
|
|
|
|
|
break;
|
2019-05-16 16:14:54 +00:00
|
|
|
|
}
|
2019-05-15 16:42:43 +00:00
|
|
|
|
|
|
|
|
|
return 0;
|
2019-05-23 15:11:35 +00:00
|
|
|
|
}
|
2019-05-15 16:42:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|