summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/utils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/MirLua/src/utils.cpp')
-rw-r--r--plugins/MirLua/src/utils.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/plugins/MirLua/src/utils.cpp b/plugins/MirLua/src/utils.cpp
new file mode 100644
index 0000000000..f8f15f185f
--- /dev/null
+++ b/plugins/MirLua/src/utils.cpp
@@ -0,0 +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;
+} \ No newline at end of file