lm_message_handler.c
author Myhailo Danylenko <isbear@ukrpost.net>
Sat, 05 Mar 2016 18:04:48 +0200
changeset 65 72ffcf688664
parent 54 6bef2082e5f9
permissions -rw-r--r--
cmake: Utilise find_package for lua


/* Copyright 2009 Myhailo Danylenko

This file is part of lua-lm.

lua-lm is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <lua.h>
#include <lauxlib.h>
#include <glib.h>
#include <loudmouth/loudmouth.h>

#include "config.h"
#include "lm_types.h"
#include "util.h"

/// lm.message_handler
/// Module, representing message handling functions.

/// message handler callback function
/// Callback function, used on incoming messages.
/// Should return true if message has been fully handled and no more
/// handlers should be called.
/// A: lm connection object, lm message object
/// R: boolean
LmHandlerResult callback_lm_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, llm_callback_t *cb)
{
	int ret;
	lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
	bless_lm_connection (cb->L, connection);
	// XXX lm_connection_unref (connection);
	bless_lm_message (cb->L, message);
	// XXX lm_message_unref (message);
	if (lua_pcall (cb->L, 2, 1, 0)) {
		W ("Message handler callback error: %s", lua_tostring (cb->L, -1));
		lua_pop (cb->L, 1);
		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
	}
	ret = lua_toboolean (cb->L, -1);
	lua_pop (cb->L, 1);
	if (ret)
		return LM_HANDLER_RESULT_REMOVE_MESSAGE;
	else
		return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
}

/// lm.message_handler.new
/// Creates new message handler object.
/// A: message handler callback function
/// R: lm message handler object
static int new_lm_handler (lua_State *L)
{
	llm_callback_t *cb;
	LmMessageHandler *handler;
	luaL_argcheck (L, lua_isfunction (L, 1), 1, "function expected");

	cb = luaL_malloc (L, sizeof (llm_callback_t));
	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
	cb->L         = L;

	handler = lm_message_handler_new ((LmHandleMessageFunction)callback_lm_handler,
					cb, (GDestroyNotify)llm_callback_destroy);
	bless_lm_handler (L, handler);
	lm_message_handler_unref (handler); // XXX
	D ("Message handler %p created", handler);
	return 1;
}

/// lm.message_handler.bless
/// Blesses given pointer to lm message handler object.
/// A: lightuserdata (C lm message handler object)
/// R: lm message handler object
static int bless_lua_lm_handler (lua_State *L)
{
	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "loudmouth message handler pointer expected");
	bless_lm_handler (L, lua_touserdata (L, 1));
	return 1;
}

// XXX: merge with next?
/// message_handler:invalidate
/// Invalidates handler.
/// R: lm message handler object
static int invalidate_lm_handler (lua_State *L)
{
	llm_handler_t *object = luaL_checklm_handler (L, 1);
	lm_message_handler_invalidate (object->handler);
	return 1;
}

/// message_handler:valid
/// Returns true if message handler is still valid.
/// R: boolean
static int valid_lm_handler (lua_State *L)
{
	llm_handler_t *object = luaL_checklm_handler (L, 1);
	lua_pushboolean (L, lm_message_handler_is_valid (object->handler));
	return 1;
}

/// message_handler:pointer
/// Returns pointer to underlying C loudmouth structure.
/// R: lightuserdata
static int pointer_lm_handler (lua_State *L)
{
	llm_handler_t *object = luaL_checklm_handler (L, 1);
	lua_pushlightuserdata (L, object->handler);
	return 1;
}

static int gc_lm_handler (lua_State *L)
{
	llm_handler_t *object = luaL_checklm_handler (L, 1);
	D ("Message handler %p gc called", object);
	lm_message_handler_unref (object->handler);
	return 0;
}

static const luaL_Reg reg_f_lm_handler[] = {
	{ "new",   new_lm_handler       },
	{ "bless", bless_lua_lm_handler },
	{ NULL,    NULL                 },
};

static const luaL_Reg reg_m_lm_handler[] = {
	{ "invalidate", invalidate_lm_handler },
	{ "valid",      valid_lm_handler      },
	{ "pointer",    pointer_lm_handler    },
	{ "__gc",       gc_lm_handler         },
	{ NULL,         NULL                  },
};

int luaopen_lm_message_handler (lua_State *L)
{
	luaL_newmetatable (L, "loudmouth.message_handler");
	lua_pushvalue (L, -1);
	lua_setfield (L, -2, "__index");
	luaL_setfuncs (L, reg_m_lm_handler, 0);
	lua_pop (L, 1);
	lua_newtable (L); // XXX we can specify here exact amount of fields
	luaL_setfuncs (L, reg_f_lm_handler, 0);
	return 1;
}