Prompts/Menus can now be handled by lua

This commit is contained in:
Andrew Pamment 2016-04-12 15:19:56 +10:00
parent 83eee32306
commit f3d8ea9d71
10 changed files with 326 additions and 28 deletions

2
bbs.c
View File

@ -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;

57
doors.c
View File

@ -16,6 +16,9 @@
# include <libutil.h>
#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");
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);
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;
}

54
files.c
View File

@ -9,7 +9,9 @@
#include <errno.h>
#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");
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);
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);
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;
}

View File

@ -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");
}

View File

@ -7,6 +7,9 @@
#include <sys/stat.h>
#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");
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);
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;
}

View File

@ -4,6 +4,9 @@
#include <ctype.h>
#include <sys/stat.h>
#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);
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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