aboutsummaryrefslogtreecommitdiff
path: root/src/irc.c
blob: 94450d9ed80dba0b2e3619d42965c3f803a785c0 (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
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#include "irc.h"

#ifdef __ATARI__
#include <atari.h>
#include <conio.h>
#include "conio.h"
#include "nio.h"
#else
#define CH_EOL '|'
unsigned char rx_buf[MAX_IRC_MSG_LEN];      // RX buffer.
unsigned short bw=0;            // # of bytes waiting.
#endif

#define MAX_MSG 512

char *msg_src, *msg_cmd, *msg_dest, *msg_text;
char *msg_args[MAX_MSG_ARGS];
int msg_argcount;

static char msgbuf[MAX_MSG] = { 0 };
static char *msg; /* with source removed */
static int msgbuf_len = 0, msg_len = 0;

static int joined = 0;

#ifdef __ATARI__
static void join_channel(void) {
	ui_print("Joining channel...\n");
	txbuf_set_str("JOIN ");
	txbuf_append_str(channel);
	txbuf_append_str("\n");
	txbuf_send();
	joined = 1;
}

static void do_pong(void) {
	ui_putchar(CH_EOL);
	ui_print("PING/PONG\n"); /* make hiding this a preference, or just ditch it */
	txbuf_set_str("PONG ");
	txbuf_append_str(msg_args[0]);
	txbuf_send();
}

static void do_privmsg(void) {
	static char chan;

	chan = (*msg_dest == '#');

	if(chan) {
		ui_putchar('<');
	} else {
		ui_putchar('*');
	}

	ui_print(msg_src);

	if(chan) {
		ui_putchar('>');
	} else {
		ui_putchar('*');
	}

	ui_putchar(' ');
	ui_print(msg_text);
}

static void do_catchall(void) {
	int i;
	if(msg_src) {
		ui_print(msg_src);
		ui_putchar(' ');
	}
	ui_print(msg_cmd);
	for(i = 0; i < msg_argcount; i++) {
		ui_putchar(' ');
		ui_print(msg_args[i]);
	}
	if(msg_text) {
		ui_putchar(' ');
		ui_print(msg_text);
	}
}

static void do_numeric(void) {
	do_catchall();

	/* RPL_ENDOFMOTD or RPL_NOMOTD */
	if(!joined && (streq(msg_cmd, "372") || streq(msg_cmd, "422"))) {
		join_channel();
	}
}

static void invalid_msg(char type) {
	ui_print("??? unknown, type ");
	ui_putchar(type);
	ui_putchar('\n');
}
#else
static void do_pong(void) { }
static void invalid_msg(char type) {
	printf("??? unknown, type %c\n", type);
}
#endif

/* msgbuf contains a complete message from the server, whose
   length is msgbuf_len. the last character *must* be CH_EOL,
   and the last argument ends with CH_EOL. */
static void parse_msg(void) {
	char *p;

#ifndef __ATARI__
	printf("\ngot message:\n");
	for(msg = msgbuf; *msg != CH_EOL; msg++)
		putchar(*msg);
	putchar('\n');
	putchar('\n');
#endif

	msg_cmd = msg_text = msg_src = msg_dest = 0;
	msg = msgbuf;

	/* ignore empty message */
	if(*msg == CH_EOL) return;

	/* if there's a final multiword arg... */
	/* FIXME: channel names can have colons, which breaks this... */
	p = strstr(msg + 1, " :"); /* +1 to skip leading colon in msg source */
	if(p) {
		msg_text = p + 2;
		*p = 0;
	}

	/* first token is either the source (with a :) or a command (without) */
	p = strtok(msg, " ");
	if(!p) {
		invalid_msg('1');
		return;
	}

	if(*p == ':') {
		msg_src = p; /* generally :irc.example.com or :nick!user@host */
		msg_cmd = strtok(0, " ");
	} else {
		msg_src = 0; /* no source supplied */
		msg_cmd = p;
	}

	if(!msg_cmd) {
		invalid_msg('2');
		return;
	}

	/* special case for ping, treat as 1 arg, even if it has space and no : */
	if(streq_i(msg_cmd, "PING")) {
		msg_argcount = 1;
		msg_args[0] = msg_cmd + 6;
		do_pong();
		return;
	} else {
		for(msg_argcount = 0; msg_argcount < MAX_MSG_ARGS; msg_argcount++) {
			p = strtok(0, " ");
			if(p) {
				msg_args[msg_argcount] = p;
			} else {
				break;
			}
		}
	}
	if(msg_argcount) msg_dest = msg_args[0];

	if(msg_src) {
		if((p = strstr(msg_src, "!"))) {
			msg_src++;
			*p = '\0';
		} else {
			msg_src = 0;
		}
	}

#ifdef __ATARI__
	OS.crsinh = 1;
	ui_start_msg();
	if(streq_i(msg_cmd, "PRIVMSG")) {
		do_privmsg();
	} else if(isdigit(msg_cmd[0])) {
		do_numeric();
	} else {
		do_catchall();
	}
	ui_end_msg();
#else
	{
		int i;
		printf("src: %s\n", msg_src ? msg_src : "<none>");
		printf("cmd: %s\n", msg_cmd ? msg_cmd : "<none>");
		printf("args: %d\n", msg_argcount);
		for(i = 0; i < msg_argcount; i++)
			printf("  %d: %s\n", i, msg_args[i]);
		printf("text: %s\n", msg_text ? msg_text : "<none>");
	}
#endif
}

static void irc_parse(void) {
	int i;
	char *p = rx_buf;

#ifndef __ATARI__
	printf("irc_parse() called, bw == %d\n", bw);
#endif

	for(i = 0; i < bw; i++) {
		msgbuf[msgbuf_len] = *p;
		if(*p == CH_EOL) {
			msgbuf[msgbuf_len + 1] = '\0';
			parse_msg();
			msgbuf_len = 0;
		} else {
			msgbuf_len++;
		}
		p++;
	}
}

#ifdef __ATARI__
int irc_read(void) {
	if(!trip) return 1;

	err = nstatus(url);

	if(err == 136) {
		ui_print("Disconnected, press any key...\n");
		cgetc();
		return 0;
	} else if(err != 1) {
		print_error(err);
		return 0;
	}

	// Get # of bytes waiting, no more than size of rx_buf
	bw = OS.dvstat[1] * 256 + OS.dvstat[0];

	if(bw > sizeof(rx_buf))
		bw = sizeof(rx_buf);

	if(bw > 0) {
      err = nread(url, rx_buf, bw);
		if(err != 1) {
			ui_print("READ ERROR: ");
			print_error(err);
			return 0;
		}

		trip = 0;
		PIA.pactl |= 1; // Flag interrupt as serviced, ready for next one.

		irc_parse();
	}

	return 1;
}

/* modern.ircdocs.horse say to do this IMMEDIATELY upon TCP
   connection, without waiting for anything from the server. */
void irc_register(void) {
	txbuf_init();
	txbuf_append_str("USER ");
	txbuf_append_str(usernick); /* local (UNIX) username, just use the nick */
	txbuf_append_str(" 0 * :FujiNetChat User\n"); /* "real" name (make it a pref?) */
	txbuf_send();

	txbuf_init();
	txbuf_append_str("NICK ");
	txbuf_append_str(usernick);
	txbuf_append_str("\n");
	txbuf_send();
}

/* only exits on error (e.g. connection closed, which might be via /QUIT). */
void irc_loop(void) {
	while(1) {
		if(!irc_read()) return;

		if(kbhit())
			if(joined)
				ui_keystroke();
			else join_channel();
	}
}

#else // !defined(__ATARI__)
/* parsetest */
int main(int argc, char **argv) {
	strcpy((char *)rx_buf, argv[1]);
	bw = strlen(rx_buf);
	irc_parse();
	/*
	*/
	return 0;
}
#endif