lm_connection.c
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 16 Mar 2016 01:53:56 +0200
changeset 66 a40beb82130c
parent 63 c17f3295f52c
permissions -rw-r--r--
node: Use new loudmouth feature - public attribute list (v0.9.7)
23
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     1
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     2
/* Copyright 2009 Myhailo Danylenko
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     3
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     4
This file is part of lua-lm.
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     5
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     6
lua-lm is free software: you can redistribute it and/or modify
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     7
it under the terms of the GNU General Public License as published by
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     8
the Free Software Foundation, either version 2 of the License, or
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
     9
(at your option) any later version.
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    10
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    11
This program is distributed in the hope that it will be useful,
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    14
GNU General Public License for more details.
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    15
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    16
You should have received a copy of the GNU General Public License
13f03e604c8a License notes
Myhailo Danylenko <isbear@ukrpost.net>
parents: 19
diff changeset
    17
along with this program.  If not, see <http://www.gnu.org/licenses/>. */
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
#include <lua.h>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
#include <lauxlib.h>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
#include <glib.h>			// GDestroyNotify, GMainContext
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
#include <loudmouth/loudmouth.h>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
    24
#include "config.h"
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    25
#include "util.h"
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
#include "lm_types.h"
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    27
#include "lm_message.h"
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
#include "lm_message_handler.h"
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
/// lm.connection
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    31
/// Central module, representing connection to the server.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    32
/// You should create a new connection object, then open it (establish
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    33
/// connection), then authenticate to the server.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
/// connection state
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
/// Stirng, representing current connection state.
4
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    37
/// G:
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
    38
const string2enum_t state_lm_connection[] = {
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
	{ "closed",         LM_CONNECTION_STATE_CLOSED         },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
	{ "opening",        LM_CONNECTION_STATE_OPENING        },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
	{ "open",           LM_CONNECTION_STATE_OPEN           },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
	{ "authenticating", LM_CONNECTION_STATE_AUTHENTICATING },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
	{ "authenticated",  LM_CONNECTION_STATE_AUTHENTICATED  },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
	{ NULL,             0                                  }, // XXX
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
};
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
/// handler priority
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
/// String, according to which handler will be placed into one
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
/// of three handler groups.
4
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    50
/// G:
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
    51
const string2enum_t priority_lm_handler[] = {
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
	{ "last",   LM_HANDLER_PRIORITY_LAST   },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
	{ "normal", LM_HANDLER_PRIORITY_NORMAL },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
	{ "first",  LM_HANDLER_PRIORITY_FIRST  },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
	{ NULL,     0                          }, // XXX
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    56
};
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
/// disconnect reason
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
/// String, indicating the reason of disconnection occured.
4
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    60
/// G:
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
    61
static const string2enum_t llm_disconnect_reason[] = {
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
	{ "ok",                LM_DISCONNECT_REASON_OK                },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
	{ "ping time out",     LM_DISCONNECT_REASON_PING_TIME_OUT     },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
	{ "hup",               LM_DISCONNECT_REASON_HUP               },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
	{ "error",             LM_DISCONNECT_REASON_ERROR             },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
	{ "resource conflict", LM_DISCONNECT_REASON_RESOURCE_CONFLICT },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
	{ "invalid xml",       LM_DISCONNECT_REASON_INVALID_XML       },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
	{ "unknown",           LM_DISCONNECT_REASON_UNKNOWN           },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
	{ "ping_time_out",     LM_DISCONNECT_REASON_PING_TIME_OUT     },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
	{ "resource_conflict", LM_DISCONNECT_REASON_RESOURCE_CONFLICT },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
	{ "invalid_xml",       LM_DISCONNECT_REASON_INVALID_XML       },
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
	{ NULL,                0                                      }, // XXX
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
};
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
/// lm.connection.new
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
/// Creates a new connection (closed).
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
/// A: string (server name), lightuserdata (C glib main context object, optional)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
/// R: lm connection object
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
    79
static int new_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
	const char *server = luaL_checkstring (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
	LmConnection *connection;
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
    83
	if (lua_gettop (L) < 2)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    84
		connection = lm_connection_new (server);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
    85
	else {
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    86
		luaL_argcheck (L, lua_islightuserdata (L, 2), 2, "glib main context lightuserdata expected");
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    87
		connection = lm_connection_new_with_context (server, (GMainContext *) lua_touserdata (L, 2));
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    88
	}
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
    89
	bless_lm_connection (L, connection);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    90
	lm_connection_unref (connection);
38
34a2b880615c Fix debugging pointer format character
Myhailo Danylenko <isbear@ukrpost.net>
parents: 31
diff changeset
    91
	D ("Connection %p created", connection);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
/// lm.connection.bless
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
/// Blesses given pointer to lm connection object.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
/// Note: it adds a reference to connection.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
/// A: lightuserdata (C lm connection object)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    99
/// R: lm connection object
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   100
static int bless_lua_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
	luaL_argcheck (L, lua_islightuserdata (L, 1), 1, "loudmouth connection lightuserdata expected");
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   103
	bless_lm_connection (L, (LmConnection *) lua_touserdata (L, 1));
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
/// connection callback function
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
/// User function, that will be called on connection establishment operation end,
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   109
/// eg. successful/unsuccessful opening or authentication.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   110
/// A: lm connection object, boolean (success)
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   111
static void callback_lm_connection (LmConnection *connection, int success, llm_callback_t *cb)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
	lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   114
	bless_lm_connection (cb->L, connection);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
	// XXX lm_connection_unref (connection);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
	lua_pushboolean (cb->L, success);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
	if (lua_pcall (cb->L, 2, 0, 0)) {
9
50f55d494efb Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   118
		W ("Connection callback error: %s", lua_tostring (cb->L, -1));
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   119
		lua_pop (cb->L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   120
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   121
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   122
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
/// connection:open
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
/// Opens connection to the server and then calls callback function.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
/// A: connection callback function
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   126
/// R: lm connection object or nil, string (error message)
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   127
static int open_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
	llm_callback_t *cb;
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   131
	GError *err = NULL;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
	luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   133
	
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   134
	cb = luaL_malloc (L, sizeof (llm_callback_t));
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   135
	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   136
	cb->L         = L;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   137
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   138
	if (lm_connection_open (object->connection, (LmResultFunction) callback_lm_connection,
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   139
				 cb, (GDestroyNotify) llm_callback_destroy, &err))
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   140
		return 1;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   141
	else {
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   142
		lua_pushnil (L);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   143
		lua_pushstring (L, err->message);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   144
		I ("Connection opening failed: %s", err -> message);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   145
		g_error_free (err);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   146
		return 2;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   147
	}
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   148
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   149
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
/// connection:authenticate
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
/// Tries to authenticate against opened connection, then calls callback function.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
/// A: string (username), string (password), string (resource), connection callback function
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   153
/// R: lm connection object or nil, string (error message)
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   154
static int authenticate_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
{
45
1fcec4b012a7 Fix warnings
Myhailo Danylenko <isbear@ukrpost.net>
parents: 38
diff changeset
   156
	llm_connection_t *object   = luaL_checklm_connection (L, 1);
1fcec4b012a7 Fix warnings
Myhailo Danylenko <isbear@ukrpost.net>
parents: 38
diff changeset
   157
	const char       *username = luaL_checkstring (L, 2);
1fcec4b012a7 Fix warnings
Myhailo Danylenko <isbear@ukrpost.net>
parents: 38
diff changeset
   158
	const char       *password = luaL_checkstring (L, 3);
1fcec4b012a7 Fix warnings
Myhailo Danylenko <isbear@ukrpost.net>
parents: 38
diff changeset
   159
	const char       *resource = luaL_checkstring (L, 4);
1fcec4b012a7 Fix warnings
Myhailo Danylenko <isbear@ukrpost.net>
parents: 38
diff changeset
   160
	GError           *err      = NULL;
1fcec4b012a7 Fix warnings
Myhailo Danylenko <isbear@ukrpost.net>
parents: 38
diff changeset
   161
	llm_callback_t   *cb;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   162
	luaL_argcheck (L, lua_isfunction (L, 5), 5, "function expected");
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   163
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
	cb = luaL_malloc (L, sizeof (llm_callback_t));
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   165
	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
	cb->L         = L;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   167
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   168
	if (lm_connection_authenticate (object->connection, username, password, resource,
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   169
						(LmResultFunction) callback_lm_connection, cb,
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   170
						(GDestroyNotify) llm_callback_destroy, &err)) {
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   171
		lua_pop (L, 3);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   172
		return 1;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   173
	} else {
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   174
		lua_pushnil (L);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   175
		lua_pushstring (L, err->message);
9
50f55d494efb Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   176
		I ("Connection authentication failed: %s", err->message);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   177
		g_error_free (err);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   178
		return 2;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   179
	}
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   181
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
/// connection:port
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
/// Gets or sets server port to connect.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
/// A: integer (optional)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   185
/// R: integer (when called with no args) or lm connection object
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   186
static int port_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   187
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   188
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   189
	if (lua_gettop (L) > 1) { // Set
63
c17f3295f52c lua: Add compatibility with lua 5.3
Myhailo Danylenko <isbear@ukrpost.net>
parents: 54
diff changeset
   190
		lm_connection_set_port (object->connection, luaL_checkinteger (L, 2));
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   191
		lua_pop (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   192
	} else { // Get
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
		lua_pushnumber (L, lm_connection_get_port (object->connection));
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   194
	}
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   195
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   196
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   197
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   198
/// connection:server
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   199
/// Gets or sets server to connect to.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   200
/// A: string (optional, server name)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   201
/// R: string (when called with no args) or lm connection object
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   202
static int server_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   203
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   204
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   205
	if (lua_gettop (L) > 1) { // Set
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   206
		lm_connection_set_server (object->connection, luaL_checkstring (L, 2));
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   207
		lua_pop (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   208
	} else { // Get
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   209
		lua_pushstring (L, lm_connection_get_server (object->connection));
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   210
	}
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   211
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   212
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   213
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   214
/// connection:jid
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   215
/// Gets or sets jid for connection.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   216
/// A: string (optional)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   217
/// R: string (when called with no args) or lm connection object
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   218
static int jid_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   219
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   220
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   221
	if (lua_gettop (L) > 1) { // Set
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   222
		lm_connection_set_jid (object->connection, luaL_checkstring (L, 2));
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   223
		lua_pop (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   224
	} else { // Get
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   225
		lua_pushstring (L, lm_connection_get_jid (object->connection));
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   226
	}
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   227
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   228
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   229
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   230
/// connection:keep_alive_rate
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   231
/// Gets or sets keep alive packets rate for connection.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   232
/// Note, that on some platforms there is no get function even in
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   233
/// loudmouth versions, that should have it according to documentation.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   234
/// integer (optional, seconds)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   235
/// integer (when called with no args) or lm connection object or nil, string (error message, when get function is not available in loudmouth)
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   236
static int keep_alive_rate_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   237
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   238
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   239
	if (lua_gettop (L) > 1) { // Set
63
c17f3295f52c lua: Add compatibility with lua 5.3
Myhailo Danylenko <isbear@ukrpost.net>
parents: 54
diff changeset
   240
		lm_connection_set_keep_alive_rate (object->connection, luaL_checkinteger (L, 2));
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   241
		lua_pop (L, 1);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   242
		return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   243
	} else { // Get
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   244
#ifdef HAVE_LM_CONNECTION_GET_KEEP_ALIVE_RATE
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   245
		lua_pushnumber (L, lm_connection_get_keep_alive_rate (object->connection));
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   246
		return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   247
#else
9
50f55d494efb Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   248
		W ("Sorry, your loudmouth have no get_keep_alive_rate ()");
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   249
		lua_pushnil (L);
19
d775d7289fe4 Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents: 16
diff changeset
   250
		lua_pushliteral (L, "Sorry, your loudmouth have no get_keep_alive_rate ()");
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   251
		return 2;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   252
#endif
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   253
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   254
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   255
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   256
/// connection:proxy
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   257
/// Gets or sets proxy server for connection.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   258
/// A: lm proxy object (optional)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   259
/// R: lm proxy object or nil (when called with no args) or lm connection object
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   260
static int proxy_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   261
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   262
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   263
	if (lua_gettop (L) > 1) { // Set
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   264
		llm_proxy_t *proxy = luaL_checklm_proxy (L, 2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   265
		lm_connection_set_proxy (object->connection, proxy->proxy);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   266
		lua_pop (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   267
	} else { // Get
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   268
		LmProxy *proxy = lm_connection_get_proxy (object->connection);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   269
		lua_pop (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   270
		if (proxy) {
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   271
			bless_lm_proxy (L, proxy);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   272
			// XXX lm_proxy_unref (proxy);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   273
		} else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   274
			lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   275
	}
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   276
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   277
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   278
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   279
/// connection:ssl
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   280
/// Gets or sets ssl object for connection.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   281
/// A: lm ssl object (optional)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   282
/// R: lm ssl object or nil (when called with no args) or lm connection object
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   283
static int ssl_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   284
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   285
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   286
	if (lua_gettop (L) > 1) { // Set
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   287
		llm_ssl_t *ssl = luaL_checklm_ssl (L, 2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   288
		lm_connection_set_ssl (object->connection, ssl->ssl);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   289
		lua_pop (L, 1);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   290
	} else { // Get
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   291
		LmSSL *ssl = lm_connection_get_ssl (object->connection);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   292
		lua_pop (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   293
		if (ssl) {
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   294
			bless_lm_ssl (L, ssl);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   295
			// XXX lm_ssl_unref (ssl);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   296
		} else
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   297
			lua_pushnil (L);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   298
	}
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   299
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   300
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   301
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   302
/// connection:close
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   303
/// Close connection.
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   304
/// R: lm connection object or nil, string (error message)
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   305
static int close_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   306
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   307
	llm_connection_t *object = luaL_checklm_connection (L, 1);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   308
	GError *err = NULL;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   309
	if (lm_connection_close (object->connection, &err))
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   310
		return 1;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   311
	else {
9
50f55d494efb Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   312
		I ("Connection close failed: %s", err->message);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   313
		lua_pushnil (L);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   314
		lua_pushstring (L, err->message);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   315
		g_error_free (err);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   316
		return 2;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   317
	}
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   318
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   319
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   320
/// connection:status
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   321
/// Returns string, describing connection state.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   322
/// R: connection state
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   323
static int status_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   324
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   325
	llm_connection_t *connection = luaL_checklm_connection (L, 1);
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   326
	luaL_pushenum (L, lm_connection_get_state (connection->connection), state_lm_connection);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   327
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   328
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   329
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   330
/// connection:send
10
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   331
/// Sends message. If specified, handler function will be called upon receiving of response to this message.
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   332
/// Handler function can be either a message handler object or just a message handler function.
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   333
/// Message can be raw xml string. However, you cannot use it with handler function.
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   334
/// In short:
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   335
/// * connection, errmsg = connection:send ( "raw xml" )
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   336
/// * connection, errmsg = connection:send ( message )
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   337
/// * connection, errmsg = connection:send ( message, function ( connection, message ) end )
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   338
/// * connection, errmsg = connection:send ( message, handler )
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   339
/// If connection is nil, errmsg contains error message.
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   340
/// A: lm message object/string, message handler callback function/lm message handler object (optional)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   341
/// R: lm connection object or nil, string (error message)
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   342
static int send_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   343
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   344
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   345
	int status;
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   346
	GError *err = NULL;
10
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   347
	if (lua_gettop (L) < 3) { // Send
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   348
		if (lua_type (L, 2) == LUA_TSTRING)
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   349
			status = lm_connection_send_raw (object->connection, lua_tostring (L, 2), &err);
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   350
		else {
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   351
			llm_message_t *message = luaL_checklm_message (L, 2);
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   352
			status = lm_connection_send (object->connection, message->message, &err);
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   353
		}
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   354
	} else if (lua_isfunction (L, 3)) { // Send w/reply, func
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   355
		llm_message_t    *message = luaL_checklm_message (L, 2);
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   356
		llm_callback_t   *cb      = luaL_malloc (L, sizeof (llm_callback_t));
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   357
		LmMessageHandler *handler;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   358
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   359
		cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
10
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   360
		cb->L         = L;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   361
		handler = lm_message_handler_new (
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   362
					(LmHandleMessageFunction)callback_lm_handler,
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   363
					cb, (GDestroyNotify)llm_callback_destroy);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   364
		status = lm_connection_send_with_reply (object->connection,
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   365
							message->message, handler, &err);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   366
		lm_message_handler_unref (handler);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   367
	} else { // Send w/reply, handler
10
aed141accdd9 Merge send_raw and send methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 9
diff changeset
   368
		llm_message_t *message = luaL_checklm_message (L, 2);
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   369
		llm_handler_t *handler = luaL_checklm_handler (L, 3);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   370
		status = lm_connection_send_with_reply (object->connection, message->message,
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   371
							handler->handler, &err);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   372
		lua_pop (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   373
	};
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   374
	lua_pop (L, 1);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   375
	if (status)
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   376
		return 1;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   377
	else {
9
50f55d494efb Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   378
		I ("Message sending failed: %s", err->message);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   379
		lua_pushnil (L);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   380
		lua_pushstring (L, err->message);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   381
		g_error_free (err);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   382
		return 2;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   383
	}
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   384
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   385
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   386
/// connection:handler
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   387
/// Registers or unregisters handler function for a given type of messages.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   388
/// To unregister handler, omit the priority argument.
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   389
/// To unregister reply handler, omit message type argument too.
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   390
/// Handler function can be specified as plain function or message handler object.
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   391
/// A: message handler callback function or lm message handler object, message type (optional), handler priority (optional)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   392
/// R: lm connection object
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   393
static int handler_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   394
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   395
	llm_connection_t *object = luaL_checklm_connection (L, 1);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   396
	int top  = lua_gettop (L);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   397
	if (top > 2) { // Have message type
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   398
		int type = luaL_checkenum (L, 3, type_lm_message);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   399
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   400
		if (lua_gettop (L) > 3) { // Register
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   401
			int priority = luaL_checkenum (L, 4, priority_lm_handler);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   402
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   403
			if (lua_isfunction (L, 2)) { // Function
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   404
				LmMessageHandler *handler;
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   405
				llm_callback_t *cb = luaL_malloc (L, sizeof (llm_callback_t));
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   406
				lua_pushvalue (L, 2);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   407
				cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   408
				cb->L         = L;
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   409
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   410
				handler = lm_message_handler_new (
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   411
						(LmHandleMessageFunction)callback_lm_handler,
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   412
						cb, (GDestroyNotify)llm_callback_destroy);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   413
				lm_connection_register_message_handler (object->connection,
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   414
									handler, type, priority);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   415
				lm_message_handler_unref (handler);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   416
			} else { // Object
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   417
				llm_handler_t *handler = luaL_checklm_handler (L, 2);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   418
				lm_connection_register_message_handler (object->connection,
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   419
									handler->handler,
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   420
									type, priority);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   421
			}
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   422
			lua_pop (L, 1);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   423
		} else { // Unregister message handler
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   424
			llm_handler_t *handler = luaL_checklm_handler (L, 2);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   425
			lm_connection_unregister_message_handler (object->connection,
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   426
								  handler->handler,
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   427
								  type);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   428
		}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   429
		lua_pop (L, 1);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   430
	} else { // Unregister reply handler
31
afcdbbce5002 Autodetect if unregister_reply_handler is present
Myhailo Danylenko <isbear@ukrpost.net>
parents: 30
diff changeset
   431
#ifdef HAVE_LM_CONNECTION_UNREGISTER_REPLY_HANDLER
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   432
		llm_handler_t *handler = luaL_checklm_handler (L, 2);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   433
		lm_connection_unregister_reply_handler (object -> connection,
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   434
		                                        handler -> handler);
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   435
#else
31
afcdbbce5002 Autodetect if unregister_reply_handler is present
Myhailo Danylenko <isbear@ukrpost.net>
parents: 30
diff changeset
   436
		W ("Sorry, your loudmouth have no unregister_reply_handler ()");
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   437
		lua_pushnil (L);
31
afcdbbce5002 Autodetect if unregister_reply_handler is present
Myhailo Danylenko <isbear@ukrpost.net>
parents: 30
diff changeset
   438
		lua_pushliteral (L, "Sorry, your loudmouth have no unregister_reply_handler ()");
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   439
		return 2;
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   440
#endif
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   441
	}
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   442
	lua_pop (L, 1);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   443
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   444
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   445
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   446
/// disconnect callback function
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   447
/// Function, that will be called when disconnection occurs.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   448
/// A: lm connection object, disconnect reason
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   449
void llm_disconnect_callback (LmConnection *connection, LmDisconnectReason reason, llm_callback_t *cb)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   450
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   451
	lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   452
	bless_lm_connection (cb->L, connection);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   453
	// XXX lm_connection_unref (connection);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   454
	luaL_pushenum (cb->L, reason, llm_disconnect_reason);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   455
	if (lua_pcall (cb->L, 2, 0, 0)) {
9
50f55d494efb Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   456
		W ("Disconnect callback error: %s", lua_tostring (cb->L, -1));
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   457
		lua_pop (cb->L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   458
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   459
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   460
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   461
/// connection:ondisconnect
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   462
/// Sets callback, that will be called on connection disconnect.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   463
/// A: disconnect callback function
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   464
/// R: lm connection object
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   465
static int ondisconnect_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   466
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   467
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   468
	llm_callback_t *cb;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   469
	luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   470
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   471
	cb = luaL_malloc (L, sizeof (llm_callback_t));
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   472
	cb->reference  = luaL_ref (L, LUA_REGISTRYINDEX);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   473
	cb->L          = L;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   474
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   475
	lm_connection_set_disconnect_function (object->connection,
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   476
					       (LmDisconnectFunction)llm_disconnect_callback,
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   477
					       cb, (GDestroyNotify)llm_callback_destroy);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   478
	return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   479
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   480
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   481
/// connection:open_wait
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   482
/// Synchronous open call, that will block until connection will be opened.
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   483
/// R: lm connection object or nil, string (error message)
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   484
static int open_wait_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   485
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   486
	llm_connection_t *object = luaL_checklm_connection (L, 1);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   487
	GError *err = NULL;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   488
	if (lm_connection_open_and_block (object->connection, &err))
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   489
		return 1;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   490
	else {
9
50f55d494efb Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   491
		I ("Synchronous connection opening failed: %s", err->message);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   492
		lua_pushnil (L);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   493
		lua_pushstring (L, err->message);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   494
		g_error_free (err);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   495
		return 2;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   496
	}
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   497
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   498
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   499
/// connection:authenticate_wait
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   500
/// Synchronous authentication call, that will wait until the end of authentication.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   501
/// A: string (username), string (password), string (resource)
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   502
/// R: lm connection object or nil, string (error message)
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   503
static int authenticate_wait_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   504
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   505
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   506
	const char *username = luaL_checkstring (L, 2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   507
	const char *password = luaL_checkstring (L, 3);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   508
	const char *resource = luaL_checkstring (L, 4);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   509
	GError *err = NULL;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   510
	if (lm_connection_authenticate_and_block (object->connection, username,
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   511
								password, resource, &err))
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   512
		return 1;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   513
	else {
9
50f55d494efb Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   514
		I ("Synchronous authentication failed: %s", err->message);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   515
		lua_pushnil (L);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   516
		lua_pushstring (L, err->message);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   517
		g_error_free (err);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   518
		return 2;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   519
	}
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   520
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   521
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   522
/// connection:send_wait
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   523
/// Synchronous call, that will send message and wait for reply to it.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   524
/// A: lm message object
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   525
/// R: lm message object or nil, string (error message)
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   526
static int send_wait_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   527
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   528
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   529
	llm_message_t *message = luaL_checklm_message (L, 2);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   530
	GError *err = NULL;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   531
	LmMessage *new = lm_connection_send_with_reply_and_block (object->connection,
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   532
									message->message, &err);
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   533
	if (!new) {
9
50f55d494efb Not use error log level, it is fatal
Myhailo Danylenko <isbear@ukrpost.net>
parents: 6
diff changeset
   534
		I ("Synchronous message sending failed: %s", err->message);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   535
		lua_pushnil (L);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   536
		lua_pushstring (L, err->message);
30
21547232c875 Fix gerror handling
Myhailo Danylenko <isbear@ukrpost.net>
parents: 23
diff changeset
   537
		g_error_free (err);
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   538
		return 2;
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   539
	} else {
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   540
		bless_lm_message (L, new);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   541
		lm_message_unref (new); // XXX
6
90073cbb535d Logging and chained methods
Myhailo Danylenko <isbear@ukrpost.net>
parents: 4
diff changeset
   542
		return 1;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   543
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   544
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   545
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   546
/// connection:pointer
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   547
/// Returns pointer to underlying C loudmouth structure.
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   548
/// R: lightuserdata
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   549
static int pointer_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   550
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   551
	llm_connection_t *object = luaL_checklm_connection (L, 1);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   552
	lua_pushlightuserdata (L, object->connection);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   553
	lua_remove (L, -2);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   554
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   555
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   556
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   557
static int gc_lm_connection (lua_State *L)
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   558
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   559
	llm_connection_t *object = luaL_checklm_connection (L, 1);
38
34a2b880615c Fix debugging pointer format character
Myhailo Danylenko <isbear@ukrpost.net>
parents: 31
diff changeset
   560
	D ("Connection %p gc called", object);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   561
	lm_connection_unref (object->connection);
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   562
	return 0;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   563
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   564
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   565
static const luaL_Reg reg_f_lm_connection[] = {
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   566
	{ "new",   new_lm_connection       },
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   567
	{ "bless", bless_lua_lm_connection },
12
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   568
	{ NULL,    NULL                    },
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   569
};
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   570
11
a8c6460d612b Naming scheme change to more ld-friendly
Myhailo Danylenko <isbear@ukrpost.net>
parents: 10
diff changeset
   571
static const luaL_Reg reg_m_lm_connection[] = {
12
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   572
	{ "open",              open_lm_connection              },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   573
	{ "close",             close_lm_connection             },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   574
	{ "authenticate",      authenticate_lm_connection      },
13
8f502c1477ac Forgot to register connection:status
Myhailo Danylenko <isbear@ukrpost.net>
parents: 12
diff changeset
   575
	{ "status",            status_lm_connection            },
12
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   576
	{ "port",              port_lm_connection              },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   577
	{ "server",            server_lm_connection            },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   578
	{ "jid",               jid_lm_connection               },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   579
	{ "keep_alive_rate",   keep_alive_rate_lm_connection   },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   580
	{ "state",             status_lm_connection            },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   581
	{ "proxy",             proxy_lm_connection             },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   582
	{ "ssl",               ssl_lm_connection               },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   583
	{ "send",              send_lm_connection              },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   584
	{ "handler",           handler_lm_connection           },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   585
	{ "ondisconnect",      ondisconnect_lm_connection      },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   586
	{ "open_wait",         open_wait_lm_connection         },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   587
	{ "authenticate_wait", authenticate_wait_lm_connection },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   588
	{ "send_wait",         send_wait_lm_connection         },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   589
	{ "pointer",           pointer_lm_connection           },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   590
	{ "__gc",              gc_lm_connection                },
63f06a23c235 Empty to field
Myhailo Danylenko <isbear@ukrpost.net>
parents: 11
diff changeset
   591
	{ NULL,                NULL                            },
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   592
};
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   593
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   594
int luaopen_lm_connection (lua_State *L)
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   595
{
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   596
	luaL_newmetatable (L, "loudmouth.connection");
19
d775d7289fe4 Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents: 16
diff changeset
   597
	lua_pushvalue (L, -1);
d775d7289fe4 Use lua_pushliteral and lua_setfield
Myhailo Danylenko <isbear@ukrpost.net>
parents: 16
diff changeset
   598
	lua_setfield (L, -2, "__index");
54
6bef2082e5f9 v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents: 45
diff changeset
   599
	luaL_setfuncs (L, reg_m_lm_connection, 0);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   600
	lua_pop (L, 1);
16
09b375e9ce32 Switch to new module organization scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents: 13
diff changeset
   601
	lua_newtable (L); // XXX we can specify here exact amount of fields
54
6bef2082e5f9 v0.9.2 Support for lua 5.2
Myhailo Danylenko <isbear@ukrpost.net>
parents: 45
diff changeset
   602
	luaL_setfuncs (L, reg_f_lm_connection, 0);
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   603
	return 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   604
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   605