From d1ccfb148b238505e0d603094116b5384cd8d37c Mon Sep 17 00:00:00 2001
From: James Booth <boothj5@gmail.com>
Date: Sun, 15 Sep 2013 22:17:32 +0100
Subject: [PATCH] Check for Lua hooks before calling

---
 src/plugins/lua_plugins.c | 202 ++++++++++++++++++++++----------------
 1 file changed, 118 insertions(+), 84 deletions(-)

diff --git a/src/plugins/lua_plugins.c b/src/plugins/lua_plugins.c
index cb9d3f30..51a04a3c 100644
--- a/src/plugins/lua_plugins.c
+++ b/src/plugins/lua_plugins.c
@@ -105,11 +105,13 @@ lua_init_hook(ProfPlugin *plugin, const char * const version, const char * const
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_init");
     lua_gettable(L, -2);
-    lua_pushstring(L, version);
-    lua_pushstring(L, status);
-    int res2 = lua_pcall(L, 2, 0, 0);
-    lua_check_error(res2);
-    lua_pop(L, 1);
+    if (!lua_isnil(L, -1)) {
+        lua_pushstring(L, version);
+        lua_pushstring(L, status);
+        int res2 = lua_pcall(L, 2, 0, 0);
+        lua_check_error(res2);
+        lua_pop(L, 1);
+    }
 }
 
 void
@@ -119,9 +121,11 @@ lua_on_start_hook(ProfPlugin *plugin)
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_start");
     lua_gettable(L, -2);
-    int res2 = lua_pcall(L, 0, 0, 0);
-    lua_check_error(res2);
-    lua_pop(L, 1);
+    if (!lua_isnil(L, -1)) {
+        int res2 = lua_pcall(L, 0, 0, 0);
+        lua_check_error(res2);
+        lua_pop(L, 1);
+    }
 }
 
 void
@@ -132,11 +136,13 @@ lua_on_connect_hook(ProfPlugin *plugin, const char * const account_name,
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_connect");
     lua_gettable(L, -2);
-    lua_pushstring(L, account_name);
-    lua_pushstring(L, fulljid);
-    int res2 = lua_pcall(L, 2, 0, 0);
-    lua_check_error(res2);
-    lua_pop(L, 1);
+    if (!lua_isnil(L, -1)) {
+        lua_pushstring(L, account_name);
+        lua_pushstring(L, fulljid);
+        int res2 = lua_pcall(L, 2, 0, 0);
+        lua_check_error(res2);
+        lua_pop(L, 1);
+    }
 }
 
 void
@@ -147,11 +153,13 @@ lua_on_disconnect_hook(ProfPlugin *plugin, const char * const account_name,
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_disconnect");
     lua_gettable(L, -2);
-    lua_pushstring(L, account_name);
-    lua_pushstring(L, fulljid);
-    int res2 = lua_pcall(L, 2, 0, 0);
-    lua_check_error(res2);
-    lua_pop(L, 1);
+    if (!lua_isnil(L, -1)) {
+        lua_pushstring(L, account_name);
+        lua_pushstring(L, fulljid);
+        int res2 = lua_pcall(L, 2, 0, 0);
+        lua_check_error(res2);
+        lua_pop(L, 1);
+    }
 }
 
 char *
@@ -162,19 +170,23 @@ lua_on_message_received_hook(ProfPlugin *plugin, const char * const jid,
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_message_received");
     lua_gettable(L, -2);
-    lua_pushstring(L, jid);
-    lua_pushstring(L, message);
-    int res2 = lua_pcall(L, 2, 1, 0);
-    lua_check_error(res2);
+    if (!lua_isnil(L, -1)) {
+        lua_pushstring(L, jid);
+        lua_pushstring(L, message);
+        int res2 = lua_pcall(L, 2, 1, 0);
+        lua_check_error(res2);
 
-    char *result = NULL;
-    if (lua_isstring(L, -1)) {
-        result = strdup(lua_tostring(L, -1));
+        char *result = NULL;
+        if (lua_isstring(L, -1)) {
+            result = strdup(lua_tostring(L, -1));
+        }
+
+        lua_pop(L, 2);
+
+        return result;
     }
 
-    lua_pop(L, 2);
-
-    return result;
+    return NULL;
 }
 
 char *
@@ -185,20 +197,24 @@ lua_on_private_message_received_hook(ProfPlugin *plugin, const char * const room
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_private_message_received");
     lua_gettable(L, -2);
-    lua_pushstring(L, room);
-    lua_pushstring(L, nick);
-    lua_pushstring(L, message);
-    int res2 = lua_pcall(L, 3, 1, 0);
-    lua_check_error(res2);
+    if (!lua_isnil(L, -1)) {
+        lua_pushstring(L, room);
+        lua_pushstring(L, nick);
+        lua_pushstring(L, message);
+        int res2 = lua_pcall(L, 3, 1, 0);
+        lua_check_error(res2);
 
-    char *result = NULL;
-    if (lua_isstring(L, -1)) {
-        result = strdup(lua_tostring(L, -1));
+        char *result = NULL;
+        if (lua_isstring(L, -1)) {
+            result = strdup(lua_tostring(L, -1));
+        }
+
+        lua_pop(L, 2);
+
+        return result;
     }
 
-    lua_pop(L, 2);
-
-    return result;
+    return  NULL;
 }
 
 char *
@@ -209,20 +225,24 @@ lua_on_room_message_received_hook(ProfPlugin *plugin, const char * const room,
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_room_message_received");
     lua_gettable(L, -2);
-    lua_pushstring(L, room);
-    lua_pushstring(L, nick);
-    lua_pushstring(L, message);
-    int res2 = lua_pcall(L, 3, 1, 0);
-    lua_check_error(res2);
+    if (!lua_isnil(L, -1)) {
+        lua_pushstring(L, room);
+        lua_pushstring(L, nick);
+        lua_pushstring(L, message);
+        int res2 = lua_pcall(L, 3, 1, 0);
+        lua_check_error(res2);
 
-    char *result = NULL;
-    if (lua_isstring(L, -1)) {
-        result = strdup(lua_tostring(L, -1));
+        char *result = NULL;
+        if (lua_isstring(L, -1)) {
+            result = strdup(lua_tostring(L, -1));
+        }
+
+        lua_pop(L, 2);
+
+        return result;
     }
 
-    lua_pop(L, 2);
-
-    return result;
+    return NULL;
 }
 
 char *
@@ -233,19 +253,23 @@ lua_on_message_send_hook(ProfPlugin *plugin, const char * const jid,
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_message_send");
     lua_gettable(L, -2);
-    lua_pushstring(L, jid);
-    lua_pushstring(L, message);
-    int res2 = lua_pcall(L, 2, 1, 0);
-    lua_check_error(res2);
+    if (!lua_isnil(L, -1)) {
+        lua_pushstring(L, jid);
+        lua_pushstring(L, message);
+        int res2 = lua_pcall(L, 2, 1, 0);
+        lua_check_error(res2);
 
-    char *result = NULL;
-    if (lua_isstring(L, -1)) {
-        result = strdup(lua_tostring(L, -1));
+        char *result = NULL;
+        if (lua_isstring(L, -1)) {
+            result = strdup(lua_tostring(L, -1));
+        }
+
+        lua_pop(L, 2);
+
+        return result;
     }
 
-    lua_pop(L, 2);
-
-    return result;
+    return NULL;
 }
 
 char *
@@ -256,20 +280,24 @@ lua_on_private_message_send_hook(ProfPlugin *plugin, const char * const room,
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_private_message_send");
     lua_gettable(L, -2);
-    lua_pushstring(L, room);
-    lua_pushstring(L, nick);
-    lua_pushstring(L, message);
-    int res2 = lua_pcall(L, 3, 1, 0);
-    lua_check_error(res2);
+    if (!lua_isnil(L, -1)) {
+        lua_pushstring(L, room);
+        lua_pushstring(L, nick);
+        lua_pushstring(L, message);
+        int res2 = lua_pcall(L, 3, 1, 0);
+        lua_check_error(res2);
 
-    char *result = NULL;
-    if (lua_isstring(L, -1)) {
-        result = strdup(lua_tostring(L, -1));
+        char *result = NULL;
+        if (lua_isstring(L, -1)) {
+            result = strdup(lua_tostring(L, -1));
+        }
+
+        lua_pop(L, 2);
+
+        return result;
     }
 
-    lua_pop(L, 2);
-
-    return result;
+    return NULL;
 }
 
 char *
@@ -280,19 +308,23 @@ lua_on_room_message_send_hook(ProfPlugin *plugin, const char * const room,
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_room_message_send");
     lua_gettable(L, -2);
-    lua_pushstring(L, room);
-    lua_pushstring(L, message);
-    int res2 = lua_pcall(L, 2, 1, 0);
-    lua_check_error(res2);
+    if (!lua_isnil(L, -1)) {
+        lua_pushstring(L, room);
+        lua_pushstring(L, message);
+        int res2 = lua_pcall(L, 2, 1, 0);
+        lua_check_error(res2);
 
-    char *result = NULL;
-    if (lua_isstring(L, -1)) {
-        result = strdup(lua_tostring(L, -1));
+        char *result = NULL;
+        if (lua_isstring(L, -1)) {
+            result = strdup(lua_tostring(L, -1));
+        }
+
+        lua_pop(L, 2);
+
+        return result;
     }
 
-    lua_pop(L, 2);
-
-    return result;
+    return NULL;
 }
 
 void
@@ -302,9 +334,11 @@ lua_on_shutdown_hook(ProfPlugin *plugin)
     lua_rawgeti(L, LUA_REGISTRYINDEX, *p_ref);
     lua_pushstring(L, "prof_on_shutdown");
     lua_gettable(L, -2);
-    int res2 = lua_pcall(L, 0, 0, 0);
-    lua_check_error(res2);
-    lua_pop(L, 1);
+    if (!lua_isnil(L, -1)) {
+        int res2 = lua_pcall(L, 0, 0, 0);
+        lua_check_error(res2);
+        lua_pop(L, 1);
+    }
 }
 
 void