summary refs log tree commit diff
path: root/litterbox.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-17 16:53:23 -0500
committerJune McEnroe <june@causal.agency>2019-12-17 16:54:03 -0500
commit0f017d65a1a1854a3369339c9f61c4f0e17f8a84 (patch)
tree4aa8f0d86b8e29b36a75e7bc64c4e190f4821e07 /litterbox.c
parentImplement basic IRC connection and message parsing (diff)
downloadlitterbox-0f017d65a1a1854a3369339c9f61c4f0e17f8a84.tar.gz
litterbox-0f017d65a1a1854a3369339c9f61c4f0e17f8a84.zip
Implement -j join
Diffstat (limited to 'litterbox.c')
-rw-r--r--litterbox.c26
1 files changed, 24 insertions, 2 deletions
diff --git a/litterbox.c b/litterbox.c
index 2b2736d..1eb663a 100644
--- a/litterbox.c
+++ b/litterbox.c
@@ -96,6 +96,19 @@ static struct Message parse(char *line) {
 	return msg;
 }
 
+static void require(const struct Message *msg, bool nick, size_t len) {
+	if (nick && !msg->nick) {
+		errx(EX_PROTOCOL, "%s missing origin nick", msg->cmd);
+	}
+	for (size_t i = 0; i < len; ++i) {
+		if (msg->params[i]) continue;
+		errx(EX_PROTOCOL, "%s missing parameter %zu", msg->cmd, 1 + i);
+	}
+}
+
+static char *self;
+static const char *join;
+
 typedef void Handler(struct Message *msg);
 
 static void handleCap(struct Message *msg) {
@@ -103,8 +116,15 @@ static void handleCap(struct Message *msg) {
 	format("CAP END\r\n");
 }
 
+static void handleReplyWelcome(struct Message *msg) {
+	require(msg, false, 1);
+	self = strdup(msg->params[0]);
+	if (!self) err(EX_OSERR, "strdup");
+	format("JOIN :%s\r\n", join);
+}
+
 static void handlePing(struct Message *msg) {
-	// TODO: Require params[0].
+	require(msg, false, 1);
 	format("PONG :%s\r\n", msg->params[0]);
 }
 
@@ -112,6 +132,7 @@ static const struct {
 	const char *cmd;
 	Handler *fn;
 } Handlers[] = {
+	{ "001", handleReplyWelcome },
 	{ "CAP", handleCap },
 	{ "PING", handlePing },
 };
@@ -139,12 +160,13 @@ int main(int argc, char *argv[]) {
 	const char *pass = NULL;
 
 	int opt;
-	while (0 < (opt = getopt(argc, argv, "!d:h:imn:p:u:vw:"))) {
+	while (0 < (opt = getopt(argc, argv, "!d:h:ij:mn:p:u:vw:"))) {
 		switch (opt) {
 			break; case '!': insecure = true;
 			break; case 'd': path = optarg;
 			break; case 'h': host = optarg;
 			break; case 'i': init = true;
+			break; case 'j': join = optarg;
 			break; case 'm': migrate = true;
 			break; case 'n': nick = optarg;
 			break; case 'p': port = optarg;