From 0ae80564fd70c7f6111f278c884c3651e7afac8d Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 16 Apr 2017 15:53:33 +1000 Subject: [PATCH] Add lua script option to menu items --- bbs.h | 1 + lua_glue.c | 21 +++++++++++++++++++++ menus.c | 8 +++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/bbs.h b/bbs.h index 6f76e0f..87f0cc8 100644 --- a/bbs.h +++ b/bbs.h @@ -297,6 +297,7 @@ extern void next_file_sub(struct user_record *user); extern void prev_file_sub(struct user_record *user); extern void lua_push_cfunctions(lua_State *L); +extern void do_lua_script(char *script); extern void bwave_create_packet(); extern void bwave_upload_reply(); diff --git a/lua_glue.c b/lua_glue.c index dee8209..9b468c9 100644 --- a/lua_glue.c +++ b/lua_glue.c @@ -180,3 +180,24 @@ void lua_push_cfunctions(lua_State *L) { lua_pushcfunction(L, l_getBBSInfo); lua_setglobal(L, "bbs_get_info"); } + +void do_lua_script(char *script) { + lua_State *L; + char buffer[PATH_MAX]; + + if (script == NULL) { + return; + } + + if (script[0] == '/') { + snprintf(buffer, PATH_MAX, "%s.lua", script); + } else { + snprintf(buffer, PATH_MAX, "%s/%s.lua", conf.script_path, script); + } + + L = luaL_newstate(); + luaL_openlibs(L); + lua_push_cfunctions(L); + luaL_dofile(L, buffer); + lua_close(L); +} \ No newline at end of file diff --git a/menus.c b/menus.c index 70e6853..03c1f11 100644 --- a/menus.c +++ b/menus.c @@ -43,6 +43,7 @@ #define MENU_NEXTFILESUB 34 #define MENU_PREVFILESUB 35 #define MENU_LISTMESSAGES 36 +#define MENU_DOSCRIPT 37 extern struct bbs_config conf; extern struct user_record *gUser; @@ -176,6 +177,8 @@ int menu_system(char *menufile) { menu[menu_items-1]->command = MENU_PREVFILESUB; } else if (strncasecmp(&buffer[8], "LISTMESSAGES", 12) == 0) { menu[menu_items-1]->command = MENU_LISTMESSAGES; + } else if (strncasecmp(&buffer[8], "DOSCRIPT", 8) == 0) { + menu[menu_items-1]->command = MENU_DOSCRIPT; } } else if (strncasecmp(buffer, "DATA", 4) == 0) { menu[menu_items-1]->data = strdup(&buffer[5]); @@ -419,7 +422,10 @@ int menu_system(char *menufile) { break; case MENU_LISTMESSAGES: list_messages(gUser); - break; + break; + case MENU_DOSCRIPT: + do_lua_script(menu[i]->data); + break; } break; }