From cb36e71300ccf1cdc1d8f5932124c92b1de9e5e1 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Wed, 23 Mar 2016 13:26:12 +1000 Subject: [PATCH] Door Games working!, stdio ones not done yet... --- .gitignore | 2 ++ Makefile | 2 +- bbs.c | 56 ++++++++++++++++++++++++++++++- bbs.h | 11 +++++++ doors.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ doors.ini | 8 +++++ main_menu.c | 5 +++ users.c | 7 ++++ 8 files changed, 184 insertions(+), 2 deletions(-) create mode 100644 doors.c create mode 100644 doors.ini diff --git a/.gitignore b/.gitignore index a3d1f24..b55fa24 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ *.core magicka msgs/* +node* +doors/* diff --git a/Makefile b/Makefile index d3b2b7e..45e319a 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CFLAGS=-I/usr/local/include DEPS = bbs.h JAMLIB = /usr/local/lib/libjam.a -OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o +OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o %.o: %.c $(DEPS) $(CC) -c -o $@ $< $(CFLAGS) diff --git a/bbs.c b/bbs.c index e27432c..1e59ed6 100644 --- a/bbs.c +++ b/bbs.c @@ -38,7 +38,55 @@ void timer_handler(int signum) { } } } +static int door_config_handler(void* user, const char* section, const char* name, + const char* value) +{ + struct bbs_config *conf = (struct bbs_config *)user; + int i; + + for (i=0;idoor_count;i++) { + if (strcasecmp(conf->doors[i]->name, section) == 0) { + // found it + if (strcasecmp(name, "key") == 0) { + conf->doors[i]->key = value[0]; + } else if (strcasecmp(name, "command") == 0) { + conf->doors[i]->command = strdup(value); + } else if (strcasecmp(name, "stdio") == 0) { + if (strcasecmp(value, "true") == 0) { + conf->doors[i]->stdio = 1; + } else { + conf->doors[i]->stdio = 0; + } + } + return 1; + } + } + + if (conf->door_count == 0) { + conf->doors = (struct door_config **)malloc(sizeof(struct door_config *)); + } else { + conf->doors = (struct door_config **)realloc(conf->doors, sizeof(struct door_config *) * (conf->door_count + 1)); + } + + conf->doors[conf->door_count] = (struct door_config *)malloc(sizeof(struct door_config)); + + conf->doors[conf->door_count]->name = strdup(section); + if (strcasecmp(name, "key") == 0) { + conf->doors[conf->door_count]->key = value[0]; + } else if (strcasecmp(name, "command") == 0) { + conf->doors[conf->door_count]->command = strdup(value); + } else if (strcasecmp(name, "stdio") == 0) { + if (strcasecmp(value, "true") == 0) { + conf->doors[conf->door_count]->stdio = 1; + } else { + conf->doors[conf->door_count]->stdio = 0; + } + } + conf->door_count++; + + return 1; +} static int mail_area_handler(void* user, const char* section, const char* name, const char* value) { @@ -324,6 +372,8 @@ void runbbs(int socket, char *config_path) { s_putstring(socket, buffer); conf.mail_conference_count = 0; + conf.door_count = 0; + // Load BBS data if (ini_parse(config_path, handler, &conf) <0) { printf("Unable to load configuration ini (%s)!\n", config_path); @@ -336,7 +386,11 @@ void runbbs(int socket, char *config_path) { exit(-1); } } - + + if (ini_parse("doors.ini", door_config_handler, &conf) <0) { + printf("Unable to load configuration ini (doors.ini)!\n"); + exit(-1); + } // find out which node we are mynode = 0; diff --git a/bbs.h b/bbs.h index 5b927ab..840a768 100644 --- a/bbs.h +++ b/bbs.h @@ -7,6 +7,13 @@ #define VERSION_MINOR 1 #define VERSION_STR "dev" +struct door_config { + char *name; + char key; + char *command; + int stdio; +}; + struct mail_area { char *name; char *path; @@ -36,6 +43,8 @@ struct bbs_config { int newuserlvl; int mail_conference_count; struct mail_conference **mail_conferences; + int door_count; + struct door_config **doors; }; struct sec_level_t { @@ -82,4 +91,6 @@ extern void main_menu(int socket, struct user_record *user); extern int mail_getemailcount(struct user_record *user); extern int mail_menu(int socket, struct user_record *user); + +extern int door_menu(int socket, struct user_record *user); #endif diff --git a/doors.c b/doors.c new file mode 100644 index 0000000..facd55a --- /dev/null +++ b/doors.c @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include "bbs.h" + +extern struct bbs_config conf; +extern int mynode; + +int write_door32sys(int socket, struct user_record *user) { + struct stat s; + char buffer[256]; + FILE *fptr; + + sprintf(buffer, "%s/node%d", conf.bbs_path, mynode); + + if (stat(buffer, &s) != 0) { + mkdir(buffer, 0755); + } + + sprintf(buffer, "%s/node%d/door32.sys", conf.bbs_path, mynode); + + fptr = fopen(buffer, "w"); + + if (!fptr) { + printf("Unable to open %s for writing!\n", buffer); + return 1; + } + + fprintf(fptr, "2\n"); // telnet type + fprintf(fptr, "%d\n", socket); // socket + fprintf(fptr, "38400\n"); // baudrate + fprintf(fptr, "Magicka %d.%d\n", VERSION_MAJOR, VERSION_MINOR); + fprintf(fptr, "%d\n", user->id); + fprintf(fptr, "%s %s\n", user->firstname, user->lastname); + fprintf(fptr, "%s\n", user->loginname); + fprintf(fptr, "%d\n", user->sec_level); + fprintf(fptr, "%d\n", user->timeleft); + fprintf(fptr, "1\n"); // ansi emulation = 1 + fprintf(fptr, "%d\n", mynode); + + fclose(fptr); + return 0; +} + +void rundoor(int socket, struct user_record *user, char *cmd, int stdio) { + char buffer[256]; + if (write_door32sys(socket, user) != 0) { + return; + } + + if (stdio) { + } else { + sprintf(buffer, "%s %d %d", cmd, mynode, socket); + system(buffer); + } +} + +int door_menu(int socket, struct user_record *user) { + int doquit = 0; + int dodoors = 0; + char prompt[128]; + int i; + char c; + while (!dodoors) { + s_displayansi(socket, "doors"); + + sprintf(prompt, "TL: %dm :> ", user->timeleft); + s_putstring(socket, prompt); + + c = s_getc(socket); + + switch(tolower(c)) { + case 'q': + dodoors = 1; + break; + case 'g': + doquit = 1; + dodoors = 1; + break; + default: + { + for (i=0;ikey)) { + rundoor(socket, user, conf.doors[i]->command, conf.doors[i]->stdio); + break; + } + } + } + break; + } + } + + return doquit; +} diff --git a/doors.ini b/doors.ini new file mode 100644 index 0000000..ad3439b --- /dev/null +++ b/doors.ini @@ -0,0 +1,8 @@ +[Test Door] +; the key to press in the door menu must not be Q or G not case sensitive +key = 0 +; the command is passed node number as first argument +; and socket number as second argument. +command = /home/andrew/MagickaBBS/doors/rundoor.sh +; whether or not to use stdio redirection +stdio = false diff --git a/main_menu.c b/main_menu.c index 71923a0..e6c977d 100644 --- a/main_menu.c +++ b/main_menu.c @@ -20,6 +20,11 @@ void main_menu(int socket, struct user_record *user) { c = s_getc(socket); switch(tolower(c)) { + case 'd': + { + doquit = door_menu(socket, user); + } + break; case 'm': { doquit = mail_menu(socket, user); diff --git a/users.c b/users.c index 0cdfdc3..edb4937 100644 --- a/users.c +++ b/users.c @@ -153,6 +153,9 @@ int inst_user(struct user_record *user) { sqlite3_close(db); exit(1); } + + user->id = sqlite3_last_insert_rowid(db); + sqlite3_close(db); return 1; } @@ -385,6 +388,10 @@ struct user_record *new_user(int socket) { user->laston = time(NULL); user->timeleft = user->sec_info->timeperday; + user->cur_file_dir = 0; + user->cur_file_sub = 0; + user->cur_mail_area = 0; + user->cur_mail_conf = 0; inst_user(user); return user;