/* 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> // GDestroyNotify, GMainContext
#include <loudmouth/loudmouth.h>
#include "config.h"
#include "util.h"
#include "lm_types.h"
#include "lm_message.h"
#include "lm_message_handler.h"
/// lm.connection
/// Central module, representing connection to the server.
/// You should create a new connection object, then open it (establish
/// connection), then authenticate to the server.
/// connection state
/// Stirng, representing current connection state.
/// G:
const string2enum_t state_lm_connection[] = {
{ "closed", LM_CONNECTION_STATE_CLOSED },
{ "opening", LM_CONNECTION_STATE_OPENING },
{ "open", LM_CONNECTION_STATE_OPEN },
{ "authenticating", LM_CONNECTION_STATE_AUTHENTICATING },
{ "authenticated", LM_CONNECTION_STATE_AUTHENTICATED },
{ NULL, 0 }, // XXX
};
/// handler priority
/// String, according to which handler will be placed into one
/// of three handler groups.
/// G:
const string2enum_t priority_lm_handler[] = {
{ "last", LM_HANDLER_PRIORITY_LAST },
{ "normal", LM_HANDLER_PRIORITY_NORMAL },
{ "first", LM_HANDLER_PRIORITY_FIRST },
{ NULL, 0 }, // XXX
};
/// disconnect reason
/// String, indicating the reason of disconnection occured.
/// G:
static const string2enum_t llm_disconnect_reason[] = {
{ "ok", LM_DISCONNECT_REASON_OK },
{ "ping time out", LM_DISCONNECT_REASON_PING_TIME_OUT },
{ "hup", LM_DISCONNECT_REASON_HUP },
{ "error", LM_DISCONNECT_REASON_ERROR },
{ "resource conflict", LM_DISCONNECT_REASON_RESOURCE_CONFLICT },
{ "invalid xml", LM_DISCONNECT_REASON_INVALID_XML },
{ "unknown", LM_DISCONNECT_REASON_UNKNOWN },
{ "ping_time_out", LM_DISCONNECT_REASON_PING_TIME_OUT },
{ "resource_conflict", LM_DISCONNECT_REASON_RESOURCE_CONFLICT },
{ "invalid_xml", LM_DISCONNECT_REASON_INVALID_XML },
{ NULL, 0 }, // XXX
};
/// lm.connection.new
/// Creates a new connection (closed).
/// A: string (server name), lightuserdata (C glib main context object, optional)
/// R: lm connection object
static int new_lm_connection (lua_State *L)
{
const char *server = luaL_checkstring (L, 1);
LmConnection *connection;
if (lua_gettop (L) < 2)
connection = lm_connection_new (server);
else {
luaL_argcheck (L, lua_islightuserdata (L, 2), 2, "glib main context lightuserdata expected");
connection = lm_connection_new_with_context (server, (GMainContext *) lua_touserdata (L, 2));
}
bless_lm_connection (L, connection);
lm_connection_unref (connection);
D ("Connection %p created", connection);
return 1;
}
/// lm.connection.bless
/// Blesses given pointer to lm connection object.
/// Note: it adds a reference to connection.
/// A: lightuserdata (C lm connection object)
/// R: lm connection object
static int bless_lua_lm_connection (lua_State *L)
{
luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "loudmouth connection lightuserdata expected");
bless_lm_connection (L, (LmConnection *) lua_touserdata (L, 1));
return 1;
}
/// connection callback function
/// User function, that will be called on connection establishment operation end,
/// eg. successful/unsuccessful opening or authentication.
/// A: lm connection object, boolean (success)
static void callback_lm_connection (LmConnection *connection, int success, llm_callback_t *cb)
{
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
bless_lm_connection (cb->L, connection);
// XXX lm_connection_unref (connection);
lua_pushboolean (cb->L, success);
if (lua_pcall (cb->L, 2, 0, 0)) {
W ("Connection callback error: %s", lua_tostring (cb->L, -1));
lua_pop (cb->L, 1);
}
}
/// connection:open
/// Opens connection to the server and then calls callback function.
/// A: connection callback function
/// R: lm connection object or nil, string (error message)
static int open_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
llm_callback_t *cb;
GError *err = NULL;
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
cb = luaL_malloc (L, sizeof (llm_callback_t));
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
cb->L = L;
if (lm_connection_open (object->connection, (LmResultFunction) callback_lm_connection,
cb, (GDestroyNotify) llm_callback_destroy, &err))
return 1;
else {
lua_pushnil (L);
lua_pushstring (L, err->message);
I ("Connection opening failed: %s", err -> message);
g_error_free (err);
return 2;
}
}
/// connection:authenticate
/// Tries to authenticate against opened connection, then calls callback function.
/// A: string (username), string (password), string (resource), connection callback function
/// R: lm connection object or nil, string (error message)
static int authenticate_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
const char *username = luaL_checkstring (L, 2);
const char *password = luaL_checkstring (L, 3);
const char *resource = luaL_checkstring (L, 4);
GError *err = NULL;
llm_callback_t *cb;
luaL_argcheck (L, lua_isfunction (L, 5), 5, "function expected");
cb = luaL_malloc (L, sizeof (llm_callback_t));
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
cb->L = L;
if (lm_connection_authenticate (object->connection, username, password, resource,
(LmResultFunction) callback_lm_connection, cb,
(GDestroyNotify) llm_callback_destroy, &err)) {
lua_pop (L, 3);
return 1;
} else {
lua_pushnil (L);
lua_pushstring (L, err->message);
I ("Connection authentication failed: %s", err->message);
g_error_free (err);
return 2;
}
}
/// connection:port
/// Gets or sets server port to connect.
/// A: integer (optional)
/// R: integer (when called with no args) or lm connection object
static int port_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
if (lua_gettop (L) > 1) { // Set
lm_connection_set_port (object->connection, luaL_checkinteger (L, 2));
lua_pop (L, 1);
} else { // Get
lua_pushnumber (L, lm_connection_get_port (object->connection));
}
return 1;
}
/// connection:server
/// Gets or sets server to connect to.
/// A: string (optional, server name)
/// R: string (when called with no args) or lm connection object
static int server_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
if (lua_gettop (L) > 1) { // Set
lm_connection_set_server (object->connection, luaL_checkstring (L, 2));
lua_pop (L, 1);
} else { // Get
lua_pushstring (L, lm_connection_get_server (object->connection));
}
return 1;
}
/// connection:jid
/// Gets or sets jid for connection.
/// A: string (optional)
/// R: string (when called with no args) or lm connection object
static int jid_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
if (lua_gettop (L) > 1) { // Set
lm_connection_set_jid (object->connection, luaL_checkstring (L, 2));
lua_pop (L, 1);
} else { // Get
lua_pushstring (L, lm_connection_get_jid (object->connection));
}
return 1;
}
/// connection:keep_alive_rate
/// Gets or sets keep alive packets rate for connection.
/// Note, that on some platforms there is no get function even in
/// loudmouth versions, that should have it according to documentation.
/// integer (optional, seconds)
/// integer (when called with no args) or lm connection object or nil, string (error message, when get function is not available in loudmouth)
static int keep_alive_rate_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
if (lua_gettop (L) > 1) { // Set
lm_connection_set_keep_alive_rate (object->connection, luaL_checkinteger (L, 2));
lua_pop (L, 1);
return 1;
} else { // Get
#ifdef HAVE_LM_CONNECTION_GET_KEEP_ALIVE_RATE
lua_pushnumber (L, lm_connection_get_keep_alive_rate (object->connection));
return 1;
#else
W ("Sorry, your loudmouth have no get_keep_alive_rate ()");
lua_pushnil (L);
lua_pushliteral (L, "Sorry, your loudmouth have no get_keep_alive_rate ()");
return 2;
#endif
}
}
/// connection:proxy
/// Gets or sets proxy server for connection.
/// A: lm proxy object (optional)
/// R: lm proxy object or nil (when called with no args) or lm connection object
static int proxy_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
if (lua_gettop (L) > 1) { // Set
llm_proxy_t *proxy = luaL_checklm_proxy (L, 2);
lm_connection_set_proxy (object->connection, proxy->proxy);
lua_pop (L, 1);
} else { // Get
LmProxy *proxy = lm_connection_get_proxy (object->connection);
lua_pop (L, 1);
if (proxy) {
bless_lm_proxy (L, proxy);
// XXX lm_proxy_unref (proxy);
} else
lua_pushnil (L);
}
return 1;
}
/// connection:ssl
/// Gets or sets ssl object for connection.
/// A: lm ssl object (optional)
/// R: lm ssl object or nil (when called with no args) or lm connection object
static int ssl_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
if (lua_gettop (L) > 1) { // Set
llm_ssl_t *ssl = luaL_checklm_ssl (L, 2);
lm_connection_set_ssl (object->connection, ssl->ssl);
lua_pop (L, 1);
} else { // Get
LmSSL *ssl = lm_connection_get_ssl (object->connection);
lua_pop (L, 1);
if (ssl) {
bless_lm_ssl (L, ssl);
// XXX lm_ssl_unref (ssl);
} else
lua_pushnil (L);
}
return 1;
}
/// connection:close
/// Close connection.
/// R: lm connection object or nil, string (error message)
static int close_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
GError *err = NULL;
if (lm_connection_close (object->connection, &err))
return 1;
else {
I ("Connection close failed: %s", err->message);
lua_pushnil (L);
lua_pushstring (L, err->message);
g_error_free (err);
return 2;
}
}
/// connection:status
/// Returns string, describing connection state.
/// R: connection state
static int status_lm_connection (lua_State *L)
{
llm_connection_t *connection = luaL_checklm_connection (L, 1);
luaL_pushenum (L, lm_connection_get_state (connection->connection), state_lm_connection);
return 1;
}
/// connection:send
/// Sends message. If specified, handler function will be called upon receiving of response to this message.
/// Handler function can be either a message handler object or just a message handler function.
/// Message can be raw xml string. However, you cannot use it with handler function.
/// In short:
/// * connection, errmsg = connection:send ( "raw xml" )
/// * connection, errmsg = connection:send ( message )
/// * connection, errmsg = connection:send ( message, function ( connection, message ) end )
/// * connection, errmsg = connection:send ( message, handler )
/// If connection is nil, errmsg contains error message.
/// A: lm message object/string, message handler callback function/lm message handler object (optional)
/// R: lm connection object or nil, string (error message)
static int send_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
int status;
GError *err = NULL;
if (lua_gettop (L) < 3) { // Send
if (lua_type (L, 2) == LUA_TSTRING)
status = lm_connection_send_raw (object->connection, lua_tostring (L, 2), &err);
else {
llm_message_t *message = luaL_checklm_message (L, 2);
status = lm_connection_send (object->connection, message->message, &err);
}
} else if (lua_isfunction (L, 3)) { // Send w/reply, func
llm_message_t *message = luaL_checklm_message (L, 2);
llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t));
LmMessageHandler *handler;
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
cb->L = L;
handler = lm_message_handler_new (
(LmHandleMessageFunction)callback_lm_handler,
cb, (GDestroyNotify)llm_callback_destroy);
status = lm_connection_send_with_reply (object->connection,
message->message, handler, &err);
lm_message_handler_unref (handler);
} else { // Send w/reply, handler
llm_message_t *message = luaL_checklm_message (L, 2);
llm_handler_t *handler = luaL_checklm_handler (L, 3);
status = lm_connection_send_with_reply (object->connection, message->message,
handler->handler, &err);
lua_pop (L, 1);
};
lua_pop (L, 1);
if (status)
return 1;
else {
I ("Message sending failed: %s", err->message);
lua_pushnil (L);
lua_pushstring (L, err->message);
g_error_free (err);
return 2;
}
}
/// connection:handler
/// Registers or unregisters handler function for a given type of messages.
/// To unregister handler, omit the priority argument.
/// To unregister reply handler, omit message type argument too.
/// Handler function can be specified as plain function or message handler object.
/// A: message handler callback function or lm message handler object, message type (optional), handler priority (optional)
/// R: lm connection object
static int handler_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
int top = lua_gettop (L);
if (top > 2) { // Have message type
int type = luaL_checkenum (L, 3, type_lm_message);
if (lua_gettop (L) > 3) { // Register
int priority = luaL_checkenum (L, 4, priority_lm_handler);
if (lua_isfunction (L, 2)) { // Function
LmMessageHandler *handler;
llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t));
lua_pushvalue (L, 2);
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
cb->L = L;
handler = lm_message_handler_new (
(LmHandleMessageFunction)callback_lm_handler,
cb, (GDestroyNotify)llm_callback_destroy);
lm_connection_register_message_handler (object->connection,
handler, type, priority);
lm_message_handler_unref (handler);
} else { // Object
llm_handler_t *handler = luaL_checklm_handler (L, 2);
lm_connection_register_message_handler (object->connection,
handler->handler,
type, priority);
}
lua_pop (L, 1);
} else { // Unregister message handler
llm_handler_t *handler = luaL_checklm_handler (L, 2);
lm_connection_unregister_message_handler (object->connection,
handler->handler,
type);
}
lua_pop (L, 1);
} else { // Unregister reply handler
#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
llm_handler_t *handler = luaL_checklm_handler (L, 2);
lm_connection_unregister_reply_handler (object -> connection,
handler -> handler);
#else
W ("Sorry, your loudmouth have no unregister_reply_handler ()");
lua_pushnil (L);
lua_pushliteral (L, "Sorry, your loudmouth have no unregister_reply_handler ()");
return 2;
#endif
}
lua_pop (L, 1);
return 1;
}
/// disconnect callback function
/// Function, that will be called when disconnection occurs.
/// A: lm connection object, disconnect reason
void llm_disconnect_callback (LmConnection *connection, LmDisconnectReason reason, llm_callback_t *cb)
{
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
bless_lm_connection (cb->L, connection);
// XXX lm_connection_unref (connection);
luaL_pushenum (cb->L, reason, llm_disconnect_reason);
if (lua_pcall (cb->L, 2, 0, 0)) {
W ("Disconnect callback error: %s", lua_tostring (cb->L, -1));
lua_pop (cb->L, 1);
}
}
/// connection:ondisconnect
/// Sets callback, that will be called on connection disconnect.
/// A: disconnect callback function
/// R: lm connection object
static int ondisconnect_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
llm_callback_t *cb;
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
cb = luaL_malloc (L, sizeof (llm_callback_t));
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
cb->L = L;
lm_connection_set_disconnect_function (object->connection,
(LmDisconnectFunction)llm_disconnect_callback,
cb, (GDestroyNotify)llm_callback_destroy);
return 1;
}
/// connection:open_wait
/// Synchronous open call, that will block until connection will be opened.
/// R: lm connection object or nil, string (error message)
static int open_wait_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
GError *err = NULL;
if (lm_connection_open_and_block (object->connection, &err))
return 1;
else {
I ("Synchronous connection opening failed: %s", err->message);
lua_pushnil (L);
lua_pushstring (L, err->message);
g_error_free (err);
return 2;
}
}
/// connection:authenticate_wait
/// Synchronous authentication call, that will wait until the end of authentication.
/// A: string (username), string (password), string (resource)
/// R: lm connection object or nil, string (error message)
static int authenticate_wait_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
const char *username = luaL_checkstring (L, 2);
const char *password = luaL_checkstring (L, 3);
const char *resource = luaL_checkstring (L, 4);
GError *err = NULL;
if (lm_connection_authenticate_and_block (object->connection, username,
password, resource, &err))
return 1;
else {
I ("Synchronous authentication failed: %s", err->message);
lua_pushnil (L);
lua_pushstring (L, err->message);
g_error_free (err);
return 2;
}
}
/// connection:send_wait
/// Synchronous call, that will send message and wait for reply to it.
/// A: lm message object
/// R: lm message object or nil, string (error message)
static int send_wait_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
llm_message_t *message = luaL_checklm_message (L, 2);
GError *err = NULL;
LmMessage *new = lm_connection_send_with_reply_and_block (object->connection,
message->message, &err);
if (!new) {
I ("Synchronous message sending failed: %s", err->message);
lua_pushnil (L);
lua_pushstring (L, err->message);
g_error_free (err);
return 2;
} else {
bless_lm_message (L, new);
lm_message_unref (new); // XXX
return 1;
}
}
/// connection:pointer
/// Returns pointer to underlying C loudmouth structure.
/// R: lightuserdata
static int pointer_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
lua_pushlightuserdata (L, object->connection);
lua_remove (L, -2);
return 1;
}
static int gc_lm_connection (lua_State *L)
{
llm_connection_t *object = luaL_checklm_connection (L, 1);
D ("Connection %p gc called", object);
lm_connection_unref (object->connection);
return 0;
}
static const luaL_Reg reg_f_lm_connection[] = {
{ "new", new_lm_connection },
{ "bless", bless_lua_lm_connection },
{ NULL, NULL },
};
static const luaL_Reg reg_m_lm_connection[] = {
{ "open", open_lm_connection },
{ "close", close_lm_connection },
{ "authenticate", authenticate_lm_connection },
{ "status", status_lm_connection },
{ "port", port_lm_connection },
{ "server", server_lm_connection },
{ "jid", jid_lm_connection },
{ "keep_alive_rate", keep_alive_rate_lm_connection },
{ "state", status_lm_connection },
{ "proxy", proxy_lm_connection },
{ "ssl", ssl_lm_connection },
{ "send", send_lm_connection },
{ "handler", handler_lm_connection },
{ "ondisconnect", ondisconnect_lm_connection },
{ "open_wait", open_wait_lm_connection },
{ "authenticate_wait", authenticate_wait_lm_connection },
{ "send_wait", send_wait_lm_connection },
{ "pointer", pointer_lm_connection },
{ "__gc", gc_lm_connection },
{ NULL, NULL },
};
int luaopen_lm_connection (lua_State *L)
{
luaL_newmetatable (L, "loudmouth.connection");
lua_pushvalue (L, -1);
lua_setfield (L, -2, "__index");
luaL_setfuncs (L, reg_m_lm_connection, 0);
lua_pop (L, 1);
lua_newtable (L); // XXX we can specify here exact amount of fields
luaL_setfuncs (L, reg_f_lm_connection, 0);
return 1;
}