lm_message.c
author Myhailo Danylenko <isbear@ukrpost.net>
Sat, 21 Feb 2009 12:43:51 +0200
changeset 7 5db1448eb857
parent 6 90073cbb535d
child 8 951f92c66821
permissions -rw-r--r--
Message have node methods


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

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

/// lm.message
/// Module, representing individual message.
/// Message have a type and optionally a sub type.

/// message type
/// Message type (root tag type).
/// G:
const string2enum_t llm_message_type[] = {
	{ "message",         LM_MESSAGE_TYPE_MESSAGE         },
	{ "presence",        LM_MESSAGE_TYPE_PRESENCE        },
	{ "iq",              LM_MESSAGE_TYPE_IQ              },
	{ "stream",          LM_MESSAGE_TYPE_STREAM          },
	{ "stream error",    LM_MESSAGE_TYPE_STREAM_ERROR    },
	{ "stream features", LM_MESSAGE_TYPE_STREAM_FEATURES },
	{ "auth",            LM_MESSAGE_TYPE_AUTH            },
	{ "challenge",       LM_MESSAGE_TYPE_CHALLENGE       },
	{ "response",        LM_MESSAGE_TYPE_RESPONSE        },
	{ "success",         LM_MESSAGE_TYPE_SUCCESS         },
	{ "failure",         LM_MESSAGE_TYPE_FAILURE         },
	{ "proceed",         LM_MESSAGE_TYPE_PROCEED         },
	{ "starttls",        LM_MESSAGE_TYPE_STARTTLS        },
	{ "unknown",         LM_MESSAGE_TYPE_UNKNOWN         },
	{ "stream:stream",   LM_MESSAGE_TYPE_STREAM          },
	{ "stream:error",    LM_MESSAGE_TYPE_STREAM_ERROR    },
	{ "stream:features", LM_MESSAGE_TYPE_STREAM_FEATURES },
	{ "stream_error",    LM_MESSAGE_TYPE_STREAM_ERROR    },
	{ "stream_features", LM_MESSAGE_TYPE_STREAM_FEATURES },
	{ NULL,              0                               },
};

/// message sub type
/// Message subtype, not all combinations of type and subtype are possible.
/// G:
const string2enum_t llm_message_sub_type[] = {
	{ "not set",       LM_MESSAGE_SUB_TYPE_NOT_SET      },
	{ "available",     LM_MESSAGE_SUB_TYPE_AVAILABLE    },
	{ "normal",        LM_MESSAGE_SUB_TYPE_NORMAL       },
	{ "chat",          LM_MESSAGE_SUB_TYPE_CHAT         },
	{ "groupchat",     LM_MESSAGE_SUB_TYPE_GROUPCHAT    },
	{ "headline",      LM_MESSAGE_SUB_TYPE_HEADLINE     },
	{ "unavailable",   LM_MESSAGE_SUB_TYPE_UNAVAILABLE  },
	{ "probe",         LM_MESSAGE_SUB_TYPE_PROBE        },
	{ "subscribe",     LM_MESSAGE_SUB_TYPE_SUBSCRIBE    },
	{ "unsubscribe",   LM_MESSAGE_SUB_TYPE_UNSUBSCRIBE  },
	{ "subscribed",    LM_MESSAGE_SUB_TYPE_SUBSCRIBED   },
	{ "unsubscribed",  LM_MESSAGE_SUB_TYPE_UNSUBSCRIBED },
	{ "get",           LM_MESSAGE_SUB_TYPE_GET          },
	{ "set",           LM_MESSAGE_SUB_TYPE_SET          },
	{ "result",        LM_MESSAGE_SUB_TYPE_RESULT       },
	{ "error",         LM_MESSAGE_SUB_TYPE_ERROR        },
	{ "not_set",       LM_MESSAGE_SUB_TYPE_NOT_SET      },
	{ NULL,            0                                },
};

/// lm.message.new
/// Creates new message object.
/// A: string (to), message type, message sub type (optional)
/// R: lm message object
static int llm_message_new (lua_State *L)
{
	const char *to   = luaL_checkstring (L, 1);
	int         type = luaL_checkenum (L, 2, llm_message_type);
	LmMessage *message;
	if (lua_gettop (L) > 2)
		message = lm_message_new_with_sub_type (to, type,
						luaL_checkenum (L, 3, llm_message_sub_type));
	else
		message = lm_message_new (to, type);
	llm_message_bless (L, message);
	lm_message_unref (message);
	D ("Message %X created", (int) message);
	return 1;
}

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

/// message:node
/// Returns root node object of message.
/// R: lm message node object
static int llm_message_node (lua_State *L)
{
	llm_message_t *object = luaL_checklm_message (L, 1);
	LmMessageNode *node = lm_message_get_node (object->message);
	llm_message_node_bless (L, node);
	// XXX lm_message_node_unref (node);
	return 1;
}

/// message:type
/// Returns two strings: message type and message sub type.
/// R: message type, message sub type
static int llm_message_kind (lua_State *L)
{
	llm_message_t *object = luaL_checklm_message (L, 1);
	luaL_pushenum (L, lm_message_get_type (object->message), llm_message_type);
	luaL_pushenum (L, lm_message_get_sub_type (object->message), llm_message_sub_type);
	return 2;
}

/// message:pointer
/// Returns pointer to underlying C loudmouth structure.
/// R: lightuserdata
static int llm_message_pointer (lua_State *L)
{
	llm_message_t *object = luaL_checklm_message (L, 1);
	lua_pushlightuserdata (L, object->message);
	return 1;
}

static int llm_message_gc (lua_State *L)
{
	llm_message_t *message = luaL_checklm_message (L, 1);
	D ("Message %X gc called", (int) message);
	lm_message_unref (message->message);
	return 0;
}

static const luaL_Reg llm_message_reg_f[] = {
	{ "new",   llm_message_new       },
	{ "bless", llm_message_bless_lua },
	{ NULL,    NULL                  },
};

static const luaL_Reg llm_message_reg_m[] = {
	{ "node",       llm_message_node            },
	{ "type",       llm_message_kind            },
	// These methods are common for message and message node
	{ "next",       llm_message_node_next       },
	{ "prev",       llm_message_node_prev       },
	{ "children",   llm_message_node_children   },
	{ "parent",     llm_message_node_parent     },
	{ "value",      llm_message_node_value      },
	{ "child",      llm_message_node_child      },
	{ "find_child", llm_message_node_find_child },
	{ "attribute",  llm_message_node_attribute  },
	{ "raw",        llm_message_node_raw        },
	{ "xml",        llm_message_node_xml        },
	// End common methods
	{ "pointer",    llm_message_pointer         },
	{ "__gc",       llm_message_gc              },
	{ NULL,         NULL                        },
};

int luaopen_lm_message (lua_State *L)
{
	luaL_newmetatable (L, "loudmouth.message");
	lua_pushstring (L, "__index");
	lua_pushvalue (L, -2);
	lua_settable (L, -3);
	luaL_register (L, NULL, llm_message_reg_m);
	lua_pop (L, 1);
	luaL_register (L, "lm.message", llm_message_reg_f);
	return 1;
}