aboutsummaryrefslogtreecommitdiff
path: root/src/commands.c
blob: 70a2485558d947dcc1e55c8e35a0e784eb28c971 (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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <peekpoke.h>
#include "fujichat.h"
#include "features.h"
#include "common.h"

void cmd_join(void);
void cmd_msg(void);
void cmd_nick(void);
void cmd_part(void);
void cmd_ping(void);
void cmd_quit(void);
void cmd_quote(void);
void cmd_ver(void);
void cmd_version(void);
void do_me(void);
void cmd_clear(void);
#ifdef FEAT_COLOR_COMMAND
void cmd_fgcolor(void);
void cmd_bgcolor(void);
#endif

fuji_cmd_t cmd_list[] = {
	{ "JOIN", ARGTYPE_OPT, cmd_join },
	{ "J", ARGTYPE_OPT, cmd_join },
	{ "MSG", ARGTYPE_REQUIRED, cmd_msg },
	{ "M", ARGTYPE_REQUIRED, cmd_msg },
	{ "NICK", ARGTYPE_OPT, cmd_nick },
	{ "PART", ARGTYPE_OPT, cmd_part },
	{ "PING", ARGTYPE_REQUIRED, cmd_ping },
	{ "QUIT", ARGTYPE_OPT, cmd_quit },
	{ "QUOTE", ARGTYPE_REQUIRED, cmd_quote },
	{ "VER", ARGTYPE_OPT, cmd_version },
	{ "VERSION", ARGTYPE_OPT, cmd_version },
	{ "ME", ARGTYPE_REQUIRED, do_me },
	{ "CLEAR", ARGTYPE_NONE, cmd_clear },
#ifdef FEAT_COLOR_COMMAND
	{ "FGCOLOR", ARGTYPE_REQUIRED, cmd_fgcolor },
	{ "BGCOLOR", ARGTYPE_REQUIRED, cmd_bgcolor },
#endif
};

#define CMD_LIST_SIZE (sizeof(cmd_list) / sizeof(fuji_cmd_t))

char *cmd_arg;

static void errmsg(char *msg) {
	puts(msg);
	bell();
}

static void err_missing_arg(void) {
	errmsg("Command requires argument");
}

static void err_arg_not_allowed(void) {
	errmsg("No argument allowed");
}

static void err_already_joined(void) {
	errmsg("You are already in a channel (use /part to leave)");
}


void do_me(void) {
	serv_msg_buf_len = sprintf(serv_msg_buf, "PRIVMSG %s :%cACTION %s%c%c",
			channel, 1, cmd_arg, 1, NL);
	send_serv_msg_buf();
}

void cmd_ver(void) {
	serv_msg_buf_len = sprintf(serv_msg_buf, "PRIVMSG %s %cVERSION%c%c",
			cmd_arg, 1, 1, NL);
	send_serv_msg_buf();
}

void cmd_version(void) {
	if(cmd_arg)
		cmd_ver();
	else
		send_server_cmd("VERSION", NULL);
}

void cmd_ping(void) {
	serv_msg_buf_len = sprintf(serv_msg_buf, "PRIVMSG %s :%cPING %03d %03d %03d%c%c",
			cmd_arg, 1, PEEK(20), PEEK(19), PEEK(18), 1, NL);
	send_serv_msg_buf();
}

void cmd_join(void) {
	if(joined_channel) {
		if(cmd_arg)
			err_already_joined();
		else
			send_server_cmd("JOIN", channel);
	} else {
		if(cmd_arg) {
			joined_channel = 1;
			strcpy(channel, cmd_arg);
			send_server_cmd("JOIN", cmd_arg);
		} else {
			err_missing_arg();
		}
	}
}

void cmd_part(void) {
	joined_channel = 0;
	if(cmd_arg) {
		serv_msg_buf_len = sprintf(serv_msg_buf, "PART %s :%s%c", channel, cmd_arg, NL);
		send_serv_msg_buf();
	} else {
		send_server_cmd("PART", channel);
	}
}

void cmd_msg(void) {
	send_server_cmd_2arg("PRIVMSG", cmd_arg);
}

void cmd_quit(void) {
	joined_channel = 0;
	send_server_cmd("QUIT", cmd_arg);
}

void cmd_quote(void) {
	send_server_cmd(cmd_arg, NULL);
}

void cmd_nick(void) {
	if(!cmd_arg) {
		printf("> You are known as %s\n", config->nick);
		return;
	}

	strcpy(config->nick, cmd_arg);
	send_server_cmd("NICK", cmd_arg);
	printf("> You are now known as %s\n", config->nick);
}

void cmd_clear(void) {
	fuji_putchar(0x7d); /* ATASCII screen clear code */
}

#ifdef FEAT_COLOR_COMMAND
static void do_color(u16_t reg) {
	u16_t c;

	if(cmd_arg[0] >= '0' && cmd_arg[0] <= '9') {
		c = atoi(cmd_arg);
		if(c < 0x100) {
			POKE(reg, c);
			return;
		}
	}

	puts("Bad color (range 0-255)");
	bell();
}

void cmd_fgcolor(void) {
	do_color(709);
}

void cmd_bgcolor(void) {
	do_color(710);
}

#endif

void handle_command(void) {
	char i, *cend;
	char *cmd = input_buf + 1;
	char *p = cmd;

	cmd_arg = NULL;

	/* convert command word to uppercase */
	while((*p != NL) && (*p != ' ')) {
		*p = toupper(*p);
		++p;
	}

	cend = p;

	/* set cmd_arg pointer, nul-terminate cmd_arg, if present */
	/* (otherwise, cmd_arg remains NULL) */
	if(*p != NL) {
		cmd_arg = p;
		/* skip extra whitespace */
		while(*cmd_arg == ' ')
			++cmd_arg;
		p = cmd_arg + 1;
		while(*p && (*p != NL))
			++p;
		*p = '\0';
	}

	/* nul-terminate cmd */
	*cend = '\0';

	for(i=0; i<CMD_LIST_SIZE; ++i) {
		if(strcmp(cmd, cmd_list[i].cmd) != 0)
			continue;

		switch(cmd_list[i].arg_type) {
			case ARGTYPE_REQUIRED:
				if(!cmd_arg) {
					err_missing_arg();
					return;
				}
				break;

			case ARGTYPE_NONE:
				if(cmd_arg) {
					err_arg_not_allowed();
					return;
				}
				break;

			default:
				break;
		}

		(cmd_list[i].handler)();
		return;
	}

	/* no idea what this is, send it as-is */
	send_server_cmd(cmd, cmd_arg);
}