Merge 0.10->trunk
[prosody.git] / util-src / windows.c
1 /* Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- 
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8 */
9
10 /*
11 * windows.c
12 * Windows support functions for Lua
13 */
14
15 #include <stdio.h>
16 #include <windows.h>
17 #include <windns.h>
18
19 #include "lua.h"
20 #include "lauxlib.h"
21
22 #if (LUA_VERSION_NUM == 502)
23 #define luaL_register(L, N, R) luaL_setfuncs(L, R, 0)
24 #endif
25
26 static int Lget_nameservers(lua_State *L) {
27         char stack_buffer[1024]; // stack allocated buffer
28         IP4_ARRAY* ips = (IP4_ARRAY*) stack_buffer;
29         DWORD len = sizeof(stack_buffer);
30         DNS_STATUS status;
31
32         status = DnsQueryConfig(DnsConfigDnsServerList, FALSE, NULL, NULL, ips, &len);
33         if (status == 0) {
34                 DWORD i;
35                 lua_createtable(L, ips->AddrCount, 0);
36                 for (i = 0; i < ips->AddrCount; i++) {
37                         DWORD ip = ips->AddrArray[i];
38                         char ip_str[16] = "";
39                         sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
40                         lua_pushstring(L, ip_str);
41                         lua_rawseti(L, -2, i+1);
42                 }
43                 return 1;
44         } else {
45                 lua_pushnil(L);
46                 lua_pushfstring(L, "DnsQueryConfig returned %d", status);
47                 return 2;
48         }
49 }
50
51 static int lerror(lua_State *L, char* string) {
52         lua_pushnil(L);
53         lua_pushfstring(L, "%s: %d", string, GetLastError());
54         return 2;
55 }
56
57 static int Lget_consolecolor(lua_State *L) {
58         HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
59         WORD color; DWORD read_len;
60         
61         CONSOLE_SCREEN_BUFFER_INFO info;
62         
63         if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
64         if (!GetConsoleScreenBufferInfo(console, &info)) return lerror(L, "GetConsoleScreenBufferInfo");
65         if (!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) return lerror(L, "ReadConsoleOutputAttribute");
66
67         lua_pushnumber(L, color);
68         return 1;
69 }
70 static int Lset_consolecolor(lua_State *L) {
71         int color = luaL_checkint(L, 1);
72         HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
73         if (console == INVALID_HANDLE_VALUE) return lerror(L, "GetStdHandle");
74         if (!SetConsoleTextAttribute(console, color)) return lerror(L, "SetConsoleTextAttribute");
75         lua_pushboolean(L, 1);
76         return 1;
77 }
78
79 static const luaL_Reg Reg[] =
80 {
81         { "get_nameservers",    Lget_nameservers        },
82         { "get_consolecolor",   Lget_consolecolor       },
83         { "set_consolecolor",   Lset_consolecolor       },
84         { NULL,         NULL    }
85 };
86
87 LUALIB_API int luaopen_util_windows(lua_State *L) {
88         lua_newtable(L);
89         luaL_register(L, NULL, Reg);
90         lua_pushliteral(L, "-3.14");
91         lua_setfield(L, -2, "version");
92         return 1;
93 }