From b5c82a91a5a136ccdd7c1dc16ba2b616a0099f7c Mon Sep 17 00:00:00 2001 From: mbays Date: Sat, 22 Jul 2023 00:00:00 +0000 Subject: [PATCH] munge links to handle following links from an old state --- main.c | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index e2c293a..822bf24 100644 --- a/main.c +++ b/main.c @@ -34,6 +34,7 @@ typedef struct Child { int out; int flag; bool reading; + int serial; bool nolink; bool plain; @@ -149,12 +150,14 @@ static void set_child_last_active(Child *child) static int stream_text(int in, int flag, int out, bool escape_pre, bool escape_all, + int munge_links_serial, bool *child_reading, int read_timeout, int pause_timeout) { char buf[256]; struct pollfd pfd[2] = { { in, POLLIN | POLLHUP, 0 }, {flag, POLLIN, 0 } }; int backticks = 0; char escape = 0; + int munge = 0; bool read_something = false; /* Note we set no total maximum time or output size limit; we leave it to @@ -184,7 +187,22 @@ static int stream_text(int in, int flag, int out, } else while (--backticks >= 0) write(out, "`", 1); } - if (escape_all && escape > 0) { + if (munge_links_serial > 0) { + // Replace "=>[whitespace]?foo" at start of a line with + // "=>[whitespace?!l+[serial]?foo" + if ((munge == 0 && *b == '=') || + (munge == 1 && *b == '>') || + (munge == 2 && (*b == ' ' || *b == '\t'))) { + munge++; + } else if (munge == 3 && (*b == '?')) { + dprintf(out, "?!l+%d", munge_links_serial); + munge = -1; + } else if (*b == '\n') { + munge = 0; + } else { + munge = -1; + } + } else if (escape_all && escape > 0) { if (escape == '\n') { if (*b == '#' || *b == '>') { write(out, " ", 1); @@ -203,6 +221,7 @@ static int stream_text(int in, int flag, int out, escape = 0; } + write(out, b, 1); ++b; } @@ -449,6 +468,18 @@ static void do_command(const State* state, Child *child, const char* q, int sock put("Gemtext mode enabled.\n"); put("=> ?!? Input command\n"); return; + } else if (0 == strncmp(q, "l+", strlen("l+"))) { + q += 2; + if (child->serial == atoi(q)) { + while (*q && *q != '?') { + q++; + } + } else { + put("20 text/gemini\r\n"); + put("That link is stale.\n"); + put("=> ?!? Input command\n"); + return; + } } else if (*q != '!') { put("40 Unknown gemrepl meta-command (use '!!' for a literal '!')\r\n"); return; @@ -458,6 +489,8 @@ static void do_command(const State* state, Child *child, const char* q, int sock return; } + child->serial++; + if (state->format != raw) { if (child->plain) put("20 text/plain\r\n"); else put("20 text/gemini\r\n"); @@ -486,6 +519,7 @@ static void do_command(const State* state, Child *child, const char* q, int sock const int succ = stream_text(child->out, child->flag, socket, state->format == pre && !child->plain, state->format == unwrapped && !child->plain, + state->format == gemtext ? child->serial : 0, &child->reading, state->read_timeout, state->pause_timeout); -- 2.11.4.GIT