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