18 #include "xmpp_helper.h" // xmpp_add_feature, xmpp_del_feature |
18 #include "xmpp_helper.h" // xmpp_add_feature, xmpp_del_feature |
19 #include "roster.h" // imstatus2char, foreach_buddy, buddy_*, current_buddy, BUDDATA, ROSTER_TYPE_* |
19 #include "roster.h" // imstatus2char, foreach_buddy, buddy_*, current_buddy, BUDDATA, ROSTER_TYPE_* |
20 #include "utils.h" // from_utf8, jidtodisp |
20 #include "utils.h" // from_utf8, jidtodisp |
21 #include "hooks.h" // hk_add_handler, hk_del_handler |
21 #include "hooks.h" // hk_add_handler, hk_del_handler |
22 #include "settings.h" // settings_set, settings_del, settings_get |
22 #include "settings.h" // settings_set, settings_del, settings_get |
|
23 #include "compl.h" // compl_new_category, compl_add_category_word, compl_del_category_word |
23 |
24 |
24 |
25 |
25 // global lua state object, necessary for uninitialization function |
26 // global lua state object, necessary for uninitialization function |
26 static lua_State *lua = NULL; |
27 static lua_State *lua = NULL; |
27 |
28 |
339 scr_LogPrint (LPRINT_LOGNORM, "lua: Command execution error: %s", lua_tostring (cb->L, -1)); |
340 scr_LogPrint (LPRINT_LOGNORM, "lua: Command execution error: %s", lua_tostring (cb->L, -1)); |
340 lua_pop (cb->L, 1); |
341 lua_pop (cb->L, 1); |
341 } |
342 } |
342 } |
343 } |
343 |
344 |
|
345 /// main.add_completion |
|
346 /// Adds word to a completion list. |
|
347 /// A: integer (completion group id), string (word) |
|
348 static int lua_main_add_completion (lua_State *L) |
|
349 { |
|
350 guint cid = luaL_checkinteger (L, 1); |
|
351 const char *word = luaL_checkstring (L, 2); |
|
352 compl_add_category_word (cid, word); |
|
353 } |
|
354 |
|
355 /// main.del_completion |
|
356 /// Removes word from a completion list. |
|
357 /// A: integer (completion group id), string (word) |
|
358 static int lua_main_del_completion (lua_State *L) |
|
359 { |
|
360 guint cid = luaL_checkinteger (L, 1); |
|
361 const char *word = luaL_checkstring (L, 2); |
|
362 compl_del_category_word (cid, word); |
|
363 } |
|
364 |
344 /// main.command |
365 /// main.command |
345 /// Associates or breaks association between mcabber command name and lua function. |
366 /// Associates or breaks association between mcabber command name and lua function. |
346 /// To unregister command omit function argument. |
367 /// To unregister command omit function argument. |
347 /// A: string (command name), command function (optional) |
368 /// If you specify a third argument, table (even empty), function will return completion group id or nothing. |
|
369 /// You can also specify an integer instead of table, then no new id will be registered. |
|
370 /// Note, that for now there are no way to unregister completion group, so, resources can be exausted easily. |
|
371 /// Also note, that it ignores keys in a completion table. |
|
372 /// A: string (command name), command function (optional), table (completions, optional)/integer (comletion group id, optional) |
|
373 /// R: integer (completion group id, optional) or nil |
348 static int lua_main_command (lua_State *L) |
374 static int lua_main_command (lua_State *L) |
349 { |
375 { |
350 const char *name = luaL_checkstring (L, 1); |
376 const char *name = luaL_checkstring (L, 1); |
351 lua_command_callback_t *cb; |
377 lua_command_callback_t *cb; |
352 if (lua_gettop (L) > 1) { // Register |
378 if (lua_gettop (L) > 1) { // Register |
|
379 guint cid = 0; |
353 luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
380 luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
354 |
381 |
|
382 if (lua_gettop (L) > 2) { // Completions provided |
|
383 if (lua_type (L, 3) == LUA_TTABLE) { |
|
384 cid = compl_new_category (); |
|
385 if (cid) { |
|
386 lua_pushnil (L); |
|
387 while (lua_next (L, 3)) { |
|
388 compl_add_category_word (cid, luaL_checkstring (L, -1)); |
|
389 lua_pop (L, 1); |
|
390 } |
|
391 } |
|
392 } else |
|
393 cid = luaL_checkinteger (L, 3); |
|
394 } |
|
395 |
355 cb = luaL_malloc (L, sizeof (lua_command_callback_t)); |
396 cb = luaL_malloc (L, sizeof (lua_command_callback_t)); |
|
397 lua_pushvalue (L, 2); |
356 cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
398 cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
357 cb->L = L; |
399 cb->L = L; |
358 cmd_add (name, "", 0, 0, (void (*) (char *p)) lua_main_command_handler, cb); |
400 cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb); |
359 |
401 |
360 lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name)); |
402 lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name)); |
|
403 |
|
404 if (cid) { |
|
405 lua_pushinteger (L, cid); |
|
406 return 1; |
|
407 } |
361 } else { // Unregister |
408 } else { // Unregister |
362 GSList *el = g_slist_find_custom (lua_added_commands, name, (GCompareFunc) g_strcmp0); |
409 GSList *el = g_slist_find_custom (lua_added_commands, name, (GCompareFunc) g_strcmp0); |
363 |
410 |
364 cb = cmd_del (name); |
411 cb = cmd_del (name); |
365 if (cb) { |
412 if (cb) { |
588 { "connection", lua_main_connection }, |
635 { "connection", lua_main_connection }, |
589 { "log", lua_main_log }, |
636 { "log", lua_main_log }, |
590 { "option", lua_main_option }, |
637 { "option", lua_main_option }, |
591 { "add_feature", lua_main_add_feature }, |
638 { "add_feature", lua_main_add_feature }, |
592 { "del_feature", lua_main_del_feature }, |
639 { "del_feature", lua_main_del_feature }, |
|
640 { "add_completion",lua_main_add_completion}, |
|
641 { "del_completion",lua_main_del_completion}, |
593 { "command", lua_main_command }, |
642 { "command", lua_main_command }, |
594 { "print_info", lua_main_print_info }, |
643 { "print_info", lua_main_print_info }, |
595 { "beep", lua_main_beep }, |
644 { "beep", lua_main_beep }, |
596 { "run", lua_main_run }, |
645 { "run", lua_main_run }, |
597 { "status", lua_main_status }, |
646 { "status", lua_main_status }, |