about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-08-12 23:55:12 -0400
committerJune McEnroe <june@causal.agency>2018-08-12 23:55:12 -0400
commit1232ce451de8372a54e477882ed9e7cfa129c4b9 (patch)
treec8f6c6e2a07c02f920128d28f29087dc84795ba3
parentFix /open ranges by passing all URLs to open(1) (diff)
downloadcatgirl-1232ce451de8372a54e477882ed9e7cfa129c4b9.tar.gz
catgirl-1232ce451de8372a54e477882ed9e7cfa129c4b9.zip
Factor out input param and add tagFind
So that /view can't just invent tags.
Diffstat (limited to '')
-rw-r--r--chat.h1
-rw-r--r--input.c43
-rw-r--r--tag.c8
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(&params, " ");
-	if (nick) {
-		ircFmt("NICK %s\r\n", nick);
-	} else {
-		uiLog(TAG_STATUS, L"/nick requires a name");
-	}
+	char *nick = param("/nick", &params, "name");
+	if (!nick) return;
+	ircFmt("NICK %s\r\n", nick);
 }
 
 static void inputJoin(struct Tag tag, char *params) {
 	(void)tag;
-	char *chan = strsep(&params, " ");
-	if (chan) {
-		ircFmt("JOIN %s\r\n", chan);
-	} else {
-		uiLog(TAG_STATUS, L"/join requires a channel");
-	}
+	char *chan = param("/join", &params, "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(&params, " ");
+	(void)tag;
+	char *view = param("/view", &params, "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);