author | Myhailo Danylenko <isbear@ukrpost.net> |
Mon, 23 Mar 2009 04:16:22 +0200 | |
changeset 59 | 4660c4f10ef1 |
parent 52 | 50d4e9bc622d |
child 72 | d049c92d0809 |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
#include <glib.h> |
|
3 |
#include <gmodule.h> |
|
4 |
#include <lua.h> |
|
5 |
#include <lauxlib.h> |
|
6 |
#include <lualib.h> |
|
1 | 7 |
#include <stdio.h> |
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 | 10 |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
11 |
#include "config.h" |
1 | 12 |
#include "util.h" |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
13 |
#include "logprint.h" // scr_LogPrint |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
14 |
#include "screen.h" // scr_Beep, scr_WriteIncomingMessage |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
15 |
#include "hbuf.h" // HBB_PREFIX_INFO |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
16 |
#include "commands.h" // process_command, cmd_add, cmd_del |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
17 |
#include "xmpp.h" // xmpp_getstatus, xmpp_getstatusmsg, lconnection |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
18 |
#include "xmpp_helper.h" // xmpp_add_feature, xmpp_del_feature |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
19 |
#include "roster.h" // imstatus2char, foreach_buddy, buddy_*, current_buddy, BUDDATA, ROSTER_TYPE_* |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
20 |
#include "utils.h" // from_utf8, jidtodisp |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
21 |
#include "hooks.h" // hk_add_handler, hk_del_handler |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
22 |
#include "settings.h" // settings_set, settings_del, settings_get |
8 | 23 |
#include "compl.h" // compl_new_category, compl_add_category_word, compl_del_category_word |
52 | 24 |
#include "events.h" // evs_* |
0 | 25 |
|
26 |
||
1 | 27 |
// global lua state object, necessary for uninitialization function |
28 |
static lua_State *lua = NULL; |
|
0 | 29 |
|
1 | 30 |
// caller sould g_free result |
18
ba74a9aadaf4
Fix namespace for config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
31 |
static char *mcabber_config_filename (const char *file) |
0 | 32 |
{ |
1 | 33 |
const char *home = getenv ("HOME"); |
34 |
if (!home) |
|
35 |
return NULL; |
|
36 |
return g_strconcat (home, "/.mcabber/", file ? file : "", NULL); |
|
0 | 37 |
} |
38 |
||
39 |
/// print |
|
1 | 40 |
/// Prints its arguments to log with default priority. |
41 |
/// A: string/number, ... |
|
0 | 42 |
static int lua_global_print (lua_State *L) |
43 |
{ |
|
1 | 44 |
lua_concat (L, lua_gettop (L)); |
13 | 45 |
scr_LogPrint (LPRINT_LOGNORM | LPRINT_NOTUTF8, lua_tostring (L, -1)); |
1 | 46 |
return 0; |
0 | 47 |
} |
48 |
||
49 |
/// dopath |
|
50 |
/// Loads lua file from default location. |
|
13 | 51 |
/// XXX: g_filename_from_utf8? |
0 | 52 |
/// A: string (filename, without ".lua") |
6 | 53 |
/// R: string (error message, optional) |
0 | 54 |
static int lua_global_dopath (lua_State *L) |
55 |
{ |
|
1 | 56 |
const char *name = luaL_checkstring (L, 1); |
57 |
size_t size = lua_objlen (L, 1); |
|
58 |
char *path; |
|
6 | 59 |
int ret = 0; |
33 | 60 |
if (size > 4 && !strncmp (name + size - 4, ".lua", 4)) |
1 | 61 |
path = mcabber_config_filename (name); |
62 |
else { |
|
63 |
char *fname = g_strconcat (name, ".lua", NULL); |
|
64 |
path = mcabber_config_filename (fname); |
|
65 |
g_free (fname); |
|
66 |
} |
|
67 |
||
6 | 68 |
if (ret = luaL_loadfile (L, path)) |
1 | 69 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Unable to compile file %s: %s", path, lua_tostring (L, -1)); |
6 | 70 |
else if (ret = lua_pcall (lua, 0, LUA_MULTRET, 0)) |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
71 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Runtime error in file %s: %s", path, lua_tostring (L, -1)); |
6 | 72 |
g_free (path); |
1 | 73 |
|
6 | 74 |
if (ret) |
75 |
return 1; |
|
76 |
else |
|
77 |
return 0; |
|
0 | 78 |
} |
79 |
||
31
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
80 |
/// yes or no ansvers |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
81 |
/// G: |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
82 |
static const string2enum_t lua_yesno[] = { |
39 | 83 |
{ "1", 1 }, |
84 |
{ "0", 0 }, |
|
31
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
85 |
{ "enable", 1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
86 |
{ "disable", 0 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
87 |
{ "true", 1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
88 |
{ "false", 0 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
89 |
{ "on", 1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
90 |
{ "off", 0 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
91 |
{ "yes", 1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
92 |
{ "no", 0 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
93 |
{ NULL, -1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
94 |
}; |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
95 |
|
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
96 |
/// main.yesno |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
97 |
/// According to yes or no ansvers returns true or false. |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
98 |
/// If ansver is not recognized, returns nil. |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
99 |
/// A: anything (string expected) |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
100 |
/// R: boolean or nil |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
101 |
static int lua_main_yesno (lua_State *L) |
0 | 102 |
{ |
31
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
103 |
int type = lua_type (L, 1); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
104 |
if (type == LUA_TSTRING) { |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
105 |
int ret = luaL_checkenum (L, 1, lua_yesno); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
106 |
if (ret == -1) |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
107 |
lua_pushnil (L); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
108 |
else |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
109 |
lua_pushboolean (L, ret); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
110 |
} else if (type == LUA_TNUMBER) |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
111 |
lua_pushboolean (L, lua_tointeger (L, 1)); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
112 |
else if (type != LUA_TBOOLEAN) |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
113 |
lua_pushnil (L); |
1 | 114 |
return 1; |
0 | 115 |
} |
116 |
||
117 |
/// log print type |
|
1 | 118 |
/// G: |
0 | 119 |
static const string2enum_t lua_lprint[] = { |
1 | 120 |
{ "normal", LPRINT_NORMAL }, |
121 |
{ "log", LPRINT_LOG }, |
|
122 |
{ "debug", LPRINT_DEBUG }, |
|
123 |
{ "notutf0", LPRINT_NOTUTF8 }, |
|
124 |
{ NULL, 0 }, |
|
0 | 125 |
}; |
126 |
||
1 | 127 |
/// roster type |
128 |
/// G: |
|
0 | 129 |
static const string2enum_t lua_roster_type[] = { |
1 | 130 |
{ "user", ROSTER_TYPE_USER }, |
131 |
{ "group", ROSTER_TYPE_GROUP }, |
|
132 |
{ "agent", ROSTER_TYPE_AGENT }, |
|
133 |
{ "room", ROSTER_TYPE_ROOM }, |
|
134 |
{ "special", ROSTER_TYPE_SPECIAL }, |
|
135 |
{ NULL, 0 }, |
|
0 | 136 |
}; |
137 |
||
138 |
/// main.log |
|
139 |
/// Prints message to log. |
|
13 | 140 |
/// Note: most likely you need notutf8 flag enabled. |
0 | 141 |
/// A: log print type, message, message... |
142 |
static int lua_main_log (lua_State *L) |
|
143 |
{ |
|
1 | 144 |
int type = luaL_checkenum_multi (L, 1, lua_lprint); |
145 |
lua_concat (L, lua_gettop (L) - 1); |
|
146 |
scr_LogPrint (type, lua_tostring (L, -1)); |
|
147 |
return 0; |
|
0 | 148 |
} |
149 |
||
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
150 |
// expects table on top |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
151 |
static void lua_options_callback (char *key, char *value, lua_State *L) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
152 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
153 |
char *loc = from_utf8 (key); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
154 |
lua_pushstring (L, loc); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
155 |
g_free (loc); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
156 |
loc = from_utf8 (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
157 |
lua_pushstring (L, loc); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
158 |
g_free (loc); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
159 |
lua_settable (L, -3); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
160 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
161 |
|
0 | 162 |
/// main.option |
163 |
/// Sets or gets value of mcabber option. |
|
164 |
/// You can specify nil as a value to delete option. |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
165 |
/// If you omit option name, it returns hash table of all options. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
166 |
/// A: string (option name, optional), string (value, optional) |
0 | 167 |
/// R: string (value, optional) |
168 |
static int lua_main_option (lua_State *L) |
|
169 |
{ |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
170 |
int top = lua_gettop (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
171 |
if (top > 0) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
172 |
char *name = to_utf8 (luaL_checkstring (L, 1)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
173 |
if (top > 1) { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
174 |
if (lua_type (L, 2) == LUA_TNIL) // Unset |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
175 |
settings_del (SETTINGS_TYPE_OPTION, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
176 |
else { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
177 |
char *value = to_utf8 (luaL_checkstring (L, 2)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
178 |
settings_set (SETTINGS_TYPE_OPTION, name, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
179 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
180 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
181 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
182 |
return 0; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
183 |
} else { // Get |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
184 |
char *value = from_utf8 (settings_get (SETTINGS_TYPE_OPTION, name)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
185 |
if (value) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
186 |
lua_pushstring (L, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
187 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
188 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
189 |
lua_pushnil (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
190 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
191 |
return 1; |
1 | 192 |
} |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
193 |
} else { // List |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
194 |
lua_newtable (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
195 |
settings_foreach (SETTINGS_TYPE_OPTION, (void (*)(char *key, char *val, void *ud)) lua_options_callback, L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
196 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
197 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
198 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
199 |
|
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
200 |
/// main.alias |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
201 |
/// Sets or gets alias. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
202 |
/// You can specify nil as a command to delete alias. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
203 |
/// If you omit alias name, it will return hash table of all aliases. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
204 |
/// A: string (alias name, optional), string (command, optional) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
205 |
/// R: string (command, optional) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
206 |
static int lua_main_alias (lua_State *L) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
207 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
208 |
int top = lua_gettop (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
209 |
if (top > 0) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
210 |
char *name = to_utf8 (luaL_checkstring (L, 1)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
211 |
if (top > 1) { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
212 |
if (lua_type (L, 2) == LUA_TNIL) { // Unset |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
213 |
settings_del (SETTINGS_TYPE_ALIAS, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
214 |
compl_del_category_word (COMPL_CMD, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
215 |
} else { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
216 |
char *value = to_utf8 (luaL_checkstring (L, 2)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
217 |
if (!settings_get (SETTINGS_TYPE_ALIAS, name)) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
218 |
compl_add_category_word (COMPL_CMD, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
219 |
settings_set (SETTINGS_TYPE_ALIAS, name, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
220 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
221 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
222 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
223 |
return 0; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
224 |
} else { // Get |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
225 |
char *value = from_utf8 (settings_get (SETTINGS_TYPE_ALIAS, name)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
226 |
if (value) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
227 |
lua_pushstring (L, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
228 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
229 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
230 |
lua_pushnil (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
231 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
232 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
233 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
234 |
} else { // List |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
235 |
lua_newtable (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
236 |
settings_foreach (SETTINGS_TYPE_ALIAS, (void (*)(char *key, char *val, void *ud)) lua_options_callback, L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
237 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
238 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
239 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
240 |
|
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
241 |
/// main.bind |
15 | 242 |
/// Sets or gets binding. |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
243 |
/// You can specify nil as a command to unbind key. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
244 |
/// If you omit keycode, it will return hash table of all bindings. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
245 |
/// A: string (keycode, optional), string (command, optional) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
246 |
/// R: string (command, optional) |
15 | 247 |
static int lua_main_binding (lua_State *L) |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
248 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
249 |
int top = lua_gettop (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
250 |
if (top > 0) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
251 |
// just to be sure... |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
252 |
char *name = to_utf8 (luaL_checkstring (L, 1)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
253 |
if (top > 1) { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
254 |
if (lua_type (L, 2) == LUA_TNIL) // Unset |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
255 |
settings_del (SETTINGS_TYPE_BINDING, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
256 |
else { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
257 |
char *value = to_utf8 (luaL_checkstring (L, 2)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
258 |
settings_set (SETTINGS_TYPE_BINDING, name, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
259 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
260 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
261 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
262 |
return 0; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
263 |
} else { // Get |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
264 |
char *value = from_utf8 (settings_get (SETTINGS_TYPE_BINDING, name)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
265 |
if (value) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
266 |
lua_pushstring (L, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
267 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
268 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
269 |
lua_pushnil (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
270 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
271 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
272 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
273 |
} else { // List |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
274 |
lua_newtable (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
275 |
settings_foreach (SETTINGS_TYPE_BINDING, (void (*)(char *key, char *val, void *ud)) lua_options_callback, L); |
1 | 276 |
return 1; |
277 |
} |
|
0 | 278 |
} |
279 |
||
33 | 280 |
/// main.fileoption |
281 |
/// Gets option, expanding it as filename. |
|
282 |
/// A: string (option name) |
|
283 |
/// R: string (expanded option value) or nil |
|
284 |
static int lua_main_fileoption (lua_State *L) |
|
285 |
{ |
|
286 |
char *fname = expand_filename (settings_opt_get (luaL_checkstring (L, 1))); |
|
287 |
if (fname) { |
|
288 |
lua_pushstring (L, fname); |
|
289 |
g_free (fname); |
|
290 |
} else |
|
291 |
lua_pushnil (L); |
|
292 |
return 1; |
|
293 |
} |
|
294 |
||
0 | 295 |
/// main.connection |
296 |
/// Returns lightuserdata of mcabber's loudmouth connection. |
|
297 |
/// This can be very useful with lua-loudmouth, and not much otherwise. |
|
298 |
/// R: lightuserdata |
|
299 |
static int lua_main_connection (lua_State *L) |
|
300 |
{ |
|
1 | 301 |
lua_pushlightuserdata (L, lconnection); |
302 |
return 1; |
|
0 | 303 |
} |
304 |
||
305 |
/// main.print_info |
|
306 |
/// Prints a system message to buddy's window. |
|
307 |
/// A: string (jid), string (message) |
|
308 |
static int lua_main_print_info (lua_State *L) |
|
309 |
{ |
|
13 | 310 |
char *jid = to_utf8 (luaL_checkstring (L, 1)); |
311 |
char *to = jidtodisp (jid); |
|
312 |
char *mesg = to_utf8 (luaL_checkstring (L, 2)); |
|
313 |
scr_WriteIncomingMessage (to, mesg, 0, HBB_PREFIX_INFO, 0); |
|
314 |
g_free (mesg); |
|
1 | 315 |
g_free (to); |
13 | 316 |
g_free (jid); |
1 | 317 |
return 0; |
0 | 318 |
} |
319 |
||
320 |
/// main.beep |
|
321 |
/// Beeps with system speaker. |
|
322 |
static int lua_main_beep (lua_State *L) |
|
323 |
{ |
|
1 | 324 |
scr_Beep (); |
325 |
return 0; |
|
0 | 326 |
} |
327 |
||
328 |
/// main.run |
|
329 |
/// Runs specified mcabber command. |
|
330 |
/// A: string |
|
331 |
static int lua_main_run (lua_State *L) |
|
332 |
{ |
|
1 | 333 |
process_command (luaL_checkstring (L, 1), TRUE); |
334 |
return 0; |
|
0 | 335 |
} |
336 |
||
337 |
/// main.status |
|
338 |
/// Returns your current status. |
|
1 | 339 |
/// R: string (status letter), string (status message) |
0 | 340 |
static int lua_main_status (lua_State *L) |
341 |
{ |
|
1 | 342 |
char *sm = from_utf8 (xmpp_getstatusmsg ()); |
343 |
lua_pushlstring (L, &imstatus2char[xmpp_getstatus ()], 1); |
|
344 |
lua_pushstring (L, sm); |
|
345 |
g_free (sm); |
|
346 |
return 2; |
|
0 | 347 |
} |
348 |
||
1 | 349 |
// expects table on top |
0 | 350 |
static void lua_rosterlist_callback (gpointer buddy, lua_State *L) |
351 |
{ |
|
13 | 352 |
char *jid = from_utf8 (buddy_getjid (buddy)); |
1 | 353 |
lua_pushnumber (L, lua_objlen (L, -1) + 1); |
13 | 354 |
lua_pushstring (L, jid); |
1 | 355 |
lua_settable (L, -3); |
13 | 356 |
g_free (jid); |
0 | 357 |
} |
358 |
||
359 |
/// main.roster |
|
360 |
/// Returns array of jids of buddies in roster. |
|
361 |
/// R: table |
|
362 |
static int lua_main_roster (lua_State *L) |
|
363 |
{ |
|
1 | 364 |
lua_newtable (L); |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
365 |
foreach_buddy (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM, (void (*) (gpointer buddy, void *data)) lua_rosterlist_callback, L); |
1 | 366 |
return 1; |
0 | 367 |
} |
368 |
||
369 |
/// main.current_buddy |
|
10 | 370 |
/// Returns jid of current selected buddy or sets current buddy to buddy with specified jid. |
371 |
/// A: string (optional) |
|
372 |
/// R: string (optional) |
|
0 | 373 |
static int lua_main_current_buddy (lua_State *L) |
374 |
{ |
|
10 | 375 |
if (lua_gettop (L) > 0) { // Set |
13 | 376 |
// XXX: we need not convert to utf, RS works on jids/names in locale charset, |
377 |
// but will jidtodisp always correctly work on such tings? |
|
10 | 378 |
char *jid = jidtodisp (luaL_checkstring (L, 1)); |
379 |
scr_RosterSearch (jid); |
|
380 |
g_free (jid); |
|
381 |
return 0; |
|
382 |
} else { // Get |
|
13 | 383 |
char *jid = from_utf8 (buddy_getjid (BUDDATA (current_buddy))); |
384 |
lua_pushstring (L, jid); |
|
385 |
g_free (jid); |
|
10 | 386 |
return 1; |
387 |
} |
|
388 |
} |
|
389 |
||
390 |
/// main.full_jid |
|
391 |
/// Returns full jid (with current resource) of specified buddy (or current, if not specified). |
|
392 |
/// Note, that if there are no resources online, it will return just what it got. |
|
393 |
/// A: string (jid, optional) |
|
394 |
/// R: string (jid) |
|
395 |
static int lua_main_full_jid (lua_State *L) |
|
396 |
{ |
|
40 | 397 |
GList *buddy; |
10 | 398 |
GSList *resources; |
399 |
GSList *resource; |
|
13 | 400 |
if (lua_gettop (L) > 0) { |
401 |
char *jid = from_utf8 (luaL_checkstring (L, 1)); |
|
402 |
buddy = buddy_search_jid (jid); |
|
403 |
g_free (jid); |
|
404 |
} else |
|
10 | 405 |
buddy = current_buddy; |
406 |
if (!buddy) |
|
407 |
return 0; |
|
408 |
resources = buddy_getresources (BUDDATA (buddy)); |
|
409 |
if (!resources) { |
|
13 | 410 |
char *loc = from_utf8 (buddy_getjid (BUDDATA (buddy))); |
411 |
lua_pushstring (L, loc); |
|
412 |
g_free (loc); |
|
413 |
} else { |
|
414 |
char *jid = from_utf8 (buddy_getjid (BUDDATA (buddy))); |
|
415 |
char *res = from_utf8 (g_slist_last (resources)->data); |
|
416 |
lua_pushfstring (L, "%s%c%s", jid, JID_RESOURCE_SEPARATOR, res); |
|
417 |
for (resource = resources; resource; resource = g_slist_next (resource)) |
|
418 |
g_free (resource->data); |
|
419 |
g_slist_free (resources); |
|
420 |
g_free (jid); |
|
421 |
g_free (res); |
|
10 | 422 |
} |
1 | 423 |
return 1; |
0 | 424 |
} |
425 |
||
426 |
typedef struct { |
|
1 | 427 |
lua_State *L; |
428 |
gpointer buddy; |
|
0 | 429 |
} lua_state_and_buddy_t; // :) |
430 |
||
29 | 431 |
/// resources table |
432 |
/// Hash table with resource name as keys and another hash tables as values. |
|
433 |
/// Inner tables contain resource-specific information: priority, status and message. |
|
0 | 434 |
static void lua_buddy_resources_callback (gpointer resource, lua_state_and_buddy_t *d) |
435 |
{ |
|
13 | 436 |
char *loc = from_utf8 (resource); |
437 |
lua_pushstring (d->L, loc); |
|
438 |
g_free (loc); |
|
1 | 439 |
lua_createtable (d->L, 0, 3); |
440 |
lua_pushstring (d->L, "priority"); |
|
441 |
lua_pushnumber (d->L, buddy_getresourceprio (d->buddy, resource)); |
|
442 |
lua_settable (d->L, -3); |
|
443 |
lua_pushstring (d->L, "status"); |
|
444 |
lua_pushlstring (d->L, &imstatus2char[buddy_getstatus (d->buddy, resource)], 1); |
|
445 |
lua_settable (d->L, -3); |
|
446 |
lua_pushstring (d->L, "message"); |
|
13 | 447 |
loc = from_utf8 (buddy_getstatusmsg (d->buddy, resource)); |
448 |
lua_pushstring (d->L, loc); |
|
449 |
g_free (loc); |
|
1 | 450 |
lua_settable (d->L, -3); |
451 |
lua_settable (d->L, -3); |
|
10 | 452 |
g_free (resource); |
0 | 453 |
} |
454 |
||
455 |
/// main.buddy_info |
|
456 |
/// Returns a hash table with information on specified buddy. |
|
29 | 457 |
/// Table contains fields type, name, onserver and resources (which points to resources table). |
0 | 458 |
/// A: string (jid) |
459 |
/// R: table |
|
460 |
static int lua_main_buddy_info (lua_State *L) |
|
461 |
{ |
|
13 | 462 |
char *loc = to_utf8 (luaL_checkstring (L, 1)); |
463 |
char *jid = jidtodisp (loc); |
|
1 | 464 |
GSList *buddy = roster_find (jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM); |
10 | 465 |
GSList *resources; |
1 | 466 |
lua_state_and_buddy_t snb; |
467 |
g_free (jid); |
|
13 | 468 |
g_free (loc); |
1 | 469 |
|
470 |
if (!buddy) { |
|
471 |
lua_pushnil (L); |
|
472 |
return 1; |
|
473 |
} |
|
0 | 474 |
|
1 | 475 |
lua_createtable (L, 0, 3); |
476 |
lua_pushstring (L, "type"); |
|
477 |
luaL_pushenum (L, buddy_gettype (BUDDATA (buddy)), lua_roster_type); |
|
478 |
lua_settable (L, -3); |
|
479 |
lua_pushstring (L, "name"); |
|
13 | 480 |
loc = from_utf8 (buddy_getname (BUDDATA (buddy))); |
481 |
lua_pushstring (L, loc); |
|
482 |
g_free (loc); |
|
1 | 483 |
lua_settable (L, -3); |
484 |
lua_pushstring (L, "onserver"); |
|
485 |
lua_pushboolean (L, buddy_getonserverflag (BUDDATA (buddy))); |
|
486 |
lua_settable (L, -3); |
|
487 |
lua_pushstring (L, "resources"); |
|
488 |
lua_createtable (L, 0, 0); |
|
489 |
snb.L = L; |
|
490 |
snb.buddy = BUDDATA (buddy); |
|
10 | 491 |
resources = buddy_getresources (BUDDATA (buddy)); |
1 | 492 |
g_slist_foreach (buddy_getresources (BUDDATA (buddy)), (GFunc) lua_buddy_resources_callback, &snb); |
10 | 493 |
g_slist_free (resources); |
494 |
lua_settable (L, -3); |
|
1 | 495 |
|
496 |
return 1; |
|
0 | 497 |
} |
498 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
499 |
// XMPP DISCO FEATURES |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
500 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
501 |
GSList *lua_added_features = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
502 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
503 |
/// main.add_feature |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
504 |
/// Adds xmlns to disco#info features list. |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
505 |
/// A: string (xmlns) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
506 |
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
|
507 |
{ |
13 | 508 |
char *xmlns = to_utf8 (luaL_checkstring (L, 1)); |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
509 |
xmpp_add_feature (xmlns); |
13 | 510 |
lua_added_features = g_slist_prepend (lua_added_features, xmlns); |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
511 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
512 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
513 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
514 |
/// main.del_feature |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
515 |
/// Removes xmlns from disco#info features list. |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
516 |
/// A: stirng (xmlns) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
517 |
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
|
518 |
{ |
13 | 519 |
char *xmlns = to_utf8 (luaL_checkstring (L, 1)); |
520 |
GSList *el = g_slist_find_custom (lua_added_features, xmlns, (GCompareFunc) strcmp); |
|
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
521 |
xmpp_del_feature (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
522 |
if (el) { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
523 |
g_free (el->data); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
524 |
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
|
525 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
526 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
527 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
528 |
|
52 | 529 |
// MCABBER EVENTS |
530 |
||
531 |
/// event context |
|
532 |
/// Enum, indicating what exactly caused event function firing. |
|
533 |
/// XXX Well, I am not really understand that EVS_* constants mapping semantics, |
|
534 |
/// so, I just provide data, obtained by experiment. |
|
535 |
/// G: |
|
536 |
static const string2enum_t lua_event_context[] = { |
|
537 |
{ "timeout", EVS_CONTEXT_TIMEOUT }, |
|
538 |
{ "cancel", EVS_CONTEXT_CANCEL }, |
|
539 |
{ "reject", EVS_CONTEXT_USER }, |
|
540 |
{ "accept", EVS_CONTEXT_USER + 1 }, |
|
541 |
{ NULL, 0 }, |
|
542 |
}; |
|
543 |
||
544 |
typedef struct { |
|
545 |
lua_State *L; |
|
546 |
int reference; |
|
547 |
} lua_event_callback_t; |
|
548 |
||
549 |
static GSList *lua_events = NULL; |
|
550 |
||
551 |
/// event function |
|
552 |
/// Function to be called, when some event state change occurs |
|
553 |
/// A: event context |
|
554 |
static int lua_event_callback (eviqs *event, int context) |
|
555 |
{ |
|
556 |
lua_event_callback_t *cb = event->data; |
|
557 |
||
558 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
559 |
luaL_pushenum (cb->L, context, lua_event_context); |
|
560 |
if (lua_pcall (cb->L, 1, 0, 0)) { |
|
561 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Event callback execution error: %s", lua_tostring (cb->L, -1)); |
|
562 |
lua_pop (cb->L, 1); |
|
563 |
} |
|
564 |
||
565 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
566 |
lua_events = g_slist_remove (lua_events, event); |
|
567 |
evs_del (event->id); // XXX |
|
568 |
||
569 |
return 0; // XXX |
|
570 |
} |
|
571 |
||
572 |
/// main.event |
|
573 |
/// Creates new event. If called without arguments, returns event id list. |
|
574 |
/// A: event function (optional), integer (expiration timeout, optional), string (description, optional) |
|
575 |
/// R: string (event id) or nothing (creation error) or table (list of event names) |
|
576 |
static int lua_main_event (lua_State *L) |
|
577 |
{ |
|
578 |
int top = lua_gettop (L); |
|
579 |
if (top > 0) { // Create |
|
580 |
eviqs *event; |
|
581 |
lua_event_callback_t *cb; |
|
582 |
int timeout = 0; |
|
583 |
char *desc = NULL; |
|
584 |
luaL_argcheck (L, lua_type (L, 1) == LUA_TFUNCTION, 1, "event function expected"); |
|
585 |
||
586 |
if (top > 1) { |
|
587 |
timeout = luaL_checkinteger (L, 2); |
|
588 |
if (top > 2) { |
|
589 |
desc = g_strdup (luaL_checkstring (L, 3)); // g_freed by mcabber |
|
590 |
lua_pop (L, 2); |
|
591 |
} else |
|
592 |
lua_pop (L, 1); |
|
593 |
} |
|
594 |
||
595 |
event = evs_new (EVS_TYPE_USER, timeout); |
|
596 |
if (!event) |
|
597 |
return 0; |
|
598 |
||
599 |
lua_pushvalue (L, 1); |
|
600 |
cb = g_new (lua_event_callback_t, 1); // g_freed by mcabber |
|
601 |
cb->L = L; |
|
602 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
603 |
||
604 |
event->data = cb; |
|
605 |
event->callback = lua_event_callback; |
|
606 |
event->desc = desc; |
|
607 |
||
608 |
lua_events = g_slist_prepend (lua_events, event); |
|
609 |
||
610 |
lua_pushstring (L, event->id); |
|
611 |
return 1; |
|
612 |
} else { // List |
|
613 |
GSList *events = evs_geteventslist (0); |
|
614 |
GSList *event; |
|
615 |
||
616 |
lua_newtable (L); |
|
617 |
for (event = events; event; event = g_slist_next (event)) { |
|
618 |
lua_pushstring (L, event->data); |
|
619 |
luaL_ref (L, -2); |
|
620 |
g_free (event->data); |
|
621 |
} |
|
622 |
g_slist_free (events); |
|
623 |
||
624 |
return 1; |
|
625 |
} |
|
626 |
} |
|
627 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
628 |
// MCABBER COMMANDS |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
629 |
|
9 | 630 |
/// completion type |
631 |
/// Built-it completion types can be specified as string, instead of id. |
|
632 |
/// G: |
|
32
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
633 |
static string2enum_t lua_completion_type[] = { // not const, we need to modify yesno |
9 | 634 |
{ "cmd", COMPL_CMD }, |
635 |
{ "jid", COMPL_JID }, |
|
636 |
{ "urljid", COMPL_URLJID }, |
|
637 |
{ "name", COMPL_NAME }, |
|
638 |
{ "status", COMPL_STATUS }, |
|
639 |
{ "filename", COMPL_FILENAME }, |
|
640 |
{ "roster", COMPL_ROSTER }, |
|
641 |
{ "buffer", COMPL_BUFFER }, |
|
642 |
{ "group", COMPL_GROUP }, |
|
643 |
{ "groupname", COMPL_GROUPNAME }, |
|
644 |
{ "multiline", COMPL_MULTILINE }, |
|
645 |
{ "room", COMPL_ROOM }, |
|
646 |
{ "resource", COMPL_RESOURCE }, |
|
647 |
{ "auth", COMPL_AUTH }, |
|
648 |
{ "request", COMPL_REQUEST }, |
|
649 |
{ "events", COMPL_EVENTS }, |
|
650 |
{ "eventsid", COMPL_EVENTSID }, |
|
651 |
{ "pgp", COMPL_PGP }, |
|
652 |
{ "color", COMPL_COLOR }, |
|
653 |
{ "otr", COMPL_OTR }, |
|
654 |
{ "ortpolicy", COMPL_OTRPOLICY }, |
|
32
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
655 |
{ "yesno", 0 }, |
9 | 656 |
{ NULL, 0 }, |
657 |
}; |
|
32
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
658 |
#define MLUA_YESNO_POS ( 21 ) |
9 | 659 |
|
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
660 |
typedef struct { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
661 |
int reference; |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
662 |
int parse_args; |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
663 |
lua_State *L; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
664 |
} lua_command_callback_t; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
665 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
666 |
static GSList *lua_added_commands = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
667 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
668 |
static GSList *lua_added_categories = NULL; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
669 |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
670 |
// returns true if string contains errors - unclosed quotes or unvalued option |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
671 |
/// command arguments table |
27 | 672 |
/// It can parse barewords (with escapes), double-quoted strings (with escapes), single-quoted strings (without escapes), options and arguments. |
24 | 673 |
/// Arguments are separated only by whitespace, so, consequential quoted strings or barewords are one argument. |
27 | 674 |
/// This strings are equal: |
24 | 675 |
/// * ab\ cd\'e\\f\" |
676 |
/// * "ab cd'e\\f\"" |
|
27 | 677 |
/// * 'ab cd'\''e\f"' |
678 |
/// * ab" cd'"'e\f"' |
|
679 |
/// Returned table have option names as keys, option values as values, and arguments as sequential members. -- option is supported. |
|
680 |
/// Example: "-t jid -m 9 -- -aa bb cc" will result in { t = 'jid', m = 9, '-aa', 'bb', 'cc' } |
|
681 |
/// Implementation notes: |
|
682 |
/// * All options should be before any arguments. First non-option argument disables options recognizing. |
|
683 |
/// * EOL is a cutting edge, that can cut much earlier, than you expect. Non-closed quoted strings lose leading quote and option without value loses its leading minus. |
|
684 |
/// * Escape character just before EOL is preserved. |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
685 |
static int luaL_pushargs (lua_State *L, const char *args) |
24 | 686 |
{ |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
687 |
const char *p = args; |
24 | 688 |
luaL_Buffer buf; |
27 | 689 |
int option = 0; |
690 |
int options = 1; |
|
24 | 691 |
|
692 |
lua_newtable (L); |
|
693 |
luaL_buffinit (L, &buf); |
|
694 |
while (*p) { |
|
695 |
if (*p == ' ') { |
|
696 |
++p; |
|
697 |
continue; |
|
698 |
} |
|
699 |
if (*p == '"') { // soft quote |
|
700 |
const char *start = ++p; |
|
701 |
while (*p) { |
|
702 |
if (*p == '\\') { // escape symbol |
|
703 |
luaL_addlstring (&buf, start, p - start); |
|
704 |
start = ++p; |
|
705 |
if (*p) // skip symbol |
|
706 |
++p; |
|
707 |
else // add last \ in line |
|
708 |
luaL_addchar (&buf, '\\'); |
|
709 |
} else if (*p == '"') // quotation end |
|
710 |
break; |
|
711 |
else |
|
712 |
++p; |
|
713 |
} |
|
27 | 714 |
luaL_addlstring (&buf, start, p - start); // XXX: eats quote on eol |
24 | 715 |
if (*p) |
716 |
++p; |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
717 |
else |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
718 |
return 1; |
24 | 719 |
} else if (*p == '\'') { // no-escape quote |
720 |
const char *start = ++p; |
|
721 |
while (*p && *p != '\'') |
|
722 |
p++; |
|
27 | 723 |
luaL_addlstring (&buf, start, p - start); // XXX: eats quote on eol |
24 | 724 |
if (*p) |
725 |
++p; |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
726 |
else |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
727 |
return 1; |
24 | 728 |
} else { // bareword |
729 |
const char *start = p; |
|
730 |
while (*p) { |
|
731 |
if (*p == '\\') { |
|
732 |
luaL_addlstring (&buf, start, p - start); |
|
733 |
start = ++p; |
|
734 |
if (*p) // skip symbol |
|
735 |
++p; |
|
736 |
else // add last \ in line |
|
737 |
luaL_addchar (&buf, '\\'); |
|
738 |
} else if (*p == ' ' || *p == '\'' || *p == '"') |
|
739 |
break; |
|
740 |
else |
|
741 |
++p; |
|
742 |
} |
|
743 |
luaL_addlstring (&buf, start, p - start); |
|
744 |
} |
|
745 |
||
746 |
if ((!*p) || *p == ' ') { |
|
27 | 747 |
const char *result; |
24 | 748 |
luaL_pushresult (&buf); |
27 | 749 |
|
750 |
result = lua_tostring (L, -1); |
|
751 |
if (options && !option && *result == '-') { // option |
|
752 |
if (*(result+1) == '-' && !*(result+2)) { // end of options |
|
753 |
lua_pop (L, 1); |
|
754 |
options = 0; |
|
755 |
} else { // option name |
|
756 |
lua_pushstring (L, result + 1); |
|
757 |
lua_remove (L, -2); |
|
758 |
option = 1; |
|
759 |
} |
|
760 |
} else if (option) { // opion value |
|
761 |
lua_settable (L, -3); |
|
762 |
option = 0; |
|
763 |
} else { // argument |
|
764 |
options = 0; |
|
765 |
luaL_ref (L, -2); |
|
766 |
} |
|
767 |
||
24 | 768 |
luaL_buffinit (L, &buf); |
769 |
} |
|
770 |
} |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
771 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
772 |
if (option) { |
27 | 773 |
luaL_ref (L, -2); // XXX: eats minus on eol |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
774 |
return 1; |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
775 |
} |
27 | 776 |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
777 |
return 0; |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
778 |
} |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
779 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
780 |
/// command function |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
781 |
/// Function to handle newly registered command. |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
782 |
/// Argument type passed depends on how command is registered. |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
783 |
/// A: string (arguments) or command arguments table |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
784 |
static void lua_main_command_handler (char *args, lua_command_callback_t *cb) |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
785 |
{ |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
786 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
787 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
788 |
if (cb->parse_args) |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
789 |
luaL_pushargs (cb->L, args); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
790 |
else |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
791 |
lua_pushstring (cb->L, args); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
792 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
793 |
if (lua_pcall (cb->L, 1, 0, 0)) { |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
794 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Command execution error: %s", lua_tostring (cb->L, -1)); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
795 |
lua_pop (cb->L, 1); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
796 |
} |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
797 |
} |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
798 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
799 |
/// main.parse_args |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
800 |
/// Function to parse command argument string to command arguments table. |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
801 |
/// A: string |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
802 |
/// R: table |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
803 |
static int lua_main_parse_args (lua_State *L) |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
804 |
{ |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
805 |
luaL_pushargs (L, luaL_checkstring (L, 1)); |
24 | 806 |
return 1; |
807 |
} |
|
808 |
||
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
809 |
/// main.add_category |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
810 |
/// Adds completion category. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
811 |
/// A: table (values are used as words for completion, optional) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
812 |
/// R: integer (category id, in fact completion type) or nil |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
813 |
static int lua_main_add_category (lua_State *L) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
814 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
815 |
guint cid; |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
816 |
|
15 | 817 |
if (lua_gettop (L) > 0) { |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
818 |
luaL_argcheck (L, lua_type (L, 1) == LUA_TTABLE, 1, "table expected"); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
819 |
cid = compl_new_category (); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
820 |
if (cid) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
821 |
lua_pushnil (L); |
17
3a12759a22b1
Fix segfault in add_category
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
822 |
while (lua_next (L, 1)) { |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
823 |
char *word = to_utf8 (luaL_checkstring (L, -1)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
824 |
if (word) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
825 |
compl_add_category_word (cid, word); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
826 |
g_free (word); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
827 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
828 |
lua_pop (L, 1); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
829 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
830 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
831 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
832 |
cid = compl_new_category (); |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
833 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
834 |
if (cid) { |
15 | 835 |
lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) cid); |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
836 |
lua_pushinteger (L, cid); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
837 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
838 |
lua_pushnil (L); |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
839 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
840 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
841 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
842 |
|
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
843 |
/// main.del_category |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
844 |
/// Removes completion category. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
845 |
/// A: integer (category id) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
846 |
static int lua_main_del_category (lua_State *L) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
847 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
848 |
guint cid = luaL_checkinteger (L, 1); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
849 |
compl_del_category (cid); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
850 |
lua_added_categories = g_slist_remove (lua_added_categories, (gpointer) cid); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
851 |
return 0; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
852 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
853 |
|
8 | 854 |
/// main.add_completion |
855 |
/// Adds word to a completion list. |
|
856 |
/// A: integer (completion group id), string (word) |
|
857 |
static int lua_main_add_completion (lua_State *L) |
|
858 |
{ |
|
13 | 859 |
guint cid = luaL_checkinteger (L, 1); |
860 |
char *word = to_utf8 (luaL_checkstring (L, 2)); // XXX |
|
8 | 861 |
compl_add_category_word (cid, word); |
13 | 862 |
g_free (word); |
8 | 863 |
} |
864 |
||
865 |
/// main.del_completion |
|
866 |
/// Removes word from a completion list. |
|
867 |
/// A: integer (completion group id), string (word) |
|
868 |
static int lua_main_del_completion (lua_State *L) |
|
869 |
{ |
|
13 | 870 |
guint cid = luaL_checkinteger (L, 1); |
871 |
char *word = to_utf8 (luaL_checkstring (L, 2)); // XXX |
|
8 | 872 |
compl_del_category_word (cid, word); |
13 | 873 |
g_free (word); |
8 | 874 |
} |
875 |
||
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
876 |
/// main.command |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
877 |
/// Associates or breaks association between mcabber command name and lua function. |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
878 |
/// To unregister command omit function argument. |
8 | 879 |
/// If you specify a third argument, table (even empty), function will return completion group id or nothing. |
9 | 880 |
/// You can also specify a string name (see completion type) instead of table, for non-builtin, you can just pass integer id. |
8 | 881 |
/// Note, that for now there are no way to unregister completion group, so, resources can be exausted easily. |
882 |
/// Also note, that it ignores keys in a completion table. |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
883 |
/// A: string (command name), command function (optional), boolean (parse args flag, optional), table (completions, optional)/completion type (or integer comletion group id, optional) |
9 | 884 |
/// R: completion type (integer completion group id or string for builtin types, optional) |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
885 |
static int lua_main_command (lua_State *L) |
39 | 886 |
{ |
13 | 887 |
const char *name = luaL_checkstring (L, 1); // XXX: to_utf8? looks like no :/ |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
888 |
lua_command_callback_t *cb; |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
889 |
int top = lua_gettop (L); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
890 |
if (top > 1) { // Register |
8 | 891 |
guint cid = 0; |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
892 |
int parse = 0; |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
893 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
894 |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
895 |
if (top > 2) { // parse flag provided |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
896 |
parse = lua_toboolean (L, 3); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
897 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
898 |
if (top > 3) { // Completions provided |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
899 |
if (lua_type (L, 4) == LUA_TTABLE) { |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
900 |
cid = compl_new_category (); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
901 |
if (cid) { |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
902 |
lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) cid); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
903 |
lua_pushnil (L); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
904 |
while (lua_next (L, 4)) { |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
905 |
char *word = to_utf8 (luaL_checkstring (L, -1)); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
906 |
compl_add_category_word (cid, word); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
907 |
lua_pop (L, 1); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
908 |
g_free (word); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
909 |
} |
8 | 910 |
} |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
911 |
} else |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
912 |
cid = luaL_checkenum (L, 4, lua_completion_type); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
913 |
} |
8 | 914 |
} |
915 |
||
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
916 |
cb = luaL_malloc (L, sizeof (lua_command_callback_t)); |
8 | 917 |
lua_pushvalue (L, 2); |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
918 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
919 |
cb->parse_args = parse; |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
920 |
cb->L = L; |
8 | 921 |
cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb); |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
922 |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
923 |
lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name)); |
8 | 924 |
|
925 |
if (cid) { |
|
9 | 926 |
luaL_pushenum (L, cid, lua_completion_type); |
8 | 927 |
return 1; |
928 |
} |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
929 |
} else { // Unregister |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
930 |
GSList *el = g_slist_find_custom (lua_added_commands, name, (GCompareFunc) g_strcmp0); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
931 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
932 |
cb = cmd_del (name); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
933 |
if (cb) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
934 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
935 |
luaL_free (cb->L, cb); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
936 |
} |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
937 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
938 |
if (el) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
939 |
g_free (el->data); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
940 |
lua_added_commands = g_slist_delete_link (lua_added_commands, el); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
941 |
} |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
942 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
943 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
944 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
945 |
|
0 | 946 |
// TIMER |
947 |
||
948 |
typedef struct { |
|
1 | 949 |
int reference; |
29 | 950 |
guint source; |
1 | 951 |
lua_State *L; |
0 | 952 |
} lua_timer_callback_t; |
953 |
||
29 | 954 |
static GSList *lua_timers = NULL; |
955 |
||
0 | 956 |
static void lua_timer_callback_destroy (lua_timer_callback_t *cb) |
957 |
{ |
|
1 | 958 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
29 | 959 |
lua_timers = g_slist_remove (lua_timers, (gpointer) cb->source); |
1 | 960 |
luaL_free (cb->L, cb); |
0 | 961 |
} |
962 |
||
963 |
/// timer function |
|
964 |
/// Function, that will be called periodically until it returns false. |
|
965 |
/// R: boolean |
|
966 |
static gboolean lua_timer_callback (lua_timer_callback_t *cb) |
|
967 |
{ |
|
1 | 968 |
int ret; |
969 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
970 |
if (lua_pcall (cb->L, 0, 1, 0)) { |
|
971 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Timer callback execution error: %s", lua_tostring (cb->L, -1)); |
|
972 |
lua_pop (cb->L, 1); |
|
973 |
return FALSE; |
|
974 |
} |
|
975 |
ret = lua_toboolean (cb->L, -1); |
|
976 |
lua_pop (cb->L, 1); |
|
977 |
return ret; |
|
0 | 978 |
} |
979 |
||
980 |
/// main.timer |
|
981 |
/// Creates new timer function, that will be called periodically. |
|
982 |
/// A: integer (interval, seconds), timer function |
|
983 |
static int lua_main_timer (lua_State *L) |
|
984 |
{ |
|
1 | 985 |
int interval = luaL_checkint (L, 1); |
29 | 986 |
guint source; |
1 | 987 |
lua_timer_callback_t *cb; |
988 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
0 | 989 |
|
1 | 990 |
cb = luaL_malloc (L, sizeof (lua_timer_callback_t)); |
991 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
992 |
cb->L = L; |
|
0 | 993 |
|
30
2cf24aced294
Definitions moved to cmake options
Myhailo Danylenko <isbear@ukrpost.net>
parents:
29
diff
changeset
|
994 |
source = g_timeout_add_seconds_full (MLUA_SOURCE_PRIORITY, interval, (GSourceFunc) lua_timer_callback, cb, (GDestroyNotify) lua_timer_callback_destroy); |
29 | 995 |
cb->source = source; |
996 |
lua_timers = g_slist_prepend (lua_timers, (gpointer) source); |
|
997 |
||
1 | 998 |
return 0; |
0 | 999 |
} |
1000 |
||
1001 |
// BACKGROUND PIPE READING |
|
1002 |
||
1003 |
typedef struct { |
|
1 | 1004 |
lua_State *L; |
1005 |
FILE *fd; |
|
1006 |
int reference; |
|
0 | 1007 |
} lua_bgread_callback_t; |
1008 |
||
30
2cf24aced294
Definitions moved to cmake options
Myhailo Danylenko <isbear@ukrpost.net>
parents:
29
diff
changeset
|
1009 |
static gchar lua_bgread_buffer[MLUA_BGREAD_BUFFER]; |
0 | 1010 |
|
1011 |
static void lua_bgread_callback_destroy (lua_bgread_callback_t *cb) |
|
1012 |
{ |
|
1 | 1013 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
1014 |
pclose (cb->fd); |
|
1015 |
luaL_free (cb->L, cb); |
|
0 | 1016 |
} |
1017 |
||
1018 |
/// background reading function |
|
1019 |
/// Function, that processes output from pipe in asynchroneous way. |
|
1020 |
/// A: string (data) or nil (eof) |
|
1021 |
/// R: boolean (false if reading should be terminated) |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1022 |
static gboolean lua_bgread_callback (GIOChannel *source, GIOCondition condition, lua_bgread_callback_t *cb) |
0 | 1023 |
{ |
1 | 1024 |
int ret = TRUE; |
0 | 1025 |
|
1 | 1026 |
if (condition | G_IO_IN) { // data |
1027 |
while (TRUE) { |
|
1028 |
gsize read = 0; |
|
30
2cf24aced294
Definitions moved to cmake options
Myhailo Danylenko <isbear@ukrpost.net>
parents:
29
diff
changeset
|
1029 |
g_io_channel_read_chars (source, lua_bgread_buffer, MLUA_BGREAD_BUFFER, &read, NULL); |
1 | 1030 |
if (!read) // exausted |
1031 |
break; |
|
0 | 1032 |
|
1 | 1033 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
1034 |
lua_pushlstring (cb->L, lua_bgread_buffer, read); |
|
1035 |
if (lua_pcall (cb->L, 1, 1, 0)) { |
|
1036 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1)); |
|
1037 |
lua_pop (cb->L, 1); |
|
1038 |
return FALSE; |
|
1039 |
} |
|
1040 |
ret = lua_toboolean (cb->L, -1); |
|
1041 |
lua_pop (cb->L, 1); |
|
1042 |
if (!ret) // enough |
|
1043 |
return FALSE; |
|
1044 |
} |
|
1045 |
} |
|
0 | 1046 |
|
1 | 1047 |
if (condition & ~G_IO_IN) { // err or hup |
1048 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
1049 |
lua_pushnil (cb->L); |
|
1050 |
if (lua_pcall (cb->L, 1, 1, 0)) { |
|
1051 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1)); |
|
1052 |
lua_pop (cb->L, 1); |
|
1053 |
return FALSE; |
|
1054 |
} |
|
1055 |
ret = lua_toboolean (cb->L, -1); |
|
1056 |
lua_pop (cb->L, 1); |
|
1057 |
} |
|
0 | 1058 |
|
1 | 1059 |
return ret; |
0 | 1060 |
} |
1061 |
||
1062 |
/// main.bgread |
|
1 | 1063 |
/// Runs specified command and passes its output to a given function. |
0 | 1064 |
/// A: string (command), background reading function |
1065 |
static int lua_main_bgread (lua_State *L) |
|
1066 |
{ |
|
1 | 1067 |
const char *command = luaL_checkstring (L, 1); |
1068 |
lua_bgread_callback_t *cb; |
|
1069 |
FILE *fd; |
|
1070 |
GIOChannel *channel; |
|
1071 |
const char *charset = NULL; |
|
1072 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
0 | 1073 |
|
1 | 1074 |
fd = popen (command, "r"); |
1075 |
if (!fd) { |
|
1076 |
lua_pushstring (L, "Error opening pipe"); |
|
1077 |
lua_error (L); |
|
1078 |
} |
|
0 | 1079 |
|
1 | 1080 |
channel = g_io_channel_unix_new (fileno (fd)); |
1081 |
// We, most likely, need this, |
|
1082 |
// But we cannot use this, |
|
1083 |
// It will block. |
|
1084 |
//if (!g_get_charset (&charset)) |
|
1085 |
g_io_channel_set_encoding (channel, charset, NULL); |
|
1086 |
g_io_channel_set_buffered (channel, FALSE); |
|
1087 |
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL); |
|
1088 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
0 | 1089 |
|
1 | 1090 |
cb = luaL_malloc (L, sizeof (lua_bgread_callback_t)); |
1091 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
1092 |
cb->L = L; |
|
1093 |
cb->fd = fd; |
|
0 | 1094 |
|
30
2cf24aced294
Definitions moved to cmake options
Myhailo Danylenko <isbear@ukrpost.net>
parents:
29
diff
changeset
|
1095 |
g_io_add_watch_full (channel, MLUA_SOURCE_PRIORITY, G_IO_IN|G_IO_HUP|G_IO_ERR, (GIOFunc) lua_bgread_callback, cb, (GDestroyNotify) lua_bgread_callback_destroy); |
1 | 1096 |
return 0; |
0 | 1097 |
} |
1098 |
||
1099 |
// MAIN INITIALIZATION CODE |
|
1100 |
||
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1101 |
#ifdef LLM_LOG_HANDLER |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1102 |
// FIXME: this should not be here |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1103 |
guint lua_lm_log_handler_id; |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1104 |
|
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1105 |
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
|
1106 |
{ |
29 | 1107 |
if (settings_opt_get_int ("lua_lm_debug")) |
1108 |
scr_LogPrint (LPRINT_LOGNORM, "%s: %s", domain, message); |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1109 |
} |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1110 |
#endif |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1111 |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1112 |
static void do_lua(char *arg, lua_State *L) |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1113 |
{ |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1114 |
if (luaL_loadbuffer (L, arg, strlen (arg), "line")) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1115 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Compilation error: %s", lua_tostring (L, -1)); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1116 |
lua_pop (L, 1); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1117 |
return; |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1118 |
} |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1119 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1120 |
if (lua_pcall (L, 0, 0, 0)) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1121 |
scr_LogPrint (LPRINT_NORMAL, "lua: Runtime error: %s", lua_tostring(lua, -1)); |
8 | 1122 |
lua_pop (L, 1); |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1123 |
return; |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1124 |
} |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1125 |
} |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1126 |
|
0 | 1127 |
static void lua_hook (hk_arg_t *args, lua_State *L) |
1128 |
{ |
|
40 | 1129 |
hk_arg_t *arg = args; |
6 | 1130 |
const char *hook = settings_opt_get ("lua_hook_function"); |
1131 |
if (!hook) |
|
1132 |
return; |
|
1133 |
lua_getglobal (lua, hook); |
|
1 | 1134 |
if (!lua_isfunction (lua, -1)) { |
1135 |
lua_pop (lua, 1); |
|
1136 |
return; |
|
1137 |
} |
|
1138 |
lua_newtable (L); |
|
1139 |
while (arg->name != NULL) { |
|
13 | 1140 |
char *name = from_utf8 (arg->name); |
1141 |
char *value = from_utf8 (arg->value); |
|
1142 |
lua_pushstring (L, name); |
|
1143 |
lua_pushstring (L, value); |
|
1 | 1144 |
lua_settable (L, -3); |
13 | 1145 |
g_free (name); |
1146 |
g_free (value); |
|
1 | 1147 |
arg++; |
1148 |
} |
|
1149 |
if (lua_pcall (lua, 1, 0, 0)) { |
|
6 | 1150 |
scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook handler: %s", lua_tostring (lua, -1)); |
1 | 1151 |
lua_pop (lua, 1); |
1152 |
} |
|
1153 |
} |
|
1154 |
||
1155 |
static void *lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { |
|
1156 |
if (nsize == 0) { |
|
1157 |
g_free (ptr); |
|
1158 |
return NULL; |
|
1159 |
} else |
|
1160 |
return g_realloc (ptr, nsize); |
|
0 | 1161 |
} |
1162 |
||
10 | 1163 |
#define reg(NAME) \ |
1164 |
{ #NAME, lua_main_##NAME }, |
|
0 | 1165 |
static const luaL_Reg lua_reg_main[] = { |
40 | 1166 |
reg ( yesno ) |
1167 |
reg ( connection ) |
|
1168 |
reg ( log ) |
|
1169 |
reg ( option ) |
|
1170 |
reg ( alias ) |
|
1171 |
reg ( binding ) |
|
1172 |
reg ( fileoption ) |
|
1173 |
reg ( add_feature ) |
|
1174 |
reg ( del_feature ) |
|
52 | 1175 |
reg ( event ) |
40 | 1176 |
reg ( parse_args ) |
1177 |
reg ( add_category ) |
|
1178 |
reg ( del_category ) |
|
1179 |
reg ( add_completion ) |
|
1180 |
reg ( del_completion ) |
|
1181 |
reg ( command ) |
|
1182 |
reg ( print_info ) |
|
1183 |
reg ( beep ) |
|
1184 |
reg ( run ) |
|
1185 |
reg ( status ) |
|
1186 |
reg ( roster ) |
|
1187 |
reg ( current_buddy ) |
|
1188 |
reg ( full_jid ) |
|
1189 |
reg ( buddy_info ) |
|
1190 |
reg ( timer ) |
|
1191 |
reg ( bgread ) |
|
10 | 1192 |
{ NULL, NULL }, |
0 | 1193 |
}; |
10 | 1194 |
#undef reg |
1195 |
||
0 | 1196 |
const gchar *g_module_check_init (GModule *module) |
1197 |
{ |
|
1 | 1198 |
lua = lua_newstate (lua_alloc, NULL); |
1199 |
if (!lua) { |
|
1200 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Initialization error"); |
|
1201 |
return "Lua initialization error"; |
|
1202 |
} |
|
0 | 1203 |
|
1 | 1204 |
luaL_openlibs (lua); |
0 | 1205 |
|
1 | 1206 |
luaL_register (lua, "main", lua_reg_main); |
1207 |
lua_pop (lua, 1); // XXX |
|
1208 |
lua_register (lua, "dopath", lua_global_dopath); |
|
1209 |
lua_register (lua, "print", lua_global_print ); |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1210 |
|
32
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1211 |
{ |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1212 |
int cid = compl_new_category (); |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1213 |
|
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1214 |
if (cid) { |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1215 |
const string2enum_t *word = lua_yesno; |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1216 |
lua_completion_type[MLUA_YESNO_POS].value = cid; |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1217 |
lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) cid); |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1218 |
while (word->string) { |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1219 |
compl_add_category_word (cid, word->string); |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1220 |
++word; |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1221 |
} |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1222 |
} |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1223 |
} |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1224 |
|
11 | 1225 |
cmd_add ("lua", "Evaluate lua string", 0, 0, (void (*) (char *p)) do_lua, lua); |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1226 |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1227 |
#ifdef LLM_LOG_HANDLER |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1228 |
// FIXME: this should not be here. |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1229 |
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
|
1230 |
#endif |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1231 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1232 |
#if 1 |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1233 |
{ |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1234 |
char *initfile = expand_filename (settings_opt_get ("lua_init_filename")); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1235 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1236 |
if (!initfile) |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1237 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Cannot determine config file name"); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1238 |
else { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1239 |
if (luaL_loadfile(lua, initfile)) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1240 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Unable to compile rc file: %s", lua_tostring (lua, -1)); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1241 |
lua_pop (lua, 1); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1242 |
} else if (lua_pcall (lua, 0, LUA_MULTRET, 0)) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1243 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Runtime error in rc file: %s", lua_tostring(lua, -1)); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1244 |
lua_pop (lua, 1); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1245 |
} else |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1246 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Loaded %s", initfile); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1247 |
g_free (initfile); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1248 |
} |
1 | 1249 |
} |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1250 |
#endif |
0 | 1251 |
|
1 | 1252 |
hk_add_handler ((hk_handler_t) lua_hook, lua); |
0 | 1253 |
|
6 | 1254 |
{ |
1255 |
hk_arg_t args[] = { |
|
1256 |
{ "hook", "hook-start" }, |
|
1257 |
{ NULL, NULL }, |
|
1258 |
}; |
|
1259 |
lua_hook (args, lua); |
|
1 | 1260 |
} |
6 | 1261 |
|
1 | 1262 |
return NULL; |
0 | 1263 |
} |
1264 |
||
52 | 1265 |
static void lua_events_destroy (eviqs *event, gpointer ignore) |
1266 |
{ |
|
1267 |
lua_event_callback_t *cb = event->data; |
|
1268 |
evs_callback (event->id, EVS_CONTEXT_CANCEL); // ignore |
|
1269 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
1270 |
evs_del (event->id); |
|
1271 |
} |
|
1272 |
||
29 | 1273 |
static void lua_timers_destroy (guint source, gpointer ignore) |
1274 |
{ |
|
1275 |
g_source_remove (source); |
|
1276 |
} |
|
1277 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1278 |
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
|
1279 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1280 |
xmpp_del_feature (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1281 |
g_free (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1282 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1283 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1284 |
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
|
1285 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1286 |
lua_command_callback_t *cb = cmd_del (name); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1287 |
if (cb) { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1288 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1289 |
luaL_free (cb->L, cb); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1290 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1291 |
g_free (name); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1292 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1293 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1294 |
static void lua_categories_destroy (guint id, gpointer ignore) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1295 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1296 |
compl_del_category (id); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1297 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1298 |
|
0 | 1299 |
void g_module_unload (GModule *module) |
1300 |
{ |
|
1 | 1301 |
if (lua) { |
6 | 1302 |
hk_arg_t args[] = { |
1303 |
{ "hook", "hook-quit" }, |
|
1304 |
{ NULL, NULL }, |
|
1305 |
}; |
|
1306 |
lua_hook (args, lua); |
|
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1307 |
|
1 | 1308 |
hk_del_handler ((hk_handler_t) lua_hook, lua); |
0 | 1309 |
|
29 | 1310 |
g_slist_foreach (lua_timers, (GFunc) lua_timers_destroy, NULL); |
1311 |
g_slist_free (lua_timers); |
|
1312 |
lua_timers = NULL; |
|
1313 |
||
52 | 1314 |
g_slist_foreach (lua_events, (GFunc) lua_events_destroy, NULL); |
1315 |
g_slist_free (lua_events); |
|
1316 |
lua_events = NULL; |
|
1317 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1318 |
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
|
1319 |
g_slist_free (lua_added_features); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1320 |
lua_added_features = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1321 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1322 |
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
|
1323 |
g_slist_free (lua_added_commands); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1324 |
lua_added_commands = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1325 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1326 |
g_slist_foreach (lua_added_categories, (GFunc) lua_categories_destroy, NULL); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1327 |
g_slist_free (lua_added_categories); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1328 |
lua_added_categories = NULL; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1329 |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1330 |
cmd_del ("lua"); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1331 |
|
1 | 1332 |
lua_close (lua); |
1333 |
lua = NULL; |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1334 |
|
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1335 |
#ifdef LLM_LOG_HANDLER |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1336 |
// FIXME: shouldn't be here |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1337 |
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
|
1338 |
#endif |
1 | 1339 |
} |
0 | 1340 |
} |
1341 |