1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
#include "stdafx.h"
static void MapToTable(lua_State *L, const PROTOCOLDESCRIPTOR* pd)
{
lua_newtable(L);
lua_pushliteral(L, "Name");
lua_pushstring(L, ptrA(mir_utf8encode(pd->szName)));
lua_settable(L, -3);
lua_pushliteral(L, "Type");
lua_pushinteger(L, pd->type);
lua_settable(L, -3);
}
static int lua_GetProto(lua_State *L)
{
ptrA name(mir_utf8decodeA(luaL_checkstring(L, 1)));
PROTOCOLDESCRIPTOR* pd = ::Proto_IsProtocolLoaded(name);
if (pd)
MapToTable(L, pd);
else
lua_pushnil(L);
return 1;
}
static int lua_ProtoIterator(lua_State *L)
{
int i = lua_tointeger(L, lua_upvalueindex(1));
int count = lua_tointeger(L, lua_upvalueindex(2));
PROTOCOLDESCRIPTOR** protos = (PROTOCOLDESCRIPTOR**)lua_touserdata(L, lua_upvalueindex(3));
if (i < count)
{
lua_pushinteger(L, (i + 1));
lua_replace(L, lua_upvalueindex(1));
MapToTable(L, protos[i]);
}
else
lua_pushnil(L);
return 1;
}
static int lua_AllProtos(lua_State *L)
{
int count;
PROTOCOLDESCRIPTOR** protos;
Proto_EnumProtocols(&count, &protos);
lua_pushinteger(L, 0);
lua_pushinteger(L, count);
lua_pushlightuserdata(L, protos);
lua_pushcclosure(L, lua_ProtoIterator, 3);
return 1;
}
static int lua_EnumProtos(lua_State *L)
{
if (!lua_isfunction(L, 1))
{
lua_pushlightuserdata(L, NULL);
return 1;
}
lua_pushvalue(L, 1);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
int count;
PROTOCOLDESCRIPTOR** protos;
Proto_EnumProtocols(&count, &protos);
for (int i = 0; i < count; i++)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
MapToTable(L, protos[i]);
if (lua_pcall(L, 1, 0, 0))
CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
}
luaL_unref(L, LUA_REGISTRYINDEX, ref);
lua_pushinteger(L, count);
return 1;
}
static void MapToTable(lua_State *L, const PROTOACCOUNT* pa)
{
lua_newtable(L);
lua_pushliteral(L, "InternalName");
lua_pushstring(L, ptrA(mir_utf8encode(pa->szModuleName)));
lua_settable(L, -3);
lua_pushliteral(L, "AccountName");
lua_pushstring(L, ptrA(mir_utf8encodeT(pa->tszAccountName)));
lua_settable(L, -3);
lua_pushliteral(L, "ProtoName");
lua_pushstring(L, ptrA(mir_utf8encode(pa->szProtoName)));
lua_settable(L, -3);
lua_pushliteral(L, "IsEnabled");
lua_pushboolean(L, pa->bIsEnabled);
lua_settable(L, -3);
lua_pushliteral(L, "IsVisible");
lua_pushboolean(L, pa->bIsVisible);
lua_settable(L, -3);
lua_pushliteral(L, "IsVirtual");
lua_pushboolean(L, pa->bIsVirtual);
lua_settable(L, -3);
lua_pushliteral(L, "OldProto");
lua_pushboolean(L, pa->bOldProto);
lua_settable(L, -3);
}
static int lua_GetAccount(lua_State *L)
{
ptrA moduleName(mir_utf8decodeA(luaL_checkstring(L, 1)));
PROTOACCOUNT* pa = ::Proto_GetAccount(moduleName);
if (pa)
MapToTable(L, pa);
else
lua_pushnil(L);
return 1;
}
static int lua_AccountIterator(lua_State *L)
{
int i = lua_tointeger(L, lua_upvalueindex(1));
int count = lua_tointeger(L, lua_upvalueindex(2));
PROTOACCOUNT** accounts = (PROTOACCOUNT**)lua_touserdata(L, lua_upvalueindex(3));
if (i < count)
{
lua_pushinteger(L, (i + 1));
lua_replace(L, lua_upvalueindex(1));
MapToTable(L, accounts[i]);
}
else
lua_pushnil(L);
return 1;
}
static int lua_AllAccounts(lua_State *L)
{
int count;
PROTOACCOUNT** accounts;
Proto_EnumAccounts(&count, &accounts);
lua_pushinteger(L, 0);
lua_pushinteger(L, count);
lua_pushlightuserdata(L, accounts);
lua_pushcclosure(L, lua_AccountIterator, 3);
return 1;
}
static int lua_EnumAccounts(lua_State *L)
{
if (!lua_isfunction(L, 1))
{
lua_pushlightuserdata(L, NULL);
return 1;
}
lua_pushvalue(L, 1);
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
int count;
PROTOACCOUNT** accounts;
Proto_EnumAccounts(&count, &accounts);
for (int i = 0; i < count; i++)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
MapToTable(L, accounts[i]);
if (lua_pcall(L, 1, 0, 0))
CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(L, -1));
}
luaL_unref(L, LUA_REGISTRYINDEX, ref);
lua_pushinteger(L, count);
return 1;
}
static luaL_Reg protocolsApi[] =
{
{ "GetProto", lua_GetProto },
{ "AllProtos", lua_AllProtos },
{ "EnumProtos", lua_EnumProtos },
{ "GetAccount", lua_GetAccount },
{ "AllAccounts", lua_AllAccounts },
{ "EnumAccounts", lua_EnumAccounts },
{ NULL, NULL }
};
LUAMOD_API int luaopen_m_protocols(lua_State *L)
{
luaL_newlib(L, protocolsApi);
return 1;
}
|