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