From f3d8ea9d71ec018cde1b3c9070a3e10f093d54cd Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Tue, 12 Apr 2016 15:19:56 +1000 Subject: [PATCH] Prompts/Menus can now be handled by lua --- bbs.c | 2 ++ doors.c | 61 ++++++++++++++++++++++++++++++----- files.c | 58 +++++++++++++++++++++++++++++---- lua_glue.c | 30 +++++++++++++++++ mail_menu.c | 61 +++++++++++++++++++++++++++++------ main_menu.c | 54 ++++++++++++++++++++++++++++--- scripts_examples/doors.lua | 20 ++++++++++++ scripts_examples/filemenu.lua | 24 ++++++++++++++ scripts_examples/mailmenu.lua | 24 ++++++++++++++ scripts_examples/mainmenu.lua | 20 ++++++++++++ 10 files changed, 326 insertions(+), 28 deletions(-) create mode 100644 scripts_examples/doors.lua create mode 100644 scripts_examples/filemenu.lua create mode 100644 scripts_examples/mailmenu.lua create mode 100644 scripts_examples/mainmenu.lua diff --git a/bbs.c b/bbs.c index 5037e32..ddcc7d5 100644 --- a/bbs.c +++ b/bbs.c @@ -13,6 +13,7 @@ #include "inih/ini.h" #include "bbs.h" #include "lua/lua.h" +#include "lua/lualib.h" #include "lua/lauxlib.h" int mynode; @@ -836,6 +837,7 @@ void runbbs(int socket, char *config_path) { luaL_openlibs(L); lua_push_cfunctions(L); luaL_dofile(L, buffer); + lua_close(L); do_internal_login = 0; } else { do_internal_login = 1; diff --git a/doors.c b/doors.c index 83169a8..e9e7438 100644 --- a/doors.c +++ b/doors.c @@ -16,6 +16,9 @@ # include #endif #include "bbs.h" +#include "lua/lua.h" +#include "lua/lualib.h" +#include "lua/lauxlib.h" extern struct bbs_config conf; extern int mynode; @@ -234,16 +237,56 @@ int door_menu(int socket, struct user_record *user) { int doquit = 0; int dodoors = 0; char prompt[128]; + char buffer[256]; int i; char c; + struct stat s; + int do_internal_menu = 0; + char *lRet; + lua_State *L; + int result; + + if (conf.script_path != NULL) { + sprintf(buffer, "%s/doors.lua", conf.script_path); + if (stat(buffer, &s) == 0) { + L = luaL_newstate(); + luaL_openlibs(L); + lua_push_cfunctions(L); + luaL_loadfile(L, buffer); + do_internal_menu = 0; + result = lua_pcall(L, 0, 1, 0); + if (result) { + fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1)); + do_internal_menu = 1; + } + } else { + do_internal_menu = 1; + } + } else { + do_internal_menu = 1; + } + while (!dodoors) { - s_displayansi(socket, "doors"); - - sprintf(prompt, "\e[0m\r\nTL: %dm :> ", user->timeleft); - s_putstring(socket, prompt); - - c = s_getc(socket); - + if (do_internal_menu == 1) { + s_displayansi(socket, "doors"); + + sprintf(prompt, "\e[0m\r\nTL: %dm :> ", user->timeleft); + s_putstring(socket, prompt); + + c = s_getc(socket); + } else { + lua_getglobal(L, "menu"); + result = lua_pcall(L, 0, 1, 0); + if (result) { + fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1)); + do_internal_menu = 1; + lua_close(L); + continue; + } + lRet = (char *)lua_tostring(L, -1); + lua_pop(L, 1); + c = lRet[0]; + } switch(tolower(c)) { case 'q': dodoors = 1; @@ -272,6 +315,8 @@ int door_menu(int socket, struct user_record *user) { break; } } - + if (do_internal_menu == 0) { + lua_close(L); + } return doquit; } diff --git a/files.c b/files.c index a1e128a..6b947f1 100644 --- a/files.c +++ b/files.c @@ -9,7 +9,9 @@ #include #include "Xmodem/zmodem.h" #include "bbs.h" - +#include "lua/lua.h" +#include "lua/lualib.h" +#include "lua/lauxlib.h" extern struct bbs_config conf; static int doCancel = 0; @@ -585,14 +587,53 @@ int file_menu(int socket, struct user_record *user) { int i; int j; char prompt[256]; + struct stat s; + int do_internal_menu = 0; + char *lRet; + lua_State *L; + int result; + + if (conf.script_path != NULL) { + sprintf(prompt, "%s/filemenu.lua", conf.script_path); + if (stat(prompt, &s) == 0) { + L = luaL_newstate(); + luaL_openlibs(L); + lua_push_cfunctions(L); + luaL_loadfile(L, prompt); + do_internal_menu = 0; + result = lua_pcall(L, 0, 1, 0); + if (result) { + fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1)); + do_internal_menu = 1; + } + } else { + do_internal_menu = 1; + } + } else { + do_internal_menu = 1; + } while (!dofiles) { - s_displayansi(socket, "filemenu"); - - sprintf(prompt, "\e[0m\r\nDir: (%d) %s\r\nSub: (%d) %s\r\nTL: %dm :> ", user->cur_file_dir, conf.file_directories[user->cur_file_dir]->name, user->cur_file_sub, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->name, user->timeleft); - s_putstring(socket, prompt); - - c = s_getc(socket); + if (do_internal_menu == 1) { + s_displayansi(socket, "filemenu"); + + sprintf(prompt, "\e[0m\r\nDir: (%d) %s\r\nSub: (%d) %s\r\nTL: %dm :> ", user->cur_file_dir, conf.file_directories[user->cur_file_dir]->name, user->cur_file_sub, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->name, user->timeleft); + s_putstring(socket, prompt); + + c = s_getc(socket); + } else { + lua_getglobal(L, "menu"); + result = lua_pcall(L, 0, 1, 0); + if (result) { + fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1)); + do_internal_menu = 1; + lua_close(L); + continue; + } + lRet = (char *)lua_tostring(L, -1); + lua_pop(L, 1); + c = lRet[0]; + } switch(tolower(c)) { case 'i': { @@ -734,5 +775,8 @@ int file_menu(int socket, struct user_record *user) { break; } } + if (do_internal_menu == 0) { + lua_close(L); + } return doquit; } diff --git a/lua_glue.c b/lua_glue.c index b17c345..a8823f4 100644 --- a/lua_glue.c +++ b/lua_glue.c @@ -106,6 +106,30 @@ int l_bbsRunDoor(lua_State *L) { return 0; } +int l_bbsTimeLeft(lua_State *L) { + lua_pushnumber(L, gUser->timeleft); + + return 1; +} + +int l_getMailAreaInfo(lua_State *L) { + lua_pushnumber(L, gUser->cur_mail_conf); + lua_pushstring(L, conf.mail_conferences[gUser->cur_mail_conf]->name); + lua_pushnumber(L, gUser->cur_mail_area); + lua_pushstring(L, conf.mail_conferences[gUser->cur_mail_conf]->mail_areas[gUser->cur_mail_area]->name); + + return 4; +} + +int l_getFileAreaInfo(lua_State *L) { + lua_pushnumber(L, gUser->cur_file_dir); + lua_pushstring(L, conf.file_directories[gUser->cur_file_dir]->name); + lua_pushnumber(L, gUser->cur_file_sub); + lua_pushstring(L, conf.file_directories[gUser->cur_file_dir]->file_subs[gUser->cur_file_sub]->name); + + return 4; +} + void lua_push_cfunctions(lua_State *L) { lua_pushcfunction(L, l_bbsWString); lua_setglobal(L, "bbs_write_string"); @@ -127,4 +151,10 @@ void lua_push_cfunctions(lua_State *L) { lua_setglobal(L, "bbs_mail_scan"); lua_pushcfunction(L, l_bbsRunDoor); lua_setglobal(L, "bbs_run_door"); + lua_pushcfunction(L, l_bbsTimeLeft); + lua_setglobal(L, "bbs_time_left"); + lua_pushcfunction(L, l_getMailAreaInfo); + lua_setglobal(L, "bbs_cur_mailarea_info"); + lua_pushcfunction(L, l_getFileAreaInfo); + lua_setglobal(L, "bbs_cur_filearea_info"); } diff --git a/mail_menu.c b/mail_menu.c index 21e26dc..bfa0db2 100644 --- a/mail_menu.c +++ b/mail_menu.c @@ -7,6 +7,9 @@ #include #include "jamlib/jam.h" #include "bbs.h" +#include "lua/lua.h" +#include "lua/lualib.h" +#include "lua/lauxlib.h" extern struct bbs_config conf; extern int mynode; @@ -1080,16 +1083,54 @@ int mail_menu(int socket, struct user_record *user) { char *dest_addr; int to_us; int wwiv_to; + struct stat s; + int do_internal_menu = 0; + char *lRet; + lua_State *L; + int result; + + if (conf.script_path != NULL) { + sprintf(buffer, "%s/mailmenu.lua", conf.script_path); + if (stat(buffer, &s) == 0) { + L = luaL_newstate(); + luaL_openlibs(L); + lua_push_cfunctions(L); + luaL_loadfile(L, buffer); + do_internal_menu = 0; + result = lua_pcall(L, 0, 1, 0); + if (result) { + fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1)); + do_internal_menu = 1; + } + } else { + do_internal_menu = 1; + } + } else { + do_internal_menu = 1; + } while (!domail) { - s_displayansi(socket, "mailmenu"); - - - sprintf(prompt, "\e[0m\r\nConf: (%d) %s\r\nArea: (%d) %s\r\nTL: %dm :> ", user->cur_mail_conf, conf.mail_conferences[user->cur_mail_conf]->name, user->cur_mail_area, conf.mail_conferences[user->cur_mail_conf]->mail_areas[user->cur_mail_area]->name, user->timeleft); - s_putstring(socket, prompt); - - c = s_getc(socket); - + if (do_internal_menu == 1) { + s_displayansi(socket, "mailmenu"); + + + sprintf(prompt, "\e[0m\r\nConf: (%d) %s\r\nArea: (%d) %s\r\nTL: %dm :> ", user->cur_mail_conf, conf.mail_conferences[user->cur_mail_conf]->name, user->cur_mail_area, conf.mail_conferences[user->cur_mail_conf]->mail_areas[user->cur_mail_area]->name, user->timeleft); + s_putstring(socket, prompt); + + c = s_getc(socket); + } else { + lua_getglobal(L, "menu"); + result = lua_pcall(L, 0, 1, 0); + if (result) { + fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1)); + do_internal_menu = 1; + lua_close(L); + continue; + } + lRet = (char *)lua_tostring(L, -1); + lua_pop(L, 1); + c = lRet[0]; + } switch(tolower(c)) { case 'p': { @@ -1556,7 +1597,9 @@ int mail_menu(int socket, struct user_record *user) { break; } } - + if (do_internal_menu == 0) { + lua_close(L); + } return doquit; } diff --git a/main_menu.c b/main_menu.c index e3c6a5a..65579a6 100644 --- a/main_menu.c +++ b/main_menu.c @@ -4,6 +4,9 @@ #include #include #include "bbs.h" +#include "lua/lua.h" +#include "lua/lualib.h" +#include "lua/lauxlib.h" extern struct bbs_config conf; @@ -14,14 +17,54 @@ void main_menu(int socket, struct user_record *user) { char buffer[256]; int i; struct stat s; + int do_internal_menu = 0; + char *lRet; + lua_State *L; + int result; + + if (conf.script_path != NULL) { + sprintf(buffer, "%s/mainmenu.lua", conf.script_path); + if (stat(buffer, &s) == 0) { + L = luaL_newstate(); + luaL_openlibs(L); + lua_push_cfunctions(L); + luaL_loadfile(L, buffer); + do_internal_menu = 0; + result = lua_pcall(L, 0, 1, 0); + if (result) { + fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1)); + do_internal_menu = 1; + } + } else { + do_internal_menu = 1; + } + } else { + do_internal_menu = 1; + } + while (!doquit) { - s_displayansi(socket, "mainmenu"); + + if (do_internal_menu == 1) { + s_displayansi(socket, "mainmenu"); - sprintf(prompt, "\r\n\e[0mTL: %dm :> ", user->timeleft); - s_putstring(socket, prompt); + sprintf(prompt, "\r\n\e[0mTL: %dm :> ", user->timeleft); + s_putstring(socket, prompt); - c = s_getc(socket); + c = s_getc(socket); + } else { + lua_getglobal(L, "menu"); + result = lua_pcall(L, 0, 1, 0); + if (result) { + fprintf(stderr, "Failed to run script: %s\n", lua_tostring(L, -1)); + do_internal_menu = 1; + lua_close(L); + continue; + } + lRet = (char *)lua_tostring(L, -1); + lua_pop(L, 1); + c = lRet[0]; + } switch(tolower(c)) { case 'a': @@ -126,4 +169,7 @@ void main_menu(int socket, struct user_record *user) { break; } } + if (do_internal_menu == 0) { + lua_close(L); + } } diff --git a/scripts_examples/doors.lua b/scripts_examples/doors.lua new file mode 100644 index 0000000..df40609 --- /dev/null +++ b/scripts_examples/doors.lua @@ -0,0 +1,20 @@ + +function menu() + -- display menu ansi + bbs_display_ansi("doors"); + + + -- display prompt + bbs_write_string("\r\n\027[0m(LUA) TL: " .. string.format("%d", bbs_time_left()) .. "m > "); + + + -- read char entered + cmd = bbs_read_char(); + + -- do stuff if you want + + + -- return the char entered + + return cmd; +end diff --git a/scripts_examples/filemenu.lua b/scripts_examples/filemenu.lua new file mode 100644 index 0000000..5a84321 --- /dev/null +++ b/scripts_examples/filemenu.lua @@ -0,0 +1,24 @@ +function menu() + -- display menu ansi + bbs_display_ansi("filemenu"); + + + -- display prompt + local dir_no; + local dir_name; + local sub_no; + local sub_name; + + dir_no, dir_name, sub_no, sub_name = bbs_cur_filearea_info(); + bbs_write_string(string.format("\r\n\027[0mDir: (%d) %s\r\nSub: (%d) %s\r\n(LUA) TL: %dm > ", dir_no, dir_name, sub_no, sub_name, bbs_time_left())); + + -- read char entered + cmd = bbs_read_char(); + + -- do stuff if you want + + + -- return the char entered + + return cmd; +end diff --git a/scripts_examples/mailmenu.lua b/scripts_examples/mailmenu.lua new file mode 100644 index 0000000..8e5e123 --- /dev/null +++ b/scripts_examples/mailmenu.lua @@ -0,0 +1,24 @@ +function menu() + -- display menu ansi + bbs_display_ansi("mailmenu"); + + + -- display prompt + local conf_no; + local conf_name; + local area_no; + local area_name; + + conf_no, conf_name, area_no, area_name = bbs_cur_mailarea_info(); + bbs_write_string(string.format("\r\n\027[0mConf: (%d) %s\r\nArea: (%d) %s\r\n(LUA) TL: %dm > ", conf_no, conf_name, area_no, area_name, bbs_time_left())); + + -- read char entered + cmd = bbs_read_char(); + + -- do stuff if you want + + + -- return the char entered + + return cmd; +end diff --git a/scripts_examples/mainmenu.lua b/scripts_examples/mainmenu.lua new file mode 100644 index 0000000..00c43e5 --- /dev/null +++ b/scripts_examples/mainmenu.lua @@ -0,0 +1,20 @@ + +function menu() + -- display menu ansi + bbs_display_ansi("mainmenu"); + + + -- display prompt + bbs_write_string("\r\n\027[0m(LUA) TL: " .. string.format("%d", bbs_time_left()) .. "m > "); + + + -- read char entered + cmd = bbs_read_char(); + + -- do stuff if you want + + + -- return the char entered + + return cmd; +end