# HG changeset patch
# User Vitaly Orekhov
# Date 1677191666 -10800
#      Fri Feb 24 01:34:26 2023 +0300
# Node ID b5a8b462184b898d7a216639dac32fa76813468a
# Parent  5c90862e39aa0fdff0df02a22297fb47c2ab77a8
util.windows: Add new functions to prepare for Windows support comeback

Windows support had been deprecated, but not ended yet. Maintaining Windows
version of Prosody will be done by me for now.

diff -r 5c90862e39aa -r b5a8b462184b util-src/windows.c
--- a/util-src/windows.c	Thu Feb 23 18:39:02 2023 +0100
+++ b/util-src/windows.c	Fri Feb 24 01:34:26 2023 +0300
@@ -31,11 +31,11 @@
 
 	status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);
 
-	if(status == 0) {
+	if (status == 0) {
 		DWORD i;
 		lua_createtable(L, ips->AddrCount, 0);
 
-		for(i = 0; i < ips->AddrCount; i++) {
+		for (i = 0; i < ips->AddrCount; i++) {
 			DWORD ip = ips->AddrArray[i];
 			char ip_str[16] = "";
 			sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
@@ -64,30 +64,31 @@
 
 	CONSOLE_SCREEN_BUFFER_INFO info;
 
-	if(console == INVALID_HANDLE_VALUE) {
+	if (console == INVALID_HANDLE_VALUE) {
 		return lerror(L, "GetStdHandle");
 	}
 
-	if(!GetConsoleScreenBufferInfo(console, &info)) {
+	if (!GetConsoleScreenBufferInfo(console, &info)) {
 		return lerror(L, "GetConsoleScreenBufferInfo");
 	}
 
-	if(!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) {
+	if (!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) {
 		return lerror(L, "ReadConsoleOutputAttribute");
 	}
 
 	lua_pushnumber(L, color);
 	return 1;
 }
-static int Lset_consolecolor(lua_State *L) {
-	int color = luaL_checkint(L, 1);
+
+static int Lset_consolecolor(lua_State* L) {
+	int color = luaL_checkinteger(L, 1);
 	HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
 
-	if(console == INVALID_HANDLE_VALUE) {
+	if (console == INVALID_HANDLE_VALUE) {
 		return lerror(L, "GetStdHandle");
 	}
 
-	if(!SetConsoleTextAttribute(console, color)) {
+	if (!SetConsoleTextAttribute(console, color)) {
 		return lerror(L, "SetConsoleTextAttribute");
 	}
 
@@ -95,10 +96,72 @@
 	return 1;
 }
 
+static int Lset_consoletitle(lua_State* L) {
+	const char* szConsoleTitle;
+	szConsoleTitle = lua_tostring(L, 1);
+
+	if (!SetConsoleTitle(szConsoleTitle)) {
+		return lerror(L, "SetConsoleTitle");
+	}
+
+	lua_pushboolean(L, 1);
+	return 1;
+}
+
+static int Lget_windowsbuild(lua_State* L) {
+	char buildLab[255];
+	DWORD dBufferSize = 1024;
+
+	RegGetValueA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", "BuildLab", RRF_RT_REG_SZ, NULL, (PVOID)&buildLab, &dBufferSize);
+	lua_pushstring(L, buildLab);
+
+	return 1;
+}
+
+static int lget_processelevation(lua_State* L) {
+	BOOL fRet = FALSE;
+	HANDLE hToken = NULL;
+	if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
+		TOKEN_ELEVATION Elevation;
+		DWORD cbSize = sizeof(TOKEN_ELEVATION);
+		if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize)) {
+			fRet = Elevation.TokenIsElevated;
+		}
+	}
+	if (hToken) {
+		CloseHandle(hToken);
+	}
+	lua_pushboolean(L, fRet);
+	return 1;
+}
+
+static int Levtp_start(lua_State* L) {
+	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
+	if (hOut == INVALID_HANDLE_VALUE) {
+		return lerror(L, "GetStdHandle");
+	}
+
+	DWORD dwMode = 0;
+	if (!GetConsoleMode(hOut, &dwMode)) {
+		return lerror(L, "GetConsoleMode");
+	}
+
+	dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_INPUT;
+	if (!SetConsoleMode(hOut, dwMode)) {
+		return lerror(L, "SetConsoleMode");
+	}
+
+	return 1;
+}
+
 static const luaL_Reg Reg[] = {
-	{ "get_nameservers",	Lget_nameservers	},
-	{ "get_consolecolor",	Lget_consolecolor	},
-	{ "set_consolecolor",	Lset_consolecolor	},
+	{ "get_nameservers",		Lget_nameservers		},
+	{ "get_consolecolor",		Lget_consolecolor		},
+	{ "set_consolecolor",		Lset_consolecolor		},
+	{ "set_consoletitle",		Lset_consoletitle		},
+	{ "get_processelevation",	lget_processelevation	},
+	{ "get_windowsbuild",		Lget_windowsbuild		},
+	{ "evtp_start",				Levtp_start				},
 	{ NULL,		NULL	}
 };
 
