lm_message.c
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 16 Mar 2016 01:53:56 +0200
changeset 66 a40beb82130c
parent 54 6bef2082e5f9
permissions -rw-r--r--
node: Use new loudmouth feature - public attribute list (v0.9.7)

/* 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 <loudmouth/loudmouth.h>
#include <string.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 have a set common methods with message node,
/// these are name, next, prev, parent, value, child, children,
/// find_child, attribute, raw, xml and path. They just save
/// you typing :node() each time and save memory for
/// one node object.

/// message type
/// Message type (root tag type).
/// G:
const string2enum_t type_lm_message[] = {
	{ "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,              LM_MESSAGE_TYPE_MESSAGE         },
};

/// message sub type
/// Message subtype, not all combinations of type and subtype are possible.
/// G:
const string2enum_t sub_type_lm_message[] = {
	{ "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,            LM_MESSAGE_SUB_TYPE_NOT_SET      },
};

/// lm.message.new
/// Creates new message object.
/// Note: you can specify nil as to argument to send message to server.
/// A: string (to), message type, message sub type (optional)
/// R: lm message object
static int new_lm_message (lua_State *L)
{
	const char *to   = NULL;
	int         type = luaL_checkenum (L, 2, type_lm_message);
	LmMessage *message;

	if (lua_type (L, 1) != LUA_TNIL)
		to = luaL_checkstring (L, 1);
	if (lua_gettop (L) > 2)
		message = lm_message_new_with_sub_type (to, type,
						luaL_checkenum (L, 3, sub_type_lm_message));
	else
		message = lm_message_new (to, type);
	bless_lm_message (L, message);
	lm_message_unref (message);
	D ("Message %p created", message);
	return 1;
}

/// message table
/// Table describes xml structure of the message, the only exception is mtype key of root table.
/// mtype is a string of form "<message type>-<message sub type>", eg "iq-set".
/// Best way to learn how this table is organized, is just to look at next example:
/// [ lm.message.create { mtype = 'iq-result', to = 'foo@bar.xyz',
/// 	command = { xmlns = 'http://jabber.org/protocol/commands', node = 'http://jabber.org/protocol/rc#set-status', status = 'executing', sessionid = 'set-status:aaa3',
/// 		x = { xmlns = 'jabber:x:data', type = 'form',
/// 			title = { "Change Status" },
/// 			instructions = { "Choose the status and status message" },
/// 			field = {{ type = 'hidden', var = 'FORM_TYPE',
/// 				value = { "http://jabber.org/protocol/rc" },
/// 			},{ type = 'list-single', label = 'Status', var = 'status',
/// 				required = { },
/// 				value = { "online" },
/// 				option = {{ label = 'Chat',
/// 					value = { "chat" },
/// 				},{ label = 'Online',
/// 					value = { "online" },
/// 				},{ label = 'Away',
/// 					value = { "away" },
/// 				},{ label = 'Extended Away',
/// 					value = { "xa" },
/// 				},{ label = 'Do Not Disturb',
/// 					value = { "dnd" },
/// 				},{ label = 'Invisible',
/// 					value = { "invisible" },
/// 				},{ label = 'Offline',
/// 					value = { "offline" },
/// 				}},
/// 			},{ type = 'text-single', label = 'Priority', var = 'status-priority',
/// 				value = { "5" },
/// 			},{ type = 'text-multi', label = 'Message', var = 'status-message' }},
/// 		},
/// 	},
/// }
/// ]
static void fill_lm_node (lua_State *L, LmMessageNode *node, int index)
{
	int top = lua_gettop (L); // 0
	for (lua_pushnil (L); lua_next (L, index) != 0; lua_pop (L, lua_gettop (L) - top - 1)) // 2 value
		if (lua_type (L, top + 2) == LUA_TTABLE) {
			const char *name = lua_tostring (L, top + 1);
			lua_pushinteger (L, 1); // 3 1
			lua_gettable (L, top + 2); // 3 value[1]
			if (lua_type (L, top + 3) == LUA_TTABLE) {
				int i = 1;
				do {
					fill_lm_node (L, lm_message_node_add_child (node, name, NULL), top + 3);
					lua_pop (L, 1); // 2 value
					lua_pushinteger (L, ++i); // 3 i
					lua_gettable (L, top + 2); // 3 value[i]
				} while (lua_type (L, top + 3) == LUA_TTABLE);
			} else
				fill_lm_node (L, lm_message_node_add_child (node, name, NULL), top + 2);
		} else if (lua_type (L, top + 1) == LUA_TNUMBER && lua_tointeger (L, top + 1) == 1)
			lm_message_node_set_value (node, lua_tostring (L, top + 2));
		else
			lm_message_node_set_attribute (node, lua_tostring (L, top + 1), lua_tostring (L, top + 2));
}

/// lm.message.create
/// Creates new message object and fills it from message table.
/// Note, that table fields are not checked for their types, so, on wrong input results may be undefined.
/// A: message table
/// R: lm message object
static int create_lm_message (lua_State *L)
{
	const char *mtype;
	const char *st      = NULL;
	const char *to      = NULL;
	LmMessage  *message;
	luaL_checktype (L, 1, LUA_TTABLE);

	lua_getfield (L, 1, "mtype");
	mtype = lua_tostring (L, -1);
	if (mtype)
		st = strchr (mtype, '-');

	lua_getfield (L, 1, "to");
	if (lua_type (L, -1) == LUA_TSTRING)
		to = lua_tostring (L, -1);

	if (st) {
		LmMessageType mt;
		lua_pushlstring (L, mtype, st - mtype);
		mt = luaL_checkenum (L, -1, type_lm_message);
		lua_pop (L, 1);
		message = lm_message_new_with_sub_type (to, mt, string2enum (st + 1, sub_type_lm_message));
	} else
		message = lm_message_new (to, luaL_checkenum (L, -2, type_lm_message));

	lua_pop (L, 2);
	lua_pushnil (L);
	lua_setfield (L, 1, "mtype");
	lua_pushnil (L);
	lua_setfield (L, 1, "to");

	fill_lm_node (L, lm_message_get_node (message), 1);

	bless_lm_message (L, message);
	lm_message_unref (message);
	D ("Message %p created", 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 bless_lua_lm_message (lua_State *L)
{
	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "lm message lightuserdata expected");
	bless_lm_message (L, lua_touserdata (L, 1));
	return 1;
}

/// message:node
/// Returns root node object of message.
/// R: lm message node object
static int node_lm_message (lua_State *L)
{
	llm_message_t *object = luaL_checklm_message (L, 1);
	LmMessageNode *node = lm_message_get_node (object->message);
	bless_lm_node (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 kind_lm_message (lua_State *L)
{
	llm_message_t *object = luaL_checklm_message (L, 1);
	luaL_pushenum (L, lm_message_get_type (object->message), type_lm_message);
	luaL_pushenum (L, lm_message_get_sub_type (object->message), sub_type_lm_message);
	return 2;
}

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

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

static const luaL_Reg reg_f_lm_message[] = {
	{ "new",    new_lm_message       },
	{ "create", create_lm_message    },
	{ "bless",  bless_lua_lm_message },
	{ NULL,     NULL                 },
};

static const luaL_Reg reg_m_lm_message[] = {
	{ "node",       node_lm_message    },
	{ "type",       kind_lm_message    },
	// These methods are common for message and message node
	{ "name",       name_lm_node       },
	{ "next",       next_lm_node       },
	{ "prev",       prev_lm_node       },
	{ "parent",     parent_lm_node     },
	{ "value",      value_lm_node      },
	{ "child",      child_lm_node      },
	{ "children",   children_lm_node   },
	{ "find_child", find_child_lm_node },
	{ "attribute",  attribute_lm_node  },
	{ "raw",        raw_lm_node        },
	{ "xml",        xml_lm_node        },
	{ "path",       path_lm_node       },
	// End common methods
	{ "pointer",    pointer_lm_message },
	{ "__gc",       gc_lm_message      },
	{ NULL,         NULL               },
};

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