diff options
author | aunsane <aunsane@gmail.com> | 2018-05-20 21:10:10 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2018-05-20 21:34:31 +0300 |
commit | a955d18f62f335f84e0926f351eb85d78224cba6 (patch) | |
tree | 7bd4d30040ec32bba5949dd2245cb68617fb4192 /plugins | |
parent | 46363eef857b69761f1d6d28da5a53a954f76900 (diff) |
MirLua: project reordering
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/MirLua/MirLua.vcxproj | 4 | ||||
-rw-r--r-- | plugins/MirLua/MirLua.vcxproj.filters | 28 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_chat.cpp (renamed from plugins/MirLua/src/m_chat.cpp) | 50 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_clist.cpp (renamed from plugins/MirLua/src/m_clist.cpp) | 240 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_core.cpp (renamed from plugins/MirLua/src/m_core.cpp) | 864 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_database.cpp (renamed from plugins/MirLua/src/m_database.cpp) | 1382 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_genmenu.cpp (renamed from plugins/MirLua/src/m_genmenu.cpp) | 228 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_hotkeys.cpp (renamed from plugins/MirLua/src/m_hotkeys.cpp) | 206 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_http.cpp (renamed from plugins/MirLua/src/m_http.cpp) | 2 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_icolib.cpp (renamed from plugins/MirLua/src/m_icolib.cpp) | 17 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_json.cpp (renamed from plugins/MirLua/src/m_json.cpp) | 4 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_message.cpp (renamed from plugins/MirLua/src/m_message.cpp) | 146 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_options.cpp (renamed from plugins/MirLua/src/m_options.cpp) | 232 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_protocols.cpp (renamed from plugins/MirLua/src/m_protocols.cpp) | 7 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_sounds.cpp (renamed from plugins/MirLua/src/m_sounds.cpp) | 8 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_srmm.cpp (renamed from plugins/MirLua/src/m_srmm.cpp) | 2 | ||||
-rw-r--r-- | plugins/MirLua/src/environment.cpp (renamed from plugins/MirLua/src/mlua_environment.cpp) | 0 | ||||
-rw-r--r-- | plugins/MirLua/src/environment.h (renamed from plugins/MirLua/src/mlua_environment.h) | 0 | ||||
-rw-r--r-- | plugins/MirLua/src/function_loader.cpp (renamed from plugins/MirLua/src/mlua_function_loader.cpp) | 0 | ||||
-rw-r--r-- | plugins/MirLua/src/function_loader.h (renamed from plugins/MirLua/src/mlua_function_loader.h) | 0 | ||||
-rw-r--r-- | plugins/MirLua/src/icons.cpp (renamed from plugins/MirLua/src/mlua_icons.cpp) | 64 | ||||
-rw-r--r-- | plugins/MirLua/src/m_database.h | 9 | ||||
-rw-r--r-- | plugins/MirLua/src/m_genmenu.h | 11 | ||||
-rw-r--r-- | plugins/MirLua/src/m_json.h | 9 | ||||
-rw-r--r-- | plugins/MirLua/src/m_protocols.h | 13 | ||||
-rw-r--r-- | plugins/MirLua/src/metatable.h (renamed from plugins/MirLua/src/mlua_metatable.h) | 5 | ||||
-rw-r--r-- | plugins/MirLua/src/module_loader.cpp (renamed from plugins/MirLua/src/mlua_module_loader.cpp) | 90 | ||||
-rw-r--r-- | plugins/MirLua/src/module_loader.h (renamed from plugins/MirLua/src/mlua_module_loader.h) | 34 | ||||
-rw-r--r-- | plugins/MirLua/src/modules.h | 44 | ||||
-rw-r--r-- | plugins/MirLua/src/options.cpp (renamed from plugins/MirLua/src/mlua_options.cpp) | 11 | ||||
-rw-r--r-- | plugins/MirLua/src/options.h (renamed from plugins/MirLua/src/mlua_options.h) | 8 | ||||
-rw-r--r-- | plugins/MirLua/src/script.cpp (renamed from plugins/MirLua/src/mlua_script.cpp) | 16 | ||||
-rw-r--r-- | plugins/MirLua/src/script.h (renamed from plugins/MirLua/src/mlua_script.h) | 8 | ||||
-rw-r--r-- | plugins/MirLua/src/script_loader.cpp (renamed from plugins/MirLua/src/mlua_script_loader.cpp) | 2 | ||||
-rw-r--r-- | plugins/MirLua/src/script_loader.h (renamed from plugins/MirLua/src/mlua_script_loader.h) | 34 | ||||
-rw-r--r-- | plugins/MirLua/src/stdafx.h | 63 | ||||
-rw-r--r-- | plugins/MirLua/src/utils.cpp (renamed from plugins/MirLua/src/mlua_utils.cpp) | 196 |
37 files changed, 2011 insertions, 2026 deletions
diff --git a/plugins/MirLua/MirLua.vcxproj b/plugins/MirLua/MirLua.vcxproj index 1872992a5f..5869a1fb6a 100644 --- a/plugins/MirLua/MirLua.vcxproj +++ b/plugins/MirLua/MirLua.vcxproj @@ -39,6 +39,10 @@ </ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="include\*.h" />
+ <ClInclude Include="src\Modules\*.h" />
+ <ClCompile Include="src\Modules\*.cpp">
+ <PrecompiledHeaderFile>..\stdafx.h</PrecompiledHeaderFile>
+ </ClCompile>
<None Include="res\*.ico" />
</ItemGroup>
</Project>
diff --git a/plugins/MirLua/MirLua.vcxproj.filters b/plugins/MirLua/MirLua.vcxproj.filters index a8ab01c0e5..4109e86555 100644 --- a/plugins/MirLua/MirLua.vcxproj.filters +++ b/plugins/MirLua/MirLua.vcxproj.filters @@ -1,4 +1,32 @@ <?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <ItemGroup>
+ <Filter Include="Header Files\Modules">
+ <UniqueIdentifier>{A6D8C279-52F8-4F14-960D-C95F06EB80C6}</UniqueIdentifier>
+ </Filter>
+ <Filter Include="Source Files\Modules">
+ <UniqueIdentifier>{B44E7536-934D-4B31-865F-FDE5D0C1ED32}</UniqueIdentifier>
+ </Filter>
+ </ItemGroup>
+ <ItemGroup>
+ <ClInclude Include="src\*.h">
+ <Filter>Header Files</Filter>
+ </ClInclude>
+ <ClInclude Include="src\Modules\*.h">
+ <Filter>Header Files\Services</Filter>
+ </ClInclude>
+ <ClCompile Include="src\*.cpp;src\*.cxx">
+ <Filter>Source Files</Filter>
+ </ClCompile>
+ <ClCompile Include="src\Modules\*.cpp">
+ <Filter>Source Files\Services</Filter>
+ </ClCompile>
+ <ResourceCompile Include="res\*.rc">
+ <Filter>Resource Files</Filter>
+ </ResourceCompile>
+ <None Include="res\*.ico;res\*.bmp;res\*.cur">
+ <Filter>Resource Files</Filter>
+ </None>
+ </ItemGroup>
<Import Project="$(ProjectDir)..\..\build\vc.common\common.filters" />
</Project>
diff --git a/plugins/MirLua/src/m_chat.cpp b/plugins/MirLua/src/Modules/m_chat.cpp index ed57b7b9de..c1a2a65d9b 100644 --- a/plugins/MirLua/src/m_chat.cpp +++ b/plugins/MirLua/src/Modules/m_chat.cpp @@ -1,25 +1,25 @@ -#include "stdafx.h"
-
-static luaL_Reg chatApi[] =
-{
- { nullptr, nullptr }
-};
-
-LUAMOD_API int luaopen_m_chat(lua_State *L)
-{
- luaL_newlib(L, chatApi);
-
- MT<GCEVENT>(L, "GCEVENT")
- .Field(&GCEVENT::pszModule, "Module", LUA_TSTRINGA)
- .Field(&GCEVENT::ptszID, "Id", LUA_TSTRINGW)
- .Field(&GCEVENT::iType, "Type", LUA_TINTEGER)
- .Field(&GCEVENT::time, "Timestamp", LUA_TINTEGER)
- .Field(&GCEVENT::time, "IsMe", LUA_TINTEGER)
- .Field(&GCEVENT::time, "Flags", LUA_TINTEGER)
- .Field(&GCEVENT::ptszNick, "Nick", LUA_TSTRINGW)
- .Field(&GCEVENT::ptszUID, "Uid", LUA_TSTRINGW)
- .Field(&GCEVENT::ptszStatus, "Status", LUA_TSTRINGW)
- .Field(&GCEVENT::ptszText, "Text", LUA_TSTRINGW);
-
- return 1;
-}
+#include "../stdafx.h" + +static luaL_Reg chatApi[] = +{ + { nullptr, nullptr } +}; + +LUAMOD_API int luaopen_m_chat(lua_State *L) +{ + luaL_newlib(L, chatApi); + + MT<GCEVENT>(L, "GCEVENT") + .Field(&GCEVENT::pszModule, "Module", LUA_TSTRINGA) + .Field(&GCEVENT::ptszID, "Id", LUA_TSTRINGW) + .Field(&GCEVENT::iType, "Type", LUA_TINTEGER) + .Field(&GCEVENT::time, "Timestamp", LUA_TINTEGER) + .Field(&GCEVENT::time, "IsMe", LUA_TINTEGER) + .Field(&GCEVENT::time, "Flags", LUA_TINTEGER) + .Field(&GCEVENT::ptszNick, "Nick", LUA_TSTRINGW) + .Field(&GCEVENT::ptszUID, "Uid", LUA_TSTRINGW) + .Field(&GCEVENT::ptszStatus, "Status", LUA_TSTRINGW) + .Field(&GCEVENT::ptszText, "Text", LUA_TSTRINGW); + + return 1; +} diff --git a/plugins/MirLua/src/m_clist.cpp b/plugins/MirLua/src/Modules/m_clist.cpp index cc32c188db..5e5b7475b8 100644 --- a/plugins/MirLua/src/m_clist.cpp +++ b/plugins/MirLua/src/Modules/m_clist.cpp @@ -1,100 +1,140 @@ -#include "stdafx.h"
-
-static int clist_AddMainMenuRoot(lua_State *L)
-{
- const char *name = luaL_checkstring(L, 1);
- int position = lua_tointeger(L, 2);
- HANDLE hIcon = (HANDLE)lua_touserdata(L, 3);
-
- HGENMENU res = Menu_CreateRoot(MO_MAIN, ptrW(Utf8DecodeW(name)), position, hIcon);
- if (res != nullptr)
- lua_pushlightuserdata(L, res);
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static int clist_AddMainMenuItem(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TTABLE);
-
- CMenuItem mi;
- MakeMenuItem(L, mi);
-
- HGENMENU res = Menu_AddMainMenuItem(&mi);
- if (res != nullptr)
- lua_pushlightuserdata(L, res);
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static int clist_AddContactMenuRoot(lua_State *L)
-{
- const char *name = luaL_checkstring(L, 1);
- int position = lua_tointeger(L, 2);
- HANDLE hIcon = (HANDLE)lua_touserdata(L, 3);
-
- HGENMENU res = Menu_CreateRoot(MO_MAIN, ptrW(Utf8DecodeW(name)), position, hIcon);
- if (res != nullptr)
- lua_pushlightuserdata(L, res);
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static int clist_AddContactMenuItem(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TTABLE);
-
- CMenuItem mi;
- MakeMenuItem(L, mi);
-
- ptrA szProto(mir_utf8decodeA(lua_tostring(L, 2)));
- HGENMENU res = Menu_AddContactMenuItem(&mi, szProto);
- if (res != nullptr)
- lua_pushlightuserdata(L, res);
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static int clist_AddTrayMenuItem(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TTABLE);
-
- CMenuItem mi;
- MakeMenuItem(L, mi);
-
- HGENMENU res = Menu_AddTrayMenuItem(&mi);
- if (res != nullptr)
- lua_pushlightuserdata(L, res);
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static luaL_Reg clistApi[] =
-{
- { "AddMainMenuRoot", clist_AddMainMenuRoot },
- { "AddMainMenuItem", clist_AddMainMenuItem },
-
- { "AddContactMenuRoot", clist_AddContactMenuRoot },
- { "AddContactMenuItem", clist_AddContactMenuItem },
-
- { "AddTrayMenuItem", clist_AddTrayMenuItem },
-
- { nullptr, nullptr }
-};
-
-LUAMOD_API int luaopen_m_clist(lua_State *L)
-{
- luaL_newlib(L, clistApi);
-
- return 1;
-}
+#include "../stdafx.h" + + + +void MakeMenuItem(lua_State *L, CMenuItem &mi) +{ + mi.langId = CMLuaEnvironment::GetEnvironmentId(L); + + lua_getfield(L, -1, "Flags"); + mi.flags = lua_tointeger(L, -1); + lua_pop(L, 1); + + if (!(mi.flags & CMIF_UNICODE)) + mi.flags |= CMIF_UNICODE; + + lua_getfield(L, -1, "Uid"); + const char* uuid = lua_tostring(L, -1); + if (UuidFromStringA((RPC_CSTR)uuid, (UUID*)&mi.uid)) + UNSET_UID(mi); + lua_pop(L, 1); + + lua_getfield(L, -1, "Name"); + mi.name.w = mir_utf8decodeW(luaL_checkstring(L, -1)); + lua_pop(L, 1); + + lua_getfield(L, -1, "Position"); + mi.position = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "Icon"); + mi.hIcolibItem = (HANDLE)lua_touserdata(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "Service"); + mi.pszService = lua_tostring(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "Parent"); + mi.root = (HGENMENU)lua_touserdata(L, -1); + lua_pop(L, 1); +} + +static int clist_AddMainMenuRoot(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + int position = lua_tointeger(L, 2); + HANDLE hIcon = (HANDLE)lua_touserdata(L, 3); + + HGENMENU res = Menu_CreateRoot(MO_MAIN, ptrW(Utf8DecodeW(name)), position, hIcon); + if (res != nullptr) + lua_pushlightuserdata(L, res); + else + lua_pushnil(L); + + return 1; +} + +static int clist_AddMainMenuItem(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TTABLE); + + CMenuItem mi; + MakeMenuItem(L, mi); + + HGENMENU res = Menu_AddMainMenuItem(&mi); + if (res != nullptr) + lua_pushlightuserdata(L, res); + else + lua_pushnil(L); + + return 1; +} + +static int clist_AddContactMenuRoot(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + int position = lua_tointeger(L, 2); + HANDLE hIcon = (HANDLE)lua_touserdata(L, 3); + + HGENMENU res = Menu_CreateRoot(MO_MAIN, ptrW(Utf8DecodeW(name)), position, hIcon); + if (res != nullptr) + lua_pushlightuserdata(L, res); + else + lua_pushnil(L); + + return 1; +} + +static int clist_AddContactMenuItem(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TTABLE); + + CMenuItem mi; + MakeMenuItem(L, mi); + + ptrA szProto(mir_utf8decodeA(lua_tostring(L, 2))); + HGENMENU res = Menu_AddContactMenuItem(&mi, szProto); + if (res != nullptr) + lua_pushlightuserdata(L, res); + else + lua_pushnil(L); + + return 1; +} + +static int clist_AddTrayMenuItem(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TTABLE); + + CMenuItem mi; + MakeMenuItem(L, mi); + + HGENMENU res = Menu_AddTrayMenuItem(&mi); + if (res != nullptr) + lua_pushlightuserdata(L, res); + else + lua_pushnil(L); + + return 1; +} + +static luaL_Reg clistApi[] = +{ + { "AddMainMenuRoot", clist_AddMainMenuRoot }, + { "AddMainMenuItem", clist_AddMainMenuItem }, + + { "AddContactMenuRoot", clist_AddContactMenuRoot }, + { "AddContactMenuItem", clist_AddContactMenuItem }, + + { "AddTrayMenuItem", clist_AddTrayMenuItem }, + + { nullptr, nullptr } +}; + +LUAMOD_API int luaopen_m_clist(lua_State *L) +{ + luaL_newlib(L, clistApi); + + return 1; +} diff --git a/plugins/MirLua/src/m_core.cpp b/plugins/MirLua/src/Modules/m_core.cpp index 9bcbdd5205..027ff163ed 100644 --- a/plugins/MirLua/src/m_core.cpp +++ b/plugins/MirLua/src/Modules/m_core.cpp @@ -1,433 +1,433 @@ -#include "stdafx.h"
-
-static int core_CreateHookableEvent(lua_State *L)
-{
- const char *name = luaL_checkstring(L, 1);
-
- HANDLE res = CreateHookableEvent(name);
- if (res != nullptr)
- lua_pushlightuserdata(L, res);
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-int HookEventLuaParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
-{
- lua_State *L = (lua_State*)obj;
-
- int ref = param;
- lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
-
- if (wParam)
- lua_pushlightuserdata(L, (void*)wParam);
- else
- lua_pushnil(L);
-
- if (lParam)
- lua_pushlightuserdata(L, (void*)lParam);
- else
- lua_pushnil(L);
-
- luaM_pcall(L, 2, 1);
-
- return lua_tointeger(L, -1);
-}
-
-int HookEventEnvParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
-{
- CMLuaEnvironment *env = (CMLuaEnvironment*)obj;
-
- int ref = param;
- lua_rawgeti(env->L, LUA_REGISTRYINDEX, ref);
-
- if (wParam)
- lua_pushlightuserdata(env->L, (void*)wParam);
- else
- lua_pushnil(env->L);
-
- if (lParam)
- lua_pushlightuserdata(env->L, (void*)lParam);
- else
- lua_pushnil(env->L);
-
- luaM_pcall(env->L, 2, 1);
-
- return lua_tointeger(env->L, -1);
-}
-
-static int core_HookEvent(lua_State *L)
-{
- const char *name = luaL_checkstring(L, 1);
- luaL_checktype(L, 2, LUA_TFUNCTION);
-
- lua_pushvalue(L, 2);
- int ref = luaL_ref(L, LUA_REGISTRYINDEX);
-
- HANDLE res = nullptr;
- CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
- if (env) {
- res = HookEventObjParam(name, HookEventEnvParam, env, ref);
- if (res)
- env->AddHookRef(res, ref);
- }
- else
- res = HookEventObjParam(name, HookEventLuaParam, L, ref);
-
- if (res == nullptr) {
- luaL_unref(L, LUA_REGISTRYINDEX, ref);
- lua_pushnil(L);
-
- return 1;
- }
-
- lua_pushlightuserdata(L, res);
-
- return 1;
-}
-
-static int core_HookTemporaryEvent(lua_State *L)
-{
- const char *name = luaL_checkstring(L, 1);
- luaL_checktype(L, 2, LUA_TFUNCTION);
-
- lua_pushvalue(L, 2);
- int ref = luaL_ref(L, LUA_REGISTRYINDEX);
-
- HANDLE res = nullptr;
- CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
- if (env) {
- res = HookEventObjParam(name, HookEventEnvParam, env, ref);
- if (res)
- env->AddHookRef(res, ref);
- }
- else
- res = HookEventObjParam(name, HookEventLuaParam, L, ref);
-
- // event does not exists, call hook immideatelly
- if (res == nullptr) {
- luaL_unref(L, LUA_REGISTRYINDEX, ref);
- lua_pushnil(L);
- lua_pushnil(L);
- luaM_pcall(L, 2, 1);
- return lua_tointeger(env->L, -1);
- }
-
- lua_pushlightuserdata(L, res);
-
- return 1;
-}
-
-static int core_UnhookEvent(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HANDLE hEvent = lua_touserdata(L, 1);
-
- int res = UnhookEvent(hEvent);
- if (!res) {
- CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
- if (env)
- env->ReleaseHookRef(hEvent);
- }
- lua_pushboolean(L, !res);
-
- return 1;
-}
-
-static int core_NotifyEventHooks(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HANDLE hEvent = lua_touserdata(L, 1);
- WPARAM wParam = (WPARAM)luaM_tomparam(L, 2);
- LPARAM lParam = (LPARAM)luaM_tomparam(L, 3);
-
- int res = NotifyEventHooks(hEvent, wParam, lParam);
- lua_pushboolean(L, res != -1);
-
- return 1;
-}
-
-/***********************************************/
-
-INT_PTR CreateServiceFunctionLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
-{
- lua_State *L = (lua_State*)obj;
-
- int ref = param;
- lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
-
- lua_pushlightuserdata(L, (void*)wParam);
- lua_pushlightuserdata(L, (void*)lParam);
- luaM_pcall(L, 2, 1);
-
- INT_PTR res = lua_tointeger(L, 1);
- lua_pushinteger(L, res);
-
- return res;
-}
-
-INT_PTR CreateServiceFunctionEnvParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param)
-{
- CMLuaEnvironment *env = (CMLuaEnvironment*)obj;
-
- int ref = param;
- lua_rawgeti(env->L, LUA_REGISTRYINDEX, ref);
-
- lua_pushlightuserdata(env->L, (void*)wParam);
- lua_pushlightuserdata(env->L, (void*)lParam);
- luaM_pcall(env->L, 2, 1);
-
- INT_PTR res = lua_tointeger(env->L, 1);
- lua_pushinteger(env->L, res);
-
- return res;
-}
-
-static int core_CreateServiceFunction(lua_State *L)
-{
- const char *name = luaL_checkstring(L, 1);
- luaL_checktype(L, 2, LUA_TFUNCTION);
-
- lua_pushvalue(L, 2);
- int ref = luaL_ref(L, LUA_REGISTRYINDEX);
-
- HANDLE res = nullptr;
- CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
- if (env) {
- res = CreateServiceFunctionObjParam(name, CreateServiceFunctionEnvParam, env, ref);
- if (res)
- env->AddServiceRef(res, ref);
- }
- else
- res = CreateServiceFunctionObjParam(name, CreateServiceFunctionLuaStateParam, L, ref);
-
- if (!res) {
- luaL_unref(L, LUA_REGISTRYINDEX, ref);
- lua_pushnil(L);
- return 1;
- }
-
- lua_pushlightuserdata(L, res);
-
- return 1;
-}
-
-static int core_CallService(lua_State *L)
-{
- const char *name = luaL_checkstring(L, 1);
- WPARAM wParam = (WPARAM)luaM_tomparam(L, 2);
- LPARAM lParam = (LPARAM)luaM_tomparam(L, 3);
-
- INT_PTR res = CallService(name, wParam, lParam);
- lua_pushinteger(L, res);
-
- return 1;
-}
-
-static int core_ServiceExists(lua_State *L)
-{
- const char *name = luaL_checkstring(L, 1);
-
- int res = ServiceExists(name);
- lua_pushboolean(L, res);
-
- return 1;
-}
-
-static int core_DestroyServiceFunction(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HANDLE hService = lua_touserdata(L, 1);
-
- CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L);
- if (env)
- env->ReleaseHookRef(hService);
-
- DestroyServiceFunction(hService);
-
- return 0;
-}
-
-/***********************************************/
-
-static int core_IsPluginLoaded(lua_State *L)
-{
- const char *value = lua_tostring(L, 1);
-
- MUUID uuid = { 0 };
- bool res = UuidFromStringA((RPC_CSTR)value, (UUID*)&uuid) == RPC_S_OK;
- if (res)
- res = IsPluginLoaded(uuid) > 0;
- lua_pushboolean(L, res);
-
- return 1;
-}
-
-static int core_Free(lua_State *L)
-{
- if (lua_islightuserdata(L, 1))
- {
- void *ptr = lua_touserdata(L, 1);
- mir_free(ptr);
- }
-
- return 0;
-}
-
-static int core_Translate(lua_State *L)
-{
- char *what = (char*)luaL_checkstring(L, 1);
-
- ptrW value(mir_utf8decodeW(what));
- lua_pushstring(L, T2Utf(TranslateW_LP(value, hLangpack)));
-
- return 1;
-}
-
-static int core_Parse(lua_State *L)
-{
- char *what = (char*)luaL_checkstring(L, 1);
-
- ptrW value(mir_utf8decodeW(what));
- lua_pushstring(L, T2Utf(VARSW(value)));
-
- return 1;
-}
-
-static int core_GetFullPath(lua_State *L)
-{
- wchar_t path[MAX_PATH];
- GetModuleFileName(nullptr, path, MAX_PATH);
-
- lua_pushstring(L, ptrA(mir_utf8encodeW(path)));
-
- return 1;
-}
-
-/***********************************************/
-
-struct core_ForkThreadParam
-{
- lua_State *L;
- int threadRef;
- int functionRef;
- HANDLE hThread;
-};
-
-std::map<HANDLE, core_ForkThreadParam*> lstThreads;
-
-void DestroyThread(core_ForkThreadParam *ftp)
-{
- luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef);
- luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->threadRef);
- lstThreads.erase(ftp->hThread);
-
- delete ftp;
-}
-
-void __cdecl ThreadFunc(void *p)
-{
- core_ForkThreadParam *ftp = (core_ForkThreadParam*)p;
-
- lua_rawgeti(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef);
- luaM_pcall(ftp->L, 0, 1);
- DestroyThread(ftp);
-}
-
-static int core_ForkThread(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TFUNCTION);
-
- core_ForkThreadParam *p = new core_ForkThreadParam();
-
- p->L = lua_newthread(L);
- p->threadRef = luaL_ref(L, LUA_REGISTRYINDEX);
- lua_pushvalue(L, 1);
- p->functionRef = luaL_ref(L, LUA_REGISTRYINDEX);
-
- p->hThread = mir_forkthread(ThreadFunc, p);
- lstThreads[p->hThread] = p;
- lua_pushlightuserdata(L, p->hThread);
-
- return 1;
-}
-
-static int core_TerminateThread(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HANDLE hThread = (HANDLE)lua_touserdata(L, 1);
-
- auto it = lstThreads.find(hThread);
- if (it != lstThreads.end()) {
- DestroyThread(it->second);
- lua_pushboolean(L, TerminateThread(hThread, 0));
- }
- else lua_pushboolean(L, 0);
-
- return 1;
-}
-
-/***********************************************/
-
-luaL_Reg coreApi[] =
-{
- { "CreateHookableEvent", core_CreateHookableEvent },
-
- { "NotifyEventHooks", core_NotifyEventHooks },
-
- { "HookEvent", core_HookEvent },
- { "HookTemporaryEvent", core_HookTemporaryEvent },
- { "UnhookEvent", core_UnhookEvent },
-
- { "CreateServiceFunction", core_CreateServiceFunction },
- { "DestroyServiceFunction", core_DestroyServiceFunction },
-
- { "ServiceExists", core_ServiceExists },
- { "CallService", core_CallService },
-
- { "IsPluginLoaded", core_IsPluginLoaded },
-
- { "Free", core_Free },
-
- { "Translate", core_Translate },
-
- { "Parse", core_Parse },
-
- { "GetFullPath", core_GetFullPath },
-
- { "ForkThread", core_ForkThread },
- { "TerminateThread", core_TerminateThread },
-
- { "Version", nullptr },
-
- { "NULL", nullptr },
- { "INVALID_HANDLE_VALUE", nullptr },
- { "CALLSERVICE_NOTFOUND", nullptr },
-
- { nullptr, nullptr }
-};
-
-/***********************************************/
-
-LUAMOD_API int luaopen_m_core(lua_State *L)
-{
- luaL_newlib(L, coreApi);
- lua_pushlightuserdata(L, nullptr);
- lua_setfield(L, -2, "NULL");
- lua_pushlightuserdata(L, INVALID_HANDLE_VALUE);
- lua_setfield(L, -2, "INVALID_HANDLE_VALUE");
- lua_pushinteger(L, CALLSERVICE_NOTFOUND);
- lua_setfield(L, -2, "CALLSERVICE_NOTFOUND");
-
- char version[128];
- Miranda_GetVersionText(version, _countof(version));
- lua_pushstring(L, ptrA(mir_utf8encode(version)));
- lua_setfield(L, -2, "Version");
-
- // set copy to global variable m
- lua_pushvalue(L, -1);
- lua_setglobal(L, "m");
-
- return 1;
+#include "../stdafx.h" + +static int core_CreateHookableEvent(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + + HANDLE res = CreateHookableEvent(name); + if (res != nullptr) + lua_pushlightuserdata(L, res); + else + lua_pushnil(L); + + return 1; +} + +int HookEventLuaParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +{ + lua_State *L = (lua_State*)obj; + + int ref = param; + lua_rawgeti(L, LUA_REGISTRYINDEX, ref); + + if (wParam) + lua_pushlightuserdata(L, (void*)wParam); + else + lua_pushnil(L); + + if (lParam) + lua_pushlightuserdata(L, (void*)lParam); + else + lua_pushnil(L); + + luaM_pcall(L, 2, 1); + + return lua_tointeger(L, -1); +} + +int HookEventEnvParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +{ + CMLuaEnvironment *env = (CMLuaEnvironment*)obj; + + int ref = param; + lua_rawgeti(env->L, LUA_REGISTRYINDEX, ref); + + if (wParam) + lua_pushlightuserdata(env->L, (void*)wParam); + else + lua_pushnil(env->L); + + if (lParam) + lua_pushlightuserdata(env->L, (void*)lParam); + else + lua_pushnil(env->L); + + luaM_pcall(env->L, 2, 1); + + return lua_tointeger(env->L, -1); +} + +static int core_HookEvent(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + luaL_checktype(L, 2, LUA_TFUNCTION); + + lua_pushvalue(L, 2); + int ref = luaL_ref(L, LUA_REGISTRYINDEX); + + HANDLE res = nullptr; + CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L); + if (env) { + res = HookEventObjParam(name, HookEventEnvParam, env, ref); + if (res) + env->AddHookRef(res, ref); + } + else + res = HookEventObjParam(name, HookEventLuaParam, L, ref); + + if (res == nullptr) { + luaL_unref(L, LUA_REGISTRYINDEX, ref); + lua_pushnil(L); + + return 1; + } + + lua_pushlightuserdata(L, res); + + return 1; +} + +static int core_HookTemporaryEvent(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + luaL_checktype(L, 2, LUA_TFUNCTION); + + lua_pushvalue(L, 2); + int ref = luaL_ref(L, LUA_REGISTRYINDEX); + + HANDLE res = nullptr; + CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L); + if (env) { + res = HookEventObjParam(name, HookEventEnvParam, env, ref); + if (res) + env->AddHookRef(res, ref); + } + else + res = HookEventObjParam(name, HookEventLuaParam, L, ref); + + // event does not exists, call hook immideatelly + if (res == nullptr) { + luaL_unref(L, LUA_REGISTRYINDEX, ref); + lua_pushnil(L); + lua_pushnil(L); + luaM_pcall(L, 2, 1); + return lua_tointeger(env->L, -1); + } + + lua_pushlightuserdata(L, res); + + return 1; +} + +static int core_UnhookEvent(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HANDLE hEvent = lua_touserdata(L, 1); + + int res = UnhookEvent(hEvent); + if (!res) { + CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L); + if (env) + env->ReleaseHookRef(hEvent); + } + lua_pushboolean(L, !res); + + return 1; +} + +static int core_NotifyEventHooks(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HANDLE hEvent = lua_touserdata(L, 1); + WPARAM wParam = (WPARAM)luaM_tomparam(L, 2); + LPARAM lParam = (LPARAM)luaM_tomparam(L, 3); + + int res = NotifyEventHooks(hEvent, wParam, lParam); + lua_pushboolean(L, res != -1); + + return 1; +} + +/***********************************************/ + +INT_PTR CreateServiceFunctionLuaStateParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +{ + lua_State *L = (lua_State*)obj; + + int ref = param; + lua_rawgeti(L, LUA_REGISTRYINDEX, ref); + + lua_pushlightuserdata(L, (void*)wParam); + lua_pushlightuserdata(L, (void*)lParam); + luaM_pcall(L, 2, 1); + + INT_PTR res = lua_tointeger(L, 1); + lua_pushinteger(L, res); + + return res; +} + +INT_PTR CreateServiceFunctionEnvParam(void *obj, WPARAM wParam, LPARAM lParam, LPARAM param) +{ + CMLuaEnvironment *env = (CMLuaEnvironment*)obj; + + int ref = param; + lua_rawgeti(env->L, LUA_REGISTRYINDEX, ref); + + lua_pushlightuserdata(env->L, (void*)wParam); + lua_pushlightuserdata(env->L, (void*)lParam); + luaM_pcall(env->L, 2, 1); + + INT_PTR res = lua_tointeger(env->L, 1); + lua_pushinteger(env->L, res); + + return res; +} + +static int core_CreateServiceFunction(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + luaL_checktype(L, 2, LUA_TFUNCTION); + + lua_pushvalue(L, 2); + int ref = luaL_ref(L, LUA_REGISTRYINDEX); + + HANDLE res = nullptr; + CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L); + if (env) { + res = CreateServiceFunctionObjParam(name, CreateServiceFunctionEnvParam, env, ref); + if (res) + env->AddServiceRef(res, ref); + } + else + res = CreateServiceFunctionObjParam(name, CreateServiceFunctionLuaStateParam, L, ref); + + if (!res) { + luaL_unref(L, LUA_REGISTRYINDEX, ref); + lua_pushnil(L); + return 1; + } + + lua_pushlightuserdata(L, res); + + return 1; +} + +static int core_CallService(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + WPARAM wParam = (WPARAM)luaM_tomparam(L, 2); + LPARAM lParam = (LPARAM)luaM_tomparam(L, 3); + + INT_PTR res = CallService(name, wParam, lParam); + lua_pushinteger(L, res); + + return 1; +} + +static int core_ServiceExists(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + + int res = ServiceExists(name); + lua_pushboolean(L, res); + + return 1; +} + +static int core_DestroyServiceFunction(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HANDLE hService = lua_touserdata(L, 1); + + CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L); + if (env) + env->ReleaseHookRef(hService); + + DestroyServiceFunction(hService); + + return 0; +} + +/***********************************************/ + +static int core_IsPluginLoaded(lua_State *L) +{ + const char *value = lua_tostring(L, 1); + + MUUID uuid = { 0 }; + bool res = UuidFromStringA((RPC_CSTR)value, (UUID*)&uuid) == RPC_S_OK; + if (res) + res = IsPluginLoaded(uuid) > 0; + lua_pushboolean(L, res); + + return 1; +} + +static int core_Free(lua_State *L) +{ + if (lua_islightuserdata(L, 1)) + { + void *ptr = lua_touserdata(L, 1); + mir_free(ptr); + } + + return 0; +} + +static int core_Translate(lua_State *L) +{ + char *what = (char*)luaL_checkstring(L, 1); + + ptrW value(mir_utf8decodeW(what)); + lua_pushstring(L, T2Utf(TranslateW_LP(value, hLangpack))); + + return 1; +} + +static int core_Parse(lua_State *L) +{ + char *what = (char*)luaL_checkstring(L, 1); + + ptrW value(mir_utf8decodeW(what)); + lua_pushstring(L, T2Utf(VARSW(value))); + + return 1; +} + +static int core_GetFullPath(lua_State *L) +{ + wchar_t path[MAX_PATH]; + GetModuleFileName(nullptr, path, MAX_PATH); + + lua_pushstring(L, ptrA(mir_utf8encodeW(path))); + + return 1; +} + +/***********************************************/ + +struct core_ForkThreadParam +{ + lua_State *L; + int threadRef; + int functionRef; + HANDLE hThread; +}; + +std::map<HANDLE, core_ForkThreadParam*> lstThreads; + +void DestroyThread(core_ForkThreadParam *ftp) +{ + luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef); + luaL_unref(ftp->L, LUA_REGISTRYINDEX, ftp->threadRef); + lstThreads.erase(ftp->hThread); + + delete ftp; +} + +void __cdecl ThreadFunc(void *p) +{ + core_ForkThreadParam *ftp = (core_ForkThreadParam*)p; + + lua_rawgeti(ftp->L, LUA_REGISTRYINDEX, ftp->functionRef); + luaM_pcall(ftp->L, 0, 1); + DestroyThread(ftp); +} + +static int core_ForkThread(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TFUNCTION); + + core_ForkThreadParam *p = new core_ForkThreadParam(); + + p->L = lua_newthread(L); + p->threadRef = luaL_ref(L, LUA_REGISTRYINDEX); + lua_pushvalue(L, 1); + p->functionRef = luaL_ref(L, LUA_REGISTRYINDEX); + + p->hThread = mir_forkthread(ThreadFunc, p); + lstThreads[p->hThread] = p; + lua_pushlightuserdata(L, p->hThread); + + return 1; +} + +static int core_TerminateThread(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HANDLE hThread = (HANDLE)lua_touserdata(L, 1); + + auto it = lstThreads.find(hThread); + if (it != lstThreads.end()) { + DestroyThread(it->second); + lua_pushboolean(L, TerminateThread(hThread, 0)); + } + else lua_pushboolean(L, 0); + + return 1; +} + +/***********************************************/ + +luaL_Reg coreApi[] = +{ + { "CreateHookableEvent", core_CreateHookableEvent }, + + { "NotifyEventHooks", core_NotifyEventHooks }, + + { "HookEvent", core_HookEvent }, + { "HookTemporaryEvent", core_HookTemporaryEvent }, + { "UnhookEvent", core_UnhookEvent }, + + { "CreateServiceFunction", core_CreateServiceFunction }, + { "DestroyServiceFunction", core_DestroyServiceFunction }, + + { "ServiceExists", core_ServiceExists }, + { "CallService", core_CallService }, + + { "IsPluginLoaded", core_IsPluginLoaded }, + + { "Free", core_Free }, + + { "Translate", core_Translate }, + + { "Parse", core_Parse }, + + { "GetFullPath", core_GetFullPath }, + + { "ForkThread", core_ForkThread }, + { "TerminateThread", core_TerminateThread }, + + { "Version", nullptr }, + + { "NULL", nullptr }, + { "INVALID_HANDLE_VALUE", nullptr }, + { "CALLSERVICE_NOTFOUND", nullptr }, + + { nullptr, nullptr } +}; + +/***********************************************/ + +LUAMOD_API int luaopen_m_core(lua_State *L) +{ + luaL_newlib(L, coreApi); + lua_pushlightuserdata(L, nullptr); + lua_setfield(L, -2, "NULL"); + lua_pushlightuserdata(L, INVALID_HANDLE_VALUE); + lua_setfield(L, -2, "INVALID_HANDLE_VALUE"); + lua_pushinteger(L, CALLSERVICE_NOTFOUND); + lua_setfield(L, -2, "CALLSERVICE_NOTFOUND"); + + char version[128]; + Miranda_GetVersionText(version, _countof(version)); + lua_pushstring(L, ptrA(mir_utf8encode(version))); + lua_setfield(L, -2, "Version"); + + // set copy to global variable m + lua_pushvalue(L, -1); + lua_setglobal(L, "m"); + + return 1; }
\ No newline at end of file diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/Modules/m_database.cpp index 7425b45ddd..3d0d53ba48 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/Modules/m_database.cpp @@ -1,691 +1,691 @@ -#include "stdafx.h"
-
-void luaM_pushdbvt(lua_State *L, const DBVARIANT &value)
-{
- switch (value.type) {
- case DBVT_BYTE:
- lua_pushinteger(L, value.bVal);
- break;
- case DBVT_WORD:
- lua_pushinteger(L,value.wVal);
- break;
- case DBVT_DWORD:
- lua_pushnumber(L, value.dVal);
- break;
- case DBVT_ASCIIZ:
- lua_pushstring(L, ptrA(mir_utf8encode(value.pszVal)));
- break;
- case DBVT_UTF8:
- lua_pushstring(L, value.pszVal);
- break;
- case DBVT_WCHAR:
- lua_pushstring(L, ptrA(mir_utf8encodeW(value.pwszVal)));
- break;
- case DBVT_BLOB:
- {
- lua_createtable(L, value.cpbVal, 0);
- for (int i = 0; i < value.cpbVal; i++) {
- lua_pushinteger(L, value.pbVal[i]);
- lua_rawseti(L, -2, i + 1);
- }
- }
- break;
- default:
- lua_pushnil(L);
- }
-}
-
-/***********************************************/
-
-static int db_ContactIterator(lua_State *L)
-{
- MCONTACT hContact = lua_tointeger(L, lua_upvalueindex(1));
- const char *szModule = lua_tostring(L, lua_upvalueindex(2));
-
- hContact = hContact == NULL
- ? db_find_first(szModule)
- : db_find_next(hContact, szModule);
-
- if (hContact) {
- lua_pushinteger(L, hContact);
- lua_pushvalue(L, -1);
- lua_replace(L, lua_upvalueindex(1));
- }
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static int db_Contacts(lua_State *L)
-{
- const char *szModule = nullptr;
-
- switch (lua_type(L, 1)) {
- case LUA_TNONE:
- break;
- case LUA_TSTRING:
- szModule = lua_tostring(L, 1);
- break;
- case LUA_TUSERDATA:
- {
- PROTOACCOUNT **pa = (PROTOACCOUNT**)luaL_checkudata(L, 1, MT_PROTOACCOUNT);
- szModule = (*pa)->szModuleName;
- break;
- }
- default:
- luaL_argerror(L, 1, luaL_typename(L, 1));
- }
-
- lua_pushinteger(L, 0);
- lua_pushstring(L, szModule);
- lua_pushcclosure(L, db_ContactIterator, 2);
-
- return 1;
-}
-
-static const char *mods[] =
-{
- "FirstName",
- "LastName",
- "Nick",
- "CustomNick",
- "Email",
- "City",
- "State",
- "Country",
- "Phone",
- "Homepage",
- "About",
- "Gender",
- "Age",
- "FullName",
- "Uid",
- "DisplayName",
- nullptr
-};
-
-static int db_GetContactInfo(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
-
- int type = 0;
- switch (lua_type(L, 2)) {
- case LUA_TNUMBER:
- type = luaL_checkinteger(L, 2);
- break;
- case LUA_TSTRING:
- type = luaL_checkoption(L, 2, nullptr, mods) + 1;
- break;
- default:
- luaL_argerror(L, 2, luaL_typename(L, 2));
- }
-
- ptrW value(Contact_GetInfo(type, hContact));
- if (value)
- lua_pushstring(L, ptrA(mir_utf8encodeW(value)));
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-/***********************************************/
-
-static int db_GetEventCount(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
-
- int res = db_event_count(hContact);
- lua_pushinteger(L, res);
-
- return 1;
-}
-
-static int db_GetFirstEvent(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
-
- MEVENT res = db_event_first(hContact);
- lua_pushinteger(L, res);
-
- return 1;
-}
-
-static int db_GetPrevEvent(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
- MEVENT hDbEvent = luaL_checkinteger(L, 2);
-
- MEVENT res = db_event_prev(hContact, hDbEvent);
- lua_pushinteger(L, res);
-
- return 1;
-}
-
-static int db_GetNextEvent(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
- MEVENT hDbEvent = luaL_checkinteger(L, 2);
-
- MEVENT res = db_event_next(hContact, hDbEvent);
- lua_pushinteger(L, res);
-
- return 1;
-}
-
-static int db_GetLastEvent(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
-
- MEVENT res = db_event_last(hContact);
- lua_pushinteger(L, res);
-
- return 1;
-}
-
-static int db_GetFirstUnreadEvent(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
-
- MEVENT res = db_event_firstUnread(hContact);
- lua_pushinteger(L, res);
-
- return 1;
-}
-
-static int db_EventIterator(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1));
- MEVENT hDbEvent = luaL_checkinteger(L, lua_upvalueindex(2));
-
- hDbEvent = hDbEvent == NULL
- ? db_event_first(hContact)
- : db_event_next(hContact, hDbEvent);
-
- if (hDbEvent) {
- lua_pushinteger(L, hDbEvent);
- lua_pushvalue(L, -1);
- lua_replace(L, lua_upvalueindex(2));
- }
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static int db_Events(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
-
- lua_pushinteger(L, hContact);
- lua_pushinteger(L, NULL);
- lua_pushcclosure(L, db_EventIterator, 2);
-
- return 1;
-}
-
-static int db_EventReverseIterator(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1));
- MEVENT hDbEvent = luaL_checkinteger(L, lua_upvalueindex(2));
-
- hDbEvent = hDbEvent == NULL
- ? db_event_last(hContact)
- : db_event_prev(hContact, hDbEvent);
-
- if (hDbEvent) {
- lua_pushinteger(L, hDbEvent);
- lua_pushvalue(L, -1);
- lua_replace(L, lua_upvalueindex(2));
- }
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static int db_EventsFromEnd(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
-
- lua_pushinteger(L, hContact);
- lua_pushinteger(L, NULL);
- lua_pushcclosure(L, db_EventReverseIterator, 2);
-
- return 1;
-}
-
-static int db_UnreadEventIterator(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1));
- MEVENT hDbEvent = luaL_checkinteger(L, lua_upvalueindex(2));
-
- hDbEvent = db_event_firstUnread(hContact);
-
- if (hDbEvent) {
- lua_pushinteger(L, hDbEvent);
- lua_pushvalue(L, -1);
- lua_replace(L, lua_upvalueindex(2));
- }
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static int db_UnreadEvents(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
-
- lua_pushinteger(L, hContact);
- lua_pushinteger(L, NULL);
- lua_pushcclosure(L, db_UnreadEventIterator, 2);
-
- return 1;
-}
-
-void MakeDbEvent(lua_State *L, DBEVENTINFO &dbei)
-{
- lua_getfield(L, -1, "Module");
- dbei.szModule = mir_strdup(lua_tostring(L, -1));
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Type");
- dbei.eventType = lua_tointeger(L, -1);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Timestamp");
- dbei.timestamp = lua_tonumber(L, -1);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Flags");
- dbei.flags = lua_tointeger(L, -1);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Blob");
- switch (lua_type(L, -1)) {
- case LUA_TTABLE:
- dbei.cbBlob = (DWORD)lua_rawlen(L, 4);
- dbei.pBlob = (BYTE*)mir_calloc(dbei.cbBlob);
- for (DWORD i = 0; i < dbei.cbBlob; i++) {
- lua_geti(L, 4, i + 1);
- dbei.pBlob[i] = lua_tointeger(L, -1);
- lua_pop(L, 1);
- }
- break;
- case LUA_TSTRING:
- size_t nLen;
- const char *str = lua_tolstring(L, -1, &nLen);
- dbei.cbBlob = (DWORD)nLen;
- dbei.pBlob = (BYTE*)mir_alloc(nLen);
- memcpy(dbei.pBlob, str, nLen);
- break;
- }
- lua_pop(L, 1);
-}
-
-static int db_AddEvent(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
-
- DBEVENTINFO dbei;
- MakeDbEvent(L, dbei);
- MEVENT hDbEvent = db_event_add(hContact, &dbei);
-
- if (hDbEvent)
- lua_pushnumber(L, hDbEvent);
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-static int db_MarkReadEvent(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
- MEVENT hDbEvent = luaL_checkinteger(L, 2);
-
- int res = db_event_markRead(hContact, hDbEvent);
- lua_pushnumber(L, res);
-
- return 1;
-}
-
-/***********************************************/
-
-static int ModulesEnumProc(const char *szModuleName, void *lParam)
-{
- if (szModuleName) {
- LIST<char>* p = (LIST<char>*)lParam;
- p->insert(mir_strdup(szModuleName));
- }
-
- return 0;
-}
-
-static int db_ModulesIterator(lua_State *L)
-{
- int i = lua_tointeger(L, lua_upvalueindex(1));
- LIST<char> ¶m = *(LIST<char>*)lua_touserdata(L, lua_upvalueindex(2));
-
- if (i < param.getCount()) {
- lua_pushinteger(L, (i + 1));
- lua_replace(L, lua_upvalueindex(1));
- lua_pushstring(L, ptrA(mir_utf8encode(param[i])));
- mir_free(param[i]);
- }
- else {
- lua_pushnil(L);
- delete ¶m;
- }
-
- return 1;
-}
-
-static int db_Modules(lua_State *L)
-{
- LIST<char> *param = new LIST<char>(5, PtrKeySortT);
-
- db_enum_modules(ModulesEnumProc, param);
-
- lua_pushinteger(L, 0);
- lua_pushlightuserdata(L, param);
- lua_pushcclosure(L, db_ModulesIterator, 2);
-
- return 1;
-}
-
-static int db_DeleteModule(lua_State *L)
-{
- MCONTACT hContact = lua_tointeger(L, 1);
- const char *szModule = luaL_checkstring(L, 2);
-
- INT_PTR res = db_delete_module(hContact, szModule);
- lua_pushboolean(L, !res);
-
- return 1;
-}
-
-static int SettingsEnumProc(const char* szSetting, void *lParam)
-{
- if (szSetting ) {
- LIST<char>* p = (LIST<char>*)lParam;
- p->insert(mir_strdup(szSetting));
- }
- return 0;
-}
-
-static int db_SettingIterator(lua_State *L)
-{
- int i = lua_tointeger(L, lua_upvalueindex(1));
- LIST<char> ¶m = *(LIST<char>*)lua_touserdata(L, lua_upvalueindex(2));
-
- if (i < param.getCount()) {
- lua_pushinteger(L, (i + 1));
- lua_replace(L, lua_upvalueindex(1));
- lua_pushstring(L, ptrA(mir_utf8encode(param[i])));
- mir_free(param[i]);
- }
- else {
- lua_pushnil(L);
- delete ¶m;
- }
-
- return 1;
-}
-
-static int db_Settings(lua_State *L)
-{
- MCONTACT hContact = lua_tointeger(L, 1);
- const char* szModule = luaL_checkstring(L, 2);
-
- LIST<char> *param = new LIST<char>(5, PtrKeySortT);
- db_enum_settings(hContact, SettingsEnumProc, szModule, param);
-
- lua_pushinteger(L, 0);
- lua_pushlightuserdata(L, param);
- lua_pushcclosure(L, db_SettingIterator, 2);
-
- return 1;
-}
-
-static int db_GetSetting(lua_State *L)
-{
- MCONTACT hContact = lua_tointeger(L, 1);
- const char *szModule = luaL_checkstring(L, 2);
- const char *szSetting = luaL_checkstring(L, 3);
-
- DBVARIANT dbv;
- if (db_get(hContact, szModule, szSetting, &dbv)) {
- lua_pushvalue(L, 4);
- return 1;
- }
-
- luaM_pushdbvt(L, dbv);
- db_free(&dbv);
-
- if (lua_isnil(L, -1) && !lua_isnoneornil(L, 4)) {
- lua_pop(L, 1);
- lua_pushvalue(L, 4);
- }
-
- return 1;
-}
-
-static int db_WriteSetting(lua_State *L)
-{
- MCONTACT hContact = lua_tointeger(L, 1);
- const char *szModule = luaL_checkstring(L, 2);
- const char *szSetting = luaL_checkstring(L, 3);
- luaL_checkany(L, 4);
-
- DBVARIANT dbv;
- if (lua_isnoneornil(L, 5)) {
- int type = lua_type(L, 4);
- switch (type) {
- case LUA_TBOOLEAN:
- dbv.type = DBVT_BYTE;
- break;
- case LUA_TNUMBER:
- dbv.type = DBVT_DWORD;
- break;
- case LUA_TSTRING:
- dbv.type = DBVT_UTF8;
- break;
- case LUA_TTABLE:
- dbv.type = DBVT_BLOB;
- break;
- default:
- lua_pushboolean(L, false);
- return 1;
- }
- }
- else
- dbv.type = luaL_checkinteger(L, 5);
-
- switch (dbv.type) {
- case DBVT_BYTE:
- dbv.bVal = lua_isboolean(L, 4)
- ? lua_toboolean(L, 4)
- : luaL_checknumber(L, 4);
- break;
- case DBVT_WORD:
- dbv.wVal = luaL_checknumber(L, 4);
- break;
- case DBVT_DWORD:
- dbv.dVal = luaL_checknumber(L, 4);
- break;
- case DBVT_UTF8:
- dbv.pszVal = mir_strdup(luaL_checkstring(L, 4));
- break;
- case DBVT_ASCIIZ:
- dbv.pszVal = mir_utf8decodeA(luaL_checkstring(L, 4));
- break;
- case DBVT_WCHAR:
- dbv.pwszVal = mir_utf8decodeW(luaL_checkstring(L, 4));
- break;
- case DBVT_BLOB:
- {
- dbv.cpbVal = (WORD)lua_rawlen(L, 4);
- dbv.pbVal = (BYTE*)mir_calloc(dbv.cpbVal);
- for (int i = 0; i < dbv.cpbVal; i++) {
- lua_geti(L, 4, i + 1);
- dbv.pbVal[i] = lua_tointeger(L, -1);
- lua_pop(L, 1);
- }
- }
- break;
- default:
- lua_pushboolean(L, false);
- return 1;
- }
-
- INT_PTR res = db_set(hContact, szModule, szSetting, &dbv);
- lua_pushboolean(L, !res);
-
- return 1;
-}
-
-static int db_DeleteSetting(lua_State *L)
-{
- MCONTACT hContact = lua_tointeger(L, 1);
- LPCSTR szModule = luaL_checkstring(L, 2);
- LPCSTR szSetting = luaL_checkstring(L, 3);
-
- INT_PTR res = db_unset(hContact, szModule, szSetting);
- lua_pushboolean(L, !res);
-
- return 1;
-}
-
-/***********************************************/
-
-static luaL_Reg databaseApi[] =
-{
- { "Contacts", db_Contacts },
- { "GetContactInfo", db_GetContactInfo },
-
- { "GetEventCount", db_GetEventCount },
-
- { "GetFirstEvent", db_GetFirstEvent },
- { "GetPrevEvent", db_GetPrevEvent },
- { "GetNextEvent", db_GetNextEvent },
- { "GetLastEvent", db_GetLastEvent },
- { "GetFirstUnreadEvent", db_GetFirstUnreadEvent },
- { "Events", db_Events },
- { "EventsFromEnd", db_EventsFromEnd },
- { "UnreadEvents", db_UnreadEvents },
- { "AddEvent", db_AddEvent },
- { "MarkReadEvent", db_MarkReadEvent },
-
- { "Settings", db_Settings },
- { "Modules", db_Modules },
-
- { "DeleteModule", db_DeleteModule },
-
- { "GetSetting", db_GetSetting },
- { "WriteSetting", db_WriteSetting },
- { "SetSetting", db_WriteSetting },
- { "DeleteSetting", db_DeleteSetting },
-
- { "DBVT_BYTE", nullptr },
- { "DBVT_WORD", nullptr },
- { "DBVT_DWORD", nullptr },
- { "DBVT_ASCIIZ", nullptr },
- { "DBVT_UTF8", nullptr },
- { "DBVT_WCHAR", nullptr },
-
- { nullptr, nullptr }
-};
-
-/***********************************************/
-
-#define MT_DBCONTACTWRITESETTING "DBCONTACTWRITESETTING"
-
-template <>
-int MT<DBCONTACTWRITESETTING>::Get(lua_State *L, DBCONTACTWRITESETTING *dbcw)
-{
- const char *key = luaL_checkstring(L, 2);
-
- if (mir_strcmpi(key, "Value") == 0)
- luaM_pushdbvt(L, dbcw->value);
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-/***********************************************/
-
-#define MT_DBEVENTINFO "DBEVENTINFO"
-
-template <>
-DBEVENTINFO* MT<DBEVENTINFO>::Init(lua_State *L)
-{
- MEVENT hDbEvent = luaL_checkinteger(L, 1);
-
- DBEVENTINFO* dbei = (DBEVENTINFO*)mir_calloc(sizeof(DBEVENTINFO));
- dbei->cbBlob = db_event_getBlobSize((MEVENT)hDbEvent);
- dbei->pBlob = (PBYTE)mir_calloc(dbei->cbBlob);
- db_event_get((MEVENT)hDbEvent, dbei);
-
- return dbei;
-}
-
-template <>
-int MT<DBEVENTINFO>::Get(lua_State *L, DBEVENTINFO *dbei)
-{
- const char *key = luaL_checkstring(L, 2);
-
- if (mir_strcmpi(key, "Blob") == 0) {
- lua_createtable(L, dbei->cbBlob, 0);
- for (DWORD i = 0; i < dbei->cbBlob; i++) {
- lua_pushinteger(L, dbei->pBlob[i]);
- lua_rawseti(L, -2, i + 1);
- }
- }
- else
- lua_pushnil(L);
-
- return 1;
-}
-
-template <>
-void MT<DBEVENTINFO>::Free(lua_State*, DBEVENTINFO **dbei)
-{
- mir_free((*dbei)->pBlob);
- mir_free(*dbei);
-}
-
-/***********************************************/
-
-LUAMOD_API int luaopen_m_database(lua_State *L)
-{
- luaL_newlib(L, databaseApi);
-
- lua_pushnumber(L, DBVT_BYTE);
- lua_setfield(L, -2, "DBVT_BYTE");
- lua_pushnumber(L, DBVT_WORD);
- lua_setfield(L, -2, "DBVT_WORD");
- lua_pushnumber(L, DBVT_DWORD);
- lua_setfield(L, -2, "DBVT_DWORD");
- lua_pushnumber(L, DBVT_ASCIIZ);
- lua_setfield(L, -2, "DBVT_ASCIIZ");
- lua_pushnumber(L, DBVT_UTF8);
- lua_setfield(L, -2, "DBVT_UTF8");
- lua_pushnumber(L, DBVT_WCHAR);
- lua_setfield(L, -2, "DBVT_WCHAR");
-
- MT<DBCONTACTWRITESETTING>(L, MT_DBCONTACTWRITESETTING)
- .Field(&DBCONTACTWRITESETTING::szModule, "Module", LUA_TSTRINGA)
- .Field(&DBCONTACTWRITESETTING::szSetting, "Setting", LUA_TSTRINGA);
-
- MT<DBEVENTINFO>(L, MT_DBEVENTINFO)
- .Field(&DBEVENTINFO::szModule, "Module", LUA_TSTRINGA)
- .Field(&DBEVENTINFO::timestamp, "Timestamp", LUA_TINTEGER)
- .Field(&DBEVENTINFO::eventType, "Type", LUA_TINTEGER)
- .Field(&DBEVENTINFO::flags, "Flags", LUA_TINTEGER);
-
- return 1;
-}
+#include "../stdafx.h" + +void luaM_pushdbvt(lua_State *L, const DBVARIANT &value) +{ + switch (value.type) { + case DBVT_BYTE: + lua_pushinteger(L, value.bVal); + break; + case DBVT_WORD: + lua_pushinteger(L,value.wVal); + break; + case DBVT_DWORD: + lua_pushnumber(L, value.dVal); + break; + case DBVT_ASCIIZ: + lua_pushstring(L, ptrA(mir_utf8encode(value.pszVal))); + break; + case DBVT_UTF8: + lua_pushstring(L, value.pszVal); + break; + case DBVT_WCHAR: + lua_pushstring(L, ptrA(mir_utf8encodeW(value.pwszVal))); + break; + case DBVT_BLOB: + { + lua_createtable(L, value.cpbVal, 0); + for (int i = 0; i < value.cpbVal; i++) { + lua_pushinteger(L, value.pbVal[i]); + lua_rawseti(L, -2, i + 1); + } + } + break; + default: + lua_pushnil(L); + } +} + +/***********************************************/ + +static int db_ContactIterator(lua_State *L) +{ + MCONTACT hContact = lua_tointeger(L, lua_upvalueindex(1)); + const char *szModule = lua_tostring(L, lua_upvalueindex(2)); + + hContact = hContact == NULL + ? db_find_first(szModule) + : db_find_next(hContact, szModule); + + if (hContact) { + lua_pushinteger(L, hContact); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(1)); + } + else + lua_pushnil(L); + + return 1; +} + +static int db_Contacts(lua_State *L) +{ + const char *szModule = nullptr; + + switch (lua_type(L, 1)) { + case LUA_TNONE: + break; + case LUA_TSTRING: + szModule = lua_tostring(L, 1); + break; + case LUA_TUSERDATA: + { + PROTOACCOUNT **pa = (PROTOACCOUNT**)luaL_checkudata(L, 1, MT_PROTOACCOUNT); + szModule = (*pa)->szModuleName; + break; + } + default: + luaL_argerror(L, 1, luaL_typename(L, 1)); + } + + lua_pushinteger(L, 0); + lua_pushstring(L, szModule); + lua_pushcclosure(L, db_ContactIterator, 2); + + return 1; +} + +static const char *mods[] = +{ + "FirstName", + "LastName", + "Nick", + "CustomNick", + "Email", + "City", + "State", + "Country", + "Phone", + "Homepage", + "About", + "Gender", + "Age", + "FullName", + "Uid", + "DisplayName", + nullptr +}; + +static int db_GetContactInfo(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + int type = 0; + switch (lua_type(L, 2)) { + case LUA_TNUMBER: + type = luaL_checkinteger(L, 2); + break; + case LUA_TSTRING: + type = luaL_checkoption(L, 2, nullptr, mods) + 1; + break; + default: + luaL_argerror(L, 2, luaL_typename(L, 2)); + } + + ptrW value(Contact_GetInfo(type, hContact)); + if (value) + lua_pushstring(L, ptrA(mir_utf8encodeW(value))); + else + lua_pushnil(L); + + return 1; +} + +/***********************************************/ + +static int db_GetEventCount(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + int res = db_event_count(hContact); + lua_pushinteger(L, res); + + return 1; +} + +static int db_GetFirstEvent(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + MEVENT res = db_event_first(hContact); + lua_pushinteger(L, res); + + return 1; +} + +static int db_GetPrevEvent(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + MEVENT hDbEvent = luaL_checkinteger(L, 2); + + MEVENT res = db_event_prev(hContact, hDbEvent); + lua_pushinteger(L, res); + + return 1; +} + +static int db_GetNextEvent(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + MEVENT hDbEvent = luaL_checkinteger(L, 2); + + MEVENT res = db_event_next(hContact, hDbEvent); + lua_pushinteger(L, res); + + return 1; +} + +static int db_GetLastEvent(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + MEVENT res = db_event_last(hContact); + lua_pushinteger(L, res); + + return 1; +} + +static int db_GetFirstUnreadEvent(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + MEVENT res = db_event_firstUnread(hContact); + lua_pushinteger(L, res); + + return 1; +} + +static int db_EventIterator(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1)); + MEVENT hDbEvent = luaL_checkinteger(L, lua_upvalueindex(2)); + + hDbEvent = hDbEvent == NULL + ? db_event_first(hContact) + : db_event_next(hContact, hDbEvent); + + if (hDbEvent) { + lua_pushinteger(L, hDbEvent); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(2)); + } + else + lua_pushnil(L); + + return 1; +} + +static int db_Events(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + lua_pushinteger(L, hContact); + lua_pushinteger(L, NULL); + lua_pushcclosure(L, db_EventIterator, 2); + + return 1; +} + +static int db_EventReverseIterator(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1)); + MEVENT hDbEvent = luaL_checkinteger(L, lua_upvalueindex(2)); + + hDbEvent = hDbEvent == NULL + ? db_event_last(hContact) + : db_event_prev(hContact, hDbEvent); + + if (hDbEvent) { + lua_pushinteger(L, hDbEvent); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(2)); + } + else + lua_pushnil(L); + + return 1; +} + +static int db_EventsFromEnd(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + lua_pushinteger(L, hContact); + lua_pushinteger(L, NULL); + lua_pushcclosure(L, db_EventReverseIterator, 2); + + return 1; +} + +static int db_UnreadEventIterator(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, lua_upvalueindex(1)); + MEVENT hDbEvent = luaL_checkinteger(L, lua_upvalueindex(2)); + + hDbEvent = db_event_firstUnread(hContact); + + if (hDbEvent) { + lua_pushinteger(L, hDbEvent); + lua_pushvalue(L, -1); + lua_replace(L, lua_upvalueindex(2)); + } + else + lua_pushnil(L); + + return 1; +} + +static int db_UnreadEvents(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + lua_pushinteger(L, hContact); + lua_pushinteger(L, NULL); + lua_pushcclosure(L, db_UnreadEventIterator, 2); + + return 1; +} + +void MakeDbEvent(lua_State *L, DBEVENTINFO &dbei) +{ + lua_getfield(L, -1, "Module"); + dbei.szModule = mir_strdup(lua_tostring(L, -1)); + lua_pop(L, 1); + + lua_getfield(L, -1, "Type"); + dbei.eventType = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "Timestamp"); + dbei.timestamp = lua_tonumber(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "Flags"); + dbei.flags = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "Blob"); + switch (lua_type(L, -1)) { + case LUA_TTABLE: + dbei.cbBlob = (DWORD)lua_rawlen(L, 4); + dbei.pBlob = (BYTE*)mir_calloc(dbei.cbBlob); + for (DWORD i = 0; i < dbei.cbBlob; i++) { + lua_geti(L, 4, i + 1); + dbei.pBlob[i] = lua_tointeger(L, -1); + lua_pop(L, 1); + } + break; + case LUA_TSTRING: + size_t nLen; + const char *str = lua_tolstring(L, -1, &nLen); + dbei.cbBlob = (DWORD)nLen; + dbei.pBlob = (BYTE*)mir_alloc(nLen); + memcpy(dbei.pBlob, str, nLen); + break; + } + lua_pop(L, 1); +} + +static int db_AddEvent(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + + DBEVENTINFO dbei; + MakeDbEvent(L, dbei); + MEVENT hDbEvent = db_event_add(hContact, &dbei); + + if (hDbEvent) + lua_pushnumber(L, hDbEvent); + else + lua_pushnil(L); + + return 1; +} + +static int db_MarkReadEvent(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + MEVENT hDbEvent = luaL_checkinteger(L, 2); + + int res = db_event_markRead(hContact, hDbEvent); + lua_pushnumber(L, res); + + return 1; +} + +/***********************************************/ + +static int ModulesEnumProc(const char *szModuleName, void *lParam) +{ + if (szModuleName) { + LIST<char>* p = (LIST<char>*)lParam; + p->insert(mir_strdup(szModuleName)); + } + + return 0; +} + +static int db_ModulesIterator(lua_State *L) +{ + int i = lua_tointeger(L, lua_upvalueindex(1)); + LIST<char> ¶m = *(LIST<char>*)lua_touserdata(L, lua_upvalueindex(2)); + + if (i < param.getCount()) { + lua_pushinteger(L, (i + 1)); + lua_replace(L, lua_upvalueindex(1)); + lua_pushstring(L, ptrA(mir_utf8encode(param[i]))); + mir_free(param[i]); + } + else { + lua_pushnil(L); + delete ¶m; + } + + return 1; +} + +static int db_Modules(lua_State *L) +{ + LIST<char> *param = new LIST<char>(5, PtrKeySortT); + + db_enum_modules(ModulesEnumProc, param); + + lua_pushinteger(L, 0); + lua_pushlightuserdata(L, param); + lua_pushcclosure(L, db_ModulesIterator, 2); + + return 1; +} + +static int db_DeleteModule(lua_State *L) +{ + MCONTACT hContact = lua_tointeger(L, 1); + const char *szModule = luaL_checkstring(L, 2); + + INT_PTR res = db_delete_module(hContact, szModule); + lua_pushboolean(L, !res); + + return 1; +} + +static int SettingsEnumProc(const char* szSetting, void *lParam) +{ + if (szSetting ) { + LIST<char>* p = (LIST<char>*)lParam; + p->insert(mir_strdup(szSetting)); + } + return 0; +} + +static int db_SettingIterator(lua_State *L) +{ + int i = lua_tointeger(L, lua_upvalueindex(1)); + LIST<char> ¶m = *(LIST<char>*)lua_touserdata(L, lua_upvalueindex(2)); + + if (i < param.getCount()) { + lua_pushinteger(L, (i + 1)); + lua_replace(L, lua_upvalueindex(1)); + lua_pushstring(L, ptrA(mir_utf8encode(param[i]))); + mir_free(param[i]); + } + else { + lua_pushnil(L); + delete ¶m; + } + + return 1; +} + +static int db_Settings(lua_State *L) +{ + MCONTACT hContact = lua_tointeger(L, 1); + const char* szModule = luaL_checkstring(L, 2); + + LIST<char> *param = new LIST<char>(5, PtrKeySortT); + db_enum_settings(hContact, SettingsEnumProc, szModule, param); + + lua_pushinteger(L, 0); + lua_pushlightuserdata(L, param); + lua_pushcclosure(L, db_SettingIterator, 2); + + return 1; +} + +static int db_GetSetting(lua_State *L) +{ + MCONTACT hContact = lua_tointeger(L, 1); + const char *szModule = luaL_checkstring(L, 2); + const char *szSetting = luaL_checkstring(L, 3); + + DBVARIANT dbv; + if (db_get(hContact, szModule, szSetting, &dbv)) { + lua_pushvalue(L, 4); + return 1; + } + + luaM_pushdbvt(L, dbv); + db_free(&dbv); + + if (lua_isnil(L, -1) && !lua_isnoneornil(L, 4)) { + lua_pop(L, 1); + lua_pushvalue(L, 4); + } + + return 1; +} + +static int db_WriteSetting(lua_State *L) +{ + MCONTACT hContact = lua_tointeger(L, 1); + const char *szModule = luaL_checkstring(L, 2); + const char *szSetting = luaL_checkstring(L, 3); + luaL_checkany(L, 4); + + DBVARIANT dbv; + if (lua_isnoneornil(L, 5)) { + int type = lua_type(L, 4); + switch (type) { + case LUA_TBOOLEAN: + dbv.type = DBVT_BYTE; + break; + case LUA_TNUMBER: + dbv.type = DBVT_DWORD; + break; + case LUA_TSTRING: + dbv.type = DBVT_UTF8; + break; + case LUA_TTABLE: + dbv.type = DBVT_BLOB; + break; + default: + lua_pushboolean(L, false); + return 1; + } + } + else + dbv.type = luaL_checkinteger(L, 5); + + switch (dbv.type) { + case DBVT_BYTE: + dbv.bVal = lua_isboolean(L, 4) + ? lua_toboolean(L, 4) + : luaL_checknumber(L, 4); + break; + case DBVT_WORD: + dbv.wVal = luaL_checknumber(L, 4); + break; + case DBVT_DWORD: + dbv.dVal = luaL_checknumber(L, 4); + break; + case DBVT_UTF8: + dbv.pszVal = mir_strdup(luaL_checkstring(L, 4)); + break; + case DBVT_ASCIIZ: + dbv.pszVal = mir_utf8decodeA(luaL_checkstring(L, 4)); + break; + case DBVT_WCHAR: + dbv.pwszVal = mir_utf8decodeW(luaL_checkstring(L, 4)); + break; + case DBVT_BLOB: + { + dbv.cpbVal = (WORD)lua_rawlen(L, 4); + dbv.pbVal = (BYTE*)mir_calloc(dbv.cpbVal); + for (int i = 0; i < dbv.cpbVal; i++) { + lua_geti(L, 4, i + 1); + dbv.pbVal[i] = lua_tointeger(L, -1); + lua_pop(L, 1); + } + } + break; + default: + lua_pushboolean(L, false); + return 1; + } + + INT_PTR res = db_set(hContact, szModule, szSetting, &dbv); + lua_pushboolean(L, !res); + + return 1; +} + +static int db_DeleteSetting(lua_State *L) +{ + MCONTACT hContact = lua_tointeger(L, 1); + LPCSTR szModule = luaL_checkstring(L, 2); + LPCSTR szSetting = luaL_checkstring(L, 3); + + INT_PTR res = db_unset(hContact, szModule, szSetting); + lua_pushboolean(L, !res); + + return 1; +} + +/***********************************************/ + +static luaL_Reg databaseApi[] = +{ + { "Contacts", db_Contacts }, + { "GetContactInfo", db_GetContactInfo }, + + { "GetEventCount", db_GetEventCount }, + + { "GetFirstEvent", db_GetFirstEvent }, + { "GetPrevEvent", db_GetPrevEvent }, + { "GetNextEvent", db_GetNextEvent }, + { "GetLastEvent", db_GetLastEvent }, + { "GetFirstUnreadEvent", db_GetFirstUnreadEvent }, + { "Events", db_Events }, + { "EventsFromEnd", db_EventsFromEnd }, + { "UnreadEvents", db_UnreadEvents }, + { "AddEvent", db_AddEvent }, + { "MarkReadEvent", db_MarkReadEvent }, + + { "Settings", db_Settings }, + { "Modules", db_Modules }, + + { "DeleteModule", db_DeleteModule }, + + { "GetSetting", db_GetSetting }, + { "WriteSetting", db_WriteSetting }, + { "SetSetting", db_WriteSetting }, + { "DeleteSetting", db_DeleteSetting }, + + { "DBVT_BYTE", nullptr }, + { "DBVT_WORD", nullptr }, + { "DBVT_DWORD", nullptr }, + { "DBVT_ASCIIZ", nullptr }, + { "DBVT_UTF8", nullptr }, + { "DBVT_WCHAR", nullptr }, + + { nullptr, nullptr } +}; + +/***********************************************/ + +#define MT_DBCONTACTWRITESETTING "DBCONTACTWRITESETTING" + +template <> +int MT<DBCONTACTWRITESETTING>::Get(lua_State *L, DBCONTACTWRITESETTING *dbcw) +{ + const char *key = luaL_checkstring(L, 2); + + if (mir_strcmpi(key, "Value") == 0) + luaM_pushdbvt(L, dbcw->value); + else + lua_pushnil(L); + + return 1; +} + +/***********************************************/ + +#define MT_DBEVENTINFO "DBEVENTINFO" + +template <> +DBEVENTINFO* MT<DBEVENTINFO>::Init(lua_State *L) +{ + MEVENT hDbEvent = luaL_checkinteger(L, 1); + + DBEVENTINFO* dbei = (DBEVENTINFO*)mir_calloc(sizeof(DBEVENTINFO)); + dbei->cbBlob = db_event_getBlobSize((MEVENT)hDbEvent); + dbei->pBlob = (PBYTE)mir_calloc(dbei->cbBlob); + db_event_get((MEVENT)hDbEvent, dbei); + + return dbei; +} + +template <> +int MT<DBEVENTINFO>::Get(lua_State *L, DBEVENTINFO *dbei) +{ + const char *key = luaL_checkstring(L, 2); + + if (mir_strcmpi(key, "Blob") == 0) { + lua_createtable(L, dbei->cbBlob, 0); + for (DWORD i = 0; i < dbei->cbBlob; i++) { + lua_pushinteger(L, dbei->pBlob[i]); + lua_rawseti(L, -2, i + 1); + } + } + else + lua_pushnil(L); + + return 1; +} + +template <> +void MT<DBEVENTINFO>::Free(lua_State*, DBEVENTINFO **dbei) +{ + mir_free((*dbei)->pBlob); + mir_free(*dbei); +} + +/***********************************************/ + +LUAMOD_API int luaopen_m_database(lua_State *L) +{ + luaL_newlib(L, databaseApi); + + lua_pushnumber(L, DBVT_BYTE); + lua_setfield(L, -2, "DBVT_BYTE"); + lua_pushnumber(L, DBVT_WORD); + lua_setfield(L, -2, "DBVT_WORD"); + lua_pushnumber(L, DBVT_DWORD); + lua_setfield(L, -2, "DBVT_DWORD"); + lua_pushnumber(L, DBVT_ASCIIZ); + lua_setfield(L, -2, "DBVT_ASCIIZ"); + lua_pushnumber(L, DBVT_UTF8); + lua_setfield(L, -2, "DBVT_UTF8"); + lua_pushnumber(L, DBVT_WCHAR); + lua_setfield(L, -2, "DBVT_WCHAR"); + + MT<DBCONTACTWRITESETTING>(L, MT_DBCONTACTWRITESETTING) + .Field(&DBCONTACTWRITESETTING::szModule, "Module", LUA_TSTRINGA) + .Field(&DBCONTACTWRITESETTING::szSetting, "Setting", LUA_TSTRINGA); + + MT<DBEVENTINFO>(L, MT_DBEVENTINFO) + .Field(&DBEVENTINFO::szModule, "Module", LUA_TSTRINGA) + .Field(&DBEVENTINFO::timestamp, "Timestamp", LUA_TINTEGER) + .Field(&DBEVENTINFO::eventType, "Type", LUA_TINTEGER) + .Field(&DBEVENTINFO::flags, "Flags", LUA_TINTEGER); + + return 1; +} diff --git a/plugins/MirLua/src/m_genmenu.cpp b/plugins/MirLua/src/Modules/m_genmenu.cpp index 50043827ed..0c2d0ab492 100644 --- a/plugins/MirLua/src/m_genmenu.cpp +++ b/plugins/MirLua/src/Modules/m_genmenu.cpp @@ -1,133 +1,95 @@ -#include "stdafx.h"
-
-void MakeMenuItem(lua_State *L, CMenuItem &mi)
-{
- mi.langId = CMLuaEnvironment::GetEnvironmentId(L);
-
- lua_getfield(L, -1, "Flags");
- mi.flags = lua_tointeger(L, -1);
- lua_pop(L, 1);
-
- if (!(mi.flags & CMIF_UNICODE))
- mi.flags |= CMIF_UNICODE;
-
- lua_getfield(L, -1, "Uid");
- const char* uuid = lua_tostring(L, -1);
- if (UuidFromStringA((RPC_CSTR)uuid, (UUID*)&mi.uid))
- UNSET_UID(mi);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Name");
- mi.name.w = mir_utf8decodeW(luaL_checkstring(L, -1));
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Position");
- mi.position = lua_tointeger(L, -1);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Icon");
- mi.hIcolibItem = (HANDLE)lua_touserdata(L, -1);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Service");
- mi.pszService = lua_tostring(L, -1);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Parent");
- mi.root = (HGENMENU)lua_touserdata(L, -1);
- lua_pop(L, 1);
-}
-
-static int genmenu_ModifyMenuItem(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1);
- ptrW name(mir_utf8decodeW(lua_tostring(L, 2)));
- HANDLE hIcolibItem = luaL_opt(L, lua_touserdata, 3, INVALID_HANDLE_VALUE);
- int flags = luaL_optinteger(L, 4, -1);
-
- if (!(flags & CMIF_UNICODE))
- flags |= CMIF_UNICODE;
-
- INT_PTR res = Menu_ModifyItem(hMenuItem, name, hIcolibItem, flags);
- lua_pushboolean(L, res == 0);
-
- return 1;
-}
-
-static int genmenu_ConfigureMenuItem(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1);
- int option = luaL_checkinteger(L, 2);
- luaL_checktype(L, 3, LUA_TLIGHTUSERDATA);
- INT_PTR value = (INT_PTR)lua_touserdata(L, 3);
-
- int res = Menu_ConfigureItem(hMenuItem, option, value);
- lua_pushboolean(L, res >= 0);
-
- return 1;
-}
-
-static int genmenu_ShowMenuItem(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1);
- bool isShow = luaM_toboolean(L, 2);
-
- Menu_ShowItem(hMenuItem, isShow);
-
- return 0;
-}
-
-static int genmenu_EnableMenuItem(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1);
- bool isEnable = luaM_toboolean(L, 2);
-
- Menu_EnableItem(hMenuItem, isEnable);
-
- return 0;
-}
-
-static int genmenu_CheckMenuItem(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1);
- bool isChecked = luaM_toboolean(L, 2);
-
- Menu_SetChecked(hMenuItem, isChecked);
-
- return 0;
-}
-
-static int genmenu_RemoveMenuItem(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1);
-
- INT_PTR res = Menu_RemoveItem(hMenuItem);
- lua_pushboolean(L, res == 0);
-
- return 1;
-}
-
-static luaL_Reg genmenuApi[] =
-{
- { "ModifyMenuItem", genmenu_ModifyMenuItem },
- { "ConfigureMenuItem", genmenu_ConfigureMenuItem },
- { "ShowMenuItem", genmenu_ShowMenuItem },
- { "EnableMenuItem", genmenu_EnableMenuItem },
- { "CheckMenuItem", genmenu_CheckMenuItem },
- { "RemoveMenuItem", genmenu_RemoveMenuItem },
-
- { nullptr, nullptr }
-};
-
-LUAMOD_API int luaopen_m_genmenu(lua_State *L)
-{
- luaL_newlib(L, genmenuApi);
-
- return 1;
-}
+#include "../stdafx.h" + +static int genmenu_ModifyMenuItem(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); + ptrW name(mir_utf8decodeW(lua_tostring(L, 2))); + HANDLE hIcolibItem = luaL_opt(L, lua_touserdata, 3, INVALID_HANDLE_VALUE); + int flags = luaL_optinteger(L, 4, -1); + + if (!(flags & CMIF_UNICODE)) + flags |= CMIF_UNICODE; + + INT_PTR res = Menu_ModifyItem(hMenuItem, name, hIcolibItem, flags); + lua_pushboolean(L, res == 0); + + return 1; +} + +static int genmenu_ConfigureMenuItem(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); + int option = luaL_checkinteger(L, 2); + luaL_checktype(L, 3, LUA_TLIGHTUSERDATA); + INT_PTR value = (INT_PTR)lua_touserdata(L, 3); + + int res = Menu_ConfigureItem(hMenuItem, option, value); + lua_pushboolean(L, res >= 0); + + return 1; +} + +static int genmenu_ShowMenuItem(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); + bool isShow = luaM_toboolean(L, 2); + + Menu_ShowItem(hMenuItem, isShow); + + return 0; +} + +static int genmenu_EnableMenuItem(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); + bool isEnable = luaM_toboolean(L, 2); + + Menu_EnableItem(hMenuItem, isEnable); + + return 0; +} + +static int genmenu_CheckMenuItem(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); + bool isChecked = luaM_toboolean(L, 2); + + Menu_SetChecked(hMenuItem, isChecked); + + return 0; +} + +static int genmenu_RemoveMenuItem(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + HGENMENU hMenuItem = (HGENMENU)lua_touserdata(L, 1); + + INT_PTR res = Menu_RemoveItem(hMenuItem); + lua_pushboolean(L, res == 0); + + return 1; +} + +static luaL_Reg genmenuApi[] = +{ + { "ModifyMenuItem", genmenu_ModifyMenuItem }, + { "ConfigureMenuItem", genmenu_ConfigureMenuItem }, + { "ShowMenuItem", genmenu_ShowMenuItem }, + { "EnableMenuItem", genmenu_EnableMenuItem }, + { "CheckMenuItem", genmenu_CheckMenuItem }, + { "RemoveMenuItem", genmenu_RemoveMenuItem }, + + { nullptr, nullptr } +}; + +LUAMOD_API int luaopen_m_genmenu(lua_State *L) +{ + luaL_newlib(L, genmenuApi); + + return 1; +} diff --git a/plugins/MirLua/src/m_hotkeys.cpp b/plugins/MirLua/src/Modules/m_hotkeys.cpp index 8e38f16bf8..1245d2348f 100644 --- a/plugins/MirLua/src/m_hotkeys.cpp +++ b/plugins/MirLua/src/Modules/m_hotkeys.cpp @@ -1,103 +1,103 @@ -#include "stdafx.h"
-
-void MakeHotkey(lua_State *L, HOTKEYDESC &hk)
-{
- lua_getfield(L, -1, "Flags");
- hk.dwFlags = lua_tointeger(L, -1);
- lua_pop(L, 1);
-
- if (!(hk.dwFlags & HKD_UNICODE))
- hk.dwFlags |= HKD_UNICODE;
-
- lua_getfield(L, -1, "Name");
- hk.pszName = mir_utf8decodeA(luaL_checkstring(L, -1));
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Description");
- hk.szDescription.w = mir_utf8decodeW(lua_tostring(L, -1));
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Section");
- hk.szSection.w = mir_utf8decodeW(luaL_optstring(L, -1, MODULENAME));
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Hotkey");
- hk.DefHotKey = lua_tointeger(L, -1);
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Service");
- hk.pszService = mir_utf8decodeA(luaL_checkstring(L, -1));
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "lParam");
- hk.lParam = (LPARAM)lua_touserdata(L, -1);
- lua_pop(L, 1);
-}
-
-static int hotkeys_Register(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TTABLE);
-
- HOTKEYDESC hk;
- MakeHotkey(L, hk);
-
- int hScriptLangpack = CMLuaEnvironment::GetEnvironmentId(L);
-
- INT_PTR res = Hotkey_Register(&hk, hScriptLangpack);
- lua_pushboolean(L, res);
-
- return 1;
-}
-
-static int hotkeys_Unregister(lua_State *L)
-{
- const char *name = luaL_checkstring(L, 1);
-
- Hotkey_Unregister(name);
-
- return 0;
-}
-
-static const char *mods[] = { "shift", "ctrl", "alt", "ext", nullptr };
-
-static int hotkeys_MakeHotkey(lua_State *L)
-{
- int mod = 0;
- switch (lua_type(L, 1))
- {
- case LUA_TNUMBER:
- mod = luaL_checkinteger(L, 1);
- break;
- case LUA_TSTRING:
- mod = (1 << (luaL_checkoption(L, 1, nullptr, mods) - 1));
- break;
- case LUA_TTABLE:
- for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1))
- mod |= (1 << (luaL_checkoption(L, -1, nullptr, mods) - 1));
- break;
- default:
- luaL_argerror(L, 1, luaL_typename(L, 1));
- }
- int vk = luaL_checknumber(L, 2);
-
- WORD res = HOTKEYCODE(mod, vk);
- lua_pushinteger(L, res);
-
- return 1;
-}
-
-static luaL_Reg hotkeysApi[] =
-{
- { "MakeHotkey", hotkeys_MakeHotkey },
- { "Register", hotkeys_Register },
- { "Unregister", hotkeys_Unregister },
-
- { nullptr, nullptr }
-};
-
-LUAMOD_API int luaopen_m_hotkeys(lua_State *L)
-{
- luaL_newlib(L, hotkeysApi);
-
- return 1;
-}
+#include "../stdafx.h" + +void MakeHotkey(lua_State *L, HOTKEYDESC &hk) +{ + lua_getfield(L, -1, "Flags"); + hk.dwFlags = lua_tointeger(L, -1); + lua_pop(L, 1); + + if (!(hk.dwFlags & HKD_UNICODE)) + hk.dwFlags |= HKD_UNICODE; + + lua_getfield(L, -1, "Name"); + hk.pszName = mir_utf8decodeA(luaL_checkstring(L, -1)); + lua_pop(L, 1); + + lua_getfield(L, -1, "Description"); + hk.szDescription.w = mir_utf8decodeW(lua_tostring(L, -1)); + lua_pop(L, 1); + + lua_getfield(L, -1, "Section"); + hk.szSection.w = mir_utf8decodeW(luaL_optstring(L, -1, MODULENAME)); + lua_pop(L, 1); + + lua_getfield(L, -1, "Hotkey"); + hk.DefHotKey = lua_tointeger(L, -1); + lua_pop(L, 1); + + lua_getfield(L, -1, "Service"); + hk.pszService = mir_utf8decodeA(luaL_checkstring(L, -1)); + lua_pop(L, 1); + + lua_getfield(L, -1, "lParam"); + hk.lParam = (LPARAM)lua_touserdata(L, -1); + lua_pop(L, 1); +} + +static int hotkeys_Register(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TTABLE); + + HOTKEYDESC hk; + MakeHotkey(L, hk); + + int hScriptLangpack = CMLuaEnvironment::GetEnvironmentId(L); + + INT_PTR res = Hotkey_Register(&hk, hScriptLangpack); + lua_pushboolean(L, res); + + return 1; +} + +static int hotkeys_Unregister(lua_State *L) +{ + const char *name = luaL_checkstring(L, 1); + + Hotkey_Unregister(name); + + return 0; +} + +static const char *mods[] = { "shift", "ctrl", "alt", "ext", nullptr }; + +static int hotkeys_MakeHotkey(lua_State *L) +{ + int mod = 0; + switch (lua_type(L, 1)) + { + case LUA_TNUMBER: + mod = luaL_checkinteger(L, 1); + break; + case LUA_TSTRING: + mod = (1 << (luaL_checkoption(L, 1, nullptr, mods) - 1)); + break; + case LUA_TTABLE: + for (lua_pushnil(L); lua_next(L, 1); lua_pop(L, 1)) + mod |= (1 << (luaL_checkoption(L, -1, nullptr, mods) - 1)); + break; + default: + luaL_argerror(L, 1, luaL_typename(L, 1)); + } + int vk = luaL_checknumber(L, 2); + + WORD res = HOTKEYCODE(mod, vk); + lua_pushinteger(L, res); + + return 1; +} + +static luaL_Reg hotkeysApi[] = +{ + { "MakeHotkey", hotkeys_MakeHotkey }, + { "Register", hotkeys_Register }, + { "Unregister", hotkeys_Unregister }, + + { nullptr, nullptr } +}; + +LUAMOD_API int luaopen_m_hotkeys(lua_State *L) +{ + luaL_newlib(L, hotkeysApi); + + return 1; +} diff --git a/plugins/MirLua/src/m_http.cpp b/plugins/MirLua/src/Modules/m_http.cpp index 5d1e7d4519..31def57a9a 100644 --- a/plugins/MirLua/src/m_http.cpp +++ b/plugins/MirLua/src/Modules/m_http.cpp @@ -1,4 +1,4 @@ -#include "stdafx.h" +#include "../stdafx.h" /***********************************************/ diff --git a/plugins/MirLua/src/m_icolib.cpp b/plugins/MirLua/src/Modules/m_icolib.cpp index a83c9877d7..86b5163290 100644 --- a/plugins/MirLua/src/m_icolib.cpp +++ b/plugins/MirLua/src/Modules/m_icolib.cpp @@ -1,4 +1,4 @@ -#include "stdafx.h"
+#include "../stdafx.h"
static void MakeSKINICONDESC(lua_State *L, SKINICONDESC &sid)
{
@@ -25,8 +25,7 @@ static void MakeSKINICONDESC(lua_State *L, SKINICONDESC &sid) sid.defaultFile.w = mir_utf8decodeW(lua_tostring(L, -1));
lua_pop(L, 1);
- if (sid.defaultFile.w == nullptr)
- {
+ if (sid.defaultFile.w == nullptr) {
sid.defaultFile.w = (wchar_t*)mir_calloc(MAX_PATH + 1);
GetModuleFileName(g_plugin.getInst(), sid.defaultFile.w, MAX_PATH);
}
@@ -52,8 +51,7 @@ static int lua_AddIcon(lua_State *L) {
SKINICONDESC sid = { };
- if (lua_type(L, 1) == LUA_TSTRING)
- {
+ if (lua_type(L, 1) == LUA_TSTRING) {
sid.flags = SIDF_ALL_UNICODE;
sid.pszName = mir_utf8decodeA(luaL_checkstring(L, 1));
sid.description.w = mir_utf8decodeW(luaL_checkstring(L, 2));
@@ -61,8 +59,7 @@ static int lua_AddIcon(lua_State *L) sid.defaultFile.w = mir_utf8decodeW(lua_tostring(L, 4));
sid.hDefaultIcon = GetIcon(IDI_SCRIPT);
- if (sid.defaultFile.w == nullptr)
- {
+ if (sid.defaultFile.w == nullptr) {
sid.defaultFile.w = (wchar_t*)mir_calloc(MAX_PATH + 1);
GetModuleFileName(g_plugin.getInst(), sid.defaultFile.w, MAX_PATH);
}
@@ -89,8 +86,7 @@ static int lua_GetIcon(lua_State *L) bool big = luaM_toboolean(L, 2);
HICON hIcon = nullptr;
- switch (lua_type(L, 1))
- {
+ switch (lua_type(L, 1)) {
case LUA_TLIGHTUSERDATA:
hIcon = IcoLib_GetIconByHandle(lua_touserdata(L, 1), big);
break;
@@ -121,8 +117,7 @@ static int lua_GetIconHandle(lua_State *L) static int lua_RemoveIcon(lua_State *L)
{
- switch (lua_type(L, 1))
- {
+ switch (lua_type(L, 1)) {
case LUA_TLIGHTUSERDATA:
IcoLib_RemoveIconByHandle(lua_touserdata(L, 1));
break;
diff --git a/plugins/MirLua/src/m_json.cpp b/plugins/MirLua/src/Modules/m_json.cpp index eccef5cff2..24e32cab09 100644 --- a/plugins/MirLua/src/m_json.cpp +++ b/plugins/MirLua/src/Modules/m_json.cpp @@ -1,4 +1,6 @@ -#include "stdafx.h" +#include "../stdafx.h" + +#define MT_JSON "JSON" static void lua2json(lua_State *L, JSONNode &node) { diff --git a/plugins/MirLua/src/m_message.cpp b/plugins/MirLua/src/Modules/m_message.cpp index 4ac1f991e9..c369237796 100644 --- a/plugins/MirLua/src/m_message.cpp +++ b/plugins/MirLua/src/Modules/m_message.cpp @@ -1,73 +1,73 @@ -#include "stdafx.h"
-
-static int message_Paste(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
- ptrW text(mir_utf8decodeW(luaL_checkstring(L, 2)));
-
- MessageWindowData mwd;
- INT_PTR res = Srmm_GetWindowData(hContact, mwd);
- lua_pushinteger(L, res);
- if (res)
- return 1;
-
- HWND hEdit = GetDlgItem(mwd.hwndWindow, 1002 /*IDC_MESSAGE*/);
- if (!hEdit) hEdit = GetDlgItem(mwd.hwndWindow, 1009 /*IDC_CHATMESSAGE*/);
-
- SendMessage(hEdit, EM_REPLACESEL, TRUE, (LPARAM)text);
-
- return 1;
-}
-
-static int message_Send(lua_State *L)
-{
- MCONTACT hContact = luaL_checkinteger(L, 1);
- const char *message = luaL_checkstring(L, 2);
-
- INT_PTR res = 1;
-
- const char *szProto = GetContactProto(hContact);
- if (db_get_b(hContact, szProto, "ChatRoom", 0) == TRUE) {
- ptrW wszChatRoom(db_get_wsa(hContact, szProto, "ChatRoomID"));
- ptrW wszMessage(mir_utf8decodeW(message));
- res = Chat_SendUserMessage(szProto, wszChatRoom, wszMessage);
- lua_pushinteger(L, res);
- }
- else if ((res = ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)message)) != ACKRESULT_FAILED) {
- DBEVENTINFO dbei = {};
- dbei.szModule = MODULENAME;
- dbei.timestamp = time(0);
- dbei.eventType = EVENTTYPE_MESSAGE;
- dbei.cbBlob = (DWORD)mir_strlen(message);
- dbei.pBlob = (PBYTE)mir_strdup(message);
- dbei.flags = DBEF_UTF | DBEF_SENT;
- db_event_add(hContact, &dbei);
-
- lua_pushinteger(L, res);
- return 1;
- }
-
- lua_pushinteger(L, res);
-
- return 1;
-}
-
-static luaL_Reg messageApi[] =
-{
- { "Paste", message_Paste },
- { "Send", message_Send },
-
- { nullptr, nullptr }
-};
-
-LUAMOD_API int luaopen_m_message(lua_State *L)
-{
- luaL_newlib(L, messageApi);
-
- MT<MessageWindowEventData>(L, "MessageWindowEventData")
- .Field(&MessageWindowEventData::uType, "Type", LUA_TINTEGER)
- .Field(&MessageWindowEventData::hContact, "hContact", LUA_TINTEGER)
- .Field(&MessageWindowEventData::uFlags, "Flags", LUA_TINTEGER);
-
- return 1;
-}
+#include "../stdafx.h" + +static int message_Paste(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + ptrW text(mir_utf8decodeW(luaL_checkstring(L, 2))); + + MessageWindowData mwd; + INT_PTR res = Srmm_GetWindowData(hContact, mwd); + lua_pushinteger(L, res); + if (res) + return 1; + + HWND hEdit = GetDlgItem(mwd.hwndWindow, 1002 /*IDC_MESSAGE*/); + if (!hEdit) hEdit = GetDlgItem(mwd.hwndWindow, 1009 /*IDC_CHATMESSAGE*/); + + SendMessage(hEdit, EM_REPLACESEL, TRUE, (LPARAM)text); + + return 1; +} + +static int message_Send(lua_State *L) +{ + MCONTACT hContact = luaL_checkinteger(L, 1); + const char *message = luaL_checkstring(L, 2); + + INT_PTR res = 1; + + const char *szProto = GetContactProto(hContact); + if (db_get_b(hContact, szProto, "ChatRoom", 0) == TRUE) { + ptrW wszChatRoom(db_get_wsa(hContact, szProto, "ChatRoomID")); + ptrW wszMessage(mir_utf8decodeW(message)); + res = Chat_SendUserMessage(szProto, wszChatRoom, wszMessage); + lua_pushinteger(L, res); + } + else if ((res = ProtoChainSend(hContact, PSS_MESSAGE, 0, (LPARAM)message)) != ACKRESULT_FAILED) { + DBEVENTINFO dbei = {}; + dbei.szModule = MODULENAME; + dbei.timestamp = time(0); + dbei.eventType = EVENTTYPE_MESSAGE; + dbei.cbBlob = (DWORD)mir_strlen(message); + dbei.pBlob = (PBYTE)mir_strdup(message); + dbei.flags = DBEF_UTF | DBEF_SENT; + db_event_add(hContact, &dbei); + + lua_pushinteger(L, res); + return 1; + } + + lua_pushinteger(L, res); + + return 1; +} + +static luaL_Reg messageApi[] = +{ + { "Paste", message_Paste }, + { "Send", message_Send }, + + { nullptr, nullptr } +}; + +LUAMOD_API int luaopen_m_message(lua_State *L) +{ + luaL_newlib(L, messageApi); + + MT<MessageWindowEventData>(L, "MessageWindowEventData") + .Field(&MessageWindowEventData::uType, "Type", LUA_TINTEGER) + .Field(&MessageWindowEventData::hContact, "hContact", LUA_TINTEGER) + .Field(&MessageWindowEventData::uFlags, "Flags", LUA_TINTEGER); + + return 1; +} diff --git a/plugins/MirLua/src/m_options.cpp b/plugins/MirLua/src/Modules/m_options.cpp index 32df2ca366..babcfe3909 100644 --- a/plugins/MirLua/src/m_options.cpp +++ b/plugins/MirLua/src/Modules/m_options.cpp @@ -1,117 +1,117 @@ -#include "stdafx.h"
-
-class CMLuaScriptOptionPage : public CDlgBase
-{
-private:
- int m_onInitDialogRef;
- int m_onApplyRef;
- lua_State *L;
-
-public:
- CMLuaScriptOptionPage(lua_State *_L, int onInitDialogRef, int onApplyRef)
- : CDlgBase(g_plugin, IDD_SCRIPTOPTIONSPAGE), L(_L),
- m_onInitDialogRef(onInitDialogRef), m_onApplyRef(onApplyRef)
- {
- }
-
- void OnInitDialog() override
- {
- if (m_onInitDialogRef)
- {
- lua_rawgeti(L, LUA_REGISTRYINDEX, m_onInitDialogRef);
- lua_pushlightuserdata(L, m_hwnd);
- luaM_pcall(L, 1, 0);
- }
- }
-
- void OnApply() override
- {
- if (m_onApplyRef)
- {
- lua_rawgeti(L, LUA_REGISTRYINDEX, m_onApplyRef);
- lua_pushlightuserdata(L, m_hwnd);
- luaM_pcall(L, 1, 0);
- }
- }
-
- void OnDestroy() override
- {
- lua_pushnil(L);
- lua_rawsetp(L, LUA_REGISTRYINDEX, this);
- }
-};
-
-void MakeOptionDialogPage(lua_State *L, OPTIONSDIALOGPAGE &odp)
-{
- odp.hInstance = g_plugin.getInst();
- odp.langId = CMLuaEnvironment::GetEnvironmentId(L);
-
- lua_getfield(L, -1, "Flags");
- odp.flags = luaL_optinteger(L, -1, ODPF_BOLDGROUPS | ODPF_UNICODE | ODPF_DONTTRANSLATE);
- lua_pop(L, 1);
-
- if (!(odp.flags & ODPF_UNICODE))
- odp.flags |= ODPF_UNICODE;
-
- lua_getfield(L, -1, "Group");
- odp.szGroup.w = mir_utf8decodeW(lua_tostring(L, -1));
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Title");
- odp.szTitle.w = mir_utf8decodeW(luaL_checkstring(L, -1));
- lua_pop(L, 1);
-
- lua_getfield(L, -1, "Tab");
- odp.szTab.w = mir_utf8decodeW(lua_tostring(L, -1));
- lua_pop(L, 1);
-
- int onInitDialogRef = LUA_NOREF;
- lua_getfield(L, -1, "OnInitDialog");
- if (lua_isfunction(L, -1))
- onInitDialogRef = luaL_ref(L, LUA_REGISTRYINDEX);
- else
- lua_pop(L, 1);
-
- int onApplyRef = LUA_NOREF;
- lua_getfield(L, -1, "OnApply");
- if (lua_isfunction(L, -1))
- onApplyRef = luaL_ref(L, LUA_REGISTRYINDEX);
- else
- lua_pop(L, 1);
-
- lua_State *T = lua_newthread(L);
- lua_rawsetp(L, LUA_REGISTRYINDEX, T);
- odp.pDialog = new CMLuaScriptOptionPage(T, onInitDialogRef, onApplyRef);
-}
-
-int opt_AddPage(lua_State *L)
-{
- luaL_checktype(L, 1, LUA_TLIGHTUSERDATA);
- WPARAM wParam = (WPARAM)lua_touserdata(L, 1);
-
- OPTIONSDIALOGPAGE odp = { 0 };
- MakeOptionDialogPage(L, odp);
-
- INT_PTR res = Options_AddPage(wParam, &odp);
- lua_pushboolean(L, !res);
-
- mir_free(odp.szGroup.w);
- mir_free(odp.szTitle.w);
- mir_free(odp.szTab.w);
-
- return 1;
-}
-
-static luaL_Reg optionsApi[] =
-{
- { "AddPage", opt_AddPage },
-
- { nullptr, nullptr }
-};
-
-LUAMOD_API int luaopen_m_options(lua_State *L)
-{
- luaL_newlib(L, optionsApi);
-
- return 1;
+#include "../stdafx.h" + +class CMLuaScriptOptionPage : public CDlgBase +{ +private: + int m_onInitDialogRef; + int m_onApplyRef; + lua_State *L; + +public: + CMLuaScriptOptionPage(lua_State *_L, int onInitDialogRef, int onApplyRef) + : CDlgBase(g_plugin, IDD_SCRIPTOPTIONSPAGE), L(_L), + m_onInitDialogRef(onInitDialogRef), m_onApplyRef(onApplyRef) + { + } + + void OnInitDialog() override + { + if (m_onInitDialogRef) + { + lua_rawgeti(L, LUA_REGISTRYINDEX, m_onInitDialogRef); + lua_pushlightuserdata(L, m_hwnd); + luaM_pcall(L, 1, 0); + } + } + + void OnApply() override + { + if (m_onApplyRef) + { + lua_rawgeti(L, LUA_REGISTRYINDEX, m_onApplyRef); + lua_pushlightuserdata(L, m_hwnd); + luaM_pcall(L, 1, 0); + } + } + + void OnDestroy() override + { + lua_pushnil(L); + lua_rawsetp(L, LUA_REGISTRYINDEX, this); + } +}; + +void MakeOptionDialogPage(lua_State *L, OPTIONSDIALOGPAGE &odp) +{ + odp.hInstance = g_plugin.getInst(); + odp.langId = CMLuaEnvironment::GetEnvironmentId(L); + + lua_getfield(L, -1, "Flags"); + odp.flags = luaL_optinteger(L, -1, ODPF_BOLDGROUPS | ODPF_UNICODE | ODPF_DONTTRANSLATE); + lua_pop(L, 1); + + if (!(odp.flags & ODPF_UNICODE)) + odp.flags |= ODPF_UNICODE; + + lua_getfield(L, -1, "Group"); + odp.szGroup.w = mir_utf8decodeW(lua_tostring(L, -1)); + lua_pop(L, 1); + + lua_getfield(L, -1, "Title"); + odp.szTitle.w = mir_utf8decodeW(luaL_checkstring(L, -1)); + lua_pop(L, 1); + + lua_getfield(L, -1, "Tab"); + odp.szTab.w = mir_utf8decodeW(lua_tostring(L, -1)); + lua_pop(L, 1); + + int onInitDialogRef = LUA_NOREF; + lua_getfield(L, -1, "OnInitDialog"); + if (lua_isfunction(L, -1)) + onInitDialogRef = luaL_ref(L, LUA_REGISTRYINDEX); + else + lua_pop(L, 1); + + int onApplyRef = LUA_NOREF; + lua_getfield(L, -1, "OnApply"); + if (lua_isfunction(L, -1)) + onApplyRef = luaL_ref(L, LUA_REGISTRYINDEX); + else + lua_pop(L, 1); + + lua_State *T = lua_newthread(L); + lua_rawsetp(L, LUA_REGISTRYINDEX, T); + odp.pDialog = new CMLuaScriptOptionPage(T, onInitDialogRef, onApplyRef); +} + +int opt_AddPage(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); + WPARAM wParam = (WPARAM)lua_touserdata(L, 1); + + OPTIONSDIALOGPAGE odp = { 0 }; + MakeOptionDialogPage(L, odp); + + INT_PTR res = Options_AddPage(wParam, &odp); + lua_pushboolean(L, !res); + + mir_free(odp.szGroup.w); + mir_free(odp.szTitle.w); + mir_free(odp.szTab.w); + + return 1; +} + +static luaL_Reg optionsApi[] = +{ + { "AddPage", opt_AddPage }, + + { nullptr, nullptr } +}; + +LUAMOD_API int luaopen_m_options(lua_State *L) +{ + luaL_newlib(L, optionsApi); + + return 1; }
\ No newline at end of file diff --git a/plugins/MirLua/src/m_protocols.cpp b/plugins/MirLua/src/Modules/m_protocols.cpp index a2e3d7fbc1..1dac3e3ea6 100644 --- a/plugins/MirLua/src/m_protocols.cpp +++ b/plugins/MirLua/src/Modules/m_protocols.cpp @@ -1,4 +1,6 @@ -#include "stdafx.h"
+#include "../stdafx.h"
+
+#define MT_PROTOCOLDESCRIPTOR "PROTOCOLDESCRIPTOR"
HANDLE hRecvMessage = nullptr;
@@ -236,6 +238,9 @@ static luaL_Reg protocolsApi[] = LUAMOD_API int luaopen_m_protocols(lua_State *L)
{
+ hRecvMessage = CreateHookableEvent(MODULENAME PSR_MESSAGE);
+ CreateProtoServiceFunction(MODULENAME, PSR_MESSAGE, FilterRecvMessage);
+
luaL_newlib(L, protocolsApi);
MT<PROTOCOLDESCRIPTOR>(L, MT_PROTOCOLDESCRIPTOR)
diff --git a/plugins/MirLua/src/m_sounds.cpp b/plugins/MirLua/src/Modules/m_sounds.cpp index 177148320a..74b17f12cd 100644 --- a/plugins/MirLua/src/m_sounds.cpp +++ b/plugins/MirLua/src/Modules/m_sounds.cpp @@ -1,4 +1,4 @@ -#include "stdafx.h"
+#include "../stdafx.h"
static int lua_AddSound(lua_State *L)
{
@@ -7,12 +7,10 @@ static int lua_AddSound(lua_State *L) ptrW section(mir_utf8decodeW(luaL_optstring(L, 3, MODULENAME)));
ptrW filePath(mir_utf8decodeW(lua_tostring(L, 4)));
- int res;
- CMPluginBase *pPlugin = GetPluginByLangId(CMLuaEnvironment::GetEnvironmentId(L));
+ int res = 1;
+ CMPluginBase *pPlugin = CMLuaEnvironment::GetEnvironment(L);
if (pPlugin != nullptr)
res = pPlugin->addSound(name, section, description, filePath);
- else
- res = 1;
lua_pushboolean(L, res == 0);
return 1;
diff --git a/plugins/MirLua/src/m_srmm.cpp b/plugins/MirLua/src/Modules/m_srmm.cpp index 8ec739bd27..f7296d1d70 100644 --- a/plugins/MirLua/src/m_srmm.cpp +++ b/plugins/MirLua/src/Modules/m_srmm.cpp @@ -1,4 +1,4 @@ -#include "stdafx.h" +#include "../stdafx.h" #define MT_BBBUTTON "BBButton" diff --git a/plugins/MirLua/src/mlua_environment.cpp b/plugins/MirLua/src/environment.cpp index 4a82e63231..4a82e63231 100644 --- a/plugins/MirLua/src/mlua_environment.cpp +++ b/plugins/MirLua/src/environment.cpp diff --git a/plugins/MirLua/src/mlua_environment.h b/plugins/MirLua/src/environment.h index aa6ebb103a..aa6ebb103a 100644 --- a/plugins/MirLua/src/mlua_environment.h +++ b/plugins/MirLua/src/environment.h diff --git a/plugins/MirLua/src/mlua_function_loader.cpp b/plugins/MirLua/src/function_loader.cpp index 6e82086d29..6e82086d29 100644 --- a/plugins/MirLua/src/mlua_function_loader.cpp +++ b/plugins/MirLua/src/function_loader.cpp diff --git a/plugins/MirLua/src/mlua_function_loader.h b/plugins/MirLua/src/function_loader.h index 9724ccdc3d..9724ccdc3d 100644 --- a/plugins/MirLua/src/mlua_function_loader.h +++ b/plugins/MirLua/src/function_loader.h diff --git a/plugins/MirLua/src/mlua_icons.cpp b/plugins/MirLua/src/icons.cpp index 3b2466bc27..4a6cc91219 100644 --- a/plugins/MirLua/src/mlua_icons.cpp +++ b/plugins/MirLua/src/icons.cpp @@ -1,33 +1,33 @@ -#include "stdafx.h"
-
-IconItem Icons[] =
-{
- { LPGEN("Script"), "script", IDI_SCRIPT },
- { LPGEN("Loaded"), "loaded", IDI_LOADED },
- { LPGEN("Failed"), "failed", IDI_FAILED },
- { LPGEN("Open"), "open", IDI_OPEN },
- { LPGEN("Reload"), "reload", IDI_RELOAD },
-};
-
-void InitIcons()
-{
- g_plugin.registerIcon(MODULENAME, Icons, MODULENAME);
-}
-
-HICON GetIcon(int iconId)
-{
- for (auto &it : Icons)
- if (it.defIconID == iconId)
- return IcoLib_GetIconByHandle(it.hIcolib);
-
- return nullptr;
-}
-
-HANDLE GetIconHandle(int iconId)
-{
- for (auto &it : Icons)
- if (it.defIconID == iconId)
- return it.hIcolib;
-
- return nullptr;
+#include "stdafx.h" + +IconItem Icons[] = +{ + { LPGEN("Script"), "script", IDI_SCRIPT }, + { LPGEN("Loaded"), "loaded", IDI_LOADED }, + { LPGEN("Failed"), "failed", IDI_FAILED }, + { LPGEN("Open"), "open", IDI_OPEN }, + { LPGEN("Reload"), "reload", IDI_RELOAD }, +}; + +void InitIcons() +{ + g_plugin.registerIcon(MODULENAME, Icons, MODULENAME); +} + +HICON GetIcon(int iconId) +{ + for (auto &it : Icons) + if (it.defIconID == iconId) + return IcoLib_GetIconByHandle(it.hIcolib); + + return nullptr; +} + +HANDLE GetIconHandle(int iconId) +{ + for (auto &it : Icons) + if (it.defIconID == iconId) + return it.hIcolib; + + return nullptr; }
\ No newline at end of file diff --git a/plugins/MirLua/src/m_database.h b/plugins/MirLua/src/m_database.h deleted file mode 100644 index 73ba76f28c..0000000000 --- a/plugins/MirLua/src/m_database.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _LUA_M_DATABASE_H_ -#define _LUA_M_DATABASE_H_ - -#include <m_database.h> - -#define MLUA_DATABASE "m_database" -LUAMOD_API int (luaopen_m_database)(lua_State *L); - -#endif //_LUA_M_DATABASE_H_
\ No newline at end of file diff --git a/plugins/MirLua/src/m_genmenu.h b/plugins/MirLua/src/m_genmenu.h deleted file mode 100644 index b368627dc8..0000000000 --- a/plugins/MirLua/src/m_genmenu.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _LUA_M_GENMENU_H_
-#define _LUA_M_GENMENU_H_
-
-#include <m_genmenu.h>
-
-#define MLUA_GENMENU "m_genmenu"
-LUAMOD_API int (luaopen_m_genmenu)(lua_State *L);
-
-void MakeMenuItem(lua_State *L, CMenuItem &mi);
-
-#endif //_LUA_M_GENMENU_H_
\ No newline at end of file diff --git a/plugins/MirLua/src/m_json.h b/plugins/MirLua/src/m_json.h deleted file mode 100644 index 1357505bec..0000000000 --- a/plugins/MirLua/src/m_json.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef _LUA_M_JSON_H_ -#define _LUA_M_JSON_H_ - -#define MT_JSON "JSON" - -#define MLUA_JSON "m_json" -LUAMOD_API int (luaopen_m_json)(lua_State *L); - -#endif //_LUA_M_PROTOCOLS_H_
\ No newline at end of file diff --git a/plugins/MirLua/src/m_protocols.h b/plugins/MirLua/src/m_protocols.h deleted file mode 100644 index b80d38023b..0000000000 --- a/plugins/MirLua/src/m_protocols.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef _LUA_M_PROTOCOLS_H_
-#define _LUA_M_PROTOCOLS_H_
-
-#define MLUA_PROTOCOLS "m_protocols"
-LUAMOD_API int (luaopen_m_protocols)(lua_State *L);
-
-#define MT_PROTOCOLDESCRIPTOR "PROTOCOLDESCRIPTOR"
-#define MT_PROTOACCOUNT "PROTOACCOUNT"
-
-extern HANDLE hRecvMessage;
-INT_PTR FilterRecvMessage(WPARAM wParam, LPARAM lParam);
-
-#endif //_LUA_M_PROTOCOLS_H_
\ No newline at end of file diff --git a/plugins/MirLua/src/mlua_metatable.h b/plugins/MirLua/src/metatable.h index 33d3b40e7e..aa736428ca 100644 --- a/plugins/MirLua/src/mlua_metatable.h +++ b/plugins/MirLua/src/metatable.h @@ -1,5 +1,4 @@ -#ifndef _LUA_METATABLE_H_
-#define _LUA_METATABLE_H_
+#pragma once
#include <map>
#include <cstddef>
@@ -343,5 +342,3 @@ const luaL_Reg MT<T>::Events[] = { template<typename T>
OBJLIST<CMTField> MT<T>::Fields(5, &CMTField::Compare);
-
-#endif //_LUA_METATABLE_H_
diff --git a/plugins/MirLua/src/mlua_module_loader.cpp b/plugins/MirLua/src/module_loader.cpp index ff9594f363..9b82f8fdcf 100644 --- a/plugins/MirLua/src/mlua_module_loader.cpp +++ b/plugins/MirLua/src/module_loader.cpp @@ -1,46 +1,46 @@ -#include "stdafx.h"
-
-CMLuaModuleLoader::CMLuaModuleLoader(lua_State *L) : L(L)
-{
-}
-
-void CMLuaModuleLoader::Load(const char *name, lua_CFunction loader)
-{
- luaL_requiref(L, name, loader, 0);
-}
-
-void CMLuaModuleLoader::Preload(const char *name, lua_CFunction loader)
-{
- luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
- lua_pushcfunction(L, loader);
- lua_setfield(L, -2, name);
- lua_pop(L, 1);
-}
-
-void CMLuaModuleLoader::LoadModules()
-{
- Log("Loading miranda modules");
-
- // load m_core module
- Load(MLUA_CORE, luaopen_m_core);
- // load all internal modules
- Preload(MLUA_CHAT, luaopen_m_chat);
- Preload(MLUA_CLIST, luaopen_m_clist);
- Preload(MLUA_DATABASE, luaopen_m_database);
- Preload(MLUA_GENMENU, luaopen_m_genmenu);
- Preload(MLUA_HOTKEYS, luaopen_m_hotkeys);
- Preload(MLUA_HTTP, luaopen_m_http);
- Preload(MLUA_ICOLIB, luaopen_m_icolib);
- Preload(MLUA_JSON, luaopen_m_json);
- Preload(MLUA_MESSAGE, luaopen_m_message);
- Preload(MLUA_OPTIONS, luaopen_m_options);
- Preload(MLUA_PROTOCOLS, luaopen_m_protocols);
- Preload(MLUA_SOUNDS, luaopen_m_sounds);
- Preload(MLUA_SRMM, luaopen_m_srmm);
-}
-
-void CMLuaModuleLoader::Load(lua_State *L)
-{
- CMLuaModuleLoader loader(L);
- loader.LoadModules();
+#include "stdafx.h" + +CMLuaModuleLoader::CMLuaModuleLoader(lua_State *L) : L(L) +{ +} + +void CMLuaModuleLoader::Load(const char *name, lua_CFunction loader) +{ + luaL_requiref(L, name, loader, 0); +} + +void CMLuaModuleLoader::Preload(const char *name, lua_CFunction loader) +{ + luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); + lua_pushcfunction(L, loader); + lua_setfield(L, -2, name); + lua_pop(L, 1); +} + +void CMLuaModuleLoader::LoadModules() +{ + Log("Loading miranda modules"); + + // load m_core module + Load(MLUA_CORE, luaopen_m_core); + // load all internal modules + Preload(MLUA_CHAT, luaopen_m_chat); + Preload(MLUA_CLIST, luaopen_m_clist); + Preload(MLUA_DATABASE, luaopen_m_database); + Preload(MLUA_GENMENU, luaopen_m_genmenu); + Preload(MLUA_HOTKEYS, luaopen_m_hotkeys); + Preload(MLUA_HTTP, luaopen_m_http); + Preload(MLUA_ICOLIB, luaopen_m_icolib); + Preload(MLUA_JSON, luaopen_m_json); + Preload(MLUA_MESSAGE, luaopen_m_message); + Preload(MLUA_OPTIONS, luaopen_m_options); + Preload(MLUA_PROTOCOLS, luaopen_m_protocols); + Preload(MLUA_SOUNDS, luaopen_m_sounds); + Preload(MLUA_SRMM, luaopen_m_srmm); +} + +void CMLuaModuleLoader::Load(lua_State *L) +{ + CMLuaModuleLoader loader(L); + loader.LoadModules(); }
\ No newline at end of file diff --git a/plugins/MirLua/src/mlua_module_loader.h b/plugins/MirLua/src/module_loader.h index bab8e79e84..5b43e7ca49 100644 --- a/plugins/MirLua/src/mlua_module_loader.h +++ b/plugins/MirLua/src/module_loader.h @@ -1,17 +1,17 @@ -#pragma once
-
-class CMLuaModuleLoader
-{
-private:
- lua_State *L;
-
- CMLuaModuleLoader(lua_State *L);
-
- void Load(const char *name, lua_CFunction loader);
- void Preload(const char *name, lua_CFunction loader);
-
- void LoadModules();
-
-public:
- static void Load(lua_State *L);
-};
+#pragma once + +class CMLuaModuleLoader +{ +private: + lua_State *L; + + CMLuaModuleLoader(lua_State *L); + + void Load(const char *name, lua_CFunction loader); + void Preload(const char *name, lua_CFunction loader); + + void LoadModules(); + +public: + static void Load(lua_State *L); +}; diff --git a/plugins/MirLua/src/modules.h b/plugins/MirLua/src/modules.h new file mode 100644 index 0000000000..7aa4402efb --- /dev/null +++ b/plugins/MirLua/src/modules.h @@ -0,0 +1,44 @@ +#pragma once + +#define MLUA_CORE "m_core" +LUAMOD_API int (luaopen_m_core)(lua_State *L); + +#define MLUA_CHAT "m_chat" +LUAMOD_API int (luaopen_m_chat)(lua_State *L); + +#define MLUA_CLIST "m_clist" +LUAMOD_API int (luaopen_m_clist)(lua_State *L); + +#define MLUA_DATABASE "m_database" +LUAMOD_API int (luaopen_m_database)(lua_State *L); + +#define MLUA_ICOLIB "m_icolib" +LUAMOD_API int (luaopen_m_icolib)(lua_State *L); + +#define MLUA_GENMENU "m_genmenu" +LUAMOD_API int (luaopen_m_genmenu)(lua_State *L); + +#define MLUA_HTTP "m_http" +LUAMOD_API int (luaopen_m_http)(lua_State *L); + +#define MLUA_HOTKEYS "m_hotkeys" +LUAMOD_API int (luaopen_m_hotkeys)(lua_State *L); + +#define MLUA_JSON "m_json" +LUAMOD_API int (luaopen_m_json)(lua_State *L); + +#define MLUA_MESSAGE "m_message" +LUAMOD_API int (luaopen_m_message)(lua_State *L); + +#define MLUA_OPTIONS "m_options" +LUAMOD_API int (luaopen_m_options)(lua_State *L); + +#define MLUA_PROTOCOLS "m_protocols" +#define MT_PROTOACCOUNT "PROTOACCOUNT" +LUAMOD_API int (luaopen_m_protocols)(lua_State *L); + +#define MLUA_SOUNDS "m_sounds" +LUAMOD_API int (luaopen_m_sounds)(lua_State *L); + +#define MLUA_SRMM "m_srmm" +LUAMOD_API int (luaopen_m_srmm)(lua_State *L);
\ No newline at end of file diff --git a/plugins/MirLua/src/mlua_options.cpp b/plugins/MirLua/src/options.cpp index 226a925fd9..3c2dade81b 100644 --- a/plugins/MirLua/src/mlua_options.cpp +++ b/plugins/MirLua/src/options.cpp @@ -1,11 +1,12 @@ #include "stdafx.h"
-CMLuaOptions::CMLuaOptions(CMLua *mLua)
+CMLuaOptions::CMLuaOptions()
: CPluginDlgBase(g_plugin, IDD_OPTIONS, MODULENAME),
- m_mLua(mLua), isScriptListInit(false),
+ isScriptListInit(false),
m_popupOnError(this, IDC_POPUPONERROR),
m_popupOnObsolete(this, IDC_POPUPONOBSOLETE),
- m_scripts(this, IDC_SCRIPTS), m_reload(this, IDC_RELOAD)
+ m_scripts(this, IDC_SCRIPTS),
+ m_reload(this, IDC_RELOAD)
{
CreateLink(m_popupOnError, "PopupOnError", DBVT_BYTE, 1);
CreateLink(m_popupOnObsolete, "PopupOnObsolete", DBVT_BYTE, 1);
@@ -16,7 +17,7 @@ CMLuaOptions::CMLuaOptions(CMLua *mLua) void CMLuaOptions::LoadScripts()
{
- for (auto &script : m_mLua->Scripts) {
+ for (auto &script : g_plugin.Scripts) {
wchar_t *fileName = NEWWSTR_ALLOCA(script->GetFileName());
int iIcon = script->GetStatus() - 1;
int iItem = m_scripts.AddItem(fileName, iIcon, (LPARAM)script);
@@ -124,7 +125,7 @@ void CMLuaOptions::OnReload(CCtrlBase*) {
isScriptListInit = false;
m_scripts.DeleteAllItems();
- m_mLua->Reload();
+ g_plugin.Reload();
LoadScripts();
isScriptListInit = true;
}
diff --git a/plugins/MirLua/src/mlua_options.h b/plugins/MirLua/src/options.h index 2e3b4ff7f7..9658ffe336 100644 --- a/plugins/MirLua/src/mlua_options.h +++ b/plugins/MirLua/src/options.h @@ -1,10 +1,8 @@ -#ifndef _LUA_OPTIONS_H_
-#define _LUA_OPTIONS_H_
+#pragma once
class CMLuaOptions : public CPluginDlgBase
{
private:
- CMLua *m_mLua;
bool isScriptListInit;
CCtrlCheck m_popupOnError;
@@ -25,7 +23,5 @@ protected: INT_PTR DlgProc(UINT msg, WPARAM wParam, LPARAM lParam);
public:
- CMLuaOptions(CMLua *mLua);
+ CMLuaOptions();
};
-
-#endif //_LUA_OPTIONS_H_
\ No newline at end of file diff --git a/plugins/MirLua/src/mlua_script.cpp b/plugins/MirLua/src/script.cpp index 9e4cc4ffbe..976ed79688 100644 --- a/plugins/MirLua/src/mlua_script.cpp +++ b/plugins/MirLua/src/script.cpp @@ -7,15 +7,15 @@ CMLuaScript::CMLuaScript(lua_State *L, const wchar_t *path) {
mir_wstrcpy(filePath, path);
- fileName = wcsrchr(filePath, '\\') + 1;
- wchar_t *dot = wcsrchr(fileName, '.');
+ fileName = wcsrchr(filePath, L'\\') + 1;
+ const wchar_t *dot = wcsrchr(fileName, '.');
size_t length = mir_wstrlen(fileName) - mir_wstrlen(dot) + 1;
ptrW name((wchar_t*)mir_calloc(sizeof(wchar_t) * (length + 1)));
mir_wstrncpy(name, fileName, length);
- moduleName = mir_utf8encodeW(name);
+ m_szModuleName = mir_utf8encodeW(name);
}
CMLuaScript::CMLuaScript(const CMLuaScript &script)
@@ -23,13 +23,13 @@ CMLuaScript::CMLuaScript(const CMLuaScript &script) {
mir_wstrcpy(filePath, script.filePath);
fileName = mir_wstrdup(script.fileName);
- moduleName = mir_strdup(script.moduleName);
+ m_szModuleName = mir_strdup(script.m_szModuleName);
}
CMLuaScript::~CMLuaScript()
{
Unload();
- mir_free(moduleName);
+ mir_free((char*)m_szModuleName);
}
const wchar_t* CMLuaScript::GetFilePath() const
@@ -82,11 +82,11 @@ bool CMLuaScript::Load() return true;
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
- lua_getfield(L, -1, moduleName);
+ lua_getfield(L, -1, m_szModuleName);
if (!lua_toboolean(L, -1)) {
lua_pop(L, 1);
lua_pushvalue(L, -2);
- lua_setfield(L, -2, moduleName);
+ lua_setfield(L, -2, m_szModuleName);
lua_pop(L, 1);
}
else
@@ -126,7 +126,7 @@ void CMLuaScript::Unload() luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
lua_pushnil(L);
- lua_setfield(L, -2, moduleName);
+ lua_setfield(L, -2, m_szModuleName);
lua_pop(L, 1);
}
diff --git a/plugins/MirLua/src/mlua_script.h b/plugins/MirLua/src/script.h index 70c044741e..8ff5b40600 100644 --- a/plugins/MirLua/src/mlua_script.h +++ b/plugins/MirLua/src/script.h @@ -1,5 +1,4 @@ -#ifndef _LUA_SCRIPT_H_
-#define _LUA_SCRIPT_H_
+#pragma once
class CMLuaScript : public CMLuaEnvironment
{
@@ -15,8 +14,7 @@ private: Status status;
int unloadRef;
- char *moduleName;
- wchar_t *fileName;
+ const wchar_t *fileName;
wchar_t filePath[MAX_PATH];
void Unload();
@@ -38,5 +36,3 @@ public: bool Load();
bool Reload();
};
-
-#endif //_LUA_SCRIPT_H_
diff --git a/plugins/MirLua/src/mlua_script_loader.cpp b/plugins/MirLua/src/script_loader.cpp index 95701b4559..0092cca372 100644 --- a/plugins/MirLua/src/mlua_script_loader.cpp +++ b/plugins/MirLua/src/script_loader.cpp @@ -29,7 +29,7 @@ void CMLuaScriptLoader::LoadScript(const wchar_t *scriptDir, const wchar_t *file PathToRelativeW(fullPath, path);
CMLuaScript *script = new CMLuaScript(L, path);
- g_mLua->Scripts.insert(script);
+ g_plugin.Scripts.insert(script);
if (!script->IsEnabled()) {
Log(L"%s:PASS", path);
diff --git a/plugins/MirLua/src/mlua_script_loader.h b/plugins/MirLua/src/script_loader.h index 74489a0500..21236df491 100644 --- a/plugins/MirLua/src/mlua_script_loader.h +++ b/plugins/MirLua/src/script_loader.h @@ -1,17 +1,17 @@ -#pragma once
-
-class CMLuaScriptLoader
-{
-private:
- lua_State *L;
-
- CMLuaScriptLoader(lua_State *L);
-
- void SetPaths();
-
- void LoadScript(const wchar_t *scriptDir, const wchar_t *file);
- void LoadScripts();
-
-public:
- static void Load(lua_State *L);
-};
+#pragma once + +class CMLuaScriptLoader +{ +private: + lua_State *L; + + CMLuaScriptLoader(lua_State *L); + + void SetPaths(); + + void LoadScript(const wchar_t *scriptDir, const wchar_t *file); + void LoadScripts(); + +public: + static void Load(lua_State *L); +}; diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index f97ecd09de..e8a358ac24 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -10,8 +10,10 @@ #include <m_core.h>
#include <m_utils.h>
#include <m_langpack.h>
+#include <m_database.h>
#include <m_options.h>
#include <m_netlib.h>
+#include <m_genmenu.h>
#include <m_http.h>
#include <m_clist.h>
#include <m_hotkeys.h>
@@ -35,24 +37,18 @@ class CMLuaScript;
-#include "mlua.h"
-#include "mlua_environment.h"
-#include "mlua_script.h"
-#include "mlua_function_loader.h"
-#include "mlua_module_loader.h"
-#include "mlua_script_loader.h"
-#include "mlua_options.h"
-#include "mlua_metatable.h"
+#include "mplugin.h"
+#include "modules.h"
+#include "environment.h"
+#include "script.h"
+#include "function_loader.h"
+#include "module_loader.h"
+#include "script_loader.h"
+#include "options.h"
+#include "metatable.h"
#define MODULENAME "MirLua"
-struct CMPlugin : public PLUGIN<CMPlugin>
-{
- CMPlugin();
-};
-
-extern CMLua *g_mLua;
-
extern int hMLuaLangpack;
extern HANDLE g_hCLibsFolder;
@@ -65,43 +61,6 @@ extern HANDLE g_hScriptsFolder; /* modules */
-#define MLUA_CORE "m_core"
-LUAMOD_API int (luaopen_m_core)(lua_State *L);
-
-#define MLUA_CHAT "m_chat"
-LUAMOD_API int (luaopen_m_chat)(lua_State *L);
-
-#define MLUA_CLIST "m_clist"
-LUAMOD_API int (luaopen_m_clist)(lua_State *L);
-
-#include "m_database.h"
-
-#define MLUA_ICOLIB "m_icolib"
-LUAMOD_API int (luaopen_m_icolib)(lua_State *L);
-
-#include "m_json.h"
-
-#include "m_genmenu.h"
-
-#define MLUA_HTTP "m_http"
-LUAMOD_API int (luaopen_m_http)(lua_State *L);
-
-#define MLUA_HOTKEYS "m_hotkeys"
-LUAMOD_API int (luaopen_m_hotkeys)(lua_State *L);
-
-#define MLUA_MESSAGE "m_message"
-LUAMOD_API int (luaopen_m_message)(lua_State *L);
-
-#define MLUA_OPTIONS "m_options"
-LUAMOD_API int (luaopen_m_options)(lua_State *L);
-
-#include "m_protocols.h"
-
-#define MLUA_SOUNDS "m_sounds"
-LUAMOD_API int (luaopen_m_sounds)(lua_State *L);
-
-#define MLUA_SRMM "m_srmm"
-LUAMOD_API int (luaopen_m_srmm)(lua_State *L);
/* utils */
diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/utils.cpp index 5ecdbc677d..f8f15f185f 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/utils.cpp @@ -1,99 +1,99 @@ -#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(nullptr, 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, MODULENAME, "PopupOnObsolete", 0))
- ShowNotification(MODULENAME, text, MB_OK | MB_ICONWARNING, NULL);
-}
-
-void ReportError(lua_State *L)
-{
- const char *message = lua_tostring(L, -1);
- Log(message);
- if (db_get_b(NULL, MODULENAME, "PopupOnError", 0))
- ShowNotification(MODULENAME, message, MB_OK | MB_ICONERROR);
-}
-
-int luaM_atpanic(lua_State *L)
-{
- ReportError(L);
- 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(L);
- return res;
-}
-
-int luaM_getenv(lua_State *L)
-{
- lua_Debug ar;
- if (lua_getstack(L, 1, &ar) == 0 ||
- lua_getinfo(L, "f", &ar) == 0 ||
- lua_iscfunction(L, -1))
- {
- lua_pop(L, 1);
- return 0;
- }
-
- const char *env = lua_getupvalue(L, -1, 1);
- if (!env || strcmp(env, "_ENV") != 0)
- {
- lua_pop(L, 1);
- return 0;
- }
-
- 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;
+#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(nullptr, 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, MODULENAME, "PopupOnObsolete", 0)) + ShowNotification(MODULENAME, text, MB_OK | MB_ICONWARNING, NULL); +} + +void ReportError(lua_State *L) +{ + const char *message = lua_tostring(L, -1); + Log(message); + if (db_get_b(NULL, MODULENAME, "PopupOnError", 0)) + ShowNotification(MODULENAME, message, MB_OK | MB_ICONERROR); +} + +int luaM_atpanic(lua_State *L) +{ + ReportError(L); + 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(L); + return res; +} + +int luaM_getenv(lua_State *L) +{ + lua_Debug ar; + if (lua_getstack(L, 1, &ar) == 0 || + lua_getinfo(L, "f", &ar) == 0 || + lua_iscfunction(L, -1)) + { + lua_pop(L, 1); + return 0; + } + + const char *env = lua_getupvalue(L, -1, 1); + if (!env || strcmp(env, "_ENV") != 0) + { + lua_pop(L, 1); + return 0; + } + + 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; }
\ No newline at end of file |