diff --git a/hooks.c b/hooks.c index bbc36a0..c873000 100644 --- a/hooks.c +++ b/hooks.c @@ -84,6 +84,8 @@ struct luadata *hooks_init(struct udata *ud, char *script) ld->L = luaL_newstate(); luaL_openlibs(ld->L); + MQTTconn = ud->mosq; + /* * Set up a global with some values. */ @@ -107,8 +109,10 @@ struct luadata *hooks_init(struct udata *ud, char *script) lua_pushcfunction(ld->L, otr_getdb); lua_setfield(ld->L, -2, "getdb"); +#ifdef WITH_MQTT lua_pushcfunction(ld->L, otr_publish); lua_setfield(ld->L, -2, "publish"); +#endif lua_setglobal(ld->L, "otr"); @@ -138,12 +142,6 @@ struct luadata *hooks_init(struct udata *ud, char *script) return (ld); } -#ifdef WITH_MQTT -void hooks_setmosq(struct mosquitto *mosq) -{ - MQTTconn = mosq; -} -#endif void hooks_exit(struct luadata *ld, char *reason) { @@ -485,9 +483,14 @@ int otr_publish(lua_State *lua) qos = lua_tonumber(lua, 3); retain = lua_tonumber(lua, 4); - rc = mosquitto_publish(MQTTconn, NULL, topic, + if (MQTTconn == NULL) { + olog(LOG_WARNING, "otr_publish(%s, %s, %d, %d): NULL MQTT connection\n", + topic, payload, qos, retain); + } else { + rc = mosquitto_publish(MQTTconn, NULL, topic, strlen(payload), payload, qos, retain); - olog(LOG_DEBUG, "LUA_PUBLISH (%s, %s, %d, %d) == %d\n", topic, payload, qos, retain, rc); + olog(LOG_DEBUG, "otr_publish(%s, %s, %d, %d) == %d\n", topic, payload, qos, retain, rc); + } } return (rc); } diff --git a/hooks.h b/hooks.h index f760676..285334a 100644 --- a/hooks.h +++ b/hooks.h @@ -3,9 +3,6 @@ #ifdef WITH_LUA # include -# ifdef WITH_MQTT -# include -# endif struct luadata { @@ -19,11 +16,6 @@ void hooks_hook(struct udata *ud, char *topic, JsonNode *obj); int hooks_norec(struct udata *ud, char *user, char *device, char *payload); JsonNode *hooks_http(struct udata *ud, char *user, char *device, char *payload); -#ifdef WITH_MQTT -# include -void hooks_setmosq(struct mosquitto *); -#endif - #endif /* WITH_LUA */ #endif