summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-10-24 18:09:13 -0400
committerJune McEnroe <june@causal.agency>2019-10-24 18:09:13 -0400
commitbc30300f5087458a8de57e79430abb61174b8832 (patch)
treeee4a5b9b580373ebe2518df782d7398e1aee298a
parentUnset non-blocking on clients (diff)
downloadpounce-bc30300f5087458a8de57e79430abb61174b8832.tar.gz
pounce-bc30300f5087458a8de57e79430abb61174b8832.zip
Register readers by client usernames
Diffstat (limited to '')
-rw-r--r--bounce.h1
-rw-r--r--client.c8
-rw-r--r--ring.c25
3 files changed, 32 insertions, 2 deletions
diff --git a/bounce.h b/bounce.h
index 38f86c8..4c0fefb 100644
--- a/bounce.h
+++ b/bounce.h
@@ -81,3 +81,4 @@ void stateParse(char *line);
 void stateSync(struct Client *client);
 
 void ringWrite(const char *line);
+size_t ringReader(const char *name);
diff --git a/client.c b/client.c
index 5970bf2..eaa8555 100644
--- a/client.c
+++ b/client.c
@@ -38,6 +38,7 @@ enum Need {
 struct Client {
 	bool error;
 	struct tls *tls;
+	size_t reader;
 	enum Need need;
 	bool serverTime;
 	char buf[4096];
@@ -112,8 +113,11 @@ static void handleNick(struct Client *client, struct Message msg) {
 }
 
 static void handleUser(struct Client *client, struct Message msg) {
-	(void)msg;
-	// TODO: Identify client by username.
+	if (!msg.params[0]) {
+		client->error = true;
+		return;
+	}
+	client->reader = ringReader(msg.params[0]);
 	client->need &= ~NeedUser;
 	if (!client->need) stateSync(client);
 	if (client->need == NeedPass) passRequired(client);
diff --git a/ring.c b/ring.c
index 40fb7df..ff83cea 100644
--- a/ring.c
+++ b/ring.c
@@ -38,3 +38,28 @@ void ringWrite(const char *line) {
 	ring.lines[i] = strdup(line);
 	if (!ring.lines[i]) err(EX_OSERR, "strdup");
 }
+
+static struct {
+	char **names;
+	size_t *reads;
+	size_t cap, len;
+} reader;
+
+size_t ringReader(const char *name) {
+	for (size_t i = 0; i < reader.len; ++i) {
+		if (!strcmp(reader.names[i], name)) return i;
+	}
+
+	if (reader.len == reader.cap) {
+		reader.cap = (reader.cap ? reader.cap * 2 : 8);
+		reader.names = realloc(reader.names, sizeof(*reader.names) * reader.cap);
+		if (!reader.names) err(EX_OSERR, "realloc");
+		reader.reads = realloc(reader.reads, sizeof(*reader.reads) * reader.cap);
+		if (!reader.reads) err(EX_OSERR, "realloc");
+	}
+
+	reader.names[reader.len] = strdup(name);
+	if (!reader.names[reader.len]) err(EX_OSERR, "strdup");
+	reader.reads[reader.len] = 0;
+	return reader.len++;
+}