Start work on New Menu System

This commit is contained in:
Andrew Pamment 2017-04-12 16:38:33 +10:00
parent ccd35b5647
commit 2c59232546
6 changed files with 296 additions and 42 deletions

6
bbs.c
View File

@ -821,8 +821,12 @@ tryagain:
}
record_last10_callers(user);
// main menu
main_menu(user);
if (conf->root_menu != NULL) {
menu_system(conf->root_menu);
} else {
main_menu(user);
}
dolog("%s is logging out, on node %d", user->loginname, mynode);
broadcast("%s is logging out, on node %d", user->loginname, mynode);

3
bbs.h
View File

@ -140,6 +140,7 @@ struct bbs_config {
int bwave_max_msgs;
struct fido_addr *main_aka;
char *root_menu;
char *external_editor_cmd;
int external_editor_stdio;
int fork;
@ -245,6 +246,8 @@ extern struct user_record *new_user();
extern struct user_record *check_user_pass(char *loginname, char *password);
extern void list_users(struct user_record *user);
extern void display_bulletins();
extern void display_textfiles();
extern void main_menu(struct user_record *user);
extern s_JamBase *open_jam_base(char *path);

View File

@ -27,6 +27,7 @@ Broadcast Address = 192.168.1.255
IP Guard Enable = true
IP Guard Timeout = 120
IP Guard Tries = 4
Root Menu = /home/andrew/MagickaBBS/menus/root.mnu
[paths]
Config Path = /home/andrew/MagickaBBS/config

2
main.c
View File

@ -481,6 +481,8 @@ static int handler(void* user, const char* section, const char* name,
conf->ipguard_timeout = atoi(value);
} else if (strcasecmp(name, "ip guard tries") == 0) {
conf->ipguard_tries = atoi(value);
} else if (strcasecmp(name, "root menu") == 0) {
conf->root_menu = strdup(value);
}
} else if (strcasecmp(section, "paths") == 0){
if (strcasecmp(name, "ansi path") == 0) {

View File

@ -10,6 +10,60 @@
extern struct bbs_config conf;
void display_bulletins() {
int i;
char buffer[PATH_MAX];
i = 0;
sprintf(buffer, "%s/bulletin%d.ans", conf.ansi_path, i);
while (stat(buffer, &s) == 0) {
sprintf(buffer, "bulletin%d", i);
s_displayansi(buffer);
s_printf(get_string(6));
s_getc();
i++;
sprintf(buffer, "%s/bulletin%d.ans", conf.ansi_path, i);
}
}
void display_textfiles() {
int i;
char buffer[5];
if (conf.text_file_count > 0) {
while(1) {
s_printf(get_string(143));
s_printf(get_string(144));
for (i=0;i<conf.text_file_count;i++) {
s_printf(get_string(145), i, conf.text_files[i]->name);
}
s_printf(get_string(146));
s_printf(get_string(147));
s_readstring(buffer, 4);
if (tolower(buffer[0]) != 'q') {
i = atoi(buffer);
if (i >= 0 && i < conf.text_file_count) {
s_printf("\r\n");
s_displayansi_p(conf.text_files[i]->path);
s_printf(get_string(6));
s_getc();
s_printf("\r\n");
}
} else {
break;
}
}
} else {
s_printf(get_string(148));
s_printf(get_string(6));
s_getc();
}
}
void main_menu(struct user_record *user) {
int doquit = 0;
char c;
@ -80,36 +134,7 @@ void main_menu(struct user_record *user) {
break;
case 'a':
{
if (conf.text_file_count > 0) {
while(1) {
s_printf(get_string(143));
s_printf(get_string(144));
for (i=0;i<conf.text_file_count;i++) {
s_printf(get_string(145), i, conf.text_files[i]->name);
}
s_printf(get_string(146));
s_printf(get_string(147));
s_readstring(buffer, 4);
if (tolower(buffer[0]) != 'q') {
i = atoi(buffer);
if (i >= 0 && i < conf.text_file_count) {
s_printf("\r\n");
s_displayansi_p(conf.text_files[i]->path);
s_printf(get_string(6));
s_getc();
s_printf("\r\n");
}
} else {
break;
}
}
} else {
s_printf(get_string(148));
s_printf(get_string(6));
s_getc();
}
display_textfiles();
}
break;
case 'c':
@ -129,17 +154,7 @@ void main_menu(struct user_record *user) {
break;
case 'b':
{
i = 0;
sprintf(buffer, "%s/bulletin%d.ans", conf.ansi_path, i);
while (stat(buffer, &s) == 0) {
sprintf(buffer, "bulletin%d", i);
s_displayansi(buffer);
s_printf(get_string(6));
s_getc();
i++;
sprintf(buffer, "%s/bulletin%d.ans", conf.ansi_path, i);
}
display_bulletins();
}
break;
case '1':

229
menus.c Normal file
View File

@ -0,0 +1,229 @@
#define MENU_SUBMENU 1
#define MENU_LOGOFF 2
#define MENU_PREVMENU 3
#define MENU_AUTOMESSAGEWRITE 4
#define MENU_TEXTFILES 5
#define MENU_CHATSYSTEM 6
#define MENU_BBSLIST 7
#define MENU_LISTUSERS 8
#define MENU_BULLETINS 9
#define MENU_LAST10 10
#define MENU_SETTINGS 11
struct menu_item {
char hotkey;
int command;
char *data;
};
int menu_system(char *menufile) {
FILE *fptr;
char buffer[256];
int menu_items = 0;
struct menu_item **menu;
char *lua_script;
int do_lua_menu;
char *ansi_file;
fptr = fopen(menufile, "r");
int i;
if (!fptr) {
s_printf("Error opening menu file! %s\r\n", menufile);
return 0;
}
lua_script = NULL;
ansi_file = NULL;
while (1) {
fgets(buffer, 256, fptr);
chomp(buffer);
if (strncasecmp(buffer, "HOTKEY", 6) == 0) {
menu_items++;
if (menu_items == 1) {
menu = (struct menu_item **)malloc(sizeof(struct menu_item *));
} else {
menu = (struct menu_item **)realloc(menu, sizeof(struct menu_item *) * (menu_items));
}
menu[menu_items-1] = (struct menu_item *)malloc(sizeof(struct menu_item));
menu[menu_items-1]->hotkey = buffer[7];
menu[menu_items-1]->data = NULL;
} else if (strncasecmp(buffer, "COMMAND", 7) == 0 && menu_items > 0) {
if (strncasemp(&buffer[8], "SUBMENU", 7) == 0) {
menu[menu_items-1]->command = MENU_SUBMENU;
} else if (strncasecmp(&buffer[8], "LOGOFF", 6) == 0) {
menu[menu_items-1]->command = MENU_LOGOFF;
} else if (strncasecmp(&buffer[8], "AUTOMESSAGE_WRITE", 17) == 0) {
menu[menu_items-1]->command = MENU_AUTOMESSAGEWRITE;
} else if (strncasecmp(&buffer[8], "TEXTFILES", 9) == 0) {
menu[menu_items-1]->command = MENU_TEXTFILES;
} else if (strncasecmp(&buffer[8], "CHATSYSTEM", 10) == 0) {
menu[menu_items-1]->command = MENU_CHATSYSTEM;
} else if (strncasecmp(&buffer[8], "BBSLIST", 7) == 0) {
menu[menu_items-1]->command = MENU_BBSLIST;
} else if (strncasecmp(&buffer[8], "LISTUSERS", 9) == 0) {
menu[menu_items-1]->command = MENU_LISTUSERS;
} else if (strncasecmp(&buffer[8], "BULLETINS", 9) == 0) {
menu[menu_items-1]->command = MENU_BULLETINS;
} else if (strncasecmp(&buffer[8], "LAST10CALLERS", 13) == 0) {
menu[menu_items-1]->command = MENU_LAST10;
} else if (strncasecmp(&buffer[8], "SETTINGS", 8) == 0) {
menu[menu_items-1]->command = MENU_SETTINGS;
}
} else if (strncasecmp(buffer, "DATA", 4) == 0) {
menu[menu_items-1]->data = strdup(&buffer[5]);
} else if (strncasecmp(buffer, "LUASCRIPT", 9) == 0) {
lua_script = strdup(&buffer[10]);
} else if (strncasecmp(buffer, "ANSIFILE", 8) == 0) {
ansi_file = strdup(&buffer[9]);
}
}
fclose(fptr);
do_lua_menu = 0;
if (conf.script_path != NULL && lua_script[0] != '/') {
snprintf(buffer, 256 "%s/%s", conf.script_path, lua_script);
do_lua_menu = 1;
} else if (lua_script[0] == '/') {
snprintf(buffer, 256 "%s", lua_script);
do_lua_menu = 1;
}
if (do_lua_menu)
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) {
dolog("Failed to run script: %s", lua_tostring(L, -1));
do_lua_menu = 0;
}
} else {
do_lua_menu = 0;
}
}
while (!doquit) {
if (do_lua_menu == 0) {
if (ansi_file != NULL) {
s_displayansi(ansi_file);
}
s_printf(get_string(142), user->timeleft);
c = s_getc();
} else {
lua_getglobal(L, "menu");
result = lua_pcall(L, 0, 1, 0);
if (result) {
dolog("Failed to run script: %s", lua_tostring(L, -1));
do_lua_menu = 0;
lua_close(L);
continue;
}
lRet = (char *)lua_tostring(L, -1);
lua_pop(L, 1);
c = lRet[0];
}
for (i=0;i<menu_items;i++) {
if (menu[i]->hotkey == c) {
switch(menu[i]->command) {
case MENU_SUBMENU:
doquit = menu_system(menu[i]->data);
if (doquit == 1) {
// free menus
if (do_lua_menu) {
lua_close(L);
}
if (lua_script != NULL) {
free(lua_script);
}
if (ansi_file != NULL) {
free(ansi_file);
}
for (i=0;i<menu_items;i++) {
if (menu[i]->data != NULL) {
free(menu[i]->data)
}
free(menu[i]);
}
free(menu);
return doquit;
}
break;
case MENU_LOGOFF:
if (do_lua_menu) {
lua_close(L);
}
if (lua_script != NULL) {
free(lua_script);
}
if (ansi_file != NULL) {
free(ansi_file);
}
for (i=0;i<menu_items;i++) {
if (menu[i]->data != NULL) {
free(menu[i]->data)
}
free(menu[i]);
}
free(menu);
return 1;
case MENU_PREVMENU:
if (do_lua_menu) {
lua_close(L);
}
if (lua_script != NULL) {
free(lua_script);
}
if (ansi_file != NULL) {
free(ansi_file);
}
for (i=0;i<menu_items;i++) {
if (menu[i]->data != NULL) {
free(menu[i]->data)
}
free(menu[i]);
}
free(menu);
return 0;
case MENU_AUTOMESSAGEWRITE:
automessage_write(user);
break;
case MENU_TEXTFILES:
display_textfiles();
break;
case MENU_CHATSYSTEM:
chat_system(user);
break;
case MENU_BBSLIST:
bbs_list(user);
break;
case MENU_LISTUSERS:
list_users(user);
break;
case MENU_BULLETINS:
display_bulletins();
break;
case MENU_LAST10:
display_last10_callers(user);
break;
case MENU_SETTINGS:
settings_menu(user);
break;
}
break;
}
}
}
// free menus;
return doquit;
}