Factor out input param and add tagFind

So that /view can't just invent tags.
master
Curtis McEnroe 2018-08-12 23:55:12 -04:00
parent 19464369c3
commit 1232ce451d
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
3 changed files with 33 additions and 19 deletions

1
chat.h
View File

@ -45,6 +45,7 @@ enum { TAGS_LEN = 256 };
const struct Tag TAG_NONE; const struct Tag TAG_NONE;
const struct Tag TAG_STATUS; const struct Tag TAG_STATUS;
const struct Tag TAG_VERBOSE; const struct Tag TAG_VERBOSE;
struct Tag tagFind(const char *name);
struct Tag tagFor(const char *name); struct Tag tagFor(const char *name);
enum { enum {

39
input.c
View File

@ -37,6 +37,13 @@ static void privmsg(struct Tag tag, bool action, const char *mesg) {
free(line); free(line);
} }
static char *param(const char *command, char **params, const char *name) {
char *param = strsep(params, " ");
if (param) return param;
uiFmt(TAG_STATUS, "%s requires a %s", command, name);
return NULL;
}
typedef void (*Handler)(struct Tag tag, char *params); typedef void (*Handler)(struct Tag tag, char *params);
static void inputMe(struct Tag tag, char *params) { static void inputMe(struct Tag tag, char *params) {
@ -45,31 +52,25 @@ static void inputMe(struct Tag tag, char *params) {
static void inputNick(struct Tag tag, char *params) { static void inputNick(struct Tag tag, char *params) {
(void)tag; (void)tag;
char *nick = strsep(&params, " "); char *nick = param("/nick", &params, "name");
if (nick) { if (!nick) return;
ircFmt("NICK %s\r\n", nick); ircFmt("NICK %s\r\n", nick);
} else {
uiLog(TAG_STATUS, L"/nick requires a name");
}
} }
static void inputJoin(struct Tag tag, char *params) { static void inputJoin(struct Tag tag, char *params) {
(void)tag; (void)tag;
char *chan = strsep(&params, " "); char *chan = param("/join", &params, "channel");
if (chan) { if (!chan) return;
ircFmt("JOIN %s\r\n", chan); ircFmt("JOIN %s\r\n", chan);
} else {
uiLog(TAG_STATUS, L"/join requires a channel");
}
} }
static void inputWho(struct Tag tag, char *params) { static void inputWho(struct Tag tag, char *params) {
(void)params; // TODO (void)params;
ircFmt("WHO %s\r\n", tag.name); ircFmt("WHO %s\r\n", tag.name);
} }
static void inputTopic(struct Tag tag, char *params) { static void inputTopic(struct Tag tag, char *params) {
if (params) { // TODO if (params) {
ircFmt("TOPIC %s :%s\r\n", tag.name, params); ircFmt("TOPIC %s :%s\r\n", tag.name, params);
} else { } else {
ircFmt("TOPIC %s\r\n", tag.name); ircFmt("TOPIC %s\r\n", tag.name);
@ -96,13 +97,19 @@ static void inputOpen(struct Tag tag, char *params) {
} }
static void inputView(struct Tag tag, char *params) { static void inputView(struct Tag tag, char *params) {
char *view = strsep(&params, " "); (void)tag;
char *view = param("/view", &params, "name or number");
if (!view) return; if (!view) return;
int num = strtol(view, &view, 0); int num = strtol(view, &view, 0);
if (view[0]) { if (!view[0]) {
uiViewTag(tagFor(view));
} else {
uiViewNum(num); uiViewNum(num);
} else {
struct Tag tag = tagFind(view);
if (tag.id != TAG_NONE.id) {
uiViewTag(tag);
} else {
uiFmt(TAG_STATUS, "No view for %s", view);
}
} }
} }

8
tag.c
View File

@ -37,11 +37,17 @@ static struct Tag Tag(size_t id) {
return (struct Tag) { id, tags.name[id] }; return (struct Tag) { id, tags.name[id] };
} }
struct Tag tagFor(const char *name) { struct Tag tagFind(const char *name) {
for (size_t id = 0; id < tags.len; ++id) { for (size_t id = 0; id < tags.len; ++id) {
if (strcmp(tags.name[id], name)) continue; if (strcmp(tags.name[id], name)) continue;
return Tag(id); return Tag(id);
} }
return TAG_NONE;
}
struct Tag tagFor(const char *name) {
struct Tag tag = tagFind(name);
if (tag.id != TAG_NONE.id) return tag;
if (tags.len == TAGS_LEN) return TAG_STATUS; if (tags.len == TAGS_LEN) return TAG_STATUS;
size_t id = tags.len++; size_t id = tags.len++;
tags.name[id] = strdup(name); tags.name[id] = strdup(name);