From 5a6929e20bcc522893b60dada0f2db28c0699567 Mon Sep 17 00:00:00 2001 From: nekobit Date: Mon, 6 Jun 2022 23:49:44 +0000 Subject: [PATCH] Regex fix mentions FossilOrigin-Name: a625188e6d0161d3377da2abcaebd23756626b7a1a404cec46d20ea467d6d820 --- docs/INSTALL.md | 10 +++----- src/status.c | 67 +++++++++++++++++++++++++++++++++++++++++-------- src/status.h | 1 + 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 6e2f162..7d79e24 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -8,13 +8,9 @@ For the following GNU/Linux distributions, you will need the following libraries - Debian: `libcurl4-gnutls-dev` - Arch: `curl` - Void: `libcurl libcurl-devel` -- libcjson - - Debian: `libcjson-dev` - - Arch: `cjson` - - Void: `cJSON cJSON-devel` -- libpcre - - Debian: `libpcre3-dev` - - Void: `libpcre2` +- libpcre2 + - Debian: `libpcre2-dev` + - Void: `pcre2 pcre2-devel` - libfcgi - Debian: `libfcgi-dev` - Void: `fcgi fcgi-devel` diff --git a/src/status.c b/src/status.c index 6fef7da..154ba35 100644 --- a/src/status.c +++ b/src/status.c @@ -394,20 +394,67 @@ char* reformat_status(struct session* ssn, size_t emos_len) { if (!content) return NULL; - char* gt_res = NULL; - char* emo_res; - if (ssn->config.stat_greentexts) - gt_res = greentextify(content); - else - gt_res = content; + char* res = make_mentions_local(content); + char* gt_res, *emo_res; if (emos) { - emo_res = emojify(gt_res, emos, emos_len); - if (gt_res != content) free(gt_res); - return emo_res; + emo_res = emojify(res, emos, emos_len); + if (emo_res != content && res != content) + free(res); + res = emo_res; } - return gt_res; + + if (ssn->config.stat_greentexts) + { + gt_res = greentextify(res); + if (gt_res != res && res != content) + free(res); + res = gt_res; + } + + return res; +} + +#define REGEX_MENTION "" + +char* make_mentions_local(char* content) +{ + char* url_format; + int error; + PCRE2_SIZE erroffset; + size_t res_len = 512; + char* res = malloc(res_len); + pcre2_code* re = pcre2_compile((PCRE2_SPTR)REGEX_MENTION, + PCRE2_ZERO_TERMINATED, 0, + &error, &erroffset, NULL); + if (re == NULL) + { + fprintf(stderr, "Couldn't parse regex at offset %ld: %d\n", erroffset, error); + pcre2_code_free(re); + return NULL; + } + + int len = easprintf(&url_format, + "", + config_host_url_insecure ? "" : "s", + getenv("HTTP_HOST")); + + int amt = pcre2_substitute(re, + (PCRE2_SPTR)content, + PCRE2_ZERO_TERMINATED, + 0, + PCRE2_SUBSTITUTE_EXTENDED | PCRE2_SUBSTITUTE_GLOBAL, + NULL, + NULL, + (PCRE2_SPTR)url_format, + len, + (PCRE2_UCHAR*)res, + &res_len); + + + free(url_format); + return amt ? res : content; } char* greentextify(char* content) diff --git a/src/status.h b/src/status.h index e2dce11..adc2485 100644 --- a/src/status.h +++ b/src/status.h @@ -92,6 +92,7 @@ char* reformat_status(struct session* ssn, struct mstdnt_emoji* emos, size_t emos_len); char* greentextify(char* content); +char* make_mentions_local(char* content); // Status frontends void status_view(struct session* ssn, mastodont_t* api, char** data);