summaryrefslogtreecommitdiff
path: root/bbs.c
blob: 2a6cbd20cf0c141e01f8f17e68c6bc97a59b163e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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);
}