From b7992849fdd3fe2d2e8344f240d74f755d711b5a Mon Sep 17 00:00:00 2001
From: Syping <schiedelrafael@keppe.org>
Date: Fri, 6 Aug 2021 05:33:01 +0200
Subject: [PATCH] xmppbot 0.6

---
 CMakeLists.txt             |  2 +-
 src/xmppbot/main.cpp       |  2 +-
 src/xmppbot/xmppbotlua.cpp | 29 ++++++++++++++++++++++++++---
 src/xmppbot/xmppbotlua.h   |  3 +++
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2c36b8c..d3b864f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,6 +1,6 @@
 cmake_minimum_required(VERSION 3.7)
 
-project(xmppbot VERSION 0.5 LANGUAGES CXX)
+project(xmppbot VERSION 0.6 LANGUAGES CXX)
 
 set(CMAKE_INCLUDE_CURRENT_DIR ON)
 
diff --git a/src/xmppbot/main.cpp b/src/xmppbot/main.cpp
index cbfa02b..8a89015 100644
--- a/src/xmppbot/main.cpp
+++ b/src/xmppbot/main.cpp
@@ -39,7 +39,7 @@ int main(int argc, char *argv[])
 {
     QCoreApplication app(argc, argv);
     app.setApplicationName(QLatin1String("xmppbot"));
-    app.setApplicationVersion(QLatin1String("0.5"));
+    app.setApplicationVersion(QLatin1String("0.6"));
 
     QCommandLineParser commandLineParser;
     commandLineParser.addPositionalArgument(QLatin1String("config"), QCoreApplication::translate("xmppbot", "Configuration file."));
diff --git a/src/xmppbot/xmppbotlua.cpp b/src/xmppbot/xmppbotlua.cpp
index 88bb99f..4f83681 100644
--- a/src/xmppbot/xmppbotlua.cpp
+++ b/src/xmppbot/xmppbotlua.cpp
@@ -61,6 +61,9 @@ XmppBotLua::XmppBotLua(QObject *parent) : QObject(parent)
 
     // Process
     pushFunction("executeProcess", executeProcess);
+
+    // Table
+    pushFunction("tableContains", tableContains);
 }
 
 XmppBotLua::~XmppBotLua()
@@ -199,7 +202,7 @@ void XmppBotLua::pushVariant(lua_State *L_p, const QVariant &variant)
             currentId++;
         }
     }
-    else if ((QMetaType::Type)variant.type() == QMetaType::QVariantList) {
+    else if (static_cast<QMetaType::Type>(variant.type()) == QMetaType::QVariantList) {
         const QVariantList variantList = variant.toList();
         lua_createtable(L_p, 0, variantList.count());
         int currentId = 1;
@@ -210,7 +213,7 @@ void XmppBotLua::pushVariant(lua_State *L_p, const QVariant &variant)
             currentId++;
         }
     }
-    else if ((QMetaType::Type)variant.type() == QMetaType::QVariantMap) {
+    else if (static_cast<QMetaType::Type>(variant.type()) == QMetaType::QVariantMap) {
         const QVariantMap variantMap = variant.toMap();
         lua_createtable(L_p, 0, variantMap.count());
         for (auto it = variantMap.constBegin(); it != variantMap.constEnd(); it++) {
@@ -219,7 +222,7 @@ void XmppBotLua::pushVariant(lua_State *L_p, const QVariant &variant)
             lua_settable(L_p, -3);
         }
     }
-    else if ((QMetaType::Type)variant.type() == QMetaType::Void || (QMetaType::Type)variant.type() == QMetaType::VoidStar) {
+    else if (static_cast<QMetaType::Type>(variant.type()) == QMetaType::Void || static_cast<QMetaType::Type>(variant.type()) == QMetaType::VoidStar) {
         lua_pushlightuserdata(L_p, variant.value<void*>());
     }
     else {
@@ -489,3 +492,23 @@ int XmppBotLua::executeProcess(lua_State *L_p)
     pushVariant(L_p, -2);
     return 2;
 }
+
+int XmppBotLua::tableContains(lua_State *L_p)
+{
+    if (getArgumentCount(L_p) >= 2) {
+        const QVariant vtable = getVariant(L_p, 1);
+        if (static_cast<QMetaType::Type>(vtable.type()) == QMetaType::QVariantMap) {
+            const QVariantMap table = vtable.toMap();
+            const QVariant toMatch = getVariant(L_p, 2);
+            for (const QVariant &value : table) {
+                if (value == toMatch) {
+                    pushVariant(L_p, true);
+                    return 1;
+                }
+            }
+            pushVariant(L_p, false);
+            return 1;
+        }
+    }
+    return 0;
+}
diff --git a/src/xmppbot/xmppbotlua.h b/src/xmppbot/xmppbotlua.h
index 18bbb6d..20eedc3 100644
--- a/src/xmppbot/xmppbotlua.h
+++ b/src/xmppbot/xmppbotlua.h
@@ -83,6 +83,9 @@ private:
     // Process
     static int executeProcess(lua_State *L_p);
 
+    // Table
+    static int tableContains(lua_State *L_p);
+
     // Lua
     lua_State *L;
 };