From 961bf5e6b1ba406951a7624962973bacfe140cd5 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Thu, 31 Dec 2015 18:38:47 +0000 Subject: MirLua: added CONTACTINFO metatable git-svn-id: http://svn.miranda-ng.org/main/trunk@15980 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_chat.cpp | 2 +- plugins/MirLua/src/m_database.cpp | 192 +++++++++++++++++++++++++++++++------ plugins/MirLua/src/m_protocols.cpp | 2 +- plugins/MirLua/src/mlua_utils.cpp | 7 +- plugins/MirLua/src/stdafx.h | 1 + 5 files changed, 172 insertions(+), 32 deletions(-) diff --git a/plugins/MirLua/src/m_chat.cpp b/plugins/MirLua/src/m_chat.cpp index 78aa88c810..e6938a8c94 100644 --- a/plugins/MirLua/src/m_chat.cpp +++ b/plugins/MirLua/src/m_chat.cpp @@ -134,7 +134,7 @@ static int gce__index(lua_State *L) static const luaL_Reg gceMeta[] = { - { MT_GCEVENT, gce__init }, + { "__init", gce__init }, { "__index", gce__index }, { NULL, NULL } }; diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index 7eaaba3a8b..84169b3cd7 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -23,8 +23,8 @@ static int lua_FindNextContact(lua_State *L) static int lua_ContactIterator(lua_State *L) { - const char *szProto = lua_tostring(L, lua_upvalueindex(1)); - MCONTACT hContact = lua_tointeger(L, lua_upvalueindex(2)); + MCONTACT hContact = lua_tointeger(L, lua_upvalueindex(1)); + const char *szProto = lua_tostring(L, lua_upvalueindex(2)); hContact = hContact == NULL ? db_find_first(szProto) @@ -46,7 +46,7 @@ static int lua_Contacts(lua_State *L) { const char *szProto = lua_tostring(L, 1); - lua_pushinteger(L, NULL); + lua_pushinteger(L, 0); lua_pushstring(L, szProto); lua_pushcclosure(L, lua_ContactIterator, 2); @@ -308,7 +308,7 @@ static int lua_SettingIterator(lua_State *L) return 1; } -static int lua_AllSettings(lua_State *L) +static int lua_Settings(lua_State *L) { MCONTACT hContact = lua_tointeger(L, 1); const char* szModule = luaL_checkstring(L, 2); @@ -517,27 +517,27 @@ static int lua_DecodeDBCONTACTWRITESETTING(lua_State *L) lua_pushliteral(L, "Value"); switch (pDBCWS->value.type) { - case DBVT_BYTE: - lua_pushinteger(L, pDBCWS->value.bVal); - break; - case DBVT_WORD: - lua_pushinteger(L, pDBCWS->value.wVal); - break; - case DBVT_DWORD: - lua_pushnumber(L, pDBCWS->value.dVal); - break; - case DBVT_ASCIIZ: - lua_pushstring(L, ptrA(mir_utf8encode(pDBCWS->value.pszVal))); - break; - case DBVT_UTF8: - lua_pushstring(L, pDBCWS->value.pszVal); - break; - case DBVT_WCHAR: - lua_pushstring(L, ptrA(mir_utf8encodeW(pDBCWS->value.pwszVal))); - break; - default: - lua_pushvalue(L, 4); - return 1; + case DBVT_BYTE: + lua_pushinteger(L, pDBCWS->value.bVal); + break; + case DBVT_WORD: + lua_pushinteger(L, pDBCWS->value.wVal); + break; + case DBVT_DWORD: + lua_pushnumber(L, pDBCWS->value.dVal); + break; + case DBVT_ASCIIZ: + lua_pushstring(L, ptrA(mir_utf8encode(pDBCWS->value.pszVal))); + break; + case DBVT_UTF8: + lua_pushstring(L, pDBCWS->value.pszVal); + break; + case DBVT_WCHAR: + lua_pushstring(L, ptrA(mir_utf8encodeW(pDBCWS->value.pwszVal))); + break; + default: + lua_pushvalue(L, 4); + return 1; } lua_settable(L, -3); @@ -566,10 +566,11 @@ static luaL_Reg databaseApi[] = { "WriteContactSetting", lua_WriteSetting }, { "WriteSetting", lua_WriteSetting }, - + { "GetContactSetting", lua_GetSetting }, { "GetSetting", lua_GetSetting }, - { "AllSettings", lua_AllSettings }, + { "AllSettings", lua_Settings }, + { "Settings", lua_Settings }, { "EnumSettings", lua_EnumSettings }, { "DeleteContactSetting", lua_DeleteSetting }, @@ -583,6 +584,8 @@ static luaL_Reg databaseApi[] = { NULL, NULL } }; +/***********************************************/ + #define MT_DBCONTACTWRITESETTING "DBCONTACTWRITESETTING" static int dbcw__init(lua_State *L) @@ -645,11 +648,13 @@ static int dbcw__index(lua_State *L) static const luaL_Reg dbcwMeta[] = { - { MT_DBCONTACTWRITESETTING, dbcw__init }, + { "__init", dbcw__init }, { "__index", dbcw__index }, { NULL, NULL } }; +/***********************************************/ + DBEVENTINFO* MT::Load(lua_State *L) { MEVENT hDbEvent = luaL_checkinteger(L, 1); @@ -668,6 +673,133 @@ void MT::Free(DBEVENTINFO **dbei) mir_free(*dbei); } +/***********************************************/ + +#define MT_CONTACTINFO "CONTACTINFO" + +static int ci__init(lua_State *L) +{ + MCONTACT udata = 0; + switch(lua_type(L, 1)) + { + case LUA_TNUMBER: + udata = lua_tointeger(L, 1); + break; + case LUA_TLIGHTUSERDATA: + udata = (MCONTACT)lua_touserdata(L, 1); + break; + default: + const char *msg = lua_pushfstring(L, "hContact expected, got %s", lua_typename(L, lua_type(L, 1))); + luaL_argerror(L, 1, msg); + } + + MCONTACT *hContact = (MCONTACT*)lua_newuserdata(L, sizeof(MCONTACT)); + *hContact = udata; + + luaL_setmetatable(L, MT_CONTACTINFO); + + return 1; +} + +static int ci__index(lua_State *L) +{ + MCONTACT *hContact = (MCONTACT*)luaL_checkudata(L, 1, MT_CONTACTINFO); + + mir_ptr ci((CONTACTINFO*)mir_calloc(sizeof(CONTACTINFO))); + ci->cbSize = sizeof(CONTACTINFO); + ci->hContact = *hContact; + + if (lua_type(L, 2) == LUA_TNUMBER) + ci->dwFlag = lua_tointeger(L, 2); + else if (lua_type(L, 2) == LUA_TSTRING) + { + const char *key = luaL_checkstring(L, 2); + + if (mir_strcmpi(key, "Handle") == 0) + { + lua_pushinteger(L, *hContact); + return 1; + } + + if (mir_strcmpi(key, "FirstName") == 0) + ci->dwFlag = CNF_FIRSTNAME; + else if (mir_strcmpi(key, "LastName") == 0) + ci->dwFlag = CNF_LASTNAME; + else if (mir_strcmpi(key, "Nick") == 0) + ci->dwFlag = CNF_NICK; + else if (mir_strcmpi(key, "FullName") == 0) + ci->dwFlag = CNF_FIRSTLAST; + else if (mir_strcmpi(key, "DisplayName") == 0) + ci->dwFlag = CNF_DISPLAY; + else if (mir_strcmpi(key, "Uid") == 0) + ci->dwFlag = CNF_UNIQUEID; + else if (mir_strcmpi(key, "Email") == 0) + ci->dwFlag = CNF_EMAIL; + else if (mir_strcmpi(key, "City") == 0) + ci->dwFlag = CNF_CITY; + else if (mir_strcmpi(key, "State") == 0) + ci->dwFlag = CNF_STATE; + else if (mir_strcmpi(key, "Country") == 0) + ci->dwFlag = CNF_COUNTRY; + else if (mir_strcmpi(key, "Phone") == 0) + ci->dwFlag = CNF_PHONE; + else if (mir_strcmpi(key, "Homepage") == 0) + ci->dwFlag = CNF_HOMEPAGE; + else if (mir_strcmpi(key, "About") == 0) + ci->dwFlag = CNF_ABOUT; + else if (mir_strcmpi(key, "Age") == 0) + ci->dwFlag = CNF_AGE; + else if (mir_strcmpi(key, "Gender") == 0) + ci->dwFlag = CNF_GENDER; + else + { + lua_pushnil(L); + return 1; + } + } + else + { + lua_pushnil(L); + return 1; + } + + ci->dwFlag |= CNF_TCHAR; + if (CallService(MS_CONTACT_GETCONTACTINFO, 0, (LPARAM)(CONTACTINFO*)ci)) + { + lua_pushnil(L); + return 1; + } + + switch (ci->type) + { + case CNFT_BYTE: + lua_pushinteger(L, ci->bVal); + break; + case CNFT_WORD: + lua_pushinteger(L, ci->wVal); + break; + case CNFT_DWORD: + lua_pushnumber(L, ci->dVal); + break; + case CNFT_ASCIIZ: + lua_pushstring(L, ptrA(mir_utf8encodeT(ci->pszVal))); + break; + default: + lua_pushnil(L); + } + + return 1; +} + +static const luaL_Reg ciMeta[] = +{ + { "__init", ci__init }, + { "__index", ci__index }, + { NULL, NULL } +}; + +/***********************************************/ + LUAMOD_API int luaopen_m_database(lua_State *L) { luaL_newlib(L, databaseApi); @@ -685,5 +817,9 @@ LUAMOD_API int luaopen_m_database(lua_State *L) luaL_setfuncs(L, dbcwMeta, 0); lua_pop(L, 1); + luaL_newmetatable(L, MT_CONTACTINFO); + luaL_setfuncs(L, ciMeta, 0); + lua_pop(L, 1); + return 1; } diff --git a/plugins/MirLua/src/m_protocols.cpp b/plugins/MirLua/src/m_protocols.cpp index ecbfe477b8..d9ab3022ca 100644 --- a/plugins/MirLua/src/m_protocols.cpp +++ b/plugins/MirLua/src/m_protocols.cpp @@ -362,7 +362,7 @@ static int ccs__index(lua_State *L) static luaL_Reg ccsMeta[] = { - { MT_CCSDATA, ccs__init }, + { "__init", ccs__init }, { "__index", ccs__index }, { NULL, NULL } }; diff --git a/plugins/MirLua/src/mlua_utils.cpp b/plugins/MirLua/src/mlua_utils.cpp index f8fbae33b8..99bae923d1 100644 --- a/plugins/MirLua/src/mlua_utils.cpp +++ b/plugins/MirLua/src/mlua_utils.cpp @@ -138,9 +138,12 @@ int luaM_totable(lua_State *L) { const char *tname = luaL_checkstring(L, 2); - //luaL_getmetatable(L, tname); - //lua_getfield(L, -1, "__init"); lua_getglobal(L, tname); + if (lua_type(L, -1) == LUA_TNIL) + { + luaL_getmetatable(L, tname); + lua_getfield(L, -1, "__init"); + } lua_pushvalue(L, 1); luaM_pcall(L, 1, 1); diff --git a/plugins/MirLua/src/stdafx.h b/plugins/MirLua/src/stdafx.h index ad4652ec1e..6cd1dde489 100644 --- a/plugins/MirLua/src/stdafx.h +++ b/plugins/MirLua/src/stdafx.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include -- cgit v1.2.3