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