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 == 501)
23 #define luaL_setfuncs(L, R, N) luaL_register(L, NULL, R)
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
34         if(status == 0) {
35                 DWORD i;
36                 lua_createtable(L, ips->AddrCount, 0);
37
38                 for(i = 0; i < ips->AddrCount; i++) {
39                         DWORD ip = ips->AddrArray[i];
40                         char ip_str[16] = "";
41                         sprintf_s(ip_str, sizeof(ip_str), "%d.%d.%d.%d", (ip >> 0) & 255, (ip >> 8) & 255, (ip >> 16) & 255, (ip >> 24) & 255);
42                         lua_pushstring(L, ip_str);
43                         lua_rawseti(L, -2, i + 1);
44                 }
45
46                 return 1;
47         } else {
48                 lua_pushnil(L);
49                 lua_pushfstring(L, "DnsQueryConfig returned %d", status);
50                 return 2;
51         }
52 }
53
54 static int lerror(lua_State* L, char* string) {
55         lua_pushnil(L);
56         lua_pushfstring(L, "%s: %d", string, GetLastError());
57         return 2;
58 }
59
60 static int Lget_consolecolor(lua_State* L) {
61         HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
62         WORD color;
63         DWORD read_len;
64
65         CONSOLE_SCREEN_BUFFER_INFO info;
66
67         if(console == INVALID_HANDLE_VALUE) {
68                 return lerror(L, "GetStdHandle");
69         }
70
71         if(!GetConsoleScreenBufferInfo(console, &info)) {
72                 return lerror(L, "GetConsoleScreenBufferInfo");
73         }
74
75         if(!ReadConsoleOutputAttribute(console, &color, 1, info.dwCursorPosition, &read_len)) {
76                 return lerror(L, "ReadConsoleOutputAttribute");
77         }
78
79         lua_pushnumber(L, color);
80         return 1;
81 }
82 static int Lset_consolecolor(lua_State* L) {
83         int color = luaL_checkint(L, 1);
84         HWND console = GetStdHandle(STD_OUTPUT_HANDLE);
85
86         if(console == INVALID_HANDLE_VALUE) {
87                 return lerror(L, "GetStdHandle");
88         }
89
90         if(!SetConsoleTextAttribute(console, color)) {
91                 return lerror(L, "SetConsoleTextAttribute");
92         }
93
94         lua_pushboolean(L, 1);
95         return 1;
96 }
97
98 static const luaL_Reg Reg[] = {
99         { "get_nameservers",    Lget_nameservers        },
100         { "get_consolecolor",   Lget_consolecolor       },
101         { "set_consolecolor",   Lset_consolecolor       },
102         { NULL,         NULL    }
103 };
104
105 LUALIB_API int luaopen_util_windows(lua_State* L) {
106         lua_newtable(L);
107         luaL_setfuncs(L, Reg, 0);
108         lua_pushliteral(L, "-3.14");
109         lua_setfield(L, -2, "version");
110         return 1;
111 }