author | Myhailo Danylenko <isbear@ukrpost.net> |
Tue, 21 Aug 2012 15:32:19 +0300 | |
changeset 129 | ea30cf6c5bea |
parent 128 | 8692a6fbe415 |
child 132 | 2827e775d5fa |
permissions | -rw-r--r-- |
103 | 1 |
|
113 | 2 |
/* Copyright 2009,2010 Myhailo Danylenko |
3 |
* Copyright 2011 Mikael Berthe |
|
103 | 4 |
|
5 |
This file is part of mcabber-lua. |
|
6 |
||
7 |
mcabber-lua is free software: you can redistribute it and/or modify |
|
8 |
it under the terms of the GNU General Public License as published by |
|
9 |
the Free Software Foundation, either version 2 of the License, or |
|
10 |
(at your option) any later version. |
|
11 |
||
12 |
This program is distributed in the hope that it will be useful, |
|
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 |
GNU General Public License for more details. |
|
16 |
||
17 |
You should have received a copy of the GNU General Public License |
|
18 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
19 |
||
20 |
#include <glib.h> |
|
21 |
#include <gmodule.h> // g_module_* |
|
22 |
#include <lua.h> |
|
23 |
#include <lauxlib.h> |
|
24 |
#include <lualib.h> |
|
25 |
#include <stdio.h> |
|
26 |
#include <stdlib.h> // getenv |
|
27 |
#include <string.h> // strcmp |
|
28 |
||
29 |
#include <mcabber/logprint.h> // scr_log_print |
|
30 |
#include <mcabber/screen.h> // scr_Beep, scr_WriteIncomingMessage |
|
31 |
#include <mcabber/hbuf.h> // HBB_PREFIX_INFO |
|
32 |
#include <mcabber/commands.h> // process_command, cmd_add, cmd_del |
|
33 |
#include <mcabber/xmpp.h> // xmpp_getstatus, xmpp_getstatusmsg, lconnection |
|
34 |
#include <mcabber/xmpp_helper.h> // xmpp_add_feature, xmpp_del_feature |
|
35 |
#include <mcabber/roster.h> // imstatus2char, foreach_buddy, buddy_*, current_buddy, BUDDATA, ROSTER_TYPE_* |
|
36 |
#include <mcabber/utils.h> // from_utf8, jidtodisp |
|
37 |
#include <mcabber/hooks.h> // hk_add_handler, hk_del_handler |
|
38 |
#include <mcabber/settings.h> // settings_set, settings_del, settings_get |
|
39 |
#include <mcabber/compl.h> // compl_new_category, compl_add_category_word, compl_del_category_word |
|
40 |
#include <mcabber/events.h> // evs_* |
|
118
1ad8103b72d6
Revert previous commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
117
diff
changeset
|
41 |
#include <mcabber/modules.h> // module_info_t |
1ad8103b72d6
Revert previous commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
117
diff
changeset
|
42 |
#include <mcabber/api.h> // mcabber_branch, mcabber_api_version, MCABBER_API_HAVE_CMD_ID |
107 | 43 |
#include <mcabber/main.h> // mcabber_version |
103 | 44 |
|
45 |
#include "config.h" |
|
46 |
#include "util.h" |
|
47 |
||
128
8692a6fbe415
Add headers for docs
Myhailo Danylenko <isbear@ukrpost.net>
parents:
125
diff
changeset
|
48 |
/// Lua for mcabber |
8692a6fbe415
Add headers for docs
Myhailo Danylenko <isbear@ukrpost.net>
parents:
125
diff
changeset
|
49 |
/// Module provides embedded lua environment with some accessors to |
8692a6fbe415
Add headers for docs
Myhailo Danylenko <isbear@ukrpost.net>
parents:
125
diff
changeset
|
50 |
/// mcabber functionality. |
8692a6fbe415
Add headers for docs
Myhailo Danylenko <isbear@ukrpost.net>
parents:
125
diff
changeset
|
51 |
|
121
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
52 |
// |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
53 |
// options |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
54 |
// |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
55 |
|
128
8692a6fbe415
Add headers for docs
Myhailo Danylenko <isbear@ukrpost.net>
parents:
125
diff
changeset
|
56 |
/// Options |
8692a6fbe415
Add headers for docs
Myhailo Danylenko <isbear@ukrpost.net>
parents:
125
diff
changeset
|
57 |
/// * lua_init_filename - lua script, that will be loaded at startup |
8692a6fbe415
Add headers for docs
Myhailo Danylenko <isbear@ukrpost.net>
parents:
125
diff
changeset
|
58 |
/// * lua_lm_debug - if lm log handler is enabled, this option controls, whether lm log messages are dropped or passed to mcabber logging facility |
121
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
59 |
#define OPT_MLUA_RC "lua_init_filename" |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
60 |
#define OPT_MLUA_LM_DEBUG "lua_lm_debug" |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
61 |
|
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
62 |
// |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
63 |
// module description |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
64 |
// |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
65 |
|
115
47bed161e3b0
Add AVV description file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
113
diff
changeset
|
66 |
void mlua_init (void); |
47bed161e3b0
Add AVV description file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
113
diff
changeset
|
67 |
void mlua_uninit (void); |
103 | 68 |
|
69 |
#ifdef LLM_LOG_HANDLER |
|
70 |
#define DESCRIPTION ( \ |
|
71 |
"Lua scripting interface\n" \ |
|
121
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
72 |
"Options: " OPT_MLUA_RC ", " OPT_MLUA_LM_DEBUG "\n" \ |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
73 |
"Command: /lua" ) |
103 | 74 |
#else |
75 |
#define DESCRIPTION ( \ |
|
76 |
"Lua scripting interface\n" \ |
|
121
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
77 |
"Options: " OPT_MLUA_RC "\n" \ |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
78 |
"Command: /lua" ) |
103 | 79 |
#endif |
80 |
||
122
9a803cc75245
Use build-time api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
121
diff
changeset
|
81 |
module_info_t info_lua = { |
9a803cc75245
Use build-time api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
121
diff
changeset
|
82 |
.branch = MCABBER_BRANCH, |
9a803cc75245
Use build-time api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
121
diff
changeset
|
83 |
.api = MCABBER_API_VERSION, |
103 | 84 |
.version = PROJECT_VERSION, |
85 |
.description = DESCRIPTION, |
|
86 |
.requires = NULL, |
|
87 |
.init = mlua_init, |
|
88 |
.uninit = mlua_uninit, |
|
89 |
.next = NULL, |
|
90 |
}; |
|
91 |
||
121
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
92 |
// |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
93 |
// globals |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
94 |
// |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
95 |
|
113 | 96 |
#ifdef MCABBER_API_HAVE_CMD_ID |
97 |
static gpointer lua_cmdid; |
|
98 |
#endif |
|
99 |
||
103 | 100 |
// global lua state object, necessary for uninitialization function |
101 |
static lua_State *lua = NULL; |
|
102 |
||
121
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
103 |
// |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
104 |
// code |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
105 |
// |
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
106 |
|
103 | 107 |
// caller sould g_free result |
108 |
static char *mcabber_config_filename (const char *file) |
|
109 |
{ |
|
110 |
const char *home = getenv ("HOME"); |
|
111 |
if (!home) |
|
112 |
return NULL; |
|
113 |
return g_strconcat (home, "/.mcabber/", file ? file : "", NULL); |
|
114 |
} |
|
115 |
||
116 |
/// print |
|
117 |
/// Prints its arguments to log with default priority. |
|
118 |
/// A: something, ... |
|
119 |
static int lua_global_print (lua_State *L) |
|
120 |
{ |
|
121 |
int top = lua_gettop (L); |
|
122 |
int i; |
|
123 |
luaL_Buffer B; |
|
124 |
luaL_buffinit (L, &B); |
|
125 |
for (i = 1; i <= top; i++) { |
|
126 |
int type = lua_type (L, i); |
|
127 |
if (i > 1) |
|
128 |
luaL_addchar (&B, '\t'); |
|
129 |
if (type == LUA_TSTRING) { |
|
130 |
size_t len; |
|
131 |
const char *str = lua_tolstring (L, i, &len); |
|
132 |
luaL_addlstring (&B, str, len); |
|
133 |
} else if (type == LUA_TNUMBER) |
|
134 |
luaL_addstring (&B, lua_tostring (L, i)); // XXX: modifies |
|
135 |
else if (type == LUA_TBOOLEAN) { |
|
136 |
if (lua_toboolean (L, i)) |
|
137 |
luaL_addstring (&B, "true"); |
|
138 |
else |
|
139 |
luaL_addstring (&B, "false"); |
|
140 |
} else if (type == LUA_TNIL) |
|
141 |
luaL_addstring (&B, "nil"); |
|
142 |
else { |
|
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
143 |
char xbuf[32]; |
103 | 144 |
luaL_addstring (&B, luaL_typename (L, i)); |
145 |
luaL_addstring (&B, ": 0x"); |
|
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
146 |
snprintf (&xbuf[0], 32, "%p", lua_topointer (L, i)); |
103 | 147 |
luaL_addlstring (&B, xbuf, 8); // XXX |
148 |
} |
|
149 |
} |
|
150 |
luaL_pushresult (&B); |
|
151 |
||
152 |
scr_log_print (LPRINT_LOGNORM | LPRINT_NOTUTF8, lua_tostring (L, -1)); |
|
153 |
return 0; |
|
154 |
} |
|
155 |
||
156 |
/// dopath |
|
157 |
/// Loads lua file from default location. |
|
158 |
/// XXX: g_filename_from_utf8? |
|
159 |
/// A: string (filename, without ".lua") |
|
160 |
/// R: string (error message, optional) |
|
161 |
static int lua_global_dopath (lua_State *L) |
|
162 |
{ |
|
163 |
const char *name = luaL_checkstring (L, 1); |
|
164 |
size_t size = lua_objlen (L, 1); |
|
165 |
char *path; |
|
166 |
int ret = 0; |
|
167 |
if (size > 4 && !strncmp (name + size - 4, ".lua", 4)) |
|
168 |
path = mcabber_config_filename (name); |
|
169 |
else { |
|
170 |
char *fname = g_strconcat (name, ".lua", NULL); |
|
171 |
path = mcabber_config_filename (fname); |
|
172 |
g_free (fname); |
|
173 |
} |
|
174 |
||
175 |
if ((ret = luaL_loadfile (L, path))) |
|
176 |
scr_log_print (LPRINT_LOGNORM, "lua: Unable to compile file %s: %s", path, lua_tostring (L, -1)); |
|
177 |
else if ((ret = lua_pcall (L, 0, LUA_MULTRET, 0))) |
|
178 |
scr_log_print (LPRINT_LOGNORM, "lua: Runtime error in file %s: %s", path, lua_tostring (L, -1)); |
|
179 |
g_free (path); |
|
180 |
||
181 |
if (ret) |
|
182 |
return 1; |
|
183 |
else |
|
184 |
return 0; |
|
185 |
} |
|
186 |
||
187 |
/// yes or no ansvers |
|
188 |
/// G: |
|
189 |
static const string2enum_t lua_yesno[] = { |
|
190 |
{ "1", 1 }, |
|
191 |
{ "0", 0 }, |
|
192 |
{ "enable", 1 }, |
|
193 |
{ "disable", 0 }, |
|
194 |
{ "true", 1 }, |
|
195 |
{ "false", 0 }, |
|
196 |
{ "on", 1 }, |
|
197 |
{ "off", 0 }, |
|
198 |
{ "yes", 1 }, |
|
199 |
{ "no", 0 }, |
|
200 |
{ NULL, -1 }, |
|
201 |
}; |
|
202 |
||
203 |
/// main.yesno |
|
204 |
/// According to yes or no ansvers returns true or false. |
|
205 |
/// If ansver is not recognized, returns nil. |
|
206 |
/// A: anything (string expected) |
|
207 |
/// R: boolean or nil |
|
208 |
static int lua_main_yesno (lua_State *L) |
|
209 |
{ |
|
210 |
int type = lua_type (L, 1); |
|
211 |
if (type == LUA_TSTRING) { |
|
212 |
int ret = luaL_checkenum (L, 1, lua_yesno); |
|
213 |
if (ret == -1) |
|
214 |
lua_pushnil (L); |
|
215 |
else |
|
216 |
lua_pushboolean (L, ret); |
|
217 |
} else if (type == LUA_TNUMBER) |
|
218 |
lua_pushboolean (L, lua_tointeger (L, 1)); |
|
219 |
else if (type != LUA_TBOOLEAN) |
|
220 |
lua_pushnil (L); |
|
221 |
return 1; |
|
222 |
} |
|
223 |
||
107 | 224 |
/// main.version |
225 |
/// Returns information about mcabber version |
|
226 |
/// R: table |
|
227 |
static int lua_main_version (lua_State *L) |
|
228 |
{ |
|
229 |
lua_createtable (L, 0, 3); |
|
230 |
lua_pushstring (L, mcabber_branch); |
|
231 |
lua_setfield (L, -2, "branch"); |
|
232 |
lua_pushinteger (L, mcabber_api_version); |
|
233 |
lua_setfield (L, -2, "api"); |
|
234 |
{ |
|
235 |
gchar *version = mcabber_version (); |
|
236 |
lua_pushstring (L, version); |
|
237 |
g_free (version); |
|
238 |
} |
|
239 |
lua_setfield (L, -2, "version"); |
|
240 |
lua_pushliteral (L, MCABBER_BRANCH); |
|
241 |
lua_setfield (L, -2, "build_branch"); |
|
242 |
lua_pushinteger (L, MCABBER_API_VERSION); |
|
243 |
lua_setfield (L, -2, "build_api"); |
|
244 |
lua_pushinteger (L, MCABBER_API_MIN); |
|
245 |
lua_setfield (L, -2, "build_api_min"); |
|
246 |
return 1; |
|
247 |
} |
|
248 |
||
103 | 249 |
/// log print type |
250 |
/// G: |
|
251 |
static const string2enum_t lua_lprint[] = { |
|
252 |
{ "normal", LPRINT_NORMAL }, |
|
253 |
{ "log", LPRINT_LOG }, |
|
254 |
{ "debug", LPRINT_DEBUG }, |
|
255 |
{ "notutf0", LPRINT_NOTUTF8 }, |
|
256 |
{ NULL, 0 }, |
|
257 |
}; |
|
258 |
||
259 |
/// roster type |
|
260 |
/// G: |
|
261 |
static const string2enum_t lua_roster_type[] = { |
|
262 |
{ "user", ROSTER_TYPE_USER }, |
|
263 |
{ "group", ROSTER_TYPE_GROUP }, |
|
264 |
{ "agent", ROSTER_TYPE_AGENT }, |
|
265 |
{ "room", ROSTER_TYPE_ROOM }, |
|
266 |
{ "special", ROSTER_TYPE_SPECIAL }, |
|
267 |
{ NULL, 0 }, |
|
268 |
}; |
|
269 |
||
270 |
/// main.log |
|
271 |
/// Prints message to log. |
|
272 |
/// Note: most likely you need notutf8 flag enabled. |
|
273 |
/// A: log print type, message, message... |
|
274 |
static int lua_main_log (lua_State *L) |
|
275 |
{ |
|
276 |
int type = luaL_checkenum_multi (L, 1, lua_lprint); |
|
277 |
lua_concat (L, lua_gettop (L) - 1); |
|
278 |
scr_log_print (type, lua_tostring (L, -1)); |
|
279 |
return 0; |
|
280 |
} |
|
281 |
||
282 |
// expects table on top |
|
283 |
static void lua_options_callback (char *key, char *value, lua_State *L) |
|
284 |
{ |
|
285 |
char *loc = from_utf8 (key); |
|
286 |
lua_pushstring (L, loc); |
|
287 |
g_free (loc); |
|
288 |
loc = from_utf8 (value); |
|
289 |
lua_pushstring (L, loc); |
|
290 |
g_free (loc); |
|
291 |
lua_settable (L, -3); |
|
292 |
} |
|
293 |
||
294 |
/// main.option |
|
295 |
/// Sets or gets value of mcabber option. |
|
296 |
/// You can specify nil as a value to delete option. |
|
297 |
/// If you omit option name, it returns hash table of all options. |
|
298 |
/// A: string (option name, optional), string (value, optional) |
|
299 |
/// R: string (value, optional) |
|
300 |
static int lua_main_option (lua_State *L) |
|
301 |
{ |
|
302 |
int top = lua_gettop (L); |
|
303 |
if (top > 0) { |
|
304 |
char *name = to_utf8 (luaL_checkstring (L, 1)); |
|
305 |
if (top > 1) { // Set |
|
306 |
if (lua_type (L, 2) == LUA_TNIL) // Unset |
|
307 |
settings_del (SETTINGS_TYPE_OPTION, name); |
|
308 |
else { // Set |
|
309 |
char *value = to_utf8 (luaL_checkstring (L, 2)); |
|
310 |
settings_set (SETTINGS_TYPE_OPTION, name, value); |
|
311 |
g_free (value); |
|
312 |
} |
|
313 |
g_free (name); |
|
314 |
return 0; |
|
315 |
} else { // Get |
|
316 |
char *value = from_utf8 (settings_get (SETTINGS_TYPE_OPTION, name)); |
|
317 |
if (value) { |
|
318 |
lua_pushstring (L, value); |
|
319 |
g_free (value); |
|
320 |
} else |
|
321 |
lua_pushnil (L); |
|
322 |
g_free (name); |
|
323 |
return 1; |
|
324 |
} |
|
325 |
} else { // List |
|
326 |
lua_newtable (L); |
|
327 |
settings_foreach (SETTINGS_TYPE_OPTION, (void (*)(char *key, char *val, void *ud)) lua_options_callback, L); |
|
328 |
return 1; |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
/// main.alias |
|
333 |
/// Sets or gets alias. |
|
334 |
/// You can specify nil as a command to delete alias. |
|
335 |
/// If you omit alias name, it will return hash table of all aliases. |
|
336 |
/// A: string (alias name, optional), string (command, optional) |
|
337 |
/// R: string (command, optional) |
|
338 |
static int lua_main_alias (lua_State *L) |
|
339 |
{ |
|
340 |
int top = lua_gettop (L); |
|
341 |
if (top > 0) { |
|
342 |
char *name = to_utf8 (luaL_checkstring (L, 1)); |
|
343 |
if (top > 1) { // Set |
|
344 |
if (lua_type (L, 2) == LUA_TNIL) { // Unset |
|
345 |
settings_del (SETTINGS_TYPE_ALIAS, name); |
|
346 |
compl_del_category_word (COMPL_CMD, name); |
|
347 |
} else { // Set |
|
348 |
char *value = to_utf8 (luaL_checkstring (L, 2)); |
|
349 |
if (!settings_get (SETTINGS_TYPE_ALIAS, name)) |
|
350 |
compl_add_category_word (COMPL_CMD, name); |
|
351 |
settings_set (SETTINGS_TYPE_ALIAS, name, value); |
|
352 |
g_free (value); |
|
353 |
} |
|
354 |
g_free (name); |
|
355 |
return 0; |
|
356 |
} else { // Get |
|
357 |
char *value = from_utf8 (settings_get (SETTINGS_TYPE_ALIAS, name)); |
|
358 |
if (value) { |
|
359 |
lua_pushstring (L, value); |
|
360 |
g_free (value); |
|
361 |
} else |
|
362 |
lua_pushnil (L); |
|
363 |
g_free (name); |
|
364 |
return 1; |
|
365 |
} |
|
366 |
} else { // List |
|
367 |
lua_newtable (L); |
|
368 |
settings_foreach (SETTINGS_TYPE_ALIAS, (void (*)(char *key, char *val, void *ud)) lua_options_callback, L); |
|
369 |
return 1; |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
/// main.bind |
|
374 |
/// Sets or gets binding. |
|
375 |
/// You can specify nil as a command to unbind key. |
|
376 |
/// If you omit keycode, it will return hash table of all bindings. |
|
377 |
/// A: string (keycode, optional), string (command, optional) |
|
378 |
/// R: string (command, optional) |
|
379 |
static int lua_main_binding (lua_State *L) |
|
380 |
{ |
|
381 |
int top = lua_gettop (L); |
|
382 |
if (top > 0) { |
|
383 |
// just to be sure... |
|
384 |
char *name = to_utf8 (luaL_checkstring (L, 1)); |
|
385 |
if (top > 1) { // Set |
|
386 |
if (lua_type (L, 2) == LUA_TNIL) // Unset |
|
387 |
settings_del (SETTINGS_TYPE_BINDING, name); |
|
388 |
else { // Set |
|
389 |
char *value = to_utf8 (luaL_checkstring (L, 2)); |
|
390 |
settings_set (SETTINGS_TYPE_BINDING, name, value); |
|
391 |
g_free (value); |
|
392 |
} |
|
393 |
g_free (name); |
|
394 |
return 0; |
|
395 |
} else { // Get |
|
396 |
char *value = from_utf8 (settings_get (SETTINGS_TYPE_BINDING, name)); |
|
397 |
if (value) { |
|
398 |
lua_pushstring (L, value); |
|
399 |
g_free (value); |
|
400 |
} else |
|
401 |
lua_pushnil (L); |
|
402 |
g_free (name); |
|
403 |
return 1; |
|
404 |
} |
|
405 |
} else { // List |
|
406 |
lua_newtable (L); |
|
407 |
settings_foreach (SETTINGS_TYPE_BINDING, (void (*)(char *key, char *val, void *ud)) lua_options_callback, L); |
|
408 |
return 1; |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
/// main.fileoption |
|
413 |
/// Gets option, expanding it as filename. |
|
414 |
/// A: string (option name) |
|
415 |
/// R: string (expanded option value) or nil |
|
416 |
static int lua_main_fileoption (lua_State *L) |
|
417 |
{ |
|
418 |
char *fname = expand_filename (settings_opt_get (luaL_checkstring (L, 1))); |
|
419 |
if (fname) { |
|
420 |
lua_pushstring (L, fname); |
|
421 |
g_free (fname); |
|
422 |
} else |
|
423 |
lua_pushnil (L); |
|
424 |
return 1; |
|
425 |
} |
|
426 |
||
427 |
#ifdef LLM_CONNECTION_ENABLE |
|
428 |
/// main.connection |
|
429 |
/// Returns lightuserdata of mcabber's loudmouth connection. |
|
430 |
/// This can be very useful with lua-loudmouth, and not much otherwise. |
|
431 |
/// R: lightuserdata or nil |
|
432 |
static int lua_main_connection (lua_State *L) |
|
433 |
{ |
|
434 |
if (xmpp_is_online ()) |
|
435 |
lua_pushlightuserdata (L, lconnection); |
|
436 |
else |
|
437 |
lua_pushnil (L); |
|
438 |
return 1; |
|
439 |
} |
|
440 |
#endif |
|
441 |
||
442 |
/// main.print_info |
|
443 |
/// Prints a system message to buddy's window. |
|
444 |
/// A: string (jid), string (message) |
|
445 |
static int lua_main_print_info (lua_State *L) |
|
446 |
{ |
|
447 |
char *jid = to_utf8 (luaL_checkstring (L, 1)); |
|
448 |
char *to = jidtodisp (jid); |
|
449 |
char *mesg = to_utf8 (luaL_checkstring (L, 2)); |
|
450 |
scr_write_incoming_message (to, mesg, 0, HBB_PREFIX_INFO, 0); |
|
451 |
g_free (mesg); |
|
452 |
g_free (to); |
|
453 |
g_free (jid); |
|
454 |
return 0; |
|
455 |
} |
|
456 |
||
457 |
/// main.beep |
|
458 |
/// Beeps with system speaker. |
|
459 |
static int lua_main_beep (lua_State *L) |
|
460 |
{ |
|
461 |
scr_beep (); |
|
462 |
return 0; |
|
463 |
} |
|
464 |
||
465 |
/// main.run |
|
466 |
/// Runs specified mcabber command. |
|
467 |
/// A: string |
|
468 |
static int lua_main_run (lua_State *L) |
|
469 |
{ |
|
470 |
process_command (luaL_checkstring (L, 1), TRUE); |
|
471 |
return 0; |
|
472 |
} |
|
473 |
||
474 |
/// main.status |
|
475 |
/// Returns your current status. |
|
476 |
/// R: string (status letter), string (status message) |
|
477 |
static int lua_main_status (lua_State *L) |
|
478 |
{ |
|
479 |
char *sm = from_utf8 (xmpp_getstatusmsg ()); |
|
480 |
lua_pushlstring (L, &imstatus2char[xmpp_getstatus ()], 1); |
|
481 |
lua_pushstring (L, sm); |
|
482 |
g_free (sm); |
|
483 |
return 2; |
|
484 |
} |
|
485 |
||
486 |
// expects table on top |
|
487 |
static void lua_rosterlist_callback (gpointer buddy, lua_State *L) |
|
488 |
{ |
|
489 |
char *jid = from_utf8 (buddy_getjid (buddy)); |
|
490 |
lua_pushnumber (L, lua_objlen (L, -1) + 1); |
|
491 |
lua_pushstring (L, jid); |
|
492 |
lua_settable (L, -3); |
|
493 |
g_free (jid); |
|
494 |
} |
|
495 |
||
496 |
/// main.roster |
|
497 |
/// Returns array of jids of buddies in roster. |
|
498 |
/// R: table |
|
499 |
static int lua_main_roster (lua_State *L) |
|
500 |
{ |
|
501 |
lua_newtable (L); |
|
502 |
foreach_buddy (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM, (void (*) (gpointer buddy, void *data)) lua_rosterlist_callback, L); |
|
503 |
return 1; |
|
504 |
} |
|
505 |
||
506 |
/// main.current_buddy |
|
507 |
/// Returns jid of current selected buddy or sets current buddy to buddy with specified jid. |
|
508 |
/// A: string (optional) |
|
509 |
/// R: string (optional) |
|
510 |
static int lua_main_current_buddy (lua_State *L) |
|
511 |
{ |
|
512 |
if (lua_gettop (L) > 0) { // Set |
|
513 |
// XXX: we need not convert to utf, RS works on jids/names in locale charset, |
|
514 |
// but will jidtodisp always correctly work on such tings? |
|
515 |
char *jid = jidtodisp (luaL_checkstring (L, 1)); |
|
516 |
scr_roster_search (jid); |
|
517 |
g_free (jid); |
|
518 |
return 0; |
|
519 |
} else { // Get |
|
520 |
char *jid = from_utf8 (buddy_getjid (BUDDATA (current_buddy))); |
|
521 |
lua_pushstring (L, jid); |
|
522 |
g_free (jid); |
|
523 |
return 1; |
|
524 |
} |
|
525 |
} |
|
526 |
||
527 |
/// main.full_jid |
|
528 |
/// Returns full jid (with current resource) of specified buddy (or current, if not specified). |
|
529 |
/// Note, that if there are no resources online, it will return just what it got. |
|
530 |
/// A: string (jid, optional) |
|
531 |
/// R: string (jid) |
|
532 |
static int lua_main_full_jid (lua_State *L) |
|
533 |
{ |
|
534 |
GList *buddy; |
|
535 |
GSList *resources; |
|
536 |
GSList *resource; |
|
537 |
if (lua_gettop (L) > 0) { |
|
538 |
char *jid = from_utf8 (luaL_checkstring (L, 1)); |
|
539 |
buddy = buddy_search_jid (jid); |
|
540 |
g_free (jid); |
|
541 |
} else |
|
542 |
buddy = current_buddy; |
|
543 |
if (!buddy) |
|
544 |
return 0; |
|
545 |
resources = buddy_getresources (BUDDATA (buddy)); |
|
546 |
if (!resources) { |
|
547 |
char *loc = from_utf8 (buddy_getjid (BUDDATA (buddy))); |
|
548 |
lua_pushstring (L, loc); |
|
549 |
g_free (loc); |
|
550 |
} else { |
|
551 |
char *jid = from_utf8 (buddy_getjid (BUDDATA (buddy))); |
|
552 |
char *res = from_utf8 (g_slist_last (resources)->data); |
|
553 |
lua_pushfstring (L, "%s%c%s", jid, JID_RESOURCE_SEPARATOR, res); |
|
554 |
for (resource = resources; resource; resource = g_slist_next (resource)) |
|
555 |
g_free (resource->data); |
|
556 |
g_slist_free (resources); |
|
557 |
g_free (jid); |
|
558 |
g_free (res); |
|
559 |
} |
|
560 |
return 1; |
|
561 |
} |
|
562 |
||
563 |
typedef struct { |
|
564 |
lua_State *L; |
|
565 |
gpointer buddy; |
|
566 |
} lua_state_and_buddy_t; // :) |
|
567 |
||
568 |
/// resources table |
|
569 |
/// Hash table with resource name as keys and another hash tables as values. |
|
570 |
/// Inner tables contain resource-specific information: priority, status and message. |
|
571 |
static void lua_buddy_resources_callback (gpointer resource, lua_state_and_buddy_t *d) |
|
572 |
{ |
|
573 |
char *loc = from_utf8 (resource); |
|
574 |
lua_pushstring (d->L, loc); |
|
575 |
g_free (loc); |
|
576 |
lua_createtable (d->L, 0, 3); |
|
577 |
lua_pushstring (d->L, "priority"); |
|
578 |
lua_pushnumber (d->L, buddy_getresourceprio (d->buddy, resource)); |
|
579 |
lua_settable (d->L, -3); |
|
580 |
lua_pushstring (d->L, "status"); |
|
581 |
lua_pushlstring (d->L, &imstatus2char[buddy_getstatus (d->buddy, resource)], 1); |
|
582 |
lua_settable (d->L, -3); |
|
583 |
lua_pushstring (d->L, "message"); |
|
584 |
loc = from_utf8 (buddy_getstatusmsg (d->buddy, resource)); |
|
585 |
lua_pushstring (d->L, loc); |
|
586 |
g_free (loc); |
|
587 |
lua_settable (d->L, -3); |
|
588 |
lua_settable (d->L, -3); |
|
589 |
g_free (resource); |
|
590 |
} |
|
591 |
||
592 |
/// main.buddy_info |
|
593 |
/// Returns a hash table with information on specified buddy. |
|
594 |
/// Table contains fields type, name, onserver and resources (which points to resources table). |
|
595 |
/// A: string (jid) |
|
596 |
/// R: table |
|
597 |
static int lua_main_buddy_info (lua_State *L) |
|
598 |
{ |
|
599 |
char *loc = to_utf8 (luaL_checkstring (L, 1)); |
|
600 |
char *jid = jidtodisp (loc); |
|
601 |
GSList *buddy = roster_find (jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM); |
|
602 |
GSList *resources; |
|
603 |
lua_state_and_buddy_t snb; |
|
604 |
g_free (jid); |
|
605 |
g_free (loc); |
|
606 |
||
607 |
if (!buddy) { |
|
608 |
lua_pushnil (L); |
|
609 |
return 1; |
|
610 |
} |
|
611 |
||
612 |
lua_createtable (L, 0, 3); |
|
613 |
lua_pushstring (L, "type"); |
|
614 |
luaL_pushenum (L, buddy_gettype (BUDDATA (buddy)), lua_roster_type); |
|
615 |
lua_settable (L, -3); |
|
616 |
lua_pushstring (L, "name"); |
|
617 |
loc = from_utf8 (buddy_getname (BUDDATA (buddy))); |
|
618 |
lua_pushstring (L, loc); |
|
619 |
g_free (loc); |
|
620 |
lua_settable (L, -3); |
|
621 |
lua_pushstring (L, "onserver"); |
|
622 |
lua_pushboolean (L, buddy_getonserverflag (BUDDATA (buddy))); |
|
623 |
lua_settable (L, -3); |
|
624 |
lua_pushstring (L, "resources"); |
|
625 |
lua_createtable (L, 0, 0); |
|
626 |
snb.L = L; |
|
627 |
snb.buddy = BUDDATA (buddy); |
|
628 |
resources = buddy_getresources (BUDDATA (buddy)); |
|
629 |
g_slist_foreach (buddy_getresources (BUDDATA (buddy)), (GFunc) lua_buddy_resources_callback, &snb); |
|
630 |
g_slist_free (resources); |
|
631 |
lua_settable (L, -3); |
|
632 |
||
633 |
return 1; |
|
634 |
} |
|
635 |
||
636 |
// XMPP DISCO FEATURES |
|
637 |
||
638 |
GSList *lua_added_features = NULL; |
|
639 |
||
640 |
/// main.add_feature |
|
641 |
/// Adds xmlns to disco#info features list. |
|
642 |
/// A: string (xmlns) |
|
643 |
static int lua_main_add_feature (lua_State *L) |
|
644 |
{ |
|
645 |
char *xmlns = to_utf8 (luaL_checkstring (L, 1)); |
|
646 |
xmpp_add_feature (xmlns); |
|
647 |
lua_added_features = g_slist_prepend (lua_added_features, xmlns); |
|
648 |
return 0; |
|
649 |
} |
|
650 |
||
651 |
/// main.del_feature |
|
652 |
/// Removes xmlns from disco#info features list. |
|
653 |
/// A: stirng (xmlns) |
|
654 |
static int lua_main_del_feature (lua_State *L) |
|
655 |
{ |
|
656 |
char *xmlns = to_utf8 (luaL_checkstring (L, 1)); |
|
657 |
GSList *el = g_slist_find_custom (lua_added_features, xmlns, (GCompareFunc) strcmp); |
|
658 |
xmpp_del_feature (xmlns); |
|
659 |
if (el) { |
|
660 |
g_free (el->data); |
|
661 |
lua_added_features = g_slist_delete_link (lua_added_features, el); |
|
662 |
} |
|
663 |
return 0; |
|
664 |
} |
|
665 |
||
666 |
// MCABBER EVENTS |
|
667 |
||
668 |
/// event context |
|
669 |
/// Enum, indicating what exactly caused event function firing. |
|
670 |
/// G: |
|
671 |
static const string2enum_t lua_event_context[] = { |
|
672 |
{ "timeout", EVS_CONTEXT_TIMEOUT }, |
|
673 |
{ "cancel", EVS_CONTEXT_CANCEL }, |
|
674 |
{ "reject", EVS_CONTEXT_REJECT }, |
|
675 |
{ "accept", EVS_CONTEXT_ACCEPT }, |
|
676 |
{ NULL, 0 }, |
|
677 |
}; |
|
678 |
||
679 |
typedef struct { |
|
680 |
lua_State *L; |
|
681 |
int reference; |
|
682 |
int evid; |
|
683 |
} lua_event_callback_t; |
|
684 |
||
685 |
static GSList *lua_events = NULL; |
|
686 |
||
687 |
static void lua_event_callback_destroy_notify (gpointer udata) |
|
688 |
{ |
|
689 |
lua_event_callback_t *cb = udata; |
|
690 |
||
691 |
luaL_unref (cb -> L, LUA_REGISTRYINDEX, cb->reference); |
|
692 |
luaL_unref (cb -> L, LUA_REGISTRYINDEX, cb->evid); |
|
693 |
luaL_free (cb -> L, cb); |
|
694 |
} |
|
695 |
||
696 |
/// event function |
|
697 |
/// Function to be called, when some event state change occurs |
|
698 |
/// A: event context, string (event args) |
|
699 |
/// R: boolean (if event shoud be preserved) |
|
700 |
static gboolean lua_event_callback (guint context, const gchar *arg, gpointer userdata) |
|
701 |
{ |
|
702 |
lua_event_callback_t *cb = userdata; |
|
703 |
||
704 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
705 |
luaL_pushenum (cb->L, context, lua_event_context); |
|
706 |
lua_pushstring (cb -> L, arg); |
|
707 |
if (lua_pcall (cb->L, 2, 1, 0)) { |
|
708 |
scr_log_print (LPRINT_LOGNORM, "lua: Event callback execution error: %s", lua_tostring (cb->L, -1)); |
|
709 |
lua_pop (cb->L, 1); |
|
710 |
} |
|
711 |
||
712 |
if (lua_toboolean (cb -> L, -1)) |
|
713 |
return TRUE; |
|
714 |
else { |
|
715 |
lua_events = g_slist_remove (lua_events, cb); // XXX |
|
716 |
return FALSE; |
|
717 |
} |
|
718 |
} |
|
719 |
||
720 |
/// main.event |
|
721 |
/// Creates new event. If called without arguments, returns event id list. |
|
722 |
/// A: event function (optional), string (event id), string (description, optional), integer (expiration timeout, optional) |
|
723 |
/// R: string (event id) or nothing (creation error) or table (list of event names) |
|
724 |
static int lua_main_event (lua_State *L) |
|
725 |
{ |
|
726 |
int top = lua_gettop (L); |
|
727 |
if (top > 0) { // Create |
|
728 |
lua_event_callback_t *cb; |
|
729 |
const char *evid = NULL; |
|
730 |
int timeout = 0; |
|
731 |
const char *desc = NULL; |
|
732 |
luaL_argcheck (L, lua_type (L, 1) == LUA_TFUNCTION, 1, "event function expected"); |
|
733 |
||
734 |
if (top > 1) { |
|
735 |
evid = luaL_checkstring (L, 2); |
|
736 |
if (top > 2) { |
|
737 |
timeout = luaL_checkinteger (L, 3); |
|
738 |
if (top > 2) { |
|
739 |
desc = luaL_checkstring (L, 4); |
|
740 |
lua_pop (L, 3); |
|
741 |
} else |
|
742 |
lua_pop (L, 2); |
|
743 |
} else |
|
744 |
lua_pop (L, 1); |
|
745 |
} |
|
746 |
||
747 |
lua_pushvalue (L, 1); // XXX |
|
748 |
cb = luaL_malloc (L, sizeof (lua_event_callback_t)); |
|
749 |
cb -> L = L; |
|
750 |
cb -> reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
751 |
cb -> evid = LUA_NOREF; |
|
752 |
lua_events = g_slist_prepend (lua_events, cb); |
|
753 |
||
754 |
evid = evs_new (desc, evid, timeout, lua_event_callback, cb, lua_event_callback_destroy_notify); |
|
755 |
if (!evid) { |
|
756 |
lua_events = g_slist_remove (lua_events, cb); // XXX |
|
757 |
return 0; |
|
758 |
} |
|
759 |
||
760 |
lua_pushstring (L, evid); |
|
761 |
lua_pushvalue (L, -1); |
|
762 |
cb -> evid = luaL_ref (L, LUA_REGISTRYINDEX); // XXX |
|
763 |
return 1; |
|
764 |
||
765 |
} else { // List |
|
766 |
GSList *events = evs_geteventslist (); |
|
767 |
GSList *event; |
|
768 |
||
769 |
lua_newtable (L); |
|
770 |
for (event = events; event; event = g_slist_next (event)) { |
|
771 |
lua_pushstring (L, event->data); |
|
772 |
luaL_ref (L, -2); |
|
773 |
} |
|
774 |
g_slist_free (events); |
|
775 |
||
776 |
return 1; |
|
777 |
} |
|
778 |
} |
|
779 |
||
780 |
// MCABBER COMMANDS |
|
781 |
||
782 |
/// completion type |
|
783 |
/// Built-it completion types can be specified as string, instead of id. |
|
784 |
/// G: |
|
785 |
static string2enum_t lua_completion_type[] = { // not const, we need to modify yesno |
|
786 |
{ "cmd", COMPL_CMD }, |
|
787 |
{ "jid", COMPL_JID }, |
|
788 |
{ "urljid", COMPL_URLJID }, |
|
789 |
{ "name", COMPL_NAME }, |
|
790 |
{ "status", COMPL_STATUS }, |
|
791 |
{ "filename", COMPL_FILENAME }, |
|
792 |
{ "roster", COMPL_ROSTER }, |
|
793 |
{ "buffer", COMPL_BUFFER }, |
|
794 |
{ "group", COMPL_GROUP }, |
|
795 |
{ "groupname", COMPL_GROUPNAME }, |
|
796 |
{ "multiline", COMPL_MULTILINE }, |
|
797 |
{ "room", COMPL_ROOM }, |
|
798 |
{ "resource", COMPL_RESOURCE }, |
|
799 |
{ "auth", COMPL_AUTH }, |
|
800 |
{ "request", COMPL_REQUEST }, |
|
801 |
{ "events", COMPL_EVENTS }, |
|
802 |
{ "eventsid", COMPL_EVENTSID }, |
|
803 |
{ "pgp", COMPL_PGP }, |
|
804 |
{ "color", COMPL_COLOR }, |
|
805 |
{ "otr", COMPL_OTR }, |
|
806 |
{ "ortpolicy", COMPL_OTRPOLICY }, |
|
807 |
{ "yesno", 0 }, |
|
808 |
{ NULL, 0 }, |
|
809 |
}; |
|
810 |
#define MLUA_YESNO_POS ( 21 ) |
|
811 |
||
812 |
typedef struct { |
|
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
813 |
int cbref; |
103 | 814 |
int parse_args; |
113 | 815 |
#ifdef MCABBER_API_HAVE_CMD_ID |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
816 |
gpointer cmid; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
817 |
#else |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
818 |
int nameref; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
819 |
#endif |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
820 |
int selfref; |
103 | 821 |
lua_State *L; |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
822 |
} lua_command_t; |
103 | 823 |
|
824 |
static GSList *lua_added_categories = NULL; |
|
825 |
||
826 |
// returns true if string contains errors - unclosed quotes or unvalued option |
|
827 |
/// command arguments table |
|
828 |
/// It can parse barewords (with escapes), double-quoted strings (with escapes), single-quoted strings (without escapes), options and arguments. |
|
829 |
/// Arguments are separated only by whitespace, so, consequential quoted strings or barewords are one argument. |
|
830 |
/// This strings are equal: |
|
831 |
/// * ab\ cd\'e\\f\" |
|
832 |
/// * "ab cd'e\\f\"" |
|
833 |
/// * 'ab cd'\''e\f"' |
|
834 |
/// * ab" cd'"'e\f"' |
|
835 |
/// Returned table have option names as keys, option values as values, and arguments as sequential members. -- option is supported. |
|
836 |
/// Example: "-t jid -m 9 -- -aa bb cc" will result in { t = 'jid', m = 9, '-aa', 'bb', 'cc' } |
|
837 |
/// Implementation notes: |
|
838 |
/// * All options should be before any arguments. First non-option argument disables options recognizing. |
|
839 |
/// * 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. |
|
840 |
/// * Escape character just before EOL is preserved. |
|
841 |
static int luaL_pushargs (lua_State *L, const char *args) |
|
842 |
{ |
|
843 |
const char *p = args; |
|
844 |
luaL_Buffer buf; |
|
845 |
int option = 0; |
|
846 |
int options = 1; |
|
847 |
||
848 |
lua_newtable (L); |
|
849 |
luaL_buffinit (L, &buf); |
|
850 |
while (*p) { |
|
851 |
if (*p == ' ') { |
|
852 |
++p; |
|
853 |
continue; |
|
854 |
} |
|
855 |
if (*p == '"') { // soft quote |
|
856 |
const char *start = ++p; |
|
857 |
while (*p) { |
|
858 |
if (*p == '\\') { // escape symbol |
|
859 |
luaL_addlstring (&buf, start, p - start); |
|
860 |
start = ++p; |
|
861 |
if (*p) // skip symbol |
|
862 |
++p; |
|
863 |
else // add last \ in line |
|
864 |
luaL_addchar (&buf, '\\'); |
|
865 |
} else if (*p == '"') // quotation end |
|
866 |
break; |
|
867 |
else |
|
868 |
++p; |
|
869 |
} |
|
870 |
luaL_addlstring (&buf, start, p - start); // XXX: eats quote on eol |
|
871 |
if (*p) |
|
872 |
++p; |
|
873 |
else |
|
874 |
return 1; |
|
875 |
} else if (*p == '\'') { // no-escape quote |
|
876 |
const char *start = ++p; |
|
877 |
while (*p && *p != '\'') |
|
878 |
p++; |
|
879 |
luaL_addlstring (&buf, start, p - start); // XXX: eats quote on eol |
|
880 |
if (*p) |
|
881 |
++p; |
|
882 |
else |
|
883 |
return 1; |
|
884 |
} else { // bareword |
|
885 |
const char *start = p; |
|
886 |
while (*p) { |
|
887 |
if (*p == '\\') { |
|
888 |
luaL_addlstring (&buf, start, p - start); |
|
889 |
start = ++p; |
|
890 |
if (*p) // skip symbol |
|
891 |
++p; |
|
892 |
else // add last \ in line |
|
893 |
luaL_addchar (&buf, '\\'); |
|
894 |
} else if (*p == ' ' || *p == '\'' || *p == '"') |
|
895 |
break; |
|
896 |
else |
|
897 |
++p; |
|
898 |
} |
|
899 |
luaL_addlstring (&buf, start, p - start); |
|
900 |
} |
|
901 |
||
902 |
if ((!*p) || *p == ' ') { |
|
903 |
const char *result; |
|
904 |
luaL_pushresult (&buf); |
|
905 |
||
906 |
result = lua_tostring (L, -1); |
|
907 |
if (options && !option && *result == '-') { // option |
|
908 |
if (*(result+1) == '-' && !*(result+2)) { // end of options |
|
909 |
lua_pop (L, 1); |
|
910 |
options = 0; |
|
911 |
} else { // option name |
|
912 |
lua_pushstring (L, result + 1); |
|
913 |
lua_remove (L, -2); |
|
914 |
option = 1; |
|
915 |
} |
|
916 |
} else if (option) { // opion value |
|
917 |
lua_settable (L, -3); |
|
918 |
option = 0; |
|
919 |
} else { // argument |
|
920 |
options = 0; |
|
921 |
luaL_ref (L, -2); |
|
922 |
} |
|
923 |
||
924 |
luaL_buffinit (L, &buf); |
|
925 |
} |
|
926 |
} |
|
927 |
||
928 |
if (option) { |
|
929 |
luaL_ref (L, -2); // XXX: eats minus on eol |
|
930 |
return 1; |
|
931 |
} |
|
932 |
||
933 |
return 0; |
|
934 |
} |
|
935 |
||
936 |
/// command function |
|
937 |
/// Function to handle newly registered command. |
|
938 |
/// Argument type passed depends on how command is registered. |
|
939 |
/// A: string (arguments) or command arguments table |
|
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
940 |
static void lua_main_command_handler (char *args, lua_command_t *cb) |
103 | 941 |
{ |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
942 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->cbref); |
103 | 943 |
|
944 |
if (cb->parse_args) |
|
945 |
luaL_pushargs (cb->L, args); |
|
946 |
else |
|
947 |
lua_pushstring (cb->L, args); |
|
948 |
||
949 |
if (lua_pcall (cb->L, 1, 0, 0)) { |
|
950 |
scr_log_print (LPRINT_LOGNORM, "lua: Command execution error: %s", lua_tostring (cb->L, -1)); |
|
951 |
lua_pop (cb->L, 1); |
|
952 |
} |
|
953 |
} |
|
954 |
||
955 |
/// main.parse_args |
|
956 |
/// Function to parse command argument string to command arguments table. |
|
957 |
/// A: string |
|
958 |
/// R: table |
|
959 |
static int lua_main_parse_args (lua_State *L) |
|
960 |
{ |
|
961 |
luaL_pushargs (L, luaL_checkstring (L, 1)); |
|
962 |
return 1; |
|
963 |
} |
|
964 |
||
965 |
/// main.add_category |
|
966 |
/// Adds completion category. |
|
967 |
/// A: table (values are used as words for completion, optional) |
|
968 |
/// R: integer (category id, in fact completion type) or nil |
|
969 |
static int lua_main_add_category (lua_State *L) |
|
970 |
{ |
|
971 |
guint cid; |
|
972 |
||
973 |
if (lua_gettop (L) > 0) { |
|
974 |
luaL_argcheck (L, lua_type (L, 1) == LUA_TTABLE, 1, "table expected"); |
|
975 |
cid = compl_new_category (); |
|
976 |
if (cid) { |
|
977 |
lua_pushnil (L); |
|
978 |
while (lua_next (L, 1)) { |
|
979 |
char *word = to_utf8 (luaL_checkstring (L, -1)); |
|
980 |
if (word) { |
|
981 |
compl_add_category_word (cid, word); |
|
982 |
g_free (word); |
|
983 |
} |
|
984 |
lua_pop (L, 1); |
|
985 |
} |
|
986 |
} |
|
987 |
} else |
|
988 |
cid = compl_new_category (); |
|
989 |
||
990 |
if (cid) { |
|
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
991 |
lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) ((gsize) cid)); |
103 | 992 |
lua_pushinteger (L, cid); |
993 |
} else |
|
994 |
lua_pushnil (L); |
|
995 |
||
996 |
return 1; |
|
997 |
} |
|
998 |
||
999 |
/// main.del_category |
|
1000 |
/// Removes completion category. |
|
1001 |
/// A: integer (category id) |
|
1002 |
static int lua_main_del_category (lua_State *L) |
|
1003 |
{ |
|
1004 |
guint cid = luaL_checkinteger (L, 1); |
|
1005 |
compl_del_category (cid); |
|
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
1006 |
lua_added_categories = g_slist_remove (lua_added_categories, (gpointer) ((gsize) cid)); |
103 | 1007 |
return 0; |
1008 |
} |
|
1009 |
||
1010 |
/// main.add_completion |
|
1011 |
/// Adds word to a completion list. |
|
1012 |
/// A: integer (completion group id), string (word) |
|
1013 |
static int lua_main_add_completion (lua_State *L) |
|
1014 |
{ |
|
1015 |
guint cid = luaL_checkinteger (L, 1); |
|
1016 |
char *word = to_utf8 (luaL_checkstring (L, 2)); // XXX |
|
1017 |
compl_add_category_word (cid, word); |
|
1018 |
g_free (word); |
|
1019 |
return 0; |
|
1020 |
} |
|
1021 |
||
1022 |
/// main.del_completion |
|
1023 |
/// Removes word from a completion list. |
|
1024 |
/// A: integer (completion group id), string (word) |
|
1025 |
static int lua_main_del_completion (lua_State *L) |
|
1026 |
{ |
|
1027 |
guint cid = luaL_checkinteger (L, 1); |
|
1028 |
char *word = to_utf8 (luaL_checkstring (L, 2)); // XXX |
|
1029 |
compl_del_category_word (cid, word); |
|
1030 |
g_free (word); |
|
1031 |
return 0; |
|
1032 |
} |
|
1033 |
||
1034 |
/// main.command |
|
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1035 |
/// Associates mcabber command name and lua function. |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1036 |
/// As a completion you can also specify a string name (see completion type) instead of table, for non-builtin, you can just pass integer id. |
103 | 1037 |
/// Note, that for now there are no way to unregister completion group, so, resources can be exausted easily. |
1038 |
/// Also note, that it ignores keys in a completion table. |
|
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1039 |
/// A: string (command name), command function, boolean (parse args flag, optional), table (completions, optional)/completion type (or integer comletion group id, optional) |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1040 |
/// R: userdata (command object) |
103 | 1041 |
static int lua_main_command (lua_State *L) |
1042 |
{ |
|
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1043 |
const char *name = luaL_checkstring (L, 1); // XXX: to_utf8? looks like no :/ |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1044 |
lua_command_t *cb; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1045 |
int top = lua_gettop (L); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1046 |
guint cid = 0; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1047 |
int parse = 0; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1048 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
103 | 1049 |
|
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1050 |
if (top > 2) { // parse flag provided |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1051 |
parse = lua_toboolean (L, 3); |
103 | 1052 |
|
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1053 |
if (top > 3) { // Completions provided |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1054 |
if (lua_type (L, 4) == LUA_TTABLE) { |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1055 |
cid = compl_new_category (); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1056 |
if (cid) { |
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
1057 |
lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) ((gsize) cid)); |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1058 |
lua_pushnil (L); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1059 |
while (lua_next (L, 4)) { |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1060 |
char *word = to_utf8 (luaL_checkstring (L, -1)); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1061 |
compl_add_category_word (cid, word); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1062 |
lua_pop (L, 1); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1063 |
g_free (word); |
103 | 1064 |
} |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1065 |
} |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1066 |
} else |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1067 |
cid = luaL_checkenum (L, 4, lua_completion_type); |
103 | 1068 |
} |
1069 |
} |
|
113 | 1070 |
|
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1071 |
cb = lua_newuserdata (L, sizeof (lua_command_t)); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1072 |
luaL_getmetatable (L, "mcabber.command"); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1073 |
lua_setmetatable (L, -2); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1074 |
lua_pushvalue (L, 2); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1075 |
cb -> cbref = luaL_ref (L, LUA_REGISTRYINDEX); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1076 |
lua_pushvalue (L, -1); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1077 |
cb -> selfref = luaL_ref (L, LUA_REGISTRYINDEX); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1078 |
cb -> parse_args = parse; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1079 |
cb -> L = L; |
113 | 1080 |
#ifdef MCABBER_API_HAVE_CMD_ID |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1081 |
cb -> cmid = cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1082 |
#else |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1083 |
lua_pushvalue (L, 1); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1084 |
cb -> nameref = luaL_ref (L, LUA_REGISTRYINDEX); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1085 |
cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1086 |
#endif |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1087 |
|
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1088 |
return 1; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1089 |
} |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1090 |
|
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1091 |
static void lua_mcabber_unregister_command (lua_State *L, lua_command_t *cb) |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1092 |
{ |
113 | 1093 |
#ifdef MCABBER_API_HAVE_CMD_ID |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1094 |
if (cb -> cmid) |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1095 |
cmd_del (cb -> cmid); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1096 |
#else |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1097 |
const char *name; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1098 |
|
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1099 |
if (cb -> nameref == LUA_NOREF) |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1100 |
return; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1101 |
|
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1102 |
lua_rawgeti (L, LUA_REGISTRYINDEX, cb -> nameref); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1103 |
name = lua_tostring (L, -1); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1104 |
cmd_del (name); |
110
bd9f24178d67
Prevent re-unregistering commands
Myhailo Danylenko <isbear@ukrpost.net>
parents:
109
diff
changeset
|
1105 |
luaL_unref (L, LUA_REGISTRYINDEX, cb -> nameref); |
bd9f24178d67
Prevent re-unregistering commands
Myhailo Danylenko <isbear@ukrpost.net>
parents:
109
diff
changeset
|
1106 |
cb -> nameref = LUA_NOREF; |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1107 |
lua_pop (L, 1); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1108 |
#endif |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1109 |
} |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1110 |
|
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1111 |
/// command:del |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1112 |
/// Unregisters given command from mcabber. Object will be destroyed later. |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1113 |
static int lua_mcabber_command_del (lua_State *L) |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1114 |
{ |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1115 |
lua_command_t *cb = luaL_checkudata (L, 1, "mcabber.command"); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1116 |
luaL_argcheck (L, cb != NULL, 1, "mcabber command object expected"); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1117 |
lua_mcabber_unregister_command (L, cb); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1118 |
if (cb -> selfref != LUA_NOREF) { |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1119 |
luaL_unref (L, LUA_REGISTRYINDEX, cb -> selfref); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1120 |
cb -> selfref = LUA_NOREF; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1121 |
} |
103 | 1122 |
return 0; |
1123 |
} |
|
1124 |
||
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1125 |
static int lua_mcabber_command_gc (lua_State *L) |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1126 |
{ |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1127 |
lua_command_t *cb = luaL_checkudata (L, 1, "mcabber.command"); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1128 |
luaL_argcheck (L, cb != NULL, 1, "mcabber command object expected"); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1129 |
lua_mcabber_unregister_command (L, cb); |
113 | 1130 |
#ifndef MCABBER_API_HAVE_CMD_ID |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1131 |
if (cb -> nameref != LUA_NOREF) |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1132 |
luaL_unref (L, LUA_REGISTRYINDEX, cb -> nameref); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1133 |
#endif |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1134 |
if (cb -> cbref != LUA_NOREF) |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1135 |
luaL_unref (L, LUA_REGISTRYINDEX, cb -> cbref); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1136 |
return 0; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1137 |
} |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1138 |
|
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1139 |
static const luaL_Reg lua_mcabber_command_reg_m[] = { |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1140 |
{ "del", lua_mcabber_command_del }, |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1141 |
{ "__gc", lua_mcabber_command_gc }, |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1142 |
{ NULL, NULL }, |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1143 |
}; |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1144 |
|
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1145 |
static void lua_command_init (lua_State *L) |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1146 |
{ |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1147 |
luaL_newmetatable (L, "mcabber.command"); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1148 |
lua_pushvalue (L, -1); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1149 |
lua_setfield (L, -2, "__index"); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1150 |
luaL_register (L, NULL, lua_mcabber_command_reg_m); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1151 |
lua_pop (L, 1); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1152 |
} |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1153 |
|
103 | 1154 |
// TIMER |
1155 |
||
1156 |
typedef struct { |
|
1157 |
int reference; |
|
1158 |
guint source; |
|
1159 |
lua_State *L; |
|
1160 |
} lua_timer_callback_t; |
|
1161 |
||
1162 |
static GSList *lua_timers = NULL; |
|
1163 |
||
1164 |
static void lua_timer_callback_destroy (lua_timer_callback_t *cb) |
|
1165 |
{ |
|
1166 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
1167 |
lua_timers = g_slist_remove (lua_timers, (gpointer) ((gsize) cb->source)); |
103 | 1168 |
luaL_free (cb->L, cb); |
1169 |
} |
|
1170 |
||
1171 |
/// timer function |
|
1172 |
/// Function, that will be called periodically until it returns false. |
|
1173 |
/// R: boolean |
|
1174 |
static gboolean lua_timer_callback (lua_timer_callback_t *cb) |
|
1175 |
{ |
|
1176 |
int ret; |
|
1177 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
1178 |
if (lua_pcall (cb->L, 0, 1, 0)) { |
|
1179 |
scr_log_print (LPRINT_LOGNORM, "lua: Timer callback execution error: %s", lua_tostring (cb->L, -1)); |
|
1180 |
lua_pop (cb->L, 1); |
|
1181 |
return FALSE; |
|
1182 |
} |
|
1183 |
ret = lua_toboolean (cb->L, -1); |
|
1184 |
lua_pop (cb->L, 1); |
|
1185 |
return ret; |
|
1186 |
} |
|
1187 |
||
1188 |
/// main.timer |
|
1189 |
/// Creates new timer function, that will be called periodically. |
|
1190 |
/// A: integer (interval, seconds), timer function |
|
1191 |
static int lua_main_timer (lua_State *L) |
|
1192 |
{ |
|
1193 |
int interval = luaL_checkint (L, 1); |
|
1194 |
guint source; |
|
1195 |
lua_timer_callback_t *cb; |
|
1196 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
1197 |
||
1198 |
cb = luaL_malloc (L, sizeof (lua_timer_callback_t)); |
|
1199 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
1200 |
cb->L = L; |
|
1201 |
||
1202 |
source = g_timeout_add_seconds_full (MLUA_SOURCE_PRIORITY, interval, (GSourceFunc) lua_timer_callback, cb, (GDestroyNotify) lua_timer_callback_destroy); |
|
1203 |
cb->source = source; |
|
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
1204 |
lua_timers = g_slist_prepend (lua_timers, (gpointer) ((gsize) source)); |
103 | 1205 |
|
1206 |
return 0; |
|
1207 |
} |
|
1208 |
||
1209 |
// BACKGROUND PIPE READING |
|
1210 |
||
1211 |
typedef struct { |
|
1212 |
lua_State *L; |
|
1213 |
guint source; |
|
1214 |
FILE *fd; |
|
1215 |
int reference; |
|
1216 |
} lua_bgread_callback_t; |
|
1217 |
||
1218 |
static GSList *lua_bgreads = NULL; |
|
1219 |
||
1220 |
static gchar lua_bgread_buffer[MLUA_BGREAD_BUFFER]; |
|
1221 |
||
1222 |
static void lua_bgread_callback_destroy (lua_bgread_callback_t *cb) |
|
1223 |
{ |
|
1224 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
1225 |
pclose (cb->fd); // Not necessary? |
|
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
1226 |
lua_bgreads = g_slist_remove (lua_bgreads, (gpointer) ((gsize) cb->source)); |
103 | 1227 |
luaL_free (cb->L, cb); |
1228 |
} |
|
1229 |
||
1230 |
/// background reading function |
|
125
3e2421384d7a
Examples updates (shortenurl)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
123
diff
changeset
|
1231 |
/// Function, that processes output from pipe in asynchronous way. |
103 | 1232 |
/// A: string (data) or nil (eof) |
1233 |
/// R: boolean (false if reading should be terminated) |
|
1234 |
static gboolean lua_bgread_callback (GIOChannel *source, GIOCondition condition, lua_bgread_callback_t *cb) |
|
1235 |
{ |
|
1236 |
int ret = TRUE; |
|
1237 |
||
1238 |
if (condition | G_IO_IN) { // data |
|
1239 |
while (TRUE) { |
|
1240 |
gsize read = 0; |
|
1241 |
g_io_channel_read_chars (source, lua_bgread_buffer, MLUA_BGREAD_BUFFER, &read, NULL); |
|
1242 |
if (!read) // exhausted |
|
1243 |
break; |
|
1244 |
||
1245 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
1246 |
lua_pushlstring (cb->L, lua_bgread_buffer, read); |
|
1247 |
if (lua_pcall (cb->L, 1, 1, 0)) { |
|
1248 |
scr_log_print (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1)); |
|
1249 |
lua_pop (cb->L, 1); |
|
1250 |
return FALSE; |
|
1251 |
} |
|
1252 |
ret = lua_toboolean (cb->L, -1); |
|
1253 |
lua_pop (cb->L, 1); |
|
1254 |
if (!ret) // enough |
|
1255 |
return FALSE; |
|
1256 |
} |
|
1257 |
} |
|
1258 |
||
1259 |
if (condition & ~G_IO_IN) { // err or hup |
|
1260 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
1261 |
lua_pushnil (cb->L); |
|
1262 |
if (lua_pcall (cb->L, 1, 1, 0)) { |
|
1263 |
scr_log_print (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1)); |
|
1264 |
lua_pop (cb->L, 1); |
|
1265 |
return FALSE; |
|
1266 |
} |
|
1267 |
ret = lua_toboolean (cb->L, -1); |
|
1268 |
lua_pop (cb->L, 1); |
|
1269 |
} |
|
1270 |
||
1271 |
return ret; |
|
1272 |
} |
|
1273 |
||
1274 |
/// main.bgread |
|
1275 |
/// Runs specified command and passes its output to a given function. |
|
1276 |
/// A: string (command), background reading function |
|
1277 |
static int lua_main_bgread (lua_State *L) |
|
1278 |
{ |
|
1279 |
const char *command = luaL_checkstring (L, 1); |
|
1280 |
lua_bgread_callback_t *cb; |
|
1281 |
FILE *fd; |
|
1282 |
GIOChannel *channel; |
|
1283 |
const char *charset = NULL; |
|
1284 |
guint source; |
|
1285 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
1286 |
||
1287 |
fd = popen (command, "r"); |
|
1288 |
if (!fd) { |
|
1289 |
lua_pushstring (L, "Error opening pipe"); |
|
1290 |
lua_error (L); |
|
1291 |
} |
|
1292 |
||
1293 |
channel = g_io_channel_unix_new (fileno (fd)); |
|
1294 |
// We, most likely, need this, |
|
1295 |
// But we cannot use this, |
|
1296 |
// It will block. |
|
1297 |
//if (!g_get_charset (&charset)) |
|
1298 |
g_io_channel_set_encoding (channel, charset, NULL); |
|
1299 |
g_io_channel_set_buffered (channel, FALSE); |
|
1300 |
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL); |
|
1301 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
1302 |
||
1303 |
cb = luaL_malloc (L, sizeof (lua_bgread_callback_t)); |
|
1304 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
1305 |
cb->L = L; |
|
1306 |
cb->fd = fd; |
|
1307 |
||
1308 |
source = 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); |
|
1309 |
cb->source = source; |
|
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
1310 |
lua_bgreads = g_slist_prepend (lua_bgreads, (gpointer) ((gsize) source)); |
103 | 1311 |
|
1312 |
// unref? |
|
1313 |
||
1314 |
return 0; |
|
1315 |
} |
|
1316 |
||
1317 |
// HOOKÂ HANDLING |
|
1318 |
||
1319 |
typedef struct { |
|
1320 |
lua_State *L; // lua environment for handler use |
|
1321 |
int nameref; // reference to hook name string |
|
1322 |
int cbref; // reference to hook handler function |
|
1323 |
int selfref; // self-reference to object |
|
1324 |
guint hid; // hook id for object destruction |
|
1325 |
} lua_hook_t; |
|
1326 |
||
1327 |
/// hook handler result |
|
1328 |
/// What to do with hook processing afterwards |
|
1329 |
/// G: |
|
1330 |
static const string2enum_t lua_hook_handler_result[] = { |
|
1331 |
{ "proceed", HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS }, |
|
1332 |
{ "stop", HOOK_HANDLER_RESULT_NO_MORE_HANDLER }, |
|
1333 |
{ "drop", HOOK_HANDLER_RESULT_NO_MORE_HANDLER_DROP_DATA }, |
|
1334 |
{ NULL, HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS }, |
|
1335 |
}; |
|
1336 |
||
1337 |
/// hook handler priority |
|
1338 |
/// Feel free to specify just a number instead of some preset value. |
|
1339 |
/// G: |
|
1340 |
static const string2enum_t lua_hook_handler_priority[] = { |
|
1341 |
{ "high", G_PRIORITY_HIGH }, |
|
1342 |
{ "default", G_PRIORITY_DEFAULT }, |
|
1343 |
{ "idle-high", G_PRIORITY_HIGH_IDLE }, |
|
1344 |
{ "idle", G_PRIORITY_DEFAULT_IDLE }, |
|
1345 |
{ "low", G_PRIORITY_LOW }, |
|
1346 |
{ NULL, G_PRIORITY_DEFAULT }, |
|
1347 |
}; |
|
1348 |
||
1349 |
/// hook function |
|
1350 |
/// Function to be called, when hook will be processsed. |
|
1351 |
/// XXX: we can provide object as argument, but is this necessary? |
|
1352 |
/// A: table (arguments hash) |
|
1353 |
/// R: hook handler result |
|
1354 |
static guint lua_hook_cb (const gchar *hookid, hk_arg_t *args, gpointer data) |
|
1355 |
{ |
|
1356 |
lua_hook_t *cb = data; |
|
1357 |
hk_arg_t *arg = args; |
|
1358 |
guint ret = HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
1359 |
lua_State *L = cb -> L; |
|
1360 |
||
1361 |
if (cb -> cbref == LUA_NOREF) |
|
1362 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
1363 |
||
1364 |
lua_rawgeti (L, LUA_REGISTRYINDEX, cb -> cbref); |
|
1365 |
if (!lua_isfunction (L, -1)) { |
|
1366 |
lua_pop (L, 1); |
|
1367 |
return HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
1368 |
} |
|
1369 |
||
1370 |
lua_newtable (L); |
|
1371 |
lua_pushliteral (L, "hook"); |
|
1372 |
lua_pushstring (L, hookid); |
|
1373 |
lua_settable (L, -3); |
|
1374 |
while (arg->name != NULL) { |
|
1375 |
char *name = from_utf8 (arg->name); |
|
1376 |
char *value = from_utf8 (arg->value); |
|
1377 |
lua_pushstring (L, name); |
|
1378 |
lua_pushstring (L, value); |
|
1379 |
lua_settable (L, -3); |
|
1380 |
g_free (name); |
|
1381 |
g_free (value); |
|
1382 |
arg++; |
|
1383 |
} |
|
1384 |
||
1385 |
if (lua_pcall (L, 1, 1, 0)) { |
|
1386 |
scr_log_print (LPRINT_NORMAL, "lua: Error in hook handler: %s", lua_tostring (L, -1)); |
|
1387 |
lua_pop (L, 1); |
|
1388 |
} else { |
|
1389 |
switch (lua_type (L, -1)) { |
|
1390 |
case LUA_TSTRING: |
|
1391 |
case LUA_TNUMBER: |
|
1392 |
ret = luaL_checkenum (L, -1, lua_hook_handler_result); |
|
1393 |
break; |
|
1394 |
default: |
|
1395 |
ret = HOOK_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
1396 |
break; |
|
1397 |
} |
|
1398 |
lua_pop (L, 1); |
|
1399 |
} |
|
1400 |
||
1401 |
return ret; |
|
1402 |
} |
|
1403 |
||
1404 |
/// main.hook |
|
1405 |
/// Installs hook handler, returns an object, that you need to keep until |
|
1406 |
/// hook handling is no more needed. |
|
1407 |
/// A: string (hook name), hook function, integer (priority, optional) |
|
1408 |
/// R: userdata (hook object) |
|
1409 |
static int lua_main_hook (lua_State *L) |
|
1410 |
{ |
|
1411 |
const char *hook_name = luaL_checkstring (L, 1); |
|
1412 |
int priority = G_PRIORITY_DEFAULT; |
|
1413 |
lua_hook_t *cb; |
|
1414 |
||
1415 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
1416 |
||
1417 |
if (lua_gettop (L) > 2) |
|
1418 |
priority = luaL_checkenum (L, 3, lua_hook_handler_priority); |
|
1419 |
||
1420 |
cb = lua_newuserdata (L, sizeof (lua_hook_t)); |
|
1421 |
luaL_getmetatable (L, "mcabber.hook"); |
|
1422 |
lua_setmetatable (L, -2); |
|
1423 |
||
1424 |
lua_pushvalue (L, -1); |
|
1425 |
cb -> selfref = luaL_ref (L, LUA_REGISTRYINDEX); |
|
1426 |
lua_pushvalue (L, 1); |
|
1427 |
cb -> nameref = luaL_ref (L, LUA_REGISTRYINDEX); |
|
1428 |
lua_pushvalue (L, 2); |
|
1429 |
cb -> cbref = luaL_ref (L, LUA_REGISTRYINDEX); |
|
1430 |
cb -> L = L; |
|
1431 |
cb -> hid = hk_add_handler (lua_hook_cb, hook_name, priority, cb); |
|
1432 |
||
1433 |
return 1; |
|
1434 |
} |
|
1435 |
||
1436 |
static void lua_mcabber_unregister_hook (lua_State *L, lua_hook_t *cb) |
|
1437 |
{ |
|
1438 |
const char *name; |
|
1439 |
||
1440 |
if (!cb -> hid || cb -> nameref == LUA_NOREF) |
|
1441 |
return; |
|
1442 |
||
1443 |
lua_rawgeti (L, LUA_REGISTRYINDEX, cb -> nameref); |
|
1444 |
name = lua_tostring (L, -1); |
|
1445 |
if (name) { |
|
1446 |
hk_del_handler (name, cb -> hid); |
|
1447 |
cb -> hid = 0; |
|
1448 |
} |
|
1449 |
||
1450 |
lua_pop (L, 1); |
|
1451 |
} |
|
1452 |
||
1453 |
/// hook:del |
|
1454 |
/// Unregisters given hook handler from mcabber. Object will be destroyed later. |
|
1455 |
static int lua_mcabber_hook_del (lua_State *L) |
|
1456 |
{ |
|
1457 |
lua_hook_t *cb = luaL_checkudata (L, 1, "mcabber.hook"); |
|
1458 |
luaL_argcheck (L, cb != NULL, 1, "mcabber hook object expected"); |
|
1459 |
lua_mcabber_unregister_hook (L, cb); |
|
1460 |
if (cb -> selfref != LUA_NOREF) { |
|
1461 |
luaL_unref (L, LUA_REGISTRYINDEX, cb -> selfref); |
|
1462 |
cb -> selfref = LUA_NOREF; |
|
1463 |
} |
|
1464 |
return 0; |
|
1465 |
} |
|
1466 |
||
1467 |
static int lua_mcabber_hook_gc (lua_State *L) |
|
1468 |
{ |
|
1469 |
lua_hook_t *cb = luaL_checkudata (L, 1, "mcabber.hook"); |
|
1470 |
luaL_argcheck (L, cb != NULL, 1, "mcabber hook object expected"); |
|
1471 |
lua_mcabber_unregister_hook (L, cb); |
|
1472 |
if (cb -> nameref != LUA_NOREF) |
|
1473 |
luaL_unref (L, LUA_REGISTRYINDEX, cb -> nameref); |
|
1474 |
if (cb -> cbref != LUA_NOREF) |
|
1475 |
luaL_unref (L, LUA_REGISTRYINDEX, cb -> cbref); |
|
1476 |
return 0; |
|
1477 |
} |
|
1478 |
||
1479 |
static const luaL_Reg lua_mcabber_hook_reg_m[] = { |
|
1480 |
{ "del", lua_mcabber_hook_del }, |
|
1481 |
{ "__gc", lua_mcabber_hook_gc }, |
|
1482 |
{ NULL, NULL }, |
|
1483 |
}; |
|
1484 |
||
1485 |
static void lua_hook_init (lua_State *L) |
|
1486 |
{ |
|
1487 |
luaL_newmetatable (L, "mcabber.hook"); |
|
1488 |
lua_pushvalue (L, -1); |
|
1489 |
lua_setfield (L, -2, "__index"); |
|
1490 |
luaL_register (L, NULL, lua_mcabber_hook_reg_m); |
|
1491 |
lua_pop (L, 1); |
|
1492 |
} |
|
1493 |
||
1494 |
// OPTION GUARDS |
|
1495 |
||
120 | 1496 |
#define MLUA_GUARD_REGISTRY "mcabber_guards" |
103 | 1497 |
|
1498 |
/// guard function |
|
1499 |
/// Function to be called, when option changes it's value. |
|
1500 |
/// Old option value is still accessible through main.option. |
|
1501 |
/// A: string (key), string (new value) |
|
1502 |
/// R: string (value to save in hash table) |
|
1503 |
static gchar *lua_guard_cb (const gchar *key, const gchar *value) |
|
1504 |
{ |
|
120 | 1505 |
lua_State *L = lua; // FIXME |
103 | 1506 |
|
120 | 1507 |
lua_getfield ( L, LUA_REGISTRYINDEX, MLUA_GUARD_REGISTRY ); // +1 |
1508 |
lua_getfield ( L, -1, key ); // +2 |
|
1509 |
if ( ! lua_isfunction ( L, -1 ) ) { |
|
1510 |
lua_pop ( L, 2 ); |
|
1511 |
return g_strdup ( value ); |
|
103 | 1512 |
} |
1513 |
||
120 | 1514 |
lua_pushstring ( L, key ); // +3 |
1515 |
lua_pushstring ( L, value ); // +4 |
|
103 | 1516 |
|
120 | 1517 |
if ( lua_pcall ( L, 2, 1, 0 ) ) { // +2 |
1518 |
scr_log_print ( LPRINT_NORMAL, "lua: Error in hook handler: %s", lua_tostring ( L, -1 ) ); |
|
1519 |
lua_pop ( L, 2 ); |
|
1520 |
return g_strdup ( value ); |
|
1521 |
} else { |
|
1522 |
gchar *result = g_strdup ( lua_tostring ( L, -1 ) ); |
|
1523 |
lua_pop ( L, 2 ); |
|
1524 |
return result; |
|
103 | 1525 |
} |
1526 |
} |
|
1527 |
||
120 | 1528 |
/// main.add_guard |
1529 |
/// Installs option guard for given option. |
|
1530 |
/// A: string ( option name ), guard function |
|
1531 |
/// R: boolean ( success ) |
|
1532 |
static int lua_main_add_guard ( lua_State *L ) |
|
103 | 1533 |
{ |
120 | 1534 |
const char *name = luaL_checkstring ( L, 1 ); |
1535 |
luaL_argcheck ( L, lua_isfunction (L, 2), 2, "function expected" ); |
|
1536 |
lua_settop ( L, 2 ); |
|
103 | 1537 |
|
120 | 1538 |
if ( settings_set_guard ( name, lua_guard_cb ) ) { |
1539 |
lua_getfield ( L, LUA_REGISTRYINDEX, MLUA_GUARD_REGISTRY ); |
|
1540 |
lua_pushvalue ( L, 2 ); |
|
1541 |
lua_setfield ( L, 3, name ); |
|
1542 |
lua_pushboolean ( L, 1 ); |
|
1543 |
} else |
|
1544 |
lua_pushboolean ( L, 0 ); |
|
103 | 1545 |
|
1546 |
return 1; |
|
1547 |
} |
|
1548 |
||
120 | 1549 |
/// main.del_guard |
1550 |
/// Removes option guard from given option. |
|
1551 |
/// By default, lua will refuse to remove guards, not installed |
|
1552 |
/// by lua. Still, you can force guard removal. |
|
1553 |
/// A: string ( option name ), boolean ( force removal ) |
|
1554 |
/// R: boolean ( success ) |
|
1555 |
static int lua_main_del_guard ( lua_State *L ) |
|
103 | 1556 |
{ |
120 | 1557 |
const char *name = luaL_checkstring ( L, 1 ); |
1558 |
lua_settop ( L, 2 ); |
|
1559 |
||
1560 |
lua_getfield ( L, LUA_REGISTRYINDEX, MLUA_GUARD_REGISTRY ); |
|
1561 |
lua_getfield ( L, 3, name ); |
|
103 | 1562 |
|
120 | 1563 |
if ( ! lua_isnil ( L, 4 ) || lua_toboolean ( L, 2 ) ) { |
1564 |
settings_del_guard ( name ); |
|
1565 |
lua_pushnil ( L ); |
|
1566 |
lua_setfield ( L, 3, name ); |
|
1567 |
lua_pushboolean ( L, 1 ); |
|
1568 |
} else |
|
1569 |
lua_pushboolean ( L, 0 ); |
|
1570 |
||
1571 |
return 1; |
|
103 | 1572 |
} |
1573 |
||
1574 |
static void lua_guard_init (lua_State *L) |
|
1575 |
{ |
|
120 | 1576 |
lua_createtable ( L, 0, 0 ); |
1577 |
lua_setfield ( L, LUA_REGISTRYINDEX, MLUA_GUARD_REGISTRY ); |
|
103 | 1578 |
} |
1579 |
||
1580 |
static void lua_guard_uninit (lua_State *L) |
|
1581 |
{ |
|
120 | 1582 |
lua_getfield ( L, LUA_REGISTRYINDEX, MLUA_GUARD_REGISTRY ); |
1583 |
lua_pushnil ( L ); |
|
1584 |
while ( lua_next ( L, -2 ) ) { |
|
1585 |
const char *key = lua_tostring ( L, -2 ); |
|
1586 |
settings_del_guard ( key ); |
|
1587 |
lua_pop ( L, 1 ); |
|
1588 |
} |
|
1589 |
lua_pop ( L, 1 ); |
|
103 | 1590 |
} |
1591 |
||
1592 |
// MAIN INITIALIZATION CODE |
|
1593 |
||
1594 |
#ifdef LLM_LOG_HANDLER |
|
1595 |
// FIXME: this should not be here |
|
1596 |
guint lua_lm_log_handler_id; |
|
1597 |
||
1598 |
void lua_lm_log_handler (const gchar *domain, GLogLevelFlags log_level, const gchar *message, gpointer ignore) |
|
1599 |
{ |
|
121
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
1600 |
if (settings_opt_get_int (OPT_MLUA_LM_DEBUG)) |
103 | 1601 |
scr_log_print (LPRINT_LOGNORM, "%s: %s", domain, message); |
1602 |
} |
|
1603 |
#endif |
|
1604 |
||
1605 |
static void do_lua(char *arg, lua_State *L) |
|
1606 |
{ |
|
1607 |
if (luaL_loadbuffer (L, arg, strlen (arg), "line")) { |
|
1608 |
scr_log_print (LPRINT_LOGNORM, "lua: Compilation error: %s", lua_tostring (L, -1)); |
|
1609 |
lua_pop (L, 1); |
|
1610 |
return; |
|
1611 |
} |
|
1612 |
||
1613 |
if (lua_pcall (L, 0, 0, 0)) { |
|
1614 |
scr_log_print (LPRINT_NORMAL, "lua: Runtime error: %s", lua_tostring(L, -1)); |
|
1615 |
lua_pop (L, 1); |
|
1616 |
return; |
|
1617 |
} |
|
1618 |
} |
|
1619 |
||
1620 |
static void *lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { |
|
1621 |
if (nsize == 0) { |
|
1622 |
g_free (ptr); |
|
1623 |
return NULL; |
|
1624 |
} else |
|
1625 |
return g_realloc (ptr, nsize); |
|
1626 |
} |
|
1627 |
||
1628 |
#define reg(NAME) \ |
|
1629 |
{ #NAME, lua_main_##NAME }, |
|
1630 |
static const luaL_Reg lua_reg_main[] = { |
|
1631 |
reg ( yesno ) |
|
1632 |
#ifdef LLM_CONNECTION_ENABLE |
|
1633 |
reg ( connection ) |
|
1634 |
#endif |
|
107 | 1635 |
reg ( version ) |
103 | 1636 |
reg ( log ) |
1637 |
reg ( option ) |
|
1638 |
reg ( alias ) |
|
1639 |
reg ( binding ) |
|
1640 |
reg ( fileoption ) |
|
1641 |
reg ( add_feature ) |
|
1642 |
reg ( del_feature ) |
|
1643 |
reg ( event ) |
|
1644 |
reg ( parse_args ) |
|
1645 |
reg ( add_category ) |
|
1646 |
reg ( del_category ) |
|
1647 |
reg ( add_completion ) |
|
1648 |
reg ( del_completion ) |
|
1649 |
reg ( command ) |
|
1650 |
reg ( print_info ) |
|
1651 |
reg ( beep ) |
|
1652 |
reg ( run ) |
|
1653 |
reg ( status ) |
|
1654 |
reg ( roster ) |
|
1655 |
reg ( current_buddy ) |
|
1656 |
reg ( full_jid ) |
|
1657 |
reg ( buddy_info ) |
|
1658 |
reg ( timer ) |
|
1659 |
reg ( alias ) |
|
1660 |
reg ( binding ) |
|
1661 |
reg ( fileoption ) |
|
1662 |
reg ( add_feature ) |
|
1663 |
reg ( del_feature ) |
|
1664 |
reg ( event ) |
|
1665 |
reg ( parse_args ) |
|
1666 |
reg ( add_category ) |
|
1667 |
reg ( del_category ) |
|
1668 |
reg ( add_completion ) |
|
1669 |
reg ( del_completion ) |
|
1670 |
reg ( command ) |
|
1671 |
reg ( print_info ) |
|
1672 |
reg ( beep ) |
|
1673 |
reg ( run ) |
|
1674 |
reg ( status ) |
|
1675 |
reg ( roster ) |
|
1676 |
reg ( current_buddy ) |
|
1677 |
reg ( full_jid ) |
|
1678 |
reg ( buddy_info ) |
|
1679 |
reg ( timer ) |
|
1680 |
reg ( bgread ) |
|
1681 |
reg ( hook ) |
|
120 | 1682 |
reg ( add_guard ) |
1683 |
reg ( del_guard ) |
|
103 | 1684 |
{ NULL, NULL }, |
1685 |
}; |
|
1686 |
#undef reg |
|
1687 |
||
1688 |
const gchar *g_module_check_init (GModule *module) |
|
1689 |
{ |
|
1690 |
lua = lua_newstate (lua_alloc, NULL); |
|
1691 |
if (!lua) |
|
1692 |
return "Lua initialization error"; |
|
1693 |
else |
|
1694 |
return NULL; |
|
1695 |
} |
|
1696 |
||
1697 |
void g_module_unload (GModule *module) |
|
1698 |
{ |
|
1699 |
if (lua) { |
|
1700 |
lua_close (lua); |
|
1701 |
lua = NULL; |
|
1702 |
} |
|
1703 |
} |
|
1704 |
||
115
47bed161e3b0
Add AVV description file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
113
diff
changeset
|
1705 |
void mlua_init (void) |
103 | 1706 |
{ |
1707 |
luaL_openlibs (lua); |
|
1708 |
||
1709 |
luaL_register (lua, "main", lua_reg_main); |
|
1710 |
lua_pop (lua, 1); // XXX |
|
1711 |
lua_register (lua, "dopath", lua_global_dopath); |
|
1712 |
lua_register (lua, "print", lua_global_print ); |
|
1713 |
||
1714 |
{ |
|
1715 |
int cid = compl_new_category (); |
|
1716 |
||
1717 |
if (cid) { |
|
1718 |
const string2enum_t *word = lua_yesno; |
|
1719 |
lua_completion_type[MLUA_YESNO_POS].value = cid; |
|
112
15f4c09ffb86
Hopefully fix size issues on 64-bit architectures
Myhailo Danylenko <isbear@ukrpost.net>
parents:
110
diff
changeset
|
1720 |
lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) ((gsize) cid)); |
103 | 1721 |
while (word->string) { |
1722 |
compl_add_category_word (cid, word->string); |
|
1723 |
++word; |
|
1724 |
} |
|
1725 |
} |
|
1726 |
} |
|
1727 |
||
113 | 1728 |
#ifdef MCABBER_API_HAVE_CMD_ID |
1729 |
lua_cmdid = cmd_add ("lua", "Evaluate lua string", 0, 0, (void (*) (char *p)) do_lua, lua); |
|
1730 |
#else |
|
103 | 1731 |
cmd_add ("lua", "Evaluate lua string", 0, 0, (void (*) (char *p)) do_lua, lua); |
113 | 1732 |
#endif |
103 | 1733 |
|
1734 |
#ifdef LLM_LOG_HANDLER |
|
1735 |
// FIXME: this should not be here. |
|
1736 |
lua_lm_log_handler_id = g_log_set_handler ("lua-lm", G_LOG_LEVEL_MASK, (GLogFunc) lua_lm_log_handler, NULL); |
|
1737 |
#endif |
|
1738 |
||
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1739 |
lua_hook_init (lua); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1740 |
lua_command_init (lua); |
120 | 1741 |
lua_guard_init (lua); |
103 | 1742 |
|
1743 |
{ |
|
121
75a7d595817c
Put help into separate files
Myhailo Danylenko <isbear@ukrpost.net>
parents:
120
diff
changeset
|
1744 |
char *initfile = expand_filename (settings_opt_get (OPT_MLUA_RC)); |
113 | 1745 |
|
103 | 1746 |
if (!initfile) |
1747 |
scr_log_print (LPRINT_LOGNORM, "lua: Cannot determine config file name"); |
|
1748 |
else { |
|
1749 |
if (luaL_loadfile(lua, initfile)) { |
|
1750 |
scr_log_print (LPRINT_LOGNORM, "lua: Unable to compile rc file: %s", lua_tostring (lua, -1)); |
|
1751 |
lua_pop (lua, 1); |
|
1752 |
} else if (lua_pcall (lua, 0, LUA_MULTRET, 0)) { |
|
1753 |
scr_log_print (LPRINT_LOGNORM, "lua: Runtime error in rc file: %s", lua_tostring(lua, -1)); |
|
1754 |
lua_pop (lua, 1); |
|
1755 |
} else |
|
1756 |
scr_log_print (LPRINT_LOGNORM, "lua: Loaded %s", initfile); |
|
1757 |
g_free (initfile); |
|
1758 |
} |
|
1759 |
} |
|
1760 |
||
1761 |
{ |
|
1762 |
hk_arg_t args[] = { |
|
1763 |
{ NULL, NULL }, |
|
1764 |
}; |
|
1765 |
hk_run_handlers("hook-lua-start", args); |
|
1766 |
} |
|
1767 |
} |
|
1768 |
||
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1769 |
static void lua_events_destroy ( gpointer data, gpointer udata ) |
103 | 1770 |
{ |
1771 |
lua_event_callback_t *cb = data; |
|
1772 |
const char *evid; |
|
1773 |
if (cb->evid == LUA_NOREF) |
|
1774 |
return; |
|
1775 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->evid); |
|
1776 |
evid = lua_tostring (cb ->L, -1); |
|
1777 |
evs_callback (evid, EVS_CONTEXT_CANCEL, "Module unloading"); |
|
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1778 |
evs_del (evid); |
103 | 1779 |
} |
1780 |
||
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1781 |
static void lua_bgreads_destroy ( gpointer data, gpointer udata ) |
103 | 1782 |
{ |
120 | 1783 |
g_source_remove ( ( gulong ) data ); |
103 | 1784 |
} |
1785 |
||
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1786 |
static void lua_timers_destroy ( gpointer data, gpointer udata ) |
103 | 1787 |
{ |
120 | 1788 |
g_source_remove ( ( gulong ) data ); |
103 | 1789 |
} |
1790 |
||
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1791 |
static void lua_features_destroy ( gpointer data, gpointer udata ) |
103 | 1792 |
{ |
120 | 1793 |
gchar *xmlns = data; |
103 | 1794 |
xmpp_del_feature (xmlns); |
1795 |
g_free (xmlns); |
|
1796 |
} |
|
1797 |
||
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1798 |
static void lua_categories_destroy ( gpointer data, gpointer udata ) |
103 | 1799 |
{ |
120 | 1800 |
compl_del_category ( ( gulong ) data ); |
103 | 1801 |
} |
1802 |
||
115
47bed161e3b0
Add AVV description file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
113
diff
changeset
|
1803 |
void mlua_uninit (void) |
103 | 1804 |
{ |
1805 |
if (lua) { |
|
1806 |
hk_arg_t args[] = { |
|
1807 |
{ NULL, NULL }, |
|
1808 |
}; |
|
1809 |
hk_run_handlers ("hook-lua-quit", args); |
|
1810 |
||
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1811 |
// hook handlers and commands will be unregistered upon objects destruction |
103 | 1812 |
|
120 | 1813 |
lua_guard_uninit ( lua ); |
1814 |
||
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1815 |
g_slist_foreach ( lua_bgreads, lua_bgreads_destroy, NULL ); |
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1816 |
g_slist_free ( lua_bgreads ); |
103 | 1817 |
lua_bgreads = NULL; |
1818 |
||
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1819 |
g_slist_foreach ( lua_timers, lua_timers_destroy, NULL ); |
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1820 |
g_slist_free ( lua_timers ); |
103 | 1821 |
lua_timers = NULL; |
1822 |
||
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1823 |
g_slist_foreach ( lua_events, lua_events_destroy, NULL ); |
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1824 |
g_slist_free ( lua_events ); |
103 | 1825 |
lua_events = NULL; |
1826 |
||
113 | 1827 |
#ifdef MCABBER_API_HAVE_CMD_ID |
1828 |
cmd_del (lua_cmdid); |
|
1829 |
lua_cmdid = NULL; |
|
1830 |
#else |
|
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1831 |
cmd_del ("lua"); |
113 | 1832 |
#endif |
109
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1833 |
|
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1834 |
lua_close (lua); |
2d2111cb5109
Switch to object-based command handling
Myhailo Danylenko <isbear@ukrpost.net>
parents:
108
diff
changeset
|
1835 |
lua = NULL; |
103 | 1836 |
|
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1837 |
g_slist_foreach ( lua_added_features, lua_features_destroy, NULL ); |
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1838 |
g_slist_free ( lua_added_features ); |
120 | 1839 |
lua_added_features = NULL; |
1840 |
||
123
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1841 |
g_slist_foreach ( lua_added_categories, lua_categories_destroy, NULL ); |
6b753fbacd4c
Do not use g_slist_free_full (for now)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
122
diff
changeset
|
1842 |
g_slist_free ( lua_added_categories ); |
103 | 1843 |
lua_added_categories = NULL; |
1844 |
||
1845 |
#ifdef LLM_LOG_HANDLER |
|
1846 |
// FIXME: shouldn't be here |
|
1847 |
g_log_remove_handler ("lua-lm", lua_lm_log_handler_id); |
|
1848 |
#endif |
|
1849 |
} |
|
1850 |
} |
|
1851 |