about summary refs log tree commit diff
path: root/input.c
blob: 12f214e7865d554a4c453b41e87e82a592f1a169 (plain) (blame)
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
/* Copyright (C) 2018  Curtis McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <err.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sysexits.h>
#include <wchar.h>

#include "chat.h"

static wchar_t *wcssep(wchar_t **stringp, const wchar_t *delim) {
	wchar_t *orig = *stringp;
	if (!orig) return NULL;
	size_t i = wcscspn(orig, delim);
	*stringp = NULL;
	if (orig[i]) {
		orig[i] = '\0';
		*stringp = &orig[i + 1];
	}
	return orig;
}

static void privmsg(bool action, const wchar_t *mesg) {
	char *line;
	int send;
	asprintf(
		&line, ":%s!%s %nPRIVMSG %s :%s%ls%s",
		chat.nick, chat.user, &send, chat.chan,
		(action ? "\1ACTION " : ""), mesg, (action ? "\1" : "")
	);
	if (!line) err(EX_OSERR, "asprintf");
	clientFmt("%s\r\n", &line[send]);
	handle(line);
	free(line);
}

typedef void (*Handler)(wchar_t *params);

static void inputMe(wchar_t *params) {
	privmsg(true, params ? params : L"");
}

static void inputNick(wchar_t *params) {
	wchar_t *nick = wcssep(&params, L" ");
	if (nick) {
		clientFmt("NICK %ls\r\n", nick);
	} else {
		uiChat("/nick requires a name");
	}
}

static void inputWho(wchar_t *params) {
	(void)params;
	clientFmt("WHO %s\r\n", chat.chan);
}

static void inputQuit(wchar_t *params) {
	if (params) {
		clientFmt("QUIT :%ls\r\n", params);
	} else {
		clientFmt("QUIT :Goodbye\r\n");
	}
}

static const struct {
	const wchar_t *command;
	Handler handler;
} COMMANDS[] = {
	{ L"me", inputMe },
	{ L"names", inputWho },
	{ L"nick", inputNick },
	{ L"quit", inputQuit },
	{ L"who", inputWho },
};
static const size_t COMMANDS_LEN = sizeof(COMMANDS) / sizeof(COMMANDS[0]);

void input(wchar_t *input) {
	if (input[0] != '/') {
		privmsg(false, input);
		return;
	}
	input++;
	wchar_t *command = wcssep(&input, L" ");
	for (size_t i = 0; i < COMMANDS_LEN; ++i) {
		if (wcscmp(command, COMMANDS[i].command)) continue;
		COMMANDS[i].handler(input);
		return;
	}
	uiFmt("/%ls isn't a recognized command", command);
}