main.c
author Myhailo Danylenko <isbear@ukrpost.net>
Tue, 24 Feb 2009 09:14:00 +0200
changeset 6 90dceae3ed1f
parent 3 a5f864d4207f
child 7 eb6d89bf1fbf
permissions -rw-r--r--
Hooks unification * hook naming scheme unification * one handler for all * handler name from option * multiple transports * dopath returns error message
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
#include <glib.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
#include <gmodule.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
#include <lua.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
#include <lauxlib.h>
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     6
#include <lualib.h>
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
     7
#include <stdio.h>
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
     8
#include <stdlib.h>    // getenv
2
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
     9
#include <string.h>    // strcmp
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    10
3
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
    11
#include "config.h"
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    12
#include "util.h"
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    13
#include "logprint.h"  // scr_LogPrint
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    14
#include "screen.h"    // scr_Beep, scr_WriteIncomingMessage
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    15
#include "hbuf.h"      // HBB_PREFIX_INFO
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    16
#include "commands.h"  // process_command, cmd_add, cmd_del
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    17
#include "xmpp.h"      // xmpp_getstatus, xmpp_getstatusmsg, lconnection, xmpp_add_feature, xmpp_del_feature
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    18
#include "roster.h"    // imstatus2char, foreach_buddy, buddy_*, current_buddy, BUDDATA, ROSTER_TYPE_*
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    19
#include "utils.h"     // from_utf8, jidtodisp
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    20
#include "hooks.h"     // hk_add_handler, hk_del_handler
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    21
#include "settings.h"  // settings_set, settings_del, settings_get
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    24
// global lua state object, necessary for uninitialization function
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    25
static lua_State *lua = NULL;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    26
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    27
// caller sould g_free result
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
char *mcabber_config_filename (const char *file)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    30
	const char *home = getenv ("HOME");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    31
	if (!home)
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    32
		return NULL;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    33
	return g_strconcat (home, "/.mcabber/", file ? file : "", NULL);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    34
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    35
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    36
/// print
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    37
/// Prints its arguments to log with default priority.
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    38
/// A: string/number, ...
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    39
static int lua_global_print (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    41
	lua_concat (L, lua_gettop (L));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    42
	scr_LogPrint (LPRINT_LOGNORM, lua_tostring (L, -1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    43
	return 0;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
/// dopath
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
/// Loads lua file from default location.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
/// A: string (filename, without ".lua")
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
    49
/// R: string (error message, optional)
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
static int lua_global_dopath (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    52
	const char *name = luaL_checkstring (L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    53
	size_t      size = lua_objlen (L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    54
	char       *path;
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
    55
	int         ret = 0;
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    56
	if (!strncmp (name + size - 4, ".lua", 4))
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    57
		path = mcabber_config_filename (name);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    58
	else {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    59
		char *fname = g_strconcat (name, ".lua", NULL);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    60
		path = mcabber_config_filename (fname);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    61
		g_free (fname);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    62
	}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    63
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
    64
	if (ret = luaL_loadfile (L, path))
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    65
		scr_LogPrint (LPRINT_LOGNORM, "lua: Unable to compile file %s: %s", path, lua_tostring (L, -1));
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
    66
	else if (ret = lua_pcall (lua, 0, LUA_MULTRET, 0))
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    67
		scr_LogPrint(LPRINT_LOGNORM, "lua: Runtime error in file %s: %s", path, lua_tostring (L, -1));
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
    68
	g_free (path);
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    69
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
    70
	if (ret)
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
    71
		return 1;
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
    72
	else
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
    73
		return 0;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    74
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
/// main.config_file
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
/// Adds mcabber default config location path to config file name.
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    78
/// Note: deprecated, use dopath.
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
/// A: string (filename)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
/// R: string (full path)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    81
static int lua_main_config_file (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    82
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    83
	char *home = mcabber_config_filename (luaL_checkstring (L, 1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    84
	if (!home) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    85
		lua_pushstring (L, "Cannot find home dir!");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    86
		lua_error (L);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    87
	}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    88
	lua_pushstring (L, home);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    89
	g_free (home);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    90
	return 1;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    91
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
/// log print type
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    94
/// G:
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
static const string2enum_t lua_lprint[] = {
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    96
	{ "normal",  LPRINT_NORMAL  },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    97
	{ "log",     LPRINT_LOG     },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    98
	{ "debug",   LPRINT_DEBUG   },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    99
	{ "notutf0", LPRINT_NOTUTF8 },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   100
	{ NULL,      0              },
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
};
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   103
/// roster type
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   104
/// G:
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
static const string2enum_t lua_roster_type[] = {
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   106
	{ "user",    ROSTER_TYPE_USER    },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   107
	{ "group",   ROSTER_TYPE_GROUP   },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   108
	{ "agent",   ROSTER_TYPE_AGENT   },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   109
	{ "room",    ROSTER_TYPE_ROOM    },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   110
	{ "special", ROSTER_TYPE_SPECIAL },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   111
	{ NULL,      0                   },
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   112
};
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   113
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   114
/// main.log
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   115
/// Prints message to log.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   116
/// A: log print type, message, message...
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   117
static int lua_main_log (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   118
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   119
	int type = luaL_checkenum_multi (L, 1, lua_lprint);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   120
	lua_concat (L, lua_gettop (L) - 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   121
	scr_LogPrint (type, lua_tostring (L, -1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   122
	return 0;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   123
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   124
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   125
/// main.option
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   126
/// Sets or gets value of mcabber option.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   127
/// You can specify nil as a value to delete option.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   128
/// XXX: Should we do types here?
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   129
/// A: string (option name), string (value, optional)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   130
/// R: string (value, optional)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   131
static int lua_main_option (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   132
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   133
	const char *name = luaL_checkstring (L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   134
	if (lua_gettop (L) > 1) { // Set
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   135
		if (lua_type (L, 2) == LUA_TNIL) // Unset
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   136
			settings_del (SETTINGS_TYPE_OPTION, name);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   137
		else { // Set
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   138
			const char *value = luaL_checkstring (L, 2);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   139
			settings_set (SETTINGS_TYPE_OPTION, name, value);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   140
		}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   141
		return 0;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   142
	} else { // Get
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   143
		const char *value = settings_get (SETTINGS_TYPE_OPTION, name);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   144
		if (value)
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   145
			lua_pushstring (L, value);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   146
		else
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   147
			lua_pushnil (L);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   148
		return 1;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   149
	}
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   150
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   151
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   152
/// main.connection
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   153
/// Returns lightuserdata of mcabber's loudmouth connection.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   154
/// This can be very useful with lua-loudmouth, and not much otherwise.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   155
/// R: lightuserdata
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   156
static int lua_main_connection (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   157
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   158
	lua_pushlightuserdata (L, lconnection);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   159
	return 1;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   160
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   161
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   162
/// main.print_info
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   163
/// Prints a system message to buddy's window.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   164
/// A: string (jid), string (message)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   165
static int lua_main_print_info (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   166
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   167
	char *to = jidtodisp (luaL_checkstring (L, 1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   168
	scr_WriteIncomingMessage (to, luaL_checkstring (L, 2), 0, HBB_PREFIX_INFO, 0);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   169
	g_free (to);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   170
	return 0;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   171
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   172
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   173
/// main.beep
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   174
/// Beeps with system speaker.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   175
static int lua_main_beep (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   176
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   177
	scr_Beep ();
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   178
	return 0;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   179
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   180
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   181
/// main.run
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   182
/// Runs specified mcabber command.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   183
/// A: string
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   184
static int lua_main_run (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   185
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   186
	process_command (luaL_checkstring (L, 1), TRUE);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   187
	return 0;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   188
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   189
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   190
/// main.status
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   191
/// Returns your current status.
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   192
/// R: string (status letter), string (status message)
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   193
static int lua_main_status (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   194
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   195
	char *sm = from_utf8 (xmpp_getstatusmsg ());
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   196
	lua_pushlstring (L, &imstatus2char[xmpp_getstatus ()], 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   197
	lua_pushstring (L, sm);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   198
	g_free (sm);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   199
	return 2;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   200
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   201
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   202
// expects table on top
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   203
static void lua_rosterlist_callback (gpointer buddy, lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   204
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   205
	lua_pushnumber (L, lua_objlen (L, -1) + 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   206
	lua_pushstring (L, buddy_getjid (buddy));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   207
	lua_settable (L, -3);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   208
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   209
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   210
/// main.roster
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   211
/// Returns array of jids of buddies in roster.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   212
/// R: table
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   213
static int lua_main_roster (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   214
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   215
	lua_newtable (L);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   216
	foreach_buddy (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM, (void (*) (gpointer buddy, void *data))lua_rosterlist_callback, L);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   217
	return 1;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   218
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   219
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   220
/// main.current_buddy
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   221
/// Returns jid of current selected buddy.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   222
/// R: string
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   223
static int lua_main_current_buddy (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   224
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   225
	lua_pushstring (L, buddy_getjid (BUDDATA (current_buddy)));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   226
	return 1;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   227
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   228
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   229
typedef struct {
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   230
	lua_State *L;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   231
	gpointer   buddy;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   232
} lua_state_and_buddy_t; // :)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   233
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   234
// expects table on top!
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   235
static void lua_buddy_resources_callback (gpointer resource, lua_state_and_buddy_t *d)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   236
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   237
	lua_pushstring (d->L, resource);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   238
	lua_createtable (d->L, 0, 3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   239
	lua_pushstring (d->L, "priority");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   240
	lua_pushnumber (d->L, buddy_getresourceprio (d->buddy, resource));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   241
	lua_settable   (d->L, -3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   242
	lua_pushstring  (d->L, "status");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   243
	lua_pushlstring (d->L, &imstatus2char[buddy_getstatus (d->buddy, resource)], 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   244
	lua_settable    (d->L, -3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   245
	lua_pushstring (d->L, "message");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   246
	lua_pushstring (d->L, buddy_getstatusmsg (d->buddy, resource));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   247
	lua_settable   (d->L, -3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   248
	lua_settable (d->L, -3);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   249
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   250
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   251
/// main.buddy_info
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   252
/// Returns a hash table with information on specified buddy.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   253
/// Table contains fields type, name, onserver and resources.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   254
/// Resources is also a hash table, that contains tables with information,
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   255
/// specific for each resource. In each resource table there are fields
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   256
/// priority, status and message.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   257
/// A: string (jid)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   258
/// R: table
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   259
static int lua_main_buddy_info (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   260
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   261
	char                  *jid   = jidtodisp (luaL_checkstring (L, 1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   262
	GSList                *buddy = roster_find (jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   263
	lua_state_and_buddy_t  snb;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   264
	g_free (jid);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   265
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   266
	if (!buddy) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   267
		lua_pushnil (L);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   268
		return 1;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   269
	}
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   270
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   271
	lua_createtable (L, 0, 3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   272
	lua_pushstring (L, "type");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   273
	luaL_pushenum  (L, buddy_gettype (BUDDATA (buddy)), lua_roster_type);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   274
	lua_settable   (L, -3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   275
	lua_pushstring (L, "name");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   276
	lua_pushstring (L, buddy_getname (BUDDATA (buddy)));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   277
	lua_settable   (L, -3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   278
	lua_pushstring  (L, "onserver");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   279
	lua_pushboolean (L, buddy_getonserverflag (BUDDATA (buddy)));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   280
	lua_settable    (L, -3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   281
	lua_pushstring  (L, "resources");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   282
	lua_createtable (L, 0, 0);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   283
	snb.L     = L;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   284
	snb.buddy = BUDDATA (buddy);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   285
	g_slist_foreach (buddy_getresources (BUDDATA (buddy)), (GFunc) lua_buddy_resources_callback, &snb);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   286
	lua_settable    (L, -3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   287
	
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   288
	return 1;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   289
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   290
2
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   291
// XMPP DISCO FEATURES
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   292
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   293
GSList *lua_added_features = NULL;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   294
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   295
/// main.add_feature
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   296
/// Adds xmlns to disco#info features list.
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   297
/// A: string (xmlns)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   298
static int lua_main_add_feature (lua_State *L)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   299
{
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   300
	const char *xmlns = luaL_checkstring (L, 1);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   301
	xmpp_add_feature (xmlns);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   302
	lua_added_features = g_slist_prepend (lua_added_features, g_strdup (xmlns));
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   303
	return 0;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   304
}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   305
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   306
/// main.del_feature
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   307
/// Removes xmlns from disco#info features list.
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   308
/// A: stirng (xmlns)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   309
static int lua_main_del_feature (lua_State *L)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   310
{
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   311
	const char *xmlns = luaL_checkstring (L, 1);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   312
	GSList     *el    = g_slist_find_custom (lua_added_features, xmlns, (GCompareFunc) strcmp);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   313
	xmpp_del_feature (xmlns);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   314
	if (el) {
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   315
		g_free (el->data);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   316
		lua_added_features = g_slist_delete_link (lua_added_features, el);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   317
	}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   318
	return 0;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   319
}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   320
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   321
// MCABBER COMMANDS
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   322
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   323
typedef struct {
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   324
	int        reference;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   325
	lua_State *L;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   326
} lua_command_callback_t;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   327
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   328
static GSList *lua_added_commands = NULL;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   329
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   330
/// command function
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   331
/// Function to handle newly registered command.
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   332
/// A: string (arguments)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   333
void lua_main_command_handler (char *args, lua_command_callback_t *cb)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   334
{
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   335
	lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   336
	lua_pushstring (cb->L, args);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   337
	if (lua_pcall (cb->L, 1, 0, 0)) {
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   338
		scr_LogPrint (LPRINT_LOGNORM, "lua: Command execution error: %s", lua_tostring (cb->L, -1));
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   339
		lua_pop (cb->L, 1);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   340
	}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   341
}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   342
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   343
/// main.add_command
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   344
/// Associates mcabber command name with lua function.
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   345
/// A: string (command name), command function
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   346
static int lua_main_add_command (lua_State *L)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   347
{
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   348
	const char             *name = luaL_checkstring (L, 1);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   349
	lua_command_callback_t *cb;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   350
	luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   351
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   352
	cb = luaL_malloc (L, sizeof (lua_command_callback_t));
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   353
	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   354
	cb->L         = L;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   355
	cmd_add (name, "", 0, 0, (void (*) (char *p)) lua_main_command_handler, cb);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   356
	lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name));
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   357
	return 0;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   358
}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   359
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   360
/// main.del_command
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   361
/// Removes command from a list of commands.
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   362
/// A: string (command name)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   363
static int lua_main_del_command (lua_State *L)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   364
{
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   365
	const char             *name = luaL_checkstring (L, 1);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   366
	GSList                 *el   = g_slist_find_custom (lua_added_commands, name, (GCompareFunc) strcmp);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   367
	lua_command_callback_t *cb   = cmd_del (name);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   368
	if (cb) {
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   369
		luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   370
		luaL_free (cb->L, cb);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   371
	}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   372
	if (el) {
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   373
		g_free (el->data);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   374
		lua_added_commands = g_slist_delete_link (lua_added_commands, el);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   375
	}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   376
	return 0;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   377
}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   378
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   379
// TIMER
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   380
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   381
#define LUA_TIMER_PRIORITY ( G_PRIORITY_HIGH_IDLE )
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   382
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   383
typedef struct {
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   384
	int        reference;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   385
	lua_State *L;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   386
} lua_timer_callback_t;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   387
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   388
static void lua_timer_callback_destroy (lua_timer_callback_t *cb)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   389
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   390
	luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   391
	luaL_free (cb->L, cb);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   392
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   393
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   394
/// timer function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   395
/// Function, that will be called periodically until it returns false.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   396
/// R: boolean
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   397
static gboolean lua_timer_callback (lua_timer_callback_t *cb)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   398
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   399
	int ret;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   400
	lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   401
	if (lua_pcall (cb->L, 0, 1, 0)) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   402
		scr_LogPrint (LPRINT_LOGNORM, "lua: Timer callback execution error: %s", lua_tostring (cb->L, -1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   403
		lua_pop (cb->L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   404
		return FALSE;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   405
	}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   406
	ret = lua_toboolean (cb->L, -1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   407
	lua_pop (cb->L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   408
	return ret;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   409
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   410
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   411
/// main.timer
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   412
/// Creates new timer function, that will be called periodically.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   413
/// A: integer (interval, seconds), timer function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   414
static int lua_main_timer (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   415
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   416
	int                   interval = luaL_checkint (L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   417
	lua_timer_callback_t *cb;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   418
	luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   419
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   420
	cb = luaL_malloc (L, sizeof (lua_timer_callback_t));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   421
	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   422
	cb->L         = L;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   423
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   424
	g_timeout_add_seconds_full (LUA_TIMER_PRIORITY, interval, (GSourceFunc) lua_timer_callback, cb, (GDestroyNotify) lua_timer_callback_destroy);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   425
	return 0;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   426
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   427
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   428
// BACKGROUND PIPE READING
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   429
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   430
#define LUA_BGREAD_BUFFER ( 4096 )
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   431
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   432
typedef struct {
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   433
	lua_State *L;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   434
	FILE      *fd;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   435
	int        reference;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   436
} lua_bgread_callback_t;
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   437
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   438
static gchar lua_bgread_buffer[LUA_BGREAD_BUFFER];
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   439
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   440
static void lua_bgread_callback_destroy (lua_bgread_callback_t *cb)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   441
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   442
	luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   443
	pclose (cb->fd);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   444
	luaL_free (cb->L, cb);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   445
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   446
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   447
/// background reading function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   448
/// Function, that processes output from pipe in asynchroneous way.
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   449
/// A: string (data) or nil (eof)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   450
/// R: boolean (false if reading should be terminated)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   451
static gboolean
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   452
lua_bgread_callback (GIOChannel *source, GIOCondition condition, lua_bgread_callback_t *cb)
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   453
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   454
	int ret = TRUE;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   455
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   456
	if (condition | G_IO_IN) { // data
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   457
		while (TRUE) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   458
			gsize read = 0;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   459
			g_io_channel_read_chars (source, lua_bgread_buffer, LUA_BGREAD_BUFFER, &read, NULL);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   460
			if (!read) // exausted
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   461
				break;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   462
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   463
			lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   464
			lua_pushlstring (cb->L, lua_bgread_buffer, read);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   465
			if (lua_pcall (cb->L, 1, 1, 0)) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   466
				scr_LogPrint (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   467
				lua_pop (cb->L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   468
				return FALSE;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   469
			}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   470
			ret = lua_toboolean (cb->L, -1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   471
			lua_pop (cb->L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   472
			if (!ret) // enough
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   473
				return FALSE;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   474
		}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   475
	}
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   476
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   477
	if (condition & ~G_IO_IN) { // err or hup
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   478
		lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   479
		lua_pushnil (cb->L);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   480
		if (lua_pcall (cb->L, 1, 1, 0)) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   481
			scr_LogPrint (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   482
			lua_pop (cb->L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   483
			return FALSE;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   484
		}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   485
		ret = lua_toboolean (cb->L, -1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   486
		lua_pop (cb->L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   487
	}
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   488
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   489
	return ret;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   490
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   491
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   492
/// main.bgread
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   493
/// Runs specified command and passes its output to a given function.
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   494
/// A: string (command), background reading function
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   495
static int lua_main_bgread (lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   496
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   497
	const char            *command = luaL_checkstring (L, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   498
	lua_bgread_callback_t *cb;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   499
	FILE                  *fd;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   500
	GIOChannel            *channel;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   501
	const char            *charset = NULL;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   502
	luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected");
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   503
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   504
	fd = popen (command, "r");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   505
	if (!fd) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   506
		lua_pushstring (L, "Error opening pipe");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   507
		lua_error (L);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   508
	}
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   509
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   510
	channel = g_io_channel_unix_new (fileno (fd));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   511
	// We, most likely, need this,
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   512
	// But we cannot use this,
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   513
	// It will block.
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   514
	//if (!g_get_charset (&charset))
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   515
	g_io_channel_set_encoding (channel, charset, NULL);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   516
	g_io_channel_set_buffered (channel, FALSE);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   517
	g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   518
	g_io_channel_set_close_on_unref (channel, TRUE);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   519
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   520
	cb = luaL_malloc (L, sizeof (lua_bgread_callback_t));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   521
	cb->reference = luaL_ref (L, LUA_REGISTRYINDEX);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   522
	cb->L         = L;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   523
	cb->fd        = fd;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   524
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   525
	g_io_add_watch_full (channel, G_PRIORITY_HIGH_IDLE, G_IO_IN|G_IO_HUP|G_IO_ERR, (GIOFunc) lua_bgread_callback, cb, (GDestroyNotify) lua_bgread_callback_destroy);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   526
	return 0;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   527
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   528
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   529
// MAIN INITIALIZATION CODE
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   530
3
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   531
#ifdef LLM_LOG_HANDLER
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   532
// FIXME: this should not be here
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   533
guint lua_lm_log_handler_id;
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   534
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   535
void lua_lm_log_handler (const gchar *domain, GLogLevelFlags log_level, const gchar *message, gpointer ignore)
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   536
{
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   537
	scr_LogPrint (LPRINT_LOGNORM, "%s: %s", domain, message);
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   538
}
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   539
#endif
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   540
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   541
static void lua_hook (hk_arg_t *args, lua_State *L)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   542
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   543
	hk_arg_t *arg = args;
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   544
	const char *hook = settings_opt_get ("lua_hook_function");
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   545
	if (!hook)
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   546
		return;
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   547
	lua_getglobal (lua, hook);
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   548
	if (!lua_isfunction (lua, -1)) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   549
		lua_pop (lua, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   550
		return;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   551
	}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   552
	lua_newtable (L);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   553
	while (arg->name != NULL) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   554
		lua_pushstring (L, arg->name);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   555
		lua_pushstring (L, arg->value);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   556
		lua_settable (L, -3);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   557
		arg++;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   558
	}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   559
	if (lua_pcall (lua, 1, 0, 0)) {
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   560
		scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook handler: %s", lua_tostring (lua, -1));
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   561
		lua_pop (lua, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   562
	}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   563
}
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   564
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   565
static void *lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   566
	if (nsize == 0) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   567
		g_free (ptr);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   568
		return NULL;
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   569
	} else
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   570
		return g_realloc (ptr, nsize);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   571
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   572
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   573
static const luaL_Reg lua_reg_main[] = {
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   574
	{ "config_file",   lua_main_config_file   },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   575
	{ "connection",    lua_main_connection    },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   576
	{ "log",           lua_main_log           },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   577
	{ "option",        lua_main_option        },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   578
	{ "add_feature",   lua_main_add_feature   },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   579
	{ "del_feature",   lua_main_del_feature   },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   580
	{ "add_command",   lua_main_add_command   },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   581
	{ "del_command",   lua_main_del_command   },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   582
	{ "print_info",    lua_main_print_info    },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   583
	{ "beep",          lua_main_beep          },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   584
	{ "run",           lua_main_run           },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   585
	{ "status",        lua_main_status        },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   586
	{ "roster",        lua_main_roster        },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   587
	{ "current_buddy", lua_main_current_buddy },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   588
	{ "buddy_info",    lua_main_buddy_info    },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   589
	{ "timer",         lua_main_timer         },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   590
	{ "bgread",        lua_main_bgread        },
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   591
	{ NULL,            NULL                   },
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   592
};
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   593
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   594
const gchar *g_module_check_init (GModule *module)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   595
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   596
	char *initfile;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   597
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   598
	lua = lua_newstate (lua_alloc, NULL);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   599
	if (!lua) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   600
		scr_LogPrint (LPRINT_LOGNORM, "lua: Initialization error");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   601
		return "Lua initialization error";
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   602
	}
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   603
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   604
	luaL_openlibs (lua);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   605
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   606
	luaL_register (lua, "main", lua_reg_main);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   607
	lua_pop (lua, 1); // XXX
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   608
	lua_register (lua, "dopath", lua_global_dopath);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   609
	lua_register (lua, "print",  lua_global_print );
3
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   610
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   611
#ifdef LLM_LOG_HANDLER
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   612
	// FIXME: this should not be here.
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   613
	lua_lm_log_handler_id = g_log_set_handler ("lua-lm", G_LOG_LEVEL_MASK, (GLogFunc) lua_lm_log_handler, NULL);
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   614
#endif
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   615
	
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   616
	initfile = mcabber_config_filename ("mcabberrc.lua");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   617
	if (!initfile)
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   618
		scr_LogPrint (LPRINT_LOGNORM, "lua: Cannot determine config file name");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   619
	else {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   620
		if (luaL_loadfile(lua, initfile)) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   621
			scr_LogPrint (LPRINT_LOGNORM, "lua: Unable to compile rc file: %s", lua_tostring (lua, -1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   622
			lua_pop (lua, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   623
		} else if (lua_pcall (lua, 0, LUA_MULTRET, 0)) {
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   624
			scr_LogPrint (LPRINT_LOGNORM, "lua: Runtime error in rc file: %s", lua_tostring(lua, -1));
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   625
			lua_pop (lua, 1);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   626
		} else
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   627
			scr_LogPrint (LPRINT_LOGNORM, "Loaded mcabberrc.lua");
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   628
		g_free (initfile);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   629
	}
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   630
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   631
	hk_add_handler ((hk_handler_t) lua_hook, lua);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   632
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   633
	{
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   634
		hk_arg_t args[] = {
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   635
			{ "hook", "hook-start" },
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   636
			{ NULL,   NULL         },
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   637
		};
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   638
		lua_hook (args, lua);
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   639
	}
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   640
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   641
	return NULL;
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   642
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   643
2
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   644
static void lua_features_destroy (char *xmlns, gpointer ignore)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   645
{
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   646
	xmpp_del_feature (xmlns);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   647
	g_free (xmlns);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   648
}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   649
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   650
static void lua_commands_destroy (char *name, gpointer ignore)
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   651
{
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   652
	lua_command_callback_t *cb = cmd_del (name);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   653
	if (cb) {
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   654
		luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   655
		luaL_free (cb->L, cb);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   656
	}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   657
	g_free (name);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   658
}
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   659
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   660
void g_module_unload (GModule *module)
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   661
{
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   662
	if (lua) {
6
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   663
		hk_arg_t args[] = {
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   664
			{ "hook", "hook-quit" },
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   665
			{ NULL,   NULL        },
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   666
		};
90dceae3ed1f Hooks unification
Myhailo Danylenko <isbear@ukrpost.net>
parents: 3
diff changeset
   667
		lua_hook (args, lua);
2
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   668
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   669
		hk_del_handler ((hk_handler_t) lua_hook, lua);
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   670
2
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   671
		g_slist_foreach (lua_added_features, (GFunc) lua_features_destroy, NULL);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   672
		g_slist_free (lua_added_features);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   673
		lua_added_features = NULL;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   674
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   675
		g_slist_foreach (lua_added_commands, (GFunc) lua_commands_destroy, NULL);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   676
		g_slist_free (lua_added_commands);
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   677
		lua_added_commands = NULL;
a88a395e6868 Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents: 1
diff changeset
   678
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   679
		lua_close (lua);
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   680
		lua = NULL;
3
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   681
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   682
#ifdef LLM_LOG_HANDLER
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   683
		// FIXME: shouldn't be here
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   684
		g_log_remove_handler ("lua-lm", lua_lm_log_handler_id);
a5f864d4207f Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents: 2
diff changeset
   685
#endif
1
7d87d323c889 Fixes and improvement
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   686
	}
0
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   687
}
65cbecad22b4 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   688