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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
#include "stdafx.h"
void Log(const char *format, ...)
{
va_list args;
va_start(args, format);
Netlib_Log(hNetlib, CMStringA().FormatV(format, args));
va_end(args);
}
void Log(const wchar_t *format, ...)
{
va_list args;
va_start(args, format);
Netlib_LogW(hNetlib, CMStringW().FormatV(format, args));
va_end(args);
}
void ShowNotification(const char *caption, const char *message, int flags, MCONTACT hContact)
{
if (Miranda_IsTerminated())
return;
if (ServiceExists(MS_POPUP_ADDPOPUPT) && db_get_b(NULL, "Popup", "ModuleIsEnabled", 1))
{
POPUPDATA ppd = { 0 };
ppd.lchContact = hContact;
mir_strncpy(ppd.lpzContactName, caption, MAX_CONTACTNAME);
mir_strncpy(ppd.lpzText, message, MAX_SECONDLINE);
if (!PUAddPopup(&ppd))
return;
}
MessageBoxA(NULL, message, caption, MB_OK | flags);
}
void ObsoleteMethod(lua_State *L, const char *message)
{
lua_Debug ar;
if (lua_getstack(L, 0, &ar) == 0 || lua_getinfo(L, "n", &ar) == 0)
return;
char text[512];
mir_snprintf(text, "%s is obsolete. %s", ar.name, message);
Log(text);
if (db_get_b(NULL, MODULE, "PopupOnObsolete", 0))
ShowNotification(MODULE, text, MB_OK | MB_ICONWARNING, NULL);
}
void ReportError(const char *message)
{
Log(message);
if (db_get_b(NULL, MODULE, "PopupOnError", 0))
ShowNotification(MODULE, message, MB_OK | MB_ICONERROR);
}
int luaM_atpanic(lua_State *L)
{
ReportError(lua_tostring(L, -1));
return 0;
}
int luaM_pcall(lua_State *L, int n, int r)
{
int res = lua_pcall(L, n, r, 0);
if (res != LUA_OK)
ReportError(lua_tostring(L, -1));
return res;
}
int luaM_print(lua_State *L)
{
CMStringA data;
int nargs = lua_gettop(L);
for (int i = 1; i <= nargs; i++)
{
switch (lua_type(L, i))
{
case LUA_TNIL:
data.Append("nil");
break;
case LUA_TBOOLEAN:
data.AppendFormat("%s", lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
case LUA_TSTRING:
data.AppendFormat("%s", lua_tostring(L, i));
break;
default:
data.AppendFormat("%s(0x%p)", luaL_typename(L, i), lua_topointer(L, i));
break;
}
data += '\t';
}
if (data.GetLength() >= 1)
data.Delete(data.GetLength() - 1, 1);
Log(data.GetBuffer());
return 0;
}
int luaM_toansi(lua_State *L)
{
const char *value = luaL_checkstring(L, 1);
int codepage = luaL_optinteger(L, 2, Langpack_GetDefaultCodePage());
ptrA string(mir_strdup(value));
lua_pushstring(L, mir_utf8decodecp(string, codepage, NULL));
return 1;
}
int luaM_toucs2(lua_State *L)
{
const char *value = luaL_checkstring(L, 1);
ptrW unicode(mir_utf8decodeW(value));
size_t length = mir_wstrlen(unicode) * sizeof(wchar_t);
ptrA string((char*)mir_calloc(length + 1));
memcpy(string, unicode, length);
lua_pushlstring(L, string, length + 1);
return 1;
}
int luaM_topointer(lua_State *L)
{
switch (lua_type(L, 1))
{
case LUA_TBOOLEAN:
lua_pushlightuserdata(L, (void*)lua_toboolean(L, 1));
break;
case LUA_TNUMBER:
{
if (lua_isinteger(L, 1))
{
lua_Integer value = lua_tointeger(L, 1);
if (value > INTPTR_MAX)
{
const char *msg = lua_pushfstring(L, "%f is larger than %d", value, INTPTR_MAX);
return luaL_argerror(L, 1, msg);
}
lua_pushlightuserdata(L, (void*)value);
}
}
break;
case LUA_TSTRING:
lua_pushlightuserdata(L, (void*)lua_tostring(L, 1));
break;
case LUA_TLIGHTUSERDATA:
lua_pushvalue(L, 1);
default:
return luaL_argerror(L, 1, luaL_typename(L, 1));
}
return 1;
}
int luaM_tonumber(lua_State *L)
{
if (lua_islightuserdata(L, 1))
{
lua_Integer value = (lua_Integer)lua_touserdata(L, 1);
lua_pushinteger(L, value);
return 1;
}
int n = lua_gettop(L);
lua_getglobal(L, "_tonumber");
lua_pushvalue(L, 1);
if (n == 2)
lua_pushvalue(L, 2);
luaM_pcall(L, n, 1);
return 1;
}
UINT_PTR luaM_tomparam(lua_State *L, int idx)
{
switch (lua_type(L, idx))
{
case LUA_TBOOLEAN:
return lua_toboolean(L, idx);
case LUA_TSTRING:
return (UINT_PTR)lua_tostring(L, idx);
case LUA_TLIGHTUSERDATA:
return (UINT_PTR)lua_touserdata(L, idx);
case LUA_TNUMBER:
{
if (lua_isinteger(L, idx))
{
lua_Integer value = lua_tointeger(L, idx);
return value <= INTPTR_MAX
? (UINT_PTR)value
: NULL;
}
}
default:
return NULL;
}
}
int luaM_interpolate(lua_State *L)
{
const char *string = luaL_checkstring(L, 1);
char pattern[128];
if (lua_istable(L, 2))
{
for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 2))
{
lua_pushvalue(L, -2);
const char *key = lua_tostring(L, -1);
const char *val = lua_tostring(L, -2);
mir_snprintf(pattern, "{%s}", key);
string = luaL_gsub(L, string, pattern, val);
lua_pop(L, 1);
}
}
else
{
int nargs = lua_gettop(L);
for (int i = 2; i <= nargs; i++)
{
const char *val = lua_tostring(L, i);
mir_snprintf(pattern, "{%d}", i - 1);
string = luaL_gsub(L, string, pattern, val);
lua_pop(L, 1);
}
}
lua_Debug ar;
size_t level = 1;
while (lua_getstack(L, level++, &ar))
{
size_t i = 1;
while (const char *name = lua_getlocal(L, &ar, i++))
{
const char *val = lua_tostring(L, -1);
if (val)
{
mir_snprintf(pattern, "${%s}", name);
string = luaL_gsub(L, string, pattern, val);
lua_pop(L, 1);
}
}
}
lua_pushstring(L, string);
return 1;
}
bool luaM_toboolean(lua_State *L, int idx)
{
if (lua_isnumber(L, idx))
return lua_tonumber(L, idx) > 0;
return lua_toboolean(L, idx) > 0;
}
|