From 1232ce451de8372a54e477882ed9e7cfa129c4b9 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sun, 12 Aug 2018 23:55:12 -0400 Subject: [PATCH] Factor out input param and add tagFind So that /view can't just invent tags. --- chat.h | 1 + input.c | 43 +++++++++++++++++++++++++------------------ tag.c | 8 +++++++- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/chat.h b/chat.h index 9961955..a4ab195 100644 --- a/chat.h +++ b/chat.h @@ -45,6 +45,7 @@ enum { TAGS_LEN = 256 }; const struct Tag TAG_NONE; const struct Tag TAG_STATUS; const struct Tag TAG_VERBOSE; +struct Tag tagFind(const char *name); struct Tag tagFor(const char *name); enum { diff --git a/input.c b/input.c index c2599c1..9f7eddf 100644 --- a/input.c +++ b/input.c @@ -37,6 +37,13 @@ static void privmsg(struct Tag tag, bool action, const char *mesg) { 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); 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) { (void)tag; - char *nick = strsep(¶ms, " "); - if (nick) { - ircFmt("NICK %s\r\n", nick); - } else { - uiLog(TAG_STATUS, L"/nick requires a name"); - } + char *nick = param("/nick", ¶ms, "name"); + if (!nick) return; + ircFmt("NICK %s\r\n", nick); } static void inputJoin(struct Tag tag, char *params) { (void)tag; - char *chan = strsep(¶ms, " "); - if (chan) { - ircFmt("JOIN %s\r\n", chan); - } else { - uiLog(TAG_STATUS, L"/join requires a channel"); - } + char *chan = param("/join", ¶ms, "channel"); + if (!chan) return; + ircFmt("JOIN %s\r\n", chan); } static void inputWho(struct Tag tag, char *params) { - (void)params; // TODO + (void)params; ircFmt("WHO %s\r\n", tag.name); } static void inputTopic(struct Tag tag, char *params) { - if (params) { // TODO + if (params) { ircFmt("TOPIC %s :%s\r\n", tag.name, params); } else { 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) { - char *view = strsep(¶ms, " "); + (void)tag; + char *view = param("/view", ¶ms, "name or number"); if (!view) return; int num = strtol(view, &view, 0); - if (view[0]) { - uiViewTag(tagFor(view)); - } else { + if (!view[0]) { 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); + } } } diff --git a/tag.c b/tag.c index 397c191..4a89d31 100644 --- a/tag.c +++ b/tag.c @@ -37,11 +37,17 @@ static struct Tag Tag(size_t 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) { if (strcmp(tags.name[id], name)) continue; 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; size_t id = tags.len++; tags.name[id] = strdup(name);