mikoto/source/app.d

116 lines
3.2 KiB
D

import std.stdio;
import core.thread;
enum CONFIG_PATH = "mikoto.cfg";
__gshared bool hasCancelled;
void main() {
import std.file : exists;
import std.range : padLeft;
import std.string : format;
version(Windows) {
import core.sys.windows.windows : SetConsoleCtrlHandler, TRUE;
if(!SetConsoleCtrlHandler(&ctrlHandler, TRUE))
stderr.writeln("Failed to register Windows CTRL handler.");
}
version(Posix) {
import core.stdc.signal : signal, SIGINT;
signal(SIGINT, &signalHander);
}
if(hasCancelled) return;
writeln(r" __ ____ __ __ ");
writeln(r" / |/ (_) /______ / /_____ ");
writeln(r" / /|_/ / / //_/ __ \/ __/ __ \");
writeln(r" / / / / / ,< / /_/ / /_/ /_/ /");
writeln(r"/_/ /_/_/_/|_|\____/\__/\____/ ");
debug writeln("== %s ========= DEBUG ==".format("aabbccddee"));
else writeln("v0.0.0".padLeft(' ', 30));
if(hasCancelled) return;
string configPath = CONFIG_PATH;
if(!configPath.exists && configPath == CONFIG_PATH)
createExampleConfig(configPath);
if(hasCancelled) return;
for(;;) {
Thread.sleep(1.seconds);
if(hasCancelled)
break;
}
writeln("Exiting!");
}
void createExampleConfig(string path) {
auto cfg = File(path, "wb+");
cfg.writeln("# and ; can be used at the start of a line for comments.");
cfg.writeln();
cfg.writeln("# General Configuration");
cfg.writeln(";chat:port {SockChatServer.DEFAULT_PORT}");
cfg.writeln(";chat:msgMaxLength {SockChatServer.DEFAULT_MSG_LENGTH_MAX}");
cfg.writeln(";chat:connMaxCount {SockChatServer.DEFAULT_MAX_CONNECTIONS}");
cfg.writeln(";chat:floodKickLength {SockChatServer.DEFAULT_FLOOD_KICK_LENGTH}");
cfg.writeln();
cfg.writeln("# Channels");
cfg.writeln("chat:channels lounge staff");
cfg.writeln();
cfg.writeln("# Lounge channel settings");
cfg.writeln("chat:channels:lounge:name Lounge");
cfg.writeln("chat:channels:lounge:autoJoin true");
cfg.writeln();
cfg.writeln("# Staff channel settings");
cfg.writeln("chat:channels:staff:name Staff");
cfg.writeln("chat:channels:staff:minRank 5");
cfg.writeln();
cfg.writeln("# Misuzu integration settings");
cfg.writeln(";msz:secret woomy");
cfg.writeln(";msz:url https://flashii.net/_sockchat");
cfg.writeln();
cfg.writeln("# MariaDB configuration");
cfg.writeln(";mariadb:host <host>");
cfg.writeln(";mariadb:user <username>");
cfg.writeln(";mariadb:pass <password>");
cfg.writeln(";mariadb:db <database>");
}
version(Posix) {
extern(C) void signalHander(int num) nothrow @nogc @system {
import core.stdc.signal : SIGINT;
if(num == SIGINT)
hasCancelled = true;
}
}
version(Windows) {
import core.sys.windows.windows : BOOL, DWORD;
extern(Windows) BOOL ctrlHandler(DWORD fdwCtrlType) nothrow @nogc @system {
import core.sys.windows.windows : CTRL_C_EVENT, CTRL_CLOSE_EVENT, TRUE, FALSE;
switch(fdwCtrlType) {
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
hasCancelled = true;
return TRUE;
default:
return FALSE;
}
}
}