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