diff options
Diffstat (limited to 'bbs.c')
-rw-r--r-- | bbs.c | 129 |
1 files changed, 129 insertions, 0 deletions
@@ -0,0 +1,129 @@ +#include <u.h> +#include <libc.h> +#include <bio.h> + +#define DEFGREET "Welcome to Antares Labs Computer Bulletin Board Sytem\r\n\r\n" +#define BAUDRATE 300 + +Biobuf bin, bout; +char *wdir = "/sys/lib/bbs"; +char username[32]; +int debug; + +void +usage(void) +{ + fprint(2, "usage: bbs [-d] [-m pwd]\n"); + exits("usage"); +} + +void +reply(char *msg, long len) +{ + char *s; + + for(s = msg; (s-msg) < len; s++){ + write(1, s, 1); + sleep(1000/(BAUDRATE/8)); + } +} + +void +greet(void) +{ + char buf[1024]; + int fd, n; + + fd = open("greeting", OREAD); + if(fd < 0) + reply(DEFGREET, strlen(DEFGREET)); + else + while((n = read(fd, buf, sizeof buf)) > 0) + reply(buf, n); + close(fd); + + reply(ctime(time(0)), 30-1); +} + +int +signup(void) +{ + char *line, *passwd; + int len; + + reply("username (32 chars max): ", 25); + line = Brdline(&bin, '\n'); + len = Blinelen(&bin); + line[len-2] = 0; + strncpy(username, line, sizeof username); + reply("password: ", 10); + line = Brdline(&bin, '\n'); + len = Blinelen(&bin); + line[len-2] = 0; + passwd = malloc(len); + strncpy(passwd, line, len); + +} + +int +login(void) +{ + char *line; + int len; + + reply("login: ", 7); + line = Brdline(&bin, '\n'); + len = Blinelen(&bin); + line[len-2] = 0; + strncpy(username, line, sizeof username); + if(strcmp(username, "new") == 0) + signup(); + + return 0; +} + +void +main(int argc, char *argv[]) +{ + char *line; + int len, i; + + ARGBEGIN{ + case 'm': + wdir = EARGF(usage()); + break; + case 'd': + debug++; + break; + }ARGEND; + + Binit(&bin, 0, OREAD); + Binit(&bout, 1, OWRITE); + + if(debug) + for(i = 0; i < argc; i++) + fprint(2, "arg%d: %s\n", i, argv[i]); + + if(chdir(wdir) < 0) + sysfatal("chdir: %r"); + + greet(); + + if(login() < 0){ + fprint(2, "login failed for %s\n", username); + exits(0); + } + while(line = Brdline(&bin, '\n')){ + if(strcmp(line, "q") == 0){ + print("bye!\n"); + exits(0); + } + len = Blinelen(&bin); + Bwrite(&bout, line, len); + Bflush(&bout); + } + + Bterm(&bin); + Bterm(&bout); + exits(0); +} |