aboutsummaryrefslogtreecommitdiff
path: root/src/fujiconf.c
blob: f77f2a901b40e8ca8fa4c75a310d75335cd1e542 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <rs232.h>
#include <errno.h>
#include <peekpoke.h>
#include "uip.h"
#include "uiplib.h"
#include "fujichat.h"
#include "common.h"

/* TODO: move these to an external file or otherwise come up with a way
	to reuse the memory */
#define SERVER_LIST_LEN (sizeof(servers) / sizeof(servers[0]))
static char *servers[][2] = {
	/*
	{ "82.165.139.95", "irc.tobug.net (US)" },
	{ "63.243.153.235", "irc.carterroadband.com (US)" },
	{ "209.133.9.109", "ircd.eyearesee.org (US)" },
	{ "89.238.135.210", "london.eyearesee.org (EU)" },
	{ "193.23.141.104", "hungary.eyearesee.org (EU)" },
	*/
	{ "na.newnet.net", "NewNet (North America)" },
	{ "eu.newnet.net", "NewNet (Europe)" },
	{ "us.undernet.org", "Undernet (US)" },
	{ "eu.undernet.org", "Undernet (Europe)" },
	{ "irc.freenode.org", "FreeNode" },
	{ "kubrick.freenode.net", "FreeNode (US)" },
	{ "irc.efnet.org", "Eris Free Net" },
	{ "82.165.139.95", "irc.tobug.net (NewNet, US)" },
};

// char rs232_already_loaded = 0;

int baud_values[] = { 1200, 2400, 4800, 9600, 19200 };
char baud_bytes[] = { RS_BAUD_1200, RS_BAUD_2400, RS_BAUD_4800, RS_BAUD_9600, RS_BAUD_19200 };
char max_baud = sizeof(baud_bytes);

static void get_conf_color(char *name, char *color, char dflt);
static char get_yesno(char *prompt, char dflt);
void save_config(void);

static void bell() {
	putchar(A_BEL);
}

static void get_baudrate(void) {
	char i, buf[5];

	putchar('\n');
	for(i=0; i<max_baud; ++i)
		if(config->baud == baud_bytes[i])
			printf("[%d] %d\n", i+1, baud_values[i]);
		else
			printf(" %d. %d\n", i+1, baud_values[i]);

	fputs("\nBaud rate: ", stdout);
	fflush(stdout);

	do {
		get_line(buf, 4);
		if(!*buf)
			return;

		i = atoi(buf);
	} while (i < 1 || i > max_baud);
	config->baud = baud_bytes[--i];
}

static void get_ip_addr(char *prompt, char *dflt, uip_ipaddr_t *addr) {
	char ok;
	char buf[HOSTLEN+1];

	do {
		printf("%s IP address [%s]: ", prompt, dflt);
		fflush(stdout);
		get_line(buf, HOSTLEN);
		if(!*buf) strcpy(buf, dflt);
		ok = uiplib_ipaddrconv(buf, (unsigned char*)addr);
		if(!ok) bell();
	} while(!ok);
}

#if 0
char scan_hatabs(char dev) {
	int entry;

	/* 0x031A is HATABS from OS equates */
	for(entry = 0x031A; entry < 0x033F; entry += 3)
		if(PEEK(entry) == dev) return 1;

	return 0;
}

int copy_file(char *dst, char *src) {
	// THIS FUNCTION IS BROKEN!
	static char buf[256];
	int i;
	FILE *from, *to;

	if( !(from = fopen(src, "rb")) ) {
		perror(src);
		i = errno;
		goto done;
	}

	if( !(to = fopen(dst, "wb")) ) {
		perror(dst);
		i = errno;
		goto done;
	}

	while( (i = fread(buf, 1, 256, from)) )
		fwrite(buf, 1, i, to);

	if(feof(from))
		i = 0;
	else
		i = errno;

done:
	if(from) fclose(from);
	if(to) fclose(to);
	return i;
}
#endif

static void config_menu(void) {
	char i, buf[HOSTLEN+1], ok = 0;
	unsigned int new_value;

	// strcpy(config->signature, CONF_SIGNATURE);
	((char *)config->signature)[0] = CONF_SIGNATURE_LO;
	((char *)config->signature)[1] = CONF_SIGNATURE_HI;
	config->version = CONF_VERSION;

	if(!config_is_valid())
		set_default_config();

	do {
		puts("\n==> Serial Port Settings\n");

		if(get_yesno("Change RS232 driver", 0)) {
			atari_exec("D:MAKEAUTO.COM");
		}

		get_baudrate();

		/* TODO: choose which Rn: device to use (e.g. on the 850),
			turn HW flow control on/off (buried in cc65 rs232 lib code) */

		puts("\n==> Network Settings\n");
		get_ip_addr("Local", format_ip(&(config->local_ip)), &(config->local_ip));
		get_ip_addr("Peer", format_ip(&(config->peer_ip)), &(config->peer_ip));
		get_ip_addr("DNS Server", format_ip(&(config->resolver_ip)), &(config->resolver_ip));

		puts("\n==> IRC Settings\n");
		puts("Server list:");
		for(i=0; i<SERVER_LIST_LEN; ++i) {
			printf("%d: %s\n   %s\n",
					i + 1,
					servers[i][0],
					servers[i][1]);
		}

		printf("\nEnter # or any server host/IP [%s]: ", config->server);
		fflush(stdout);
		fgets(buf, HOSTLEN, stdin);

		if(*buf != '\n') {
			if(buf[0] >= '1' && buf[0] <= (SERVER_LIST_LEN + '0') && buf[1] == '\n') {
				strcpy(buf, servers[buf[0] - '1'][0]);
			}

			for(i=0; buf[i] && (buf[i] != '\n'); ++i)
				;

			buf[i] = '\0';
			strcpy(config->server, buf);
		}

		printf("Server port [%d]: ", config->server_port);
		get_line(buf, 10);
		new_value = atoi(buf);
		if(new_value) config->server_port = new_value;

		printf("Your nickname [%s]: ", config->nick);
		get_line(buf, NICKLEN);
		if(*buf) strcpy(config->nick, buf);

		printf("Your 'real' name [%s]: ", config->real_name);
		get_line(buf, NICKLEN);
		if(*buf) strcpy(config->real_name, buf);

		do {
			printf("Autojoin channel (or 0 for none)\n[%s]: ", config->channel);
			get_line(buf, NICKLEN);

			switch(*buf) {
				case '\0':
					ok = 1;
					break;

				case '#':
				case '&':
					strcpy(config->channel, buf);
					ok = 1;
					break;

				case '0':
					*(config->channel) = '\0';
					ok = 1;
					break;

				default:
					// user-friendly:
					*(config->channel) = '#';
					strcpy((config->channel) + 1, buf);
					ok = 1;
					break;

					// user-hostile:
					/*
					puts("IRC channel names must begin with # or &");
					bell();
					ok = 0;
					*/
			}

			/* TODO: scan for illegal chars, set ok=0 */
		} while(!ok);


		puts("\n==> User Interface Settings\n");
		do {
			get_conf_color("Background", &(config->bg_color), config->bg_color);
			get_conf_color("Foreground", &(config->fg_color), config->fg_color);
			ok = ((config->bg_color & 0x0e) != (config->fg_color & 0x0e));
			if(!ok)
				puts("These colors would make your text\n"
						"impossible to see. Try again.\n");
		} while(!ok);

		new_value =
			(get_yesno("Disable bell", config->ui_flags & UIFLAG_NOBELL) ? UIFLAG_NOBELL : 0);

		if(!(new_value & UIFLAG_NOBELL)) {
			new_value |=
				(get_yesno("Visual bell", config->ui_flags & UIFLAG_VBELL) ? UIFLAG_VBELL : 0);

			new_value |=
				(get_yesno("Bell on msg", config->ui_flags & UIFLAG_MSGBELL) ? UIFLAG_MSGBELL : 0);
		};

		new_value |=
			(get_yesno("Show ping/pong", config->ui_flags & UIFLAG_SHOWPING) ? UIFLAG_SHOWPING : 0);

		new_value |=
			(get_yesno("Hide MOTD", config->ui_flags & UIFLAG_HIDEMOTD) ? UIFLAG_HIDEMOTD : 0);

		config->ui_flags = new_value;

		puts("\n==> Config complete\n");
		ok = get_yesno("Is this correct", 1);
	} while(!ok);

	if(get_yesno("Save this config", 1))
		save_config();

	putchar('\n');
}

void save_config(void) {
	FILE *f = fopen(DEFAULT_CONF_FILE, "wb");
	if(f) {
		fwrite(config, sizeof(fuji_conf_t), 1, f);
		fclose(f);
		puts("Config saved to " DEFAULT_CONF_FILE);
	}
}

static void get_conf_color(char *name, char *color, char dflt) {
	char buf[5];

	printf("%s color [%d]: ", name, dflt);
	fgets(buf, 5, stdin);

	if(!(*buf >= '0' && *buf <= '9'))
		*color = dflt;
	else
		*color = atoi(buf);
}

static char get_yesno(char *prompt, char dflt) {
	char buf[5];

	printf("%s [%c/%c]: ",
			prompt,
			(dflt ? 'Y' : 'y'),
			(dflt ? 'n' : 'N'));

	fflush(stdout);
	get_line(buf, 4);
	switch(*buf) {
		case 'y':
		case 'Y':
			return 1;

		case 'n':
		case 'N':
			return 0;

		default:
			return dflt;
	}
}

void reboot(void) {
	asm("jmp $e477");
}

void main(void) {
	char *next_file, buf[2];
	int ret;

	disable_break();

	putchar(125);
	puts(BANNER " Setup\n");
	get_config();

	if(!config_is_valid())
		set_default_config();

	config_menu();

	/*
	if(rs232_already_loaded) {
		puts("\nAn RS232 driver is already resident.\n"
				"You must reboot to use the new driver.\n");
		if(get_yesno("Reboot now", 1))
			reboot();
	}
	*/

	if(get_yesno("Start FujiChat now", 1)) {
		// next_file = IRC_LOADER_FILENAME;
		next_file = IRC_FILENAME;
	} else {
		next_file = MENU_FILENAME;
	}

	printf("\nLoading %s...\n", next_file);
	ret = atari_exec(next_file);

	printf("Error %d!\n\xfd", ret);
	puts("Press Enter to exit to DOS");
	get_line(buf, 2);
}