From 5669af5c2d53e0c58c31c93231c274882c4543ce Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 15 Apr 2020 11:02:00 +0200
Subject: [PATCH] Adding functions to alter continuation state of a client.

---
 inc/lektor/net.h |  4 ++++
 src/net/listen.c | 34 ++++++++++++++++++++++++++++++----
 2 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/inc/lektor/net.h b/inc/lektor/net.h
index f862eb6d..1ae6e87d 100644
--- a/inc/lektor/net.h
+++ b/inc/lektor/net.h
@@ -46,6 +46,10 @@ struct lkt_state {
 /* Send a message to the connected client. */
 void lkt_state_send(struct lkt_state *srv, size_t c, struct lkt_message *msg);
 
+/* Get and set continuation state for a client. */
+bool lkt_get_continuation(struct lkt_state *srv, size_t c);
+void lkt_set_continuation(struct lkt_state *srv, size_t c, int i);
+
 /* Get the mask to watch for events for a given client, to be used with the `idle` command.
  * Return a pointer to the client's mask */
 enum mpd_idle_flag *lkt_client_get_mask(struct lkt_state *srv, size_t c);
diff --git a/src/net/listen.c b/src/net/listen.c
index d2730be6..2c7b7396 100644
--- a/src/net/listen.c
+++ b/src/net/listen.c
@@ -54,13 +54,25 @@ struct lkt_client {
     bool request_close;
 
     bool authentificated;
+    int continuation;
 };
 
-static bool
+static inline bool
 lkt_close_client(struct lkt_state *srv, size_t c)
 {
-    srv->clients[c - 1].request_close = true;
-    return true;
+    return srv->clients[c - 1].request_close = true;
+}
+
+inline bool
+lkt_get_continuation(struct lkt_state *srv, size_t c)
+{
+    return srv->clients[c - 1].continuation;
+}
+
+inline void
+lkt_set_continuation(struct lkt_state *srv, size_t c, int i)
+{
+    srv->clients[c - 1].continuation = i;
 }
 
 void
@@ -77,6 +89,15 @@ lkt_state_send(struct lkt_state *srv, size_t c, struct lkt_message *msg)
     srv->fds[c].events |= POLLOUT;
 }
 
+static inline void
+send_continue(struct lkt_state *srv, size_t c, int i)
+{
+    struct lkt_message *cont = lkt_message_new();
+    cont->data_len = snprintf(cont->data, LKT_MESSAGE_MAX, "continue: %d\n", i);
+    cont->data[LKT_MESSAGE_MAX - 1] = '\0';
+    lkt_state_send(srv, c, cont);
+}
+
 static void
 send_ok(struct lkt_state *srv, size_t c)
 {
@@ -137,7 +158,8 @@ handle_simple_command(struct lkt_state *srv, size_t c, struct lkt_command cmd)
 
     switch (*lkt_client_get_mask(srv, c)) {
     case MPD_IDLE_NONE:
-        /* Commands that require authentification. */
+        /* Commands that require authentification.
+           TODO: Move authentification verification inside commands. */
         if (lkt_client_auth(srv, c, false)) {
             if (!strcmp(cmd.name, "__adduser")) {
                 err = !command_user_add(srv->db, cmd.args);
@@ -164,6 +186,10 @@ handle_simple_command(struct lkt_state *srv, size_t c, struct lkt_command cmd)
             err = !lkt_close_client(srv, c);
         else if (!strcmp(cmd.name, "ping"))
             err = 0;
+        else if (!strcmp(cmd.name, "continue")) {
+            srv->clients[c - 1].continuation = atoi(cmd.args[0]);
+            err = 0;
+        }
 
         else if (!strcmp(cmd.name, "next"))
             err = !command_next(srv->db, &srv->win, &srv->mpd_idle_events);
-- 
GitLab